Statistics
| Branch: | Revision:

ffmpeg / libavcodec / dca.c @ f2401c21

History | View | Annotate | Download (55 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 843c7aa8 Måns Rullgård
#include "libavutil/intmath.h"
30 0dc7df28 Måns Rullgård
#include "libavutil/intreadwrite.h"
31 01ca9ac3 Kostya Shishkov
#include "avcodec.h"
32
#include "dsputil.h"
33 1429224b Måns Rullgård
#include "fft.h"
34 9106a698 Stefano Sabatini
#include "get_bits.h"
35 b2755007 Stefano Sabatini
#include "put_bits.h"
36 01ca9ac3 Kostya Shishkov
#include "dcadata.h"
37
#include "dcahuff.h"
38 98c98e04 Diego Biurrun
#include "dca.h"
39 4f99c31c Michael Niedermayer
#include "synth_filter.h"
40 309d16a4 Måns Rullgård
#include "dcadsp.h"
41 01ca9ac3 Kostya Shishkov
42
//#define TRACE
43
44 774e9acf Nick Brereton
#define DCA_PRIM_CHANNELS_MAX (7)
45 01ca9ac3 Kostya Shishkov
#define DCA_SUBBANDS (32)
46
#define DCA_ABITS_MAX (32)      /* Should be 28 */
47 63c3b716 Nick Brereton
#define DCA_SUBSUBFRAMES_MAX (4)
48 774e9acf Nick Brereton
#define DCA_SUBFRAMES_MAX (16)
49 77b4b7c3 Nick Brereton
#define DCA_BLOCKS_MAX (16)
50 01ca9ac3 Kostya Shishkov
#define DCA_LFE_MAX (3)
51
52
enum DCAMode {
53
    DCA_MONO = 0,
54
    DCA_CHANNEL,
55
    DCA_STEREO,
56
    DCA_STEREO_SUMDIFF,
57
    DCA_STEREO_TOTAL,
58
    DCA_3F,
59
    DCA_2F1R,
60
    DCA_3F1R,
61
    DCA_2F2R,
62
    DCA_3F2R,
63
    DCA_4F2R
64
};
65
66 87c3b9bc Benjamin Larsson
/* Tables for mapping dts channel configurations to libavcodec multichannel api.
67
 * Some compromises have been made for special configurations. Most configurations
68
 * are never used so complete accuracy is not needed.
69
 *
70
 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
71 e22192ec Benjamin Larsson
 * S  -> side, when both rear and back are configured move one of them to the side channel
72 87c3b9bc Benjamin Larsson
 * OV -> center back
73 9d9b9d32 Andreas Öman
 * All 2 channel configurations -> CH_LAYOUT_STEREO
74 87c3b9bc Benjamin Larsson
 */
75
76
static const int64_t dca_core_channel_layout[] = {
77 9d9b9d32 Andreas Öman
    CH_FRONT_CENTER,                                               ///< 1, A
78
    CH_LAYOUT_STEREO,                                              ///< 2, A + B (dual mono)
79
    CH_LAYOUT_STEREO,                                              ///< 2, L + R (stereo)
80
    CH_LAYOUT_STEREO,                                              ///< 2, (L+R) + (L-R) (sum-difference)
81
    CH_LAYOUT_STEREO,                                              ///< 2, LT +RT (left and right total)
82 c6eaba62 Benjamin Larsson
    CH_LAYOUT_STEREO|CH_FRONT_CENTER,                              ///< 3, C+L+R
83
    CH_LAYOUT_STEREO|CH_BACK_CENTER,                               ///< 3, L+R+S
84
    CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_BACK_CENTER,               ///< 4, C + L + R+ S
85
    CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT,                   ///< 4, L + R +SL+ SR
86
    CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_SIDE_LEFT|CH_SIDE_RIGHT,   ///< 5, C + L + R+ SL+SR
87
    CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT|CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER,                 ///< 6, CL + CR + L + R + SL + SR
88
    CH_LAYOUT_STEREO|CH_BACK_LEFT|CH_BACK_RIGHT|CH_FRONT_CENTER|CH_BACK_CENTER,                                   ///< 6, C + L + R+ LR + RR + OV
89
    CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_FRONT_LEFT_OF_CENTER|CH_BACK_CENTER|CH_BACK_LEFT|CH_BACK_RIGHT,   ///< 6, CF+ CR+LF+ RF+LR + RR
90
    CH_FRONT_LEFT_OF_CENTER|CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
91 9d9b9d32 Andreas Öman
    CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT|CH_BACK_LEFT|CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2+ SR1 + SR2
92 c6eaba62 Benjamin Larsson
    CH_FRONT_LEFT_OF_CENTER|CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_BACK_CENTER|CH_SIDE_RIGHT, ///< 8, CL + C+ CR + L + R + SL + S+ SR
93 87c3b9bc Benjamin Larsson
};
94
95 92765276 Benjamin Larsson
static const int8_t dca_lfe_index[] = {
96
    1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
97
};
98
99 3d5a9ba1 Nick Brereton
static const int8_t dca_channel_reorder_lfe[][9] = {
100
    { 0, -1, -1, -1, -1, -1, -1, -1, -1},
101
    { 0,  1, -1, -1, -1, -1, -1, -1, -1},
102
    { 0,  1, -1, -1, -1, -1, -1, -1, -1},
103
    { 0,  1, -1, -1, -1, -1, -1, -1, -1},
104
    { 0,  1, -1, -1, -1, -1, -1, -1, -1},
105
    { 2,  0,  1, -1, -1, -1, -1, -1, -1},
106
    { 0,  1,  3, -1, -1, -1, -1, -1, -1},
107
    { 2,  0,  1,  4, -1, -1, -1, -1, -1},
108
    { 0,  1,  3,  4, -1, -1, -1, -1, -1},
109
    { 2,  0,  1,  4,  5, -1, -1, -1, -1},
110
    { 3,  4,  0,  1,  5,  6, -1, -1, -1},
111
    { 2,  0,  1,  4,  5,  6, -1, -1, -1},
112
    { 0,  6,  4,  5,  2,  3, -1, -1, -1},
113
    { 4,  2,  5,  0,  1,  6,  7, -1, -1},
114
    { 5,  6,  0,  1,  7,  3,  8,  4, -1},
115
    { 4,  2,  5,  0,  1,  6,  8,  7, -1},
116 92765276 Benjamin Larsson
};
117
118 3d5a9ba1 Nick Brereton
static const int8_t dca_channel_reorder_lfe_xch[][9] = {
119
    { 0,  2, -1, -1, -1, -1, -1, -1, -1},
120
    { 0,  1,  3, -1, -1, -1, -1, -1, -1},
121
    { 0,  1,  3, -1, -1, -1, -1, -1, -1},
122
    { 0,  1,  3, -1, -1, -1, -1, -1, -1},
123
    { 0,  1,  3, -1, -1, -1, -1, -1, -1},
124
    { 2,  0,  1,  4, -1, -1, -1, -1, -1},
125
    { 0,  1,  3,  4, -1, -1, -1, -1, -1},
126
    { 2,  0,  1,  4,  5, -1, -1, -1, -1},
127
    { 0,  1,  4,  5,  3, -1, -1, -1, -1},
128
    { 2,  0,  1,  5,  6,  4, -1, -1, -1},
129
    { 3,  4,  0,  1,  6,  7,  5, -1, -1},
130
    { 2,  0,  1,  4,  5,  6,  7, -1, -1},
131
    { 0,  6,  4,  5,  2,  3,  7, -1, -1},
132
    { 4,  2,  5,  0,  1,  7,  8,  6, -1},
133
    { 5,  6,  0,  1,  8,  3,  9,  4,  7},
134
    { 4,  2,  5,  0,  1,  6,  9,  8,  7},
135 92765276 Benjamin Larsson
};
136
137 3d5a9ba1 Nick Brereton
static const int8_t dca_channel_reorder_nolfe[][9] = {
138
    { 0, -1, -1, -1, -1, -1, -1, -1, -1},
139
    { 0,  1, -1, -1, -1, -1, -1, -1, -1},
140
    { 0,  1, -1, -1, -1, -1, -1, -1, -1},
141
    { 0,  1, -1, -1, -1, -1, -1, -1, -1},
142
    { 0,  1, -1, -1, -1, -1, -1, -1, -1},
143
    { 2,  0,  1, -1, -1, -1, -1, -1, -1},
144
    { 0,  1,  2, -1, -1, -1, -1, -1, -1},
145
    { 2,  0,  1,  3, -1, -1, -1, -1, -1},
146
    { 0,  1,  2,  3, -1, -1, -1, -1, -1},
147
    { 2,  0,  1,  3,  4, -1, -1, -1, -1},
148
    { 2,  3,  0,  1,  4,  5, -1, -1, -1},
149
    { 2,  0,  1,  3,  4,  5, -1, -1, -1},
150
    { 0,  5,  3,  4,  1,  2, -1, -1, -1},
151
    { 3,  2,  4,  0,  1,  5,  6, -1, -1},
152
    { 4,  5,  0,  1,  6,  2,  7,  3, -1},
153
    { 3,  2,  4,  0,  1,  5,  7,  6, -1},
154
};
155
156
static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
157
    { 0,  1, -1, -1, -1, -1, -1, -1, -1},
158
    { 0,  1,  2, -1, -1, -1, -1, -1, -1},
159
    { 0,  1,  2, -1, -1, -1, -1, -1, -1},
160
    { 0,  1,  2, -1, -1, -1, -1, -1, -1},
161
    { 0,  1,  2, -1, -1, -1, -1, -1, -1},
162
    { 2,  0,  1,  3, -1, -1, -1, -1, -1},
163
    { 0,  1,  2,  3, -1, -1, -1, -1, -1},
164
    { 2,  0,  1,  3,  4, -1, -1, -1, -1},
165
    { 0,  1,  3,  4,  2, -1, -1, -1, -1},
166
    { 2,  0,  1,  4,  5,  3, -1, -1, -1},
167
    { 2,  3,  0,  1,  5,  6,  4, -1, -1},
168
    { 2,  0,  1,  3,  4,  5,  6, -1, -1},
169
    { 0,  5,  3,  4,  1,  2,  6, -1, -1},
170
    { 3,  2,  4,  0,  1,  6,  7,  5, -1},
171
    { 4,  5,  0,  1,  7,  2,  8,  3,  6},
172
    { 3,  2,  4,  0,  1,  5,  8,  7,  6},
173
};
174 87c3b9bc Benjamin Larsson
175 01ca9ac3 Kostya Shishkov
#define DCA_DOLBY 101           /* FIXME */
176
177
#define DCA_CHANNEL_BITS 6
178
#define DCA_CHANNEL_MASK 0x3F
179
180
#define DCA_LFE 0x80
181
182
#define HEADER_SIZE 14
183
184 4df7beb3 Alexander E. Patrakov
#define DCA_MAX_FRAME_SIZE 16384
185 01ca9ac3 Kostya Shishkov
186
/** Bit allocation */
187
typedef struct {
188
    int offset;                 ///< code values offset
189
    int maxbits[8];             ///< max bits in VLC
190
    int wrap;                   ///< wrap for get_vlc2()
191
    VLC vlc[8];                 ///< actual codes
192
} BitAlloc;
193
194
static BitAlloc dca_bitalloc_index;    ///< indexes for samples VLC select
195
static BitAlloc dca_tmode;             ///< transition mode VLCs
196
static BitAlloc dca_scalefactor;       ///< scalefactor VLCs
197
static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
198
199 5a6a6cc7 Diego Biurrun
static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
200 01ca9ac3 Kostya Shishkov
{
201
    return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
202
}
203
204
typedef struct {
205
    AVCodecContext *avctx;
206
    /* Frame header */
207
    int frame_type;             ///< type of the current frame
208
    int samples_deficit;        ///< deficit sample count
209
    int crc_present;            ///< crc is present in the bitstream
210
    int sample_blocks;          ///< number of PCM sample blocks
211
    int frame_size;             ///< primary frame byte size
212
    int amode;                  ///< audio channels arrangement
213
    int sample_rate;            ///< audio sampling rate
214
    int bit_rate;               ///< transmission bit rate
215 9ed73b48 Benjamin Larsson
    int bit_rate_index;         ///< transmission bit rate index
216 01ca9ac3 Kostya Shishkov
217
    int downmix;                ///< embedded downmix enabled
218
    int dynrange;               ///< embedded dynamic range flag
219
    int timestamp;              ///< embedded time stamp flag
220
    int aux_data;               ///< auxiliary data flag
221
    int hdcd;                   ///< source material is mastered in HDCD
222
    int ext_descr;              ///< extension audio descriptor flag
223
    int ext_coding;             ///< extended coding flag
224
    int aspf;                   ///< audio sync word insertion flag
225
    int lfe;                    ///< low frequency effects flag
226
    int predictor_history;      ///< predictor history flag
227
    int header_crc;             ///< header crc check bytes
228
    int multirate_inter;        ///< multirate interpolator switch
229
    int version;                ///< encoder software revision
230
    int copy_history;           ///< copy history
231
    int source_pcm_res;         ///< source pcm resolution
232
    int front_sum;              ///< front sum/difference flag
233
    int surround_sum;           ///< surround sum/difference flag
234
    int dialog_norm;            ///< dialog normalisation parameter
235
236
    /* Primary audio coding header */
237
    int subframes;              ///< number of subframes
238 ebf71dbd Benjamin Larsson
    int total_channels;         ///< number of channels including extensions
239 01ca9ac3 Kostya Shishkov
    int prim_channels;          ///< number of primary audio channels
240
    int subband_activity[DCA_PRIM_CHANNELS_MAX];    ///< subband activity count
241
    int vq_start_subband[DCA_PRIM_CHANNELS_MAX];    ///< high frequency vq start subband
242
    int joint_intensity[DCA_PRIM_CHANNELS_MAX];     ///< joint intensity coding index
243
    int transient_huffman[DCA_PRIM_CHANNELS_MAX];   ///< transient mode code book
244
    int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
245
    int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];    ///< bit allocation quantizer select
