]> git.sesse.net Git - ffmpeg/blob - libavcodec/tta.c
Merge commit '46ce9ded96ffcb798b03da894cdb5fdac376a6ee'
[ffmpeg] / libavcodec / tta.c
1 /*
2  * TTA (The Lossless True Audio) decoder
3  * Copyright (c) 2006 Alex Beregszaszi
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * TTA (The Lossless True Audio) decoder
25  * @see http://www.true-audio.com/
26  * @see http://tta.corecodec.org/
27  * @author Alex Beregszaszi
28  */
29
30 #define BITSTREAM_READER_LE
31 #include <limits.h>
32 #include "ttadata.h"
33 #include "avcodec.h"
34 #include "get_bits.h"
35 #include "unary.h"
36 #include "internal.h"
37 #include "libavutil/crc.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavutil/opt.h"
40
41 #define FORMAT_SIMPLE    1
42 #define FORMAT_ENCRYPTED 2
43
44 typedef struct TTAContext {
45     AVClass *class;
46     AVCodecContext *avctx;
47     const AVCRC *crc_table;
48
49     int format, channels, bps;
50     unsigned data_length;
51     int frame_length, last_frame_length;
52
53     int32_t *decode_buffer;
54
55     uint8_t crc_pass[8];
56     uint8_t *pass;
57     TTAChannel *ch_ctx;
58 } TTAContext;
59
60 static inline void ttafilter_process(TTAFilter *c, int32_t *in)
61 {
62     register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
63
64     if (c->error < 0) {
65         qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
66         qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
67     } else if (c->error > 0) {
68         qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
69         qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
70     }
71
72     sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
73            dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
74
75     dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
76     dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
77
78     dx[4] = ((dl[4] >> 30) | 1);
79     dx[5] = ((dl[5] >> 30) | 2) & ~1;
80     dx[6] = ((dl[6] >> 30) | 2) & ~1;
81     dx[7] = ((dl[7] >> 30) | 4) & ~3;
82
83     c->error = *in;
84     *in += (sum >> c->shift);
85
86     dl[4] = -dl[5]; dl[5] = -dl[6];
87     dl[6] = *in - dl[7]; dl[7] = *in;
88     dl[5] += dl[6]; dl[4] += dl[5];
89 }
90
91 static const int64_t tta_channel_layouts[7] = {
92     AV_CH_LAYOUT_STEREO,
93     AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
94     AV_CH_LAYOUT_QUAD,
95     0,
96     AV_CH_LAYOUT_5POINT1_BACK,
97     AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
98     AV_CH_LAYOUT_7POINT1_WIDE
99 };
100
101 static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
102 {
103     uint32_t crc, CRC;
104
105     CRC = AV_RL32(buf + buf_size);
106     crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
107     if (CRC != (crc ^ 0xFFFFFFFFU)) {
108         av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
109         return AVERROR_INVALIDDATA;
110     }
111
112     return 0;
113 }
114
115 static uint64_t tta_check_crc64(uint8_t *pass)
116 {
117     uint64_t crc = UINT64_MAX, poly = 0x42F0E1EBA9EA3693U;
118     uint8_t *end = pass + strlen(pass);
119     int i;
120
121     while (pass < end) {
122         crc ^= (uint64_t)*pass++ << 56;
123         for (i = 0; i < 8; i++)
124             crc = (crc << 1) ^ (poly & (((int64_t) crc) >> 63));
125     }
126
127     return crc ^ UINT64_MAX;
128 }
129
130 static av_cold int tta_decode_init(AVCodecContext * avctx)
131 {
132     TTAContext *s = avctx->priv_data;
133     GetBitContext gb;
134     int total_frames;
135
136     s->avctx = avctx;
137
138     // 30bytes includes TTA1 header
139     if (avctx->extradata_size < 22)
140         return AVERROR_INVALIDDATA;
141
142     s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
143     init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
144     if (show_bits_long(&gb, 32) == AV_RL32("TTA1")) {
145         /* signature */
146         skip_bits_long(&gb, 32);
147
148         s->format = get_bits(&gb, 16);
149         if (s->format > 2) {
150             av_log(avctx, AV_LOG_ERROR, "Invalid format\n");
151             return AVERROR_INVALIDDATA;
152         }
153         if (s->format == FORMAT_ENCRYPTED) {
154             if (!s->pass) {
155                 av_log(avctx, AV_LOG_ERROR, "Missing password for encrypted stream. Please use the -password option\n");
156                 return AVERROR(EINVAL);
157             }
158             AV_WL64(s->crc_pass, tta_check_crc64(s->pass));
159         }
160         avctx->channels = s->channels = get_bits(&gb, 16);
161         if (s->channels > 1 && s->channels < 9)
162             avctx->channel_layout = tta_channel_layouts[s->channels-2];
163         avctx->bits_per_raw_sample = get_bits(&gb, 16);
164         s->bps = (avctx->bits_per_raw_sample + 7) / 8;
165         avctx->sample_rate = get_bits_long(&gb, 32);
166         s->data_length = get_bits_long(&gb, 32);
167         skip_bits_long(&gb, 32); // CRC32 of header
168
169         if (s->channels == 0) {
170             av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
171             return AVERROR_INVALIDDATA;
172         } else if (avctx->sample_rate == 0) {
173             av_log(avctx, AV_LOG_ERROR, "Invalid samplerate\n");
174             return AVERROR_INVALIDDATA;
175         }
176
177         switch(s->bps) {
178         case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
179         case 2:
180             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
181             break;
182         case 3:
183             avctx->sample_fmt = AV_SAMPLE_FMT_S32;
184             break;
185         //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
186         default:
187             av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
188             return AVERROR_INVALIDDATA;
189         }
190
191         // prevent overflow
192         if (avctx->sample_rate > 0x7FFFFFu) {
193             av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
194             return AVERROR(EINVAL);
195         }
196         s->frame_length = 256 * avctx->sample_rate / 245;
197
198         s->last_frame_length = s->data_length % s->frame_length;
199         total_frames = s->data_length / s->frame_length +
200                        (s->last_frame_length ? 1 : 0);
201
202         av_log(avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
203             s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
204             avctx->block_align);
205         av_log(avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
206             s->data_length, s->frame_length, s->last_frame_length, total_frames);
207
208         if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
209             av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
210             return AVERROR_INVALIDDATA;
211         }
212
213         if (s->bps < 3) {
214             s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
215             if (!s->decode_buffer)
216                 return AVERROR(ENOMEM);
217         } else
218             s->decode_buffer = NULL;
219         s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
220         if (!s->ch_ctx) {
221             av_freep(&s->decode_buffer);
222             return AVERROR(ENOMEM);
223         }
224     } else {
225         av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
226         return AVERROR_INVALIDDATA;
227     }
228
229     return 0;
230 }
231
232 static int tta_decode_frame(AVCodecContext *avctx, void *data,
233                             int *got_frame_ptr, AVPacket *avpkt)
234 {
235     AVFrame *frame     = data;
236     const uint8_t *buf = avpkt->data;
237     int buf_size = avpkt->size;
238     TTAContext *s = avctx->priv_data;
239     GetBitContext gb;
240     int i, ret;
241     int cur_chan = 0, framelen = s->frame_length;
242     int32_t *p;
243
244     if (avctx->err_recognition & AV_EF_CRCCHECK) {
245         if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4))
246             return AVERROR_INVALIDDATA;
247     }
248
249     if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
250         return ret;
251
252     /* get output buffer */
253     frame->nb_samples = framelen;
254     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
255         return ret;
256
257     // decode directly to output buffer for 24-bit sample format
258     if (s->bps == 3)
259         s->decode_buffer = (int32_t *)frame->data[0];
260
261     // init per channel states
262     for (i = 0; i < s->channels; i++) {
263         TTAFilter *filter = &s->ch_ctx[i].filter;
264         s->ch_ctx[i].predictor = 0;
265         ff_tta_filter_init(filter, ff_tta_filter_configs[s->bps-1]);
266         if (s->format == FORMAT_ENCRYPTED) {
267             int i;
268             for (i = 0; i < 8; i++)
269                 filter->qm[i] = sign_extend(s->crc_pass[i], 8);
270         }
271         ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
272     }
273
274     i = 0;
275     for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
276         int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
277         TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
278         TTARice *rice = &s->ch_ctx[cur_chan].rice;
279         uint32_t unary, depth, k;
280         int32_t value;
281
282         unary = get_unary(&gb, 0, get_bits_left(&gb));
283
284         if (unary == 0) {
285             depth = 0;
286             k = rice->k0;
287         } else {
288             depth = 1;
289             k = rice->k1;
290             unary--;
291         }
292
293         if (get_bits_left(&gb) < k) {
294             ret = AVERROR_INVALIDDATA;
295             goto error;
296         }
297
298         if (k) {
299             if (k > MIN_CACHE_BITS) {
300                 ret = AVERROR_INVALIDDATA;
301                 goto error;
302             }
303             value = (unary << k) + get_bits(&gb, k);
304         } else
305             value = unary;
306
307         // FIXME: copy paste from original
308         switch (depth) {
309         case 1:
310             rice->sum1 += value - (rice->sum1 >> 4);
311             if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
312                 rice->k1--;
313             else if(rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
314                 rice->k1++;
315             value += ff_tta_shift_1[rice->k0];
316         default:
317             rice->sum0 += value - (rice->sum0 >> 4);
318             if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
319                 rice->k0--;
320             else if(rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
321                 rice->k0++;
322         }
323
324         // extract coded value
325         *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
326
327         // run hybrid filter
328         ttafilter_process(filter, p);
329
330         // fixed order prediction
331 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
332         switch (s->bps) {
333         case 1: *p += PRED(*predictor, 4); break;
334         case 2:
335         case 3: *p += PRED(*predictor, 5); break;
336         case 4: *p +=      *predictor;     break;
337         }
338         *predictor = *p;
339
340         // flip channels
341         if (cur_chan < (s->channels-1))
342             cur_chan++;
343         else {
344             // decorrelate in case of multiple channels
345             if (s->channels > 1) {
346                 int32_t *r = p - 1;
347                 for (*p += *r / 2; r > p - s->channels; r--)
348                     *r = *(r + 1) - *r;
349             }
350             cur_chan = 0;
351             i++;
352             // check for last frame
353             if (i == s->last_frame_length && get_bits_left(&gb) / 8 == 4) {
354                 frame->nb_samples = framelen = s->last_frame_length;
355                 break;
356             }
357         }
358     }
359
360     align_get_bits(&gb);
361     if (get_bits_left(&gb) < 32) {
362         ret = AVERROR_INVALIDDATA;
363         goto error;
364     }
365     skip_bits_long(&gb, 32); // frame crc
366
367     // convert to output buffer
368     switch (s->bps) {
369     case 1: {
370         uint8_t *samples = (uint8_t *)frame->data[0];
371         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
372             *samples++ = *p + 0x80;
373         break;
374         }
375     case 2: {
376         int16_t *samples = (int16_t *)frame->data[0];
377         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
378             *samples++ = *p;
379         break;
380         }
381     case 3: {
382         // shift samples for 24-bit sample format
383         int32_t *samples = (int32_t *)frame->data[0];
384         for (i = 0; i < framelen * s->channels; i++)
385             *samples++ <<= 8;
386         // reset decode buffer
387         s->decode_buffer = NULL;
388         break;
389         }
390     }
391
392     *got_frame_ptr = 1;
393
394     return buf_size;
395 error:
396     // reset decode buffer
397     if (s->bps == 3)
398         s->decode_buffer = NULL;
399     return ret;
400 }
401
402 static av_cold int tta_decode_close(AVCodecContext *avctx) {
403     TTAContext *s = avctx->priv_data;
404
405     if (s->bps < 3)
406         av_free(s->decode_buffer);
407     s->decode_buffer = NULL;
408     av_freep(&s->ch_ctx);
409
410     return 0;
411 }
412
413 #define OFFSET(x) offsetof(TTAContext, x)
414 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
415 static const AVOption options[] = {
416     { "password", "Set decoding password", OFFSET(pass), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
417     { NULL },
418 };
419
420 static const AVClass tta_decoder_class = {
421     .class_name = "TTA Decoder",
422     .item_name  = av_default_item_name,
423     .option     = options,
424     .version    = LIBAVUTIL_VERSION_INT,
425 };
426
427 AVCodec ff_tta_decoder = {
428     .name           = "tta",
429     .type           = AVMEDIA_TYPE_AUDIO,
430     .id             = AV_CODEC_ID_TTA,
431     .priv_data_size = sizeof(TTAContext),
432     .init           = tta_decode_init,
433     .close          = tta_decode_close,
434     .decode         = tta_decode_frame,
435     .capabilities   = CODEC_CAP_DR1,
436     .long_name      = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
437     .priv_class     = &tta_decoder_class,
438 };