~ubuntu-branches/ubuntu/precise/libmpc/precise

« back to all changes in this revision

Viewing changes to libmpcdec/mpc_decoder.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2009-03-04 10:08:01 UTC
  • Revision ID: james.westby@ubuntu.com-20090304100801-k5vtj8jz06nomhlu
Tags: upstream-0.1~r435
Import upstream version 0.1~r435

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <mpc/mpcdec.h>
 
39
#include <mpc/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"
 
46
 
 
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;
 
52
 
 
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;
 
61
 
 
62
//------------------------------------------------------------------------------
 
63
// types
 
64
//------------------------------------------------------------------------------
 
65
enum
 
66
{
 
67
    MEMSIZE   = MPC_DECODER_MEMSIZE, // overall buffer size
 
68
    MEMSIZE2  = (MEMSIZE/2),         // size of one buffer
 
69
    MEMMASK   = (MEMSIZE-1)
 
70
};
 
71
 
 
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);
 
79
 
 
80
/**
 
81
 * set the scf indexes for seeking use
 
82
 * needed only for sv7 seeking
 
83
 * @param d
 
84
 */
 
85
void mpc_decoder_reset_scf(mpc_decoder * d, int value)
 
86
{
 
87
        memset(d->SCF_Index_L, value, sizeof d->SCF_Index_L );
 
88
        memset(d->SCF_Index_R, value, sizeof d->SCF_Index_R );
 
89
}
 
90
 
 
91
 
 
92
void mpc_decoder_setup(mpc_decoder *d)
 
93
{
 
94
        memset(d, 0, sizeof *d);
 
95
 
 
96
        d->__r1 = 1;
 
97
        d->__r2 = 1;
 
98
 
 
99
        mpc_decoder_init_quant(d, 1.0f);
 
100
}
 
101
 
 
102
void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
 
103
{
 
104
        d->stream_version     = si->stream_version;
 
105
        d->ms                 = si->ms;
 
106
        d->max_band           = si->max_band;
 
107
        d->channels           = si->channels;
 
108
        d->samples_to_skip    = MPC_DECODER_SYNTH_DELAY + si->beg_silence;
 
109
 
 
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;
 
112
        else
 
113
                d->samples = si->samples;
 
114
}
 
115
 
 
116
mpc_decoder * mpc_decoder_init(mpc_streaminfo *si)
 
117
{
 
118
        mpc_decoder* p_tmp = malloc(sizeof(mpc_decoder));
 
119
 
 
120
        if (p_tmp != 0) {
 
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
 
124
        }
 
125
 
 
126
        return p_tmp;
 
127
}
 
128
 
 
129
void mpc_decoder_exit(mpc_decoder *d)
 
130
{
 
131
        free(d);
 
132
}
 
133
 
 
134
void mpc_decoder_decode_frame(mpc_decoder * d,
 
135
                                                          mpc_bits_reader * r,
 
136
                                                          mpc_frame_info * i)
 
137
{
 
138
        mpc_bits_reader r_sav = *r;
 
139
        mpc_int64_t samples_left;
 
140
 
 
141
        samples_left = d->samples - d->decoded_samples + MPC_DECODER_SYNTH_DELAY;
 
142
 
 
143
        if (samples_left <= 0 && d->samples != 0) {
 
144
                i->samples = 0;
 
145
                i->bits = -1;
 
146
                return;
 
147
        }
 
148
 
 
149
        if (d->stream_version == 8)
 
150
                mpc_decoder_read_bitstream_sv8(d, r, i->is_key_frame);
 
151
        else
 
152
                mpc_decoder_read_bitstream_sv7(d, r);
 
153
 
 
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);
 
157
        }
 
158
 
 
159
        d->decoded_samples += MPC_FRAME_LENGTH;
 
160
 
 
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;
 
168
                }
 
169
        }
 
170
 
 
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);
 
173
 
 
174
        if (d->samples_to_skip) {
 
175
                if (i->samples <= d->samples_to_skip) {
 
176
                        d->samples_to_skip -= i->samples;
 
177
                        i->samples = 0;
 
178
                } else {
 
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;
 
183
                }
 
184
        }
 
185
}
 
186
 
 
187
void
 
188
mpc_decoder_requantisierung(mpc_decoder *d)
 
189
{
 
190
    mpc_int32_t     Band;
 
191
    mpc_int32_t     n;
 
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;
 
198
    mpc_int16_t*    L;
 
199
    mpc_int16_t*    R;
 
200
        const mpc_int32_t Last_Band = d->max_band;
 
201
 
 
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])
 
206
#else
 
207
 
 
208
#error FIXME, Cc table is in 18.14 format
 
209
 
 
210
#endif
 
211
#else
 
212
#define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
 
213
    MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
 
214
#endif
 
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;
 
219
        L  = d->Q[Band].L;
 
220
        R  = d->Q[Band].R;
 
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++));
 
229
                        *YR   = templ - tempr;
 
230
                    }
 
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++));
 
235
                        *YR   = templ - tempr;
 
236
                    }
 
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++));
 
241
                        *YR   = templ - tempr;
 
