OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / media / libstagefright / codecs / mp3dec / src / pvmp3_framedecoder.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /*
19 ------------------------------------------------------------------------------
20
21    PacketVideo Corp.
22    MP3 Decoder Library
23
24    Filename: pvmp3_framedecoder.cpp
25
26    Functions:
27     pvmp3_framedecoder
28     pvmp3_InitDecoder
29     pvmp3_resetDecoder
30
31     Date: 09/21/2007
32
33 ------------------------------------------------------------------------------
34  REVISION HISTORY
35
36
37  Description:
38
39 ------------------------------------------------------------------------------
40  INPUT AND OUTPUT DEFINITIONS
41
42 Input
43     pExt = pointer to the external interface structure. See the file
44            pvmp3decoder_api.h for a description of each field.
45            Data type of pointer to a tPVMP3DecoderExternal
46            structure.
47
48     pMem = void pointer to hide the internal implementation of the library
49            It is cast back to a tmp3dec_file structure. This structure
50            contains information that needs to persist between calls to
51            this function, or is too big to be placed on the stack, even
52            though the data is only needed during execution of this function
53            Data type void pointer, internally pointer to a tmp3dec_file
54            structure.
55
56
57  Outputs:
58      status = ERROR condition.  see structure  ERROR_CODE
59
60  Pointers and Buffers Modified:
61     pMem contents are modified.
62     pExt: (more detail in the file pvmp3decoder_api.h)
63     inputBufferUsedLength - number of array elements used up by the stream.
64     samplingRate - sampling rate in samples per sec
65     bitRate - bit rate in bits per second, varies frame to frame.
66
67
68
69 ------------------------------------------------------------------------------
70  FUNCTIONS DESCRIPTION
71
72     pvmp3_framedecoder
73         frame decoder library driver
74     pvmp3_InitDecoder
75         Decoder Initialization
76     pvmp3_resetDecoder
77         Reset Decoder
78
79 ------------------------------------------------------------------------------
80  REQUIREMENTS
81
82
83 ------------------------------------------------------------------------------
84  REFERENCES
85
86  [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
87      ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
88
89 ------------------------------------------------------------------------------
90  PSEUDO-CODE
91
92 ------------------------------------------------------------------------------
93 */
94
95
96 /*----------------------------------------------------------------------------
97 ; INCLUDES
98 ----------------------------------------------------------------------------*/
99
100
101 #include "pvmp3_framedecoder.h"
102 #include "pvmp3_dec_defs.h"
103 #include "pvmp3_poly_phase_synthesis.h"
104 #include "pvmp3_tables.h"
105 #include "pvmp3_imdct_synth.h"
106 #include "pvmp3_alias_reduction.h"
107 #include "pvmp3_reorder.h"
108 #include "pvmp3_dequantize_sample.h"
109 #include "pvmp3_stereo_proc.h"
110 #include "pvmp3_mpeg2_stereo_proc.h"
111 #include "pvmp3_get_side_info.h"
112 #include "pvmp3_get_scale_factors.h"
113 #include "pvmp3_mpeg2_get_scale_factors.h"
114 #include "pvmp3_decode_header.h"
115 #include "pvmp3_get_main_data_size.h"
116 #include "s_tmp3dec_file.h"
117 #include "pvmp3_getbits.h"
118 #include "mp3_mem_funcs.h"
119
120
121 /*----------------------------------------------------------------------------
122 ; MACROS
123 ; Define module specific macros here
124 ----------------------------------------------------------------------------*/
125
126
127 /*----------------------------------------------------------------------------
128 ; DEFINES
129 ; Include all pre-processor statements here. Include conditional
130 ; compile variables also.
131 ----------------------------------------------------------------------------*/
132
133 /*----------------------------------------------------------------------------
134 ; LOCAL FUNCTION DEFINITIONS
135 ; Function Prototype declaration
136 ----------------------------------------------------------------------------*/
137
138 /*----------------------------------------------------------------------------
139 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
140 ; Variable declaration - defined here and used outside this module
141 ----------------------------------------------------------------------------*/
142
143 /*----------------------------------------------------------------------------
144 ; EXTERNAL FUNCTION REFERENCES
145 ; Declare functions defined elsewhere and referenced in this module
146 ----------------------------------------------------------------------------*/
147
148 /*----------------------------------------------------------------------------
149 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
150 ; Declare variables used in this module but defined elsewhere
151 ----------------------------------------------------------------------------*/
152
153 /*----------------------------------------------------------------------------
154 ; FUNCTION CODE
155 ----------------------------------------------------------------------------*/
156
157 ERROR_CODE pvmp3_framedecoder(tPVMP3DecoderExternal *pExt,
158                               void              *pMem)
159 {
160
161     ERROR_CODE        errorCode  = NO_DECODING_ERROR;
162
163     int32   crc_error_count = 0;
164     uint32  sent_crc = 0;
165     uint32  computed_crc = 0;
166
167     tmp3dec_chan   *pChVars[CHAN];
168     tmp3dec_file   *pVars = (tmp3dec_file *)pMem;
169
170     mp3Header info_data;
171     mp3Header *info = &info_data;
172
173     pVars->inputStream.pBuffer  = pExt->pInputBuffer;
174
175
176     pVars->inputStream.usedBits  = pExt->inputBufferUsedLength << 3;
177     pVars->inputStream.inputBufferCurrentLength  = pExt->inputBufferCurrentLength;
178
179
180     errorCode = pvmp3_decode_header(&pVars->inputStream,
181                                     info,
182                                     &computed_crc);
183
184     if (errorCode != NO_DECODING_ERROR)
185     {
186         pExt->outputFrameSize = 0;
187         return errorCode;
188     }
189
190     pVars->num_channels = (info->mode == MPG_MD_MONO) ? 1 : 2;
191     pExt->num_channels = pVars->num_channels;
192
193     int32 outputFrameSize = (info->version_x == MPEG_1) ?
194                             2 * SUBBANDS_NUMBER * FILTERBANK_BANDS :
195                             SUBBANDS_NUMBER * FILTERBANK_BANDS;
196
197     outputFrameSize = (info->mode == MPG_MD_MONO) ? outputFrameSize : outputFrameSize << 1;
198
199
200     /*
201      *  Check if output buffer has enough room to hold output PCM
202      */
203     if (pExt->outputFrameSize >= outputFrameSize)
204     {
205         pExt->outputFrameSize = outputFrameSize;
206     }
207     else
208     {
209         pExt->outputFrameSize = 0;
210         return OUTPUT_BUFFER_TOO_SMALL;
211     }
212
213
214     pChVars[ LEFT] = &pVars->perChan[ LEFT];
215     pChVars[RIGHT] = &pVars->perChan[RIGHT];
216
217
218
219
220     if (info->error_protection)
221     {
222         /*
223          *  Get crc content
224          */
225         sent_crc = getUpTo17bits(&pVars->inputStream, 16);
226     }
227
228
229     if (info->layer_description == 3)
230     {
231         int32 gr;
232         int32 ch;
233         uint32 main_data_end;
234         int32 bytes_to_discard;
235         int16 *ptrOutBuffer = pExt->pOutputBuffer;
236
237         /*
238          * Side Information must be extracted from the bitstream and store for use
239          * during the decoded of the associated frame
240          */
241
242         errorCode = pvmp3_get_side_info(&pVars->inputStream,
243                                         &pVars->sideInfo,
244                                         info,
245                                         &computed_crc);
246
247         if (errorCode != NO_DECODING_ERROR)
248         {
249             pExt->outputFrameSize = 0;
250             return errorCode;
251         }
252
253         /*
254          *  If CRC was sent, check that matches the one got while parsing data
255          *  disable crc if this is the desired mode
256          */
257         if (info->error_protection)
258         {
259             if ((computed_crc != sent_crc) && pExt->crcEnabled)
260             {
261                 crc_error_count++;
262             }
263         }
264
265         /*
266          * main data (scalefactors, Huffman coded, etc,) are not necessarily located
267          * adjacent to the side-info. Beginning of main data is located using
268          * field "main_data_begin" of the current frame. The length does not include
269          * header and side info.
270          * "main_data_begin" points to the first bit of main data of a frame. It is a negative
271          * offset in bytes from the first byte of the sync word
272          * main_data_begin = 0  <===> main data start rigth after side info.
273          */
274
275         int32 temp = pvmp3_get_main_data_size(info, pVars);
276
277
278         /*
279          *  Check if available data holds a full frame, if not flag an error
280          */
281
282         if ((uint32)pVars->predicted_frame_size > pVars->inputStream.inputBufferCurrentLength)
283         {
284             pExt->outputFrameSize = 0;
285             return NO_ENOUGH_MAIN_DATA_ERROR;
286         }
287
288         /*
289          *  Fill in internal circular buffer
290          */
291         fillMainDataBuf(pVars, temp);
292
293
294         main_data_end = pVars->mainDataStream.usedBits >> 3; /* in bytes */
295         if ((main_data_end << 3) < pVars->mainDataStream.usedBits)
296         {
297             main_data_end++;
298             pVars->mainDataStream.usedBits = main_data_end << 3;
299         }
300
301
302         bytes_to_discard = pVars->frame_start - pVars->sideInfo.main_data_begin - main_data_end;
303
304
305         if (main_data_end > BUFSIZE)   /* check overflow on the buffer */
306         {
307             pVars->frame_start -= BUFSIZE;
308
309             pVars->mainDataStream.usedBits -= (BUFSIZE << 3);
310         }
311
312         pVars->frame_start += temp;
313
314
315         if (bytes_to_discard < 0 || crc_error_count)
316         {
317             /*
318              *  Not enough data to decode, then we should avoid reading this
319              *  data ( getting/ignoring sido info and scale data)
320              *  Main data could be located in the previous frame, so an unaccounted
321              *  frame can cause incorrect processing
322              *  Just run the polyphase filter to "clean" the history buffer
323              */
324             errorCode = NO_ENOUGH_MAIN_DATA_ERROR;
325
326             /*
327              *  Clear the input to these filters
328              */
329
330             pv_memset((void*)pChVars[RIGHT]->work_buf_int32,
331                       0,
332                       SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[RIGHT]->work_buf_int32[0]));
333
334             pv_memset((void*)pChVars[LEFT]->work_buf_int32,
335                       0,
336                       SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[LEFT]->work_buf_int32[0]));
337
338             /*  clear circular buffers, to avoid any glitch */
339             pv_memset((void*)&pChVars[ LEFT]->circ_buffer[576],
340                       0,
341                       480*sizeof(pChVars[ LEFT]->circ_buffer[0]));
342             pv_memset((void*)&pChVars[RIGHT]->circ_buffer[576],
343                       0,
344                       480*sizeof(pChVars[RIGHT]->circ_buffer[0]));
345
346             pChVars[ LEFT]->used_freq_lines = 575;
347             pChVars[RIGHT]->used_freq_lines = 575;
348
349         }
350         else
351         {
352             pVars->mainDataStream.usedBits += (bytes_to_discard << 3);
353         }
354
355         /*
356          *  if (fr_ps->header->version_x == MPEG_1), use 2 granules, otherwise just 1
357          */
358         for (gr = 0; gr < (1 + !(info->version_x)); gr++)
359         {
360             if (errorCode != NO_ENOUGH_MAIN_DATA_ERROR)
361             {
362                 for (ch = 0; ch < pVars->num_channels; ch++)
363                 {
364                     int32 part2_start = pVars->mainDataStream.usedBits;
365
366                     if (info->version_x == MPEG_1)
367                     {
368
369                         pvmp3_get_scale_factors(&pVars->scaleFactors[ch],
370                                                 &pVars->sideInfo,
371                                                 gr,
372                                                 ch,
373                                                 &pVars->mainDataStream);
374                     }
375                     else
376                     {
377                         int32 * tmp = pVars->Scratch_mem;
378                         pvmp3_mpeg2_get_scale_factors(&pVars->scaleFactors[ch],
379                                                       &pVars->sideInfo,
380                                                       gr,
381                                                       ch,
382                                                       info,
383                                                       (uint32 *)tmp,
384                                                       &pVars->mainDataStream);
385                     }
386
387                     pChVars[ch]->used_freq_lines = pvmp3_huffman_parsing(pChVars[ch]->work_buf_int32,
388                                                    &pVars->sideInfo.ch[ch].gran[gr],
389                                                    pVars,
390                                                    part2_start,
391                                                    info);
392
393
394                     pvmp3_dequantize_sample(pChVars[ch]->work_buf_int32,
395                                             &pVars->scaleFactors[ch],
396                                             &pVars->sideInfo.ch[ch].gran[gr],
397                                             pChVars[ch]->used_freq_lines,
398                                             info);
399
400
401
402
403                 }   /* for (ch=0; ch<stereo; ch++)  */
404
405                 if (pVars->num_channels == 2)
406                 {
407
408                     int32 used_freq_lines = (pChVars[ LEFT]->used_freq_lines  >
409                                              pChVars[RIGHT]->used_freq_lines) ?
410                                             pChVars[ LEFT]->used_freq_lines  :
411                                             pChVars[RIGHT]->used_freq_lines;
412
413                     pChVars[ LEFT]->used_freq_lines = used_freq_lines;
414                     pChVars[RIGHT]->used_freq_lines = used_freq_lines;
415
416                     if (info->version_x == MPEG_1)
417                     {
418                         pvmp3_stereo_proc(pChVars[ LEFT]->work_buf_int32,
419                                           pChVars[RIGHT]->work_buf_int32,
420                                           &pVars->scaleFactors[RIGHT],
421                                           &pVars->sideInfo.ch[LEFT].gran[gr],
422                                           used_freq_lines,
423                                           info);
424                     }
425                     else
426                     {
427                         int32 * tmp = pVars->Scratch_mem;
428                         pvmp3_mpeg2_stereo_proc(pChVars[ LEFT]->work_buf_int32,
429                                                 pChVars[RIGHT]->work_buf_int32,
430                                                 &pVars->scaleFactors[RIGHT],
431                                                 &pVars->sideInfo.ch[ LEFT].gran[gr],
432                                                 &pVars->sideInfo.ch[RIGHT].gran[gr],
433                                                 (uint32 *)tmp,
434                                                 used_freq_lines,
435                                                 info);
436                     }
437                 }
438
439             } /* if ( errorCode != NO_ENOUGH_MAIN_DATA_ERROR) */
440
441             for (ch = 0; ch < pVars->num_channels; ch++)
442             {
443
444                 pvmp3_reorder(pChVars[ch]->work_buf_int32,
445                               &pVars->sideInfo.ch[ch].gran[gr],
446                               &pChVars[ ch]->used_freq_lines,
447                               info,
448                               pVars->Scratch_mem);
449
450                 pvmp3_alias_reduction(pChVars[ch]->work_buf_int32,
451                                       &pVars->sideInfo.ch[ch].gran[gr],
452                                       &pChVars[ ch]->used_freq_lines,
453                                       info);
454
455
456                 /*
457                  *   IMDCT
458                  */
459                 /* set mxposition
460                  * In case of mixed blocks, # of bands with long
461                  * blocks (2 or 4) else 0
462                  */
463                 uint16 mixedBlocksLongBlocks = 0; /*  0 = long or short, 2=mixed, 4=mixed 2.5@8000 */
464                 if (pVars->sideInfo.ch[ch].gran[gr].mixed_block_flag &&
465                         pVars->sideInfo.ch[ch].gran[gr].window_switching_flag)
466                 {
467                     if ((info->version_x == MPEG_2_5) && (info->sampling_frequency == 2))
468                     {
469                         mixedBlocksLongBlocks = 4; /* mpeg2.5 @ 8 KHz */
470                     }
471                     else
472                     {
473                         mixedBlocksLongBlocks = 2;
474                     }
475                 }
476
477                 pvmp3_imdct_synth(pChVars[ch]->work_buf_int32,
478                                   pChVars[ch]->overlap,
479                                   pVars->sideInfo.ch[ch].gran[gr].block_type,
480                                   mixedBlocksLongBlocks,
481                                   pChVars[ ch]->used_freq_lines,
482                                   pVars->Scratch_mem);
483
484
485                 /*
486                  *   Polyphase synthesis
487                  */
488
489                 pvmp3_poly_phase_synthesis(pChVars[ch],
490                                            pVars->num_channels,
491                                            pExt->equalizerType,
492                                            &ptrOutBuffer[ch]);
493
494
495             }/* end ch loop */
496
497             ptrOutBuffer += pVars->num_channels * SUBBANDS_NUMBER * FILTERBANK_BANDS;
498         }  /*   for (gr=0;gr<Max_gr;gr++)  */
499
500         /* skip ancillary data */
501         if (info->bitrate_index > 0)
502         { /* if not free-format */
503
504             int32 ancillary_data_lenght = pVars->predicted_frame_size << 3;
505
506             ancillary_data_lenght  -= pVars->inputStream.usedBits;
507
508             /* skip ancillary data */
509             if (ancillary_data_lenght > 0)
510             {
511                 pVars->inputStream.usedBits += ancillary_data_lenght;
512             }
513
514         }
515
516         /*
517          *  This overrides a possible NO_ENOUGH_MAIN_DATA_ERROR
518          */
519         errorCode = NO_DECODING_ERROR;
520
521     }
522     else
523     {
524         /*
525          * The info on the header leads to an unsupported layer, more data
526          * will not fix this, so this is a bad frame,
527          */
528
529         pExt->outputFrameSize = 0;
530         return UNSUPPORTED_LAYER;
531     }
532
533     pExt->inputBufferUsedLength = pVars->inputStream.usedBits >> 3;
534     pExt->totalNumberOfBitsUsed += pVars->inputStream.usedBits;
535     pExt->version = info->version_x;
536     pExt->samplingRate = mp3_s_freq[info->version_x][info->sampling_frequency];
537     pExt->bitRate = mp3_bitrate[pExt->version][info->bitrate_index];
538
539
540     /*
541      *  Always verify buffer overrun condition
542      */
543
544     if (pExt->inputBufferUsedLength > pExt->inputBufferCurrentLength)
545     {
546         pExt->outputFrameSize = 0;
547         errorCode = NO_ENOUGH_MAIN_DATA_ERROR;
548     }
549
550     return errorCode;
551
552 }
553
554
555 /*----------------------------------------------------------------------------
556 ; FUNCTION CODE
557 ----------------------------------------------------------------------------*/
558
559 __inline void fillDataBuf(tmp3Bits *pMainData,
560                           uint32 val)       /* val to write into the buffer */
561 {
562     pMainData->pBuffer[module(pMainData->offset++, BUFSIZE)] = (uint8)val;
563 }
564
565
566 void fillMainDataBuf(void  *pMem, int32 temp)
567 {
568     tmp3dec_file   *pVars = (tmp3dec_file *)pMem;
569
570
571     int32 offset = (pVars->inputStream.usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
572
573     /*
574      *  Check if input circular buffer boundaries need to be enforced
575      */
576     if ((offset + temp) < BUFSIZE)
577     {
578         uint8 * ptr = pVars->inputStream.pBuffer + offset;
579
580         offset = pVars->mainDataStream.offset;
581
582         /*
583          *  Check if main data circular buffer boundaries need to be enforced
584          */
585         if ((offset + temp) < BUFSIZE)
586         {
587             pv_memcpy((pVars->mainDataStream.pBuffer + offset), ptr, temp*sizeof(uint8));
588             pVars->mainDataStream.offset += temp;
589         }
590         else
591         {
592             int32 tmp1 = *(ptr++);
593             for (int32 nBytes = temp >> 1; nBytes != 0; nBytes--)  /* read main data. */
594             {
595                 int32 tmp2 = *(ptr++);
596                 fillDataBuf(&pVars->mainDataStream, tmp1);
597                 fillDataBuf(&pVars->mainDataStream, tmp2);
598                 tmp1 = *(ptr++);
599             }
600
601             if (temp&1)
602             {
603                 fillDataBuf(&pVars->mainDataStream, tmp1);
604             }
605
606             /* adjust circular buffer counter */
607             pVars->mainDataStream.offset = module(pVars->mainDataStream.offset, BUFSIZE);
608         }
609     }
610     else
611     {
612         for (int32 nBytes = temp >> 1; nBytes != 0; nBytes--)  /* read main data. */
613         {
614             fillDataBuf(&pVars->mainDataStream, *(pVars->inputStream.pBuffer + module(offset++  , BUFSIZE)));
615             fillDataBuf(&pVars->mainDataStream, *(pVars->inputStream.pBuffer + module(offset++  , BUFSIZE)));
616         }
617         if (temp&1)
618         {
619             fillDataBuf(&pVars->mainDataStream, *(pVars->inputStream.pBuffer + module(offset  , BUFSIZE)));
620         }
621     }
622
623
624     pVars->inputStream.usedBits += (temp) << INBUF_ARRAY_INDEX_SHIFT;
625 }
626
627
628
629
630 /*----------------------------------------------------------------------------
631 ; FUNCTION CODE
632 ----------------------------------------------------------------------------*/
633
634 uint32 pvmp3_decoderMemRequirements(void)
635 {
636     uint32 size;
637
638     size = (uint32) sizeof(tmp3dec_file);
639     return (size);
640 }
641
642
643
644 /*----------------------------------------------------------------------------
645 ; FUNCTION CODE
646 ----------------------------------------------------------------------------*/
647
648 #include "pvmp3_decode_huff_cw.h"
649
650 void pvmp3_InitDecoder(tPVMP3DecoderExternal *pExt,
651                        void  *pMem)
652 {
653
654     tmp3dec_file      *pVars;
655     huffcodetab       *pHuff;
656
657     pVars = (tmp3dec_file *)pMem;
658
659     pVars->num_channels = 0;
660
661     pExt->totalNumberOfBitsUsed = 0;
662     pExt->inputBufferCurrentLength = 0;
663     pExt->inputBufferUsedLength    = 0;
664
665     pVars->mainDataStream.offset = 0;
666
667     pv_memset((void*)pVars->mainDataBuffer,
668               0,
669               BUFSIZE*sizeof(*pVars->mainDataBuffer));
670
671
672     pVars->inputStream.pBuffer = pExt->pInputBuffer;
673
674     /*
675      *  Initialize huffman decoding table
676      */
677
678     pHuff = pVars->ht;
679     pHuff[0].linbits = 0;
680     pHuff[0].pdec_huff_tab = pvmp3_decode_huff_cw_tab0;
681     pHuff[1].linbits = 0;
682     pHuff[1].pdec_huff_tab = pvmp3_decode_huff_cw_tab1;
683     pHuff[2].linbits = 0;
684     pHuff[2].pdec_huff_tab = pvmp3_decode_huff_cw_tab2;
685     pHuff[3].linbits = 0;
686     pHuff[3].pdec_huff_tab = pvmp3_decode_huff_cw_tab3;
687     pHuff[4].linbits = 0;
688     pHuff[4].pdec_huff_tab = pvmp3_decode_huff_cw_tab0; /* tbl 4 is not used */
689     pHuff[5].linbits = 4;
690     pHuff[5].pdec_huff_tab = pvmp3_decode_huff_cw_tab5;
691     pHuff[6].linbits = 0;
692     pHuff[6].pdec_huff_tab = pvmp3_decode_huff_cw_tab6;
693     pHuff[7].linbits = 0;
694     pHuff[7].pdec_huff_tab = pvmp3_decode_huff_cw_tab7;
695     pHuff[8].linbits = 0;
696     pHuff[8].pdec_huff_tab = pvmp3_decode_huff_cw_tab8;
697     pHuff[9].linbits = 0;
698     pHuff[9].pdec_huff_tab = pvmp3_decode_huff_cw_tab9;
699     pHuff[10].linbits = 0;
700     pHuff[10].pdec_huff_tab = pvmp3_decode_huff_cw_tab10;
701     pHuff[11].linbits = 0;
702     pHuff[11].pdec_huff_tab = pvmp3_decode_huff_cw_tab11;
703     pHuff[12].linbits = 0;
704     pHuff[12].pdec_huff_tab = pvmp3_decode_huff_cw_tab12;
705     pHuff[13].linbits = 0;
706     pHuff[13].pdec_huff_tab = pvmp3_decode_huff_cw_tab13;
707     pHuff[14].linbits = 0;
708     pHuff[14].pdec_huff_tab = pvmp3_decode_huff_cw_tab0; /* tbl 14 is not used */
709     pHuff[15].linbits = 0;
710     pHuff[15].pdec_huff_tab = pvmp3_decode_huff_cw_tab15;
711     pHuff[16].linbits = 1;
712     pHuff[16].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
713     pHuff[17].linbits = 2;
714     pHuff[17].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
715     pHuff[18].linbits = 3;
716     pHuff[18].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
717     pHuff[19].linbits = 4;
718     pHuff[19].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
719     pHuff[20].linbits = 6;
720     pHuff[20].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
721     pHuff[21].linbits = 8;
722     pHuff[21].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
723     pHuff[22].linbits = 10;
724     pHuff[22].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
725     pHuff[23].linbits = 13;
726     pHuff[23].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
727     pHuff[24].linbits = 4;
728     pHuff[24].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
729     pHuff[25].linbits = 5;
730     pHuff[25].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
731     pHuff[26].linbits = 6;
732     pHuff[26].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
733     pHuff[27].linbits = 7;
734     pHuff[27].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
735     pHuff[28].linbits = 8;
736     pHuff[28].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
737     pHuff[29].linbits = 9;
738     pHuff[29].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
739     pHuff[30].linbits = 11;
740     pHuff[30].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
741     pHuff[31].linbits = 13;
742     pHuff[31].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
743     pHuff[32].linbits = 0;
744     pHuff[32].pdec_huff_tab = pvmp3_decode_huff_cw_tab32;
745     pHuff[33].linbits = 0;
746     pHuff[33].pdec_huff_tab = pvmp3_decode_huff_cw_tab33;
747
748     /*
749      *  Initialize polysynthesis circular buffer mechanism
750      */
751     /* clear buffers */
752
753     pvmp3_resetDecoder(pMem);
754
755 }
756
757
758 /*----------------------------------------------------------------------------
759 ; FUNCTION CODE
760 ----------------------------------------------------------------------------*/
761
762
763 void pvmp3_resetDecoder(void  *pMem)
764 {
765
766     tmp3dec_file      *pVars;
767     tmp3dec_chan      *pChVars[CHAN];
768
769     pVars = (tmp3dec_file *)pMem;
770     pChVars[ LEFT] = &pVars->perChan[ LEFT];
771     pChVars[RIGHT] = &pVars->perChan[RIGHT];
772
773     pVars->frame_start = 0;
774
775     pVars->mainDataStream.offset = 0;
776
777     pVars->mainDataStream.pBuffer =  pVars->mainDataBuffer;
778     pVars->mainDataStream.usedBits = 0;
779
780
781     pVars->inputStream.usedBits = 0; // in bits
782
783
784     pChVars[ LEFT]->used_freq_lines = 575;
785     pChVars[RIGHT]->used_freq_lines = 575;
786
787
788     /*
789      *  Initialize polysynthesis circular buffer mechanism
790      */
791
792     pv_memset((void*)&pChVars[ LEFT]->circ_buffer[576],
793               0,
794               480*sizeof(pChVars[ LEFT]->circ_buffer[0]));
795     pv_memset((void*)&pChVars[RIGHT]->circ_buffer[576],
796               0,
797               480*sizeof(pChVars[RIGHT]->circ_buffer[0]));
798
799
800     pv_memset((void*)pChVars[ LEFT]->overlap,
801               0,
802               SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[ LEFT]->overlap[0]));
803
804
805     pv_memset((void*)pChVars[ RIGHT]->overlap,
806               0,
807               SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[ RIGHT]->overlap[0]));
808
809
810
811
812
813     /*
814      *  Clear all the structures
815      */
816
817
818     pv_memset((void*)&pVars->scaleFactors[RIGHT],
819               0,
820               sizeof(mp3ScaleFactors));
821
822     pv_memset((void*)&pVars->scaleFactors[LEFT],
823               0,
824               sizeof(mp3ScaleFactors));
825
826     pv_memset((void*)&pVars->sideInfo,
827               0,
828               sizeof(mp3SideInfo));
829
830     pv_memset((void*)&pVars->sideInfo,
831               0,
832               sizeof(mp3SideInfo));
833
834 }