Further changes in mpc buffered seek. Remove magical number and replace it with a...
[kugel-rb.git] / apps / codecs / libmusepack / mpc_decoder.c
blobe63d1efaec783dad5b39441ec2110326b3c18b03
1 /*
2 Copyright (c) 2005, The Musepack Development Team
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
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
20 written permission.
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.
38 #include "musepack.h"
39 #include "internal.h"
40 #include "requant.h"
41 #include "huffman.h"
43 //SV7 tables
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
51 //SV4/5/6 tables
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];
59 #endif
61 #ifndef MPC_LITTLE_ENDIAN
62 #define SWAP(X) mpc_swap32(X)
63 #else
64 #define SWAP(X) X
65 #endif
67 #ifdef SCF_HACK
68 #define SCF_DIFF(SCF, D) (SCF == -128 ? -128 : SCF + D)
69 #else
70 #define SCF_DIFF(SCF, D) SCF + D
71 #endif
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 //------------------------------------------------------------------------------
95 // types
96 //------------------------------------------------------------------------------
97 enum
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 //------------------------------------------------------------------------------
128 // utility functions
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;
143 return count;
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,
158 0xFFFFFFFF
161 /* F U N C T I O N S */
163 // resets bitstream decoding
164 static void
165 mpc_decoder_reset_bitstream_decode(mpc_decoder *d)
167 d->dword = 0;
168 d->next = 0;
169 d->pos = 0;
170 d->Zaehler = 0;
171 d->WordsRead = 0;
174 // reports the number of read bits
175 static mpc_uint32_t
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;
183 d->dword = d->next;
184 d->next = SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]);
185 d->pos -= 32;
186 ++(d->WordsRead);
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;
195 d->pos += bits;
197 if (d->pos < 32) {
198 out >>= (32 - d->pos);
200 else {
201 mpc_move_next(d);
202 if (d->pos) {
203 out <<= d->pos;
204 out |= d->dword >> (32 - d->pos);
208 return out & mask[bits];
211 static void
212 mpc_decoder_make_huffman_lookup(
213 mpc_uint8_t* lookup, size_t length, const HuffmanTyp* Table, size_t elements )
215 size_t i;
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 )
222 idx--;
223 *lookup++ = (mpc_uint8_t)idx;
226 return;
229 #ifdef MPC_SUPPORT_SV456
230 // decode SCFI-bundle (sv4,5,6)
231 static void
232 mpc_decoder_scfi_bundle_read(
233 mpc_decoder *d,
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;
239 if (d->pos > 26) {
240 code |= d->next >> (32 - d->pos);
242 while (code < Table->Code) {
243 Table++;
246 // set the new position within bitstream without performing a dummy-read
247 if ((d->pos += Table->Length) >= 32) {
248 mpc_move_next(d);
251 *SCFI = Table->Value >> 1;
252 *DSCF = Table->Value & 1;
255 // basic huffman decoding routine
256 // works with maximum lengths up to 14
257 static mpc_int32_t
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;
263 if (d->pos > 18) {
264 code |= d->next >> (32 - d->pos);
266 while (code < Table->Code) {
267 Table++;
270 // set the new position within bitstream without performing a dummy-read
271 if ((d->pos += Table->Length) >= 32) {
272 mpc_move_next(d);
275 return Table->Value;
277 #endif
279 // faster huffman through previewing less bits
280 // works with maximum lengths up to 10
281 static mpc_int32_t
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;
287 if (d->pos > 22) {
288 code |= d->next >> (32 - d->pos);
290 while (code < Table->Code) {
291 Table++;
294 // set the new position within bitstream without performing a dummy-read
295 if ((d->pos += Table->Length) >= 32) {
296 mpc_move_next(d);
299 return Table->Value;
302 // even faster huffman through previewing even less bits
303 // works with maximum lengths up to 5
304 static mpc_int32_t
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;
310 if (d->pos > 27) {
311 code |= d->next >> (32 - d->pos);
313 while (code < Table->Code) {
314 Table++;
317 // set the new position within bitstream without performing a dummy-read
318 if ((d->pos += Table->Length) >= 32) {
319 mpc_move_next(d);
322 return Table->Value;
325 /* partial lookup table decode */
326 static mpc_int32_t
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) {
339 Table++;
342 // set the new position within bitstream without performing a dummy-read
343 if ((d->pos += Table->Length) >= 32) {
344 mpc_move_next(d);
347 return Table->Value;
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) {
365 mpc_move_next(d);
368 return Table->Value;
371 static void
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);
378 static void
379 mpc_decoder_reset_synthesis(mpc_decoder *d)
381 mpc_decoder_reset_v(d);
384 static void
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);
391 static void
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;
399 d->MS_used = 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);
412 #endif
413 memset(d->Q , 0, sizeof d->Q );
414 memset(d->MS_Flag , 0, sizeof d->MS_Flag );
417 mpc_uint32_t
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
428 case 0x04:
429 case 0x05:
430 case 0x06:
431 mpc_decoder_read_bitstream_sv6(d);
432 break;
433 #endif
434 case 0x07:
435 case 0x17:
436 mpc_decoder_read_bitstream_sv7(d, FALSE);
437 break;
438 default:
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);
446 static mpc_uint32_t
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);
459 } else {
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
487 case 0x04:
488 case 0x05:
489 case 0x06:
490 mpc_decoder_read_bitstream_sv6(d);
491 break;
492 #endif
493 case 0x07:
494 case 0x17:
495 mpc_decoder_read_bitstream_sv7(d, FALSE);
496 break;
497 default:
498 return (mpc_uint32_t)(-1);
500 d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == d->FwdJumpInfo;
502 d->DecodedFrames++;
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;
513 // synthesize signal
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) {
525 // Encoder bugfix
526 mod_block = 1152;
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;
545 else {
546 output_frame_length -= d->samples_to_skip;
547 memmove(
548 buffer,
549 buffer + d->samples_to_skip,
550 output_frame_length * sizeof (MPC_SAMPLE_FORMAT));
551 memmove(
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(
563 mpc_decoder *d,
564 MPC_SAMPLE_FORMAT *buffer,
565 mpc_uint32_t *vbr_update_acc,
566 mpc_uint32_t *vbr_update_bits)
568 for(;;)
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) ) {
576 return 0;
579 /**************** ERROR CONCEALMENT *****************/
580 if (d->FrameWasValid == 0 ) {
581 // error occurred in bitstream
582 return (mpc_uint32_t)(-1);
584 else {
585 if (vbr_update_acc && vbr_update_bits) {
586 (*vbr_update_acc) ++;
587 vbr_ring = (d->Zaehler << 5) + d->pos - vbr_ring;
588 if (vbr_ring < 0) {
589 vbr_ring += 524288;
591 (*vbr_update_bits) += vbr_ring;
595 mpc_decoder_update_buffer(d);
597 if (valid_samples > 0) {
598 return valid_samples;
603 void
604 mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band)
606 mpc_int32_t Band;
607 mpc_int32_t n;
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;
614 mpc_int16_t* L;
615 mpc_int16_t* R;
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])
621 #else
623 #error FIXME, Cc table is in 18.14 format
625 #endif
626 #else
627 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
628 MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
629 #endif
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;
634 L = d->Q[Band].L;
635 R = d->Q[Band].R;
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++));
644 *YR = templ - tempr;
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++));
650 *YR = templ - tempr;
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++));
656 *YR = templ - tempr;
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++);
672 } else {
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 ) {
689 *YR = *YL = 0;
694 /************************** LR-coded **************************/
695 else {
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++);
720 *YR = 0;
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++);
725 *YR = 0;
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++);
730 *YR = 0;
734 else {
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 ) {
738 *YL = 0;
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 ) {
743 *YL = 0;
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 ) {
748 *YL = 0;
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 ) {
753 *YR = *YL = 0;
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 ******************************************/
798 void
799 mpc_decoder_read_bitstream_sv6(mpc_decoder *d)
801 mpc_int32_t n,k;
802 mpc_int32_t Max_used_Band=0;
803 const HuffmanTyp *Table;
804 const HuffmanTyp *x1;
805 const HuffmanTyp *x2;
806 mpc_int8_t *L;
807 mpc_int8_t *R;
808 mpc_int16_t *QL;
809 mpc_int16_t *QR;
810 mpc_int8_t *ResL = d->Res_L;
811 mpc_int8_t *ResR = d->Res_R;
813 /************************ HEADER **************************/
814 ResL = d->Res_L;
815 ResR = d->Res_R;
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)];
823 if (d->MS_used) {
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 *****************************/
833 ResL = d->Res_L;
834 ResR = d->Res_R;
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 ********************************/
841 ResL = d->Res_L;
842 ResR = d->Res_R;
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)
847 if (*ResL)
849 /*********** DSCF ************/
850 if (d->DSCF_Flag_L[n]==1)
852 switch (d->SCFI_L[n])
854 case 3:
855 L[0] = L[2] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
856 L[1] = L[0];
857 L[2] = L[1];
858 break;
859 case 1:
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);
862 L[2] = L[1];
863 break;
864 case 2:
865 L[0] = L[2] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
866 L[1] = L[0];
867 L[2] = L[1] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
868 break;
869 case 0:
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);
873 break;
874 default:
875 return;
876 break;
879 /************ SCF ************/
880 else
882 switch (d->SCFI_L[n])
884 case 3:
885 L[0] = mpc_decoder_bitstream_read(d, 6);
886 L[1] = L[0];
887 L[2] = L[1];
888 break;
889 case 1:
890 L[0] = mpc_decoder_bitstream_read(d, 6);
891 L[1] = mpc_decoder_bitstream_read(d, 6);
892 L[2] = L[1];
893 break;
894 case 2:
895 L[0] = mpc_decoder_bitstream_read(d, 6);
896 L[1] = L[0];
897 L[2] = mpc_decoder_bitstream_read(d, 6);
898 break;
899 case 0:
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);
903 break;
904 default:
905 return;
906 break;
910 if (*ResR)
912 /*********** DSCF ************/
913 if (d->DSCF_Flag_R[n]==1)
915 switch (d->SCFI_R[n])
917 case 3:
918 R[0] = R[2] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
919 R[1] = R[0];
920 R[2] = R[1];
921 break;
922 case 1:
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);
925 R[2] = R[1];
926 break;
927 case 2:
928 R[0] = R[2] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
929 R[1] = R[0];
930 R[2] = R[1] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
931 break;
932 case 0:
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);
936 break;
937 default:
938 return;
939 break;
942 /************ SCF ************/
943 else
945 switch (d->SCFI_R[n])
947 case 3:
948 R[0] = mpc_decoder_bitstream_read(d, 6);
949 R[1] = R[0];
950 R[2] = R[1];
951 break;
952 case 1:
953 R[0] = mpc_decoder_bitstream_read(d, 6);
954 R[1] = mpc_decoder_bitstream_read(d, 6);
955 R[2] = R[1];
956 break;
957 case 2:
958 R[0] = mpc_decoder_bitstream_read(d, 6);
959 R[1] = R[0];
960 R[2] = mpc_decoder_bitstream_read(d, 6);
961 break;
962 case 0:
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);
966 break;
967 default:
968 return;
969 break;
975 /**************************** Samples ****************************/
976 ResL = d->Res_L;
977 ResR = d->Res_R;
978 for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR)
980 // setting pointers
981 x1 = mpc_table_SampleHuff[*ResL];
982 x2 = mpc_table_SampleHuff[*ResR];
983 QL = d->Q[n].L;
984 QR = d->Q[n].R;
986 if (x1!=NULL || x2!=NULL)
987 for (k=0; k<36; ++k)
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)
994 for (k=0; k<36; ++k)
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 ******************************************/
1003 void
1004 mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t fastSeeking)
1006 mpc_int32_t n,k;
1007 mpc_int32_t Max_used_Band=0;
1008 const HuffmanTyp *Table;
1009 mpc_int32_t idx;
1010 mpc_int8_t *L ,*R;
1011 mpc_int16_t *LQ ,*RQ;
1012 mpc_int8_t *ResL,*ResR;
1013 mpc_uint32_t tmp;
1014 mpc_uint8_t *LUT;
1015 mpc_uint8_t max_length;
1017 /***************************** Header *****************************/
1018 ResL = d->Res_L;
1019 ResR = d->Res_R;
1021 // first subband
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);
1026 } else {
1027 d->MS_Flag[0] = 0;
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) {
1046 Max_used_Band = n;
1047 } else {
1048 d->MS_Flag[n] = 0;
1051 /****************************** SCFI ******************************/
1052 L = d->SCFI_L;
1053 R = d->SCFI_R;
1054 ResL = d->Res_L;
1055 ResR = d->Res_R;
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 ****************************/
1062 ResL = d->Res_L;
1063 ResR = d->Res_R;
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) {
1067 if (*ResL)
1069 switch (d->SCFI_L[n])
1071 case 1:
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);
1076 L[2] = L[1];
1077 break;
1078 case 3:
1079 idx = Decode_DSCF ();
1080 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1081 L[1] = L[0];
1082 L[2] = L[1];
1083 break;
1084 case 2:
1085 idx = Decode_DSCF ();
1086 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1087 L[1] = L[0];
1088 idx = Decode_DSCF ();
1089 L[2] = (idx!=8) ? SCF_DIFF(L[1], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1090 break;
1091 case 0:
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);
1098 break;
1099 default:
1100 return;
1101 break;
1104 if (*ResR)
1106 switch (d->SCFI_R[n])
1108 case 1:
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);
1113 R[2] = R[1];
1114 break;
1115 case 3:
1116 idx = Decode_DSCF ();
1117 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1118 R[1] = R[0];
1119 R[2] = R[1];
1120 break;
1121 case 2:
1122 idx = Decode_DSCF ();
1123 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1124 R[1] = R[0];
1125 idx = Decode_DSCF ();
1126 R[2] = (idx!=8) ? SCF_DIFF(R[1], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1127 break;
1128 case 0:
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);
1135 break;
1136 default:
1137 return;
1138 break;
1143 if (fastSeeking)
1144 return;
1146 /***************************** Samples ****************************/
1147 ResL = d->Res_L;
1148 ResR = d->Res_R;
1149 LQ = d->Q[0].L;
1150 RQ = d->Q[0].R;
1151 for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, LQ+=36, RQ+=36)
1153 /************** links **************/
1154 switch (*ResL)
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:
1158 LQ += 36;
1159 break;
1160 case -1:
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;
1165 break;
1166 case 0:
1167 LQ += 36;// increase pointer
1168 break;
1169 case 1:
1170 if (mpc_decoder_bitstream_read(d, 1)) {
1171 Table = mpc_table_HuffQ[1][1];
1172 LUT = LUT1_1;
1173 max_length = 9;
1174 } else {
1175 Table = mpc_table_HuffQ[0][1];
1176 LUT = LUT1_0;
1177 max_length = 6;
1179 for (k=0; k<12; ++k)
1181 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1182 *LQ++ = idx30[idx];
1183 *LQ++ = idx31[idx];
1184 *LQ++ = idx32[idx];
1186 break;
1187 case 2:
1188 if (mpc_decoder_bitstream_read(d, 1)) {
1189 Table = mpc_table_HuffQ[1][2];
1190 LUT = LUT2_1;
1191 max_length = 10;
1192 } else {
1193 Table = mpc_table_HuffQ[0][2];
1194 LUT = LUT2_0;
1195 max_length = 7;
1197 for (k=0; k<18; ++k)
1199 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1200 *LQ++ = idx50[idx];
1201 *LQ++ = idx51[idx];
1203 break;
1204 case 3:
1205 if (mpc_decoder_bitstream_read(d, 1)) {
1206 Table = mpc_table_HuffQ[1][3];
1207 LUT = LUT3_1;
1208 max_length = 5;
1209 } else {
1210 Table = mpc_table_HuffQ[0][3];
1211 LUT = LUT3_0;
1212 max_length = 4;
1214 for (k=0; k<36; ++k)
1215 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1216 break;
1217 case 4:
1218 if (mpc_decoder_bitstream_read(d, 1)) {
1219 Table = mpc_table_HuffQ[1][4];
1220 LUT = LUT4_1;
1221 max_length = 5;
1222 } else {
1223 Table = mpc_table_HuffQ[0][4];
1224 LUT = LUT4_0;
1225 max_length = 4;
1227 for (k=0; k<36; ++k)
1228 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1229 break;
1230 case 5:
1231 if (mpc_decoder_bitstream_read(d, 1)) {
1232 Table = mpc_table_HuffQ[1][5];
1233 LUT = LUT5_1;
1234 max_length = 8;
1235 } else {
1236 Table = mpc_table_HuffQ[0][5];
1237 LUT = LUT5_0;
1238 max_length = 6;
1240 for (k=0; k<36; ++k)
1241 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1242 break;
1243 case 6:
1244 if (mpc_decoder_bitstream_read(d, 1)) {
1245 Table = mpc_table_HuffQ[1][6];
1246 LUT = LUT6_1;
1247 max_length = 7;
1248 for (k=0; k<36; ++k)
1249 *LQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
1250 } else {
1251 Table = mpc_table_HuffQ[0][6];
1252 LUT = LUT6_0;
1253 max_length = 7;
1254 for (k=0; k<36; ++k)
1255 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1257 break;
1258 case 7:
1259 if (mpc_decoder_bitstream_read(d, 1)) {
1260 Table = mpc_table_HuffQ[1][7];
1261 LUT = LUT7_1;
1262 max_length = 8;
1263 for (k=0; k<36; ++k)
1264 *LQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
1265 } else {
1266 Table = mpc_table_HuffQ[0][7];
1267 LUT = LUT7_0;
1268 max_length = 8;
1269 for (k=0; k<36; ++k)
1270 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1272 break;
1273 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
1274 tmp = Dc[*ResL];
1275 for (k=0; k<36; ++k)
1276 *LQ++ = (mpc_int16_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;
1277 break;
1278 default:
1279 return;
1281 /************** rechts **************/
1282 switch (*ResR)
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:
1286 RQ += 36;
1287 break;
1288 case -1:
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;
1293 break;
1294 case 0:
1295 RQ += 36;// increase pointer
1296 break;
1297 case 1:
1298 if (mpc_decoder_bitstream_read(d, 1)) {
1299 Table = mpc_table_HuffQ[1][1];
1300 LUT = LUT1_1;
1301 max_length = 9;
1302 } else {
1303 Table = mpc_table_HuffQ[0][1];
1304 LUT = LUT1_0;
1305 max_length = 6;
1307 for (k=0; k<12; ++k)
1309 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1310 *RQ++ = idx30[idx];
1311 *RQ++ = idx31[idx];
1312 *RQ++ = idx32[idx];
1314 break;
1315 case 2:
1316 if (mpc_decoder_bitstream_read(d, 1)) {
1317 Table = mpc_table_HuffQ[1][2];
1318 LUT = LUT2_1;
1319 max_length = 10;
1320 } else {
1321 Table = mpc_table_HuffQ[0][2];
1322 LUT = LUT2_0;
1323 max_length = 7;
1325 for (k=0; k<18; ++k)
1327 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1328 *RQ++ = idx50[idx];
1329 *RQ++ = idx51[idx];
1331 break;
1332 case 3:
1333 if (mpc_decoder_bitstream_read(d, 1)) {
1334 Table = mpc_table_HuffQ[1][3];
1335 LUT = LUT3_1;
1336 max_length = 5;
1337 } else {
1338 Table = mpc_table_HuffQ[0][3];
1339 LUT = LUT3_0;
1340 max_length = 4;
1342 for (k=0; k<36; ++k)
1343 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1344 break;
1345 case 4:
1346 if (mpc_decoder_bitstream_read(d, 1)) {
1347 Table = mpc_table_HuffQ[1][4];
1348 LUT = LUT4_1;
1349 max_length = 5;
1350 } else {
1351 Table = mpc_table_HuffQ[0][4];
1352 LUT = LUT4_0;
1353 max_length = 4;
1355 for (k=0; k<36; ++k)
1356 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1357 break;
1358 case 5:
1359 if (mpc_decoder_bitstream_read(d, 1)) {
1360 Table = mpc_table_HuffQ[1][5];
1361 LUT = LUT5_1;
1362 max_length = 8;
1363 } else {
1364 Table = mpc_table_HuffQ[0][5];
1365 LUT = LUT5_0;
1366 max_length = 6;
1368 for (k=0; k<36; ++k)
1369 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1370 break;
1371 case 6:
1372 if (mpc_decoder_bitstream_read(d, 1)) {
1373 Table = mpc_table_HuffQ[1][6];
1374 LUT = LUT6_1;
1375 max_length = 7;
1376 for (k=0; k<36; ++k)
1377 *RQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
1378 } else {
1379 Table = mpc_table_HuffQ[0][6];
1380 LUT = LUT6_0;
1381 max_length = 7;
1382 for (k=0; k<36; ++k)
1383 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1385 break;
1386 case 7:
1387 if (mpc_decoder_bitstream_read(d, 1)) {
1388 Table = mpc_table_HuffQ[1][7];
1389 LUT = LUT7_1;
1390 max_length = 8;
1391 for (k=0; k<36; ++k)
1392 *RQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
1393 } else {
1394 Table = mpc_table_HuffQ[0][7];
1395 LUT = LUT7_0;
1396 max_length = 8;
1397 for (k=0; k<36; ++k)
1398 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1400 break;
1401 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
1402 tmp = Dc[*ResR];
1403 for (k=0; k<36; ++k)
1404 *RQ++ = (mpc_int16_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;
1405 break;
1406 default:
1407 return;
1412 void mpc_decoder_setup(mpc_decoder *d, mpc_reader *r)
1414 d->r = r;
1416 d->MPCHeaderPos = 0;
1417 d->StreamVersion = 0;
1418 d->MS_used = 0;
1419 d->FwdJumpInfo = 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;
1427 d->WordsRead = 0;
1428 d->Max_Band = 0;
1429 d->SampleRate = 0;
1430 d->__r1 = 1;
1431 d->__r2 = 1;
1433 d->dword = 0;
1434 d->pos = 0;
1435 d->Zaehler = 0;
1436 d->Ring = 0;
1437 d->WordsRead = 0;
1438 d->Max_Band = 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;
1463 d->Y_L = Y_L;
1464 d->Y_R = Y_R;
1466 #if defined(CPU_COLDFIRE)
1467 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
1468 #endif
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;
1503 mpc_uint32_t fpos;
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);
1509 fpos = bitPos >> 5;
1511 // fill buffer and initialize decoder
1512 f_seek(d, fpos*4 + d->MPCHeaderPos);
1513 f_read_dword(d, d->Speicher, MEMSIZE);
1514 d->Ring = 0;
1515 d->Zaehler = 0;
1516 d->pos = bitPos & 31;
1517 d->WordsRead = fpos;
1518 d->dword = SWAP(d->Speicher[0]);
1519 d->next = SWAP(d->Speicher[1]);
1521 return TRUE;
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);
1532 // bits in frame
1533 frameSize = mpc_decoder_bitstream_read(d, 20);
1535 // jump forward
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;
1550 case 0x05:
1551 case 0x06: fpos = 64; break;
1552 case 0x07:
1553 case 0x17: fpos = 200; break;
1555 return fpos;
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 );
1567 #ifdef SCF_HACK
1568 memset(d->SCF_Index_L , -128, sizeof d->SCF_Index_L );
1569 memset(d->SCF_Index_R , -128, sizeof d->SCF_Index_R );
1570 #else
1571 memset(d->SCF_Index_L , 0, sizeof d->SCF_Index_L );
1572 memset(d->SCF_Index_R , 0, sizeof d->SCF_Index_R );
1573 #endif
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 );
1581 #endif
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));
1691 else
1693 #ifdef MPC_SUPPORT_SV456
1694 mpc_decoder_read_bitstream_sv6(d);
1695 #else
1696 return FALSE;
1697 #endif
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;
1706 return FALSE;
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;
1717 // update buffer
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);
1728 return TRUE;
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]);
1737 d->Ring = 0;
1742 void mpc_decoder_update_buffer(mpc_decoder *d)
1744 if ((d->Ring ^ d->Zaehler) & MEMSIZE2) {
1745 // update buffer
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) {
1754 // required dword
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);
1772 } else {
1774 // DWORD aligned
1775 f_seek(d, fpos*4 + d->MPCHeaderPos);
1776 d->Zaehler = 0;
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) {
1788 bits += d->pos;
1789 d->pos = bits & 31;
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;