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 /* The below code is borrowed from ./test/mp3reader.cpp */
parseHeader(uint32_t header,size_t * frame_size,uint32_t * out_sampling_rate=NULL,uint32_t * out_channels=NULL,uint32_t * out_bitrate=NULL,uint32_t * out_num_samples=NULL)158 static bool parseHeader(
159 uint32_t header, size_t *frame_size,
160 uint32_t *out_sampling_rate = NULL, uint32_t *out_channels = NULL ,
161 uint32_t *out_bitrate = NULL, uint32_t *out_num_samples = NULL) {
162 *frame_size = 0;
163
164 if (out_sampling_rate) {
165 *out_sampling_rate = 0;
166 }
167
168 if (out_channels) {
169 *out_channels = 0;
170 }
171
172 if (out_bitrate) {
173 *out_bitrate = 0;
174 }
175
176 if (out_num_samples) {
177 *out_num_samples = 1152;
178 }
179
180 if ((header & 0xffe00000) != 0xffe00000) {
181 return false;
182 }
183
184 unsigned version = (header >> 19) & 3;
185
186 if (version == 0x01) {
187 return false;
188 }
189
190 unsigned layer = (header >> 17) & 3;
191
192 if (layer == 0x00) {
193 return false;
194 }
195
196 unsigned bitrate_index = (header >> 12) & 0x0f;
197
198 if (bitrate_index == 0 || bitrate_index == 0x0f) {
199 // Disallow "free" bitrate.
200 return false;
201 }
202
203 unsigned sampling_rate_index = (header >> 10) & 3;
204
205 if (sampling_rate_index == 3) {
206 return false;
207 }
208
209 static const int kSamplingRateV1[] = { 44100, 48000, 32000 };
210 int sampling_rate = kSamplingRateV1[sampling_rate_index];
211 if (version == 2 /* V2 */) {
212 sampling_rate /= 2;
213 } else if (version == 0 /* V2.5 */) {
214 sampling_rate /= 4;
215 }
216
217 unsigned padding = (header >> 9) & 1;
218
219 if (layer == 3) {
220 // layer I
221
222 static const int kBitrateV1[] = {
223 32, 64, 96, 128, 160, 192, 224, 256,
224 288, 320, 352, 384, 416, 448
225 };
226
227 static const int kBitrateV2[] = {
228 32, 48, 56, 64, 80, 96, 112, 128,
229 144, 160, 176, 192, 224, 256
230 };
231
232 int bitrate =
233 (version == 3 /* V1 */)
234 ? kBitrateV1[bitrate_index - 1]
235 : kBitrateV2[bitrate_index - 1];
236
237 if (out_bitrate) {
238 *out_bitrate = bitrate;
239 }
240
241 *frame_size = (12000 * bitrate / sampling_rate + padding) * 4;
242
243 if (out_num_samples) {
244 *out_num_samples = 384;
245 }
246 } else {
247 // layer II or III
248
249 static const int kBitrateV1L2[] = {
250 32, 48, 56, 64, 80, 96, 112, 128,
251 160, 192, 224, 256, 320, 384
252 };
253
254 static const int kBitrateV1L3[] = {
255 32, 40, 48, 56, 64, 80, 96, 112,
256 128, 160, 192, 224, 256, 320
257 };
258
259 static const int kBitrateV2[] = {
260 8, 16, 24, 32, 40, 48, 56, 64,
261 80, 96, 112, 128, 144, 160
262 };
263
264 int bitrate;
265 if (version == 3 /* V1 */) {
266 bitrate = (layer == 2 /* L2 */)
267 ? kBitrateV1L2[bitrate_index - 1]
268 : kBitrateV1L3[bitrate_index - 1];
269
270 if (out_num_samples) {
271 *out_num_samples = 1152;
272 }
273 } else {
274 // V2 (or 2.5)
275
276 bitrate = kBitrateV2[bitrate_index - 1];
277 if (out_num_samples) {
278 *out_num_samples = (layer == 1 /* L3 */) ? 576 : 1152;
279 }
280 }
281
282 if (out_bitrate) {
283 *out_bitrate = bitrate;
284 }
285
286 if (version == 3 /* V1 */) {
287 *frame_size = 144000 * bitrate / sampling_rate + padding;
288 } else {
289 // V2 or V2.5
290 size_t tmp = (layer == 1 /* L3 */) ? 72000 : 144000;
291 *frame_size = tmp * bitrate / sampling_rate + padding;
292 }
293 }
294
295 if (out_sampling_rate) {
296 *out_sampling_rate = sampling_rate;
297 }
298
299 if (out_channels) {
300 int channel_mode = (header >> 6) & 3;
301
302 *out_channels = (channel_mode == 3) ? 1 : 2;
303 }
304
305 return true;
306 }
307
U32_AT(const uint8_t * ptr)308 static uint32_t U32_AT(const uint8_t *ptr) {
309 return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
310 }
311
312 // Check if the input is valid by checking if it contains a sync word
validate_input(uint8 * buf,uint32 inSize)313 static ERROR_CODE validate_input(uint8 *buf, uint32 inSize)
314 {
315 /*
316 * Verify that at least the header is complete
317 * Note that SYNC_WORD_LNGTH is in unit of bits, but inSize is in unit of bytes.
318 */
319 if (inSize < ((SYNC_WORD_LNGTH + 21) >> 3))
320 {
321 return NO_ENOUGH_MAIN_DATA_ERROR;
322 }
323
324 size_t totalInSize = 0;
325 size_t frameSize = 0;
326 while (totalInSize <= (inSize - 4)) {
327 if (!parseHeader(U32_AT(buf + totalInSize), &frameSize)) {
328 return SYNCH_LOST_ERROR;
329 }
330 // Buffer needs to be large enough to include complete frame
331 if ((frameSize > inSize) || (totalInSize > (inSize - frameSize))) {
332 return SYNCH_LOST_ERROR;
333 }
334 totalInSize += frameSize;
335 }
336
337 return NO_DECODING_ERROR;
338 }
339
pvmp3_framedecoder(tPVMP3DecoderExternal * pExt,void * pMem)340 ERROR_CODE pvmp3_framedecoder(tPVMP3DecoderExternal *pExt,
341 void *pMem)
342 {
343
344 ERROR_CODE errorCode = NO_DECODING_ERROR;
345
346 int32 crc_error_count = 0;
347 uint32 sent_crc = 0;
348 uint32 computed_crc = 0;
349
350 tmp3dec_chan *pChVars[CHAN];
351 tmp3dec_file *pVars = (tmp3dec_file *)pMem;
352
353 mp3Header info_data;
354 mp3Header *info = &info_data;
355
356 errorCode = validate_input(pExt->pInputBuffer, pExt->inputBufferCurrentLength);
357 if (errorCode != NO_DECODING_ERROR)
358 {
359 pExt->outputFrameSize = 0;
360 return errorCode;
361 }
362
363 pVars->inputStream.pBuffer = pExt->pInputBuffer;
364
365
366 pVars->inputStream.usedBits = pExt->inputBufferUsedLength << 3;
367 pVars->inputStream.inputBufferCurrentLength = pExt->inputBufferCurrentLength;
368
369
370 errorCode = pvmp3_decode_header(&pVars->inputStream,
371 info,
372 &computed_crc);
373
374 if (errorCode != NO_DECODING_ERROR)
375 {
376 pExt->outputFrameSize = 0;
377 return errorCode;
378 }
379
380 pVars->num_channels = (info->mode == MPG_MD_MONO) ? 1 : 2;
381 pExt->num_channels = pVars->num_channels;
382
383 int32 outputFrameSize = (info->version_x == MPEG_1) ?
384 2 * SUBBANDS_NUMBER * FILTERBANK_BANDS :
385 SUBBANDS_NUMBER * FILTERBANK_BANDS;
386
387 outputFrameSize = (info->mode == MPG_MD_MONO) ? outputFrameSize : outputFrameSize << 1;
388
389
390 /*
391 * Check if output buffer has enough room to hold output PCM
392 */
393 if (pExt->outputFrameSize >= outputFrameSize)
394 {
395 pExt->outputFrameSize = outputFrameSize;
396 }
397 else
398 {
399 pExt->outputFrameSize = 0;
400 return OUTPUT_BUFFER_TOO_SMALL;
401 }
402
403
404 pChVars[ LEFT] = &pVars->perChan[ LEFT];
405 pChVars[RIGHT] = &pVars->perChan[RIGHT];
406
407
408
409
410 if (info->error_protection)
411 {
412 if (!bitsAvailable(&pVars->inputStream, 16))
413 {
414 return SIDE_INFO_ERROR;
415 }
416
417 /*
418 * Get crc content
419 */
420 sent_crc = getUpTo17bits(&pVars->inputStream, 16);
421 }
422
423
424 if (info->layer_description == 3)
425 {
426 int32 gr;
427 int32 ch;
428 uint32 main_data_end;
429 int32 bytes_to_discard;
430 int16 *ptrOutBuffer = pExt->pOutputBuffer;
431
432 /*
433 * Side Information must be extracted from the bitstream and store for use
434 * during the decoded of the associated frame
435 */
436
437 errorCode = pvmp3_get_side_info(&pVars->inputStream,
438 &pVars->sideInfo,
439 info,
440 &computed_crc);
441
442 if (errorCode != NO_DECODING_ERROR)
443 {
444 pExt->outputFrameSize = 0;
445 return errorCode;
446 }
447
448 /*
449 * If CRC was sent, check that matches the one got while parsing data
450 * disable crc if this is the desired mode
451 */
452 if (info->error_protection)
453 {
454 if ((computed_crc != sent_crc) && pExt->crcEnabled)
455 {
456 crc_error_count++;
457 }
458 }
459
460 /*
461 * main data (scalefactors, Huffman coded, etc,) are not necessarily located
462 * adjacent to the side-info. Beginning of main data is located using
463 * field "main_data_begin" of the current frame. The length does not include
464 * header and side info.
465 * "main_data_begin" points to the first bit of main data of a frame. It is a negative
466 * offset in bytes from the first byte of the sync word
467 * main_data_begin = 0 <===> main data start rigth after side info.
468 */
469
470 int32 temp = pvmp3_get_main_data_size(info, pVars);
471
472
473 /*
474 * Check if available data holds a full frame, if not flag an error
475 */
476
477 if ((uint32)pVars->predicted_frame_size > pVars->inputStream.inputBufferCurrentLength)
478 {
479 pExt->outputFrameSize = 0;
480 return NO_ENOUGH_MAIN_DATA_ERROR;
481 }
482
483 /*
484 * Fill in internal circular buffer
485 */
486 fillMainDataBuf(pVars, temp);
487
488
489 main_data_end = pVars->mainDataStream.usedBits >> 3; /* in bytes */
490 if ((main_data_end << 3) < pVars->mainDataStream.usedBits)
491 {
492 main_data_end++;
493 pVars->mainDataStream.usedBits = main_data_end << 3;
494 }
495
496
497 // force signed computation; buffer sizes and offsets are all going to be
498 // well within the constraints of 32-bit signed math.
499 bytes_to_discard = pVars->frame_start
500 - ((int32)pVars->sideInfo.main_data_begin)
501 - ((int32)main_data_end);
502
503
504 if (main_data_end > BUFSIZE) /* check overflow on the buffer */
505 {
506 pVars->frame_start -= BUFSIZE;
507
508 pVars->mainDataStream.usedBits -= (BUFSIZE << 3);
509 }
510
511 pVars->frame_start += temp;
512
513
514 if (bytes_to_discard < 0 || crc_error_count)
515 {
516 /*
517 * Not enough data to decode, then we should avoid reading this
518 * data ( getting/ignoring sido info and scale data)
519 * Main data could be located in the previous frame, so an unaccounted
520 * frame can cause incorrect processing
521 * Just run the polyphase filter to "clean" the history buffer
522 */
523 errorCode = NO_ENOUGH_MAIN_DATA_ERROR;
524
525 /*
526 * Clear the input to these filters
527 */
528
529 pv_memset((void*)pChVars[RIGHT]->work_buf_int32,
530 0,
531 SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[RIGHT]->work_buf_int32[0]));
532
533 pv_memset((void*)pChVars[LEFT]->work_buf_int32,
534 0,
535 SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[LEFT]->work_buf_int32[0]));
536
537 /* clear circular buffers, to avoid any glitch */
538 pv_memset((void*)&pChVars[ LEFT]->circ_buffer[576],
539 0,
540 480*sizeof(pChVars[ LEFT]->circ_buffer[0]));
541 pv_memset((void*)&pChVars[RIGHT]->circ_buffer[576],
542 0,
543 480*sizeof(pChVars[RIGHT]->circ_buffer[0]));
544
545 pChVars[ LEFT]->used_freq_lines = 575;
546 pChVars[RIGHT]->used_freq_lines = 575;
547
548 }
549 else
550 {
551 pVars->mainDataStream.usedBits += (bytes_to_discard << 3);
552 }
553
554 /*
555 * if (fr_ps->header->version_x == MPEG_1), use 2 granules, otherwise just 1
556 */
557 for (gr = 0; gr < (1 + !(info->version_x)); gr++)
558 {
559 if (errorCode != NO_ENOUGH_MAIN_DATA_ERROR)
560 {
561 for (ch = 0; ch < pVars->num_channels; ch++)
562 {
563 int32 part2_start = pVars->mainDataStream.usedBits;
564
565 if (info->version_x == MPEG_1)
566 {
567
568 pvmp3_get_scale_factors(&pVars->scaleFactors[ch],
569 &pVars->sideInfo,
570 gr,
571 ch,
572 &pVars->mainDataStream);
573 }
574 else
575 {
576 int32 * tmp = pVars->Scratch_mem;
577 pvmp3_mpeg2_get_scale_factors(&pVars->scaleFactors[ch],
578 &pVars->sideInfo,
579 gr,
580 ch,
581 info,
582 (uint32 *)tmp,
583 &pVars->mainDataStream);
584 }
585
586 pChVars[ch]->used_freq_lines = pvmp3_huffman_parsing(pChVars[ch]->work_buf_int32,
587 &pVars->sideInfo.ch[ch].gran[gr],
588 pVars,
589 part2_start,
590 info);
591
592
593 pvmp3_dequantize_sample(pChVars[ch]->work_buf_int32,
594 &pVars->scaleFactors[ch],
595 &pVars->sideInfo.ch[ch].gran[gr],
596 pChVars[ch]->used_freq_lines,
597 info);
598
599
600
601
602 } /* for (ch=0; ch<stereo; ch++) */
603
604 if (pVars->num_channels == 2)
605 {
606
607 int32 used_freq_lines = (pChVars[ LEFT]->used_freq_lines >
608 pChVars[RIGHT]->used_freq_lines) ?
609 pChVars[ LEFT]->used_freq_lines :
610 pChVars[RIGHT]->used_freq_lines;
611
612 pChVars[ LEFT]->used_freq_lines = used_freq_lines;
613 pChVars[RIGHT]->used_freq_lines = used_freq_lines;
614
615 if (info->version_x == MPEG_1)
616 {
617 pvmp3_stereo_proc(pChVars[ LEFT]->work_buf_int32,
618 pChVars[RIGHT]->work_buf_int32,
619 &pVars->scaleFactors[RIGHT],
620 &pVars->sideInfo.ch[LEFT].gran[gr],
621 used_freq_lines,
622 info);
623 }
624 else
625 {
626 int32 * tmp = pVars->Scratch_mem;
627 pvmp3_mpeg2_stereo_proc(pChVars[ LEFT]->work_buf_int32,
628 pChVars[RIGHT]->work_buf_int32,
629 &pVars->scaleFactors[RIGHT],
630 &pVars->sideInfo.ch[ LEFT].gran[gr],
631 &pVars->sideInfo.ch[RIGHT].gran[gr],
632 (uint32 *)tmp,
633 used_freq_lines,
634 info);
635 }
636 }
637
638 } /* if ( errorCode != NO_ENOUGH_MAIN_DATA_ERROR) */
639
640 for (ch = 0; ch < pVars->num_channels; ch++)
641 {
642
643 pvmp3_reorder(pChVars[ch]->work_buf_int32,
644 &pVars->sideInfo.ch[ch].gran[gr],
645 &pChVars[ ch]->used_freq_lines,
646 info,
647 pVars->Scratch_mem);
648
649 pvmp3_alias_reduction(pChVars[ch]->work_buf_int32,
650 &pVars->sideInfo.ch[ch].gran[gr],
651 &pChVars[ ch]->used_freq_lines,
652 info);
653
654
655 /*
656 * IMDCT
657 */
658 /* set mxposition
659 * In case of mixed blocks, # of bands with long
660 * blocks (2 or 4) else 0
661 */
662 uint16 mixedBlocksLongBlocks = 0; /* 0 = long or short, 2=mixed, 4=mixed 2.5@8000 */
663 if (pVars->sideInfo.ch[ch].gran[gr].mixed_block_flag &&
664 pVars->sideInfo.ch[ch].gran[gr].window_switching_flag)
665 {
666 if ((info->version_x == MPEG_2_5) && (info->sampling_frequency == 2))
667 {
668 mixedBlocksLongBlocks = 4; /* mpeg2.5 @ 8 KHz */
669 }
670 else
671 {
672 mixedBlocksLongBlocks = 2;
673 }
674 }
675
676 pvmp3_imdct_synth(pChVars[ch]->work_buf_int32,
677 pChVars[ch]->overlap,
678 pVars->sideInfo.ch[ch].gran[gr].block_type,
679 mixedBlocksLongBlocks,
680 pChVars[ ch]->used_freq_lines,
681 pVars->Scratch_mem);
682
683
684 /*
685 * Polyphase synthesis
686 */
687
688 pvmp3_poly_phase_synthesis(pChVars[ch],
689 pVars->num_channels,
690 pExt->equalizerType,
691 &ptrOutBuffer[ch]);
692
693
694 }/* end ch loop */
695
696 ptrOutBuffer += pVars->num_channels * SUBBANDS_NUMBER * FILTERBANK_BANDS;
697 } /* for (gr=0;gr<Max_gr;gr++) */
698
699 /* skip ancillary data */
700 if (info->bitrate_index > 0)
701 { /* if not free-format */
702
703 int32 ancillary_data_lenght = pVars->predicted_frame_size << 3;
704
705 ancillary_data_lenght -= pVars->inputStream.usedBits;
706
707 /* skip ancillary data */
708 if (ancillary_data_lenght > 0)
709 {
710 pVars->inputStream.usedBits += ancillary_data_lenght;
711 }
712
713 }
714
715 /*
716 * This overrides a possible NO_ENOUGH_MAIN_DATA_ERROR
717 */
718 errorCode = NO_DECODING_ERROR;
719
720 }
721 else
722 {
723 /*
724 * The info on the header leads to an unsupported layer, more data
725 * will not fix this, so this is a bad frame,
726 */
727
728 pExt->outputFrameSize = 0;
729 return UNSUPPORTED_LAYER;
730 }
731
732 pExt->inputBufferUsedLength = pVars->inputStream.usedBits >> 3;
733 pExt->totalNumberOfBitsUsed += pVars->inputStream.usedBits;
734 pExt->version = info->version_x;
735 pExt->samplingRate = mp3_s_freq[info->version_x][info->sampling_frequency];
736 pExt->bitRate = mp3_bitrate[pExt->version][info->bitrate_index];
737
738
739 /*
740 * Always verify buffer overrun condition
741 */
742
743 if (pExt->inputBufferUsedLength > pExt->inputBufferCurrentLength)
744 {
745 pExt->outputFrameSize = 0;
746 errorCode = NO_ENOUGH_MAIN_DATA_ERROR;
747 }
748
749 return errorCode;
750
751 }
752
753
754 /*----------------------------------------------------------------------------
755 ; FUNCTION CODE
756 ----------------------------------------------------------------------------*/
757
fillDataBuf(tmp3Bits * pMainData,uint32 val)758 __inline void fillDataBuf(tmp3Bits *pMainData,
759 uint32 val) /* val to write into the buffer */
760 {
761 pMainData->pBuffer[module(pMainData->offset++, BUFSIZE)] = (uint8)val;
762 }
763
764
fillMainDataBuf(void * pMem,int32 temp)765 void fillMainDataBuf(void *pMem, int32 temp)
766 {
767 tmp3dec_file *pVars = (tmp3dec_file *)pMem;
768
769
770 int32 offset = (pVars->inputStream.usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
771
772 /*
773 * Check if input circular buffer boundaries need to be enforced
774 */
775 if ((offset + temp) < BUFSIZE)
776 {
777 uint8 * ptr = pVars->inputStream.pBuffer + offset;
778
779 offset = pVars->mainDataStream.offset;
780
781 /*
782 * Check if main data circular buffer boundaries need to be enforced
783 */
784 if ((offset + temp) < BUFSIZE)
785 {
786 pv_memcpy((pVars->mainDataStream.pBuffer + offset), ptr, temp*sizeof(uint8));
787 pVars->mainDataStream.offset += temp;
788 }
789 else
790 {
791 for (int32 nBytes = temp; nBytes != 0; nBytes--) /* read main data. */
792 {
793 int32 tmp = *(ptr++);
794 fillDataBuf(&pVars->mainDataStream, tmp);
795 }
796
797 /* adjust circular buffer counter */
798 pVars->mainDataStream.offset = module(pVars->mainDataStream.offset, BUFSIZE);
799 }
800 }
801 else
802 {
803 for (int32 nBytes = temp; nBytes != 0; nBytes--) /* read main data. */
804 {
805 fillDataBuf(&pVars->mainDataStream, *(pVars->inputStream.pBuffer + module(offset++ , BUFSIZE)));
806 }
807 }
808
809
810 pVars->inputStream.usedBits += (temp) << INBUF_ARRAY_INDEX_SHIFT;
811 }
812
813
814
815
816 /*----------------------------------------------------------------------------
817 ; FUNCTION CODE
818 ----------------------------------------------------------------------------*/
819
pvmp3_decoderMemRequirements(void)820 uint32 pvmp3_decoderMemRequirements(void)
821 {
822 uint32 size;
823
824 size = (uint32) sizeof(tmp3dec_file);
825 return (size);
826 }
827
828
829
830 /*----------------------------------------------------------------------------
831 ; FUNCTION CODE
832 ----------------------------------------------------------------------------*/
833
834 #include "pvmp3_decode_huff_cw.h"
835
pvmp3_InitDecoder(tPVMP3DecoderExternal * pExt,void * pMem)836 void pvmp3_InitDecoder(tPVMP3DecoderExternal *pExt,
837 void *pMem)
838 {
839
840 tmp3dec_file *pVars;
841 huffcodetab *pHuff;
842
843 pVars = (tmp3dec_file *)pMem;
844 memset(pVars, 0, sizeof(*pVars));
845
846 pExt->totalNumberOfBitsUsed = 0;
847 pExt->inputBufferCurrentLength = 0;
848 pExt->inputBufferUsedLength = 0;
849
850 pVars->inputStream.pBuffer = pExt->pInputBuffer;
851
852 /*
853 * Initialize huffman decoding table
854 */
855
856 pHuff = pVars->ht;
857 pHuff[0].linbits = 0;
858 pHuff[0].pdec_huff_tab = pvmp3_decode_huff_cw_tab0;
859 pHuff[1].linbits = 0;
860 pHuff[1].pdec_huff_tab = pvmp3_decode_huff_cw_tab1;
861 pHuff[2].linbits = 0;
862 pHuff[2].pdec_huff_tab = pvmp3_decode_huff_cw_tab2;
863 pHuff[3].linbits = 0;
864 pHuff[3].pdec_huff_tab = pvmp3_decode_huff_cw_tab3;
865 pHuff[4].linbits = 0;
866 pHuff[4].pdec_huff_tab = pvmp3_decode_huff_cw_tab0; /* tbl 4 is not used */
867 pHuff[5].linbits = 4;
868 pHuff[5].pdec_huff_tab = pvmp3_decode_huff_cw_tab5;
869 pHuff[6].linbits = 0;
870 pHuff[6].pdec_huff_tab = pvmp3_decode_huff_cw_tab6;
871 pHuff[7].linbits = 0;
872 pHuff[7].pdec_huff_tab = pvmp3_decode_huff_cw_tab7;
873 pHuff[8].linbits = 0;
874 pHuff[8].pdec_huff_tab = pvmp3_decode_huff_cw_tab8;
875 pHuff[9].linbits = 0;
876 pHuff[9].pdec_huff_tab = pvmp3_decode_huff_cw_tab9;
877 pHuff[10].linbits = 0;
878 pHuff[10].pdec_huff_tab = pvmp3_decode_huff_cw_tab10;
879 pHuff[11].linbits = 0;
880 pHuff[11].pdec_huff_tab = pvmp3_decode_huff_cw_tab11;
881 pHuff[12].linbits = 0;
882 pHuff[12].pdec_huff_tab = pvmp3_decode_huff_cw_tab12;
883 pHuff[13].linbits = 0;
884 pHuff[13].pdec_huff_tab = pvmp3_decode_huff_cw_tab13;
885 pHuff[14].linbits = 0;
886 pHuff[14].pdec_huff_tab = pvmp3_decode_huff_cw_tab0; /* tbl 14 is not used */
887 pHuff[15].linbits = 0;
888 pHuff[15].pdec_huff_tab = pvmp3_decode_huff_cw_tab15;
889 pHuff[16].linbits = 1;
890 pHuff[16].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
891 pHuff[17].linbits = 2;
892 pHuff[17].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
893 pHuff[18].linbits = 3;
894 pHuff[18].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
895 pHuff[19].linbits = 4;
896 pHuff[19].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
897 pHuff[20].linbits = 6;
898 pHuff[20].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
899 pHuff[21].linbits = 8;
900 pHuff[21].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
901 pHuff[22].linbits = 10;
902 pHuff[22].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
903 pHuff[23].linbits = 13;
904 pHuff[23].pdec_huff_tab = pvmp3_decode_huff_cw_tab16;
905 pHuff[24].linbits = 4;
906 pHuff[24].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
907 pHuff[25].linbits = 5;
908 pHuff[25].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
909 pHuff[26].linbits = 6;
910 pHuff[26].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
911 pHuff[27].linbits = 7;
912 pHuff[27].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
913 pHuff[28].linbits = 8;
914 pHuff[28].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
915 pHuff[29].linbits = 9;
916 pHuff[29].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
917 pHuff[30].linbits = 11;
918 pHuff[30].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
919 pHuff[31].linbits = 13;
920 pHuff[31].pdec_huff_tab = pvmp3_decode_huff_cw_tab24;
921 pHuff[32].linbits = 0;
922 pHuff[32].pdec_huff_tab = pvmp3_decode_huff_cw_tab32;
923 pHuff[33].linbits = 0;
924 pHuff[33].pdec_huff_tab = pvmp3_decode_huff_cw_tab33;
925
926 /*
927 * Initialize polysynthesis circular buffer mechanism
928 */
929 /* clear buffers */
930
931 pvmp3_resetDecoder(pMem);
932
933 }
934
935
936 /*----------------------------------------------------------------------------
937 ; FUNCTION CODE
938 ----------------------------------------------------------------------------*/
939
940
pvmp3_resetDecoder(void * pMem)941 void pvmp3_resetDecoder(void *pMem)
942 {
943
944 tmp3dec_file *pVars;
945 tmp3dec_chan *pChVars[CHAN];
946
947 pVars = (tmp3dec_file *)pMem;
948 pChVars[ LEFT] = &pVars->perChan[ LEFT];
949 pChVars[RIGHT] = &pVars->perChan[RIGHT];
950
951 pVars->frame_start = 0;
952
953 pVars->mainDataStream.offset = 0;
954
955 pVars->mainDataStream.pBuffer = pVars->mainDataBuffer;
956 pVars->mainDataStream.usedBits = 0;
957
958
959 pVars->inputStream.usedBits = 0; // in bits
960
961
962 pChVars[ LEFT]->used_freq_lines = 575;
963 pChVars[RIGHT]->used_freq_lines = 575;
964
965
966 /*
967 * Initialize polysynthesis circular buffer mechanism
968 */
969
970 pv_memset((void*)&pChVars[ LEFT]->circ_buffer[576],
971 0,
972 480*sizeof(pChVars[ LEFT]->circ_buffer[0]));
973 pv_memset((void*)&pChVars[RIGHT]->circ_buffer[576],
974 0,
975 480*sizeof(pChVars[RIGHT]->circ_buffer[0]));
976
977
978 pv_memset((void*)pChVars[ LEFT]->overlap,
979 0,
980 SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[ LEFT]->overlap[0]));
981
982
983 pv_memset((void*)pChVars[ RIGHT]->overlap,
984 0,
985 SUBBANDS_NUMBER*FILTERBANK_BANDS*sizeof(pChVars[ RIGHT]->overlap[0]));
986
987
988
989
990
991 /*
992 * Clear all the structures
993 */
994
995
996 pv_memset((void*)&pVars->scaleFactors[RIGHT],
997 0,
998 sizeof(mp3ScaleFactors));
999
1000 pv_memset((void*)&pVars->scaleFactors[LEFT],
1001 0,
1002 sizeof(mp3ScaleFactors));
1003
1004 pv_memset((void*)&pVars->sideInfo,
1005 0,
1006 sizeof(mp3SideInfo));
1007
1008 pv_memset((void*)&pVars->sideInfo,
1009 0,
1010 sizeof(mp3SideInfo));
1011
1012 }
1013