graphics-ffmpeg.c 11 KB

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