dca: Refactor dca_filter_channels() a little
[libav.git] / libavcodec / dcadec.c
CommitLineData
01ca9ac3
KS
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
217e4ff4
NM
7 * Copyright (C) 2012 Paul B Mahol
8 * Copyright (C) 2014 Niels Möller
01ca9ac3 9 *
2912e87a 10 * This file is part of Libav.
01ca9ac3 11 *
2912e87a 12 * Libav is free software; you can redistribute it and/or
01ca9ac3
KS
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
2912e87a 17 * Libav is distributed in the hope that it will be useful,
01ca9ac3
KS
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
2912e87a 23 * License along with Libav; if not, write to the Free Software
01ca9ac3
KS
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
01ca9ac3
KS
27#include <math.h>
28#include <stddef.h>
29#include <stdio.h>
30
217e4ff4 31#include "libavutil/attributes.h"
a903f8f0 32#include "libavutil/channel_layout.h"
f5a2d285 33#include "libavutil/common.h"
cb5042d0 34#include "libavutil/float_dsp.h"
218aefce 35#include "libavutil/internal.h"
0dc7df28 36#include "libavutil/intreadwrite.h"
681e7268 37#include "libavutil/mathematics.h"
3c8507a8 38#include "libavutil/opt.h"
61d5313d 39#include "libavutil/samplefmt.h"
45ff7c93 40
01ca9ac3 41#include "avcodec.h"
98c98e04 42#include "dca.h"
25f613f8 43#include "dca_syncwords.h"
45ff7c93 44#include "dcadata.h"
309d16a4 45#include "dcadsp.h"
45ff7c93
GD
46#include "dcahuff.h"
47#include "fft.h"
c73d99e6 48#include "fmtconvert.h"
45ff7c93 49#include "get_bits.h"
594d4d5d 50#include "internal.h"
45ff7c93 51#include "mathops.h"
2c681139 52#include "profiles.h"
45ff7c93
GD
53#include "put_bits.h"
54#include "synth_filter.h"
01ca9ac3 55
bf5d46d8
MR
56#if ARCH_ARM
57# include "arm/dca.h"
58#endif
59
01ca9ac3
KS
60enum DCAMode {
61 DCA_MONO = 0,
62 DCA_CHANNEL,
63 DCA_STEREO,
64 DCA_STEREO_SUMDIFF,
65 DCA_STEREO_TOTAL,
66 DCA_3F,
67 DCA_2F1R,
68 DCA_3F1R,
69 DCA_2F2R,
70 DCA_3F2R,
71 DCA_4F2R
72};
73
7e06e0ed
AH
74/* -1 are reserved or unknown */
75static const int dca_ext_audio_descr_mask[] = {
76 DCA_EXT_XCH,
77 -1,
78 DCA_EXT_X96,
79 DCA_EXT_XCH | DCA_EXT_X96,
80 -1,
81 -1,
82 DCA_EXT_XXCH,
83 -1,
84};
85
87c3b9bc
BL
86/* Tables for mapping dts channel configurations to libavcodec multichannel api.
87 * Some compromises have been made for special configurations. Most configurations
88 * are never used so complete accuracy is not needed.
89 *
90 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
e22192ec 91 * S -> side, when both rear and back are configured move one of them to the side channel
87c3b9bc 92 * OV -> center back
c2fcd0a7 93 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
87c3b9bc 94 */
cc276c85 95static const uint64_t dca_core_channel_layout[] = {
f37b4efe
SG
96 AV_CH_FRONT_CENTER, ///< 1, A
97 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
98 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
99 AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
100 AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
101 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
102 AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
103 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
104 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
105
106 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
107 AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
108
109 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
110 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
111
112 AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
113 AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
114
115 AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
116 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
117 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
118
119 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
120 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
121 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
122
123 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
124 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
125 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
126
127 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
128 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
129 AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
87c3b9bc
BL
130};
131
f37b4efe 132#define DCA_DOLBY 101 /* FIXME */
01ca9ac3 133
f37b4efe
SG
134#define DCA_CHANNEL_BITS 6
135#define DCA_CHANNEL_MASK 0x3F
01ca9ac3 136
f37b4efe 137#define DCA_LFE 0x80
01ca9ac3 138
f37b4efe 139#define HEADER_SIZE 14
01ca9ac3 140
7dc827b7
TW
141#define DCA_NSYNCAUX 0x9A1105A0
142
01ca9ac3 143/** Bit allocation */
7f9f771e 144typedef struct BitAlloc {
01ca9ac3
KS
145 int offset; ///< code values offset
146 int maxbits[8]; ///< max bits in VLC
147 int wrap; ///< wrap for get_vlc2()
148 VLC vlc[8]; ///< actual codes
149} BitAlloc;
150
151static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
152static BitAlloc dca_tmode; ///< transition mode VLCs
153static BitAlloc dca_scalefactor; ///< scalefactor VLCs
154static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
155
f37b4efe
SG
156static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
157 int idx)
01ca9ac3 158{
f37b4efe
SG
159 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
160 ba->offset;
01ca9ac3
KS
161}
162
98a6fff9 163static av_cold void dca_init_vlcs(void)
01ca9ac3 164{
5e534865 165 static int vlcs_initialized = 0;
0cfa85dd
KS
166 int i, j, c = 14;
167 static VLC_TYPE dca_table[23622][2];
01ca9ac3 168
5e534865 169 if (vlcs_initialized)
01ca9ac3
KS
170 return;
171
172 dca_bitalloc_index.offset = 1;
45ff7c93 173 dca_bitalloc_index.wrap = 2;
0cfa85dd 174 for (i = 0; i < 5; i++) {
3a651f59
DB
175 dca_bitalloc_index.vlc[i].table = &dca_table[ff_dca_vlc_offs[i]];
176 dca_bitalloc_index.vlc[i].table_allocated = ff_dca_vlc_offs[i + 1] - ff_dca_vlc_offs[i];
01ca9ac3
KS
177 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
178 bitalloc_12_bits[i], 1, 1,
0cfa85dd
KS
179 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
180 }
01ca9ac3 181 dca_scalefactor.offset = -64;
45ff7c93 182 dca_scalefactor.wrap = 2;
0cfa85dd 183 for (i = 0; i < 5; i++) {
3a651f59
DB
184 dca_scalefactor.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 5]];
185 dca_scalefactor.vlc[i].table_allocated = ff_dca_vlc_offs[i + 6] - ff_dca_vlc_offs[i + 5];
01ca9ac3
KS
186 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
187 scales_bits[i], 1, 1,
0cfa85dd
KS
188 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
189 }
01ca9ac3 190 dca_tmode.offset = 0;
45ff7c93 191 dca_tmode.wrap = 1;
0cfa85dd 192 for (i = 0; i < 4; i++) {
3a651f59
DB
193 dca_tmode.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 10]];
194 dca_tmode.vlc[i].table_allocated = ff_dca_vlc_offs[i + 11] - ff_dca_vlc_offs[i + 10];
01ca9ac3
KS
195 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
196 tmode_bits[i], 1, 1,
0cfa85dd
KS
197 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
198 }
01ca9ac3 199
d1177cb5 200 for (i = 0; i < 10; i++)
f37b4efe
SG
201 for (j = 0; j < 7; j++) {
202 if (!bitalloc_codes[i][j])
203 break;
204 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
205 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
3a651f59
DB
206 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[ff_dca_vlc_offs[c]];
207 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = ff_dca_vlc_offs[c + 1] - ff_dca_vlc_offs[c];
f37b4efe
SG
208
209 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
01ca9ac3
KS
210 bitalloc_sizes[i],
211 bitalloc_bits[i][j], 1, 1,
0cfa85dd
KS
212 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
213 c++;
01ca9ac3 214 }
5e534865 215 vlcs_initialized = 1;
01ca9ac3
KS
216}
217
218static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
219{
f37b4efe 220 while (len--)
01ca9ac3
KS
221 *dst++ = get_bits(gb, bits);
222}
223
f37b4efe 224static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
01ca9ac3
KS
225{
226 int i, j;
aebf0707 227 static const uint8_t adj_table[4] = { 16, 18, 20, 23 };
01ca9ac3 228 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
f37b4efe 229 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
01ca9ac3 230
58b42345
AK
231 s->audio_header.total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
232 s->audio_header.prim_channels = s->audio_header.total_channels;
774e9acf 233
58b42345
AK
234 if (s->audio_header.prim_channels > DCA_PRIM_CHANNELS_MAX)
235 s->audio_header.prim_channels = DCA_PRIM_CHANNELS_MAX;
6baef06e 236
58b42345
AK
237 for (i = base_channel; i < s->audio_header.prim_channels; i++) {
238 s->audio_header.subband_activity[i] = get_bits(&s->gb, 5) + 2;
239 if (s->audio_header.subband_activity[i] > DCA_SUBBANDS)
240 s->audio_header.subband_activity[i] = DCA_SUBBANDS;
6baef06e 241 }
58b42345
AK
242 for (i = base_channel; i < s->audio_header.prim_channels; i++) {
243 s->audio_header.vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
244 if (s->audio_header.vq_start_subband[i] > DCA_SUBBANDS)
245 s->audio_header.vq_start_subband[i] = DCA_SUBBANDS;
6baef06e 246 }
58b42345
AK
247 get_array(&s->gb, s->audio_header.joint_intensity + base_channel,
248 s->audio_header.prim_channels - base_channel, 3);
249 get_array(&s->gb, s->audio_header.transient_huffman + base_channel,
250 s->audio_header.prim_channels - base_channel, 2);
251 get_array(&s->gb, s->audio_header.scalefactor_huffman + base_channel,
252 s->audio_header.prim_channels - base_channel, 3);
253 get_array(&s->gb, s->audio_header.bitalloc_huffman + base_channel,
254 s->audio_header.prim_channels - base_channel, 3);
6baef06e
NB
255
256 /* Get codebooks quantization indexes */
774e9acf 257 if (!base_channel)
58b42345 258 memset(s->audio_header.quant_index_huffman, 0, sizeof(s->audio_header.quant_index_huffman));
6baef06e 259 for (j = 1; j < 11; j++)
58b42345
AK
260 for (i = base_channel; i < s->audio_header.prim_channels; i++)
261 s->audio_header.quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
6baef06e
NB
262
263 /* Get scale factor adjustment */
264 for (j = 0; j < 11; j++)
58b42345 265 for (i = base_channel; i < s->audio_header.prim_channels; i++)
aebf0707 266 s->audio_header.scalefactor_adj[i][j] = 16;
6baef06e
NB
267
268 for (j = 1; j < 11; j++)
58b42345
AK
269 for (i = base_channel; i < s->audio_header.prim_channels; i++)
270 if (s->audio_header.quant_index_huffman[i][j] < thr[j])
271 s->audio_header.scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
6baef06e
NB
272
273 if (s->crc_present) {
274 /* Audio header CRC check */
275 get_bits(&s->gb, 16);
276 }
277
f37b4efe 278 s->current_subframe = 0;
6baef06e
NB
279 s->current_subsubframe = 0;
280
f37b4efe 281 return 0;
6baef06e
NB
282}
283
f37b4efe 284static int dca_parse_frame_header(DCAContext *s)
6baef06e 285{
01ca9ac3
KS
286 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
287
288 /* Sync code */
999e7ebd 289 skip_bits_long(&s->gb, 32);
01ca9ac3
KS
290
291 /* Frame header */
292 s->frame_type = get_bits(&s->gb, 1);
293 s->samples_deficit = get_bits(&s->gb, 5) + 1;
294 s->crc_present = get_bits(&s->gb, 1);
295 s->sample_blocks = get_bits(&s->gb, 7) + 1;
296 s->frame_size = get_bits(&s->gb, 14) + 1;
297 if (s->frame_size < 95)
f44059d2 298 return AVERROR_INVALIDDATA;
01ca9ac3 299 s->amode = get_bits(&s->gb, 6);
19cf7163 300 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
01ca9ac3 301 if (!s->sample_rate)
f44059d2 302 return AVERROR_INVALIDDATA;
d5b3a863 303 s->bit_rate_index = get_bits(&s->gb, 5);
3a651f59 304 s->bit_rate = ff_dca_bit_rates[s->bit_rate_index];
01ca9ac3 305 if (!s->bit_rate)
f44059d2 306 return AVERROR_INVALIDDATA;
01ca9ac3 307
27245b4e 308 skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
01ca9ac3
KS
309 s->dynrange = get_bits(&s->gb, 1);
310 s->timestamp = get_bits(&s->gb, 1);
311 s->aux_data = get_bits(&s->gb, 1);
312 s->hdcd = get_bits(&s->gb, 1);
313 s->ext_descr = get_bits(&s->gb, 3);
314 s->ext_coding = get_bits(&s->gb, 1);
315 s->aspf = get_bits(&s->gb, 1);
316 s->lfe = get_bits(&s->gb, 2);
317 s->predictor_history = get_bits(&s->gb, 1);
318
a9d50bb5
MS
319 if (s->lfe > 2) {
320 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
321 return AVERROR_INVALIDDATA;
322 }
323
01ca9ac3
KS
324 /* TODO: check CRC */
325 if (s->crc_present)
326 s->header_crc = get_bits(&s->gb, 16);
327
328 s->multirate_inter = get_bits(&s->gb, 1);
329 s->version = get_bits(&s->gb, 4);
330 s->copy_history = get_bits(&s->gb, 2);
331 s->source_pcm_res = get_bits(&s->gb, 3);
332 s->front_sum = get_bits(&s->gb, 1);
333 s->surround_sum = get_bits(&s->gb, 1);
334 s->dialog_norm = get_bits(&s->gb, 4);
335
336 /* FIXME: channels mixing levels */
cc826626 337 s->output = s->amode;
f37b4efe
SG
338 if (s->lfe)
339 s->output |= DCA_LFE;
01ca9ac3 340
01ca9ac3 341 /* Primary audio coding header */
58b42345 342 s->audio_header.subframes = get_bits(&s->gb, 4) + 1;
01ca9ac3 343
774e9acf 344 return dca_parse_audio_coding_header(s, 0);
01ca9ac3
KS
345}
346
e6ffd997 347static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
01ca9ac3 348{
f37b4efe
SG
349 if (level < 5) {
350 /* huffman encoded */
351 value += get_bitalloc(gb, &dca_scalefactor, level);
45ff7c93 352 value = av_clip(value, 0, (1 << log2range) - 1);
e6ffd997
RB
353 } else if (level < 8) {
354 if (level + 1 > log2range) {
355 skip_bits(gb, level + 1 - log2range);
356 value = get_bits(gb, log2range);
357 } else {
358 value = get_bits(gb, level + 1);
359 }
360 }
f37b4efe 361 return value;
01ca9ac3
KS
362}
363
f37b4efe 364static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
01ca9ac3
KS
365{
366 /* Primary audio coding side information */
367 int j, k;
368
39f4d329 369 if (get_bits_left(&s->gb) < 0)
f44059d2 370 return AVERROR_INVALIDDATA;
39f4d329 371
774e9acf 372 if (!base_channel) {
f37b4efe 373 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
774e9acf
NB
374 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
375 }
376
58b42345
AK
377 for (j = base_channel; j < s->audio_header.prim_channels; j++) {
378 for (k = 0; k < s->audio_header.subband_activity[j]; k++)
379 s->dca_chan[j].prediction_mode[k] = get_bits(&s->gb, 1);
01ca9ac3
KS
380 }
381
382 /* Get prediction codebook */
58b42345
AK
383 for (j = base_channel; j < s->audio_header.prim_channels; j++) {
384 for (k = 0; k < s->audio_header.subband_activity[j]; k++) {
385 if (s->dca_chan[j].prediction_mode[k] > 0) {
01ca9ac3 386 /* (Prediction coefficient VQ address) */
58b42345 387 s->dca_chan[j].prediction_vq[k] = get_bits(&s->gb, 12);
01ca9ac3
KS
388 }
389 }
390 }
391
392 /* Bit allocation index */
58b42345
AK
393 for (j = base_channel; j < s->audio_header.prim_channels; j++) {
394 for (k = 0; k < s->audio_header.vq_start_subband[j]; k++) {
395 if (s->audio_header.bitalloc_huffman[j] == 6)
396 s->dca_chan[j].bitalloc[k] = get_bits(&s->gb, 5);
397 else if (s->audio_header.bitalloc_huffman[j] == 5)
398 s->dca_chan[j].bitalloc[k] = get_bits(&s->gb, 4);
399 else if (s->audio_header.bitalloc_huffman[j] == 7) {
ebf71dbd
BL
400 av_log(s->avctx, AV_LOG_ERROR,
401 "Invalid bit allocation index\n");
f44059d2 402 return AVERROR_INVALIDDATA;
ebf71dbd 403 } else {
58b42345
AK
404 s->dca_chan[j].bitalloc[k] =
405 get_bitalloc(&s->gb, &dca_bitalloc_index, s->audio_header.bitalloc_huffman[j]);
01ca9ac3
KS
406 }
407
58b42345 408 if (s->dca_chan[j].bitalloc[k] > 26) {
6a85dfc8 409 ff_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
58b42345 410 j, k, s->dca_chan[j].bitalloc[k]);
f44059d2 411 return AVERROR_INVALIDDATA;
01ca9ac3
KS
412 }
413 }
414 }
415
416 /* Transition mode */
58b42345
AK
417 for (j = base_channel; j < s->audio_header.prim_channels; j++) {
418 for (k = 0; k < s->audio_header.subband_activity[j]; k++) {
419 s->dca_chan[j].transition_mode[k] = 0;
774e9acf 420 if (s->subsubframes[s->current_subframe] > 1 &&
58b42345
AK
421 k < s->audio_header.vq_start_subband[j] && s->dca_chan[j].bitalloc[k] > 0) {
422 s->dca_chan[j].transition_mode[k] =
423 get_bitalloc(&s->gb, &dca_tmode, s->audio_header.transient_huffman[j]);
01ca9ac3
KS
424 }
425 }
426 }
427
39f4d329 428 if (get_bits_left(&s->gb) < 0)
f44059d2 429 return AVERROR_INVALIDDATA;
39f4d329 430
58b42345 431 for (j = base_channel; j < s->audio_header.prim_channels; j++) {
a9f87158 432 const uint32_t *scale_table;
e6ffd997 433 int scale_sum, log_size;
01ca9ac3 434
58b42345
AK
435 memset(s->dca_chan[j].scale_factor, 0,
436 s->audio_header.subband_activity[j] * sizeof(s->dca_chan[j].scale_factor[0][0]) * 2);
01ca9ac3 437
58b42345 438 if (s->audio_header.scalefactor_huffman[j] == 6) {
3a651f59 439 scale_table = ff_dca_scale_factor_quant7;
45ff7c93 440 log_size = 7;
e6ffd997 441 } else {
3a651f59 442 scale_table = ff_dca_scale_factor_quant6;
45ff7c93 443 log_size = 6;
e6ffd997 444 }
01ca9ac3
KS
445
446 /* When huffman coded, only the difference is encoded */
447 scale_sum = 0;
448
58b42345
AK
449 for (k = 0; k < s->audio_header.subband_activity[j]; k++) {
450 if (k >= s->audio_header.vq_start_subband[j] || s->dca_chan[j].bitalloc[k] > 0) {
451 scale_sum = get_scale(&s->gb, s->audio_header.scalefactor_huffman[j], scale_sum, log_size);
452 s->dca_chan[j].scale_factor[k][0] = scale_table[scale_sum];
01ca9ac3
KS
453 }
454
58b42345 455 if (k < s->audio_header.vq_start_subband[j] && s->dca_chan[j].transition_mode[k]) {
01ca9ac3 456 /* Get second scale factor */
58b42345
AK
457 scale_sum = get_scale(&s->gb, s->audio_header.scalefactor_huffman[j], scale_sum, log_size);
458 s->dca_chan[j].scale_factor[k][1] = scale_table[scale_sum];
01ca9ac3
KS
459 }
460 }
461 }
462
463 /* Joint subband scale factor codebook select */
58b42345 464 for (j = base_channel; j < s->audio_header.prim_channels; j++) {
01ca9ac3 465 /* Transmitted only if joint subband coding enabled */
58b42345
AK
466 if (s->audio_header.joint_intensity[j] > 0)
467 s->dca_chan[j].joint_huff = get_bits(&s->gb, 3);
01ca9ac3
KS
468 }
469
39f4d329 470 if (get_bits_left(&s->gb) < 0)
f44059d2 471 return AVERROR_INVALIDDATA;
39f4d329 472
01ca9ac3 473 /* Scale factors for joint subband coding */
58b42345 474 for (j = base_channel; j < s->audio_header.prim_channels; j++) {
01ca9ac3
KS
475 int source_channel;
476
477 /* Transmitted only if joint subband coding enabled */
58b42345 478 if (s->audio_header.joint_intensity[j] > 0) {
01ca9ac3 479 int scale = 0;
58b42345 480 source_channel = s->audio_header.joint_intensity[j] - 1;
01ca9ac3
KS
481
482 /* When huffman coded, only the difference is encoded
483 * (is this valid as well for joint scales ???) */
484
58b42345
AK
485 for (k = s->audio_header.subband_activity[j];
486 k < s->audio_header.subband_activity[source_channel]; k++) {
487 scale = get_scale(&s->gb, s->dca_chan[j].joint_huff, 64 /* bias */, 7);
488 s->dca_chan[j].joint_scale_factor[k] = scale; /*joint_scale_table[scale]; */
01ca9ac3
KS
489 }
490
268fcbe2 491 if (!(s->debug_flag & 0x02)) {
01ca9ac3
KS
492 av_log(s->avctx, AV_LOG_DEBUG,
493 "Joint stereo coding not supported\n");
494 s->debug_flag |= 0x02;
495 }
496 }
497 }
498
01ca9ac3 499 /* Dynamic range coefficient */
ace7f813 500 if (!base_channel && s->dynrange)
01ca9ac3
KS
501 s->dynrange_coef = get_bits(&s->gb, 8);
502
503 /* Side information CRC check word */
504 if (s->crc_present) {
505 get_bits(&s->gb, 16);
506 }
507
508 /*
509 * Primary audio data arrays
510 */
511
512 /* VQ encoded high frequency subbands */
58b42345
AK
513 for (j = base_channel; j < s->audio_header.prim_channels; j++)
514 for (k = s->audio_header.vq_start_subband[j]; k < s->audio_header.subband_activity[j]; k++)
01ca9ac3 515 /* 1 vector -> 32 samples */
58b42345 516 s->dca_chan[j].high_freq_vq[k] = get_bits(&s->gb, 10);
01ca9ac3
KS
517
518 /* Low frequency effect data */
774e9acf 519 if (!base_channel && s->lfe) {
01ca9ac3 520 /* LFE samples */
45ff7c93 521 int lfe_samples = 2 * s->lfe * (4 + block_index);
774e9acf 522 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
01ca9ac3
KS
523 float lfe_scale;
524
6baef06e 525 for (j = lfe_samples; j < lfe_end_sample; j++) {
01ca9ac3
KS
526 /* Signed 8 bits int */
527 s->lfe_data[j] = get_sbits(&s->gb, 8);
528 }
529
530 /* Scale factor index */
e6ffd997 531 skip_bits(&s->gb, 1);
3a651f59 532 s->lfe_scale_factor = ff_dca_scale_factor_quant7[get_bits(&s->gb, 7)];
01ca9ac3
KS
533
534 /* Quantization step size * scale factor */
535 lfe_scale = 0.035 * s->lfe_scale_factor;
536
6baef06e 537 for (j = lfe_samples; j < lfe_end_sample; j++)
01ca9ac3
KS
538 s->lfe_data[j] *= lfe_scale;
539 }
540
01ca9ac3
KS
541 return 0;
542}
543
f37b4efe 544static void qmf_32_subbands(DCAContext *s, int chans,
40d94967 545 float samples_in[DCA_SUBBANDS][SAMPLES_PER_SUBBAND], float *samples_out,
9d06d7bc 546 float scale)
01ca9ac3 547{
b6398969 548 const float *prCoeff;
01ca9ac3 549
58b42345 550 int sb_act = s->audio_header.subband_activity[chans];
01ca9ac3 551
f37b4efe 552 scale *= sqrt(1 / 8.0);
01ca9ac3
KS
553
554 /* Select filter */
555 if (!s->multirate_inter) /* Non-perfect reconstruction */
3a651f59 556 prCoeff = ff_dca_fir_32bands_nonperfect;
01ca9ac3 557 else /* Perfect reconstruction */
3a651f59 558 prCoeff = ff_dca_fir_32bands_perfect;
01ca9ac3 559
800ffab4 560 s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
58b42345
AK
561 s->dca_chan[chans].subband_fir_hist,
562 &s->dca_chan[chans].hist_index,
563 s->dca_chan[chans].subband_fir_noidea, prCoeff,
800ffab4 564 samples_out, s->raXin, scale);
01ca9ac3
KS
565}
566
217e4ff4
NM
567static QMF64_table *qmf64_precompute(void)
568{
569 unsigned i, j;
570 QMF64_table *table = av_malloc(sizeof(*table));
571 if (!table)
572 return NULL;
573
574 for (i = 0; i < 32; i++)
575 for (j = 0; j < 32; j++)
576 table->dct4_coeff[i][j] = cos((2 * i + 1) * (2 * j + 1) * M_PI / 128);
577 for (i = 0; i < 32; i++)
578 for (j = 0; j < 32; j++)
579 table->dct2_coeff[i][j] = cos((2 * i + 1) * j * M_PI / 64);
580
581 /* FIXME: Is the factor 0.125 = 1/8 right? */
582 for (i = 0; i < 32; i++)
583 table->rcos[i] = 0.125 / cos((2 * i + 1) * M_PI / 256);
584 for (i = 0; i < 32; i++)
585 table->rsin[i] = -0.125 / sin((2 * i + 1) * M_PI / 256);
586
587 return table;
588}
589
590/* FIXME: Totally unoptimized. Based on the reference code and
591 * http://multimedia.cx/mirror/dca-transform.pdf, with guessed tweaks
592 * for doubling the size. */
40d94967
AH
593static void qmf_64_subbands(DCAContext *s, int chans,
594 float samples_in[DCA_SUBBANDS_X96K][SAMPLES_PER_SUBBAND],
217e4ff4
NM
595 float *samples_out, float scale)
596{
597 float raXin[64];
598 float A[32], B[32];
58b42345
AK
599 float *raX = s->dca_chan[chans].subband_fir_hist;
600 float *raZ = s->dca_chan[chans].subband_fir_noidea;
217e4ff4
NM
601 unsigned i, j, k, subindex;
602
40d94967 603 for (i = s->audio_header.subband_activity[chans]; i < DCA_SUBBANDS_X96K; i++)
217e4ff4 604 raXin[i] = 0.0;
d0a3e89d 605 for (subindex = 0; subindex < SAMPLES_PER_SUBBAND; subindex++) {
58b42345 606 for (i = 0; i < s->audio_header.subband_activity[chans]; i++)
217e4ff4
NM
607 raXin[i] = samples_in[i][subindex];
608
609 for (k = 0; k < 32; k++) {
610 A[k] = 0.0;
611 for (i = 0; i < 32; i++)
612 A[k] += (raXin[2 * i] + raXin[2 * i + 1]) * s->qmf64_table->dct4_coeff[k][i];
613 }
614 for (k = 0; k < 32; k++) {
615 B[k] = raXin[0] * s->qmf64_table->dct2_coeff[k][0];
616 for (i = 1; i < 32; i++)
617 B[k] += (raXin[2 * i] + raXin[2 * i - 1]) * s->qmf64_table->dct2_coeff[k][i];
618 }
619 for (k = 0; k < 32; k++) {
620 raX[k] = s->qmf64_table->rcos[k] * (A[k] + B[k]);
621 raX[63 - k] = s->qmf64_table->rsin[k] * (A[k] - B[k]);
622 }
623
40d94967 624 for (i = 0; i < DCA_SUBBANDS_X96K; i++) {
217e4ff4
NM
625 float out = raZ[i];
626 for (j = 0; j < 1024; j += 128)
627 out += ff_dca_fir_64bands[j + i] * (raX[j + i] - raX[j + 63 - i]);
628 *samples_out++ = out * scale;
629 }
630
40d94967 631 for (i = 0; i < DCA_SUBBANDS_X96K; i++) {
217e4ff4
NM
632 float hist = 0.0;
633 for (j = 0; j < 1024; j += 128)
634 hist += ff_dca_fir_64bands[64 + j + i] * (-raX[i + j] - raX[j + 63 - i]);
635
636 raZ[i] = hist;
637 }
638
639 /* FIXME: Make buffer circular, to avoid this move. */
640 memmove(raX + 64, raX, (1024 - 64) * sizeof(*raX));
641 }
642}
643
644static void lfe_interpolation_fir(DCAContext *s, const float *samples_in,
87ec849f 645 float *samples_out)
01ca9ac3
KS
646{
647 /* samples_in: An array holding decimated samples.
648 * Samples in current subframe starts from samples_in[0],
649 * while samples_in[-1], samples_in[-2], ..., stores samples
650 * from last subframe as history.
651 *
652 * samples_out: An array holding interpolated samples
653 */
654
5fdbfcb5 655 int idx;
01ca9ac3 656 const float *prCoeff;
01ca9ac3
KS
657 int deciindex;
658
659 /* Select decimation filter */
217e4ff4 660 if (s->lfe == 1) {
45ff7c93 661 idx = 1;
3a651f59 662 prCoeff = ff_dca_lfe_fir_128;
01ca9ac3 663 } else {
217e4ff4
NM
664 idx = 0;
665 if (s->exss_ext_mask & DCA_EXT_EXSS_XLL)
666 prCoeff = ff_dca_lfe_xll_fir_64;
667 else
668 prCoeff = ff_dca_lfe_fir_64;
01ca9ac3
KS
669 }
670 /* Interpolation */
217e4ff4 671 for (deciindex = 0; deciindex < 2 * s->lfe; deciindex++) {
87ec849f 672 s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
766fefe8 673 samples_in++;
5fdbfcb5 674 samples_out += 2 * 32 * (1 + idx);
01ca9ac3
KS
675 }
676}
677
678/* downmixing routines */
64c312aa
JR
679#define MIX_REAR1(samples, s1, rs, coef) \
680 samples[0][i] += samples[s1][i] * coef[rs][0]; \
681 samples[1][i] += samples[s1][i] * coef[rs][1];
f37b4efe 682
64c312aa
JR
683#define MIX_REAR2(samples, s1, s2, rs, coef) \
684 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
685 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
f37b4efe
SG
686
687#define MIX_FRONT3(samples, coef) \
64c312aa
JR
688 t = samples[c][i]; \
689 u = samples[l][i]; \
690 v = samples[r][i]; \
691 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
692 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
01ca9ac3 693
f37b4efe
SG
694#define DOWNMIX_TO_STEREO(op1, op2) \
695 for (i = 0; i < 256; i++) { \
696 op1 \
697 op2 \
01ca9ac3
KS
698 }
699
aaa44d02
TW
700static void dca_downmix(float **samples, int srcfmt, int lfe_present,
701 float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
9d06d7bc 702 const int8_t *channel_mapping)
01ca9ac3 703{
f37b4efe 704 int c, l, r, sl, sr, s;
01ca9ac3 705 int i;
df984493 706 float t, u, v;
01ca9ac3
KS
707
708 switch (srcfmt) {
709 case DCA_MONO:
01ca9ac3
KS
710 case DCA_4F2R:
711 av_log(NULL, 0, "Not implemented!\n");
712 break;
220494ad 713 case DCA_CHANNEL:
01ca9ac3 714 case DCA_STEREO:
220494ad
TW
715 case DCA_STEREO_TOTAL:
716 case DCA_STEREO_SUMDIFF:
01ca9ac3
KS
717 break;
718 case DCA_3F:
64c312aa
JR
719 c = channel_mapping[0];
720 l = channel_mapping[1];
721 r = channel_mapping[2];
f37b4efe 722 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
01ca9ac3
KS
723 break;
724 case DCA_2F1R:
64c312aa
JR
725 s = channel_mapping[2];
726 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
01ca9ac3
KS
727 break;
728 case DCA_3F1R:
64c312aa
JR
729 c = channel_mapping[0];
730 l = channel_mapping[1];
731 r = channel_mapping[2];
732 s = channel_mapping[3];
c31a76e4 733 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
64c312aa 734 MIX_REAR1(samples, s, 3, coef));
01ca9ac3
KS
735 break;
736 case DCA_2F2R:
64c312aa
JR
737 sl = channel_mapping[2];
738 sr = channel_mapping[3];
739 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
01ca9ac3
KS
740 break;
741 case DCA_3F2R:
64c312aa
JR
742 c = channel_mapping[0];
743 l = channel_mapping[1];
744 r = channel_mapping[2];
745 sl = channel_mapping[3];
746 sr = channel_mapping[4];
c31a76e4 747 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
64c312aa 748 MIX_REAR2(samples, sl, sr, 3, coef));
01ca9ac3
KS
749 break;
750 }
aaa44d02 751 if (lfe_present) {
3a651f59
DB
752 int lf_buf = ff_dca_lfe_index[srcfmt];
753 int lf_idx = ff_dca_channels[srcfmt];
aaa44d02
TW
754 for (i = 0; i < 256; i++) {
755 samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
756 samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
757 }
758 }
01ca9ac3
KS
759}
760
00a856e3 761#ifndef decode_blockcodes
01ca9ac3
KS
762/* Very compact version of the block code decoder that does not use table
763 * look-up but is slightly slower */
b6293e27 764static int decode_blockcode(int code, int levels, int32_t *values)
01ca9ac3
KS
765{
766 int i;
767 int offset = (levels - 1) >> 1;
768
769 for (i = 0; i < 4; i++) {
843c7aa8 770 int div = FASTDIV(code, levels);
f37b4efe 771 values[i] = code - offset - div * levels;
45ff7c93 772 code = div;
01ca9ac3
KS
773 }
774
00a856e3
MR
775 return code;
776}
777
b6293e27 778static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
00a856e3
MR
779{
780 return decode_blockcode(code1, levels, values) |
781 decode_blockcode(code2, levels, values + 4);
01ca9ac3 782}
00a856e3 783#endif
01ca9ac3 784
f37b4efe
SG
785static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
786static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
01ca9ac3 787
f37b4efe 788static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
01ca9ac3
KS
789{
790 int k, l;
791 int subsubframe = s->current_subsubframe;
aebf0707 792 const uint32_t *quant_step_table;
01ca9ac3
KS
793
794 /*
795 * Audio data
796 */
797
798 /* Select quantization step size table */
9ed73b48 799 if (s->bit_rate_index == 0x1f)
aebf0707 800 quant_step_table = ff_dca_lossless_quant;
01ca9ac3 801 else
aebf0707 802 quant_step_table = ff_dca_lossy_quant;
01ca9ac3 803
58b42345 804 for (k = base_channel; k < s->audio_header.prim_channels; k++) {
aebf0707 805 int32_t (*subband_samples)[8] = s->dca_chan[k].subband_samples[block_index];
26ffcc7d 806
39f4d329 807 if (get_bits_left(&s->gb) < 0)
f44059d2 808 return AVERROR_INVALIDDATA;
39f4d329 809
58b42345 810 for (l = 0; l < s->audio_header.vq_start_subband[k]; l++) {
01ca9ac3
KS
811 int m;
812
813 /* Select the mid-tread linear quantizer */
58b42345 814 int abits = s->dca_chan[k].bitalloc[l];
01ca9ac3 815
aebf0707 816 uint32_t quant_step_size = quant_step_table[abits];
01ca9ac3
KS
817
818 /*
819 * Extract bits from the bit stream
820 */
aebf0707
AH
821 if (!abits)
822 memset(subband_samples[l], 0, SAMPLES_PER_SUBBAND *
823 sizeof(subband_samples[l][0]));
824 else {
825 uint32_t rscale;
69e17136 826 /* Deal with transients */
58b42345
AK
827 int sfi = s->dca_chan[k].transition_mode[l] &&
828 subsubframe >= s->dca_chan[k].transition_mode[l];
aebf0707
AH
829 /* Determine quantization index code book and its type.
830 Select quantization index code book */
831 int sel = s->audio_header.quant_index_huffman[k][abits];
832
833 rscale = (s->dca_chan[k].scale_factor[l][sfi] *
834 s->audio_header.scalefactor_adj[k][sel] + 8) >> 4;
69e17136 835
f37b4efe
SG
836 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
837 if (abits <= 7) {
2bb29da6 838 /* Block code */
00a856e3 839 int block_code1, block_code2, size, levels, err;
2bb29da6 840
f37b4efe
SG
841 size = abits_sizes[abits - 1];
842 levels = abits_levels[abits - 1];
2bb29da6
MR
843
844 block_code1 = get_bits(&s->gb, size);
2bb29da6 845 block_code2 = get_bits(&s->gb, size);
45ff7c93 846 err = decode_blockcodes(block_code1, block_code2,
aebf0707 847 levels, subband_samples[l]);
00a856e3
MR
848 if (err) {
849 av_log(s->avctx, AV_LOG_ERROR,
850 "ERROR: block code look-up failed\n");
851 return AVERROR_INVALIDDATA;
852 }
f37b4efe 853 } else {
2bb29da6 854 /* no coding */
d0a3e89d 855 for (m = 0; m < SAMPLES_PER_SUBBAND; m++)
aebf0707 856 subband_samples[l][m] = get_sbits(&s->gb, abits - 3);
2bb29da6 857 }
f37b4efe 858 } else {
2bb29da6 859 /* Huffman coded */
d0a3e89d 860 for (m = 0; m < SAMPLES_PER_SUBBAND; m++)
aebf0707
AH
861 subband_samples[l][m] = get_bitalloc(&s->gb,
862 &dca_smpl_bitalloc[abits], sel);
01ca9ac3 863 }
aebf0707 864 s->dcadsp.dequantize(subband_samples[l], quant_step_size, rscale);
69e17136 865 }
26ffcc7d
BA
866 }
867
58b42345 868 for (l = 0; l < s->audio_header.vq_start_subband[k]; l++) {
26ffcc7d 869 int m;
01ca9ac3
KS
870 /*
871 * Inverse ADPCM if in prediction mode
872 */
58b42345 873 if (s->dca_chan[k].prediction_mode[l]) {
01ca9ac3 874 int n;
7686afd0 875 if (s->predictor_history)
58b42345 876 subband_samples[l][0] += (ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][0] *
aebf0707
AH
877 (int64_t)s->dca_chan[k].subband_samples_hist[l][3] +
878 ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][1] *
879 (int64_t)s->dca_chan[k].subband_samples_hist[l][2] +
880 ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][2] *
881 (int64_t)s->dca_chan[k].subband_samples_hist[l][1] +
882 ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][3] *
883 (int64_t)s->dca_chan[k].subband_samples_hist[l][0]) +
884 (1 << 12) >> 13;
d0a3e89d 885 for (m = 1; m < SAMPLES_PER_SUBBAND; m++) {
aebf0707
AH
886 int64_t sum = ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][0] *
887 (int64_t)subband_samples[l][m - 1];
7686afd0 888 for (n = 2; n <= 4; n++)
01ca9ac3 889 if (m >= n)
58b42345 890 sum += ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][n - 1] *
aebf0707 891 (int64_t)subband_samples[l][m - n];
01ca9ac3 892 else if (s->predictor_history)
58b42345 893 sum += ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][n - 1] *
aebf0707
AH
894 (int64_t)s->dca_chan[k].subband_samples_hist[l][m - n + 4];
895 subband_samples[l][m] += (int32_t)(sum + (1 << 12) >> 13);
01ca9ac3
KS
896 }
897 }
58b42345 898
01ca9ac3 899 }
58b42345
AK
900 /* Backup predictor history for adpcm */
901 for (l = 0; l < DCA_SUBBANDS; l++)
902 AV_COPY128(s->dca_chan[k].subband_samples_hist[l], &subband_samples[l][4]);
903
01ca9ac3
KS
904
905 /*
906 * Decode VQ encoded high frequencies
907 */
58b42345 908 if (s->audio_header.subband_activity[k] > s->audio_header.vq_start_subband[k]) {
01ca9ac3 909 if (!s->debug_flag & 0x01) {
f37b4efe
SG
910 av_log(s->avctx, AV_LOG_DEBUG,
911 "Stream with high frequencies VQ coding\n");
01ca9ac3
KS
912 s->debug_flag |= 0x01;
913 }
58b42345 914
2008f760
AH
915 s->dcadsp.decode_hf(subband_samples, s->dca_chan[k].high_freq_vq,
916 ff_dca_high_freq_vq,
917 subsubframe * SAMPLES_PER_SUBBAND,
918 s->dca_chan[k].scale_factor,
919 s->audio_header.vq_start_subband[k],
920 s->audio_header.subband_activity[k]);
01ca9ac3
KS
921 }
922 }
923
924 /* Check for DSYNC after subsubframe */
774e9acf 925 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
8a213179 926 if (get_bits(&s->gb, 16) != 0xFFFF) {
01ca9ac3 927 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
f261e508 928 return AVERROR_INVALIDDATA;
01ca9ac3
KS
929 }
930 }
931
6baef06e
NB
932 return 0;
933}
934
217e4ff4 935static int dca_filter_channels(DCAContext *s, int block_index, int upsample)
6baef06e 936{
6baef06e
NB
937 int k;
938
217e4ff4 939 if (upsample) {
40d94967 940 LOCAL_ALIGNED(32, float, samples, [DCA_SUBBANDS_X96K], [SAMPLES_PER_SUBBAND]);
aebf0707 941
217e4ff4
NM
942 if (!s->qmf64_table) {
943 s->qmf64_table = qmf64_precompute();
944 if (!s->qmf64_table)
945 return AVERROR(ENOMEM);
946 }
947
948 /* 64 subbands QMF */
58b42345 949 for (k = 0; k < s->audio_header.prim_channels; k++) {
a46a4f72 950 int channel = s->channel_order_tab[k];
aebf0707
AH
951 int32_t (*subband_samples)[SAMPLES_PER_SUBBAND] =
952 s->dca_chan[k].subband_samples[block_index];
953
954 s->fmt_conv.int32_to_float(samples[0], subband_samples[0],
40d94967 955 DCA_SUBBANDS_X96K * SAMPLES_PER_SUBBAND);
58b42345 956
a46a4f72 957 if (channel >= 0)
aebf0707 958 qmf_64_subbands(s, k, samples,
a46a4f72 959 s->samples_chanptr[channel],
217e4ff4
NM
960 /* Upsampling needs a factor 2 here. */
961 M_SQRT2 / 32768.0);
962 }
963 } else {
964 /* 32 subbands QMF */
40d94967 965 LOCAL_ALIGNED(32, float, samples, [DCA_SUBBANDS], [SAMPLES_PER_SUBBAND]);
aebf0707 966
58b42345 967 for (k = 0; k < s->audio_header.prim_channels; k++) {
a46a4f72 968 int channel = s->channel_order_tab[k];
aebf0707
AH
969 int32_t (*subband_samples)[SAMPLES_PER_SUBBAND] =
970 s->dca_chan[k].subband_samples[block_index];
971
972 s->fmt_conv.int32_to_float(samples[0], subband_samples[0],
40d94967 973 DCA_SUBBANDS * SAMPLES_PER_SUBBAND);
58b42345 974
a46a4f72 975 if (channel >= 0)
aebf0707 976 qmf_32_subbands(s, k, samples,
a46a4f72 977 s->samples_chanptr[channel],
217e4ff4
NM
978 M_SQRT1_2 / 32768.0);
979 }
01ca9ac3
KS
980 }
981
01ca9ac3 982 /* Generate LFE samples for this subsubframe FIXME!!! */
b6a97199 983 if (s->lfe) {
217e4ff4
NM
984 float *samples = s->samples_chanptr[ff_dca_lfe_index[s->amode]];
985 lfe_interpolation_fir(s,
6baef06e 986 s->lfe_data + 2 * s->lfe * (block_index + 4),
217e4ff4
NM
987 samples);
988 if (upsample) {
989 unsigned i;
990 /* Should apply the filter in Table 6-11 when upsampling. For
991 * now, just duplicate. */
992 for (i = 511; i > 0; i--) {
993 samples[2 * i] =
994 samples[2 * i + 1] = samples[i];
995 }
996 samples[1] = samples[0];
997 }
01ca9ac3
KS
998 }
999
217e4ff4
NM
1000 /* FIXME: This downmixing is probably broken with upsample.
1001 * Probably totally broken also with XLL in general. */
aaa44d02 1002 /* Downmixing to Stereo */
58b42345 1003 if (s->audio_header.prim_channels + !!s->lfe > 2 &&
aaa44d02
TW
1004 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1005 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
1006 s->channel_order_tab);
1007 }
1008
01ca9ac3
KS
1009 return 0;
1010}
1011
f37b4efe 1012static int dca_subframe_footer(DCAContext *s, int base_channel)
01ca9ac3 1013{
44b17d79 1014 int in, out, aux_data_count, aux_data_end, reserved;
7dc827b7 1015 uint32_t nsyncaux;
01ca9ac3
KS
1016
1017 /*
1018 * Unpack optional information
1019 */
1020
774e9acf
NB
1021 /* presumably optional information only appears in the core? */
1022 if (!base_channel) {
d1177cb5 1023 if (s->timestamp)
999e7ebd 1024 skip_bits_long(&s->gb, 32);
01ca9ac3 1025
44b17d79 1026 if (s->aux_data) {
d1177cb5 1027 aux_data_count = get_bits(&s->gb, 6);
01ca9ac3 1028
44b17d79
TW
1029 // align (32-bit)
1030 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1031
1032 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1033
7dc827b7
TW
1034 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1035 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1036 nsyncaux);
44b17d79 1037 return AVERROR_INVALIDDATA;
7dc827b7 1038 }
44b17d79
TW
1039
1040 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1041 avpriv_request_sample(s->avctx,
1042 "Auxiliary Decode Time Stamp Flag");
1043 // align (4-bit)
1044 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1045 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1046 skip_bits_long(&s->gb, 44);
1047 }
1048
1049 if ((s->core_downmix = get_bits1(&s->gb))) {
7dc827b7
TW
1050 int am = get_bits(&s->gb, 3);
1051 switch (am) {
44b17d79
TW
1052 case 0:
1053 s->core_downmix_amode = DCA_MONO;
1054 break;
1055 case 1:
1056 s->core_downmix_amode = DCA_STEREO;
1057 break;
1058 case 2:
1059 s->core_downmix_amode = DCA_STEREO_TOTAL;
1060 break;
1061 case 3:
1062 s->core_downmix_amode = DCA_3F;
1063 break;
1064 case 4:
1065 s->core_downmix_amode = DCA_2F1R;
1066 break;
1067 case 5:
1068 s->core_downmix_amode = DCA_2F2R;
1069 break;
1070 case 6:
1071 s->core_downmix_amode = DCA_3F1R;
1072 break;
1073 default:
7dc827b7
TW
1074 av_log(s->avctx, AV_LOG_ERROR,
1075 "Invalid mode %d for embedded downmix coefficients\n",
1076 am);
44b17d79
TW
1077 return AVERROR_INVALIDDATA;
1078 }
3a651f59 1079 for (out = 0; out < ff_dca_channels[s->core_downmix_amode]; out++) {
58b42345 1080 for (in = 0; in < s->audio_header.prim_channels + !!s->lfe; in++) {
44b17d79 1081 uint16_t tmp = get_bits(&s->gb, 9);
7dc827b7
TW
1082 if ((tmp & 0xFF) > 241) {
1083 av_log(s->avctx, AV_LOG_ERROR,
1084 "Invalid downmix coefficient code %"PRIu16"\n",
1085 tmp);
44b17d79 1086 return AVERROR_INVALIDDATA;
7dc827b7 1087 }
44b17d79
TW
1088 s->core_downmix_codes[in][out] = tmp;
1089 }
1090 }
1091 }
1092
1093 align_get_bits(&s->gb); // byte align
1094 skip_bits(&s->gb, 16); // nAUXCRC16
1095
c12c085b
TW
1096 /*
1097 * additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1098 *
1099 * Note: don't check for overreads, aux_data_count can't be trusted.
1100 */
1101 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) > 0) {
44b17d79
TW
1102 avpriv_request_sample(s->avctx,
1103 "Core auxiliary data reserved content");
1104 skip_bits_long(&s->gb, reserved);
1105 }
1106 }
01ca9ac3 1107
27245b4e 1108 if (s->crc_present && s->dynrange)
d1177cb5 1109 get_bits(&s->gb, 16);
774e9acf 1110 }
01ca9ac3 1111
01ca9ac3
KS
1112 return 0;
1113}
1114
1115/**
1116 * Decode a dca frame block
1117 *
1118 * @param s pointer to the DCAContext
1119 */
1120
f37b4efe 1121static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
01ca9ac3 1122{
f44059d2 1123 int ret;
01ca9ac3
KS
1124
1125 /* Sanity check */
58b42345 1126 if (s->current_subframe >= s->audio_header.subframes) {
01ca9ac3 1127 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
58b42345 1128 s->current_subframe, s->audio_header.subframes);
f44059d2 1129 return AVERROR_INVALIDDATA;
01ca9ac3
KS
1130 }
1131
1132 if (!s->current_subsubframe) {
01ca9ac3 1133 /* Read subframe header */
f44059d2
JR
1134 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1135 return ret;
01ca9ac3
KS
1136 }
1137
1138 /* Read subsubframe */
f44059d2
JR
1139 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1140 return ret;
01ca9ac3
KS
1141
1142 /* Update state */
1143 s->current_subsubframe++;
774e9acf 1144 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
01ca9ac3
KS
1145 s->current_subsubframe = 0;
1146 s->current_subframe++;
1147 }
58b42345 1148 if (s->current_subframe >= s->audio_header.subframes) {
01ca9ac3 1149 /* Read subframe footer */
f44059d2
JR
1150 if ((ret = dca_subframe_footer(s, base_channel)))
1151 return ret;
01ca9ac3
KS
1152 }
1153
1154 return 0;
1155}
1156
6b7b8585
NM
1157static float dca_dmix_code(unsigned code)
1158{
1159 int sign = (code >> 8) - 1;
1160 code &= 0xff;
3a651f59 1161 return ((ff_dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1U << 15));
6b7b8585
NM
1162}
1163
971177f7 1164static int scan_for_extensions(AVCodecContext *avctx)
01ca9ac3 1165{
01ca9ac3 1166 DCAContext *s = avctx->priv_data;
4c160fa2 1167 int core_ss_end, ret = 0;
774e9acf 1168
f5a2d285
AH
1169 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1170
7e06e0ed
AH
1171 /* only scan for extensions if ext_descr was unknown or indicated a
1172 * supported XCh extension */
1173 if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
7e06e0ed
AH
1174 /* if ext_descr was unknown, clear s->core_ext_mask so that the
1175 * extensions scan can fill it up */
1176 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1177
1178 /* extensions start at 32-bit boundaries into bitstream */
1179 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1180
f37b4efe
SG
1181 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1182 uint32_t bits = get_bits_long(&s->gb, 32);
971177f7 1183 int i;
774e9acf 1184
f37b4efe 1185 switch (bits) {
25f613f8 1186 case DCA_SYNCWORD_XCH: {
f37b4efe 1187 int ext_amode, xch_fsize;
0712c230 1188
58b42345 1189 s->xch_base_channel = s->audio_header.prim_channels;
d0a18850 1190
f37b4efe
SG
1191 /* validate sync word using XCHFSIZE field */
1192 xch_fsize = show_bits(&s->gb, 10);
1193 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1194 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1195 continue;
f5a2d285 1196
f37b4efe
SG
1197 /* skip length-to-end-of-frame field for the moment */
1198 skip_bits(&s->gb, 10);
774e9acf 1199
f37b4efe 1200 s->core_ext_mask |= DCA_EXT_XCH;
774e9acf 1201
f37b4efe
SG
1202 /* extension amode(number of channels in extension) should be 1 */
1203 /* AFAIK XCh is not used for more channels */
1204 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
45ff7c93
GD
1205 av_log(avctx, AV_LOG_ERROR,
1206 "XCh extension amode %d not supported!\n",
1207 ext_amode);
272fcc32
JR
1208 continue;
1209 }
f37b4efe
SG
1210
1211 /* much like core primary audio coding header */
1212 dca_parse_audio_coding_header(s, s->xch_base_channel);
1213
1214 for (i = 0; i < (s->sample_blocks / 8); i++)
1215 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1216 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1217 continue;
1218 }
1219
1220 s->xch_present = 1;
1221 break;
774e9acf 1222 }
25f613f8 1223 case DCA_SYNCWORD_XXCH:
f37b4efe
SG
1224 /* XXCh: extended channels */
1225 /* usually found either in core or HD part in DTS-HD HRA streams,
1226 * but not in DTS-ES which contains XCh extensions instead */
1227 s->core_ext_mask |= DCA_EXT_XXCH;
1228 break;
1229
1230 case 0x1d95f262: {
1231 int fsize96 = show_bits(&s->gb, 12) + 1;
1232 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1233 continue;
774e9acf 1234
f37b4efe
SG
1235 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1236 get_bits_count(&s->gb));
1237 skip_bits(&s->gb, 12);
1238 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1239 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
774e9acf 1240
f37b4efe
SG
1241 s->core_ext_mask |= DCA_EXT_X96;
1242 break;
1243 }
1244 }
6baef06e 1245
f37b4efe
SG
1246 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1247 }
7e06e0ed
AH
1248 } else {
1249 /* no supported extensions, skip the rest of the core substream */
1250 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1251 }
1252
1253 if (s->core_ext_mask & DCA_EXT_X96)
1254 s->profile = FF_PROFILE_DTS_96_24;
1255 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1256 s->profile = FF_PROFILE_DTS_ES;
1257
f5a2d285 1258 /* check for ExSS (HD part) */
f37b4efe 1259 if (s->dca_buffer_size - s->frame_size > 32 &&
25f613f8 1260 get_bits_long(&s->gb, 32) == DCA_SYNCWORD_SUBSTREAM)
b339019d 1261 ff_dca_exss_parse_header(s);
f5a2d285 1262
971177f7
AH
1263 return ret;
1264}
1265
77788598 1266static int set_channel_layout(AVCodecContext *avctx, int channels, int num_core_channels)
971177f7 1267{
971177f7 1268 DCAContext *s = avctx->priv_data;
77788598 1269 int i;
92765276 1270
f37b4efe 1271 if (s->amode < 16) {
87c3b9bc 1272 avctx->channel_layout = dca_core_channel_layout[s->amode];
87c3b9bc 1273
58b42345 1274 if (s->audio_header.prim_channels + !!s->lfe > 2 &&
c8cf461c
TW
1275 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1276 /*
1277 * Neither the core's auxiliary data nor our default tables contain
1278 * downmix coefficients for the additional channel coded in the XCh
1279 * extension, so when we're doing a Stereo downmix, don't decode it.
1280 */
1281 s->xch_disable = 1;
1282 }
1283
3c8507a8 1284 if (s->xch_present && !s->xch_disable) {
63e8d976 1285 avctx->channel_layout |= AV_CH_BACK_CENTER;
774e9acf 1286 if (s->lfe) {
63e8d976 1287 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
3a651f59 1288 s->channel_order_tab = ff_dca_channel_reorder_lfe_xch[s->amode];
774e9acf 1289 } else {
3a651f59 1290 s->channel_order_tab = ff_dca_channel_reorder_nolfe_xch[s->amode];
774e9acf
NB
1291 }
1292 } else {
45ff7c93 1293 channels = num_core_channels + !!s->lfe;
08634e7b 1294 s->xch_present = 0; /* disable further xch processing */
774e9acf 1295 if (s->lfe) {
63e8d976 1296 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
3a651f59 1297 s->channel_order_tab = ff_dca_channel_reorder_lfe[s->amode];
774e9acf 1298 } else
3a651f59 1299 s->channel_order_tab = ff_dca_channel_reorder_nolfe[s->amode];
774e9acf 1300 }
92765276 1301
e245d4f4
LB
1302 if (channels < ff_dca_channels[s->amode])
1303 return AVERROR_INVALIDDATA;
1304
08634e7b
NB
1305 if (channels > !!s->lfe &&
1306 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
f44059d2 1307 return AVERROR_INVALIDDATA;
4e06acbd 1308
801c39e1 1309 if (num_core_channels + !!s->lfe > 2 &&
89de5157 1310 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
45ff7c93 1311 channels = 2;
58b42345 1312 s->output = s->audio_header.prim_channels == 2 ? s->amode : DCA_STEREO;
63e8d976 1313 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
44b17d79
TW
1314
1315 /* Stereo downmix coefficients
1316 *
1317 * The decoder can only downmix to 2-channel, so we need to ensure
1318 * embedded downmix coefficients are actually targeting 2-channel.
aaa44d02 1319 */
44b17d79
TW
1320 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1321 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
801c39e1 1322 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
6b7b8585
NM
1323 /* Range checked earlier */
1324 s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
1325 s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
44b17d79 1326 }
6bfdb2de 1327 s->output = s->core_downmix_amode;
44b17d79
TW
1328 } else {
1329 int am = s->amode & DCA_CHANNEL_MASK;
3a651f59 1330 if (am >= FF_ARRAY_ELEMS(ff_dca_default_coeffs)) {
44b17d79
TW
1331 av_log(s->avctx, AV_LOG_ERROR,
1332 "Invalid channel mode %d\n", am);
1333 return AVERROR_INVALIDDATA;
1334 }
801c39e1 1335 if (num_core_channels + !!s->lfe >
3a651f59 1336 FF_ARRAY_ELEMS(ff_dca_default_coeffs[0])) {
44b17d79 1337 avpriv_request_sample(s->avctx, "Downmixing %d channels",
58b42345 1338 s->audio_header.prim_channels + !!s->lfe);
44b17d79
TW
1339 return AVERROR_PATCHWELCOME;
1340 }
801c39e1 1341 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
3a651f59
DB
1342 s->downmix_coef[i][0] = ff_dca_default_coeffs[am][i][0];
1343 s->downmix_coef[i][1] = ff_dca_default_coeffs[am][i][1];
44b17d79
TW
1344 }
1345 }
6a85dfc8 1346 ff_dlog(s->avctx, "Stereo downmix coeffs:\n");
801c39e1 1347 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
6a85dfc8 1348 ff_dlog(s->avctx, "L, input channel %d = %f\n", i,
44b17d79 1349 s->downmix_coef[i][0]);
6a85dfc8 1350 ff_dlog(s->avctx, "R, input channel %d = %f\n", i,
44b17d79
TW
1351 s->downmix_coef[i][1]);
1352 }
6a85dfc8 1353 ff_dlog(s->avctx, "\n");
92765276
BL
1354 }
1355 } else {
41ed7ab4 1356 av_log(avctx, AV_LOG_ERROR, "Nonstandard configuration %d !\n", s->amode);
f44059d2 1357 return AVERROR_INVALIDDATA;
92765276 1358 }
77788598
AH
1359
1360 return 0;
1361}
1362
1363/**
1364 * Main frame decoding function
1365 * FIXME add arguments
1366 */
1367static int dca_decode_frame(AVCodecContext *avctx, void *data,
1368 int *got_frame_ptr, AVPacket *avpkt)
1369{
1370 AVFrame *frame = data;
1371 const uint8_t *buf = avpkt->data;
1372 int buf_size = avpkt->size;
1373
1374 int lfe_samples;
1375 int num_core_channels = 0;
1376 int i, ret;
1377 float **samples_flt;
1378 DCAContext *s = avctx->priv_data;
1379 int channels, full_channels;
1380 int upsample = 0;
1381
1382 s->exss_ext_mask = 0;
1383 s->xch_present = 0;
1384
1385 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1386 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1387 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1388 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1389 return AVERROR_INVALIDDATA;
1390 }
1391
1392 if ((ret = dca_parse_frame_header(s)) < 0) {
1393 // seems like the frame is corrupt, try with the next one
1394 return ret;
1395 }
1396 // set AVCodec values with parsed data
1397 avctx->sample_rate = s->sample_rate;
1398 avctx->bit_rate = s->bit_rate;
1399
1400 s->profile = FF_PROFILE_DTS;
1401
d0a3e89d 1402 for (i = 0; i < (s->sample_blocks / SAMPLES_PER_SUBBAND); i++) {
77788598
AH
1403 if ((ret = dca_decode_block(s, 0, i))) {
1404 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1405 return ret;
1406 }
1407 }
1408
1409 /* record number of core channels incase less than max channels are requested */
58b42345 1410 num_core_channels = s->audio_header.prim_channels;
77788598
AH
1411
1412 if (s->ext_coding)
1413 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
1414 else
1415 s->core_ext_mask = 0;
1416
1417 ret = scan_for_extensions(avctx);
1418
1419 avctx->profile = s->profile;
1420
58b42345 1421 full_channels = channels = s->audio_header.prim_channels + !!s->lfe;
77788598
AH
1422
1423 ret = set_channel_layout(avctx, channels, num_core_channels);
1424 if (ret < 0)
1425 return ret;
8ac0f676 1426 avctx->channels = channels;
4a24837e 1427
0eea2129 1428 /* get output buffer */
d0a3e89d 1429 frame->nb_samples = 256 * (s->sample_blocks / SAMPLES_PER_SUBBAND);
217e4ff4
NM
1430 if (s->exss_ext_mask & DCA_EXT_EXSS_XLL) {
1431 int xll_nb_samples = s->xll_segments * s->xll_smpl_in_seg;
1432 /* Check for invalid/unsupported conditions first */
1433 if (s->xll_residual_channels > channels) {
1434 av_log(s->avctx, AV_LOG_WARNING,
1435 "DCA: too many residual channels (%d, core channels %d). Disabling XLL\n",
1436 s->xll_residual_channels, channels);
1437 s->exss_ext_mask &= ~DCA_EXT_EXSS_XLL;
1438 } else if (xll_nb_samples != frame->nb_samples &&
1439 2 * frame->nb_samples != xll_nb_samples) {
1440 av_log(s->avctx, AV_LOG_WARNING,
1441 "DCA: unsupported upsampling (%d XLL samples, %d core samples). Disabling XLL\n",
1442 xll_nb_samples, frame->nb_samples);
1443 s->exss_ext_mask &= ~DCA_EXT_EXSS_XLL;
1444 } else {
1445 if (2 * frame->nb_samples == xll_nb_samples) {
1446 av_log(s->avctx, AV_LOG_INFO,
1447 "XLL: upsampling core channels by a factor of 2\n");
1448 upsample = 1;
1449
1450 frame->nb_samples = xll_nb_samples;
1451 // FIXME: Is it good enough to copy from the first channel set?
1452 avctx->sample_rate = s->xll_chsets[0].sampling_frequency;
1453 }
1454 /* If downmixing to stereo, don't decode additional channels.
1455 * FIXME: Using the xch_disable flag for this doesn't seem right. */
1456 if (!s->xch_disable)
1457 avctx->channels += s->xll_channels - s->xll_residual_channels;
1458 }
1459 }
1460
1461 /* FIXME: This is an ugly hack, to just revert to the default
1462 * layout if we have additional channels. Need to convert the XLL
1463 * channel masks to libav channel_layout mask. */
1464 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels)
1465 avctx->channel_layout = 0;
1466
759001c5 1467 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
0eea2129
JR
1468 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1469 return ret;
1470 }
45ff7c93 1471 samples_flt = (float **) frame->extended_data;
6baef06e 1472
61d5313d
JR
1473 /* allocate buffer for extra channels if downmixing */
1474 if (avctx->channels < full_channels) {
1475 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
182821cf 1476 frame->nb_samples,
61d5313d
JR
1477 avctx->sample_fmt, 0);
1478 if (ret < 0)
1479 return ret;
1480
1481 av_fast_malloc(&s->extra_channels_buffer,
1482 &s->extra_channels_buffer_size, ret);
1483 if (!s->extra_channels_buffer)
1484 return AVERROR(ENOMEM);
1485
45ff7c93 1486 ret = av_samples_fill_arrays((uint8_t **) s->extra_channels, NULL,
61d5313d
JR
1487 s->extra_channels_buffer,
1488 full_channels - channels,
182821cf 1489 frame->nb_samples, avctx->sample_fmt, 0);
61d5313d
JR
1490 if (ret < 0)
1491 return ret;
1492 }
1493
6baef06e 1494 /* filter to get final output */
d0a3e89d 1495 for (i = 0; i < (s->sample_blocks / SAMPLES_PER_SUBBAND); i++) {
64c312aa 1496 int ch;
217e4ff4 1497 unsigned block = upsample ? 512 : 256;
64c312aa 1498 for (ch = 0; ch < channels; ch++)
217e4ff4 1499 s->samples_chanptr[ch] = samples_flt[ch] + i * block;
61d5313d 1500 for (; ch < full_channels; ch++)
217e4ff4 1501 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * block;
64c312aa 1502
217e4ff4 1503 dca_filter_channels(s, i, upsample);
b12b16c5
NB
1504
1505 /* If this was marked as a DTS-ES stream we need to subtract back- */
1506 /* channel from SL & SR to remove matrixed back-channel signal */
f37b4efe 1507 if ((s->source_pcm_res & 1) && s->xch_present) {
64c312aa
JR
1508 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1509 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1510 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
cb5042d0
JR
1511 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1512 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
b12b16c5 1513 }
01ca9ac3
KS
1514 }
1515
6baef06e 1516 /* update lfe history */
d0a3e89d 1517 lfe_samples = 2 * s->lfe * (s->sample_blocks / SAMPLES_PER_SUBBAND);
f37b4efe 1518 for (i = 0; i < 2 * s->lfe * 4; i++)
6baef06e 1519 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
6baef06e 1520
217e4ff4
NM
1521 if (s->exss_ext_mask & DCA_EXT_EXSS_XLL) {
1522 ret = ff_dca_xll_decode_audio(s, frame);
1523 if (ret < 0)
1524 return ret;
1525 }
30d70e79
TW
1526 /* AVMatrixEncoding
1527 *
1528 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
1529 ret = ff_side_data_update_matrix_encoding(frame,
1530 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
1531 AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
1532 if (ret < 0)
1533 return ret;
1534
182821cf 1535 *got_frame_ptr = 1;
0eea2129 1536
01ca9ac3
KS
1537 return buf_size;
1538}
1539
01ca9ac3 1540/**
01ca9ac3
KS
1541 * DCA initialization
1542 *
1543 * @param avctx pointer to the AVCodecContext
1544 */
1545
f37b4efe 1546static av_cold int dca_decode_init(AVCodecContext *avctx)
01ca9ac3
KS
1547{
1548 DCAContext *s = avctx->priv_data;
1549
1550 s->avctx = avctx;
1551 dca_init_vlcs();
01ca9ac3 1552
7c6eb0a1 1553 avpriv_float_dsp_init(&s->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
7d485f16 1554 ff_mdct_init(&s->imdct, 6, 1, 1.0);
f462ed1f 1555 ff_synth_filter_init(&s->synth);
309d16a4 1556 ff_dcadsp_init(&s->dcadsp);
c73d99e6 1557 ff_fmt_convert_init(&s->fmt_conv, avctx);
cfec09e9 1558
64c312aa 1559 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1402ee72 1560
b5ec6383 1561 /* allow downmixing to stereo */
89de5157
TW
1562 if (avctx->channels > 2 &&
1563 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
1564 avctx->channels = 2;
1402ee72 1565
01ca9ac3
KS
1566 return 0;
1567}
1568
f37b4efe 1569static av_cold int dca_decode_end(AVCodecContext *avctx)
89df5e95
MN
1570{
1571 DCAContext *s = avctx->priv_data;
1572 ff_mdct_end(&s->imdct);
61d5313d 1573 av_freep(&s->extra_channels_buffer);
217e4ff4
NM
1574 av_freep(&s->xll_sample_buf);
1575 av_freep(&s->qmf64_table);
89df5e95
MN
1576 return 0;
1577}
01ca9ac3 1578
3c8507a8 1579static const AVOption options[] = {
45ff7c93 1580 { "disable_xch", "disable decoding of the XCh extension", offsetof(DCAContext, xch_disable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
217e4ff4 1581 { "disable_xll", "disable decoding of the XLL extension", offsetof(DCAContext, xll_disable), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
3c8507a8
TW
1582 { NULL },
1583};
1584
1585static const AVClass dca_decoder_class = {
1586 .class_name = "DCA decoder",
1587 .item_name = av_default_item_name,
1588 .option = options,
1589 .version = LIBAVUTIL_VERSION_INT,
1590};
1591
d36beb3f 1592AVCodec ff_dca_decoder = {
f37b4efe 1593 .name = "dca",
b2bed932 1594 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
f37b4efe 1595 .type = AVMEDIA_TYPE_AUDIO,
36ef5369 1596 .id = AV_CODEC_ID_DTS,
f37b4efe
SG
1597 .priv_data_size = sizeof(DCAContext),
1598 .init = dca_decode_init,
1599 .decode = dca_decode_frame,
1600 .close = dca_decode_end,
def97856 1601 .capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
64c312aa 1602 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
f37b4efe 1603 AV_SAMPLE_FMT_NONE },
2c681139 1604 .profiles = NULL_IF_CONFIG_SMALL(ff_dca_profiles),
3c8507a8 1605 .priv_class = &dca_decoder_class,
01ca9ac3 1606};