00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 # ifdef HAVE_CONFIG_H
00023 # include "config.h"
00024 # endif
00025
00026 # include "global.h"
00027
00028 # include <stdlib.h>
00029
00030 # include "bit.h"
00031 # include "stream.h"
00032 # include "frame.h"
00033 # include "timer.h"
00034 # include "layer12.h"
00035 # include "layer3.h"
00036
00037 static
00038 unsigned long const bitrate_table[5][15] = {
00039
00040 { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000,
00041 256000, 288000, 320000, 352000, 384000, 416000, 448000 },
00042 { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000,
00043 128000, 160000, 192000, 224000, 256000, 320000, 384000 },
00044 { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000,
00045 112000, 128000, 160000, 192000, 224000, 256000, 320000 },
00046
00047
00048 { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000,
00049 128000, 144000, 160000, 176000, 192000, 224000, 256000 },
00050 { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000,
00051 64000, 80000, 96000, 112000, 128000, 144000, 160000 }
00052 };
00053
00054 static
00055 unsigned int const samplerate_table[3] = { 44100, 48000, 32000 };
00056
00057 static
00058 int (*const decoder_table[3])(struct mad_stream *, struct mad_frame *) = {
00059 mad_layer_I,
00060 mad_layer_II,
00061 mad_layer_III
00062 };
00063
00064
00065
00066
00067
00068 void mad_header_init(struct mad_header *header)
00069 {
00070 header->layer = 0;
00071 header->mode = 0;
00072 header->mode_extension = 0;
00073 header->emphasis = 0;
00074
00075 header->bitrate = 0;
00076 header->samplerate = 0;
00077
00078 header->crc_check = 0;
00079 header->crc_target = 0;
00080
00081 header->flags = 0;
00082 header->private_bits = 0;
00083
00084 header->duration = mad_timer_zero;
00085 }
00086
00087
00088
00089
00090
00091 void mad_frame_init(struct mad_frame *frame)
00092 {
00093 mad_header_init(&frame->header);
00094
00095 frame->options = 0;
00096
00097 frame->overlap = 0;
00098 mad_frame_mute(frame);
00099 }
00100
00101
00102
00103
00104
00105 void mad_frame_finish(struct mad_frame *frame)
00106 {
00107 mad_header_finish(&frame->header);
00108
00109 if (frame->overlap) {
00110 free(frame->overlap);
00111 frame->overlap = 0;
00112 }
00113 }
00114
00115
00116
00117
00118
00119 static
00120 int decode_header(struct mad_header *header, struct mad_stream *stream)
00121 {
00122 unsigned int index;
00123
00124 header->flags = 0;
00125 header->private_bits = 0;
00126
00127
00128
00129
00130 mad_bit_skip(&stream->ptr, 11);
00131
00132
00133 if (mad_bit_read(&stream->ptr, 1) == 0)
00134 header->flags |= MAD_FLAG_MPEG_2_5_EXT;
00135
00136
00137 if (mad_bit_read(&stream->ptr, 1) == 0)
00138 header->flags |= MAD_FLAG_LSF_EXT;
00139 else if (header->flags & MAD_FLAG_MPEG_2_5_EXT) {
00140 stream->error = MAD_ERROR_LOSTSYNC;
00141 return -1;
00142 }
00143
00144
00145 header->layer = 4 - mad_bit_read(&stream->ptr, 2);
00146
00147 if (header->layer == 4) {
00148 stream->error = MAD_ERROR_BADLAYER;
00149 return -1;
00150 }
00151
00152
00153 if (mad_bit_read(&stream->ptr, 1) == 0) {
00154 header->flags |= MAD_FLAG_PROTECTION;
00155 header->crc_check = mad_bit_crc(stream->ptr, 16, 0xffff);
00156 }
00157
00158
00159 index = mad_bit_read(&stream->ptr, 4);
00160
00161 if (index == 15) {
00162 stream->error = MAD_ERROR_BADBITRATE;
00163 return -1;
00164 }
00165
00166 if (header->flags & MAD_FLAG_LSF_EXT)
00167 header->bitrate = bitrate_table[3 + (header->layer >> 1)][index];
00168 else
00169 header->bitrate = bitrate_table[header->layer - 1][index];
00170
00171
00172 index = mad_bit_read(&stream->ptr, 2);
00173
00174 if (index == 3) {
00175 stream->error = MAD_ERROR_BADSAMPLERATE;
00176 return -1;
00177 }
00178
00179 header->samplerate = samplerate_table[index];
00180
00181 if (header->flags & MAD_FLAG_LSF_EXT) {
00182 header->samplerate /= 2;
00183
00184 if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
00185 header->samplerate /= 2;
00186 }
00187
00188
00189 if (mad_bit_read(&stream->ptr, 1))
00190 header->flags |= MAD_FLAG_PADDING;
00191
00192
00193 if (mad_bit_read(&stream->ptr, 1))
00194 header->private_bits |= MAD_PRIVATE_HEADER;
00195
00196
00197 header->mode = 3 - mad_bit_read(&stream->ptr, 2);
00198
00199
00200 header->mode_extension = mad_bit_read(&stream->ptr, 2);
00201
00202
00203 if (mad_bit_read(&stream->ptr, 1))
00204 header->flags |= MAD_FLAG_COPYRIGHT;
00205
00206
00207 if (mad_bit_read(&stream->ptr, 1))
00208 header->flags |= MAD_FLAG_ORIGINAL;
00209
00210
00211 header->emphasis = mad_bit_read(&stream->ptr, 2);
00212
00213 # if defined(OPT_STRICT)
00214
00215
00216
00217
00218
00219 if (header->emphasis == MAD_EMPHASIS_RESERVED) {
00220 stream->error = MAD_ERROR_BADEMPHASIS;
00221 return -1;
00222 }
00223 # endif
00224
00225
00226
00227
00228 if (header->flags & MAD_FLAG_PROTECTION)
00229 header->crc_target = mad_bit_read(&stream->ptr, 16);
00230
00231 return 0;
00232 }
00233
00234
00235
00236
00237
00238 static
00239 int free_bitrate(struct mad_stream *stream, struct mad_header const *header)
00240 {
00241 struct mad_bitptr keep_ptr;
00242 unsigned long rate = 0;
00243 unsigned int pad_slot, slots_per_frame;
00244 unsigned char const *ptr = 0;
00245
00246 keep_ptr = stream->ptr;
00247
00248 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
00249 slots_per_frame = (header->layer == MAD_LAYER_III &&
00250 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
00251
00252 while (mad_stream_sync(stream) == 0) {
00253 struct mad_stream peek_stream;
00254 struct mad_header peek_header;
00255
00256 peek_stream = *stream;
00257 peek_header = *header;
00258
00259 if (decode_header(&peek_header, &peek_stream) == 0 &&
00260 peek_header.layer == header->layer &&
00261 peek_header.samplerate == header->samplerate) {
00262 unsigned int N;
00263
00264 ptr = mad_bit_nextbyte(&stream->ptr);
00265
00266 N = ptr - stream->this_frame;
00267
00268 if (header->layer == MAD_LAYER_I) {
00269 rate = (unsigned long) header->samplerate *
00270 (N - 4 * pad_slot + 4) / 48 / 1000;
00271 }
00272 else {
00273 rate = (unsigned long) header->samplerate *
00274 (N - pad_slot + 1) / slots_per_frame / 1000;
00275 }
00276
00277 if (rate >= 8)
00278 break;
00279 }
00280
00281 mad_bit_skip(&stream->ptr, 8);
00282 }
00283
00284 stream->ptr = keep_ptr;
00285
00286 if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) {
00287 stream->error = MAD_ERROR_LOSTSYNC;
00288 return -1;
00289 }
00290
00291 stream->freerate = rate * 1000;
00292
00293 return 0;
00294 }
00295
00296
00297
00298
00299
00300 int mad_header_decode(struct mad_header *header, struct mad_stream *stream)
00301 {
00302 register unsigned char const *ptr, *end;
00303 unsigned int pad_slot, N;
00304
00305 ptr = stream->next_frame;
00306 end = stream->bufend;
00307
00308 if (ptr == 0) {
00309 stream->error = MAD_ERROR_BUFPTR;
00310 goto fail;
00311 }
00312
00313
00314 if (stream->skiplen) {
00315 if (!stream->sync)
00316 ptr = stream->this_frame;
00317
00318 if (end - ptr < stream->skiplen) {
00319 stream->skiplen -= end - ptr;
00320 stream->next_frame = end;
00321
00322 stream->error = MAD_ERROR_BUFLEN;
00323 goto fail;
00324 }
00325
00326 ptr += stream->skiplen;
00327 stream->skiplen = 0;
00328
00329 stream->sync = 1;
00330 }
00331
00332 sync:
00333
00334 if (stream->sync) {
00335 if (end - ptr < MAD_BUFFER_GUARD) {
00336 stream->next_frame = ptr;
00337
00338 stream->error = MAD_ERROR_BUFLEN;
00339 goto fail;
00340 }
00341 else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
00342
00343 stream->this_frame = ptr;
00344 stream->next_frame = ptr + 1;
00345
00346 stream->error = MAD_ERROR_LOSTSYNC;
00347 goto fail;
00348 }
00349 }
00350 else {
00351 mad_bit_init(&stream->ptr, ptr);
00352
00353 if (mad_stream_sync(stream) == -1) {
00354 if (end - stream->next_frame >= MAD_BUFFER_GUARD)
00355 stream->next_frame = end - MAD_BUFFER_GUARD;
00356
00357 stream->error = MAD_ERROR_BUFLEN;
00358 goto fail;
00359 }
00360
00361 ptr = mad_bit_nextbyte(&stream->ptr);
00362 }
00363
00364
00365 stream->this_frame = ptr;
00366 stream->next_frame = ptr + 1;
00367
00368 mad_bit_init(&stream->ptr, stream->this_frame);
00369
00370 if (decode_header(header, stream) == -1)
00371 goto fail;
00372
00373
00374 mad_timer_set(&header->duration, 0,
00375 32 * MAD_NSBSAMPLES(header), header->samplerate);
00376
00377
00378 if (header->bitrate == 0) {
00379 if ((stream->freerate == 0 || !stream->sync) &&
00380 free_bitrate(stream, header) == -1)
00381 goto fail;
00382
00383 header->bitrate = stream->freerate;
00384 header->flags |= MAD_FLAG_FREEFORMAT;
00385 }
00386
00387
00388 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
00389
00390 if (header->layer == MAD_LAYER_I)
00391 N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4;
00392 else {
00393 unsigned int slots_per_frame;
00394
00395 slots_per_frame = (header->layer == MAD_LAYER_III &&
00396 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
00397
00398 N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot;
00399 }
00400
00401
00402 if (N + MAD_BUFFER_GUARD > end - stream->this_frame) {
00403 stream->next_frame = stream->this_frame;
00404
00405 stream->error = MAD_ERROR_BUFLEN;
00406 goto fail;
00407 }
00408
00409 stream->next_frame = stream->this_frame + N;
00410
00411 if (!stream->sync) {
00412
00413
00414 ptr = stream->next_frame;
00415 if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
00416 ptr = stream->next_frame = stream->this_frame + 1;
00417 goto sync;
00418 }
00419
00420 stream->sync = 1;
00421 }
00422
00423 header->flags |= MAD_FLAG_INCOMPLETE;
00424
00425 return 0;
00426
00427 fail:
00428 stream->sync = 0;
00429
00430 return -1;
00431 }
00432
00433
00434
00435
00436
00437 int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream)
00438 {
00439 frame->options = stream->options;
00440
00441
00442
00443
00444 if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) &&
00445 mad_header_decode(&frame->header, stream) == -1)
00446 goto fail;
00447
00448
00449
00450 frame->header.flags &= ~MAD_FLAG_INCOMPLETE;
00451
00452 if (decoder_table[frame->header.layer - 1](stream, frame) == -1) {
00453 if (!MAD_RECOVERABLE(stream->error))
00454 stream->next_frame = stream->this_frame;
00455
00456 goto fail;
00457 }
00458
00459
00460
00461 if (frame->header.layer != MAD_LAYER_III) {
00462 struct mad_bitptr next_frame;
00463
00464 mad_bit_init(&next_frame, stream->next_frame);
00465
00466 stream->anc_ptr = stream->ptr;
00467 stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame);
00468
00469 mad_bit_finish(&next_frame);
00470 }
00471
00472 return 0;
00473
00474 fail:
00475 stream->anc_bitlen = 0;
00476 return -1;
00477 }
00478
00479
00480
00481
00482
00483 void mad_frame_mute(struct mad_frame *frame)
00484 {
00485 unsigned int s, sb;
00486
00487 for (s = 0; s < 36; ++s) {
00488 for (sb = 0; sb < 32; ++sb) {
00489 frame->sbsample[0][s][sb] =
00490 frame->sbsample[1][s][sb] = 0;
00491 }
00492 }
00493
00494 if (frame->overlap) {
00495 for (s = 0; s < 18; ++s) {
00496 for (sb = 0; sb < 32; ++sb) {
00497 (*frame->overlap)[0][sb][s] =
00498 (*frame->overlap)[1][sb][s] = 0;
00499 }
00500 }
00501 }
00502 }