Browse code

tta encoder

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2013/05/27 22:52:36
Showing 7 changed files
... ...
@@ -60,6 +60,7 @@ version <next>:
60 60
 - support for WavPack muxing (raw and in Matroska)
61 61
 - XVideo output device
62 62
 - vignette filter
63
+- True Audio (TTA) encoder
63 64
 
64 65
 
65 66
 version 1.2:
... ...
@@ -231,6 +231,7 @@ Codecs:
231 231
   truespeech.c                          Kostya Shishkov
232 232
   tscc.c                                Kostya Shishkov
233 233
   tta.c                                 Alex Beregszaszi, Jaikrishnan Menon
234
+  ttaenc.c                              Paul B Mahol
234 235
   txd.c                                 Ivo van Poorten
235 236
   ulti*                                 Kostya Shishkov
236 237
   v410*.c                               Derek Buitenhuis
... ...
@@ -899,7 +899,7 @@ following image formats are supported:
899 899
 @item Speex                  @tab  E  @tab  E
900 900
     @tab supported through external library libspeex
901 901
 @item TAK (Tom's lossless Audio Kompressor)  @tab     @tab  X
902
-@item True Audio (TTA)       @tab     @tab  X
902
+@item True Audio (TTA)       @tab  X  @tab  X
903 903
 @item TrueHD                 @tab     @tab  X
904 904
     @tab Used in HD-DVD and Blu-Ray discs.
905 905
 @item TwinVQ (VQF flavor)    @tab     @tab  X
... ...
@@ -430,6 +430,7 @@ OBJS-$(CONFIG_TRUESPEECH_DECODER)      += truespeech.o
430 430
 OBJS-$(CONFIG_TSCC_DECODER)            += tscc.o msrledec.o
431 431
 OBJS-$(CONFIG_TSCC2_DECODER)           += tscc2.o
432 432
 OBJS-$(CONFIG_TTA_DECODER)             += tta.o ttadata.o
433
+OBJS-$(CONFIG_TTA_ENCODER)             += ttaenc.o ttadata.o
433 434
 OBJS-$(CONFIG_TWINVQ_DECODER)          += twinvq.o
434 435
 OBJS-$(CONFIG_TXD_DECODER)             += txd.o s3tc.o
435 436
 OBJS-$(CONFIG_ULTI_DECODER)            += ulti.o
... ...
@@ -365,7 +365,7 @@ void avcodec_register_all(void)
365 365
     REGISTER_DECODER(TAK,               tak);
366 366
     REGISTER_DECODER(TRUEHD,            truehd);
367 367
     REGISTER_DECODER(TRUESPEECH,        truespeech);
368
-    REGISTER_DECODER(TTA,               tta);
368
+    REGISTER_ENCDEC (TTA,               tta);
369 369
     REGISTER_DECODER(TWINVQ,            twinvq);
370 370
     REGISTER_DECODER(VMDAUDIO,          vmdaudio);
371 371
     REGISTER_ENCDEC (VORBIS,            vorbis);
372 372
new file mode 100644
... ...
@@ -0,0 +1,232 @@
0
+/*
1
+ * TTA (The Lossless True Audio) encoder
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#define BITSTREAM_WRITER_LE
21
+#include "ttadata.h"
22
+#include "avcodec.h"
23
+#include "put_bits.h"
24
+#include "internal.h"
25
+#include "libavutil/crc.h"
26
+
27
+typedef struct TTAEncContext {
28
+    const AVCRC *crc_table;
29
+    int bps;
30
+    TTAChannel *ch_ctx;
31
+} TTAEncContext;
32
+
33
+static av_cold int tta_encode_init(AVCodecContext *avctx)
34
+{
35
+    TTAEncContext *s = avctx->priv_data;
36
+
37
+    s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
38
+
39
+    switch (avctx->sample_fmt) {
40
+    case AV_SAMPLE_FMT_U8:
41
+        avctx->bits_per_raw_sample = 8;
42
+        break;
43
+    case AV_SAMPLE_FMT_S16:
44
+        avctx->bits_per_raw_sample = 16;
45
+        break;
46
+    case AV_SAMPLE_FMT_S32:
47
+        if (avctx->bits_per_raw_sample > 24)
48
+            av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
49
+        avctx->bits_per_raw_sample = 24;
50
+    }
51
+
52
+    s->bps = avctx->bits_per_raw_sample >> 3;
53
+    avctx->frame_size = 256 * avctx->sample_rate / 245;
54
+
55
+    s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
56
+    if (!s->ch_ctx)
57
+        return AVERROR(ENOMEM);
58
+
59
+    return 0;
60
+}
61
+
62
+static inline void ttafilter_process(TTAFilter *c, int32_t *in)
63
+{
64
+    register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
65
+
66
+    if (c->error < 0) {
67
+        qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
68
+        qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
69
+    } else if (c->error > 0) {
70
+        qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
71
+        qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
72
+    }
73
+
74
+    sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
75
+           dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
76
+
77
+    dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
78
+    dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
79
+
80
+    dx[4] = ((dl[4] >> 30) | 1);
81
+    dx[5] = ((dl[5] >> 30) | 2) & ~1;
82
+    dx[6] = ((dl[6] >> 30) | 2) & ~1;
83
+    dx[7] = ((dl[7] >> 30) | 4) & ~3;
84
+
85
+    dl[4] = -dl[5]; dl[5] = -dl[6];
86
+    dl[6] = *in - dl[7]; dl[7] = *in;
87
+    dl[5] += dl[6]; dl[4] += dl[5];
88
+
89
+    *in -= (sum >> c->shift);
90
+    c->error = *in;
91
+}
92
+
93
+static int32_t get_sample(const AVFrame *frame, int sample,
94
+                          enum AVSampleFormat format)
95
+{
96
+    int32_t ret;
97
+
98
+    if (format == AV_SAMPLE_FMT_U8) {
99
+        ret = frame->data[0][sample] - 0x80;
100
+    } else if (format == AV_SAMPLE_FMT_S16) {
101
+        const int16_t *ptr = (const int16_t *)frame->data[0];
102
+        ret = ptr[sample];
103
+    } else {
104
+        const int32_t *ptr = (const int32_t *)frame->data[0];
105
+        ret = ptr[sample] >> 8;
106
+    }
107
+
108
+    return ret;
109
+}
110
+
111
+static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
112
+                            const AVFrame *frame, int *got_packet_ptr)
113
+{
114
+    TTAEncContext *s = avctx->priv_data;
115
+    PutBitContext pb;
116
+    int ret, i, out_bytes, cur_chan = 0, res = 0, samples = 0;
117
+
118
+    if ((ret = ff_alloc_packet2(avctx, avpkt, frame->nb_samples * 2 * s->bps)) < 0)
119
+        return ret;
120
+    init_put_bits(&pb, avpkt->data, avpkt->size);
121
+
122
+    // init per channel states
123
+    for (i = 0; i < avctx->channels; i++) {
124
+        s->ch_ctx[i].predictor = 0;
125
+        ff_tta_filter_init(&s->ch_ctx[i].filter, ff_tta_filter_configs[s->bps - 1]);
126
+        ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
127
+    }
128
+
129
+    for (i = 0; i < frame->nb_samples * avctx->channels; i++) {
130
+        TTAChannel *c = &s->ch_ctx[cur_chan];
131
+        TTAFilter *filter = &c->filter;
132
+        TTARice *rice = &c->rice;
133
+        uint32_t k, unary, outval;
134
+        int32_t value, temp;
135
+
136
+        value = get_sample(frame, samples++, avctx->sample_fmt);
137
+
138
+        if (avctx->channels > 1) {
139
+            if (cur_chan < avctx->channels - 1)
140
+                value  = res = get_sample(frame, samples, avctx->sample_fmt) - value;
141
+            else
142
+                value -= res / 2;
143
+        }
144
+
145
+        temp = value;
146
+#define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
147
+        switch (s->bps) {
148
+        case 1: value -= PRED(c->predictor, 4); break;
149
+        case 2:
150
+        case 3: value -= PRED(c->predictor, 5); break;
151
+        }
152
+        c->predictor = temp;
153
+
154
+        ttafilter_process(filter, &value);
155
+        outval = (value > 0) ? (value << 1) - 1: -value << 1;
156
+
157
+        k = rice->k0;
158
+
159
+        rice->sum0 += outval - (rice->sum0 >> 4);
160
+        if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
161
+            rice->k0--;
162
+        else if (rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
163
+            rice->k0++;
164
+
165
+        if (outval >= ff_tta_shift_1[k]) {
166
+            outval -= ff_tta_shift_1[k];
167
+            k = rice->k1;
168
+
169
+            rice->sum1 += outval - (rice->sum1 >> 4);
170
+            if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
171
+                rice->k1--;
172
+            else if (rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
173
+                rice->k1++;
174
+
175
+            unary = 1 + (outval >> k);
176
+            do {
177
+                if (unary > 31) {
178
+                    put_bits(&pb, 31, 0x7FFFFFFF);
179
+                    unary -= 31;
180
+                } else {
181
+                    put_bits(&pb, unary, (1 << unary) - 1);
182
+                    unary = 0;
183
+                }
184
+            } while (unary);
185
+        }
186
+
187
+        put_bits(&pb, 1, 0);
188
+
189
+        if (k)
190
+            put_bits(&pb, k, outval & (ff_tta_shift_1[k] - 1));
191
+
192
+        if (cur_chan < avctx->channels - 1)
193
+            cur_chan++;
194
+        else
195
+            cur_chan = 0;
196
+    }
197
+
198
+    flush_put_bits(&pb);
199
+    out_bytes = put_bits_count(&pb) >> 3;
200
+    put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX);
201
+    flush_put_bits(&pb);
202
+
203
+    avpkt->pts      = frame->pts;
204
+    avpkt->size     = out_bytes + 4;
205
+    avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
206
+    *got_packet_ptr = 1;
207
+    return 0;
208
+}
209
+
210
+static av_cold int tta_encode_close(AVCodecContext *avctx)
211
+{
212
+    TTAEncContext *s = avctx->priv_data;
213
+    av_freep(&s->ch_ctx);
214
+    return 0;
215
+}
216
+
217
+AVCodec ff_tta_encoder = {
218
+    .name           = "tta",
219
+    .type           = AVMEDIA_TYPE_AUDIO,
220
+    .id             = AV_CODEC_ID_TTA,
221
+    .priv_data_size = sizeof(TTAEncContext),
222
+    .init           = tta_encode_init,
223
+    .close          = tta_encode_close,
224
+    .encode2        = tta_encode_frame,
225
+    .capabilities   = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_LOSSLESS,
226
+    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8,
227
+                                                     AV_SAMPLE_FMT_S16,
228
+                                                     AV_SAMPLE_FMT_S32,
229
+                                                     AV_SAMPLE_FMT_NONE },
230
+    .long_name      = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
231
+};
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 55
32
-#define LIBAVCODEC_VERSION_MINOR  13
32
+#define LIBAVCODEC_VERSION_MINOR  14
33 33
 #define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \