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
31
36
37 #define BITSTREAM_READER_LE
45
46 #define FORMAT_SIMPLE 1
47 #define FORMAT_ENCRYPTED 2
48
53
57
59
65
70 0,
74 };
75
77 {
78 uint32_t crc, CRC;
79
81 crc =
av_crc(
s->crc_table, 0xFFFFFFFFU, buf, buf_size);
82 if (CRC != (crc ^ 0xFFFFFFFFU)) {
85 }
86
87 return 0;
88 }
89
91 {
92 uint64_t crc = UINT64_MAX, poly = 0x42F0E1EBA9EA3693
U;
95
97 crc ^= (uint64_t)*
pass++ << 56;
98 for (
i = 0;
i < 8;
i++)
99 crc = (crc << 1) ^ (poly & (((int64_t) crc) >> 63));
100 }
101
102 return crc ^ UINT64_MAX;
103 }
104
106 {
108
111 sizeof(*
s->decode_buffer) *
s->channels);
112 if (!
s->decode_buffer)
114 } else
115 s->decode_buffer =
NULL;
120 }
121
122 return 0;
123 }
124
126 {
129 int total_frames;
131
133
134 // 22 bytes for a TTA1 header
137
142
144 /* signature */
146
151 }
154 av_log(avctx,
AV_LOG_ERROR,
"Missing password for encrypted stream. Please use the -password option\n");
156 }
158 }
160 if (
s->channels > 1 &&
s->channels < 9)
167
168 if (
s->channels == 0 ||
s->channels > 16) {
174 }
175
178 case 2:
180 break;
181 case 3:
183 break;
184 //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
185 default:
188 }
189
190 // prevent overflow
194 }
196
197 s->last_frame_length =
s->data_length %
s->frame_length;
198 total_frames =
s->data_length /
s->frame_length +
199 (
s->last_frame_length ? 1 : 0);
200
205 s->data_length,
s->frame_length,
s->last_frame_length, total_frames);
206
207 if(
s->frame_length >= UINT_MAX / (
s->channels *
sizeof(
int32_t))){
210 }
211 } else {
214 }
215
217
219 }
220
222 int *got_frame_ptr,
AVPacket *avpkt)
223 {
226 const uint8_t *buf = avpkt->
data;
227 int buf_size = avpkt->
size;
231 int cur_chan = 0, framelen =
s->frame_length;
232 uint32_t *p;
233
235 if (buf_size < 4 ||
238 }
239
242
243 /* get output buffer */
244 frame->nb_samples = framelen;
247
248 // decode directly to output buffer for 24-bit sample format
251
252 // init per channel states
253 for (
i = 0;
i <
s->channels;
i++) {
255 s->ch_ctx[
i].predictor = 0;
259 for (
i = 0;
i < 8;
i++)
261 }
263 }
264
266 for (p =
s->decode_buffer; (
int32_t*)p < s->decode_buffer + (framelen *
s->channels); p++) {
269 TTARice *rice = &
s->ch_ctx[cur_chan].rice;
270 uint32_t unary, depth, k;
272
274
275 if (unary == 0) {
276 depth = 0;
278 } else {
279 depth = 1;
281 unary--;
282 }
283
287 }
288
289 if (k) {
293 }
295 } else
297
298 // FIXME: copy paste from original
299 switch (depth) {
300 case 1:
307 default:
313 }
314
315 // extract coded value
317
318 // run hybrid filter
321
322 // fixed order prediction
323 #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
326 case 2:
329 }
331
332 // flip channels
333 if (cur_chan < (
s->channels-1))
334 cur_chan++;
335 else {
336 // decorrelate in case of multiple channels
337 if (
s->channels > 1) {
339 for (*p += *
r / 2;
r > (
int32_t*)p -
s->channels;
r--)
341 }
342 cur_chan = 0;
344 // check for last frame
346 frame->nb_samples = framelen =
s->last_frame_length;
347 break;
348 }
349 }
350 }
351
356 }
358
359 // convert to output buffer
361 case 1: {
363 for (p =
s->decode_buffer; (
int32_t*)p <
s->decode_buffer + (framelen *
s->channels); p++)
365 break;
366 }
367 case 2: {
369 for (p =
s->decode_buffer; (
int32_t*)p <
s->decode_buffer + (framelen *
s->channels); p++)
371 break;
372 }
373 case 3: {
374 // shift samples for 24-bit sample format
376 for (
i = 0;
i < framelen *
s->channels;
i++)
378 // reset decode buffer
379 s->decode_buffer =
NULL;
380 break;
381 }
382 }
383
384 *got_frame_ptr = 1;
385
386 return buf_size;
388 // reset decode buffer
390 s->decode_buffer =
NULL;
392 }
393
396
399 s->decode_buffer =
NULL;
401
402 return 0;
403 }
404
405 #define OFFSET(x) offsetof(TTAContext, x)
406 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
410 };
411
417 };
418
431 };