00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #define BITSTREAM_READER_LE
00031
00032 #include <limits.h>
00033 #include "avcodec.h"
00034 #include "get_bits.h"
00035 #include "libavutil/crc.h"
00036
00037 #define FORMAT_SIMPLE 1
00038 #define FORMAT_ENCRYPTED 2
00039
00040 #define MAX_ORDER 16
00041 typedef struct TTAFilter {
00042 int32_t shift, round, error;
00043 int32_t qm[MAX_ORDER];
00044 int32_t dx[MAX_ORDER];
00045 int32_t dl[MAX_ORDER];
00046 } TTAFilter;
00047
00048 typedef struct TTARice {
00049 uint32_t k0, k1, sum0, sum1;
00050 } TTARice;
00051
00052 typedef struct TTAChannel {
00053 int32_t predictor;
00054 TTAFilter filter;
00055 TTARice rice;
00056 } TTAChannel;
00057
00058 typedef struct TTAContext {
00059 AVCodecContext *avctx;
00060 AVFrame frame;
00061 GetBitContext gb;
00062 const AVCRC *crc_table;
00063
00064 int format, channels, bps;
00065 unsigned data_length;
00066 int frame_length, last_frame_length, total_frames;
00067
00068 int32_t *decode_buffer;
00069
00070 TTAChannel *ch_ctx;
00071 } TTAContext;
00072
00073 static const uint32_t shift_1[] = {
00074 0x00000001, 0x00000002, 0x00000004, 0x00000008,
00075 0x00000010, 0x00000020, 0x00000040, 0x00000080,
00076 0x00000100, 0x00000200, 0x00000400, 0x00000800,
00077 0x00001000, 0x00002000, 0x00004000, 0x00008000,
00078 0x00010000, 0x00020000, 0x00040000, 0x00080000,
00079 0x00100000, 0x00200000, 0x00400000, 0x00800000,
00080 0x01000000, 0x02000000, 0x04000000, 0x08000000,
00081 0x10000000, 0x20000000, 0x40000000, 0x80000000,
00082 0x80000000, 0x80000000, 0x80000000, 0x80000000,
00083 0x80000000, 0x80000000, 0x80000000, 0x80000000
00084 };
00085
00086 static const uint32_t * const shift_16 = shift_1 + 4;
00087
00088 static const int32_t ttafilter_configs[4] = {
00089 10,
00090 9,
00091 10,
00092 12
00093 };
00094
00095 static void ttafilter_init(TTAFilter *c, int32_t shift) {
00096 memset(c, 0, sizeof(TTAFilter));
00097 c->shift = shift;
00098 c->round = shift_1[shift-1];
00099
00100 }
00101
00102 static inline void ttafilter_process(TTAFilter *c, int32_t *in)
00103 {
00104 register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
00105
00106 if (c->error < 0) {
00107 qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
00108 qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
00109 } else if (c->error > 0) {
00110 qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
00111 qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
00112 }
00113
00114 sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
00115 dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
00116
00117 dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
00118 dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
00119
00120 dx[4] = ((dl[4] >> 30) | 1);
00121 dx[5] = ((dl[5] >> 30) | 2) & ~1;
00122 dx[6] = ((dl[6] >> 30) | 2) & ~1;
00123 dx[7] = ((dl[7] >> 30) | 4) & ~3;
00124
00125 c->error = *in;
00126 *in += (sum >> c->shift);
00127
00128 dl[4] = -dl[5]; dl[5] = -dl[6];
00129 dl[6] = *in - dl[7]; dl[7] = *in;
00130 dl[5] += dl[6]; dl[4] += dl[5];
00131 }
00132
00133 static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
00134 {
00135 c->k0 = k0;
00136 c->k1 = k1;
00137 c->sum0 = shift_16[k0];
00138 c->sum1 = shift_16[k1];
00139 }
00140
00141 static int tta_get_unary(GetBitContext *gb)
00142 {
00143 int ret = 0;
00144
00145
00146 while (get_bits_left(gb) > 0 && get_bits1(gb))
00147 ret++;
00148 return ret;
00149 }
00150
00151 static const int64_t tta_channel_layouts[7] = {
00152 AV_CH_LAYOUT_STEREO,
00153 AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
00154 AV_CH_LAYOUT_QUAD,
00155 0,
00156 AV_CH_LAYOUT_5POINT1_BACK,
00157 AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
00158 AV_CH_LAYOUT_7POINT1_WIDE
00159 };
00160
00161 static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
00162 {
00163 uint32_t crc, CRC;
00164
00165 CRC = AV_RL32(buf + buf_size);
00166 crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
00167 if (CRC != (crc ^ 0xFFFFFFFFU)) {
00168 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00169 return AVERROR_INVALIDDATA;
00170 }
00171
00172 return 0;
00173 }
00174
00175 static av_cold int tta_decode_init(AVCodecContext * avctx)
00176 {
00177 TTAContext *s = avctx->priv_data;
00178
00179 s->avctx = avctx;
00180
00181
00182 if (avctx->extradata_size < 30)
00183 return -1;
00184
00185 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
00186 if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
00187 {
00188 if (avctx->err_recognition & AV_EF_CRCCHECK) {
00189 s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
00190 tta_check_crc(s, avctx->extradata, 18);
00191 }
00192
00193
00194 skip_bits_long(&s->gb, 32);
00195
00196 s->format = get_bits(&s->gb, 16);
00197 if (s->format > 2) {
00198 av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
00199 return -1;
00200 }
00201 if (s->format == FORMAT_ENCRYPTED) {
00202 av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
00203 return AVERROR(EINVAL);
00204 }
00205 avctx->channels = s->channels = get_bits(&s->gb, 16);
00206 if (s->channels > 1 && s->channels < 9)
00207 avctx->channel_layout = tta_channel_layouts[s->channels-2];
00208 avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
00209 s->bps = (avctx->bits_per_coded_sample + 7) / 8;
00210 avctx->sample_rate = get_bits_long(&s->gb, 32);
00211 s->data_length = get_bits_long(&s->gb, 32);
00212 skip_bits_long(&s->gb, 32);
00213
00214 if (s->channels == 0) {
00215 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
00216 return AVERROR_INVALIDDATA;
00217 } else if (avctx->sample_rate == 0) {
00218 av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
00219 return AVERROR_INVALIDDATA;
00220 }
00221
00222 switch(s->bps) {
00223 case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
00224 case 2:
00225 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00226 avctx->bits_per_raw_sample = 16;
00227 break;
00228 case 3:
00229 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00230 avctx->bits_per_raw_sample = 24;
00231 break;
00232
00233 default:
00234 av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
00235 return AVERROR_INVALIDDATA;
00236 }
00237
00238
00239 if (avctx->sample_rate > 0x7FFFFFu) {
00240 av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
00241 return AVERROR(EINVAL);
00242 }
00243 s->frame_length = 256 * avctx->sample_rate / 245;
00244
00245 s->last_frame_length = s->data_length % s->frame_length;
00246 s->total_frames = s->data_length / s->frame_length +
00247 (s->last_frame_length ? 1 : 0);
00248
00249 av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
00250 s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
00251 avctx->block_align);
00252 av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
00253 s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
00254
00255 if (s->total_frames < 0)
00256 return AVERROR_INVALIDDATA;
00257
00258
00259 if (avctx->extradata_size <= 26 || s->total_frames > INT_MAX / 4 ||
00260 avctx->extradata_size - 26 < s->total_frames * 4)
00261 av_log(avctx, AV_LOG_WARNING, "Seek table missing or too small\n");
00262 else if (avctx->err_recognition & AV_EF_CRCCHECK) {
00263 if (avctx->extradata_size < 26 + s->total_frames * 4 || tta_check_crc(s, avctx->extradata + 22, s->total_frames * 4))
00264 return AVERROR_INVALIDDATA;
00265 }
00266 skip_bits_long(&s->gb, 32 * s->total_frames);
00267 skip_bits_long(&s->gb, 32);
00268
00269 if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
00270 av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
00271 return -1;
00272 }
00273
00274 if (s->bps < 3) {
00275 s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
00276 if (!s->decode_buffer)
00277 return AVERROR(ENOMEM);
00278 } else
00279 s->decode_buffer = NULL;
00280 s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
00281 if (!s->ch_ctx) {
00282 av_freep(&s->decode_buffer);
00283 return AVERROR(ENOMEM);
00284 }
00285 } else {
00286 av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
00287 return -1;
00288 }
00289
00290 avcodec_get_frame_defaults(&s->frame);
00291 avctx->coded_frame = &s->frame;
00292
00293 return 0;
00294 }
00295
00296 static int tta_decode_frame(AVCodecContext *avctx, void *data,
00297 int *got_frame_ptr, AVPacket *avpkt)
00298 {
00299 const uint8_t *buf = avpkt->data;
00300 int buf_size = avpkt->size;
00301 TTAContext *s = avctx->priv_data;
00302 int i, ret;
00303 int cur_chan = 0, framelen = s->frame_length;
00304 int32_t *p;
00305
00306 if (avctx->err_recognition & AV_EF_CRCCHECK) {
00307 if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4))
00308 return AVERROR_INVALIDDATA;
00309 }
00310
00311 init_get_bits(&s->gb, buf, buf_size*8);
00312
00313
00314 s->total_frames--;
00315 if (!s->total_frames && s->last_frame_length)
00316 framelen = s->last_frame_length;
00317
00318
00319 s->frame.nb_samples = framelen;
00320 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00321 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00322 return ret;
00323 }
00324
00325
00326 if (s->bps == 3)
00327 s->decode_buffer = (int32_t *)s->frame.data[0];
00328
00329
00330 for (i = 0; i < s->channels; i++) {
00331 s->ch_ctx[i].predictor = 0;
00332 ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1]);
00333 rice_init(&s->ch_ctx[i].rice, 10, 10);
00334 }
00335
00336 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
00337 int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
00338 TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
00339 TTARice *rice = &s->ch_ctx[cur_chan].rice;
00340 uint32_t unary, depth, k;
00341 int32_t value;
00342
00343 unary = tta_get_unary(&s->gb);
00344
00345 if (unary == 0) {
00346 depth = 0;
00347 k = rice->k0;
00348 } else {
00349 depth = 1;
00350 k = rice->k1;
00351 unary--;
00352 }
00353
00354 if (get_bits_left(&s->gb) < k) {
00355 ret = AVERROR_INVALIDDATA;
00356 goto error;
00357 }
00358
00359 if (k) {
00360 if (k > MIN_CACHE_BITS) {
00361 ret = AVERROR_INVALIDDATA;
00362 goto error;
00363 }
00364 value = (unary << k) + get_bits(&s->gb, k);
00365 } else
00366 value = unary;
00367
00368
00369 switch (depth) {
00370 case 1:
00371 rice->sum1 += value - (rice->sum1 >> 4);
00372 if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
00373 rice->k1--;
00374 else if(rice->sum1 > shift_16[rice->k1 + 1])
00375 rice->k1++;
00376 value += shift_1[rice->k0];
00377 default:
00378 rice->sum0 += value - (rice->sum0 >> 4);
00379 if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
00380 rice->k0--;
00381 else if(rice->sum0 > shift_16[rice->k0 + 1])
00382 rice->k0++;
00383 }
00384
00385
00386 *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
00387
00388
00389 ttafilter_process(filter, p);
00390
00391
00392 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
00393 switch (s->bps) {
00394 case 1: *p += PRED(*predictor, 4); break;
00395 case 2:
00396 case 3: *p += PRED(*predictor, 5); break;
00397 case 4: *p += *predictor; break;
00398 }
00399 *predictor = *p;
00400
00401
00402 if (cur_chan < (s->channels-1))
00403 cur_chan++;
00404 else {
00405
00406 if (s->channels > 1) {
00407 int32_t *r = p - 1;
00408 for (*p += *r / 2; r > p - s->channels; r--)
00409 *r = *(r + 1) - *r;
00410 }
00411 cur_chan = 0;
00412 }
00413 }
00414
00415 if (get_bits_left(&s->gb) < 32) {
00416 ret = AVERROR_INVALIDDATA;
00417 goto error;
00418 }
00419 skip_bits_long(&s->gb, 32);
00420
00421
00422 switch (s->bps) {
00423 case 1: {
00424 uint8_t *samples = (uint8_t *)s->frame.data[0];
00425 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
00426 *samples++ = *p + 0x80;
00427 break;
00428 }
00429 case 2: {
00430 uint16_t *samples = (int16_t *)s->frame.data[0];
00431 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
00432 *samples++ = *p;
00433 break;
00434 }
00435 case 3: {
00436
00437 int32_t *samples = (int32_t *)s->frame.data[0];
00438 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
00439 *samples++ <<= 8;
00440
00441 s->decode_buffer = NULL;
00442 break;
00443 }
00444 }
00445
00446 *got_frame_ptr = 1;
00447 *(AVFrame *)data = s->frame;
00448
00449 return buf_size;
00450 error:
00451
00452 if (s->bps == 3)
00453 s->decode_buffer = NULL;
00454 return ret;
00455 }
00456
00457 static av_cold int tta_decode_close(AVCodecContext *avctx) {
00458 TTAContext *s = avctx->priv_data;
00459
00460 if (s->bps < 3)
00461 av_free(s->decode_buffer);
00462 s->decode_buffer = NULL;
00463 av_freep(&s->ch_ctx);
00464
00465 return 0;
00466 }
00467
00468 AVCodec ff_tta_decoder = {
00469 .name = "tta",
00470 .type = AVMEDIA_TYPE_AUDIO,
00471 .id = AV_CODEC_ID_TTA,
00472 .priv_data_size = sizeof(TTAContext),
00473 .init = tta_decode_init,
00474 .close = tta_decode_close,
00475 .decode = tta_decode_frame,
00476 .capabilities = CODEC_CAP_DR1,
00477 .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
00478 };