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_MAX_FRAME_SIZE 16384
46 #define DCA_MAX_EXSS_HEADER_SIZE 4096
48 #define DCA_BUFFER_PADDING_SIZE 1024
50 enum DCAExtensionMask {
51 DCA_EXT_CORE = 0x001, ///< core in core substream
52 DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
53 DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
54 DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
55 DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
56 DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
57 DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
58 DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
59 DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
60 DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
63 typedef struct DCAContext {
64 AVClass *class; ///< class for AVOptions
65 AVCodecContext *avctx;
67 int frame_type; ///< type of the current frame
68 int samples_deficit; ///< deficit sample count
69 int crc_present; ///< crc is present in the bitstream
70 int sample_blocks; ///< number of PCM sample blocks
71 int frame_size; ///< primary frame byte size
72 int amode; ///< audio channels arrangement
73 int sample_rate; ///< audio sampling rate
74 int bit_rate; ///< transmission bit rate
75 int bit_rate_index; ///< transmission bit rate index
77 int dynrange; ///< embedded dynamic range flag
78 int timestamp; ///< embedded time stamp flag
79 int aux_data; ///< auxiliary data flag
80 int hdcd; ///< source material is mastered in HDCD
81 int ext_descr; ///< extension audio descriptor flag
82 int ext_coding; ///< extended coding flag
83 int aspf; ///< audio sync word insertion flag
84 int lfe; ///< low frequency effects flag
85 int predictor_history; ///< predictor history flag
86 int header_crc; ///< header crc check bytes
87 int multirate_inter; ///< multirate interpolator switch
88 int version; ///< encoder software revision
89 int copy_history; ///< copy history
90 int source_pcm_res; ///< source pcm resolution
91 int front_sum; ///< front sum/difference flag
92 int surround_sum; ///< surround sum/difference flag
93 int dialog_norm; ///< dialog normalisation parameter
95 /* Primary audio coding header */
96 int subframes; ///< number of subframes
97 int total_channels; ///< number of channels including extensions
98 int prim_channels; ///< number of primary audio channels
99 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
100 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
101 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
102 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
103 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
104 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
105 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
106 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
108 /* Primary audio coding side information */
109 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
110 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
111 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
112 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
113 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
114 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
115 int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];///< scale factors (2 if transient)
116 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
117 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
118 float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients
119 int dynrange_coef; ///< dynamic range coefficient
121 /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
122 * Input: primary audio channels (incl. LFE if present)
123 * Output: downmix audio channels (up to 4, no LFE) */
124 uint8_t core_downmix; ///< embedded downmix coefficients available
125 uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix
126 uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes)
128 int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
130 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
131 int lfe_scale_factor;
133 /* Subband samples history (for ADPCM) */
134 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
135 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
136 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
137 int hist_index[DCA_PRIM_CHANNELS_MAX];
138 DECLARE_ALIGNED(32, float, raXin)[32];
140 int output; ///< type of output
142 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
143 float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
144 float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
145 uint8_t *extra_channels_buffer;
146 unsigned int extra_channels_buffer_size;
148 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
149 int dca_buffer_size; ///< how much data is in the dca_buffer
151 const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
153 /* Current position in DCA frame */
154 int current_subframe;
155 int current_subsubframe;
157 int core_ext_mask; ///< present extensions in the core substream
159 /* XCh extension information */
160 int xch_present; ///< XCh extension present and valid
161 int xch_base_channel; ///< index of first (only) channel containing XCH data
162 int xch_disable; ///< whether the XCh extension should be decoded or not
164 /* ExSS header parser */
165 int static_fields; ///< static fields present
166 int mix_metadata; ///< mixing metadata present
167 int num_mix_configs; ///< number of mix out configurations
168 int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
172 int debug_flag; ///< used for suppressing repeated error messages output
173 AVFloatDSPContext fdsp;
175 SynthFilterContext synth;
176 DCADSPContext dcadsp;
177 FmtConvertContext fmt_conv;
180 extern av_export const uint32_t avpriv_dca_sample_rates[16];
183 * Convert bitstream to one representation based on sync marker
185 int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
188 void ff_dca_exss_parse_header(DCAContext *s);
190 #endif /* AVCODEC_DCA_H */