246
    int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
247
    float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];   ///< scale factor adjustment
248
249
    /* Primary audio coding side information */
250 774e9acf Nick Brereton
    int subsubframes[DCA_SUBFRAMES_MAX];           ///< number of subsubframes
251
    int partial_samples[DCA_SUBFRAMES_MAX];        ///< partial subsubframe samples count
252 01ca9ac3 Kostya Shishkov
    int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< prediction mode (ADPCM used or not)
253
    int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];      ///< prediction VQ coefs
254
    int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];           ///< bit allocation index
255
    int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< transition mode (transients)
256
    int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];    ///< scale factors (2 if transient)
257
    int joint_huff[DCA_PRIM_CHANNELS_MAX];                       ///< joint subband scale factors codebook
258
    int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
259
    int downmix_coef[DCA_PRIM_CHANNELS_MAX][2];                  ///< stereo downmix coefficients
260
    int dynrange_coef;                                           ///< dynamic range coefficient
261
262
    int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];       ///< VQ encoded high frequency subbands
263
264 6baef06e Nick Brereton
    float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)];      ///< Low frequency effect data
265 01ca9ac3 Kostya Shishkov
    int lfe_scale_factor;
266
267
    /* Subband samples history (for ADPCM) */
268
    float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
269 84dc2d8a Måns Rullgård
    DECLARE_ALIGNED(16, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
270 38d52f3e Måns Rullgård
    DECLARE_ALIGNED(16, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
271 47f0e052 Michael Niedermayer
    int hist_index[DCA_PRIM_CHANNELS_MAX];
272 84dc2d8a Måns Rullgård
    DECLARE_ALIGNED(16, float, raXin)[32];
273 01ca9ac3 Kostya Shishkov
274
    int output;                 ///< type of output
275 1402ee72 Benjamin Larsson
    float add_bias;             ///< output bias
276
    float scale_bias;           ///< output scale
277 01ca9ac3 Kostya Shishkov
278 77b4b7c3 Nick Brereton
    DECLARE_ALIGNED(16, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
279 774e9acf Nick Brereton
    DECLARE_ALIGNED(16, float, samples)[(DCA_PRIM_CHANNELS_MAX+1)*256];
280
    const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX+1];
281 01ca9ac3 Kostya Shishkov
282
    uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
283
    int dca_buffer_size;        ///< how much data is in the dca_buffer
284
285 92765276 Benjamin Larsson
    const int8_t* channel_order_tab;                             ///< channel reordering table, lfe and non lfe
286 01ca9ac3 Kostya Shishkov
    GetBitContext gb;
287
    /* Current position in DCA frame */
288
    int current_subframe;
289
    int current_subsubframe;
290
291 d0a18850 Nick Brereton
    /* XCh extension information */
292
    int xch_present;
293
    int xch_base_channel;       ///< index of first (only) channel containing XCH data
294
295 01ca9ac3 Kostya Shishkov
    int debug_flag;             ///< used for suppressing repeated error messages output
296
    DSPContext dsp;
297 01b22147 Måns Rullgård
    FFTContext imdct;
298 f462ed1f Måns Rullgård
    SynthFilterContext synth;
299 309d16a4 Måns Rullgård
    DCADSPContext dcadsp;
300 01ca9ac3 Kostya Shishkov
} DCAContext;
301
302 0cfa85dd Kostya Shishkov
static const uint16_t dca_vlc_offs[] = {
303
        0,   512,   640,   768,  1282,  1794,  2436,  3080,  3770,  4454,  5364,
304
     5372,  5380,  5388,  5392,  5396,  5412,  5420,  5428,  5460,  5492,  5508,
305
     5572,  5604,  5668,  5796,  5860,  5892,  6412,  6668,  6796,  7308,  7564,
306
     7820,  8076,  8620,  9132,  9388,  9910, 10166, 10680, 11196, 11726, 12240,
307
    12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
308
    18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
309
};
310
311 98a6fff9 Zuxy Meng
static av_cold void dca_init_vlcs(void)
312 01ca9ac3 Kostya Shishkov
{
313 5e534865 Diego Biurrun
    static int vlcs_initialized = 0;
314 0cfa85dd Kostya Shishkov
    int i, j, c = 14;
315
    static VLC_TYPE dca_table[23622][2];
316 01ca9ac3 Kostya Shishkov
317 5e534865 Diego Biurrun
    if (vlcs_initialized)
318 01ca9ac3 Kostya Shishkov
        return;
319
320
    dca_bitalloc_index.offset = 1;
321 32dd6a9c Sergey Vlasov
    dca_bitalloc_index.wrap = 2;
322 0cfa85dd Kostya Shishkov
    for (i = 0; i < 5; i++) {
323
        dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
324
        dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
325 01ca9ac3 Kostya Shishkov
        init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
326
                 bitalloc_12_bits[i], 1, 1,
327 0cfa85dd Kostya Shishkov
                 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
328
    }
329 01ca9ac3 Kostya Shishkov
    dca_scalefactor.offset = -64;
330
    dca_scalefactor.wrap = 2;
331 0cfa85dd Kostya Shishkov
    for (i = 0; i < 5; i++) {
332
        dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
333
        dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
334 01ca9ac3 Kostya Shishkov
        init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
335
                 scales_bits[i], 1, 1,
336 0cfa85dd Kostya Shishkov
                 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
337
    }
338 01ca9ac3 Kostya Shishkov
    dca_tmode.offset = 0;
339
    dca_tmode.wrap = 1;
340 0cfa85dd Kostya Shishkov
    for (i = 0; i < 4; i++) {
341
        dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
342
        dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
343 01ca9ac3 Kostya Shishkov
        init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
344
                 tmode_bits[i], 1, 1,
345 0cfa85dd Kostya Shishkov
                 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
346
    }
347 01ca9ac3 Kostya Shishkov
348 d1177cb5 Nick Brereton
    for (i = 0; i < 10; i++)
349
        for (j = 0; j < 7; j++){
350
            if (!bitalloc_codes[i][j]) break;
351 01ca9ac3 Kostya Shishkov
            dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
352
            dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
353 0cfa85dd Kostya Shishkov
            dca_smpl_bitalloc[i+1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
354
            dca_smpl_bitalloc[i+1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
355 01ca9ac3 Kostya Shishkov
            init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
356
                     bitalloc_sizes[i],
357
                     bitalloc_bits[i][j], 1, 1,
358 0cfa85dd Kostya Shishkov
                     bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
359
            c++;
360 01ca9ac3 Kostya Shishkov
        }
361 5e534865 Diego Biurrun
    vlcs_initialized = 1;
362 01ca9ac3 Kostya Shishkov
}
363
364
static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
365
{
366
    while(len--)
367
        *dst++ = get_bits(gb, bits);
368
}
369
370 774e9acf Nick Brereton
static int dca_parse_audio_coding_header(DCAContext * s, int base_channel)
371 01ca9ac3 Kostya Shishkov
{
372
    int i, j;
373
    static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
374
    static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
375
    static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
376
377 774e9acf Nick Brereton
    s->total_channels    = get_bits(&s->gb, 3) + 1 + base_channel;
378 6baef06e Nick Brereton
    s->prim_channels     = s->total_channels;
379 774e9acf Nick Brereton
380 6baef06e Nick Brereton
    if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
381 774e9acf Nick Brereton
        s->prim_channels = DCA_PRIM_CHANNELS_MAX;
382 6baef06e Nick Brereton
383
384 774e9acf Nick Brereton
    for (i = base_channel; i < s->prim_channels; i++) {
385 6baef06e Nick Brereton
        s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
386
        if (s->subband_activity[i] > DCA_SUBBANDS)
387
            s->subband_activity[i] = DCA_SUBBANDS;
388
    }
389 774e9acf Nick Brereton
    for (i = base_channel; i < s->prim_channels; i++) {
390 6baef06e Nick Brereton
        s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
391
        if (s->vq_start_subband[i] > DCA_SUBBANDS)
392
            s->vq_start_subband[i] = DCA_SUBBANDS;
393
    }
394 774e9acf Nick Brereton
    get_array(&s->gb, s->joint_intensity + base_channel,     s->prim_channels - base_channel, 3);
395
    get_array(&s->gb, s->transient_huffman + base_channel,   s->prim_channels - base_channel, 2);
396
    get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
397
    get_array(&s->gb, s->bitalloc_huffman + base_channel,    s->prim_channels - base_channel, 3);
398 6baef06e Nick Brereton
399
    /* Get codebooks quantization indexes */
400 774e9acf Nick Brereton
    if (!base_channel)
401
        memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
402 6baef06e Nick Brereton
    for (j = 1; j < 11; j++)
403 774e9acf Nick Brereton
        for (i = base_channel; i < s->prim_channels; i++)
404 6baef06e Nick Brereton
            s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
405
406
    /* Get scale factor adjustment */
407
    for (j = 0; j < 11; j++)
408 774e9acf Nick Brereton
        for (i = base_channel; i < s->prim_channels; i++)
409 6baef06e Nick Brereton
            s->scalefactor_adj[i][j] = 1;
410
411
    for (j = 1; j < 11; j++)
412 774e9acf Nick Brereton
        for (i = base_channel; i < s->prim_channels; i++)
413 6baef06e Nick Brereton
            if (s->quant_index_huffman[i][j] < thr[j])
414
                s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
415
416
    if (s->crc_present) {
417
        /* Audio header CRC check */
418
        get_bits(&s->gb, 16);
419
    }
420
421
    s->current_subframe = 0;
422
    s->current_subsubframe = 0;
423
424
#ifdef TRACE
425
    av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
426
    av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
427 774e9acf Nick Brereton
    for (i = base_channel; i < s->prim_channels; i++){
428 6baef06e Nick Brereton
        av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
429
        av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
430
        av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
431
        av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
432
        av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
433
        av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
434
        av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
435
        for (j = 0; j < 11; j++)
436
            av_log(s->avctx, AV_LOG_DEBUG, " %i",
437
                   s->quant_index_huffman[i][j]);
438
        av_log(s->avctx, AV_LOG_DEBUG, "\n");
439
        av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
440
        for (j = 0; j < 11; j++)
441
            av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
442
        av_log(s->avctx, AV_LOG_DEBUG, "\n");
443
    }
444
#endif
445
446
  return 0;
447
}
448
449
static int dca_parse_frame_header(DCAContext * s)
450
{
451 01ca9ac3 Kostya Shishkov
    init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
452
453
    /* Sync code */
454
    get_bits(&s->gb, 32);
455
456
    /* Frame header */
457
    s->frame_type        = get_bits(&s->gb, 1);
458
    s->samples_deficit   = get_bits(&s->gb, 5) + 1;
459
    s->crc_present       = get_bits(&s->gb, 1);
460
    s->sample_blocks     = get_bits(&s->gb, 7) + 1;
461
    s->frame_size        = get_bits(&s->gb, 14) + 1;
462
    if (s->frame_size < 95)
463
        return -1;
464
    s->amode             = get_bits(&s->gb, 6);
465
    s->sample_rate       = dca_sample_rates[get_bits(&s->gb, 4)];
466
    if (!s->sample_rate)
467
        return -1;
468 d5b3a863 Benjamin Larsson
    s->bit_rate_index    = get_bits(&s->gb, 5);
469 9ed73b48 Benjamin Larsson
    s->bit_rate          = dca_bit_rates[s->bit_rate_index];
470 01ca9ac3 Kostya Shishkov
    if (!s->bit_rate)
471
        return -1;
472
473
    s->downmix           = get_bits(&s->gb, 1);
474
    s->dynrange          = get_bits(&s->gb, 1);
475
    s->timestamp         = get_bits(&s->gb, 1);
476
    s->aux_data          = get_bits(&s->gb, 1);
477
    s->hdcd              = get_bits(&s->gb, 1);
478
    s->ext_descr         = get_bits(&s->gb, 3);
479
    s->ext_coding        = get_bits(&s->gb, 1);
480
    s->aspf              = get_bits(&s->gb, 1);
481
    s->lfe               = get_bits(&s->gb, 2);
482
    s->predictor_history = get_bits(&s->gb, 1);
483
484
    /* TODO: check CRC */
485
    if (s->crc_present)
486
        s->header_crc    = get_bits(&s->gb, 16);
487
488
    s->multirate_inter   = get_bits(&s->gb, 1);
489
    s->version           = get_bits(&s->gb, 4);
490
    s->copy_history      = get_bits(&s->gb, 2);
491
    s->source_pcm_res    = get_bits(&s->gb, 3);
492
    s->front_sum         = get_bits(&s->gb, 1);
493
    s->surround_sum      = get_bits(&s->gb, 1);
494
    s->dialog_norm       = get_bits(&s->gb, 4);
495
496
    /* FIXME: channels mixing levels */
497 cc826626 Justin Ruggles
    s->output = s->amode;
498 d1177cb5 Nick Brereton
    if (s->lfe) s->output |= DCA_LFE;
499 01ca9ac3 Kostya Shishkov
500
#ifdef TRACE
501
    av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
502
    av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
503
    av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
504
    av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
505
           s->sample_blocks, s->sample_blocks * 32);
506
    av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
507
    av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
508
           s->amode, dca_channels[s->amode]);
509 49c91c34 Benjamin Larsson
    av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
510
           s->sample_rate);
511
    av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
512
           s->bit_rate);
