graphics-ffmpeg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 inline size_t get_dst_position(size_t src, const size_t w,
  123. const size_t h, const size_t x,
  124. const size_t y, int orient)
  125. {
  126. size_t res_x = 0;
  127. size_t res_y = 0;
  128. if (orient == 2) {
  129. /*
  130. * Orientation 2: Flip X
  131. *
  132. * 888888 888888
  133. * 88 -> 88
  134. * 8888 -> 8888
  135. * 88 -> 88
  136. * 88 88
  137. *
  138. * (0, 0) -> (w, 0)
  139. * (0, h) -> (w, h)
  140. * (w, 0) -> (0, 0)
  141. * (w, h) -> (0, h)
  142. *
  143. * (w - x, y)
  144. */
  145. res_x = w - 1 - x;
  146. res_y = y;
  147. } else if (orient == 3) {
  148. /*
  149. * Orientation 3: 180°
  150. *
  151. * 88 888888
  152. * 88 -> 88
  153. * 8888 -> 8888
  154. * 88 -> 88
  155. * 888888 88
  156. *
  157. * (0, 0) -> (w, h)
  158. * (0, h) -> (w, 0)
  159. * (w, 0) -> (0, h)
  160. * (w, h) -> (0, 0)
  161. *
  162. * (w - x, h - y)
  163. */
  164. res_x = w - 1 - x;
  165. res_y = h - 1 - y;
  166. } else if (orient == 4) {
  167. /*
  168. * Orientation 4: Flip Y
  169. *
  170. * 88 888888
  171. * 88 -> 88
  172. * 8888 -> 8888
  173. * 88 -> 88
  174. * 888888 88
  175. *
  176. * (0, 0) -> (0, h)
  177. * (0, h) -> (0, 0)
  178. * (w, 0) -> (w, h)
  179. * (w, h) -> (w, 0)
  180. *
  181. * (x, h - y)
  182. */
  183. res_x = x;
  184. res_y = h - 1 - y;
  185. } else if (orient == 5) {
  186. /*
  187. * Orientation 5: Flip Y + 90° CW
  188. *
  189. * 8888888888 888888
  190. * 88 88 -> 88
  191. * 88 -> 8888
  192. * -> 88
  193. * 88
  194. *
  195. * (0, 0) -> (0, 0)
  196. * (0, h) -> (w, 0)
  197. * (w, 0) -> (0, h)
  198. * (w, h) -> (w, h)
  199. *
  200. * (y, x)
  201. */
  202. res_x = y;
  203. res_y = x;
  204. } else if (orient == 6) {
  205. /*
  206. * Orientation 6: 90° CW
  207. *
  208. * 88 888888
  209. * 88 88 -> 88
  210. * 8888888888 -> 8888
  211. * -> 88
  212. * 88
  213. *
  214. * (0, 0) -> (w, 0)
  215. * (0, h) -> (0, 0)
  216. * (w, 0) -> (w, h)
  217. * (w, h) -> (0, h)
  218. *
  219. * (w - y, x)
  220. */
  221. res_x = w - 1 - y;
  222. res_y = x;
  223. } else if (orient == 7) {
  224. /*
  225. * Orientation 7: Flip Y + 90° CCW
  226. *
  227. * 88 888888
  228. * 88 88 -> 88
  229. * 8888888888 -> 8888
  230. * -> 88
  231. * 88
  232. *
  233. * (0, 0) -> (w, h)
  234. * (0, h) -> (0, h)
  235. * (w, 0) -> (w, 0)
  236. * (w, h) -> (0, 0)
  237. *
  238. * (w - y, h - x)
  239. */
  240. res_x = w - 1 - y;
  241. res_y = h - 1 - x;
  242. } else if (orient == 8) {
  243. /*
  244. * Orientation 8: 90° CCW
  245. *
  246. * 8888888888 888888
  247. * 88 88 -> 88
  248. * 88 -> 8888
  249. * -> 88
  250. * 88
  251. *
  252. * (0, 0) -> (0, h)
  253. * (0, h) -> (w, h)
  254. * (w, 0) -> (0, 0)
  255. * (w, h) -> (w, 0)
  256. *
  257. * (y, h - x)
  258. */
  259. res_x = y;
  260. res_y = h - 1 - x;
  261. }
  262. return (res_x + res_y * w) * 4;
  263. }
  264. #define TILE_SIZE 16
  265. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  266. static void *ffmpeg_image_orient(struct ffmpeg_image *info, void *in_data,
  267. int orient)
  268. {
  269. const size_t sx = (size_t)info->cx;
  270. const size_t sy = (size_t)info->cy;
  271. uint8_t *data = NULL;
  272. if (orient == 0 || orient == 1)
  273. return in_data;
  274. data = bmalloc(sx * 4 * sy);
  275. if (orient >= 5 && orient < 9) {
  276. info->cx = (int)sy;
  277. info->cy = (int)sx;
  278. }
  279. uint8_t *src = in_data;
  280. size_t off_dst;
  281. size_t off_src = 0;
  282. for (size_t y0 = 0; y0 < sy; y0 += TILE_SIZE) {
  283. for (size_t x0 = 0; x0 < sx; x0 += TILE_SIZE) {
  284. size_t lim_x = MIN((size_t)sx, x0 + TILE_SIZE);
  285. size_t lim_y = MIN((size_t)sy, y0 + TILE_SIZE);
  286. for (size_t y = y0; y < lim_y; y++) {
  287. for (size_t x = x0; x < lim_x; x++) {
  288. off_src = (x + y * sx) * 4;
  289. off_dst = get_dst_position(off_src,
  290. info->cx,
  291. info->cy, x,
  292. y, orient);
  293. memcpy(data + off_dst, src + off_src,
  294. 4);
  295. }
  296. }
  297. }
  298. }
  299. bfree(in_data);
  300. return data;
  301. }
  302. static void *ffmpeg_image_reformat_frame(struct ffmpeg_image *info,
  303. AVFrame *frame,
  304. enum gs_image_alpha_mode alpha_mode)
  305. {
  306. struct SwsContext *sws_ctx = NULL;
  307. void *data = NULL;
  308. int ret = 0;
  309. AVDictionary *dict = frame->metadata;
  310. AVDictionaryEntry *entry = NULL;
  311. int orient = 0;
  312. if (dict) {
  313. entry = av_dict_get(dict, "Orientation", NULL,
  314. AV_DICT_MATCH_CASE);
  315. if (entry && entry->value) {
  316. orient = atoi(entry->value);
  317. }
  318. }
  319. if (info->format == AV_PIX_FMT_BGR0) {
  320. data = ffmpeg_image_copy_data_straight(info, frame);
  321. } else if (info->format == AV_PIX_FMT_RGBA ||
  322. info->format == AV_PIX_FMT_BGRA) {
  323. if (alpha_mode == GS_IMAGE_ALPHA_STRAIGHT) {
  324. data = ffmpeg_image_copy_data_straight(info, frame);
  325. } else {
  326. const size_t linesize = (size_t)info->cx * 4;
  327. const size_t totalsize = info->cy * linesize;
  328. data = bmalloc(totalsize);
  329. const size_t src_linesize = frame->linesize[0];
  330. const size_t min_line = linesize < src_linesize
  331. ? linesize
  332. : src_linesize;
  333. uint8_t *dst = data;
  334. const uint8_t *src = frame->data[0];
  335. const size_t row_elements = min_line >> 2;
  336. if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB) {
  337. for (int y = 0; y < info->cy; y++) {
  338. gs_premultiply_xyza_srgb_loop_restrict(
  339. dst, src, row_elements);
  340. dst += linesize;
  341. src += src_linesize;
  342. }
  343. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY) {
  344. for (int y = 0; y < info->cy; y++) {
  345. gs_premultiply_xyza_loop_restrict(
  346. dst, src, row_elements);
  347. dst += linesize;
  348. src += src_linesize;
  349. }
  350. }
  351. }
  352. } else if (info->format == AV_PIX_FMT_RGBA64BE) {
  353. const size_t dst_linesize = (size_t)info->cx * 4;
  354. data = bmalloc(info->cy * dst_linesize);
  355. const size_t src_linesize = frame->linesize[0];
  356. const size_t src_min_line = (dst_linesize * 2) < src_linesize
  357. ? (dst_linesize * 2)
  358. : src_linesize;
  359. const size_t row_elements = src_min_line >> 3;
  360. uint8_t *dst = data;
  361. const uint8_t *src = frame->data[0];
  362. uint16_t value[4];
  363. float f[4];
  364. if (alpha_mode == GS_IMAGE_ALPHA_STRAIGHT) {
  365. for (int y = 0; y < info->cy; y++) {
  366. for (size_t x = 0; x < row_elements; ++x) {
  367. memcpy(value, src, sizeof(value));
  368. f[0] = (float)obs_bswap16(value[0]) /
  369. 65535.0f;
  370. f[1] = (float)obs_bswap16(value[1]) /
  371. 65535.0f;
  372. f[2] = (float)obs_bswap16(value[2]) /
  373. 65535.0f;
  374. f[3] = (float)obs_bswap16(value[3]) /
  375. 65535.0f;
  376. gs_float4_to_u8x4(dst, f);
  377. dst += sizeof(*dst) * 4;
  378. src += sizeof(value);
  379. }
  380. src += src_linesize - src_min_line;
  381. }
  382. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB) {
  383. for (int y = 0; y < info->cy; y++) {
  384. for (size_t x = 0; x < row_elements; ++x) {
  385. memcpy(value, src, sizeof(value));
  386. f[0] = (float)obs_bswap16(value[0]) /
  387. 65535.0f;
  388. f[1] = (float)obs_bswap16(value[1]) /
  389. 65535.0f;
  390. f[2] = (float)obs_bswap16(value[2]) /
  391. 65535.0f;
  392. f[3] = (float)obs_bswap16(value[3]) /
  393. 65535.0f;
  394. gs_float3_srgb_nonlinear_to_linear(f);
  395. gs_premultiply_float4(f);
  396. gs_float3_srgb_linear_to_nonlinear(f);
  397. gs_float4_to_u8x4(dst, f);
  398. dst += sizeof(*dst) * 4;
  399. src += sizeof(value);
  400. }
  401. src += src_linesize - src_min_line;
  402. }
  403. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY) {
  404. for (int y = 0; y < info->cy; y++) {
  405. for (size_t x = 0; x < row_elements; ++x) {
  406. memcpy(value, src, sizeof(value));
  407. f[0] = (float)obs_bswap16(value[0]) /
  408. 65535.0f;
  409. f[1] = (float)obs_bswap16(value[1]) /
  410. 65535.0f;
  411. f[2] = (float)obs_bswap16(value[2]) /
  412. 65535.0f;
  413. f[3] = (float)obs_bswap16(value[3]) /
  414. 65535.0f;
  415. gs_premultiply_float4(f);
  416. gs_float4_to_u8x4(dst, f);
  417. dst += sizeof(*dst) * 4;
  418. src += sizeof(value);
  419. }
  420. src += src_linesize - src_min_line;
  421. }
  422. }
  423. info->format = AV_PIX_FMT_RGBA;
  424. } else {
  425. static const enum AVPixelFormat format = AV_PIX_FMT_BGRA;
  426. sws_ctx = sws_getContext(info->cx, info->cy, info->format,
  427. info->cx, info->cy, format, SWS_POINT,
  428. NULL, NULL, NULL);
  429. if (!sws_ctx) {
  430. blog(LOG_WARNING,
  431. "Failed to create scale context "
  432. "for '%s'",
  433. info->file);
  434. goto fail;
  435. }
  436. uint8_t *pointers[4];
  437. int linesizes[4];
  438. ret = av_image_alloc(pointers, linesizes, info->cx, info->cy,
  439. format, 32);
  440. if (ret < 0) {
  441. blog(LOG_WARNING, "av_image_alloc failed for '%s': %s",
  442. info->file, av_err2str(ret));
  443. sws_freeContext(sws_ctx);
  444. goto fail;
  445. }
  446. ret = sws_scale(sws_ctx, (const uint8_t *const *)frame->data,
  447. frame->linesize, 0, info->cy, pointers,
  448. linesizes);
  449. sws_freeContext(sws_ctx);
  450. if (ret < 0) {
  451. blog(LOG_WARNING, "sws_scale failed for '%s': %s",
  452. info->file, av_err2str(ret));
  453. av_freep(pointers);
  454. goto fail;
  455. }
  456. const size_t linesize = (size_t)info->cx * 4;
  457. data = bmalloc(info->cy * linesize);
  458. const uint8_t *src = pointers[0];
  459. uint8_t *dst = data;
  460. for (size_t y = 0; y < (size_t)info->cy; y++) {
  461. memcpy(dst, src, linesize);
  462. dst += linesize;
  463. src += linesizes[0];
  464. }
  465. av_freep(pointers);
  466. if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB) {
  467. gs_premultiply_xyza_srgb_loop(data, (size_t)info->cx *
  468. info->cy);
  469. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY) {
  470. gs_premultiply_xyza_loop(data,
  471. (size_t)info->cx * info->cy);
  472. }
  473. info->format = format;
  474. }
  475. data = ffmpeg_image_orient(info, data, orient);
  476. fail:
  477. return data;
  478. }
  479. static void *ffmpeg_image_decode(struct ffmpeg_image *info,
  480. enum gs_image_alpha_mode alpha_mode)
  481. {
  482. AVPacket packet = {0};
  483. void *data = NULL;
  484. AVFrame *frame = av_frame_alloc();
  485. int got_frame = 0;
  486. int ret;
  487. if (!frame) {
  488. blog(LOG_WARNING, "Failed to create frame data for '%s'",
  489. info->file);
  490. return NULL;
  491. }
  492. ret = av_read_frame(info->fmt_ctx, &packet);
  493. if (ret < 0) {
  494. blog(LOG_WARNING, "Failed to read image frame from '%s': %s",
  495. info->file, av_err2str(ret));
  496. goto fail;
  497. }
  498. while (!got_frame) {
  499. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  500. ret = avcodec_send_packet(info->decoder_ctx, &packet);
  501. if (ret == 0)
  502. ret = avcodec_receive_frame(info->decoder_ctx, frame);
  503. got_frame = (ret == 0);
  504. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  505. ret = 0;
  506. #else
  507. ret = avcodec_decode_video2(info->decoder_ctx, frame,
  508. &got_frame, &packet);
  509. #endif
  510. if (ret < 0) {
  511. blog(LOG_WARNING, "Failed to decode frame for '%s': %s",
  512. info->file, av_err2str(ret));
  513. goto fail;
  514. }
  515. }
  516. data = ffmpeg_image_reformat_frame(info, frame, alpha_mode);
  517. fail:
  518. av_packet_unref(&packet);
  519. av_frame_free(&frame);
  520. return data;
  521. }
  522. void gs_init_image_deps(void)
  523. {
  524. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  525. av_register_all();
  526. #endif
  527. }
  528. void gs_free_image_deps(void) {}
  529. static inline enum gs_color_format convert_format(enum AVPixelFormat format)
  530. {
  531. switch ((int)format) {
  532. case AV_PIX_FMT_RGBA:
  533. return GS_RGBA;
  534. case AV_PIX_FMT_BGRA:
  535. return GS_BGRA;
  536. case AV_PIX_FMT_BGR0:
  537. return GS_BGRX;
  538. case AV_PIX_FMT_RGBA64BE:
  539. return GS_RGBA16;
  540. }
  541. return GS_BGRX;
  542. }
  543. uint8_t *gs_create_texture_file_data(const char *file,
  544. enum gs_color_format *format,
  545. uint32_t *cx_out, uint32_t *cy_out)
  546. {
  547. struct ffmpeg_image image;
  548. uint8_t *data = NULL;
  549. if (ffmpeg_image_init(&image, file)) {
  550. data = ffmpeg_image_decode(&image, GS_IMAGE_ALPHA_STRAIGHT);
  551. if (data) {
  552. *format = convert_format(image.format);
  553. *cx_out = (uint32_t)image.cx;
  554. *cy_out = (uint32_t)image.cy;
  555. }
  556. ffmpeg_image_free(&image);
  557. }
  558. return data;
  559. }
  560. uint8_t *gs_create_texture_file_data2(const char *file,
  561. enum gs_image_alpha_mode alpha_mode,
  562. enum gs_color_format *format,
  563. uint32_t *cx_out, uint32_t *cy_out)
  564. {
  565. struct ffmpeg_image image;
  566. uint8_t *data = NULL;
  567. if (ffmpeg_image_init(&image, file)) {
  568. data = ffmpeg_image_decode(&image, alpha_mode);
  569. if (data) {
  570. *format = convert_format(image.format);
  571. *cx_out = (uint32_t)image.cx;
  572. *cy_out = (uint32_t)image.cy;
  573. }
  574. ffmpeg_image_free(&image);
  575. }
  576. return data;
  577. }