DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
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 "packager/media/formats/mp2t/es_parser_adts.h"
6 
7 #include <stdint.h>
8 
9 #include <list>
10 
11 #include "packager/base/logging.h"
12 #include "packager/base/strings/string_number_conversions.h"
13 #include "packager/media/base/audio_timestamp_helper.h"
14 #include "packager/media/base/bit_reader.h"
15 #include "packager/media/base/media_sample.h"
16 #include "packager/media/base/timestamp.h"
17 #include "packager/media/formats/mp2t/adts_header.h"
18 #include "packager/media/formats/mp2t/mp2t_common.h"
19 #include "packager/media/formats/mpeg/adts_constants.h"
20 
21 namespace edash_packager {
22 namespace media {
23 
24 // Return true if buf corresponds to an ADTS syncword.
25 // |buf| size must be at least 2.
26 static bool isAdtsSyncWord(const uint8_t* buf) {
27  return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0);
28 }
29 
30 // Look for an ADTS syncword.
31 // |new_pos| returns
32 // - either the byte position of the ADTS frame (if found)
33 // - or the byte position of 1st byte that was not processed (if not found).
34 // In every case, the returned value in |new_pos| is such that new_pos >= pos
35 // |frame_sz| returns the size of the ADTS frame (if found).
36 // Return whether a syncword was found.
37 static bool LookForSyncWord(const uint8_t* raw_es,
38  int raw_es_size,
39  int pos,
40  int* new_pos,
41  int* frame_sz) {
42  DCHECK_GE(pos, 0);
43  DCHECK_LE(pos, raw_es_size);
44 
45  int max_offset = raw_es_size - kAdtsHeaderMinSize;
46  if (pos >= max_offset) {
47  // Do not change the position if:
48  // - max_offset < 0: not enough bytes to get a full header
49  // Since pos >= 0, this is a subcase of the next condition.
50  // - pos >= max_offset: might be the case after reading one full frame,
51  // |pos| is then incremented by the frame size and might then point
52  // to the end of the buffer.
53  *new_pos = pos;
54  return false;
55  }
56 
57  for (int offset = pos; offset < max_offset; offset++) {
58  const uint8_t* cur_buf = &raw_es[offset];
59 
60  if (!isAdtsSyncWord(cur_buf))
61  // The first 12 bits must be 1.
62  // The layer field (2 bits) must be set to 0.
63  continue;
64 
65  int frame_size =
66  mp2t::AdtsHeader::GetAdtsFrameSize(cur_buf, kAdtsHeaderMinSize);
67  if (frame_size < kAdtsHeaderMinSize) {
68  // Too short to be an ADTS frame.
69  continue;
70  }
71 
72  // Check whether there is another frame
73  // |size| apart from the current one.
74  int remaining_size = raw_es_size - offset;
75  if (remaining_size >= frame_size + 2 &&
76  !isAdtsSyncWord(&cur_buf[frame_size])) {
77  continue;
78  }
79 
80  *new_pos = offset;
81  *frame_sz = frame_size;
82  return true;
83  }
84 
85  *new_pos = max_offset;
86  return false;
87 }
88 
89 namespace mp2t {
90 
91 EsParserAdts::EsParserAdts(uint32_t pid,
92  const NewStreamInfoCB& new_stream_info_cb,
93  const EmitSampleCB& emit_sample_cb,
94  bool sbr_in_mimetype)
95  : EsParser(pid),
96  new_stream_info_cb_(new_stream_info_cb),
97  emit_sample_cb_(emit_sample_cb),
98  sbr_in_mimetype_(sbr_in_mimetype) {
99 }
100 
101 EsParserAdts::~EsParserAdts() {
102 }
103 
104 bool EsParserAdts::Parse(const uint8_t* buf,
105  int size,
106  int64_t pts,
107  int64_t dts) {
108  int raw_es_size;
109  const uint8_t* raw_es;
110 
111  // The incoming PTS applies to the access unit that comes just after
112  // the beginning of |buf|.
113  if (pts != kNoTimestamp) {
114  es_byte_queue_.Peek(&raw_es, &raw_es_size);
115  pts_list_.push_back(EsPts(raw_es_size, pts));
116  }
117 
118  // Copy the input data to the ES buffer.
119  es_byte_queue_.Push(buf, size);
120  es_byte_queue_.Peek(&raw_es, &raw_es_size);
121 
122  // Look for every ADTS frame in the ES buffer starting at offset = 0
123  int es_position = 0;
124  int frame_size;
125  while (LookForSyncWord(raw_es, raw_es_size, es_position,
126  &es_position, &frame_size)) {
127  const uint8_t* frame_ptr = raw_es + es_position;
128  DVLOG(LOG_LEVEL_ES)
129  << "ADTS syncword @ pos=" << es_position
130  << " frame_size=" << frame_size;
131  DVLOG(LOG_LEVEL_ES)
132  << "ADTS header: "
133  << base::HexEncode(frame_ptr, kAdtsHeaderMinSize);
134 
135  // Do not process the frame if this one is a partial frame.
136  int remaining_size = raw_es_size - es_position;
137  if (frame_size > remaining_size)
138  break;
139 
140  size_t header_size = AdtsHeader::GetAdtsHeaderSize(frame_ptr, frame_size);
141 
142  // Update the audio configuration if needed.
143  DCHECK_GE(frame_size, kAdtsHeaderMinSize);
144  if (!UpdateAudioConfiguration(frame_ptr, frame_size))
145  return false;
146 
147  // Get the PTS & the duration of this access unit.
148  while (!pts_list_.empty() &&
149  pts_list_.front().first <= es_position) {
150  audio_timestamp_helper_->SetBaseTimestamp(pts_list_.front().second);
151  pts_list_.pop_front();
152  }
153 
154  int64_t current_pts = audio_timestamp_helper_->GetTimestamp();
155  int64_t frame_duration =
156  audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame);
157 
158  // Emit an audio frame.
159  bool is_key_frame = true;
160 
161  scoped_refptr<MediaSample> sample =
163  frame_ptr + header_size,
164  frame_size - header_size,
165  is_key_frame);
166  sample->set_pts(current_pts);
167  sample->set_dts(current_pts);
168  sample->set_duration(frame_duration);
169  emit_sample_cb_.Run(pid(), sample);
170 
171  // Update the PTS of the next frame.
172  audio_timestamp_helper_->AddFrames(kSamplesPerAACFrame);
173 
174  // Skip the current frame.
175  es_position += frame_size;
176  }
177 
178  // Discard all the bytes that have been processed.
179  DiscardEs(es_position);
180 
181  return true;
182 }
183 
184 void EsParserAdts::Flush() {
185 }
186 
187 void EsParserAdts::Reset() {
188  es_byte_queue_.Reset();
189  pts_list_.clear();
190  last_audio_decoder_config_ = scoped_refptr<AudioStreamInfo>();
191 }
192 
193 bool EsParserAdts::UpdateAudioConfiguration(const uint8_t* adts_frame,
194  size_t adts_frame_size) {
195  const uint8_t kAacSampleSizeBits(16);
196 
197  AdtsHeader adts_header;
198  if (!adts_header.Parse(adts_frame, adts_frame_size)) {
199  LOG(ERROR) << "Error parsing ADTS frame header.";
200  return false;
201  }
202  std::vector<uint8_t> audio_specific_config;
203  if (!adts_header.GetAudioSpecificConfig(&audio_specific_config))
204  return false;
205 
206  if (last_audio_decoder_config_) {
207  // Verify that the audio decoder config has not changed.
208  if (last_audio_decoder_config_->extra_data() == audio_specific_config) {
209  // Audio configuration has not changed.
210  return true;
211  }
212  NOTIMPLEMENTED() << "Varying audio configurations are not supported.";
213  return false;
214  }
215 
216  // The following code is written according to ISO 14496 Part 3 Table 1.11 and
217  // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers
218  // to SBR doubling the AAC sample rate.)
219  int samples_per_second = adts_header.GetSamplingFrequency();
220  int extended_samples_per_second = sbr_in_mimetype_
221  ? std::min(2 * samples_per_second, 48000)
222  : samples_per_second;
223 
224  last_audio_decoder_config_ = scoped_refptr<StreamInfo>(
225  new AudioStreamInfo(
226  pid(),
227  kMpeg2Timescale,
228  kInfiniteDuration,
229  kCodecAAC,
231  adts_header.GetObjectType()),
232  std::string(),
233  kAacSampleSizeBits,
234  adts_header.GetNumChannels(),
235  extended_samples_per_second,
236  0,
237  0,
238  audio_specific_config.data(),
239  audio_specific_config.size(),
240  false));
241 
242  DVLOG(1) << "Sampling frequency: " << samples_per_second;
243  DVLOG(1) << "Extended sampling frequency: " << extended_samples_per_second;
244  DVLOG(1) << "Channel config: " << adts_header.GetNumChannels();
245  DVLOG(1) << "Object type: " << adts_header.GetObjectType();
246  // Reset the timestamp helper to use a new sampling frequency.
247  if (audio_timestamp_helper_) {
248  int64_t base_timestamp = audio_timestamp_helper_->GetTimestamp();
249  audio_timestamp_helper_.reset(
250  new AudioTimestampHelper(kMpeg2Timescale, samples_per_second));
251  audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
252  } else {
253  audio_timestamp_helper_.reset(
254  new AudioTimestampHelper(kMpeg2Timescale, extended_samples_per_second));
255  }
256 
257  // Audio config notification.
258  new_stream_info_cb_.Run(last_audio_decoder_config_);
259 
260  return true;
261 }
262 
263 void EsParserAdts::DiscardEs(int nbytes) {
264  DCHECK_GE(nbytes, 0);
265  if (nbytes <= 0)
266  return;
267 
268  // Adjust the ES position of each PTS.
269  for (EsPtsList::iterator it = pts_list_.begin(); it != pts_list_.end(); ++it)
270  it->first -= nbytes;
271 
272  // Discard |nbytes| of ES.
273  es_byte_queue_.Pop(nbytes);
274 }
275 
276 } // namespace mp2t
277 } // namespace media
278 } // namespace edash_packager
static size_t GetAdtsFrameSize(const uint8_t *data, size_t num_bytes)
Definition: adts_header.cc:23
void Push(const uint8_t *data, int size)
Append new bytes to the end of the queue.
Definition: byte_queue.cc:29
static scoped_refptr< MediaSample > CopyFrom(const uint8_t *data, size_t size, bool is_key_frame)
Definition: media_sample.cc:45
static size_t GetAdtsHeaderSize(const uint8_t *data, size_t num_bytes)
Definition: adts_header.cc:31
void Reset()
Reset the queue to the empty state.
Definition: byte_queue.cc:24
static std::string GetCodecString(AudioCodec codec, uint8_t audio_object_type)
void Peek(const uint8_t **data, int *size) const
Definition: byte_queue.cc:63