Revision 5aefe3eb libavcodec/ac3dec.c

View differences:

libavcodec/ac3dec.c
52 52
/** table for grouping exponents */
53 53
static uint8_t exp_ungroup_tbl[128][3];
54 54

  
55
static int16_t l3_quantizers_1[32];
56
static int16_t l3_quantizers_2[32];
57
static int16_t l3_quantizers_3[32];
58 55

  
59
static int16_t l5_quantizers_1[128];
60
static int16_t l5_quantizers_2[128];
61
static int16_t l5_quantizers_3[128];
56
/** tables for ungrouping mantissas */
57
static float b1_mantissas[32][3];
58
static float b2_mantissas[128][3];
59
static float b3_mantissas[8];
60
static float b4_mantissas[128][2];
61
static float b5_mantissas[16];
62 62

  
63
static int16_t l7_quantizers[7];
64

  
65
static int16_t l11_quantizers_1[128];
66
static int16_t l11_quantizers_2[128];
67

  
68
static int16_t l15_quantizers[15];
69

  
70
static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 };
63
/**
64
 * Quantization table: levels for symmetric. bits for asymmetric.
65
 * reference: Table 7.18 Mapping of bap to Quantizer
66
 */
67
static const uint8_t qntztab[16] = {
68
    0, 3, 5, 7, 11, 15,
69
    5, 6, 7, 8, 9, 10, 11, 12, 14, 16
70
};
71 71

  
72 72
/* Adjustmens in dB gain */
73 73
#define LEVEL_MINUS_3DB         0.7071067811865476
......
193 193
       window[i] = sqrt(local_window[i] / sum);
194 194
}
195 195

  
196
/*
197
 * Generate quantizer tables.
198
 */
199
static void generate_quantizers_table(int16_t quantizers[], int level, int length)
196
static inline float
197
symmetric_dequant(int code, int levels)
200 198
{
201
    int i;
202

  
203
    for (i = 0; i < length; i++)
204
        quantizers[i] = ((2 * i - level + 1) << 15) / level;
205
}
206

  
207
static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
208
{
209
    int i, j;
210
    int16_t v;
211

  
212
    for (i = 0; i < length1; i++) {
213
        v = ((2 * i - level + 1) << 15) / level;
214
        for (j = 0; j < length2; j++)
215
            quantizers[i * length2 + j] = v;
216
    }
217

  
218
    for (i = length1 * length2; i < size; i++)
219
        quantizers[i] = 0;
220
}
221

  
222
static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
223
{
224
    int i, j;
225
    int16_t v;
226

  
227
    for (i = 0; i < length1; i++) {
228
        v = ((2 * (i % level) - level + 1) << 15) / level;
229
        for (j = 0; j < length2; j++)
230
            quantizers[i * length2 + j] = v;
231
    }
232

  
233
    for (i = length1 * length2; i < size; i++)
234
        quantizers[i] = 0;
235

  
236
}
237

  
238
static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
239
{
240
    int i, j;
241

  
242
    for (i = 0; i < length1; i++)
243
        for (j = 0; j < length2; j++)
244
            quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
245

  
246
    for (i = length1 * length2; i < size; i++)
247
        quantizers[i] = 0;
199
    return (code - (levels >> 1)) * (2.0f / levels);
248 200
}
249 201

  
250 202
/*
......
254 206
{
255 207
    int i;
256 208

  
257
    /* Quantizer ungrouping tables. */
258
    // for level-3 quantizers
259
    generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
260
    generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
261
    generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
262

  
263
    //for level-5 quantizers
264
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
265
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
266
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
267

  
268
    //for level-7 quantizers
269
    generate_quantizers_table(l7_quantizers, 7, 7);
270

  
271
    //for level-4 quantizers
272
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
273
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
274

  
275
    //for level-15 quantizers
276
    generate_quantizers_table(l15_quantizers, 15, 15);
277
    /* End Quantizer ungrouping tables. */
209
    /* generate grouped mantissa tables
210
       reference: Section 7.3.5 Ungrouping of Mantissas */
