Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / formats / mpeg / adts_stream_parser.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 "media/formats/mpeg/adts_stream_parser.h"
6
7 #include "media/formats/mpeg/adts_constants.h"
8
9 namespace media {
10
11 static const uint32 kADTSStartCodeMask = 0xfff00000;
12
13 ADTSStreamParser::ADTSStreamParser()
14     : MPEGAudioStreamParserBase(kADTSStartCodeMask, kCodecAAC) {}
15
16 ADTSStreamParser::~ADTSStreamParser() {}
17
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) const {
24   DCHECK(data);
25   DCHECK_GE(size, 0);
26   DCHECK(frame_size);
27
28   if (size < 8)
29     return 0;
30
31   BitReader reader(data, size);
32   int sync;
33   int version;
34   int layer;
35   int protection_absent;
36   int profile;
37   size_t sample_rate_index;
38   size_t channel_layout_index;
39   int frame_length;
40   size_t num_data_blocks;
41   int unused;
42
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))) {
56     return -1;
57   }
58
59   DVLOG(2) << "Header data :" << std::hex
60            << " sync 0x" << sync
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;
66
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
72                         << " sync 0x" << sync
73                         << " version 0x" << version
74                         << " layer 0x" << layer
75                         << " sample_rate_index 0x" << sample_rate_index
76                         << " channel_layout_index 0x" << channel_layout_index;
77     return -1;
78   }
79
80   if (sample_rate)
81     *sample_rate = kADTSFrequencyTable[sample_rate_index];
82
83   if (frame_size)
84     *frame_size = frame_length;
85
86   if (sample_count)
87     *sample_count = (num_data_blocks + 1) * kSamplesPerAACFrame;
88
89   if (channel_layout)
90     *channel_layout = kADTSChannelLayoutTable[channel_layout_index];
91
92   return bytes_read;
93 }
94
95 }  // namespace media