80 { -1, -1, -1, -1, 2, 4, 6, 8 },
81 { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
94 unsigned int min_channels = 1;
95 unsigned int max_channels = 2;
167 int sign,
delta, diff, step;
171 step_index = av_clip(step_index, 0, 88);
178 diff = ((2 * delta + 1) * step) >>
shift;
180 if (sign) predictor -= diff;
181 else predictor += diff;
197 step_index = av_clip(step_index, 0, 88);
199 sign = nibble & (1 <<
shift);
200 delta = nibble & ((1 <<
shift) - 1);
201 diff = ((2 * delta + 1) * step) >>
shift;
203 if (sign) predictor -= diff;
204 else predictor += diff;
220 step_index = av_clip(step_index, 0, 88);
223 if (nibble & 4) diff += step;
224 if (nibble & 2) diff += step >> 1;
225 if (nibble & 1) diff += step >> 2;
243 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->
idelta;
246 c->
sample1 = av_clip_int16(predictor);
259 step_index = av_clip(step_index, 0, 48);
263 diff = ((2 * delta + 1) * step) >> 3;
265 if (sign) predictor -= diff;
266 else predictor += diff;
268 c->
predictor = av_clip(predictor, -2048, 2047);
276 int sign,
delta, diff;
284 diff = ((2 * delta + 1) * c->
step) >> 3;
290 c->
step = av_clip(new_step, 511, 32767);
297 int sign,
delta, diff;
299 sign = nibble & (1<<(size-1));
300 delta = nibble & ((1<<(size-1))-1);
307 if (delta >= (2*size - 3) && c->
step < 3)
309 else if (delta == 0 && c->
step > 0)
325 c->
step = av_clip(c->
step, 127, 24567);
338 out0 += sample_offset;
342 out1 += sample_offset;
345 shift = 12 - (in[4+i*2] & 15);
346 filter = in[4+i*2] >> 4;
361 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
363 s_1 = av_clip_int16(s);
374 shift = 12 - (in[5+i*2] & 15);
375 filter = in[5+i*2] >> 4;
388 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
390 s_1 = av_clip_int16(s);
402 out0 += 28 * (3 - channels);
403 out1 += 28 * (3 - channels);
414 int k0, signmask, nb_bits,
count;
415 int size = buf_size*8;
423 k0 = 1 << (nb_bits-2);
424 signmask = 1 << (nb_bits-1);
427 for (i = 0; i < avctx->
channels; i++) {
435 for (i = 0; i < avctx->
channels; i++) {
450 if (delta & signmask)
478 int buf_size,
int *coded_samples,
int *approx_nb_samples)
483 int has_coded_samples = 0;
487 *approx_nb_samples = 0;
495 if (buf_size < 76 * ch)
500 if (buf_size < 34 * ch)
511 nb_samples = buf_size * 2 / ch;
526 return (buf_size - header_size) * 2 / ch;
531 has_coded_samples = 1;
532 *coded_samples = bytestream2_get_le32(gb);
533 *coded_samples -= *coded_samples % 28;
534 nb_samples = (buf_size - 12) / 30 * 28;
537 has_coded_samples = 1;
538 *coded_samples = bytestream2_get_le32(gb);
539 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
542 nb_samples = (buf_size - ch) / ch * 2;
549 has_coded_samples = 1;
552 header_size = 4 + 9 * ch;
553 *coded_samples = bytestream2_get_le32(gb);
556 header_size = 4 + 5 * ch;
557 *coded_samples = bytestream2_get_le32(gb);
558 *approx_nb_samples = 1;
561 header_size = 4 + 5 * ch;
562 *coded_samples = bytestream2_get_be32(gb);
563 *approx_nb_samples = 1;
566 *coded_samples -= *coded_samples % 28;
567 nb_samples = (buf_size - header_size) * 2 / ch;
568 nb_samples -= nb_samples % 28;
573 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
578 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
583 nb_samples = (buf_size - 4 * ch) * 2 / ch;
591 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
597 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
603 int samples_per_byte;
613 nb_samples += buf_size * samples_per_byte / ch;
618 int buf_bits = buf_size * 8 - 2;
619 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
620 int block_hdr_size = 22 * ch;
621 int block_size = block_hdr_size + nbits * ch * 4095;
622 int nblocks = buf_bits / block_size;
623 int bits_left = buf_bits - nblocks * block_size;
624 nb_samples = nblocks * 4096;
625 if (bits_left >= block_hdr_size)
626 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
631 nb_samples = buf_size / (8 * ch) * 14;
634 has_coded_samples = 1;
636 *coded_samples = bytestream2_get_be32(gb);
637 *coded_samples -= *coded_samples % 14;
638 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
641 nb_samples = buf_size / (9 * ch) * 16;
644 nb_samples = (buf_size / 128) * 224 / ch;
647 nb_samples = buf_size / (16 * ch) * 28;
652 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
659 int *got_frame_ptr,
AVPacket *avpkt)
663 int buf_size = avpkt->
size;
666 int n,
m, channel, i;
671 int nb_samples, coded_samples, approx_nb_samples,
ret;
675 nb_samples =
get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
676 if (nb_samples <= 0) {
685 samples = (
short *)frame->
data[0];
691 if (!approx_nb_samples && coded_samples != nb_samples)
693 frame->
nb_samples = nb_samples = coded_samples;
702 for (channel = 0; channel < avctx->
channels; channel++) {
705 cs = &(c->
status[channel]);
709 predictor =
sign_extend(bytestream2_get_be16u(&gb), 16);
710 step_index = predictor & 0x7F;
731 samples = samples_p[channel];
733 for (m = 0; m < 64; m += 2) {
734 int byte = bytestream2_get_byteu(&gb);
758 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
759 for (i = 0; i < avctx->
channels; i++) {
761 samples = &samples_p[i][1 + n * samples_per_block];
762 for (m = 0; m < samples_per_block; m++) {
770 for (n = 0; n < (nb_samples - 1) / 8; n++) {
771 for (i = 0; i < avctx->
channels; i++) {
773 samples = &samples_p[i][1 + n * 8];
774 for (m = 0; m < 8; m += 2) {
775 int v = bytestream2_get_byteu(&gb);
784 for (i = 0; i < avctx->
channels; i++)
787 for (i = 0; i < avctx->
channels; i++) {
796 for (i = 0; i < avctx->
channels; i++) {
797 samples = (int16_t *)frame->
data[i];
799 for (n = nb_samples >> 1; n > 0; n--) {
800 int v = bytestream2_get_byteu(&gb);
810 block_predictor = bytestream2_get_byteu(&gb);
811 if (block_predictor > 6) {
819 block_predictor = bytestream2_get_byteu(&gb);
820 if (block_predictor > 6) {
842 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
843 int byte = bytestream2_get_byteu(&gb);
850 for (channel = 0; channel < avctx->
channels; channel++) {
860 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
861 int v = bytestream2_get_byteu(&gb);
870 int decode_top_nibble_next = 0;
872 const int16_t *samples_end = samples + avctx->
channels * nb_samples;
888 #define DK3_GET_NEXT_NIBBLE() \
889 if (decode_top_nibble_next) { \
890 nibble = last_byte >> 4; \
891 decode_top_nibble_next = 0; \
893 last_byte = bytestream2_get_byteu(&gb); \
894 nibble = last_byte & 0x0F; \
895 decode_top_nibble_next = 1; \
898 while (samples < samples_end) {
931 for (channel = 0; channel < avctx->
channels; channel++) {
942 for (n = nb_samples >> (1 - st); n > 0; n--) {
944 int v = bytestream2_get_byteu(&gb);
959 int v = bytestream2_get_byteu(&gb);
966 int v = bytestream2_get_byteu(&gb);
972 for (channel = 0; channel < avctx->
channels; channel++) {
982 for (n = 0; n < nb_samples / 2; n++) {
985 byte[0] = bytestream2_get_byteu(&gb);
987 byte[1] = bytestream2_get_byteu(&gb);
988 for(channel = 0; channel < avctx->
channels; channel++) {
991 for(channel = 0; channel < avctx->
channels; channel++) {
998 for (channel = 0; channel < avctx->
channels; channel++) {
999 int16_t *smp = samples_p[channel];
1001 for (n = nb_samples / 2; n > 0; n--) {
1002 int v = bytestream2_get_byteu(&gb);
1008 for (n = nb_samples / 2; n > 0; n--) {
1009 for (channel = 0; channel < avctx->
channels; channel++) {
1010 int v = bytestream2_get_byteu(&gb);
1021 int16_t *out0 = samples_p[0];
1022 int16_t *out1 = samples_p[1];
1023 int samples_per_block = 28 * (3 - avctx->
channels) * 4;
1024 int sample_offset = 0;
1028 avctx->
channels, sample_offset)) < 0)
1031 sample_offset += samples_per_block;
1036 for (i=0; i<=st; i++) {
1044 for (i=0; i<=st; i++)
1047 for (n = nb_samples >> (1 - st); n > 0; n--) {
1048 int byte = bytestream2_get_byteu(&gb);
1054 for (n = nb_samples >> (1 - st); n > 0; n--) {
1055 int byte = bytestream2_get_byteu(&gb);
1062 int previous_left_sample, previous_right_sample;
1063 int current_left_sample, current_right_sample;
1064 int next_left_sample, next_right_sample;
1065 int coeff1l, coeff2l, coeff1r, coeff2r;
1066 int shift_left, shift_right;
1074 current_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1075 previous_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1076 current_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1077 previous_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1079 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1080 int byte = bytestream2_get_byteu(&gb);
1086 byte = bytestream2_get_byteu(&gb);
1087 shift_left = 20 - (byte >> 4);
1088 shift_right = 20 - (byte & 0x0F);
1090 for (count2 = 0; count2 < 28; count2++) {
1091 byte = bytestream2_get_byteu(&gb);
1092 next_left_sample =
sign_extend(byte >> 4, 4) << shift_left;
1093 next_right_sample =
sign_extend(byte, 4) << shift_right;
1095 next_left_sample = (next_left_sample +
1096 (current_left_sample * coeff1l) +
1097 (previous_left_sample * coeff2l) + 0x80) >> 8;
1098 next_right_sample = (next_right_sample +
1099 (current_right_sample * coeff1r) +
1100 (previous_right_sample * coeff2r) + 0x80) >> 8;
1102 previous_left_sample = current_left_sample;
1103 current_left_sample = av_clip_int16(next_left_sample);
1104 previous_right_sample = current_right_sample;
1105 current_right_sample = av_clip_int16(next_right_sample);
1106 *samples++ = current_left_sample;
1107 *samples++ = current_right_sample;
1119 for(channel = 0; channel < avctx->
channels; channel++) {
1120 int byte = bytestream2_get_byteu(&gb);
1123 shift[channel] = 20 - (byte & 0x0F);
1125 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1128 byte[0] = bytestream2_get_byteu(&gb);
1129 if (st) byte[1] = bytestream2_get_byteu(&gb);
1130 for(i = 4; i >= 0; i-=4) {
1131 for(channel = 0; channel < avctx->
channels; channel++) {
1135 c->
status[channel].
sample2 * coeff[channel][1] + 0x80) >> 8;
1153 int previous_sample, current_sample, next_sample;
1156 unsigned int channel;
1161 for (channel=0; channel<avctx->
channels; channel++)
1162 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1163 bytestream2_get_le32(&gb)) +
1166 for (channel=0; channel<avctx->
channels; channel++) {
1168 samplesC = samples_p[channel];
1171 current_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1172 previous_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1178 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1179 int byte = bytestream2_get_byte(&gb);
1181 current_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1182 previous_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1184 for (count2=0; count2<28; count2++)
1185 *samplesC++ =
sign_extend(bytestream2_get_be16(&gb), 16);
1189 shift = 20 - (byte & 0x0F);
1191 for (count2=0; count2<28; count2++) {
1195 byte = bytestream2_get_byte(&gb);
1199 next_sample += (current_sample * coeff1) +
1200 (previous_sample * coeff2);
1201 next_sample = av_clip_int16(next_sample >> 8);
1203 previous_sample = current_sample;
1204 current_sample = next_sample;
1205 *samplesC++ = current_sample;
1211 }
else if (count != count1) {
1213 count =
FFMAX(count, count1);
1227 for (channel=0; channel<avctx->
channels; channel++) {
1229 int16_t *
s = samples_p[channel];
1230 for (n = 0; n < 4; n++, s += 32) {
1236 val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1237 shift[
n] = 20 - (val & 0x0F);
1241 for (m=2; m<32; m+=2) {
1242 s = &samples_p[channel][
m];
1243 for (n = 0; n < 4; n++, s += 32) {
1245 int byte = bytestream2_get_byteu(&gb);
1248 pred = s[-1] * coeff[0][
n] + s[-2] * coeff[1][
n];
1249 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1252 pred = s[0] * coeff[0][
n] + s[-1] * coeff[1][
n];
1253 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1268 for (n = nb_samples >> (1 - st); n > 0; n--) {
1269 int v = bytestream2_get_byteu(&gb);
1276 for (i = 0; i < avctx->
channels; i++) {
1287 for (n = nb_samples >> (1 - st); n > 0; n--) {
1288 int v = bytestream2_get_byteu(&gb);
1295 for (n = nb_samples >> (1 - st); n > 0; n--) {
1296 int v = bytestream2_get_byteu(&gb);
1306 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1308 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1313 for (n = nb_samples >> (1 - st); n > 0; n--) {
1314 int byte = bytestream2_get_byteu(&gb);
1321 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1322 int byte = bytestream2_get_byteu(&gb);
1326 (byte >> 2) & 0x07, 3, 0);
1331 for (n = nb_samples >> (2 - st); n > 0; n--) {
1332 int byte = bytestream2_get_byteu(&gb);
1336 (byte >> 4) & 0x03, 2, 2);
1338 (byte >> 2) & 0x03, 2, 2);
1349 for (n = nb_samples >> (1 - st); n > 0; n--) {
1350 int v = bytestream2_get_byteu(&gb);
1357 int samples_per_block;
1361 samples_per_block = avctx->
extradata[0] / 16;
1362 blocks = nb_samples / avctx->
extradata[0];
1364 samples_per_block = nb_samples / 16;
1368 for (m = 0; m < blocks; m++) {
1369 for (channel = 0; channel < avctx->
channels; channel++) {
1373 samples = samples_p[channel] + m * 16;
1375 for (i = 0; i < samples_per_block; i++) {
1376 int byte = bytestream2_get_byteu(&gb);
1377 int scale = 1 << (byte >> 4);
1378 int index = byte & 0xf;
1383 for (n = 0; n < 16; n++) {
1389 byte = bytestream2_get_byteu(&gb);
1393 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1394 ((sampledat * scale) << 11)) >> 11;
1395 *samples = av_clip_int16(sampledat);
1421 for (i = 0; i < avctx->
channels; i++)
1422 for (n = 0; n < 16; n++)
1423 table[i][n] =
sign_extend(bytestream2_get_be16u(&tb), 16);
1425 for (i = 0; i < avctx->
channels; i++)
1426 for (n = 0; n < 16; n++)
1427 table[i][n] =
sign_extend(bytestream2_get_be16u(&gb), 16);
1430 for (i = 0; i < avctx->
channels; i++) {
1436 for (ch = 0; ch < avctx->
channels; ch++) {
1437 samples = samples_p[ch];
1440 for (i = 0; i < nb_samples / 14; i++) {
1441 int byte = bytestream2_get_byteu(&gb);
1442 int index = (byte >> 4) & 7;
1443 unsigned int exp = byte & 0x0F;
1444 int factor1 = table[ch][index * 2];
1445 int factor2 = table[ch][index * 2 + 1];
1448 for (n = 0; n < 14; n++) {
1454 byte = bytestream2_get_byteu(&gb);
1459 + c->
status[ch].
sample2 * factor2) >> 11) + (sampledat << exp);
1460 *samples = av_clip_int16(sampledat);
1469 for (channel = 0; channel < avctx->
channels; channel++) {
1470 samples = samples_p[channel];
1473 for (i = 0; i < nb_samples / 28; i++) {
1477 header = bytestream2_get_byteu(&gb);
1481 for (n = 0; n < 28; n++) {
1484 switch (header >> 4) {
1498 prev = av_clip((prev + 0x20) >> 6, -0x200000, 0x1fffff);
1500 byte = bytestream2_get_byteu(&gb);
1506 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1507 *samples++ = av_clip_int16(sampledat >> 6);
1540 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1541 AVCodec ff_ ## name_ ## _decoder = { \
1543 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1544 .type = AVMEDIA_TYPE_AUDIO, \
1546 .priv_data_size = sizeof(ADPCMDecodeContext), \
1547 .init = adpcm_decode_init, \
1548 .decode = adpcm_decode_frame, \
1549 .capabilities = CODEC_CAP_DR1, \
1550 .sample_fmts = sample_fmts_, \