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 == -128 ? -128 : 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 MEMSIZE
= MPC_DECODER_MEMSIZE
, // overall buffer size
100 MEMSIZE2
= (MEMSIZE
/2), // size of one buffer
101 MEMMASK
= (MEMSIZE
-1)
104 //------------------------------------------------------------------------------
105 // forward declarations
106 //------------------------------------------------------------------------------
107 void mpc_decoder_read_bitstream_sv6(mpc_decoder
*d
);
108 void mpc_decoder_read_bitstream_sv7(mpc_decoder
*d
, mpc_bool_t fastSeeking
);
109 void mpc_decoder_update_buffer(mpc_decoder
*d
);
110 mpc_bool_t
mpc_decoder_seek_sample(mpc_decoder
*d
, mpc_int64_t destsample
);
111 void mpc_decoder_requantisierung(mpc_decoder
*d
, const mpc_int32_t Last_Band
);
112 void mpc_decoder_seek_to(mpc_decoder
*d
, mpc_uint32_t bitPos
);
113 void mpc_decoder_seek_forward(mpc_decoder
*d
, mpc_uint32_t bits
);
114 mpc_uint32_t
mpc_decoder_jump_frame(mpc_decoder
*d
);
115 void mpc_decoder_fill_buffer(mpc_decoder
*d
);
116 void mpc_decoder_reset_state(mpc_decoder
*d
);
117 static mpc_uint32_t
get_initial_fpos(mpc_decoder
*d
, mpc_uint32_t StreamVersion
);
118 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
);
119 static void mpc_move_next(mpc_decoder
*d
);
121 mpc_uint32_t Speicher
[MPC_DECODER_MEMSIZE
];
122 MPC_SAMPLE_FORMAT Y_L
[36][32] IBSS_ATTR_MPC_LARGE_IRAM
;
123 MPC_SAMPLE_FORMAT Y_R
[36][32] IBSS_ATTR_MPC_LARGE_IRAM
;
125 //------------------------------------------------------------------------------
127 //------------------------------------------------------------------------------
128 static mpc_int32_t
f_read(mpc_decoder
*d
, void *ptr
, size_t size
)
130 return d
->r
->read(d
->r
->data
, ptr
, size
);
133 static mpc_bool_t
f_seek(mpc_decoder
*d
, mpc_int32_t offset
)
135 return d
->r
->seek(d
->r
->data
, offset
);
138 static mpc_int32_t
f_read_dword(mpc_decoder
*d
, mpc_uint32_t
* ptr
, mpc_uint32_t count
)
140 count
= f_read(d
, ptr
, count
<< 2) >> 2;
144 //------------------------------------------------------------------------------
145 // huffman & bitstream functions
146 //------------------------------------------------------------------------------
147 static const mpc_uint32_t mask
[33] ICONST_ATTR
= {
148 0x00000000, 0x00000001, 0x00000003, 0x00000007,
149 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
150 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
151 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
152 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
153 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
154 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
155 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
159 /* F U N C T I O N S */
161 // resets bitstream decoding
163 mpc_decoder_reset_bitstream_decode(mpc_decoder
*d
)
172 // reports the number of read bits
174 mpc_decoder_bits_read(mpc_decoder
*d
)
176 return 32 * d
->WordsRead
+ d
->pos
;
179 static void mpc_move_next(mpc_decoder
*d
) {
180 d
->Zaehler
= (d
->Zaehler
+ 1) & MEMMASK
;
182 d
->next
= SWAP(d
->Speicher
[(d
->Zaehler
+ 1) & MEMMASK
]);
187 // read desired number of bits out of the bitstream
188 static inline mpc_uint32_t
189 mpc_decoder_bitstream_read(mpc_decoder
*d
, const mpc_uint32_t bits
)
191 mpc_uint32_t out
= d
->dword
;
196 out
>>= (32 - d
->pos
);
202 out
|= d
->dword
>> (32 - d
->pos
);
206 return out
& mask
[bits
];
210 mpc_decoder_make_huffman_lookup(
211 mpc_uint8_t
* lookup
, size_t length
, const HuffmanTyp
* Table
, size_t elements
)
214 size_t idx
= elements
;
215 mpc_uint32_t dval
= (mpc_uint32_t
)0x80000000L
/ length
* 2;
216 mpc_uint32_t val
= dval
- 1;
218 for ( i
= 0; i
< length
; i
++, val
+= dval
) {
219 while ( idx
> 0 && val
>= Table
[idx
-1].Code
)
221 *lookup
++ = (mpc_uint8_t
)idx
;
227 #ifdef MPC_SUPPORT_SV456
228 // decode SCFI-bundle (sv4,5,6)
230 mpc_decoder_scfi_bundle_read(
232 const HuffmanTyp
* Table
, mpc_int8_t
* SCFI
, mpc_bool_t
* DSCF
)
234 // load preview and decode
235 mpc_uint32_t code
= d
->dword
<< d
->pos
;
238 code
|= d
->next
>> (32 - d
->pos
);
240 while (code
< Table
->Code
) {
244 // set the new position within bitstream without performing a dummy-read
245 if ((d
->pos
+= Table
->Length
) >= 32) {
249 *SCFI
= Table
->Value
>> 1;
250 *DSCF
= Table
->Value
& 1;
253 // basic huffman decoding routine
254 // works with maximum lengths up to 14
256 mpc_decoder_huffman_decode(mpc_decoder
*d
, const HuffmanTyp
*Table
)
258 // load preview and decode
259 mpc_uint32_t code
= d
->dword
<< d
->pos
;
262 code
|= d
->next
>> (32 - d
->pos
);
264 while (code
< Table
->Code
) {
268 // set the new position within bitstream without performing a dummy-read
269 if ((d
->pos
+= Table
->Length
) >= 32) {
277 // faster huffman through previewing less bits
278 // works with maximum lengths up to 10
280 mpc_decoder_huffman_decode_fast(mpc_decoder
*d
, const HuffmanTyp
* Table
)
282 // load preview and decode
283 mpc_uint32_t code
= d
->dword
<< d
->pos
;
286 code
|= d
->next
>> (32 - d
->pos
);
288 while (code
< Table
->Code
) {
292 // set the new position within bitstream without performing a dummy-read
293 if ((d
->pos
+= Table
->Length
) >= 32) {
300 // even faster huffman through previewing even less bits
301 // works with maximum lengths up to 5
303 mpc_decoder_huffman_decode_faster(mpc_decoder
*d
, const HuffmanTyp
* Table
)
305 // load preview and decode
306 mpc_uint32_t code
= d
->dword
<< d
->pos
;
309 code
|= d
->next
>> (32 - d
->pos
);
311 while (code
< Table
->Code
) {
315 // set the new position within bitstream without performing a dummy-read
316 if ((d
->pos
+= Table
->Length
) >= 32) {
323 /* partial lookup table decode */
325 mpc_decoder_huffman_decode_fasterer(mpc_decoder
*d
, const HuffmanTyp
* Table
, const mpc_uint8_t
* tab
, mpc_uint16_t unused_bits
)
327 // load preview and decode
328 mpc_uint32_t code
= d
->dword
<< d
->pos
;
330 if (d
->pos
> 18) { // preview 14 bits
331 code
|= d
->next
>> (32 - d
->pos
);
334 Table
+= tab
[(size_t)(code
>> unused_bits
) ];
336 while (code
< Table
->Code
) {
340 // set the new position within bitstream without performing a dummy-read
341 if ((d
->pos
+= Table
->Length
) >= 32) {
348 /* full decode using lookup table */
349 static inline mpc_int32_t
350 mpc_decoder_huffman_decode_fastest(mpc_decoder
*d
, const HuffmanTyp
* Table
, const mpc_uint8_t
* tab
, mpc_uint16_t unused_bits
)
352 // load preview and decode
353 mpc_uint32_t code
= d
->dword
<< d
->pos
;
355 if (d
->pos
> unused_bits
) {
356 code
|= d
->next
>> (32 - d
->pos
);
359 Table
+=tab
[(size_t)(code
>> unused_bits
) ];
361 // set the new position within bitstream without performing a dummy-read
362 if ((d
->pos
+= Table
->Length
) >= 32) {
370 mpc_decoder_reset_v(mpc_decoder
*d
)
372 memset(d
->V_L
, 0, sizeof d
->V_L
);
373 memset(d
->V_R
, 0, sizeof d
->V_R
);
377 mpc_decoder_reset_synthesis(mpc_decoder
*d
)
379 mpc_decoder_reset_v(d
);
383 mpc_decoder_reset_y(mpc_decoder
*d
)
385 memset(d
->Y_L
, 0, sizeof Y_L
);
386 memset(d
->Y_R
, 0, sizeof Y_R
);
390 mpc_decoder_reset_globals(mpc_decoder
*d
)
392 mpc_decoder_reset_bitstream_decode(d
);
394 d
->DecodedFrames
= 0;
395 d
->SeekTableIndex
= 0;
396 d
->MaxDecodedFrames
= 0;
397 d
->StreamVersion
= 0;
400 memset(d
->Y_L
, 0, sizeof Y_L
);
401 memset(d
->Y_R
, 0, sizeof Y_R
);
402 memset(d
->SCF_Index_L
, 0, sizeof d
->SCF_Index_L
);
403 memset(d
->SCF_Index_R
, 0, sizeof d
->SCF_Index_R
);
404 memset(d
->Res_L
, 0, sizeof d
->Res_L
);
405 memset(d
->Res_R
, 0, sizeof d
->Res_R
);
406 memset(d
->SCFI_L
, 0, sizeof d
->SCFI_L
);
407 memset(d
->SCFI_R
, 0, sizeof d
->SCFI_R
);
408 #ifdef MPC_SUPPORT_SV456
409 memset(d
->DSCF_Flag_L
, 0, sizeof d
->DSCF_Flag_L
);
410 memset(d
->DSCF_Flag_R
, 0, sizeof d
->DSCF_Flag_R
);
412 memset(d
->Q
, 0, sizeof d
->Q
);
413 memset(d
->MS_Flag
, 0, sizeof d
->MS_Flag
);
417 mpc_decoder_decode_frame(mpc_decoder
*d
, mpc_uint32_t
*in_buffer
,
418 mpc_uint32_t in_len
, MPC_SAMPLE_FORMAT
*out_buffer
)
420 mpc_decoder_reset_bitstream_decode(d
);
421 if (in_len
> sizeof(Speicher
)) in_len
= sizeof(Speicher
);
422 memcpy(d
->Speicher
, in_buffer
, in_len
);
423 d
->dword
= SWAP(d
->Speicher
[0]);
424 d
->next
= SWAP(d
->Speicher
[1]);
425 switch (d
->StreamVersion
) {
426 #ifdef MPC_SUPPORT_SV456
430 mpc_decoder_read_bitstream_sv6(d
);
435 mpc_decoder_read_bitstream_sv7(d
, FALSE
);
438 return (mpc_uint32_t
)(-1);
440 mpc_decoder_requantisierung(d
, d
->Max_Band
);
441 mpc_decoder_synthese_filter_float(d
, out_buffer
);
442 return mpc_decoder_bits_read(d
);
446 mpc_decoder_decode_internal(mpc_decoder
*d
, MPC_SAMPLE_FORMAT
*buffer
)
448 mpc_uint32_t output_frame_length
= MPC_FRAME_LENGTH
;
450 mpc_uint32_t FrameBitCnt
= 0;
452 // output the last part of the last frame here, if needed
453 if (d
->last_block_samples
> 0) {
454 output_frame_length
= d
->last_block_samples
;
455 d
->last_block_samples
= 0; // it's going to be handled now, so reset it
456 if (!d
->TrueGaplessPresent
) {
457 mpc_decoder_reset_y(d
);
459 mpc_decoder_bitstream_read(d
, 20);
460 mpc_decoder_read_bitstream_sv7(d
, FALSE
);
461 mpc_decoder_requantisierung(d
, d
->Max_Band
);
463 mpc_decoder_synthese_filter_float(d
, buffer
);
464 return output_frame_length
;
467 if (d
->DecodedFrames
>= d
->OverallFrames
) {
468 return (mpc_uint32_t
)(-1); // end of file -> abort decoding
471 if (d
->DecodedFrames
== 0)
472 d
->SeekTable
[0] = mpc_decoder_bits_read(d
);
474 // read jump-info for validity check of frame
475 d
->FwdJumpInfo
= mpc_decoder_bitstream_read(d
, 20);
477 d
->ActDecodePos
= (d
->Zaehler
<< 5) + d
->pos
;
479 // decode data and check for validity of frame
480 FrameBitCnt
= mpc_decoder_bits_read(d
);
481 switch (d
->StreamVersion
) {
482 #ifdef MPC_SUPPORT_SV456
486 mpc_decoder_read_bitstream_sv6(d
);
491 mpc_decoder_read_bitstream_sv7(d
, FALSE
);
494 return (mpc_uint32_t
)(-1);
496 d
->FrameWasValid
= mpc_decoder_bits_read(d
) - FrameBitCnt
== d
->FwdJumpInfo
;
500 /* update seek table */
501 if (d
->SeekTable_Step
== 1) {
502 d
->SeekTable
[d
->DecodedFrames
] = d
->FwdJumpInfo
+ 20;
504 if ((d
->DecodedFrames
-1) % d
->SeekTable_Step
== 0) {
505 d
->SeekTable
[d
->SeekTableIndex
] = d
->SeekTableCounter
;
506 d
->SeekTableIndex
+= 1;
507 d
->SeekTableCounter
= 0;
509 d
->SeekTableCounter
+= d
->FwdJumpInfo
+ 20;
513 mpc_decoder_requantisierung(d
, d
->Max_Band
);
515 mpc_decoder_synthese_filter_float(d
, buffer
);
517 // cut off first MPC_DECODER_SYNTH_DELAY zero-samples
518 if (d
->DecodedFrames
== d
->OverallFrames
&& d
->StreamVersion
>= 6) {
519 // reconstruct exact filelength
520 mpc_int32_t mod_block
= mpc_decoder_bitstream_read(d
, 11);
521 mpc_int32_t FilterDecay
;
523 if (mod_block
== 0) {
527 FilterDecay
= (mod_block
+ MPC_DECODER_SYNTH_DELAY
) % MPC_FRAME_LENGTH
;
529 // additional FilterDecay samples are needed for decay of synthesis filter
530 if (MPC_DECODER_SYNTH_DELAY
+ mod_block
>= MPC_FRAME_LENGTH
) {
531 // this variable will be checked for at the top of the function
532 d
->last_block_samples
= FilterDecay
;
534 else { // there are only FilterDecay samples needed for this frame
535 output_frame_length
= FilterDecay
;
539 if (d
->samples_to_skip
) {
540 if (output_frame_length
< d
->samples_to_skip
) {
541 d
->samples_to_skip
-= output_frame_length
;
542 output_frame_length
= 0;
545 output_frame_length
-= d
->samples_to_skip
;
548 buffer
+ d
->samples_to_skip
,
549 output_frame_length
* sizeof (MPC_SAMPLE_FORMAT
));
551 buffer
+ MPC_FRAME_LENGTH
,
552 buffer
+ MPC_FRAME_LENGTH
+ d
->samples_to_skip
,
553 output_frame_length
* sizeof (MPC_SAMPLE_FORMAT
));
554 d
->samples_to_skip
= 0;
558 return output_frame_length
;
561 mpc_uint32_t
mpc_decoder_decode(
563 MPC_SAMPLE_FORMAT
*buffer
,
564 mpc_uint32_t
*vbr_update_acc
,
565 mpc_uint32_t
*vbr_update_bits
)
569 mpc_uint32_t RING
= d
->Zaehler
;
570 mpc_int32_t vbr_ring
= (RING
<< 5) + d
->pos
;
572 mpc_uint32_t valid_samples
= mpc_decoder_decode_internal(d
, buffer
);
574 if (valid_samples
== (mpc_uint32_t
)(-1) ) {
578 /**************** ERROR CONCEALMENT *****************/
579 if (d
->FrameWasValid
== 0 ) {
580 // error occurred in bitstream
581 return (mpc_uint32_t
)(-1);
584 if (vbr_update_acc
&& vbr_update_bits
) {
585 (*vbr_update_acc
) ++;
586 vbr_ring
= (d
->Zaehler
<< 5) + d
->pos
- vbr_ring
;
590 (*vbr_update_bits
) += vbr_ring
;
594 mpc_decoder_update_buffer(d
);
596 if (valid_samples
> 0) {
597 return valid_samples
;
603 mpc_decoder_requantisierung(mpc_decoder
*d
, const mpc_int32_t Last_Band
)
607 MPC_SAMPLE_FORMAT facL
;
608 MPC_SAMPLE_FORMAT facR
;
609 MPC_SAMPLE_FORMAT templ
;
610 MPC_SAMPLE_FORMAT tempr
;
611 MPC_SAMPLE_FORMAT
* YL
;
612 MPC_SAMPLE_FORMAT
* YR
;
616 #ifdef MPC_FIXED_POINT
617 #if MPC_FIXED_POINT_FRACTPART == 14
618 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
619 MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
622 #error FIXME, Cc table is in 18.14 format
626 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
627 MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
629 // requantization and scaling of subband-samples
630 for ( Band
= 0; Band
<= Last_Band
; Band
++ ) { // setting pointers
631 YL
= d
->Y_L
[0] + Band
;
632 YR
= d
->Y_R
[0] + Band
;
635 /************************** MS-coded **************************/
636 if ( d
->MS_Flag
[Band
] ) {
637 if ( d
->Res_L
[Band
] ) {
638 if ( d
->Res_R
[Band
] ) { // M!=0, S!=0
639 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][0]);
640 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][0]);
641 for ( n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
642 *YL
= (templ
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++))+(tempr
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++));
645 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][1]);
646 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][1]);
647 for ( ; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
648 *YL
= (templ
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++))+(tempr
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++));
651 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][2]);
652 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][2]);
653 for ( ; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
654 *YL
= (templ
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++))+(tempr
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++));
657 } else { // M!=0, S==0
658 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][0]);
659 for ( n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
660 *YR
= *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
662 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][1]);
663 for ( ; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
664 *YR
= *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
666 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][2]);
667 for ( ; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
668 *YR
= *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
672 if (d
->Res_R
[Band
]) // M==0, S!=0
674 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][0]);
675 for ( n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
676 *YR
= - (*YL
= MPC_MULTIPLY_FLOAT_INT(facR
,*(R
++)));
678 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][1]);
679 for ( ; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
680 *YR
= - (*YL
= MPC_MULTIPLY_FLOAT_INT(facR
,*(R
++)));
682 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][2]);
683 for ( ; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
684 *YR
= - (*YL
= MPC_MULTIPLY_FLOAT_INT(facR
,*(R
++)));
686 } else { // M==0, S==0
687 for ( n
= 0; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
693 /************************** LR-coded **************************/
695 if ( d
->Res_L
[Band
] ) {
696 if ( d
->Res_R
[Band
] ) { // L!=0, R!=0
697 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][0]);
698 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][0]);
699 for (n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
700 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
701 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
703 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][1]);
704 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][1]);
705 for (; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
706 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
707 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
709 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][2]);
710 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][2]);
711 for (; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
712 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
713 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
715 } else { // L!=0, R==0
716 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][0]);
717 for ( n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
718 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
721 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][1]);
722 for ( ; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
723 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
726 facL
= MPC_MULTIPLY_SCF( Cc
[d
->Res_L
[Band
]] , (unsigned char)d
->SCF_Index_L
[Band
][2]);
727 for ( ; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
728 *YL
= MPC_MULTIPLY_FLOAT_INT(facL
,*L
++);
734 if ( d
->Res_R
[Band
] ) { // L==0, R!=0
735 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][0]);
736 for ( n
= 0; n
< 12; n
++, YL
+= 32, YR
+= 32 ) {
738 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
740 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][1]);
741 for ( ; n
< 24; n
++, YL
+= 32, YR
+= 32 ) {
743 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
745 facR
= MPC_MULTIPLY_SCF( Cc
[d
->Res_R
[Band
]] , (unsigned char)d
->SCF_Index_R
[Band
][2]);
746 for ( ; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
748 *YR
= MPC_MULTIPLY_FLOAT_INT(facR
,*R
++);
750 } else { // L==0, R==0
751 for ( n
= 0; n
< 36; n
++, YL
+= 32, YR
+= 32 ) {
760 #ifdef MPC_SUPPORT_SV456
761 static const unsigned char Q_res
[32][16] ICONST_ATTR
= {
762 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
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,17,0,0,0,0,0,0,0,0},
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,17,0,0,0,0,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},
796 /****************************************** SV 6 ******************************************/
798 mpc_decoder_read_bitstream_sv6(mpc_decoder
*d
)
801 mpc_int32_t Max_used_Band
=0;
802 const HuffmanTyp
*Table
;
803 const HuffmanTyp
*x1
;
804 const HuffmanTyp
*x2
;
809 mpc_int8_t
*ResL
= d
->Res_L
;
810 mpc_int8_t
*ResR
= d
->Res_R
;
812 /************************ HEADER **************************/
815 for (n
=0; n
<= d
->Max_Band
; ++n
, ++ResL
, ++ResR
)
817 if (n
<11) Table
= mpc_table_Region_A
;
818 else if (n
>=11 && n
<=22) Table
= mpc_table_Region_B
;
819 else /*if (n>=23)*/ Table
= mpc_table_Region_C
;
821 *ResL
= Q_res
[n
][mpc_decoder_huffman_decode(d
, Table
)];
823 d
->MS_Flag
[n
] = mpc_decoder_bitstream_read(d
, 1);
825 *ResR
= Q_res
[n
][mpc_decoder_huffman_decode(d
, Table
)];
827 // only perform the following procedure up to the maximum non-zero subband
828 if (*ResL
|| *ResR
) Max_used_Band
= n
;
831 /************************* SCFI-Bundle *****************************/
834 for (n
=0; n
<=Max_used_Band
; ++n
, ++ResL
, ++ResR
) {
835 if (*ResL
) mpc_decoder_scfi_bundle_read(d
, mpc_table_SCFI_Bundle
, &(d
->SCFI_L
[n
]), &(d
->DSCF_Flag_L
[n
]));
836 if (*ResR
) mpc_decoder_scfi_bundle_read(d
, mpc_table_SCFI_Bundle
, &(d
->SCFI_R
[n
]), &(d
->DSCF_Flag_R
[n
]));
839 /***************************** SCFI ********************************/
842 L
= d
->SCF_Index_L
[0];
843 R
= d
->SCF_Index_R
[0];
844 for (n
=0; n
<= Max_used_Band
; ++n
, ++ResL
, ++ResR
, L
+=3, R
+=3)
848 /*********** DSCF ************/
849 if (d
->DSCF_Flag_L
[n
]==1)
851 switch (d
->SCFI_L
[n
])
854 L
[0] = L
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
859 L
[0] = L
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
860 L
[1] = L
[0] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
864 L
[0] = L
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
866 L
[2] = L
[1] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
869 L
[0] = L
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
870 L
[1] = L
[0] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
871 L
[2] = L
[1] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
878 /************ SCF ************/
881 switch (d
->SCFI_L
[n
])
884 L
[0] = mpc_decoder_bitstream_read(d
, 6);
889 L
[0] = mpc_decoder_bitstream_read(d
, 6);
890 L
[1] = mpc_decoder_bitstream_read(d
, 6);
894 L
[0] = mpc_decoder_bitstream_read(d
, 6);
896 L
[2] = mpc_decoder_bitstream_read(d
, 6);
899 L
[0] = mpc_decoder_bitstream_read(d
, 6);
900 L
[1] = mpc_decoder_bitstream_read(d
, 6);
901 L
[2] = mpc_decoder_bitstream_read(d
, 6);
911 /*********** DSCF ************/
912 if (d
->DSCF_Flag_R
[n
]==1)
914 switch (d
->SCFI_R
[n
])
917 R
[0] = R
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
922 R
[0] = R
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
923 R
[1] = R
[0] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
927 R
[0] = R
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
929 R
[2] = R
[1] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
932 R
[0] = R
[2] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
933 R
[1] = R
[0] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
934 R
[2] = R
[1] + mpc_decoder_huffman_decode_fast(d
, mpc_table_DSCF_Entropie
);
941 /************ SCF ************/
944 switch (d
->SCFI_R
[n
])
947 R
[0] = mpc_decoder_bitstream_read(d
, 6);
952 R
[0] = mpc_decoder_bitstream_read(d
, 6);
953 R
[1] = mpc_decoder_bitstream_read(d
, 6);
957 R
[0] = mpc_decoder_bitstream_read(d
, 6);
959 R
[2] = mpc_decoder_bitstream_read(d
, 6);
962 R
[0] = mpc_decoder_bitstream_read(d
, 6);
963 R
[1] = mpc_decoder_bitstream_read(d
, 6);
964 R
[2] = mpc_decoder_bitstream_read(d
, 6);
974 /**************************** Samples ****************************/
977 for (n
=0; n
<= Max_used_Band
; ++n
, ++ResL
, ++ResR
)
980 x1
= mpc_table_SampleHuff
[*ResL
];
981 x2
= mpc_table_SampleHuff
[*ResR
];
985 if (x1
!=NULL
|| x2
!=NULL
)
988 if (x1
!= NULL
) *QL
++ = mpc_decoder_huffman_decode_fast(d
, x1
);
989 if (x2
!= NULL
) *QR
++ = mpc_decoder_huffman_decode_fast(d
, x2
);
992 if (*ResL
>7 || *ResR
>7)
995 if (*ResL
>7) *QL
++ = (mpc_int16_t
)mpc_decoder_bitstream_read(d
, Res_bit
[*ResL
]) - Dc
[*ResL
];
996 if (*ResR
>7) *QR
++ = (mpc_int16_t
)mpc_decoder_bitstream_read(d
, Res_bit
[*ResR
]) - Dc
[*ResR
];
1000 #endif //MPC_SUPPORT_SV456
1001 /****************************************** SV 7 ******************************************/
1003 mpc_decoder_read_bitstream_sv7(mpc_decoder
*d
, mpc_bool_t fastSeeking
)
1006 mpc_int32_t Max_used_Band
=0;
1007 const HuffmanTyp
*Table
;
1010 mpc_int16_t
*LQ
,*RQ
;
1011 mpc_int8_t
*ResL
,*ResR
;
1014 mpc_uint8_t max_length
;
1016 /***************************** Header *****************************/
1021 *ResL
= mpc_decoder_bitstream_read(d
, 4);
1022 *ResR
= mpc_decoder_bitstream_read(d
, 4);
1023 if (d
->MS_used
&& !(*ResL
==0 && *ResR
==0)) {
1024 d
->MS_Flag
[0] = mpc_decoder_bitstream_read(d
, 1);
1029 // consecutive subbands
1030 ++ResL
; ++ResR
; // increase pointers
1031 for (n
=1; n
<= d
->Max_Band
; ++n
, ++ResL
, ++ResR
)
1033 idx
= mpc_decoder_huffman_decode_fast(d
, mpc_table_HuffHdr
);
1034 *ResL
= (idx
!=4) ? *(ResL
-1) + idx
: (int) mpc_decoder_bitstream_read(d
, 4);
1036 idx
= mpc_decoder_huffman_decode_fast(d
, mpc_table_HuffHdr
);
1037 *ResR
= (idx
!=4) ? *(ResR
-1) + idx
: (int) mpc_decoder_bitstream_read(d
, 4);
1039 if (d
->MS_used
&& !(*ResL
==0 && *ResR
==0)) {
1040 d
->MS_Flag
[n
] = mpc_decoder_bitstream_read(d
, 1);
1043 // only perform following procedures up to the maximum non-zero subband
1044 if (*ResL
!=0 || *ResR
!=0) {
1050 /****************************** SCFI ******************************/
1055 for (n
=0; n
<= Max_used_Band
; ++n
, ++L
, ++R
, ++ResL
, ++ResR
) {
1056 if (*ResL
) *L
= mpc_decoder_huffman_decode_faster(d
, mpc_table_HuffSCFI
);
1057 if (*ResR
) *R
= mpc_decoder_huffman_decode_faster(d
, mpc_table_HuffSCFI
);
1060 /**************************** SCF/DSCF ****************************/
1063 L
= d
->SCF_Index_L
[0];
1064 R
= d
->SCF_Index_R
[0];
1065 for (n
=0; n
<=Max_used_Band
; ++n
, ++ResL
, ++ResR
, L
+=3, R
+=3) {
1068 switch (d
->SCFI_L
[n
])
1071 idx
= Decode_DSCF ();
1072 L
[0] = (idx
!=8) ? SCF_DIFF(L
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1073 idx
= Decode_DSCF ();
1074 L
[1] = (idx
!=8) ? SCF_DIFF(L
[0], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1078 idx
= Decode_DSCF ();
1079 L
[0] = (idx
!=8) ? SCF_DIFF(L
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1084 idx
= Decode_DSCF ();
1085 L
[0] = (idx
!=8) ? SCF_DIFF(L
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1087 idx
= Decode_DSCF ();
1088 L
[2] = (idx
!=8) ? SCF_DIFF(L
[1], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1091 idx
= Decode_DSCF ();
1092 L
[0] = (idx
!=8) ? SCF_DIFF(L
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1093 idx
= Decode_DSCF ();
1094 L
[1] = (idx
!=8) ? SCF_DIFF(L
[0], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1095 idx
= Decode_DSCF ();
1096 L
[2] = (idx
!=8) ? SCF_DIFF(L
[1], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1105 switch (d
->SCFI_R
[n
])
1108 idx
= Decode_DSCF ();
1109 R
[0] = (idx
!=8) ? SCF_DIFF(R
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1110 idx
= Decode_DSCF ();
1111 R
[1] = (idx
!=8) ? SCF_DIFF(R
[0], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1115 idx
= Decode_DSCF ();
1116 R
[0] = (idx
!=8) ? SCF_DIFF(R
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1121 idx
= Decode_DSCF ();
1122 R
[0] = (idx
!=8) ? SCF_DIFF(R
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1124 idx
= Decode_DSCF ();
1125 R
[2] = (idx
!=8) ? SCF_DIFF(R
[1], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1128 idx
= Decode_DSCF ();
1129 R
[0] = (idx
!=8) ? SCF_DIFF(R
[2], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1130 idx
= Decode_DSCF ();
1131 R
[1] = (idx
!=8) ? SCF_DIFF(R
[0], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1132 idx
= Decode_DSCF ();
1133 R
[2] = (idx
!=8) ? SCF_DIFF(R
[1], idx
) : (int) mpc_decoder_bitstream_read(d
, 6);
1145 /***************************** Samples ****************************/
1150 for (n
=0; n
<= Max_used_Band
; ++n
, ++ResL
, ++ResR
, LQ
+=36, RQ
+=36)
1152 /************** links **************/
1155 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
1156 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
1160 for (k
=0; k
<36; k
++ ) {
1161 tmp
= mpc_random_int(d
);
1162 *LQ
++ = ((tmp
>> 24) & 0xFF) + ((tmp
>> 16) & 0xFF) + ((tmp
>> 8) & 0xFF) + ((tmp
>> 0) & 0xFF) - 510;
1166 LQ
+= 36;// increase pointer
1169 if (mpc_decoder_bitstream_read(d
, 1)) {
1170 Table
= mpc_table_HuffQ
[1][1];
1174 Table
= mpc_table_HuffQ
[0][1];
1178 for (k
=0; k
<12; ++k
)
1180 idx
= HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1187 if (mpc_decoder_bitstream_read(d
, 1)) {
1188 Table
= mpc_table_HuffQ
[1][2];
1192 Table
= mpc_table_HuffQ
[0][2];
1196 for (k
=0; k
<18; ++k
)
1198 idx
= HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1204 if (mpc_decoder_bitstream_read(d
, 1)) {
1205 Table
= mpc_table_HuffQ
[1][3];
1209 Table
= mpc_table_HuffQ
[0][3];
1213 for (k
=0; k
<36; ++k
)
1214 *LQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1217 if (mpc_decoder_bitstream_read(d
, 1)) {
1218 Table
= mpc_table_HuffQ
[1][4];
1222 Table
= mpc_table_HuffQ
[0][4];
1226 for (k
=0; k
<36; ++k
)
1227 *LQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1230 if (mpc_decoder_bitstream_read(d
, 1)) {
1231 Table
= mpc_table_HuffQ
[1][5];
1235 Table
= mpc_table_HuffQ
[0][5];
1239 for (k
=0; k
<36; ++k
)
1240 *LQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1243 if (mpc_decoder_bitstream_read(d
, 1)) {
1244 Table
= mpc_table_HuffQ
[1][6];
1247 for (k
=0; k
<36; ++k
)
1248 *LQ
++ = HUFFMAN_DECODE_FASTERER ( d
, Table
, LUT
, max_length
);
1250 Table
= mpc_table_HuffQ
[0][6];
1253 for (k
=0; k
<36; ++k
)
1254 *LQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1258 if (mpc_decoder_bitstream_read(d
, 1)) {
1259 Table
= mpc_table_HuffQ
[1][7];
1262 for (k
=0; k
<36; ++k
)
1263 *LQ
++ = HUFFMAN_DECODE_FASTERER ( d
, Table
, LUT
, max_length
);
1265 Table
= mpc_table_HuffQ
[0][7];
1268 for (k
=0; k
<36; ++k
)
1269 *LQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1272 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
1274 for (k
=0; k
<36; ++k
)
1275 *LQ
++ = (mpc_int16_t
)mpc_decoder_bitstream_read(d
, Res_bit
[*ResL
]) - tmp
;
1280 /************** rechts **************/
1283 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
1284 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
1288 for (k
=0; k
<36; k
++ ) {
1289 tmp
= mpc_random_int(d
);
1290 *RQ
++ = ((tmp
>> 24) & 0xFF) + ((tmp
>> 16) & 0xFF) + ((tmp
>> 8) & 0xFF) + ((tmp
>> 0) & 0xFF) - 510;
1294 RQ
+= 36;// increase pointer
1297 if (mpc_decoder_bitstream_read(d
, 1)) {
1298 Table
= mpc_table_HuffQ
[1][1];
1302 Table
= mpc_table_HuffQ
[0][1];
1306 for (k
=0; k
<12; ++k
)
1308 idx
= HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1315 if (mpc_decoder_bitstream_read(d
, 1)) {
1316 Table
= mpc_table_HuffQ
[1][2];
1320 Table
= mpc_table_HuffQ
[0][2];
1324 for (k
=0; k
<18; ++k
)
1326 idx
= HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1332 if (mpc_decoder_bitstream_read(d
, 1)) {
1333 Table
= mpc_table_HuffQ
[1][3];
1337 Table
= mpc_table_HuffQ
[0][3];
1341 for (k
=0; k
<36; ++k
)
1342 *RQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1345 if (mpc_decoder_bitstream_read(d
, 1)) {
1346 Table
= mpc_table_HuffQ
[1][4];
1350 Table
= mpc_table_HuffQ
[0][4];
1354 for (k
=0; k
<36; ++k
)
1355 *RQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1358 if (mpc_decoder_bitstream_read(d
, 1)) {
1359 Table
= mpc_table_HuffQ
[1][5];
1363 Table
= mpc_table_HuffQ
[0][5];
1367 for (k
=0; k
<36; ++k
)
1368 *RQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1371 if (mpc_decoder_bitstream_read(d
, 1)) {
1372 Table
= mpc_table_HuffQ
[1][6];
1375 for (k
=0; k
<36; ++k
)
1376 *RQ
++ = HUFFMAN_DECODE_FASTERER ( d
, Table
, LUT
, max_length
);
1378 Table
= mpc_table_HuffQ
[0][6];
1381 for (k
=0; k
<36; ++k
)
1382 *RQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1386 if (mpc_decoder_bitstream_read(d
, 1)) {
1387 Table
= mpc_table_HuffQ
[1][7];
1390 for (k
=0; k
<36; ++k
)
1391 *RQ
++ = HUFFMAN_DECODE_FASTERER ( d
, Table
, LUT
, max_length
);
1393 Table
= mpc_table_HuffQ
[0][7];
1396 for (k
=0; k
<36; ++k
)
1397 *RQ
++ = HUFFMAN_DECODE_FASTEST ( d
, Table
, LUT
, max_length
);
1400 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
1402 for (k
=0; k
<36; ++k
)
1403 *RQ
++ = (mpc_int16_t
)mpc_decoder_bitstream_read(d
, Res_bit
[*ResR
]) - tmp
;
1411 void mpc_decoder_setup(mpc_decoder
*d
, mpc_reader
*r
)
1415 d
->MPCHeaderPos
= 0;
1416 d
->StreamVersion
= 0;
1419 d
->ActDecodePos
= 0;
1420 d
->FrameWasValid
= 0;
1421 d
->OverallFrames
= 0;
1422 d
->DecodedFrames
= 0;
1423 d
->MaxDecodedFrames
= 0;
1424 d
->TrueGaplessPresent
= 0;
1425 d
->last_block_samples
= 0;
1438 d
->SeekTable
= NULL
;
1439 d
->SeekTable_Step
= 1;
1440 d
->SeekTableIndex
= 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
->Speicher
= Speicher
;
1465 #if defined(CPU_COLDFIRE)
1466 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
1470 void mpc_decoder_destroy(mpc_decoder
*d
)
1472 if (d
->SeekTable
!= NULL
)
1476 static void mpc_decoder_set_streaminfo(mpc_decoder
*d
, mpc_streaminfo
*si
)
1478 mpc_uint32_t seekTableSize
;
1480 mpc_decoder_reset_synthesis(d
);
1481 mpc_decoder_reset_globals(d
);
1483 d
->StreamVersion
= si
->stream_version
;
1484 d
->MS_used
= si
->ms
;
1485 d
->Max_Band
= si
->max_band
;
1486 d
->OverallFrames
= si
->frames
;
1487 d
->MPCHeaderPos
= si
->header_position
;
1488 d
->TrueGaplessPresent
= si
->is_true_gapless
;
1489 d
->SampleRate
= (mpc_int32_t
)si
->sample_freq
;
1491 d
->samples_to_skip
= MPC_DECODER_SYNTH_DELAY
;
1493 if (d
->SeekTable
!= NULL
)
1496 seekTableSize
= si
->frames
;
1497 d
->SeekTable
= (mpc_uint32_t
*) calloc( sizeof(mpc_uint32_t
), seekTableSize
);
1498 d
->SeekTable_Step
= si
->frames
/ seekTableSize
;
1499 if (si
->frames
% seekTableSize
)
1500 d
->SeekTable_Step
+=1;
1503 mpc_bool_t
mpc_decoder_initialize(mpc_decoder
*d
, mpc_streaminfo
*si
)
1505 mpc_uint32_t bitPos
;
1508 mpc_decoder_set_streaminfo(d
, si
);
1510 // setting position to the beginning of the data-bitstream
1511 bitPos
= get_initial_fpos(d
, d
->StreamVersion
);
1514 // fill buffer and initialize decoder
1515 f_seek(d
, fpos
*4 + d
->MPCHeaderPos
);
1516 f_read_dword(d
, d
->Speicher
, MEMSIZE
);
1519 d
->pos
= bitPos
& 31;
1520 d
->WordsRead
= fpos
;
1521 d
->dword
= SWAP(d
->Speicher
[0]);
1522 d
->next
= SWAP(d
->Speicher
[1]);
1523 d
->SeekTable_Step
= 1;
1528 // jumps over the current frame
1529 mpc_uint32_t
mpc_decoder_jump_frame(mpc_decoder
*d
) {
1531 mpc_uint32_t frameSize
;
1533 // ensure the buffer is full
1534 mpc_decoder_update_buffer(d
);
1537 frameSize
= mpc_decoder_bitstream_read(d
, 20);
1540 mpc_decoder_seek_forward(d
, frameSize
);
1542 return frameSize
+ 20;
1546 static mpc_uint32_t
get_initial_fpos(mpc_decoder
*d
, mpc_uint32_t StreamVersion
)
1548 mpc_uint32_t fpos
= 0;
1549 (void) StreamVersion
;
1551 // setting position to the beginning of the data-bitstream
1552 switch ( d
->StreamVersion
) {
1553 case 0x04: fpos
= 48; break;
1555 case 0x06: fpos
= 64; break;
1557 case 0x17: fpos
= 200; break;
1562 mpc_bool_t
mpc_decoder_seek_seconds(mpc_decoder
*d
, double seconds
)
1564 return mpc_decoder_seek_sample(d
, (mpc_int64_t
)(seconds
* (double)d
->SampleRate
+ 0.5));
1567 void mpc_decoder_reset_state(mpc_decoder
*d
) {
1569 memset(d
->Y_L
, 0, sizeof Y_L
);
1570 memset(d
->Y_R
, 0, sizeof Y_R
);
1572 memset(d
->SCF_Index_L
, -128, sizeof d
->SCF_Index_L
);
1573 memset(d
->SCF_Index_R
, -128, sizeof d
->SCF_Index_R
);
1575 memset(d
->SCF_Index_L
, 0, sizeof d
->SCF_Index_L
);
1576 memset(d
->SCF_Index_R
, 0, sizeof d
->SCF_Index_R
);
1578 memset(d
->Res_L
, 0, sizeof d
->Res_L
);
1579 memset(d
->Res_R
, 0, sizeof d
->Res_R
);
1580 memset(d
->SCFI_L
, 0, sizeof d
->SCFI_L
);
1581 memset(d
->SCFI_R
, 0, sizeof d
->SCFI_R
);
1582 #ifdef MPC_SUPPORT_SV456
1583 memset(d
->DSCF_Flag_L
, 0, sizeof d
->DSCF_Flag_L
);
1584 memset(d
->DSCF_Flag_R
, 0, sizeof d
->DSCF_Flag_R
);
1586 memset(d
->Q
, 0, sizeof d
->Q
);
1587 memset(d
->MS_Flag
, 0, sizeof d
->MS_Flag
);
1591 mpc_bool_t
mpc_decoder_seek_sample(mpc_decoder
*d
, mpc_int64_t destsample
)
1593 mpc_uint32_t fpos
= 0; // the bit to seek to
1594 mpc_uint32_t seekFrame
= 0; // the frame to seek to
1595 mpc_uint32_t lastFrame
= 0; // last frame to seek to before scanning scale factors
1596 mpc_int32_t delta
= 0; // direction of seek
1598 destsample
+= MPC_DECODER_SYNTH_DELAY
;
1599 seekFrame
= (mpc_uint32_t
) ((destsample
) / MPC_FRAME_LENGTH
);
1600 d
->samples_to_skip
= (mpc_uint32_t
)((destsample
) % MPC_FRAME_LENGTH
);
1602 // prevent from desired position out of allowed range
1603 seekFrame
= seekFrame
< d
->OverallFrames
? seekFrame
: d
->OverallFrames
;
1605 // seek direction (note: avoids casting to int64)
1606 delta
= (d
->DecodedFrames
> seekFrame
? -(mpc_int32_t
)(d
->DecodedFrames
- seekFrame
) : (mpc_int32_t
)(seekFrame
- d
->DecodedFrames
));
1608 // update max decoded frames
1609 if (d
->DecodedFrames
> d
->MaxDecodedFrames
)
1610 d
->MaxDecodedFrames
= d
->DecodedFrames
;
1613 lastFrame
= seekFrame
- 33 + 1 - d
->SeekTable_Step
;
1615 if (d
->MaxDecodedFrames
== 0) {
1617 mpc_decoder_reset_state(d
);
1619 // starts from the beginning since no frames have been decoded yet, or not using seek table
1620 fpos
= get_initial_fpos(d
, d
->StreamVersion
);
1622 // seek to the first frame
1623 mpc_decoder_seek_to(d
, fpos
);
1625 // reset number of decoded frames
1626 d
->DecodedFrames
= 0;
1628 // jump to the last frame, updating seek table
1629 if (d
->SeekTable_Step
== 1) {
1630 d
->SeekTable
[0] = (mpc_uint32_t
)fpos
;
1631 for (;d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
++)
1632 d
->SeekTable
[d
->DecodedFrames
+1] = mpc_decoder_jump_frame(d
);
1634 d
->SeekTableIndex
= 0;
1635 d
->SeekTableCounter
= (mpc_uint32_t
)fpos
;
1636 for (;d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
++) {
1637 if (d
->DecodedFrames
% d
->SeekTable_Step
== 0) {
1638 d
->SeekTable
[d
->SeekTableIndex
] = d
->SeekTableCounter
;
1639 d
->SeekTableIndex
+= 1;
1640 d
->SeekTableCounter
= 0;
1642 d
->SeekTableCounter
+= mpc_decoder_jump_frame(d
);
1647 } else if (delta
< 0) {
1649 mpc_decoder_reset_state(d
);
1651 // jumps backwards using the seek table
1652 fpos
= d
->SeekTable
[0];
1653 if (d
->SeekTable_Step
== 1) {
1654 for (d
->DecodedFrames
= 0;d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
++)
1655 fpos
+= d
->SeekTable
[d
->DecodedFrames
+1];
1657 d
->SeekTableIndex
= 0;
1658 //d->SeekTableCounter = 0;
1659 for (d
->DecodedFrames
= 0;d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
+=d
->SeekTable_Step
, d
->SeekTableIndex
++)
1660 fpos
+= d
->SeekTable
[d
->SeekTableIndex
+1];
1661 d
->SeekTableCounter
= d
->SeekTable
[d
->SeekTableIndex
];
1663 mpc_decoder_seek_to(d
, fpos
);
1665 } else if (delta
> 33) {
1667 mpc_decoder_reset_state(d
);
1669 // jumps forward from the current position
1670 if (d
->MaxDecodedFrames
> lastFrame
) { // REVIEW: Correct?? or (d->MaxDecodedFrames > d->DecodedFrames)
1671 // jump to the last usable position in the seek table
1672 if (d
->SeekTable_Step
== 1) {
1673 fpos
= mpc_decoder_bits_read(d
);
1674 for (; d
->DecodedFrames
< d
->MaxDecodedFrames
&& d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
++)
1675 fpos
+= d
->SeekTable
[d
->DecodedFrames
+1];
1677 // could test SeekTable offset and jump to next entry but this is easier for now...
1678 //d->SeekTableIndex = 0;
1679 //d->SeekTableCounter = 0;
1680 fpos
= d
->SeekTable
[0];
1681 d
->SeekTableIndex
= 0;
1682 for (d
->DecodedFrames
= 0;d
->DecodedFrames
< d
->MaxDecodedFrames
&& d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
+=d
->SeekTable_Step
, d
->SeekTableIndex
++)
1683 fpos
+= d
->SeekTable
[d
->SeekTableIndex
+1];
1684 d
->SeekTableCounter
= d
->SeekTable
[d
->SeekTableIndex
];
1687 mpc_decoder_seek_to(d
, fpos
);
1689 if (d
->SeekTable_Step
== 1) {
1690 for (;d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
++)
1691 d
->SeekTable
[d
->DecodedFrames
+1] = mpc_decoder_jump_frame(d
);
1693 for (;d
->DecodedFrames
< lastFrame
; d
->DecodedFrames
++) {
1694 if (d
->DecodedFrames
% d
->SeekTable_Step
== 0) {
1695 d
->SeekTable
[d
->SeekTableIndex
] = d
->SeekTableCounter
;
1696 d
->SeekTableIndex
+= 1;
1697 d
->SeekTableCounter
= 0;
1699 d
->SeekTableCounter
+= mpc_decoder_jump_frame(d
);
1705 mpc_decoder_update_buffer(d
);
1707 for (;d
->DecodedFrames
< seekFrame
; d
->DecodedFrames
++) {
1709 mpc_uint32_t FrameBitCnt
;
1711 d
->FwdJumpInfo
= mpc_decoder_bitstream_read(d
, 20); // read jump-info
1712 d
->ActDecodePos
= (d
->Zaehler
<< 5) + d
->pos
;
1713 FrameBitCnt
= mpc_decoder_bits_read(d
);
1714 // scanning the scalefactors (and check for validity of frame)
1715 if (d
->StreamVersion
>= 7) {
1716 mpc_decoder_read_bitstream_sv7(d
, (d
->DecodedFrames
< seekFrame
- 1));
1719 #ifdef MPC_SUPPORT_SV456
1720 mpc_decoder_read_bitstream_sv6(d
);
1726 FrameBitCnt
= mpc_decoder_bits_read(d
) - FrameBitCnt
;
1728 if (d
->FwdJumpInfo
> FrameBitCnt
)
1729 mpc_decoder_seek_forward(d
, d
->FwdJumpInfo
- FrameBitCnt
);
1730 else if (FrameBitCnt
!= d
->FwdJumpInfo
)
1731 // Bug in perform_jump;
1734 // REVIEW: Only if decodedFrames < maxDecodedFrames??
1735 if (d
->SeekTable_Step
== 1) {
1736 // check that the frame length corresponds with any data already in the seek table
1737 if (d
->SeekTable
[d
->DecodedFrames
+1] != 0 && d
->SeekTable
[d
->DecodedFrames
+1] != d
->FwdJumpInfo
+ 20)
1739 d
->SeekTable
[d
->DecodedFrames
+1] = d
->FwdJumpInfo
+ 20;
1741 if (d
->DecodedFrames
% d
->SeekTable_Step
== 0) {
1742 if (d
->SeekTable
[d
->SeekTableIndex
] != 0 && d
->SeekTable
[d
->SeekTableIndex
] != d
->SeekTableCounter
)
1744 d
->SeekTable
[d
->SeekTableIndex
] = d
->SeekTableCounter
;
1745 d
->SeekTableIndex
+= 1;
1746 d
->SeekTableCounter
= 0;
1748 d
->SeekTableCounter
+= d
->FwdJumpInfo
+ 20;
1752 mpc_decoder_update_buffer(d
);
1754 if (d
->DecodedFrames
== seekFrame
- 1) {
1756 // initialize the synth correctly for perfect decoding
1757 mpc_decoder_requantisierung(d
, d
->Max_Band
);
1758 mpc_decoder_synthese_filter_float(d
, NULL
);
1768 void mpc_decoder_fill_buffer(mpc_decoder
*d
) {
1770 f_read_dword(d
, d
->Speicher
, MEMSIZE
);
1771 d
->dword
= SWAP(d
->Speicher
[d
->Zaehler
= 0]);
1772 d
->next
= SWAP(d
->Speicher
[1]);
1778 void mpc_decoder_update_buffer(mpc_decoder
*d
)
1780 if ((d
->Ring
^ d
->Zaehler
) & MEMSIZE2
) {
1782 f_read_dword(d
, d
->Speicher
+ (d
->Ring
& MEMSIZE2
), MEMSIZE2
);
1783 d
->Ring
= d
->Zaehler
;
1788 void mpc_decoder_seek_to(mpc_decoder
*d
, mpc_uint32_t bitPos
) {
1791 mpc_uint32_t fpos
= (bitPos
>> 5);
1792 mpc_uint32_t bufferStart
= d
->WordsRead
- d
->Zaehler
;
1793 if ((d
->Zaehler
& MEMSIZE2
) != FALSE
)
1794 bufferStart
+= MEMSIZE2
;
1796 if (fpos
>= bufferStart
&& fpos
< bufferStart
+ MEMSIZE
) {
1798 // required position is within the buffer, no need to seek
1799 d
->Zaehler
= (fpos
- bufferStart
+ ((d
->Zaehler
& MEMSIZE2
) != FALSE
? MEMSIZE2
: 0)) & MEMMASK
;
1800 d
->pos
= bitPos
& 31;
1801 d
->WordsRead
= fpos
;
1802 d
->dword
= SWAP(d
->Speicher
[d
->Zaehler
]);
1803 d
->next
= SWAP(d
->Speicher
[(d
->Zaehler
+ 1) & MEMMASK
]);
1805 mpc_decoder_update_buffer(d
);
1811 f_seek(d
, fpos
*4 + d
->MPCHeaderPos
);
1813 d
->pos
= bitPos
& 31;
1814 d
->WordsRead
= fpos
;
1816 mpc_decoder_fill_buffer(d
);
1822 void mpc_decoder_seek_forward(mpc_decoder
*d
, mpc_uint32_t bits
) {
1826 bits
= bits
>> 5; // to DWORDs
1827 d
->Zaehler
= (d
->Zaehler
+ bits
) & MEMMASK
;
1828 d
->dword
= SWAP(d
->Speicher
[d
->Zaehler
]);
1829 d
->next
= SWAP(d
->Speicher
[(d
->Zaehler
+ 1) & MEMMASK
]);
1830 d
->WordsRead
+= bits
;