File ffmpeg-new-channel-layout.patch of Package chromium-ffmpeg-extra (Revision b30401c0254353da4e772a68807e8dc6)

Currently displaying revision b30401c0254353da4e772a68807e8dc6 , Show latest

334
 
1
From 6e554a30893150793c2638e3689cf208ffc8e375 Mon Sep 17 00:00:00 2001
2
From: Dale Curtis <dalecurtis@chromium.org>
3
Date: Sat, 2 Apr 2022 05:13:53 +0000
4
Subject: [PATCH] Roll src/third_party/ffmpeg/ 574c39cce..32b2d1d526 (1125
5
 commits)
6
7
https://chromium.googlesource.com/chromium/third_party/ffmpeg.git/+log/574c39cce323..32b2d1d526
8
9
Created with:
10
  roll-dep src/third_party/ffmpeg
11
12
Fixed: 1293918
13
Cq-Include-Trybots: luci.chromium.try:mac_chromium_asan_rel_ng,linux_chromium_asan_rel_ng,linux_chromium_chromeos_asan_rel_ng
14
Change-Id: I41945d0f963e3d1f65940067bac22f63b68e37d2
15
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3565647
16
Auto-Submit: Dale Curtis <dalecurtis@chromium.org>
17
Reviewed-by: Dan Sanders <sandersd@chromium.org>
18
Commit-Queue: Dale Curtis <dalecurtis@chromium.org>
19
Cr-Commit-Position: refs/heads/main@{#988253}
20
---
21
 .../clear_key_cdm/ffmpeg_cdm_audio_decoder.cc | 29 ++++++++++---------
22
 media/ffmpeg/ffmpeg_common.cc                 | 11 +++----
23
 media/filters/audio_file_reader.cc            |  9 +++---
24
 media/filters/audio_file_reader_unittest.cc   |  6 ++--
25
 .../filters/audio_video_metadata_extractor.cc | 11 +++++--
26
 .../filters/ffmpeg_aac_bitstream_converter.cc |  7 +++--
27
 ...ffmpeg_aac_bitstream_converter_unittest.cc |  2 +-
28
 media/filters/ffmpeg_audio_decoder.cc         | 13 +++++----
29
 8 files changed, 51 insertions(+), 37 deletions(-)
30
31
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
32
index e4fc3f460e2..9b1ad9f7675 100644
33
--- a/media/cdm/library_cdm/clear_key_cdm/ffmpeg_cdm_audio_decoder.cc
34
+++ b/media/cdm/library_cdm/clear_key_cdm/ffmpeg_cdm_audio_decoder.cc
35
@@ -74,7 +74,7 @@ void CdmAudioDecoderConfigToAVCodecContext(
36
       codec_context->sample_fmt = AV_SAMPLE_FMT_NONE;
37
   }
38
 
39
-  codec_context->channels = config.channel_count;
40
+  codec_context->ch_layout.nb_channels = config.channel_count;
41
   codec_context->sample_rate = config.samples_per_second;
42
 
43
   if (config.extra_data) {
44
@@ -124,8 +124,8 @@ void CopySamples(cdm::AudioFormat cdm_format,
45
     case cdm::kAudioFormatPlanarS16:
46
     case cdm::kAudioFormatPlanarF32: {
47
       const int decoded_size_per_channel =
48
-          decoded_audio_size / av_frame.channels;
49
-      for (int i = 0; i < av_frame.channels; ++i) {
50
+          decoded_audio_size / av_frame.ch_layout.nb_channels;
51
+      for (int i = 0; i < av_frame.ch_layout.nb_channels; ++i) {
52
         memcpy(output_buffer, av_frame.extended_data[i],
53
                decoded_size_per_channel);
54
         output_buffer += decoded_size_per_channel;
55
@@ -185,13 +185,14 @@ bool FFmpegCdmAudioDecoder::Initialize(
56
   // Success!
57
   decoding_loop_ = std::make_unique<FFmpegDecodingLoop>(codec_context_.get());
58
   samples_per_second_ = config.samples_per_second;
59
-  bytes_per_frame_ = codec_context_->channels * config.bits_per_channel / 8;
60
+  bytes_per_frame_ =
61
+      codec_context_->ch_layout.nb_channels * config.bits_per_channel / 8;
62
   output_timestamp_helper_ =
63
       std::make_unique<AudioTimestampHelper>(config.samples_per_second);
64
   is_initialized_ = true;
65
 
66
   // Store initial values to guard against midstream configuration changes.
67
-  channels_ = codec_context_->channels;
68
+  channels_ = codec_context_->ch_layout.nb_channels;
69
   av_sample_format_ = codec_context_->sample_fmt;
70
 
71
   return true;
72
@@ -291,17 +292,19 @@ cdm::Status FFmpegCdmAudioDecoder::DecodeBuffer(
73
   for (auto& frame : audio_frames) {
74
     int decoded_audio_size = 0;
75
     if (frame->sample_rate != samples_per_second_ ||
76
-        frame->channels != channels_ || frame->format != av_sample_format_) {
77
+        frame->ch_layout.nb_channels != channels_ ||
78
+        frame->format != av_sample_format_) {
79
       DLOG(ERROR) << "Unsupported midstream configuration change!"
80
                   << " Sample Rate: " << frame->sample_rate << " vs "
81
-                  << samples_per_second_ << ", Channels: " << frame->channels
82
-                  << " vs " << channels_ << ", Sample Format: " << frame->format
83
-                  << " vs " << av_sample_format_;
84
+                  << samples_per_second_
85
+                  << ", Channels: " << frame->ch_layout.nb_channels << " vs "
86
+                  << channels_ << ", Sample Format: " << frame->format << " vs "
87
+                  << av_sample_format_;
88
       return cdm::kDecodeError;
89
     }
90
 
91
     decoded_audio_size = av_samples_get_buffer_size(
92
-        nullptr, codec_context_->channels, frame->nb_samples,
93
+        nullptr, codec_context_->ch_layout.nb_channels, frame->nb_samples,
94
         codec_context_->sample_fmt, 1);
95
     if (!decoded_audio_size)
96
       continue;
97
@@ -320,9 +323,9 @@ bool FFmpegCdmAudioDecoder::OnNewFrame(
98
     size_t* total_size,
99
     std::vector<std::unique_ptr<AVFrame, ScopedPtrAVFreeFrame>>* audio_frames,
100
     AVFrame* frame) {
101
-  *total_size += av_samples_get_buffer_size(nullptr, codec_context_->channels,
102
-                                            frame->nb_samples,
103
-                                            codec_context_->sample_fmt, 1);
104
+  *total_size += av_samples_get_buffer_size(
105
+      nullptr, codec_context_->ch_layout.nb_channels, frame->nb_samples,
106
+      codec_context_->sample_fmt, 1);
107
   audio_frames->emplace_back(av_frame_clone(frame));
108
   return true;
109
 }
110
diff --git a/media/ffmpeg/ffmpeg_common.cc b/media/ffmpeg/ffmpeg_common.cc
111
index 87ca8969626..76f03d6608e 100644
112
--- a/media/ffmpeg/ffmpeg_common.cc
113
+++ b/media/ffmpeg/ffmpeg_common.cc
114
@@ -345,10 +345,11 @@ bool AVCodecContextToAudioDecoderConfig(const AVCodecContext* codec_context,
115
       codec_context->sample_fmt, codec_context->codec_id);
116
 
117
   ChannelLayout channel_layout =
118
-      codec_context->channels > 8
119
+      codec_context->ch_layout.nb_channels > 8
120
           ? CHANNEL_LAYOUT_DISCRETE
121
-          : ChannelLayoutToChromeChannelLayout(codec_context->channel_layout,
122
-                                               codec_context->channels);
123
+          : ChannelLayoutToChromeChannelLayout(
124
+                codec_context->ch_layout.u.mask,
125
+                codec_context->ch_layout.nb_channels);
126
 
127
   int sample_rate = codec_context->sample_rate;
128
   switch (codec) {
129
@@ -401,7 +402,7 @@ bool AVCodecContextToAudioDecoderConfig(const AVCodecContext* codec_context,
130
                      extra_data, encryption_scheme, seek_preroll,
131
                      codec_context->delay);
132
   if (channel_layout == CHANNEL_LAYOUT_DISCRETE)
133
-    config->SetChannelsForDiscrete(codec_context->channels);
134
+    config->SetChannelsForDiscrete(codec_context->ch_layout.nb_channels);
135
 
136
 #if BUILDFLAG(ENABLE_PLATFORM_AC3_EAC3_AUDIO)
137
   // These are bitstream formats unknown to ffmpeg, so they don't have
138
@@ -470,7 +471,7 @@ void AudioDecoderConfigToAVCodecContext(const AudioDecoderConfig& config,
139
 
140
   // TODO(scherkus): should we set |channel_layout|? I'm not sure if FFmpeg uses
141
   // said information to decode.
142
-  codec_context->channels = config.channels();
143
+  codec_context->ch_layout.nb_channels = config.channels();
144
   codec_context->sample_rate = config.samples_per_second();
145
 
146
   if (config.extra_data().empty()) {
147
diff --git a/media/filters/audio_file_reader.cc b/media/filters/audio_file_reader.cc
148
index 5f257bdfaa6..e1be5aa9a5b 100644
149
--- a/media/filters/audio_file_reader.cc
150
+++ b/media/filters/audio_file_reader.cc
151
@@ -113,14 +113,15 @@ bool AudioFileReader::OpenDecoder() {
152
 
153
   // Verify the channel layout is supported by Chrome.  Acts as a sanity check
154
   // against invalid files.  See http://crbug.com/171962
155
-  if (ChannelLayoutToChromeChannelLayout(codec_context_->channel_layout,
156
-                                         codec_context_->channels) ==
157
+  if (ChannelLayoutToChromeChannelLayout(
158
+          codec_context_->ch_layout.u.mask,
159
+          codec_context_->ch_layout.nb_channels) ==
160
       CHANNEL_LAYOUT_UNSUPPORTED) {
161
     return false;
162
   }
163
 
164
   // Store initial values to guard against midstream configuration changes.
165
-  channels_ = codec_context_->channels;
166
+  channels_ = codec_context_->ch_layout.nb_channels;
167
   audio_codec_ = CodecIDToAudioCodec(codec_context_->codec_id);
168
   sample_rate_ = codec_context_->sample_rate;
169
   av_sample_format_ = codec_context_->sample_fmt;
170
@@ -223,7 +224,7 @@ bool AudioFileReader::OnNewFrame(
171
   if (frames_read < 0)
172
     return false;
173
 
174
-  const int channels = frame->channels;
175
+  const int channels = frame->ch_layout.nb_channels;
176
   if (frame->sample_rate != sample_rate_ || channels != channels_ ||
177
       frame->format != av_sample_format_) {
178
     DLOG(ERROR) << "Unsupported midstream configuration change!"
179
diff --git a/media/filters/audio_file_reader_unittest.cc b/media/filters/audio_file_reader_unittest.cc
180
index 2aba7927a31..1f45a50cace 100644
181
--- a/media/filters/audio_file_reader_unittest.cc
182
+++ b/media/filters/audio_file_reader_unittest.cc
183
@@ -121,11 +121,11 @@ class AudioFileReaderTest : public testing::Test {
184
     EXPECT_FALSE(reader_->Open());
185
   }
186
 
187
-  void RunTestFailingDecode(const char* fn) {
188
+  void RunTestFailingDecode(const char* fn, int expect_read = 0) {
189
     Initialize(fn);
190
     EXPECT_TRUE(reader_->Open());
191
     std::vector<std::unique_ptr<AudioBus>> decoded_audio_packets;
192
-    EXPECT_EQ(reader_->Read(&decoded_audio_packets), 0);
193
+    EXPECT_EQ(reader_->Read(&decoded_audio_packets), expect_read);
194
   }
195
 
196
   void RunTestPartialDecode(const char* fn) {
197
@@ -219,7 +219,7 @@ TEST_F(AudioFileReaderTest, AAC_ADTS) {
198
 }
199
 
200
 TEST_F(AudioFileReaderTest, MidStreamConfigChangesFail) {
201
-  RunTestFailingDecode("midstream_config_change.mp3");
202
+  RunTestFailingDecode("midstream_config_change.mp3", 42624);
203
 }
204
 #endif
205
 
206
diff --git a/media/filters/audio_video_metadata_extractor.cc b/media/filters/audio_video_metadata_extractor.cc
207
index 185819eb936..69ff508c221 100644
208
--- a/media/filters/audio_video_metadata_extractor.cc
209
+++ b/media/filters/audio_video_metadata_extractor.cc
210
@@ -113,6 +113,15 @@ bool AudioVideoMetadataExtractor::Extract(DataSource* source,
211
     if (!stream)
212
       continue;
213
 
214
+    void* display_matrix =
215
+        av_stream_get_side_data(stream, AV_PKT_DATA_DISPLAYMATRIX, nullptr);
216
+    if (display_matrix) {
217
+      rotation_ = VideoTransformation::FromFFmpegDisplayMatrix(
218
+                      static_cast<int32_t*>(display_matrix))
219
+                      .rotation;
220
+      info.tags["rotate"] = base::NumberToString(rotation_);
221
+    }
222
+
223
     // Extract dictionary from streams also. Needed for containers that attach
224
     // metadata to contained streams instead the container itself, like OGG.
225
     ExtractDictionary(stream->metadata, &info.tags);
226
@@ -255,8 +264,6 @@ void AudioVideoMetadataExtractor::ExtractDictionary(AVDictionary* metadata,
227
     if (raw_tags->find(tag->key) == raw_tags->end())
228
       (*raw_tags)[tag->key] = tag->value;
229
 
230
-    if (ExtractInt(tag, "rotate", &rotation_))
231
-      continue;
232
     if (ExtractString(tag, "album", &album_))
233
       continue;
234
     if (ExtractString(tag, "artist", &artist_))
235
diff --git a/media/filters/ffmpeg_aac_bitstream_converter.cc b/media/filters/ffmpeg_aac_bitstream_converter.cc
236
index 6f231c85729..ca5e5fb927d 100644
237
--- a/media/filters/ffmpeg_aac_bitstream_converter.cc
238
+++ b/media/filters/ffmpeg_aac_bitstream_converter.cc
239
@@ -195,14 +195,15 @@ bool FFmpegAACBitstreamConverter::ConvertPacket(AVPacket* packet) {
240
   if (!header_generated_ || codec_ != stream_codec_parameters_->codec_id ||
241
       audio_profile_ != stream_codec_parameters_->profile ||
242
       sample_rate_index_ != sample_rate_index ||
243
-      channel_configuration_ != stream_codec_parameters_->channels ||
244
+      channel_configuration_ !=
245
+          stream_codec_parameters_->ch_layout.nb_channels ||
246
       frame_length_ != header_plus_packet_size) {
247
     header_generated_ =
248
         GenerateAdtsHeader(stream_codec_parameters_->codec_id,
249
                            0,  // layer
250
                            stream_codec_parameters_->profile, sample_rate_index,
251
                            0,  // private stream
252
-                           stream_codec_parameters_->channels,
253
+                           stream_codec_parameters_->ch_layout.nb_channels,
254
                            0,  // originality
255
                            0,  // home
256
                            0,  // copyrighted_stream
257
@@ -214,7 +215,7 @@ bool FFmpegAACBitstreamConverter::ConvertPacket(AVPacket* packet) {
258
     codec_ = stream_codec_parameters_->codec_id;
259
     audio_profile_ = stream_codec_parameters_->profile;
260
     sample_rate_index_ = sample_rate_index;
261
-    channel_configuration_ = stream_codec_parameters_->channels;
262
+    channel_configuration_ = stream_codec_parameters_->ch_layout.nb_channels;
263
     frame_length_ = header_plus_packet_size;
264
   }
265
 
266
diff --git a/media/filters/ffmpeg_aac_bitstream_converter_unittest.cc b/media/filters/ffmpeg_aac_bitstream_converter_unittest.cc
267
index 1fd4c5ccd7d..f59bcd8fdaf 100644
268
--- a/media/filters/ffmpeg_aac_bitstream_converter_unittest.cc
269
+++ b/media/filters/ffmpeg_aac_bitstream_converter_unittest.cc
270
@@ -34,7 +34,7 @@ class FFmpegAACBitstreamConverterTest : public testing::Test {
271
     memset(&test_parameters_, 0, sizeof(AVCodecParameters));
272
     test_parameters_.codec_id = AV_CODEC_ID_AAC;
273
     test_parameters_.profile = FF_PROFILE_AAC_MAIN;
274
-    test_parameters_.channels = 2;
275
+    test_parameters_.ch_layout.nb_channels = 2;
276
     test_parameters_.extradata = extradata_header_;
277
     test_parameters_.extradata_size = sizeof(extradata_header_);
278
   }
279
diff --git a/media/filters/ffmpeg_audio_decoder.cc b/media/filters/ffmpeg_audio_decoder.cc
280
index 6a56c675f7d..4615fdeb3fb 100644
281
--- a/media/filters/ffmpeg_audio_decoder.cc
282
+++ b/media/filters/ffmpeg_audio_decoder.cc
283
@@ -28,7 +28,7 @@ namespace media {
284
 
285
 // Return the number of channels from the data in |frame|.
286
 static inline int DetermineChannels(AVFrame* frame) {
287
-  return frame->channels;
288
+  return frame->ch_layout.nb_channels;
289
 }
290
 
291
 // Called by FFmpeg's allocation routine to allocate a buffer. Uses
292
@@ -231,7 +231,7 @@ bool FFmpegAudioDecoder::OnNewFrame(const DecoderBuffer& buffer,
293
   // Translate unsupported into discrete layouts for discrete configurations;
294
   // ffmpeg does not have a labeled discrete configuration internally.
295
   ChannelLayout channel_layout = ChannelLayoutToChromeChannelLayout(
296
-      codec_context_->channel_layout, codec_context_->channels);
297
+      codec_context_->ch_layout.u.mask, codec_context_->ch_layout.nb_channels);
298
   if (channel_layout == CHANNEL_LAYOUT_UNSUPPORTED &&
299
       config_.channel_layout() == CHANNEL_LAYOUT_DISCRETE) {
300
     channel_layout = CHANNEL_LAYOUT_DISCRETE;
301
@@ -348,11 +348,11 @@ bool FFmpegAudioDecoder::ConfigureDecoder(const AudioDecoderConfig& config) {
302
   // Success!
303
   av_sample_format_ = codec_context_->sample_fmt;
304
 
305
-  if (codec_context_->channels != config.channels()) {
306
+  if (codec_context_->ch_layout.nb_channels != config.channels()) {
307
     MEDIA_LOG(ERROR, media_log_)
308
         << "Audio configuration specified " << config.channels()
309
         << " channels, but FFmpeg thinks the file contains "
310
-        << codec_context_->channels << " channels";
311
+        << codec_context_->ch_layout.nb_channels << " channels";
312
     ReleaseFFmpegResources();
313
     state_ = DecoderState::kUninitialized;
314
     return false;
315
@@ -403,7 +403,7 @@ int FFmpegAudioDecoder::GetAudioBuffer(struct AVCodecContext* s,
316
   if (frame->nb_samples <= 0)
317
     return AVERROR(EINVAL);
318
 
319
-  if (s->channels != channels) {
320
+  if (s->ch_layout.nb_channels != channels) {
321
     DLOG(ERROR) << "AVCodecContext and AVFrame disagree on channel count.";
322
     return AVERROR(EINVAL);
323
   }
324
@@ -436,7 +436,8 @@ int FFmpegAudioDecoder::GetAudioBuffer(struct AVCodecContext* s,
325
   ChannelLayout channel_layout =
326
       config_.channel_layout() == CHANNEL_LAYOUT_DISCRETE
327
           ? CHANNEL_LAYOUT_DISCRETE
328
-          : ChannelLayoutToChromeChannelLayout(s->channel_layout, s->channels);
329
+          : ChannelLayoutToChromeChannelLayout(s->ch_layout.u.mask,
330
+                                               s->ch_layout.nb_channels);
331
 
332
   if (channel_layout == CHANNEL_LAYOUT_UNSUPPORTED) {
333
     DLOG(ERROR) << "Unsupported channel layout.";
334