wip: hacks 2252-multi-stream
authorCarl Hetherington <cth@carlh.net>
Thu, 27 Oct 2022 20:22:07 +0000 (22:22 +0200)
committerCarl Hetherington <cth@carlh.net>
Thu, 27 Oct 2022 20:22:07 +0000 (22:22 +0200)
src/lib/ffmpeg.cc
src/lib/ffmpeg.h
src/lib/ffmpeg_examiner.cc
src/lib/ffmpeg_examiner.h
src/lib/ffmpeg_video_stream.h [new file with mode: 0644]

index 6c8509b6cd71de2eef9e5a75e3c58b2e9f1cd87f..14525c235336acbc6b3ff26c69fc875cc2c1e43b 100644 (file)
@@ -250,11 +250,12 @@ FFmpeg::setup_decoders ()
 AVCodecContext *
 FFmpeg::video_codec_context () const
 {
-       if (!_video_stream) {
+       auto str = _ffmpeg_content->video_stream();
+       if (!str) {
                return nullptr;
        }
 
-       return _codec_context[_video_stream.get()];
+       return _codec_context[str->index(_format_context)];
 }
 
 
index 25d26e8136ad5a5d5b7cb28ee4012e1a87a8e228..e407d5fb7d32c57003a0ea9ac2dc378ea3eed369 100644 (file)
@@ -78,8 +78,6 @@ protected:
 
        /** AVFrame used for decoding video */
        AVFrame* _video_frame = nullptr;
-       /** Index of video stream within AVFormatContext */
-       boost::optional<int> _video_stream;
 
        AVFrame* audio_frame (std::shared_ptr<const FFmpegAudioStream> stream);
 
index fdcacb465602693fba0bf668cc82d7bd9b82ddd6..2abeec4937f8ce1d84b956551cc1abf9c6160f01 100644 (file)
@@ -66,12 +66,19 @@ FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Jo
        , _need_video_length (false)
        , _pulldown (false)
 {
-       /* Find audio and subtitle streams */
+       /* Find streams */
 
        for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
                auto s = _format_context->streams[i];
                auto codec = _codec_context[i] ? _codec_context[i]->codec : nullptr;
-               if (s->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && codec) {
+               if (s->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+                       _video_streams.push_back(
+                               make_shared<VideoStream>(
+                                       stream_name(s),
+                                       s->id,
+                                       av_q2d(av_guess_frame_rate(_format_context, _format_context->streams[i], 0))
+                                       ));
+               } else if (s->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && codec) {
 
                        /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
                           so bodge it here.  No idea why we should have to do this.
@@ -108,6 +115,30 @@ FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Jo
                }
        }
 
+       /* Choose the best video stream */
+       using ScoredStream = pair<shared_ptr<FFmpegVideoStream>, int>;
+       vector<ScoredStream> streams_with_scores;
+       for (auto stream: _video_streams) {
+               auto s = stream->stream(_format_context);
+               auto const frame_rate = av_q2d(s->avg_frame_rate);
+               int score = 0;
+               if (1 < frame_rate && frame_rate < 1000) {
+                       ++score;
+               }
+               if (s->avg_frame_rate.num > 0 && s->avg_frame_rate.den > 0) {
+                       ++score;
+               }
+               streams_with_scores.push_back({stream, score});
+       }
+
+       streams_with_scores.sort([](ScoredStream const& a, ScoredStream const& b) {
+               return a.second > b.second;
+       });
+
+       if (!streams_with_scores.empty()) {
+               _best_video_stream = streams_with_scores[0].first;
+       }
+
        if (job && _need_video_length) {
                job->sub (_("Finding length"));
        }
@@ -143,7 +174,7 @@ FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Jo
 
                auto context = _codec_context[packet->stream_index];
 
-               if (_video_stream && packet->stream_index == _video_stream.get()) {
+               if (_best_video_stream && packet->stream_index == _best_video_stream->index(_format_context)) {
                        video_packet (context, temporal_reference, packet);
                }
 
@@ -166,8 +197,8 @@ FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Jo
                }
        }
 
-       if (_video_stream) {
-               auto context = _codec_context[_video_stream.get()];
+       if (_best_video_stream) {
+               auto context = _codec_context[_best_video_stream->index(_format_context)];
                while (video_packet(context, temporal_reference, nullptr)) {}
        }
 
@@ -176,9 +207,9 @@ FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Jo
                audio_packet(context, i, nullptr);
        }
 
-       if (_video_stream) {
+       if (_best_video_stream) {
                /* This code taken from get_rotation() in ffmpeg:cmdutils.c */
-               auto stream = _format_context->streams[*_video_stream];
+               auto stream = _best_video_stream->stream(_format_context);
                auto rotate_tag = av_dict_get (stream->metadata, "rotate", 0, 0);
                uint8_t* displaymatrix = av_stream_get_side_data (stream, AV_PKT_DATA_DISPLAYMATRIX, 0);
                _rotation = 0;
@@ -215,7 +246,7 @@ FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Jo
 bool
 FFmpegExaminer::video_packet (AVCodecContext* context, string& temporal_reference, AVPacket* packet)
 {
-       DCPOMATIC_ASSERT (_video_stream);
+       DCPOMATIC_ASSERT(_best_video_stream);
 
        if (_first_video && !_need_video_length && temporal_reference.size() >= (PULLDOWN_CHECK_FRAMES * 2)) {
                return false;
@@ -467,7 +498,7 @@ FFmpegExaminer::yuv () const
 bool
 FFmpegExaminer::has_video () const
 {
-       return static_cast<bool>(_video_stream);
+       return !_video_streams.empty();
 }
 
 
index 10d060707588be939ba20925c4a2d41b6035bea1..d129ca34729723d31921c8da326340d47321284b 100644 (file)
@@ -93,6 +93,8 @@ private:
        std::string subtitle_stream_name (AVStream* s) const;
        boost::optional<dcpomatic::ContentTime> frame_time (AVFrame* frame, AVStream* stream) const;
 
+       std::vector<std::shared_ptr<FFmpegVideoStream>> _video_streams;
+       std::shared_ptr<FFmpegVideoStream> _best_video_stream;
        std::vector<std::shared_ptr<FFmpegSubtitleStream>> _subtitle_streams;
        std::vector<std::shared_ptr<FFmpegAudioStream>> _audio_streams;
        boost::optional<dcpomatic::ContentTime> _first_video;
diff --git a/src/lib/ffmpeg_video_stream.h b/src/lib/ffmpeg_video_stream.h
new file mode 100644 (file)
index 0000000..ea65388
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+    Copyright (C) 2022 Carl Hetherington <cth@carlh.net>
+
+    This file is part of DCP-o-matic.
+
+    DCP-o-matic is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    DCP-o-matic is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "ffmpeg_stream.h"
+
+
+class FFmpegVideoStream : public FFmpegStream
+{
+public:
+       FFmpegVideoStream(std::string name, int id, float frame_rate)
+               : FFmpegStream(name, id)
+               , _frame_rate(frame_rate)
+       {}
+
+private:
+       float _frame_rate;
+};
+