graphics-ffmpeg.c 5.5 KB

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