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>
41
42 #define FORMAT_SIMPLE 1
43 #define FORMAT_ENCRYPTED 2
44
49
53
55
60
62 {
64
66 qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
67 qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
68 }
else if (c->
error > 0) {
69 qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
70 qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
71 }
72
73 sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
74 dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
75
76 dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
77 dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
78
79 dx[4] = ((dl[4] >> 30) | 1);
80 dx[5] = ((dl[5] >> 30) | 2) & ~1;
81 dx[6] = ((dl[6] >> 30) | 2) & ~1;
82 dx[7] = ((dl[7] >> 30) | 4) & ~3;
83
85 *in += (sum >> c->
shift);
86
87 dl[4] = -dl[5]; dl[5] = -dl[6];
88 dl[6] = *in - dl[7]; dl[7] = *
in;
89 dl[5] += dl[6]; dl[4] += dl[5];
90 }
91
96 0,
100 };
101
103 {
104 uint32_t crc, CRC;
105
108 if (CRC != (crc ^ 0xFFFFFFFFU)) {
111 }
112
113 return 0;
114 }
115
117 {
118 uint64_t crc = UINT64_MAX,
poly = 0x42F0E1EBA9EA3693
U;
120 int i;
121
122 while (pass < end) {
123 crc ^= (uint64_t)*pass++ << 56;
124 for (i = 0; i < 8; i++)
125 crc = (crc << 1) ^ (poly & (((int64_t) crc) >> 63));
126 }
127
128 return crc ^ UINT64_MAX;
129 }
130
132 {
134
139 } else
145 }
146
147 return 0;
148 }
149
151 {
154 int total_frames;
155
157
158 // 30bytes includes TTA1 header
161
165 /* signature */
167
172 }
175 av_log(avctx,
AV_LOG_ERROR,
"Missing password for encrypted stream. Please use the -password option\n");
177 }
179 }
188
195 }
196
199 case 2:
201 break;
202 case 3:
204 break;
205 //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
206 default:
209 }
210
211 // prevent overflow
215 }
217
221
227
231 }
232 } else {
235 }
236
238 }
239
241 int *got_frame_ptr,
AVPacket *avpkt)
242 {
246 int buf_size = avpkt->
size;
252
256 }
257
260
261 /* get output buffer */
265
266 // decode directly to output buffer for 24-bit sample format
269
270 // init per channel states
276 int i;
277 for (i = 0; i < 8; i++)
279 }
281 }
282
283 i = 0;
288 uint32_t unary,
depth, k;
290
292
293 if (unary == 0) {
294 depth = 0;
296 } else {
297 depth = 1;
299 unary--;
300 }
301
304 goto error;
305 }
306
307 if (k) {
310 goto error;
311 }
312 value = (unary << k) +
get_bits(&gb, k);
313 } else
314 value = unary;
315
316 // FIXME: copy paste from original
317 switch (depth) {
318 case 1:
319 rice->
sum1 += value - (rice->
sum1 >> 4);
325 default:
326 rice->
sum0 += value - (rice->
sum0 >> 4);
331 }
332
333 // extract coded value
334 *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
335
336 // run hybrid filter
338
339 // fixed order prediction
340 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
342 case 1: *p +=
PRED(*predictor, 4);
break;
343 case 2:
344 case 3: *p +=
PRED(*predictor, 5);
break;
346 }
347 *predictor = *p;
348
349 // flip channels
351 cur_chan++;
352 else {
353 // decorrelate in case of multiple channels
356 for (*p += *r / 2; r > p - s->
channels; r--)
358 }
359 cur_chan = 0;
360 i++;
361 // check for last frame
364 break;
365 }
366 }
367 }
368
372 goto error;
373 }
375
376 // convert to output buffer
378 case 1: {
381 *samples++ = *p + 0x80;
382 break;
383 }
384 case 2: {
385 int16_t *samples = (int16_t *)frame->
data[0];
387 *samples++ = *p;
388 break;
389 }
390 case 3: {
391 // shift samples for 24-bit sample format
394 *samples++ <<= 8;
395 // reset decode buffer
397 break;
398 }
399 }
400
401 *got_frame_ptr = 1;
402
403 return buf_size;
404 error:
405 // reset decode buffer
409 }
410
412 {
416 }
417
420
425
426 return 0;
427 }
428
429 #define OFFSET(x) offsetof(TTAContext, x)
430 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
433 { NULL },
434 };
435
441 };
442
455 };