summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libmusepack/mpc_decoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libmusepack/mpc_decoder.c')
-rw-r--r--lib/rbcodec/codecs/libmusepack/mpc_decoder.c767
1 files changed, 767 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libmusepack/mpc_decoder.c b/lib/rbcodec/codecs/libmusepack/mpc_decoder.c
new file mode 100644
index 0000000000..3bfc4cc7fc
--- /dev/null
+++ b/lib/rbcodec/codecs/libmusepack/mpc_decoder.c
@@ -0,0 +1,767 @@
1/*
2 Copyright (c) 2005-2009, The Musepack Development Team
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
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.
16
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.
21
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.
33*/
34/// \file mpc_decoder.c
35/// Core decoding routines and logic.
36
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"
47
48//SV7 tables
49extern const mpc_lut_data mpc_HuffQ [7] [2];
50extern const mpc_lut_data mpc_HuffHdr;
51extern const mpc_huffman mpc_table_HuffSCFI [ 4];
52extern const mpc_lut_data mpc_HuffDSCF;
53
54//SV8 tables
55extern const mpc_can_data mpc_can_Bands;
56extern const mpc_can_data mpc_can_SCFI[2];
57extern const mpc_can_data mpc_can_DSCF[2];
58extern const mpc_can_data mpc_can_Res [2];
59extern const mpc_can_data mpc_can_Q [8][2];
60extern const mpc_can_data mpc_can_Q1;
61extern const mpc_can_data mpc_can_Q9up;
62
63//Decoder globals (g_Y_L and g_Y_R do not fit into iram for all targets)
64static mpc_decoder g_mpc_decoder IBSS_ATTR;
65static MPC_SAMPLE_FORMAT g_V_L[MPC_V_MEM + 960 ] IBSS_ATTR MEM_ALIGN_ATTR;
66static MPC_SAMPLE_FORMAT g_Y_L[MPC_FRAME_LENGTH] IBSS_ATTR_MPC_LARGE_IRAM MEM_ALIGN_ATTR;
67static MPC_SAMPLE_FORMAT g_V_R[MPC_V_MEM + 960 ] IBSS_ATTR MEM_ALIGN_ATTR;
68static MPC_SAMPLE_FORMAT g_Y_R[MPC_FRAME_LENGTH] IBSS_ATTR_MPC_LARGE_IRAM MEM_ALIGN_ATTR;
69
70//SV7 globals (decoding results for bundled quantizers (3- and 5-step))
71static const mpc_int32_t g_sv7_idx30[] ICONST_ATTR =
72{-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};
73static const mpc_int32_t g_sv7_idx31[] ICONST_ATTR =
74{-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};
75static const mpc_int32_t g_sv7_idx32[] ICONST_ATTR =
76{-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};
77static const mpc_int32_t g_sv7_idx50[] ICONST_ATTR =
78{-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};
79static const mpc_int32_t g_sv7_idx51[] ICONST_ATTR =
80{-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};
81
82//SV8 globals (decoding results for bundled quantizers (3- and 5-step))
83static const mpc_int8_t g_sv8_idx50[125] ICONST_ATTR =
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 -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,
88 -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};
89static const mpc_int8_t g_sv8_idx51[125] ICONST_ATTR =
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 -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,
94 -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};
95static const mpc_int8_t g_sv8_idx52[125] ICONST_ATTR =
96{-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,
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 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,
99 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,
100 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};
101static const mpc_int8_t g_sv8_HuffQ2_var[125] ICONST_ATTR =
102{ 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,
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 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,
105 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,
106 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};
107
108//------------------------------------------------------------------------------
109// types
110//------------------------------------------------------------------------------
111enum
112{
113 MEMSIZE = MPC_DECODER_MEMSIZE, // overall buffer size
114 MEMSIZE2 = (MEMSIZE/2), // size of one buffer
115 MEMMASK = (MEMSIZE-1)
116};
117
118//------------------------------------------------------------------------------
119// forward declarations
120//------------------------------------------------------------------------------
121static void mpc_decoder_requantisierung (mpc_decoder *d)
122 ICODE_ATTR_MPC_LARGE_IRAM;
123static void mpc_decoder_read_bitstream_sv7(mpc_decoder * d,
124 mpc_bits_reader * r)
125 ICODE_ATTR_MPC_LARGE_IRAM;
126static void mpc_decoder_read_bitstream_sv8(mpc_decoder * d,
127 mpc_bits_reader * r,
128 mpc_bool_t is_key_frame)
129 ICODE_ATTR_MPC_SV8_BS_DEC;
130
131//------------------------------------------------------------------------------
132// macros
133//------------------------------------------------------------------------------
134#define REQUANT_M1_S1_SAMPLES(IDX) \
135 *(YL+=IDX) = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++)); \
136 *(YR+=IDX) = templ - tempr;
137
138#define REQUANT_M1_S1(SUBFRAME) \
139 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
140 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
141 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
142 REQUANT_M1_S1_SAMPLES( 0); \
143 REQUANT_M1_S1_SAMPLES(32); \
144 REQUANT_M1_S1_SAMPLES(32); \
145 REQUANT_M1_S1_SAMPLES(32); \
146 }
147
148#define REQUANT_M1_S0_SAMPLES(IDX) \
149 *(YR+=IDX) = *(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
150
151#define REQUANT_M1_S0(SUBFRAME) \
152 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
153 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
154 REQUANT_M1_S0_SAMPLES( 0); \
155 REQUANT_M1_S0_SAMPLES(32); \
156 REQUANT_M1_S0_SAMPLES(32); \
157 REQUANT_M1_S0_SAMPLES(32); \
158 }
159
160#define REQUANT_M0_S1_SAMPLES(IDX) \
161 *(YR+=IDX) = -(*(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
162
163#define REQUANT_M0_S1(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+=4, YL += 32, YR += 32) { \
166 REQUANT_M0_S1_SAMPLES( 0); \
167 REQUANT_M0_S1_SAMPLES(32); \
168 REQUANT_M0_S1_SAMPLES(32); \
169 REQUANT_M0_S1_SAMPLES(32); \
170 }
171
172#define REQUANT_L1_R1_SAMPLES(IDX) \
173 *(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facL,*L++); \
174 *(YR+=IDX) = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
175
176#define REQUANT_L1_R1(SUBFRAME) \
177 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
178 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
179 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
180 REQUANT_L1_R1_SAMPLES( 0); \
181 REQUANT_L1_R1_SAMPLES(32); \
182 REQUANT_L1_R1_SAMPLES(32); \
183 REQUANT_L1_R1_SAMPLES(32); \
184 }
185
186#define REQUANT_L1_R0_SAMPLES(IDX) \
187 *(YL+=IDX) = MPC_MULTIPLY_FLOAT_INT(facL,*L++); \
188 *(YR+=IDX) = 0;
189
190#define REQUANT_L1_R0(SUBFRAME) \
191 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][SUBFRAME] & 0xFF); \
192 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
193 REQUANT_L1_R0_SAMPLES( 0); \
194 REQUANT_L1_R0_SAMPLES(32); \
195 REQUANT_L1_R0_SAMPLES(32); \
196 REQUANT_L1_R0_SAMPLES(32); \
197 }
198
199#define REQUANT_L0_R1_SAMPLES(IDX) \
200 *(YL+=IDX) = 0; \
201 *(YR+=IDX) = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
202
203#define REQUANT_L0_R1(SUBFRAME) \
204 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][SUBFRAME] & 0xFF); \
205 for (n = 0; n < 12; n+=4, YL += 32, YR += 32) { \
206 REQUANT_L0_R1_SAMPLES( 0); \
207 REQUANT_L0_R1_SAMPLES(32); \
208 REQUANT_L0_R1_SAMPLES(32); \
209 REQUANT_L0_R1_SAMPLES(32); \
210 }
211
212#define REQUANT_SILENCE_SAMPLES(IDX) \
213 *(YR+=IDX) = *(YL+=IDX) = 0;
214
215#define REQUANT_SILENCE \
216 for (n = 0; n < 36; n+=4, YL += 32, YR += 32) { \
217 REQUANT_SILENCE_SAMPLES( 0); \
218 REQUANT_SILENCE_SAMPLES(32); \
219 REQUANT_SILENCE_SAMPLES(32); \
220 REQUANT_SILENCE_SAMPLES(32); \
221 }
222
223/**
224 * set the scf indexes for seeking use
225 * needed only for sv7 seeking
226 * @param d
227 */
228void mpc_decoder_reset_scf(mpc_decoder * d, int value)
229{
230 memset(d->SCF_Index_L, value, sizeof d->SCF_Index_L );
231 memset(d->SCF_Index_R, value, sizeof d->SCF_Index_R );
232}
233
234static void mpc_decoder_setup(mpc_decoder *d)
235{
236#if defined(CPU_COLDFIRE)
237 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
238#endif
239
240 memset(d, 0, sizeof *d);
241
242 d->__r1 = 1;
243 d->__r2 = 1;
244 d->V_L = g_V_L;
245 d->V_R = g_V_R;
246 d->Y_L = g_Y_L;
247 d->Y_R = g_Y_R;
248
249 memset(d->V_L, 0, sizeof(g_V_L));
250 memset(d->V_R, 0, sizeof(g_V_R));
251 memset(d->Y_L, 0, sizeof(g_Y_L));
252 memset(d->Y_R, 0, sizeof(g_Y_R));
253
254 mpc_decoder_init_quant(d, MAKE_MPC_SAMPLE(1.0));
255}
256
257static void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
258{
259 d->stream_version = si->stream_version;
260 d->ms = si->ms;
261 d->max_band = si->max_band;
262 d->channels = si->channels;
263 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + si->beg_silence;
264
265 if (si->stream_version == 7 && si->is_true_gapless)
266 d->samples = ((si->samples + MPC_FRAME_LENGTH - 1) / MPC_FRAME_LENGTH) * MPC_FRAME_LENGTH;
267 else
268 d->samples = si->samples;
269}
270
271mpc_decoder * mpc_decoder_init(mpc_streaminfo *si)
272{
273 mpc_decoder* p_tmp = &g_mpc_decoder;
274
275 if (p_tmp != 0) {
276 mpc_decoder_setup(p_tmp);
277 mpc_decoder_set_streaminfo(p_tmp, si);
278 huff_init_lut(LUT_DEPTH);
279 }
280
281 return p_tmp;
282}
283
284/* rockbox: not used
285void mpc_decoder_exit(mpc_decoder *d)
286{
287 (void)d;
288}
289*/
290
291void mpc_decoder_decode_frame(mpc_decoder * d,
292 mpc_bits_reader * r,
293 mpc_frame_info * i)
294{
295 mpc_bits_reader r_sav = *r;
296 mpc_int64_t samples_left;
297
298 samples_left = d->samples - d->decoded_samples + MPC_DECODER_SYNTH_DELAY;
299
300 if (samples_left <= 0 && d->samples != 0) {
301 i->samples = 0;
302 i->bits = -1;
303 return;
304 }
305
306 if (d->stream_version == 8) {
307 mpc_decoder_read_bitstream_sv8(d, r, i->is_key_frame);
308 } else {
309 mpc_decoder_read_bitstream_sv7(d, r);
310 }
311
312 if (d->samples_to_skip < MPC_FRAME_LENGTH + MPC_DECODER_SYNTH_DELAY) {
313 mpc_decoder_requantisierung(d);
314 mpc_decoder_synthese_filter_float(d, i->buffer, d->channels);
315 }
316
317 d->decoded_samples += MPC_FRAME_LENGTH;
318
319 // reconstruct exact filelength
320 if (d->decoded_samples - d->samples < MPC_FRAME_LENGTH && d->stream_version == 7) {
321 int last_frame_samples = mpc_bits_read(r, 11);
322 if (d->decoded_samples == d->samples) {
323 if (last_frame_samples == 0) last_frame_samples = MPC_FRAME_LENGTH;
324 d->samples += last_frame_samples - MPC_FRAME_LENGTH;
325 samples_left += last_frame_samples - MPC_FRAME_LENGTH;
326 }
327 }
328
329 i->samples = samples_left > MPC_FRAME_LENGTH ? MPC_FRAME_LENGTH : samples_left < 0 ? 0 : (mpc_uint32_t) samples_left;
330 i->bits = (mpc_uint32_t) (((r->buff - r_sav.buff) << 3) + r_sav.count - r->count);
331
332 if (d->samples_to_skip) {
333 if (i->samples <= d->samples_to_skip) {
334 d->samples_to_skip -= i->samples;
335 i->samples = 0;
336 } else {
337 i->samples -= d->samples_to_skip;
338
339 /* move valid samples to beginning for channel 0. noninterleaved! */
340 memmove(i->buffer,
341 i->buffer + d->samples_to_skip,
342 i->samples * sizeof(MPC_SAMPLE_FORMAT));
343 /* move valid samples to beginning for channel 1. noninterleaved! */
344 memmove(i->buffer + MPC_FRAME_LENGTH,
345 i->buffer + MPC_FRAME_LENGTH + d->samples_to_skip,
346 i->samples * sizeof(MPC_SAMPLE_FORMAT));
347
348 d->samples_to_skip = 0;
349 }
350 }
351}
352
353static void
354mpc_decoder_requantisierung(mpc_decoder *d)
355{
356 mpc_int32_t Band;
357 mpc_int32_t n;
358 MPC_SAMPLE_FORMAT facL;
359 MPC_SAMPLE_FORMAT facR;
360 MPC_SAMPLE_FORMAT templ;
361 MPC_SAMPLE_FORMAT tempr;
362 MPC_SAMPLE_FORMAT* YL;
363 MPC_SAMPLE_FORMAT* YR;
364 mpc_int16_t* L;
365 mpc_int16_t* R;
366 const mpc_int32_t Last_Band = d->max_band;
367
368#ifdef MPC_FIXED_POINT
369#if MPC_FIXED_POINT_FRACTPART == 14
370#define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
371 MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
372#else
373
374#error FIXME, Cc table is in 18.14 format
375
376#endif
377#else
378#define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
379 MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
380#endif
381 // requantization and scaling of subband-samples
382 for ( Band = 0; Band <= Last_Band; Band++ ) { // setting pointers
383 YL = d->Y_L + Band;
384 YR = d->Y_R + Band;
385 L = d->Q[Band].L;
386 R = d->Q[Band].R;
387 /************************** MS-coded **************************/
388 if ( d->MS_Flag [Band] ) {
389 if ( d->Res_L [Band] ) {
390 if ( d->Res_R [Band] ) { // M!=0, S!=0
391 REQUANT_M1_S1(0);
392 REQUANT_M1_S1(1);
393 REQUANT_M1_S1(2);
394 } else { // M!=0, S==0
395 REQUANT_M1_S0(0);
396 REQUANT_M1_S0(1);
397 REQUANT_M1_S0(2);
398 }
399 } else {
400 if ( d->Res_R[Band] ) // M==0, S!=0
401 {
402 REQUANT_M0_S1(0);
403 REQUANT_M0_S1(1);
404 REQUANT_M0_S1(2);
405 } else { // M==0, S==0
406 REQUANT_SILENCE;
407 }
408 }
409 }
410 /************************** LR-coded **************************/
411 else {
412 if ( d->Res_L [Band] ) {
413 if ( d->Res_R [Band] ) { // L!=0, R!=0
414 REQUANT_L1_R1(0);
415 REQUANT_L1_R1(1);
416 REQUANT_L1_R1(2);
417 } else { // L!=0, R==0
418 REQUANT_L1_R0(0);
419 REQUANT_L1_R0(1);
420 REQUANT_L1_R0(2);
421 }
422 }
423 else {
424 if ( d->Res_R [Band] ) { // L==0, R!=0
425 REQUANT_L0_R1(0);
426 REQUANT_L0_R1(1);
427 REQUANT_L0_R1(2);
428 } else { // L==0, R==0
429 REQUANT_SILENCE;
430 }
431 }
432 }
433 }
434}
435
436static void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r)
437{
438 mpc_int32_t n, idx, Max_used_Band = 0;
439
440 /***************************** Header *****************************/
441
442 // first subband
443 d->Res_L[0] = mpc_bits_read(r, 4);
444 d->Res_R[0] = mpc_bits_read(r, 4);
445 if (!(d->Res_L[0] == 0 && d->Res_R[0] == 0)) {
446 if (d->ms)
447 d->MS_Flag[0] = mpc_bits_read(r, 1);
448 Max_used_Band = 1;
449 }
450
451 // consecutive subbands
452 for ( n = 1; n <= d->max_band; n++ ) {
453 idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
454 d->Res_L[n] = (idx!=4) ? d->Res_L[n - 1] + idx : (int) mpc_bits_read(r, 4);
455
456 idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
457 d->Res_R[n] = (idx!=4) ? d->Res_R[n - 1] + idx : (int) mpc_bits_read(r, 4);
458
459 if (!(d->Res_L[n] == 0 && d->Res_R[n] == 0)) {
460 if (d->ms)
461 d->MS_Flag[n] = mpc_bits_read(r, 1);
462 Max_used_Band = n + 1;
463 }
464 }
465
466 /****************************** SCFI ******************************/
467 for ( n = 0; n < Max_used_Band; n++ ) {
468 if (d->Res_L[n])
469 d->SCFI_L[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
470 if (d->Res_R[n])
471 d->SCFI_R[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
472 }
473
474 /**************************** SCF/DSCF ****************************/
475 for ( n = 0; n < Max_used_Band; n++ ) {
476 mpc_int32_t * SCF = d->SCF_Index_L[n];
477 mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
478 do {
479 if (Res) {
480 switch (SCFI) {
481 case 1:
482 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
483 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
484 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
485 SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
486 SCF[2] = SCF[1];
487 break;
488 case 3:
489 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
490 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
491 SCF[1] = SCF[0];
492 SCF[2] = SCF[1];
493 break;
494 case 2:
495 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
496 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
497 SCF[1] = SCF[0];
498 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
499 SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
500 break;
501 case 0:
502 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
503 SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
504 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
505 SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
506 idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
507 SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
508 break;
509 default:
510 return;
511 }
512 if (SCF[0] > 1024)
513 SCF[0] = 0x8080;
514 if (SCF[1] > 1024)
515 SCF[1] = 0x8080;
516 if (SCF[2] > 1024)
517 SCF[2] = 0x8080;
518 }
519 Res = d->Res_R[n];
520 SCFI = d->SCFI_R[n];
521 } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
522 }
523
524// if (d->seeking == TRUE)
525// return;
526
527 /***************************** Samples ****************************/
528 for ( n = 0; n < Max_used_Band; n++ ) {
529 mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
530 do {
531 mpc_uint32_t nbit;
532 mpc_int32_t k, dc;
533 const mpc_lut_data *Table;
534 switch (Res) {
535 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
536 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17: case 0:
537 break;
538 case -1:
539 for (k=0; k<36; k++ ) {
540 mpc_uint32_t tmp = mpc_random_int(d);
541 q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
542 }
543 break;
544 case 1:
545 Table = & mpc_HuffQ[0][mpc_bits_read(r, 1)];
546 for ( k = 0; k < 36; k += 3) {
547 idx = mpc_bits_huff_lut(r, Table);
548 q[k] = g_sv7_idx30[idx];
549 q[k + 1] = g_sv7_idx31[idx];
550 q[k + 2] = g_sv7_idx32[idx];
551 }
552 break;
553 case 2:
554 Table = & mpc_HuffQ[1][mpc_bits_read(r, 1)];
555 for ( k = 0; k < 36; k += 2) {
556 idx = mpc_bits_huff_lut(r, Table);
557 q[k] = g_sv7_idx50[idx];
558 q[k + 1] = g_sv7_idx51[idx];
559 }
560 break;
561 case 3:
562 case 4:
563 case 5:
564 case 6:
565 case 7:
566 Table = & mpc_HuffQ[Res - 1][mpc_bits_read(r, 1)];
567 for ( k = 0; k < 36; k++ )
568 q[k] = mpc_bits_huff_lut(r, Table);
569 break;
570 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
571 nbit = Res_bit[Res];
572 dc = Dc[Res];
573 for ( k = 0; k < 36; k++ )
574 q[k] = (mpc_int32_t)mpc_bits_read(r, nbit) - dc;
575 break;
576 default:
577 return;
578 }
579
580 Res = d->Res_R[n];
581 } while (q == d->Q[n].L && (q = d->Q[n].R));
582 }
583}
584
585static void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r, mpc_bool_t is_key_frame)
586{
587 mpc_int32_t n, Max_used_Band;
588 const mpc_can_data * Table, * Tables[2];
589
590 /***************************** Header *****************************/
591
592 if (is_key_frame == MPC_TRUE) {
593 Max_used_Band = mpc_bits_log_dec(r, d->max_band + 1);
594 } else {
595 Max_used_Band = d->last_max_band + mpc_bits_can_dec(r, & mpc_can_Bands);
596 if (Max_used_Band > 32) Max_used_Band -= 33;
597 }
598 d->last_max_band = Max_used_Band;
599
600 if (Max_used_Band) {
601 d->Res_L[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
602 d->Res_R[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
603 if (d->Res_L[Max_used_Band-1] > 15) d->Res_L[Max_used_Band-1] -= 17;
604 if (d->Res_R[Max_used_Band-1] > 15) d->Res_R[Max_used_Band-1] -= 17;
605 for ( n = Max_used_Band - 2; n >= 0; n--) {
606 d->Res_L[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_L[n + 1] > 2]) + d->Res_L[n + 1];
607 if (d->Res_L[n] > 15) d->Res_L[n] -= 17;
608 d->Res_R[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_R[n + 1] > 2]) + d->Res_R[n + 1];
609 if (d->Res_R[n] > 15) d->Res_R[n] -= 17;
610 }
611
612 if (d->ms) {
613 mpc_uint_t cnt = 0, tot = 0;
614 mpc_uint32_t tmp = 0;
615 for( n = 0; n < Max_used_Band; n++)
616 if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 )
617 tot++;
618 cnt = mpc_bits_log_dec(r, tot);
619 if (cnt != 0 && cnt != tot)
620 tmp = mpc_bits_enum_dec(r, mini(cnt, tot-cnt), tot);
621 if (cnt * 2 > tot) tmp = ~tmp;
622 for( n = Max_used_Band - 1; n >= 0; n--)
623 if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 ) {
624 d->MS_Flag[n] = tmp & 1;
625 tmp >>= 1;
626 }
627 }
628 }
629
630 for( n = Max_used_Band; n <= d->max_band; n++)
631 d->Res_L[n] = d->Res_R[n] = 0;
632
633 /****************************** SCFI ******************************/
634 if (is_key_frame == MPC_TRUE){
635 for( n = 0; n < 32; n++)
636 d->DSCF_Flag_L[n] = d->DSCF_Flag_R[n] = 1; // new block -> force key frame
637 }
638
639 Tables[0] = & mpc_can_SCFI[0];
640 Tables[1] = & mpc_can_SCFI[1];
641 for ( n = 0; n < Max_used_Band; n++ ) {
642 int tmp = 0, cnt = -1;
643 if (d->Res_L[n]) cnt++;
644 if (d->Res_R[n]) cnt++;
645 if (cnt >= 0) {
646 tmp = mpc_bits_can_dec(r, Tables[cnt]);
647 if (d->Res_L[n]) d->SCFI_L[n] = tmp >> (2 * cnt);
648 if (d->Res_R[n]) d->SCFI_R[n] = tmp & 3;
649 }
650 }
651
652 /**************************** SCF/DSCF ****************************/
653
654 for ( n = 0; n < Max_used_Band; n++ ) {
655 mpc_int32_t * SCF = d->SCF_Index_L[n];
656 mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
657 mpc_bool_t * DSCF_Flag = &d->DSCF_Flag_L[n];
658
659 do {
660 if ( Res ) {
661 int m;
662 if (*DSCF_Flag == 1) {
663 SCF[0] = (mpc_int32_t)mpc_bits_read(r, 7) - 6;
664 *DSCF_Flag = 0;
665 } else {
666 mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[1]);
667 if (tmp == 64)
668 tmp += mpc_bits_read(r, 6);
669 SCF[0] = ((SCF[2] - 25 + tmp) & 127) - 6;
670 }
671 for( m = 0; m < 2; m++){
672 if (((SCFI << m) & 2) == 0) {
673 mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[0]);
674 if (tmp == 31)
675 tmp = 64 + mpc_bits_read(r, 6);
676 SCF[m + 1] = ((SCF[m] - 25 + tmp) & 127) - 6;
677 } else
678 SCF[m + 1] = SCF[m];
679 }
680 }
681 Res = d->Res_R[n];
682 SCFI = d->SCFI_R[n];
683 DSCF_Flag = &d->DSCF_Flag_R[n];
684 } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
685 }
686
687 /***************************** Samples ****************************/
688 for ( n = 0; n < Max_used_Band; n++ ) {
689 mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
690 static const mpc_uint32_t thres[] = {0, 0, 3, 0, 0, 1, 3, 4, 8};
691 do {
692 mpc_uint32_t nbit;
693 mpc_uint32_t k = 0, idx = 1, dc;
694 if (Res != 0) {
695 if (Res == 2) {
696 Tables[0] = & mpc_can_Q [0][0];
697 Tables[1] = & mpc_can_Q [0][1];
698 idx = 2 * thres[Res];
699 for ( ; k < 36; k += 3) {
700 int tmp = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
701 q[k] = g_sv8_idx50[tmp];
702 q[k + 1] = g_sv8_idx51[tmp];
703 q[k + 2] = g_sv8_idx52[tmp];
704 idx = (idx >> 1) + g_sv8_HuffQ2_var[tmp];
705 }
706 } else if (Res == 1) {
707 Table = & mpc_can_Q1;
708 for( ; k < 36; ){
709 mpc_uint32_t kmax = k + 18;
710 mpc_uint_t cnt = mpc_bits_can_dec(r, Table);
711 idx = 0;
712 if (cnt > 0 && cnt < 18)
713 idx = mpc_bits_enum_dec(r, cnt <= 9 ? cnt : 18 - cnt, 18);
714 if (cnt > 9) idx = ~idx;
715 for ( ; k < kmax; k++) {
716 q[k] = 0;
717 if ( idx & (1 << 17) )
718 q[k] = (mpc_bits_read(r, 1) << 1) - 1;
719 idx <<= 1;
720 }
721 }
722 } else if (Res == -1) {
723 for ( ; k<36; k++ ) {
724 mpc_uint32_t tmp = mpc_random_int(d);
725 q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
726 }
727 } else if (Res <= 4) {
728 Table = & mpc_can_Q[1][Res - 3];
729 for ( ; k < 36; k += 2 ) {
730 union {
731 mpc_int8_t sym;
732 struct { mpc_int8_t s1:4, s2:4; };
733 } tmp;
734 tmp.sym = mpc_bits_can_dec(r, Table);
735 q[k] = tmp.s1;
736 q[k + 1] = tmp.s2;
737 }
738 } else if (Res <= 8) {
739 Tables[0] = & mpc_can_Q [Res - 3][0];
740 Tables[1] = & mpc_can_Q [Res - 3][1];
741 idx = 2 * thres[Res];
742 for ( ; k < 36; k++ ) {
743 q[k] = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
744 idx = (idx >> 1) + absi(q[k]);
745 }
746 } else if (Res == 9) {
747 dc = Dc[Res];
748 for ( ; k < 36; k++ ) {
749 q[k] = (unsigned char) mpc_bits_can_dec(r, & mpc_can_Q9up);
750 q[k] -= dc;
751 }
752 } else {
753 nbit = (Res - 9);
754 dc = Dc[Res];
755 for ( ; k < 36; k++ ) {
756 q[k] = (unsigned char) mpc_bits_can_dec(r, & mpc_can_Q9up);
757 q[k] = (q[k] << nbit) | mpc_bits_read(r, nbit);
758 q[k] -= dc;
759 }
760 }
761 }
762
763 Res = d->Res_R[n];
764 } while (q == d->Q[n].L && (q = d->Q[n].R));
765 }
766}
767