graphics-ffmpeg.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #ifdef _MSC_VER
  99. #define obs_bswap16(v) _byteswap_ushort(v)
  100. #else
  101. #define obs_bswap16(v) __builtin_bswap16(v)
  102. #endif
  103. static void *ffmpeg_image_reformat_frame(struct ffmpeg_image *info,
  104. AVFrame *frame)
  105. {
  106. struct SwsContext *sws_ctx = NULL;
  107. void *data = NULL;
  108. int ret = 0;
  109. if (info->format == AV_PIX_FMT_RGBA ||
  110. info->format == AV_PIX_FMT_BGRA ||
  111. info->format == AV_PIX_FMT_BGR0) {
  112. const size_t linesize = (size_t)info->cx * 4;
  113. const size_t totalsize = info->cy * linesize;
  114. data = bmalloc(totalsize);
  115. const size_t src_linesize = frame->linesize[0];
  116. if (linesize != src_linesize) {
  117. const size_t min_line = linesize < src_linesize
  118. ? linesize
  119. : src_linesize;
  120. uint8_t *dst = data;
  121. const uint8_t *src = frame->data[0];
  122. for (int y = 0; y < info->cy; y++) {
  123. memcpy(dst, src, min_line);
  124. dst += linesize;
  125. src += src_linesize;
  126. }
  127. } else {
  128. memcpy(data, frame->data[0], totalsize);
  129. }
  130. } else if (info->format == AV_PIX_FMT_RGBA64BE) {
  131. const size_t linesize = (size_t)info->cx * 8;
  132. data = bmalloc(info->cy * linesize);
  133. const size_t src_linesize = frame->linesize[0];
  134. const size_t min_line = linesize < src_linesize ? linesize
  135. : src_linesize;
  136. const size_t pairs = min_line >> 1;
  137. const uint8_t *src = frame->data[0];
  138. uint16_t *dst = data;
  139. for (int y = 0; y < info->cy; y++) {
  140. for (size_t x = 0; x < pairs; ++x) {
  141. uint16_t value;
  142. memcpy(&value, src, sizeof(value));
  143. *dst = obs_bswap16(value);
  144. ++dst;
  145. src += sizeof(value);
  146. }
  147. src += src_linesize - min_line;
  148. }
  149. } else {
  150. static const enum AVPixelFormat format = AV_PIX_FMT_BGRA;
  151. sws_ctx = sws_getContext(info->cx, info->cy, info->format,
  152. info->cx, info->cy, format, SWS_POINT,
  153. NULL, NULL, NULL);
  154. if (!sws_ctx) {
  155. blog(LOG_WARNING,
  156. "Failed to create scale context "
  157. "for '%s'",
  158. info->file);
  159. goto fail;
  160. }
  161. uint8_t *pointers[4];
  162. int linesizes[4];
  163. ret = av_image_alloc(pointers, linesizes, info->cx, info->cy,
  164. format, 32);
  165. if (ret < 0) {
  166. blog(LOG_WARNING, "av_image_alloc failed for '%s': %s",
  167. info->file, av_err2str(ret));
  168. sws_freeContext(sws_ctx);
  169. goto fail;
  170. }
  171. ret = sws_scale(sws_ctx, (const uint8_t *const *)frame->data,
  172. frame->linesize, 0, info->cy, pointers,
  173. linesizes);
  174. sws_freeContext(sws_ctx);
  175. if (ret < 0) {
  176. blog(LOG_WARNING, "sws_scale failed for '%s': %s",
  177. info->file, av_err2str(ret));
  178. av_freep(pointers);
  179. goto fail;
  180. }
  181. const size_t linesize = (size_t)info->cx * 4;
  182. data = bmalloc(info->cy * linesize);
  183. const uint8_t *src = pointers[0];
  184. uint8_t *dst = data;
  185. for (size_t y = 0; y < (size_t)info->cy; y++) {
  186. memcpy(dst, src, linesize);
  187. dst += linesize;
  188. src += linesizes[0];
  189. }
  190. av_freep(pointers);
  191. info->format = format;
  192. }
  193. fail:
  194. return data;
  195. }
  196. static void *ffmpeg_image_decode(struct ffmpeg_image *info)
  197. {
  198. AVPacket packet = {0};
  199. void *data = NULL;
  200. AVFrame *frame = av_frame_alloc();
  201. int got_frame = 0;
  202. int ret;
  203. if (!frame) {
  204. blog(LOG_WARNING, "Failed to create frame data for '%s'",
  205. info->file);
  206. return NULL;
  207. }
  208. ret = av_read_frame(info->fmt_ctx, &packet);
  209. if (ret < 0) {
  210. blog(LOG_WARNING, "Failed to read image frame from '%s': %s",
  211. info->file, av_err2str(ret));
  212. goto fail;
  213. }
  214. while (!got_frame) {
  215. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  216. ret = avcodec_send_packet(info->decoder_ctx, &packet);
  217. if (ret == 0)
  218. ret = avcodec_receive_frame(info->decoder_ctx, frame);
  219. got_frame = (ret == 0);
  220. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  221. ret = 0;
  222. #else
  223. ret = avcodec_decode_video2(info->decoder_ctx, frame,
  224. &got_frame, &packet);
  225. #endif
  226. if (ret < 0) {
  227. blog(LOG_WARNING, "Failed to decode frame for '%s': %s",
  228. info->file, av_err2str(ret));
  229. goto fail;
  230. }
  231. }
  232. data = ffmpeg_image_reformat_frame(info, frame);
  233. fail:
  234. av_packet_unref(&packet);
  235. av_frame_free(&frame);
  236. return data;
  237. }
  238. void gs_init_image_deps(void)
  239. {
  240. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  241. av_register_all();
  242. #endif
  243. }
  244. void gs_free_image_deps(void) {}
  245. static inline enum gs_color_format convert_format(enum AVPixelFormat format)
  246. {
  247. switch ((int)format) {
  248. case AV_PIX_FMT_RGBA:
  249. return GS_RGBA;
  250. case AV_PIX_FMT_BGRA:
  251. return GS_BGRA;
  252. case AV_PIX_FMT_BGR0:
  253. return GS_BGRX;
  254. case AV_PIX_FMT_RGBA64BE:
  255. return GS_RGBA16;
  256. }
  257. return GS_BGRX;
  258. }
  259. uint8_t *gs_create_texture_file_data(const char *file,
  260. enum gs_color_format *format,
  261. uint32_t *cx_out, uint32_t *cy_out)
  262. {
  263. struct ffmpeg_image image;
  264. uint8_t *data = NULL;
  265. if (ffmpeg_image_init(&image, file)) {
  266. data = ffmpeg_image_decode(&image);
  267. if (data) {
  268. *format = convert_format(image.format);
  269. *cx_out = (uint32_t)image.cx;
  270. *cy_out = (uint32_t)image.cy;
  271. }
  272. ffmpeg_image_free(&image);
  273. }
  274. return data;
  275. }