OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/formats/mp2t/es_parser_adts.h" | 5 #include "media/formats/mp2t/es_parser_adts.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "media/base/audio_timestamp_helper.h" | 13 #include "media/base/audio_timestamp_helper.h" |
14 #include "media/base/bit_reader.h" | 14 #include "media/base/bit_reader.h" |
15 #include "media/base/channel_layout.h" | 15 #include "media/base/channel_layout.h" |
| 16 #include "media/base/media_util.h" |
16 #include "media/base/stream_parser_buffer.h" | 17 #include "media/base/stream_parser_buffer.h" |
17 #include "media/base/timestamp_constants.h" | 18 #include "media/base/timestamp_constants.h" |
18 #include "media/formats/common/offset_byte_queue.h" | 19 #include "media/formats/common/offset_byte_queue.h" |
19 #include "media/formats/mp2t/mp2t_common.h" | 20 #include "media/formats/mp2t/mp2t_common.h" |
20 #include "media/formats/mpeg/adts_constants.h" | 21 #include "media/formats/mpeg/adts_constants.h" |
21 #include "media/formats/mpeg/adts_header_parser.h" | |
22 | 22 |
23 namespace media { | 23 namespace media { |
24 | 24 |
25 static int ExtractAdtsFrameSize(const uint8_t* adts_header) { | 25 static int ExtractAdtsFrameSize(const uint8_t* adts_header) { |
26 return ((static_cast<int>(adts_header[5]) >> 5) | | 26 return ((static_cast<int>(adts_header[5]) >> 5) | |
27 (static_cast<int>(adts_header[4]) << 3) | | 27 (static_cast<int>(adts_header[4]) << 3) | |
28 ((static_cast<int>(adts_header[3]) & 0x3) << 11)); | 28 ((static_cast<int>(adts_header[3]) & 0x3) << 11)); |
29 } | 29 } |
30 | 30 |
31 // Return true if buf corresponds to an ADTS syncword. | 31 // Return true if buf corresponds to an ADTS syncword. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 | 116 |
117 EsParserAdts::~EsParserAdts() { | 117 EsParserAdts::~EsParserAdts() { |
118 } | 118 } |
119 | 119 |
120 bool EsParserAdts::ParseFromEsQueue() { | 120 bool EsParserAdts::ParseFromEsQueue() { |
121 // Look for every ADTS frame in the ES buffer. | 121 // Look for every ADTS frame in the ES buffer. |
122 AdtsFrame adts_frame; | 122 AdtsFrame adts_frame; |
123 while (LookForAdtsFrame(&adts_frame)) { | 123 while (LookForAdtsFrame(&adts_frame)) { |
124 // Update the audio configuration if needed. | 124 // Update the audio configuration if needed. |
125 DCHECK_GE(adts_frame.size, kADTSHeaderMinSize); | 125 DCHECK_GE(adts_frame.size, kADTSHeaderMinSize); |
126 if (!UpdateAudioConfiguration(adts_frame.data)) | 126 if (!UpdateAudioConfiguration(adts_frame.data, adts_frame.size)) |
127 return false; | 127 return false; |
128 | 128 |
129 // Get the PTS & the duration of this access unit. | 129 // Get the PTS & the duration of this access unit. |
130 TimingDesc current_timing_desc = | 130 TimingDesc current_timing_desc = |
131 GetTimingDescriptor(adts_frame.queue_offset); | 131 GetTimingDescriptor(adts_frame.queue_offset); |
132 if (current_timing_desc.pts != kNoTimestamp) | 132 if (current_timing_desc.pts != kNoTimestamp) |
133 audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts); | 133 audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts); |
134 | 134 |
135 if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp) { | 135 if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp) { |
136 DVLOG(1) << "Skipping audio frame with unknown timestamp"; | 136 DVLOG(1) << "Skipping audio frame with unknown timestamp"; |
(...skipping 29 matching lines...) Expand all Loading... |
166 return true; | 166 return true; |
167 } | 167 } |
168 | 168 |
169 void EsParserAdts::Flush() { | 169 void EsParserAdts::Flush() { |
170 } | 170 } |
171 | 171 |
172 void EsParserAdts::ResetInternal() { | 172 void EsParserAdts::ResetInternal() { |
173 last_audio_decoder_config_ = AudioDecoderConfig(); | 173 last_audio_decoder_config_ = AudioDecoderConfig(); |
174 } | 174 } |
175 | 175 |
176 bool EsParserAdts::UpdateAudioConfiguration(const uint8_t* adts_header) { | 176 bool EsParserAdts::UpdateAudioConfiguration(const uint8_t* adts_header, |
177 AudioDecoderConfig audio_decoder_config; | 177 int size) { |
178 size_t orig_sample_rate = 0; | 178 int orig_sample_rate; |
179 if (!ParseAdtsHeader(adts_header, sbr_in_mimetype_, &audio_decoder_config, | 179 ChannelLayout channel_layout; |
180 &orig_sample_rate)) | 180 std::vector<uint8_t> extra_data; |
| 181 if (adts_parser_.ParseFrameHeader(adts_header, size, nullptr, |
| 182 &orig_sample_rate, &channel_layout, nullptr, |
| 183 nullptr, &extra_data) <= 0) { |
181 return false; | 184 return false; |
| 185 } |
| 186 |
| 187 // The following code is written according to ISO 14496 Part 3 Table 1.11 and |
| 188 // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers |
| 189 // to SBR doubling the AAC sample rate.) |
| 190 // TODO(damienv) : Extend sample rate cap to 96kHz for Level 5 content. |
| 191 const int extended_samples_per_second = |
| 192 sbr_in_mimetype_ ? std::min(2 * orig_sample_rate, 48000) |
| 193 : orig_sample_rate; |
| 194 |
| 195 AudioDecoderConfig audio_decoder_config( |
| 196 kCodecAAC, kSampleFormatS16, channel_layout, extended_samples_per_second, |
| 197 extra_data, Unencrypted()); |
182 | 198 |
183 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) { | 199 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) { |
184 DVLOG(1) << "Sampling frequency: " | 200 DVLOG(1) << "Sampling frequency: " |
185 << audio_decoder_config.samples_per_second() | 201 << audio_decoder_config.samples_per_second() |
186 << " SBR=" << sbr_in_mimetype_; | 202 << " SBR=" << sbr_in_mimetype_; |
187 DVLOG(1) << "Channel layout: " | 203 DVLOG(1) << "Channel layout: " |
188 << ChannelLayoutToString(audio_decoder_config.channel_layout()); | 204 << ChannelLayoutToString(audio_decoder_config.channel_layout()); |
189 | 205 |
190 // For AAC audio with SBR (Spectral Band Replication) the sampling rate is | 206 // For AAC audio with SBR (Spectral Band Replication) the sampling rate is |
191 // doubled in ParseAdtsHeader above, but AudioTimestampHelper should still | 207 // doubled above, but AudioTimestampHelper should still use the original |
192 // use the original sample rate to compute audio timestamps and durations | 208 // sample rate to compute audio timestamps and durations correctly. |
193 // correctly. | |
194 | 209 |
195 // Reset the timestamp helper to use a new time scale. | 210 // Reset the timestamp helper to use a new time scale. |
196 if (audio_timestamp_helper_ && | 211 if (audio_timestamp_helper_ && |
197 audio_timestamp_helper_->base_timestamp() != kNoTimestamp) { | 212 audio_timestamp_helper_->base_timestamp() != kNoTimestamp) { |
198 base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp(); | 213 base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp(); |
199 audio_timestamp_helper_.reset(new AudioTimestampHelper(orig_sample_rate)); | 214 audio_timestamp_helper_.reset(new AudioTimestampHelper(orig_sample_rate)); |
200 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp); | 215 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp); |
201 } else { | 216 } else { |
202 audio_timestamp_helper_.reset(new AudioTimestampHelper(orig_sample_rate)); | 217 audio_timestamp_helper_.reset(new AudioTimestampHelper(orig_sample_rate)); |
203 } | 218 } |
204 // Audio config notification. | 219 // Audio config notification. |
205 last_audio_decoder_config_ = audio_decoder_config; | 220 last_audio_decoder_config_ = audio_decoder_config; |
206 new_audio_config_cb_.Run(audio_decoder_config); | 221 new_audio_config_cb_.Run(audio_decoder_config); |
207 } | 222 } |
208 | 223 |
209 return true; | 224 return true; |
210 } | 225 } |
211 | 226 |
212 } // namespace mp2t | 227 } // namespace mp2t |
213 } // namespace media | 228 } // namespace media |
OLD | NEW |