1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include "mp3reader.h"
35
U32_AT(const uint8_t * ptr)36 static uint32_t U32_AT(const uint8_t *ptr) {
37 return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
38 }
39
parseHeader(uint32_t header,size_t * frame_size,uint32_t * out_sampling_rate=NULL,uint32_t * out_channels=NULL,uint32_t * out_bitrate=NULL,uint32_t * out_num_samples=NULL)40 static bool parseHeader(
41 uint32_t header, size_t *frame_size,
42 uint32_t *out_sampling_rate = NULL, uint32_t *out_channels = NULL ,
43 uint32_t *out_bitrate = NULL, uint32_t *out_num_samples = NULL) {
44 *frame_size = 0;
45
46 if (out_sampling_rate) {
47 *out_sampling_rate = 0;
48 }
49
50 if (out_channels) {
51 *out_channels = 0;
52 }
53
54 if (out_bitrate) {
55 *out_bitrate = 0;
56 }
57
58 if (out_num_samples) {
59 *out_num_samples = 1152;
60 }
61
62 if ((header & 0xffe00000) != 0xffe00000) {
63 return false;
64 }
65
66 unsigned version = (header >> 19) & 3;
67
68 if (version == 0x01) {
69 return false;
70 }
71
72 unsigned layer = (header >> 17) & 3;
73
74 if (layer == 0x00) {
75 return false;
76 }
77
78 unsigned bitrate_index = (header >> 12) & 0x0f;
79
80 if (bitrate_index == 0 || bitrate_index == 0x0f) {
81 // Disallow "free" bitrate.
82 return false;
83 }
84
85 unsigned sampling_rate_index = (header >> 10) & 3;
86
87 if (sampling_rate_index == 3) {
88 return false;
89 }
90
91 static const int kSamplingRateV1[] = { 44100, 48000, 32000 };
92 int sampling_rate = kSamplingRateV1[sampling_rate_index];
93 if (version == 2 /* V2 */) {
94 sampling_rate /= 2;
95 } else if (version == 0 /* V2.5 */) {
96 sampling_rate /= 4;
97 }
98
99 unsigned padding = (header >> 9) & 1;
100
101 if (layer == 3) {
102 // layer I
103
104 static const int kBitrateV1[] = {
105 32, 64, 96, 128, 160, 192, 224, 256,
106 288, 320, 352, 384, 416, 448
107 };
108
109 static const int kBitrateV2[] = {
110 32, 48, 56, 64, 80, 96, 112, 128,
111 144, 160, 176, 192, 224, 256
112 };
113
114 int bitrate =
115 (version == 3 /* V1 */)
116 ? kBitrateV1[bitrate_index - 1]
117 : kBitrateV2[bitrate_index - 1];
118
119 if (out_bitrate) {
120 *out_bitrate = bitrate;
121 }
122
123 *frame_size = (12000 * bitrate / sampling_rate + padding) * 4;
124
125 if (out_num_samples) {
126 *out_num_samples = 384;
127 }
128 } else {
129 // layer II or III
130
131 static const int kBitrateV1L2[] = {
132 32, 48, 56, 64, 80, 96, 112, 128,
133 160, 192, 224, 256, 320, 384
134 };
135
136 static const int kBitrateV1L3[] = {
137 32, 40, 48, 56, 64, 80, 96, 112,
138 128, 160, 192, 224, 256, 320
139 };
140
141 static const int kBitrateV2[] = {
142 8, 16, 24, 32, 40, 48, 56, 64,
143 80, 96, 112, 128, 144, 160
144 };
145
146 int bitrate;
147 if (version == 3 /* V1 */) {
148 bitrate = (layer == 2 /* L2 */)
149 ? kBitrateV1L2[bitrate_index - 1]
150 : kBitrateV1L3[bitrate_index - 1];
151
152 if (out_num_samples) {
153 *out_num_samples = 1152;
154 }
155 } else {
156 // V2 (or 2.5)
157
158 bitrate = kBitrateV2[bitrate_index - 1];
159 if (out_num_samples) {
160 *out_num_samples = (layer == 1 /* L3 */) ? 576 : 1152;
161 }
162 }
163
164 if (out_bitrate) {
165 *out_bitrate = bitrate;
166 }
167
168 if (version == 3 /* V1 */) {
169 *frame_size = 144000 * bitrate / sampling_rate + padding;
170 } else {
171 // V2 or V2.5
172 size_t tmp = (layer == 1 /* L3 */) ? 72000 : 144000;
173 *frame_size = tmp * bitrate / sampling_rate + padding;
174 }
175 }
176
177 if (out_sampling_rate) {
178 *out_sampling_rate = sampling_rate;
179 }
180
181 if (out_channels) {
182 int channel_mode = (header >> 6) & 3;
183
184 *out_channels = (channel_mode == 3) ? 1 : 2;
185 }
186
187 return true;
188 }
189
190 // Mask to extract the version, layer, sampling rate parts of the MP3 header,
191 // which should be same for all MP3 frames.
192 static const uint32_t kMask = 0xfffe0c00;
193
sourceReadAt(FILE * fp,off64_t offset,void * data,size_t size)194 static ssize_t sourceReadAt(FILE *fp, off64_t offset, void *data, size_t size) {
195 int retVal = fseek(fp, offset, SEEK_SET);
196 if (retVal != EXIT_SUCCESS) {
197 return 0;
198 } else {
199 return fread(data, 1, size, fp);
200 }
201 }
202
203 // Resync to next valid MP3 frame in the file.
resync(FILE * fp,uint32_t match_header,off64_t * inout_pos,uint32_t * out_header)204 static bool resync(
205 FILE *fp, uint32_t match_header,
206 off64_t *inout_pos, uint32_t *out_header) {
207
208 if (*inout_pos == 0) {
209 // Skip an optional ID3 header if syncing at the very beginning
210 // of the datasource.
211
212 for (;;) {
213 uint8_t id3header[10];
214 int retVal = sourceReadAt(fp, *inout_pos, id3header,
215 sizeof(id3header));
216 if (retVal < (ssize_t)sizeof(id3header)) {
217 // If we can't even read these 10 bytes, we might as well bail
218 // out, even if there _were_ 10 bytes of valid mp3 audio data...
219 return false;
220 }
221
222 if (memcmp("ID3", id3header, 3)) {
223 break;
224 }
225
226 // Skip the ID3v2 header.
227
228 size_t len =
229 ((id3header[6] & 0x7f) << 21)
230 | ((id3header[7] & 0x7f) << 14)
231 | ((id3header[8] & 0x7f) << 7)
232 | (id3header[9] & 0x7f);
233
234 len += 10;
235
236 *inout_pos += len;
237 }
238
239 }
240
241 off64_t pos = *inout_pos;
242 bool valid = false;
243
244 const int32_t kMaxReadBytes = 1024;
245 const int32_t kMaxBytesChecked = 128 * 1024;
246 uint8_t buf[kMaxReadBytes];
247 ssize_t bytesToRead = kMaxReadBytes;
248 ssize_t totalBytesRead = 0;
249 ssize_t remainingBytes = 0;
250 bool reachEOS = false;
251 uint8_t *tmp = buf;
252
253 do {
254 if (pos >= *inout_pos + kMaxBytesChecked) {
255 // Don't scan forever.
256 break;
257 }
258
259 if (remainingBytes < 4) {
260 if (reachEOS) {
261 break;
262 } else {
263 memcpy(buf, tmp, remainingBytes);
264 bytesToRead = kMaxReadBytes - remainingBytes;
265
266 /*
267 * The next read position should start from the end of
268 * the last buffer, and thus should include the remaining
269 * bytes in the buffer.
270 */
271 totalBytesRead = sourceReadAt(fp, pos + remainingBytes,
272 buf + remainingBytes, bytesToRead);
273
274 if (totalBytesRead <= 0) {
275 break;
276 }
277 reachEOS = (totalBytesRead != bytesToRead);
278 remainingBytes += totalBytesRead;
279 tmp = buf;
280 continue;
281 }
282 }
283
284 uint32_t header = U32_AT(tmp);
285
286 if (match_header != 0 && (header & kMask) != (match_header & kMask)) {
287 ++pos;
288 ++tmp;
289 --remainingBytes;
290 continue;
291 }
292
293 size_t frame_size;
294 uint32_t sample_rate, num_channels, bitrate;
295 if (!parseHeader(
296 header, &frame_size,
297 &sample_rate, &num_channels, &bitrate)) {
298 ++pos;
299 ++tmp;
300 --remainingBytes;
301 continue;
302 }
303
304 // We found what looks like a valid frame,
305 // now find its successors.
306
307 off64_t test_pos = pos + frame_size;
308
309 valid = true;
310 const int FRAME_MATCH_REQUIRED = 3;
311 for (int j = 0; j < FRAME_MATCH_REQUIRED; ++j) {
312 uint8_t tmp[4];
313 ssize_t retval = sourceReadAt(fp, test_pos, tmp, sizeof(tmp));
314 if (retval < (ssize_t)sizeof(tmp)) {
315 valid = false;
316 break;
317 }
318
319 uint32_t test_header = U32_AT(tmp);
320
321 if ((test_header & kMask) != (header & kMask)) {
322 valid = false;
323 break;
324 }
325
326 size_t test_frame_size;
327 if (!parseHeader(test_header, &test_frame_size)) {
328 valid = false;
329 break;
330 }
331
332 test_pos += test_frame_size;
333 }
334
335 if (valid) {
336 *inout_pos = pos;
337
338 if (out_header != NULL) {
339 *out_header = header;
340 }
341 }
342
343 ++pos;
344 ++tmp;
345 --remainingBytes;
346 } while (!valid);
347
348 return valid;
349 }
350
Mp3Reader()351 Mp3Reader::Mp3Reader() : mFp(NULL) {
352 }
353
354 // Initialize the MP3 reader.
init(const char * file)355 bool Mp3Reader::init(const char *file) {
356
357 // Open the file.
358 mFp = fopen(file, "rb");
359 if (mFp == NULL) return false;
360
361 // Sync to the first valid frame.
362 off64_t pos = 0;
363 uint32_t header;
364 bool success = resync(mFp, 0 /*match_header*/, &pos, &header);
365 if (success == false) return false;
366
367 mCurrentPos = pos;
368 mFixedHeader = header;
369
370 size_t frame_size;
371 return parseHeader(header, &frame_size, &mSampleRate,
372 &mNumChannels, &mBitrate);
373 }
374
375 // Get the next valid MP3 frame.
getFrame(void * buffer,uint32_t * size)376 bool Mp3Reader::getFrame(void *buffer, uint32_t *size) {
377
378 size_t frame_size;
379 uint32_t bitrate;
380 uint32_t num_samples;
381 uint32_t sample_rate;
382 for (;;) {
383 ssize_t n = sourceReadAt(mFp, mCurrentPos, buffer, 4);
384 if (n < 4) {
385 return false;
386 }
387
388 uint32_t header = U32_AT((const uint8_t *)buffer);
389
390 if ((header & kMask) == (mFixedHeader & kMask)
391 && parseHeader(
392 header, &frame_size, &sample_rate, NULL /*out_channels*/,
393 &bitrate, &num_samples)) {
394 break;
395 }
396
397 // Lost sync.
398 off64_t pos = mCurrentPos;
399 if (!resync(mFp, mFixedHeader, &pos, NULL /*out_header*/)) {
400 // Unable to resync. Signalling end of stream.
401 return false;
402 }
403
404 mCurrentPos = pos;
405
406 // Try again with the new position.
407 }
408 ssize_t n = sourceReadAt(mFp, mCurrentPos, buffer, frame_size);
409 if (n < (ssize_t)frame_size) {
410 return false;
411 }
412
413 *size = frame_size;
414 mCurrentPos += frame_size;
415 return true;
416 }
417
418 // Close the MP3 reader.
close()419 void Mp3Reader::close() {
420 assert(mFp != NULL);
421 fclose(mFp);
422 }
423
~Mp3Reader()424 Mp3Reader::~Mp3Reader() {
425 }
426