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/mpeg1_audio_stream_parser.h"
9 static const uint32 kMPEG1StartCodeMask
= 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 // Offset in bytes from the end of the MP3 header to "Xing" or "Info" tags which
79 // indicate a frame is silent metadata frame. Values taken from FFmpeg.
80 static const int kXingHeaderMap
[2][2] = {{32, 17}, {17, 9}};
82 // Frame header field constants.
83 static const int kBitrateFree
= 0;
84 static const int kBitrateBad
= 0xf;
85 static const int kSampleRateReserved
= 3;
86 static const int kCodecDelay
= 529;
89 bool MPEG1AudioStreamParser::ParseHeader(
90 const scoped_refptr
<MediaLog
>& media_log
,
93 BitReader
reader(data
, kHeaderSize
);
99 int sample_rate_index
;
105 if (!reader
.ReadBits(11, &sync
) ||
106 !reader
.ReadBits(2, &version
) ||
107 !reader
.ReadBits(2, &layer
) ||
108 !reader
.ReadBits(1, &is_protected
) ||
109 !reader
.ReadBits(4, &bitrate_index
) ||
110 !reader
.ReadBits(2, &sample_rate_index
) ||
111 !reader
.ReadBits(1, &has_padding
) ||
112 !reader
.ReadBits(1, &is_private
) ||
113 !reader
.ReadBits(2, &channel_mode
) ||
114 !reader
.ReadBits(6, &other_flags
)) {
118 DVLOG(2) << "Header data :" << std::hex
119 << " sync 0x" << sync
120 << " version 0x" << version
121 << " layer 0x" << layer
122 << " bitrate_index 0x" << bitrate_index
123 << " sample_rate_index 0x" << sample_rate_index
124 << " channel_mode 0x" << channel_mode
;
127 version
== kVersionReserved
||
128 layer
== kLayerReserved
||
129 bitrate_index
== kBitrateFree
|| bitrate_index
== kBitrateBad
||
130 sample_rate_index
== kSampleRateReserved
) {
131 MEDIA_LOG(ERROR
, media_log
)
132 << "Invalid header data :" << std::hex
<< " sync 0x" << sync
133 << " version 0x" << version
<< " layer 0x" << layer
134 << " bitrate_index 0x" << bitrate_index
<< " sample_rate_index 0x"
135 << sample_rate_index
<< " channel_mode 0x" << channel_mode
;
139 if (layer
== kLayer2
&& kIsAllowed
[bitrate_index
][channel_mode
]) {
140 MEDIA_LOG(ERROR
, media_log
) << "Invalid (bitrate_index, channel_mode)"
141 << " combination :" << std::hex
142 << " bitrate_index " << bitrate_index
143 << " channel_mode " << channel_mode
;
147 int bitrate
= kBitrateMap
[bitrate_index
][kVersionLayerMap
[version
][layer
]];
150 MEDIA_LOG(ERROR
, media_log
) << "Invalid bitrate :" << std::hex
151 << " version " << version
<< " layer " << layer
152 << " bitrate_index " << bitrate_index
;
156 DVLOG(2) << " bitrate " << bitrate
;
158 int frame_sample_rate
= kSampleRateMap
[sample_rate_index
][version
];
159 if (frame_sample_rate
== 0) {
160 MEDIA_LOG(ERROR
, media_log
) << "Invalid sample rate :" << std::hex
161 << " version " << version
162 << " sample_rate_index " << sample_rate_index
;
165 header
->sample_rate
= frame_sample_rate
;
167 // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf
169 int samples_per_frame
;
172 samples_per_frame
= 384;
176 samples_per_frame
= 1152;
180 if (version
== kVersion2
|| version
== kVersion2_5
)
181 samples_per_frame
= 576;
183 samples_per_frame
= 1152;
189 header
->sample_count
= samples_per_frame
;
191 // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf
192 // Text just below Table 2.1.5.
193 if (layer
== kLayer1
) {
194 // This formulation is a slight variation on the equation below,
195 // but has slightly different truncation characteristics to deal
196 // with the fact that Layer 1 has 4 byte "slots" instead of single
198 header
->frame_size
= 4 * (12 * bitrate
* 1000 / frame_sample_rate
);
201 ((samples_per_frame
/ 8) * bitrate
* 1000) / frame_sample_rate
;
205 header
->frame_size
+= (layer
== kLayer1
) ? 4 : 1;
207 // Map Stereo(0), Joint Stereo(1), and Dual Channel (2) to
208 // CHANNEL_LAYOUT_STEREO and Single Channel (3) to CHANNEL_LAYOUT_MONO.
209 header
->channel_layout
=
210 (channel_mode
== 3) ? CHANNEL_LAYOUT_MONO
: CHANNEL_LAYOUT_STEREO
;
212 header
->version
= static_cast<Version
>(version
);
213 header
->layer
= static_cast<Layer
>(layer
);
214 header
->channel_mode
= channel_mode
;
219 MPEG1AudioStreamParser::MPEG1AudioStreamParser()
220 : MPEGAudioStreamParserBase(kMPEG1StartCodeMask
, kCodecMP3
, kCodecDelay
) {}
222 MPEG1AudioStreamParser::~MPEG1AudioStreamParser() {}
224 int MPEG1AudioStreamParser::ParseFrameHeader(const uint8
* data
,
228 ChannelLayout
* channel_layout
,
230 bool* metadata_frame
) const {
235 if (size
< kHeaderSize
)
239 if (!ParseHeader(media_log(), data
, &header
))
242 *frame_size
= header
.frame_size
;
244 *sample_rate
= header
.sample_rate
;
246 *sample_count
= header
.sample_count
;
248 *channel_layout
= header
.channel_layout
;
250 *metadata_frame
= false;
252 const int header_bytes_read
= kHeaderSize
;
253 if (header
.layer
!= kLayer3
)
254 return header_bytes_read
;
256 // Check if this is a XING frame and tell the base parser to skip it if so.
257 const int xing_header_index
=
258 kXingHeaderMap
[header
.version
== kVersion2
||
259 header
.version
== kVersion2_5
][header
.channel_mode
== 3];
262 // It's not a XING frame if the frame isn't big enough to be one.
264 header_bytes_read
+ xing_header_index
+ static_cast<int>(sizeof(tag
))) {
265 return header_bytes_read
;
268 // If we don't have enough data available to check, return 0 so frame parsing
269 // will be retried once more data is available.
270 BitReader
reader(data
+ header_bytes_read
, size
- header_bytes_read
);
271 if (!reader
.SkipBits(xing_header_index
* 8) ||
272 !reader
.ReadBits(sizeof(tag
) * 8, &tag
)) {
276 // Check to see if the tag contains 'Xing' or 'Info'
277 if (tag
== 0x496e666f || tag
== 0x58696e67) {
278 MEDIA_LOG(DEBUG
, media_log()) << "Skipping XING header.";
280 *metadata_frame
= true;
281 return header_bytes_read
+ reader
.bits_read() / 8;
284 // If it wasn't a XING frame, just return the number consumed bytes.
285 return header_bytes_read
;