242
                    }
 
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++);
 
247
                    }
 
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++);
 
251
                    }
 
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++);
 
255
                    }
 
256
                }
 
257
            } else {
 
258
                if (d->Res_R[Band])    // M==0, S!=0
 
259
                {
 
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++)));
 
263
                    }
 
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++)));
 
267
                    }
 
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++)));
 
271
                    }
 
272
                } else {    // M==0, S==0
 
273
                    for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
 
274
                        *YR = *YL = 0;
 
275
                    }
 
276
                }
 
277
            }
 
278
        }
 
279
        /************************** LR-coded **************************/
 
280
        else {
 
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++);
 
288
                    }
 
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++);
 
294
                    }
 
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++);
 
300
                    }
 
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++);
 
305
                        *YR = 0;
 
306
                    }
 
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++);
 
310
                        *YR = 0;
 
311
                    }
 
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++);
 
315
                        *YR = 0;
 
316
                    }
 
317
                }
 
318
            }
 
319
            else {
 
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 ) {
 
323
                        *YL = 0;
 
324
                        *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
 
325
                    }
 
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 ) {
 
328
                        *YL = 0;
 
329
                        *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
 
330
                    }
 
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 ) {
 
333
                        *YL = 0;
 
334
                        *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
 
335
                    }
 
336
                } else {    // L==0, R==0
 
337
                    for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
 
338
                        *YR = *YL = 0;
 
339
                    }
 
340
                }
 
341
            }
 
342
        }
 
343
    }
 
344
}
 
345
 
 
346
void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r)
 
347
{
 
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};
 
354
 
 
355
    mpc_int32_t n, idx, Max_used_Band = 0;
 
356
 
 
357
    /***************************** Header *****************************/
 
358
 
 
359
    // first subband
 
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)) {
 
363
                if (d->ms)
 
364
                d->MS_Flag[0] = mpc_bits_read(r, 1);
 
365
                Max_used_Band = 1;
 
366
        }
 
367
 
 
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);
 
372
 
 
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);
 
375
 
 
376
                if (!(d->Res_L[n] == 0 && d->Res_R[n] == 0)) {
 
377
                        if (d->ms)
 
378
                d->MS_Flag[n] = mpc_bits_read(r, 1);
 
379
                        Max_used_Band = n + 1;
 
380
                }
 
381
    }
 
382
 
 
383
    /****************************** SCFI ******************************/
 
384
    for ( n = 0; n < Max_used_Band; n++ ) {
 
385
                if (d->Res_L[n])
 
386
                        d->SCFI_L[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
 
387
                if (d->Res_R[n])
 
388
                        d->SCFI_R[n] = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
 
389
    }
 
390
 
 
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];
 
395
                do {
 
396
                        if (Res) {
 
397
                                switch (SCFI) {
 
398
                                        case 1:
 
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);
 
403
                                                SCF[2] = SCF[1];
 
404
                                                break;
 
405
                                        case 3:
 
406
                                                idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 
407
                                                SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
 
408
                                                SCF[1] = SCF[0];
 
409
                                                SCF[2] = SCF[1];
 
410
                                                break;
 
411
                                        case 2:
 
412
                                                idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 
413
                                                SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
 
414
                                                SCF[1] = SCF[0];
 
415
                                                idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 
416
                                                SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
 
417
                                                break;
 
418
                                        case 0:
 
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);
 
425
                                                break;
 
426
                                        default:
 
427
                                                return;
 
428
                                }
 
429
                                if (SCF[0] > 1024)
 
430
                                        SCF[0] = 0x8080;
 
431
                                if (SCF[1] > 1024)
 
432
                                        SCF[1] = 0x8080;
 
433
                                if (SCF[2] > 1024)
 
434
                                        SCF[2] = 0x8080;
 
435
                        }
 
436
                        Res = d->Res_R[n];
 
437
                        SCFI = d->SCFI_R[n];
 
438
                } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
 
439
    }
 
440
 
 
441
//     if (d->seeking == TRUE)
 
442
//         return;
 
443
 
 
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];
 
447
                do {
 
448
                        mpc_int32_t k;
 
449
                        const mpc_lut_data *Table;
 
450
                        switch (Res) {
 
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:
 
453
                                        break;
 
454
                                case -1:
 
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;
 
458
                                        }
 
459
                                        break;
 
460
                                case 1:
 
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);
 
464
                                                q[k] = idx30[idx];
 
465
                                                q[k + 1] = idx31[idx];
 
466
                                                q[k + 2] = idx32[idx];
 
467
                                        }
 
468
                                        break;
 
469
                                case 2:
 
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);
 
473
                                                q[k] = idx50[idx];
 
474
                                                q[k + 1] = idx51[idx];
 
475
                                        }
 
476
                                        break;
 
477
                                case 3:
 
478
                                case 4:
 
479
                                case 5:
 
480
                                case 6:
 
481
                                case 7:
 
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);
 
485
                                        break;
 
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];
 
489
                                        break;
 
490
                                default:
 
491
                                        return;
 
492
                        }
 
493
 
 
494
                        Res = d->Res_R[n];
 
