~ubuntu-branches/debian/squeeze/gstreamer0.10-ffmpeg/squeeze

« back to all changes in this revision

Viewing changes to gst-libs/ext/ffmpeg/libavcodec/ac3dec.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2010-02-19 18:14:59 UTC
  • mfrom: (4.1.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20100219181459-mect96st3px2jfsi
Tags: 0.10.9.2-1
* New upstream pre-release:
  + debian/patches/03_restricted-caps.patch,
    debian/patches/04_ignore-vdpau.patch:
    - Dropped, merged upstream.
* debian/patches/03_too-new-codec-ids.patch:
  + Disable some ffmpeg codec IDs because Debian's
    ffmpeg is once again too old...

Show diffs side-by-side

added added

removed removed

Lines of Context:
189
189
 
190
190
    ac3_common_init();
191
191
    ac3_tables_init();
192
 
    ff_mdct_init(&s->imdct_256, 8, 1);
193
 
    ff_mdct_init(&s->imdct_512, 9, 1);
 
192
    ff_mdct_init(&s->imdct_256, 8, 1, 1.0);
 
193
    ff_mdct_init(&s->imdct_512, 9, 1, 1.0);
194
194
    ff_kbd_window_init(s->window, 5.0, 256);
195
195
    dsputil_init(&s->dsp, avctx);
196
196
    av_lfg_init(&s->dith_state, 0);
280
280
    /* get decoding parameters from header info */
281
281
    s->bit_alloc_params.sr_code     = hdr.sr_code;
282
282
    s->channel_mode                 = hdr.channel_mode;
 
283
    s->channel_layout               = hdr.channel_layout;
283
284
    s->lfe_on                       = hdr.lfe_on;
284
285
    s->bit_alloc_params.sr_shift    = hdr.sr_shift;
285
286
    s->sample_rate                  = hdr.sample_rate;
313
314
        s->skip_syntax           = 1;
314
315
        memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
315
316
        return ac3_parse_header(s);
316
 
    } else {
 
317
    } else if (CONFIG_EAC3_DECODER) {
317
318
        s->eac3 = 1;
318
319
        return ff_eac3_parse_header(s);
 
320
    } else {
 
321
        av_log(s->avctx, AV_LOG_ERROR, "E-AC-3 support not compiled in\n");
 
322
        return -1;
319
323
    }
320
324
}
321
325
 
408
412
 */
