annotate src/libmad-0.15.1b/frame.c @ 169:223a55898ab9 tip default

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