]> git.sesse.net Git - nageru/blobdiff - image_input.cpp
Make the guess button work.
[nageru] / image_input.cpp
index 49c2f62d8aef7837417baab77002e3f261f2461d..b8e2b02942b4ea01993131f2ee73acf595479e05 100644 (file)
@@ -18,6 +18,7 @@ extern "C" {
 #include <mutex>
 #include <thread>
 
+#include "ffmpeg_raii.h"
 #include "flags.h"
 
 using namespace std;
@@ -99,45 +100,6 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image(const string &pathnam
        return all_images[pathname];
 }
 
-// Some helpers to make RAII versions of FFmpeg objects.
-// The cleanup functions don't interact all that well with unique_ptr,
-// so things get a bit messy and verbose, but overall it's worth it to ensure
-// we never leak things by accident in error paths.
-
-namespace {
-
-void avformat_close_input_unique(AVFormatContext *format_ctx)
-{
-       avformat_close_input(&format_ctx);
-}
-
-unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>
-avformat_open_input_unique(const char *pathname,
-                           AVInputFormat *fmt, AVDictionary **options)
-{
-       AVFormatContext *format_ctx = nullptr;
-       if (avformat_open_input(&format_ctx, pathname, fmt, options) != 0) {
-               format_ctx = nullptr;
-       }
-       return unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>(
-               format_ctx, avformat_close_input_unique);
-}
-
-void av_frame_free_unique(AVFrame *frame)
-{
-       av_frame_free(&frame);
-}
-
-unique_ptr<AVFrame, decltype(av_frame_free_unique)*>
-av_frame_alloc_unique()
-{
-       AVFrame *frame = av_frame_alloc();
-       return unique_ptr<AVFrame, decltype(av_frame_free_unique)*>(
-               frame, av_frame_free_unique);
-}
-
-}  // namespace
-
 shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pathname)
 {
        // Note: Call before open, not after; otherwise, there's a race.
@@ -163,7 +125,7 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
 
        int stream_index = -1;
        for (unsigned i = 0; i < format_ctx->nb_streams; ++i) {
-               if (format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+               if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
                        stream_index = i;
                        break;
                }
@@ -173,22 +135,28 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
                return nullptr;
        }
 
-       AVCodecContext *codec_ctx = format_ctx->streams[stream_index]->codec;
-       AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);
+       const AVCodecParameters *codecpar = format_ctx->streams[stream_index]->codecpar;
+       AVCodecContextWithDeleter codec_ctx = avcodec_alloc_context3_unique(nullptr);
+       if (avcodec_parameters_to_context(codec_ctx.get(), codecpar) < 0) {
+               fprintf(stderr, "%s: Cannot fill codec parameters\n", pathname.c_str());
+               return nullptr;
+       }
+       AVCodec *codec = avcodec_find_decoder(codecpar->codec_id);
        if (codec == nullptr) {
                fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str());
                return nullptr;
        }
-       if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
+       if (avcodec_open2(codec_ctx.get(), codec, nullptr) < 0) {
                fprintf(stderr, "%s: Cannot open decoder\n", pathname.c_str());
                return nullptr;
        }
        unique_ptr<AVCodecContext, decltype(avcodec_close)*> codec_ctx_cleanup(
-               codec_ctx, avcodec_close);
+               codec_ctx.get(), avcodec_close);
 
        // Read packets until we have a frame or there are none left.
        int frame_finished = 0;
-       auto frame = av_frame_alloc_unique();
+       AVFrameWithDeleter frame = av_frame_alloc_unique();
+       bool eof = false;
        do {
                AVPacket pkt;
                unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
@@ -196,29 +164,28 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
                av_init_packet(&pkt);
                pkt.data = nullptr;
                pkt.size = 0;
-               if (av_read_frame(format_ctx.get(), &pkt) < 0) {
-                       break;
-               }
-               if (pkt.stream_index != stream_index) {
-                       continue;
+               if (av_read_frame(format_ctx.get(), &pkt) == 0) {
+                       if (pkt.stream_index != stream_index) {
+                               continue;
+                       }
+                       if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) {
+                               fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str());
+                               return nullptr;
+                       }
+               } else {
+                       eof = true;  // Or error, but ignore that for the time being.
                }
 
-               if (avcodec_decode_video2(codec_ctx, frame.get(), &frame_finished, &pkt) < 0) {
-                       fprintf(stderr, "%s: Cannot decode frame\n", pathname.c_str());
+               int err = avcodec_receive_frame(codec_ctx.get(), frame.get());
+               if (err == 0) {
+                       frame_finished = true;
+                       break;
+               } else if (err != AVERROR(EAGAIN)) {
+                       fprintf(stderr, "%s: Cannot receive frame from codec.\n", pathname.c_str());
                        return nullptr;
                }
-       } while (!frame_finished);
+       } while (!eof);
 
-       // See if there's a cached frame for us.
-       if (!frame_finished) {
-               AVPacket pkt;
-               pkt.data = nullptr;
-               pkt.size = 0;
-               if (avcodec_decode_video2(codec_ctx, frame.get(), &frame_finished, &pkt) < 0) {
-                       fprintf(stderr, "%s: Cannot decode frame\n", pathname.c_str());
-                       return nullptr;
-               }
-       }
        if (!frame_finished) {
                fprintf(stderr, "%s: Decoder did not output frame.\n", pathname.c_str());
                return nullptr;