211
    for(i=0; i<32; i++) {
212
        /* bap=1 mantissas */
213
        b1_mantissas[i][0] = symmetric_dequant( i / 9     , 3);
214
        b1_mantissas[i][1] = symmetric_dequant((i % 9) / 3, 3);
215
        b1_mantissas[i][2] = symmetric_dequant((i % 9) % 3, 3);
216
    }
217
    for(i=0; i<128; i++) {
218
        /* bap=2 mantissas */
219
        b2_mantissas[i][0] = symmetric_dequant( i / 25     , 5);
220
        b2_mantissas[i][1] = symmetric_dequant((i % 25) / 5, 5);
221
        b2_mantissas[i][2] = symmetric_dequant((i % 25) % 5, 5);
222

  
223
        /* bap=4 mantissas */
224
        b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
225
        b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
226
    }
227
    /* generate ungrouped mantissa tables
228
       reference: Tables 7.21 and 7.23 */
229
    for(i=0; i<7; i++) {
230
        /* bap=3 mantissas */
231
        b3_mantissas[i] = symmetric_dequant(i, 7);
232
    }
233
    for(i=0; i<15; i++) {
234
        /* bap=5 mantissas */
235
        b5_mantissas[i] = symmetric_dequant(i, 15);
236
    }
278 237

  
279 238
    //generate scale factors
280 239
    for (i = 0; i < 25; i++)
281
        scale_factors[i] = pow(2.0, -(i + 15));
240
        scale_factors[i] = pow(2.0, -i);
282 241

  
283 242
    /* generate exponent tables
284 243
       reference: Section 7.1.3 Exponent Decoding */
......
440 399
            for(j=0; j<12; j++) {
441 400
                for(ch=1; ch<=ctx->nfchans; ch++) {
442 401
                    if(ctx->chincpl[ch-1])
443
                        ctx->transform_coeffs[ch][i] = ctx->transform_coeffs_cpl[i] * ctx->cplco[ch-1][bnd];
402
                        ctx->transform_coeffs[ch][i] = ctx->transform_coeffs_cpl[i] * ctx->cplco[ch-1][bnd] * 8.0f;
444 403
                }
445 404
                i++;
446 405
            }
......
449 408
}
450 409

  
451 410
typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
452
    int16_t l3_quantizers[3];
453
    int16_t l5_quantizers[3];
454
    int16_t l11_quantizers[2];
455
    int l3ptr;
456
    int l5ptr;
457
    int l11ptr;
411
    float b1_mant[3];
412
    float b2_mant[3];
413
    float b4_mant[2];
414
    int b1ptr;
415
    int b2ptr;
416
    int b4ptr;
458 417
} mant_groups;
459 418

  
460 419
/* Get the transform coefficients for particular channel */
......
491 450
        tbap = bap[i];
