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/mp3_stream_parser.h"
9 static const uint32 kMP3StartCodeMask = 0xffe00000;
11 // Map that determines which bitrate_index & channel_mode combinations
13 // Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
14 static const bool kIsAllowed[17][4] = {
15 { true, true, true, true }, // free
16 { true, false, false, false }, // 32
17 { true, false, false, false }, // 48
18 { true, false, false, false }, // 56
19 { true, true, true, true }, // 64
20 { true, false, false, false }, // 80
21 { true, true, true, true }, // 96
22 { true, true, true, true }, // 112
23 { true, true, true, true }, // 128
24 { true, true, true, true }, // 160
25 { true, true, true, true }, // 192
26 { false, true, true, true }, // 224
27 { false, true, true, true }, // 256
28 { false, true, true, true }, // 320
29 { false, true, true, true }, // 384
30 { false, false, false, false } // bad
33 // Maps version and layer information in the frame header
34 // into an index for the |kBitrateMap|.
35 // Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
36 static const int kVersionLayerMap[4][4] = {
37 // { reserved, L3, L2, L1 }
38 { 5, 4, 4, 3 }, // MPEG 2.5
39 { 5, 5, 5, 5 }, // reserved
40 { 5, 4, 4, 3 }, // MPEG 2
41 { 5, 2, 1, 0 } // MPEG 1
44 // Maps the bitrate index field in the header and an index
45 // from |kVersionLayerMap| to a frame bitrate.
46 // Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
47 static const int kBitrateMap[16][6] = {
48 // { V1L1, V1L2, V1L3, V2L1, V2L2 & V2L3, reserved }
50 { 32, 32, 32, 32, 8, 0 },
51 { 64, 48, 40, 48, 16, 0 },
52 { 96, 56, 48, 56, 24, 0 },
53 { 128, 64, 56, 64, 32, 0 },
54 { 160, 80, 64, 80, 40, 0 },
55 { 192, 96, 80, 96, 48, 0 },
56 { 224, 112, 96, 112, 56, 0 },
57 { 256, 128, 112, 128, 64, 0 },
58 { 288, 160, 128, 144, 80, 0 },
59 { 320, 192, 160, 160, 96, 0 },
60 { 352, 224, 192, 176, 112, 0 },
61 { 384, 256, 224, 192, 128, 0 },
62 { 416, 320, 256, 224, 144, 0 },
63 { 448, 384, 320, 256, 160, 0 },
67 // Maps the sample rate index and version fields from the frame header
69 // Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
70 static const int kSampleRateMap[4][4] = {
71 // { V2.5, reserved, V2, V1 }
72 { 11025, 0, 22050, 44100 },
73 { 12000, 0, 24000, 48000 },
74 { 8000, 0, 16000, 32000 },
78 // Frame header field constants.
79 static const int kVersion2 = 2;
80 static const int kVersionReserved = 1;
81 static const int kVersion2_5 = 0;
82 static const int kLayerReserved = 0;
83 static const int kLayer1 = 3;
84 static const int kLayer2 = 2;
85 static const int kLayer3 = 1;
86 static const int kBitrateFree = 0;
87 static const int kBitrateBad = 0xf;
88 static const int kSampleRateReserved = 3;
90 MP3StreamParser::MP3StreamParser()
91 : MPEGAudioStreamParserBase(kMP3StartCodeMask, kCodecMP3) {}
93 MP3StreamParser::~MP3StreamParser() {}
95 int MP3StreamParser::ParseFrameHeader(const uint8* data,
99 ChannelLayout* channel_layout,
100 int* sample_count) const {
108 BitReader reader(data, size);
114 int sample_rate_index;
120 if (!reader.ReadBits(11, &sync) ||
121 !reader.ReadBits(2, &version) ||
122 !reader.ReadBits(2, &layer) ||
123 !reader.ReadBits(1, &is_protected) ||
124 !reader.ReadBits(4, &bitrate_index) ||
125 !reader.ReadBits(2, &sample_rate_index) ||
126 !reader.ReadBits(1, &has_padding) ||
127 !reader.ReadBits(1, &is_private) ||
128 !reader.ReadBits(2, &channel_mode) ||
129 !reader.ReadBits(6, &other_flags)) {
133 DVLOG(2) << "Header data :" << std::hex
134 << " sync 0x" << sync
135 << " version 0x" << version
136 << " layer 0x" << layer
137 << " bitrate_index 0x" << bitrate_index
138 << " sample_rate_index 0x" << sample_rate_index
139 << " channel_mode 0x" << channel_mode;
142 version == kVersionReserved ||
143 layer == kLayerReserved ||
144 bitrate_index == kBitrateFree || bitrate_index == kBitrateBad ||
145 sample_rate_index == kSampleRateReserved) {
146 MEDIA_LOG(log_cb()) << "Invalid header data :" << std::hex
147 << " sync 0x" << sync
148 << " version 0x" << version
149 << " layer 0x" << layer
150 << " bitrate_index 0x" << bitrate_index
151 << " sample_rate_index 0x" << sample_rate_index
152 << " channel_mode 0x" << channel_mode;
156 if (layer == kLayer2 && kIsAllowed[bitrate_index][channel_mode]) {
157 MEDIA_LOG(log_cb()) << "Invalid (bitrate_index, channel_mode) combination :"
159 << " bitrate_index " << bitrate_index
160 << " channel_mode " << channel_mode;
164 int bitrate = kBitrateMap[bitrate_index][kVersionLayerMap[version][layer]];
167 MEDIA_LOG(log_cb()) << "Invalid bitrate :" << std::hex
168 << " version " << version
169 << " layer " << layer
170 << " bitrate_index " << bitrate_index;
174 DVLOG(2) << " bitrate " << bitrate;
176 int frame_sample_rate = kSampleRateMap[sample_rate_index][version];
177 if (frame_sample_rate == 0) {
178 MEDIA_LOG(log_cb()) << "Invalid sample rate :" << std::hex
179 << " version " << version
180 << " sample_rate_index " << sample_rate_index;
185 *sample_rate = frame_sample_rate;
187 // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf
189 int samples_per_frame;
192 samples_per_frame = 384;
196 samples_per_frame = 1152;
200 if (version == kVersion2 || version == kVersion2_5)
201 samples_per_frame = 576;
203 samples_per_frame = 1152;
211 *sample_count = samples_per_frame;
213 // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf
214 // Text just below Table 2.1.5.
215 if (layer == kLayer1) {
216 // This formulation is a slight variation on the equation below,
217 // but has slightly different truncation characteristics to deal
218 // with the fact that Layer 1 has 4 byte "slots" instead of single
220 *frame_size = 4 * (12 * bitrate * 1000 / frame_sample_rate);
223 ((samples_per_frame / 8) * bitrate * 1000) / frame_sample_rate;
227 *frame_size += (layer == kLayer1) ? 4 : 1;
229 if (channel_layout) {
230 // Map Stereo(0), Joint Stereo(1), and Dual Channel (2) to
231 // CHANNEL_LAYOUT_STEREO and Single Channel (3) to CHANNEL_LAYOUT_MONO.
233 (channel_mode == 3) ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;