Browse code

DCA/DTA encoder

Alexander E. Patrakov authored on 2011/04/27 04:07:55
Showing 5 changed files
... ...
@@ -604,7 +604,7 @@ following image formats are supported:
604 604
     @tab Codec used in Delphine Software International games.
605 605
 @item COOK                   @tab     @tab  X
606 606
     @tab All versions except 5.1 are supported.
607
-@item DCA (DTS Coherent Acoustics)  @tab     @tab  X
607
+@item DCA (DTS Coherent Acoustics)  @tab  X  @tab  X
608 608
 @item DPCM id RoQ            @tab  X  @tab  X
609 609
     @tab Used in Quake III, Jedi Knight 2, other computer games.
610 610
 @item DPCM Interplay         @tab     @tab  X
... ...
@@ -105,6 +105,7 @@ OBJS-$(CONFIG_COOK_DECODER)            += cook.o
105 105
 OBJS-$(CONFIG_CSCD_DECODER)            += cscd.o
106 106
 OBJS-$(CONFIG_CYUV_DECODER)            += cyuv.o
107 107
 OBJS-$(CONFIG_DCA_DECODER)             += dca.o synth_filter.o dcadsp.o
108
+OBJS-$(CONFIG_DCA_ENCODER)             += dcaenc.o
108 109
 OBJS-$(CONFIG_DFA_DECODER)             += dfa.o
109 110
 OBJS-$(CONFIG_DNXHD_DECODER)           += dnxhddec.o dnxhddata.o
110 111
 OBJS-$(CONFIG_DNXHD_ENCODER)           += dnxhdenc.o dnxhddata.o       \
... ...
@@ -246,7 +246,7 @@ void avcodec_register_all(void)
246 246
     REGISTER_DECODER (BINKAUDIO_DCT, binkaudio_dct);
247 247
     REGISTER_DECODER (BINKAUDIO_RDFT, binkaudio_rdft);
248 248
     REGISTER_DECODER (COOK, cook);
249
-    REGISTER_DECODER (DCA, dca);
249
+    REGISTER_ENCDEC  (DCA, dca);
250 250
     REGISTER_DECODER (DSICINAUDIO, dsicinaudio);
251 251
     REGISTER_DECODER (EAC3, eac3);
252 252
     REGISTER_ENCDEC  (FLAC, flac);
