f403673818321f51d34e032b36f4120b5c52fe72
[kugel-rb.git] / apps / codecs / libmusepack / mpc_decoder.c
blobf403673818321f51d34e032b36f4120b5c52fe72
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 "mpcdec.h"
39 #include "minimax.h"
40 #include "decoder.h"
41 #include "huffman.h"
42 #include "internal.h"
43 #include "mpcdec_math.h"
44 #include "requant.h"
45 #include "mpc_bits_reader.h"
47 //SV7 tables
48 extern const mpc_lut_data mpc_HuffQ [7] [2];
49 extern const mpc_lut_data mpc_HuffHdr;
50 extern const mpc_huffman mpc_table_HuffSCFI [ 4];
51 extern const mpc_lut_data mpc_HuffDSCF;
53 //SV8 tables
54 extern const mpc_can_data mpc_can_Bands;
55 extern const mpc_can_data mpc_can_SCFI[2];
56 extern const mpc_can_data mpc_can_DSCF[2];
57 extern const mpc_can_data mpc_can_Res [2];
58 extern const mpc_can_data mpc_can_Q [8][2];
59 extern const mpc_can_data mpc_can_Q1;
60 extern const mpc_can_data mpc_can_Q9up;
62 //Decoder globals (g_Y_L and g_Y_R do not fit into iram for all targets)
63 static mpc_decoder g_mpc_decoder IBSS_ATTR;
64 static MPC_SAMPLE_FORMAT g_Y_L[MPC_FRAME_LENGTH] IBSS_ATTR_MPC_LARGE_IRAM;
65 static MPC_SAMPLE_FORMAT g_Y_R[MPC_FRAME_LENGTH] IBSS_ATTR_MPC_LARGE_IRAM;
67 //SV7 globals (decoding results for bundled quantizers (3- and 5-step))
68 static const mpc_int32_t g_sv7_idx30[] ICONST_ATTR =
69 {-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};
70 static const mpc_int32_t g_sv7_idx31[] ICONST_ATTR =
71 {-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};
72 static const mpc_int32_t g_sv7_idx32[] ICONST_ATTR =
73 {-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};
74 static const mpc_int32_t g_sv7_idx50[] ICONST_ATTR =
75 {-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};
76 static const mpc_int32_t g_sv7_idx51[] ICONST_ATTR =
77 {-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};
79 //SV8 globals (decoding results for bundled quantizers (3- and 5-step))
80 static const mpc_int8_t g_sv8_idx50[125] ICONST_ATTR =
81 {-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,
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 static const mpc_int8_t g_sv8_idx51[125] ICONST_ATTR =
87 {-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,
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 static const mpc_int8_t g_sv8_idx52[125] ICONST_ATTR =
93 {-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,
94 -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,
95 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,
96 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,
97 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};
98 static const mpc_int8_t g_sv8_HuffQ2_var[125] ICONST_ATTR =
99 { 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,
100 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,
101 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,
102 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,
103 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};
105 //------------------------------------------------------------------------------
106 // types
107 //------------------------------------------------------------------------------
108 enum
110 MEMSIZE = MPC_DECODER_MEMSIZE, // overall buffer size
111 MEMSIZE2 = (MEMSIZE/2), // size of one buffer
112 MEMMASK = (MEMSIZE-1)
115 //------------------------------------------------------------------------------
116 // forward declarations
117 //------------------------------------------------------------------------------
118 void mpc_decoder_requantisierung (mpc_decoder *d)
119 ICODE_ATTR_MPC_LARGE_IRAM;
120 void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r)
121 ICODE_ATTR_MPC_LARGE_IRAM;
122 void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r,
123 mpc_bool_t is_key_frame);
125 //------------------------------------------------------------------------------
126 // macros
127 //------------------------------------------------------------------------------
128 #define REQUANT_M1_S1_SAMPLES(IDX) \
129 *(YL+=IDX) = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++)); \
130 *(YR+=IDX) = templ - tempr;
132 #define REQUANT_M1_S1(SUBFRAME) \
133 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
134 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
135 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
136 REQUANT_M1_S1_SAMPLES( 0); \
137 REQUANT_M1_S1_SAMPLES(32); \
138 REQUANT_M1_S1_SAMPLES(32); \
139 REQUANT_M1_S1_SAMPLES(32); \
142 #define REQUANT_M1_S0_SAMPLES(IDX) \
143 *(YR+=IDX) = *(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
145 #define REQUANT_M1_S0(SUBFRAME) \
146 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
147 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
148 REQUANT_M1_S0_SAMPLES( 0); \
149 REQUANT_M1_S0_SAMPLES(32); \
150 REQUANT_M1_S0_SAMPLES(32); \
151 REQUANT_M1_S0_SAMPLES(32); \
154 #define REQUANT_M0_S1_SAMPLES(IDX) \
155 *(YR+=IDX) = -(*(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
157 #define REQUANT_M0_S1(SUBFRAME) \
158 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
159 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
160 REQUANT_M0_S1_SAMPLES( 0); \
161 REQUANT_M0_S1_SAMPLES(32); \
162 REQUANT_M0_S1_SAMPLES(32); \
163 REQUANT_M0_S1_SAMPLES(32); \
166 #define REQUANT_L1_R1_SAMPLES(IDX) \
167 *(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facL,*L++); \
168 *(YR+=IDX) = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
170 #define REQUANT_L1_R1(SUBFRAME) \
171 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
172 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
173 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
174 REQUANT_L1_R1_SAMPLES( 0); \
175 REQUANT_L1_R1_SAMPLES(32); \
176 REQUANT_L1_R1_SAMPLES(32); \
177 REQUANT_L1_R1_SAMPLES(32); \
180 #define REQUANT_L1_R0_SAMPLES(IDX) \
181 *(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facL,*L++); \
182 *(YR+=IDX) = 0;
184 #define REQUANT_L1_R0(SUBFRAME) \
185 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
186 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
187 REQUANT_L1_R0_SAMPLES( 0); \
188 REQUANT_L1_R0_SAMPLES(32); \
189 REQUANT_L1_R0_SAMPLES(32); \
190 REQUANT_L1_R0_SAMPLES(32); \
193 #define REQUANT_L0_R1_SAMPLES(IDX) \
194 *(YL+=IDX) = 0; \
195 *(YR+=IDX) = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
197 #define REQUANT_L0_R1(SUBFRAME) \
198 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
199 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
200 REQUANT_L0_R1_SAMPLES( 0); \
201 REQUANT_L0_R1_SAMPLES(32); \
202 REQUANT_L0_R1_SAMPLES(32); \
203 REQUANT_L0_R1_SAMPLES(32); \
206 #define REQUANT_SILENCE_SAMPLES(IDX) \
207 *(YR+=IDX) = *(YL+=IDX) = 0;
209 #define REQUANT_SILENCE \
210 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
211 REQUANT_SILENCE_SAMPLES( 0); \
212 REQUANT_SILENCE_SAMPLES(32); \
213 REQUANT_SILENCE_SAMPLES(32); \
214 REQUANT_SILENCE_SAMPLES(32); \
218 * set the scf indexes for seeking use
219 * needed only for sv7 seeking
220 * @param d
222 void mpc_decoder_reset_scf(mpc_decoder * d, int value)
224 memset(d->SCF_Index_L, value, sizeof d->SCF_Index_L );
225 memset(d->SCF_Index_R, value, sizeof d->SCF_Index_R );
228 static void mpc_decoder_setup(mpc_decoder *d)
230 memset(d, 0, sizeof *d);
232 d->__r1 = 1;
233 d->__r2 = 1;
234 d->Y_L = g_Y_L;
235 d->Y_R = g_Y_R;
237 memset(d->Y_L, 0, sizeof(g_Y_L));
238 memset(d->Y_R, 0, sizeof(g_Y_R));
240 mpc_decoder_init_quant(d, 1.0f);
243 static void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
245 d->stream_version = si->stream_version;
246 d->ms = si->ms;
247 d->max_band = si->max_band;
248 d->channels = si->channels;
249 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + si->beg_silence;
251 if (si->stream_version == 7 && si->is_true_gapless)
252 d->samples = ((si->samples + MPC_FRAME_LENGTH - 1) / MPC_FRAME_LENGTH) * MPC_FRAME_LENGTH;
253 else
254 d->samples = si->samples;
257 mpc_decoder * mpc_decoder_init(mpc_streaminfo *si)
259 mpc_decoder* p_tmp = &g_mpc_decoder;
261 if (p_tmp != 0) {
262 mpc_decoder_setup(p_tmp);
263 mpc_decoder_set_streaminfo(p_tmp, si);
264 huff_init_lut(LUT_DEPTH);
267 return p_tmp;
270 void mpc_decoder_exit(mpc_decoder *d)
272 (void)d;
275 void mpc_decoder_decode_frame(mpc_decoder * d,
276 mpc_bits_reader * r,
277 mpc_frame_info * i)
279 mpc_bits_reader r_sav = *r;
280 mpc_int64_t samples_left;
282 samples_left = d->samples - d->decoded_samples + MPC_DECODER_SYNTH_DELAY;
284 if (samples_left <= 0 && d->samples != 0) {
285 i->samples = 0;
286 i->bits = -1;
287 return;
290 if (d->stream_version == 8) {
291 mpc_decoder_read_bitstream_sv8(d, r, i->is_key_frame);
292 } else {
293 mpc_decoder_read_bitstream_sv7(d, r);
296 if (d->samples_to_skip < MPC_FRAME_LENGTH + MPC_DECODER_SYNTH_DELAY) {
297 mpc_decoder_requantisierung(d);
298 mpc_decoder_synthese_filter_float(d, i->buffer, d->channels);
301 d->decoded_samples += MPC_FRAME_LENGTH;
303 // reconstruct exact filelength
304 if (d->decoded_samples - d->samples < MPC_FRAME_LENGTH && d->stream_version == 7) {
305 int last_frame_samples = mpc_bits_read(r, 11);
306 if (d->decoded_samples == d->samples) {
307 if (last_frame_samples == 0) last_frame_samples = MPC_FRAME_LENGTH;
308 d->samples += last_frame_samples - MPC_FRAME_LENGTH;
309 samples_left += last_frame_samples - MPC_FRAME_LENGTH;
313 i->samples = samples_left > MPC_FRAME_LENGTH ? MPC_FRAME_LENGTH : samples_left < 0 ? 0 : (mpc_uint32_t) samples_left;
314 i->bits = (mpc_uint32_t) (((r->buff - r_sav.buff) << 3) + r_sav.count - r->count);
316 if (d->samples_to_skip) {
317 if (i->samples <= d->samples_to_skip) {
318 d->samples_to_skip -= i->samples;
319 i->samples = 0;
320 } else {
321 i->samples -= d->samples_to_skip;
323 /* move valid samples to beginning for channel 0. noninterleaved! */
324 memmove(i->buffer,
325 i->buffer + d->samples_to_skip,
326 i->samples * sizeof(MPC_SAMPLE_FORMAT));
327 /* move valid samples to beginning for channel 1. noninterleaved! */
328 memmove(i->buffer + MPC_FRAME_LENGTH,
329 i->buffer + MPC_FRAME_LENGTH + d->samples_to_skip,
330 i->samples * sizeof(MPC_SAMPLE_FORMAT));
332 d->samples_to_skip = 0;
337 void
338 mpc_decoder_requantisierung(mpc_decoder *d)
340 mpc_int32_t Band;
341 mpc_int32_t n;
342 MPC_SAMPLE_FORMAT facL;
343 MPC_SAMPLE_FORMAT facR;
344 MPC_SAMPLE_FORMAT templ;
345 MPC_SAMPLE_FORMAT tempr;
346 MPC_SAMPLE_FORMAT* YL;
347 MPC_SAMPLE_FORMAT* YR;
348 mpc_int16_t* L;
349 mpc_int16_t* R;
350 const mpc_int32_t Last_Band = d->max_band;
352 #ifdef MPC_FIXED_POINT
353 #if MPC_FIXED_POINT_FRACTPART == 14
354 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
355 MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
356 #else
358 #error FIXME, Cc table is in 18.14 format
360 #endif
361 #else
362 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
363 MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
364 #endif
365 // requantization and scaling of subband-samples
366 for ( Band = 0; Band <= Last_Band; Band++ ) { // setting pointers
367 YL = d->Y_L + Band;
368 YR = d->Y_R + Band;
369 L = d->Q[Band].L;
370 R = d->Q[Band].R;
371 /************************** MS-coded **************************/
372 if ( d->MS_Flag [Band] ) {
373 if ( d->Res_L [Band] ) {
374 if ( d->Res_R [Band] ) { // M!=0, S!=0
375 REQUANT_M1_S1(0);
376 REQUANT_M1_S1(1);
377 REQUANT_M1_S1(2);
378 } else { // M!=0, S==0
379 REQUANT_M1_S0(0);
380 REQUANT_M1_S0(1);
381 REQUANT_M1_S0(2);
383 } else {
384 if ( d->Res_R[Band] ) // M==0, S!=0
386 REQUANT_M0_S1(0);
387 REQUANT_M0_S1(1);
388 REQUANT_M0_S1(2);
389 } else { // M==0, S==0
390 REQUANT_SILENCE;
394 /************************** LR-coded **************************/
395 else {
396 if ( d->Res_L [Band] ) {
397 if ( d->Res_R [Band] ) { // L!=0, R!=0
398 REQUANT_L1_R1(0);
399 REQUANT_L1_R1(1);
400 REQUANT_L1_R1(2);
401 } else { // L!=0, R==0
402 REQUANT_L1_R0(0);
403 REQUANT_L1_R0(1);
404 REQUANT_L1_R0(2);
407 else {
408 if ( d->Res_R [Band] ) { // L==0, R!=0
409 REQUANT_L0_R1(0);
410 REQUANT_L0_R1(1);
411 REQUANT_L0_R1(2);
412 } else { // L==0, R==0
413 REQUANT_SILENCE;
420 void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r)
422 mpc_int32_t n, idx, Max_used_Band = 0;
424 /***************************** Header *****************************/
426 // first subband
427 d->Res_L[0] = mpc_bits_read(r, 4);
428 d->Res_R[0] = mpc_bits_read(r, 4);
429 if (!(d->Res_L[0] == 0 && d->Res_R[0] == 0)) {
430 if (d->ms)
431 d->MS_Flag[0] = mpc_bits_read(r, 1);
432 Max_used_Band = 1;
435 // consecutive subbands
436 for ( n = 1; n <= d->max_band; n++ ) {
437 idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
438 d->Res_L[n] = (idx!=4) ? d->Res_L[n - 1] + idx : (int) mpc_bits_read(r, 4);
440 idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
441 d->Res_R[n] = (idx!=4) ? d->Res_R[n - 1] + idx : (int) mpc_bits_read(r, 4);
443 if (!(d->Res_L[n] == 0 && d->Res_R[n] == 0)) {
444 if (d->ms)
445 d->MS_Flag[n] = mpc_bits_read(r, 1);
446 Max_used_Band = n + 1;
450 /****************************** SCFI ******************************/
451 for ( n = 0; n < Max_used_Band; n++ ) {
452 if (d->Res_L[n])
453 d->SCFI_L[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
454 if (d->Res_R[n])
455 d->SCFI_R[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
458 /**************************** SCF/DSCF ****************************/
459 for ( n = 0; n < Max_used_Band; n++ ) {
460 mpc_int32_t * SCF = d->SCF_Index_L[n];
461 mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
462 do {
463 if (Res) {
464 switch (SCFI) {
465 case 1:
466 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
467 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
468 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
469 SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
470 SCF[2] = SCF[1];
471 break;
472 case 3:
473 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
474 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
475 SCF[1] = SCF[0];
476 SCF[2] = SCF[1];
477 break;
478 case 2:
479 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
480 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
481 SCF[1] = SCF[0];
482 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
483 SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
484 break;
485 case 0:
486 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
487 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
488 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
489 SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
490 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
491 SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
492 break;
493 default:
494 return;
496 if (SCF[0] > 1024)
497 SCF[0] = 0x8080;
498 if (SCF[1] > 1024)
499 SCF[1] = 0x8080;
500 if (SCF[2] > 1024)
501 SCF[2] = 0x8080;
503 Res = d->Res_R[n];
504 SCFI = d->SCFI_R[n];
505 } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
508 // if (d->seeking == TRUE)
509 // return;
511 /***************************** Samples ****************************/
512 for ( n = 0; n < Max_used_Band; n++ ) {
513 mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
514 do {
515 mpc_int32_t k;
516 const mpc_lut_data *Table;
517 switch (Res) {
518 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
519 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17: case 0:
520 break;
521 case -1:
522 for (k=0; k<36; k++ ) {
523 mpc_uint32_t tmp = mpc_random_int(d);
524 q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
526 break;
527 case 1:
528 Table = & mpc_HuffQ[0][mpc_bits_read(r, 1)];
529 for ( k = 0; k < 36; k += 3) {
530 idx = mpc_bits_huff_lut(r, Table);
531 q[k] = g_sv7_idx30[idx];
532 q[k + 1] = g_sv7_idx31[idx];
533 q[k + 2] = g_sv7_idx32[idx];
535 break;
536 case 2:
537 Table = & mpc_HuffQ[1][mpc_bits_read(r, 1)];
538 for ( k = 0; k < 36; k += 2) {
539 idx = mpc_bits_huff_lut(r, Table);
540 q[k] = g_sv7_idx50[idx];
541 q[k + 1] = g_sv7_idx51[idx];
543 break;
544 case 3:
545 case 4:
546 case 5:
547 case 6:
548 case 7:
549 Table = & mpc_HuffQ[Res - 1][mpc_bits_read(r, 1)];
550 for ( k = 0; k < 36; k++ )
551 q[k] = mpc_bits_huff_lut(r, Table);
552 break;
553 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
554 for ( k = 0; k < 36; k++ )
555 q[k] = (mpc_int32_t)mpc_bits_read(r, Res_bit[Res]) - Dc[Res];
556 break;
557 default:
558 return;
561 Res = d->Res_R[n];
562 } while (q == d->Q[n].L && (q = d->Q[n].R));
566 void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r, mpc_bool_t is_key_frame)
568 mpc_int32_t n, Max_used_Band;
569 const mpc_can_data * Table, * Tables[2];
571 /***************************** Header *****************************/
573 if (is_key_frame == MPC_TRUE) {
574 Max_used_Band = mpc_bits_log_dec(r, d->max_band + 1);
575 } else {
576 Max_used_Band = d->last_max_band + mpc_bits_can_dec(r, & mpc_can_Bands);
577 if (Max_used_Band > 32) Max_used_Band -= 33;
579 d->last_max_band = Max_used_Band;
581 if (Max_used_Band) {
582 d->Res_L[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
583 d->Res_R[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
584 if (d->Res_L[Max_used_Band-1] > 15) d->Res_L[Max_used_Band-1] -= 17;
585 if (d->Res_R[Max_used_Band-1] > 15) d->Res_R[Max_used_Band-1] -= 17;
586 for ( n = Max_used_Band - 2; n >= 0; n--) {
587 d->Res_L[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_L[n + 1] > 2]) + d->Res_L[n + 1];
588 if (d->Res_L[n] > 15) d->Res_L[n] -= 17;
589 d->Res_R[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_R[n + 1] > 2]) + d->Res_R[n + 1];
590 if (d->Res_R[n] > 15) d->Res_R[n] -= 17;
593 if (d->ms) {
594 int cnt = 0, tot = 0;
595 mpc_uint32_t tmp = 0;
596 for( n = 0; n < Max_used_Band; n++)
597 if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 )
598 tot++;
599 cnt = mpc_bits_log_dec(r, tot);
600 if (cnt != 0 && cnt != tot)
601 tmp = mpc_bits_enum_dec(r, mini(cnt, tot-cnt), tot);
602 if (cnt * 2 > tot) tmp = ~tmp;
603 for( n = Max_used_Band - 1; n >= 0; n--)
604 if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 ) {
605 d->MS_Flag[n] = tmp & 1;
606 tmp >>= 1;
611 for( n = Max_used_Band; n <= d->max_band; n++)
612 d->Res_L[n] = d->Res_R[n] = 0;
614 /****************************** SCFI ******************************/
615 if (is_key_frame == MPC_TRUE){
616 for( n = 0; n < 32; n++)
617 d->DSCF_Flag_L[n] = d->DSCF_Flag_R[n] = 1; // new block -> force key frame
620 Tables[0] = & mpc_can_SCFI[0];
621 Tables[1] = & mpc_can_SCFI[1];
622 for ( n = 0; n < Max_used_Band; n++ ) {
623 int tmp = 0, cnt = -1;
624 if (d->Res_L[n]) cnt++;
625 if (d->Res_R[n]) cnt++;
626 if (cnt >= 0) {
627 tmp = mpc_bits_can_dec(r, Tables[cnt]);
628 if (d->Res_L[n]) d->SCFI_L[n] = tmp >> (2 * cnt);
629 if (d->Res_R[n]) d->SCFI_R[n] = tmp & 3;
633 /**************************** SCF/DSCF ****************************/
635 for ( n = 0; n < Max_used_Band; n++ ) {
636 mpc_int32_t * SCF = d->SCF_Index_L[n];
637 mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
638 mpc_bool_t * DSCF_Flag = &d->DSCF_Flag_L[n];
640 do {
641 if ( Res ) {
642 int m;
643 if (*DSCF_Flag == 1) {
644 SCF[0] = (mpc_int32_t)mpc_bits_read(r, 7) - 6;
645 *DSCF_Flag = 0;
646 } else {
647 mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[1]);
648 if (tmp == 64)
649 tmp += mpc_bits_read(r, 6);
650 SCF[0] = ((SCF[2] - 25 + tmp) & 127) - 6;
652 for( m = 0; m < 2; m++){
653 if (((SCFI << m) & 2) == 0) {
654 mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[0]);
655 if (tmp == 31)
656 tmp = 64 + mpc_bits_read(r, 6);
657 SCF[m + 1] = ((SCF[m] - 25 + tmp) & 127) - 6;
658 } else
659 SCF[m + 1] = SCF[m];
662 Res = d->Res_R[n];
663 SCFI = d->SCFI_R[n];
664 DSCF_Flag = &d->DSCF_Flag_R[n];
665 } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
668 /***************************** Samples ****************************/
669 for ( n = 0; n < Max_used_Band; n++ ) {
670 mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
671 static const int thres[] = {0, 0, 3, 0, 0, 1, 3, 4, 8};
672 do {
673 mpc_int32_t k = 0, idx = 1;
674 if (Res != 0) {
675 if (Res == 2) {
676 Tables[0] = & mpc_can_Q [0][0];
677 Tables[1] = & mpc_can_Q [0][1];
678 idx = 2 * thres[Res];
679 for ( ; k < 36; k += 3) {
680 int tmp = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
681 q[k] = g_sv8_idx50[tmp];
682 q[k + 1] = g_sv8_idx51[tmp];
683 q[k + 2] = g_sv8_idx52[tmp];
684 idx = (idx >> 1) + g_sv8_HuffQ2_var[tmp];
686 } else if (Res == 1) {
687 Table = & mpc_can_Q1;
688 for( ; k < 36; ){
689 int kmax = k + 18;
690 mpc_uint_t cnt = mpc_bits_can_dec(r, Table);
691 idx = 0;
692 if (cnt > 0 && cnt < 18)
693 idx = mpc_bits_enum_dec(r, cnt <= 9 ? cnt : 18 - cnt, 18);
694 if (cnt > 9) idx = ~idx;
695 for ( ; k < kmax; k++) {
696 q[k] = 0;
697 if ( idx & (1 << 17) )
698 q[k] = (mpc_bits_read(r, 1) << 1) - 1;
699 idx <<= 1;
702 } else if (Res == -1) {
703 for ( ; k<36; k++ ) {
704 mpc_uint32_t tmp = mpc_random_int(d);
705 q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
707 } else if (Res <= 4) {
708 Table = & mpc_can_Q[1][Res - 3];
709 for ( ; k < 36; k += 2 ) {
710 union {
711 mpc_int8_t sym;
712 struct { mpc_int8_t s1:4, s2:4; };
713 } tmp;
714 tmp.sym = mpc_bits_can_dec(r, Table);
715 q[k] = tmp.s1;
716 q[k + 1] = tmp.s2;
718 } else if (Res <= 8) {
719 Tables[0] = & mpc_can_Q [Res - 3][0];
720 Tables[1] = & mpc_can_Q [Res - 3][1];
721 idx = 2 * thres[Res];
722 for ( ; k < 36; k++ ) {
723 q[k] = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
724 idx = (idx >> 1) + absi(q[k]);
726 } else {
727 for ( ; k < 36; k++ ) {
728 q[k] = (unsigned char) mpc_bits_can_dec(r, & mpc_can_Q9up);
729 if (Res != 9)
730 q[k] = (q[k] << (Res - 9)) | mpc_bits_read(r, Res - 9);
731 q[k] -= Dc[Res];
736 Res = d->Res_R[n];
737 } while (q == d->Q[n].L && (q = d->Q[n].R));