495
                } while (q == d->Q[n].L && (q = d->Q[n].R));
 
496
    }
 
497
}
 
498
 
 
499
void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r, mpc_bool_t is_key_frame)
 
500
{
 
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};
 
505
 
 
506
        mpc_int32_t n, Max_used_Band;
 
507
        const mpc_can_data * Table, * Tables[2];
 
508
 
 
509
        /***************************** Header *****************************/
 
510
 
 
511
        if (is_key_frame == MPC_TRUE) {
 
512
                Max_used_Band = mpc_bits_log_dec(r, d->max_band + 1);
 
513
        } else {
 
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;
 
516
        }
 
517
        d->last_max_band = Max_used_Band;
 
518
 
 
519
        if (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;
 
529
                }
 
530
 
 
531
                if (d->ms) {
 
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 )
 
536
                                        tot++;
 
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;
 
544
                                        tmp >>= 1;
 
545
                                }
 
546
                }
 
547
        }
 
548
 
 
549
        for( n = Max_used_Band; n <= d->max_band; n++)
 
550
                d->Res_L[n] = d->Res_R[n] = 0;
 
551
 
 
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
 
556
        }
 
557
 
 
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++;
 
564
                if (cnt >= 0) {
 
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;
 
568
                }
 
569
        }
 
570
 
 
571
        /**************************** SCF/DSCF ****************************/
 
572
 
 
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];
 
577
 
 
578
                do {
 
579
                        if ( Res ) {
 
580
                                int m;
 
581
                                if (*DSCF_Flag == 1) {
 
582
                                        SCF[0] = (mpc_int32_t)mpc_bits_read(r, 7) - 6;
 
583
                                        *DSCF_Flag = 0;
 
584
                                } else {
 
585
                                        mpc_uint_t tmp = mpc_bits_can_dec(r, & mpc_can_DSCF[1]);
 
586
                                        if (tmp == 64)
 
587
                                                tmp += mpc_bits_read(r, 6);
 
588
                                        SCF[0] = ((SCF[2] - 25 + tmp) & 127) - 6;
 
589
                                }
 
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]);
 
593
                                                if (tmp == 31)
 
594
                                                        tmp = 64 + mpc_bits_read(r, 6);
 
595
                                                SCF[m + 1] = ((SCF[m] - 25 + tmp) & 127) - 6;
 
596
                                        } else
 
597
                                                SCF[m + 1] = SCF[m];
 
598
                                }
 
599
                        }
 
600
                        Res = d->Res_R[n];
 
601
                        SCFI = d->SCFI_R[n];
 
602
                        DSCF_Flag = &d->DSCF_Flag_R[n];
 
603
                } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
 
604
        }
 
605
 
 
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};
 
612
 
 
613
                do {
 
614
                        mpc_int32_t k = 0, idx = 1;
 
615
                        if (Res != 0) {
 
616
                                if (Res == 2) {
 
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]]);
 
622
                                                q[k] = idx50[tmp];
 
623
                                                q[k + 1] = idx51[tmp];
 
624
                                                q[k + 2] = idx52[tmp];
 
625
                                                idx = (idx >> 1) + HuffQ2_var[tmp];
 
626
                                        }
 
627
                                } else if (Res == 1) {
 
628
                                        Table = & mpc_can_Q1;
 
629
                                        for( ; k < 36; ){
 
630
                                                int kmax = k + 18;
 
631
                                                mpc_uint_t cnt = mpc_bits_can_dec(r, Table);
 
632
                                                idx = 0;
 
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++) {
 
637
                                                        q[k] = 0;
 
638
                                                        if ( idx & (1 << 17) )
 
639
                                                                q[k] = (mpc_bits_read(r, 1) << 1) - 1;
 
640
                                                        idx <<= 1;
 
641
                                                }
 
642
                                        }
 
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;
 
647
                                        }
 
648
                                } else if (Res <= 4) {
 
649
                                        Table = & mpc_can_Q[1][Res - 3];
 
650
                                        for ( ; k < 36; k += 2 ) {
 
651
                                                union {
 
652
                                                        mpc_int8_t sym;
 
653
                                                        struct { mpc_int8_t s1:4, s2:4; };
 
654
                                                } tmp;
 
655
                                                tmp.sym = mpc_bits_can_dec(r, Table);
 
656
                                                q[k] = tmp.s1;
 
657
                                                q[k + 1] = tmp.s2;
 
658
                                        }
 
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]);
 
666
                                        }
 
667
                                } else {
 
668
                                        for ( ; k < 36; k++ ) {
 
669
                                                q[k] = (unsigned char) mpc_bits_can_dec(r, & mpc_can_Q9up);
 
670
                                                if (Res != 9)
 
671
                                                        q[k] = (q[k] << (Res - 9)) | mpc_bits_read(r, Res - 9);
 
672
                                                q[k] -= Dc[Res];
 
673
                                        }
 
674
                                }
 
675
                        }
 
676
 
 
677
                        Res = d->Res_R[n];
 
678
                } while (q == d->Q[n].L && (q = d->Q[n].R));
 
679
        }
 
680
}
 
681