graphics-ffmpeg.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. struct ffmpeg_image {
  8. const char *file;
  9. AVFormatContext *fmt_ctx;
  10. AVCodecContext *decoder_ctx;
  11. int cx, cy;
  12. enum AVPixelFormat format;
  13. };
  14. static bool ffmpeg_image_open_decoder_context(struct ffmpeg_image *info)
  15. {
  16. AVFormatContext *const fmt_ctx = info->fmt_ctx;
  17. int ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, 1, NULL,
  18. 0);
  19. if (ret < 0) {
  20. blog(LOG_WARNING, "Couldn't find video stream in file '%s': %s",
  21. info->file, av_err2str(ret));
  22. return false;
  23. }
  24. AVStream *const stream = fmt_ctx->streams[ret];
  25. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  26. AVCodecParameters *const codecpar = stream->codecpar;
  27. AVCodec *const decoder = avcodec_find_decoder(codecpar->codec_id);
  28. #else
  29. AVCodecContext *const decoder_ctx = stream->codec;
  30. AVCodec *const decoder = avcodec_find_decoder(decoder_ctx->codec_id);
  31. #endif
  32. if (!decoder) {
  33. blog(LOG_WARNING, "Failed to find decoder for file '%s'",
  34. info->file);
  35. return false;
  36. }
  37. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  38. AVCodecContext *const decoder_ctx = avcodec_alloc_context3(decoder);
  39. avcodec_parameters_to_context(decoder_ctx, codecpar);
  40. #endif
  41. info->decoder_ctx = decoder_ctx;
  42. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  43. info->cx = codecpar->width;
  44. info->cy = codecpar->height;
  45. info->format = codecpar->format;
  46. #else
  47. info->cx = decoder_ctx->width;
  48. info->cy = decoder_ctx->height;
  49. info->format = decoder_ctx->pix_fmt;
  50. #endif
  51. ret = avcodec_open2(decoder_ctx, decoder, NULL);
  52. if (ret < 0) {
  53. blog(LOG_WARNING,
  54. "Failed to open video codec for file '%s': "
  55. "%s",
  56. info->file, av_err2str(ret));
  57. return false;
  58. }
  59. return true;
  60. }
  61. static void ffmpeg_image_free(struct ffmpeg_image *info)
  62. {
  63. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  64. avcodec_free_context(&info->decoder_ctx);
  65. #else
  66. avcodec_close(info->decoder_ctx);
  67. #endif
  68. avformat_close_input(&info->fmt_ctx);
  69. }
  70. static bool ffmpeg_image_init(struct ffmpeg_image *info, const char *file)
  71. {
  72. int ret;
  73. if (!file || !*file)
  74. return false;
  75. memset(info, 0, sizeof(struct ffmpeg_image));
  76. info->file = file;
  77. ret = avformat_open_input(&info->fmt_ctx, file, NULL, NULL);
  78. if (ret < 0) {
  79. blog(LOG_WARNING, "Failed to open file '%s': %s", info->file,
  80. av_err2str(ret));
  81. return false;
  82. }
  83. ret = avformat_find_stream_info(info->fmt_ctx, NULL);
  84. if (ret < 0) {
  85. blog(LOG_WARNING,
  86. "Could not find stream info for file '%s':"
  87. " %s",
  88. info->file, av_err2str(ret));
  89. goto fail;
  90. }
  91. if (!ffmpeg_image_open_decoder_context(info))
  92. goto fail;
  93. return true;
  94. fail:
  95. ffmpeg_image_free(info);
  96. return false;
  97. }
  98. static bool ffmpeg_image_reformat_frame(struct ffmpeg_image *info,
  99. AVFrame *frame, uint8_t *out,
  100. int linesize)
  101. {
  102. struct SwsContext *sws_ctx = NULL;
  103. int ret = 0;
  104. if (info->format == AV_PIX_FMT_RGBA ||
  105. info->format == AV_PIX_FMT_BGRA ||
  106. info->format == AV_PIX_FMT_BGR0) {
  107. if (linesize != frame->linesize[0]) {
  108. int min_line = linesize < frame->linesize[0]
  109. ? linesize
  110. : frame->linesize[0];
  111. for (int y = 0; y < info->cy; y++)
  112. memcpy(out + y * linesize,
  113. frame->data[0] + y * frame->linesize[0],
  114. min_line);
  115. } else {
  116. memcpy(out, frame->data[0], linesize * info->cy);
  117. }
  118. } else {
  119. static const enum AVPixelFormat format = AV_PIX_FMT_BGRA;
  120. sws_ctx = sws_getContext(info->cx, info->cy, info->format,
  121. info->cx, info->cy, format, SWS_POINT,
  122. NULL, NULL, NULL);
  123. if (!sws_ctx) {
  124. blog(LOG_WARNING,
  125. "Failed to create scale context "
  126. "for '%s'",
  127. info->file);
  128. return false;
  129. }
  130. uint8_t *pointers[4];
  131. int linesizes[4];
  132. ret = av_image_alloc(pointers, linesizes, info->cx, info->cy,
  133. format, 32);
  134. if (ret < 0) {
  135. blog(LOG_WARNING, "av_image_alloc failed for '%s': %s",
  136. info->file, av_err2str(ret));
  137. sws_freeContext(sws_ctx);
  138. return false;
  139. }
  140. ret = sws_scale(sws_ctx, (const uint8_t *const *)frame->data,
  141. frame->linesize, 0, info->cy, pointers,
  142. linesizes);
  143. sws_freeContext(sws_ctx);
  144. if (ret < 0) {
  145. blog(LOG_WARNING, "sws_scale failed for '%s': %s",
  146. info->file, av_err2str(ret));
  147. av_freep(pointers);
  148. return false;
  149. }
  150. for (size_t y = 0; y < (size_t)info->cy; y++)
  151. memcpy(out + y * linesize,
  152. pointers[0] + y * linesizes[0], linesize);
  153. av_freep(pointers);
  154. info->format = format;
  155. }
  156. return true;
  157. }
  158. static bool ffmpeg_image_decode(struct ffmpeg_image *info, uint8_t *out,
  159. int linesize)
  160. {
  161. AVPacket packet = {0};
  162. bool success = false;
  163. AVFrame *frame = av_frame_alloc();
  164. int got_frame = 0;
  165. int ret;
  166. if (!frame) {
  167. blog(LOG_WARNING, "Failed to create frame data for '%s'",
  168. info->file);
  169. return false;
  170. }
  171. ret = av_read_frame(info->fmt_ctx, &packet);
  172. if (ret < 0) {
  173. blog(LOG_WARNING, "Failed to read image frame from '%s': %s",
  174. info->file, av_err2str(ret));
  175. goto fail;
  176. }
  177. while (!got_frame) {
  178. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  179. ret = avcodec_send_packet(info->decoder_ctx, &packet);
  180. if (ret == 0)
  181. ret = avcodec_receive_frame(info->decoder_ctx, frame);
  182. got_frame = (ret == 0);
  183. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  184. ret = 0;
  185. #else
  186. ret = avcodec_decode_video2(info->decoder_ctx, frame,
  187. &got_frame, &packet);
  188. #endif
  189. if (ret < 0) {
  190. blog(LOG_WARNING, "Failed to decode frame for '%s': %s",
  191. info->file, av_err2str(ret));
  192. goto fail;
  193. }
  194. }
  195. success = ffmpeg_image_reformat_frame(info, frame, out, linesize);
  196. fail:
  197. av_packet_unref(&packet);
  198. av_frame_free(&frame);
  199. return success;
  200. }
  201. void gs_init_image_deps(void)
  202. {
  203. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  204. av_register_all();
  205. #endif
  206. }
  207. void gs_free_image_deps(void) {}
  208. static inline enum gs_color_format convert_format(enum AVPixelFormat format)
  209. {
  210. switch ((int)format) {
  211. case AV_PIX_FMT_RGBA:
  212. return GS_RGBA;
  213. case AV_PIX_FMT_BGRA:
  214. return GS_BGRA;
  215. case AV_PIX_FMT_BGR0:
  216. return GS_BGRX;
  217. }
  218. return GS_BGRX;
  219. }
  220. uint8_t *gs_create_texture_file_data(const char *file,
  221. enum gs_color_format *format,
  222. uint32_t *cx_out, uint32_t *cy_out)
  223. {
  224. struct ffmpeg_image image;
  225. uint8_t *data = NULL;
  226. if (ffmpeg_image_init(&image, file)) {
  227. data = bmalloc(image.cx * image.cy * 4);
  228. if (ffmpeg_image_decode(&image, data, image.cx * 4)) {
  229. *format = convert_format(image.format);
  230. *cx_out = (uint32_t)image.cx;
  231. *cy_out = (uint32_t)image.cy;
  232. } else {
  233. bfree(data);
  234. data = NULL;
  235. }
  236. ffmpeg_image_free(&image);
  237. }
  238. return data;
  239. }