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/mpeg/adts_stream_parser.h"
6
7 #include <stddef.h>
8
9 #include "build/build_config.h"
10 #include "media/base/media_log.h"
11 #include "media/formats/mp4/aac.h"
12 #include "media/formats/mpeg/adts_constants.h"
13
14 namespace media {
15
16 constexpr uint32_t kADTSStartCodeMask = 0xfff00000;
17
ADTSStreamParser()18 ADTSStreamParser::ADTSStreamParser()
19 : MPEGAudioStreamParserBase(kADTSStartCodeMask, kCodecAAC, 0) {}
20
21 ADTSStreamParser::~ADTSStreamParser() = default;
22
ParseFrameHeader(const uint8_t * data,int size,int * frame_size,int * sample_rate,ChannelLayout * channel_layout,int * sample_count,bool * metadata_frame,std::vector<uint8_t> * extra_data)23 int ADTSStreamParser::ParseFrameHeader(const uint8_t* data,
24 int size,
25 int* frame_size,
26 int* sample_rate,
27 ChannelLayout* channel_layout,
28 int* sample_count,
29 bool* metadata_frame,
30 std::vector<uint8_t>* extra_data) {
31 DCHECK(data);
32 DCHECK_GE(size, 0);
33
34 if (size < kADTSHeaderMinSize)
35 return 0;
36
37 BitReader reader(data, size);
38 int sync;
39 int version;
40 int layer;
41 int protection_absent;
42 int profile;
43 size_t sample_rate_index;
44 size_t channel_layout_index;
45 int frame_length;
46 size_t num_data_blocks;
47 int unused;
48
49 if (!reader.ReadBits(12, &sync) ||
50 !reader.ReadBits(1, &version) ||
51 !reader.ReadBits(2, &layer) ||
52 !reader.ReadBits(1, &protection_absent) ||
53 !reader.ReadBits(2, &profile) ||
54 !reader.ReadBits(4, &sample_rate_index) ||
55 !reader.ReadBits(1, &unused) ||
56 !reader.ReadBits(3, &channel_layout_index) ||
57 !reader.ReadBits(4, &unused) ||
58 !reader.ReadBits(13, &frame_length) ||
59 !reader.ReadBits(11, &unused) ||
60 !reader.ReadBits(2, &num_data_blocks) ||
61 (!protection_absent && !reader.ReadBits(16, &unused))) {
62 return -1;
63 }
64
65 DVLOG(2) << "Header data :" << std::hex << " sync 0x" << sync << " version 0x"
66 << version << " layer 0x" << layer << " profile 0x" << profile
67 << " sample_rate_index 0x" << sample_rate_index
68 << " channel_layout_index 0x" << channel_layout_index;
69
70 const int bytes_read = reader.bits_read() / 8;
71 if (sync != 0xfff || layer != 0 || frame_length < bytes_read ||
72 sample_rate_index >= kADTSFrequencyTableSize ||
73 channel_layout_index >= kADTSChannelLayoutTableSize) {
74 if (media_log()) {
75 LIMITED_MEDIA_LOG(DEBUG, media_log(), adts_parse_error_limit_, 5)
76 << "Invalid header data :" << std::hex << " sync 0x" << sync
77 << " version 0x" << version << " layer 0x" << layer
78 << " sample_rate_index 0x" << sample_rate_index
79 << " channel_layout_index 0x" << channel_layout_index;
80 }
81 return -1;
82 }
83
84 if (sample_rate)
85 *sample_rate = kADTSFrequencyTable[sample_rate_index];
86
87 if (frame_size)
88 *frame_size = frame_length;
89
90 if (sample_count)
91 *sample_count = (num_data_blocks + 1) * kSamplesPerAACFrame;
92
93 if (channel_layout)
94 *channel_layout = kADTSChannelLayoutTable[channel_layout_index];
95
96 if (metadata_frame)
97 *metadata_frame = false;
98
99 if (extra_data) {
100 // See mp4::AAC::Parse() for details. We don't need to worry about writing
101 // extensions since we can't have extended ADTS by this point (it's
102 // explicitly rejected as invalid above).
103 DCHECK_NE(sample_rate_index, 15u);
104
105 // The following code is written according to ISO 14496 Part 3 Table 1.13 -
106 // Syntax of AudioSpecificConfig.
107 const uint16_t esds = (((((profile + 1) << 4) + sample_rate_index) << 4) +
108 channel_layout_index)
109 << 3;
110 extra_data->push_back(esds >> 8);
111 extra_data->push_back(esds & 0xFF);
112 }
113
114 return bytes_read;
115 }
116
117 } // namespace media
118