492 451
        switch (tbap) {
493 452
            case 0:
494
                coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * LEVEL_MINUS_3DB;
453
                coeffs[i] = ((av_random(&ctx->dith_state) & 0xFFFF) * LEVEL_MINUS_3DB) / 32768.0f;
495 454
                break;
496 455

  
497 456
            case 1:
498
                if (m->l3ptr > 2) {
457
                if(m->b1ptr > 2) {
499 458
                    gcode = get_bits(gb, 5);
500
                    m->l3_quantizers[0] = l3_quantizers_1[gcode];
501
                    m->l3_quantizers[1] = l3_quantizers_2[gcode];
502
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
503
                    m->l3ptr = 0;
459
                    m->b1_mant[0] = b1_mantissas[gcode][0];
460
                    m->b1_mant[1] = b1_mantissas[gcode][1];
461
                    m->b1_mant[2] = b1_mantissas[gcode][2];
462
                    m->b1ptr = 0;
504 463
                }
505
                coeffs[i] = m->l3_quantizers[m->l3ptr++];
464
                coeffs[i] = m->b1_mant[m->b1ptr++];
506 465
                break;
507 466

  
508 467
            case 2:
509
                if (m->l5ptr > 2) {
468
                if(m->b2ptr > 2) {
510 469
                    gcode = get_bits(gb, 7);
511
                    m->l5_quantizers[0] = l5_quantizers_1[gcode];
512
                    m->l5_quantizers[1] = l5_quantizers_2[gcode];
513
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
514
                    m->l5ptr = 0;
470
                    m->b2_mant[0] = b2_mantissas[gcode][0];
471
                    m->b2_mant[1] = b2_mantissas[gcode][1];
472
                    m->b2_mant[2] = b2_mantissas[gcode][2];
473
                    m->b2ptr = 0;
515 474
                }
516
                coeffs[i] = m->l5_quantizers[m->l5ptr++];
475
                coeffs[i] = m->b2_mant[m->b2ptr++];
517 476
                break;
518 477

  
519 478
            case 3:
520
                coeffs[i] = l7_quantizers[get_bits(gb, 3)];
479
                coeffs[i] = b3_mantissas[get_bits(gb, 3)];
521 480
                break;
522 481

  
523 482
            case 4:
524
                if (m->l11ptr > 1) {
483
                if(m->b4ptr > 1) {
525 484
                    gcode = get_bits(gb, 7);
526
                    m->l11_quantizers[0] = l11_quantizers_1[gcode];
527
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
528
                    m->l11ptr = 0;
485
                    m->b4_mant[0] = b4_mantissas[gcode][0];
486
                    m->b4_mant[1] = b4_mantissas[gcode][1];
487
                    m->b4ptr = 0;
529 488
                }
530
                coeffs[i] = m->l11_quantizers[m->l11ptr++];
489
                coeffs[i] = m->b4_mant[m->b4ptr++];
531 490
                break;
532 491

  
533 492
            case 5:
534
                coeffs[i] = l15_quantizers[get_bits(gb, 4)];
493
                coeffs[i] = b5_mantissas[get_bits(gb, 4)];
535 494
                break;
536 495

  
537 496
            default:
538
                coeffs[i] = get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]);
497
                coeffs[i] = get_sbits(gb, qntztab[tbap]) * scale_factors[qntztab[tbap]-1];
539 498
                break;
540 499
        }
541 500
        coeffs[i] *= scale_factors[exps[i]];
......
587 546
    int got_cplchan = 0;
588 547
    mant_groups m;
589 548

  
590
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
549
    m.b1ptr = m.b2ptr = m.b4ptr = 3;
591 550

  
592 551
    for (i = 0; i < ctx->nfchans; i++) {
593 552
        /* transform coefficients for individual channel */
......
747 706

  
748 707
    if (get_bits1(gb)) { /* dynamic range */
749 708
        dynrng = get_sbits(gb, 8);
750
        ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
709
        ctx->dynrng = (((dynrng & 0x1f) | 0x20) << 13) * pow(2.0, -(18 - (dynrng >> 5)));
751 710
    } else if(blk == 0) {
752 711
        ctx->dynrng = 1.0;
753 712
    }
......
755 714
    if(acmod == AC3_ACMOD_DUALMONO) { /* dynamic range 1+1 mode */
756 715
        if(get_bits1(gb)) {
757 716
            dynrng = get_sbits(gb, 8);
758
            ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
717
            ctx->dynrng2 = (((dynrng & 0x1f) | 0x20) << 13) * pow(2.0, -(18 - (dynrng >> 5)));
759 718
        } else if(blk == 0) {
760 719
            ctx->dynrng2 = 1.0;
761 720
        }
......
807 766
                        cplcoexp = get_bits(gb, 4);
808 767
                        cplcomant = get_bits(gb, 4);
809 768
                        if (cplcoexp == 15)
810
                            cplcomant <<= 14;
769
                            ctx->cplco[i][bnd] = cplcomant / 16.0f;
811 770
                        else
812
                            cplcomant = (cplcomant | 0x10) << 13;
813
                        ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
771
                            ctx->cplco[i][bnd] = (cplcomant + 16.0f) / 32.0f;
772
                        ctx->cplco[i][bnd] *= scale_factors[cplcoexp + mstrcplco];
814 773
                    }
815 774
                }
816 775

  

Also available in: Unified diff