00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/samplefmt.h"
00029 #include "tak.h"
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "internal.h"
00033 #include "unary.h"
00034
00035 #define MAX_SUBFRAMES 8
00036 #define MAX_PREDICTORS 256
00037
00038 typedef struct MCDParam {
00039 int8_t present;
00040 int8_t index;
00041 int8_t chan1;
00042 int8_t chan2;
00043 } MCDParam;
00044
00045 typedef struct TAKDecContext {
00046 AVCodecContext *avctx;
00047 AVFrame frame;
00048 DSPContext dsp;
00049 TAKStreamInfo ti;
00050 GetBitContext gb;
00051
00052 int uval;
00053 int nb_samples;
00054 uint8_t *decode_buffer;
00055 unsigned int decode_buffer_size;
00056 int32_t *decoded[TAK_MAX_CHANNELS];
00057
00058 int8_t lpc_mode[TAK_MAX_CHANNELS];
00059 int8_t sample_shift[TAK_MAX_CHANNELS];
00060 int16_t predictors[MAX_PREDICTORS];
00061 int nb_subframes;
00062 int16_t subframe_len[MAX_SUBFRAMES];
00063 int subframe_scale;
00064
00065 int8_t dmode;
00066
00067 MCDParam mcdparams[TAK_MAX_CHANNELS];
00068
00069 int8_t coding_mode[128];
00070 DECLARE_ALIGNED(16, int16_t, filter)[MAX_PREDICTORS];
00071 DECLARE_ALIGNED(16, int16_t, residues)[544];
00072 } TAKDecContext;
00073
00074 static const int8_t mc_dmodes[] = { 1, 3, 4, 6, };
00075
00076 static const uint16_t predictor_sizes[] = {
00077 4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 160, 192, 224, 256, 0,
00078 };
00079
00080 static const struct CParam {
00081 int init;
00082 int escape;
00083 int scale;
00084 int aescape;
00085 int bias;
00086 } xcodes[50] = {
00087 { 0x01, 0x0000001, 0x0000001, 0x0000003, 0x0000008 },
00088 { 0x02, 0x0000003, 0x0000001, 0x0000007, 0x0000006 },
00089 { 0x03, 0x0000005, 0x0000002, 0x000000E, 0x000000D },
00090 { 0x03, 0x0000003, 0x0000003, 0x000000D, 0x0000018 },
00091 { 0x04, 0x000000B, 0x0000004, 0x000001C, 0x0000019 },
00092 { 0x04, 0x0000006, 0x0000006, 0x000001A, 0x0000030 },
00093 { 0x05, 0x0000016, 0x0000008, 0x0000038, 0x0000032 },
00094 { 0x05, 0x000000C, 0x000000C, 0x0000034, 0x0000060 },
00095 { 0x06, 0x000002C, 0x0000010, 0x0000070, 0x0000064 },
00096 { 0x06, 0x0000018, 0x0000018, 0x0000068, 0x00000C0 },
00097 { 0x07, 0x0000058, 0x0000020, 0x00000E0, 0x00000C8 },
00098 { 0x07, 0x0000030, 0x0000030, 0x00000D0, 0x0000180 },
00099 { 0x08, 0x00000B0, 0x0000040, 0x00001C0, 0x0000190 },
00100 { 0x08, 0x0000060, 0x0000060, 0x00001A0, 0x0000300 },
00101 { 0x09, 0x0000160, 0x0000080, 0x0000380, 0x0000320 },
00102 { 0x09, 0x00000C0, 0x00000C0, 0x0000340, 0x0000600 },
00103 { 0x0A, 0x00002C0, 0x0000100, 0x0000700, 0x0000640 },
00104 { 0x0A, 0x0000180, 0x0000180, 0x0000680, 0x0000C00 },
00105 { 0x0B, 0x0000580, 0x0000200, 0x0000E00, 0x0000C80 },
00106 { 0x0B, 0x0000300, 0x0000300, 0x0000D00, 0x0001800 },
00107 { 0x0C, 0x0000B00, 0x0000400, 0x0001C00, 0x0001900 },
00108 { 0x0C, 0x0000600, 0x0000600, 0x0001A00, 0x0003000 },
00109 { 0x0D, 0x0001600, 0x0000800, 0x0003800, 0x0003200 },
00110 { 0x0D, 0x0000C00, 0x0000C00, 0x0003400, 0x0006000 },
00111 { 0x0E, 0x0002C00, 0x0001000, 0x0007000, 0x0006400 },
00112 { 0x0E, 0x0001800, 0x0001800, 0x0006800, 0x000C000 },
00113 { 0x0F, 0x0005800, 0x0002000, 0x000E000, 0x000C800 },
00114 { 0x0F, 0x0003000, 0x0003000, 0x000D000, 0x0018000 },
00115 { 0x10, 0x000B000, 0x0004000, 0x001C000, 0x0019000 },
00116 { 0x10, 0x0006000, 0x0006000, 0x001A000, 0x0030000 },
00117 { 0x11, 0x0016000, 0x0008000, 0x0038000, 0x0032000 },
00118 { 0x11, 0x000C000, 0x000C000, 0x0034000, 0x0060000 },
00119 { 0x12, 0x002C000, 0x0010000, 0x0070000, 0x0064000 },
00120 { 0x12, 0x0018000, 0x0018000, 0x0068000, 0x00C0000 },
00121 { 0x13, 0x0058000, 0x0020000, 0x00E0000, 0x00C8000 },
00122 { 0x13, 0x0030000, 0x0030000, 0x00D0000, 0x0180000 },
00123 { 0x14, 0x00B0000, 0x0040000, 0x01C0000, 0x0190000 },
00124 { 0x14, 0x0060000, 0x0060000, 0x01A0000, 0x0300000 },
00125 { 0x15, 0x0160000, 0x0080000, 0x0380000, 0x0320000 },
00126 { 0x15, 0x00C0000, 0x00C0000, 0x0340000, 0x0600000 },
00127 { 0x16, 0x02C0000, 0x0100000, 0x0700000, 0x0640000 },
00128 { 0x16, 0x0180000, 0x0180000, 0x0680000, 0x0C00000 },
00129 { 0x17, 0x0580000, 0x0200000, 0x0E00000, 0x0C80000 },
00130 { 0x17, 0x0300000, 0x0300000, 0x0D00000, 0x1800000 },
00131 { 0x18, 0x0B00000, 0x0400000, 0x1C00000, 0x1900000 },
00132 { 0x18, 0x0600000, 0x0600000, 0x1A00000, 0x3000000 },
00133 { 0x19, 0x1600000, 0x0800000, 0x3800000, 0x3200000 },
00134 { 0x19, 0x0C00000, 0x0C00000, 0x3400000, 0x6000000 },
00135 { 0x1A, 0x2C00000, 0x1000000, 0x7000000, 0x6400000 },
00136 { 0x1A, 0x1800000, 0x1800000, 0x6800000, 0xC000000 },
00137 };
00138
00139 static int set_bps_params(AVCodecContext *avctx)
00140 {
00141 switch (avctx->bits_per_raw_sample) {
00142 case 8:
00143 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
00144 break;
00145 case 16:
00146 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
00147 break;
00148 case 24:
00149 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
00150 break;
00151 default:
00152 av_log(avctx, AV_LOG_ERROR, "invalid/unsupported bits per sample: %d\n",
00153 avctx->bits_per_raw_sample);
00154 return AVERROR_INVALIDDATA;
00155 }
00156
00157 return 0;
00158 }
00159
00160 static void set_sample_rate_params(AVCodecContext *avctx)
00161 {
00162 TAKDecContext *s = avctx->priv_data;
00163 int shift = 3 - (avctx->sample_rate / 11025);
00164 shift = FFMAX(0, shift);
00165 s->uval = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << shift;
00166 s->subframe_scale = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << 1;
00167 }
00168
00169 static av_cold int tak_decode_init(AVCodecContext *avctx)
00170 {
00171 TAKDecContext *s = avctx->priv_data;
00172
00173 ff_tak_init_crc();
00174 ff_dsputil_init(&s->dsp, avctx);
00175
00176 s->avctx = avctx;
00177 avcodec_get_frame_defaults(&s->frame);
00178 avctx->coded_frame = &s->frame;
00179 avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
00180
00181 set_sample_rate_params(avctx);
00182
00183 return set_bps_params(avctx);
00184 }
00185
00186 static void decode_lpc(int32_t *coeffs, int mode, int length)
00187 {
00188 int i;
00189
00190 if (length < 2)
00191 return;
00192
00193 if (mode == 1) {
00194 int a1 = *coeffs++;
00195 for (i = 0; i < length - 1 >> 1; i++) {
00196 *coeffs += a1;
00197 coeffs[1] += *coeffs;
00198 a1 = coeffs[1];
00199 coeffs += 2;
00200 }
00201 if (length - 1 & 1)
00202 *coeffs += a1;
00203 } else if (mode == 2) {
00204 int a1 = coeffs[1];
00205 int a2 = a1 + *coeffs;
00206 coeffs[1] = a2;
00207 if (length > 2) {
00208 coeffs += 2;
00209 for (i = 0; i < length - 2 >> 1; i++) {
00210 int a3 = *coeffs + a1;
00211 int a4 = a3 + a2;
00212 *coeffs = a4;
00213 a1 = coeffs[1] + a3;
00214 a2 = a1 + a4;
00215 coeffs[1] = a2;
00216 coeffs += 2;
00217 }
00218 if (length & 1)
00219 *coeffs += a1 + a2;
00220 }
00221 } else if (mode == 3) {
00222 int a1 = coeffs[1];
00223 int a2 = a1 + *coeffs;
00224 coeffs[1] = a2;
00225 if (length > 2) {
00226 int a3 = coeffs[2];
00227 int a4 = a3 + a1;
00228 int a5 = a4 + a2;
00229 coeffs += 3;
00230 for (i = 0; i < length - 3; i++) {
00231 a3 += *coeffs;
00232 a4 += a3;
00233 a5 += a4;
00234 *coeffs = a5;
00235 coeffs++;
00236 }
00237 }
00238 }
00239 }
00240
00241 static int decode_segment(TAKDecContext *s, int8_t mode, int32_t *decoded, int len)
00242 {
00243 struct CParam code;
00244 GetBitContext *gb = &s->gb;
00245 int i;
00246
00247 if (!mode) {
00248 memset(decoded, 0, len * sizeof(*decoded));
00249 return 0;
00250 }
00251
00252 if (mode > FF_ARRAY_ELEMS(xcodes))
00253 return AVERROR_INVALIDDATA;
00254 code = xcodes[mode - 1];
00255
00256 for (i = 0; i < len; i++) {
00257 int x = get_bits_long(gb, code.init);
00258 if (x >= code.escape && get_bits1(gb)) {
00259 x |= 1 << code.init;
00260 if (x >= code.aescape) {
00261 int scale = get_unary(gb, 1, 9);
00262 if (scale == 9) {
00263 int scale_bits = get_bits(gb, 3);
00264 if (scale_bits > 0) {
00265 if (scale_bits == 7) {
00266 scale_bits += get_bits(gb, 5);
00267 if (scale_bits > 29)
00268 return AVERROR_INVALIDDATA;
00269 }
00270 scale = get_bits_long(gb, scale_bits) + 1;
00271 x += code.scale * scale;
00272 }
00273 x += code.bias;
00274 } else
00275 x += code.scale * scale - code.escape;
00276 } else
00277 x -= code.escape;
00278 }
00279 decoded[i] = (x >> 1) ^ -(x & 1);
00280 }
00281
00282 return 0;
00283 }
00284
00285 static int decode_residues(TAKDecContext *s, int32_t *decoded, int length)
00286 {
00287 GetBitContext *gb = &s->gb;
00288 int i, mode, ret;
00289
00290 if (length > s->nb_samples)
00291 return AVERROR_INVALIDDATA;
00292
00293 if (get_bits1(gb)) {
00294 int wlength, rval;
00295
00296 wlength = length / s->uval;
00297
00298 rval = length - (wlength * s->uval);
00299
00300 if (rval < s->uval / 2)
00301 rval += s->uval;
00302 else
00303 wlength++;
00304
00305 if (wlength <= 1 || wlength > 128)
00306 return AVERROR_INVALIDDATA;
00307
00308 s->coding_mode[0] = mode = get_bits(gb, 6);
00309
00310 for (i = 1; i < wlength; i++) {
00311 int c = get_unary(gb, 1, 6);
00312
00313 switch (c) {
00314 case 6:
00315 mode = get_bits(gb, 6);
00316 break;
00317 case 5:
00318 case 4:
00319 case 3: {
00320
00321 int sign = get_bits1(gb);
00322 mode += (-sign ^ (c - 1)) + sign;
00323 break;
00324 }
00325 case 2:
00326 mode++;
00327 break;
00328 case 1:
00329 mode--;
00330 break;
00331 }
00332 s->coding_mode[i] = mode;
00333 }
00334
00335 i = 0;
00336 while (i < wlength) {
00337 int len = 0;
00338
00339 mode = s->coding_mode[i];
00340 do {
00341 if (i >= wlength - 1)
00342 len += rval;
00343 else
00344 len += s->uval;
00345 i++;
00346
00347 if (i == wlength)
00348 break;
00349 } while (s->coding_mode[i] == mode);
00350
00351 if ((ret = decode_segment(s, mode, decoded, len)) < 0)
00352 return ret;
00353 decoded += len;
00354 }
00355 } else {
00356 mode = get_bits(gb, 6);
00357 if ((ret = decode_segment(s, mode, decoded, length)) < 0)
00358 return ret;
00359 }
00360
00361 return 0;
00362 }
00363
00364 static int get_bits_esc4(GetBitContext *gb)
00365 {
00366 if (get_bits1(gb))
00367 return get_bits(gb, 4) + 1;
00368 else
00369 return 0;
00370 }
00371
00372 static int decode_subframe(TAKDecContext *s, int32_t *decoded,
00373 int subframe_size, int prev_subframe_size)
00374 {
00375 GetBitContext *gb = &s->gb;
00376 int tmp, x, y, i, j, ret = 0;
00377 int dshift, size, filter_quant, filter_order;
00378 int tfilter[MAX_PREDICTORS];
00379
00380 if (!get_bits1(gb))
00381 return decode_residues(s, decoded, subframe_size);
00382
00383 filter_order = predictor_sizes[get_bits(gb, 4)];
00384
00385 if (prev_subframe_size > 0 && get_bits1(gb)) {
00386 if (filter_order > prev_subframe_size)
00387 return AVERROR_INVALIDDATA;
00388
00389 decoded -= filter_order;
00390 subframe_size += filter_order;
00391
00392 if (filter_order > subframe_size)
00393 return AVERROR_INVALIDDATA;
00394 } else {
00395 int lpc_mode;
00396
00397 if (filter_order > subframe_size)
00398 return AVERROR_INVALIDDATA;
00399
00400 lpc_mode = get_bits(gb, 2);
00401 if (lpc_mode > 2)
00402 return AVERROR_INVALIDDATA;
00403
00404 if ((ret = decode_residues(s, decoded, filter_order)) < 0)
00405 return ret;
00406
00407 if (lpc_mode)
00408 decode_lpc(decoded, lpc_mode, filter_order);
00409 }
00410
00411 dshift = get_bits_esc4(gb);
00412 size = get_bits1(gb) + 6;
00413
00414 filter_quant = 10;
00415 if (get_bits1(gb)) {
00416 filter_quant -= get_bits(gb, 3) + 1;
00417 if (filter_quant < 3)
00418 return AVERROR_INVALIDDATA;
00419 }
00420
00421 s->predictors[0] = get_sbits(gb, 10);
00422 s->predictors[1] = get_sbits(gb, 10);
00423 s->predictors[2] = get_sbits(gb, size) << (10 - size);
00424 s->predictors[3] = get_sbits(gb, size) << (10 - size);
00425 if (filter_order > 4) {
00426 tmp = size - get_bits1(gb);
00427
00428 for (i = 4; i < filter_order; i++) {
00429 if (!(i & 3))
00430 x = tmp - get_bits(gb, 2);
00431 s->predictors[i] = get_sbits(gb, x) << (10 - size);
00432 }
00433 }
00434
00435 tfilter[0] = s->predictors[0] << 6;
00436 for (i = 1; i < filter_order; i++) {
00437 int32_t *p1 = &tfilter[0];
00438 int32_t *p2 = &tfilter[i - 1];
00439
00440 for (j = 0; j < (i + 1) / 2; j++) {
00441 x = *p1 + (s->predictors[i] * *p2 + 256 >> 9);
00442 *p2 += s->predictors[i] * *p1 + 256 >> 9;
00443 *p1++ = x;
00444 p2--;
00445 }
00446
00447 tfilter[i] = s->predictors[i] << 6;
00448 }
00449
00450 x = 1 << (32 - (15 - filter_quant));
00451 y = 1 << ((15 - filter_quant) - 1);
00452 for (i = 0, j = filter_order - 1; i < filter_order / 2; i++, j--) {
00453 tmp = y + tfilter[j];
00454 s->filter[j] = x - ((tfilter[i] + y) >> (15 - filter_quant));
00455 s->filter[i] = x - ((tfilter[j] + y) >> (15 - filter_quant));
00456 }
00457
00458 if ((ret = decode_residues(s, &decoded[filter_order],
00459 subframe_size - filter_order)) < 0)
00460 return ret;
00461
00462 for (i = 0; i < filter_order; i++)
00463 s->residues[i] = *decoded++ >> dshift;
00464
00465 y = FF_ARRAY_ELEMS(s->residues) - filter_order;
00466 x = subframe_size - filter_order;
00467 while (x > 0) {
00468 tmp = FFMIN(y, x);
00469
00470 for (i = 0; i < tmp; i++) {
00471 int v = 1 << (filter_quant - 1);
00472
00473 if (!(filter_order & 15)) {
00474 v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
00475 filter_order);
00476 } else if (filter_order & 4) {
00477 for (j = 0; j < filter_order; j += 4) {
00478 v += s->residues[i + j + 3] * s->filter[j + 3] +
00479 s->residues[i + j + 2] * s->filter[j + 2] +
00480 s->residues[i + j + 1] * s->filter[j + 1] +
00481 s->residues[i + j ] * s->filter[j ];
00482 }
00483 } else {
00484 for (j = 0; j < filter_order; j += 8) {
00485 v += s->residues[i + j + 7] * s->filter[j + 7] +
00486 s->residues[i + j + 6] * s->filter[j + 6] +
00487 s->residues[i + j + 5] * s->filter[j + 5] +
00488 s->residues[i + j + 4] * s->filter[j + 4] +
00489 s->residues[i + j + 3] * s->filter[j + 3] +
00490 s->residues[i + j + 2] * s->filter[j + 2] +
00491 s->residues[i + j + 1] * s->filter[j + 1] +
00492 s->residues[i + j ] * s->filter[j ];
00493 }
00494 }
00495 v = (av_clip(v >> filter_quant, -8192, 8191) << dshift) - *decoded;
00496 *decoded++ = v;
00497 s->residues[filter_order + i] = v >> dshift;
00498 }
00499
00500 x -= tmp;
00501 if (x > 0)
00502 memcpy(s->residues, &s->residues[y], 2 * filter_order);
00503 }
00504
00505 emms_c();
00506
00507 return 0;
00508 }
00509
00510 static int decode_channel(TAKDecContext *s, int chan)
00511 {
00512 AVCodecContext *avctx = s->avctx;
00513 GetBitContext *gb = &s->gb;
00514 int32_t *decoded = s->decoded[chan];
00515 int left = s->nb_samples - 1;
00516 int i = 0, ret, prev = 0;
00517
00518 s->sample_shift[chan] = get_bits_esc4(gb);
00519 if (s->sample_shift[chan] >= avctx->bits_per_raw_sample)
00520 return AVERROR_INVALIDDATA;
00521
00522 *decoded++ = get_sbits(gb, avctx->bits_per_raw_sample - s->sample_shift[chan]);
00523 s->lpc_mode[chan] = get_bits(gb, 2);
00524 s->nb_subframes = get_bits(gb, 3) + 1;
00525
00526 if (s->nb_subframes > 1) {
00527 if (get_bits_left(gb) < (s->nb_subframes - 1) * 6)
00528 return AVERROR_INVALIDDATA;
00529
00530 for (; i < s->nb_subframes - 1; i++) {
00531 int v = get_bits(gb, 6);
00532
00533 s->subframe_len[i] = (v - prev) * s->subframe_scale;
00534 if (s->subframe_len[i] <= 0)
00535 return AVERROR_INVALIDDATA;
00536
00537 left -= s->subframe_len[i];
00538 prev = v;
00539 }
00540
00541 if (left <= 0)
00542 return AVERROR_INVALIDDATA;
00543 }
00544 s->subframe_len[i] = left;
00545
00546 prev = 0;
00547 for (i = 0; i < s->nb_subframes; i++) {
00548 if ((ret = decode_subframe(s, decoded, s->subframe_len[i], prev)) < 0)
00549 return ret;
00550 decoded += s->subframe_len[i];
00551 prev = s->subframe_len[i];
00552 }
00553
00554 return 0;
00555 }
00556
00557 static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
00558 {
00559 GetBitContext *gb = &s->gb;
00560 int32_t *p1 = s->decoded[c1] + 1;
00561 int32_t *p2 = s->decoded[c2] + 1;
00562 int i;
00563 int dshift, dfactor;
00564
00565 switch (s->dmode) {
00566 case 1:
00567 for (i = 0; i < length; i++) {
00568 int32_t a = p1[i];
00569 int32_t b = p2[i];
00570 p2[i] = a + b;
00571 }
00572 break;
00573 case 2:
00574 for (i = 0; i < length; i++) {
00575 int32_t a = p1[i];
00576 int32_t b = p2[i];
00577 p1[i] = b - a;
00578 }
00579 break;
00580 case 3:
00581 for (i = 0; i < length; i++) {
00582 int32_t a = p1[i];
00583 int32_t b = p2[i];
00584 a -= b >> 1;
00585 p1[i] = a;
00586 p2[i] = a + b;
00587 }
00588 break;
00589 case 4:
00590 FFSWAP(int32_t*, p1, p2);
00591 case 5:
00592 dshift = get_bits_esc4(gb);
00593 dfactor = get_sbits(gb, 10);
00594 for (i = 0; i < length; i++) {
00595 int32_t a = p1[i];
00596 int32_t b = p2[i];
00597 b = dfactor * (b >> dshift) + 128 >> 8 << dshift;
00598 p1[i] = b - a;
00599 }
00600 break;
00601 case 6:
00602 FFSWAP(int32_t*, p1, p2);
00603 case 7: {
00604 int length2, order_half, filter_order, dval1, dval2;
00605 int tmp, x, code_size;
00606
00607 if (length < 256)
00608 return AVERROR_INVALIDDATA;
00609
00610 dshift = get_bits_esc4(gb);
00611 filter_order = 8 << get_bits1(gb);
00612 dval1 = get_bits1(gb);
00613 dval2 = get_bits1(gb);
00614
00615 for (i = 0; i < filter_order; i++) {
00616 if (!(i & 3))
00617 code_size = 14 - get_bits(gb, 3);
00618 s->filter[i] = get_sbits(gb, code_size);
00619 }
00620
00621 order_half = filter_order / 2;
00622 length2 = length - (filter_order - 1);
00623
00624
00625 if (dval1) {
00626 for (i = 0; i < order_half; i++) {
00627 int32_t a = p1[i];
00628 int32_t b = p2[i];
00629 p1[i] = a + b;
00630 }
00631 }
00632
00633
00634 if (dval2) {
00635 for (i = length2 + order_half; i < length; i++) {
00636 int32_t a = p1[i];
00637 int32_t b = p2[i];
00638 p1[i] = a + b;
00639 }
00640 }
00641
00642
00643 for (i = 0; i < filter_order; i++)
00644 s->residues[i] = *p2++ >> dshift;
00645
00646 p1 += order_half;
00647 x = FF_ARRAY_ELEMS(s->residues) - filter_order;
00648 for (; length2 > 0; length2 -= tmp) {
00649 tmp = FFMIN(length2, x);
00650
00651 for (i = 0; i < tmp; i++)
00652 s->residues[filter_order + i] = *p2++ >> dshift;
00653
00654 for (i = 0; i < tmp; i++) {
00655 int v = 1 << 9;
00656
00657 if (filter_order == 16) {
00658 v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
00659 filter_order);
00660 } else {
00661 v += s->residues[i + 7] * s->filter[7] +
00662 s->residues[i + 6] * s->filter[6] +
00663 s->residues[i + 5] * s->filter[5] +
00664 s->residues[i + 4] * s->filter[4] +
00665 s->residues[i + 3] * s->filter[3] +
00666 s->residues[i + 2] * s->filter[2] +
00667 s->residues[i + 1] * s->filter[1] +
00668 s->residues[i ] * s->filter[0];
00669 }
00670
00671 v = (av_clip(v >> 10, -8192, 8191) << dshift) - *p1;
00672 *p1++ = v;
00673 }
00674
00675 memcpy(s->residues, &s->residues[tmp], 2 * filter_order);
00676 }
00677
00678 emms_c();
00679 break;
00680 }
00681 }
00682
00683 return 0;
00684 }
00685
00686 static int tak_decode_frame(AVCodecContext *avctx, void *data,
00687 int *got_frame_ptr, AVPacket *pkt)
00688 {
00689 TAKDecContext *s = avctx->priv_data;
00690 GetBitContext *gb = &s->gb;
00691 int chan, i, ret, hsize;
00692
00693 if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
00694 return AVERROR_INVALIDDATA;
00695
00696 init_get_bits(gb, pkt->data, pkt->size * 8);
00697
00698 if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0)
00699 return ret;
00700
00701 if (avctx->err_recognition & AV_EF_CRCCHECK) {
00702 hsize = get_bits_count(gb) / 8;
00703 if (ff_tak_check_crc(pkt->data, hsize)) {
00704 av_log(avctx, AV_LOG_ERROR, "CRC error\n");
00705 return AVERROR_INVALIDDATA;
00706 }
00707 }
00708
00709 if (s->ti.codec != TAK_CODEC_MONO_STEREO &&
00710 s->ti.codec != TAK_CODEC_MULTICHANNEL) {
00711 av_log(avctx, AV_LOG_ERROR, "unsupported codec: %d\n", s->ti.codec);
00712 return AVERROR_PATCHWELCOME;
00713 }
00714 if (s->ti.data_type) {
00715 av_log(avctx, AV_LOG_ERROR,
00716 "unsupported data type: %d\n", s->ti.data_type);
00717 return AVERROR_INVALIDDATA;
00718 }
00719 if (s->ti.codec == TAK_CODEC_MONO_STEREO && s->ti.channels > 2) {
00720 av_log(avctx, AV_LOG_ERROR,
00721 "invalid number of channels: %d\n", s->ti.channels);
00722 return AVERROR_INVALIDDATA;
00723 }
00724 if (s->ti.channels > 6) {
00725 av_log(avctx, AV_LOG_ERROR,
00726 "unsupported number of channels: %d\n", s->ti.channels);
00727 return AVERROR_INVALIDDATA;
00728 }
00729
00730 if (s->ti.frame_samples <= 0) {
00731 av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of samples\n");
00732 return AVERROR_INVALIDDATA;
00733 }
00734
00735 if (s->ti.bps != avctx->bits_per_raw_sample) {
00736 avctx->bits_per_raw_sample = s->ti.bps;
00737 if ((ret = set_bps_params(avctx)) < 0)
00738 return ret;
00739 }
00740 if (s->ti.sample_rate != avctx->sample_rate) {
00741 avctx->sample_rate = s->ti.sample_rate;
00742 set_sample_rate_params(avctx);
00743 }
00744 if (s->ti.ch_layout)
00745 avctx->channel_layout = s->ti.ch_layout;
00746 avctx->channels = s->ti.channels;
00747
00748 s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples
00749 : s->ti.frame_samples;
00750
00751 s->frame.nb_samples = s->nb_samples;
00752 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0)
00753 return ret;
00754
00755 if (avctx->bits_per_raw_sample <= 16) {
00756 int buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
00757 s->nb_samples,
00758 AV_SAMPLE_FMT_S32P, 0);
00759 av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size, buf_size);
00760 if (!s->decode_buffer)
00761 return AVERROR(ENOMEM);
00762 ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
00763 s->decode_buffer, avctx->channels,
00764 s->nb_samples, AV_SAMPLE_FMT_S32P, 0);
00765 if (ret < 0)
00766 return ret;
00767 } else {
00768 for (chan = 0; chan < avctx->channels; chan++)
00769 s->decoded[chan] = (int32_t *)s->frame.extended_data[chan];
00770 }
00771
00772 if (s->nb_samples < 16) {
00773 for (chan = 0; chan < avctx->channels; chan++) {
00774 int32_t *decoded = s->decoded[chan];
00775 for (i = 0; i < s->nb_samples; i++)
00776 decoded[i] = get_sbits(gb, avctx->bits_per_raw_sample);
00777 }
00778 } else {
00779 if (s->ti.codec == TAK_CODEC_MONO_STEREO) {
00780 for (chan = 0; chan < avctx->channels; chan++)
00781 if (ret = decode_channel(s, chan))
00782 return ret;
00783
00784 if (avctx->channels == 2) {
00785 s->nb_subframes = get_bits(gb, 1) + 1;
00786 if (s->nb_subframes > 1) {
00787 s->subframe_len[1] = get_bits(gb, 6);
00788 }
00789
00790 s->dmode = get_bits(gb, 3);
00791 if (ret = decorrelate(s, 0, 1, s->nb_samples - 1))
00792 return ret;
00793 }
00794 } else if (s->ti.codec == TAK_CODEC_MULTICHANNEL) {
00795 if (get_bits1(gb)) {
00796 int ch_mask = 0;
00797
00798 chan = get_bits(gb, 4) + 1;
00799 if (chan > avctx->channels)
00800 return AVERROR_INVALIDDATA;
00801
00802 for (i = 0; i < chan; i++) {
00803 int nbit = get_bits(gb, 4);
00804
00805 if (nbit >= avctx->channels)
00806 return AVERROR_INVALIDDATA;
00807
00808 if (ch_mask & 1 << nbit)
00809 return AVERROR_INVALIDDATA;
00810
00811 s->mcdparams[i].present = get_bits1(gb);
00812 if (s->mcdparams[i].present) {
00813 s->mcdparams[i].index = get_bits(gb, 2);
00814 s->mcdparams[i].chan2 = get_bits(gb, 4);
00815 if (s->mcdparams[i].index == 1) {
00816 if ((nbit == s->mcdparams[i].chan2) ||
00817 (ch_mask & 1 << s->mcdparams[i].chan2))
00818 return AVERROR_INVALIDDATA;
00819
00820 ch_mask |= 1 << s->mcdparams[i].chan2;
00821 } else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) {
00822 return AVERROR_INVALIDDATA;
00823 }
00824 }
00825 s->mcdparams[i].chan1 = nbit;
00826
00827 ch_mask |= 1 << nbit;
00828 }
00829 } else {
00830 chan = avctx->channels;
00831 for (i = 0; i < chan; i++) {
00832 s->mcdparams[i].present = 0;
00833 s->mcdparams[i].chan1 = i;
00834 }
00835 }
00836
00837 for (i = 0; i < chan; i++) {
00838 if (s->mcdparams[i].present && s->mcdparams[i].index == 1)
00839 if (ret = decode_channel(s, s->mcdparams[i].chan2))
00840 return ret;
00841
00842 if (ret = decode_channel(s, s->mcdparams[i].chan1))
00843 return ret;
00844
00845 if (s->mcdparams[i].present) {
00846 s->dmode = mc_dmodes[s->mcdparams[i].index];
00847 if (ret = decorrelate(s,
00848 s->mcdparams[i].chan2,
00849 s->mcdparams[i].chan1,
00850 s->nb_samples - 1))
00851 return ret;
00852 }
00853 }
00854 }
00855
00856 for (chan = 0; chan < avctx->channels; chan++) {
00857 int32_t *decoded = s->decoded[chan];
00858
00859 if (s->lpc_mode[chan])
00860 decode_lpc(decoded, s->lpc_mode[chan], s->nb_samples);
00861
00862 if (s->sample_shift[chan] > 0)
00863 for (i = 0; i < s->nb_samples; i++)
00864 decoded[i] <<= s->sample_shift[chan];
00865 }
00866 }
00867
00868 align_get_bits(gb);
00869 skip_bits(gb, 24);
00870 if (get_bits_left(gb) < 0)
00871 av_log(avctx, AV_LOG_DEBUG, "overread\n");
00872 else if (get_bits_left(gb) > 0)
00873 av_log(avctx, AV_LOG_DEBUG, "underread\n");
00874
00875 if (avctx->err_recognition & AV_EF_CRCCHECK) {
00876 if (ff_tak_check_crc(pkt->data + hsize,
00877 get_bits_count(gb) / 8 - hsize)) {
00878 av_log(avctx, AV_LOG_ERROR, "CRC error\n");
00879 return AVERROR_INVALIDDATA;
00880 }
00881 }
00882
00883
00884 switch (avctx->sample_fmt) {
00885 case AV_SAMPLE_FMT_U8P:
00886 for (chan = 0; chan < avctx->channels; chan++) {
00887 uint8_t *samples = (uint8_t *)s->frame.extended_data[chan];
00888 int32_t *decoded = s->decoded[chan];
00889 for (i = 0; i < s->nb_samples; i++)
00890 samples[i] = decoded[i] + 0x80;
00891 }
00892 break;
00893 case AV_SAMPLE_FMT_S16P:
00894 for (chan = 0; chan < avctx->channels; chan++) {
00895 int16_t *samples = (int16_t *)s->frame.extended_data[chan];
00896 int32_t *decoded = s->decoded[chan];
00897 for (i = 0; i < s->nb_samples; i++)
00898 samples[i] = decoded[i];
00899 }
00900 break;
00901 case AV_SAMPLE_FMT_S32P:
00902 for (chan = 0; chan < avctx->channels; chan++) {
00903 int32_t *samples = (int32_t *)s->frame.extended_data[chan];
00904 for (i = 0; i < s->nb_samples; i++)
00905 samples[i] <<= 8;
00906 }
00907 break;
00908 }
00909
00910 *got_frame_ptr = 1;
00911 *(AVFrame *)data = s->frame;
00912
00913 return pkt->size;
00914 }
00915
00916 static av_cold int tak_decode_close(AVCodecContext *avctx)
00917 {
00918 TAKDecContext *s = avctx->priv_data;
00919
00920 av_freep(&s->decode_buffer);
00921
00922 return 0;
00923 }
00924
00925 AVCodec ff_tak_decoder = {
00926 .name = "tak",
00927 .type = AVMEDIA_TYPE_AUDIO,
00928 .id = AV_CODEC_ID_TAK,
00929 .priv_data_size = sizeof(TAKDecContext),
00930 .init = tak_decode_init,
00931 .close = tak_decode_close,
00932 .decode = tak_decode_frame,
00933 .capabilities = CODEC_CAP_DR1,
00934 .long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
00935 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
00936 AV_SAMPLE_FMT_S16P,
00937 AV_SAMPLE_FMT_S32P,
00938 AV_SAMPLE_FMT_NONE },
00939 };