513 01ca9ac3 Kostya Shishkov
    av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
514
    av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
515
    av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
516
    av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
517
    av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
518
    av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
519
    av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
520
    av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
521
    av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
522
    av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
523
           s->predictor_history);
524
    av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
525
    av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
526
           s->multirate_inter);
527
    av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
528
    av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
529
    av_log(s->avctx, AV_LOG_DEBUG,
530
           "source pcm resolution: %i (%i bits/sample)\n",
531
           s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
532
    av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
533
    av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
534
    av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
535
    av_log(s->avctx, AV_LOG_DEBUG, "\n");
536
#endif
537
538
    /* Primary audio coding header */
539
    s->subframes         = get_bits(&s->gb, 4) + 1;
540
541 774e9acf Nick Brereton
    return dca_parse_audio_coding_header(s, 0);
542 01ca9ac3 Kostya Shishkov
}
543
544
545 c5d13492 Kostya Shishkov
static inline int get_scale(GetBitContext *gb, int level, int value)
546 01ca9ac3 Kostya Shishkov
{
547
   if (level < 5) {
548
       /* huffman encoded */
549 c5d13492 Kostya Shishkov
       value += get_bitalloc(gb, &dca_scalefactor, level);
550 d1177cb5 Nick Brereton
   } else if (level < 8)
551 01ca9ac3 Kostya Shishkov
       value = get_bits(gb, level + 1);
552
   return value;
553
}
554
555 774e9acf Nick Brereton
static int dca_subframe_header(DCAContext * s, int base_channel, int block_index)
556 01ca9ac3 Kostya Shishkov
{
557
    /* Primary audio coding side information */
558
    int j, k;
559
560 774e9acf Nick Brereton
    if (!base_channel) {
561
        s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
562
        s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
563
    }
564
565
    for (j = base_channel; j < s->prim_channels; j++) {
566 01ca9ac3 Kostya Shishkov
        for (k = 0; k < s->subband_activity[j]; k++)
567
            s->prediction_mode[j][k] = get_bits(&s->gb, 1);
568
    }
569
570
    /* Get prediction codebook */
571 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
572 01ca9ac3 Kostya Shishkov
        for (k = 0; k < s->subband_activity[j]; k++) {
573
            if (s->prediction_mode[j][k] > 0) {
574
                /* (Prediction coefficient VQ address) */
575
                s->prediction_vq[j][k] = get_bits(&s->gb, 12);
576
            }
577
        }
578
    }
