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
35
36 #define BITSTREAM_READER_LE
44
45 #define FORMAT_SIMPLE 1
46 #define FORMAT_ENCRYPTED 2
47
52
56
58
64
69 0,
73 };
74
76 {
77 uint32_t crc, CRC;
78
81 if (CRC != (crc ^ 0xFFFFFFFFU)) {
84 }
85
86 return 0;
87 }
88
90 {
91 uint64_t crc = UINT64_MAX, poly = 0x42F0E1EBA9EA3693
U;
93 int i;
94
95 while (pass < end) {
96 crc ^= (uint64_t)*pass++ << 56;
97 for (i = 0; i < 8; i++)
98 crc = (crc << 1) ^ (poly & (((int64_t) crc) >> 63));
99 }
100
101 return crc ^ UINT64_MAX;
102 }
103
105 {
107
112 } else
118 }
119
120 return 0;
121 }
122
124 {
127 int total_frames;
128 int ret;
129
131
132 // 30bytes includes TTA1 header
135
138 if (ret < 0)
139 return ret;
140
142 /* signature */
144
149 }
152 av_log(avctx,
AV_LOG_ERROR,
"Missing password for encrypted stream. Please use the -password option\n");
154 }
156 }
165
172 }
173
176 case 2:
178 break;
179 case 3:
181 break;
182 //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
183 default:
186 }
187
188 // prevent overflow
192 }
194
198
204
208 }
209 } else {
212 }
213
215
217 }
218
220 int *got_frame_ptr,
AVPacket *avpkt)
221 {
225 int buf_size = avpkt->
size;
228 int i, ret;
231
233 if (buf_size < 4 ||
236 }
237
239 return ret;
240
241 /* get output buffer */
244 return ret;
245
246 // decode directly to output buffer for 24-bit sample format
249
250 // init per channel states
256 int i;
257 for (i = 0; i < 8; i++)
259 }
261 }
262
263 i = 0;
268 uint32_t unary, depth, k;
270
272
273 if (unary == 0) {
274 depth = 0;
276 } else {
277 depth = 1;
279 unary--;
280 }
281
285 }
286
287 if (k) {
291 }
292 value = (unary << k) +
get_bits(&gb, k);
293 } else
294 value = unary;
295
296 // FIXME: copy paste from original
297 switch (depth) {
298 case 1:
299 rice->
sum1 += value - (rice->
sum1 >> 4);
305 default:
306 rice->
sum0 += value - (rice->
sum0 >> 4);
311 }
312
313 // extract coded value
314 *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
315
316 // run hybrid filter
319
320 // fixed order prediction
321 #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
323 case 1: *p +=
PRED(*predictor, 4);
break;
324 case 2:
325 case 3: *p +=
PRED(*predictor, 5);
break;
326 case 4: *p += *predictor; break;
327 }
328 *predictor = *p;
329
330 // flip channels
332 cur_chan++;
333 else {
334 // decorrelate in case of multiple channels
337 for (*p += *r / 2; r > p - s->
channels; r--)
339 }
340 cur_chan = 0;
341 i++;
342 // check for last frame
345 break;
346 }
347 }
348 }
349
354 }
356
357 // convert to output buffer
359 case 1: {
362 *samples++ = *p + 0x80;
363 break;
364 }
365 case 2: {
366 int16_t *samples = (int16_t *)frame->
data[0];
368 *samples++ = *p;
369 break;
370 }
371 case 3: {
372 // shift samples for 24-bit sample format
375 *samples++ <<= 8;
376 // reset decode buffer
378 break;
379 }
380 }
381
382 *got_frame_ptr = 1;
383
384 return buf_size;
386 // reset decode buffer
389 return ret;
390 }
391
393 {
397 }
398
401
406
407 return 0;
408 }
409
410 #define OFFSET(x) offsetof(TTAContext, x)
411 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
415 };
416
422 };
423
436 };
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int init_thread_copy(AVCodecContext *avctx)
#define LIBAVUTIL_VERSION_INT
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static av_cold int init(AVCodecContext *avctx)
static av_cold int tta_decode_init(AVCodecContext *avctx)
static int tta_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
void ff_tta_rice_init(TTARice *c, uint32_t k0, uint32_t k1)
const char * av_default_item_name(void *ptr)
Return the context name.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_CH_LAYOUT_STEREO
static const int64_t tta_channel_layouts[7]
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
static av_cold int tta_decode_close(AVCodecContext *avctx)
enum AVSampleFormat sample_fmt
audio sample format
static av_cold int end(AVCodecContext *avctx)
Multithreading support functions.
static uint64_t tta_check_crc64(uint8_t *pass)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define AV_CH_LOW_FREQUENCY
Public header for CRC hash function implementation.
static const AVOption options[]
bitstream reader API header.
av_cold void ff_ttadsp_init(TTADSPContext *c)
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
static int get_bits_left(GetBitContext *gb)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int allocate_buffers(AVCodecContext *avctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_CH_LAYOUT_QUAD
const char * name
Name of the codec implementation.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
uint64_t channel_layout
Audio channel layout.
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
const uint32_t ff_tta_shift_1[]
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
GLsizei GLboolean const GLfloat * value
const uint32_t *const ff_tta_shift_16
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
void(* filter_process)(int32_t *qm, int32_t *dx, int32_t *dl, int32_t *error, int32_t *in, int32_t shift, int32_t round)
#define AV_EF_EXPLODE
abort decoding on minor error detection
#define AV_CH_LAYOUT_5POINT1_BACK
static void error(const char *err)
Libavcodec external API header.
static const AVClass tta_decoder_class
int sample_rate
samples per second
const uint8_t ff_tta_filter_configs[]
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Describe the class of an AVClass context structure.
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data...
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static av_const int sign_extend(int val, unsigned bits)
#define AV_CH_BACK_CENTER
#define AV_CH_LAYOUT_7POINT1_WIDE
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
common internal api header.
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
int channels
number of audio channels
static const uint8_t * align_get_bits(GetBitContext *s)
#define av_malloc_array(a, b)
void ff_tta_filter_init(TTAFilter *c, int32_t shift)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
void * av_mallocz_array(size_t nmemb, size_t size)