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.
5 #include "media/formats/mpeg/adts_stream_parser.h"
7 #include "media/formats/mpeg/adts_constants.h"
11 static const uint32 kADTSStartCodeMask = 0xfff00000;
13 ADTSStreamParser::ADTSStreamParser()
14 : MPEGAudioStreamParserBase(kADTSStartCodeMask, kCodecAAC) {}
16 ADTSStreamParser::~ADTSStreamParser() {}
18 int ADTSStreamParser::ParseFrameHeader(const uint8* data,
22 ChannelLayout* channel_layout,
23 int* sample_count) const {
31 BitReader reader(data, size);
35 int protection_absent;
37 size_t sample_rate_index;
38 size_t channel_layout_index;
40 size_t num_data_blocks;
43 if (!reader.ReadBits(12, &sync) ||
44 !reader.ReadBits(1, &version) ||
45 !reader.ReadBits(2, &layer) ||
46 !reader.ReadBits(1, &protection_absent) ||
47 !reader.ReadBits(2, &profile) ||
48 !reader.ReadBits(4, &sample_rate_index) ||
49 !reader.ReadBits(1, &unused) ||
50 !reader.ReadBits(3, &channel_layout_index) ||
51 !reader.ReadBits(4, &unused) ||
52 !reader.ReadBits(13, &frame_length) ||
53 !reader.ReadBits(11, &unused) ||
54 !reader.ReadBits(2, &num_data_blocks) ||
55 (!protection_absent && !reader.ReadBits(16, &unused))) {
59 DVLOG(2) << "Header data :" << std::hex
61 << " version 0x" << version
62 << " layer 0x" << layer
63 << " profile 0x" << profile
64 << " sample_rate_index 0x" << sample_rate_index
65 << " channel_layout_index 0x" << channel_layout_index;
67 const int bytes_read = reader.bits_read() / 8;
68 if (sync != 0xfff || layer != 0 || frame_length < bytes_read ||
69 sample_rate_index >= kADTSFrequencyTableSize ||
70 channel_layout_index >= kADTSChannelLayoutTableSize) {
71 MEDIA_LOG(log_cb()) << "Invalid header data :" << std::hex
73 << " version 0x" << version
74 << " layer 0x" << layer
75 << " sample_rate_index 0x" << sample_rate_index
76 << " channel_layout_index 0x" << channel_layout_index;
81 *sample_rate = kADTSFrequencyTable[sample_rate_index];
84 *frame_size = frame_length;
87 *sample_count = (num_data_blocks + 1) * kSamplesPerAACFrame;
90 *channel_layout = kADTSChannelLayoutTable[channel_layout_index];