579
580
    /* Bit allocation index */
581 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
582 01ca9ac3 Kostya Shishkov
        for (k = 0; k < s->vq_start_subband[j]; k++) {
583
            if (s->bitalloc_huffman[j] == 6)
584
                s->bitalloc[j][k] = get_bits(&s->gb, 5);
585
            else if (s->bitalloc_huffman[j] == 5)
586
                s->bitalloc[j][k] = get_bits(&s->gb, 4);
587 ebf71dbd Benjamin Larsson
            else if (s->bitalloc_huffman[j] == 7) {
588
                av_log(s->avctx, AV_LOG_ERROR,
589
                       "Invalid bit allocation index\n");
590
                return -1;
591
            } else {
592 01ca9ac3 Kostya Shishkov
                s->bitalloc[j][k] =
593 c5d13492 Kostya Shishkov
                    get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
594 01ca9ac3 Kostya Shishkov
            }
595
596
            if (s->bitalloc[j][k] > 26) {
597
//                 av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
598
//                          j, k, s->bitalloc[j][k]);
599
                return -1;
600
            }
601
        }
602
    }
603
604
    /* Transition mode */
605 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
606 01ca9ac3 Kostya Shishkov
        for (k = 0; k < s->subband_activity[j]; k++) {
607
            s->transition_mode[j][k] = 0;
608 774e9acf Nick Brereton
            if (s->subsubframes[s->current_subframe] > 1 &&
609 01ca9ac3 Kostya Shishkov
                k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
610
                s->transition_mode[j][k] =
611
                    get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
612
            }
613
        }
614
    }
615
616 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
617 a9f87158 Michael Niedermayer
        const uint32_t *scale_table;
618 01ca9ac3 Kostya Shishkov
        int scale_sum;
619
620
        memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
621
622
        if (s->scalefactor_huffman[j] == 6)
623 a9f87158 Michael Niedermayer
            scale_table = scale_factor_quant7;
624 01ca9ac3 Kostya Shishkov
        else
625 a9f87158 Michael Niedermayer
            scale_table = scale_factor_quant6;
626 01ca9ac3 Kostya Shishkov
627
        /* When huffman coded, only the difference is encoded */
628
        scale_sum = 0;
629
630
        for (k = 0; k < s->subband_activity[j]; k++) {
631
            if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
632 c5d13492 Kostya Shishkov
                scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
633 01ca9ac3 Kostya Shishkov
                s->scale_factor[j][k][0] = scale_table[scale_sum];
634
            }
635
636
            if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
637
                /* Get second scale factor */
638 c5d13492 Kostya Shishkov
                scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
639 01ca9ac3 Kostya Shishkov
                s->scale_factor[j][k][1] = scale_table[scale_sum];
640
            }
641
        }
642
    }
643
644
    /* Joint subband scale factor codebook select */
645 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
646 01ca9ac3 Kostya Shishkov
        /* Transmitted only if joint subband coding enabled */
647
        if (s->joint_intensity[j] > 0)
648
            s->joint_huff[j] = get_bits(&s->gb, 3);
649
    }
650
651
    /* Scale factors for joint subband coding */
652 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
653 01ca9ac3 Kostya Shishkov
        int source_channel;
654
655
        /* Transmitted only if joint subband coding enabled */
656
        if (s->joint_intensity[j] > 0) {
657
            int scale = 0;
658
            source_channel = s->joint_intensity[j] - 1;
659
660
            /* When huffman coded, only the difference is encoded
661
             * (is this valid as well for joint scales ???) */
662
663
            for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
664 c5d13492 Kostya Shishkov
                scale = get_scale(&s->gb, s->joint_huff[j], 0);
665 01ca9ac3 Kostya Shishkov
                scale += 64;    /* bias */
666
                s->joint_scale_factor[j][k] = scale;    /*joint_scale_table[scale]; */
667
            }
668
669 268fcbe2 Reimar Döffinger
            if (!(s->debug_flag & 0x02)) {
670 01ca9ac3 Kostya Shishkov
                av_log(s->avctx, AV_LOG_DEBUG,
671
                       "Joint stereo coding not supported\n");
672
                s->debug_flag |= 0x02;
673
            }
674
        }
675
    }
676
677
    /* Stereo downmix coefficients */
678 774e9acf Nick Brereton
    if (!base_channel && s->prim_channels > 2) {
679 d1177cb5 Nick Brereton
        if (s->downmix) {
680 774e9acf Nick Brereton
            for (j = base_channel; j < s->prim_channels; j++) {
681 6369e6eb Justin Ruggles
                s->downmix_coef[j][0] = get_bits(&s->gb, 7);
682
                s->downmix_coef[j][1] = get_bits(&s->gb, 7);
683
            }
684 c31a76e4 Justin Ruggles
        } else {
685
            int am = s->amode & DCA_CHANNEL_MASK;
686 774e9acf Nick Brereton
            for (j = base_channel; j < s->prim_channels; j++) {
687 c31a76e4 Justin Ruggles
                s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
688
                s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
689
            }
690
        }
691 01ca9ac3 Kostya Shishkov
    }
692
693
    /* Dynamic range coefficient */
694
    if (s->dynrange)
695
        s->dynrange_coef = get_bits(&s->gb, 8);
696
697
    /* Side information CRC check word */
