diff --git a/media/filters/ffmpeg_demuxer.h b/media/filters/ffmpeg_demuxer.h index c147309..48a8f6a 100644 --- a/media/filters/ffmpeg_demuxer.h +++ b/media/filters/ffmpeg_demuxer.h @@ -151,6 +151,8 @@ base::TimeDelta start_time() const { return start_time_; } void set_start_time(base::TimeDelta time) { start_time_ = time; } + int64_t first_dts() const { return first_dts_; } + private: friend class FFmpegDemuxerTest; @@ -208,6 +210,7 @@ bool fixup_chained_ogg_; int num_discarded_packet_warnings_; + int64_t first_dts_; int64_t last_packet_pos_; int64_t last_packet_dts_; }; --- a/media/filters/ffmpeg_demuxer.cc 2021-06-28 16:40:34.456861225 +0200 +++ b/media/filters/ffmpeg_demuxer.cc 2021-06-28 16:49:27.746115753 +0200 @@ -58,7 +58,7 @@ namespace { -constexpr int64_t kInvalidPTSMarker = static_cast(0x8000000000000000); +constexpr int64_t kRelativeTsBase = static_cast(0x7ffeffffffffffff); void SetAVStreamDiscard(AVStream* stream, AVDiscard discard) { DCHECK(stream); @@ -96,7 +96,7 @@ sample_rate); } -static base::TimeDelta ExtractStartTime(AVStream* stream) { +static base::TimeDelta ExtractStartTime(AVStream* stream, int64_t first_dts) { // The default start time is zero. base::TimeDelta start_time; @@ -106,12 +106,12 @@ // Next try to use the first DTS value, for codecs where we know PTS == DTS // (excludes all H26x codecs). The start time must be returned in PTS. - if (av_stream_get_first_dts(stream) != kNoFFmpegTimestamp && + if (first_dts != AV_NOPTS_VALUE && stream->codecpar->codec_id != AV_CODEC_ID_HEVC && stream->codecpar->codec_id != AV_CODEC_ID_H264 && stream->codecpar->codec_id != AV_CODEC_ID_MPEG4) { const base::TimeDelta first_pts = - ConvertFromTimeBase(stream->time_base, av_stream_get_first_dts(stream)); + ConvertFromTimeBase(stream->time_base, first_dts); if (first_pts < start_time) start_time = first_pts; } @@ -280,6 +280,7 @@ fixup_negative_timestamps_(false), fixup_chained_ogg_(false), num_discarded_packet_warnings_(0), + first_dts_(AV_NOPTS_VALUE), last_packet_pos_(AV_NOPTS_VALUE), last_packet_dts_(AV_NOPTS_VALUE) { DCHECK(demuxer_); @@ -346,6 +347,11 @@ int64_t packet_dts = packet->dts == AV_NOPTS_VALUE ? packet->pts : packet->dts; + if (first_dts_ == AV_NOPTS_VALUE && packet->dts != AV_NOPTS_VALUE && + last_packet_dts_ != AV_NOPTS_VALUE) { + first_dts_ = packet->dts - (last_packet_dts_ + kRelativeTsBase); + } + // Chained ogg files have non-monotonically increasing position and time stamp // values, which prevents us from using them to determine if a packet should // be dropped. Since chained ogg is only allowed on single track audio only @@ -1439,7 +1445,7 @@ max_duration = std::max(max_duration, streams_[i]->duration()); - base::TimeDelta start_time = ExtractStartTime(stream); + base::TimeDelta start_time = ExtractStartTime(stream, streams_[i]->first_dts()); // Note: This value is used for seeking, so we must take the true value and // not the one possibly clamped to zero below. @@ -1596,7 +1602,7 @@ for (const auto& stream : streams_) { if (!stream || stream->IsEnabled() != enabled) continue; - if (av_stream_get_first_dts(stream->av_stream()) == kInvalidPTSMarker) + if (stream->first_dts() == AV_NOPTS_VALUE) continue; if (!lowest_start_time_stream || stream->start_time() < lowest_start_time_stream->start_time()) { @@ -1617,7 +1623,7 @@ if (stream->type() != DemuxerStream::VIDEO) continue; - if (av_stream_get_first_dts(stream->av_stream()) == kInvalidPTSMarker) + if (stream->first_dts() == AV_NOPTS_VALUE) continue; if (!stream->IsEnabled()) diff --git a/media/cdm/library_cdm/clear_key_cdm/ffmpeg_cdm_audio_decoder.cc b/media/cdm/library_cdm/clear_key_cdm/ffmpeg_cdm_audio_decoder.cc index e4fc3f46..9b1ad9f 100644 --- a/media/cdm/library_cdm/clear_key_cdm/ffmpeg_cdm_audio_decoder.cc +++ b/media/cdm/library_cdm/clear_key_cdm/ffmpeg_cdm_audio_decoder.cc @@ -74,7 +74,7 @@ codec_context->sample_fmt = AV_SAMPLE_FMT_NONE; } - codec_context->ch_layout.nb_channels = config.channel_count; + codec_context->channels = config.channel_count; codec_context->sample_rate = config.samples_per_second; if (config.extra_data) { @@ -124,8 +124,8 @@ case cdm::kAudioFormatPlanarS16: case cdm::kAudioFormatPlanarF32: { const int decoded_size_per_channel = - decoded_audio_size / av_frame.ch_layout.nb_channels; - for (int i = 0; i < av_frame.ch_layout.nb_channels; ++i) { + decoded_audio_size / av_frame.channels; + for (int i = 0; i < av_frame.channels; ++i) { memcpy(output_buffer, av_frame.extended_data[i], decoded_size_per_channel); output_buffer += decoded_size_per_channel; @@ -185,14 +185,13 @@ // Success! decoding_loop_ = std::make_unique(codec_context_.get()); samples_per_second_ = config.samples_per_second; - bytes_per_frame_ = - codec_context_->ch_layout.nb_channels * config.bits_per_channel / 8; + bytes_per_frame_ = codec_context_->channels * config.bits_per_channel / 8; output_timestamp_helper_ = std::make_unique(config.samples_per_second); is_initialized_ = true; // Store initial values to guard against midstream configuration changes. - channels_ = codec_context_->ch_layout.nb_channels; + channels_ = codec_context_->channels; av_sample_format_ = codec_context_->sample_fmt; return true; @@ -291,9 +291,8 @@ for (auto& frame : audio_frames) { int decoded_audio_size = 0; if (frame->sample_rate != samples_per_second_ || - frame->ch_layout.nb_channels != channels_ || - frame->format != av_sample_format_) { - DLOG(ERROR) << "Unsupported midstream configuration change!" + frame->channels != channels_ || frame->format != av_sample_format_) { + DLOG(ERROR) << "Unsupported midstream configuration change!" << " Sample Rate: " << frame->sample_rate << " vs " << samples_per_second_ << ", Channels: " << frame->ch_layout.nb_channels << " vs " @@ -303,7 +302,7 @@ } decoded_audio_size = av_samples_get_buffer_size( - nullptr, codec_context_->ch_layout.nb_channels, frame->nb_samples, + nullptr, codec_context_->channels, frame->nb_samples, codec_context_->sample_fmt, 1); if (!decoded_audio_size) continue; @@ -323,7 +322,7 @@ std::vector>* audio_frames, AVFrame* frame) { *total_size += av_samples_get_buffer_size( - nullptr, codec_context_->ch_layout.nb_channels, frame->nb_samples, + nullptr, codec_context_->channels, frame->nb_samples, codec_context_->sample_fmt, 1); audio_frames->emplace_back(av_frame_clone(frame)); return true; diff --git a/media/ffmpeg/ffmpeg_common.cc b/media/ffmpeg/ffmpeg_common.cc index 87ca896..76f03d6 100644 --- a/media/ffmpeg/ffmpeg_common.cc +++ b/media/ffmpeg/ffmpeg_common.cc @@ -345,11 +345,10 @@ codec_context->sample_fmt, codec_context->codec_id); ChannelLayout channel_layout = - codec_context->ch_layout.nb_channels > 8 + codec_context->channels > 8 ? CHANNEL_LAYOUT_DISCRETE - : ChannelLayoutToChromeChannelLayout( - codec_context->ch_layout.u.mask, - codec_context->ch_layout.nb_channels); + : ChannelLayoutToChromeChannelLayout(codec_context->channel_layout, + codec_context->channels); int sample_rate = codec_context->sample_rate; switch (codec) { @@ -401,7 +402,7 @@ extra_data, encryption_scheme, seek_preroll, codec_context->delay); if (channel_layout == CHANNEL_LAYOUT_DISCRETE) - config->SetChannelsForDiscrete(codec_context->ch_layout.nb_channels); + config->SetChannelsForDiscrete(codec_context->channels); #if BUILDFLAG(ENABLE_PLATFORM_AC3_EAC3_AUDIO) // These are bitstream formats unknown to ffmpeg, so they don't have @@ -470,7 +471,7 @@ // TODO(scherkus): should we set |channel_layout|? I'm not sure if FFmpeg uses // said information to decode. - codec_context->ch_layout.nb_channels = config.channels(); + codec_context->channels = config.channels(); codec_context->sample_rate = config.samples_per_second(); if (config.extra_data().empty()) { diff --git a/media/filters/audio_file_reader.cc b/media/filters/audio_file_reader.cc index 5f257bd..e1be5aa 100644 --- a/media/filters/audio_file_reader.cc +++ b/media/filters/audio_file_reader.cc @@ -113,15 +113,14 @@ // Verify the channel layout is supported by Chrome. Acts as a sanity check // against invalid files. See http://crbug.com/171962 - if (ChannelLayoutToChromeChannelLayout( - codec_context_->ch_layout.u.mask, - codec_context_->ch_layout.nb_channels) == + if (ChannelLayoutToChromeChannelLayout(codec_context_->channel_layout, + codec_context_->channels) == CHANNEL_LAYOUT_UNSUPPORTED) { return false; } // Store initial values to guard against midstream configuration changes. - channels_ = codec_context_->ch_layout.nb_channels; + channels_ = codec_context_->channels; audio_codec_ = CodecIDToAudioCodec(codec_context_->codec_id); sample_rate_ = codec_context_->sample_rate; av_sample_format_ = codec_context_->sample_fmt; @@ -223,7 +224,7 @@ if (frames_read < 0) return false; - const int channels = frame->ch_layout.nb_channels; + const int channels = frame->channels; if (frame->sample_rate != sample_rate_ || channels != channels_ || frame->format != av_sample_format_) { DLOG(ERROR) << "Unsupported midstream configuration change!" @@ -243,10 +243,10 @@ // silence from being output. In the case where we are also discarding some // portion of the packet (as indicated by a negative pts), we further want to // adjust the duration downward by however much exists before zero. - if (audio_codec_ == AudioCodec::kAAC && frame->duration) { + if (audio_codec_ == AudioCodec::kAAC && frame->pkt_duration) { const base::TimeDelta pkt_duration = ConvertFromTimeBase( glue_->format_context()->streams[stream_index_]->time_base, - frame->duration + std::min(static_cast(0), frame->pts)); + frame->pkt_duration + std::min(static_cast(0), frame->pts)); const base::TimeDelta frame_duration = base::Seconds(frames_read / static_cast(sample_rate_)); diff --git a/media/filters/ffmpeg_aac_bitstream_converter.cc b/media/filters/ffmpeg_aac_bitstream_converter.cc index 6f231c8..ca5e5fb 100644 --- a/media/filters/ffmpeg_aac_bitstream_converter.cc +++ b/media/filters/ffmpeg_aac_bitstream_converter.cc @@ -195,15 +195,14 @@ if (!header_generated_ || codec_ != stream_codec_parameters_->codec_id || audio_profile_ != stream_codec_parameters_->profile || sample_rate_index_ != sample_rate_index || - channel_configuration_ != - stream_codec_parameters_->ch_layout.nb_channels || + channel_configuration_ != stream_codec_parameters_->channels || frame_length_ != header_plus_packet_size) { header_generated_ = GenerateAdtsHeader(stream_codec_parameters_->codec_id, 0, // layer stream_codec_parameters_->profile, sample_rate_index, 0, // private stream - stream_codec_parameters_->ch_layout.nb_channels, + stream_codec_parameters_->channels, 0, // originality 0, // home 0, // copyrighted_stream @@ -214,7 +215,7 @@ codec_ = stream_codec_parameters_->codec_id; audio_profile_ = stream_codec_parameters_->profile; sample_rate_index_ = sample_rate_index; - channel_configuration_ = stream_codec_parameters_->ch_layout.nb_channels; + channel_configuration_ = stream_codec_parameters_->channels; frame_length_ = header_plus_packet_size; } diff --git a/media/filters/ffmpeg_aac_bitstream_converter_unittest.cc b/media/filters/ffmpeg_aac_bitstream_converter_unittest.cc index 1fd4c5c..f59bcd8f 100644 --- a/media/filters/ffmpeg_aac_bitstream_converter_unittest.cc +++ b/media/filters/ffmpeg_aac_bitstream_converter_unittest.cc @@ -34,7 +34,7 @@ memset(&test_parameters_, 0, sizeof(AVCodecParameters)); test_parameters_.codec_id = AV_CODEC_ID_AAC; test_parameters_.profile = FF_PROFILE_AAC_MAIN; - test_parameters_.ch_layout.nb_channels = 2; + test_parameters_.channels = 2; test_parameters_.extradata = extradata_header_; test_parameters_.extradata_size = sizeof(extradata_header_); } diff --git a/media/filters/ffmpeg_audio_decoder.cc b/media/filters/ffmpeg_audio_decoder.cc index 6a56c67..4615fdeb 100644 --- a/media/filters/ffmpeg_audio_decoder.cc +++ b/media/filters/ffmpeg_audio_decoder.cc @@ -28,7 +28,7 @@ // Return the number of channels from the data in |frame|. static inline int DetermineChannels(AVFrame* frame) { - return frame->ch_layout.nb_channels; + return frame->channels; } // Called by FFmpeg's allocation routine to allocate a buffer. Uses @@ -231,7 +231,7 @@ // Translate unsupported into discrete layouts for discrete configurations; // ffmpeg does not have a labeled discrete configuration internally. ChannelLayout channel_layout = ChannelLayoutToChromeChannelLayout( - codec_context_->ch_layout.u.mask, codec_context_->ch_layout.nb_channels); + codec_context_->channel_layout, codec_context_->channels); if (channel_layout == CHANNEL_LAYOUT_UNSUPPORTED && config_.channel_layout() == CHANNEL_LAYOUT_DISCRETE) { channel_layout = CHANNEL_LAYOUT_DISCRETE; @@ -348,11 +348,11 @@ // Success! av_sample_format_ = codec_context_->sample_fmt; - if (codec_context_->ch_layout.nb_channels != config.channels()) { + if (codec_context_->channels != config.channels()) { MEDIA_LOG(ERROR, media_log_) << "Audio configuration specified " << config.channels() << " channels, but FFmpeg thinks the file contains " - << codec_context_->ch_layout.nb_channels << " channels"; + << codec_context_->channels << " channels"; ReleaseFFmpegResources(); state_ = DecoderState::kUninitialized; return false; @@ -403,7 +403,7 @@ if (frame->nb_samples <= 0) return AVERROR(EINVAL); - if (s->ch_layout.nb_channels != channels) { + if (s->channels != channels) { DLOG(ERROR) << "AVCodecContext and AVFrame disagree on channel count."; return AVERROR(EINVAL); } @@ -436,8 +436,7 @@ ChannelLayout channel_layout = config_.channel_layout() == CHANNEL_LAYOUT_DISCRETE ? CHANNEL_LAYOUT_DISCRETE - : ChannelLayoutToChromeChannelLayout(s->ch_layout.u.mask, - s->ch_layout.nb_channels); + : ChannelLayoutToChromeChannelLayout(s->channel_layout, s->channels); if (channel_layout == CHANNEL_LAYOUT_UNSUPPORTED) { DLOG(ERROR) << "Unsupported channel layout.";