graphics-ffmpeg.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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,
  19. -1, 1, 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, "Failed to open video codec for file '%s': "
  37. "%s", info->file, av_err2str(ret));
  38. return false;
  39. }
  40. return true;
  41. }
  42. static void ffmpeg_image_free(struct ffmpeg_image *info)
  43. {
  44. avcodec_close(info->decoder_ctx);
  45. avformat_close_input(&info->fmt_ctx);
  46. }
  47. static bool ffmpeg_image_init(struct ffmpeg_image *info, const char *file)
  48. {
  49. int ret;
  50. if (!file || !*file)
  51. return false;
  52. memset(info, 0, sizeof(struct ffmpeg_image));
  53. info->file = file;
  54. info->stream_idx = -1;
  55. ret = avformat_open_input(&info->fmt_ctx, file, NULL, NULL);
  56. if (ret < 0) {
  57. blog(LOG_WARNING, "Failed to open file '%s': %s",
  58. info->file, av_err2str(ret));
  59. return false;
  60. }
  61. ret = avformat_find_stream_info(info->fmt_ctx, NULL);
  62. if (ret < 0) {
  63. blog(LOG_WARNING, "Could not find stream info for file '%s':"
  64. " %s", info->file, av_err2str(ret));
  65. goto fail;
  66. }
  67. if (!ffmpeg_image_open_decoder_context(info))
  68. goto fail;
  69. info->cx = info->decoder_ctx->width;
  70. info->cy = info->decoder_ctx->height;
  71. info->format = info->decoder_ctx->pix_fmt;
  72. return true;
  73. fail:
  74. ffmpeg_image_free(info);
  75. return false;
  76. }
  77. static bool ffmpeg_image_reformat_frame(struct ffmpeg_image *info,
  78. AVFrame *frame, uint8_t *out, int linesize)
  79. {
  80. struct SwsContext *sws_ctx = NULL;
  81. int ret = 0;
  82. if (info->format == AV_PIX_FMT_RGBA ||
  83. info->format == AV_PIX_FMT_BGRA ||
  84. info->format == AV_PIX_FMT_BGR0) {
  85. if (linesize != frame->linesize[0]) {
  86. int min_line = linesize < frame->linesize[0] ?
  87. linesize : frame->linesize[0];
  88. for (int y = 0; y < info->cy; y++)
  89. memcpy(out + y * linesize,
  90. frame->data[0] + y * frame->linesize[0],
  91. min_line);
  92. } else {
  93. memcpy(out, frame->data[0], linesize * info->cy);
  94. }
  95. } else {
  96. sws_ctx = sws_getContext(info->cx, info->cy, info->format,
  97. info->cx, info->cy, AV_PIX_FMT_BGRA,
  98. SWS_POINT, NULL, NULL, NULL);
  99. if (!sws_ctx) {
  100. blog(LOG_WARNING, "Failed to create scale context "
  101. "for '%s'", info->file);
  102. return false;
  103. }
  104. ret = sws_scale(sws_ctx, (const uint8_t *const*)frame->data,
  105. frame->linesize, 0, info->cy, &out, &linesize);
  106. sws_freeContext(sws_ctx);
  107. if (ret < 0) {
  108. blog(LOG_WARNING, "sws_scale failed for '%s': %s",
  109. info->file, av_err2str(ret));
  110. return false;
  111. }
  112. info->format = AV_PIX_FMT_BGRA;
  113. }
  114. return true;
  115. }
  116. static bool ffmpeg_image_decode(struct ffmpeg_image *info, uint8_t *out,
  117. int linesize)
  118. {
  119. AVPacket packet = {0};
  120. bool success = false;
  121. AVFrame *frame = av_frame_alloc();
  122. int got_frame = 0;
  123. int ret;
  124. if (!frame) {
  125. blog(LOG_WARNING, "Failed to create frame data for '%s'",
  126. info->file);
  127. return false;
  128. }
  129. ret = av_read_frame(info->fmt_ctx, &packet);
  130. if (ret < 0) {
  131. blog(LOG_WARNING, "Failed to read image frame from '%s': %s",
  132. info->file, av_err2str(ret));
  133. goto fail;
  134. }
  135. while (!got_frame) {
  136. ret = avcodec_decode_video2(info->decoder_ctx, frame,
  137. &got_frame, &packet);
  138. if (ret < 0) {
  139. blog(LOG_WARNING, "Failed to decode frame for '%s': %s",
  140. info->file, av_err2str(ret));
  141. goto fail;
  142. }
  143. }
  144. success = ffmpeg_image_reformat_frame(info, frame, out, linesize);
  145. fail:
  146. av_free_packet(&packet);
  147. av_frame_free(&frame);
  148. return success;
  149. }
  150. void gs_init_image_deps(void)
  151. {
  152. av_register_all();
  153. }
  154. void gs_free_image_deps(void)
  155. {
  156. }
  157. static inline enum gs_color_format convert_format(enum AVPixelFormat format)
  158. {
  159. switch ((int)format) {
  160. case AV_PIX_FMT_RGBA: return GS_RGBA;
  161. case AV_PIX_FMT_BGRA: return GS_BGRA;
  162. case AV_PIX_FMT_BGR0: return GS_BGRX;
  163. }
  164. return GS_BGRX;
  165. }
  166. gs_texture_t gs_texture_create_from_file(const char *file)
  167. {
  168. struct ffmpeg_image image;
  169. gs_texture_t tex = NULL;
  170. if (ffmpeg_image_init(&image, file)) {
  171. uint8_t *data = malloc(image.cx * image.cy * 4);
  172. if (ffmpeg_image_decode(&image, data, image.cx * 4)) {
  173. tex = gs_texture_create(image.cx, image.cy,
  174. convert_format(image.format),
  175. 1, (const uint8_t**)&data, 0);
  176. }
  177. ffmpeg_image_free(&image);
  178. free(data);
  179. }
  180. return tex;
  181. }