Tidy up and fix obtaining video length when it's not in the header.
authorCarl Hetherington <cth@carlh.net>
Sat, 19 Oct 2024 18:20:00 +0000 (20:20 +0200)
committerCarl Hetherington <cth@carlh.net>
Sat, 19 Oct 2024 18:20:00 +0000 (20:20 +0200)
I think this has been broken for a while as we would come out of the
loop even if we still wanted to find the video length.

src/lib/ffmpeg_examiner.cc
src/lib/ffmpeg_examiner.h

index 51ade8e89cb844336baa4dfac51ef56be3f4fbea..6d3b614734d13557bc5960c9ebfb79e451dd6ae1 100644 (file)
@@ -144,24 +144,25 @@ FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Jo
 
                auto context = _codec_context[packet->stream_index];
 
+               auto carry_on = false;
+
                if (_video_stream && packet->stream_index == _video_stream.get()) {
-                       video_packet (context, temporal_reference, packet);
+                       if (video_packet(context, temporal_reference, packet)) {
+                               carry_on = true;
+                       }
                }
 
-               bool got_all_audio = true;
-
                for (size_t i = 0; i < _audio_streams.size(); ++i) {
                        if (_audio_streams[i]->uses_index(_format_context, packet->stream_index)) {
-                               audio_packet (context, _audio_streams[i], packet);
-                       }
-                       if (!_audio_streams[i]->first_audio) {
-                               got_all_audio = false;
+                               if (audio_packet(context, _audio_streams[i], packet)) {
+                                       carry_on = true;
+                               }
                        }
                }
 
                av_packet_free (&packet);
 
-               if (_first_video && got_all_audio && temporal_reference.size() >= (PULLDOWN_CHECK_FRAMES * 2)) {
+               if (!carry_on) {
                        /* All done */
                        break;
                }
@@ -261,26 +262,27 @@ FFmpegExaminer::video_packet (AVCodecContext* context, string& temporal_referenc
 }
 
 
-void
+bool
 FFmpegExaminer::audio_packet (AVCodecContext* context, shared_ptr<FFmpegAudioStream> stream, AVPacket* packet)
 {
        if (stream->first_audio) {
-               return;
+               return false;
        }
 
        int r = avcodec_send_packet (context, packet);
        if (r < 0) {
                LOG_WARNING("avcodec_send_packet returned %1 for an audio packet", r);
-               return;
+               return false;
        }
 
        auto frame = audio_frame (stream);
 
        if (avcodec_receive_frame (context, frame) < 0) {
-               return;
+               return false;
        }
 
        stream->first_audio = frame_time (frame, stream->stream(_format_context));
+       return true;
 }
 
 
index 45313ec18cc22f501bf626c74f6bb5459d4a18e4..b2dcbb27d2ff5408af31501f968f106a0854cd5f 100644 (file)
@@ -89,7 +89,7 @@ public:
 
 private:
        bool video_packet (AVCodecContext* context, std::string& temporal_reference, AVPacket* packet);
-       void audio_packet (AVCodecContext* context, std::shared_ptr<FFmpegAudioStream>, AVPacket* packet);
+       bool audio_packet (AVCodecContext* context, std::shared_ptr<FFmpegAudioStream>, AVPacket* packet);
 
        std::string stream_name (AVStream* s) const;
        std::string subtitle_stream_name (AVStream* s) const;