rtoss - Blame information for rev 26

Subversion Repositories:
Rev:
Rev Author Line No. Line
14 roytam 1 /*
2  * libmad - MPEG audio decoder library
26 roytam 3  * Copyright (C) 2000-2004 Underbit Technologies, Inc.
14 roytam 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  *
26 roytam 19  * $Id: frame.c,v 1.29 2004/02/04 22:59:19 rob Exp $
14 roytam 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   { 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->flags        = 0;
125   header->private_bits = 0;
126  
127   /* header() */
128  
129   /* syncword */
130   mad_bit_skip(&stream->ptr, 11);
131  
132   /* MPEG 2.5 indicator (really part of syncword) */
133   if (mad_bit_read(&stream->ptr, 1) == 0)
134     header->flags |= MAD_FLAG_MPEG_2_5_EXT;
135  
136   /* ID */
137   if (mad_bit_read(&stream->ptr, 1) == 0)
138     header->flags |= MAD_FLAG_LSF_EXT;
139   else if (header->flags & MAD_FLAG_MPEG_2_5_EXT) {
140     stream->error = MAD_ERROR_LOSTSYNC;
141     return -1;
142   }
143  
144   /* layer */
145   header->layer = 4 - mad_bit_read(&stream->ptr, 2);
146  
147   if (header->layer == 4) {
148     stream->error = MAD_ERROR_BADLAYER;
149     return -1;
150   }
151  
152   /* protection_bit */
153   if (mad_bit_read(&stream->ptr, 1) == 0) {
154     header->flags    |= MAD_FLAG_PROTECTION;
155     header->crc_check = mad_bit_crc(stream->ptr, 16, 0xffff);
156   }
157  
158   /* bitrate_index */
159   index = mad_bit_read(&stream->ptr, 4);
160  
161   if (index == 15) {
162     stream->error = MAD_ERROR_BADBITRATE;
163     return -1;
164   }
165  
166   if (header->flags & MAD_FLAG_LSF_EXT)
167     header->bitrate = bitrate_table[3 + (header->layer >> 1)][index];
168   else
169     header->bitrate = bitrate_table[header->layer - 1][index];
170  
171   /* sampling_frequency */
172   index = mad_bit_read(&stream->ptr, 2);
173  
174   if (index == 3) {
175     stream->error = MAD_ERROR_BADSAMPLERATE;
176     return -1;
177   }
178  
179   header->samplerate = samplerate_table[index];
180  
181   if (header->flags & MAD_FLAG_LSF_EXT) {
182     header->samplerate /= 2;
183  
184     if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
185       header->samplerate /= 2;
186   }
187  
188   /* padding_bit */
189   if (mad_bit_read(&stream->ptr, 1))
190     header->flags |= MAD_FLAG_PADDING;
191  
192   /* private_bit */
193   if (mad_bit_read(&stream->ptr, 1))
194     header->private_bits |= MAD_PRIVATE_HEADER;
195  
196   /* mode */
197   header->mode = 3 - mad_bit_read(&stream->ptr, 2);
198  
199   /* mode_extension */
200   header->mode_extension = mad_bit_read(&stream->ptr, 2);
201  
202   /* copyright */
203   if (mad_bit_read(&stream->ptr, 1))
204     header->flags |= MAD_FLAG_COPYRIGHT;
205  
206   /* original/copy */
207   if (mad_bit_read(&stream->ptr, 1))
208     header->flags |= MAD_FLAG_ORIGINAL;
209  
210   /* emphasis */
211   header->emphasis = mad_bit_read(&stream->ptr, 2);
212  
213 # if defined(OPT_STRICT)
214   /*
215    * ISO/IEC 11172-3 says this is a reserved emphasis value, but
216    * streams exist which use it anyway. Since the value is not important
217    * to the decoder proper, we allow it unless OPT_STRICT is defined.
218    */
219   if (header->emphasis == MAD_EMPHASIS_RESERVED) {
220     stream->error = MAD_ERROR_BADEMPHASIS;
221     return -1;
222   }
223 # endif
224  
225   /* error_check() */
226  
227   /* crc_check */
228   if (header->flags & MAD_FLAG_PROTECTION)
229     header->crc_target = mad_bit_read(&stream->ptr, 16);
230  
231   return 0;
232 }
233  
234 /*
235  * NAME:        free_bitrate()
236  * DESCRIPTION: attempt to discover the bitstream's free bitrate
237  */
238 static
239 int free_bitrate(struct mad_stream *stream, struct mad_header const *header)
240 {
241   struct mad_bitptr keep_ptr;
242   unsigned long rate = 0;
243   unsigned int pad_slot, slots_per_frame;
244   unsigned char const *ptr = 0;
245  
246   keep_ptr = stream->ptr;
247  
248   pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
249   slots_per_frame = (header->layer == MAD_LAYER_III &&
250                      (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
251  
252   while (mad_stream_sync(stream) == 0) {
253     struct mad_stream peek_stream;
254     struct mad_header peek_header;
255  
256     peek_stream = *stream;
257     peek_header = *header;
258  
259     if (decode_header(&peek_header, &peek_stream) == 0 &&
260         peek_header.layer == header->layer &&
261         peek_header.samplerate == header->samplerate) {
262       unsigned int N;
263  
264       ptr = mad_bit_nextbyte(&stream->ptr);
265  
266       N = ptr - stream->this_frame;
267  
268       if (header->layer == MAD_LAYER_I) {
269         rate = (unsigned long) header->samplerate *
270           (N - 4 * pad_slot + 4) / 48 / 1000;
271       }
272       else {
273         rate = (unsigned long) header->samplerate *
274           (N - pad_slot + 1) / slots_per_frame / 1000;
275       }
276  
277       if (rate >= 8)
278         break;
279     }
280  
281     mad_bit_skip(&stream->ptr, 8);
282   }
283  
284   stream->ptr = keep_ptr;
285  
286   if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) {
287     stream->error = MAD_ERROR_LOSTSYNC;
288     return -1;
289   }
290  
291   stream->freerate = rate * 1000;
292  
293   return 0;
294 }
295  
296 /*
297  * NAME:        header->decode()
298  * DESCRIPTION: read the next frame header from the stream
299  */
300 int mad_header_decode(struct mad_header *header, struct mad_stream *stream)
301 {
302   register unsigned char const *ptr, *end;
303   unsigned int pad_slot, N;
304  
305   ptr = stream->next_frame;
306   end = stream->bufend;
307  
308   if (ptr == 0) {
309     stream->error = MAD_ERROR_BUFPTR;
310     goto fail;
311   }
312  
313   /* stream skip */
314   if (stream->skiplen) {
315     if (!stream->sync)
316       ptr = stream->this_frame;
317  
318     if (end - ptr < stream->skiplen) {
319       stream->skiplen   -= end - ptr;
320       stream->next_frame = end;
321  
322       stream->error = MAD_ERROR_BUFLEN;
323       goto fail;
324     }
325  
326     ptr += stream->skiplen;
327     stream->skiplen = 0;
328  
329     stream->sync = 1;
330   }
331  
332  sync:
333   /* synchronize */
334   if (stream->sync) {
335     if (end - ptr < MAD_BUFFER_GUARD) {
336       stream->next_frame = ptr;
337  
338       stream->error = MAD_ERROR_BUFLEN;
339       goto fail;
340     }
341     else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
342       /* mark point where frame sync word was expected */
343       stream->this_frame = ptr;
344       stream->next_frame = ptr + 1;
345  
346       stream->error = MAD_ERROR_LOSTSYNC;
347       goto fail;
348     }
349   }
350   else {
351     mad_bit_init(&stream->ptr, ptr);
352  
353     if (mad_stream_sync(stream) == -1) {
354       if (end - stream->next_frame >= MAD_BUFFER_GUARD)
355         stream->next_frame = end - MAD_BUFFER_GUARD;
356  
357       stream->error = MAD_ERROR_BUFLEN;
358       goto fail;
359     }
360  
361     ptr = mad_bit_nextbyte(&stream->ptr);
362   }
363  
364   /* begin processing */
365   stream->this_frame = ptr;
366   stream->next_frame = ptr + 1;  /* possibly bogus sync word */
367  
368   mad_bit_init(&stream->ptr, stream->this_frame);
369  
370   if (decode_header(header, stream) == -1)
371     goto fail;
372  
373   /* calculate frame duration */
374   mad_timer_set(&header->duration, 0,
375                 32 * MAD_NSBSAMPLES(header), header->samplerate);
376  
377   /* calculate free bit rate */
378   if (header->bitrate == 0) {
26 roytam 379     if ((stream->freerate == 0 || !stream->sync ||
380          (header->layer == MAD_LAYER_III && stream->freerate > 640000)) &&
14 roytam 381         free_bitrate(stream, header) == -1)
382       goto fail;
383  
384     header->bitrate = stream->freerate;
385     header->flags  |= MAD_FLAG_FREEFORMAT;
386   }
387  
388   /* calculate beginning of next frame */
389   pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
390  
391   if (header->layer == MAD_LAYER_I)
392     N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4;
393   else {
394     unsigned int slots_per_frame;
395  
396     slots_per_frame = (header->layer == MAD_LAYER_III &&
397                        (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
398  
399     N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot;
400   }
401  
402   /* verify there is enough data left in buffer to decode this frame */
403   if (N + MAD_BUFFER_GUARD > end - stream->this_frame) {
404     stream->next_frame = stream->this_frame;
405  
406     stream->error = MAD_ERROR_BUFLEN;
407     goto fail;
408   }
409  
410   stream->next_frame = stream->this_frame + N;
411  
412   if (!stream->sync) {
413     /* check that a valid frame header follows this frame */
414  
415     ptr = stream->next_frame;
416     if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
417       ptr = stream->next_frame = stream->this_frame + 1;
418       goto sync;
419     }
420  
421     stream->sync = 1;
422   }
423  
424   header->flags |= MAD_FLAG_INCOMPLETE;
425  
426   return 0;
427  
428  fail:
429   stream->sync = 0;
430  
431   return -1;
432 }
433  
434 /*
435  * NAME:        frame->decode()
436  * DESCRIPTION: decode a single frame from a bitstream
437  */
438 int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream)
439 {
440   frame->options = stream->options;
441  
442   /* header() */
443   /* error_check() */
444  
445   if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) &&
446       mad_header_decode(&frame->header, stream) == -1)
447     goto fail;
448  
449   /* audio_data() */
450  
451   frame->header.flags &= ~MAD_FLAG_INCOMPLETE;
452  
453   if (decoder_table[frame->header.layer - 1](stream, frame) == -1) {
454     if (!MAD_RECOVERABLE(stream->error))
455       stream->next_frame = stream->this_frame;
456  
457     goto fail;
458   }
459  
460   /* ancillary_data() */
461  
462   if (frame->header.layer != MAD_LAYER_III) {
463     struct mad_bitptr next_frame;
464  
465     mad_bit_init(&next_frame, stream->next_frame);
466  
467     stream->anc_ptr    = stream->ptr;
468     stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame);
469  
470     mad_bit_finish(&next_frame);
471   }
472  
473   return 0;
474  
475  fail:
476   stream->anc_bitlen = 0;
477   return -1;
478 }
479  
480 /*
481  * NAME:        frame->mute()
482  * DESCRIPTION: zero all subband values so the frame becomes silent
483  */
484 void mad_frame_mute(struct mad_frame *frame)
485 {
486   unsigned int s, sb;
487  
488   for (s = 0; s < 36; ++s) {
489     for (sb = 0; sb < 32; ++sb) {
490       frame->sbsample[0][s][sb] =
491       frame->sbsample[1][s][sb] = 0;
492     }
493   }
494  
495   if (frame->overlap) {
496     for (s = 0; s < 18; ++s) {
497       for (sb = 0; sb < 32; ++sb) {
498         (*frame->overlap)[0][sb][s] =
499         (*frame->overlap)[1][sb][s] = 0;
500       }
501     }
502   }
503 }