• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mp3_stream_parser.h"
6 
7 namespace media {
8 
9 static const uint32 kMP3StartCodeMask = 0xffe00000;
10 
11 // Map that determines which bitrate_index & channel_mode combinations
12 // are allowed.
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
31 };
32 
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
42 };
43 
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 }
49   { 0, 0, 0, 0, 0, 0 },
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 },
64   { 0, 0, 0, 0, 0}
65 };
66 
67 // Maps the sample rate index and version fields from the frame header
68 // to a sample rate.
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 },
75   { 0, 0, 0, 0 }
76 };
77 
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}};
81 
82 // Frame header field constants.
83 static const int kVersion2 = 2;
84 static const int kVersionReserved = 1;
85 static const int kVersion2_5 = 0;
86 static const int kLayerReserved = 0;
87 static const int kLayer1 = 3;
88 static const int kLayer2 = 2;
89 static const int kLayer3 = 1;
90 static const int kBitrateFree = 0;
91 static const int kBitrateBad = 0xf;
92 static const int kSampleRateReserved = 3;
93 static const int kCodecDelay = 529;
94 
MP3StreamParser()95 MP3StreamParser::MP3StreamParser()
96     : MPEGAudioStreamParserBase(kMP3StartCodeMask, kCodecMP3, kCodecDelay) {}
97 
~MP3StreamParser()98 MP3StreamParser::~MP3StreamParser() {}
99 
ParseFrameHeader(const uint8 * data,int size,int * frame_size,int * sample_rate,ChannelLayout * channel_layout,int * sample_count,bool * metadata_frame) const100 int MP3StreamParser::ParseFrameHeader(const uint8* data,
101                                       int size,
102                                       int* frame_size,
103                                       int* sample_rate,
104                                       ChannelLayout* channel_layout,
105                                       int* sample_count,
106                                       bool* metadata_frame) const {
107   DCHECK(data);
108   DCHECK_GE(size, 0);
109   DCHECK(frame_size);
110 
111   if (size < 4)
112     return 0;
113 
114   BitReader reader(data, size);
115   int sync;
116   int version;
117   int layer;
118   int is_protected;
119   int bitrate_index;
120   int sample_rate_index;
121   int has_padding;
122   int is_private;
123   int channel_mode;
124   int other_flags;
125 
126   if (!reader.ReadBits(11, &sync) ||
127       !reader.ReadBits(2, &version) ||
128       !reader.ReadBits(2, &layer) ||
129       !reader.ReadBits(1, &is_protected) ||
130       !reader.ReadBits(4, &bitrate_index) ||
131       !reader.ReadBits(2, &sample_rate_index) ||
132       !reader.ReadBits(1, &has_padding) ||
133       !reader.ReadBits(1, &is_private) ||
134       !reader.ReadBits(2, &channel_mode) ||
135       !reader.ReadBits(6, &other_flags)) {
136     return -1;
137   }
138 
139   DVLOG(2) << "Header data :" << std::hex
140            << " sync 0x" << sync
141            << " version 0x" << version
142            << " layer 0x" << layer
143            << " bitrate_index 0x" << bitrate_index
144            << " sample_rate_index 0x" << sample_rate_index
145            << " channel_mode 0x" << channel_mode;
146 
147   if (sync != 0x7ff ||
148       version == kVersionReserved ||
149       layer == kLayerReserved ||
150       bitrate_index == kBitrateFree || bitrate_index == kBitrateBad ||
151       sample_rate_index == kSampleRateReserved) {
152     MEDIA_LOG(log_cb()) << "Invalid header data :" << std::hex
153                         << " sync 0x" << sync
154                         << " version 0x" << version
155                         << " layer 0x" << layer
156                         << " bitrate_index 0x" << bitrate_index
157                         << " sample_rate_index 0x" << sample_rate_index
158                         << " channel_mode 0x" << channel_mode;
159     return -1;
160   }
161 
162   if (layer == kLayer2 && kIsAllowed[bitrate_index][channel_mode]) {
163     MEDIA_LOG(log_cb()) << "Invalid (bitrate_index, channel_mode) combination :"
164                         << std::hex
165                         << " bitrate_index " << bitrate_index
166                         << " channel_mode " << channel_mode;
167     return -1;
168   }
169 
170   int bitrate = kBitrateMap[bitrate_index][kVersionLayerMap[version][layer]];
171 
172   if (bitrate == 0) {
173     MEDIA_LOG(log_cb()) << "Invalid bitrate :" << std::hex
174                         << " version " << version
175                         << " layer " << layer
176                         << " bitrate_index " << bitrate_index;
177     return -1;
178   }
179 
180   DVLOG(2) << " bitrate " << bitrate;
181 
182   int frame_sample_rate = kSampleRateMap[sample_rate_index][version];
183   if (frame_sample_rate == 0) {
184     MEDIA_LOG(log_cb()) << "Invalid sample rate :" << std::hex
185                         << " version " << version
186                         << " sample_rate_index " << sample_rate_index;
187     return -1;
188   }
189 
190   if (sample_rate)
191     *sample_rate = frame_sample_rate;
192 
193   // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf
194   // Table 2.1.5
195   int samples_per_frame;
196   switch (layer) {
197     case kLayer1:
198       samples_per_frame = 384;
199       break;
200 
201     case kLayer2:
202       samples_per_frame = 1152;
203       break;
204 
205     case kLayer3:
206       if (version == kVersion2 || version == kVersion2_5)
207         samples_per_frame = 576;
208       else
209         samples_per_frame = 1152;
210       break;
211 
212     default:
213       return -1;
214   }
215 
216   if (sample_count)
217     *sample_count = samples_per_frame;
218 
219   // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf
220   // Text just below Table 2.1.5.
221   if (layer == kLayer1) {
222     // This formulation is a slight variation on the equation below,
223     // but has slightly different truncation characteristics to deal
224     // with the fact that Layer 1 has 4 byte "slots" instead of single
225     // byte ones.
226     *frame_size = 4 * (12 * bitrate * 1000 / frame_sample_rate);
227   } else {
228     *frame_size =
229         ((samples_per_frame / 8) * bitrate * 1000) / frame_sample_rate;
230   }
231 
232   if (has_padding)
233     *frame_size += (layer == kLayer1) ? 4 : 1;
234 
235   if (channel_layout) {
236     // Map Stereo(0), Joint Stereo(1), and Dual Channel (2) to
237     // CHANNEL_LAYOUT_STEREO and Single Channel (3) to CHANNEL_LAYOUT_MONO.
238     *channel_layout =
239         (channel_mode == 3) ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;
240   }
241 
242   if (metadata_frame)
243     *metadata_frame = false;
244 
245   const int header_bytes_read = reader.bits_read() / 8;
246   if (layer != kLayer3)
247     return header_bytes_read;
248 
249   // Check if this is a XING frame and tell the base parser to skip it if so.
250   const int xing_header_index =
251       kXingHeaderMap[version == kVersion2 ||
252                      version == kVersion2_5][channel_mode == 3];
253   uint32_t tag = 0;
254 
255   // It's not a XING frame if the frame isn't big enough to be one.
256   if (*frame_size <
257       header_bytes_read + xing_header_index + static_cast<int>(sizeof(tag))) {
258     return header_bytes_read;
259   }
260 
261   // If we don't have enough data available to check, return 0 so frame parsing
262   // will be retried once more data is available.
263   if (!reader.SkipBits(xing_header_index * 8) ||
264       !reader.ReadBits(sizeof(tag) * 8, &tag)) {
265     return 0;
266   }
267 
268   // Check to see if the tag contains 'Xing' or 'Info'
269   if (tag == 0x496e666f || tag == 0x58696e67) {
270     MEDIA_LOG(log_cb()) << "Skipping XING header.";
271     if (metadata_frame)
272       *metadata_frame = true;
273     return reader.bits_read() / 8;
274   }
275 
276   // If it wasn't a XING frame, just return the number consumed bytes.
277   return header_bytes_read;
278 }
279 
280 }  // namespace media
281