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