698
    if (s->crc_present) {
699
        get_bits(&s->gb, 16);
700
    }
701
702
    /*
703
     * Primary audio data arrays
704
     */
705
706
    /* VQ encoded high frequency subbands */
707 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++)
708 01ca9ac3 Kostya Shishkov
        for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
709
            /* 1 vector -> 32 samples */
710
            s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
711
712
    /* Low frequency effect data */
713 774e9acf Nick Brereton
    if (!base_channel && s->lfe) {
714 01ca9ac3 Kostya Shishkov
        /* LFE samples */
715 6baef06e Nick Brereton
        int lfe_samples = 2 * s->lfe * (4 + block_index);
716 774e9acf Nick Brereton
        int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
717 01ca9ac3 Kostya Shishkov
        float lfe_scale;
718
719 6baef06e Nick Brereton
        for (j = lfe_samples; j < lfe_end_sample; j++) {
720 01ca9ac3 Kostya Shishkov
            /* Signed 8 bits int */
721
            s->lfe_data[j] = get_sbits(&s->gb, 8);
722
        }
723
724
        /* Scale factor index */
725
        s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
726
727
        /* Quantization step size * scale factor */
728
        lfe_scale = 0.035 * s->lfe_scale_factor;
729
730 6baef06e Nick Brereton
        for (j = lfe_samples; j < lfe_end_sample; j++)
731 01ca9ac3 Kostya Shishkov
            s->lfe_data[j] *= lfe_scale;
732
    }
733
734
#ifdef TRACE
735 774e9acf Nick Brereton
    av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes[s->current_subframe]);
736 01ca9ac3 Kostya Shishkov
    av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
737 774e9acf Nick Brereton
           s->partial_samples[s->current_subframe]);
738
    for (j = base_channel; j < s->prim_channels; j++) {
739 01ca9ac3 Kostya Shishkov
        av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
740
        for (k = 0; k < s->subband_activity[j]; k++)
741
            av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
742
        av_log(s->avctx, AV_LOG_DEBUG, "\n");
743
    }
744 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
745 01ca9ac3 Kostya Shishkov
        for (k = 0; k < s->subband_activity[j]; k++)
746
                av_log(s->avctx, AV_LOG_DEBUG,
747
                       "prediction coefs: %f, %f, %f, %f\n",
748
                       (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
749
                       (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
750
                       (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
751
                       (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
752
    }
753 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
754 01ca9ac3 Kostya Shishkov
        av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
755
        for (k = 0; k < s->vq_start_subband[j]; k++)
756
            av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
757
        av_log(s->avctx, AV_LOG_DEBUG, "\n");
758
    }
759 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
760 01ca9ac3 Kostya Shishkov
        av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
761
        for (k = 0; k < s->subband_activity[j]; k++)
762
            av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
763
        av_log(s->avctx, AV_LOG_DEBUG, "\n");
764
    }
765 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
766 01ca9ac3 Kostya Shishkov
        av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
767
        for (k = 0; k < s->subband_activity[j]; k++) {
768
            if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
769
                av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
770
            if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
771
                av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
772
        }
773
        av_log(s->avctx, AV_LOG_DEBUG, "\n");
774
    }
775 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++) {
776 01ca9ac3 Kostya Shishkov
        if (s->joint_intensity[j] > 0) {
777 56e4603e Sergey Vlasov
            int source_channel = s->joint_intensity[j] - 1;
778 01ca9ac3 Kostya Shishkov
            av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
779
            for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
780
                av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
781
            av_log(s->avctx, AV_LOG_DEBUG, "\n");
782
        }
783
    }
784 774e9acf Nick Brereton
    if (!base_channel && s->prim_channels > 2 && s->downmix) {
785 01ca9ac3 Kostya Shishkov
        av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
786
        for (j = 0; j < s->prim_channels; j++) {
787
            av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
788
            av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
789
        }
790
        av_log(s->avctx, AV_LOG_DEBUG, "\n");
791
    }
792 774e9acf Nick Brereton
    for (j = base_channel; j < s->prim_channels; j++)
793 01ca9ac3 Kostya Shishkov
        for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
794
            av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
795 774e9acf Nick Brereton
    if (!base_channel && s->lfe) {
796 6baef06e Nick Brereton
        int lfe_samples = 2 * s->lfe * (4 + block_index);
797
        int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
798
799 01ca9ac3 Kostya Shishkov
        av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
800 6baef06e Nick Brereton
        for (j = lfe_samples; j < lfe_end_sample; j++)
801 01ca9ac3 Kostya Shishkov
            av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
802
        av_log(s->avctx, AV_LOG_DEBUG, "\n");
803
    }
804
#endif
805
806
    return 0;
807
}
808
809
static void qmf_32_subbands(DCAContext * s, int chans,
810
                            float samples_in[32][8], float *samples_out,
811
                            float scale, float bias)
812
{
813 b6398969 Reimar Döffinger
    const float *prCoeff;
814 d49dded5 Michael Niedermayer
    int i;
815 01ca9ac3 Kostya Shishkov
816 b92d483b Måns Rullgård
    int sb_act = s->subband_activity[chans];
817 89df5e95 Michael Niedermayer
    int subindex;
818 01ca9ac3 Kostya Shishkov
819 89df5e95 Michael Niedermayer
    scale *= sqrt(1/8.0);
820 01ca9ac3 Kostya Shishkov
821
    /* Select filter */
822
    if (!s->multirate_inter)    /* Non-perfect reconstruction */
823 b6398969 Reimar Döffinger
        prCoeff = fir_32bands_nonperfect;
824 01ca9ac3 Kostya Shishkov
    else                        /* Perfect reconstruction */
825 b6398969 Reimar Döffinger
        prCoeff = fir_32bands_perfect;
826 01ca9ac3 Kostya Shishkov
827
    /* Reconstructed channel sample index */
828
    for (subindex = 0; subindex < 8; subindex++) {
829
        /* Load in one sample from each subband and clear inactive subbands */
830 b92d483b Måns Rullgård
        for (i = 0; i < sb_act; i++){
831 0dc7df28 Måns Rullgård
            uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ ((i-1)&2)<<30;
832
            AV_WN32A(&s->raXin[i], v);
833 89df5e95 Michael Niedermayer
        }
834 01ca9ac3 Kostya Shishkov
        for (; i < 32; i++)
835 6d9d289e Måns Rullgård
            s->raXin[i] = 0.0;
836 01ca9ac3 Kostya Shishkov
837 f462ed1f Måns Rullgård
        s->synth.synth_filter_float(&s->imdct,
838 4f99c31c Michael Niedermayer
                              s->subband_fir_hist[chans], &s->hist_index[chans],
839
                              s->subband_fir_noidea[chans], prCoeff,
840
                              samples_out, s->raXin, scale, bias);
841 89df5e95 Michael Niedermayer
        samples_out+= 32;
842 01ca9ac3 Kostya Shishkov
843
    }
844
}
845
846 309d16a4 Måns Rullgård
static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
847 01ca9ac3 Kostya Shishkov
                                  int num_deci_sample, float *samples_in,
848
                                  float *samples_out, float scale,
849
                                  float bias)
850
{
851
    /* samples_in: An array holding decimated samples.
852
     *   Samples in current subframe starts from samples_in[0],
853
     *   while samples_in[-1], samples_in[-2], ..., stores samples
854
     *   from last subframe as history.
855
     *
856
     * samples_out: An array holding interpolated samples
857
     */
858
859 309d16a4 Måns Rullgård
    int decifactor;
860 01ca9ac3 Kostya Shishkov
    const float *prCoeff;
861
    int deciindex;
862
863
    /* Select decimation filter */
864
    if (decimation_select == 1) {
865 766fefe8 Måns Rullgård
        decifactor = 64;
866 01ca9ac3 Kostya Shishkov
        prCoeff = lfe_fir_128;
867
    } else {
868 766fefe8 Måns Rullgård
        decifactor = 32;
869 01ca9ac3 Kostya Shishkov
        prCoeff = lfe_fir_64;
870
    }
871
    /* Interpolation */
872
    for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
873 309d16a4 Måns Rullgård
        s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor,
874
                          scale, bias);
875 766fefe8 Måns Rullgård
        samples_in++;
876 309d16a4 Måns Rullgård
        samples_out += 2 * decifactor;
877 01ca9ac3 Kostya Shishkov
    }
878
}
879
880
/* downmixing routines */
881 c31a76e4 Justin Ruggles
#define MIX_REAR1(samples, si1, rs, coef) \
882
     samples[i]     += samples[si1] * coef[rs][0]; \
883
     samples[i+256] += samples[si1] * coef[rs][1];
884 01ca9ac3 Kostya Shishkov
885 c31a76e4 Justin Ruggles
#define MIX_REAR2(samples, si1, si2, rs, coef) \
886
     samples[i]     += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
887
     samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
