ffmpeg / libavcodec / dca.c @ fbdcdaee
History | View | Annotate | Download (68.7 KB)
1 |
/*
|
---|---|
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
|
7 |
*
|
8 |
* This file is part of FFmpeg.
|
9 |
*
|
10 |
* FFmpeg 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.
|
14 |
*
|
15 |
* FFmpeg 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.
|
19 |
*
|
20 |
* You should have received a copy of the GNU Lesser General Public
|
21 |
* License along with FFmpeg; if not, write to the Free Software
|
22 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
23 |
*/
|
24 |
|
25 |
#include <math.h> |
26 |
#include <stddef.h> |
27 |
#include <stdio.h> |
28 |
|
29 |
#include "libavutil/common.h" |
30 |
#include "libavutil/intmath.h" |
31 |
#include "libavutil/intreadwrite.h" |
32 |
#include "libavcore/audioconvert.h" |
33 |
#include "avcodec.h" |
34 |
#include "dsputil.h" |
35 |
#include "fft.h" |
36 |
#include "get_bits.h" |
37 |
#include "put_bits.h" |
38 |
#include "dcadata.h" |
39 |
#include "dcahuff.h" |
40 |
#include "dca.h" |
41 |
#include "synth_filter.h" |
42 |
#include "dcadsp.h" |
43 |
#include "fmtconvert.h" |
44 |
|
45 |
//#define TRACE
|
46 |
|
47 |
#define DCA_PRIM_CHANNELS_MAX (7) |
48 |
#define DCA_SUBBANDS (32) |
49 |
#define DCA_ABITS_MAX (32) /* Should be 28 */ |
50 |
#define DCA_SUBSUBFRAMES_MAX (4) |
51 |
#define DCA_SUBFRAMES_MAX (16) |
52 |
#define DCA_BLOCKS_MAX (16) |
53 |
#define DCA_LFE_MAX (3) |
54 |
|
55 |
enum DCAMode {
|
56 |
DCA_MONO = 0,
|
57 |
DCA_CHANNEL, |
58 |
DCA_STEREO, |
59 |
DCA_STEREO_SUMDIFF, |
60 |
DCA_STEREO_TOTAL, |
61 |
DCA_3F, |
62 |
DCA_2F1R, |
63 |
DCA_3F1R, |
64 |
DCA_2F2R, |
65 |
DCA_3F2R, |
66 |
DCA_4F2R |
67 |
}; |
68 |
|
69 |
/* these are unconfirmed but should be mostly correct */
|
70 |
enum DCAExSSSpeakerMask {
|
71 |
DCA_EXSS_FRONT_CENTER = 0x0001,
|
72 |
DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
|
73 |
DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
|
74 |
DCA_EXSS_LFE = 0x0008,
|
75 |
DCA_EXSS_REAR_CENTER = 0x0010,
|
76 |
DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
|
77 |
DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
|
78 |
DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
|
79 |
DCA_EXSS_OVERHEAD = 0x0100,
|
80 |
DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
|
81 |
DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
|
82 |
DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
|
83 |
DCA_EXSS_LFE2 = 0x1000,
|
84 |
DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
|
85 |
DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
|
86 |
DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
|
87 |
}; |
88 |
|
89 |
enum DCAExtensionMask {
|
90 |
DCA_EXT_CORE = 0x001, ///< core in core substream |
91 |
DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream |
92 |
DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream |
93 |
DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream |
94 |
DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream) |
95 |
DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS |
96 |
DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS |
97 |
DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS |
98 |
DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS |
99 |
DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS |
100 |
}; |
101 |
|
102 |
/* Tables for mapping dts channel configurations to libavcodec multichannel api.
|
103 |
* Some compromises have been made for special configurations. Most configurations
|
104 |
* are never used so complete accuracy is not needed.
|
105 |
*
|
106 |
* L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
|
107 |
* S -> side, when both rear and back are configured move one of them to the side channel
|
108 |
* OV -> center back
|
109 |
* All 2 channel configurations -> AV_CH_LAYOUT_STEREO
|
110 |
*/
|
111 |
|
112 |
static const int64_t dca_core_channel_layout[] = { |
113 |
AV_CH_FRONT_CENTER, ///< 1, A
|
114 |
AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
|
115 |
AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
|
116 |
AV_CH_LAYOUT_STEREO, ///< 2, (L+R) + (L-R) (sum-difference)
|
117 |
AV_CH_LAYOUT_STEREO, ///< 2, LT +RT (left and right total)
|
118 |
AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER, ///< 3, C+L+R
|
119 |
AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER, ///< 3, L+R+S
|
120 |
AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|AV_CH_BACK_CENTER, ///< 4, C + L + R+ S
|
121 |
AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, ///< 4, L + R +SL+ SR
|
122 |
AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, ///< 5, C + L + R+ SL+SR
|
123 |
AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
|
124 |
AV_CH_LAYOUT_STEREO|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT|AV_CH_FRONT_CENTER|AV_CH_BACK_CENTER, ///< 6, C + L + R+ LR + RR + OV
|
125 |
AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_BACK_CENTER|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT, ///< 6, CF+ CR+LF+ RF+LR + RR
|
126 |
AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
|
127 |
AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2+ SR1 + SR2
|
128 |
AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_BACK_CENTER|AV_CH_SIDE_RIGHT, ///< 8, CL + C+ CR + L + R + SL + S+ SR
|
129 |
}; |
130 |
|
131 |
static const int8_t dca_lfe_index[] = { |
132 |
1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3 |
133 |
}; |
134 |
|
135 |
static const int8_t dca_channel_reorder_lfe[][9] = { |
136 |
{ 0, -1, -1, -1, -1, -1, -1, -1, -1}, |
137 |
{ 0, 1, -1, -1, -1, -1, -1, -1, -1}, |
138 |
{ 0, 1, -1, -1, -1, -1, -1, -1, -1}, |
139 |
{ 0, 1, -1, -1, -1, -1, -1, -1, -1}, |
140 |
{ 0, 1, -1, -1, -1, -1, -1, -1, -1}, |
141 |
{ 2, 0, 1, -1, -1, -1, -1, -1, -1}, |
142 |
{ 0, 1, 3, -1, -1, -1, -1, -1, -1}, |
143 |
{ 2, 0, 1, 4, -1, -1, -1, -1, -1}, |
144 |
{ 0, 1, 3, 4, -1, -1, -1, -1, -1}, |
145 |
{ 2, 0, 1, 4, 5, -1, -1, -1, -1}, |
146 |
{ 3, 4, 0, 1, 5, 6, -1, -1, -1}, |
147 |
{ 2, 0, 1, 4, 5, 6, -1, -1, -1}, |
148 |
{ 0, 6, 4, 5, 2, 3, -1, -1, -1}, |
149 |
{ 4, 2, 5, 0, 1, 6, 7, -1, -1}, |
150 |
{ 5, 6, 0, 1, 7, 3, 8, 4, -1}, |
151 |
{ 4, 2, 5, 0, 1, 6, 8, 7, -1}, |
152 |
}; |
153 |
|
154 |
static const int8_t dca_channel_reorder_lfe_xch[][9] = { |
155 |
{ 0, 2, -1, -1, -1, -1, -1, -1, -1}, |
156 |
{ 0, 1, 3, -1, -1, -1, -1, -1, -1}, |
157 |
{ 0, 1, 3, -1, -1, -1, -1, -1, -1}, |
158 |
{ 0, 1, 3, -1, -1, -1, -1, -1, -1}, |
159 |
{ 0, 1, 3, -1, -1, -1, -1, -1, -1}, |
160 |
{ 2, 0, 1, 4, -1, -1, -1, -1, -1}, |
161 |
{ 0, 1, 3, 4, -1, -1, -1, -1, -1}, |
162 |
{ 2, 0, 1, 4, 5, -1, -1, -1, -1}, |
163 |
{ 0, 1, 4, 5, 3, -1, -1, -1, -1}, |
164 |
{ 2, 0, 1, 5, 6, 4, -1, -1, -1}, |
165 |
{ 3, 4, 0, 1, 6, 7, 5, -1, -1}, |
166 |
{ 2, 0, 1, 4, 5, 6, 7, -1, -1}, |
167 |
{ 0, 6, 4, 5, 2, 3, 7, -1, -1}, |
168 |
{ 4, 2, 5, 0, 1, 7, 8, 6, -1}, |
169 |
{ 5, 6, 0, 1, 8, 3, 9, 4, 7}, |
170 |
{ 4, 2, 5, 0, 1, 6, 9, 8, 7}, |
171 |
}; |
172 |
|
173 |
static const int8_t dca_channel_reorder_nolfe[][9] = { |
174 |
{ 0, -1, -1, -1, -1, -1, -1, -1, -1}, |
175 |
{ 0, 1, -1, -1, -1, -1, -1, -1, -1}, |
176 |
{ 0, 1, -1, -1, -1, -1, -1, -1, -1}, |
177 |
{ 0, 1, -1, -1, -1, -1, -1, -1, -1}, |
178 |
{ 0, 1, -1, -1, -1, -1, -1, -1, -1}, |
179 |
{ 2, 0, 1, -1, -1, -1, -1, -1, -1}, |
180 |
{ 0, 1, 2, -1, -1, -1, -1, -1, -1}, |
181 |
{ 2, 0, 1, 3, -1, -1, -1, -1, -1}, |
182 |
{ 0, 1, 2, 3, -1, -1, -1, -1, -1}, |
183 |
{ 2, 0, 1, 3, 4, -1, -1, -1, -1}, |
184 |
{ 2, 3, 0, 1, 4, 5, -1, -1, -1}, |
185 |
{ 2, 0, 1, 3, 4, 5, -1, -1, -1}, |
186 |
{ 0, 5, 3, 4, 1, 2, -1, -1, -1}, |
187 |
{ 3, 2, 4, 0, 1, 5, 6, -1, -1}, |
188 |
{ 4, 5, 0, 1, 6, 2, 7, 3, -1}, |
189 |
{ 3, 2, 4, 0, 1, 5, 7, 6, -1}, |
190 |
}; |
191 |
|
192 |
static const int8_t dca_channel_reorder_nolfe_xch[][9] = { |
193 |
{ 0, 1, -1, -1, -1, -1, -1, -1, -1}, |
194 |
{ 0, 1, 2, -1, -1, -1, -1, -1, -1}, |
195 |
{ 0, 1, 2, -1, -1, -1, -1, -1, -1}, |
196 |
{ 0, 1, 2, -1, -1, -1, -1, -1, -1}, |
197 |
{ 0, 1, 2, -1, -1, -1, -1, -1, -1}, |
198 |
{ 2, 0, 1, 3, -1, -1, -1, -1, -1}, |
199 |
{ 0, 1, 2, 3, -1, -1, -1, -1, -1}, |
200 |
{ 2, 0, 1, 3, 4, -1, -1, -1, -1}, |
201 |
{ 0, 1, 3, 4, 2, -1, -1, -1, -1}, |
202 |
{ 2, 0, 1, 4, 5, 3, -1, -1, -1}, |
203 |
{ 2, 3, 0, 1, 5, 6, 4, -1, -1}, |
204 |
{ 2, 0, 1, 3, 4, 5, 6, -1, -1}, |
205 |
{ 0, 5, 3, 4, 1, 2, 6, -1, -1}, |
206 |
{ 3, 2, 4, 0, 1, 6, 7, 5, -1}, |
207 |
{ 4, 5, 0, 1, 7, 2, 8, 3, 6}, |
208 |
{ 3, 2, 4, 0, 1, 5, 8, 7, 6}, |
209 |
}; |
210 |
|
211 |
#define DCA_DOLBY 101 /* FIXME */ |
212 |
|
213 |
#define DCA_CHANNEL_BITS 6 |
214 |
#define DCA_CHANNEL_MASK 0x3F |
215 |
|
216 |
#define DCA_LFE 0x80 |
217 |
|
218 |
#define HEADER_SIZE 14 |
219 |
|
220 |
#define DCA_MAX_FRAME_SIZE 16384 |
221 |
#define DCA_MAX_EXSS_HEADER_SIZE 4096 |
222 |
|
223 |
#define DCA_BUFFER_PADDING_SIZE 1024 |
224 |
|
225 |
/** Bit allocation */
|
226 |
typedef struct { |
227 |
int offset; ///< code values offset |
228 |
int maxbits[8]; ///< max bits in VLC |
229 |
int wrap; ///< wrap for get_vlc2() |
230 |
VLC vlc[8]; ///< actual codes |
231 |
} BitAlloc; |
232 |
|
233 |
static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select |
234 |
static BitAlloc dca_tmode; ///< transition mode VLCs |
235 |
static BitAlloc dca_scalefactor; ///< scalefactor VLCs |
236 |
static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs |
237 |
|
238 |
static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx) |
239 |
{ |
240 |
return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
|
241 |
} |
242 |
|
243 |
typedef struct { |
244 |
AVCodecContext *avctx; |
245 |
/* Frame header */
|
246 |
int frame_type; ///< type of the current frame |
247 |
int samples_deficit; ///< deficit sample count |
248 |
int crc_present; ///< crc is present in the bitstream |
249 |
int sample_blocks; ///< number of PCM sample blocks |
250 |
int frame_size; ///< primary frame byte size |
251 |
int amode; ///< audio channels arrangement |
252 |
int sample_rate; ///< audio sampling rate |
253 |
int bit_rate; ///< transmission bit rate |
254 |
int bit_rate_index; ///< transmission bit rate index |
255 |
|
256 |
int downmix; ///< embedded downmix enabled |
257 |
int dynrange; ///< embedded dynamic range flag |
258 |
int timestamp; ///< embedded time stamp flag |
259 |
int aux_data; ///< auxiliary data flag |
260 |
int hdcd; ///< source material is mastered in HDCD |
261 |
int ext_descr; ///< extension audio descriptor flag |
262 |
int ext_coding; ///< extended coding flag |
263 |
int aspf; ///< audio sync word insertion flag |
264 |
int lfe; ///< low frequency effects flag |
265 |
int predictor_history; ///< predictor history flag |
266 |
int header_crc; ///< header crc check bytes |
267 |
int multirate_inter; ///< multirate interpolator switch |
268 |
int version; ///< encoder software revision |
269 |
int copy_history; ///< copy history |
270 |
int source_pcm_res; ///< source pcm resolution |
271 |
int front_sum; ///< front sum/difference flag |
272 |
int surround_sum; ///< surround sum/difference flag |
273 |
int dialog_norm; ///< dialog normalisation parameter |
274 |
|
275 |
/* Primary audio coding header */
|
276 |
int subframes; ///< number of subframes |
277 |
int is_channels_set; ///< check for if the channel number is already set |
278 |
int total_channels; ///< number of channels including extensions |
279 |
int prim_channels; ///< number of primary audio channels |
280 |
int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count |
281 |
int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband |
282 |
int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index |
283 |
int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book |
284 |
int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book |
285 |
int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select |
286 |
int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select |
287 |
float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment |
288 |
|
289 |
/* Primary audio coding side information */
|
290 |
int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes |
291 |
int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count |
292 |
int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not) |
293 |
int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs |
294 |
int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index |
295 |
int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients) |
296 |
int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient) |
297 |
int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook |
298 |
int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors |
299 |
int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients |
300 |
int dynrange_coef; ///< dynamic range coefficient |
301 |
|
302 |
int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands |
303 |
|
304 |
float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data |
305 |
int lfe_scale_factor;
|
306 |
|
307 |
/* Subband samples history (for ADPCM) */
|
308 |
float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4]; |
309 |
DECLARE_ALIGNED(16, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512]; |
310 |
DECLARE_ALIGNED(16, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32]; |
311 |
int hist_index[DCA_PRIM_CHANNELS_MAX];
|
312 |
DECLARE_ALIGNED(16, float, raXin)[32]; |
313 |
|
314 |
int output; ///< type of output |
315 |
float scale_bias; ///< output scale |
316 |
|
317 |
DECLARE_ALIGNED(16, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8]; |
318 |
DECLARE_ALIGNED(16, float, samples)[(DCA_PRIM_CHANNELS_MAX+1)*256]; |
319 |
const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX+1]; |
320 |
|
321 |
uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE]; |
322 |
int dca_buffer_size; ///< how much data is in the dca_buffer |
323 |
|
324 |
const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe |
325 |
GetBitContext gb; |
326 |
/* Current position in DCA frame */
|
327 |
int current_subframe;
|
328 |
int current_subsubframe;
|
329 |
|
330 |
/* XCh extension information */
|
331 |
int xch_present;
|
332 |
int xch_base_channel; ///< index of first (only) channel containing XCH data |
333 |
|
334 |
/* Other detected extensions in the core substream */
|
335 |
int xxch_present;
|
336 |
int x96_present;
|
337 |
|
338 |
/* ExSS header parser */
|
339 |
int static_fields; ///< static fields present |
340 |
int mix_metadata; ///< mixing metadata present |
341 |
int num_mix_configs; ///< number of mix out configurations |
342 |
int mix_config_num_ch[4]; ///< number of channels in each mix out configuration |
343 |
|
344 |
int profile;
|
345 |
|
346 |
int debug_flag; ///< used for suppressing repeated error messages output |
347 |
DSPContext dsp; |
348 |
FFTContext imdct; |
349 |
SynthFilterContext synth; |
350 |
DCADSPContext dcadsp; |
351 |
FmtConvertContext fmt_conv; |
352 |
} DCAContext; |
353 |
|
354 |
static const uint16_t dca_vlc_offs[] = { |
355 |
0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364, |
356 |
5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508, |
357 |
5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564, |
358 |
7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240, |
359 |
12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264, |
360 |
18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622, |
361 |
}; |
362 |
|
363 |
static av_cold void dca_init_vlcs(void) |
364 |
{ |
365 |
static int vlcs_initialized = 0; |
366 |
int i, j, c = 14; |
367 |
static VLC_TYPE dca_table[23622][2]; |
368 |
|
369 |
if (vlcs_initialized)
|
370 |
return;
|
371 |
|
372 |
dca_bitalloc_index.offset = 1;
|
373 |
dca_bitalloc_index.wrap = 2;
|
374 |
for (i = 0; i < 5; i++) { |
375 |
dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]]; |
376 |
dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
|
377 |
init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
|
378 |
bitalloc_12_bits[i], 1, 1, |
379 |
bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); |
380 |
} |
381 |
dca_scalefactor.offset = -64;
|
382 |
dca_scalefactor.wrap = 2;
|
383 |
for (i = 0; i < 5; i++) { |
384 |
dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
|
385 |
dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5]; |
386 |
init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
|
387 |
scales_bits[i], 1, 1, |
388 |
scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); |
389 |
} |
390 |
dca_tmode.offset = 0;
|
391 |
dca_tmode.wrap = 1;
|
392 |
for (i = 0; i < 4; i++) { |
393 |
dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
|
394 |
dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10]; |
395 |
init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
|
396 |
tmode_bits[i], 1, 1, |
397 |
tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); |
398 |
} |
399 |
|
400 |
for (i = 0; i < 10; i++) |
401 |
for (j = 0; j < 7; j++){ |
402 |
if (!bitalloc_codes[i][j]) break; |
403 |
dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
|
404 |
dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4); |
405 |
dca_smpl_bitalloc[i+1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
|
406 |
dca_smpl_bitalloc[i+1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c]; |
407 |
init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
|
408 |
bitalloc_sizes[i], |
409 |
bitalloc_bits[i][j], 1, 1, |
410 |
bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC); |
411 |
c++; |
412 |
} |
413 |
vlcs_initialized = 1;
|
414 |
} |
415 |
|
416 |
static inline void get_array(GetBitContext *gb, int *dst, int len, int bits) |
417 |
{ |
418 |
while(len--)
|
419 |
*dst++ = get_bits(gb, bits); |
420 |
} |
421 |
|
422 |
static int dca_parse_audio_coding_header(DCAContext * s, int base_channel) |
423 |
{ |
424 |
int i, j;
|
425 |
static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 }; |
426 |
static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 }; |
427 |
static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 }; |
428 |
|
429 |
s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel; |
430 |
s->prim_channels = s->total_channels; |
431 |
|
432 |
if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
|
433 |
s->prim_channels = DCA_PRIM_CHANNELS_MAX; |
434 |
|
435 |
|
436 |
for (i = base_channel; i < s->prim_channels; i++) {
|
437 |
s->subband_activity[i] = get_bits(&s->gb, 5) + 2; |
438 |
if (s->subband_activity[i] > DCA_SUBBANDS)
|
439 |
s->subband_activity[i] = DCA_SUBBANDS; |
440 |
} |
441 |
for (i = base_channel; i < s->prim_channels; i++) {
|
442 |
s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1; |
443 |
if (s->vq_start_subband[i] > DCA_SUBBANDS)
|
444 |
s->vq_start_subband[i] = DCA_SUBBANDS; |
445 |
} |
446 |
get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
|
447 |
get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
|
448 |
get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
|
449 |
get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
|
450 |
|
451 |
/* Get codebooks quantization indexes */
|
452 |
if (!base_channel)
|
453 |
memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman)); |
454 |
for (j = 1; j < 11; j++) |
455 |
for (i = base_channel; i < s->prim_channels; i++)
|
456 |
s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]); |
457 |
|
458 |
/* Get scale factor adjustment */
|
459 |
for (j = 0; j < 11; j++) |
460 |
for (i = base_channel; i < s->prim_channels; i++)
|
461 |
s->scalefactor_adj[i][j] = 1;
|
462 |
|
463 |
for (j = 1; j < 11; j++) |
464 |
for (i = base_channel; i < s->prim_channels; i++)
|
465 |
if (s->quant_index_huffman[i][j] < thr[j])
|
466 |
s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
|
467 |
|
468 |
if (s->crc_present) {
|
469 |
/* Audio header CRC check */
|
470 |
get_bits(&s->gb, 16);
|
471 |
} |
472 |
|
473 |
s->current_subframe = 0;
|
474 |
s->current_subsubframe = 0;
|
475 |
|
476 |
#ifdef TRACE
|
477 |
av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
|
478 |
av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
|
479 |
for (i = base_channel; i < s->prim_channels; i++){
|
480 |
av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
|
481 |
av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
|
482 |
av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
|
483 |
av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
|
484 |
av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
|
485 |
av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
|
486 |
av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
|
487 |
for (j = 0; j < 11; j++) |
488 |
av_log(s->avctx, AV_LOG_DEBUG, " %i",
|
489 |
s->quant_index_huffman[i][j]); |
490 |
av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
491 |
av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
|
492 |
for (j = 0; j < 11; j++) |
493 |
av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
|
494 |
av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
495 |
} |
496 |
#endif
|
497 |
|
498 |
return 0; |
499 |
} |
500 |
|
501 |
static int dca_parse_frame_header(DCAContext * s) |
502 |
{ |
503 |
init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
|
504 |
|
505 |
/* Sync code */
|
506 |
get_bits(&s->gb, 32);
|
507 |
|
508 |
/* Frame header */
|
509 |
s->frame_type = get_bits(&s->gb, 1);
|
510 |
s->samples_deficit = get_bits(&s->gb, 5) + 1; |
511 |
s->crc_present = get_bits(&s->gb, 1);
|
512 |
s->sample_blocks = get_bits(&s->gb, 7) + 1; |
513 |
s->frame_size = get_bits(&s->gb, 14) + 1; |
514 |
if (s->frame_size < 95) |
515 |
return -1; |
516 |
s->amode = get_bits(&s->gb, 6);
|
517 |
s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
|
518 |
if (!s->sample_rate)
|
519 |
return -1; |
520 |
s->bit_rate_index = get_bits(&s->gb, 5);
|
521 |
s->bit_rate = dca_bit_rates[s->bit_rate_index]; |
522 |
if (!s->bit_rate)
|
523 |
return -1; |
524 |
|
525 |
s->downmix = get_bits(&s->gb, 1);
|
526 |
s->dynrange = get_bits(&s->gb, 1);
|
527 |
s->timestamp = get_bits(&s->gb, 1);
|
528 |
s->aux_data = get_bits(&s->gb, 1);
|
529 |
s->hdcd = get_bits(&s->gb, 1);
|
530 |
s->ext_descr = get_bits(&s->gb, 3);
|
531 |
s->ext_coding = get_bits(&s->gb, 1);
|
532 |
s->aspf = get_bits(&s->gb, 1);
|
533 |
s->lfe = get_bits(&s->gb, 2);
|
534 |
s->predictor_history = get_bits(&s->gb, 1);
|
535 |
|
536 |
/* TODO: check CRC */
|
537 |
if (s->crc_present)
|
538 |
s->header_crc = get_bits(&s->gb, 16);
|
539 |
|
540 |
s->multirate_inter = get_bits(&s->gb, 1);
|
541 |
s->version = get_bits(&s->gb, 4);
|
542 |
s->copy_history = get_bits(&s->gb, 2);
|
543 |
s->source_pcm_res = get_bits(&s->gb, 3);
|
544 |
s->front_sum = get_bits(&s->gb, 1);
|
545 |
s->surround_sum = get_bits(&s->gb, 1);
|
546 |
s->dialog_norm = get_bits(&s->gb, 4);
|
547 |
|
548 |
/* FIXME: channels mixing levels */
|
549 |
s->output = s->amode; |
550 |
if (s->lfe) s->output |= DCA_LFE;
|
551 |
|
552 |
#ifdef TRACE
|
553 |
av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
|
554 |
av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
|
555 |
av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
|
556 |
av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
|
557 |
s->sample_blocks, s->sample_blocks * 32);
|
558 |
av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
|
559 |
av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
|
560 |
s->amode, dca_channels[s->amode]); |
561 |
av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
|
562 |
s->sample_rate); |
563 |
av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
|
564 |
s->bit_rate); |
565 |
av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
|
566 |
av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
|
567 |
av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
|
568 |
av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
|
569 |
av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
|
570 |
av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
|
571 |
av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
|
572 |
av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
|
573 |
av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
|
574 |
av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
|
575 |
s->predictor_history); |
576 |
av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
|
577 |
av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
|
578 |
s->multirate_inter); |
579 |
av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
|
580 |
av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
|
581 |
av_log(s->avctx, AV_LOG_DEBUG, |
582 |
"source pcm resolution: %i (%i bits/sample)\n",
|
583 |
s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]); |
584 |
av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
|
585 |
av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
|
586 |
av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
|
587 |
av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
588 |
#endif
|
589 |
|
590 |
/* Primary audio coding header */
|
591 |
s->subframes = get_bits(&s->gb, 4) + 1; |
592 |
|
593 |
return dca_parse_audio_coding_header(s, 0); |
594 |
} |
595 |
|
596 |
|
597 |
static inline int get_scale(GetBitContext *gb, int level, int value) |
598 |
{ |
599 |
if (level < 5) { |
600 |
/* huffman encoded */
|
601 |
value += get_bitalloc(gb, &dca_scalefactor, level); |
602 |
} else if (level < 8) |
603 |
value = get_bits(gb, level + 1);
|
604 |
return value;
|
605 |
} |
606 |
|
607 |
static int dca_subframe_header(DCAContext * s, int base_channel, int block_index) |
608 |
{ |
609 |
/* Primary audio coding side information */
|
610 |
int j, k;
|
611 |
|
612 |
if (get_bits_left(&s->gb) < 0) |
613 |
return -1; |
614 |
|
615 |
if (!base_channel) {
|
616 |
s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1; |
617 |
s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
|
618 |
} |
619 |
|
620 |
for (j = base_channel; j < s->prim_channels; j++) {
|
621 |
for (k = 0; k < s->subband_activity[j]; k++) |
622 |
s->prediction_mode[j][k] = get_bits(&s->gb, 1);
|
623 |
} |
624 |
|
625 |
/* Get prediction codebook */
|
626 |
for (j = base_channel; j < s->prim_channels; j++) {
|
627 |
for (k = 0; k < s->subband_activity[j]; k++) { |
628 |
if (s->prediction_mode[j][k] > 0) { |
629 |
/* (Prediction coefficient VQ address) */
|
630 |
s->prediction_vq[j][k] = get_bits(&s->gb, 12);
|
631 |
} |
632 |
} |
633 |
} |
634 |
|
635 |
/* Bit allocation index */
|
636 |
for (j = base_channel; j < s->prim_channels; j++) {
|
637 |
for (k = 0; k < s->vq_start_subband[j]; k++) { |
638 |
if (s->bitalloc_huffman[j] == 6) |
639 |
s->bitalloc[j][k] = get_bits(&s->gb, 5);
|
640 |
else if (s->bitalloc_huffman[j] == 5) |
641 |
s->bitalloc[j][k] = get_bits(&s->gb, 4);
|
642 |
else if (s->bitalloc_huffman[j] == 7) { |
643 |
av_log(s->avctx, AV_LOG_ERROR, |
644 |
"Invalid bit allocation index\n");
|
645 |
return -1; |
646 |
} else {
|
647 |
s->bitalloc[j][k] = |
648 |
get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]); |
649 |
} |
650 |
|
651 |
if (s->bitalloc[j][k] > 26) { |
652 |
// av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
|
653 |
// j, k, s->bitalloc[j][k]);
|
654 |
return -1; |
655 |
} |
656 |
} |
657 |
} |
658 |
|
659 |
/* Transition mode */
|
660 |
for (j = base_channel; j < s->prim_channels; j++) {
|
661 |
for (k = 0; k < s->subband_activity[j]; k++) { |
662 |
s->transition_mode[j][k] = 0;
|
663 |
if (s->subsubframes[s->current_subframe] > 1 && |
664 |
k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
|
665 |
s->transition_mode[j][k] = |
666 |
get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]); |
667 |
} |
668 |
} |
669 |
} |
670 |
|
671 |
if (get_bits_left(&s->gb) < 0) |
672 |
return -1; |
673 |
|
674 |
for (j = base_channel; j < s->prim_channels; j++) {
|
675 |
const uint32_t *scale_table;
|
676 |
int scale_sum;
|
677 |
|
678 |
memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2); |
679 |
|
680 |
if (s->scalefactor_huffman[j] == 6) |
681 |
scale_table = scale_factor_quant7; |
682 |
else
|
683 |
scale_table = scale_factor_quant6; |
684 |
|
685 |
/* When huffman coded, only the difference is encoded */
|
686 |
scale_sum = 0;
|
687 |
|
688 |
for (k = 0; k < s->subband_activity[j]; k++) { |
689 |
if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) { |
690 |
scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum); |
691 |
s->scale_factor[j][k][0] = scale_table[scale_sum];
|
692 |
} |
693 |
|
694 |
if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
|
695 |
/* Get second scale factor */
|
696 |
scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum); |
697 |
s->scale_factor[j][k][1] = scale_table[scale_sum];
|
698 |
} |
699 |
} |
700 |
} |
701 |
|
702 |
/* Joint subband scale factor codebook select */
|
703 |
for (j = base_channel; j < s->prim_channels; j++) {
|
704 |
/* Transmitted only if joint subband coding enabled */
|
705 |
if (s->joint_intensity[j] > 0) |
706 |
s->joint_huff[j] = get_bits(&s->gb, 3);
|
707 |
} |
708 |
|
709 |
if (get_bits_left(&s->gb) < 0) |
710 |
return -1; |
711 |
|
712 |
/* Scale factors for joint subband coding */
|
713 |
for (j = base_channel; j < s->prim_channels; j++) {
|
714 |
int source_channel;
|
715 |
|
716 |
/* Transmitted only if joint subband coding enabled */
|
717 |
if (s->joint_intensity[j] > 0) { |
718 |
int scale = 0; |
719 |
source_channel = s->joint_intensity[j] - 1;
|
720 |
|
721 |
/* When huffman coded, only the difference is encoded
|
722 |
* (is this valid as well for joint scales ???) */
|
723 |
|
724 |
for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
|
725 |
scale = get_scale(&s->gb, s->joint_huff[j], 0);
|
726 |
scale += 64; /* bias */ |
727 |
s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
|
728 |
} |
729 |
|
730 |
if (!(s->debug_flag & 0x02)) { |
731 |
av_log(s->avctx, AV_LOG_DEBUG, |
732 |
"Joint stereo coding not supported\n");
|
733 |
s->debug_flag |= 0x02;
|
734 |
} |
735 |
} |
736 |
} |
737 |
|
738 |
/* Stereo downmix coefficients */
|
739 |
if (!base_channel && s->prim_channels > 2) { |
740 |
if (s->downmix) {
|
741 |
for (j = base_channel; j < s->prim_channels; j++) {
|
742 |
s->downmix_coef[j][0] = get_bits(&s->gb, 7); |
743 |
s->downmix_coef[j][1] = get_bits(&s->gb, 7); |
744 |
} |
745 |
} else {
|
746 |
int am = s->amode & DCA_CHANNEL_MASK;
|
747 |
for (j = base_channel; j < s->prim_channels; j++) {
|
748 |
s->downmix_coef[j][0] = dca_default_coeffs[am][j][0]; |
749 |
s->downmix_coef[j][1] = dca_default_coeffs[am][j][1]; |
750 |
} |
751 |
} |
752 |
} |
753 |
|
754 |
/* Dynamic range coefficient */
|
755 |
if (!base_channel && s->dynrange)
|
756 |
s->dynrange_coef = get_bits(&s->gb, 8);
|
757 |
|
758 |
/* Side information CRC check word */
|
759 |
if (s->crc_present) {
|
760 |
get_bits(&s->gb, 16);
|
761 |
} |
762 |
|
763 |
/*
|
764 |
* Primary audio data arrays
|
765 |
*/
|
766 |
|
767 |
/* VQ encoded high frequency subbands */
|
768 |
for (j = base_channel; j < s->prim_channels; j++)
|
769 |
for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
|
770 |
/* 1 vector -> 32 samples */
|
771 |
s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
|
772 |
|
773 |
/* Low frequency effect data */
|
774 |
if (!base_channel && s->lfe) {
|
775 |
/* LFE samples */
|
776 |
int lfe_samples = 2 * s->lfe * (4 + block_index); |
777 |
int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]); |
778 |
float lfe_scale;
|
779 |
|
780 |
for (j = lfe_samples; j < lfe_end_sample; j++) {
|
781 |
/* Signed 8 bits int */
|
782 |
s->lfe_data[j] = get_sbits(&s->gb, 8);
|
783 |
} |
784 |
|
785 |
/* Scale factor index */
|
786 |
s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
|
787 |
|
788 |
/* Quantization step size * scale factor */
|
789 |
lfe_scale = 0.035 * s->lfe_scale_factor; |
790 |
|
791 |
for (j = lfe_samples; j < lfe_end_sample; j++)
|
792 |
s->lfe_data[j] *= lfe_scale; |
793 |
} |
794 |
|
795 |
#ifdef TRACE
|
796 |
av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes[s->current_subframe]);
|
797 |
av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
|
798 |
s->partial_samples[s->current_subframe]); |
799 |
for (j = base_channel; j < s->prim_channels; j++) {
|
800 |
av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
|
801 |
for (k = 0; k < s->subband_activity[j]; k++) |
802 |
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
|
803 |
av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
804 |
} |
805 |
for (j = base_channel; j < s->prim_channels; j++) {
|
806 |
for (k = 0; k < s->subband_activity[j]; k++) |
807 |
av_log(s->avctx, AV_LOG_DEBUG, |
808 |
"prediction coefs: %f, %f, %f, %f\n",
|
809 |
(float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192, |
810 |
(float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192, |
811 |
(float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192, |
812 |
(float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192); |
813 |
} |
814 |
for (j = base_channel; j < s->prim_channels; j++) {
|
815 |
av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
|
816 |
for (k = 0; k < s->vq_start_subband[j]; k++) |
817 |
av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
|
818 |
av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
819 |
} |
820 |
for (j = base_channel; j < s->prim_channels; j++) {
|
821 |
av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
|
822 |
for (k = 0; k < s->subband_activity[j]; k++) |
823 |
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
|
824 |
av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
825 |
} |
826 |
for (j = base_channel; j < s->prim_channels; j++) {
|
827 |
av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
|
828 |
for (k = 0; k < s->subband_activity[j]; k++) { |
829 |
if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) |
830 |
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]); |
831 |
if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
|
832 |
av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]); |
833 |
} |
834 |
av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
835 |
} |
836 |
for (j = base_channel; j < s->prim_channels; j++) {
|
837 |
if (s->joint_intensity[j] > 0) { |
838 |
int source_channel = s->joint_intensity[j] - 1; |
839 |
av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
|
840 |
for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
|
841 |
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
|
842 |
av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
843 |
} |
844 |
} |
845 |
if (!base_channel && s->prim_channels > 2 && s->downmix) { |
846 |
av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
|
847 |
for (j = 0; j < s->prim_channels; j++) { |
848 |
av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]); |
849 |
av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]); |
850 |
} |
851 |
av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
852 |
} |
853 |
for (j = base_channel; j < s->prim_channels; j++)
|
854 |
for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
|
855 |
av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
|
856 |
if (!base_channel && s->lfe) {
|
857 |
int lfe_samples = 2 * s->lfe * (4 + block_index); |
858 |
int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]); |
859 |
|
860 |
av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
|
861 |
for (j = lfe_samples; j < lfe_end_sample; j++)
|
862 |
av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
|
863 |
av_log(s->avctx, AV_LOG_DEBUG, "\n");
|
864 |
} |
865 |
#endif
|
866 |
|
867 |
return 0; |
868 |
} |
869 |
|
870 |
static void qmf_32_subbands(DCAContext * s, int chans, |
871 |
float samples_in[32][8], float *samples_out, |
872 |
float scale)
|
873 |
{ |
874 |
const float *prCoeff; |
875 |
int i;
|
876 |
|
877 |
int sb_act = s->subband_activity[chans];
|
878 |
int subindex;
|
879 |
|
880 |
scale *= sqrt(1/8.0); |
881 |
|
882 |
/* Select filter */
|
883 |
if (!s->multirate_inter) /* Non-perfect reconstruction */ |
884 |
prCoeff = fir_32bands_nonperfect; |
885 |
else /* Perfect reconstruction */ |
886 |
prCoeff = fir_32bands_perfect; |
887 |
|
888 |
/* Reconstructed channel sample index */
|
889 |
for (subindex = 0; subindex < 8; subindex++) { |
890 |
/* Load in one sample from each subband and clear inactive subbands */
|
891 |
for (i = 0; i < sb_act; i++){ |
892 |
uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ ((i-1)&2)<<30; |
893 |
AV_WN32A(&s->raXin[i], v); |
894 |
} |
895 |
for (; i < 32; i++) |
896 |
s->raXin[i] = 0.0; |
897 |
|
898 |
s->synth.synth_filter_float(&s->imdct, |
899 |
s->subband_fir_hist[chans], &s->hist_index[chans], |
900 |
s->subband_fir_noidea[chans], prCoeff, |
901 |
samples_out, s->raXin, scale); |
902 |
samples_out+= 32;
|
903 |
|
904 |
} |
905 |
} |
906 |
|
907 |
static void lfe_interpolation_fir(DCAContext *s, int decimation_select, |
908 |
int num_deci_sample, float *samples_in, |
909 |
float *samples_out, float scale) |
910 |
{ |
911 |
/* samples_in: An array holding decimated samples.
|
912 |
* Samples in current subframe starts from samples_in[0],
|
913 |
* while samples_in[-1], samples_in[-2], ..., stores samples
|
914 |
* from last subframe as history.
|
915 |
*
|
916 |
* samples_out: An array holding interpolated samples
|
917 |
*/
|
918 |
|
919 |
int decifactor;
|
920 |
const float *prCoeff; |
921 |
int deciindex;
|
922 |
|
923 |
/* Select decimation filter */
|
924 |
if (decimation_select == 1) { |
925 |
decifactor = 64;
|
926 |
prCoeff = lfe_fir_128; |
927 |
} else {
|
928 |
decifactor = 32;
|
929 |
prCoeff = lfe_fir_64; |
930 |
} |
931 |
/* Interpolation */
|
932 |
for (deciindex = 0; deciindex < num_deci_sample; deciindex++) { |
933 |
s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, |
934 |
scale); |
935 |
samples_in++; |
936 |
samples_out += 2 * decifactor;
|
937 |
} |
938 |
} |
939 |
|
940 |
/* downmixing routines */
|
941 |
#define MIX_REAR1(samples, si1, rs, coef) \
|
942 |
samples[i] += samples[si1] * coef[rs][0]; \
|
943 |
samples[i+256] += samples[si1] * coef[rs][1]; |
944 |
|
945 |
#define MIX_REAR2(samples, si1, si2, rs, coef) \
|
946 |
samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \ |
947 |
samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1]; |
948 |
|
949 |
#define MIX_FRONT3(samples, coef) \
|
950 |
t = samples[i+c]; \ |
951 |
u = samples[i+l]; \ |
952 |
v = samples[i+r]; \ |
953 |
samples[i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \ |
954 |
samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1]; |
955 |
|
956 |
#define DOWNMIX_TO_STEREO(op1, op2) \
|
957 |
for (i = 0; i < 256; i++){ \ |
958 |
op1 \ |
959 |
op2 \ |
960 |
} |
961 |
|
962 |
static void dca_downmix(float *samples, int srcfmt, |
963 |
int downmix_coef[DCA_PRIM_CHANNELS_MAX][2], |
964 |
const int8_t *channel_mapping)
|
965 |
{ |
966 |
int c,l,r,sl,sr,s;
|
967 |
int i;
|
968 |
float t, u, v;
|
969 |
float coef[DCA_PRIM_CHANNELS_MAX][2]; |
970 |
|
971 |
for (i=0; i<DCA_PRIM_CHANNELS_MAX; i++) { |
972 |
coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]]; |
973 |
coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]]; |
974 |
} |
975 |
|
976 |
switch (srcfmt) {
|
977 |
case DCA_MONO:
|
978 |
case DCA_CHANNEL:
|
979 |
case DCA_STEREO_TOTAL:
|
980 |
case DCA_STEREO_SUMDIFF:
|
981 |
case DCA_4F2R:
|
982 |
av_log(NULL, 0, "Not implemented!\n"); |
983 |
break;
|
984 |
case DCA_STEREO:
|
985 |
break;
|
986 |
case DCA_3F:
|
987 |
c = channel_mapping[0] * 256; |
988 |
l = channel_mapping[1] * 256; |
989 |
r = channel_mapping[2] * 256; |
990 |
DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),); |
991 |
break;
|
992 |
case DCA_2F1R:
|
993 |
s = channel_mapping[2] * 256; |
994 |
DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef),);
|
995 |
break;
|
996 |
case DCA_3F1R:
|
997 |
c = channel_mapping[0] * 256; |
998 |
l = channel_mapping[1] * 256; |
999 |
r = channel_mapping[2] * 256; |
1000 |
s = channel_mapping[3] * 256; |
1001 |
DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), |
1002 |
MIX_REAR1(samples, i + s, 3, coef));
|
1003 |
break;
|
1004 |
case DCA_2F2R:
|
1005 |
sl = channel_mapping[2] * 256; |
1006 |
sr = channel_mapping[3] * 256; |
1007 |
DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef),);
|
1008 |
break;
|
1009 |
case DCA_3F2R:
|
1010 |
c = channel_mapping[0] * 256; |
1011 |
l = channel_mapping[1] * 256; |
1012 |
r = channel_mapping[2] * 256; |
1013 |
sl = channel_mapping[3] * 256; |
1014 |
sr = channel_mapping[4] * 256; |
1015 |
DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), |
1016 |
MIX_REAR2(samples, i + sl, i + sr, 3, coef));
|
1017 |
break;
|
1018 |
} |
1019 |
} |
1020 |
|
1021 |
|
1022 |
/* Very compact version of the block code decoder that does not use table
|
1023 |
* look-up but is slightly slower */
|
1024 |
static int decode_blockcode(int code, int levels, int *values) |
1025 |
{ |
1026 |
int i;
|
1027 |
int offset = (levels - 1) >> 1; |
1028 |
|
1029 |
for (i = 0; i < 4; i++) { |
1030 |
int div = FASTDIV(code, levels);
|
1031 |
values[i] = code - offset - div*levels; |
1032 |
code = div; |
1033 |
} |
1034 |
|
1035 |
if (code == 0) |
1036 |
return 0; |
1037 |
else {
|
1038 |
av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n"); |
1039 |
return -1; |
1040 |
} |
1041 |
} |
1042 |
|
1043 |
static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 }; |
1044 |
static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 }; |
1045 |
|
1046 |
static int dca_subsubframe(DCAContext * s, int base_channel, int block_index) |
1047 |
{ |
1048 |
int k, l;
|
1049 |
int subsubframe = s->current_subsubframe;
|
1050 |
|
1051 |
const float *quant_step_table; |
1052 |
|
1053 |
/* FIXME */
|
1054 |
float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index]; |
1055 |
LOCAL_ALIGNED_16(int, block, [8]); |
1056 |
|
1057 |
/*
|
1058 |
* Audio data
|
1059 |
*/
|
1060 |
|
1061 |
/* Select quantization step size table */
|
1062 |
if (s->bit_rate_index == 0x1f) |
1063 |
quant_step_table = lossless_quant_d; |
1064 |
else
|
1065 |
quant_step_table = lossy_quant_d; |
1066 |
|
1067 |
for (k = base_channel; k < s->prim_channels; k++) {
|
1068 |
if (get_bits_left(&s->gb) < 0) |
1069 |
return -1; |
1070 |
|
1071 |
for (l = 0; l < s->vq_start_subband[k]; l++) { |
1072 |
int m;
|
1073 |
|
1074 |
/* Select the mid-tread linear quantizer */
|
1075 |
int abits = s->bitalloc[k][l];
|
1076 |
|
1077 |
float quant_step_size = quant_step_table[abits];
|
1078 |
|
1079 |
/*
|
1080 |
* Determine quantization index code book and its type
|
1081 |
*/
|
1082 |
|
1083 |
/* Select quantization index code book */
|
1084 |
int sel = s->quant_index_huffman[k][abits];
|
1085 |
|
1086 |
/*
|
1087 |
* Extract bits from the bit stream
|
1088 |
*/
|
1089 |
if (!abits){
|
1090 |
memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0])); |
1091 |
} else {
|
1092 |
/* Deal with transients */
|
1093 |
int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
|
1094 |
float rscale = quant_step_size * s->scale_factor[k][l][sfi] * s->scalefactor_adj[k][sel];
|
1095 |
|
1096 |
if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){ |
1097 |
if (abits <= 7){ |
1098 |
/* Block code */
|
1099 |
int block_code1, block_code2, size, levels;
|
1100 |
|
1101 |
size = abits_sizes[abits-1];
|
1102 |
levels = abits_levels[abits-1];
|
1103 |
|
1104 |
block_code1 = get_bits(&s->gb, size); |
1105 |
/* FIXME Should test return value */
|
1106 |
decode_blockcode(block_code1, levels, block); |
1107 |
block_code2 = get_bits(&s->gb, size); |
1108 |
decode_blockcode(block_code2, levels, &block[4]);
|
1109 |
}else{
|
1110 |
/* no coding */
|
1111 |
for (m = 0; m < 8; m++) |
1112 |
block[m] = get_sbits(&s->gb, abits - 3);
|
1113 |
} |
1114 |
}else{
|
1115 |
/* Huffman coded */
|
1116 |
for (m = 0; m < 8; m++) |
1117 |
block[m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel); |
1118 |
} |
1119 |
|
1120 |
s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l], |
1121 |
block, rscale, 8);
|
1122 |
} |
1123 |
|
1124 |
/*
|
1125 |
* Inverse ADPCM if in prediction mode
|
1126 |
*/
|
1127 |
if (s->prediction_mode[k][l]) {
|
1128 |
int n;
|
1129 |
for (m = 0; m < 8; m++) { |
1130 |
for (n = 1; n <= 4; n++) |
1131 |
if (m >= n)
|
1132 |
subband_samples[k][l][m] += |
1133 |
(adpcm_vb[s->prediction_vq[k][l]][n - 1] *
|
1134 |
subband_samples[k][l][m - n] / 8192);
|
1135 |
else if (s->predictor_history) |
1136 |
subband_samples[k][l][m] += |
1137 |
(adpcm_vb[s->prediction_vq[k][l]][n - 1] *
|
1138 |
s->subband_samples_hist[k][l][m - n + |
1139 |
4] / 8192); |
1140 |
} |
1141 |
} |
1142 |
} |
1143 |
|
1144 |
/*
|
1145 |
* Decode VQ encoded high frequencies
|
1146 |
*/
|
1147 |
for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
|
1148 |
/* 1 vector -> 32 samples but we only need the 8 samples
|
1149 |
* for this subsubframe. */
|
1150 |
int m;
|
1151 |
|
1152 |
if (!s->debug_flag & 0x01) { |
1153 |
av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
|
1154 |
s->debug_flag |= 0x01;
|
1155 |
} |
1156 |
|
1157 |
for (m = 0; m < 8; m++) { |
1158 |
subband_samples[k][l][m] = |
1159 |
high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
|
1160 |
m] |
1161 |
* (float) s->scale_factor[k][l][0] / 16.0; |
1162 |
} |
1163 |
} |
1164 |
} |
1165 |
|
1166 |
/* Check for DSYNC after subsubframe */
|
1167 |
if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) { |
1168 |
if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */ |
1169 |
#ifdef TRACE
|
1170 |
av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
|
1171 |
#endif
|
1172 |
} else {
|
1173 |
av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
|
1174 |
} |
1175 |
} |
1176 |
|
1177 |
/* Backup predictor history for adpcm */
|
1178 |
for (k = base_channel; k < s->prim_channels; k++)
|
1179 |
for (l = 0; l < s->vq_start_subband[k]; l++) |
1180 |
memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
|
1181 |
4 * sizeof(subband_samples[0][0][0])); |
1182 |
|
1183 |
return 0; |
1184 |
} |
1185 |
|
1186 |
static int dca_filter_channels(DCAContext * s, int block_index) |
1187 |
{ |
1188 |
float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index]; |
1189 |
int k;
|
1190 |
|
1191 |
/* 32 subbands QMF */
|
1192 |
for (k = 0; k < s->prim_channels; k++) { |
1193 |
/* static float pcm_to_double[8] =
|
1194 |
{32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
|
1195 |
qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
|
1196 |
M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ );
|
1197 |
} |
1198 |
|
1199 |
/* Down mixing */
|
1200 |
if (s->avctx->request_channels == 2 && s->prim_channels > 2) { |
1201 |
dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab); |
1202 |
} |
1203 |
|
1204 |
/* Generate LFE samples for this subsubframe FIXME!!! */
|
1205 |
if (s->output & DCA_LFE) {
|
1206 |
lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
|
1207 |
s->lfe_data + 2 * s->lfe * (block_index + 4), |
1208 |
&s->samples[256 * dca_lfe_index[s->amode]],
|
1209 |
(1.0/256.0)*s->scale_bias); |
1210 |
/* Outputs 20bits pcm samples */
|
1211 |
} |
1212 |
|
1213 |
return 0; |
1214 |
} |
1215 |
|
1216 |
|
1217 |
static int dca_subframe_footer(DCAContext * s, int base_channel) |
1218 |
{ |
1219 |
int aux_data_count = 0, i; |
1220 |
|
1221 |
/*
|
1222 |
* Unpack optional information
|
1223 |
*/
|
1224 |
|
1225 |
/* presumably optional information only appears in the core? */
|
1226 |
if (!base_channel) {
|
1227 |
if (s->timestamp)
|
1228 |
get_bits(&s->gb, 32);
|
1229 |
|
1230 |
if (s->aux_data)
|
1231 |
aux_data_count = get_bits(&s->gb, 6);
|
1232 |
|
1233 |
for (i = 0; i < aux_data_count; i++) |
1234 |
get_bits(&s->gb, 8);
|
1235 |
|
1236 |
if (s->crc_present && (s->downmix || s->dynrange))
|
1237 |
get_bits(&s->gb, 16);
|
1238 |
} |
1239 |
|
1240 |
return 0; |
1241 |
} |
1242 |
|
1243 |
/**
|
1244 |
* Decode a dca frame block
|
1245 |
*
|
1246 |
* @param s pointer to the DCAContext
|
1247 |
*/
|
1248 |
|
1249 |
static int dca_decode_block(DCAContext * s, int base_channel, int block_index) |
1250 |
{ |
1251 |
|
1252 |
/* Sanity check */
|
1253 |
if (s->current_subframe >= s->subframes) {
|
1254 |
av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
|
1255 |
s->current_subframe, s->subframes); |
1256 |
return -1; |
1257 |
} |
1258 |
|
1259 |
if (!s->current_subsubframe) {
|
1260 |
#ifdef TRACE
|
1261 |
av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
|
1262 |
#endif
|
1263 |
/* Read subframe header */
|
1264 |
if (dca_subframe_header(s, base_channel, block_index))
|
1265 |
return -1; |
1266 |
} |
1267 |
|
1268 |
/* Read subsubframe */
|
1269 |
#ifdef TRACE
|
1270 |
av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
|
1271 |
#endif
|
1272 |
if (dca_subsubframe(s, base_channel, block_index))
|
1273 |
return -1; |
1274 |
|
1275 |
/* Update state */
|
1276 |
s->current_subsubframe++; |
1277 |
if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
|
1278 |
s->current_subsubframe = 0;
|
1279 |
s->current_subframe++; |
1280 |
} |
1281 |
if (s->current_subframe >= s->subframes) {
|
1282 |
#ifdef TRACE
|
1283 |
av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
|
1284 |
#endif
|
1285 |
/* Read subframe footer */
|
1286 |
if (dca_subframe_footer(s, base_channel))
|
1287 |
return -1; |
1288 |
} |
1289 |
|
1290 |
return 0; |
1291 |
} |
1292 |
|
1293 |
/**
|
1294 |
* Convert bitstream to one representation based on sync marker
|
1295 |
*/
|
1296 |
static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst, |
1297 |
int max_size)
|
1298 |
{ |
1299 |
uint32_t mrk; |
1300 |
int i, tmp;
|
1301 |
const uint16_t *ssrc = (const uint16_t *) src; |
1302 |
uint16_t *sdst = (uint16_t *) dst; |
1303 |
PutBitContext pb; |
1304 |
|
1305 |
if ((unsigned)src_size > (unsigned)max_size) { |
1306 |
// av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
|
1307 |
// return -1;
|
1308 |
src_size = max_size; |
1309 |
} |
1310 |
|
1311 |
mrk = AV_RB32(src); |
1312 |
switch (mrk) {
|
1313 |
case DCA_MARKER_RAW_BE:
|
1314 |
memcpy(dst, src, src_size); |
1315 |
return src_size;
|
1316 |
case DCA_MARKER_RAW_LE:
|
1317 |
for (i = 0; i < (src_size + 1) >> 1; i++) |
1318 |
*sdst++ = av_bswap16(*ssrc++); |
1319 |
return src_size;
|
1320 |
case DCA_MARKER_14B_BE:
|
1321 |
case DCA_MARKER_14B_LE:
|
1322 |
init_put_bits(&pb, dst, max_size); |
1323 |
for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) { |
1324 |
tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
|
1325 |
put_bits(&pb, 14, tmp);
|
1326 |
} |
1327 |
flush_put_bits(&pb); |
1328 |
return (put_bits_count(&pb) + 7) >> 3; |
1329 |
default:
|
1330 |
return -1; |
1331 |
} |
1332 |
} |
1333 |
|
1334 |
/**
|
1335 |
* Return the number of channels in an ExSS speaker mask (HD)
|
1336 |
*/
|
1337 |
static int dca_exss_mask2count(int mask) |
1338 |
{ |
1339 |
/* count bits that mean speaker pairs twice */
|
1340 |
return av_popcount(mask)
|
1341 |
+ av_popcount(mask & ( |
1342 |
DCA_EXSS_CENTER_LEFT_RIGHT |
1343 |
| DCA_EXSS_FRONT_LEFT_RIGHT |
1344 |
| DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1345 |
| DCA_EXSS_WIDE_LEFT_RIGHT |
1346 |
| DCA_EXSS_SIDE_LEFT_RIGHT |
1347 |
| DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1348 |
| DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1349 |
| DCA_EXSS_REAR_LEFT_RIGHT |
1350 |
| DCA_EXSS_REAR_HIGH_LEFT_RIGHT |
1351 |
)); |
1352 |
} |
1353 |
|
1354 |
/**
|
1355 |
* Skip mixing coefficients of a single mix out configuration (HD)
|
1356 |
*/
|
1357 |
static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch) |
1358 |
{ |
1359 |
int i;
|
1360 |
|
1361 |
for (i = 0; i < channels; i++) { |
1362 |
int mix_map_mask = get_bits(gb, out_ch);
|
1363 |
int num_coeffs = av_popcount(mix_map_mask);
|
1364 |
skip_bits_long(gb, num_coeffs * 6);
|
1365 |
} |
1366 |
} |
1367 |
|
1368 |
/**
|
1369 |
* Parse extension substream asset header (HD)
|
1370 |
*/
|
1371 |
static int dca_exss_parse_asset_header(DCAContext *s) |
1372 |
{ |
1373 |
int header_pos = get_bits_count(&s->gb);
|
1374 |
int header_size;
|
1375 |
int channels;
|
1376 |
int embedded_stereo = 0; |
1377 |
int embedded_6ch = 0; |
1378 |
int drc_code_present;
|
1379 |
int extensions_mask;
|
1380 |
int i, j;
|
1381 |
|
1382 |
if (get_bits_left(&s->gb) < 16) |
1383 |
return -1; |
1384 |
|
1385 |
/* We will parse just enough to get to the extensions bitmask with which
|
1386 |
* we can set the profile value. */
|
1387 |
|
1388 |
header_size = get_bits(&s->gb, 9) + 1; |
1389 |
skip_bits(&s->gb, 3); // asset index |
1390 |
|
1391 |
if (s->static_fields) {
|
1392 |
if (get_bits1(&s->gb))
|
1393 |
skip_bits(&s->gb, 4); // asset type descriptor |
1394 |
if (get_bits1(&s->gb))
|
1395 |
skip_bits_long(&s->gb, 24); // language descriptor |
1396 |
|
1397 |
if (get_bits1(&s->gb)) {
|
1398 |
/* How can one fit 1024 bytes of text here if the maximum value
|
1399 |
* for the asset header size field above was 512 bytes? */
|
1400 |
int text_length = get_bits(&s->gb, 10) + 1; |
1401 |
if (get_bits_left(&s->gb) < text_length * 8) |
1402 |
return -1; |
1403 |
skip_bits_long(&s->gb, text_length * 8); // info text |
1404 |
} |
1405 |
|
1406 |
skip_bits(&s->gb, 5); // bit resolution - 1 |
1407 |
skip_bits(&s->gb, 4); // max sample rate code |
1408 |
channels = get_bits(&s->gb, 8) + 1; |
1409 |
|
1410 |
if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers |
1411 |
int spkr_remap_sets;
|
1412 |
int spkr_mask_size = 16; |
1413 |
int num_spkrs[7]; |
1414 |
|
1415 |
if (channels > 2) |
1416 |
embedded_stereo = get_bits1(&s->gb); |
1417 |
if (channels > 6) |
1418 |
embedded_6ch = get_bits1(&s->gb); |
1419 |
|
1420 |
if (get_bits1(&s->gb)) {
|
1421 |
spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2; |
1422 |
skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
|
1423 |
} |
1424 |
|
1425 |
spkr_remap_sets = get_bits(&s->gb, 3);
|
1426 |
|
1427 |
for (i = 0; i < spkr_remap_sets; i++) { |
1428 |
/* std layout mask for each remap set */
|
1429 |
num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size)); |
1430 |
} |
1431 |
|
1432 |
for (i = 0; i < spkr_remap_sets; i++) { |
1433 |
int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1; |
1434 |
if (get_bits_left(&s->gb) < 0) |
1435 |
return -1; |
1436 |
|
1437 |
for (j = 0; j < num_spkrs[i]; j++) { |
1438 |
int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
|
1439 |
int num_dec_ch = av_popcount(remap_dec_ch_mask);
|
1440 |
skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes |
1441 |
} |
1442 |
} |
1443 |
|
1444 |
} else {
|
1445 |
skip_bits(&s->gb, 3); // representation type |
1446 |
} |
1447 |
} |
1448 |
|
1449 |
drc_code_present = get_bits1(&s->gb); |
1450 |
if (drc_code_present)
|
1451 |
get_bits(&s->gb, 8); // drc code |
1452 |
|
1453 |
if (get_bits1(&s->gb))
|
1454 |
skip_bits(&s->gb, 5); // dialog normalization code |
1455 |
|
1456 |
if (drc_code_present && embedded_stereo)
|
1457 |
get_bits(&s->gb, 8); // drc stereo code |
1458 |
|
1459 |
if (s->mix_metadata && get_bits1(&s->gb)) {
|
1460 |
skip_bits(&s->gb, 1); // external mix |
1461 |
skip_bits(&s->gb, 6); // post mix gain code |
1462 |
|
1463 |
if (get_bits(&s->gb, 2) != 3) // mixer drc code |
1464 |
skip_bits(&s->gb, 3); // drc limit |
1465 |
else
|
1466 |
skip_bits(&s->gb, 8); // custom drc code |
1467 |
|
1468 |
if (get_bits1(&s->gb)) // channel specific scaling |
1469 |
for (i = 0; i < s->num_mix_configs; i++) |
1470 |
skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes |
1471 |
else
|
1472 |
skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes |
1473 |
|
1474 |
for (i = 0; i < s->num_mix_configs; i++) { |
1475 |
if (get_bits_left(&s->gb) < 0) |
1476 |
return -1; |
1477 |
dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]); |
1478 |
if (embedded_6ch)
|
1479 |
dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
|
1480 |
if (embedded_stereo)
|
1481 |
dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
|
1482 |
} |
1483 |
} |
1484 |
|
1485 |
switch (get_bits(&s->gb, 2)) { |
1486 |
case 0: extensions_mask = get_bits(&s->gb, 12); break; |
1487 |
case 1: extensions_mask = DCA_EXT_EXSS_XLL; break; |
1488 |
case 2: extensions_mask = DCA_EXT_EXSS_LBR; break; |
1489 |
case 3: extensions_mask = 0; /* aux coding */ break; |
1490 |
} |
1491 |
|
1492 |
/* not parsed further, we were only interested in the extensions mask */
|
1493 |
|
1494 |
if (get_bits_left(&s->gb) < 0) |
1495 |
return -1; |
1496 |
|
1497 |
if (get_bits_count(&s->gb) - header_pos > header_size * 8) { |
1498 |
av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
|
1499 |
return -1; |
1500 |
} |
1501 |
skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
|
1502 |
|
1503 |
if (extensions_mask & DCA_EXT_EXSS_XLL)
|
1504 |
s->profile = FF_PROFILE_DTS_HD_MA; |
1505 |
else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 | |
1506 |
DCA_EXT_EXSS_XXCH)) |
1507 |
s->profile = FF_PROFILE_DTS_HD_HRA; |
1508 |
|
1509 |
if (!(extensions_mask & DCA_EXT_CORE))
|
1510 |
av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
|
1511 |
if (!!(extensions_mask & DCA_EXT_XCH) != s->xch_present)
|
1512 |
av_log(s->avctx, AV_LOG_WARNING, "DTS XCh detection mismatch.\n");
|
1513 |
if (!!(extensions_mask & DCA_EXT_XXCH) != s->xxch_present)
|
1514 |
av_log(s->avctx, AV_LOG_WARNING, "DTS XXCh detection mismatch.\n");
|
1515 |
if (!!(extensions_mask & DCA_EXT_X96) != s->x96_present)
|
1516 |
av_log(s->avctx, AV_LOG_WARNING, "DTS X96 detection mismatch.\n");
|
1517 |
|
1518 |
return 0; |
1519 |
} |
1520 |
|
1521 |
/**
|
1522 |
* Parse extension substream header (HD)
|
1523 |
*/
|
1524 |
static void dca_exss_parse_header(DCAContext *s) |
1525 |
{ |
1526 |
int ss_index;
|
1527 |
int blownup;
|
1528 |
int header_size;
|
1529 |
int hd_size;
|
1530 |
int num_audiop = 1; |
1531 |
int num_assets = 1; |
1532 |
int active_ss_mask[8]; |
1533 |
int i, j;
|
1534 |
|
1535 |
if (get_bits_left(&s->gb) < 52) |
1536 |
return;
|
1537 |
|
1538 |
skip_bits(&s->gb, 8); // user data |
1539 |
ss_index = get_bits(&s->gb, 2);
|
1540 |
|
1541 |
blownup = get_bits1(&s->gb); |
1542 |
header_size = get_bits(&s->gb, 8 + 4 * blownup) + 1; |
1543 |
hd_size = get_bits_long(&s->gb, 16 + 4 * blownup) + 1; |
1544 |
|
1545 |
s->static_fields = get_bits1(&s->gb); |
1546 |
if (s->static_fields) {
|
1547 |
skip_bits(&s->gb, 2); // reference clock code |
1548 |
skip_bits(&s->gb, 3); // frame duration code |
1549 |
|
1550 |
if (get_bits1(&s->gb))
|
1551 |
skip_bits_long(&s->gb, 36); // timestamp |
1552 |
|
1553 |
/* a single stream can contain multiple audio assets that can be
|
1554 |
* combined to form multiple audio presentations */
|
1555 |
|
1556 |
num_audiop = get_bits(&s->gb, 3) + 1; |
1557 |
if (num_audiop > 1) { |
1558 |
av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
|
1559 |
/* ignore such streams for now */
|
1560 |
return;
|
1561 |
} |
1562 |
|
1563 |
num_assets = get_bits(&s->gb, 3) + 1; |
1564 |
if (num_assets > 1) { |
1565 |
av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
|
1566 |
/* ignore such streams for now */
|
1567 |
return;
|
1568 |
} |
1569 |
|
1570 |
for (i = 0; i < num_audiop; i++) |
1571 |
active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
|
1572 |
|
1573 |
for (i = 0; i < num_audiop; i++) |
1574 |
for (j = 0; j <= ss_index; j++) |
1575 |
if (active_ss_mask[i] & (1 << j)) |
1576 |
skip_bits(&s->gb, 8); // active asset mask |
1577 |
|
1578 |
s->mix_metadata = get_bits1(&s->gb); |
1579 |
if (s->mix_metadata) {
|
1580 |
int mix_out_mask_size;
|
1581 |
|
1582 |
skip_bits(&s->gb, 2); // adjustment level |
1583 |
mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2; |
1584 |
s->num_mix_configs = get_bits(&s->gb, 2) + 1; |
1585 |
|
1586 |
for (i = 0; i < s->num_mix_configs; i++) { |
1587 |
int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
|
1588 |
s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask); |
1589 |
} |
1590 |
} |
1591 |
} |
1592 |
|
1593 |
for (i = 0; i < num_assets; i++) |
1594 |
skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size |
1595 |
|
1596 |
for (i = 0; i < num_assets; i++) { |
1597 |
if (dca_exss_parse_asset_header(s))
|
1598 |
return;
|
1599 |
} |
1600 |
|
1601 |
/* not parsed further, we were only interested in the extensions mask
|
1602 |
* from the asset header */
|
1603 |
} |
1604 |
|
1605 |
/**
|
1606 |
* Main frame decoding function
|
1607 |
* FIXME add arguments
|
1608 |
*/
|
1609 |
static int dca_decode_frame(AVCodecContext * avctx, |
1610 |
void *data, int *data_size, |
1611 |
AVPacket *avpkt) |
1612 |
{ |
1613 |
const uint8_t *buf = avpkt->data;
|
1614 |
int buf_size = avpkt->size;
|
1615 |
|
1616 |
int lfe_samples;
|
1617 |
int num_core_channels = 0; |
1618 |
int i;
|
1619 |
int16_t *samples = data; |
1620 |
DCAContext *s = avctx->priv_data; |
1621 |
int channels;
|
1622 |
int core_ss_end;
|
1623 |
|
1624 |
|
1625 |
s->xch_present = 0;
|
1626 |
s->x96_present = 0;
|
1627 |
s->xxch_present = 0;
|
1628 |
|
1629 |
s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, |
1630 |
DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE); |
1631 |
if (s->dca_buffer_size == -1) { |
1632 |
av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
|
1633 |
return -1; |
1634 |
} |
1635 |
|
1636 |
init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
|
1637 |
if (dca_parse_frame_header(s) < 0) { |
1638 |
//seems like the frame is corrupt, try with the next one
|
1639 |
*data_size=0;
|
1640 |
return buf_size;
|
1641 |
} |
1642 |
//set AVCodec values with parsed data
|
1643 |
avctx->sample_rate = s->sample_rate; |
1644 |
avctx->bit_rate = s->bit_rate; |
1645 |
|
1646 |
s->profile = FF_PROFILE_DTS; |
1647 |
|
1648 |
for (i = 0; i < (s->sample_blocks / 8); i++) { |
1649 |
dca_decode_block(s, 0, i);
|
1650 |
} |
1651 |
|
1652 |
/* record number of core channels incase less than max channels are requested */
|
1653 |
num_core_channels = s->prim_channels; |
1654 |
|
1655 |
/* extensions start at 32-bit boundaries into bitstream */
|
1656 |
skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
|
1657 |
|
1658 |
core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
|
1659 |
|
1660 |
while(core_ss_end - get_bits_count(&s->gb) >= 32) { |
1661 |
uint32_t bits = get_bits_long(&s->gb, 32);
|
1662 |
|
1663 |
switch(bits) {
|
1664 |
case 0x5a5a5a5a: { |
1665 |
int ext_amode, xch_fsize;
|
1666 |
|
1667 |
s->xch_base_channel = s->prim_channels; |
1668 |
|
1669 |
/* validate sync word using XCHFSIZE field */
|
1670 |
xch_fsize = show_bits(&s->gb, 10);
|
1671 |
if((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) && |
1672 |
(s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1)) |
1673 |
continue;
|
1674 |
|
1675 |
/* skip length-to-end-of-frame field for the moment */
|
1676 |
skip_bits(&s->gb, 10);
|
1677 |
|
1678 |
s->profile = FFMAX(s->profile, FF_PROFILE_DTS_ES); |
1679 |
|
1680 |
/* extension amode should == 1, number of channels in extension */
|
1681 |
/* AFAIK XCh is not used for more channels */
|
1682 |
if ((ext_amode = get_bits(&s->gb, 4)) != 1) { |
1683 |
av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
|
1684 |
" supported!\n",ext_amode);
|
1685 |
continue;
|
1686 |
} |
1687 |
|
1688 |
/* much like core primary audio coding header */
|
1689 |
dca_parse_audio_coding_header(s, s->xch_base_channel); |
1690 |
|
1691 |
for (i = 0; i < (s->sample_blocks / 8); i++) { |
1692 |
dca_decode_block(s, s->xch_base_channel, i); |
1693 |
} |
1694 |
|
1695 |
s->xch_present = 1;
|
1696 |
break;
|
1697 |
} |
1698 |
case 0x47004a03: |
1699 |
/* XXCh: extended channels */
|
1700 |
/* usually found either in core or HD part in DTS-HD HRA streams,
|
1701 |
* but not in DTS-ES which contains XCh extensions instead */
|
1702 |
s->xxch_present = 1;
|
1703 |
s->profile = FFMAX(s->profile, FF_PROFILE_DTS_ES); |
1704 |
break;
|
1705 |
|
1706 |
case 0x1d95f262: { |
1707 |
int fsize96 = show_bits(&s->gb, 12) + 1; |
1708 |
if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96) |
1709 |
continue;
|
1710 |
|
1711 |
av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n", get_bits_count(&s->gb));
|
1712 |
skip_bits(&s->gb, 12);
|
1713 |
av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
|
1714 |
av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4)); |
1715 |
|
1716 |
s->x96_present = 1;
|
1717 |
s->profile = FFMAX(s->profile, FF_PROFILE_DTS_96_24); |
1718 |
break;
|
1719 |
} |
1720 |
} |
1721 |
|
1722 |
skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
|
1723 |
} |
1724 |
|
1725 |
/* check for ExSS (HD part) */
|
1726 |
if (s->dca_buffer_size - s->frame_size > 32 |
1727 |
&& get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
|
1728 |
dca_exss_parse_header(s); |
1729 |
|
1730 |
avctx->profile = s->profile; |
1731 |
|
1732 |
channels = s->prim_channels + !!s->lfe; |
1733 |
|
1734 |
if (s->amode<16) { |
1735 |
avctx->channel_layout = dca_core_channel_layout[s->amode]; |
1736 |
|
1737 |
if (s->xch_present && (!avctx->request_channels ||
|
1738 |
avctx->request_channels > num_core_channels + !!s->lfe)) { |
1739 |
avctx->channel_layout |= AV_CH_BACK_CENTER; |
1740 |
if (s->lfe) {
|
1741 |
avctx->channel_layout |= AV_CH_LOW_FREQUENCY; |
1742 |
s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode]; |
1743 |
} else {
|
1744 |
s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode]; |
1745 |
} |
1746 |
} else {
|
1747 |
channels = num_core_channels + !!s->lfe; |
1748 |
s->xch_present = 0; /* disable further xch processing */ |
1749 |
if (s->lfe) {
|
1750 |
avctx->channel_layout |= AV_CH_LOW_FREQUENCY; |
1751 |
s->channel_order_tab = dca_channel_reorder_lfe[s->amode]; |
1752 |
} else
|
1753 |
s->channel_order_tab = dca_channel_reorder_nolfe[s->amode]; |
1754 |
} |
1755 |
|
1756 |
if (channels > !!s->lfe &&
|
1757 |
s->channel_order_tab[channels - 1 - !!s->lfe] < 0) |
1758 |
return -1; |
1759 |
|
1760 |
if (avctx->request_channels == 2 && s->prim_channels > 2) { |
1761 |
channels = 2;
|
1762 |
s->output = DCA_STEREO; |
1763 |
avctx->channel_layout = AV_CH_LAYOUT_STEREO; |
1764 |
} |
1765 |
} else {
|
1766 |
av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
|
1767 |
return -1; |
1768 |
} |
1769 |
|
1770 |
|
1771 |
/* There is nothing that prevents a dts frame to change channel configuration
|
1772 |
but FFmpeg doesn't support that so only set the channels if it is previously
|
1773 |
unset. Ideally during the first probe for channels the crc should be checked
|
1774 |
and only set avctx->channels when the crc is ok. Right now the decoder could
|
1775 |
set the channels based on a broken first frame.*/
|
1776 |
if (s->is_channels_set == 0) { |
1777 |
s->is_channels_set = 1;
|
1778 |
avctx->channels = channels; |
1779 |
} |
1780 |
if (avctx->channels != channels) {
|
1781 |
av_log(avctx, AV_LOG_ERROR, "DCA decoder does not support number of "
|
1782 |
"channels changing in stream. Skipping frame.\n");
|
1783 |
return -1; |
1784 |
} |
1785 |
|
1786 |
if (*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels) |
1787 |
return -1; |
1788 |
*data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels; |
1789 |
|
1790 |
/* filter to get final output */
|
1791 |
for (i = 0; i < (s->sample_blocks / 8); i++) { |
1792 |
dca_filter_channels(s, i); |
1793 |
|
1794 |
/* If this was marked as a DTS-ES stream we need to subtract back- */
|
1795 |
/* channel from SL & SR to remove matrixed back-channel signal */
|
1796 |
if((s->source_pcm_res & 1) && s->xch_present) { |
1797 |
float* back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256; |
1798 |
float* lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256; |
1799 |
float* rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256; |
1800 |
int j;
|
1801 |
for(j = 0; j < 256; ++j) { |
1802 |
lt_chan[j] -= back_chan[j] * M_SQRT1_2; |
1803 |
rt_chan[j] -= back_chan[j] * M_SQRT1_2; |
1804 |
} |
1805 |
} |
1806 |
|
1807 |
s->fmt_conv.float_to_int16_interleave(samples, s->samples_chanptr, 256, channels);
|
1808 |
samples += 256 * channels;
|
1809 |
} |
1810 |
|
1811 |
/* update lfe history */
|
1812 |
lfe_samples = 2 * s->lfe * (s->sample_blocks / 8); |
1813 |
for (i = 0; i < 2 * s->lfe * 4; i++) { |
1814 |
s->lfe_data[i] = s->lfe_data[i + lfe_samples]; |
1815 |
} |
1816 |
|
1817 |
return buf_size;
|
1818 |
} |
1819 |
|
1820 |
|
1821 |
|
1822 |
/**
|
1823 |
* DCA initialization
|
1824 |
*
|
1825 |
* @param avctx pointer to the AVCodecContext
|
1826 |
*/
|
1827 |
|
1828 |
static av_cold int dca_decode_init(AVCodecContext * avctx) |
1829 |
{ |
1830 |
DCAContext *s = avctx->priv_data; |
1831 |
int i;
|
1832 |
|
1833 |
s->avctx = avctx; |
1834 |
dca_init_vlcs(); |
1835 |
|
1836 |
dsputil_init(&s->dsp, avctx); |
1837 |
ff_mdct_init(&s->imdct, 6, 1, 1.0); |
1838 |
ff_synth_filter_init(&s->synth); |
1839 |
ff_dcadsp_init(&s->dcadsp); |
1840 |
ff_fmt_convert_init(&s->fmt_conv, avctx); |
1841 |
|
1842 |
for (i = 0; i < DCA_PRIM_CHANNELS_MAX+1; i++) |
1843 |
s->samples_chanptr[i] = s->samples + i * 256;
|
1844 |
avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
1845 |
|
1846 |
s->scale_bias = 1.0; |
1847 |
|
1848 |
/* allow downmixing to stereo */
|
1849 |
if (avctx->channels > 0 && avctx->request_channels < avctx->channels && |
1850 |
avctx->request_channels == 2) {
|
1851 |
avctx->channels = avctx->request_channels; |
1852 |
} |
1853 |
|
1854 |
return 0; |
1855 |
} |
1856 |
|
1857 |
static av_cold int dca_decode_end(AVCodecContext * avctx) |
1858 |
{ |
1859 |
DCAContext *s = avctx->priv_data; |
1860 |
ff_mdct_end(&s->imdct); |
1861 |
return 0; |
1862 |
} |
1863 |
|
1864 |
static const AVProfile profiles[] = { |
1865 |
{ FF_PROFILE_DTS, "DTS" },
|
1866 |
{ FF_PROFILE_DTS_ES, "DTS-ES" },
|
1867 |
{ FF_PROFILE_DTS_96_24, "DTS 96/24" },
|
1868 |
{ FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
|
1869 |
{ FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
|
1870 |
{ FF_PROFILE_UNKNOWN }, |
1871 |
}; |
1872 |
|
1873 |
AVCodec ff_dca_decoder = { |
1874 |
.name = "dca",
|
1875 |
.type = AVMEDIA_TYPE_AUDIO, |
1876 |
.id = CODEC_ID_DTS, |
1877 |
.priv_data_size = sizeof(DCAContext),
|
1878 |
.init = dca_decode_init, |
1879 |
.decode = dca_decode_frame, |
1880 |
.close = dca_decode_end, |
1881 |
.long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
|
1882 |
.capabilities = CODEC_CAP_CHANNEL_CONF, |
1883 |
.profiles = NULL_IF_CONFIG_SMALL(profiles), |
1884 |
}; |