autodetection: convert path to native separators before displaying it.
[Rockbox.git] / apps / codecs / libmusepack / mpc_decoder.c
blobf7687c5bad0b7d50e719e47cb5b2d25c7f583276
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 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 //------------------------------------------------------------------------------
126 // utility functions
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;
141 return count;
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,
156 0xFFFFFFFF
159 /* F U N C T I O N S */
161 // resets bitstream decoding
162 static void
163 mpc_decoder_reset_bitstream_decode(mpc_decoder *d)
165 d->dword = 0;
166 d->next = 0;
167 d->pos = 0;
168 d->Zaehler = 0;
169 d->WordsRead = 0;
172 // reports the number of read bits
173 static mpc_uint32_t
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;
181 d->dword = d->next;
182 d->next = SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]);
183 d->pos -= 32;
184 ++(d->WordsRead);
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;
193 d->pos += bits;
195 if (d->pos < 32) {
196 out >>= (32 - d->pos);
198 else {
199 mpc_move_next(d);
200 if (d->pos) {
201 out <<= d->pos;
202 out |= d->dword >> (32 - d->pos);
206 return out & mask[bits];
209 static void
210 mpc_decoder_make_huffman_lookup(
211 mpc_uint8_t* lookup, size_t length, const HuffmanTyp* Table, size_t elements )
213 size_t i;
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 )
220 idx--;
221 *lookup++ = (mpc_uint8_t)idx;
224 return;
227 #ifdef MPC_SUPPORT_SV456
228 // decode SCFI-bundle (sv4,5,6)
229 static void
230 mpc_decoder_scfi_bundle_read(
231 mpc_decoder *d,
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;
237 if (d->pos > 26) {
238 code |= d->next >> (32 - d->pos);
240 while (code < Table->Code) {
241 Table++;
244 // set the new position within bitstream without performing a dummy-read
245 if ((d->pos += Table->Length) >= 32) {
246 mpc_move_next(d);
249 *SCFI = Table->Value >> 1;
250 *DSCF = Table->Value & 1;
253 // basic huffman decoding routine
254 // works with maximum lengths up to 14
255 static mpc_int32_t
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;
261 if (d->pos > 18) {
262 code |= d->next >> (32 - d->pos);
264 while (code < Table->Code) {
265 Table++;
268 // set the new position within bitstream without performing a dummy-read
269 if ((d->pos += Table->Length) >= 32) {
270 mpc_move_next(d);
273 return Table->Value;
275 #endif
277 // faster huffman through previewing less bits
278 // works with maximum lengths up to 10
279 static mpc_int32_t
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;
285 if (d->pos > 22) {
286 code |= d->next >> (32 - d->pos);
288 while (code < Table->Code) {
289 Table++;
292 // set the new position within bitstream without performing a dummy-read
293 if ((d->pos += Table->Length) >= 32) {
294 mpc_move_next(d);
297 return Table->Value;
300 // even faster huffman through previewing even less bits
301 // works with maximum lengths up to 5
302 static mpc_int32_t
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;
308 if (d->pos > 27) {
309 code |= d->next >> (32 - d->pos);
311 while (code < Table->Code) {
312 Table++;
315 // set the new position within bitstream without performing a dummy-read
316 if ((d->pos += Table->Length) >= 32) {
317 mpc_move_next(d);
320 return Table->Value;
323 /* partial lookup table decode */
324 static mpc_int32_t
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) {
337 Table++;
340 // set the new position within bitstream without performing a dummy-read
341 if ((d->pos += Table->Length) >= 32) {
342 mpc_move_next(d);
345 return Table->Value;
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) {
363 mpc_move_next(d);
366 return Table->Value;
369 static void
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);
376 static void
377 mpc_decoder_reset_synthesis(mpc_decoder *d)
379 mpc_decoder_reset_v(d);
382 static void
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);
389 static void
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;
398 d->MS_used = 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);
411 #endif
412 memset(d->Q , 0, sizeof d->Q );
413 memset(d->MS_Flag , 0, sizeof d->MS_Flag );
416 mpc_uint32_t
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
427 case 0x04:
428 case 0x05:
429 case 0x06:
430 mpc_decoder_read_bitstream_sv6(d);
431 break;
432 #endif
433 case 0x07:
434 case 0x17:
435 mpc_decoder_read_bitstream_sv7(d, FALSE);
436 break;
437 default:
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);
445 static mpc_uint32_t
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);
458 } else {
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
483 case 0x04:
484 case 0x05:
485 case 0x06:
486 mpc_decoder_read_bitstream_sv6(d);
487 break;
488 #endif
489 case 0x07:
490 case 0x17:
491 mpc_decoder_read_bitstream_sv7(d, FALSE);
492 break;
493 default:
494 return (mpc_uint32_t)(-1);
496 d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == d->FwdJumpInfo;
498 d->DecodedFrames++;
500 /* update seek table */
501 if (d->SeekTable_Step == 1) {
502 d->SeekTable [d->DecodedFrames] = d->FwdJumpInfo + 20;
503 } else {
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;
512 // synthesize signal
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) {
524 // Encoder bugfix
525 mod_block = 1152;
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;
544 else {
545 output_frame_length -= d->samples_to_skip;
546 memmove(
547 buffer,
548 buffer + d->samples_to_skip,
549 output_frame_length * sizeof (MPC_SAMPLE_FORMAT));
550 memmove(
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(
562 mpc_decoder *d,
563 MPC_SAMPLE_FORMAT *buffer,
564 mpc_uint32_t *vbr_update_acc,
565 mpc_uint32_t *vbr_update_bits)
567 for(;;)
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) ) {
575 return 0;
578 /**************** ERROR CONCEALMENT *****************/
579 if (d->FrameWasValid == 0 ) {
580 // error occurred in bitstream
581 return (mpc_uint32_t)(-1);
583 else {
584 if (vbr_update_acc && vbr_update_bits) {
585 (*vbr_update_acc) ++;
586 vbr_ring = (d->Zaehler << 5) + d->pos - vbr_ring;
587 if (vbr_ring < 0) {
588 vbr_ring += 524288;
590 (*vbr_update_bits) += vbr_ring;
594 mpc_decoder_update_buffer(d);
596 if (valid_samples > 0) {
597 return valid_samples;
602 void
603 mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band)
605 mpc_int32_t Band;
606 mpc_int32_t n;
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;
613 mpc_int16_t* L;
614 mpc_int16_t* R;
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])
620 #else
622 #error FIXME, Cc table is in 18.14 format
624 #endif
625 #else
626 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
627 MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
628 #endif
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;
633 L = d->Q[Band].L;
634 R = d->Q[Band].R;
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++));
643 *YR = templ - tempr;
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++));
649 *YR = templ - tempr;
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++));
655 *YR = templ - tempr;
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++);
671 } else {
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 ) {
688 *YR = *YL = 0;
693 /************************** LR-coded **************************/
694 else {
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++);
719 *YR = 0;
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++);
724 *YR = 0;
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++);
729 *YR = 0;
733 else {
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 ) {
737 *YL = 0;
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 ) {
742 *YL = 0;
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 ) {
747 *YL = 0;
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 ) {
752 *YR = *YL = 0;
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 ******************************************/
797 void
798 mpc_decoder_read_bitstream_sv6(mpc_decoder *d)
800 mpc_int32_t n,k;
801 mpc_int32_t Max_used_Band=0;
802 const HuffmanTyp *Table;
803 const HuffmanTyp *x1;
804 const HuffmanTyp *x2;
805 mpc_int8_t *L;
806 mpc_int8_t *R;
807 mpc_int16_t *QL;
808 mpc_int16_t *QR;
809 mpc_int8_t *ResL = d->Res_L;
810 mpc_int8_t *ResR = d->Res_R;
812 /************************ HEADER **************************/
813 ResL = d->Res_L;
814 ResR = d->Res_R;
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)];
822 if (d->MS_used) {
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 *****************************/
832 ResL = d->Res_L;
833 ResR = d->Res_R;
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 ********************************/
840 ResL = d->Res_L;
841 ResR = d->Res_R;
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)
846 if (*ResL)
848 /*********** DSCF ************/
849 if (d->DSCF_Flag_L[n]==1)
851 switch (d->SCFI_L[n])
853 case 3:
854 L[0] = L[2] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
855 L[1] = L[0];
856 L[2] = L[1];
857 break;
858 case 1:
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);
861 L[2] = L[1];
862 break;
863 case 2:
864 L[0] = L[2] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
865 L[1] = L[0];
866 L[2] = L[1] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
867 break;
868 case 0:
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);
872 break;
873 default:
874 return;
875 break;
878 /************ SCF ************/
879 else
881 switch (d->SCFI_L[n])
883 case 3:
884 L[0] = mpc_decoder_bitstream_read(d, 6);
885 L[1] = L[0];
886 L[2] = L[1];
887 break;
888 case 1:
889 L[0] = mpc_decoder_bitstream_read(d, 6);
890 L[1] = mpc_decoder_bitstream_read(d, 6);
891 L[2] = L[1];
892 break;
893 case 2:
894 L[0] = mpc_decoder_bitstream_read(d, 6);
895 L[1] = L[0];
896 L[2] = mpc_decoder_bitstream_read(d, 6);
897 break;
898 case 0:
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);
902 break;
903 default:
904 return;
905 break;
909 if (*ResR)
911 /*********** DSCF ************/
912 if (d->DSCF_Flag_R[n]==1)
914 switch (d->SCFI_R[n])
916 case 3:
917 R[0] = R[2] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
918 R[1] = R[0];
919 R[2] = R[1];
920 break;
921 case 1:
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);
924 R[2] = R[1];
925 break;
926 case 2:
927 R[0] = R[2] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
928 R[1] = R[0];
929 R[2] = R[1] + mpc_decoder_huffman_decode_fast(d, mpc_table_DSCF_Entropie);
930 break;
931 case 0:
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);
935 break;
936 default:
937 return;
938 break;
941 /************ SCF ************/
942 else
944 switch (d->SCFI_R[n])
946 case 3:
947 R[0] = mpc_decoder_bitstream_read(d, 6);
948 R[1] = R[0];
949 R[2] = R[1];
950 break;
951 case 1:
952 R[0] = mpc_decoder_bitstream_read(d, 6);
953 R[1] = mpc_decoder_bitstream_read(d, 6);
954 R[2] = R[1];
955 break;
956 case 2:
957 R[0] = mpc_decoder_bitstream_read(d, 6);
958 R[1] = R[0];
959 R[2] = mpc_decoder_bitstream_read(d, 6);
960 break;
961 case 0:
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);
965 break;
966 default:
967 return;
968 break;
974 /**************************** Samples ****************************/
975 ResL = d->Res_L;
976 ResR = d->Res_R;
977 for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR)
979 // setting pointers
980 x1 = mpc_table_SampleHuff[*ResL];
981 x2 = mpc_table_SampleHuff[*ResR];
982 QL = d->Q[n].L;
983 QR = d->Q[n].R;
985 if (x1!=NULL || x2!=NULL)
986 for (k=0; k<36; ++k)
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)
993 for (k=0; k<36; ++k)
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 ******************************************/
1002 void
1003 mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t fastSeeking)
1005 mpc_int32_t n,k;
1006 mpc_int32_t Max_used_Band=0;
1007 const HuffmanTyp *Table;
1008 mpc_int32_t idx;
1009 mpc_int8_t *L ,*R;
1010 mpc_int16_t *LQ ,*RQ;
1011 mpc_int8_t *ResL,*ResR;
1012 mpc_uint32_t tmp;
1013 mpc_uint8_t *LUT;
1014 mpc_uint8_t max_length;
1016 /***************************** Header *****************************/
1017 ResL = d->Res_L;
1018 ResR = d->Res_R;
1020 // first subband
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);
1025 } else {
1026 d->MS_Flag[0] = 0;
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) {
1045 Max_used_Band = n;
1046 } else {
1047 d->MS_Flag[n] = 0;
1050 /****************************** SCFI ******************************/
1051 L = d->SCFI_L;
1052 R = d->SCFI_R;
1053 ResL = d->Res_L;
1054 ResR = d->Res_R;
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 ****************************/
1061 ResL = d->Res_L;
1062 ResR = d->Res_R;
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) {
1066 if (*ResL)
1068 switch (d->SCFI_L[n])
1070 case 1:
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);
1075 L[2] = L[1];
1076 break;
1077 case 3:
1078 idx = Decode_DSCF ();
1079 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1080 L[1] = L[0];
1081 L[2] = L[1];
1082 break;
1083 case 2:
1084 idx = Decode_DSCF ();
1085 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1086 L[1] = L[0];
1087 idx = Decode_DSCF ();
1088 L[2] = (idx!=8) ? SCF_DIFF(L[1], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1089 break;
1090 case 0:
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);
1097 break;
1098 default:
1099 return;
1100 break;
1103 if (*ResR)
1105 switch (d->SCFI_R[n])
1107 case 1:
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);
1112 R[2] = R[1];
1113 break;
1114 case 3:
1115 idx = Decode_DSCF ();
1116 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1117 R[1] = R[0];
1118 R[2] = R[1];
1119 break;
1120 case 2:
1121 idx = Decode_DSCF ();
1122 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1123 R[1] = R[0];
1124 idx = Decode_DSCF ();
1125 R[2] = (idx!=8) ? SCF_DIFF(R[1], idx) : (int) mpc_decoder_bitstream_read(d, 6);
1126 break;
1127 case 0:
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);
1134 break;
1135 default:
1136 return;
1137 break;
1142 if (fastSeeking)
1143 return;
1145 /***************************** Samples ****************************/
1146 ResL = d->Res_L;
1147 ResR = d->Res_R;
1148 LQ = d->Q[0].L;
1149 RQ = d->Q[0].R;
1150 for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, LQ+=36, RQ+=36)
1152 /************** links **************/
1153 switch (*ResL)
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:
1157 LQ += 36;
1158 break;
1159 case -1:
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;
1164 break;
1165 case 0:
1166 LQ += 36;// increase pointer
1167 break;
1168 case 1:
1169 if (mpc_decoder_bitstream_read(d, 1)) {
1170 Table = mpc_table_HuffQ[1][1];
1171 LUT = LUT1_1;
1172 max_length = 9;
1173 } else {
1174 Table = mpc_table_HuffQ[0][1];
1175 LUT = LUT1_0;
1176 max_length = 6;
1178 for (k=0; k<12; ++k)
1180 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1181 *LQ++ = idx30[idx];
1182 *LQ++ = idx31[idx];
1183 *LQ++ = idx32[idx];
1185 break;
1186 case 2:
1187 if (mpc_decoder_bitstream_read(d, 1)) {
1188 Table = mpc_table_HuffQ[1][2];
1189 LUT = LUT2_1;
1190 max_length = 10;
1191 } else {
1192 Table = mpc_table_HuffQ[0][2];
1193 LUT = LUT2_0;
1194 max_length = 7;
1196 for (k=0; k<18; ++k)
1198 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1199 *LQ++ = idx50[idx];
1200 *LQ++ = idx51[idx];
1202 break;
1203 case 3:
1204 if (mpc_decoder_bitstream_read(d, 1)) {
1205 Table = mpc_table_HuffQ[1][3];
1206 LUT = LUT3_1;
1207 max_length = 5;
1208 } else {
1209 Table = mpc_table_HuffQ[0][3];
1210 LUT = LUT3_0;
1211 max_length = 4;
1213 for (k=0; k<36; ++k)
1214 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1215 break;
1216 case 4:
1217 if (mpc_decoder_bitstream_read(d, 1)) {
1218 Table = mpc_table_HuffQ[1][4];
1219 LUT = LUT4_1;
1220 max_length = 5;
1221 } else {
1222 Table = mpc_table_HuffQ[0][4];
1223 LUT = LUT4_0;
1224 max_length = 4;
1226 for (k=0; k<36; ++k)
1227 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1228 break;
1229 case 5:
1230 if (mpc_decoder_bitstream_read(d, 1)) {
1231 Table = mpc_table_HuffQ[1][5];
1232 LUT = LUT5_1;
1233 max_length = 8;
1234 } else {
1235 Table = mpc_table_HuffQ[0][5];
1236 LUT = LUT5_0;
1237 max_length = 6;
1239 for (k=0; k<36; ++k)
1240 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1241 break;
1242 case 6:
1243 if (mpc_decoder_bitstream_read(d, 1)) {
1244 Table = mpc_table_HuffQ[1][6];
1245 LUT = LUT6_1;
1246 max_length = 7;
1247 for (k=0; k<36; ++k)
1248 *LQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
1249 } else {
1250 Table = mpc_table_HuffQ[0][6];
1251 LUT = LUT6_0;
1252 max_length = 7;
1253 for (k=0; k<36; ++k)
1254 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1256 break;
1257 case 7:
1258 if (mpc_decoder_bitstream_read(d, 1)) {
1259 Table = mpc_table_HuffQ[1][7];
1260 LUT = LUT7_1;
1261 max_length = 8;
1262 for (k=0; k<36; ++k)
1263 *LQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
1264 } else {
1265 Table = mpc_table_HuffQ[0][7];
1266 LUT = LUT7_0;
1267 max_length = 8;
1268 for (k=0; k<36; ++k)
1269 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1271 break;
1272 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
1273 tmp = Dc[*ResL];
1274 for (k=0; k<36; ++k)
1275 *LQ++ = (mpc_int16_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;
1276 break;
1277 default:
1278 return;
1280 /************** rechts **************/
1281 switch (*ResR)
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:
1285 RQ += 36;
1286 break;
1287 case -1:
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;
1292 break;
1293 case 0:
1294 RQ += 36;// increase pointer
1295 break;
1296 case 1:
1297 if (mpc_decoder_bitstream_read(d, 1)) {
1298 Table = mpc_table_HuffQ[1][1];
1299 LUT = LUT1_1;
1300 max_length = 9;
1301 } else {
1302 Table = mpc_table_HuffQ[0][1];
1303 LUT = LUT1_0;
1304 max_length = 6;
1306 for (k=0; k<12; ++k)
1308 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1309 *RQ++ = idx30[idx];
1310 *RQ++ = idx31[idx];
1311 *RQ++ = idx32[idx];
1313 break;
1314 case 2:
1315 if (mpc_decoder_bitstream_read(d, 1)) {
1316 Table = mpc_table_HuffQ[1][2];
1317 LUT = LUT2_1;
1318 max_length = 10;
1319 } else {
1320 Table = mpc_table_HuffQ[0][2];
1321 LUT = LUT2_0;
1322 max_length = 7;
1324 for (k=0; k<18; ++k)
1326 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1327 *RQ++ = idx50[idx];
1328 *RQ++ = idx51[idx];
1330 break;
1331 case 3:
1332 if (mpc_decoder_bitstream_read(d, 1)) {
1333 Table = mpc_table_HuffQ[1][3];
1334 LUT = LUT3_1;
1335 max_length = 5;
1336 } else {
1337 Table = mpc_table_HuffQ[0][3];
1338 LUT = LUT3_0;
1339 max_length = 4;
1341 for (k=0; k<36; ++k)
1342 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1343 break;
1344 case 4:
1345 if (mpc_decoder_bitstream_read(d, 1)) {
1346 Table = mpc_table_HuffQ[1][4];
1347 LUT = LUT4_1;
1348 max_length = 5;
1349 } else {
1350 Table = mpc_table_HuffQ[0][4];
1351 LUT = LUT4_0;
1352 max_length = 4;
1354 for (k=0; k<36; ++k)
1355 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1356 break;
1357 case 5:
1358 if (mpc_decoder_bitstream_read(d, 1)) {
1359 Table = mpc_table_HuffQ[1][5];
1360 LUT = LUT5_1;
1361 max_length = 8;
1362 } else {
1363 Table = mpc_table_HuffQ[0][5];
1364 LUT = LUT5_0;
1365 max_length = 6;
1367 for (k=0; k<36; ++k)
1368 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1369 break;
1370 case 6:
1371 if (mpc_decoder_bitstream_read(d, 1)) {
1372 Table = mpc_table_HuffQ[1][6];
1373 LUT = LUT6_1;
1374 max_length = 7;
1375 for (k=0; k<36; ++k)
1376 *RQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
1377 } else {
1378 Table = mpc_table_HuffQ[0][6];
1379 LUT = LUT6_0;
1380 max_length = 7;
1381 for (k=0; k<36; ++k)
1382 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1384 break;
1385 case 7:
1386 if (mpc_decoder_bitstream_read(d, 1)) {
1387 Table = mpc_table_HuffQ[1][7];
1388 LUT = LUT7_1;
1389 max_length = 8;
1390 for (k=0; k<36; ++k)
1391 *RQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
1392 } else {
1393 Table = mpc_table_HuffQ[0][7];
1394 LUT = LUT7_0;
1395 max_length = 8;
1396 for (k=0; k<36; ++k)
1397 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
1399 break;
1400 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
1401 tmp = Dc[*ResR];
1402 for (k=0; k<36; ++k)
1403 *RQ++ = (mpc_int16_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;
1404 break;
1405 default:
1406 return;
1411 void mpc_decoder_setup(mpc_decoder *d, mpc_reader *r)
1413 d->r = r;
1415 d->MPCHeaderPos = 0;
1416 d->StreamVersion = 0;
1417 d->MS_used = 0;
1418 d->FwdJumpInfo = 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;
1426 d->WordsRead = 0;
1427 d->Max_Band = 0;
1428 d->SampleRate = 0;
1429 d->__r1 = 1;
1430 d->__r2 = 1;
1432 d->dword = 0;
1433 d->pos = 0;
1434 d->Zaehler = 0;
1435 d->Ring = 0;
1436 d->WordsRead = 0;
1437 d->Max_Band = 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;
1462 d->Y_L = Y_L;
1463 d->Y_R = Y_R;
1465 #if defined(CPU_COLDFIRE)
1466 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
1467 #endif
1470 void mpc_decoder_destroy(mpc_decoder *d)
1472 if (d->SeekTable != NULL)
1473 free(d->SeekTable);
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)
1494 free(d->SeekTable);
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;
1506 mpc_uint32_t fpos;
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);
1512 fpos = bitPos >> 5;
1514 // fill buffer and initialize decoder
1515 f_seek(d, fpos*4 + d->MPCHeaderPos);
1516 f_read_dword(d, d->Speicher, MEMSIZE);
1517 d->Ring = 0;
1518 d->Zaehler = 0;
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;
1525 return TRUE;
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);
1536 // bits in frame
1537 frameSize = mpc_decoder_bitstream_read(d, 20);
1539 // jump forward
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;
1554 case 0x05:
1555 case 0x06: fpos = 64; break;
1556 case 0x07:
1557 case 0x17: fpos = 200; break;
1559 return fpos;
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 );
1571 #ifdef SCF_HACK
1572 memset(d->SCF_Index_L , -128, sizeof d->SCF_Index_L );
1573 memset(d->SCF_Index_R , -128, sizeof d->SCF_Index_R );
1574 #else
1575 memset(d->SCF_Index_L , 0, sizeof d->SCF_Index_L );
1576 memset(d->SCF_Index_R , 0, sizeof d->SCF_Index_R );
1577 #endif
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 );
1585 #endif
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;
1612 if (seekFrame > 33)
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);
1633 } else {
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];
1656 } else {
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];
1676 } else {
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);
1692 } else {
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);
1704 // REVIEW: Needed?
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));
1718 else {
1719 #ifdef MPC_SUPPORT_SV456
1720 mpc_decoder_read_bitstream_sv6(d);
1721 #else
1722 return FALSE;
1723 #endif
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;
1732 return FALSE;
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)
1738 return FALSE;
1739 d->SeekTable [d->DecodedFrames+1] = d->FwdJumpInfo + 20;
1740 } else {
1741 if (d->DecodedFrames % d->SeekTable_Step == 0) {
1742 if (d->SeekTable[d->SeekTableIndex] != 0 && d->SeekTable[d->SeekTableIndex] != d->SeekTableCounter)
1743 return FALSE;
1744 d->SeekTable[d->SeekTableIndex] = d->SeekTableCounter;
1745 d->SeekTableIndex += 1;
1746 d->SeekTableCounter = 0;
1748 d->SeekTableCounter += d->FwdJumpInfo + 20;
1751 // update buffer
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);
1764 return TRUE;
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]);
1773 d->Ring = 0;
1778 void mpc_decoder_update_buffer(mpc_decoder *d)
1780 if ((d->Ring ^ d->Zaehler) & MEMSIZE2) {
1781 // update buffer
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) {
1790 // required dword
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);
1808 } else {
1810 // DWORD aligned
1811 f_seek(d, fpos*4 + d->MPCHeaderPos);
1812 d->Zaehler = 0;
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) {
1824 bits += d->pos;
1825 d->pos = bits & 31;
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;