888 01ca9ac3 Kostya Shishkov
889 c31a76e4 Justin Ruggles
#define MIX_FRONT3(samples, coef) \
890 01ca9ac3 Kostya Shishkov
    t = samples[i]; \
891 c31a76e4 Justin Ruggles
    samples[i]     = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \
892
    samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];
893 01ca9ac3 Kostya Shishkov
894
#define DOWNMIX_TO_STEREO(op1, op2) \
895 d1177cb5 Nick Brereton
    for (i = 0; i < 256; i++){ \
896 01ca9ac3 Kostya Shishkov
        op1 \
897
        op2 \
898
    }
899
900 c31a76e4 Justin Ruggles
static void dca_downmix(float *samples, int srcfmt,
901
                        int downmix_coef[DCA_PRIM_CHANNELS_MAX][2])
902 01ca9ac3 Kostya Shishkov
{
903
    int i;
904
    float t;
905 c31a76e4 Justin Ruggles
    float coef[DCA_PRIM_CHANNELS_MAX][2];
906
907 d1177cb5 Nick Brereton
    for (i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
908 c31a76e4 Justin Ruggles
        coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
909
        coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
910
    }
911 01ca9ac3 Kostya Shishkov
912
    switch (srcfmt) {
913
    case DCA_MONO:
914
    case DCA_CHANNEL:
915
    case DCA_STEREO_TOTAL:
916
    case DCA_STEREO_SUMDIFF:
917
    case DCA_4F2R:
918
        av_log(NULL, 0, "Not implemented!\n");
919
        break;
920
    case DCA_STEREO:
921
        break;
922
    case DCA_3F:
923 c31a76e4 Justin Ruggles
        DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
924 01ca9ac3 Kostya Shishkov
        break;
925
    case DCA_2F1R:
926 c31a76e4 Justin Ruggles
        DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),);
927 01ca9ac3 Kostya Shishkov
        break;
928
    case DCA_3F1R:
929 c31a76e4 Justin Ruggles
        DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
930
                          MIX_REAR1(samples, i + 768, 3, coef));
931 01ca9ac3 Kostya Shishkov
        break;
932
    case DCA_2F2R:
933 c31a76e4 Justin Ruggles
        DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),);
934 01ca9ac3 Kostya Shishkov
        break;
935
    case DCA_3F2R:
936 c31a76e4 Justin Ruggles
        DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
937
                          MIX_REAR2(samples, i + 768, i + 1024, 3, coef));
938 01ca9ac3 Kostya Shishkov
        break;
939
    }
940
}
941
942
943
/* Very compact version of the block code decoder that does not use table
944
 * look-up but is slightly slower */
945
static int decode_blockcode(int code, int levels, int *values)
946
{
947
    int i;
948
    int offset = (levels - 1) >> 1;
949
950
    for (i = 0; i < 4; i++) {
951 843c7aa8 Måns Rullgård
        int div = FASTDIV(code, levels);
952
        values[i] = code - offset - div*levels;
953
        code = div;
954 01ca9ac3 Kostya Shishkov
    }
955
956
    if (code == 0)
957
        return 0;
958
    else {
959
        av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
960
        return -1;
961
    }
962
}
963
964
static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
965
static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
966
967 774e9acf Nick Brereton
static int dca_subsubframe(DCAContext * s, int base_channel, int block_index)
968 01ca9ac3 Kostya Shishkov
{
969
    int k, l;
970
    int subsubframe = s->current_subsubframe;
971
972 a9f87158 Michael Niedermayer
    const float *quant_step_table;
973 01ca9ac3 Kostya Shishkov
974
    /* FIXME */
975 77b4b7c3 Nick Brereton
    float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
976 69e17136 Måns Rullgård
    LOCAL_ALIGNED_16(int, block, [8]);
977 01ca9ac3 Kostya Shishkov
978
    /*
979
     * Audio data
980
     */
981
982
    /* Select quantization step size table */
983 9ed73b48 Benjamin Larsson
    if (s->bit_rate_index == 0x1f)
984 a9f87158 Michael Niedermayer
        quant_step_table = lossless_quant_d;
985 01ca9ac3 Kostya Shishkov
    else
986 a9f87158 Michael Niedermayer
        quant_step_table = lossy_quant_d;
987 01ca9ac3 Kostya Shishkov
988 774e9acf Nick Brereton
    for (k = base_channel; k < s->prim_channels; k++) {
989 01ca9ac3 Kostya Shishkov
        for (l = 0; l < s->vq_start_subband[k]; l++) {
990
            int m;
991
992
            /* Select the mid-tread linear quantizer */
993
            int abits = s->bitalloc[k][l];
994
995
            float quant_step_size = quant_step_table[abits];
996
997
            /*
998
             * Determine quantization index code book and its type
999
             */
1000
1001
            /* Select quantization index code book */
1002
            int sel = s->quant_index_huffman[k][abits];
1003
1004
            /*
1005
             * Extract bits from the bit stream
1006
             */
1007 d1177cb5 Nick Brereton
            if (!abits){
1008 01ca9ac3 Kostya Shishkov
                memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
1009 69e17136 Måns Rullgård
            } else {
1010
                /* Deal with transients */
1011
                int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1012
                float rscale = quant_step_size * s->scale_factor[k][l][sfi] * s->scalefactor_adj[k][sel];
1013
1014 d1177cb5 Nick Brereton
                if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
1015
                    if (abits <= 7){
1016 2bb29da6 Måns Rullgård
                        /* Block code */
1017
                        int block_code1, block_code2, size, levels;
1018
1019
                        size = abits_sizes[abits-1];
1020
                        levels = abits_levels[abits-1];
1021
1022
                        block_code1 = get_bits(&s->gb, size);
1023
                        /* FIXME Should test return value */
1024
                        decode_blockcode(block_code1, levels, block);
1025
                        block_code2 = get_bits(&s->gb, size);
1026
                        decode_blockcode(block_code2, levels, &block[4]);
1027
                    }else{
1028
                        /* no coding */
1029
                        for (m = 0; m < 8; m++)
1030
                            block[m] = get_sbits(&s->gb, abits - 3);
1031
                    }
1032 01ca9ac3 Kostya Shishkov
                }else{
1033 2bb29da6 Måns Rullgård
                    /* Huffman coded */
1034 01ca9ac3 Kostya Shishkov
                    for (m = 0; m < 8; m++)
1035 2bb29da6 Måns Rullgård
                        block[m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
1036 01ca9ac3 Kostya Shishkov
                }
1037
1038 69e17136 Måns Rullgård
                s->dsp.int32_to_float_fmul_scalar(subband_samples[k][l],
1039
                                                  block, rscale, 8);
1040
            }
1041 01ca9ac3 Kostya Shishkov
1042
            /*
1043
             * Inverse ADPCM if in prediction mode
1044
             */
1045
            if (s->prediction_mode[k][l]) {
1046
                int n;
1047
                for (m = 0; m < 8; m++) {
1048
                    for (n = 1; n <= 4; n++)
1049
                        if (m >= n)
1050
                            subband_samples[k][l][m] +=
1051
                                (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1052
                                 subband_samples[k][l][m - n] / 8192);
1053
                        else if (s->predictor_history)
1054
                            subband_samples[k][l][m] +=
1055
                                (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1056
                                 s->subband_samples_hist[k][l][m - n +
1057
                                                               4] / 8192);
1058
                }
1059
            }
1060
        }
1061
1062
        /*
1063
         * Decode VQ encoded high frequencies
1064
         */
1065
        for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1066
            /* 1 vector -> 32 samples but we only need the 8 samples
1067
             * for this subsubframe. */
1068
            int m;
1069
1070
            if (!s->debug_flag & 0x01) {
1071
                av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
1072
                s->debug_flag |= 0x01;
1073
            }
1074
1075
            for (m = 0; m < 8; m++) {
1076
                subband_samples[k][l][m] =
1077
                    high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
1078
                                                        m]
1079
                    * (float) s->scale_factor[k][l][0] / 16.0;
1080
            }
1081
        }
1082
    }
1083
1084
    /* Check for DSYNC after subsubframe */
1085 774e9acf Nick Brereton
    if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1086 01ca9ac3 Kostya Shishkov
        if (0xFFFF == get_bits(&s->gb, 16)) {   /* 0xFFFF */
1087
#ifdef TRACE
1088
            av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1089
#endif
1090
        } else {
1091
            av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1092
        }
1093
    }
1094
1095
    /* Backup predictor history for adpcm */
1096 774e9acf Nick Brereton
    for (k = base_channel; k < s->prim_channels; k++)
1097 01ca9ac3 Kostya Shishkov
        for (l = 0; l < s->vq_start_subband[k]; l++)
1098
            memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
1099
                        4 * sizeof(subband_samples[0][0][0]));
1100
1101 6baef06e Nick Brereton
    return 0;
