Minor optimization for musepack codec through moving functions to IRAM on PP processo...
[kugel-rb.git] / apps / codecs / libmusepack / mpc_decoder.c
blob3f8fbc7aad7971e94dc8f4a3aeb9dc5eadf8dffd
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);
126 * set the scf indexes for seeking use
127 * needed only for sv7 seeking
128 * @param d
130 void mpc_decoder_reset_scf(mpc_decoder * d, int value)
132 memset(d->SCF_Index_L, value, sizeof d->SCF_Index_L );
133 memset(d->SCF_Index_R, value, sizeof d->SCF_Index_R );
136 void mpc_decoder_setup(mpc_decoder *d)
138 memset(d, 0, sizeof *d);
140 d->__r1 = 1;
141 d->__r2 = 1;
142 d->Y_L = g_Y_L;
143 d->Y_R = g_Y_R;
145 memset(d->Y_L, 0, sizeof(g_Y_L));
146 memset(d->Y_R, 0, sizeof(g_Y_R));
148 mpc_decoder_init_quant(d, 1.0f);
151 void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
153 d->stream_version = si->stream_version;
154 d->ms = si->ms;
155 d->max_band = si->max_band;
156 d->channels = si->channels;
157 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + si->beg_silence;
159 if (si->stream_version == 7 && si->is_true_gapless)
160 d->samples = ((si->samples + MPC_FRAME_LENGTH - 1) / MPC_FRAME_LENGTH) * MPC_FRAME_LENGTH;
161 else
162 d->samples = si->samples;
165 mpc_decoder * mpc_decoder_init(mpc_streaminfo *si)
167 mpc_decoder* p_tmp = &g_mpc_decoder;
169 if (p_tmp != 0) {
170 mpc_decoder_setup(p_tmp);
171 mpc_decoder_set_streaminfo(p_tmp, si);
172 huff_init_lut(LUT_DEPTH);
175 return p_tmp;
178 void mpc_decoder_exit(mpc_decoder *d)
180 (void)d;
183 void mpc_decoder_decode_frame(mpc_decoder * d,
184 mpc_bits_reader * r,
185 mpc_frame_info * i)
187 mpc_bits_reader r_sav = *r;
188 mpc_int64_t samples_left;
190 samples_left = d->samples - d->decoded_samples + MPC_DECODER_SYNTH_DELAY;
192 if (samples_left <= 0 && d->samples != 0) {
193 i->samples = 0;
194 i->bits = -1;
195 return;
198 if (d->stream_version == 8) {
199 mpc_decoder_read_bitstream_sv8(d, r, i->is_key_frame);
200 } else {
201 mpc_decoder_read_bitstream_sv7(d, r);
204 if (d->samples_to_skip < MPC_FRAME_LENGTH + MPC_DECODER_SYNTH_DELAY) {
205 mpc_decoder_requantisierung(d);
206 mpc_decoder_synthese_filter_float(d, i->buffer, d->channels);
209 d->decoded_samples += MPC_FRAME_LENGTH;
211 // reconstruct exact filelength
212 if (d->decoded_samples - d->samples < MPC_FRAME_LENGTH && d->stream_version == 7) {
213 int last_frame_samples = mpc_bits_read(r, 11);
214 if (d->decoded_samples == d->samples) {
215 if (last_frame_samples == 0) last_frame_samples = MPC_FRAME_LENGTH;
216 d->samples += last_frame_samples - MPC_FRAME_LENGTH;
217 samples_left += last_frame_samples - MPC_FRAME_LENGTH;
221 i->samples = samples_left > MPC_FRAME_LENGTH ? MPC_FRAME_LENGTH : samples_left < 0 ? 0 : (mpc_uint32_t) samples_left;
222 i->bits = (mpc_uint32_t) (((r->buff - r_sav.buff) << 3) + r_sav.count - r->count);
224 if (d->samples_to_skip) {
225 if (i->samples <= d->samples_to_skip) {
226 d->samples_to_skip -= i->samples;
227 i->samples = 0;
228 } else {
229 i->samples -= d->samples_to_skip;
231 /* move valid samples to beginning for channel 0. noninterleaved! */
232 memmove(i->buffer,
233 i->buffer + d->samples_to_skip,
234 i->samples * sizeof(MPC_SAMPLE_FORMAT));
235 /* move valid samples to beginning for channel 1. noninterleaved! */
236 memmove(i->buffer + MPC_FRAME_LENGTH,
237 i->buffer + MPC_FRAME_LENGTH + d->samples_to_skip,
238 i->samples * sizeof(MPC_SAMPLE_FORMAT));
240 d->samples_to_skip = 0;
245 void
246 mpc_decoder_requantisierung(mpc_decoder *d)
248 mpc_int32_t Band;
249 mpc_int32_t n;
250 MPC_SAMPLE_FORMAT facL;
251 MPC_SAMPLE_FORMAT facR;
252 MPC_SAMPLE_FORMAT templ;
253 MPC_SAMPLE_FORMAT tempr;
254 MPC_SAMPLE_FORMAT* YL;
255 MPC_SAMPLE_FORMAT* YR;
256 mpc_int16_t* L;
257 mpc_int16_t* R;
258 const mpc_int32_t Last_Band = d->max_band;
260 #ifdef MPC_FIXED_POINT
261 #if MPC_FIXED_POINT_FRACTPART == 14
262 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
263 MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
264 #else
266 #error FIXME, Cc table is in 18.14 format
268 #endif
269 #else
270 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
271 MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
272 #endif
273 // requantization and scaling of subband-samples
274 for ( Band = 0; Band <= Last_Band; Band++ ) { // setting pointers
275 YL = d->Y_L + Band;
276 YR = d->Y_R + Band;
277 L = d->Q[Band].L;
278 R = d->Q[Band].R;
279 /************************** MS-coded **************************/
280 if ( d->MS_Flag [Band] ) {
281 if ( d->Res_L [Band] ) {
282 if ( d->Res_R [Band] ) { // M!=0, S!=0
283 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][0] & 0xFF);
284 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
285 for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
286 *YL = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
287 *YR = templ - tempr;
289 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
290 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
291 for ( ; n < 24; n++, YL += 32, YR += 32 ) {
292 *YL = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
293 *YR = templ - tempr;
295 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
296 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
297 for ( ; n < 36; n++, YL += 32, YR += 32 ) {
298 *YL = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
299 *YR = templ - tempr;
301 } else { // M!=0, S==0
302 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][0] & 0xFF);
303 for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
304 *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
306 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
307 for ( ; n < 24; n++, YL += 32, YR += 32 ) {
308 *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
310 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
311 for ( ; n < 36; n++, YL += 32, YR += 32 ) {
312 *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
315 } else {
316 if (d->Res_R[Band]) // M==0, S!=0
318 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
319 for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
320 *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
322 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
323 for ( ; n < 24; n++, YL += 32, YR += 32 ) {
324 *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
326 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
327 for ( ; n < 36; n++, YL += 32, YR += 32 ) {
328 *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
330 } else { // M==0, S==0
331 for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
332 *YR = *YL = 0;
337 /************************** LR-coded **************************/
338 else {
339 if ( d->Res_L [Band] ) {
340 if ( d->Res_R [Band] ) { // L!=0, R!=0
341 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][0] & 0xFF);
342 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
343 for (n = 0; n < 12; n++, YL += 32, YR += 32 ) {
344 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
345 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
347 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
348 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
349 for (; n < 24; n++, YL += 32, YR += 32 ) {
350 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
351 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
353 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
354 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
355 for (; n < 36; n++, YL += 32, YR += 32 ) {
356 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
357 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
359 } else { // L!=0, R==0
360 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][0] & 0xFF);
361 for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
362 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
363 *YR = 0;
365 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
366 for ( ; n < 24; n++, YL += 32, YR += 32 ) {
367 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
368 *YR = 0;
370 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
371 for ( ; n < 36; n++, YL += 32, YR += 32 ) {
372 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
373 *YR = 0;
377 else {
378 if ( d->Res_R [Band] ) { // L==0, R!=0
379 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
380 for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
381 *YL = 0;
382 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
384 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
385 for ( ; n < 24; n++, YL += 32, YR += 32 ) {
386 *YL = 0;
387 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
389 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
390 for ( ; n < 36; n++, YL += 32, YR += 32 ) {
391 *YL = 0;
392 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
394 } else { // L==0, R==0
395 for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
396 *YR = *YL = 0;
404 void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r)
406 mpc_int32_t n, idx, Max_used_Band = 0;
408 /***************************** Header *****************************/
410 // first subband
411 d->Res_L[0] = mpc_bits_read(r, 4);
412 d->Res_R[0] = mpc_bits_read(r, 4);
413 if (!(d->Res_L[0] == 0 && d->Res_R[0] == 0)) {
414 if (d->ms)
415 d->MS_Flag[0] = mpc_bits_read(r, 1);
416 Max_used_Band = 1;
419 // consecutive subbands
420 for ( n = 1; n <= d->max_band; n++ ) {
421 idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
422 d->Res_L[n] = (idx!=4) ? d->Res_L[n - 1] + idx : (int) mpc_bits_read(r, 4);
424 idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
425 d->Res_R[n] = (idx!=4) ? d->Res_R[n - 1] + idx : (int) mpc_bits_read(r, 4);
427 if (!(d->Res_L[n] == 0 && d->Res_R[n] == 0)) {
428 if (d->ms)
429 d->MS_Flag[n] = mpc_bits_read(r, 1);
430 Max_used_Band = n + 1;
434 /****************************** SCFI ******************************/
435 for ( n = 0; n < Max_used_Band; n++ ) {
436 if (d->Res_L[n])
437 d->SCFI_L[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
438 if (d->Res_R[n])
439 d->SCFI_R[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
442 /**************************** SCF/DSCF ****************************/
443 for ( n = 0; n < Max_used_Band; n++ ) {
444 mpc_int32_t * SCF = d->SCF_Index_L[n];
445 mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
446 do {
447 if (Res) {
448 switch (SCFI) {
449 case 1:
450 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
451 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
452 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
453 SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
454 SCF[2] = SCF[1];
455 break;
456 case 3:
457 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
458 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
459 SCF[1] = SCF[0];
460 SCF[2] = SCF[1];
461 break;
462 case 2:
463 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
464 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
465 SCF[1] = SCF[0];
466 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
467 SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
468 break;
469 case 0:
470 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
471 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
472 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
473 SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
474 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
475 SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
476 break;
477 default:
478 return;
480 if (SCF[0] > 1024)
481 SCF[0] = 0x8080;
482 if (SCF[1] > 1024)
483 SCF[1] = 0x8080;
484 if (SCF[2] > 1024)
485 SCF[2] = 0x8080;
487 Res = d->Res_R[n];
488 SCFI = d->SCFI_R[n];
489 } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
492 // if (d->seeking == TRUE)
493 // return;
495 /***************************** Samples ****************************/
496 for ( n = 0; n < Max_used_Band; n++ ) {
497 mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
498 do {
499 mpc_int32_t k;
500 const mpc_lut_data *Table;
501 switch (Res) {
502 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
503 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17: case 0:
504 break;
505 case -1:
506 for (k=0; k<36; k++ ) {
507 mpc_uint32_t tmp = mpc_random_int(d);
508 q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
510 break;
511 case 1:
512 Table = & mpc_HuffQ[0][mpc_bits_read(r, 1)];
513 for ( k = 0; k < 36; k += 3) {
514 idx = mpc_bits_huff_lut(r, Table);
515 q[k] = g_sv7_idx30[idx];
516 q[k + 1] = g_sv7_idx31[idx];
517 q[k + 2] = g_sv7_idx32[idx];
519 break;
520 case 2:
521 Table = & mpc_HuffQ[1][mpc_bits_read(r, 1)];
522 for ( k = 0; k < 36; k += 2) {
523 idx = mpc_bits_huff_lut(r, Table);
524 q[k] = g_sv7_idx50[idx];
525 q[k + 1] = g_sv7_idx51[idx];
527 break;
528 case 3:
529 case 4:
530 case 5:
531 case 6:
532 case 7:
533 Table = & mpc_HuffQ[Res - 1][mpc_bits_read(r, 1)];
534 for ( k = 0; k < 36; k++ )
535 q[k] = mpc_bits_huff_lut(r, Table);
536 break;
537 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
538 for ( k = 0; k < 36; k++ )
539 q[k] = (mpc_int32_t)mpc_bits_read(r, Res_bit[Res]) - Dc[Res];
540 break;
541 default:
542 return;
545 Res = d->Res_R[n];
546 } while (q == d->Q[n].L && (q = d->Q[n].R));
550 void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r, mpc_bool_t is_key_frame)
552 mpc_int32_t n, Max_used_Band;
553 const mpc_can_data * Table, * Tables[2];
555 /***************************** Header *****************************/
557 if (is_key_frame == MPC_TRUE) {
558 Max_used_Band = mpc_bits_log_dec(r, d->max_band + 1);
559 } else {
560 Max_used_Band = d->last_max_band + mpc_bits_can_dec(r, & mpc_can_Bands);
561 if (Max_used_Band > 32) Max_used_Band -= 33;
563 d->last_max_band = Max_used_Band;
565 if (Max_used_Band) {
566 d->Res_L[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
567 d->Res_R[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
568 if (d->Res_L[Max_used_Band-1] > 15) d->Res_L[Max_used_Band-1] -= 17;
569 if (d->Res_R[Max_used_Band-1] > 15) d->Res_R[Max_used_Band-1] -= 17;
570 for ( n = Max_used_Band - 2; n >= 0; n--) {
571 d->Res_L[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_L[n + 1] > 2]) + d->Res_L[n + 1];
572 if (d->Res_L[n] > 15) d->Res_L[n] -= 17;
573 d->Res_R[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_R[n + 1] > 2]) + d->Res_R[n + 1];
574 if (d->Res_R[n] > 15) d->Res_R[n] -= 17;
577 if (d->ms) {
578 int cnt = 0, tot = 0;
579 mpc_uint32_t tmp = 0;
580 for( n = 0; n < Max_used_Band; n++)
581 if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 )
582 tot++;
583 cnt = mpc_bits_log_dec(r, tot);
584 if (cnt != 0 && cnt != tot)
585 tmp = mpc_bits_enum_dec(r, mini(cnt, tot-cnt), tot);
586 if (cnt * 2 > tot) tmp = ~tmp;
587 for( n = Max_used_Band - 1; n >= 0; n--)
588 if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 ) {
589 d->MS_Flag[n] = tmp & 1;
590 tmp >>= 1;
595 for( n = Max_used_Band; n <= d->max_band; n++)
596 d->Res_L[n] = d->Res_R[n] = 0;
598 /****************************** SCFI ******************************/
599 if (is_key_frame == MPC_TRUE){
600 for( n = 0; n < 32; n++)
601 d->DSCF_Flag_L[n] = d->DSCF_Flag_R[n] = 1; // new block -> force key frame
604 Tables[0] = & mpc_can_SCFI[0];
605 Tables[1] = & mpc_can_SCFI[1];
606 for ( n = 0; n < Max_used_Band; n++ ) {
607 int tmp = 0, cnt = -1;
608 if (d->Res_L[n]) cnt++;
609 if (d->Res_R[n]) cnt++;
610 if (cnt >= 0) {
611 tmp = mpc_bits_can_dec(r, Tables[cnt]);
612 if (d->Res_L[n]) d->SCFI_L[n] = tmp >> (2 * cnt);
613 if (d->Res_R[n]) d->SCFI_R[n] = tmp & 3;
617 /**************************** SCF/DSCF ****************************/
619 for ( n = 0; n < Max_used_Band; n++ ) {
620 mpc_int32_t * SCF = d->SCF_Index_L[n];
621 mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
622 mpc_bool_t * DSCF_Flag = &d->DSCF_Flag_L[n];
624 do {
625 if ( Res ) {
626 int m;
627 if (*DSCF_Flag == 1) {
628 SCF[0] = (mpc_int32_t)mpc_bits_read(r, 7) - 6;
629 *DSCF_Flag = 0;
630 } else {
631 mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[1]);
632 if (tmp == 64)
633 tmp += mpc_bits_read(r, 6);
634 SCF[0] = ((SCF[2] - 25 + tmp) & 127) - 6;
636 for( m = 0; m < 2; m++){
637 if (((SCFI << m) & 2) == 0) {
638 mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[0]);
639 if (tmp == 31)
640 tmp = 64 + mpc_bits_read(r, 6);
641 SCF[m + 1] = ((SCF[m] - 25 + tmp) & 127) - 6;
642 } else
643 SCF[m + 1] = SCF[m];
646 Res = d->Res_R[n];
647 SCFI = d->SCFI_R[n];
648 DSCF_Flag = &d->DSCF_Flag_R[n];
649 } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
652 /***************************** Samples ****************************/
653 for ( n = 0; n < Max_used_Band; n++ ) {
654 mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
655 static const int thres[] = {0, 0, 3, 0, 0, 1, 3, 4, 8};
656 do {
657 mpc_int32_t k = 0, idx = 1;
658 if (Res != 0) {
659 if (Res == 2) {
660 Tables[0] = & mpc_can_Q [0][0];
661 Tables[1] = & mpc_can_Q [0][1];
662 idx = 2 * thres[Res];
663 for ( ; k < 36; k += 3) {
664 int tmp = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
665 q[k] = g_sv8_idx50[tmp];
666 q[k + 1] = g_sv8_idx51[tmp];
667 q[k + 2] = g_sv8_idx52[tmp];
668 idx = (idx >> 1) + g_sv8_HuffQ2_var[tmp];
670 } else if (Res == 1) {
671 Table = & mpc_can_Q1;
672 for( ; k < 36; ){
673 int kmax = k + 18;
674 mpc_uint_t cnt = mpc_bits_can_dec(r, Table);
675 idx = 0;
676 if (cnt > 0 && cnt < 18)
677 idx = mpc_bits_enum_dec(r, cnt <= 9 ? cnt : 18 - cnt, 18);
678 if (cnt > 9) idx = ~idx;
679 for ( ; k < kmax; k++) {
680 q[k] = 0;
681 if ( idx & (1 << 17) )
682 q[k] = (mpc_bits_read(r, 1) << 1) - 1;
683 idx <<= 1;
686 } else if (Res == -1) {
687 for ( ; k<36; k++ ) {
688 mpc_uint32_t tmp = mpc_random_int(d);
689 q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
691 } else if (Res <= 4) {
692 Table = & mpc_can_Q[1][Res - 3];
693 for ( ; k < 36; k += 2 ) {
694 union {
695 mpc_int8_t sym;
696 struct { mpc_int8_t s1:4, s2:4; };
697 } tmp;
698 tmp.sym = mpc_bits_can_dec(r, Table);
699 q[k] = tmp.s1;
700 q[k + 1] = tmp.s2;
702 } else if (Res <= 8) {
703 Tables[0] = & mpc_can_Q [Res - 3][0];
704 Tables[1] = & mpc_can_Q [Res - 3][1];
705 idx = 2 * thres[Res];
706 for ( ; k < 36; k++ ) {
707 q[k] = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
708 idx = (idx >> 1) + absi(q[k]);
710 } else {
711 for ( ; k < 36; k++ ) {
712 q[k] = (unsigned char) mpc_bits_can_dec(r, & mpc_can_Q9up);
713 if (Res != 9)
714 q[k] = (q[k] << (Res - 9)) | mpc_bits_read(r, Res - 9);
715 q[k] -= Dc[Res];
720 Res = d->Res_R[n];
721 } while (q == d->Q[n].L && (q = d->Q[n].R));