Housekeeping. Comment musepack code seqments unused in rockbox.
[kugel-rb.git] / apps / codecs / libmusepack / mpc_decoder.c
blob7ff677aa27f7cf71e1e8c51cce3489364dac5653
1 /*
2 Copyright (c) 2005-2009, 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.
34 /// \file mpc_decoder.c
35 /// Core decoding routines and logic.
37 #include <string.h>
38 #include <codecs.h>
39 #include "mpcdec.h"
40 #include "minimax.h"
41 #include "decoder.h"
42 #include "huffman.h"
43 #include "internal.h"
44 #include "mpcdec_math.h"
45 #include "requant.h"
46 #include "mpc_bits_reader.h"
48 //SV7 tables
49 extern const mpc_lut_data mpc_HuffQ [7] [2];
50 extern const mpc_lut_data mpc_HuffHdr;
51 extern const mpc_huffman mpc_table_HuffSCFI [ 4];
52 extern const mpc_lut_data mpc_HuffDSCF;
54 //SV8 tables
55 extern const mpc_can_data mpc_can_Bands;
56 extern const mpc_can_data mpc_can_SCFI[2];
57 extern const mpc_can_data mpc_can_DSCF[2];
58 extern const mpc_can_data mpc_can_Res [2];
59 extern const mpc_can_data mpc_can_Q [8][2];
60 extern const mpc_can_data mpc_can_Q1;
61 extern const mpc_can_data mpc_can_Q9up;
63 //Decoder globals (g_Y_L and g_Y_R do not fit into iram for all targets)
64 static mpc_decoder g_mpc_decoder IBSS_ATTR;
65 static MPC_SAMPLE_FORMAT g_Y_L[MPC_FRAME_LENGTH] IBSS_ATTR_MPC_LARGE_IRAM;
66 static MPC_SAMPLE_FORMAT g_Y_R[MPC_FRAME_LENGTH] IBSS_ATTR_MPC_LARGE_IRAM;
68 //SV7 globals (decoding results for bundled quantizers (3- and 5-step))
69 static const mpc_int32_t g_sv7_idx30[] ICONST_ATTR =
70 {-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1};
71 static const mpc_int32_t g_sv7_idx31[] ICONST_ATTR =
72 {-1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1};
73 static const mpc_int32_t g_sv7_idx32[] ICONST_ATTR =
74 {-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
75 static const mpc_int32_t g_sv7_idx50[] ICONST_ATTR =
76 {-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2};
77 static const mpc_int32_t g_sv7_idx51[] ICONST_ATTR =
78 {-2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
80 //SV8 globals (decoding results for bundled quantizers (3- and 5-step))
81 static const mpc_int8_t g_sv8_idx50[125] ICONST_ATTR =
82 {-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,
83 -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,
84 -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,
85 -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,
86 -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2};
87 static const mpc_int8_t g_sv8_idx51[125] ICONST_ATTR =
88 {-2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
89 -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
90 -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
91 -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
92 -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
93 static const mpc_int8_t g_sv8_idx52[125] ICONST_ATTR =
94 {-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
95 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
98 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
99 static const mpc_int8_t g_sv8_HuffQ2_var[125] ICONST_ATTR =
100 { 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6,
101 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5,
102 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4,
103 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5,
104 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6};
106 //------------------------------------------------------------------------------
107 // types
108 //------------------------------------------------------------------------------
109 enum
111 MEMSIZE = MPC_DECODER_MEMSIZE, // overall buffer size
112 MEMSIZE2 = (MEMSIZE/2), // size of one buffer
113 MEMMASK = (MEMSIZE-1)
116 //------------------------------------------------------------------------------
117 // forward declarations
118 //------------------------------------------------------------------------------
119 void mpc_decoder_requantisierung (mpc_decoder *d)
120 ICODE_ATTR_MPC_LARGE_IRAM;
121 void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r)
122 ICODE_ATTR_MPC_LARGE_IRAM;
123 void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r,
124 mpc_bool_t is_key_frame);
126 //------------------------------------------------------------------------------
127 // macros
128 //------------------------------------------------------------------------------
129 #define REQUANT_M1_S1_SAMPLES(IDX) \
130 *(YL+=IDX) = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++)); \
131 *(YR+=IDX) = templ - tempr;
133 #define REQUANT_M1_S1(SUBFRAME) \
134 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
135 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
136 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
137 REQUANT_M1_S1_SAMPLES( 0); \
138 REQUANT_M1_S1_SAMPLES(32); \
139 REQUANT_M1_S1_SAMPLES(32); \
140 REQUANT_M1_S1_SAMPLES(32); \
143 #define REQUANT_M1_S0_SAMPLES(IDX) \
144 *(YR+=IDX) = *(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
146 #define REQUANT_M1_S0(SUBFRAME) \
147 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
148 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
149 REQUANT_M1_S0_SAMPLES( 0); \
150 REQUANT_M1_S0_SAMPLES(32); \
151 REQUANT_M1_S0_SAMPLES(32); \
152 REQUANT_M1_S0_SAMPLES(32); \
155 #define REQUANT_M0_S1_SAMPLES(IDX) \
156 *(YR+=IDX) = -(*(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
158 #define REQUANT_M0_S1(SUBFRAME) \
159 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
160 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
161 REQUANT_M0_S1_SAMPLES( 0); \
162 REQUANT_M0_S1_SAMPLES(32); \
163 REQUANT_M0_S1_SAMPLES(32); \
164 REQUANT_M0_S1_SAMPLES(32); \
167 #define REQUANT_L1_R1_SAMPLES(IDX) \
168 *(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facL,*L++); \
169 *(YR+=IDX) = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
171 #define REQUANT_L1_R1(SUBFRAME) \
172 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
173 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
174 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
175 REQUANT_L1_R1_SAMPLES( 0); \
176 REQUANT_L1_R1_SAMPLES(32); \
177 REQUANT_L1_R1_SAMPLES(32); \
178 REQUANT_L1_R1_SAMPLES(32); \
181 #define REQUANT_L1_R0_SAMPLES(IDX) \
182 *(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facL,*L++); \
183 *(YR+=IDX) = 0;
185 #define REQUANT_L1_R0(SUBFRAME) \
186 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
187 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
188 REQUANT_L1_R0_SAMPLES( 0); \
189 REQUANT_L1_R0_SAMPLES(32); \
190 REQUANT_L1_R0_SAMPLES(32); \
191 REQUANT_L1_R0_SAMPLES(32); \
194 #define REQUANT_L0_R1_SAMPLES(IDX) \
195 *(YL+=IDX) = 0; \
196 *(YR+=IDX) = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
198 #define REQUANT_L0_R1(SUBFRAME) \
199 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
200 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
201 REQUANT_L0_R1_SAMPLES( 0); \
202 REQUANT_L0_R1_SAMPLES(32); \
203 REQUANT_L0_R1_SAMPLES(32); \
204 REQUANT_L0_R1_SAMPLES(32); \
207 #define REQUANT_SILENCE_SAMPLES(IDX) \
208 *(YR+=IDX) = *(YL+=IDX) = 0;
210 #define REQUANT_SILENCE \
211 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
212 REQUANT_SILENCE_SAMPLES( 0); \
213 REQUANT_SILENCE_SAMPLES(32); \
214 REQUANT_SILENCE_SAMPLES(32); \
215 REQUANT_SILENCE_SAMPLES(32); \
219 * set the scf indexes for seeking use
220 * needed only for sv7 seeking
221 * @param d
223 void mpc_decoder_reset_scf(mpc_decoder * d, int value)
225 memset(d->SCF_Index_L, value, sizeof d->SCF_Index_L );
226 memset(d->SCF_Index_R, value, sizeof d->SCF_Index_R );
229 static void mpc_decoder_setup(mpc_decoder *d)
231 #if defined(CPU_COLDFIRE)
232 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
233 #endif
235 memset(d, 0, sizeof *d);
237 d->__r1 = 1;
238 d->__r2 = 1;
239 d->Y_L = g_Y_L;
240 d->Y_R = g_Y_R;
242 memset(d->Y_L, 0, sizeof(g_Y_L));
243 memset(d->Y_R, 0, sizeof(g_Y_R));
245 mpc_decoder_init_quant(d, 1.0f);
248 static void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
250 d->stream_version = si->stream_version;
251 d->ms = si->ms;
252 d->max_band = si->max_band;
253 d->channels = si->channels;
254 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + si->beg_silence;
256 if (si->stream_version == 7 && si->is_true_gapless)
257 d->samples = ((si->samples + MPC_FRAME_LENGTH - 1) / MPC_FRAME_LENGTH) * MPC_FRAME_LENGTH;
258 else
259 d->samples = si->samples;
262 mpc_decoder * mpc_decoder_init(mpc_streaminfo *si)
264 mpc_decoder* p_tmp = &g_mpc_decoder;
266 if (p_tmp != 0) {
267 mpc_decoder_setup(p_tmp);
268 mpc_decoder_set_streaminfo(p_tmp, si);
269 huff_init_lut(LUT_DEPTH);
272 return p_tmp;
275 /* rockbox: not used
276 void mpc_decoder_exit(mpc_decoder *d)
278 (void)d;
282 void mpc_decoder_decode_frame(mpc_decoder * d,
283 mpc_bits_reader * r,
284 mpc_frame_info * i)
286 mpc_bits_reader r_sav = *r;
287 mpc_int64_t samples_left;
289 samples_left = d->samples - d->decoded_samples + MPC_DECODER_SYNTH_DELAY;
291 if (samples_left <= 0 && d->samples != 0) {
292 i->samples = 0;
293 i->bits = -1;
294 return;
297 if (d->stream_version == 8) {
298 mpc_decoder_read_bitstream_sv8(d, r, i->is_key_frame);
299 } else {
300 mpc_decoder_read_bitstream_sv7(d, r);
303 if (d->samples_to_skip < MPC_FRAME_LENGTH + MPC_DECODER_SYNTH_DELAY) {
304 mpc_decoder_requantisierung(d);
305 mpc_decoder_synthese_filter_float(d, i->buffer, d->channels);
308 d->decoded_samples += MPC_FRAME_LENGTH;
310 // reconstruct exact filelength
311 if (d->decoded_samples - d->samples < MPC_FRAME_LENGTH && d->stream_version == 7) {
312 int last_frame_samples = mpc_bits_read(r, 11);
313 if (d->decoded_samples == d->samples) {
314 if (last_frame_samples == 0) last_frame_samples = MPC_FRAME_LENGTH;
315 d->samples += last_frame_samples - MPC_FRAME_LENGTH;
316 samples_left += last_frame_samples - MPC_FRAME_LENGTH;
320 i->samples = samples_left > MPC_FRAME_LENGTH ? MPC_FRAME_LENGTH : samples_left < 0 ? 0 : (mpc_uint32_t) samples_left;
321 i->bits = (mpc_uint32_t) (((r->buff - r_sav.buff) << 3) + r_sav.count - r->count);
323 if (d->samples_to_skip) {
324 if (i->samples <= d->samples_to_skip) {
325 d->samples_to_skip -= i->samples;
326 i->samples = 0;
327 } else {
328 i->samples -= d->samples_to_skip;
330 /* move valid samples to beginning for channel 0. noninterleaved! */
331 memmove(i->buffer,
332 i->buffer + d->samples_to_skip,
333 i->samples * sizeof(MPC_SAMPLE_FORMAT));
334 /* move valid samples to beginning for channel 1. noninterleaved! */
335 memmove(i->buffer + MPC_FRAME_LENGTH,
336 i->buffer + MPC_FRAME_LENGTH + d->samples_to_skip,
337 i->samples * sizeof(MPC_SAMPLE_FORMAT));
339 d->samples_to_skip = 0;
344 void
345 mpc_decoder_requantisierung(mpc_decoder *d)
347 mpc_int32_t Band;
348 mpc_int32_t n;
349 MPC_SAMPLE_FORMAT facL;
350 MPC_SAMPLE_FORMAT facR;
351 MPC_SAMPLE_FORMAT templ;
352 MPC_SAMPLE_FORMAT tempr;
353 MPC_SAMPLE_FORMAT* YL;
354 MPC_SAMPLE_FORMAT* YR;
355 mpc_int16_t* L;
356 mpc_int16_t* R;
357 const mpc_int32_t Last_Band = d->max_band;
359 #ifdef MPC_FIXED_POINT
360 #if MPC_FIXED_POINT_FRACTPART == 14
361 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
362 MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
363 #else
365 #error FIXME, Cc table is in 18.14 format
367 #endif
368 #else
369 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
370 MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
371 #endif
372 // requantization and scaling of subband-samples
373 for ( Band = 0; Band <= Last_Band; Band++ ) { // setting pointers
374 YL = d->Y_L + Band;
375 YR = d->Y_R + Band;
376 L = d->Q[Band].L;
377 R = d->Q[Band].R;
378 /************************** MS-coded **************************/
379 if ( d->MS_Flag [Band] ) {
380 if ( d->Res_L [Band] ) {
381 if ( d->Res_R [Band] ) { // M!=0, S!=0
382 REQUANT_M1_S1(0);
383 REQUANT_M1_S1(1);
384 REQUANT_M1_S1(2);
385 } else { // M!=0, S==0
386 REQUANT_M1_S0(0);
387 REQUANT_M1_S0(1);
388 REQUANT_M1_S0(2);
390 } else {
391 if ( d->Res_R[Band] ) // M==0, S!=0
393 REQUANT_M0_S1(0);
394 REQUANT_M0_S1(1);
395 REQUANT_M0_S1(2);
396 } else { // M==0, S==0
397 REQUANT_SILENCE;
401 /************************** LR-coded **************************/
402 else {
403 if ( d->Res_L [Band] ) {
404 if ( d->Res_R [Band] ) { // L!=0, R!=0
405 REQUANT_L1_R1(0);
406 REQUANT_L1_R1(1);
407 REQUANT_L1_R1(2);
408 } else { // L!=0, R==0
409 REQUANT_L1_R0(0);
410 REQUANT_L1_R0(1);
411 REQUANT_L1_R0(2);
414 else {
415 if ( d->Res_R [Band] ) { // L==0, R!=0
416 REQUANT_L0_R1(0);
417 REQUANT_L0_R1(1);
418 REQUANT_L0_R1(2);
419 } else { // L==0, R==0
420 REQUANT_SILENCE;
427 void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r)
429 mpc_int32_t n, idx, Max_used_Band = 0;
431 /***************************** Header *****************************/
433 // first subband
434 d->Res_L[0] = mpc_bits_read(r, 4);
435 d->Res_R[0] = mpc_bits_read(r, 4);
436 if (!(d->Res_L[0] == 0 && d->Res_R[0] == 0)) {
437 if (d->ms)
438 d->MS_Flag[0] = mpc_bits_read(r, 1);
439 Max_used_Band = 1;
442 // consecutive subbands
443 for ( n = 1; n <= d->max_band; n++ ) {
444 idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
445 d->Res_L[n] = (idx!=4) ? d->Res_L[n - 1] + idx : (int) mpc_bits_read(r, 4);
447 idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
448 d->Res_R[n] = (idx!=4) ? d->Res_R[n - 1] + idx : (int) mpc_bits_read(r, 4);
450 if (!(d->Res_L[n] == 0 && d->Res_R[n] == 0)) {
451 if (d->ms)
452 d->MS_Flag[n] = mpc_bits_read(r, 1);
453 Max_used_Band = n + 1;
457 /****************************** SCFI ******************************/
458 for ( n = 0; n < Max_used_Band; n++ ) {
459 if (d->Res_L[n])
460 d->SCFI_L[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
461 if (d->Res_R[n])
462 d->SCFI_R[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
465 /**************************** SCF/DSCF ****************************/
466 for ( n = 0; n < Max_used_Band; n++ ) {
467 mpc_int32_t * SCF = d->SCF_Index_L[n];
468 mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
469 do {
470 if (Res) {
471 switch (SCFI) {
472 case 1:
473 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
474 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
475 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
476 SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
477 SCF[2] = SCF[1];
478 break;
479 case 3:
480 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
481 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
482 SCF[1] = SCF[0];
483 SCF[2] = SCF[1];
484 break;
485 case 2:
486 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
487 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
488 SCF[1] = SCF[0];
489 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
490 SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
491 break;
492 case 0:
493 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
494 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
495 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
496 SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
497 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
498 SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
499 break;
500 default:
501 return;
503 if (SCF[0] > 1024)
504 SCF[0] = 0x8080;
505 if (SCF[1] > 1024)
506 SCF[1] = 0x8080;
507 if (SCF[2] > 1024)
508 SCF[2] = 0x8080;
510 Res = d->Res_R[n];
511 SCFI = d->SCFI_R[n];
512 } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
515 // if (d->seeking == TRUE)
516 // return;
518 /***************************** Samples ****************************/
519 for ( n = 0; n < Max_used_Band; n++ ) {
520 mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
521 do {
522 mpc_int32_t k;
523 const mpc_lut_data *Table;
524 switch (Res) {
525 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
526 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17: case 0:
527 break;
528 case -1:
529 for (k=0; k<36; k++ ) {
530 mpc_uint32_t tmp = mpc_random_int(d);
531 q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
533 break;
534 case 1:
535 Table = & mpc_HuffQ[0][mpc_bits_read(r, 1)];
536 for ( k = 0; k < 36; k += 3) {
537 idx = mpc_bits_huff_lut(r, Table);
538 q[k] = g_sv7_idx30[idx];
539 q[k + 1] = g_sv7_idx31[idx];
540 q[k + 2] = g_sv7_idx32[idx];
542 break;
543 case 2:
544 Table = & mpc_HuffQ[1][mpc_bits_read(r, 1)];
545 for ( k = 0; k < 36; k += 2) {
546 idx = mpc_bits_huff_lut(r, Table);
547 q[k] = g_sv7_idx50[idx];
548 q[k + 1] = g_sv7_idx51[idx];
550 break;
551 case 3:
552 case 4:
553 case 5:
554 case 6:
555 case 7:
556 Table = & mpc_HuffQ[Res - 1][mpc_bits_read(r, 1)];
557 for ( k = 0; k < 36; k++ )
558 q[k] = mpc_bits_huff_lut(r, Table);
559 break;
560 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
561 for ( k = 0; k < 36; k++ )
562 q[k] = (mpc_int32_t)mpc_bits_read(r, Res_bit[Res]) - Dc[Res];
563 break;
564 default:
565 return;
568 Res = d->Res_R[n];
569 } while (q == d->Q[n].L && (q = d->Q[n].R));
573 void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r, mpc_bool_t is_key_frame)
575 mpc_int32_t n, Max_used_Band;
576 const mpc_can_data * Table, * Tables[2];
578 /***************************** Header *****************************/
580 if (is_key_frame == MPC_TRUE) {
581 Max_used_Band = mpc_bits_log_dec(r, d->max_band + 1);
582 } else {
583 Max_used_Band = d->last_max_band + mpc_bits_can_dec(r, & mpc_can_Bands);
584 if (Max_used_Band > 32) Max_used_Band -= 33;
586 d->last_max_band = Max_used_Band;
588 if (Max_used_Band) {
589 d->Res_L[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
590 d->Res_R[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
591 if (d->Res_L[Max_used_Band-1] > 15) d->Res_L[Max_used_Band-1] -= 17;
592 if (d->Res_R[Max_used_Band-1] > 15) d->Res_R[Max_used_Band-1] -= 17;
593 for ( n = Max_used_Band - 2; n >= 0; n--) {
594 d->Res_L[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_L[n + 1] > 2]) + d->Res_L[n + 1];
595 if (d->Res_L[n] > 15) d->Res_L[n] -= 17;
596 d->Res_R[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_R[n + 1] > 2]) + d->Res_R[n + 1];
597 if (d->Res_R[n] > 15) d->Res_R[n] -= 17;
600 if (d->ms) {
601 int cnt = 0, tot = 0;
602 mpc_uint32_t tmp = 0;
603 for( n = 0; n < Max_used_Band; n++)
604 if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 )
605 tot++;
606 cnt = mpc_bits_log_dec(r, tot);
607 if (cnt != 0 && cnt != tot)
608 tmp = mpc_bits_enum_dec(r, mini(cnt, tot-cnt), tot);
609 if (cnt * 2 > tot) tmp = ~tmp;
610 for( n = Max_used_Band - 1; n >= 0; n--)
611 if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 ) {
612 d->MS_Flag[n] = tmp & 1;
613 tmp >>= 1;
618 for( n = Max_used_Band; n <= d->max_band; n++)
619 d->Res_L[n] = d->Res_R[n] = 0;
621 /****************************** SCFI ******************************/
622 if (is_key_frame == MPC_TRUE){
623 for( n = 0; n < 32; n++)
624 d->DSCF_Flag_L[n] = d->DSCF_Flag_R[n] = 1; // new block -> force key frame
627 Tables[0] = & mpc_can_SCFI[0];
628 Tables[1] = & mpc_can_SCFI[1];
629 for ( n = 0; n < Max_used_Band; n++ ) {
630 int tmp = 0, cnt = -1;
631 if (d->Res_L[n]) cnt++;
632 if (d->Res_R[n]) cnt++;
633 if (cnt >= 0) {
634 tmp = mpc_bits_can_dec(r, Tables[cnt]);
635 if (d->Res_L[n]) d->SCFI_L[n] = tmp >> (2 * cnt);
636 if (d->Res_R[n]) d->SCFI_R[n] = tmp & 3;
640 /**************************** SCF/DSCF ****************************/
642 for ( n = 0; n < Max_used_Band; n++ ) {
643 mpc_int32_t * SCF = d->SCF_Index_L[n];
644 mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
645 mpc_bool_t * DSCF_Flag = &d->DSCF_Flag_L[n];
647 do {
648 if ( Res ) {
649 int m;
650 if (*DSCF_Flag == 1) {
651 SCF[0] = (mpc_int32_t)mpc_bits_read(r, 7) - 6;
652 *DSCF_Flag = 0;
653 } else {
654 mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[1]);
655 if (tmp == 64)
656 tmp += mpc_bits_read(r, 6);
657 SCF[0] = ((SCF[2] - 25 + tmp) & 127) - 6;
659 for( m = 0; m < 2; m++){
660 if (((SCFI << m) & 2) == 0) {
661 mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[0]);
662 if (tmp == 31)
663 tmp = 64 + mpc_bits_read(r, 6);
664 SCF[m + 1] = ((SCF[m] - 25 + tmp) & 127) - 6;
665 } else
666 SCF[m + 1] = SCF[m];
669 Res = d->Res_R[n];
670 SCFI = d->SCFI_R[n];
671 DSCF_Flag = &d->DSCF_Flag_R[n];
672 } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
675 /***************************** Samples ****************************/
676 for ( n = 0; n < Max_used_Band; n++ ) {
677 mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
678 static const int thres[] = {0, 0, 3, 0, 0, 1, 3, 4, 8};
679 do {
680 mpc_int32_t k = 0, idx = 1;
681 if (Res != 0) {
682 if (Res == 2) {
683 Tables[0] = & mpc_can_Q [0][0];
684 Tables[1] = & mpc_can_Q [0][1];
685 idx = 2 * thres[Res];
686 for ( ; k < 36; k += 3) {
687 int tmp = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
688 q[k] = g_sv8_idx50[tmp];
689 q[k + 1] = g_sv8_idx51[tmp];
690 q[k + 2] = g_sv8_idx52[tmp];
691 idx = (idx >> 1) + g_sv8_HuffQ2_var[tmp];
693 } else if (Res == 1) {
694 Table = & mpc_can_Q1;
695 for( ; k < 36; ){
696 int kmax = k + 18;
697 mpc_uint_t cnt = mpc_bits_can_dec(r, Table);
698 idx = 0;
699 if (cnt > 0 && cnt < 18)
700 idx = mpc_bits_enum_dec(r, cnt <= 9 ? cnt : 18 - cnt, 18);
701 if (cnt > 9) idx = ~idx;
702 for ( ; k < kmax; k++) {
703 q[k] = 0;
704 if ( idx & (1 << 17) )
705 q[k] = (mpc_bits_read(r, 1) << 1) - 1;
706 idx <<= 1;
709 } else if (Res == -1) {
710 for ( ; k<36; k++ ) {
711 mpc_uint32_t tmp = mpc_random_int(d);
712 q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
714 } else if (Res <= 4) {
715 Table = & mpc_can_Q[1][Res - 3];
716 for ( ; k < 36; k += 2 ) {
717 union {
718 mpc_int8_t sym;
719 struct { mpc_int8_t s1:4, s2:4; };
720 } tmp;
721 tmp.sym = mpc_bits_can_dec(r, Table);
722 q[k] = tmp.s1;
723 q[k + 1] = tmp.s2;
725 } else if (Res <= 8) {
726 Tables[0] = & mpc_can_Q [Res - 3][0];
727 Tables[1] = & mpc_can_Q [Res - 3][1];
728 idx = 2 * thres[Res];
729 for ( ; k < 36; k++ ) {
730 q[k] = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
731 idx = (idx >> 1) + absi(q[k]);
733 } else {
734 for ( ; k < 36; k++ ) {
735 q[k] = (unsigned char) mpc_bits_can_dec(r, & mpc_can_Q9up);
736 if (Res != 9)
737 q[k] = (q[k] << (Res - 9)) | mpc_bits_read(r, Res - 9);
738 q[k] -= Dc[Res];
743 Res = d->Res_R[n];
744 } while (q == d->Q[n].L && (q = d->Q[n].R));