• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "media/formats/mp2t/es_parser_adts.h"
6 
7 #include <list>
8 
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "media/base/audio_timestamp_helper.h"
13 #include "media/base/bit_reader.h"
14 #include "media/base/buffers.h"
15 #include "media/base/channel_layout.h"
16 #include "media/base/stream_parser_buffer.h"
17 #include "media/formats/common/offset_byte_queue.h"
18 #include "media/formats/mp2t/mp2t_common.h"
19 #include "media/formats/mpeg/adts_constants.h"
20 
21 namespace media {
22 
ExtractAdtsFrameSize(const uint8 * adts_header)23 static int ExtractAdtsFrameSize(const uint8* adts_header) {
24   return ((static_cast<int>(adts_header[5]) >> 5) |
25           (static_cast<int>(adts_header[4]) << 3) |
26           ((static_cast<int>(adts_header[3]) & 0x3) << 11));
27 }
28 
ExtractAdtsFrequencyIndex(const uint8 * adts_header)29 static size_t ExtractAdtsFrequencyIndex(const uint8* adts_header) {
30   return ((adts_header[2] >> 2) & 0xf);
31 }
32 
ExtractAdtsChannelConfig(const uint8 * adts_header)33 static size_t ExtractAdtsChannelConfig(const uint8* adts_header) {
34   return (((adts_header[3] >> 6) & 0x3) |
35           ((adts_header[2] & 0x1) << 2));
36 }
37 
38 // Return true if buf corresponds to an ADTS syncword.
39 // |buf| size must be at least 2.
isAdtsSyncWord(const uint8 * buf)40 static bool isAdtsSyncWord(const uint8* buf) {
41   // The first 12 bits must be 1.
42   // The layer field (2 bits) must be set to 0.
43   return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0);
44 }
45 
46 namespace mp2t {
47 
48 struct EsParserAdts::AdtsFrame {
49   // Pointer to the ES data.
50   const uint8* data;
51 
52   // Frame size;
53   int size;
54 
55   // Frame offset in the ES queue.
56   int64 queue_offset;
57 };
58 
LookForAdtsFrame(AdtsFrame * adts_frame)59 bool EsParserAdts::LookForAdtsFrame(AdtsFrame* adts_frame) {
60   int es_size;
61   const uint8* es;
62   es_queue_->Peek(&es, &es_size);
63 
64   int max_offset = es_size - kADTSHeaderMinSize;
65   if (max_offset <= 0)
66     return false;
67 
68   for (int offset = 0; offset < max_offset; offset++) {
69     const uint8* cur_buf = &es[offset];
70     if (!isAdtsSyncWord(cur_buf))
71       continue;
72 
73     int frame_size = ExtractAdtsFrameSize(cur_buf);
74     if (frame_size < kADTSHeaderMinSize) {
75       // Too short to be an ADTS frame.
76       continue;
77     }
78 
79     int remaining_size = es_size - offset;
80     if (remaining_size < frame_size) {
81       // Not a full frame: will resume when we have more data.
82       es_queue_->Pop(offset);
83       return false;
84     }
85 
86     // Check whether there is another frame
87     // |size| apart from the current one.
88     if (remaining_size >= frame_size + 2 &&
89         !isAdtsSyncWord(&cur_buf[frame_size])) {
90       continue;
91     }
92 
93     es_queue_->Pop(offset);
94     es_queue_->Peek(&adts_frame->data, &es_size);
95     adts_frame->queue_offset = es_queue_->head();
96     adts_frame->size = frame_size;
97     DVLOG(LOG_LEVEL_ES)
98         << "ADTS syncword @ pos=" << adts_frame->queue_offset
99         << " frame_size=" << adts_frame->size;
100     DVLOG(LOG_LEVEL_ES)
101         << "ADTS header: "
102         << base::HexEncode(adts_frame->data, kADTSHeaderMinSize);
103     return true;
104   }
105 
106   es_queue_->Pop(max_offset);
107   return false;
108 }
109 
SkipAdtsFrame(const AdtsFrame & adts_frame)110 void EsParserAdts::SkipAdtsFrame(const AdtsFrame& adts_frame) {
111   DCHECK_EQ(adts_frame.queue_offset, es_queue_->head());
112   es_queue_->Pop(adts_frame.size);
113 }
114 
EsParserAdts(const NewAudioConfigCB & new_audio_config_cb,const EmitBufferCB & emit_buffer_cb,bool sbr_in_mimetype)115 EsParserAdts::EsParserAdts(
116     const NewAudioConfigCB& new_audio_config_cb,
117     const EmitBufferCB& emit_buffer_cb,
118     bool sbr_in_mimetype)
119   : new_audio_config_cb_(new_audio_config_cb),
120     emit_buffer_cb_(emit_buffer_cb),
121     sbr_in_mimetype_(sbr_in_mimetype) {
122 }
123 
~EsParserAdts()124 EsParserAdts::~EsParserAdts() {
125 }
126 
ParseFromEsQueue()127 bool EsParserAdts::ParseFromEsQueue() {
128   // Look for every ADTS frame in the ES buffer.
129   AdtsFrame adts_frame;
130   while (LookForAdtsFrame(&adts_frame)) {
131     // Update the audio configuration if needed.
132     DCHECK_GE(adts_frame.size, kADTSHeaderMinSize);
133     if (!UpdateAudioConfiguration(adts_frame.data))
134       return false;
135 
136     // Get the PTS & the duration of this access unit.
137     TimingDesc current_timing_desc =
138         GetTimingDescriptor(adts_frame.queue_offset);
139     if (current_timing_desc.pts != kNoTimestamp())
140       audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts);
141 
142     if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp()) {
143       DVLOG(1) << "Audio frame with unknown timestamp";
144       return false;
145     }
146     base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
147     base::TimeDelta frame_duration =
148         audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame);
149 
150     // Emit an audio frame.
151     bool is_key_frame = true;
152 
153     // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
154     // type and allow multiple audio tracks. See https://crbug.com/341581.
155     scoped_refptr<StreamParserBuffer> stream_parser_buffer =
156         StreamParserBuffer::CopyFrom(
157             adts_frame.data,
158             adts_frame.size,
159             is_key_frame,
160             DemuxerStream::AUDIO, 0);
161     stream_parser_buffer->set_timestamp(current_pts);
162     stream_parser_buffer->SetDecodeTimestamp(
163         DecodeTimestamp::FromPresentationTime(current_pts));
164     stream_parser_buffer->set_duration(frame_duration);
165     emit_buffer_cb_.Run(stream_parser_buffer);
166 
167     // Update the PTS of the next frame.
168     audio_timestamp_helper_->AddFrames(kSamplesPerAACFrame);
169 
170     // Skip the current frame.
171     SkipAdtsFrame(adts_frame);
172   }
173 
174   return true;
175 }
176 
Flush()177 void EsParserAdts::Flush() {
178 }
179 
ResetInternal()180 void EsParserAdts::ResetInternal() {
181   last_audio_decoder_config_ = AudioDecoderConfig();
182 }
183 
UpdateAudioConfiguration(const uint8 * adts_header)184 bool EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) {
185   size_t frequency_index = ExtractAdtsFrequencyIndex(adts_header);
186   if (frequency_index >= kADTSFrequencyTableSize) {
187     // Frequency index 13 & 14 are reserved
188     // while 15 means that the frequency is explicitly written
189     // (not supported).
190     return false;
191   }
192 
193   size_t channel_configuration = ExtractAdtsChannelConfig(adts_header);
194   if (channel_configuration == 0 ||
195       channel_configuration >= kADTSChannelLayoutTableSize) {
196     // TODO(damienv): Add support for inband channel configuration.
197     return false;
198   }
199 
200   // TODO(damienv): support HE-AAC frequency doubling (SBR)
201   // based on the incoming ADTS profile.
202   int samples_per_second = kADTSFrequencyTable[frequency_index];
203   int adts_profile = (adts_header[2] >> 6) & 0x3;
204 
205   // The following code is written according to ISO 14496 Part 3 Table 1.11 and
206   // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
207   // to SBR doubling the AAC sample rate.)
208   // TODO(damienv) : Extend sample rate cap to 96kHz for Level 5 content.
209   int extended_samples_per_second = sbr_in_mimetype_
210       ? std::min(2 * samples_per_second, 48000)
211       : samples_per_second;
212 
213   // The following code is written according to ISO 14496 Part 3 Table 1.13 -
214   // Syntax of AudioSpecificConfig.
215   uint16 extra_data_int =
216       // Note: adts_profile is in the range [0,3], since the ADTS header only
217       // allows two bits for its value.
218       ((adts_profile + 1) << 11) +
219       (frequency_index << 7) +
220       (channel_configuration << 3);
221   uint8 extra_data[2] = {
222       static_cast<uint8>(extra_data_int >> 8),
223       static_cast<uint8>(extra_data_int & 0xff)
224   };
225 
226   AudioDecoderConfig audio_decoder_config(
227       kCodecAAC,
228       kSampleFormatS16,
229       kADTSChannelLayoutTable[channel_configuration],
230       extended_samples_per_second,
231       extra_data,
232       arraysize(extra_data),
233       false);
234 
235   if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
236     DVLOG(1) << "Sampling frequency: " << samples_per_second;
237     DVLOG(1) << "Extended sampling frequency: " << extended_samples_per_second;
238     DVLOG(1) << "Channel config: " << channel_configuration;
239     DVLOG(1) << "Adts profile: " << adts_profile;
240     // Reset the timestamp helper to use a new time scale.
241     if (audio_timestamp_helper_ &&
242         audio_timestamp_helper_->base_timestamp() != kNoTimestamp()) {
243       base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
244       audio_timestamp_helper_.reset(
245         new AudioTimestampHelper(samples_per_second));
246       audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
247     } else {
248       audio_timestamp_helper_.reset(
249           new AudioTimestampHelper(samples_per_second));
250     }
251     // Audio config notification.
252     last_audio_decoder_config_ = audio_decoder_config;
253     new_audio_config_cb_.Run(audio_decoder_config);
254   }
255 
256   return true;
257 }
258 
259 }  // namespace mp2t
260 }  // namespace media
261