1102
}
1103
1104
static int dca_filter_channels(DCAContext * s, int block_index)
1105
{
1106
    float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1107
    int k;
1108
1109 01ca9ac3 Kostya Shishkov
    /* 32 subbands QMF */
1110
    for (k = 0; k < s->prim_channels; k++) {
1111
/*        static float pcm_to_double[8] =
1112
            {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
1113 92765276 Benjamin Larsson
         qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
1114 1402ee72 Benjamin Larsson
                            M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ ,
1115
                            s->add_bias );
1116 01ca9ac3 Kostya Shishkov
    }
1117
1118
    /* Down mixing */
1119 6baef06e Nick Brereton
    if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1120 c31a76e4 Justin Ruggles
        dca_downmix(s->samples, s->amode, s->downmix_coef);
1121 01ca9ac3 Kostya Shishkov
    }
1122
1123
    /* Generate LFE samples for this subsubframe FIXME!!! */
1124
    if (s->output & DCA_LFE) {
1125 309d16a4 Måns Rullgård
        lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1126 6baef06e Nick Brereton
                              s->lfe_data + 2 * s->lfe * (block_index + 4),
1127 92765276 Benjamin Larsson
                              &s->samples[256 * dca_lfe_index[s->amode]],
1128 1402ee72 Benjamin Larsson
                              (1.0/256.0)*s->scale_bias,  s->add_bias);
1129 01ca9ac3 Kostya Shishkov
        /* Outputs 20bits pcm samples */
1130
    }
1131
1132
    return 0;
1133
}
1134
1135
1136 774e9acf Nick Brereton
static int dca_subframe_footer(DCAContext * s, int base_channel)
1137 01ca9ac3 Kostya Shishkov
{
1138
    int aux_data_count = 0, i;
1139
1140
    /*
1141
     * Unpack optional information
1142
     */
1143
1144 774e9acf Nick Brereton
    /* presumably optional information only appears in the core? */
1145
    if (!base_channel) {
1146 d1177cb5 Nick Brereton
        if (s->timestamp)
1147
            get_bits(&s->gb, 32);
1148 01ca9ac3 Kostya Shishkov
1149 d1177cb5 Nick Brereton
        if (s->aux_data)
1150
            aux_data_count = get_bits(&s->gb, 6);
1151 01ca9ac3 Kostya Shishkov
1152 d1177cb5 Nick Brereton
        for (i = 0; i < aux_data_count; i++)
1153
            get_bits(&s->gb, 8);
1154 01ca9ac3 Kostya Shishkov
1155 d1177cb5 Nick Brereton
        if (s->crc_present && (s->downmix || s->dynrange))
1156
            get_bits(&s->gb, 16);
1157 774e9acf Nick Brereton
    }
1158 01ca9ac3 Kostya Shishkov
1159
    return 0;
1160
}
1161
1162
/**
1163
 * Decode a dca frame block
1164
 *
1165
 * @param s     pointer to the DCAContext
1166
 */
1167
1168 774e9acf Nick Brereton
static int dca_decode_block(DCAContext * s, int base_channel, int block_index)
1169 01ca9ac3 Kostya Shishkov
{
1170
1171
    /* Sanity check */
1172
    if (s->current_subframe >= s->subframes) {
1173
        av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1174
               s->current_subframe, s->subframes);
1175
        return -1;
1176
    }
1177
1178
    if (!s->current_subsubframe) {
1179
#ifdef TRACE
1180
        av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1181
#endif
1182
        /* Read subframe header */
1183 774e9acf Nick Brereton
        if (dca_subframe_header(s, base_channel, block_index))
1184 01ca9ac3 Kostya Shishkov
            return -1;
1185
    }
1186
1187
    /* Read subsubframe */
1188
#ifdef TRACE
1189
    av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1190
#endif
1191 774e9acf Nick Brereton
    if (dca_subsubframe(s, base_channel, block_index))
1192 01ca9ac3 Kostya Shishkov
        return -1;
1193
1194
    /* Update state */
1195
    s->current_subsubframe++;
1196 774e9acf Nick Brereton
    if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1197 01ca9ac3 Kostya Shishkov
        s->current_subsubframe = 0;
1198
        s->current_subframe++;
1199
    }
1200
    if (s->current_subframe >= s->subframes) {
1201
#ifdef TRACE
1202
        av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1203
#endif
1204
        /* Read subframe footer */
1205 774e9acf Nick Brereton
        if (dca_subframe_footer(s, base_channel))
1206 01ca9ac3 Kostya Shishkov
            return -1;
1207
    }
1208
1209
    return 0;
1210
}
1211
1212
/**
1213
 * Convert bitstream to one representation based on sync marker
1214
 */
1215 a9f87158 Michael Niedermayer
static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
1216 01ca9ac3 Kostya Shishkov
                          int max_size)
1217
{
1218
    uint32_t mrk;
1219
    int i, tmp;
1220 a9f87158 Michael Niedermayer
    const uint16_t *ssrc = (const uint16_t *) src;
1221
    uint16_t *sdst = (uint16_t *) dst;
1222 01ca9ac3 Kostya Shishkov
    PutBitContext pb;
1223
1224 d1177cb5 Nick Brereton
    if ((unsigned)src_size > (unsigned)max_size) {
1225 d041a598 Kostya Shishkov
//        av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
1226
//        return -1;
1227
        src_size = max_size;
1228 56fd7cc5 Benjamin Larsson
    }
1229 9f1473b3 Michael Niedermayer
1230 01ca9ac3 Kostya Shishkov
    mrk = AV_RB32(src);
1231
    switch (mrk) {
1232
    case DCA_MARKER_RAW_BE:
1233 56523713 Michael Niedermayer
        memcpy(dst, src, src_size);
1234
        return src_size;
1235 01ca9ac3 Kostya Shishkov
    case DCA_MARKER_RAW_LE:
1236 56523713 Michael Niedermayer
        for (i = 0; i < (src_size + 1) >> 1; i++)
1237 8fc0162a Måns Rullgård
            *sdst++ = av_bswap16(*ssrc++);
1238 56523713 Michael Niedermayer
        return src_size;
1239 01ca9ac3 Kostya Shishkov
    case DCA_MARKER_14B_BE:
1240
    case DCA_MARKER_14B_LE:
1241
        init_put_bits(&pb, dst, max_size);
1242
        for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
1243
            tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1244
            put_bits(&pb, 14, tmp);
1245
        }
1246
        flush_put_bits(&pb);
1247
        return (put_bits_count(&pb) + 7) >> 3;
1248
    default:
1249
        return -1;
1250
    }
1251
}
1252
1253
/**
1254
 * Main frame decoding function
1255
 * FIXME add arguments
1256
 */
1257
static int dca_decode_frame(AVCodecContext * avctx,
1258
                            void *data, int *data_size,
1259 7a00bbad Thilo Borgmann
                            AVPacket *avpkt)
