1 From 2aef9000a1c8d76d3072365ffcb471ebffa20d3d Mon Sep 17 00:00:00 2001
2 From: Andreas Schneider <asn@cryptomilk.org>
3 Date: Tue, 15 Mar 2022 14:26:16 +0100
4 Subject: [PATCH] Track first_dts instead of using non-upstream functions
6 The function av_stream_get_first_dts() is not an upstream ffmpeg function and
7 is not available if you build with system ffmpeg. We can easily track the
11 https://ffmpeg.org/pipermail/ffmpeg-devel/2021-September/285401.html
15 Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
16 Change-Id: I90ba3cf2f2e16f56a0b405f26c67f911349fb71d
18 media/filters/ffmpeg_demuxer.cc | 18 ++++++++++++------
19 media/filters/ffmpeg_demuxer.h | 3 +++
20 3 files changed, 16 insertions(+), 6 deletions(-)
22 diff --git a/media/filters/ffmpeg_demuxer.cc b/media/filters/ffmpeg_demuxer.cc
23 index 111899b661..799fc6e941 100644
24 --- a/media/filters/ffmpeg_demuxer.cc
25 +++ b/media/filters/ffmpeg_demuxer.cc
26 @@ -97,7 +97,7 @@ static base::TimeDelta FramesToTimeDelta(int frames, double sample_rate) {
27 frames * base::Time::kMicrosecondsPerSecond / sample_rate);
30 -static base::TimeDelta ExtractStartTime(AVStream* stream) {
31 +static base::TimeDelta ExtractStartTime(AVStream* stream, int64_t first_dts) {
32 // The default start time is zero.
33 base::TimeDelta start_time;
35 @@ -107,12 +107,12 @@ static base::TimeDelta ExtractStartTime(AVStream* stream) {
37 // Next try to use the first DTS value, for codecs where we know PTS == DTS
38 // (excludes all H26x codecs). The start time must be returned in PTS.
39 - if (av_stream_get_first_dts(stream) != kNoFFmpegTimestamp &&
40 + if (first_dts != AV_NOPTS_VALUE &&
41 stream->codecpar->codec_id != AV_CODEC_ID_HEVC &&
42 stream->codecpar->codec_id != AV_CODEC_ID_H264 &&
43 stream->codecpar->codec_id != AV_CODEC_ID_MPEG4) {
44 const base::TimeDelta first_pts =
45 - ConvertFromTimeBase(stream->time_base, av_stream_get_first_dts(stream));
46 + ConvertFromTimeBase(stream->time_base, first_dts);
47 if (first_pts < start_time)
48 start_time = first_pts;
50 @@ -283,6 +283,7 @@ FFmpegDemuxerStream::FFmpegDemuxerStream(
51 fixup_negative_timestamps_(false),
52 fixup_chained_ogg_(false),
53 num_discarded_packet_warnings_(0),
54 + first_dts_(AV_NOPTS_VALUE),
55 last_packet_pos_(AV_NOPTS_VALUE),
56 last_packet_dts_(AV_NOPTS_VALUE) {
58 @@ -349,6 +350,10 @@ void FFmpegDemuxerStream::EnqueuePacket(ScopedAVPacket packet) {
60 packet->dts == AV_NOPTS_VALUE ? packet->pts : packet->dts;
62 + if (first_dts_ == AV_NOPTS_VALUE) {
63 + first_dts_ = packet_dts;
66 // Chained ogg files have non-monotonically increasing position and time stamp
67 // values, which prevents us from using them to determine if a packet should
68 // be dropped. Since chained ogg is only allowed on single track audio only
69 @@ -1442,7 +1447,8 @@ void FFmpegDemuxer::OnFindStreamInfoDone(int result) {
71 max_duration = std::max(max_duration, streams_[i]->duration());
73 - base::TimeDelta start_time = ExtractStartTime(stream);
74 + base::TimeDelta start_time =
75 + ExtractStartTime(stream, streams_[i]->first_dts());
77 // Note: This value is used for seeking, so we must take the true value and
78 // not the one possibly clamped to zero below.
79 diff --git a/media/filters/ffmpeg_demuxer.h b/media/filters/ffmpeg_demuxer.h
80 index c147309d6f..48a8f6ad8c 100644
81 --- a/media/filters/ffmpeg_demuxer.h
82 +++ b/media/filters/ffmpeg_demuxer.h
83 @@ -151,6 +151,8 @@ class MEDIA_EXPORT FFmpegDemuxerStream : public DemuxerStream {
84 base::TimeDelta start_time() const { return start_time_; }
85 void set_start_time(base::TimeDelta time) { start_time_ = time; }
87 + int64_t first_dts() const { return first_dts_; }
90 friend class FFmpegDemuxerTest;
92 @@ -208,6 +210,7 @@ class MEDIA_EXPORT FFmpegDemuxerStream : public DemuxerStream {
93 bool fixup_chained_ogg_;
95 int num_discarded_packet_warnings_;
97 int64_t last_packet_pos_;
98 int64_t last_packet_dts_;