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 
pvmp3_framedecoder(tPVMP3DecoderExternal * pExt,void * pMem)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         // force signed computation; buffer sizes and offsets are all going to be
303         // well within the constraints of 32-bit signed math.
304         bytes_to_discard = pVars->frame_start
305                            - ((int32)pVars->sideInfo.main_data_begin)
306                            - ((int32)main_data_end);
307 
308 
309         if (main_data_end > BUFSIZE)   /* check overflow on the buffer */
310         {
311             pVars->frame_start -= BUFSIZE;
312 
313             pVars->mainDataStream.usedBits -= (BUFSIZE << 3);
314         }
315 
316         pVars->frame_start += temp;
317 
318 
319         if (bytes_to_discard < 0 || crc_error_count)
320         {
321             /*
322              *  Not enough data to decode, then we should avoid reading this
323              *  data ( getting/ignoring sido info and scale data)
324              *  Main data could be located in the previous frame, so an unaccounted
325              *  frame can cause incorrect processing
326              *  Just run the polyphase filter to "clean" the history buffer
327              */
328             errorCode = NO_ENOUGH_MAIN_DATA_ERROR;
329 
330             /*
331              *  Clear the input to these filters
332              */
333 
334             pv_memset((void*)pChVars[RIGHT]->work_buf_int32,
335                       0,
336                       SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[RIGHT]->work_buf_int32[0]));
337 
338             pv_memset((void*)pChVars[LEFT]->work_buf_int32,
339                       0,
340                       SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[LEFT]->work_buf_int32[0]));
341 
342             /*  clear circular buffers, to avoid any glitch */
343             pv_memset((void*)&pChVars[ LEFT]->circ_buffer[576],
344                       0,
345                       480*sizeof(pChVars[ LEFT]->circ_buffer[0]));
346             pv_memset((void*)&pChVars[RIGHT]->circ_buffer[576],
347                       0,
348                       480*sizeof(pChVars[RIGHT]->circ_buffer[0]));
349 
350             pChVars[ LEFT]->used_freq_lines = 575;
351             pChVars[RIGHT]->used_freq_lines = 575;
352 
353         }
354         else
355         {
356             pVars->mainDataStream.usedBits += (bytes_to_discard << 3);
357         }
358 
359         /*
360          *  if (fr_ps->header->version_x == MPEG_1), use 2 granules, otherwise just 1
361          */
362         for (gr = 0; gr < (1 + !(info->version_x)); gr++)
363         {
364             if (errorCode != NO_ENOUGH_MAIN_DATA_ERROR)
365             {
366                 for (ch = 0; ch < pVars->num_channels; ch++)
367                 {
368                     int32 part2_start = pVars->mainDataStream.usedBits;
369 
370                     if (info->version_x == MPEG_1)
371                     {
372 
373                         pvmp3_get_scale_factors(&pVars->scaleFactors[ch],
374                                                 &pVars->sideInfo,
375                                                 gr,
376                                                 ch,
377                                                 &pVars->mainDataStream);
378                     }
379                     else
380                     {
381                         int32 * tmp = pVars->Scratch_mem;
382                         pvmp3_mpeg2_get_scale_factors(&pVars->scaleFactors[ch],
383                                                       &pVars->sideInfo,
384                                                       gr,
385                                                       ch,
386                                                       info,
387                                                       (uint32 *)tmp,
388                                                       &pVars->mainDataStream);
389                     }
390 
391                     pChVars[ch]->used_freq_lines = pvmp3_huffman_parsing(pChVars[ch]->work_buf_int32,
392                                                    &pVars->sideInfo.ch[ch].gran[gr],
393                                                    pVars,
394                                                    part2_start,
395                                                    info);
396 
397 
398                     pvmp3_dequantize_sample(pChVars[ch]->work_buf_int32,
399                                             &pVars->scaleFactors[ch],
400                                             &pVars->sideInfo.ch[ch].gran[gr],
401                                             pChVars[ch]->used_freq_lines,
402                                             info);
403 
404 
405 
406 
407                 }   /* for (ch=0; ch<stereo; ch++)  */
408 
409                 if (pVars->num_channels == 2)
410                 {
411 
412                     int32 used_freq_lines = (pChVars[ LEFT]->used_freq_lines  >
413                                              pChVars[RIGHT]->used_freq_lines) ?
414                                             pChVars[ LEFT]->used_freq_lines  :
415                                             pChVars[RIGHT]->used_freq_lines;
416 
417                     pChVars[ LEFT]->used_freq_lines = used_freq_lines;
418                     pChVars[RIGHT]->used_freq_lines = used_freq_lines;
419 
420                     if (info->version_x == MPEG_1)
421                     {
422                         pvmp3_stereo_proc(pChVars[ LEFT]->work_buf_int32,
423                                           pChVars[RIGHT]->work_buf_int32,
424                                           &pVars->scaleFactors[RIGHT],
425                                           &pVars->sideInfo.ch[LEFT].gran[gr],
426                                           used_freq_lines,
427                                           info);
428                     }
429                     else
430                     {
431                         int32 * tmp = pVars->Scratch_mem;
432                         pvmp3_mpeg2_stereo_proc(pChVars[ LEFT]->work_buf_int32,
433                                                 pChVars[RIGHT]->work_buf_int32,
434                                                 &pVars->scaleFactors[RIGHT],
435                                                 &pVars->sideInfo.ch[ LEFT].gran[gr],
436                                                 &pVars->sideInfo.ch[RIGHT].gran[gr],
437                                                 (uint32 *)tmp,
438                                                 used_freq_lines,
439                                                 info);
440                     }
441                 }
442 
443             } /* if ( errorCode != NO_ENOUGH_MAIN_DATA_ERROR) */
444 
445             for (ch = 0; ch < pVars->num_channels; ch++)
446             {
447 
448                 pvmp3_reorder(pChVars[ch]->work_buf_int32,
449                               &pVars->sideInfo.ch[ch].gran[gr],
450                               &pChVars[ ch]->used_freq_lines,
451                               info,
452                               pVars->Scratch_mem);
453 
454                 pvmp3_alias_reduction(pChVars[ch]->work_buf_int32,
455                                       &pVars->sideInfo.ch[ch].gran[gr],
456                                       &pChVars[ ch]->used_freq_lines,
457                                       info);
458 
459 
460                 /*
461                  *   IMDCT
462                  */
463                 /* set mxposition
464                  * In case of mixed blocks, # of bands with long
465                  * blocks (2 or 4) else 0
466                  */
467                 uint16 mixedBlocksLongBlocks = 0; /*  0 = long or short, 2=mixed, 4=mixed 2.5@8000 */
468                 if (pVars->sideInfo.ch[ch].gran[gr].mixed_block_flag &&
469                         pVars->sideInfo.ch[ch].gran[gr].window_switching_flag)
470                 {
471                     if ((info->version_x == MPEG_2_5) && (info->sampling_frequency == 2))
472                     {
473                         mixedBlocksLongBlocks = 4; /* mpeg2.5 @ 8 KHz */
474                     }
475                     else
476                     {
477                         mixedBlocksLongBlocks = 2;
478                     }
479                 }
480 
481                 pvmp3_imdct_synth(pChVars[ch]->work_buf_int32,
482                                   pChVars[ch]->overlap,
483                                   pVars->sideInfo.ch[ch].gran[gr].block_type,
484                                   mixedBlocksLongBlocks,
485                                   pChVars[ ch]->used_freq_lines,
486                                   pVars->Scratch_mem);
487 
488 
489                 /*
490                  *   Polyphase synthesis
491                  */
492 
493                 pvmp3_poly_phase_synthesis(pChVars[ch],
494                                            pVars->num_channels,
495                                            pExt->equalizerType,
496                                            &ptrOutBuffer[ch]);
497 
498 
499             }/* end ch loop */
500 
501             ptrOutBuffer += pVars->num_channels * SUBBANDS_NUMBER * FILTERBANK_BANDS;
502         }  /*   for (gr=0;gr<Max_gr;gr++)  */
503 
504         /* skip ancillary data */
505         if (info->bitrate_index > 0)
506         { /* if not free-format */
507 
508             int32 ancillary_data_lenght = pVars->predicted_frame_size << 3;
509 
510             ancillary_data_lenght  -= pVars->inputStream.usedBits;
511 
512             /* skip ancillary data */
513             if (ancillary_data_lenght > 0)
514             {
515                 pVars->inputStream.usedBits += ancillary_data_lenght;
516             }
517 
518         }
519 
520         /*
521          *  This overrides a possible NO_ENOUGH_MAIN_DATA_ERROR
522          */
523         errorCode = NO_DECODING_ERROR;
524 
525     }
526     else
527     {
528         /*
529          * The info on the header leads to an unsupported layer, more data
530          * will not fix this, so this is a bad frame,
531          */
532 
533         pExt->outputFrameSize = 0;
534         return UNSUPPORTED_LAYER;
535     }
536 
537     pExt->inputBufferUsedLength = pVars->inputStream.usedBits >> 3;
538     pExt->totalNumberOfBitsUsed += pVars->inputStream.usedBits;
539     pExt->version = info->version_x;
540     pExt->samplingRate = mp3_s_freq[info->version_x][info->sampling_frequency];
541     pExt->bitRate = mp3_bitrate[pExt->version][info->bitrate_index];
542 
543 
544     /*
545      *  Always verify buffer overrun condition
546      */
547 
548     if (pExt->inputBufferUsedLength > pExt->inputBufferCurrentLength)
549     {
550         pExt->outputFrameSize = 0;
551         errorCode = NO_ENOUGH_MAIN_DATA_ERROR;
552     }
553 
554     return errorCode;
555 
556 }
557 
558 
559 /*----------------------------------------------------------------------------
560 ; FUNCTION CODE
561 ----------------------------------------------------------------------------*/
562 
fillDataBuf(tmp3Bits * pMainData,uint32 val)563 __inline void fillDataBuf(tmp3Bits *pMainData,
564                           uint32 val)       /* val to write into the buffer */
565 {
566     pMainData->pBuffer[module(pMainData->offset++, BUFSIZE)] = (uint8)val;
567 }
568 
569 
fillMainDataBuf(void * pMem,int32 temp)570 void fillMainDataBuf(void  *pMem, int32 temp)
571 {
572     tmp3dec_file   *pVars = (tmp3dec_file *)pMem;
573 
574 
575     int32 offset = (pVars->inputStream.usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
576 
577     /*
578      *  Check if input circular buffer boundaries need to be enforced
579      */
580     if ((offset + temp) < BUFSIZE)
581     {
582         uint8 * ptr = pVars->inputStream.pBuffer + offset;
583 
584         offset = pVars->mainDataStream.offset;
585 
586         /*
587          *  Check if main data circular buffer boundaries need to be enforced
588          */
589         if ((offset + temp) < BUFSIZE)
590         {
591             pv_memcpy((pVars->mainDataStream.pBuffer + offset), ptr, temp*sizeof(uint8));
592             pVars->mainDataStream.offset += temp;
593         }
594         else
595         {
596             int32 tmp1 = *(ptr++);
597             for (int32 nBytes = temp >> 1; nBytes != 0; nBytes--)  /* read main data. */
598             {
599                 int32 tmp2 = *(ptr++);
600                 fillDataBuf(&pVars->mainDataStream, tmp1);
601                 fillDataBuf(&pVars->mainDataStream, tmp2);
602                 tmp1 = *(ptr++);
603             }
604 
605             if (temp&1)
606             {
607                 fillDataBuf(&pVars->mainDataStream, tmp1);
608             }
609 
610             /* adjust circular buffer counter */
611             pVars->mainDataStream.offset = module(pVars->mainDataStream.offset, BUFSIZE);
612         }
613     }
614     else
615     {
616         for (int32 nBytes = temp >> 1; nBytes != 0; nBytes--)  /* read main data. */
617         {
618             fillDataBuf(&pVars->mainDataStream, *(pVars->inputStream.pBuffer + module(offset++  , BUFSIZE)));
619             fillDataBuf(&pVars->mainDataStream, *(pVars->inputStream.pBuffer + module(offset++  , BUFSIZE)));
620         }
621         if (temp&1)
622         {
623             fillDataBuf(&pVars->mainDataStream, *(pVars->inputStream.pBuffer + module(offset  , BUFSIZE)));
624         }
625     }
626 
627 
628     pVars->inputStream.usedBits += (temp) << INBUF_ARRAY_INDEX_SHIFT;
629 }
630 
631 
632 
633 
634 /*----------------------------------------------------------------------------
635 ; FUNCTION CODE
636 ----------------------------------------------------------------------------*/
637 
pvmp3_decoderMemRequirements(void)638 uint32 pvmp3_decoderMemRequirements(void)
639 {
640     uint32 size;
641 
642     size = (uint32) sizeof(tmp3dec_file);
643     return (size);
644 }
645 
646 
647 
648 /*----------------------------------------------------------------------------
649 ; FUNCTION CODE
650 ----------------------------------------------------------------------------*/
651 
652 #include "pvmp3_decode_huff_cw.h"
653 
pvmp3_InitDecoder(tPVMP3DecoderExternal * pExt,void * pMem)654 void pvmp3_InitDecoder(tPVMP3DecoderExternal *pExt,
655                        void  *pMem)
656 {
657 
658     tmp3dec_file      *pVars;
659     huffcodetab       *pHuff;
660 
661     pVars = (tmp3dec_file *)pMem;
662     memset(pVars, 0, sizeof(*pVars));
663 
664     pExt->totalNumberOfBitsUsed = 0;
665     pExt->inputBufferCurrentLength = 0;
666     pExt->inputBufferUsedLength    = 0;
667 
668     pVars->inputStream.pBuffer = pExt->pInputBuffer;
669 
670     /*
671      *  Initialize huffman decoding table
672      */
673 
674     pHuff = pVars->ht;
675     pHuff[0].linbits = 0;
676     pHuff[0].pdec_huff_tab = pvmp3_decode_huff_cw_tab0;
677     pHuff[1].linbits = 0;
678     pHuff[1].pdec_huff_tab = pvmp3_decode_huff_cw_tab1;
679     pHuff[2].linbits = 0;
680     pHuff[2].pdec_huff_tab = pvmp3_decode_huff_cw_tab2;
681     pHuff[3].linbits = 0;
682     pHuff[3].pdec_huff_tab = pvmp3_decode_huff_cw_tab3;
683     pHuff[4].linbits = 0;
684     pHuff[4].pdec_huff_tab = pvmp3_decode_huff_cw_tab0; /* tbl 4 is not used */
685     pHuff[5].linbits = 4;
686     pHuff[5].pdec_huff_tab = pvmp3_decode_huff_cw_tab5;
687     pHuff[6].linbits = 0;
688     pHuff[6].pdec_huff_tab = pvmp3_decode_huff_cw_tab6;
689     pHuff[7].linbits = 0;
690     pHuff[7].pdec_huff_tab = pvmp3_decode_huff_cw_tab7;
691     pHuff[8].linbits = 0;
692     pHuff[8].pdec_huff_tab = pvmp3_decode_huff_cw_tab8;
693     pHuff[9].linbits = 0;
694     pHuff[9].pdec_huff_tab = pvmp3_decode_huff_cw_tab9;
695     pHuff[10].linbits = 0;
696     pHuff[10].pdec_huff_tab = pvmp3_decode_huff_cw_tab10;
697     pHuff[11].linbits = 0;
698     pHuff[11].pdec_huff_tab = pvmp3_decode_huff_cw_tab11;
699     pHuff[12].linbits = 0;
700     pHuff[12].pdec_huff_tab = pvmp3_decode_huff_cw_tab12;
701     pHuff[13].linbits = 0;
702     pHuff[13].pdec_huff_tab = pvmp3_decode_huff_cw_tab13;
703     pHuff[14].linbits = 0;
704     pHuff[14].pdec_huff_tab = pvmp3_decode_huff_cw_tab0; /* tbl 14 is not used */
705     pHuff[15].linbits = 0;
706     pHuff[15].pdec_huff_tab = pvmp3_decode_huff_cw_tab15;
707     pHuff[16].linbits = 1;
708     pHuff[16].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
709     pHuff[17].linbits = 2;
710     pHuff[17].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
711     pHuff[18].linbits = 3;
712     pHuff[18].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
713     pHuff[19].linbits = 4;
714     pHuff[19].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
715     pHuff[20].linbits = 6;
716     pHuff[20].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
717     pHuff[21].linbits = 8;
718     pHuff[21].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
719     pHuff[22].linbits = 10;
720     pHuff[22].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
721     pHuff[23].linbits = 13;
722     pHuff[23].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
723     pHuff[24].linbits = 4;
724     pHuff[24].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
725     pHuff[25].linbits = 5;
726     pHuff[25].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
727     pHuff[26].linbits = 6;
728     pHuff[26].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
729     pHuff[27].linbits = 7;
730     pHuff[27].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
731     pHuff[28].linbits = 8;
732     pHuff[28].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
733     pHuff[29].linbits = 9;
734     pHuff[29].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
735     pHuff[30].linbits = 11;
736     pHuff[30].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
737     pHuff[31].linbits = 13;
738     pHuff[31].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
739     pHuff[32].linbits = 0;
740     pHuff[32].pdec_huff_tab = pvmp3_decode_huff_cw_tab32;
741     pHuff[33].linbits = 0;
742     pHuff[33].pdec_huff_tab = pvmp3_decode_huff_cw_tab33;
743 
744     /*
745      *  Initialize polysynthesis circular buffer mechanism
746      */
747     /* clear buffers */
748 
749     pvmp3_resetDecoder(pMem);
750 
751 }
752 
753 
754 /*----------------------------------------------------------------------------
755 ; FUNCTION CODE
756 ----------------------------------------------------------------------------*/
757 
758 
pvmp3_resetDecoder(void * pMem)759 void pvmp3_resetDecoder(void  *pMem)
760 {
761 
762     tmp3dec_file      *pVars;
763     tmp3dec_chan      *pChVars[CHAN];
764 
765     pVars = (tmp3dec_file *)pMem;
766     pChVars[ LEFT] = &pVars->perChan[ LEFT];
767     pChVars[RIGHT] = &pVars->perChan[RIGHT];
768 
769     pVars->frame_start = 0;
770 
771     pVars->mainDataStream.offset = 0;
772 
773     pVars->mainDataStream.pBuffer =  pVars->mainDataBuffer;
774     pVars->mainDataStream.usedBits = 0;
775 
776 
777     pVars->inputStream.usedBits = 0; // in bits
778 
779 
780     pChVars[ LEFT]->used_freq_lines = 575;
781     pChVars[RIGHT]->used_freq_lines = 575;
782 
783 
784     /*
785      *  Initialize polysynthesis circular buffer mechanism
786      */
787 
788     pv_memset((void*)&pChVars[ LEFT]->circ_buffer[576],
789               0,
790               480*sizeof(pChVars[ LEFT]->circ_buffer[0]));
791     pv_memset((void*)&pChVars[RIGHT]->circ_buffer[576],
792               0,
793               480*sizeof(pChVars[RIGHT]->circ_buffer[0]));
794 
795 
796     pv_memset((void*)pChVars[ LEFT]->overlap,
797               0,
798               SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[ LEFT]->overlap[0]));
799 
800 
801     pv_memset((void*)pChVars[ RIGHT]->overlap,
802               0,
803               SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[ RIGHT]->overlap[0]));
804 
805 
806 
807 
808 
809     /*
810      *  Clear all the structures
811      */
812 
813 
814     pv_memset((void*)&pVars->scaleFactors[RIGHT],
815               0,
816               sizeof(mp3ScaleFactors));
817 
818     pv_memset((void*)&pVars->scaleFactors[LEFT],
819               0,
820               sizeof(mp3ScaleFactors));
821 
822     pv_memset((void*)&pVars->sideInfo,
823               0,
824               sizeof(mp3SideInfo));
825 
826     pv_memset((void*)&pVars->sideInfo,
827               0,
828               sizeof(mp3SideInfo));
829 
830 }
831