2 Copyright (c) 2005, The Musepack Development Team
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
17 * Neither the name of the The Musepack Development Team nor the
18 names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /// \file mpc_decoder.c
36 /// Core decoding routines and logic.
44 extern const HuffmanTyp
* mpc_table_HuffQ
[2] [8];
45 extern const HuffmanTyp mpc_table_HuffHdr
[10];
46 extern const HuffmanTyp mpc_table_HuffSCFI
[ 4];
47 extern const HuffmanTyp mpc_table_HuffDSCF
[16];
50 #ifdef MPC_SUPPORT_SV456
52 extern const HuffmanTyp
* mpc_table_SampleHuff
[18];
53 extern const HuffmanTyp mpc_table_SCFI_Bundle
[ 8];
54 extern const HuffmanTyp mpc_table_DSCF_Entropie
[13];
55 extern const HuffmanTyp mpc_table_Region_A
[16];
56 extern const HuffmanTyp mpc_table_Region_B
[ 8];
57 extern const HuffmanTyp mpc_table_Region_C
[ 4];
61 #ifndef MPC_LITTLE_ENDIAN
62 #define SWAP(X) mpc_swap32(X)
68 #define SCF_DIFF(SCF, D) (SCF == 127 ? 127 : SCF + D)
70 #define SCF_DIFF(SCF, D) SCF + D
73 #define LOOKUP(x, e, q) mpc_decoder_make_huffman_lookup ( (q), sizeof(q), (x), (e) )
74 #define Decode_DSCF() HUFFMAN_DECODE_FASTEST ( d, mpc_table_HuffDSCF, LUTDSCF, 6 )
75 #define HUFFMAN_DECODE_FASTEST(d,a,b,c) mpc_decoder_huffman_decode_fastest ( (d), (a), (b), 32-(c) )
76 #define HUFFMAN_DECODE_FASTERER(d,a,b,c) mpc_decoder_huffman_decode_fasterer ( (d), (a), (b), 32-(c) )
78 mpc_uint8_t LUT1_0
[1<< 6] IBSS_ATTR_MPC_LARGE_IRAM
;
79 mpc_uint8_t LUT1_1
[1<< 9] IBSS_ATTR_MPC_LARGE_IRAM
; // 576 Bytes
80 mpc_uint8_t LUT2_0
[1<< 7] IBSS_ATTR_MPC_LARGE_IRAM
;
81 mpc_uint8_t LUT2_1
[1<<10] IBSS_ATTR_MPC_LARGE_IRAM
; // 1152 Bytes
82 mpc_uint8_t LUT3_0
[1<< 4] IBSS_ATTR_MPC_LARGE_IRAM
;
83 mpc_uint8_t LUT3_1
[1<< 5] IBSS_ATTR_MPC_LARGE_IRAM
; // 48 Bytes
84 mpc_uint8_t LUT4_0
[1<< 4] IBSS_ATTR_MPC_LARGE_IRAM
;
85 mpc_uint8_t LUT4_1
[1<< 5] IBSS_ATTR_MPC_LARGE_IRAM
; // 48 Bytes
86 mpc_uint8_t LUT5_0
[1<< 6] IBSS_ATTR_MPC_LARGE_IRAM
;
87 mpc_uint8_t LUT5_1
[1<< 8] IBSS_ATTR_MPC_LARGE_IRAM
; // 320 Bytes
88 mpc_uint8_t LUT6_0
[1<< 7] IBSS_ATTR_MPC_LARGE_IRAM
;
89 mpc_uint8_t LUT6_1
[1<< 7] IBSS_ATTR_MPC_LARGE_IRAM
; // 256 Bytes
90 mpc_uint8_t LUT7_0
[1<< 8] IBSS_ATTR_MPC_LARGE_IRAM
;
91 mpc_uint8_t LUT7_1
[1<< 8] IBSS_ATTR_MPC_LARGE_IRAM
; // 512 Bytes
92 mpc_uint8_t LUTDSCF
[1<< 6] IBSS_ATTR_MPC_LARGE_IRAM
; // 64 Bytes = 2976 Bytes
94 //------------------------------------------------------------------------------
96 //------------------------------------------------------------------------------
99 SEEK_PRE_DECODE
= 33, // number of frames to be pre-decoded
100 MEMSIZE
= MPC_DECODER_MEMSIZE
, // overall buffer size
101 MEMSIZE2
= (MEMSIZE
/2), // size of one buffer
102 MEMMASK
= (MEMSIZE
-1)
105 //------------------------------------------------------------------------------
106 // forward declarations
107 //------------------------------------------------------------------------------
108 void mpc_decoder_read_bitstream_sv6(mpc_decoder
*d
);
109 void mpc_decoder_read_bitstream_sv7(mpc_decoder
*d
, mpc_bool_t fastSeeking
);
110 void mpc_decoder_update_buffer(mpc_decoder
*d
);
111 mpc_bool_t
mpc_decoder_seek_sample(mpc_decoder
*d
, mpc_int64_t destsample
);
112 void mpc_decoder_requantisierung(mpc_decoder
*d
, const mpc_int32_t Last_Band
);
113 void mpc_decoder_seek_to(mpc_decoder
*d
, mpc_uint32_t bitPos
);
114 void mpc_decoder_seek_forward(mpc_decoder
*d
, mpc_uint32_t bits
);
115 mpc_uint32_t
mpc_decoder_jump_frame(mpc_decoder
*d
);
116 void mpc_decoder_fill_buffer(mpc_decoder
*d
);
117 void mpc_decoder_reset_state(mpc_decoder
*d
);
118 static mpc_uint32_t
get_initial_fpos(mpc_decoder
*d
, mpc_uint32_t StreamVersion
);
119 static inline mpc_int32_t
mpc_decoder_huffman_decode_fastest(mpc_decoder
*d
, const HuffmanTyp
* Table
, const mpc_uint8_t
* tab
, mpc_uint16_t unused_bits
);
120 static void mpc_move_next(mpc_decoder
*d
);
122 mpc_uint32_t Seekbuffer
[MPC_SEEK_BUFFER_SIZE
];
123 mpc_uint32_t Speicher
[MPC_DECODER_MEMSIZE
];
124 MPC_SAMPLE_FORMAT Y_L
[36][32] IBSS_ATTR_MPC_LARGE_IRAM
;
125 MPC_SAMPLE_FORMAT Y_R
[36][32] IBSS_ATTR_MPC_LARGE_IRAM
;
127 //------------------------------------------------------------------------------
129 //------------------------------------------------------------------------------
130 static mpc_int32_t
f_read(mpc_decoder
*d
, void *ptr
, size_t size
)
132 return d
->r
->read(d
->r
->data
, ptr
, size
);
135 static mpc_bool_t
f_seek(mpc_decoder
*d
, mpc_int32_t offset
)
137 return d
->r
->seek(d
->r
->data
, offset
);
140 static mpc_int32_t
f_read_dword(mpc_decoder
*d
, mpc_uint32_t
* ptr
, mpc_uint32_t count
)
142 count
= f_read(d
, ptr
, count
<< 2) >> 2;
146 //------------------------------------------------------------------------------
147 // huffman & bitstream functions
148 //------------------------------------------------------------------------------
149 static const mpc_uint32_t mask
[33] ICONST_ATTR
= {
150 0x00000000, 0x00000001, 0x00000003, 0x00000007,
151 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
152 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
153 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
154 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
155 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
156 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
157 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
161 /* F U N C T I O N S */
163 // resets bitstream decoding
165 mpc_decoder_reset_bitstream_decode(mpc_decoder
*d
)
174 // reports the number of read bits
176 mpc_decoder_bits_read(mpc_decoder
*d
)
178 return 32 * d
->WordsRead
+ d
->pos
;
181 static void mpc_move_next(mpc_decoder
*d
) {
182 d
->Zaehler
= (d
->Zaehler
+ 1) & MEMMASK
;
184 d
->next
= SWAP(d
->Speicher
[(d
->Zaehler
+ 1) & MEMMASK
]);
189 // read desired number of bits out of the bitstream
190 static inline mpc_uint32_t
191 mpc_decoder_bitstream_read(mpc_decoder
*d
, const mpc_uint32_t bits
)
193 mpc_uint32_t out
= d
->dword
;
198 out
>>= (32 - d
->pos
);
204 out
|= d
->dword
>> (32 - d
->pos
);
208 return out
& mask
[bits
];
212 mpc_decoder_make_huffman_lookup(
213 mpc_uint8_t
* lookup
, size_t length
, const HuffmanTyp
* Table
, size_t elements
)
216 size_t idx
= elements
;
217 mpc_uint32_t dval
= (mpc_uint32_t
)0x80000000L
/ length
* 2;
218 mpc_uint32_t val
= dval
- 1;
220 for ( i
= 0; i
< length
; i
++, val
+= dval
) {
221 while ( idx
> 0 && val
>= Table
[idx
-1].Code
)
223 *lookup
++ = (mpc_uint8_t
)idx
;
229 #ifdef MPC_SUPPORT_SV456
230 // decode SCFI-bundle (sv4,5,6)
232 mpc_decoder_scfi_bundle_read(
234 const HuffmanTyp
* Table
, mpc_int8_t
* SCFI
, mpc_bool_t
* DSCF
)
236 // load preview and decode
237 mpc_uint32_t code
= d
->dword
<< d
->pos
;
240 code
|= d
->next
>> (32 - d
->pos
);
242 while (code
< Table
->Code
) {
246 // set the new position within bitstream without performing a dummy-read
247 if ((d
->pos
+= Table
->Length
) >= 32) {
251 *SCFI
= Table
->Value
>> 1;
252 *DSCF
= Table
->Value
& 1;
255 // basic huffman decoding routine
256 // works with maximum lengths up to 14
258 mpc_decoder_huffman_decode(mpc_decoder
*d
, const HuffmanTyp
*Table
)
260 // load preview and decode
261 mpc_uint32_t code
= d
->dword
<< d
->pos
;
264 code
|= d
->next
>> (32 - d
->pos
);
266 while (code
< Table
->Code
) {
270 // set the new position within bitstream without performing a dummy-read
271 if ((d
->pos
+= Table
->Length
) >= 32) {
279 // faster huffman through previewing less bits
280 // works with maximum lengths up to 10
282 mpc_decoder_huffman_decode_fast(mpc_decoder
*d
, const HuffmanTyp
* Table
)
284 // load preview and decode
285 mpc_uint32_t code
= d
->dword
<< d
->pos
;
288 code
|= d
->next
>> (32 - d
->pos
);
290 while (code
< Table
->Code
) {
294 // set the new position within bitstream without performing a dummy-read
295 if ((d
->pos
+= Table
->Length
) >= 32) {
302 // even faster huffman through previewing even less bits
303 // works with maximum lengths up to 5
305 mpc_decoder_huffman_decode_faster(mpc_decoder
*d
, const HuffmanTyp
* Table
)
307 // load preview and decode
308 mpc_uint32_t code
= d
->dword
<< d
->pos
;
311 code
|= d
->next
>> (32 - d
->pos
);
313 while (code
< Table
->Code
) {
317 // set the new position within bitstream without performing a dummy-read
318 if ((d
->pos
+= Table
->Length
) >= 32) {
325 /* partial lookup table decode */
327 mpc_decoder_huffman_decode_fasterer(mpc_decoder
*d
, const HuffmanTyp
* Table
, const mpc_uint8_t
* tab
, mpc_uint16_t unused_bits
)
329 // load preview and decode
330 mpc_uint32_t code
= d
->dword
<< d
->pos
;
332 if (d
->pos
> 18) { // preview 14 bits
333 code
|= d
->next
>> (32 - d
->pos
);
336 Table
+= tab
[(size_t)(code
>> unused_bits
) ];
338 while (code
< Table
->Code
) {
342 // set the new position within bitstream without performing a dummy-read
343 if ((d
->pos
+= Table
->Length
) >= 32) {
350 /* full decode using lookup table */
351 static inline mpc_int32_t
352 mpc_decoder_huffman_decode_fastest(mpc_decoder
*d
, const HuffmanTyp
* Table
, const mpc_uint8_t
* tab
, mpc_uint16_t unused_bits
)
354 // load preview and decode
355 mpc_uint32_t code
= d
->dword
<< d
->pos
;
357 if (d
->pos
> unused_bits
) {
358 code
|= d
->next
>> (32 - d
->pos
);
361 Table
+=tab
[(size_t)(code
>> unused_bits
) ];
363 // set the new position within bitstream without performing a dummy-read
364 if ((d
->pos
+= Table
->Length
) >= 32) {
372 mpc_decoder_reset_v(mpc_decoder
*d
)
374 memset(d
->V_L
, 0, sizeof d
->V_L
);
375 memset(d
->V_R
, 0, sizeof d
->V_R
);
379 mpc_decoder_reset_synthesis(mpc_decoder
*d
)
381 mpc_decoder_reset_v(d
);
385 mpc_decoder_reset_y(mpc_decoder
*d
)
387 memset(d
->Y_L
, 0, sizeof Y_L
);
388 memset(d
->Y_R
, 0, sizeof Y_R
);
392 mpc_decoder_reset_globals(mpc_decoder
*d
)
394 mpc_decoder_reset_bitstream_decode(d
);
396 d
->DecodedFrames
= 0;
397 d
->MaxDecodedFrames
= 0;
398 d
->StreamVersion
= 0;
401 memset(d
->Y_L
, 0, sizeof Y_L
);
402 memset(d
->Y_R
, 0, sizeof Y_R
);
403 memset(d
->SCF_Index_L
, 0, sizeof d
->SCF_Index_L
);
404 memset(d
->SCF_Index_R
, 0, sizeof d
->SCF_Index_R
);
405 memset(d
->Res_L
, 0, sizeof d
->Res_L
);
406 memset(d
->Res_R
, 0, sizeof d
->Res_R
);
407 memset(d
->SCFI_L
, 0, sizeof d
->SCFI_L
);
408 memset(d
->SCFI_R
, 0, sizeof d
->SCFI_R
);
409 #ifdef MPC_SUPPORT_SV456
410 memset(d
->DSCF_Flag_L
, 0, sizeof d
->DSCF_Flag_L
);
411 memset(d
->DSCF_Flag_R
, 0, sizeof d
->DSCF_Flag_R
);
413 memset(d
->Q
, 0, sizeof d
->Q
);
414 memset(d
->MS_Flag
, 0, sizeof d
->MS_Flag
);
418 mpc_decoder_decode_frame(mpc_decoder
*d
, mpc_uint32_t
*in_buffer
,
419 mpc_uint32_t in_len
, MPC_SAMPLE_FORMAT
*out_buffer
)
421 mpc_decoder_reset_bitstream_decode(d
);
422 if (in_len
> sizeof(Speicher
)) in_len
= sizeof(Speicher
);
423 memcpy(d
->Speicher
, in_buffer
, in_len
);
424 d
->dword
= SWAP(d
->Speicher
[0]);
425 d
->next
= SWAP(d
->Speicher
[1]);
426 switch (d
->StreamVersion
) {
427 #ifdef MPC_SUPPORT_SV456
431 mpc_decoder_read_bitstream_sv6(d
);
436 mpc_decoder_read_bitstream_sv7(d
, FALSE
);
439 return (mpc_uint32_t
)(-1);
441 mpc_decoder_requantisierung(d
, d
->Max_Band
);
442 mpc_decoder_synthese_filter_float(d
, out_buffer
);
443 return mpc_decoder_bits_read(d
);
447 mpc_decoder_decode_internal(mpc_decoder
*d
, MPC_SAMPLE_FORMAT
*buffer
)
449 mpc_uint32_t output_frame_length
= MPC_FRAME_LENGTH
;
451 mpc_uint32_t FrameBitCnt
= 0;
453 // output the last part of the last frame here, if needed
454 if (d
->last_block_samples
> 0) {
455 output_frame_length
= d
->last_block_samples
;
456 d
->last_block_samples
= 0; // it's going to be handled now, so reset it
457 if (!d
->TrueGaplessPresent
) {
458 mpc_decoder_reset_y(d
);
460 mpc_decoder_bitstream_read(d
, 20);
461 mpc_decoder_read_bitstream_sv7(d
, FALSE
);
462 mpc_decoder_requantisierung(d
, d
->Max_Band
);
464 mpc_decoder_synthese_filter_float(d
, buffer
);
465 return output_frame_length
;
468 if (d
->DecodedFrames
>= d
->OverallFrames
) {
469 return (mpc_uint32_t
)(-1); // end of file -> abort decoding
472 if (d
->DecodedFrames
== 0)
474 d
->SeekTable
[0] = mpc_decoder_bits_read(d
);
475 d
->SeekTableCounter
= 0;
478 // read jump-info for validity check of frame
479 d
->FwdJumpInfo
= mpc_decoder_bitstream_read(d
, 20);
481 d
->ActDecodePos
= (d
->Zaehler
<< 5) + d
->pos
;
483 // decode data and check for validity of frame
484 FrameBitCnt
= mpc_decoder_bits_read(d
);
485 switch (d
->StreamVersion
) {
486 #ifdef MPC_SUPPORT_SV456
490 mpc_decoder_read_bitstream_sv6(d
);
495 mpc_decoder_read_bitstream_sv7(d
, FALSE
);
498 return (mpc_uint32_t
)(-1);
500 d
->FrameWasValid
= mpc_decoder_bits_read(d
) - FrameBitCnt
== d
->FwdJumpInfo
;
504 /* update seek table */
505 d
->SeekTableCounter
+= d
->FwdJumpInfo
+ 20;
506 if (0 == ((d
->DecodedFrames
) & (d
->SeekTable_Mask
)))
508 d
->SeekTable
[d
->DecodedFrames
>>d
->SeekTable_Step
] = d
->SeekTableCounter
;
509 d
->MaxDecodedFrames
= d
->DecodedFrames
;
510 d
->SeekTableCounter
= 0;
514 mpc_decoder_requantisierung(d
, d
->Max_Band
);
516 mpc_decoder_synthese_filter_float(d
, buffer
);
518 // cut off first MPC_DECODER_SYNTH_DELAY zero-samples
519 if (d
->DecodedFrames
== d
->OverallFrames
&& d
->StreamVersion
>= 6) {
520 // reconstruct exact filelength
521 mpc_int32_t mod_block
= mpc_decoder_bitstream_read(d
, 11);
522 mpc_int32_t FilterDecay
;
524 if (mod_block
== 0) {
528 FilterDecay
= (mod_block
+ MPC_DECODER_SYNTH_DELAY
) % MPC_FRAME_LENGTH
;
530 // additional FilterDecay samples are needed for decay of synthesis filter
531 if (MPC_DECODER_SYNTH_DELAY
+ mod_block
>= MPC_FRAME_LENGTH
) {
532 // this variable will be checked for at the top of the function
533 d
->last_block_samples
= FilterDecay
;
535 else { // there are only FilterDecay samples needed for this frame
536 output_frame_length
= FilterDecay
;
540 if (d
->samples_to_skip
) {
541 if (output_frame_length
< d
->samples_to_skip
) {
542 d
->samples_to_skip
-= output_frame_length
;
543 output_frame_length
= 0;
546 output_frame_length
-= d
->samples_to_skip
;
549 buffer
+ d
->samples_to_skip
,
550 output_frame_length
* sizeof (MPC_SAMPLE_FORMAT
));
552 buffer
+ MPC_FRAME_LENGTH
,
553 buffer
+ MPC_FRAME_LENGTH
+ d
->samples_to_skip
,
554 output_frame_length
* sizeof (MPC_SAMPLE_FORMAT
));
555 d
->samples_to_skip
= 0;
559 return output_frame_length
;
562 mpc_uint32_t
mpc_decoder_decode(
564 MPC_SAMPLE_FORMAT
*buffer
,
565 mpc_uint32_t
*vbr_update_acc
,
566 mpc_uint32_t
*vbr_update_bits
)
570 mpc_uint32_t RING
= d
->Zaehler
;
571 mpc_int32_t vbr_ring
= (RING
<< 5) + d
->pos
;
573 mpc_uint32_t valid_samples
= mpc_decoder_decode_internal(d
, buffer
);
575 if (valid_samples
== (mpc_uint32_t
)(-1) ) {
579 /**************** ERROR CONCEALMENT *****************/
580 if (d
->FrameWasValid
== 0 ) {
581 // error occurred in bitstream
582 return (mpc_uint32_t
)(-1);
585 if (vbr_update_acc
&& vbr_update_bits
) {
586 (*vbr_update_acc
) ++;
587 vbr_ring
= (d
->Zaehler
<< 5) + d
->pos
- vbr_ring
;
591 (*vbr_update_bits
) += vbr_ring
;
595 mpc_decoder_update_buffer(d
);
597 if (valid_samples
> 0) {
598 return valid_samples
;
604 mpc_decoder_requantisierung(mpc_decoder
*d
, const mpc_int32_t Last_Band
)
608 MPC_SAMPLE_FORMAT facL
;
609 MPC_SAMPLE_FORMAT facR
;
610 MPC_SAMPLE_FORMAT templ
;
611 MPC_SAMPLE_FORMAT tempr
;
612 MPC_SAMPLE_FORMAT
* YL
;
613 MPC_SAMPLE_FORMAT
* YR
;
617 #ifdef MPC_FIXED_POINT
618 #if MPC_FIXED_POINT_FRACTPART == 14
619 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
620 MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
623 #error FIXME, Cc table is in 18.14 format
627 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
628 MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
630 // requantization and scaling of subband-samples
631 for ( Band
= 0; Band
<= Last_Band
; Band
++ ) { // setting pointers
632 YL
= d
->Y_L
[0] + Band
;
633 YR
= d
->Y_R
[0] + Band
;
636 /************************** MS-coded **************************/
637 if ( d
->MS_Flag
[Band
] ) {
638 if ( d
->Res_L
[Band
] ) {
639 if ( d
->Res_R
[Band
] ) { // M!=0, S!=0
640 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][0]);
641 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][0]);
642 for ( n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
643 *YL
= (templ
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++))+(tempr
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++));
646 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][1]);
647 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][1]);
648 for ( ; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
649 *YL
= (templ
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++))+(tempr
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++));
652 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][2]);
653 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][2]);
654 for ( ; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
655 *YL
= (templ
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++))+(tempr
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++));
658 } else { // M!=0, S==0
659 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][0]);
660 for ( n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
661 *YR
= *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
663 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][1]);
664 for ( ; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
665 *YR
= *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
667 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][2]);
668 for ( ; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
669 *YR
= *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
673 if (d
->Res_R
[Band
]) // M==0, S!=0
675 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][0]);
676 for ( n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
677 *YR
= - (*YL
= MPC_MULTIPLY_FLOAT_INT(facR
,*(R
++)));
679 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][1]);
680 for ( ; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
681 *YR
= - (*YL
= MPC_MULTIPLY_FLOAT_INT(facR
,*(R
++)));
683 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][2]);
684 for ( ; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
685 *YR
= - (*YL
= MPC_MULTIPLY_FLOAT_INT(facR
,*(R
++)));
687 } else { // M==0, S==0
688 for ( n
= 0; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
694 /************************** LR-coded **************************/
696 if ( d
->Res_L
[Band
] ) {
697 if ( d
->Res_R
[Band
] ) { // L!=0, R!=0
698 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][0]);
699 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][0]);
700 for (n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
701 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
702 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
704 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][1]);
705 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][1]);
706 for (; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
707 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
708 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
710 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][2]);
711 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][2]);
712 for (; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
713 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
714 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
716 } else { // L!=0, R==0
717 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][0]);
718 for ( n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
719 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
722 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][1]);
723 for ( ; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
724 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
727 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][2]);
728 for ( ; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
729 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
735 if ( d
->Res_R
[Band
] ) { // L==0, R!=0
736 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][0]);
737 for ( n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
739 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
741 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][1]);
742 for ( ; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
744 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
746 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][2]);
747 for ( ; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
749 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
751 } else { // L==0, R==0
752 for ( n
= 0; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
761 #ifdef MPC_SUPPORT_SV456
762 static const unsigned char Q_res
[32][16] ICONST_ATTR
= {
763 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
764 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
765 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
766 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
767 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
768 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
769 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
770 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
771 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
772 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
773 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
774 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
775 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
776 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
777 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
778 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
779 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
780 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
781 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
782 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
783 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
784 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
785 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
786 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
787 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
788 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
789 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
790 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
791 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
792 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
793 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
794 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
797 /****************************************** SV 6 ******************************************/
799 mpc_decoder_read_bitstream_sv6(mpc_decoder
*d
)
802 mpc_int32_t Max_used_Band
=0;
803 const HuffmanTyp
*Table
;
804 const HuffmanTyp
*x1
;
805 const HuffmanTyp
*x2
;
810 mpc_int8_t
*ResL
= d
->Res_L
;
811 mpc_int8_t
*ResR
= d
->Res_R
;
813 /************************ HEADER **************************/
816 for (n
=0; n
<= d
->Max_Band
; ++n
, ++ResL
, ++ResR
)
818 if (n
<11) Table
= mpc_table_Region_A
;
819 else if (n
>=11 && n
<=22) Table
= mpc_table_Region_B
;
820 else /*if (n>=23)*/ Table
= mpc_table_Region_C
;
822 *ResL
= Q_res
[n
][mpc_decoder_huffman_decode(d
, Table
)];
824 d
->MS_Flag
[n
] = mpc_decoder_bitstream_read(d
, 1);
826 *ResR
= Q_res
[n
][mpc_decoder_huffman_decode(d
, Table
)];
828 // only perform the following procedure up to the maximum non-zero subband
829 if (*ResL
|| *ResR
) Max_used_Band
= n
;
832 /************************* SCFI-Bundle *****************************/
835 for (n
=0; n
<=Max_used_Band
; ++n
, ++ResL
, ++ResR
) {
836 if (*ResL
) mpc_decoder_scfi_bundle_read(d
, mpc_table_SCFI_Bundle
, &(d
->SCFI_L
[n
]), &(d
->DSCF_Flag_L
[n
]));
837 if (*ResR
) mpc_decoder_scfi_bundle_read(d
, mpc_table_SCFI_Bundle
, &(d
->SCFI_R
[n
]), &(d
->DSCF_Flag_R
[n
]));
840 /***************************** SCFI ********************************/
843 L
= d
->SCF_Index_L
[0];
844 R
= d
->SCF_Index_R
[0];
845 for (n
=0; n
<= Max_used_Band
; ++n
, ++ResL
, ++ResR
, L
+=3, R
+=3)
849 /*********** DSCF ************/
850 if (d
->DSCF_Flag_L
[n
]==1)
852 switch (d
->SCFI_L
[n
])
855 L
[0] = L
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
860 L
[0] = L
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
861 L
[1] = L
[0] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
865 L
[0] = L
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
867 L
[2] = L
[1] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
870 L
[0] = L
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
871 L
[1] = L
[0] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
872 L
[2] = L
[1] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
879 /************ SCF ************/
882 switch (d
->SCFI_L
[n
])
885 L
[0] = mpc_decoder_bitstream_read(d
, 6);
890 L
[0] = mpc_decoder_bitstream_read(d
, 6);
891 L
[1] = mpc_decoder_bitstream_read(d
, 6);
895 L
[0] = mpc_decoder_bitstream_read(d
, 6);
897 L
[2] = mpc_decoder_bitstream_read(d
, 6);
900 L
[0] = mpc_decoder_bitstream_read(d
, 6);
901 L
[1] = mpc_decoder_bitstream_read(d
, 6);
902 L
[2] = mpc_decoder_bitstream_read(d
, 6);
912 /*********** DSCF ************/
913 if (d
->DSCF_Flag_R
[n
]==1)
915 switch (d
->SCFI_R
[n
])
918 R
[0] = R
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
923 R
[0] = R
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
924 R
[1] = R
[0] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
928 R
[0] = R
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
930 R
[2] = R
[1] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
933 R
[0] = R
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
934 R
[1] = R
[0] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
935 R
[2] = R
[1] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
942 /************ SCF ************/
945 switch (d
->SCFI_R
[n
])
948 R
[0] = mpc_decoder_bitstream_read(d
, 6);
953 R
[0] = mpc_decoder_bitstream_read(d
, 6);
954 R
[1] = mpc_decoder_bitstream_read(d
, 6);
958 R
[0] = mpc_decoder_bitstream_read(d
, 6);
960 R
[2] = mpc_decoder_bitstream_read(d
, 6);
963 R
[0] = mpc_decoder_bitstream_read(d
, 6);
964 R
[1] = mpc_decoder_bitstream_read(d
, 6);
965 R
[2] = mpc_decoder_bitstream_read(d
, 6);
975 /**************************** Samples ****************************/
978 for (n
=0; n
<= Max_used_Band
; ++n
, ++ResL
, ++ResR
)
981 x1
= mpc_table_SampleHuff
[*ResL
];
982 x2
= mpc_table_SampleHuff
[*ResR
];
986 if (x1
!=NULL
|| x2
!=NULL
)
989 if (x1
!= NULL
) *QL
++ = mpc_decoder_huffman_decode_fast(d
, x1
);
990 if (x2
!= NULL
) *QR
++ = mpc_decoder_huffman_decode_fast(d
, x2
);
993 if (*ResL
>7 || *ResR
>7)
996 if (*ResL
>7) *QL
++ = (mpc_int16_t
)mpc_decoder_bitstream_read(d
, Res_bit
[*ResL
]) - Dc
[*ResL
];
997 if (*ResR
>7) *QR
++ = (mpc_int16_t
)mpc_decoder_bitstream_read(d
, Res_bit
[*ResR
]) - Dc
[*ResR
];
1001 #endif //MPC_SUPPORT_SV456
1002 /****************************************** SV 7 ******************************************/
1004 mpc_decoder_read_bitstream_sv7(mpc_decoder
*d
, mpc_bool_t fastSeeking
)
1007 mpc_int32_t Max_used_Band
=0;
1008 const HuffmanTyp
*Table
;
1011 mpc_int16_t
*LQ
,*RQ
;
1012 mpc_int8_t
*ResL
,*ResR
;
1015 mpc_uint8_t max_length
;
1017 /***************************** Header *****************************/
1022 *ResL
= mpc_decoder_bitstream_read(d
, 4);
1023 *ResR
= mpc_decoder_bitstream_read(d
, 4);
1024 if (d
->MS_used
&& !(*ResL
==0 && *ResR
==0)) {
1025 d
->MS_Flag
[0] = mpc_decoder_bitstream_read(d
, 1);
1030 // consecutive subbands
1031 ++ResL
; ++ResR
; // increase pointers
1032 for (n
=1; n
<= d
->Max_Band
; ++n
, ++ResL
, ++ResR
)
1034 idx
= mpc_decoder_huffman_decode_fast(d
, mpc_table_HuffHdr
);
1035 *ResL
= (idx
!=4) ? *(ResL
-1) + idx
: (int) mpc_decoder_bitstream_read(d
, 4);
1037 idx
= mpc_decoder_huffman_decode_fast(d
, mpc_table_HuffHdr
);
1038 *ResR
= (idx
!=4) ? *(ResR
-1) + idx
: (int) mpc_decoder_bitstream_read(d
, 4);
1040 if (d
->MS_used
&& !(*ResL
==0 && *ResR
==0)) {
1041 d
->MS_Flag
[n
] = mpc_decoder_bitstream_read(d
, 1);
1044 // only perform following procedures up to the maximum non-zero subband
1045 if (*ResL
!=0 || *ResR
!=0) {
1051 /****************************** SCFI ******************************/
1056 for (n
=0; n
<= Max_used_Band
; ++n
, ++L
, ++R
, ++ResL
, ++ResR
) {
1057 if (*ResL
) *L
= mpc_decoder_huffman_decode_faster(d
, mpc_table_HuffSCFI
);
1058 if (*ResR
) *R
= mpc_decoder_huffman_decode_faster(d
, mpc_table_HuffSCFI
);
1061 /**************************** SCF/DSCF ****************************/
1064 L
= d
->SCF_Index_L
[0];
1065 R
= d
->SCF_Index_R
[0];
1066 for (n
=0; n
<=Max_used_Band
; ++n
, ++ResL
, ++ResR
, L
+=3, R
+=3) {
1069 switch (d
->SCFI_L
[n
])
1072 idx
= Decode_DSCF ();
1073 L
[0] = (idx
!=8) ? SCF_DIFF(L
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1074 idx
= Decode_DSCF ();
1075 L
[1] = (idx
!=8) ? SCF_DIFF(L
[0], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1079 idx
= Decode_DSCF ();
1080 L
[0] = (idx
!=8) ? SCF_DIFF(L
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1085 idx
= Decode_DSCF ();
1086 L
[0] = (idx
!=8) ? SCF_DIFF(L
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1088 idx
= Decode_DSCF ();
1089 L
[2] = (idx
!=8) ? SCF_DIFF(L
[1], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1092 idx
= Decode_DSCF ();
1093 L
[0] = (idx
!=8) ? SCF_DIFF(L
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1094 idx
= Decode_DSCF ();
1095 L
[1] = (idx
!=8) ? SCF_DIFF(L
[0], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1096 idx
= Decode_DSCF ();
1097 L
[2] = (idx
!=8) ? SCF_DIFF(L
[1], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1106 switch (d
->SCFI_R
[n
])
1109 idx
= Decode_DSCF ();
1110 R
[0] = (idx
!=8) ? SCF_DIFF(R
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1111 idx
= Decode_DSCF ();
1112 R
[1] = (idx
!=8) ? SCF_DIFF(R
[0], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1116 idx
= Decode_DSCF ();
1117 R
[0] = (idx
!=8) ? SCF_DIFF(R
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1122 idx
= Decode_DSCF ();
1123 R
[0] = (idx
!=8) ? SCF_DIFF(R
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1125 idx
= Decode_DSCF ();
1126 R
[2] = (idx
!=8) ? SCF_DIFF(R
[1], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1129 idx
= Decode_DSCF ();
1130 R
[0] = (idx
!=8) ? SCF_DIFF(R
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1131 idx
= Decode_DSCF ();
1132 R
[1] = (idx
!=8) ? SCF_DIFF(R
[0], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1133 idx
= Decode_DSCF ();
1134 R
[2] = (idx
!=8) ? SCF_DIFF(R
[1], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1146 /***************************** Samples ****************************/
1151 for (n
=0; n
<= Max_used_Band
; ++n
, ++ResL
, ++ResR
, LQ
+=36, RQ
+=36)
1153 /************** links **************/
1156 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
1157 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
1161 for (k
=0; k
<36; k
++ ) {
1162 tmp
= mpc_random_int(d
);
1163 *LQ
++ = ((tmp
>> 24) & 0xFF) + ((tmp
>> 16) & 0xFF) + ((tmp
>> 8) & 0xFF) + ((tmp
>> 0) & 0xFF) - 510;
1167 LQ
+= 36;// increase pointer
1170 if (mpc_decoder_bitstream_read(d
, 1)) {
1171 Table
= mpc_table_HuffQ
[1][1];
1175 Table
= mpc_table_HuffQ
[0][1];
1179 for (k
=0; k
<12; ++k
)
1181 idx
= HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1188 if (mpc_decoder_bitstream_read(d
, 1)) {
1189 Table
= mpc_table_HuffQ
[1][2];
1193 Table
= mpc_table_HuffQ
[0][2];
1197 for (k
=0; k
<18; ++k
)
1199 idx
= HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1205 if (mpc_decoder_bitstream_read(d
, 1)) {
1206 Table
= mpc_table_HuffQ
[1][3];
1210 Table
= mpc_table_HuffQ
[0][3];
1214 for (k
=0; k
<36; ++k
)
1215 *LQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1218 if (mpc_decoder_bitstream_read(d
, 1)) {
1219 Table
= mpc_table_HuffQ
[1][4];
1223 Table
= mpc_table_HuffQ
[0][4];
1227 for (k
=0; k
<36; ++k
)
1228 *LQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1231 if (mpc_decoder_bitstream_read(d
, 1)) {
1232 Table
= mpc_table_HuffQ
[1][5];
1236 Table
= mpc_table_HuffQ
[0][5];
1240 for (k
=0; k
<36; ++k
)
1241 *LQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1244 if (mpc_decoder_bitstream_read(d
, 1)) {
1245 Table
= mpc_table_HuffQ
[1][6];
1248 for (k
=0; k
<36; ++k
)
1249 *LQ
++ = HUFFMAN_DECODE_FASTERER ( d
, Table
, LUT
, max_length
);
1251 Table
= mpc_table_HuffQ
[0][6];
1254 for (k
=0; k
<36; ++k
)
1255 *LQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1259 if (mpc_decoder_bitstream_read(d
, 1)) {
1260 Table
= mpc_table_HuffQ
[1][7];
1263 for (k
=0; k
<36; ++k
)
1264 *LQ
++ = HUFFMAN_DECODE_FASTERER ( d
, Table
, LUT
, max_length
);
1266 Table
= mpc_table_HuffQ
[0][7];
1269 for (k
=0; k
<36; ++k
)
1270 *LQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1273 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
1275 for (k
=0; k
<36; ++k
)
1276 *LQ
++ = (mpc_int16_t
)mpc_decoder_bitstream_read(d
, Res_bit
[*ResL
]) - tmp
;
1281 /************** rechts **************/
1284 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
1285 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
1289 for (k
=0; k
<36; k
++ ) {
1290 tmp
= mpc_random_int(d
);
1291 *RQ
++ = ((tmp
>> 24) & 0xFF) + ((tmp
>> 16) & 0xFF) + ((tmp
>> 8) & 0xFF) + ((tmp
>> 0) & 0xFF) - 510;
1295 RQ
+= 36;// increase pointer
1298 if (mpc_decoder_bitstream_read(d
, 1)) {
1299 Table
= mpc_table_HuffQ
[1][1];
1303 Table
= mpc_table_HuffQ
[0][1];
1307 for (k
=0; k
<12; ++k
)
1309 idx
= HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1316 if (mpc_decoder_bitstream_read(d
, 1)) {
1317 Table
= mpc_table_HuffQ
[1][2];
1321 Table
= mpc_table_HuffQ
[0][2];
1325 for (k
=0; k
<18; ++k
)
1327 idx
= HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1333 if (mpc_decoder_bitstream_read(d
, 1)) {
1334 Table
= mpc_table_HuffQ
[1][3];
1338 Table
= mpc_table_HuffQ
[0][3];
1342 for (k
=0; k
<36; ++k
)
1343 *RQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1346 if (mpc_decoder_bitstream_read(d
, 1)) {
1347 Table
= mpc_table_HuffQ
[1][4];
1351 Table
= mpc_table_HuffQ
[0][4];
1355 for (k
=0; k
<36; ++k
)
1356 *RQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1359 if (mpc_decoder_bitstream_read(d
, 1)) {
1360 Table
= mpc_table_HuffQ
[1][5];
1364 Table
= mpc_table_HuffQ
[0][5];
1368 for (k
=0; k
<36; ++k
)
1369 *RQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1372 if (mpc_decoder_bitstream_read(d
, 1)) {
1373 Table
= mpc_table_HuffQ
[1][6];
1376 for (k
=0; k
<36; ++k
)
1377 *RQ
++ = HUFFMAN_DECODE_FASTERER ( d
, Table
, LUT
, max_length
);
1379 Table
= mpc_table_HuffQ
[0][6];
1382 for (k
=0; k
<36; ++k
)
1383 *RQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1387 if (mpc_decoder_bitstream_read(d
, 1)) {
1388 Table
= mpc_table_HuffQ
[1][7];
1391 for (k
=0; k
<36; ++k
)
1392 *RQ
++ = HUFFMAN_DECODE_FASTERER ( d
, Table
, LUT
, max_length
);
1394 Table
= mpc_table_HuffQ
[0][7];
1397 for (k
=0; k
<36; ++k
)
1398 *RQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1401 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
1403 for (k
=0; k
<36; ++k
)
1404 *RQ
++ = (mpc_int16_t
)mpc_decoder_bitstream_read(d
, Res_bit
[*ResR
]) - tmp
;
1412 void mpc_decoder_setup(mpc_decoder
*d
, mpc_reader
*r
)
1416 d
->MPCHeaderPos
= 0;
1417 d
->StreamVersion
= 0;
1420 d
->ActDecodePos
= 0;
1421 d
->FrameWasValid
= 0;
1422 d
->OverallFrames
= 0;
1423 d
->DecodedFrames
= 0;
1424 d
->MaxDecodedFrames
= 0;
1425 d
->TrueGaplessPresent
= 0;
1426 d
->last_block_samples
= 0;
1439 d
->SeekTable_Step
= 0;
1440 d
->SeekTable_Mask
= 0;
1441 d
->SeekTableCounter
= 0;
1443 mpc_decoder_initialisiere_quantisierungstabellen(d
, 1.0f
);
1445 LOOKUP ( mpc_table_HuffQ
[0][1], 27, LUT1_0
);
1446 LOOKUP ( mpc_table_HuffQ
[1][1], 27, LUT1_1
);
1447 LOOKUP ( mpc_table_HuffQ
[0][2], 25, LUT2_0
);
1448 LOOKUP ( mpc_table_HuffQ
[1][2], 25, LUT2_1
);
1449 LOOKUP ( mpc_table_HuffQ
[0][3], 7, LUT3_0
);
1450 LOOKUP ( mpc_table_HuffQ
[1][3], 7, LUT3_1
);
1451 LOOKUP ( mpc_table_HuffQ
[0][4], 9, LUT4_0
);
1452 LOOKUP ( mpc_table_HuffQ
[1][4], 9, LUT4_1
);
1453 LOOKUP ( mpc_table_HuffQ
[0][5], 15, LUT5_0
);
1454 LOOKUP ( mpc_table_HuffQ
[1][5], 15, LUT5_1
);
1455 LOOKUP ( mpc_table_HuffQ
[0][6], 31, LUT6_0
);
1456 LOOKUP ( mpc_table_HuffQ
[1][6], 31, LUT6_1
);
1457 LOOKUP ( mpc_table_HuffQ
[0][7], 63, LUT7_0
);
1458 LOOKUP ( mpc_table_HuffQ
[1][7], 63, LUT7_1
);
1459 LOOKUP ( mpc_table_HuffDSCF
, 16, LUTDSCF
);
1461 d
->SeekTable
= Seekbuffer
;
1462 d
->Speicher
= Speicher
;
1466 #if defined(CPU_COLDFIRE)
1467 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
1471 static void mpc_decoder_set_streaminfo(mpc_decoder
*d
, mpc_streaminfo
*si
)
1473 mpc_uint32_t seekTableSize
;
1475 mpc_decoder_reset_synthesis(d
);
1476 mpc_decoder_reset_globals(d
);
1478 d
->StreamVersion
= si
->stream_version
;
1479 d
->MS_used
= si
->ms
;
1480 d
->Max_Band
= si
->max_band
;
1481 d
->OverallFrames
= si
->frames
;
1482 d
->MPCHeaderPos
= si
->header_position
;
1483 d
->TrueGaplessPresent
= si
->is_true_gapless
;
1484 d
->SampleRate
= (mpc_int32_t
)si
->sample_freq
;
1486 d
->samples_to_skip
= MPC_DECODER_SYNTH_DELAY
;
1488 memset(d
->SeekTable
, 0, sizeof(Seekbuffer
));
1490 // limit used table size to MPC_SEEK_BUFFER_SIZE
1491 seekTableSize
= min(si
->frames
, MPC_SEEK_BUFFER_SIZE
);
1492 // frames per buffer to not exceed buffer and to be able to seek full file
1493 while ( seekTableSize
< si
->frames
/ (1<<d
->SeekTable_Step
) )
1495 d
->SeekTable_Step
++;
1497 d
->SeekTable_Mask
= (1 << d
->SeekTable_Step
) - 1;
1500 mpc_bool_t
mpc_decoder_initialize(mpc_decoder
*d
, mpc_streaminfo
*si
)
1502 mpc_uint32_t bitPos
;
1505 mpc_decoder_set_streaminfo(d
, si
);
1507 // setting position to the beginning of the data-bitstream
1508 bitPos
= get_initial_fpos(d
, d
->StreamVersion
);
1511 // fill buffer and initialize decoder
1512 f_seek(d
, fpos
*4 + d
->MPCHeaderPos
);
1513 f_read_dword(d
, d
->Speicher
, MEMSIZE
);
1516 d
->pos
= bitPos
& 31;
1517 d
->WordsRead
= fpos
;
1518 d
->dword
= SWAP(d
->Speicher
[0]);
1519 d
->next
= SWAP(d
->Speicher
[1]);
1524 // jumps over the current frame
1525 mpc_uint32_t
mpc_decoder_jump_frame(mpc_decoder
*d
) {
1527 mpc_uint32_t frameSize
;
1529 // ensure the buffer is full
1530 mpc_decoder_update_buffer(d
);
1533 frameSize
= mpc_decoder_bitstream_read(d
, 20);
1536 mpc_decoder_seek_forward(d
, frameSize
);
1538 return frameSize
+ 20;
1542 static mpc_uint32_t
get_initial_fpos(mpc_decoder
*d
, mpc_uint32_t StreamVersion
)
1544 mpc_uint32_t fpos
= 0;
1545 (void) StreamVersion
;
1547 // setting position to the beginning of the data-bitstream
1548 switch ( d
->StreamVersion
) {
1549 case 0x04: fpos
= 48; break;
1551 case 0x06: fpos
= 64; break;
1553 case 0x17: fpos
= 200; break;
1558 mpc_bool_t
mpc_decoder_seek_seconds(mpc_decoder
*d
, double seconds
)
1560 return mpc_decoder_seek_sample(d
, (mpc_int64_t
)(seconds
* (double)d
->SampleRate
+ 0.5));
1563 void mpc_decoder_reset_state(mpc_decoder
*d
) {
1565 memset(d
->Y_L
, 0, sizeof Y_L
);
1566 memset(d
->Y_R
, 0, sizeof Y_R
);
1568 memset(d
->SCF_Index_L
, 127, sizeof d
->SCF_Index_L
);
1569 memset(d
->SCF_Index_R
, 127, sizeof d
->SCF_Index_R
);
1571 memset(d
->SCF_Index_L
, 0, sizeof d
->SCF_Index_L
);
1572 memset(d
->SCF_Index_R
, 0, sizeof d
->SCF_Index_R
);
1574 memset(d
->Res_L
, 0, sizeof d
->Res_L
);
1575 memset(d
->Res_R
, 0, sizeof d
->Res_R
);
1576 memset(d
->SCFI_L
, 0, sizeof d
->SCFI_L
);
1577 memset(d
->SCFI_R
, 0, sizeof d
->SCFI_R
);
1578 #ifdef MPC_SUPPORT_SV456
1579 memset(d
->DSCF_Flag_L
, 0, sizeof d
->DSCF_Flag_L
);
1580 memset(d
->DSCF_Flag_R
, 0, sizeof d
->DSCF_Flag_R
);
1582 memset(d
->Q
, 0, sizeof d
->Q
);
1583 memset(d
->MS_Flag
, 0, sizeof d
->MS_Flag
);
1587 mpc_bool_t
mpc_decoder_seek_sample(mpc_decoder
*d
, mpc_int64_t destsample
)
1589 mpc_uint32_t fpos
= 0; // the bit to seek to
1590 mpc_uint32_t seekFrame
= 0; // the frame to seek to
1591 mpc_uint32_t lastFrame
= 0; // last frame to seek to before scanning scale factors
1592 mpc_int32_t delta
= 0; // direction of seek
1594 destsample
+= MPC_DECODER_SYNTH_DELAY
;
1595 seekFrame
= (mpc_uint32_t
) ((destsample
) / MPC_FRAME_LENGTH
);
1596 d
->samples_to_skip
= (mpc_uint32_t
)((destsample
) % MPC_FRAME_LENGTH
);
1598 // prevent from desired position out of allowed range
1599 seekFrame
= seekFrame
< d
->OverallFrames
? seekFrame
: d
->OverallFrames
;
1601 // seek direction (note: avoids casting to int64)
1602 delta
= (d
->DecodedFrames
> seekFrame
? -(mpc_int32_t
)(d
->DecodedFrames
- seekFrame
) : (mpc_int32_t
)(seekFrame
- d
->DecodedFrames
));
1604 if (seekFrame
> SEEK_PRE_DECODE
)
1605 lastFrame
= seekFrame
- SEEK_PRE_DECODE
+ 1 - (1<<d
->SeekTable_Step
);
1607 if (d
->MaxDecodedFrames
== 0) // nothing decoded yet, parse stream
1609 mpc_decoder_reset_state(d
);
1611 // starts from the beginning since no frames have been decoded yet, or not using seek table
1612 fpos
= get_initial_fpos(d
, d
->StreamVersion
);
1614 // seek to the first frame
1615 mpc_decoder_seek_to(d
, fpos
);
1617 // jump to the last frame via parsing, updating seek table
1618 d
->SeekTable
[0] = (mpc_uint32_t
)fpos
;
1619 d
->SeekTableCounter
= 0;
1620 for (d
->DecodedFrames
= 0; d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
++)
1622 d
->SeekTableCounter
+= mpc_decoder_jump_frame(d
);
1623 if (0 == ((d
->DecodedFrames
+1) & (d
->SeekTable_Mask
)))
1625 d
->SeekTable
[(d
->DecodedFrames
+1)>>d
->SeekTable_Step
] = d
->SeekTableCounter
;
1626 d
->MaxDecodedFrames
= d
->DecodedFrames
;
1627 d
->SeekTableCounter
= 0;
1631 else if (delta
< 0) // jump backwards, seek table is already available
1633 mpc_decoder_reset_state(d
);
1635 // jumps backwards using the seek table
1636 fpos
= d
->SeekTable
[0];
1637 for (d
->DecodedFrames
= 0; d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
++)
1639 if (0 == ((d
->DecodedFrames
+1) & (d
->SeekTable_Mask
)))
1641 fpos
+= d
->SeekTable
[(d
->DecodedFrames
+1)>>d
->SeekTable_Step
];
1642 d
->SeekTableCounter
= 0;
1645 mpc_decoder_seek_to(d
, fpos
);
1647 else if (delta
> SEEK_PRE_DECODE
) // jump forward, seek table is available
1649 mpc_decoder_reset_state(d
);
1651 // 1st loop: jump to the last usable position in the seek table
1652 fpos
= mpc_decoder_bits_read(d
);
1653 for (; d
->DecodedFrames
< d
->MaxDecodedFrames
&& d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
++)
1655 if (0 == ((d
->DecodedFrames
+1) & (d
->SeekTable_Mask
)))
1657 fpos
+= d
->SeekTable
[(d
->DecodedFrames
+1)>>d
->SeekTable_Step
];
1658 d
->SeekTableCounter
= 0;
1661 mpc_decoder_seek_to(d
, fpos
);
1663 // 2nd loop: jump the residual frames via parsing, update seek table
1664 for (;d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
++)
1666 d
->SeekTableCounter
+= mpc_decoder_jump_frame(d
);
1667 if (0 == ((d
->DecodedFrames
+1) & (d
->SeekTable_Mask
)))
1669 d
->SeekTable
[(d
->DecodedFrames
+1)>>d
->SeekTable_Step
] = d
->SeekTableCounter
;
1670 d
->MaxDecodedFrames
= d
->DecodedFrames
;
1671 d
->SeekTableCounter
= 0;
1675 // until here we jumped to desired position -SEEK_PRE_DECODE frames
1677 // now we decode the last SEEK_PRE_DECODE frames until we reach the seek
1678 // position. this is neccessary as mpc uses entropy coding in time domain
1679 for (;d
->DecodedFrames
< seekFrame
; d
->DecodedFrames
++)
1681 mpc_uint32_t FrameBitCnt
;
1683 d
->FwdJumpInfo
= mpc_decoder_bitstream_read(d
, 20); // read jump-info
1684 d
->ActDecodePos
= (d
->Zaehler
<< 5) + d
->pos
;
1685 FrameBitCnt
= mpc_decoder_bits_read(d
);
1686 // scanning the scalefactors (and check for validity of frame)
1687 if (d
->StreamVersion
>= 7)
1689 mpc_decoder_read_bitstream_sv7(d
, (d
->DecodedFrames
< seekFrame
- 1));
1693 #ifdef MPC_SUPPORT_SV456
1694 mpc_decoder_read_bitstream_sv6(d
);
1700 FrameBitCnt
= mpc_decoder_bits_read(d
) - FrameBitCnt
;
1702 if (d
->FwdJumpInfo
> FrameBitCnt
)
1703 mpc_decoder_seek_forward(d
, d
->FwdJumpInfo
- FrameBitCnt
);
1704 else if (FrameBitCnt
!= d
->FwdJumpInfo
)
1705 // Bug in perform_jump;
1708 // update seek table, if there new entries to fill
1709 d
->SeekTableCounter
+= d
->FwdJumpInfo
+ 20;
1710 if (0 == ((d
->DecodedFrames
+1) & (d
->SeekTable_Mask
)))
1712 d
->SeekTable
[(d
->DecodedFrames
+1)>>d
->SeekTable_Step
] = d
->SeekTableCounter
;
1713 d
->MaxDecodedFrames
= d
->DecodedFrames
;
1714 d
->SeekTableCounter
= 0;
1718 mpc_decoder_update_buffer(d
);
1720 if (d
->DecodedFrames
== seekFrame
- 1)
1722 // initialize the synth correctly for perfect decoding
1723 mpc_decoder_requantisierung(d
, d
->Max_Band
);
1724 mpc_decoder_synthese_filter_float(d
, NULL
);
1732 void mpc_decoder_fill_buffer(mpc_decoder
*d
) {
1734 f_read_dword(d
, d
->Speicher
, MEMSIZE
);
1735 d
->dword
= SWAP(d
->Speicher
[d
->Zaehler
= 0]);
1736 d
->next
= SWAP(d
->Speicher
[1]);
1742 void mpc_decoder_update_buffer(mpc_decoder
*d
)
1744 if ((d
->Ring
^ d
->Zaehler
) & MEMSIZE2
) {
1746 f_read_dword(d
, d
->Speicher
+ (d
->Ring
& MEMSIZE2
), MEMSIZE2
);
1747 d
->Ring
= d
->Zaehler
;
1752 void mpc_decoder_seek_to(mpc_decoder
*d
, mpc_uint32_t bitPos
) {
1755 mpc_uint32_t fpos
= (bitPos
>> 5);
1756 mpc_uint32_t bufferStart
= d
->WordsRead
- d
->Zaehler
;
1757 if ((d
->Zaehler
& MEMSIZE2
) != FALSE
)
1758 bufferStart
+= MEMSIZE2
;
1760 if (fpos
>= bufferStart
&& fpos
< bufferStart
+ MEMSIZE
) {
1762 // required position is within the buffer, no need to seek
1763 d
->Zaehler
= (fpos
- bufferStart
+ ((d
->Zaehler
& MEMSIZE2
) != FALSE
? MEMSIZE2
: 0)) & MEMMASK
;
1764 d
->pos
= bitPos
& 31;
1765 d
->WordsRead
= fpos
;
1766 d
->dword
= SWAP(d
->Speicher
[d
->Zaehler
]);
1767 d
->next
= SWAP(d
->Speicher
[(d
->Zaehler
+ 1) & MEMMASK
]);
1769 mpc_decoder_update_buffer(d
);
1775 f_seek(d
, fpos
*4 + d
->MPCHeaderPos
);
1777 d
->pos
= bitPos
& 31;
1778 d
->WordsRead
= fpos
;
1780 mpc_decoder_fill_buffer(d
);
1786 void mpc_decoder_seek_forward(mpc_decoder
*d
, mpc_uint32_t bits
) {
1790 bits
= bits
>> 5; // to DWORDs
1791 d
->Zaehler
= (d
->Zaehler
+ bits
) & MEMMASK
;
1792 d
->dword
= SWAP(d
->Speicher
[d
->Zaehler
]);
1793 d
->next
= SWAP(d
->Speicher
[(d
->Zaehler
+ 1) & MEMMASK
]);
1794 d
->WordsRead
+= bits
;