Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / mpeg / adts_stream_parser.cc
bloba59d9ba219d45630e9cd34504fc075d08b11704e
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"
9 namespace media {
11 static const uint32 kADTSStartCodeMask = 0xfff00000;
13 ADTSStreamParser::ADTSStreamParser()
14 : MPEGAudioStreamParserBase(kADTSStartCodeMask, kCodecAAC, 0) {}
16 ADTSStreamParser::~ADTSStreamParser() {}
18 int ADTSStreamParser::ParseFrameHeader(const uint8* data,
19 int size,
20 int* frame_size,
21 int* sample_rate,
22 ChannelLayout* channel_layout,
23 int* sample_count,
24 bool* metadata_frame) const {
25 DCHECK(data);
26 DCHECK_GE(size, 0);
27 DCHECK(frame_size);
29 if (size < 8)
30 return 0;
32 BitReader reader(data, size);
33 int sync;
34 int version;
35 int layer;
36 int protection_absent;
37 int profile;
38 size_t sample_rate_index;
39 size_t channel_layout_index;
40 int frame_length;
41 size_t num_data_blocks;
42 int unused;
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))) {
57 return -1;
60 DVLOG(2) << "Header data :" << std::hex
61 << " sync 0x" << sync
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;
77 return -1;
80 if (sample_rate)
81 *sample_rate = kADTSFrequencyTable[sample_rate_index];
83 if (frame_size)
84 *frame_size = frame_length;
86 if (sample_count)
87 *sample_count = (num_data_blocks + 1) * kSamplesPerAACFrame;
89 if (channel_layout)
90 *channel_layout = kADTSChannelLayoutTable[channel_layout_index];
92 if (metadata_frame)
93 *metadata_frame = false;
95 return bytes_read;
98 } // namespace media