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);
317
} else if (CONFIG_EAC3_DECODER) {
318
319
return ff_eac3_parse_header(s);
321
av_log(s->avctx, AV_LOG_ERROR, "E-AC-3 support not compiled in\n");
409
413
static void calc_transform_coeffs_cpl(AC3DecodeContext *s)
411
int i, j, ch, bnd, subbnd;
414
i = s->start_freq[CPL_CH];
415
for(bnd=0; bnd<s->num_cpl_bands; bnd++) {
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];
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);
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];
428
} while(s->cpl_band_struct[subbnd]);
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;
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)
523
533
static void remove_dithering(AC3DecodeContext *s) {
529
536
for(ch=1; ch<=s->fbw_channels; ch++) {
530
if(!s->dither_flag[ch]) {
531
coeffs = s->fixed_coeffs[ch];
533
if(s->channel_in_cpl[ch])
534
end = s->start_freq[CPL_CH];
536
end = s->end_freq[ch];
537
for(i=0; i<end; i++) {
541
if(s->channel_in_cpl[ch]) {
542
bap = s->bap[CPL_CH];
543
for(; i<s->end_freq[CPL_CH]; i++) {
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;
558
552
/* if AHT is used, mantissas for all blocks are encoded in the first
559
553
block of the frame. */
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];
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];
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.
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)
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)
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;
738
733
n_subbands = end_subband - start_subband;
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);
740
band_struct = coded_band_struct;
745
741
} else if (!blk) {
747
&default_band_struct[start_subband+1],
742
band_struct = &default_band_struct[start_subband+1];
744
/* no change in band structure */
750
band_struct[n_subbands-1] = 0;
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
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);
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);
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);
877
870
s->start_freq[CPL_CH] = cpl_start_subband * 12 + 37;
878
871
s->end_freq[CPL_CH] = cpl_end_subband * 12 + 37;
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,
875
ff_eac3_default_cpl_band_struct,
876
&s->num_cpl_bands, s->cpl_band_sizes);
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");
942
av_log(s->avctx, AV_LOG_WARNING, "Warning: new rematrixing strategy not present in block 0\n");
943
s->num_rematrixing_bands = 0;
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];
1179
1171
gain *= s->dynamic_range[0];
1220
1212
* Decode a single AC-3 frame.
1222
1214
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
1223
const uint8_t *buf, int buf_size)
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];
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);
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;
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;
1257
if(err && err != AAC_AC3_PARSE_ERROR_CRC) {
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");
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;
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];
1302
1297
avctx->channels = s->out_channels;
1298
avctx->channel_layout = s->channel_layout;
1304
1300
/* set downmixing coefficients if needed */
1305
1301
if(s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&
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");
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;