253 253
new file mode 100644
... ...
@@ -0,0 +1,587 @@
0
+/*
1
+ * DCA encoder
2
+ * Copyright (C) 2008 Alexander E. Patrakov
3
+ *               2010 Benjamin Larsson
4
+ *               2011 Xiang Wang
5
+ *
6
+ * This file is part of FFmpeg.
7
+ *
8
+ * FFmpeg is free software; you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation; either
11
+ * version 2.1 of the License, or (at your option) any later version.
12
+ *
13
+ * FFmpeg is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
+ * Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public
19
+ * License along with FFmpeg; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
+ */
22
+
23
+#include "libavutil/common.h"
24
+#include "libavutil/avassert.h"
25
+#include "libavutil/audioconvert.h"
26
+#include "avcodec.h"
27
+#include "get_bits.h"
28
+#include "put_bits.h"
29
+#include "dcaenc.h"
30
+#include "dcadata.h"
31
+
32
+#undef NDEBUG
33
+
34
+#define MAX_CHANNELS 6
35
+#define DCA_SUBBANDS_32 32
36
+#define DCA_MAX_FRAME_SIZE 16383
37
+#define DCA_HEADER_SIZE 13
38
+
39
+#define DCA_SUBBANDS 32 ///< Subband activity count
40
+#define QUANTIZER_BITS 16
41
+#define SUBFRAMES 1
42
+#define SUBSUBFRAMES 4
43
+#define PCM_SAMPLES (SUBFRAMES*SUBSUBFRAMES*8)
44
+#define LFE_BITS 8
45
+#define LFE_INTERPOLATION 64
46
+#define LFE_PRESENT 2
47
+#define LFE_MISSING 0
48
+
49
+static const int8_t dca_lfe_index[] = {
50
+    1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
51
+};
52
+
53
+static const int8_t dca_channel_reorder_lfe[][9] = {
54
+    { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
55
+    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
56
+    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
57
+    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
58
+    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
59
+    { 1,  2,  0, -1, -1, -1, -1, -1, -1 },
60
+    { 0,  1, -1,  2, -1, -1, -1, -1, -1 },
61
+    { 1,  2,  0, -1,  3, -1, -1, -1, -1 },
62
+    { 0,  1, -1,  2,  3, -1, -1, -1, -1 },
63
+    { 1,  2,  0, -1,  3,  4, -1, -1, -1 },
64
+    { 2,  3, -1,  0,  1,  4,  5, -1, -1 },
65
+    { 1,  2,  0, -1,  3,  4,  5, -1, -1 },
66
+    { 0, -1,  4,  5,  2,  3,  1, -1, -1 },
67
+    { 3,  4,  1, -1,  0,  2,  5,  6, -1 },
68
+    { 2,  3, -1,  5,  7,  0,  1,  4,  6 },
69
+    { 3,  4,  1, -1,  0,  2,  5,  7,  6 },
70
+};
71
+
72
+static const int8_t dca_channel_reorder_nolfe[][9] = {
73
+    { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
74
+    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
75
+    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
76
+    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
77
+    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
78
+    { 1,  2,  0, -1, -1, -1, -1, -1, -1 },
79
+    { 0,  1,  2, -1, -1, -1, -1, -1, -1 },
80
+    { 1,  2,  0,  3, -1, -1, -1, -1, -1 },
81
+    { 0,  1,  2,  3, -1, -1, -1, -1, -1 },
82
+    { 1,  2,  0,  3,  4, -1, -1, -1, -1 },
83
+    { 2,  3,  0,  1,  4,  5, -1, -1, -1 },
84
+    { 1,  2,  0,  3,  4,  5, -1, -1, -1 },
85
+    { 0,  4,  5,  2,  3,  1, -1, -1, -1 },
86
+    { 3,  4,  1,  0,  2,  5,  6, -1, -1 },
87
+    { 2,  3,  5,  7,  0,  1,  4,  6, -1 },
88
+    { 3,  4,  1,  0,  2,  5,  7,  6, -1 },
89
+};
90
+
91
+typedef struct {
92
+    PutBitContext pb;
93
+    int32_t history[MAX_CHANNELS][512]; /* This is a circular buffer */
94
+    int start[MAX_CHANNELS];
95
+    int frame_size;
96
+    int prim_channels;
97
+    int lfe_channel;
98
+    int sample_rate_code;
99
+    int scale_factor[MAX_CHANNELS][DCA_SUBBANDS_32];
100
+    int lfe_scale_factor;
101
+    int lfe_data[SUBFRAMES*SUBSUBFRAMES*4];
102
+
103
+    int a_mode;                         ///< audio channels arrangement
104
+    int num_channel;
105
+    int lfe_state;
106
+    int lfe_offset;
107
+    const int8_t *channel_order_tab;    ///< channel reordering table, lfe and non lfe
108
+
109
+    int32_t pcm[FFMAX(LFE_INTERPOLATION, DCA_SUBBANDS_32)];
110
+    int32_t subband[PCM_SAMPLES][MAX_CHANNELS][DCA_SUBBANDS_32]; /* [sample][channel][subband] */
111
+} DCAContext;
112
+
113
+static int32_t cos_table[128];
114
+
115
+static inline int32_t mul32(int32_t a, int32_t b)
116
+{
117
+    int64_t r = (int64_t) a * b;
118
+    /* round the result before truncating - improves accuracy */
119
+    return (r + 0x80000000) >> 32;
120
+}
121
+
122
+/* Integer version of the cosine modulated Pseudo QMF */
123
+
124
+static void qmf_init(void)
125
+{
126
+    int i;
127
+    int32_t c[17], s[17];
128
+    s[0] = 0;           /* sin(index * PI / 64) * 0x7fffffff */
129
+    c[0] = 0x7fffffff;  /* cos(index * PI / 64) * 0x7fffffff */
130
+
131
+    for (i = 1; i <= 16; i++) {
132
+        s[i] = 2 * (mul32(c[i - 1], 105372028)  + mul32(s[i - 1], 2144896908));
133
+        c[i] = 2 * (mul32(c[i - 1], 2144896908) - mul32(s[i - 1], 105372028));
134
+    }
135
+
136
+    for (i = 0; i < 16; i++) {
137
+        cos_table[i      ]  =  c[i]      >> 3; /* avoid output overflow */
138
+        cos_table[i +  16]  =  s[16 - i] >> 3;
139
+        cos_table[i +  32]  = -s[i]      >> 3;
140
+        cos_table[i +  48]  = -c[16 - i] >> 3;
141
+        cos_table[i +  64]  = -c[i]      >> 3;
142
+        cos_table[i +  80]  = -s[16 - i] >> 3;
143
+        cos_table[i +  96]  =  s[i]      >> 3;
144
+        cos_table[i + 112]  =  c[16 - i] >> 3;
145
+    }
146
+}
147
+
148
+static int32_t band_delta_factor(int band, int sample_num)
149
+{
150
+    int index = band * (2 * sample_num + 1);
151
+    if (band == 0)
152
+        return 0x07ffffff;
153
+    else
154
+        return cos_table[index & 127];
155
+}
156
+
157
+static void add_new_samples(DCAContext *c, const int32_t *in,
158
+                            int count, int channel)
159
+{
160
+    int i;
161
+
162
+    /* Place new samples into the history buffer */
163
+    for (i = 0; i < count; i++) {
164
+        c->history[channel][c->start[channel] + i] = in[i];
165
+        av_assert0(c->start[channel] + i < 512);
166
+    }
167
+    c->start[channel] += count;
168
+    if (c->start[channel] == 512)
169
+        c->start[channel] = 0;
170
+    av_assert0(c->start[channel] < 512);
171
+}
172
+
173
+static void qmf_decompose(DCAContext *c, int32_t in[32], int32_t out[32],
174
+                          int channel)
175
+{
176
+    int band, i, j, k;
177
+    int32_t resp;
178
+    int32_t accum[DCA_SUBBANDS_32] = {0};
179
+
180
+    add_new_samples(c, in, DCA_SUBBANDS_32, channel);
181
+
182
+    /* Calculate the dot product of the signal with the (possibly inverted)
183
+       reference decoder's response to this vector:
184
+       (0.0, 0.0, ..., 0.0, -1.0, 1.0, 0.0, ..., 0.0)
185
+       so that -1.0 cancels 1.0 from the previous step */
186
+
187
+    for (k = 48, j = 0, i = c->start[channel]; i < 512; k++, j++, i++)
188
+        accum[(k & 32) ? (31 - (k & 31)) : (k & 31)] += mul32(c->history[channel][i], UnQMF[j]);
189
+    for (i = 0; i < c->start[channel]; k++, j++, i++)
190
+        accum[(k & 32) ? (31 - (k & 31)) : (k & 31)] += mul32(c->history[channel][i], UnQMF[j]);
191
+
192
+    resp = 0;
193
+    /* TODO: implement FFT instead of this naive calculation */
194
+    for (band = 0; band < DCA_SUBBANDS_32; band++) {
195
+        for (j = 0; j < 32; j++)
196
+            resp += mul32(accum[j], band_delta_factor(band, j));
197
+
198
+        out[band] = (band & 2) ? (-resp) : resp;
199
+    }
200
+}
201
+
202
+static int32_t lfe_fir_64i[512];
203
+static int lfe_downsample(DCAContext *c, int32_t in[LFE_INTERPOLATION])
204
+{
205
+    int i, j;
206
+    int channel = c->prim_channels;
207
+    int32_t accum = 0;
208
+
209
+    add_new_samples(c, in, LFE_INTERPOLATION, channel);
210
+    for (i = c->start[channel], j = 0; i < 512; i++, j++)
211
+        accum += mul32(c->history[channel][i], lfe_fir_64i[j]);
212
+    for (i = 0; i < c->start[channel]; i++, j++)
213
+        accum += mul32(c->history[channel][i], lfe_fir_64i[j]);
214
+    return accum;
215
+}
216
+
217
+static void init_lfe_fir(void)
218
+{
219
+    static int initialized = 0;
220
+    int i;
221
+    if (initialized)
222
+        return;
223
+
224
+    for (i = 0; i < 512; i++)
225
+        lfe_fir_64i[i] = lfe_fir_64[i] * (1 << 25); //float -> int32_t
226
+    initialized = 1;
227
+}
228
+
229
+static void put_frame_header(DCAContext *c)
230
+{
231
+    /* SYNC */
232
+    put_bits(&c->pb, 16, 0x7ffe);
233
+    put_bits(&c->pb, 16, 0x8001);
234
+
235
+    /* Frame type: normal */
236
+    put_bits(&c->pb, 1, 1);
237
+
238
+    /* Deficit sample count: none */
239
+    put_bits(&c->pb, 5, 31);
240
+
241
+    /* CRC is not present */
242
+    put_bits(&c->pb, 1, 0);
243
+
244
+    /* Number of PCM sample blocks */
245
+    put_bits(&c->pb, 7, PCM_SAMPLES-1);
246
+
247
+    /* Primary frame byte size */
248
+    put_bits(&c->pb, 14, c->frame_size-1);
249
+
250
+    /* Audio channel arrangement: L + R (stereo) */
251
+    put_bits(&c->pb, 6, c->num_channel);
252
+
253
+    /* Core audio sampling frequency */
254
+    put_bits(&c->pb, 4, c->sample_rate_code);
255
+
256
+    /* Transmission bit rate: 1411.2 kbps */
257
+    put_bits(&c->pb, 5, 0x16); /* FIXME: magic number */
258
+
259
+    /* Embedded down mix: disabled */
260
+    put_bits(&c->pb, 1, 0);
261
+
262
+    /* Embedded dynamic range flag: not present */
263
+    put_bits(&c->pb, 1, 0);
264
+
265
+    /* Embedded time stamp flag: not present */
266
+    put_bits(&c->pb, 1, 0);
267
+
268
+    /* Auxiliary data flag: not present */
269
+    put_bits(&c->pb, 1, 0);
270
+
271
+    /* HDCD source: no */
272
+    put_bits(&c->pb, 1, 0);
273
+
274
+    /* Extension audio ID: N/A */
275
+    put_bits(&c->pb, 3, 0);
276
+
277
+    /* Extended audio data: not present */
278
+    put_bits(&c->pb, 1, 0);
279
+
280
+    /* Audio sync word insertion flag: after each sub-frame */
281
+    put_bits(&c->pb, 1, 0);
282
+
283
+    /* Low frequency effects flag: not present or interpolation factor=64 */
284
+    put_bits(&c->pb, 2, c->lfe_state);
285
+
286
+    /* Predictor history switch flag: on */
287
+    put_bits(&c->pb, 1, 1);
288
+
289
+    /* No CRC */
290
+    /* Multirate interpolator switch: non-perfect reconstruction */
291
+    put_bits(&c->pb, 1, 0);
292
+
293
+    /* Encoder software revision: 7 */
294
+    put_bits(&c->pb, 4, 7);
295
+
296
+    /* Copy history: 0 */
297
+    put_bits(&c->pb, 2, 0);
298
+
299
+    /* Source PCM resolution: 16 bits, not DTS ES */
300
+    put_bits(&c->pb, 3, 0);
301
+
302
+    /* Front sum/difference coding: no */
303
+    put_bits(&c->pb, 1, 0);
304
+
305
+    /* Surrounds sum/difference coding: no */
306
+    put_bits(&c->pb, 1, 0);
307
+
308
+    /* Dialog normalization: 0 dB */
309
+    put_bits(&c->pb, 4, 0);
310
+}
311
+
312
+static void put_primary_audio_header(DCAContext *c)
313
+{
314
+    static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
315
+    static const int thr[11]    = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
316
+
317
+    int ch, i;
318
+    /* Number of subframes */
319
+    put_bits(&c->pb, 4, SUBFRAMES - 1);
320
+
321
+    /* Number of primary audio channels */
322
+    put_bits(&c->pb, 3, c->prim_channels - 1);
323
+
324
+    /* Subband activity count */
325
+    for (ch = 0; ch < c->prim_channels; ch++)
326
+        put_bits(&c->pb, 5, DCA_SUBBANDS - 2);
327
+
328
+    /* High frequency VQ start subband */
329
+    for (ch = 0; ch < c->prim_channels; ch++)
330
+        put_bits(&c->pb, 5, DCA_SUBBANDS - 1);
331
+
332
+    /* Joint intensity coding index: 0, 0 */
333
+    for (ch = 0; ch < c->prim_channels; ch++)
334
+        put_bits(&c->pb, 3, 0);
335
+
336
+    /* Transient mode codebook: A4, A4 (arbitrary) */
337
+    for (ch = 0; ch < c->prim_channels; ch++)
338
+        put_bits(&c->pb, 2, 0);
339
+
340
+    /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
341
+    for (ch = 0; ch < c->prim_channels; ch++)
342
+        put_bits(&c->pb, 3, 6);
343
+
344
+    /* Bit allocation quantizer select: linear 5-bit */
345
+    for (ch = 0; ch < c->prim_channels; ch++)
346
+        put_bits(&c->pb, 3, 6);
347
+
348
+    /* Quantization index codebook select: dummy data
349
+       to avoid transmission of scale factor adjustment */
350
+
351
+    for (i = 1; i < 11; i++)
352
+        for (ch = 0; ch < c->prim_channels; ch++)
353
+            put_bits(&c->pb, bitlen[i], thr[i]);
354
+
355
+    /* Scale factor adjustment index: not transmitted */
356
+}
357
+
358
+/**
359
+ * 8-23 bits quantization
360
+ * @param sample
361
+ * @param bits
362
+ */
363
+static inline uint32_t quantize(int32_t sample, int bits)
364
+{
365
+    av_assert0(sample <    1 << (bits - 1));
366
+    av_assert0(sample >= -(1 << (bits - 1)));
367
+    sample &= sample & ((1 << bits) - 1);
368
+    return sample;
369
+}
370
+
371
+static inline int find_scale_factor7(int64_t max_value, int bits)
372
+{
373
+    int i = 0, j = 128, q;
374
+    max_value = ((max_value << 15) / lossy_quant[bits + 3]) >> (bits - 1);
375
+    while (i < j) {
376
+        q = (i + j) >> 1;
377
+        if (max_value < scale_factor_quant7[q])
378
+            j = q;
379
+        else
380
+            i = q + 1;
381
+    }
382
+    av_assert1(i < 128);
383
+    return i;
384
+}
385
+
386
+static inline void put_sample7(DCAContext *c, int64_t sample, int bits,
387
+                               int scale_factor)
388
+{
389
+    sample = (sample << 15) / ((int64_t) lossy_quant[bits + 3] * scale_factor_quant7[scale_factor]);
390
+    put_bits(&c->pb, bits, quantize((int) sample, bits));
391
+}
392
+
393
+static void put_subframe(DCAContext *c,
394
+                         int32_t subband_data[8 * SUBSUBFRAMES][MAX_CHANNELS][32],
395
+                         int subframe)
396
+{
397
+    int i, sub, ss, ch, max_value;
398
+    int32_t *lfe_data = c->lfe_data + 4 * SUBSUBFRAMES * subframe;
399
+
400
+    /* Subsubframes count */
401
+    put_bits(&c->pb, 2, SUBSUBFRAMES -1);
402
+
403
+    /* Partial subsubframe sample count: dummy */
404
+    put_bits(&c->pb, 3, 0);
405
+
406
+    /* Prediction mode: no ADPCM, in each channel and subband */
407
+    for (ch = 0; ch < c->prim_channels; ch++)
408
+        for (sub = 0; sub < DCA_SUBBANDS; sub++)
409
+            put_bits(&c->pb, 1, 0);
410
+
411
+    /* Prediction VQ addres: not transmitted */
412
+    /* Bit allocation index */
413
+    for (ch = 0; ch < c->prim_channels; ch++)
414
+        for (sub = 0; sub < DCA_SUBBANDS; sub++)
415
+            put_bits(&c->pb, 5, QUANTIZER_BITS+3);
416
+
417
+    if (SUBSUBFRAMES > 1) {
418
+        /* Transition mode: none for each channel and subband */
419
+        for (ch = 0; ch < c->prim_channels; ch++)
420
+            for (sub = 0; sub < DCA_SUBBANDS; sub++)
421
+                put_bits(&c->pb, 1, 0); /* codebook A4 */
422
+    }
423
+
424
+    /* Determine scale_factor */
425
+    for (ch = 0; ch < c->prim_channels; ch++)
426
+        for (sub = 0; sub < DCA_SUBBANDS; sub++) {
427
+            max_value = 0;
428
+            for (i = 0; i < 8 * SUBSUBFRAMES; i++)
429
+                max_value = FFMAX(max_value, FFABS(subband_data[i][ch][sub]));
430
+            c->scale_factor[ch][sub] = find_scale_factor7(max_value, QUANTIZER_BITS);
431
+        }
432
+
433
+    if (c->lfe_channel) {
434
+        max_value = 0;
435
+        for (i = 0; i < 4 * SUBSUBFRAMES; i++)
436
+            max_value = FFMAX(max_value, FFABS(lfe_data[i]));
437
+        c->lfe_scale_factor = find_scale_factor7(max_value, LFE_BITS);
438
+    }
439
+
440
+    /* Scale factors: the same for each channel and subband,
441
+       encoded according to Table D.1.2 */
442
+    for (ch = 0; ch < c->prim_channels; ch++)
443
+        for (sub = 0; sub < DCA_SUBBANDS; sub++)
444
+            put_bits(&c->pb, 7, c->scale_factor[ch][sub]);
445
+
446
+    /* Joint subband scale factor codebook select: not transmitted */
447
+    /* Scale factors for joint subband coding: not transmitted */
448
+    /* Stereo down-mix coefficients: not transmitted */
449
+    /* Dynamic range coefficient: not transmitted */
450
+    /* Stde information CRC check word: not transmitted */
451
+    /* VQ encoded high frequency subbands: not transmitted */
452
+
453
+    /* LFE data */
454
+    if (c->lfe_channel) {
455
+        for (i = 0; i < 4 * SUBSUBFRAMES; i++)
456
+            put_sample7(c, lfe_data[i], LFE_BITS, c->lfe_scale_factor);
457
+        put_bits(&c->pb, 8, c->lfe_scale_factor);
458
+    }
459
+
460
+    /* Audio data (subsubframes) */
461
+
462
+    for (ss = 0; ss < SUBSUBFRAMES ; ss++)
463
+        for (ch = 0; ch < c->prim_channels; ch++)
464
+            for (sub = 0; sub < DCA_SUBBANDS; sub++)
465
+                for (i = 0; i < 8; i++)
466
+                    put_sample7(c, subband_data[ss * 8 + i][ch][sub], QUANTIZER_BITS, c->scale_factor[ch][sub]);
467
+
468
+    /* DSYNC */
469
+    put_bits(&c->pb, 16, 0xffff);
470
+}
471
+
472
+static void put_frame(DCAContext *c,
473
+                      int32_t subband_data[PCM_SAMPLES][MAX_CHANNELS][32],
474
+                      uint8_t *frame)
475
+{
476
+    int i;
477
+    init_put_bits(&c->pb, frame + DCA_HEADER_SIZE, DCA_MAX_FRAME_SIZE-DCA_HEADER_SIZE);
478
+
479
+    put_primary_audio_header(c);
480
+    for (i = 0; i < SUBFRAMES; i++)
481
+        put_subframe(c, &subband_data[SUBSUBFRAMES * 8 * i], i);
482
+
483
+    flush_put_bits(&c->pb);
484
+    c->frame_size = (put_bits_count(&c->pb) >> 3) + DCA_HEADER_SIZE;
485
+
486
+    init_put_bits(&c->pb, frame, DCA_HEADER_SIZE);
487
+    put_frame_header(c);
488
+    flush_put_bits(&c->pb);
489
+}
490
+
491
+static int encode_frame(AVCodecContext *avctx, uint8_t *frame,
492
+                        int buf_size, void *data)
493
+{
494
+    int i, k, channel;
495
+    DCAContext *c = avctx->priv_data;
496
+    int16_t *samples = data;
497
+    int real_channel = 0;
498
+
499
+    for (i = 0; i < PCM_SAMPLES; i ++) { /* i is the decimated sample number */
500
+        for (channel = 0; channel < c->prim_channels + 1; channel++) {
501
+            /* Get 32 PCM samples */
502
+            for (k = 0; k < 32; k++) { /* k is the sample number in a 32-sample block */
503
+                c->pcm[k] = samples[avctx->channels * (32 * i + k) + channel] << 16;
504
+            }
505
+            /* Put subband samples into the proper place */
506
+            real_channel = c->channel_order_tab[channel];
507
+            if (real_channel >= 0) {
508
+                qmf_decompose(c, c->pcm, &c->subband[i][real_channel][0], real_channel);
509
+            }
510
+        }
511
+    }
512
+
513
+    if (c->lfe_channel) {
514
+        for (i = 0; i < PCM_SAMPLES / 2; i++) {
515
+            for (k = 0; k < LFE_INTERPOLATION; k++) /* k is the sample number in a 32-sample block */
516
+                c->pcm[k] = samples[avctx->channels * (LFE_INTERPOLATION*i+k) + c->lfe_offset] << 16;
517
+            c->lfe_data[i] = lfe_downsample(c, c->pcm);
518
+        }
519
+    }
520
+
521
+    put_frame(c, c->subband, frame);
522
+
523
+    return c->frame_size;
524
+}
525
+
526
+static int encode_init(AVCodecContext *avctx)
527
+{
528
+    DCAContext *c = avctx->priv_data;
529
+    int i;
530
+
531
+    c->prim_channels = avctx->channels;
532
+    c->lfe_channel   = (avctx->channels == 3 || avctx->channels == 6);
533
+
534
+    switch (avctx->channel_layout) {
535
+    case AV_CH_LAYOUT_STEREO:       c->a_mode = 2; c->num_channel = 2; break;
536
+    case AV_CH_LAYOUT_5POINT0:      c->a_mode = 9; c->num_channel = 9; break;
537
+    case AV_CH_LAYOUT_5POINT1:      c->a_mode = 9; c->num_channel = 9; break;
538
+    case AV_CH_LAYOUT_5POINT0_BACK: c->a_mode = 9; c->num_channel = 9; break;
539
+    case AV_CH_LAYOUT_5POINT1_BACK: c->a_mode = 9; c->num_channel = 9; break;
540
+    default:
541
+    av_log(avctx, AV_LOG_ERROR,
542
+           "Only stereo, 5.0, 5.1 channel layouts supported at the moment!\n");
543
+    return AVERROR_PATCHWELCOME;
544
+    }
545
+
546
+    if (c->lfe_channel) {
547
+        init_lfe_fir();
548
+        c->prim_channels--;
549
+        c->channel_order_tab = dca_channel_reorder_lfe[c->a_mode];
550
+        c->lfe_state         = LFE_PRESENT;
551
+        c->lfe_offset        = dca_lfe_index[c->a_mode];
552
+    } else {
553
+        c->channel_order_tab = dca_channel_reorder_nolfe[c->a_mode];
554
+        c->lfe_state         = LFE_MISSING;
555
+    }
556
+
557
+    for (i = 0; i < 16; i++) {
558
+        if (dca_sample_rates[i] && (dca_sample_rates[i] == avctx->sample_rate))
559
+            break;
560
+    }
561
+    if (i == 16) {
562
+        av_log(avctx, AV_LOG_ERROR, "Sample rate %iHz not supported, only ", avctx->sample_rate);
563
+        for (i = 0; i < 16; i++)
564
+            av_log(avctx, AV_LOG_ERROR, "%d, ", dca_sample_rates[i]);
565
+        av_log(avctx, AV_LOG_ERROR, "supported.\n");
566
+        return -1;
567
+    }
568
+    c->sample_rate_code = i;
569
+
570
+    avctx->frame_size = 32 * PCM_SAMPLES;
571
+
572
+    if (!cos_table[127])
573
+        qmf_init();
574
+    return 0;
575
+}
576
+
577
+AVCodec ff_dca_encoder = {
578
+    .name           = "dca",
579
+    .type           = AVMEDIA_TYPE_AUDIO,
580
+    .id             = CODEC_ID_DTS,
581
+    .priv_data_size = sizeof(DCAContext),
582
+    .init           = encode_init,
583
+    .encode         = encode_frame,
584
+    .capabilities   = CODEC_CAP_EXPERIMENTAL,
585
+    .sample_fmts    = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
586
+};
0 587
new file mode 100644
... ...
@@ -0,0 +1,544 @@
0
+/*
1
+ * DCA encoder tables
2
+ * Copyright (C) 2008 Alexander E. Patrakov
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVCODEC_DCAENC_H
22
+#define AVCODEC_DCAENC_H
23
+
24
+/* This is a scaled version of the response of the reference decoder to
25
+   this vector of subband samples: ( 1.0 0.0 0.0 ... 0.0 )
26
+   */
27
+
28
+static const int32_t UnQMF[512] = {
29
+    7,
30
+    4,
31
+    -961,
32
+    -2844,
33
+    -8024,
34
+    -18978,
35
+    -32081,
36
+    -15635,
37
+    -16582,
38
+    -18359,
39
+    -17180,
40
+    -14868,
41
+    -11664,
42
+    -8051,
43
+    -4477,
44
+    -1327,
45
+    -1670,
46
+    -6019,
47
+    -11590,
48
+    -18030,
49
+    -24762,
50
+    -30965,
51
+    -35947,
52
+    -36145,
53
+    -37223,
54
+    -86311,
55
+    -57024,
56
+    -27215,
57
+    -11274,
58
+    -4684,
59
+    42,
60
+    108,
61
+    188,
62
+    250,
63
+    -1007,
64
+    -596,
65
+    -2289,
66
+    -12218,
67
+    -27191,
68
+    -124367,
69
+    -184256,
70
+    -250538,
71
+    -323499,
72
+    -397784,
73
+    -468855,
74
+    -532072,
75
+    -583000,
76
+    -618041,
77
+    -777916,
78
+    -783868,
79
+    -765968,
80
+    -724740,
81
+    -662468,
82
+    -583058,
83
+    -490548,
84
+    -401623,
85
+    -296090,
86
+    -73154,
87
+    -36711,
88
+    -7766,
89
+    -2363,
90
+    -4905,
91
+    2388,
92
+    2681,
93
+    5651,
94
+    4086,
95
+    71110,
96
+    139742,
97
+    188067,
98
+    151237,
99
+    101355,
100
+    309917,
101
+    343690,
102
+    358839,
103
+    357555,
104
+    334606,
105
+    289625,
106
+    224152,
107
+    142063,
108
+    48725,
109
+    74996,
110
+    238425,
111
+    411666,
112
+    584160,
113
+    744276,
114
+    880730,
115
+    983272,
116
+    1041933,
117
+    1054396,
118
+    789531,
119
+    851022,
120
+    864032,
121
+    675431,
122
+    418134,
123
+    35762,
124
+    66911,
125
+    103502,
126
+    136403,
127
+    -55147,
128
+    -245269,
129
+    -499595,
130
+    -808470,
131
+    -1136858,
132
+    -2010912,
133
+    -2581654,
134
+    -3151901,
135
+    -3696328,
136
+    -4196599,
137
+    -4633761,
138
+    -4993229,
139
+    -5262495,
140
+    -5436311,
141
+    -477650,
142
+    -901314,
143
+    -1308090,
144
+    -1677468,
145
+    -1985525,
146
+    -2212848,
147
+    -2341196,
148
+    -2373915,
149
+    -2269552,
150
+    -2620489,
151
+    -2173858,
152
+    -1629954,
153
+    -946595,
154
+    -193499,
155
+    1119459,
156
+    1138657,
157
+    1335311,
158
+    1126544,
159
+    2765033,
160
+    3139603,
161
+    3414913,
162
+    3599213,
163
+    3676363,
164
+    3448981,
165
+    3328726,
166
+    3111551,
167
+    2810887,
168
+    2428657,
169
+    1973684,
170
+    1457278,
171
+    893848,
172
+    300995,
173
+    -292521,
174
+    -867621,
175
+    -1404936,
176
+    -1871278,
177
+    -2229831,
178
+    -2440932,
179
+    -2462684,
180
+    -2255006,
181
+    -1768898,
182
+    -1079574,
183
+    82115,
184
+    1660302,
185
+    3660715,
186
+    6123610,
187
+    8329598,
188
+    11888744,
189
+    15722147,
190
+    19737089,
191
+    25647773,
192
+    31039399,
193
+    36868007,
194
+    43124253,
195
+    49737161,
196
+    56495958,
197
+    63668945,
198
+    71039511,
199
+    78540240,
200
+    86089058,
201
+    93600041,
202
+    100981151,
203
+    108136061,
204
+    114970055,
205
+    121718321,
206
+    127566038,
207
+    132774642,
208
+    137247294,
209
+    140894737,
210
+    143635018,
211
+    145395599,
212
+    146114032,
213
+    145742999,
214
+    144211606,
215
+    141594341,
216
+    137808404,
217
+    132914122,
218
+    126912246,
219
+    120243281,
220
+    112155281,
221
+    103338368,
222
+    93904953,
223
+    83439152,
224
+    72921548,
225
+    62192990,
226
+    51434918,
227
+    40894003,
228
+    30786726,
229
+    21384955,
230
+    12939112,
231
+    5718193,
232
+    -5790,
233
+    -3959261,
234
+    -5870978,
235
+    -5475538,
236
+    -2517061,
237
+    3247310,
238
+    12042937,
239
+    24076729,
240
+    39531397,
241
+    58562863,
242
+    81297002,
243
+    107826748,
244
+    138209187,
245
+    172464115,
246
+    210569037,
247
+    252468018,
248
+    298045453,
249
+    347168648,
250
+    399634888,
251
+    455137189,
252
+    513586535,
253
+    574537650,
254
+    637645129,
255
+    702597163,
256
+    768856566,
257
+    836022040,
258
+    903618096,
259
+    971159680,
260
+    1038137214,
261
+    1103987353,
262
+    1168195035,
263
+    1230223053,
264
+    1289539180,
265
+    1345620373,
266
+    1397957958,
267
+    1446063657,
268
+    1489474689,
269
+    1527740502,
270
+    1560502307,
271
+    1587383079,
272
+    1608071145,
273
+    1622301248,
274
+    1629859340,
275
+    1630584888,
276
+    1624373875,
277
+    1611178348,
278
+    1591018893,
279
+    1563948667,
280
+    1530105004,
281
+    1489673227,
282
+    1442904075,
283
+    1390107674,
284
+    1331590427,
285
+    1267779478,
286
+    1199115126,
287
+    1126053392,
288
+    1049146257,
289
+    968928307,
290
+    885965976,
291
+    800851610,
292
+    714186243,
293
+    626590147,
294
+    538672486,
295
+    451042824,
296
+    364299927,
297
+    279026812,
298
+    195785029,
299
+    115109565,
300
+    37503924,
301
+    -36564551,
302
+    -106668063,
303
+    -172421668,
304
+    -233487283,
305
+    -289575706,
306
+    -340448569,
307
+    -385919511,
308
+    -425854915,
309
+    -460174578,
310
+    -488840702,
311
+    -511893328,
312
+    -529405118,
313
+    -541489888,
314
+    -548312207,
315
+    -550036471,
316
+    -547005316,
317
+    -539436808,
318
+    -527630488,
319
+    -512084785,
320
+    -492941605,
321
+    -470665204,
322
+    -445668379,
323
+    -418328829,
324
+    -389072810,
325
+    -358293846,
326
+    -326396227,
327
+    -293769619,
328
+    -260792276,
329
+    -227825056,
330
+    -195208961,
331
+    -163262121,
332
+    -132280748,
333
+    -102533727,
334
+    -74230062,
335
+    -47600637,
336
+    -22817785,
337
+    -25786,
338
+    20662895,
339
+    39167253,
340
+    55438413,
341
+    69453741,
342
+    81242430,
343
+    90795329,
344
+    98213465,
345
+    103540643,
346
+    106917392,
347
+    108861938,
348
+    108539682,
349
+    106780704,
350
+    103722568,
351
+    99043289,
352
+    93608686,
353
+    87266209,
354
+    80212203,
355
+    72590022,
356
+    64603428,
357
+    56362402,
358
+    48032218,
359
+    39749162,
360
+    31638971,
361
+    23814664,
362
+    16376190,
363
+    9409836,
364
+    2988017,
365
+    -2822356,
366
+    -7976595,
367
+    -12454837,
368
+    -16241147,
369
+    -19331944,
370
+    -21735011,
371
+    -23468284,
372
+    -24559822,
373
+    -25042936,
374
+    -25035583,
375
+    -24429587,
376
+    -23346408,
377
+    -21860411,
378
+    -20015718,
379
+    -17025330,
380
+    -14968728,
381
+    -12487138,
382
+    -9656319,
383
+    -7846681,
384
+    -5197816,
385
+    -2621904,
386
+    -144953,
387
+    2144746,
388
+    3990570,
389
+    5845884,
390
+    7454650,
391
+    8820394,
392
+    9929891,
393
+    10784445,
394
+    11390921,
395
+    11762056,
396
+    11916017,
397
+    12261189,
398
+    12117604,
399
+    11815303,
400
+    11374622,
401
+    10815301,
402
+    10157241,
403
+    9418799,
404
+    8629399,
405
+    7780776,
406
+    7303680,
407
+    6353499,
408
+    5392738,
409
+    4457895,
410
+    3543062,
411
+    1305978,
412
+    1402521,
413
+    1084092,
414
+    965652,
415
+    -151008,
416
+    -666667,
417
+    -1032157,
418
+    -1231475,
419
+    -1319043,
420
+    -1006023,
421
+    -915720,
422
+    -773426,
423
+    -612377,
424
+    -445864,
425
+    -291068,
426
+    -161337,
427
+    -66484,
428
+    -11725,
429
+    133453,
430
+    388184,
431
+    615856,
432
+    804033,
433
+    942377,
434
+    1022911,
435
+    1041247,
436
+    995854,
437
+    891376,
438
+    572246,
439
+    457992,
440
+    316365,
441
+    172738,
442
+    43037,
443
+    -117662,
444
+    -98542,
445
+    -70279,
446
+    -41458,
447
+    -535790,
448
+    -959038,
449
+    -1364456,
450
+    -1502265,
451
+    -1568530,
452
+    -2378681,
453
+    -2701111,
454
+    -2976407,
455
+    -3182552,
456
+    -3314415,
457
+    -3366600,
458
+    -3337701,
459
+    -3232252,
460
+    -3054999,
461
+    1984841,
462
+    1925903,
463
+    1817377,
464
+    1669153,
465
+    1490069,
466
+    1292040,
467
+    1086223,
468
+    890983,
469
+    699163,
470
+    201358,
471
+    266971,
472
+    296990,
473
+    198419,
474
+    91119,
475
+    4737,
476
+    5936,
477
+    2553,
478
+    2060,
479
+    -3828,
480
+    -1664,
481
+    -4917,
482
+    -20796,
483
+    -36822,
484
+    -131247,
485
+    -154923,
486
+    -162055,
487
+    -161354,
488
+    -148762,
489
+    -125754,
490
+    -94473,
491
+    -57821,
492
+    -19096,
493
+    15172,
494
+    43004,
495
+    65624,
496
+    81354,
497
+    89325,
498
+    89524,
499
+    82766,
500
+    71075,
501
+    55128,
502
+    13686,
503
+    6921,
504
+    1449,
505
+    420,
506
+    785,
507
+    -215,
508
+    -179,
509
+    -113,
510
+    -49,
511
+    6002,
512
+    16007,
513
+    42978,
514
+    100662,
515
+    171472,
516
+    83975,
517
+    93702,
518
+    108813,
519
+    111893,
520
+    110272,
521
+    103914,
522
+    93973,
523
+    81606,
524
+    68041,
525
+    -54058,
526
+    -60695,
527
+    -65277,
528
+    -67224,
529
+    -66213,
530
+    -62082,
531
+    -55574,
532
+    -42988,
533
+    -35272,
534
+    -63735,
535
+    -33501,
536
+    -12671,
537
+    -4038,
538
+    -1232,
539
+    5,
540
+    7
541
+};
542
+
543
+#endif /* AVCODEC_DCAENC_H */