graphics-ffmpeg.c 6.0 KB

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