swscale: make access to filter data conditional on filter type.
[libav.git] / libavcodec / tta.c
CommitLineData
a24c4a2a
AB
1/*
2 * TTA (The Lossless True Audio) decoder
3 * Copyright (c) 2006 Alex Beregszaszi
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
a24c4a2a
AB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
a24c4a2a 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
a24c4a2a
AB
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
e5a389a1 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a24c4a2a
AB
20 */
21
22/**
ba87f080 23 * @file
a24c4a2a 24 * TTA (The Lossless True Audio) decoder
ad4cd0c2
DB
25 * @see http://www.true-audio.com/
26 * @see http://tta.corecodec.org/
a24c4a2a 27 * @author Alex Beregszaszi
a24c4a2a
AB
28 */
29
aaf47bcd 30#define BITSTREAM_READER_LE
a24c4a2a
AB
31//#define DEBUG
32#include <limits.h>
33#include "avcodec.h"
9106a698 34#include "get_bits.h"
2af3dc86 35#include "libavutil/crc.h"
a24c4a2a 36
b16960a8
JR
37#define FORMAT_SIMPLE 1
38#define FORMAT_ENCRYPTED 2
a24c4a2a 39
5113b3bd
MR
40#define MAX_ORDER 16
41typedef struct TTAFilter {
42 int32_t shift, round, error, mode;
43 int32_t qm[MAX_ORDER];
44 int32_t dx[MAX_ORDER];
45 int32_t dl[MAX_ORDER];
46} TTAFilter;
47
48typedef struct TTARice {
49 uint32_t k0, k1, sum0, sum1;
50} TTARice;
51
52typedef struct TTAChannel {
53 int32_t predictor;
54 TTAFilter filter;
55 TTARice rice;
56} TTAChannel;
57
a24c4a2a
AB
58typedef struct TTAContext {
59 AVCodecContext *avctx;
0eea2129 60 AVFrame frame;
a24c4a2a 61 GetBitContext gb;
2af3dc86 62 const AVCRC *crc_table;
a24c4a2a 63
b16960a8 64 int format, channels, bps, data_length;
a24c4a2a
AB
65 int frame_length, last_frame_length, total_frames;
66
79a32e3b 67 int32_t *decode_buffer;
5113b3bd 68
c058dc22 69 TTAChannel *ch_ctx;
a24c4a2a
AB
70} TTAContext;
71
79a32e3b 72static const uint32_t shift_1[] = {
a24c4a2a
AB
73 0x00000001, 0x00000002, 0x00000004, 0x00000008,
74 0x00000010, 0x00000020, 0x00000040, 0x00000080,
75 0x00000100, 0x00000200, 0x00000400, 0x00000800,
76 0x00001000, 0x00002000, 0x00004000, 0x00008000,
77 0x00010000, 0x00020000, 0x00040000, 0x00080000,
78 0x00100000, 0x00200000, 0x00400000, 0x00800000,
79 0x01000000, 0x02000000, 0x04000000, 0x08000000,
80 0x10000000, 0x20000000, 0x40000000, 0x80000000,
81 0x80000000, 0x80000000, 0x80000000, 0x80000000,
82 0x80000000, 0x80000000, 0x80000000, 0x80000000
83};
84
cf2baeb3 85static const uint32_t * const shift_16 = shift_1 + 4;
a24c4a2a 86
cf2baeb3 87static const int32_t ttafilter_configs[4][2] = {
a24c4a2a
AB
88 {10, 1},
89 {9, 1},
90 {10, 1},
91 {12, 0}
92};
93
79a32e3b 94static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) {
a24c4a2a
AB
95 memset(c, 0, sizeof(TTAFilter));
96 c->shift = shift;
97 c->round = shift_1[shift-1];
98// c->round = 1 << (shift - 1);
99 c->mode = mode;
100}
101
102// FIXME: copy paste from original
79a32e3b 103static inline void memshl(register int32_t *a, register int32_t *b) {
a24c4a2a
AB
104 *a++ = *b++;
105 *a++ = *b++;
106 *a++ = *b++;
107 *a++ = *b++;
108 *a++ = *b++;
109 *a++ = *b++;
110 *a++ = *b++;
111 *a = *b;
112}
113
114// FIXME: copy paste from original
115// mode=1 encoder, mode=0 decoder
79a32e3b
BL
116static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) {
117 register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
a24c4a2a
AB
118
119 if (!c->error) {
120 sum += *dl++ * *qm, qm++;
121 sum += *dl++ * *qm, qm++;
122 sum += *dl++ * *qm, qm++;
123 sum += *dl++ * *qm, qm++;
124 sum += *dl++ * *qm, qm++;
125 sum += *dl++ * *qm, qm++;
126 sum += *dl++ * *qm, qm++;
127 sum += *dl++ * *qm, qm++;
128 dx += 8;
129 } else if(c->error < 0) {
130 sum += *dl++ * (*qm -= *dx++), qm++;
131 sum += *dl++ * (*qm -= *dx++), qm++;
132 sum += *dl++ * (*qm -= *dx++), qm++;
133 sum += *dl++ * (*qm -= *dx++), qm++;
134 sum += *dl++ * (*qm -= *dx++), qm++;
135 sum += *dl++ * (*qm -= *dx++), qm++;
136 sum += *dl++ * (*qm -= *dx++), qm++;
137 sum += *dl++ * (*qm -= *dx++), qm++;
138 } else {
139 sum += *dl++ * (*qm += *dx++), qm++;
140 sum += *dl++ * (*qm += *dx++), qm++;
141 sum += *dl++ * (*qm += *dx++), qm++;
142 sum += *dl++ * (*qm += *dx++), qm++;
143 sum += *dl++ * (*qm += *dx++), qm++;
144 sum += *dl++ * (*qm += *dx++), qm++;
145 sum += *dl++ * (*qm += *dx++), qm++;
146 sum += *dl++ * (*qm += *dx++), qm++;
147 }
148
149 *(dx-0) = ((*(dl-1) >> 30) | 1) << 2;
150 *(dx-1) = ((*(dl-2) >> 30) | 1) << 1;
151 *(dx-2) = ((*(dl-3) >> 30) | 1) << 1;
152 *(dx-3) = ((*(dl-4) >> 30) | 1);
153
154 // compress
155 if (mode) {
156 *dl = *in;
157 *in -= (sum >> c->shift);
158 c->error = *in;
159 } else {
160 c->error = *in;
161 *in += (sum >> c->shift);
162 *dl = *in;
163 }
164
165 if (c->mode) {
166 *(dl-1) = *dl - *(dl-1);
167 *(dl-2) = *(dl-1) - *(dl-2);
168 *(dl-3) = *(dl-2) - *(dl-3);
169 }
170
171 memshl(c->dl, c->dl + 1);
172 memshl(c->dx, c->dx + 1);
173}
174
79a32e3b 175static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
a24c4a2a
AB
176{
177 c->k0 = k0;
178 c->k1 = k1;
179 c->sum0 = shift_16[k0];
180 c->sum1 = shift_16[k1];
181}
182
183static int tta_get_unary(GetBitContext *gb)
184{
185 int ret = 0;
186
187 // count ones
35f9d8c2 188 while (get_bits_left(gb) > 0 && get_bits1(gb))
a24c4a2a
AB
189 ret++;
190 return ret;
191}
192
2af3dc86
PM
193static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
194{
195 uint32_t crc, CRC;
196
197 CRC = AV_RL32(buf + buf_size);
198 crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
199 if (CRC != (crc ^ 0xFFFFFFFFU)) {
200 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
201 return AVERROR_INVALIDDATA;
202 }
203
204 return 0;
205}
206
98a6fff9 207static av_cold int tta_decode_init(AVCodecContext * avctx)
a24c4a2a
AB
208{
209 TTAContext *s = avctx->priv_data;
a24c4a2a
AB
210
211 s->avctx = avctx;
212
213 // 30bytes includes a seektable with one frame
214 if (avctx->extradata_size < 30)
215 return -1;
216
b59efc94 217 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
2bb6eba2 218 if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
a24c4a2a 219 {
2af3dc86
PM
220 if (avctx->err_recognition & AV_EF_CRCCHECK) {
221 s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
222 if (tta_check_crc(s, avctx->extradata, 18))
223 return AVERROR_INVALIDDATA;
224 }
225
a24c4a2a 226 /* signature */
9aff2d17 227 skip_bits_long(&s->gb, 32);
a24c4a2a 228
b16960a8
JR
229 s->format = get_bits(&s->gb, 16);
230 if (s->format > 2) {
231 av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
a24c4a2a
AB
232 return -1;
233 }
b16960a8
JR
234 if (s->format == FORMAT_ENCRYPTED) {
235 av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
236 return AVERROR(EINVAL);
237 }
1a174c28 238 avctx->channels = s->channels = get_bits(&s->gb, 16);
dd1c8f3e
LA
239 avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
240 s->bps = (avctx->bits_per_coded_sample + 7) / 8;
1a174c28 241 avctx->sample_rate = get_bits_long(&s->gb, 32);
1a174c28 242 s->data_length = get_bits_long(&s->gb, 32);
9aff2d17 243 skip_bits_long(&s->gb, 32); // CRC32 of header
a24c4a2a 244
8bd1f1a4
SG
245 if (s->channels == 0) {
246 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
247 return AVERROR_INVALIDDATA;
7416d610
RB
248 } else if (avctx->sample_rate == 0) {
249 av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
250 return AVERROR_INVALIDDATA;
8bd1f1a4
SG
251 }
252
b16960a8 253 switch(s->bps) {
c6056d40
JR
254 case 2:
255 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
256 avctx->bits_per_raw_sample = 16;
257 break;
258 case 3:
259 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
260 avctx->bits_per_raw_sample = 24;
261 break;
262 default:
263 av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
264 return AVERROR_INVALIDDATA;
a24c4a2a
AB
265 }
266
b5050539
JR
267 // prevent overflow
268 if (avctx->sample_rate > 0x7FFFFF) {
269 av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
270 return AVERROR(EINVAL);
271 }
272 s->frame_length = 256 * avctx->sample_rate / 245;
a24c4a2a
AB
273
274 s->last_frame_length = s->data_length % s->frame_length;
275 s->total_frames = s->data_length / s->frame_length +
276 (s->last_frame_length ? 1 : 0);
277
b16960a8
JR
278 av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
279 s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
a24c4a2a
AB
280 avctx->block_align);
281 av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
282 s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
283
284 // FIXME: seek table
2af3dc86
PM
285 if (get_bits_left(&s->gb) < 32 * s->total_frames + 32)
286 av_log(avctx, AV_LOG_WARNING, "Seek table missing or too small\n");
287 else if (avctx->err_recognition & AV_EF_CRCCHECK) {
288 if (tta_check_crc(s, avctx->extradata + 22, s->total_frames * 4))
289 return AVERROR_INVALIDDATA;
290 }
9aff2d17
PM
291 skip_bits_long(&s->gb, 32 * s->total_frames);
292 skip_bits_long(&s->gb, 32); // CRC32 of seektable
a24c4a2a 293
3a1a7e32
MN
294 if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
295 av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
296 return -1;
297 }
298
c6056d40
JR
299 if (s->bps == 2) {
300 s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
2f1d212f
JR
301 if (!s->decode_buffer)
302 return AVERROR(ENOMEM);
c6056d40 303 }
5113b3bd 304 s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
2f1d212f
JR
305 if (!s->ch_ctx) {
306 av_freep(&s->decode_buffer);
5113b3bd 307 return AVERROR(ENOMEM);
2f1d212f 308 }
a24c4a2a
AB
309 } else {
310 av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
311 return -1;
312 }
313
0eea2129
JR
314 avcodec_get_frame_defaults(&s->frame);
315 avctx->coded_frame = &s->frame;
316
a24c4a2a
AB
317 return 0;
318}
319
0eea2129
JR
320static int tta_decode_frame(AVCodecContext *avctx, void *data,
321 int *got_frame_ptr, AVPacket *avpkt)
a24c4a2a 322{
7a00bbad
TB
323 const uint8_t *buf = avpkt->data;
324 int buf_size = avpkt->size;
a24c4a2a 325 TTAContext *s = avctx->priv_data;
0eea2129 326 int i, ret;
7b7a74a1
JR
327 int cur_chan = 0, framelen = s->frame_length;
328 int32_t *p;
a24c4a2a 329
2af3dc86
PM
330 if (avctx->err_recognition & AV_EF_CRCCHECK) {
331 if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4))
332 return AVERROR_INVALIDDATA;
333 }
334
a24c4a2a 335 init_get_bits(&s->gb, buf, buf_size*8);
a24c4a2a 336
8664682d
JR
337 // FIXME: seeking
338 s->total_frames--;
339 if (!s->total_frames && s->last_frame_length)
340 framelen = s->last_frame_length;
a24c4a2a 341
0eea2129
JR
342 /* get output buffer */
343 s->frame.nb_samples = framelen;
344 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
345 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
346 return ret;
8664682d
JR
347 }
348
c6056d40
JR
349 // decode directly to output buffer for 24-bit sample format
350 if (s->bps == 3)
01ed1c39 351 s->decode_buffer = (int32_t *)s->frame.data[0];
c6056d40 352
8664682d
JR
353 // init per channel states
354 for (i = 0; i < s->channels; i++) {
355 s->ch_ctx[i].predictor = 0;
356 ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
357 rice_init(&s->ch_ctx[i].rice, 10, 10);
358 }
e6923f68 359
8664682d
JR
360 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
361 int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
362 TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
363 TTARice *rice = &s->ch_ctx[cur_chan].rice;
364 uint32_t unary, depth, k;
365 int32_t value;
366
367 unary = tta_get_unary(&s->gb);
368
369 if (unary == 0) {
370 depth = 0;
371 k = rice->k0;
372 } else {
373 depth = 1;
374 k = rice->k1;
375 unary--;
a24c4a2a
AB
376 }
377
6ab681a4
JR
378 if (get_bits_left(&s->gb) < k) {
379 ret = AVERROR_INVALIDDATA;
380 goto error;
381 }
a24c4a2a 382
8664682d 383 if (k) {
6ab681a4
JR
384 if (k > MIN_CACHE_BITS) {
385 ret = AVERROR_INVALIDDATA;
386 goto error;
387 }
8664682d
JR
388 value = (unary << k) + get_bits(&s->gb, k);
389 } else
390 value = unary;
391
392 // FIXME: copy paste from original
393 switch (depth) {
394 case 1:
395 rice->sum1 += value - (rice->sum1 >> 4);
396 if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
397 rice->k1--;
398 else if(rice->sum1 > shift_16[rice->k1 + 1])
399 rice->k1++;
400 value += shift_1[rice->k0];
401 default:
402 rice->sum0 += value - (rice->sum0 >> 4);
403 if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
404 rice->k0--;
405 else if(rice->sum0 > shift_16[rice->k0 + 1])
406 rice->k0++;
407 }
a9837b58 408
8664682d 409 // extract coded value
15e35274 410#define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
8664682d 411 *p = UNFOLD(value);
a24c4a2a 412
8664682d
JR
413 // run hybrid filter
414 ttafilter_process(filter, p, 0);
a24c4a2a 415
8664682d 416 // fixed order prediction
79a32e3b 417#define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
8664682d
JR
418 switch (s->bps) {
419 case 1: *p += PRED(*predictor, 4); break;
420 case 2:
421 case 3: *p += PRED(*predictor, 5); break;
422 case 4: *p += *predictor; break;
423 }
424 *predictor = *p;
425
426 // flip channels
427 if (cur_chan < (s->channels-1))
428 cur_chan++;
429 else {
430 // decorrelate in case of stereo integer
431 if (s->channels > 1) {
432 int32_t *r = p - 1;
433 for (*p += *r / 2; r > p - s->channels; r--)
434 *r = *(r + 1) - *r;
a24c4a2a 435 }
8664682d 436 cur_chan = 0;
a24c4a2a 437 }
8664682d 438 }
a24c4a2a 439
6ab681a4
JR
440 if (get_bits_left(&s->gb) < 32) {
441 ret = AVERROR_INVALIDDATA;
442 goto error;
443 }
9aff2d17 444 skip_bits_long(&s->gb, 32); // frame crc
8664682d
JR
445
446 // convert to output buffer
c6056d40 447 if (s->bps == 2) {
0eea2129 448 int16_t *samples = (int16_t *)s->frame.data[0];
c6056d40
JR
449 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
450 *samples++ = *p;
451 } else {
452 // shift samples for 24-bit sample format
0eea2129 453 int32_t *samples = (int32_t *)s->frame.data[0];
b656c4d0 454 for (i = 0; i < framelen * s->channels; i++)
c6056d40
JR
455 *samples++ <<= 8;
456 // reset decode buffer
457 s->decode_buffer = NULL;
8664682d 458 }
a24c4a2a 459
0eea2129
JR
460 *got_frame_ptr = 1;
461 *(AVFrame *)data = s->frame;
c6056d40 462
a24c4a2a 463 return buf_size;
6ab681a4
JR
464error:
465 // reset decode buffer
466 if (s->bps == 3)
467 s->decode_buffer = NULL;
468 return ret;
a24c4a2a
AB
469}
470
98a6fff9 471static av_cold int tta_decode_close(AVCodecContext *avctx) {
a24c4a2a
AB
472 TTAContext *s = avctx->priv_data;
473
437fb1c8 474 av_free(s->decode_buffer);
5113b3bd 475 av_freep(&s->ch_ctx);
a24c4a2a
AB
476
477 return 0;
478}
479
d36beb3f 480AVCodec ff_tta_decoder = {
ec6402b7
AK
481 .name = "tta",
482 .type = AVMEDIA_TYPE_AUDIO,
483 .id = CODEC_ID_TTA,
484 .priv_data_size = sizeof(TTAContext),
485 .init = tta_decode_init,
486 .close = tta_decode_close,
487 .decode = tta_decode_frame,
0eea2129 488 .capabilities = CODEC_CAP_DR1,
68dddf53 489 .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
a24c4a2a 490};