, _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.
}
}
+ /* 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"));
}
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);
}
}
}
- 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)) {}
}
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;
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;
bool
FFmpegExaminer::has_video () const
{
- return static_cast<bool>(_video_stream);
+ return !_video_streams.empty();
}
--- /dev/null
+/*
+ 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;
+};
+