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