graphics-ffmpeg.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "graphics.h"
  2. #include <libavcodec/avcodec.h>
  3. #include <libavformat/avformat.h>
  4. #include <libswscale/swscale.h>
  5. struct ffmpeg_image {
  6. const char *file;
  7. AVFormatContext *fmt_ctx;
  8. AVCodecContext *decoder_ctx;
  9. AVCodec *decoder;
  10. AVStream *stream;
  11. int stream_idx;
  12. int cx, cy;
  13. enum AVPixelFormat format;
  14. };
  15. static bool ffmpeg_image_open_decoder_context(struct ffmpeg_image *info)
  16. {
  17. int ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_VIDEO,
  18. -1, 1, NULL, 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. info->stream_idx = ret;
  25. info->stream = info->fmt_ctx->streams[ret];
  26. info->decoder_ctx = info->stream->codec;
  27. info->decoder = avcodec_find_decoder(info->decoder_ctx->codec_id);
  28. if (!info->decoder) {
  29. blog(LOG_WARNING, "Failed to find decoder for file '%s'",
  30. info->file);
  31. return false;
  32. }
  33. ret = avcodec_open2(info->decoder_ctx, info->decoder, NULL);
  34. if (ret < 0) {
  35. blog(LOG_WARNING, "Failed to open video codec for file '%s': "
  36. "%s", info->file, av_err2str(ret));
  37. return false;
  38. }
  39. return true;
  40. }
  41. static void ffmpeg_image_free(struct ffmpeg_image *info)
  42. {
  43. avcodec_close(info->decoder_ctx);
  44. avformat_close_input(&info->fmt_ctx);
  45. }
  46. static bool ffmpeg_image_init(struct ffmpeg_image *info, const char *file)
  47. {
  48. int ret;
  49. memset(info, 0, sizeof(struct ffmpeg_image));
  50. info->file = file;
  51. info->stream_idx = -1;
  52. ret = avformat_open_input(&info->fmt_ctx, file, NULL, NULL);
  53. if (ret < 0) {
  54. blog(LOG_WARNING, "Failed to open file '%s': %s",
  55. info->file, av_err2str(ret));
  56. return false;
  57. }
  58. ret = avformat_find_stream_info(info->fmt_ctx, NULL);
  59. if (ret < 0) {
  60. blog(LOG_WARNING, "Could not find stream info for file '%s':"
  61. " %s", info->file, av_err2str(ret));
  62. goto fail;
  63. }
  64. if (!ffmpeg_image_open_decoder_context(info))
  65. goto fail;
  66. info->cx = info->decoder_ctx->width;
  67. info->cy = info->decoder_ctx->height;
  68. info->format = info->decoder_ctx->pix_fmt;
  69. return true;
  70. fail:
  71. ffmpeg_image_free(info);
  72. return false;
  73. }
  74. static bool ffmpeg_image_reformat_frame(struct ffmpeg_image *info,
  75. AVFrame *frame, uint8_t *out, int linesize)
  76. {
  77. struct SwsContext *sws_ctx = NULL;
  78. int ret = 0;
  79. sws_ctx = sws_getContext(info->cx, info->cy, info->format,
  80. info->cx, info->cy, AV_PIX_FMT_BGRA,
  81. SWS_POINT, NULL, NULL, NULL);
  82. if (!sws_ctx) {
  83. blog(LOG_WARNING, "Failed to create scale context for '%s'",
  84. info->file);
  85. return false;
  86. }
  87. ret = sws_scale(sws_ctx, (const uint8_t *const*)frame->data,
  88. frame->linesize, 0, info->cy, &out, &linesize);
  89. sws_freeContext(sws_ctx);
  90. if (ret < 0) {
  91. blog(LOG_WARNING, "sws_scale failed for '%s': %s",
  92. info->file, av_err2str(ret));
  93. return false;
  94. }
  95. return true;
  96. }
  97. static bool ffmpeg_image_decode(struct ffmpeg_image *info, uint8_t *out,
  98. int linesize)
  99. {
  100. AVPacket packet = {0};
  101. bool success = false;
  102. AVFrame *frame = avcodec_alloc_frame();
  103. int got_frame = 0;
  104. int ret;
  105. if (!frame) {
  106. blog(LOG_WARNING, "Failed to create frame data for '%s'",
  107. info->file);
  108. return false;
  109. }
  110. ret = av_read_frame(info->fmt_ctx, &packet);
  111. if (ret < 0) {
  112. blog(LOG_WARNING, "Failed to read image frame from '%s': %s",
  113. info->file, av_err2str(ret));
  114. goto fail;
  115. }
  116. while (!got_frame) {
  117. ret = avcodec_decode_video2(info->decoder_ctx, frame,
  118. &got_frame, &packet);
  119. if (ret < 0) {
  120. blog(LOG_WARNING, "Failed to decode frame for '%s': %s",
  121. info->file, av_err2str(ret));
  122. goto fail;
  123. }
  124. }
  125. success = ffmpeg_image_reformat_frame(info, frame, out, linesize);
  126. fail:
  127. av_free_packet(&packet);
  128. avcodec_free_frame(&frame);
  129. return success;
  130. }
  131. void gs_init_image_deps(void)
  132. {
  133. av_register_all();
  134. }
  135. void gs_free_image_deps(void)
  136. {
  137. }
  138. texture_t gs_create_texture_from_file(const char *file)
  139. {
  140. struct ffmpeg_image image;
  141. texture_t tex = NULL;
  142. if (ffmpeg_image_init(&image, file)) {
  143. uint8_t *data = malloc(image.cx * image.cy * 4);
  144. if (ffmpeg_image_decode(&image, data, image.cx * 4))
  145. tex = gs_create_texture(image.cx, image.cy, GS_BGRA, 1,
  146. (const uint8_t**)&data, 0);
  147. ffmpeg_image_free(&image);
  148. free(data);
  149. }
  150. return tex;
  151. }