1260 01ca9ac3 Kostya Shishkov
{
1261 7a00bbad Thilo Borgmann
    const uint8_t *buf = avpkt->data;
1262
    int buf_size = avpkt->size;
1263 01ca9ac3 Kostya Shishkov
1264 6baef06e Nick Brereton
    int lfe_samples;
1265 774e9acf Nick Brereton
    int num_core_channels = 0;
1266 8ed96a5e Andreas Öman
    int i;
1267 01ca9ac3 Kostya Shishkov
    int16_t *samples = data;
1268
    DCAContext *s = avctx->priv_data;
1269
    int channels;
1270
1271
1272 d0a18850 Nick Brereton
    s->xch_present = 0;
1273 01ca9ac3 Kostya Shishkov
    s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
1274
    if (s->dca_buffer_size == -1) {
1275 56fd7cc5 Benjamin Larsson
        av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1276 01ca9ac3 Kostya Shishkov
        return -1;
1277
    }
1278
1279
    init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1280
    if (dca_parse_frame_header(s) < 0) {
1281
        //seems like the frame is corrupt, try with the next one
1282 75316e1d Limin Wang
        *data_size=0;
1283 01ca9ac3 Kostya Shishkov
        return buf_size;
1284
    }
1285
    //set AVCodec values with parsed data
1286
    avctx->sample_rate = s->sample_rate;
1287
    avctx->bit_rate = s->bit_rate;
1288
1289 6baef06e Nick Brereton
    for (i = 0; i < (s->sample_blocks / 8); i++) {
1290 774e9acf Nick Brereton
        dca_decode_block(s, 0, i);
1291
    }
1292
1293
    /* record number of core channels incase less than max channels are requested */
1294
    num_core_channels = s->prim_channels;
1295
1296
    /* extensions start at 32-bit boundaries into bitstream */
1297 324a94b0 Benjamin Larsson
    skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1298 774e9acf Nick Brereton
1299
    while(get_bits_left(&s->gb) >= 32) {
1300 324a94b0 Benjamin Larsson
        uint32_t bits = get_bits_long(&s->gb, 32);
1301 774e9acf Nick Brereton
1302
        switch(bits) {
1303
        case 0x5a5a5a5a: {
1304 0712c230 Nick Brereton
            int ext_amode, xch_fsize;
1305
1306 d0a18850 Nick Brereton
            s->xch_base_channel = s->prim_channels;
1307
1308 0712c230 Nick Brereton
            /* validate sync word using XCHFSIZE field */
1309
            xch_fsize = show_bits(&s->gb, 10);
1310
            if((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1311
               (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1312
                continue;
1313 774e9acf Nick Brereton
1314
            /* skip length-to-end-of-frame field for the moment */
1315
            skip_bits(&s->gb, 10);
1316
1317
            /* extension amode should == 1, number of channels in extension */
1318
            /* AFAIK XCh is not used for more channels */
1319
            if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1320
                av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
1321
                       " supported!\n",ext_amode);
1322
                continue;
1323
            }
1324
1325
            /* much like core primary audio coding header */
1326 d0a18850 Nick Brereton
            dca_parse_audio_coding_header(s, s->xch_base_channel);
1327 774e9acf Nick Brereton
1328
            for (i = 0; i < (s->sample_blocks / 8); i++) {
1329 d0a18850 Nick Brereton
                dca_decode_block(s, s->xch_base_channel, i);
1330 774e9acf Nick Brereton
            }
1331
1332 d0a18850 Nick Brereton
            s->xch_present = 1;
1333 774e9acf Nick Brereton
            break;
1334
        }
1335
        case 0x1d95f262:
1336
            av_log(avctx, AV_LOG_DEBUG, "Possible X96 extension found at %d bits\n", get_bits_count(&s->gb));
1337
            av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", get_bits(&s->gb, 12)+1);
1338
            av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1339
            break;
1340
        }
1341
1342 324a94b0 Benjamin Larsson
        skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1343 6baef06e Nick Brereton
    }
1344
1345 cc826626 Justin Ruggles
    channels = s->prim_channels + !!s->lfe;
1346 92765276 Benjamin Larsson
1347
    if (s->amode<16) {
1348 87c3b9bc Benjamin Larsson
        avctx->channel_layout = dca_core_channel_layout[s->amode];
1349
1350 d0a18850 Nick Brereton
        if (s->xch_present && (!avctx->request_channels ||
1351 774e9acf Nick Brereton
                            avctx->request_channels > num_core_channels)) {
1352
            avctx->channel_layout |= CH_BACK_CENTER;
1353
            if (s->lfe) {
1354
                avctx->channel_layout |= CH_LOW_FREQUENCY;
1355
                s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
1356
            } else {
1357
                s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
1358
            }
1359
        } else {
1360
            if (s->lfe) {
1361
                avctx->channel_layout |= CH_LOW_FREQUENCY;
1362
                s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1363
            } else
1364
                s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1365
        }
1366 92765276 Benjamin Larsson
1367 4e06acbd Laurent Aimar
        if (s->prim_channels > 0 &&
1368
            s->channel_order_tab[s->prim_channels - 1] < 0)
1369
            return -1;
1370
1371 d1177cb5 Nick Brereton
        if (avctx->request_channels == 2 && s->prim_channels > 2) {
1372 92765276 Benjamin Larsson
            channels = 2;
1373
            s->output = DCA_STEREO;
1374
            avctx->channel_layout = CH_LAYOUT_STEREO;
1375
        }
1376
    } else {
1377
        av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
1378
        return -1;
1379
    }
1380
1381 cc826626 Justin Ruggles
1382 4a24837e Benjamin Larsson
    /* There is nothing that prevents a dts frame to change channel configuration
1383
       but FFmpeg doesn't support that so only set the channels if it is previously
1384
       unset. Ideally during the first probe for channels the crc should be checked
1385
       and only set avctx->channels when the crc is ok. Right now the decoder could
1386
       set the channels based on a broken first frame.*/
1387
    if (!avctx->channels)
1388
        avctx->channels = channels;
1389
1390 d1177cb5 Nick Brereton
    if (*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
1391 01ca9ac3 Kostya Shishkov
        return -1;
1392 a7bc9d1b Andreas Öman
    *data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels;
1393 6baef06e Nick Brereton
1394
    /* filter to get final output */
1395 01ca9ac3 Kostya Shishkov
    for (i = 0; i < (s->sample_blocks / 8); i++) {
1396 6baef06e Nick Brereton
        dca_filter_channels(s, i);
1397 b12b16c5 Nick Brereton
1398
        /* If this was marked as a DTS-ES stream we need to subtract back- */
1399
        /* channel from SL & SR to remove matrixed back-channel signal */
1400
        if((s->source_pcm_res & 1) && s->xch_present) {
1401
            float* back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;
1402
            float* lt_chan   = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
1403
            float* rt_chan   = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
1404
            int j;
1405
            for(j = 0; j < 256; ++j) {
1406 f2401c21 Christophe Gisquet
                lt_chan[j] -= (back_chan[j] - s->add_bias) * M_SQRT1_2;
1407
                rt_chan[j] -= (back_chan[j] - s->add_bias) * M_SQRT1_2;
1408 b12b16c5 Nick Brereton
            }
1409
        }
1410
1411 8ed96a5e Andreas Öman
        s->dsp.float_to_int16_interleave(samples, s->samples_chanptr, 256, channels);
1412
        samples += 256 * channels;
1413 01ca9ac3 Kostya Shishkov
    }
1414
1415 6baef06e Nick Brereton
    /* update lfe history */
1416
    lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
1417
    for (i = 0; i < 2 * s->lfe * 4; i++) {
1418
        s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1419
    }
1420
1421 01ca9ac3 Kostya Shishkov
    return buf_size;
1422
}
1423
1424
1425
1426
/**
1427
 * DCA initialization
1428
 *
1429
 * @param avctx     pointer to the AVCodecContext
1430
 */
1431
1432 98a6fff9 Zuxy Meng
static av_cold int dca_decode_init(AVCodecContext * avctx)
1433 01ca9ac3 Kostya Shishkov
{
1434
    DCAContext *s = avctx->priv_data;
1435 8ed96a5e Andreas Öman
    int i;
1436 01ca9ac3 Kostya Shishkov
1437
    s->avctx = avctx;
1438
    dca_init_vlcs();
1439
1440
    dsputil_init(&s->dsp, avctx);
1441 7d485f16 Siarhei Siamashka
    ff_mdct_init(&s->imdct, 6, 1, 1.0);
1442 f462ed1f Måns Rullgård
    ff_synth_filter_init(&s->synth);
1443 309d16a4 Måns Rullgård
    ff_dcadsp_init(&s->dcadsp);
1444 cfec09e9 Justin Ruggles
1445 6baef06e Nick Brereton
    for (i = 0; i < DCA_PRIM_CHANNELS_MAX+1; i++)
1446 8ed96a5e Andreas Öman
        s->samples_chanptr[i] = s->samples + i * 256;
1447 fd76c37f Peter Ross
    avctx->sample_fmt = SAMPLE_FMT_S16;
1448 1402ee72 Benjamin Larsson
1449 d1177cb5 Nick Brereton
    if (s->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
1450 1402ee72 Benjamin Larsson
        s->add_bias = 385.0f;
1451
        s->scale_bias = 1.0 / 32768.0;
1452
    } else {
1453
        s->add_bias = 0.0f;
1454
        s->scale_bias = 1.0;
1455 ba5fc967 Benjamin Larsson
1456
        /* allow downmixing to stereo */
1457
        if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
1458
                avctx->request_channels == 2) {
1459
            avctx->channels = avctx->request_channels;
1460
        }
1461 1402ee72 Benjamin Larsson
    }
1462
1463
1464 01ca9ac3 Kostya Shishkov
    return 0;
1465
}
1466
1467 89df5e95 Michael Niedermayer
static av_cold int dca_decode_end(AVCodecContext * avctx)
1468
{
1469
    DCAContext *s = avctx->priv_data;
1470
    ff_mdct_end(&s->imdct);
1471
    return 0;
1472
}
1473 01ca9ac3 Kostya Shishkov
1474
AVCodec dca_decoder = {
1475
    .name = "dca",
1476 72415b2a Stefano Sabatini
    .type = AVMEDIA_TYPE_AUDIO,
1477 01ca9ac3 Kostya Shishkov
    .id = CODEC_ID_DTS,
1478
    .priv_data_size = sizeof(DCAContext),
1479
    .init = dca_decode_init,
1480
    .decode = dca_decode_frame,
1481 89df5e95 Michael Niedermayer
    .close = dca_decode_end,
1482 fe4bf374 Stefano Sabatini
    .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
1483 01ca9ac3 Kostya Shishkov
};