409
413
static void calc_transform_coeffs_cpl(AC3DecodeContext *s)
410
414
{
411
 
    int i, j, ch, bnd, subbnd;
 
415
    int bin, band, ch;
412
416
 
413
 
    subbnd = -1;
414
 
    i = s->start_freq[CPL_CH];
415
 
    for(bnd=0; bnd<s->num_cpl_bands; bnd++) {
416
 
        do {
417
 
            subbnd++;
418
 
            for(j=0; j<12; j++) {
419
 
                for(ch=1; ch<=s->fbw_channels; ch++) {
420
 
                    if(s->channel_in_cpl[ch]) {
421
 
                        s->fixed_coeffs[ch][i] = ((int64_t)s->fixed_coeffs[CPL_CH][i] * (int64_t)s->cpl_coords[ch][bnd]) >> 23;
422
 
                        if (ch == 2 && s->phase_flags[bnd])
423
 
                            s->fixed_coeffs[ch][i] = -s->fixed_coeffs[ch][i];
424
 
                    }
425
 
                }
426
 
                i++;
 
417
    bin = s->start_freq[CPL_CH];
 
418
    for (band = 0; band < s->num_cpl_bands; band++) {
 
419
        int band_start = bin;
 
420
        int band_end = bin + s->cpl_band_sizes[band];
 
421
        for (ch = 1; ch <= s->fbw_channels; ch++) {
 
422
            if (s->channel_in_cpl[ch]) {
 
423
                int cpl_coord = s->cpl_coords[ch][band] << 5;
 
424
                for (bin = band_start; bin < band_end; bin++) {
 
425
                    s->fixed_coeffs[ch][bin] = MULH(s->fixed_coeffs[CPL_CH][bin] << 4, cpl_coord);
 
426
                }
 
427
                if (ch == 2 && s->phase_flags[band]) {
 
428
                    for (bin = band_start; bin < band_end; bin++)
 
429
                        s->fixed_coeffs[2][bin] = -s->fixed_coeffs[2][bin];
 
430
                }
427
431
            }
428
 
        } while(s->cpl_band_struct[subbnd]);
 
432
        }
 
433
        bin = band_end;
429
434
    }
430
435
}
431
436
 
452
457
    uint8_t *baps = s->bap[ch_index];
453
458
    int8_t *exps = s->dexps[ch_index];
454
459
    int *coeffs = s->fixed_coeffs[ch_index];
 
460
    int dither = (ch_index == CPL_CH) || s->dither_flag[ch_index];
455
461
    GetBitContext *gbc = &s->gbc;
456
462
    int freq;
457
463
 
460
466
        int mantissa;
461
467
        switch(bap){
462
468
            case 0:
463
 
                mantissa = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
 
469
                if (dither)
 
470
                    mantissa = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
 
471
                else
 
472
                    mantissa = 0;
464
473
                break;
465
474
            case 1:
466
475
                if(m->b1){
517
526
}
518
527
 
519
528
/**
520
 
 * Remove random dithering from coefficients with zero-bit mantissas
 
529
 * Remove random dithering from coupling range coefficients with zero-bit
 
530
 * mantissas for coupled channels which do not use dithering.
521
531
 * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0)
522
532
 */
523
533
static void remove_dithering(AC3DecodeContext *s) {
524
534
    int ch, i;
525
 
    int end=0;
526
 
    int *coeffs;
527
 
    uint8_t *bap;
528
535
 
529
536
    for(ch=1; ch<=s->fbw_channels; ch++) {
530
 
        if(!s->dither_flag[ch]) {
531
 
            coeffs = s->fixed_coeffs[ch];
532
 
            bap = s->bap[ch];
533
 
            if(s->channel_in_cpl[ch])
534
 
                end = s->start_freq[CPL_CH];
535
 
            else
536
 
                end = s->end_freq[ch];
537
 
            for(i=0; i<end; i++) {
538
 
                if(!bap[i])
539
 
                    coeffs[i] = 0;
540
 
            }
541
 
            if(s->channel_in_cpl[ch]) {
542
 
                bap = s->bap[CPL_CH];
543
 
                for(; i<s->end_freq[CPL_CH]; i++) {
544
 
                    if(!bap[i])
545
 
                        coeffs[i] = 0;
546
 
                }
 
537
        if(!s->dither_flag[ch] && s->channel_in_cpl[ch]) {
 
538
            for(i = s->start_freq[CPL_CH]; i<s->end_freq[CPL_CH]; i++) {
 
539
                if(!s->bap[CPL_CH][i])
 
540
                    s->fixed_coeffs[ch][i] = 0;
547
541
            }
548
542
        }
549
543
    }
558
552
        /* if AHT is used, mantissas for all blocks are encoded in the first
559
553
           block of the frame. */
560
554
        int bin;
561
 
        if (!blk)
 
555
        if (!blk && CONFIG_EAC3_DECODER)
562
556
            ff_eac3_decode_transform_coeffs_aht_ch(s, ch);
563
557
        for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
564
558
            s->fixed_coeffs[ch][bin] = s->pre_mantissa[ch][bin][blk] >> s->dexps[ch][bin];
609
603
{
610
604
    int bnd, i;
611
605
    int end, bndend;
612
 
    int tmp0, tmp1;
613
606
 
614
607
    end = FFMIN(s->end_freq[1], s->end_freq[2]);
615
608
 
617
610
        if(s->rematrixing_flags[bnd]) {
618
611
            bndend = FFMIN(end, ff_ac3_rematrix_band_tab[bnd+1]);
619
612
            for(i=ff_ac3_rematrix_band_tab[bnd]; i<bndend; i++) {
620
 
                tmp0 = s->fixed_coeffs[1][i];
621
 
                tmp1 = s->fixed_coeffs[2][i];
622
 
                s->fixed_coeffs[1][i] = tmp0 + tmp1;
623
 
                s->fixed_coeffs[2][i] = tmp0 - tmp1;
 
613
                int tmp0 = s->fixed_coeffs[1][i];
 
614
                s->fixed_coeffs[1][i] += s->fixed_coeffs[2][i];
 
615
                s->fixed_coeffs[2][i]  = tmp0 - s->fixed_coeffs[2][i];
624
616
            }
625
617
        }
626
618
    }
714
706
 
715
707
/**
716
708
 * Decode band structure for coupling, spectral extension, or enhanced coupling.
 
709
 * The band structure defines how many subbands are in each band.  For each
 
710
 * subband in the range, 1 means it is combined with the previous band, and 0
 
711
 * means that it starts a new band.
 
712
 *
717
713
 * @param[in] gbc bit reader context
718
714
 * @param[in] blk block number
719
715
 * @param[in] eac3 flag to indicate E-AC-3
721
717
 * @param[in] start_subband subband number for start of range
722
718
 * @param[in] end_subband subband number for end of range
723
719
 * @param[in] default_band_struct default band structure table
724
 
 * @param[out] band_struct decoded band structure
725
 
 * @param[out] num_subbands number of subbands (optionally NULL)
726
720
 * @param[out] num_bands number of bands (optionally NULL)
727
721
 * @param[out] band_sizes array containing the number of bins in each band (optionally NULL)
728
722
 */
729
723
static void decode_band_structure(GetBitContext *gbc, int blk, int eac3,
730
724
                                  int ecpl, int start_subband, int end_subband,
731
725
                                  const uint8_t *default_band_struct,
732
 
                                  uint8_t *band_struct, int *num_subbands,
733
726
                                  int *num_bands, uint8_t *band_sizes)
734
727
{
735
728
    int subbnd, bnd, n_subbands, n_bands=0;
736
729
    uint8_t bnd_sz[22];
 
730
    uint8_t coded_band_struct[22];
 
731
    const uint8_t *band_struct;
737
732
 
738
733
    n_subbands = end_subband - start_subband;
739
734
 
740
735
    /* decode band structure from bitstream or use default */
741
736
    if (!eac3 || get_bits1(gbc)) {
742
737
        for (subbnd = 0; subbnd < n_subbands - 1; subbnd++) {
743
 
            band_struct[subbnd] = get_bits1(gbc);
 
738
            coded_band_struct[subbnd] = get_bits1(gbc);
744
739
        }
 
740
        band_struct = coded_band_struct;
745
741
    } else if (!blk) {
746
 
        memcpy(band_struct,
747
 
               &default_band_struct[start_subband+1],
748
 
               n_subbands-1);
 
742
        band_struct = &default_band_struct[start_subband+1];
 
743
    } else {
 
744
        /* no change in band structure */
 
745
        return;
749
746
    }
750
 
    band_struct[n_subbands-1] = 0;
751
747
 
752
748
    /* calculate number of bands and band sizes based on band structure.
753
749
       note that the first 4 subbands in enhanced coupling span only 6 bins
767
763
    }
768
764
 
769
765
    /* set optional output params */
770
 
    if (num_subbands)
771
 
        *num_subbands = n_subbands;
772
766
    if (num_bands)
773
767
        *num_bands = n_bands;
774
768
    if (band_sizes)
822
816
    /* spectral extension strategy */
823
817
    if (s->eac3 && (!blk || get_bits1(gbc))) {
824
818
        if (get_bits1(gbc)) {
825
 
            ff_log_missing_feature(s->avctx, "Spectral extension", 1);
 
819
            av_log_missing_feature(s->avctx, "Spectral extension", 1);
826
820
            return -1;
827
821
        }
828
822
        /* TODO: parse spectral extension strategy info */
847
841
            /* check for enhanced coupling */
848
842
            if (s->eac3 && get_bits1(gbc)) {
849
843
                /* TODO: parse enhanced coupling strategy info */
850
 
                ff_log_missing_feature(s->avctx, "Enhanced coupling", 1);
 
844
                av_log_missing_feature(s->avctx, "Enhanced coupling", 1);
851
845
                return -1;
852
846
            }
853
847
 
868
862
            /* TODO: modify coupling end freq if spectral extension is used */
869
863
            cpl_start_subband = get_bits(gbc, 4);
870
864
            cpl_end_subband   = get_bits(gbc, 4) + 3;
871
 
            s->num_cpl_subbands = cpl_end_subband - cpl_start_subband;
872
 
            if (s->num_cpl_subbands < 0) {
873
 
                av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d > %d)\n",
 
865
            if (cpl_start_subband >= cpl_end_subband) {
 
866
                av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d >= %d)\n",
874
867
                       cpl_start_subband, cpl_end_subband);
875
868
                return -1;
876
869
            }
877
870
            s->start_freq[CPL_CH] = cpl_start_subband * 12 + 37;
878
871
            s->end_freq[CPL_CH]   = cpl_end_subband   * 12 + 37;
879
872
 
880
 
           decode_band_structure(gbc, blk, s->eac3, 0,
881
 
                                 cpl_start_subband, cpl_end_subband,
882
 
                                 ff_eac3_default_cpl_band_struct,
883
 
                                 s->cpl_band_struct, &s->num_cpl_subbands,
884
 
                                 &s->num_cpl_bands, NULL);
 
873
            decode_band_structure(gbc, blk, s->eac3, 0, cpl_start_subband,
 
874
                                  cpl_end_subband,
 
875
                                  ff_eac3_default_cpl_band_struct,
 
876
                                  &s->num_cpl_bands, s->cpl_band_sizes);
885
877
        } else {
886
878
            /* coupling not in use */
887
879
            for (ch = 1; ch <= fbw_channels; ch++) {
947
939
            for(bnd=0; bnd<s->num_rematrixing_bands; bnd++)
948
940
                s->rematrixing_flags[bnd] = get_bits1(gbc);
949
941
        } else if (!blk) {
950
 
            av_log(s->avctx, AV_LOG_ERROR, "new rematrixing strategy must be present in block 0\n");
951
 
            return -1;
 
942
            av_log(s->avctx, AV_LOG_WARNING, "Warning: new rematrixing strategy not present in block 0\n");
 
943
            s->num_rematrixing_bands = 0;
952
944
        }
953
945
    }
954
946
 
1174
1166
    for(ch=1; ch<=s->channels; ch++) {
1175
1167
        float gain = s->mul_bias / 4194304.0f;
1176
1168
        if(s->channel_mode == AC3_CHMODE_DUALMONO) {
1177
 
            gain *= s->dynamic_range[ch-1];
 
1169
            gain *= s->dynamic_range[2-ch];
1178
1170
        } else {
1179
1171
            gain *= s->dynamic_range[0];
1180
1172
        }
1220
1212
 * Decode a single AC-3 frame.
1221
1213
 */
1222
1214
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
1223
 
                            const uint8_t *buf, int buf_size)
 
1215
                            AVPacket *avpkt)
1224
1216
{
 
1217
    const uint8_t *buf = avpkt->data;
 
1218
    int buf_size = avpkt->size;
1225
1219
    AC3DecodeContext *s = avctx->priv_data;
1226
1220
    int16_t *out_samples = (int16_t *)data;
1227
1221
    int blk, ch, err;
 
1222
    const uint8_t *channel_map;
 
1223
    const float *output[AC3_MAX_CHANNELS];
1228
1224
 
1229
1225
    /* initialize the GetBitContext with the start of valid AC-3 Frame */
1230
1226
    if (s->input_buffer) {
1240
1236
    *data_size = 0;
1241
1237
    err = parse_frame_header(s);
1242
1238
 
1243
 
    /* check that reported frame size fits in input buffer */
1244
 
    if(s->frame_size > buf_size) {
1245
 
        av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1246
 
        err = AAC_AC3_PARSE_ERROR_FRAME_SIZE;
1247
 
    }
1248
 
 
1249
 
    /* check for crc mismatch */
1250
 
    if(err != AAC_AC3_PARSE_ERROR_FRAME_SIZE && avctx->error_recognition >= FF_ER_CAREFUL) {
1251
 
        if(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, &buf[2], s->frame_size-2)) {
1252
 
            av_log(avctx, AV_LOG_ERROR, "frame CRC mismatch\n");
1253
 
            err = AAC_AC3_PARSE_ERROR_CRC;
1254
 
        }
1255
 
    }
1256
 
 
1257
 
    if(err && err != AAC_AC3_PARSE_ERROR_CRC) {
 
1239
    if (err) {
1258
1240
        switch(err) {
1259
1241
            case AAC_AC3_PARSE_ERROR_SYNC:
1260
1242
                av_log(avctx, AV_LOG_ERROR, "frame sync error\n");
1282
1264
                av_log(avctx, AV_LOG_ERROR, "invalid header\n");
1283
1265
                break;
1284
1266
        }
 
1267
    } else {
 
1268
        /* check that reported frame size fits in input buffer */
 
1269
        if (s->frame_size > buf_size) {
 
1270
            av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
 
1271
            err = AAC_AC3_PARSE_ERROR_FRAME_SIZE;
 
1272
        } else if (avctx->error_recognition >= FF_ER_CAREFUL) {
 
1273
            /* check for crc mismatch */
 
1274
            if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, &buf[2], s->frame_size-2)) {
 
1275
                av_log(avctx, AV_LOG_ERROR, "frame CRC mismatch\n");
 
1276
                err = AAC_AC3_PARSE_ERROR_CRC;
 
1277
            }
 
1278
        }
1285
1279
    }
1286
1280
 
1287
1281
    /* if frame is ok, set audio parameters */
1298
1292
                avctx->request_channels < s->channels) {
1299
1293
            s->out_channels = avctx->request_channels;
1300
1294
            s->output_mode  = avctx->request_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;
 
1295
            s->channel_layout = ff_ac3_channel_layout_tab[s->output_mode];
1301
1296
        }
1302
1297
        avctx->channels = s->out_channels;
 
1298
        avctx->channel_layout = s->channel_layout;
1303
1299
 
1304
1300
        /* set downmixing coefficients if needed */
1305
1301
        if(s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&
1313
1309
    }
1314
1310
 
1315
1311
    /* decode the audio blocks */
 
1312
    channel_map = ff_ac3_dec_channel_map[s->output_mode & ~AC3_OUTPUT_LFEON][s->lfe_on];
 
1313
    for (ch = 0; ch < s->out_channels; ch++)
 
1314
        output[ch] = s->output[channel_map[ch]];
1316
1315
    for (blk = 0; blk < s->num_blocks; blk++) {
1317
 
        const float *output[s->out_channels];
1318
1316
        if (!err && decode_audio_block(s, blk)) {
1319
1317
            av_log(avctx, AV_LOG_ERROR, "error decoding the audio block\n");
1320
1318
            err = 1;
1321
1319
        }
1322
 
        for (ch = 0; ch < s->out_channels; ch++)
1323
 
            output[ch] = s->output[ch];
1324
1320
        s->dsp.float_to_int16_interleave(out_samples, output, 256, s->out_channels);
1325
1321
        out_samples += 256 * s->out_channels;
1326
1322
    }
1353
1349
    .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
1354
1350
};
1355
1351
 
 
1352
#if CONFIG_EAC3_DECODER
1356
1353
AVCodec eac3_decoder = {
1357
1354
    .name = "eac3",
1358
1355
    .type = CODEC_TYPE_AUDIO,
1363
1360
    .decode = ac3_decode_frame,
1364
1361
    .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52B (AC-3, E-AC-3)"),
1365
1362
};
 
1363
#endif