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