2
Copyright (c) 2005-2009, The Musepack Development Team
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are
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
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.
38
#include <mpc/mpcdec.h>
39
#include <mpc/minimax.h>
43
#include "mpcdec_math.h"
45
#include "mpc_bits_reader.h"
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;
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
//------------------------------------------------------------------------------
64
//------------------------------------------------------------------------------
67
MEMSIZE = MPC_DECODER_MEMSIZE, // overall buffer size
68
MEMSIZE2 = (MEMSIZE/2), // size of one buffer
72
//------------------------------------------------------------------------------
73
// forward declarations
74
//------------------------------------------------------------------------------
75
void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r);
76
void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r,
77
mpc_bool_t is_key_frame);
78
static void mpc_decoder_requantisierung(mpc_decoder *d);
81
* set the scf indexes for seeking use
82
* needed only for sv7 seeking
85
void mpc_decoder_reset_scf(mpc_decoder * d, int value)
87
memset(d->SCF_Index_L, value, sizeof d->SCF_Index_L );
88
memset(d->SCF_Index_R, value, sizeof d->SCF_Index_R );
92
void mpc_decoder_setup(mpc_decoder *d)
94
memset(d, 0, sizeof *d);
99
mpc_decoder_init_quant(d, 1.0f);
102
void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
104
d->stream_version = si->stream_version;
106
d->max_band = si->max_band;
107
d->channels = si->channels;
108
d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + si->beg_silence;
110
if (si->stream_version == 7 && si->is_true_gapless)
111
d->samples = ((si->samples + MPC_FRAME_LENGTH - 1) / MPC_FRAME_LENGTH) * MPC_FRAME_LENGTH;
113
d->samples = si->samples;
116
mpc_decoder * mpc_decoder_init(mpc_streaminfo *si)
118
mpc_decoder* p_tmp = malloc(sizeof(mpc_decoder));
121
mpc_decoder_setup(p_tmp);
122
mpc_decoder_set_streaminfo(p_tmp, si);
123
huff_init_lut(LUT_DEPTH); // FIXME : this needs to be called only once when the library is loaded
129
void mpc_decoder_exit(mpc_decoder *d)
134
void mpc_decoder_decode_frame(mpc_decoder * d,
138
mpc_bits_reader r_sav = *r;
139
mpc_int64_t samples_left;
141
samples_left = d->samples - d->decoded_samples + MPC_DECODER_SYNTH_DELAY;
143
if (samples_left <= 0 && d->samples != 0) {
149
if (d->stream_version == 8)
150
mpc_decoder_read_bitstream_sv8(d, r, i->is_key_frame);
152
mpc_decoder_read_bitstream_sv7(d, r);
154
if (d->samples_to_skip < MPC_FRAME_LENGTH + MPC_DECODER_SYNTH_DELAY) {
155
mpc_decoder_requantisierung(d);
156
mpc_decoder_synthese_filter_float(d, i->buffer, d->channels);
159
d->decoded_samples += MPC_FRAME_LENGTH;
161
// reconstruct exact filelength
162
if (d->decoded_samples - d->samples < MPC_FRAME_LENGTH && d->stream_version == 7) {
163
int last_frame_samples = mpc_bits_read(r, 11);
164
if (d->decoded_samples == d->samples) {
165
if (last_frame_samples == 0) last_frame_samples = MPC_FRAME_LENGTH;
166
d->samples += last_frame_samples - MPC_FRAME_LENGTH;
167
samples_left += last_frame_samples - MPC_FRAME_LENGTH;
171
i->samples = samples_left > MPC_FRAME_LENGTH ? MPC_FRAME_LENGTH : samples_left < 0 ? 0 : (mpc_uint32_t) samples_left;
172
i->bits = (mpc_uint32_t) (((r->buff - r_sav.buff) << 3) + r_sav.count - r->count);
174
if (d->samples_to_skip) {
175
if (i->samples <= d->samples_to_skip) {
176
d->samples_to_skip -= i->samples;
179
i->samples -= d->samples_to_skip;
180
memmove(i->buffer, i->buffer + d->samples_to_skip * d->channels,
181
i->samples * d->channels * sizeof (MPC_SAMPLE_FORMAT));
182
d->samples_to_skip = 0;
188
mpc_decoder_requantisierung(mpc_decoder *d)
192
MPC_SAMPLE_FORMAT facL;
193
MPC_SAMPLE_FORMAT facR;
194
MPC_SAMPLE_FORMAT templ;
195
MPC_SAMPLE_FORMAT tempr;
196
MPC_SAMPLE_FORMAT* YL;
197
MPC_SAMPLE_FORMAT* YR;
200
const mpc_int32_t Last_Band = d->max_band;
202
#ifdef MPC_FIXED_POINT
203
#if MPC_FIXED_POINT_FRACTPART == 14
204
#define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
205
MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
208
#error FIXME, Cc table is in 18.14 format
212
#define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
213
MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
215
// requantization and scaling of subband-samples
216
for ( Band = 0; Band <= Last_Band; Band++ ) { // setting pointers
217
YL = d->Y_L[0] + Band;
218
YR = d->Y_R[0] + Band;
221
/************************** MS-coded **************************/
222
if ( d->MS_Flag [Band] ) {
223
if ( d->Res_L [Band] ) {
224
if ( d->Res_R [Band] ) { // M!=0, S!=0
225
facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][0] & 0xFF);
226
facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
227
for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
228
*YL = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
231
facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
232
facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
233
for ( ; n < 24; n++, YL += 32, YR += 32 ) {
234
*YL = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
237
facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
238
facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
239
for ( ; n < 36; n++, YL += 32, YR += 32 ) {
240
*YL = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
243
} else { // M!=0, S==0
244
facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][0] & 0xFF);
245
for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
246
*YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
248
facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
249
for ( ; n < 24; n++, YL += 32, YR += 32 ) {
250
*YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
252
facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
253
for ( ; n < 36; n++, YL += 32, YR += 32 ) {
254
*YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
258
if (d->Res_R[Band]) // M==0, S!=0
260
facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
261
for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
262
*YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
264
facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
265
for ( ; n < 24; n++, YL += 32, YR += 32 ) {
266
*YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
268
facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
269
for ( ; n < 36; n++, YL += 32, YR += 32 ) {
270
*YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
272
} else { // M==0, S==0
273
for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
279
/************************** LR-coded **************************/
281
if ( d->Res_L [Band] ) {
282
if ( d->Res_R [Band] ) { // L!=0, R!=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 = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
287
*YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
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 = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
293
*YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
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 = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
299
*YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
301
} else { // L!=0, R==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
*YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
307
facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
308
for ( ; n < 24; n++, YL += 32, YR += 32 ) {
309
*YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
312
facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
313
for ( ; n < 36; n++, YL += 32, YR += 32 ) {
314
*YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
320
if ( d->Res_R [Band] ) { // L==0, R!=0
321
facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
322
for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
324
*YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
326
facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
327
for ( ; n < 24; n++, YL += 32, YR += 32 ) {
329
*YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
331
facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
332
for ( ; n < 36; n++, YL += 32, YR += 32 ) {
334
*YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
336
} else { // L==0, R==0
337
for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
346
void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r)
348
// these arrays hold decoding results for bundled quantizers (3- and 5-step)
349
static const mpc_int32_t idx30[] = { -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};
350
static const mpc_int32_t idx31[] = { -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};
351
static const mpc_int32_t idx32[] = { -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};
352
static const mpc_int32_t idx50[] = { -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};
353
static const mpc_int32_t idx51[] = { -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};
355
mpc_int32_t n, idx, Max_used_Band = 0;
357
/***************************** Header *****************************/
360
d->Res_L[0] = mpc_bits_read(r, 4);
361
d->Res_R[0] = mpc_bits_read(r, 4);
362
if (!(d->Res_L[0] == 0 && d->Res_R[0] == 0)) {
364
d->MS_Flag[0] = mpc_bits_read(r, 1);
368
// consecutive subbands
369
for ( n = 1; n <= d->max_band; n++ ) {
370
idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
371
d->Res_L[n] = (idx!=4) ? d->Res_L[n - 1] + idx : (int) mpc_bits_read(r, 4);
373
idx = mpc_bits_huff_lut(r, & mpc_HuffHdr);
374
d->Res_R[n] = (idx!=4) ? d->Res_R[n - 1] + idx : (int) mpc_bits_read(r, 4);
376
if (!(d->Res_L[n] == 0 && d->Res_R[n] == 0)) {
378
d->MS_Flag[n] = mpc_bits_read(r, 1);
379
Max_used_Band = n + 1;
383
/****************************** SCFI ******************************/
384
for ( n = 0; n < Max_used_Band; n++ ) {
386
d->SCFI_L[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
388
d->SCFI_R[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
391
/**************************** SCF/DSCF ****************************/
392
for ( n = 0; n < Max_used_Band; n++ ) {
393
mpc_int32_t * SCF = d->SCF_Index_L[n];
394
mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
399
idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
400
SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
401
idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
402
SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
406
idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
407
SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
412
idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
413
SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
415
idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
416
SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
419
idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
420
SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
421
idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
422
SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
423
idx = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
424
SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
438
} while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
441
// if (d->seeking == TRUE)
444
/***************************** Samples ****************************/
445
for ( n = 0; n < Max_used_Band; n++ ) {
446
mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
449
const mpc_lut_data *Table;
451
case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
452
case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17: case 0:
455
for (k=0; k<36; k++ ) {
456
mpc_uint32_t tmp = mpc_random_int(d);
457
q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
461
Table = & mpc_HuffQ[0][mpc_bits_read(r, 1)];
462
for ( k = 0; k < 36; k += 3) {
463
idx = mpc_bits_huff_lut(r, Table);
465
q[k + 1] = idx31[idx];
466
q[k + 2] = idx32[idx];
470
Table = & mpc_HuffQ[1][mpc_bits_read(r, 1)];
471
for ( k = 0; k < 36; k += 2) {
472
idx = mpc_bits_huff_lut(r, Table);
474
q[k + 1] = idx51[idx];
482
Table = & mpc_HuffQ[Res - 1][mpc_bits_read(r, 1)];
483
for ( k = 0; k < 36; k++ )
484
q[k] = mpc_bits_huff_lut(r, Table);
486
case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
487
for ( k = 0; k < 36; k++ )
488
q[k] = (mpc_int32_t)mpc_bits_read(r, Res_bit[Res]) - Dc[Res];
495
} while (q == d->Q[n].L && (q = d->Q[n].R));
499
void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r, mpc_bool_t is_key_frame)
501
// these arrays hold decoding results for bundled quantizers (3- and 5-step)
502
static const mpc_int8_t idx50[125] = {-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, -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, -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, -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, -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};
503
static const mpc_int8_t idx51[125] = {-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, -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, -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, -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, -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};
504
static const mpc_int8_t idx52[125] = {-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, -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, 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, 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, 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};
506
mpc_int32_t n, Max_used_Band;
507
const mpc_can_data * Table, * Tables[2];
509
/***************************** Header *****************************/
511
if (is_key_frame == MPC_TRUE) {
512
Max_used_Band = mpc_bits_log_dec(r, d->max_band + 1);
514
Max_used_Band = d->last_max_band + mpc_bits_can_dec(r, & mpc_can_Bands);
515
if (Max_used_Band > 32) Max_used_Band -= 33;
517
d->last_max_band = Max_used_Band;
520
d->Res_L[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
521
d->Res_R[Max_used_Band-1] = mpc_bits_can_dec(r, & mpc_can_Res[0]);
522
if (d->Res_L[Max_used_Band-1] > 15) d->Res_L[Max_used_Band-1] -= 17;
523
if (d->Res_R[Max_used_Band-1] > 15) d->Res_R[Max_used_Band-1] -= 17;
524
for ( n = Max_used_Band - 2; n >= 0; n--) {
525
d->Res_L[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_L[n + 1] > 2]) + d->Res_L[n + 1];
526
if (d->Res_L[n] > 15) d->Res_L[n] -= 17;
527
d->Res_R[n] = mpc_bits_can_dec(r, & mpc_can_Res[d->Res_R[n + 1] > 2]) + d->Res_R[n + 1];
528
if (d->Res_R[n] > 15) d->Res_R[n] -= 17;
532
int cnt = 0, tot = 0;
533
mpc_uint32_t tmp = 0;
534
for( n = 0; n < Max_used_Band; n++)
535
if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 )
537
cnt = mpc_bits_log_dec(r, tot);
538
if (cnt != 0 && cnt != tot)
539
tmp = mpc_bits_enum_dec(r, mini(cnt, tot-cnt), tot);
540
if (cnt * 2 > tot) tmp = ~tmp;
541
for( n = Max_used_Band - 1; n >= 0; n--)
542
if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 ) {
543
d->MS_Flag[n] = tmp & 1;
549
for( n = Max_used_Band; n <= d->max_band; n++)
550
d->Res_L[n] = d->Res_R[n] = 0;
552
/****************************** SCFI ******************************/
553
if (is_key_frame == MPC_TRUE){
554
for( n = 0; n < 32; n++)
555
d->DSCF_Flag_L[n] = d->DSCF_Flag_R[n] = 1; // new block -> force key frame
558
Tables[0] = & mpc_can_SCFI[0];
559
Tables[1] = & mpc_can_SCFI[1];
560
for ( n = 0; n < Max_used_Band; n++ ) {
561
int tmp = 0, cnt = -1;
562
if (d->Res_L[n]) cnt++;
563
if (d->Res_R[n]) cnt++;
565
tmp = mpc_bits_can_dec(r, Tables[cnt]);
566
if (d->Res_L[n]) d->SCFI_L[n] = tmp >> (2 * cnt);
567
if (d->Res_R[n]) d->SCFI_R[n] = tmp & 3;
571
/**************************** SCF/DSCF ****************************/
573
for ( n = 0; n < Max_used_Band; n++ ) {
574
mpc_int32_t * SCF = d->SCF_Index_L[n];
575
mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
576
mpc_bool_t * DSCF_Flag = &d->DSCF_Flag_L[n];
581
if (*DSCF_Flag == 1) {
582
SCF[0] = (mpc_int32_t)mpc_bits_read(r, 7) - 6;
585
mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[1]);
587
tmp += mpc_bits_read(r, 6);
588
SCF[0] = ((SCF[2] - 25 + tmp) & 127) - 6;
590
for( m = 0; m < 2; m++){
591
if (((SCFI << m) & 2) == 0) {
592
mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[0]);
594
tmp = 64 + mpc_bits_read(r, 6);
595
SCF[m + 1] = ((SCF[m] - 25 + tmp) & 127) - 6;
602
DSCF_Flag = &d->DSCF_Flag_R[n];
603
} while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
606
/***************************** Samples ****************************/
607
for ( n = 0; n < Max_used_Band; n++ ) {
608
mpc_int16_t *q = d->Q[n].L, Res = d->Res_L[n];
609
static const unsigned int thres[] = {0, 0, 3, 0, 0, 1, 3, 4, 8};
610
static const mpc_int8_t HuffQ2_var[5*5*5] =
611
{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, 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, 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, 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, 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};
614
mpc_int32_t k = 0, idx = 1;
617
Tables[0] = & mpc_can_Q [0][0];
618
Tables[1] = & mpc_can_Q [0][1];
619
idx = 2 * thres[Res];
620
for ( ; k < 36; k += 3) {
621
int tmp = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
623
q[k + 1] = idx51[tmp];
624
q[k + 2] = idx52[tmp];
625
idx = (idx >> 1) + HuffQ2_var[tmp];
627
} else if (Res == 1) {
628
Table = & mpc_can_Q1;
631
mpc_uint_t cnt = mpc_bits_can_dec(r, Table);
633
if (cnt > 0 && cnt < 18)
634
idx = mpc_bits_enum_dec(r, cnt <= 9 ? cnt : 18 - cnt, 18);
635
if (cnt > 9) idx = ~idx;
636
for ( ; k < kmax; k++) {
638
if ( idx & (1 << 17) )
639
q[k] = (mpc_bits_read(r, 1) << 1) - 1;
643
} else if (Res == -1) {
644
for ( ; k<36; k++ ) {
645
mpc_uint32_t tmp = mpc_random_int(d);
646
q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
648
} else if (Res <= 4) {
649
Table = & mpc_can_Q[1][Res - 3];
650
for ( ; k < 36; k += 2 ) {
653
struct { mpc_int8_t s1:4, s2:4; };
655
tmp.sym = mpc_bits_can_dec(r, Table);
659
} else if (Res <= 8) {
660
Tables[0] = & mpc_can_Q [Res - 3][0];
661
Tables[1] = & mpc_can_Q [Res - 3][1];
662
idx = 2 * thres[Res];
663
for ( ; k < 36; k++ ) {
664
q[k] = mpc_bits_can_dec(r, Tables[idx > thres[Res]]);
665
idx = (idx >> 1) + absi(q[k]);
668
for ( ; k < 36; k++ ) {
669
q[k] = (unsigned char) mpc_bits_can_dec(r, & mpc_can_Q9up);
671
q[k] = (q[k] << (Res - 9)) | mpc_bits_read(r, Res - 9);
678
} while (q == d->Q[n].L && (q = d->Q[n].R));