2 * DCA compatible decoder
3 * Copyright (C) 2004 Gildas Bazin
4 * Copyright (C) 2004 Benjamin Zores
5 * Copyright (C) 2006 Benjamin Larsson
6 * Copyright (C) 2007 Konstantin Shishkov
8 * This file is part of Libav.
10 * Libav is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * Libav is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "libavutil/float_dsp.h"
31 #include "libavutil/internal.h"
35 #include "fmtconvert.h"
38 #define DCA_PRIM_CHANNELS_MAX (7)
39 #define DCA_ABITS_MAX (32) /* Should be 28 */
40 #define DCA_SUBSUBFRAMES_MAX (4)
41 #define DCA_SUBFRAMES_MAX (16)
42 #define DCA_BLOCKS_MAX (16)
43 #define DCA_LFE_MAX (3)
45 #define DCA_PRIM_CHANNELS_MAX (7)
46 #define DCA_ABITS_MAX (32) /* Should be 28 */
47 #define DCA_SUBSUBFRAMES_MAX (4)
48 #define DCA_SUBFRAMES_MAX (16)
49 #define DCA_BLOCKS_MAX (16)
50 #define DCA_LFE_MAX (3)
51 #define DCA_XLL_FBANDS_MAX (4)
52 #define DCA_XLL_SEGMENTS_MAX (16)
53 #define DCA_XLL_CHSETS_MAX (16)
54 #define DCA_XLL_CHANNELS_MAX (16)
55 #define DCA_XLL_AORDER_MAX (15)
57 /* Arbitrary limit; not sure what the maximum really is, but much larger. */
58 #define DCA_XLL_DMIX_NCOEFFS_MAX (18)
60 #define DCA_MAX_FRAME_SIZE 16384
61 #define DCA_MAX_EXSS_HEADER_SIZE 4096
63 #define DCA_BUFFER_PADDING_SIZE 1024
65 enum DCAExtensionMask {
66 DCA_EXT_CORE = 0x001, ///< core in core substream
67 DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
68 DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
69 DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
70 DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
71 DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
72 DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
73 DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
74 DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
75 DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
78 typedef struct XllChSetSubHeader {
79 int channels; ///< number of channels in channel set, at most 16
80 int residual_encode; ///< residual channel encoding
81 int bit_resolution; ///< input sample bit-width
82 int bit_width; ///< original input sample bit-width
83 int sampling_frequency; ///< sampling frequency
84 int samp_freq_interp; ///< sampling frequency interpolation multiplier
85 int replacement_set; ///< replacement channel set group
86 int active_replace_set; ///< current channel set is active channel set
88 int downmix_coeff_code_embedded;
91 int hier_chset; ///< hierarchical channel set
93 int downmix_coeffs[DCA_XLL_DMIX_NCOEFFS_MAX];
96 int mapping_coeffs_present;
99 /* m_nOrigChanOrder */
100 uint8_t orig_chan_order[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
101 uint8_t orig_chan_order_inv[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
102 /* Coefficients for channel pairs (at most 8), m_anPWChPairsCoeffs */
103 int8_t pw_ch_pairs_coeffs[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX/2];
104 /* m_nCurrHighestLPCOrder */
105 uint8_t adapt_order_max[DCA_XLL_FBANDS_MAX];
106 /* m_pnAdaptPredOrder */
107 uint8_t adapt_order[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
108 /* m_pnFixedPredOrder */
109 uint8_t fixed_order[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
110 /* m_pnLPCReflCoeffsQInd, unsigned version */
111 uint8_t lpc_refl_coeffs_q_ind[DCA_XLL_FBANDS_MAX]
112 [DCA_XLL_CHANNELS_MAX][DCA_XLL_AORDER_MAX];
114 int lsb_fsize[DCA_XLL_FBANDS_MAX];
115 int8_t scalable_lsbs[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
116 int8_t bit_width_adj_per_ch[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
119 typedef struct XllNavi {
120 GetBitContext gb; // Context for parsing the data segments
121 unsigned band_size[DCA_XLL_FBANDS_MAX];
122 unsigned segment_size[DCA_XLL_FBANDS_MAX][DCA_XLL_SEGMENTS_MAX];
123 unsigned chset_size[DCA_XLL_FBANDS_MAX][DCA_XLL_SEGMENTS_MAX][DCA_XLL_CHSETS_MAX];
126 typedef struct QMF64_table {
127 float dct4_coeff[32][32];
128 float dct2_coeff[32][32];
133 /* Primary audio coding header */
134 typedef struct DCAAudioHeader {
135 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
136 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
137 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
138 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
139 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
140 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
141 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
142 uint32_t scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
144 int subframes; ///< number of subframes
145 int total_channels; ///< number of channels including extensions
146 int prim_channels; ///< number of primary audio channels
149 typedef struct DCAChan {
150 DECLARE_ALIGNED(32, int32_t, subband_samples)[DCA_BLOCKS_MAX][DCA_SUBBANDS][SAMPLES_PER_SUBBAND];
152 /* Subband samples history (for ADPCM) */
153 DECLARE_ALIGNED(32, int32_t, subband_samples_hist)[DCA_SUBBANDS][4];
156 /* Half size is sufficient for core decoding, but for 96 kHz data
157 * we need QMF with 64 subbands and 1024 samples. */
158 DECLARE_ALIGNED(32, float, subband_fir_hist)[1024];
159 DECLARE_ALIGNED(32, float, subband_fir_noidea)[64];
161 /* Primary audio coding side information */
162 int prediction_mode[DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
163 int prediction_vq[DCA_SUBBANDS]; ///< prediction VQ coefs
164 int bitalloc[DCA_SUBBANDS]; ///< bit allocation index
165 int transition_mode[DCA_SUBBANDS]; ///< transition mode (transients)
166 int32_t scale_factor[DCA_SUBBANDS][2];///< scale factors (2 if transient)
167 int joint_huff; ///< joint subband scale factors codebook
168 int joint_scale_factor[DCA_SUBBANDS]; ///< joint subband scale factors
170 int32_t high_freq_vq[DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
174 typedef struct DCAContext {
175 AVClass *class; ///< class for AVOptions
176 AVCodecContext *avctx;
178 int frame_type; ///< type of the current frame
179 int samples_deficit; ///< deficit sample count
180 int crc_present; ///< crc is present in the bitstream
181 int sample_blocks; ///< number of PCM sample blocks
182 int frame_size; ///< primary frame byte size
183 int amode; ///< audio channels arrangement
184 int sample_rate; ///< audio sampling rate
185 int bit_rate; ///< transmission bit rate
186 int bit_rate_index; ///< transmission bit rate index
188 int dynrange; ///< embedded dynamic range flag
189 int timestamp; ///< embedded time stamp flag
190 int aux_data; ///< auxiliary data flag
191 int hdcd; ///< source material is mastered in HDCD
192 int ext_descr; ///< extension audio descriptor flag
193 int ext_coding; ///< extended coding flag
194 int aspf; ///< audio sync word insertion flag
195 int lfe; ///< low frequency effects flag
196 int predictor_history; ///< predictor history flag
197 int header_crc; ///< header crc check bytes
198 int multirate_inter; ///< multirate interpolator switch
199 int version; ///< encoder software revision
200 int copy_history; ///< copy history
201 int source_pcm_res; ///< source pcm resolution
202 int front_sum; ///< front sum/difference flag
203 int surround_sum; ///< surround sum/difference flag
204 int dialog_norm; ///< dialog normalisation parameter
206 /* Primary audio coding header */
207 DCAAudioHeader audio_header;
209 /* Primary audio coding side information */
210 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
211 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
212 float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients
213 int dynrange_coef; ///< dynamic range coefficient
215 /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
216 * Input: primary audio channels (incl. LFE if present)
217 * Output: downmix audio channels (up to 4, no LFE) */
218 uint8_t core_downmix; ///< embedded downmix coefficients available
219 uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix
220 uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes)
223 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
224 int lfe_scale_factor;
226 /* Subband samples history (for ADPCM) */
227 DECLARE_ALIGNED(32, float, raXin)[32];
229 DCAChan dca_chan[DCA_PRIM_CHANNELS_MAX];
231 int output; ///< type of output
233 float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
234 float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
235 uint8_t *extra_channels_buffer;
236 unsigned int extra_channels_buffer_size;
238 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
239 int dca_buffer_size; ///< how much data is in the dca_buffer
241 const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
243 /* Current position in DCA frame */
244 int current_subframe;
245 int current_subsubframe;
247 int core_ext_mask; ///< present extensions in the core substream
248 int exss_ext_mask; ///< Non-core extensions
250 /* XCh extension information */
251 int xch_present; ///< XCh extension present and valid
252 int xch_base_channel; ///< index of first (only) channel containing XCH data
253 int xch_disable; ///< whether the XCh extension should be decoded or not
255 /* XLL extension information */
257 int xll_nch_sets; ///< number of channel sets per frame
258 int xll_channels; ///< total number of channels (in all channel sets)
259 int xll_residual_channels; ///< number of residual channels
260 int xll_segments; ///< number of segments per frame
261 int xll_log_smpl_in_seg; ///< supposedly this is "nBits4SamplLoci"
262 int xll_smpl_in_seg; ///< samples in segment per one frequency band for the first channel set
263 int xll_bits4seg_size; ///< number of bits used to read segment size
264 int xll_banddata_crc; ///< presence of CRC16 within each frequency band
265 int xll_scalable_lsb;
266 int xll_bits4ch_mask; ///< channel position mask
267 int xll_fixed_lsb_width;
268 XllChSetSubHeader xll_chsets[DCA_XLL_CHSETS_MAX];
271 unsigned int xll_sample_buf_size;
273 /* ExSS header parser */
274 int static_fields; ///< static fields present
275 int mix_metadata; ///< mixing metadata present
276 int num_mix_configs; ///< number of mix out configurations
277 int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
280 int one2one_map_chtospkr;
282 int debug_flag; ///< used for suppressing repeated error messages output
283 AVFloatDSPContext fdsp;
285 SynthFilterContext synth;
286 DCADSPContext dcadsp;
287 QMF64_table *qmf64_table;
288 FmtConvertContext fmt_conv;
291 extern av_export const uint32_t avpriv_dca_sample_rates[16];
294 * Convert bitstream to one representation based on sync marker
296 int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
299 void ff_dca_exss_parse_header(DCAContext *s);
301 int ff_dca_xll_decode_header(DCAContext *s);
302 int ff_dca_xll_decode_navi(DCAContext *s, int asset_end);
303 int ff_dca_xll_decode_audio(DCAContext *s, AVFrame *frame);
305 #endif /* AVCODEC_DCA_H */