Statistics
| Branch: | Revision:

ffmpeg / libavcodec / dca.c @ 80ba1ddb

History | View | Annotate | Download (68.6 KB)

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