Upstream version 7.36.149.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, 0) {}
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,
24                                        bool* metadata_frame) const {
25   DCHECK(data);
26   DCHECK_GE(size, 0);
27   DCHECK(frame_size);
28
29   if (size < 8)
30     return 0;
31
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;
43
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;
58   }
59
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;
67
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(log_cb()) << "Invalid header data :" << std::hex
73                         << " sync 0x" << sync
74                         << " version 0x" << version
75                         << " layer 0x" << layer
76                         << " sample_rate_index 0x" << sample_rate_index
77                         << " channel_layout_index 0x" << channel_layout_index;
78     return -1;
79   }
80
81   if (sample_rate)
82     *sample_rate = kADTSFrequencyTable[sample_rate_index];
83
84   if (frame_size)
85     *frame_size = frame_length;
86
87   if (sample_count)
88     *sample_count = (num_data_blocks + 1) * kSamplesPerAACFrame;
89
90   if (channel_layout)
91     *channel_layout = kADTSChannelLayoutTable[channel_layout_index];
92
93   if (metadata_frame)
94     *metadata_frame = false;
95
96   return bytes_read;
97 }
98
99 }  // namespace media