Refactor mpc's requantization to use identical macros for each subframe.
[kugel-rb.git] / apps / codecs / libmusepack / mpc_decoder.c
blob9e4ef634bf6a2e64bf129c4f0a6703eaeac8642a
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(SUBFRAME) \
129 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
130 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
131 for (n = 0; n < 12; n++, YL += 32, YR += 32) { \
132 *YL = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++)); \
133 *YR = templ - tempr; \
136 #define REQUANT_M1_S0(SUBFRAME) \
137 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
138 for (n = 0; n < 12; n++, YL += 32, YR += 32) { \
139 *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++); \
142 #define REQUANT_M0_S1(SUBFRAME) \
143 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
144 for (n = 0; n < 12; n++, YL += 32, YR += 32) { \
145 *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*R++)); \
148 #define REQUANT_L1_R1(SUBFRAME) \
149 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
150 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
151 for (n = 0; n < 12; n++, YL += 32, YR += 32) { \
152 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++); \
153 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++); \
156 #define REQUANT_L1_R0(SUBFRAME) \
157 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
158 for (n = 0; n < 12; n++, YL += 32, YR += 32) { \
159 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++); \
160 *YR = 0; \
163 #define REQUANT_L0_R1(SUBFRAME) \
164 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
165 for (n = 0; n < 12; n++, YL += 32, YR += 32) { \
166 *YL = 0; \
167 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++); \
170 #define REQUANT_SILENCE \
171 for (n = 0; n < 36; n++, YL += 32, YR += 32) { \
172 *YR = *YL = 0; \
177 * set the scf indexes for seeking use
178 * needed only for sv7 seeking
179 * @param d
181 void mpc_decoder_reset_scf(mpc_decoder * d, int value)
183 memset(d->SCF_Index_L, value, sizeof d->SCF_Index_L );
184 memset(d->SCF_Index_R, value, sizeof d->SCF_Index_R );
187 static void mpc_decoder_setup(mpc_decoder *d)
189 memset(d, 0, sizeof *d);
191 d->__r1 = 1;
192 d->__r2 = 1;
193 d->Y_L = g_Y_L;
194 d->Y_R = g_Y_R;
196 memset(d->Y_L, 0, sizeof(g_Y_L));
197 memset(d->Y_R, 0, sizeof(g_Y_R));
199 mpc_decoder_init_quant(d, 1.0f);
202 static void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
204 d->stream_version = si->stream_version;
205 d->ms = si->ms;
206 d->max_band = si->max_band;
207 d->channels = si->channels;
208 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + si->beg_silence;
210 if (si->stream_version == 7 && si->is_true_gapless)
211 d->samples = ((si->samples + MPC_FRAME_LENGTH - 1) / MPC_FRAME_LENGTH) * MPC_FRAME_LENGTH;
212 else
213 d->samples = si->samples;
216 mpc_decoder * mpc_decoder_init(mpc_streaminfo *si)
218 mpc_decoder* p_tmp = &g_mpc_decoder;
220 if (p_tmp != 0) {
221 mpc_decoder_setup(p_tmp);
222 mpc_decoder_set_streaminfo(p_tmp, si);
223 huff_init_lut(LUT_DEPTH);
226 return p_tmp;
229 void mpc_decoder_exit(mpc_decoder *d)
231 (void)d;
234 void mpc_decoder_decode_frame(mpc_decoder * d,
235 mpc_bits_reader * r,
236 mpc_frame_info * i)
238 mpc_bits_reader r_sav = *r;
239 mpc_int64_t samples_left;
241 samples_left = d->samples - d->decoded_samples + MPC_DECODER_SYNTH_DELAY;
243 if (samples_left <= 0 && d->samples != 0) {
244 i->samples = 0;
245 i->bits = -1;
246 return;
249 if (d->stream_version == 8) {
250 mpc_decoder_read_bitstream_sv8(d, r, i->is_key_frame);
251 } else {
252 mpc_decoder_read_bitstream_sv7(d, r);
255 if (d->samples_to_skip < MPC_FRAME_LENGTH + MPC_DECODER_SYNTH_DELAY) {
256 mpc_decoder_requantisierung(d);
257 mpc_decoder_synthese_filter_float(d, i->buffer, d->channels);
260 d->decoded_samples += MPC_FRAME_LENGTH;
262 // reconstruct exact filelength
263 if (d->decoded_samples - d->samples < MPC_FRAME_LENGTH && d->stream_version == 7) {
264 int last_frame_samples = mpc_bits_read(r, 11);
265 if (d->decoded_samples == d->samples) {
266 if (last_frame_samples == 0) last_frame_samples = MPC_FRAME_LENGTH;
267 d->samples += last_frame_samples - MPC_FRAME_LENGTH;
268 samples_left += last_frame_samples - MPC_FRAME_LENGTH;
272 i->samples = samples_left > MPC_FRAME_LENGTH ? MPC_FRAME_LENGTH : samples_left < 0 ? 0 : (mpc_uint32_t) samples_left;
273 i->bits = (mpc_uint32_t) (((r->buff - r_sav.buff) << 3) + r_sav.count - r->count);
275 if (d->samples_to_skip) {
276 if (i->samples <= d->samples_to_skip) {
277 d->samples_to_skip -= i->samples;
278 i->samples = 0;
279 } else {
280 i->samples -= d->samples_to_skip;
282 /* move valid samples to beginning for channel 0. noninterleaved! */
283 memmove(i->buffer,
284 i->buffer + d->samples_to_skip,
285 i->samples * sizeof(MPC_SAMPLE_FORMAT));
286 /* move valid samples to beginning for channel 1. noninterleaved! */
287 memmove(i->buffer + MPC_FRAME_LENGTH,
288 i->buffer + MPC_FRAME_LENGTH + d->samples_to_skip,
289 i->samples * sizeof(MPC_SAMPLE_FORMAT));
291 d->samples_to_skip = 0;
296 void
297 mpc_decoder_requantisierung(mpc_decoder *d)
299 mpc_int32_t Band;
300 mpc_int32_t n;
301 MPC_SAMPLE_FORMAT facL;
302 MPC_SAMPLE_FORMAT facR;
303 MPC_SAMPLE_FORMAT templ;
304 MPC_SAMPLE_FORMAT tempr;
305 MPC_SAMPLE_FORMAT* YL;
306 MPC_SAMPLE_FORMAT* YR;
307 mpc_int16_t* L;
308 mpc_int16_t* R;
309 const mpc_int32_t Last_Band = d->max_band;
311 #ifdef MPC_FIXED_POINT
312 #if MPC_FIXED_POINT_FRACTPART == 14
313 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
314 MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
315 #else
317 #error FIXME, Cc table is in 18.14 format
319 #endif
320 #else
321 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
322 MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
323 #endif
324 // requantization and scaling of subband-samples
325 for ( Band = 0; Band <= Last_Band; Band++ ) { // setting pointers
326 YL = d->Y_L + Band;
327 YR = d->Y_R + Band;
328 L = d->Q[Band].L;
329 R = d->Q[Band].R;
330 /************************** MS-coded **************************/
331 if ( d->MS_Flag [Band] ) {
332 if ( d->Res_L [Band] ) {
333 if ( d->Res_R [Band] ) { // M!=0, S!=0
334 REQUANT_M1_S1(0);
335 REQUANT_M1_S1(1);
336 REQUANT_M1_S1(2);
337 } else { // M!=0, S==0
338 REQUANT_M1_S0(0);
339 REQUANT_M1_S0(1);
340 REQUANT_M1_S0(2);
342 } else {
343 if ( d->Res_R[Band] ) // M==0, S!=0
345 REQUANT_M0_S1(0);
346 REQUANT_M0_S1(1);
347 REQUANT_M0_S1(2);
348 } else { // M==0, S==0
349 REQUANT_SILENCE;
353 /************************** LR-coded **************************/
354 else {
355 if ( d->Res_L [Band] ) {
356 if ( d->Res_R [Band] ) { // L!=0, R!=0
357 REQUANT_L1_R1(0);
358 REQUANT_L1_R1(1);
359 REQUANT_L1_R1(2);
360 } else { // L!=0, R==0
361 REQUANT_L1_R0(0);
362 REQUANT_L1_R0(1);
363 REQUANT_L1_R0(2);
366 else {
367 if ( d->Res_R [Band] ) { // L==0, R!=0
368 REQUANT_L0_R1(0);
369 REQUANT_L0_R1(1);
370 REQUANT_L0_R1(2);
371 } else { // L==0, R==0
372 REQUANT_SILENCE;
379 void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r)
381 mpc_int32_t n, idx, Max_used_Band = 0;
383 /***************************** Header *****************************/
385 // first subband
386 d->Res_L[0] = mpc_bits_read(r, 4);
387 d->Res_R[0] = mpc_bits_read(r, 4);
388 if (!(d->Res_L[0] == 0 && d->Res_R[0] == 0)) {
389 if (d->ms)
390 d->MS_Flag[0] = mpc_bits_read(r, 1);
391 Max_used_Band = 1;
394 // consecutive subbands
395 for ( n = 1; n <= d->max_band; n++ ) {
396 idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
397 d->Res_L[n] = (idx!=4) ? d->Res_L[n - 1] + idx : (int) mpc_bits_read(r, 4);
399 idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
400 d->Res_R[n] = (idx!=4) ? d->Res_R[n - 1] + idx : (int) mpc_bits_read(r, 4);
402 if (!(d->Res_L[n] == 0 && d->Res_R[n] == 0)) {
403 if (d->ms)
404 d->MS_Flag[n] = mpc_bits_read(r, 1);
405 Max_used_Band = n + 1;
409 /****************************** SCFI ******************************/
410 for ( n = 0; n < Max_used_Band; n++ ) {
411 if (d->Res_L[n])
412 d->SCFI_L[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
413 if (d->Res_R[n])
414 d->SCFI_R[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
417 /**************************** SCF/DSCF ****************************/
418 for ( n = 0; n < Max_used_Band; n++ ) {
419 mpc_int32_t * SCF = d->SCF_Index_L[n];
420 mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
421 do {
422 if (Res) {
423 switch (SCFI) {
424 case 1:
425 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
426 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
427 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
428 SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
429 SCF[2] = SCF[1];
430 break;
431 case 3:
432 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
433 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
434 SCF[1] = SCF[0];
435 SCF[2] = SCF[1];
436 break;
437 case 2:
438 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
439 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
440 SCF[1] = SCF[0];
441 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
442 SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
443 break;
444 case 0:
445 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
446 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
447 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
448 SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
449 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
450 SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
451 break;
452 default:
453 return;
455 if (SCF[0] > 1024)
456 SCF[0] = 0x8080;
457 if (SCF[1] > 1024)
458 SCF[1] = 0x8080;
459 if (SCF[2] > 1024)
460 SCF[2] = 0x8080;
462 Res = d->Res_R[n];
463 SCFI = d->SCFI_R[n];
464 } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
467 // if (d->seeking == TRUE)
468 // return;
470 /***************************** Samples ****************************/
471 for ( n = 0; n < Max_used_Band; n++ ) {
472 mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
473 do {
474 mpc_int32_t k;
475 const mpc_lut_data *Table;
476 switch (Res) {
477 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
478 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17: case 0:
479 break;
480 case -1:
481 for (k=0; k<36; k++ ) {
482 mpc_uint32_t tmp = mpc_random_int(d);
483 q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
485 break;
486 case 1:
487 Table = & mpc_HuffQ[0][mpc_bits_read(r, 1)];
488 for ( k = 0; k < 36; k += 3) {
489 idx = mpc_bits_huff_lut(r, Table);
490 q[k] = g_sv7_idx30[idx];
491 q[k + 1] = g_sv7_idx31[idx];
492 q[k + 2] = g_sv7_idx32[idx];
494 break;
495 case 2:
496 Table = & mpc_HuffQ[1][mpc_bits_read(r, 1)];
497 for ( k = 0; k < 36; k += 2) {
498 idx = mpc_bits_huff_lut(r, Table);
499 q[k] = g_sv7_idx50[idx];
500 q[k + 1] = g_sv7_idx51[idx];
502 break;
503 case 3:
504 case 4:
505 case 5:
506 case 6:
507 case 7:
508 Table = & mpc_HuffQ[Res - 1][mpc_bits_read(r, 1)];
509 for ( k = 0; k < 36; k++ )
510 q[k] = mpc_bits_huff_lut(r, Table);
511 break;
512 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
513 for ( k = 0; k < 36; k++ )
514 q[k] = (mpc_int32_t)mpc_bits_read(r, Res_bit[Res]) - Dc[Res];
515 break;
516 default:
517 return;
520 Res = d->Res_R[n];
521 } while (q == d->Q[n].L && (q = d->Q[n].R));
525 void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r, mpc_bool_t is_key_frame)
527 mpc_int32_t n, Max_used_Band;
528 const mpc_can_data * Table, * Tables[2];
530 /***************************** Header *****************************/
532 if (is_key_frame == MPC_TRUE) {
533 Max_used_Band = mpc_bits_log_dec(r, d->max_band + 1);
534 } else {
535 Max_used_Band = d->last_max_band + mpc_bits_can_dec(r, & mpc_can_Bands);
536 if (Max_used_Band > 32) Max_used_Band -= 33;
538 d->last_max_band = Max_used_Band;
540 if (Max_used_Band) {
541 d->Res_L[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
542 d->Res_R[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
543 if (d->Res_L[Max_used_Band-1] > 15) d->Res_L[Max_used_Band-1] -= 17;
544 if (d->Res_R[Max_used_Band-1] > 15) d->Res_R[Max_used_Band-1] -= 17;
545 for ( n = Max_used_Band - 2; n >= 0; n--) {
546 d->Res_L[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_L[n + 1] > 2]) + d->Res_L[n + 1];
547 if (d->Res_L[n] > 15) d->Res_L[n] -= 17;
548 d->Res_R[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_R[n + 1] > 2]) + d->Res_R[n + 1];
549 if (d->Res_R[n] > 15) d->Res_R[n] -= 17;
552 if (d->ms) {
553 int cnt = 0, tot = 0;
554 mpc_uint32_t tmp = 0;
555 for( n = 0; n < Max_used_Band; n++)
556 if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 )
557 tot++;
558 cnt = mpc_bits_log_dec(r, tot);
559 if (cnt != 0 && cnt != tot)
560 tmp = mpc_bits_enum_dec(r, mini(cnt, tot-cnt), tot);
561 if (cnt * 2 > tot) tmp = ~tmp;
562 for( n = Max_used_Band - 1; n >= 0; n--)
563 if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 ) {
564 d->MS_Flag[n] = tmp & 1;
565 tmp >>= 1;
570 for( n = Max_used_Band; n <= d->max_band; n++)
571 d->Res_L[n] = d->Res_R[n] = 0;
573 /****************************** SCFI ******************************/
574 if (is_key_frame == MPC_TRUE){
575 for( n = 0; n < 32; n++)
576 d->DSCF_Flag_L[n] = d->DSCF_Flag_R[n] = 1; // new block -> force key frame
579 Tables[0] = & mpc_can_SCFI[0];
580 Tables[1] = & mpc_can_SCFI[1];
581 for ( n = 0; n < Max_used_Band; n++ ) {
582 int tmp = 0, cnt = -1;
583 if (d->Res_L[n]) cnt++;
584 if (d->Res_R[n]) cnt++;
585 if (cnt >= 0) {
586 tmp = mpc_bits_can_dec(r, Tables[cnt]);
587 if (d->Res_L[n]) d->SCFI_L[n] = tmp >> (2 * cnt);
588 if (d->Res_R[n]) d->SCFI_R[n] = tmp & 3;
592 /**************************** SCF/DSCF ****************************/
594 for ( n = 0; n < Max_used_Band; n++ ) {
595 mpc_int32_t * SCF = d->SCF_Index_L[n];
596 mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
597 mpc_bool_t * DSCF_Flag = &d->DSCF_Flag_L[n];
599 do {
600 if ( Res ) {
601 int m;
602 if (*DSCF_Flag == 1) {
603 SCF[0] = (mpc_int32_t)mpc_bits_read(r, 7) - 6;
604 *DSCF_Flag = 0;
605 } else {
606 mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[1]);
607 if (tmp == 64)
608 tmp += mpc_bits_read(r, 6);
609 SCF[0] = ((SCF[2] - 25 + tmp) & 127) - 6;
611 for( m = 0; m < 2; m++){
612 if (((SCFI << m) & 2) == 0) {
613 mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[0]);
614 if (tmp == 31)
615 tmp = 64 + mpc_bits_read(r, 6);
616 SCF[m + 1] = ((SCF[m] - 25 + tmp) & 127) - 6;
617 } else
618 SCF[m + 1] = SCF[m];
621 Res = d->Res_R[n];
622 SCFI = d->SCFI_R[n];
623 DSCF_Flag = &d->DSCF_Flag_R[n];
624 } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
627 /***************************** Samples ****************************/
628 for ( n = 0; n < Max_used_Band; n++ ) {
629 mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
630 static const int thres[] = {0, 0, 3, 0, 0, 1, 3, 4, 8};
631 do {
632 mpc_int32_t k = 0, idx = 1;
633 if (Res != 0) {
634 if (Res == 2) {
635 Tables[0] = & mpc_can_Q [0][0];
636 Tables[1] = & mpc_can_Q [0][1];
637 idx = 2 * thres[Res];
638 for ( ; k < 36; k += 3) {
639 int tmp = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
640 q[k] = g_sv8_idx50[tmp];
641 q[k + 1] = g_sv8_idx51[tmp];
642 q[k + 2] = g_sv8_idx52[tmp];
643 idx = (idx >> 1) + g_sv8_HuffQ2_var[tmp];
645 } else if (Res == 1) {
646 Table = & mpc_can_Q1;
647 for( ; k < 36; ){
648 int kmax = k + 18;
649 mpc_uint_t cnt = mpc_bits_can_dec(r, Table);
650 idx = 0;
651 if (cnt > 0 && cnt < 18)
652 idx = mpc_bits_enum_dec(r, cnt <= 9 ? cnt : 18 - cnt, 18);
653 if (cnt > 9) idx = ~idx;
654 for ( ; k < kmax; k++) {
655 q[k] = 0;
656 if ( idx & (1 << 17) )
657 q[k] = (mpc_bits_read(r, 1) << 1) - 1;
658 idx <<= 1;
661 } else if (Res == -1) {
662 for ( ; k<36; k++ ) {
663 mpc_uint32_t tmp = mpc_random_int(d);
664 q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
666 } else if (Res <= 4) {
667 Table = & mpc_can_Q[1][Res - 3];
668 for ( ; k < 36; k += 2 ) {
669 union {
670 mpc_int8_t sym;
671 struct { mpc_int8_t s1:4, s2:4; };
672 } tmp;
673 tmp.sym = mpc_bits_can_dec(r, Table);
674 q[k] = tmp.s1;
675 q[k + 1] = tmp.s2;
677 } else if (Res <= 8) {
678 Tables[0] = & mpc_can_Q [Res - 3][0];
679 Tables[1] = & mpc_can_Q [Res - 3][1];
680 idx = 2 * thres[Res];
681 for ( ; k < 36; k++ ) {
682 q[k] = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
683 idx = (idx >> 1) + absi(q[k]);
685 } else {
686 for ( ; k < 36; k++ ) {
687 q[k] = (unsigned char) mpc_bits_can_dec(r, & mpc_can_Q9up);
688 if (Res != 9)
689 q[k] = (q[k] << (Res - 9)) | mpc_bits_read(r, Res - 9);
690 q[k] -= Dc[Res];
695 Res = d->Res_R[n];
696 } while (q == d->Q[n].L && (q = d->Q[n].R));