Forgot to remove wps_data.remove_wps in checkwps too.
[kugel-rb.git] / apps / codecs / libmad / frame.c
blob3fb971d3e11aebdeae1d6d3866e4a6aa1097469c
1 /*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
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.
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.
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
19 * $Id$
22 # ifdef HAVE_CONFIG_H
23 # include "config.h"
24 # endif
26 # include "global.h"
28 # include "bit.h"
29 # include "stream.h"
30 # include "frame.h"
31 # include "timer.h"
32 # include "layer12.h"
33 # include "layer3.h"
34 # include "codeclib.h"
36 static
37 unsigned long const bitrate_table[5][15] = {
38 /* MPEG-1 */
39 { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */
40 256000, 288000, 320000, 352000, 384000, 416000, 448000 },
41 { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */
42 128000, 160000, 192000, 224000, 256000, 320000, 384000 },
43 { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */
44 112000, 128000, 160000, 192000, 224000, 256000, 320000 },
46 /* MPEG-2 LSF */
47 { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */
48 128000, 144000, 160000, 176000, 192000, 224000, 256000 },
49 { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */
50 64000, 80000, 96000, 112000, 128000, 144000, 160000 } /* II & III */
53 static
54 unsigned int const samplerate_table[3] = { 44100, 48000, 32000 };
56 static
57 int (*const decoder_table[3])(struct mad_stream *, struct mad_frame *) = {
58 mad_layer_I,
59 mad_layer_II,
60 mad_layer_III
64 * NAME: header->init()
65 * DESCRIPTION: initialize header struct
67 void mad_header_init(struct mad_header *header)
69 header->layer = 0;
70 header->mode = 0;
71 header->mode_extension = 0;
72 header->emphasis = 0;
74 header->bitrate = 0;
75 header->samplerate = 0;
77 header->crc_check = 0;
78 header->crc_target = 0;
80 header->flags = 0;
81 header->private_bits = 0;
83 header->duration = mad_timer_zero;
87 * NAME: frame->init()
88 * DESCRIPTION: initialize frame struct
90 void mad_frame_init(struct mad_frame *frame)
92 mad_header_init(&frame->header);
94 frame->options = 0;
96 frame->overlap = 0;
97 mad_frame_mute(frame);
101 * NAME: frame->finish()
102 * DESCRIPTION: deallocate any dynamic memory associated with frame
104 void mad_frame_finish(struct mad_frame *frame)
106 mad_header_finish(&frame->header);
108 if (frame->overlap) {
109 free(frame->overlap);
110 frame->overlap = 0;
115 * NAME: decode_header()
116 * DESCRIPTION: read header data and following CRC word
118 static
119 int decode_header(struct mad_header *header, struct mad_stream *stream)
121 unsigned int index;
123 header->flags = 0;
124 header->private_bits = 0;
126 /* header() */
128 /* syncword */
129 mad_bit_skip(&stream->ptr, 11);
131 /* MPEG 2.5 indicator (really part of syncword) */
132 if (mad_bit_read(&stream->ptr, 1) == 0)
133 header->flags |= MAD_FLAG_MPEG_2_5_EXT;
135 /* ID */
136 if (mad_bit_read(&stream->ptr, 1) == 0)
137 header->flags |= MAD_FLAG_LSF_EXT;
138 else if (header->flags & MAD_FLAG_MPEG_2_5_EXT) {
139 stream->error = MAD_ERROR_LOSTSYNC;
140 return -1;
143 /* layer */
144 header->layer = 4 - mad_bit_read(&stream->ptr, 2);
146 if (header->layer == 4) {
147 stream->error = MAD_ERROR_BADLAYER;
148 return -1;
151 /* protection_bit */
152 if (mad_bit_read(&stream->ptr, 1) == 0) {
153 header->flags |= MAD_FLAG_PROTECTION;
154 header->crc_check = mad_bit_crc(stream->ptr, 16, 0xffff);
157 /* bitrate_index */
158 index = mad_bit_read(&stream->ptr, 4);
160 if (index == 15) {
161 stream->error = MAD_ERROR_BADBITRATE;
162 return -1;
165 if (header->flags & MAD_FLAG_LSF_EXT)
166 header->bitrate = bitrate_table[3 + (header->layer >> 1)][index];
167 else
168 header->bitrate = bitrate_table[header->layer - 1][index];
170 /* sampling_frequency */
171 index = mad_bit_read(&stream->ptr, 2);
173 if (index == 3) {
174 stream->error = MAD_ERROR_BADSAMPLERATE;
175 return -1;
178 header->samplerate = samplerate_table[index];
180 if (header->flags & MAD_FLAG_LSF_EXT) {
181 header->samplerate /= 2;
183 if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
184 header->samplerate /= 2;
187 /* padding_bit */
188 if (mad_bit_read(&stream->ptr, 1))
189 header->flags |= MAD_FLAG_PADDING;
191 /* private_bit */
192 if (mad_bit_read(&stream->ptr, 1))
193 header->private_bits |= MAD_PRIVATE_HEADER;
195 /* mode */
196 header->mode = 3 - mad_bit_read(&stream->ptr, 2);
198 /* mode_extension */
199 header->mode_extension = mad_bit_read(&stream->ptr, 2);
201 /* copyright */
202 if (mad_bit_read(&stream->ptr, 1))
203 header->flags |= MAD_FLAG_COPYRIGHT;
205 /* original/copy */
206 if (mad_bit_read(&stream->ptr, 1))
207 header->flags |= MAD_FLAG_ORIGINAL;
209 /* emphasis */
210 header->emphasis = mad_bit_read(&stream->ptr, 2);
212 # if defined(OPT_STRICT)
214 * ISO/IEC 11172-3 says this is a reserved emphasis value, but
215 * streams exist which use it anyway. Since the value is not important
216 * to the decoder proper, we allow it unless OPT_STRICT is defined.
218 if (header->emphasis == MAD_EMPHASIS_RESERVED) {
219 stream->error = MAD_ERROR_BADEMPHASIS;
220 return -1;
222 # endif
224 /* error_check() */
226 /* crc_check */
227 if (header->flags & MAD_FLAG_PROTECTION)
228 header->crc_target = mad_bit_read(&stream->ptr, 16);
230 return 0;
234 * NAME: free_bitrate()
235 * DESCRIPTION: attempt to discover the bitstream's free bitrate
237 static
238 int free_bitrate(struct mad_stream *stream, struct mad_header const *header)
240 struct mad_bitptr keep_ptr;
241 unsigned long rate = 0;
242 unsigned int pad_slot, slots_per_frame;
243 unsigned char const *ptr = 0;
245 keep_ptr = stream->ptr;
247 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
248 slots_per_frame = (header->layer == MAD_LAYER_III &&
249 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
251 while (mad_stream_sync(stream) == 0) {
252 struct mad_stream peek_stream;
253 struct mad_header peek_header;
255 peek_stream = *stream;
256 peek_header = *header;
258 if (decode_header(&peek_header, &peek_stream) == 0 &&
259 peek_header.layer == header->layer &&
260 peek_header.samplerate == header->samplerate) {
261 unsigned int N;
263 ptr = mad_bit_nextbyte(&stream->ptr);
265 N = ptr - stream->this_frame;
267 if (header->layer == MAD_LAYER_I) {
268 rate = (unsigned long) header->samplerate *
269 (N - 4 * pad_slot + 4) / 48 / 1000;
271 else {
272 rate = (unsigned long) header->samplerate *
273 (N - pad_slot + 1) / slots_per_frame / 1000;
276 if (rate >= 8)
277 break;
280 mad_bit_skip(&stream->ptr, 8);
283 stream->ptr = keep_ptr;
285 if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) {
286 stream->error = MAD_ERROR_LOSTSYNC;
287 return -1;
290 stream->freerate = rate * 1000;
292 return 0;
296 * NAME: header->decode()
297 * DESCRIPTION: read the next frame header from the stream
299 int mad_header_decode(struct mad_header *header, struct mad_stream *stream)
301 register unsigned char const *ptr, *end;
302 unsigned int pad_slot, N;
304 ptr = stream->next_frame;
305 end = stream->bufend;
307 if (ptr == 0) {
308 stream->error = MAD_ERROR_BUFPTR;
309 goto fail;
312 /* stream skip */
313 if (stream->skiplen) {
314 if (!stream->sync)
315 ptr = stream->this_frame;
317 if (end - ptr < (long) stream->skiplen) {
318 stream->skiplen -= end - ptr;
319 stream->next_frame = end;
321 stream->error = MAD_ERROR_BUFLEN;
322 goto fail;
325 ptr += stream->skiplen;
326 stream->skiplen = 0;
328 stream->sync = 1;
331 sync:
332 /* synchronize */
333 if (stream->sync) {
334 if (end - ptr < MAD_BUFFER_GUARD) {
335 stream->next_frame = ptr;
337 stream->error = MAD_ERROR_BUFLEN;
338 goto fail;
340 else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
341 /* mark point where frame sync word was expected */
342 stream->this_frame = ptr;
343 stream->next_frame = ptr + 1;
345 stream->error = MAD_ERROR_LOSTSYNC;
346 goto fail;
349 else {
350 mad_bit_init(&stream->ptr, ptr);
352 if (mad_stream_sync(stream) == -1) {
353 if (end - stream->next_frame >= MAD_BUFFER_GUARD)
354 stream->next_frame = end - MAD_BUFFER_GUARD;
356 stream->error = MAD_ERROR_BUFLEN;
357 goto fail;
360 ptr = mad_bit_nextbyte(&stream->ptr);
363 /* begin processing */
364 stream->this_frame = ptr;
365 stream->next_frame = ptr + 1; /* possibly bogus sync word */
367 mad_bit_init(&stream->ptr, stream->this_frame);
369 if (decode_header(header, stream) == -1)
370 goto fail;
372 /* calculate frame duration */
373 mad_timer_set(&header->duration, 0,
374 32 * MAD_NSBSAMPLES(header), header->samplerate);
376 /* calculate free bit rate */
377 if (header->bitrate == 0) {
378 if ((stream->freerate == 0 || !stream->sync ||
379 (header->layer == MAD_LAYER_III && stream->freerate > 640000)) &&
380 free_bitrate(stream, header) == -1)
381 goto fail;
383 header->bitrate = stream->freerate;
384 header->flags |= MAD_FLAG_FREEFORMAT;
387 /* calculate beginning of next frame */
388 pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
390 if (header->layer == MAD_LAYER_I)
391 N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4;
392 else {
393 unsigned int slots_per_frame;
395 slots_per_frame = (header->layer == MAD_LAYER_III &&
396 (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
398 N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot;
401 /* verify there is enough data left in buffer to decode this frame */
402 if ((long)(N + MAD_BUFFER_GUARD) > end - stream->this_frame) {
403 stream->next_frame = stream->this_frame;
405 stream->error = MAD_ERROR_BUFLEN;
406 goto fail;
409 stream->next_frame = stream->this_frame + N;
411 if (!stream->sync) {
412 /* check that a valid frame header follows this frame */
414 ptr = stream->next_frame;
415 if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
416 ptr = stream->next_frame = stream->this_frame + 1;
417 goto sync;
420 stream->sync = 1;
423 header->flags |= MAD_FLAG_INCOMPLETE;
425 return 0;
427 fail:
428 stream->sync = 0;
430 return -1;
434 * NAME: frame->decode()
435 * DESCRIPTION: decode a single frame from a bitstream
437 int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream)
439 frame->options = stream->options;
441 /* header() */
442 /* error_check() */
444 if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) &&
445 mad_header_decode(&frame->header, stream) == -1)
446 goto fail;
448 /* audio_data() */
450 frame->header.flags &= ~MAD_FLAG_INCOMPLETE;
452 if (decoder_table[frame->header.layer - 1](stream, frame) == -1) {
453 if (!MAD_RECOVERABLE(stream->error))
454 stream->next_frame = stream->this_frame;
456 goto fail;
459 /* ancillary_data() */
461 if (frame->header.layer != MAD_LAYER_III) {
462 struct mad_bitptr next_frame;
464 mad_bit_init(&next_frame, stream->next_frame);
466 stream->anc_ptr = stream->ptr;
467 stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame);
469 mad_bit_finish(&next_frame);
474 return 0;
476 fail:
477 stream->anc_bitlen = 0;
478 return -1;
482 * NAME: frame->mute()
483 * DESCRIPTION: zero all subband values so the frame becomes silent
485 void mad_frame_mute(struct mad_frame *frame)
487 unsigned int s, sb;
489 for (s = 0; s < 36; ++s) {
490 for (sb = 0; sb < 32; ++sb) {
491 (*frame->sbsample)[0][s][sb] =
492 (*frame->sbsample)[1][s][sb] = 0;
496 if (frame->overlap) {
497 for (s = 0; s < 18; ++s) {
498 for (sb = 0; sb < 32; ++sb) {
499 (*frame->overlap)[0][sb][s] =
500 (*frame->overlap)[1][sb][s] = 0;