| 1 | /* |
| 2 | * libmad - MPEG audio decoder library |
| 3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | * $Id: frame.c,v 1.29 2004/02/04 22:59:19 rob Exp $ |
| 20 | */ |
| 21 | |
| 22 | /*# ifdef HAVE_CONFIG_H*/ |
| 23 | # include "mad_config.h" |
| 24 | /*# endif*/ |
| 25 | |
| 26 | # include "mad_global.h" |
| 27 | |
| 28 | # include <stdlib.h> |
| 29 | |
| 30 | # include "mad_bit.h" |
| 31 | # include "mad_stream.h" |
| 32 | # include "mad_frame.h" |
| 33 | # include "mad_timer.h" |
| 34 | # include "mad_layer12.h" |
| 35 | # include "mad_layer3.h" |
| 36 | |
| 37 | static |
| 38 | unsigned long const bitrate_table[5][15] = { |
| 39 | /* MPEG-1 */ |
| 40 | { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */ |
| 41 | 256000, 288000, 320000, 352000, 384000, 416000, 448000 }, |
| 42 | { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */ |
| 43 | 128000, 160000, 192000, 224000, 256000, 320000, 384000 }, |
| 44 | { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */ |
| 45 | 112000, 128000, 160000, 192000, 224000, 256000, 320000 }, |
| 46 | |
| 47 | /* MPEG-2 LSF */ |
| 48 | { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */ |
| 49 | 128000, 144000, 160000, 176000, 192000, 224000, 256000 }, |
| 50 | { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */ |
| 51 | 64000, 80000, 96000, 112000, 128000, 144000, 160000 } /* II & III */ |
| 52 | }; |
| 53 | |
| 54 | static |
| 55 | unsigned int const samplerate_table[3] = { 44100, 48000, 32000 }; |
| 56 | |
| 57 | static |
| 58 | int (*const decoder_table[3])(struct mad_stream *, struct mad_frame *) = { |
| 59 | mad_layer_I, |
| 60 | mad_layer_II, |
| 61 | mad_layer_III |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * NAME: header->init() |
| 66 | * DESCRIPTION: initialize header struct |
| 67 | */ |
| 68 | void mad_header_init(struct mad_header *header) |
| 69 | { |
| 70 | header->layer = 0; |
| 71 | header->mode = 0; |
| 72 | header->mode_extension = 0; |
| 73 | header->emphasis = 0; |
| 74 | |
| 75 | header->bitrate = 0; |
| 76 | header->samplerate = 0; |
| 77 | |
| 78 | header->crc_check = 0; |
| 79 | header->crc_target = 0; |
| 80 | |
| 81 | header->flags = 0; |
| 82 | header->private_bits = 0; |
| 83 | |
| 84 | header->duration = mad_timer_zero; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * NAME: frame->init() |
| 89 | * DESCRIPTION: initialize frame struct |
| 90 | */ |
| 91 | void mad_frame_init(struct mad_frame *frame) |
| 92 | { |
| 93 | mad_header_init(&frame->header); |
| 94 | |
| 95 | frame->options = 0; |
| 96 | |
| 97 | frame->overlap = 0; |
| 98 | mad_frame_mute(frame); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * NAME: frame->finish() |
| 103 | * DESCRIPTION: deallocate any dynamic memory associated with frame |
| 104 | */ |
| 105 | void mad_frame_finish(struct mad_frame *frame) |
| 106 | { |
| 107 | mad_header_finish(&frame->header); |
| 108 | |
| 109 | if (frame->overlap) { |
| 110 | free(frame->overlap); |
| 111 | frame->overlap = 0; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * NAME: decode_header() |
| 117 | * DESCRIPTION: read header data and following CRC word |
| 118 | */ |
| 119 | static |
| 120 | int decode_header(struct mad_header *header, struct mad_stream *stream) |
| 121 | { |
| 122 | unsigned int index; |
| 123 | |
| 124 | header->offset = stream->this_offset; |
| 125 | header->flags = 0; |
| 126 | header->private_bits = 0; |
| 127 | |
| 128 | /* header() */ |
| 129 | |
| 130 | /* syncword */ |
| 131 | mad_bit_skip(&stream->ptr, 11); |
| 132 | |
| 133 | /* MPEG 2.5 indicator (really part of syncword) */ |
| 134 | if (mad_bit_read(&stream->ptr, 1) == 0) |
| 135 | header->flags |= MAD_FLAG_MPEG_2_5_EXT; |
| 136 | |
| 137 | /* ID */ |
| 138 | if (mad_bit_read(&stream->ptr, 1) == 0) |
| 139 | header->flags |= MAD_FLAG_LSF_EXT; |
| 140 | else if (header->flags & MAD_FLAG_MPEG_2_5_EXT) { |
| 141 | stream->error = MAD_ERROR_LOSTSYNC; |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | /* layer */ |
| 146 | header->layer = 4 - mad_bit_read(&stream->ptr, 2); |
| 147 | |
| 148 | if (header->layer == 4) { |
| 149 | stream->error = MAD_ERROR_BADLAYER; |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | /* protection_bit */ |
| 154 | if (mad_bit_read(&stream->ptr, 1) == 0) { |
| 155 | header->flags |= MAD_FLAG_PROTECTION; |
| 156 | header->crc_check = mad_bit_crc(stream->ptr, 16, 0xffff); |
| 157 | } |
| 158 | |
| 159 | /* bitrate_index */ |
| 160 | index = mad_bit_read(&stream->ptr, 4); |
| 161 | |
| 162 | if (index == 15) { |
| 163 | stream->error = MAD_ERROR_BADBITRATE; |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | if (header->flags & MAD_FLAG_LSF_EXT) |
| 168 | header->bitrate = bitrate_table[3 + (header->layer >> 1)][index]; |
| 169 | else |
| 170 | header->bitrate = bitrate_table[header->layer - 1][index]; |
| 171 | |
| 172 | /* sampling_frequency */ |
| 173 | index = mad_bit_read(&stream->ptr, 2); |
| 174 | |
| 175 | if (index == 3) { |
| 176 | stream->error = MAD_ERROR_BADSAMPLERATE; |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | header->samplerate = samplerate_table[index]; |
| 181 | |
| 182 | if (header->flags & MAD_FLAG_LSF_EXT) { |
| 183 | header->samplerate /= 2; |
| 184 | |
| 185 | if (header->flags & MAD_FLAG_MPEG_2_5_EXT) |
| 186 | header->samplerate /= 2; |
| 187 | } |
| 188 | |
| 189 | /* padding_bit */ |
| 190 | if (mad_bit_read(&stream->ptr, 1)) |
| 191 | header->flags |= MAD_FLAG_PADDING; |
| 192 | |
| 193 | /* private_bit */ |
| 194 | if (mad_bit_read(&stream->ptr, 1)) |
| 195 | header->private_bits |= MAD_PRIVATE_HEADER; |
| 196 | |
| 197 | /* mode */ |
| 198 | header->mode = 3 - mad_bit_read(&stream->ptr, 2); |
| 199 | |
| 200 | /* mode_extension */ |
| 201 | header->mode_extension = mad_bit_read(&stream->ptr, 2); |
| 202 | |
| 203 | /* copyright */ |
| 204 | if (mad_bit_read(&stream->ptr, 1)) |
| 205 | header->flags |= MAD_FLAG_COPYRIGHT; |
| 206 | |
| 207 | /* original/copy */ |
| 208 | if (mad_bit_read(&stream->ptr, 1)) |
| 209 | header->flags |= MAD_FLAG_ORIGINAL; |
| 210 | |
| 211 | /* emphasis */ |
| 212 | header->emphasis = mad_bit_read(&stream->ptr, 2); |
| 213 | |
| 214 | # if defined(OPT_STRICT) |
| 215 | /* |
| 216 | * ISO/IEC 11172-3 says this is a reserved emphasis value, but |
| 217 | * streams exist which use it anyway. Since the value is not important |
| 218 | * to the decoder proper, we allow it unless OPT_STRICT is defined. |
| 219 | */ |
| 220 | if (header->emphasis == MAD_EMPHASIS_RESERVED) { |
| 221 | stream->error = MAD_ERROR_BADEMPHASIS; |
| 222 | return -1; |
| 223 | } |
| 224 | # endif |
| 225 | |
| 226 | /* error_check() */ |
| 227 | |
| 228 | /* crc_check */ |
| 229 | if (header->flags & MAD_FLAG_PROTECTION) |
| 230 | header->crc_target = mad_bit_read(&stream->ptr, 16); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * NAME: free_bitrate() |
| 237 | * DESCRIPTION: attempt to discover the bitstream's free bitrate |
| 238 | */ |
| 239 | static |
| 240 | int free_bitrate(struct mad_stream *stream, struct mad_header const *header) |
| 241 | { |
| 242 | struct mad_bitptr keep_ptr; |
| 243 | unsigned long rate = 0; |
| 244 | unsigned int pad_slot, slots_per_frame; |
| 245 | unsigned char const *ptr = 0; |
| 246 | |
| 247 | keep_ptr = stream->ptr; |
| 248 | |
| 249 | pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0; |
| 250 | slots_per_frame = (header->layer == MAD_LAYER_III && |
| 251 | (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144; |
| 252 | |
| 253 | while (mad_stream_sync(stream) == 0) { |
| 254 | struct mad_stream peek_stream; |
| 255 | struct mad_header peek_header; |
| 256 | |
| 257 | peek_stream = *stream; |
| 258 | peek_header = *header; |
| 259 | |
| 260 | if (decode_header(&peek_header, &peek_stream) == 0 && |
| 261 | peek_header.layer == header->layer && |
| 262 | peek_header.samplerate == header->samplerate) { |
| 263 | unsigned int N; |
| 264 | |
| 265 | ptr = mad_bit_nextbyte(&stream->ptr); |
| 266 | |
| 267 | N = ptr - stream->this_frame; |
| 268 | |
| 269 | if (header->layer == MAD_LAYER_I) { |
| 270 | rate = (unsigned long) header->samplerate * |
| 271 | (N - 4 * pad_slot + 4) / 48 / 1000; |
| 272 | } |
| 273 | else { |
| 274 | rate = (unsigned long) header->samplerate * |
| 275 | (N - pad_slot + 1) / slots_per_frame / 1000; |
| 276 | } |
| 277 | |
| 278 | if (rate >= 8) |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | mad_bit_skip(&stream->ptr, 8); |
| 283 | } |
| 284 | |
| 285 | stream->ptr = keep_ptr; |
| 286 | |
| 287 | if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) { |
| 288 | stream->error = MAD_ERROR_LOSTSYNC; |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | stream->freerate = rate * 1000; |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * NAME: header->decode() |
| 299 | * DESCRIPTION: read the next frame header from the stream |
| 300 | */ |
| 301 | int mad_header_decode(struct mad_header *header, struct mad_stream *stream) |
| 302 | { |
| 303 | register unsigned char const *ptr, *end; |
| 304 | unsigned int pad_slot, N; |
| 305 | |
| 306 | ptr = stream->next_frame; |
| 307 | end = stream->bufend; |
| 308 | |
| 309 | if (ptr == 0) { |
| 310 | stream->error = MAD_ERROR_BUFPTR; |
| 311 | goto fail; |
| 312 | } |
| 313 | |
| 314 | /* stream skip */ |
| 315 | if (stream->skiplen) { |
| 316 | if (!stream->sync) |
| 317 | ptr = stream->this_frame; |
| 318 | |
| 319 | if (end - ptr < stream->skiplen) { |
| 320 | stream->skiplen -= end - ptr; |
| 321 | stream->next_frame = end; |
| 322 | |
| 323 | stream->error = MAD_ERROR_BUFLEN; |
| 324 | goto fail; |
| 325 | } |
| 326 | |
| 327 | ptr += stream->skiplen; |
| 328 | stream->skiplen = 0; |
| 329 | |
| 330 | stream->sync = 1; |
| 331 | } |
| 332 | |
| 333 | sync: |
| 334 | /* synchronize */ |
| 335 | if (stream->sync) { |
| 336 | if (end - ptr < MAD_BUFFER_GUARD) { |
| 337 | stream->next_frame = ptr; |
| 338 | |
| 339 | stream->error = MAD_ERROR_BUFLEN; |
| 340 | goto fail; |
| 341 | } |
| 342 | else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) { |
| 343 | /* mark point where frame sync word was expected */ |
| 344 | stream->this_offset += (ptr - stream->this_frame); /* Erez Volk */ |
| 345 | stream->this_frame = ptr; |
| 346 | stream->next_frame = ptr + 1; |
| 347 | |
| 348 | stream->error = MAD_ERROR_LOSTSYNC; |
| 349 | goto fail; |
| 350 | } |
| 351 | } |
| 352 | else { |
| 353 | mad_bit_init(&stream->ptr, ptr); |
| 354 | |
| 355 | if (mad_stream_sync(stream) == -1) { |
| 356 | if (end - stream->next_frame >= MAD_BUFFER_GUARD) |
| 357 | stream->next_frame = end - MAD_BUFFER_GUARD; |
| 358 | |
| 359 | stream->error = MAD_ERROR_BUFLEN; |
| 360 | goto fail; |
| 361 | } |
| 362 | |
| 363 | ptr = mad_bit_nextbyte(&stream->ptr); |
| 364 | } |
| 365 | |
| 366 | /* begin processing */ |
| 367 | stream->this_offset += (ptr - stream->this_frame); /* Erez Volk */ |
| 368 | stream->this_frame = ptr; |
| 369 | stream->next_frame = ptr + 1; /* possibly bogus sync word */ |
| 370 | |
| 371 | mad_bit_init(&stream->ptr, stream->this_frame); |
| 372 | |
| 373 | if (decode_header(header, stream) == -1) |
| 374 | goto fail; |
| 375 | |
| 376 | /* calculate frame duration */ |
| 377 | mad_timer_set(&header->duration, 0, |
| 378 | 32 * MAD_NSBSAMPLES(header), header->samplerate); |
| 379 | |
| 380 | /* calculate free bit rate */ |
| 381 | if (header->bitrate == 0) { |
| 382 | if ((stream->freerate == 0 || !stream->sync || |
| 383 | (header->layer == MAD_LAYER_III && stream->freerate > 640000)) && |
| 384 | free_bitrate(stream, header) == -1) |
| 385 | goto fail; |
| 386 | |
| 387 | header->bitrate = stream->freerate; |
| 388 | header->flags |= MAD_FLAG_FREEFORMAT; |
| 389 | } |
| 390 | |
| 391 | /* calculate beginning of next frame */ |
| 392 | pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0; |
| 393 | |
| 394 | if (header->layer == MAD_LAYER_I) |
| 395 | N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4; |
| 396 | else { |
| 397 | unsigned int slots_per_frame; |
| 398 | |
| 399 | slots_per_frame = (header->layer == MAD_LAYER_III && |
| 400 | (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144; |
| 401 | |
| 402 | N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot; |
| 403 | } |
| 404 | |
| 405 | /* verify there is enough data left in buffer to decode this frame */ |
| 406 | if (N + MAD_BUFFER_GUARD > end - stream->this_frame) { |
| 407 | stream->next_frame = stream->this_frame; |
| 408 | |
| 409 | stream->error = MAD_ERROR_BUFLEN; |
| 410 | goto fail; |
| 411 | } |
| 412 | |
| 413 | stream->next_frame = stream->this_frame + N; |
| 414 | |
| 415 | if (!stream->sync) { |
| 416 | /* check that a valid frame header follows this frame */ |
| 417 | |
| 418 | ptr = stream->next_frame; |
| 419 | if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) { |
| 420 | ptr = stream->next_frame = stream->this_frame + 1; |
| 421 | goto sync; |
| 422 | } |
| 423 | |
| 424 | stream->sync = 1; |
| 425 | } |
| 426 | |
| 427 | header->flags |= MAD_FLAG_INCOMPLETE; |
| 428 | |
| 429 | return 0; |
| 430 | |
| 431 | fail: |
| 432 | stream->sync = 0; |
| 433 | |
| 434 | return -1; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * NAME: frame->decode() |
| 439 | * DESCRIPTION: decode a single frame from a bitstream |
| 440 | */ |
| 441 | int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream) |
| 442 | { |
| 443 | frame->options = stream->options; |
| 444 | |
| 445 | /* header() */ |
| 446 | /* error_check() */ |
| 447 | |
| 448 | if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) && |
| 449 | mad_header_decode(&frame->header, stream) == -1) |
| 450 | goto fail; |
| 451 | |
| 452 | /* audio_data() */ |
| 453 | |
| 454 | frame->header.flags &= ~MAD_FLAG_INCOMPLETE; |
| 455 | |
| 456 | if (decoder_table[frame->header.layer - 1](stream, frame) == -1) { |
| 457 | if (!MAD_RECOVERABLE(stream->error)) |
| 458 | stream->next_frame = stream->this_frame; |
| 459 | |
| 460 | goto fail; |
| 461 | } |
| 462 | |
| 463 | /* ancillary_data() */ |
| 464 | |
| 465 | if (frame->header.layer != MAD_LAYER_III) { |
| 466 | struct mad_bitptr next_frame; |
| 467 | |
| 468 | mad_bit_init(&next_frame, stream->next_frame); |
| 469 | |
| 470 | stream->anc_ptr = stream->ptr; |
| 471 | stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame); |
| 472 | |
| 473 | mad_bit_finish(&next_frame); |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | |
| 478 | fail: |
| 479 | stream->anc_bitlen = 0; |
| 480 | return -1; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * NAME: frame->mute() |
| 485 | * DESCRIPTION: zero all subband values so the frame becomes silent |
| 486 | */ |
| 487 | void mad_frame_mute(struct mad_frame *frame) |
| 488 | { |
| 489 | unsigned int s, sb; |
| 490 | |
| 491 | for (s = 0; s < 36; ++s) { |
| 492 | for (sb = 0; sb < 32; ++sb) { |
| 493 | frame->sbsample[0][s][sb] = |
| 494 | frame->sbsample[1][s][sb] = 0; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (frame->overlap) { |
| 499 | for (s = 0; s < 18; ++s) { |
| 500 | for (sb = 0; sb < 32; ++sb) { |
| 501 | (*frame->overlap)[0][sb][s] = |
| 502 | (*frame->overlap)[1][sb][s] = 0; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | } |