X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=libavcodec%2Fdcadec.c;h=8ca8c34510b801179969f7937388ae49812bf49c;hb=9a738c27dceb4b975784b23213a46f5cb560d1c2;hp=6b0812cf7079803fc566790253d0ed2301d5ed49;hpb=36ef5369ee9b336febc2c270f8718cec4476cb85;p=ffmpeg diff --git a/libavcodec/dcadec.c b/libavcodec/dcadec.c index 6b0812cf707..8ca8c34510b 100644 --- a/libavcodec/dcadec.c +++ b/libavcodec/dcadec.c @@ -26,24 +26,27 @@ #include #include +#include "libavutil/channel_layout.h" #include "libavutil/common.h" #include "libavutil/float_dsp.h" -#include "libavutil/intmath.h" +#include "libavutil/internal.h" #include "libavutil/intreadwrite.h" #include "libavutil/mathematics.h" -#include "libavutil/audioconvert.h" +#include "libavutil/opt.h" +#include "libavutil/samplefmt.h" + #include "avcodec.h" -#include "dsputil.h" +#include "dca.h" +#include "dcadata.h" +#include "dcadsp.h" +#include "dcahuff.h" #include "fft.h" +#include "fmtconvert.h" #include "get_bits.h" +#include "internal.h" +#include "mathops.h" #include "put_bits.h" -#include "dcadata.h" -#include "dcahuff.h" -#include "dca.h" -#include "dca_parser.h" #include "synth_filter.h" -#include "dcadsp.h" -#include "fmtconvert.h" #if ARCH_ARM # include "arm/dca.h" @@ -52,7 +55,6 @@ //#define TRACE #define DCA_PRIM_CHANNELS_MAX (7) -#define DCA_SUBBANDS (32) #define DCA_ABITS_MAX (32) /* Should be 28 */ #define DCA_SUBSUBFRAMES_MAX (4) #define DCA_SUBFRAMES_MAX (16) @@ -172,79 +174,79 @@ static const int8_t dca_lfe_index[] = { }; static const int8_t dca_channel_reorder_lfe[][9] = { - { 0, -1, -1, -1, -1, -1, -1, -1, -1}, - { 0, 1, -1, -1, -1, -1, -1, -1, -1}, - { 0, 1, -1, -1, -1, -1, -1, -1, -1}, - { 0, 1, -1, -1, -1, -1, -1, -1, -1}, - { 0, 1, -1, -1, -1, -1, -1, -1, -1}, - { 2, 0, 1, -1, -1, -1, -1, -1, -1}, - { 0, 1, 3, -1, -1, -1, -1, -1, -1}, - { 2, 0, 1, 4, -1, -1, -1, -1, -1}, - { 0, 1, 3, 4, -1, -1, -1, -1, -1}, - { 2, 0, 1, 4, 5, -1, -1, -1, -1}, - { 3, 4, 0, 1, 5, 6, -1, -1, -1}, - { 2, 0, 1, 4, 5, 6, -1, -1, -1}, - { 0, 6, 4, 5, 2, 3, -1, -1, -1}, - { 4, 2, 5, 0, 1, 6, 7, -1, -1}, - { 5, 6, 0, 1, 7, 3, 8, 4, -1}, - { 4, 2, 5, 0, 1, 6, 8, 7, -1}, + { 0, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 0, 1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 3, -1, -1, -1, -1, -1, -1 }, + { 2, 0, 1, 4, -1, -1, -1, -1, -1 }, + { 0, 1, 3, 4, -1, -1, -1, -1, -1 }, + { 2, 0, 1, 4, 5, -1, -1, -1, -1 }, + { 3, 4, 0, 1, 5, 6, -1, -1, -1 }, + { 2, 0, 1, 4, 5, 6, -1, -1, -1 }, + { 0, 6, 4, 5, 2, 3, -1, -1, -1 }, + { 4, 2, 5, 0, 1, 6, 7, -1, -1 }, + { 5, 6, 0, 1, 7, 3, 8, 4, -1 }, + { 4, 2, 5, 0, 1, 6, 8, 7, -1 }, }; static const int8_t dca_channel_reorder_lfe_xch[][9] = { - { 0, 2, -1, -1, -1, -1, -1, -1, -1}, - { 0, 1, 3, -1, -1, -1, -1, -1, -1}, - { 0, 1, 3, -1, -1, -1, -1, -1, -1}, - { 0, 1, 3, -1, -1, -1, -1, -1, -1}, - { 0, 1, 3, -1, -1, -1, -1, -1, -1}, - { 2, 0, 1, 4, -1, -1, -1, -1, -1}, - { 0, 1, 3, 4, -1, -1, -1, -1, -1}, - { 2, 0, 1, 4, 5, -1, -1, -1, -1}, - { 0, 1, 4, 5, 3, -1, -1, -1, -1}, - { 2, 0, 1, 5, 6, 4, -1, -1, -1}, - { 3, 4, 0, 1, 6, 7, 5, -1, -1}, - { 2, 0, 1, 4, 5, 6, 7, -1, -1}, - { 0, 6, 4, 5, 2, 3, 7, -1, -1}, - { 4, 2, 5, 0, 1, 7, 8, 6, -1}, - { 5, 6, 0, 1, 8, 3, 9, 4, 7}, - { 4, 2, 5, 0, 1, 6, 9, 8, 7}, + { 0, 2, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 3, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 3, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 3, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 3, -1, -1, -1, -1, -1, -1 }, + { 2, 0, 1, 4, -1, -1, -1, -1, -1 }, + { 0, 1, 3, 4, -1, -1, -1, -1, -1 }, + { 2, 0, 1, 4, 5, -1, -1, -1, -1 }, + { 0, 1, 4, 5, 3, -1, -1, -1, -1 }, + { 2, 0, 1, 5, 6, 4, -1, -1, -1 }, + { 3, 4, 0, 1, 6, 7, 5, -1, -1 }, + { 2, 0, 1, 4, 5, 6, 7, -1, -1 }, + { 0, 6, 4, 5, 2, 3, 7, -1, -1 }, + { 4, 2, 5, 0, 1, 7, 8, 6, -1 }, + { 5, 6, 0, 1, 8, 3, 9, 4, 7 }, + { 4, 2, 5, 0, 1, 6, 9, 8, 7 }, }; static const int8_t dca_channel_reorder_nolfe[][9] = { - { 0, -1, -1, -1, -1, -1, -1, -1, -1}, - { 0, 1, -1, -1, -1, -1, -1, -1, -1}, - { 0, 1, -1, -1, -1, -1, -1, -1, -1}, - { 0, 1, -1, -1, -1, -1, -1, -1, -1}, - { 0, 1, -1, -1, -1, -1, -1, -1, -1}, - { 2, 0, 1, -1, -1, -1, -1, -1, -1}, - { 0, 1, 2, -1, -1, -1, -1, -1, -1}, - { 2, 0, 1, 3, -1, -1, -1, -1, -1}, - { 0, 1, 2, 3, -1, -1, -1, -1, -1}, - { 2, 0, 1, 3, 4, -1, -1, -1, -1}, - { 2, 3, 0, 1, 4, 5, -1, -1, -1}, - { 2, 0, 1, 3, 4, 5, -1, -1, -1}, - { 0, 5, 3, 4, 1, 2, -1, -1, -1}, - { 3, 2, 4, 0, 1, 5, 6, -1, -1}, - { 4, 5, 0, 1, 6, 2, 7, 3, -1}, - { 3, 2, 4, 0, 1, 5, 7, 6, -1}, + { 0, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 0, 1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 2, -1, -1, -1, -1, -1, -1 }, + { 2, 0, 1, 3, -1, -1, -1, -1, -1 }, + { 0, 1, 2, 3, -1, -1, -1, -1, -1 }, + { 2, 0, 1, 3, 4, -1, -1, -1, -1 }, + { 2, 3, 0, 1, 4, 5, -1, -1, -1 }, + { 2, 0, 1, 3, 4, 5, -1, -1, -1 }, + { 0, 5, 3, 4, 1, 2, -1, -1, -1 }, + { 3, 2, 4, 0, 1, 5, 6, -1, -1 }, + { 4, 5, 0, 1, 6, 2, 7, 3, -1 }, + { 3, 2, 4, 0, 1, 5, 7, 6, -1 }, }; static const int8_t dca_channel_reorder_nolfe_xch[][9] = { - { 0, 1, -1, -1, -1, -1, -1, -1, -1}, - { 0, 1, 2, -1, -1, -1, -1, -1, -1}, - { 0, 1, 2, -1, -1, -1, -1, -1, -1}, - { 0, 1, 2, -1, -1, -1, -1, -1, -1}, - { 0, 1, 2, -1, -1, -1, -1, -1, -1}, - { 2, 0, 1, 3, -1, -1, -1, -1, -1}, - { 0, 1, 2, 3, -1, -1, -1, -1, -1}, - { 2, 0, 1, 3, 4, -1, -1, -1, -1}, - { 0, 1, 3, 4, 2, -1, -1, -1, -1}, - { 2, 0, 1, 4, 5, 3, -1, -1, -1}, - { 2, 3, 0, 1, 5, 6, 4, -1, -1}, - { 2, 0, 1, 3, 4, 5, 6, -1, -1}, - { 0, 5, 3, 4, 1, 2, 6, -1, -1}, - { 3, 2, 4, 0, 1, 6, 7, 5, -1}, - { 4, 5, 0, 1, 7, 2, 8, 3, 6}, - { 3, 2, 4, 0, 1, 5, 8, 7, 6}, + { 0, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 2, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 2, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 2, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 2, -1, -1, -1, -1, -1, -1 }, + { 2, 0, 1, 3, -1, -1, -1, -1, -1 }, + { 0, 1, 2, 3, -1, -1, -1, -1, -1 }, + { 2, 0, 1, 3, 4, -1, -1, -1, -1 }, + { 0, 1, 3, 4, 2, -1, -1, -1, -1 }, + { 2, 0, 1, 4, 5, 3, -1, -1, -1 }, + { 2, 3, 0, 1, 5, 6, 4, -1, -1 }, + { 2, 0, 1, 3, 4, 5, 6, -1, -1 }, + { 0, 5, 3, 4, 1, 2, 6, -1, -1 }, + { 3, 2, 4, 0, 1, 6, 7, 5, -1 }, + { 4, 5, 0, 1, 7, 2, 8, 3, 6 }, + { 3, 2, 4, 0, 1, 5, 8, 7, 6 }, }; #define DCA_DOLBY 101 /* FIXME */ @@ -261,6 +263,8 @@ static const int8_t dca_channel_reorder_nolfe_xch[][9] = { #define DCA_BUFFER_PADDING_SIZE 1024 +#define DCA_NSYNCAUX 0x9A1105A0 + /** Bit allocation */ typedef struct { int offset; ///< code values offset @@ -282,8 +286,8 @@ static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, } typedef struct { + AVClass *class; ///< class for AVOptions AVCodecContext *avctx; - AVFrame frame; /* Frame header */ int frame_type; ///< type of the current frame int samples_deficit; ///< deficit sample count @@ -295,7 +299,6 @@ typedef struct { int bit_rate; ///< transmission bit rate int bit_rate_index; ///< transmission bit rate index - int downmix; ///< embedded downmix enabled int dynrange; ///< embedded dynamic range flag int timestamp; ///< embedded time stamp flag int aux_data; ///< auxiliary data flag @@ -316,7 +319,6 @@ typedef struct { /* Primary audio coding header */ int subframes; ///< number of subframes - int is_channels_set; ///< check for if the channel number is already set int total_channels; ///< number of channels including extensions int prim_channels; ///< number of primary audio channels int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count @@ -335,13 +337,20 @@ typedef struct { int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients) - int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient) + int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];///< scale factors (2 if transient) int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors - int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients + float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients int dynrange_coef; ///< dynamic range coefficient - int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands + /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1) + * Input: primary audio channels (incl. LFE if present) + * Output: downmix audio channels (up to 4, no LFE) */ + uint8_t core_downmix; ///< embedded downmix coefficients available + uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix + uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes) + + int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data int lfe_scale_factor; @@ -354,11 +363,12 @@ typedef struct { DECLARE_ALIGNED(32, float, raXin)[32]; int output; ///< type of output - float scale_bias; ///< output scale DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8]; - DECLARE_ALIGNED(32, float, samples)[(DCA_PRIM_CHANNELS_MAX + 1) * 256]; - const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1]; + float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1]; + float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1]; + uint8_t *extra_channels_buffer; + unsigned int extra_channels_buffer_size; uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE]; int dca_buffer_size; ///< how much data is in the dca_buffer @@ -374,6 +384,7 @@ typedef struct { /* XCh extension information */ int xch_present; ///< XCh extension present and valid int xch_base_channel; ///< index of first (only) channel containing XCH data + int xch_disable; ///< whether the XCh extension should be decoded or not /* ExSS header parser */ int static_fields; ///< static fields present @@ -410,27 +421,27 @@ static av_cold void dca_init_vlcs(void) return; dca_bitalloc_index.offset = 1; - dca_bitalloc_index.wrap = 2; + dca_bitalloc_index.wrap = 2; for (i = 0; i < 5; i++) { - dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]]; + dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]]; dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i]; init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12, bitalloc_12_bits[i], 1, 1, bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); } dca_scalefactor.offset = -64; - dca_scalefactor.wrap = 2; + dca_scalefactor.wrap = 2; for (i = 0; i < 5; i++) { - dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]]; + dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]]; dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5]; init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129, scales_bits[i], 1, 1, scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); } dca_tmode.offset = 0; - dca_tmode.wrap = 1; + dca_tmode.wrap = 1; for (i = 0; i < 4; i++) { - dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]]; + dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]]; dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10]; init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4, tmode_bits[i], 1, 1, @@ -474,7 +485,6 @@ static int dca_parse_audio_coding_header(DCAContext *s, int base_channel) if (s->prim_channels > DCA_PRIM_CHANNELS_MAX) s->prim_channels = DCA_PRIM_CHANNELS_MAX; - for (i = base_channel; i < s->prim_channels; i++) { s->subband_activity[i] = get_bits(&s->gb, 5) + 2; if (s->subband_activity[i] > DCA_SUBBANDS) @@ -569,7 +579,7 @@ static int dca_parse_frame_header(DCAContext *s) if (!s->bit_rate) return AVERROR_INVALIDDATA; - s->downmix = get_bits(&s->gb, 1); + skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1) s->dynrange = get_bits(&s->gb, 1); s->timestamp = get_bits(&s->gb, 1); s->aux_data = get_bits(&s->gb, 1); @@ -580,6 +590,11 @@ static int dca_parse_frame_header(DCAContext *s) s->lfe = get_bits(&s->gb, 2); s->predictor_history = get_bits(&s->gb, 1); + if (s->lfe > 2) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe); + return AVERROR_INVALIDDATA; + } + /* TODO: check CRC */ if (s->crc_present) s->header_crc = get_bits(&s->gb, 16); @@ -610,7 +625,6 @@ static int dca_parse_frame_header(DCAContext *s) s->sample_rate); av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n", s->bit_rate); - av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix); av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange); av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp); av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data); @@ -636,18 +650,17 @@ static int dca_parse_frame_header(DCAContext *s) #endif /* Primary audio coding header */ - s->subframes = get_bits(&s->gb, 4) + 1; + s->subframes = get_bits(&s->gb, 4) + 1; return dca_parse_audio_coding_header(s, 0); } - static inline int get_scale(GetBitContext *gb, int level, int value, int log2range) { if (level < 5) { /* huffman encoded */ value += get_bitalloc(gb, &dca_scalefactor, level); - value = av_clip(value, 0, (1 << log2range) - 1); + value = av_clip(value, 0, (1 << log2range) - 1); } else if (level < 8) { if (level + 1 > log2range) { skip_bits(gb, level + 1 - log2range); @@ -704,8 +717,8 @@ static int dca_subframe_header(DCAContext *s, int base_channel, int block_index) } if (s->bitalloc[j][k] > 26) { - // av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index [%i][%i] too big (%i)\n", - // j, k, s->bitalloc[j][k]); + av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n", + j, k, s->bitalloc[j][k]); return AVERROR_INVALIDDATA; } } @@ -735,10 +748,10 @@ static int dca_subframe_header(DCAContext *s, int base_channel, int block_index) if (s->scalefactor_huffman[j] == 6) { scale_table = scale_factor_quant7; - log_size = 7; + log_size = 7; } else { scale_table = scale_factor_quant6; - log_size = 6; + log_size = 6; } /* When huffman coded, only the difference is encoded */ @@ -793,27 +806,6 @@ static int dca_subframe_header(DCAContext *s, int base_channel, int block_index) } } - /* Stereo downmix coefficients */ - if (!base_channel && s->prim_channels > 2) { - if (s->downmix) { - for (j = base_channel; j < s->prim_channels; j++) { - s->downmix_coef[j][0] = get_bits(&s->gb, 7); - s->downmix_coef[j][1] = get_bits(&s->gb, 7); - } - } else { - int am = s->amode & DCA_CHANNEL_MASK; - if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) { - av_log(s->avctx, AV_LOG_ERROR, - "Invalid channel mode %d\n", am); - return AVERROR_INVALIDDATA; - } - for (j = base_channel; j < s->prim_channels; j++) { - s->downmix_coef[j][0] = dca_default_coeffs[am][j][0]; - s->downmix_coef[j][1] = dca_default_coeffs[am][j][1]; - } - } - } - /* Dynamic range coefficient */ if (!base_channel && s->dynrange) s->dynrange_coef = get_bits(&s->gb, 8); @@ -836,7 +828,7 @@ static int dca_subframe_header(DCAContext *s, int base_channel, int block_index) /* Low frequency effect data */ if (!base_channel && s->lfe) { /* LFE samples */ - int lfe_samples = 2 * s->lfe * (4 + block_index); + int lfe_samples = 2 * s->lfe * (4 + block_index); int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]); float lfe_scale; @@ -908,21 +900,11 @@ static int dca_subframe_header(DCAContext *s, int base_channel, int block_index) av_log(s->avctx, AV_LOG_DEBUG, "\n"); } } - if (!base_channel && s->prim_channels > 2 && s->downmix) { - av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n"); - for (j = 0; j < s->prim_channels; j++) { - av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j, - dca_downmix_coeffs[s->downmix_coef[j][0]]); - av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j, - dca_downmix_coeffs[s->downmix_coef[j][1]]); - } - av_log(s->avctx, AV_LOG_DEBUG, "\n"); - } for (j = base_channel; j < s->prim_channels; j++) for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]); if (!base_channel && s->lfe) { - int lfe_samples = 2 * s->lfe * (4 + block_index); + int lfe_samples = 2 * s->lfe * (4 + block_index); int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]); av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n"); @@ -940,10 +922,8 @@ static void qmf_32_subbands(DCAContext *s, int chans, float scale) { const float *prCoeff; - int i; int sb_act = s->subband_activity[chans]; - int subindex; scale *= sqrt(1 / 8.0); @@ -953,30 +933,16 @@ static void qmf_32_subbands(DCAContext *s, int chans, else /* Perfect reconstruction */ prCoeff = fir_32bands_perfect; - for (i = sb_act; i < 32; i++) - s->raXin[i] = 0.0; - - /* Reconstructed channel sample index */ - for (subindex = 0; subindex < 8; subindex++) { - /* Load in one sample from each subband and clear inactive subbands */ - for (i = 0; i < sb_act; i++) { - unsigned sign = (i - 1) & 2; - uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30; - AV_WN32A(&s->raXin[i], v); - } - - s->synth.synth_filter_float(&s->imdct, - s->subband_fir_hist[chans], - &s->hist_index[chans], - s->subband_fir_noidea[chans], prCoeff, - samples_out, s->raXin, scale); - samples_out += 32; - } + s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct, + s->subband_fir_hist[chans], + &s->hist_index[chans], + s->subband_fir_noidea[chans], prCoeff, + samples_out, s->raXin, scale); } static void lfe_interpolation_fir(DCAContext *s, int decimation_select, int num_deci_sample, float *samples_in, - float *samples_out, float scale) + float *samples_out) { /* samples_in: An array holding decimated samples. * Samples in current subframe starts from samples_in[0], @@ -986,41 +952,41 @@ static void lfe_interpolation_fir(DCAContext *s, int decimation_select, * samples_out: An array holding interpolated samples */ - int decifactor; + int idx; const float *prCoeff; int deciindex; /* Select decimation filter */ if (decimation_select == 1) { - decifactor = 64; + idx = 1; prCoeff = lfe_fir_128; } else { - decifactor = 32; + idx = 0; prCoeff = lfe_fir_64; } /* Interpolation */ for (deciindex = 0; deciindex < num_deci_sample; deciindex++) { - s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale); + s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff); samples_in++; - samples_out += 2 * decifactor; + samples_out += 2 * 32 * (1 + idx); } } /* downmixing routines */ -#define MIX_REAR1(samples, si1, rs, coef) \ - samples[i] += samples[si1] * coef[rs][0]; \ - samples[i+256] += samples[si1] * coef[rs][1]; +#define MIX_REAR1(samples, s1, rs, coef) \ + samples[0][i] += samples[s1][i] * coef[rs][0]; \ + samples[1][i] += samples[s1][i] * coef[rs][1]; -#define MIX_REAR2(samples, si1, si2, rs, coef) \ - samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs + 1][0]; \ - samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs + 1][1]; +#define MIX_REAR2(samples, s1, s2, rs, coef) \ + samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \ + samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1]; #define MIX_FRONT3(samples, coef) \ - t = samples[i + c]; \ - u = samples[i + l]; \ - v = samples[i + r]; \ - samples[i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \ - samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1]; + t = samples[c][i]; \ + u = samples[l][i]; \ + v = samples[r][i]; \ + samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \ + samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1]; #define DOWNMIX_TO_STEREO(op1, op2) \ for (i = 0; i < 256; i++) { \ @@ -1028,70 +994,71 @@ static void lfe_interpolation_fir(DCAContext *s, int decimation_select, op2 \ } -static void dca_downmix(float *samples, int srcfmt, - int downmix_coef[DCA_PRIM_CHANNELS_MAX][2], +static void dca_downmix(float **samples, int srcfmt, int lfe_present, + float coef[DCA_PRIM_CHANNELS_MAX + 1][2], const int8_t *channel_mapping) { int c, l, r, sl, sr, s; int i; float t, u, v; - float coef[DCA_PRIM_CHANNELS_MAX][2]; - - for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) { - coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]]; - coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]]; - } switch (srcfmt) { case DCA_MONO: - case DCA_CHANNEL: - case DCA_STEREO_TOTAL: - case DCA_STEREO_SUMDIFF: case DCA_4F2R: av_log(NULL, 0, "Not implemented!\n"); break; + case DCA_CHANNEL: case DCA_STEREO: + case DCA_STEREO_TOTAL: + case DCA_STEREO_SUMDIFF: break; case DCA_3F: - c = channel_mapping[0] * 256; - l = channel_mapping[1] * 256; - r = channel_mapping[2] * 256; + c = channel_mapping[0]; + l = channel_mapping[1]; + r = channel_mapping[2]; DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), ); break; case DCA_2F1R: - s = channel_mapping[2] * 256; - DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef), ); + s = channel_mapping[2]; + DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), ); break; case DCA_3F1R: - c = channel_mapping[0] * 256; - l = channel_mapping[1] * 256; - r = channel_mapping[2] * 256; - s = channel_mapping[3] * 256; + c = channel_mapping[0]; + l = channel_mapping[1]; + r = channel_mapping[2]; + s = channel_mapping[3]; DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), - MIX_REAR1(samples, i + s, 3, coef)); + MIX_REAR1(samples, s, 3, coef)); break; case DCA_2F2R: - sl = channel_mapping[2] * 256; - sr = channel_mapping[3] * 256; - DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef), ); + sl = channel_mapping[2]; + sr = channel_mapping[3]; + DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), ); break; case DCA_3F2R: - c = channel_mapping[0] * 256; - l = channel_mapping[1] * 256; - r = channel_mapping[2] * 256; - sl = channel_mapping[3] * 256; - sr = channel_mapping[4] * 256; + c = channel_mapping[0]; + l = channel_mapping[1]; + r = channel_mapping[2]; + sl = channel_mapping[3]; + sr = channel_mapping[4]; DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), - MIX_REAR2(samples, i + sl, i + sr, 3, coef)); + MIX_REAR2(samples, sl, sr, 3, coef)); break; } + if (lfe_present) { + int lf_buf = dca_lfe_index[srcfmt]; + int lf_idx = dca_channels[srcfmt]; + for (i = 0; i < 256; i++) { + samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0]; + samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1]; + } + } } - #ifndef decode_blockcodes /* Very compact version of the block code decoder that does not use table * look-up but is slightly slower */ -static int decode_blockcode(int code, int levels, int *values) +static int decode_blockcode(int code, int levels, int32_t *values) { int i; int offset = (levels - 1) >> 1; @@ -1099,13 +1066,13 @@ static int decode_blockcode(int code, int levels, int *values) for (i = 0; i < 4; i++) { int div = FASTDIV(code, levels); values[i] = code - offset - div * levels; - code = div; + code = div; } return code; } -static int decode_blockcodes(int code1, int code2, int levels, int *values) +static int decode_blockcodes(int code1, int code2, int levels, int32_t *values) { return decode_blockcode(code1, levels, values) | decode_blockcode(code2, levels, values + 4); @@ -1115,16 +1082,6 @@ static int decode_blockcodes(int code1, int code2, int levels, int *values) static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 }; static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 }; -#ifndef int8x8_fmul_int32 -static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale) -{ - float fscale = scale / 16.0; - int i; - for (i = 0; i < 8; i++) - dst[i] = src[i] * fscale; -} -#endif - static int dca_subsubframe(DCAContext *s, int base_channel, int block_index) { int k, l; @@ -1134,7 +1091,7 @@ static int dca_subsubframe(DCAContext *s, int base_channel, int block_index) /* FIXME */ float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index]; - LOCAL_ALIGNED_16(int, block, [8]); + LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]); /* * Audio data @@ -1147,6 +1104,8 @@ static int dca_subsubframe(DCAContext *s, int base_channel, int block_index) quant_step_table = lossy_quant_d; for (k = base_channel; k < s->prim_channels; k++) { + float rscale[DCA_SUBBANDS]; + if (get_bits_left(&s->gb) < 0) return AVERROR_INVALIDDATA; @@ -1169,12 +1128,13 @@ static int dca_subsubframe(DCAContext *s, int base_channel, int block_index) * Extract bits from the bit stream */ if (!abits) { - memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0])); + rscale[l] = 0; + memset(block + 8 * l, 0, 8 * sizeof(block[0])); } else { /* Deal with transients */ int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l]; - float rscale = quant_step_size * s->scale_factor[k][l][sfi] * - s->scalefactor_adj[k][sel]; + rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] * + s->scalefactor_adj[k][sel]; if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) { if (abits <= 7) { @@ -1186,8 +1146,8 @@ static int dca_subsubframe(DCAContext *s, int base_channel, int block_index) block_code1 = get_bits(&s->gb, size); block_code2 = get_bits(&s->gb, size); - err = decode_blockcodes(block_code1, block_code2, - levels, block); + err = decode_blockcodes(block_code1, block_code2, + levels, block + 8 * l); if (err) { av_log(s->avctx, AV_LOG_ERROR, "ERROR: block code look-up failed\n"); @@ -1196,34 +1156,48 @@ static int dca_subsubframe(DCAContext *s, int base_channel, int block_index) } else { /* no coding */ for (m = 0; m < 8; m++) - block[m] = get_sbits(&s->gb, abits - 3); + block[8 * l + m] = get_sbits(&s->gb, abits - 3); } } else { /* Huffman coded */ for (m = 0; m < 8; m++) - block[m] = get_bitalloc(&s->gb, - &dca_smpl_bitalloc[abits], sel); + block[8 * l + m] = get_bitalloc(&s->gb, + &dca_smpl_bitalloc[abits], sel); } - - s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l], - block, rscale, 8); } + } + s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0], + block, rscale, 8 * s->vq_start_subband[k]); + + for (l = 0; l < s->vq_start_subband[k]; l++) { + int m; /* * Inverse ADPCM if in prediction mode */ if (s->prediction_mode[k][l]) { int n; - for (m = 0; m < 8; m++) { - for (n = 1; n <= 4; n++) + if (s->predictor_history) + subband_samples[k][l][0] += (adpcm_vb[s->prediction_vq[k][l]][0] * + s->subband_samples_hist[k][l][3] + + adpcm_vb[s->prediction_vq[k][l]][1] * + s->subband_samples_hist[k][l][2] + + adpcm_vb[s->prediction_vq[k][l]][2] * + s->subband_samples_hist[k][l][1] + + adpcm_vb[s->prediction_vq[k][l]][3] * + s->subband_samples_hist[k][l][0]) * + (1.0f / 8192); + for (m = 1; m < 8; m++) { + float sum = adpcm_vb[s->prediction_vq[k][l]][0] * + subband_samples[k][l][m - 1]; + for (n = 2; n <= 4; n++) if (m >= n) - subband_samples[k][l][m] += - (adpcm_vb[s->prediction_vq[k][l]][n - 1] * - subband_samples[k][l][m - n] / 8192); + sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] * + subband_samples[k][l][m - n]; else if (s->predictor_history) - subband_samples[k][l][m] += - (adpcm_vb[s->prediction_vq[k][l]][n - 1] * - s->subband_samples_hist[k][l][m - n + 4] / 8192); + sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] * + s->subband_samples_hist[k][l][m - n + 4]; + subband_samples[k][l][m] += sum * 1.0f / 8192; } } } @@ -1231,20 +1205,16 @@ static int dca_subsubframe(DCAContext *s, int base_channel, int block_index) /* * Decode VQ encoded high frequencies */ - for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) { - /* 1 vector -> 32 samples but we only need the 8 samples - * for this subsubframe. */ - int hfvq = s->high_freq_vq[k][l]; - + if (s->subband_activity[k] > s->vq_start_subband[k]) { if (!s->debug_flag & 0x01) { av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n"); s->debug_flag |= 0x01; } - - int8x8_fmul_int32(subband_samples[k][l], - &high_freq_vq[hfvq][subsubframe * 8], - s->scale_factor[k][l][0]); + s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k], + high_freq_vq, subsubframe * 8, + s->scale_factor[k], s->vq_start_subband[k], + s->subband_activity[k]); } } @@ -1256,15 +1226,14 @@ static int dca_subsubframe(DCAContext *s, int base_channel, int block_index) #endif } else { av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n"); + return AVERROR_INVALIDDATA; } } /* Backup predictor history for adpcm */ for (k = base_channel; k < s->prim_channels; k++) for (l = 0; l < s->vq_start_subband[k]; l++) - memcpy(s->subband_samples_hist[k][l], - &subband_samples[k][l][4], - 4 * sizeof(subband_samples[0][0][0])); + AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]); return 0; } @@ -1276,34 +1245,34 @@ static int dca_filter_channels(DCAContext *s, int block_index) /* 32 subbands QMF */ for (k = 0; k < s->prim_channels; k++) { -/* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0, - 0, 8388608.0, 8388608.0 };*/ - qmf_32_subbands(s, k, subband_samples[k], - &s->samples[256 * s->channel_order_tab[k]], - M_SQRT1_2 * s->scale_bias /* pcm_to_double[s->source_pcm_res] */); - } - - /* Down mixing */ - if (s->avctx->request_channels == 2 && s->prim_channels > 2) { - dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab); + if (s->channel_order_tab[k] >= 0) + qmf_32_subbands(s, k, subband_samples[k], + s->samples_chanptr[s->channel_order_tab[k]], + M_SQRT1_2 / 32768.0); } /* Generate LFE samples for this subsubframe FIXME!!! */ - if (s->output & DCA_LFE) { + if (s->lfe) { lfe_interpolation_fir(s, s->lfe, 2 * s->lfe, s->lfe_data + 2 * s->lfe * (block_index + 4), - &s->samples[256 * dca_lfe_index[s->amode]], - (1.0 / 256.0) * s->scale_bias); + s->samples_chanptr[dca_lfe_index[s->amode]]); /* Outputs 20bits pcm samples */ } + /* Downmixing to Stereo */ + if (s->prim_channels + !!s->lfe > 2 && + s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) { + dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef, + s->channel_order_tab); + } + return 0; } - static int dca_subframe_footer(DCAContext *s, int base_channel) { - int aux_data_count = 0, i; + int in, out, aux_data_count, aux_data_end, reserved; + uint32_t nsyncaux; /* * Unpack optional information @@ -1314,13 +1283,89 @@ static int dca_subframe_footer(DCAContext *s, int base_channel) if (s->timestamp) skip_bits_long(&s->gb, 32); - if (s->aux_data) + if (s->aux_data) { aux_data_count = get_bits(&s->gb, 6); - for (i = 0; i < aux_data_count; i++) - get_bits(&s->gb, 8); + // align (32-bit) + skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31); + + aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb); + + if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) { + av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n", + nsyncaux); + return AVERROR_INVALIDDATA; + } + + if (get_bits1(&s->gb)) { // bAUXTimeStampFlag + avpriv_request_sample(s->avctx, + "Auxiliary Decode Time Stamp Flag"); + // align (4-bit) + skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4); + // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4) + skip_bits_long(&s->gb, 44); + } - if (s->crc_present && (s->downmix || s->dynrange)) + if ((s->core_downmix = get_bits1(&s->gb))) { + int am = get_bits(&s->gb, 3); + switch (am) { + case 0: + s->core_downmix_amode = DCA_MONO; + break; + case 1: + s->core_downmix_amode = DCA_STEREO; + break; + case 2: + s->core_downmix_amode = DCA_STEREO_TOTAL; + break; + case 3: + s->core_downmix_amode = DCA_3F; + break; + case 4: + s->core_downmix_amode = DCA_2F1R; + break; + case 5: + s->core_downmix_amode = DCA_2F2R; + break; + case 6: + s->core_downmix_amode = DCA_3F1R; + break; + default: + av_log(s->avctx, AV_LOG_ERROR, + "Invalid mode %d for embedded downmix coefficients\n", + am); + return AVERROR_INVALIDDATA; + } + for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) { + for (in = 0; in < s->prim_channels + !!s->lfe; in++) { + uint16_t tmp = get_bits(&s->gb, 9); + if ((tmp & 0xFF) > 241) { + av_log(s->avctx, AV_LOG_ERROR, + "Invalid downmix coefficient code %"PRIu16"\n", + tmp); + return AVERROR_INVALIDDATA; + } + s->core_downmix_codes[in][out] = tmp; + } + } + } + + align_get_bits(&s->gb); // byte align + skip_bits(&s->gb, 16); // nAUXCRC16 + + // additional data (reserved, cf. ETSI TS 102 114 V1.4.1) + if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) { + av_log(s->avctx, AV_LOG_ERROR, + "Overread auxiliary data by %d bits\n", -reserved); + return AVERROR_INVALIDDATA; + } else if (reserved) { + avpriv_request_sample(s->avctx, + "Core auxiliary data reserved content"); + skip_bits_long(&s->gb, reserved); + } + } + + if (s->crc_present && s->dynrange) get_bits(&s->gb, 16); } @@ -1417,11 +1462,11 @@ static int dca_exss_parse_asset_header(DCAContext *s) { int header_pos = get_bits_count(&s->gb); int header_size; - int channels; + int channels = 0; int embedded_stereo = 0; int embedded_6ch = 0; int drc_code_present; - int extensions_mask; + int extensions_mask = 0; int i, j; if (get_bits_left(&s->gb) < 16) @@ -1485,7 +1530,6 @@ static int dca_exss_parse_asset_header(DCAContext *s) skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes } } - } else { skip_bits(&s->gb, 3); // representation type } @@ -1528,10 +1572,18 @@ static int dca_exss_parse_asset_header(DCAContext *s) } switch (get_bits(&s->gb, 2)) { - case 0: extensions_mask = get_bits(&s->gb, 12); break; - case 1: extensions_mask = DCA_EXT_EXSS_XLL; break; - case 2: extensions_mask = DCA_EXT_EXSS_LBR; break; - case 3: extensions_mask = 0; /* aux coding */ break; + case 0: + extensions_mask = get_bits(&s->gb, 12); + break; + case 1: + extensions_mask = DCA_EXT_EXSS_XLL; + break; + case 2: + extensions_mask = DCA_EXT_EXSS_LBR; + break; + case 3: + extensions_mask = 0; /* aux coding */ + break; } /* not parsed further, we were only interested in the extensions mask */ @@ -1596,14 +1648,15 @@ static void dca_exss_parse_header(DCAContext *s) num_audiop = get_bits(&s->gb, 3) + 1; if (num_audiop > 1) { - av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations."); + avpriv_request_sample(s->avctx, + "Multiple DTS-HD audio presentations"); /* ignore such streams for now */ return; } num_assets = get_bits(&s->gb, 3) + 1; if (num_assets > 1) { - av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets."); + avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets"); /* ignore such streams for now */ return; } @@ -1643,6 +1696,13 @@ static void dca_exss_parse_header(DCAContext *s) * from the asset header */ } +static float dca_dmix_code(unsigned code) +{ + int sign = (code >> 8) - 1; + code &= 0xff; + return ((dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1U << 15)); +} + /** * Main frame decoding function * FIXME add arguments @@ -1650,19 +1710,18 @@ static void dca_exss_parse_header(DCAContext *s) static int dca_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { + AVFrame *frame = data; const uint8_t *buf = avpkt->data; - int buf_size = avpkt->size; + int buf_size = avpkt->size; int lfe_samples; int num_core_channels = 0; int i, ret; - float *samples_flt; - int16_t *samples_s16; + float **samples_flt; DCAContext *s = avctx->priv_data; - int channels; + int channels, full_channels; int core_ss_end; - s->xch_present = 0; s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer, @@ -1672,12 +1731,11 @@ static int dca_decode_frame(AVCodecContext *avctx, void *data, return AVERROR_INVALIDDATA; } - init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8); if ((ret = dca_parse_frame_header(s)) < 0) { - //seems like the frame is corrupt, try with the next one + // seems like the frame is corrupt, try with the next one return ret; } - //set AVCodec values with parsed data + // set AVCodec values with parsed data avctx->sample_rate = s->sample_rate; avctx->bit_rate = s->bit_rate; @@ -1703,7 +1761,6 @@ static int dca_decode_frame(AVCodecContext *avctx, void *data, /* only scan for extensions if ext_descr was unknown or indicated a * supported XCh extension */ if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) { - /* if ext_descr was unknown, clear s->core_ext_mask so that the * extensions scan can fill it up */ s->core_ext_mask = FFMAX(s->core_ext_mask, 0); @@ -1734,8 +1791,9 @@ static int dca_decode_frame(AVCodecContext *avctx, void *data, /* extension amode(number of channels in extension) should be 1 */ /* AFAIK XCh is not used for more channels */ if ((ext_amode = get_bits(&s->gb, 4)) != 1) { - av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not" - " supported!\n", ext_amode); + av_log(avctx, AV_LOG_ERROR, + "XCh extension amode %d not supported!\n", + ext_amode); continue; } @@ -1793,13 +1851,30 @@ static int dca_decode_frame(AVCodecContext *avctx, void *data, avctx->profile = s->profile; - channels = s->prim_channels + !!s->lfe; + full_channels = channels = s->prim_channels + !!s->lfe; if (s->amode < 16) { avctx->channel_layout = dca_core_channel_layout[s->amode]; - if (s->xch_present && (!avctx->request_channels || - avctx->request_channels > num_core_channels + !!s->lfe)) { + if (s->prim_channels + !!s->lfe > 2 && + avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) { + /* + * Neither the core's auxiliary data nor our default tables contain + * downmix coefficients for the additional channel coded in the XCh + * extension, so when we're doing a Stereo downmix, don't decode it. + */ + s->xch_disable = 1; + } + +#if FF_API_REQUEST_CHANNELS +FF_DISABLE_DEPRECATION_WARNINGS + if (s->xch_present && !s->xch_disable && + (!avctx->request_channels || + avctx->request_channels > num_core_channels + !!s->lfe)) { +FF_ENABLE_DEPRECATION_WARNINGS +#else + if (s->xch_present && !s->xch_disable) { +#endif avctx->channel_layout |= AV_CH_BACK_CENTER; if (s->lfe) { avctx->channel_layout |= AV_CH_LOW_FREQUENCY; @@ -1808,7 +1883,7 @@ static int dca_decode_frame(AVCodecContext *avctx, void *data, s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode]; } } else { - channels = num_core_channels + !!s->lfe; + channels = num_core_channels + !!s->lfe; s->xch_present = 0; /* disable further xch processing */ if (s->lfe) { avctx->channel_layout |= AV_CH_LOW_FREQUENCY; @@ -1821,65 +1896,107 @@ static int dca_decode_frame(AVCodecContext *avctx, void *data, s->channel_order_tab[channels - 1 - !!s->lfe] < 0) return AVERROR_INVALIDDATA; - if (avctx->request_channels == 2 && s->prim_channels > 2) { - channels = 2; - s->output = DCA_STEREO; + if (num_core_channels + !!s->lfe > 2 && + avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) { + channels = 2; + s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO; avctx->channel_layout = AV_CH_LAYOUT_STEREO; + + /* Stereo downmix coefficients + * + * The decoder can only downmix to 2-channel, so we need to ensure + * embedded downmix coefficients are actually targeting 2-channel. + */ + if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO || + s->core_downmix_amode == DCA_STEREO_TOTAL)) { + for (i = 0; i < num_core_channels + !!s->lfe; i++) { + /* Range checked earlier */ + s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]); + s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]); + } + s->output = s->core_downmix_amode; + } else { + int am = s->amode & DCA_CHANNEL_MASK; + if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) { + av_log(s->avctx, AV_LOG_ERROR, + "Invalid channel mode %d\n", am); + return AVERROR_INVALIDDATA; + } + if (num_core_channels + !!s->lfe > + FF_ARRAY_ELEMS(dca_default_coeffs[0])) { + avpriv_request_sample(s->avctx, "Downmixing %d channels", + s->prim_channels + !!s->lfe); + return AVERROR_PATCHWELCOME; + } + for (i = 0; i < num_core_channels + !!s->lfe; i++) { + s->downmix_coef[i][0] = dca_default_coeffs[am][i][0]; + s->downmix_coef[i][1] = dca_default_coeffs[am][i][1]; + } + } + av_dlog(s->avctx, "Stereo downmix coeffs:\n"); + for (i = 0; i < num_core_channels + !!s->lfe; i++) { + av_dlog(s->avctx, "L, input channel %d = %f\n", i, + s->downmix_coef[i][0]); + av_dlog(s->avctx, "R, input channel %d = %f\n", i, + s->downmix_coef[i][1]); + } + av_dlog(s->avctx, "\n"); } } else { av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode); return AVERROR_INVALIDDATA; } - - - /* There is nothing that prevents a dts frame to change channel configuration - but Libav doesn't support that so only set the channels if it is previously - unset. Ideally during the first probe for channels the crc should be checked - and only set avctx->channels when the crc is ok. Right now the decoder could - set the channels based on a broken first frame.*/ - if (s->is_channels_set == 0) { - s->is_channels_set = 1; - avctx->channels = channels; - } - if (avctx->channels != channels) { - av_log(avctx, AV_LOG_ERROR, "DCA decoder does not support number of " - "channels changing in stream. Skipping frame.\n"); - return AVERROR_PATCHWELCOME; - } + avctx->channels = channels; /* get output buffer */ - s->frame.nb_samples = 256 * (s->sample_blocks / 8); - if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) { + frame->nb_samples = 256 * (s->sample_blocks / 8); + if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } - samples_flt = (float *) s->frame.data[0]; - samples_s16 = (int16_t *) s->frame.data[0]; + samples_flt = (float **) frame->extended_data; + + /* allocate buffer for extra channels if downmixing */ + if (avctx->channels < full_channels) { + ret = av_samples_get_buffer_size(NULL, full_channels - channels, + frame->nb_samples, + avctx->sample_fmt, 0); + if (ret < 0) + return ret; + + av_fast_malloc(&s->extra_channels_buffer, + &s->extra_channels_buffer_size, ret); + if (!s->extra_channels_buffer) + return AVERROR(ENOMEM); + + ret = av_samples_fill_arrays((uint8_t **) s->extra_channels, NULL, + s->extra_channels_buffer, + full_channels - channels, + frame->nb_samples, avctx->sample_fmt, 0); + if (ret < 0) + return ret; + } /* filter to get final output */ for (i = 0; i < (s->sample_blocks / 8); i++) { + int ch; + + for (ch = 0; ch < channels; ch++) + s->samples_chanptr[ch] = samples_flt[ch] + i * 256; + for (; ch < full_channels; ch++) + s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256; + dca_filter_channels(s, i); /* If this was marked as a DTS-ES stream we need to subtract back- */ /* channel from SL & SR to remove matrixed back-channel signal */ if ((s->source_pcm_res & 1) && s->xch_present) { - float *back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256; - float *lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256; - float *rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256; + float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]]; + float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]]; + float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]]; s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256); s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256); } - - if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) { - s->fmt_conv.float_interleave(samples_flt, s->samples_chanptr, 256, - channels); - samples_flt += 256 * channels; - } else { - s->fmt_conv.float_to_int16_interleave(samples_s16, - s->samples_chanptr, 256, - channels); - samples_s16 += 256 * channels; - } } /* update lfe history */ @@ -1887,14 +2004,20 @@ static int dca_decode_frame(AVCodecContext *avctx, void *data, for (i = 0; i < 2 * s->lfe * 4; i++) s->lfe_data[i] = s->lfe_data[i + lfe_samples]; - *got_frame_ptr = 1; - *(AVFrame *) data = s->frame; + /* AVMatrixEncoding + * + * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */ + ret = ff_side_data_update_matrix_encoding(frame, + (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ? + AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE); + if (ret < 0) + return ret; + + *got_frame_ptr = 1; return buf_size; } - - /** * DCA initialization * @@ -1904,7 +2027,6 @@ static int dca_decode_frame(AVCodecContext *avctx, void *data, static av_cold int dca_decode_init(AVCodecContext *avctx) { DCAContext *s = avctx->priv_data; - int i; s->avctx = avctx; dca_init_vlcs(); @@ -1915,25 +2037,18 @@ static av_cold int dca_decode_init(AVCodecContext *avctx) ff_dcadsp_init(&s->dcadsp); ff_fmt_convert_init(&s->fmt_conv, avctx); - for (i = 0; i < DCA_PRIM_CHANNELS_MAX + 1; i++) - s->samples_chanptr[i] = s->samples + i * 256; - - if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) { - avctx->sample_fmt = AV_SAMPLE_FMT_FLT; - s->scale_bias = 1.0 / 32768.0; - } else { - avctx->sample_fmt = AV_SAMPLE_FMT_S16; - s->scale_bias = 1.0; - } + avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; /* allow downmixing to stereo */ - if (avctx->channels > 0 && avctx->request_channels < avctx->channels && - avctx->request_channels == 2) { - avctx->channels = avctx->request_channels; - } - - avcodec_get_frame_defaults(&s->frame); - avctx->coded_frame = &s->frame; +#if FF_API_REQUEST_CHANNELS +FF_DISABLE_DEPRECATION_WARNINGS + if (avctx->request_channels == 2) + avctx->request_channel_layout = AV_CH_LAYOUT_STEREO; +FF_ENABLE_DEPRECATION_WARNINGS +#endif + if (avctx->channels > 2 && + avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) + avctx->channels = 2; return 0; } @@ -1942,6 +2057,7 @@ static av_cold int dca_decode_end(AVCodecContext *avctx) { DCAContext *s = avctx->priv_data; ff_mdct_end(&s->imdct); + av_freep(&s->extra_channels_buffer); return 0; } @@ -1954,18 +2070,30 @@ static const AVProfile profiles[] = { { FF_PROFILE_UNKNOWN }, }; +static const AVOption options[] = { + { "disable_xch", "disable decoding of the XCh extension", offsetof(DCAContext, xch_disable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM }, + { NULL }, +}; + +static const AVClass dca_decoder_class = { + .class_name = "DCA decoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + AVCodec ff_dca_decoder = { .name = "dca", + .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"), .type = AVMEDIA_TYPE_AUDIO, .id = AV_CODEC_ID_DTS, .priv_data_size = sizeof(DCAContext), .init = dca_decode_init, .decode = dca_decode_frame, .close = dca_decode_end, - .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"), .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1, - .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT, - AV_SAMPLE_FMT_S16, + .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }, .profiles = NULL_IF_CONFIG_SMALL(profiles), + .priv_class = &dca_decoder_class, };