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
, 0) {}
16 ADTSStreamParser::~ADTSStreamParser() {}
18 int ADTSStreamParser::ParseFrameHeader(const uint8
* data
,
22 ChannelLayout
* channel_layout
,
24 bool* metadata_frame
) const {
32 BitReader
reader(data
, size
);
36 int protection_absent
;
38 size_t sample_rate_index
;
39 size_t channel_layout_index
;
41 size_t num_data_blocks
;
44 if (!reader
.ReadBits(12, &sync
) ||
45 !reader
.ReadBits(1, &version
) ||
46 !reader
.ReadBits(2, &layer
) ||
47 !reader
.ReadBits(1, &protection_absent
) ||
48 !reader
.ReadBits(2, &profile
) ||
49 !reader
.ReadBits(4, &sample_rate_index
) ||
50 !reader
.ReadBits(1, &unused
) ||
51 !reader
.ReadBits(3, &channel_layout_index
) ||
52 !reader
.ReadBits(4, &unused
) ||
53 !reader
.ReadBits(13, &frame_length
) ||
54 !reader
.ReadBits(11, &unused
) ||
55 !reader
.ReadBits(2, &num_data_blocks
) ||
56 (!protection_absent
&& !reader
.ReadBits(16, &unused
))) {
60 DVLOG(2) << "Header data :" << std::hex
62 << " version 0x" << version
63 << " layer 0x" << layer
64 << " profile 0x" << profile
65 << " sample_rate_index 0x" << sample_rate_index
66 << " channel_layout_index 0x" << channel_layout_index
;
68 const int bytes_read
= reader
.bits_read() / 8;
69 if (sync
!= 0xfff || layer
!= 0 || frame_length
< bytes_read
||
70 sample_rate_index
>= kADTSFrequencyTableSize
||
71 channel_layout_index
>= kADTSChannelLayoutTableSize
) {
72 MEDIA_LOG(DEBUG
, media_log())
73 << "Invalid header data :" << std::hex
<< " sync 0x" << sync
74 << " version 0x" << version
<< " 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
];
93 *metadata_frame
= false;