Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / media / formats / mp2t / es_parser_adts.cc
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
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
29 static size_t ExtractAdtsFrequencyIndex(const uint8* adts_header) {
30   return ((adts_header[2] >> 2) & 0xf);
31 }
32
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.
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
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
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
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
124 EsParserAdts::~EsParserAdts() {
125 }
126
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) << "Skipping audio frame with unknown timestamp";
144       SkipAdtsFrame(adts_frame);
145       continue;
146     }
147     base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
148     base::TimeDelta frame_duration =
149         audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame);
150
151     // Emit an audio frame.
152     bool is_key_frame = true;
153
154     // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
155     // type and allow multiple audio tracks. See https://crbug.com/341581.
156     scoped_refptr<StreamParserBuffer> stream_parser_buffer =
157         StreamParserBuffer::CopyFrom(
158             adts_frame.data,
159             adts_frame.size,
160             is_key_frame,
161             DemuxerStream::AUDIO, 0);
162     stream_parser_buffer->set_timestamp(current_pts);
163     stream_parser_buffer->SetDecodeTimestamp(
164         DecodeTimestamp::FromPresentationTime(current_pts));
165     stream_parser_buffer->set_duration(frame_duration);
166     emit_buffer_cb_.Run(stream_parser_buffer);
167
168     // Update the PTS of the next frame.
169     audio_timestamp_helper_->AddFrames(kSamplesPerAACFrame);
170
171     // Skip the current frame.
172     SkipAdtsFrame(adts_frame);
173   }
174
175   return true;
176 }
177
178 void EsParserAdts::Flush() {
179 }
180
181 void EsParserAdts::ResetInternal() {
182   last_audio_decoder_config_ = AudioDecoderConfig();
183 }
184
185 bool EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) {
186   size_t frequency_index = ExtractAdtsFrequencyIndex(adts_header);
187   if (frequency_index >= kADTSFrequencyTableSize) {
188     // Frequency index 13 & 14 are reserved
189     // while 15 means that the frequency is explicitly written
190     // (not supported).
191     return false;
192   }
193
194   size_t channel_configuration = ExtractAdtsChannelConfig(adts_header);
195   if (channel_configuration == 0 ||
196       channel_configuration >= kADTSChannelLayoutTableSize) {
197     // TODO(damienv): Add support for inband channel configuration.
198     return false;
199   }
200
201   // TODO(damienv): support HE-AAC frequency doubling (SBR)
202   // based on the incoming ADTS profile.
203   int samples_per_second = kADTSFrequencyTable[frequency_index];
204   int adts_profile = (adts_header[2] >> 6) & 0x3;
205
206   // The following code is written according to ISO 14496 Part 3 Table 1.11 and
207   // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
208   // to SBR doubling the AAC sample rate.)
209   // TODO(damienv) : Extend sample rate cap to 96kHz for Level 5 content.
210   int extended_samples_per_second = sbr_in_mimetype_
211       ? std::min(2 * samples_per_second, 48000)
212       : samples_per_second;
213
214   // The following code is written according to ISO 14496 Part 3 Table 1.13 -
215   // Syntax of AudioSpecificConfig.
216   uint16 extra_data_int =
217       // Note: adts_profile is in the range [0,3], since the ADTS header only
218       // allows two bits for its value.
219       ((adts_profile + 1) << 11) +
220       (frequency_index << 7) +
221       (channel_configuration << 3);
222   uint8 extra_data[2] = {
223       static_cast<uint8>(extra_data_int >> 8),
224       static_cast<uint8>(extra_data_int & 0xff)
225   };
226
227   AudioDecoderConfig audio_decoder_config(
228       kCodecAAC,
229       kSampleFormatS16,
230       kADTSChannelLayoutTable[channel_configuration],
231       extended_samples_per_second,
232       extra_data,
233       arraysize(extra_data),
234       false);
235
236   if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
237     DVLOG(1) << "Sampling frequency: " << samples_per_second;
238     DVLOG(1) << "Extended sampling frequency: " << extended_samples_per_second;
239     DVLOG(1) << "Channel config: " << channel_configuration;
240     DVLOG(1) << "Adts profile: " << adts_profile;
241     // Reset the timestamp helper to use a new time scale.
242     if (audio_timestamp_helper_ &&
243         audio_timestamp_helper_->base_timestamp() != kNoTimestamp()) {
244       base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
245       audio_timestamp_helper_.reset(
246         new AudioTimestampHelper(samples_per_second));
247       audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
248     } else {
249       audio_timestamp_helper_.reset(
250           new AudioTimestampHelper(samples_per_second));
251     }
252     // Audio config notification.
253     last_audio_decoder_config_ = audio_decoder_config;
254     new_audio_config_cb_.Run(audio_decoder_config);
255   }
256
257   return true;
258 }
259
260 }  // namespace mp2t
261 }  // namespace media