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