1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
#include <string.h>
#include <stdio.h>
#include <mad.h>
#include <assert.h>
#include <stdlib.h>
#include "codec.h"
#include "cmp3.h"
#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))
#define READBUFFER 5*8192
#define CACHESIZE 81920
struct buffer {
FILE *file;
// input buffer, for MPEG data
char input[READBUFFER];
int remaining;
// output buffer, supplied by player
char *output;
int readsize;
// cache, for extra decoded samples
char cache[CACHESIZE];
int cachefill;
};
static struct buffer buffer;
static struct mad_stream stream;
static struct mad_frame frame;
static struct mad_synth synth;
static mad_timer_t timer;
static int endoffile;
static int
cmp3_decode (void);
//static int
//cmp3_get_stream_info (void);
static int
cmp3_get_stream_info2 (void);
int
cmp3_init (const char *fname, int track, float start, float end) {
buffer.file = fopen (fname, "rb");
if (!buffer.file) {
printf ("failed to read %s\n", fname);
return -1;
}
buffer.remaining = 0;
buffer.output = NULL;
buffer.readsize = 0;
buffer.cachefill = 0;
endoffile = 0;
cmp3.info.position = 0;
mad_stream_init(&stream);
mad_frame_init(&frame);
mad_synth_init(&synth);
mad_timer_reset(&timer);
if (cmp3_get_stream_info2 () == -1) {
return -1;
}
fseek (buffer.file, 0, SEEK_SET);
// cmp3_decode (); // this 1st run will read 1st frame, but will not decode anything except header
return 0;
}
/****************************************************************************
* Converts a sample from libmad's fixed point number format to a signed *
* short (16 bits). *
****************************************************************************/
static signed short MadFixedToSshort(mad_fixed_t Fixed)
{
/* A fixed point number is formed of the following bit pattern:
*
* SWWWFFFFFFFFFFFFFFFFFFFFFFFFFFFF
* MSB LSB
* S ==> Sign (0 is positive, 1 is negative)
* W ==> Whole part bits
* F ==> Fractional part bits
*
* This pattern contains MAD_F_FRACBITS fractional bits, one
* should alway use this macro when working on the bits of a fixed
* point number. It is not guaranteed to be constant over the
* different platforms supported by libmad.
*
* The signed short value is formed, after clipping, by the least
* significant whole part bit, followed by the 15 most significant
* fractional part bits. Warning: this is a quick and dirty way to
* compute the 16-bit number, madplay includes much better
* algorithms.
*/
/* Clipping */
if(Fixed>=MAD_F_ONE)
return(32767);
if(Fixed<=-MAD_F_ONE)
return(-32768);
/* Conversion. */
Fixed=Fixed>>(MAD_F_FRACBITS-15);
return((signed short)Fixed);
}
#define MadErrorString(x) mad_stream_errorstr(x)
static int
cmp3_get_stream_info2 (void) {
int nframe = 0;
int nskipped = 0;
float duration = 0;
for (;;) {
uint32_t hdr;
uint8_t sync;
if (fread (&sync, 1, 1, buffer.file) != 1) {
break; // eof
}
if (sync != 0xff) {
nskipped++;
continue; // not an mpeg frame
}
else {
// 2nd sync byte
if (fread (&sync, 1, 1, buffer.file) != 1) {
break; // eof
}
if ((sync >> 5) != 7) {
nskipped++;
continue;
}
}
// found frame
hdr = (0xff<<24) | (sync << 16);
// read 2 bytes more
if (fread (&sync, 1, 1, buffer.file) != 1) {
break; // eof
}
hdr |= sync << 8;
if (fread (&sync, 1, 1, buffer.file) != 1) {
break; // eof
}
hdr |= sync;
// printf ("header=%x, skipped %d\n", hdr, nskipped);
nskipped = 0;
// parse header
// sync bits
int usync = hdr & 0xffe00000;
// printf ("sync = %xh\n", usync);
if (usync != 0xffe00000) {
printf ("fatal error: mp3 header parser is broken\n");
// exit (0);
}
// layer info
int layer = (hdr & (3<<17)) >> 17;
// printf ("layer = %xh\n", layer);
// bitrate
int brtable[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1 };
int bitrate = (hdr & (0x0f<<12)) >> 12;
bitrate = brtable[bitrate];
// printf ("bitrate = %d\n", bitrate);
// samplerate
int srtable[4] = {44100, 48000, 32000, -1};
int samplerate = (hdr & (0x03<<10))>>10;
samplerate = srtable[samplerate];
// printf ("samplerate = %d\n", samplerate);
// padding
int padding = (hdr & (0x1 << 9)) >> 9;
// printf ("padding = %d\n", padding);
int chantbl[4] = { 2, 2, 2, 1 };
int nchannels = (hdr & (0x3 << 6)) >> 6;
nchannels = chantbl[nchannels];
if (nframe == 0) {
cmp3.info.bitsPerSample = 16;
cmp3.info.channels = nchannels;
cmp3.info.samplesPerSecond = samplerate;
}
// packetlength
int packetlength = 0;
bitrate *= 1000;
if (layer == 3) { // layer1
packetlength = (12 * bitrate / samplerate + padding) * 4;
}
else if (layer == 2) { // layer2
packetlength = 144 * bitrate / samplerate + padding;
}
else if (layer == 1) { // layer3
duration += (float)1152 / samplerate;
packetlength = 144 * bitrate / samplerate + padding;
}
//printf ("packetlength = %d\n", packetlength);
nframe++;
fseek (buffer.file, packetlength-4, SEEK_CUR);
}
cmp3.info.duration = duration;
printf ("song duration: %f\n", duration);
return 0;
}
static int
cmp3_skip2 (float seconds) {
int nframe = 0;
int nskipped = 0;
float duration = 0;
for (;;) {
uint32_t hdr;
uint8_t sync;
if (fread (&sync, 1, 1, buffer.file) != 1) {
break; // eof
}
if (sync != 0xff) {
nskipped++;
continue; // not an mpeg frame
}
else {
// 2nd sync byte
if (fread (&sync, 1, 1, buffer.file) != 1) {
break; // eof
}
if ((sync >> 5) != 7) {
nskipped++;
continue;
}
}
// found frame
hdr = (0xff<<24) | (sync << 16);
// read 2 bytes more
if (fread (&sync, 1, 1, buffer.file) != 1) {
break; // eof
}
hdr |= sync << 8;
if (fread (&sync, 1, 1, buffer.file) != 1) {
break; // eof
}
hdr |= sync;
// printf ("header=%x, skipped %d\n", hdr, nskipped);
nskipped = 0;
// parse header
// sync bits
int usync = hdr & 0xffe00000;
// printf ("sync = %xh\n", usync);
if (usync != 0xffe00000) {
printf ("fatal error: mp3 header parser is broken\n");
// exit (0);
}
// layer info
int layer = (hdr & (3<<17)) >> 17;
// printf ("layer = %xh\n", layer);
// bitrate
int brtable[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1 };
int bitrate = (hdr & (0x0f<<12)) >> 12;
bitrate = brtable[bitrate];
// printf ("bitrate = %d\n", bitrate);
// samplerate
int srtable[4] = {44100, 48000, 32000, -1};
int samplerate = (hdr & (0x03<<10))>>10;
samplerate = srtable[samplerate];
// printf ("samplerate = %d\n", samplerate);
// padding
int padding = (hdr & (0x1 << 9)) >> 9;
// printf ("padding = %d\n", padding);
int chantbl[4] = { 2, 2, 2, 1 };
int nchannels = (hdr & (0x3 << 6)) >> 6;
nchannels = chantbl[nchannels];
// packetlength
int packetlength = 0;
bitrate *= 1000;
if (layer == 3) { // layer1
packetlength = (12 * bitrate / samplerate + padding) * 4;
}
else if (layer == 2) { // layer2
packetlength = 144 * bitrate / samplerate + padding;
}
else if (layer == 1) { // layer3
duration += (float)1152 / samplerate;
packetlength = 144 * bitrate / samplerate + padding;
}
if (duration > seconds) {
fseek (buffer.file, -4, SEEK_SET);
// set decoder timer
timer.seconds = (int)duration;
timer.fraction = (int)((duration - (float)timer.seconds)*MAD_TIMER_RESOLUTION);
return 0;
}
nframe++;
fseek (buffer.file, packetlength-4, SEEK_CUR);
}
return -1;
}
static int
cmp3_decode (void) {
for (;;) {
// read more MPEG data if needed
if(stream.buffer==NULL || stream.error==MAD_ERROR_BUFLEN) {
// copy part of last frame to beginning
if (stream.next_frame != NULL) {
buffer.remaining = stream.bufend - stream.next_frame;
memmove (buffer.input, stream.next_frame, buffer.remaining);
}
int size = READBUFFER - buffer.remaining;
int bytesread = 0;
char *bytes = buffer.input + buffer.remaining;
if (!endoffile) {
bytesread = fread (bytes, 1, size, buffer.file);
if (bytesread < size) {
// end of file
endoffile = 1;
size -= bytesread;
bytes += bytesread;
}
}
bytesread += buffer.remaining;
if (bytesread) {
mad_stream_buffer(&stream,buffer.input,bytesread);
}
else {
return -1;
}
stream.error=0;
}
// decode next frame
if(mad_frame_decode(&frame,&stream))
{
if(MAD_RECOVERABLE(stream.error))
{
if(stream.error!=MAD_ERROR_LOSTSYNC)
{
fprintf(stderr,"recoverable frame level error (%s)\n",
MadErrorString(&stream));
fflush(stderr);
}
continue;
}
else {
if(stream.error==MAD_ERROR_BUFLEN)
continue;
else
{
fprintf(stderr,"unrecoverable frame level error (%s).\n",
MadErrorString(&stream));
break;
}
}
}
mad_timer_add(&timer,frame.header.duration);
mad_synth_frame(&synth,&frame);
char *cache = &buffer.cache[buffer.cachefill];
int i;
for(i=0;i<synth.pcm.length;i++)
{
if (buffer.readsize > 0) {
*((int16_t*)buffer.output) = MadFixedToSshort(synth.pcm.samples[0][i]);
buffer.output+=2;
buffer.readsize-=2;
if(MAD_NCHANNELS(&frame.header)==2) {
*((int16_t*)buffer.output) = MadFixedToSshort(synth.pcm.samples[1][i]);
buffer.output+=2;
buffer.readsize-=2;
}
}
else if (buffer.cachefill < CACHESIZE) {
assert (buffer.cachefill < CACHESIZE-2);
*((int16_t*)cache) = MadFixedToSshort(synth.pcm.samples[0][i]);
cache+=2;
buffer.cachefill+=2;
if (MAD_NCHANNELS(&frame.header) == 2) {
assert (buffer.cachefill < CACHESIZE-2);
*((int16_t*)cache) = MadFixedToSshort(synth.pcm.samples[1][i]);
cache+=2;
buffer.cachefill+=2;
}
}
else {
printf ("cache overflow!\n");
break;
}
}
if (buffer.readsize == 0) {
break;
}
if (buffer.readsize > 0 && endoffile) {
// fill rest with zeroes, and return -1
memset (buffer.output, 0, buffer.readsize);
return -1;
}
}
cmp3.info.position = (float)timer.seconds + (float)timer.fraction / MAD_TIMER_RESOLUTION;
return 0;
}
void
cmp3_free (void) {
if (buffer.file) {
fclose (buffer.file);
buffer.file = NULL;
mad_synth_finish (&synth);
mad_frame_finish (&frame);
mad_stream_finish (&stream);
}
}
int
cmp3_read (char *bytes, int size) {
int result;
int totalread = 0;
int ret = 0;
if (buffer.cachefill > 0) {
int sz = min (size, buffer.cachefill);
memcpy (bytes, buffer.cache, sz);
bytes += sz;
size -= sz;
totalread += sz;
if (buffer.cachefill > sz) {
memmove (buffer.cache, &buffer.cache[sz], buffer.cachefill-sz);
buffer.cachefill -= sz;
}
else {
buffer.cachefill = 0;
}
}
if (size > 0) {
buffer.output = bytes;
buffer.readsize = size;
ret = cmp3_decode ();
totalread += size;
}
return ret;
}
int
cmp3_seek (float time) {
if (!buffer.file) {
return -1;
}
// restart file, and load until we hit required pos
mad_synth_finish (&synth);
mad_frame_finish (&frame);
mad_stream_finish (&stream);
endoffile = 0;
fseek(buffer.file, 0, SEEK_SET);
mad_stream_init(&stream);
mad_frame_init(&frame);
mad_synth_init(&synth);
mad_timer_reset(&timer);
if (cmp3_skip2 (time) == -1) {
return -1;
}
return 0;
}
int
cmp3_add (const char *fname) {
return 0;
}
codec_t cmp3 = {
.init = cmp3_init,
.free = cmp3_free,
.read = cmp3_read,
.seek = cmp3_seek
};
|