graphics-ffmpeg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include "graphics.h"
  2. #include <libavcodec/avcodec.h>
  3. #include <libavformat/avformat.h>
  4. #include <libavutil/imgutils.h>
  5. #include <libswscale/swscale.h>
  6. #include "../obs-ffmpeg-compat.h"
  7. #include "srgb.h"
  8. struct ffmpeg_image {
  9. const char *file;
  10. AVFormatContext *fmt_ctx;
  11. AVCodecContext *decoder_ctx;
  12. int cx, cy;
  13. enum AVPixelFormat format;
  14. };
  15. static bool ffmpeg_image_open_decoder_context(struct ffmpeg_image *info)
  16. {
  17. AVFormatContext *const fmt_ctx = info->fmt_ctx;
  18. int ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, 1, NULL,
  19. 0);
  20. if (ret < 0) {
  21. blog(LOG_WARNING, "Couldn't find video stream in file '%s': %s",
  22. info->file, av_err2str(ret));
  23. return false;
  24. }
  25. AVStream *const stream = fmt_ctx->streams[ret];
  26. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  27. AVCodecParameters *const codecpar = stream->codecpar;
  28. AVCodec *const decoder = avcodec_find_decoder(codecpar->codec_id);
  29. #else
  30. AVCodecContext *const decoder_ctx = stream->codec;
  31. AVCodec *const decoder = avcodec_find_decoder(decoder_ctx->codec_id);
  32. #endif
  33. if (!decoder) {
  34. blog(LOG_WARNING, "Failed to find decoder for file '%s'",
  35. info->file);
  36. return false;
  37. }
  38. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  39. AVCodecContext *const decoder_ctx = avcodec_alloc_context3(decoder);
  40. avcodec_parameters_to_context(decoder_ctx, codecpar);
  41. #endif
  42. info->decoder_ctx = decoder_ctx;
  43. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  44. info->cx = codecpar->width;
  45. info->cy = codecpar->height;
  46. info->format = codecpar->format;
  47. #else
  48. info->cx = decoder_ctx->width;
  49. info->cy = decoder_ctx->height;
  50. info->format = decoder_ctx->pix_fmt;
  51. #endif
  52. ret = avcodec_open2(decoder_ctx, decoder, NULL);
  53. if (ret < 0) {
  54. blog(LOG_WARNING,
  55. "Failed to open video codec for file '%s': "
  56. "%s",
  57. info->file, av_err2str(ret));
  58. return false;
  59. }
  60. return true;
  61. }
  62. static void ffmpeg_image_free(struct ffmpeg_image *info)
  63. {
  64. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  65. avcodec_free_context(&info->decoder_ctx);
  66. #else
  67. avcodec_close(info->decoder_ctx);
  68. #endif
  69. avformat_close_input(&info->fmt_ctx);
  70. }
  71. static bool ffmpeg_image_init(struct ffmpeg_image *info, const char *file)
  72. {
  73. int ret;
  74. if (!file || !*file)
  75. return false;
  76. memset(info, 0, sizeof(struct ffmpeg_image));
  77. info->file = file;
  78. ret = avformat_open_input(&info->fmt_ctx, file, NULL, NULL);
  79. if (ret < 0) {
  80. blog(LOG_WARNING, "Failed to open file '%s': %s", info->file,
  81. av_err2str(ret));
  82. return false;
  83. }
  84. ret = avformat_find_stream_info(info->fmt_ctx, NULL);
  85. if (ret < 0) {
  86. blog(LOG_WARNING,
  87. "Could not find stream info for file '%s':"
  88. " %s",
  89. info->file, av_err2str(ret));
  90. goto fail;
  91. }
  92. if (!ffmpeg_image_open_decoder_context(info))
  93. goto fail;
  94. return true;
  95. fail:
  96. ffmpeg_image_free(info);
  97. return false;
  98. }
  99. #ifdef _MSC_VER
  100. #define obs_bswap16(v) _byteswap_ushort(v)
  101. #else
  102. #define obs_bswap16(v) __builtin_bswap16(v)
  103. #endif
  104. static void *ffmpeg_image_copy_data_straight(struct ffmpeg_image *info,
  105. AVFrame *frame)
  106. {
  107. const size_t linesize = (size_t)info->cx * 4;
  108. const size_t totalsize = info->cy * linesize;
  109. void *data = bmalloc(totalsize);
  110. const size_t src_linesize = frame->linesize[0];
  111. if (linesize != src_linesize) {
  112. const size_t min_line = linesize < src_linesize ? linesize
  113. : src_linesize;
  114. uint8_t *dst = data;
  115. const uint8_t *src = frame->data[0];
  116. for (int y = 0; y < info->cy; y++) {
  117. memcpy(dst, src, min_line);
  118. dst += linesize;
  119. src += src_linesize;
  120. }
  121. } else {
  122. memcpy(data, frame->data[0], totalsize);
  123. }
  124. return data;
  125. }
  126. static void *ffmpeg_image_reformat_frame(struct ffmpeg_image *info,
  127. AVFrame *frame,
  128. enum gs_image_alpha_mode alpha_mode)
  129. {
  130. struct SwsContext *sws_ctx = NULL;
  131. void *data = NULL;
  132. int ret = 0;
  133. if (info->format == AV_PIX_FMT_BGR0) {
  134. data = ffmpeg_image_copy_data_straight(info, frame);
  135. } else if (info->format == AV_PIX_FMT_RGBA ||
  136. info->format == AV_PIX_FMT_BGRA) {
  137. if (alpha_mode == GS_IMAGE_ALPHA_STRAIGHT) {
  138. data = ffmpeg_image_copy_data_straight(info, frame);
  139. } else {
  140. const size_t linesize = (size_t)info->cx * 4;
  141. const size_t totalsize = info->cy * linesize;
  142. data = bmalloc(totalsize);
  143. const size_t src_linesize = frame->linesize[0];
  144. const size_t min_line = linesize < src_linesize
  145. ? linesize
  146. : src_linesize;
  147. uint8_t *dst = data;
  148. const uint8_t *src = frame->data[0];
  149. const size_t row_elements = min_line >> 2;
  150. if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB) {
  151. for (int y = 0; y < info->cy; y++) {
  152. gs_premultiply_xyza_srgb_loop_restrict(
  153. dst, src, row_elements);
  154. dst += linesize;
  155. src += src_linesize;
  156. }
  157. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY) {
  158. for (int y = 0; y < info->cy; y++) {
  159. gs_premultiply_xyza_loop_restrict(
  160. dst, src, row_elements);
  161. dst += linesize;
  162. src += src_linesize;
  163. }
  164. }
  165. }
  166. } else if (info->format == AV_PIX_FMT_RGBA64BE) {
  167. const size_t dst_linesize = (size_t)info->cx * 4;
  168. data = bmalloc(info->cy * dst_linesize);
  169. const size_t src_linesize = frame->linesize[0];
  170. const size_t src_min_line = (dst_linesize * 2) < src_linesize
  171. ? (dst_linesize * 2)
  172. : src_linesize;
  173. const size_t row_elements = src_min_line >> 3;
  174. uint8_t *dst = data;
  175. const uint8_t *src = frame->data[0];
  176. uint16_t value[4];
  177. float f[4];
  178. if (alpha_mode == GS_IMAGE_ALPHA_STRAIGHT) {
  179. for (int y = 0; y < info->cy; y++) {
  180. for (size_t x = 0; x < row_elements; ++x) {
  181. memcpy(value, src, sizeof(value));
  182. f[0] = (float)obs_bswap16(value[0]) /
  183. 65535.0f;
  184. f[1] = (float)obs_bswap16(value[1]) /
  185. 65535.0f;
  186. f[2] = (float)obs_bswap16(value[2]) /
  187. 65535.0f;
  188. f[3] = (float)obs_bswap16(value[3]) /
  189. 65535.0f;
  190. gs_float3_srgb_linear_to_nonlinear(f);
  191. gs_float4_to_u8x4(dst, f);
  192. dst += sizeof(*dst) * 4;
  193. src += sizeof(value);
  194. }
  195. src += src_linesize - src_min_line;
  196. }
  197. } else {
  198. for (int y = 0; y < info->cy; y++) {
  199. for (size_t x = 0; x < row_elements; ++x) {
  200. memcpy(value, src, sizeof(value));
  201. f[0] = (float)obs_bswap16(value[0]) /
  202. 65535.0f;
  203. f[1] = (float)obs_bswap16(value[1]) /
  204. 65535.0f;
  205. f[2] = (float)obs_bswap16(value[2]) /
  206. 65535.0f;
  207. f[3] = (float)obs_bswap16(value[3]) /
  208. 65535.0f;
  209. gs_premultiply_float4(f);
  210. gs_float3_srgb_linear_to_nonlinear(f);
  211. gs_float4_to_u8x4(dst, f);
  212. dst += sizeof(*dst) * 4;
  213. src += sizeof(value);
  214. }
  215. src += src_linesize - src_min_line;
  216. }
  217. }
  218. info->format = AV_PIX_FMT_RGBA;
  219. } else {
  220. static const enum AVPixelFormat format = AV_PIX_FMT_BGRA;
  221. sws_ctx = sws_getContext(info->cx, info->cy, info->format,
  222. info->cx, info->cy, format, SWS_POINT,
  223. NULL, NULL, NULL);
  224. if (!sws_ctx) {
  225. blog(LOG_WARNING,
  226. "Failed to create scale context "
  227. "for '%s'",
  228. info->file);
  229. goto fail;
  230. }
  231. uint8_t *pointers[4];
  232. int linesizes[4];
  233. ret = av_image_alloc(pointers, linesizes, info->cx, info->cy,
  234. format, 32);
  235. if (ret < 0) {
  236. blog(LOG_WARNING, "av_image_alloc failed for '%s': %s",
  237. info->file, av_err2str(ret));
  238. sws_freeContext(sws_ctx);
  239. goto fail;
  240. }
  241. ret = sws_scale(sws_ctx, (const uint8_t *const *)frame->data,
  242. frame->linesize, 0, info->cy, pointers,
  243. linesizes);
  244. sws_freeContext(sws_ctx);
  245. if (ret < 0) {
  246. blog(LOG_WARNING, "sws_scale failed for '%s': %s",
  247. info->file, av_err2str(ret));
  248. av_freep(pointers);
  249. goto fail;
  250. }
  251. const size_t linesize = (size_t)info->cx * 4;
  252. data = bmalloc(info->cy * linesize);
  253. const uint8_t *src = pointers[0];
  254. uint8_t *dst = data;
  255. for (size_t y = 0; y < (size_t)info->cy; y++) {
  256. memcpy(dst, src, linesize);
  257. dst += linesize;
  258. src += linesizes[0];
  259. }
  260. av_freep(pointers);
  261. if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB) {
  262. gs_premultiply_xyza_srgb_loop(data, (size_t)info->cx *
  263. info->cy);
  264. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY) {
  265. gs_premultiply_xyza_loop(data,
  266. (size_t)info->cx * info->cy);
  267. }
  268. info->format = format;
  269. }
  270. fail:
  271. return data;
  272. }
  273. static void *ffmpeg_image_decode(struct ffmpeg_image *info,
  274. enum gs_image_alpha_mode alpha_mode)
  275. {
  276. AVPacket packet = {0};
  277. void *data = NULL;
  278. AVFrame *frame = av_frame_alloc();
  279. int got_frame = 0;
  280. int ret;
  281. if (!frame) {
  282. blog(LOG_WARNING, "Failed to create frame data for '%s'",
  283. info->file);
  284. return NULL;
  285. }
  286. ret = av_read_frame(info->fmt_ctx, &packet);
  287. if (ret < 0) {
  288. blog(LOG_WARNING, "Failed to read image frame from '%s': %s",
  289. info->file, av_err2str(ret));
  290. goto fail;
  291. }
  292. while (!got_frame) {
  293. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  294. ret = avcodec_send_packet(info->decoder_ctx, &packet);
  295. if (ret == 0)
  296. ret = avcodec_receive_frame(info->decoder_ctx, frame);
  297. got_frame = (ret == 0);
  298. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  299. ret = 0;
  300. #else
  301. ret = avcodec_decode_video2(info->decoder_ctx, frame,
  302. &got_frame, &packet);
  303. #endif
  304. if (ret < 0) {
  305. blog(LOG_WARNING, "Failed to decode frame for '%s': %s",
  306. info->file, av_err2str(ret));
  307. goto fail;
  308. }
  309. }
  310. data = ffmpeg_image_reformat_frame(info, frame, alpha_mode);
  311. fail:
  312. av_packet_unref(&packet);
  313. av_frame_free(&frame);
  314. return data;
  315. }
  316. void gs_init_image_deps(void)
  317. {
  318. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  319. av_register_all();
  320. #endif
  321. }
  322. void gs_free_image_deps(void) {}
  323. static inline enum gs_color_format convert_format(enum AVPixelFormat format)
  324. {
  325. switch ((int)format) {
  326. case AV_PIX_FMT_RGBA:
  327. return GS_RGBA;
  328. case AV_PIX_FMT_BGRA:
  329. return GS_BGRA;
  330. case AV_PIX_FMT_BGR0:
  331. return GS_BGRX;
  332. case AV_PIX_FMT_RGBA64BE:
  333. return GS_RGBA16;
  334. }
  335. return GS_BGRX;
  336. }
  337. uint8_t *gs_create_texture_file_data(const char *file,
  338. enum gs_color_format *format,
  339. uint32_t *cx_out, uint32_t *cy_out)
  340. {
  341. struct ffmpeg_image image;
  342. uint8_t *data = NULL;
  343. if (ffmpeg_image_init(&image, file)) {
  344. data = ffmpeg_image_decode(&image, GS_IMAGE_ALPHA_STRAIGHT);
  345. if (data) {
  346. *format = convert_format(image.format);
  347. *cx_out = (uint32_t)image.cx;
  348. *cy_out = (uint32_t)image.cy;
  349. }
  350. ffmpeg_image_free(&image);
  351. }
  352. return data;
  353. }
  354. uint8_t *gs_create_texture_file_data2(const char *file,
  355. enum gs_image_alpha_mode alpha_mode,
  356. enum gs_color_format *format,
  357. uint32_t *cx_out, uint32_t *cy_out)
  358. {
  359. struct ffmpeg_image image;
  360. uint8_t *data = NULL;
  361. if (ffmpeg_image_init(&image, file)) {
  362. data = ffmpeg_image_decode(&image, alpha_mode);
  363. if (data) {
  364. *format = convert_format(image.format);
  365. *cx_out = (uint32_t)image.cx;
  366. *cy_out = (uint32_t)image.cy;
  367. }
  368. ffmpeg_image_free(&image);
  369. }
  370. return data;
  371. }