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;
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);
284 diff = ((2 * delta + 1) * c->
step) >> 3;
290 c->
step = av_clip(new_step, 511, 32767);
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)
476 int buf_size,
int *coded_samples)
481 int has_coded_samples = 0;
492 if (buf_size < 76 * ch)
497 if (buf_size < 34 * ch)
508 nb_samples = buf_size * 2 / ch;
523 return (buf_size - header_size) * 2 / ch;
528 has_coded_samples = 1;
529 *coded_samples = bytestream2_get_le32(gb);
530 *coded_samples -= *coded_samples % 28;
531 nb_samples = (buf_size - 12) / 30 * 28;
534 has_coded_samples = 1;
535 *coded_samples = bytestream2_get_le32(gb);
536 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
539 nb_samples = (buf_size - ch) / ch * 2;
546 has_coded_samples = 1;
549 header_size = 4 + 9 * ch;
550 *coded_samples = bytestream2_get_le32(gb);
553 header_size = 4 + 5 * ch;
554 *coded_samples = bytestream2_get_le32(gb);
557 header_size = 4 + 5 * ch;
558 *coded_samples = bytestream2_get_be32(gb);
561 *coded_samples -= *coded_samples % 28;
562 nb_samples = (buf_size - header_size) * 2 / ch;
563 nb_samples -= nb_samples % 28;
568 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
573 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
578 nb_samples = (buf_size - 4 * ch) * 2 / ch;
586 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
592 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
598 int samples_per_byte;
608 nb_samples += buf_size * samples_per_byte / ch;
613 int buf_bits = buf_size * 8 - 2;
614 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
615 int block_hdr_size = 22 * ch;
616 int block_size = block_hdr_size + nbits * ch * 4095;
617 int nblocks = buf_bits / block_size;
618 int bits_left = buf_bits - nblocks * block_size;
619 nb_samples = nblocks * 4096;
620 if (bits_left >= block_hdr_size)
621 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
626 nb_samples = buf_size / (8 * ch) * 14;
629 has_coded_samples = 1;
631 *coded_samples = bytestream2_get_be32(gb);
632 *coded_samples -= *coded_samples % 14;
633 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
636 nb_samples = buf_size / (9 * ch) * 16;
639 nb_samples = (buf_size / 128) * 224 / ch;
642 nb_samples = buf_size / (16 * ch) * 28;
647 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
654 int *got_frame_ptr,
AVPacket *avpkt)
658 int buf_size = avpkt->
size;
661 int n,
m, channel, i;
666 int nb_samples, coded_samples,
ret;
670 nb_samples =
get_nb_samples(avctx, &gb, buf_size, &coded_samples);
671 if (nb_samples <= 0) {
680 samples = (
short *)frame->
data[0];
686 if (coded_samples != nb_samples)
688 frame->
nb_samples = nb_samples = coded_samples;
697 for (channel = 0; channel < avctx->
channels; channel++) {
700 cs = &(c->
status[channel]);
704 predictor =
sign_extend(bytestream2_get_be16u(&gb), 16);
705 step_index = predictor & 0x7F;
726 samples = samples_p[channel];
728 for (m = 0; m < 64; m += 2) {
729 int byte = bytestream2_get_byteu(&gb);
753 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
754 for (i = 0; i < avctx->
channels; i++) {
756 samples = &samples_p[i][1 + n * samples_per_block];
757 for (m = 0; m < samples_per_block; m++) {
765 for (n = 0; n < (nb_samples - 1) / 8; n++) {
766 for (i = 0; i < avctx->
channels; i++) {
768 samples = &samples_p[i][1 + n * 8];
769 for (m = 0; m < 8; m += 2) {
770 int v = bytestream2_get_byteu(&gb);
779 for (i = 0; i < avctx->
channels; i++)
782 for (i = 0; i < avctx->
channels; i++) {
791 for (i = 0; i < avctx->
channels; i++) {
792 samples = (int16_t *)frame->
data[i];
794 for (n = nb_samples >> 1; n > 0; n--) {
795 int v = bytestream2_get_byteu(&gb);
805 block_predictor = bytestream2_get_byteu(&gb);
806 if (block_predictor > 6) {
814 block_predictor = bytestream2_get_byteu(&gb);
815 if (block_predictor > 6) {
837 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
838 int byte = bytestream2_get_byteu(&gb);
845 for (channel = 0; channel < avctx->
channels; channel++) {
855 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
856 int v = bytestream2_get_byteu(&gb);
865 int decode_top_nibble_next = 0;
867 const int16_t *samples_end = samples + avctx->
channels * nb_samples;
883 #define DK3_GET_NEXT_NIBBLE() \
884 if (decode_top_nibble_next) { \
885 nibble = last_byte >> 4; \
886 decode_top_nibble_next = 0; \
888 last_byte = bytestream2_get_byteu(&gb); \
889 nibble = last_byte & 0x0F; \
890 decode_top_nibble_next = 1; \
893 while (samples < samples_end) {
923 for (channel = 0; channel < avctx->
channels; channel++) {
934 for (n = nb_samples >> (1 - st); n > 0; n--) {
936 int v = bytestream2_get_byteu(&gb);
951 int v = bytestream2_get_byteu(&gb);
958 int v = bytestream2_get_byteu(&gb);
964 for (channel = 0; channel < avctx->
channels; channel++) {
974 for (n = 0; n < nb_samples / 2; n++) {
977 byte[0] = bytestream2_get_byteu(&gb);
979 byte[1] = bytestream2_get_byteu(&gb);
980 for(channel = 0; channel < avctx->
channels; channel++) {
983 for(channel = 0; channel < avctx->
channels; channel++) {
990 for (channel = 0; channel < avctx->
channels; channel++) {
991 int16_t *smp = samples_p[channel];
993 for (n = nb_samples / 2; n > 0; n--) {
994 int v = bytestream2_get_byteu(&gb);
1000 for (n = nb_samples / 2; n > 0; n--) {
1001 for (channel = 0; channel < avctx->
channels; channel++) {
1002 int v = bytestream2_get_byteu(&gb);
1013 int16_t *out0 = samples_p[0];
1014 int16_t *out1 = samples_p[1];
1015 int samples_per_block = 28 * (3 - avctx->
channels) * 4;
1016 int sample_offset = 0;
1020 avctx->
channels, sample_offset)) < 0)
1023 sample_offset += samples_per_block;
1028 for (i=0; i<=st; i++) {
1036 for (i=0; i<=st; i++)
1039 for (n = nb_samples >> (1 - st); n > 0; n--) {
1040 int byte = bytestream2_get_byteu(&gb);
1046 for (n = nb_samples >> (1 - st); n > 0; n--) {
1047 int byte = bytestream2_get_byteu(&gb);
1054 int previous_left_sample, previous_right_sample;
1055 int current_left_sample, current_right_sample;
1056 int next_left_sample, next_right_sample;
1057 int coeff1l, coeff2l, coeff1r, coeff2r;
1058 int shift_left, shift_right;
1066 current_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1067 previous_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1068 current_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1069 previous_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1071 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1072 int byte = bytestream2_get_byteu(&gb);
1078 byte = bytestream2_get_byteu(&gb);
1079 shift_left = 20 - (byte >> 4);
1080 shift_right = 20 - (byte & 0x0F);
1082 for (count2 = 0; count2 < 28; count2++) {
1083 byte = bytestream2_get_byteu(&gb);
1084 next_left_sample =
sign_extend(byte >> 4, 4) << shift_left;
1085 next_right_sample =
sign_extend(byte, 4) << shift_right;
1087 next_left_sample = (next_left_sample +
1088 (current_left_sample * coeff1l) +
1089 (previous_left_sample * coeff2l) + 0x80) >> 8;
1090 next_right_sample = (next_right_sample +
1091 (current_right_sample * coeff1r) +
1092 (previous_right_sample * coeff2r) + 0x80) >> 8;
1094 previous_left_sample = current_left_sample;
1095 current_left_sample = av_clip_int16(next_left_sample);
1096 previous_right_sample = current_right_sample;
1097 current_right_sample = av_clip_int16(next_right_sample);
1098 *samples++ = current_left_sample;
1099 *samples++ = current_right_sample;
1111 for(channel = 0; channel < avctx->
channels; channel++) {
1112 int byte = bytestream2_get_byteu(&gb);
1115 shift[channel] = 20 - (byte & 0x0F);
1117 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1120 byte[0] = bytestream2_get_byteu(&gb);
1121 if (st) byte[1] = bytestream2_get_byteu(&gb);
1122 for(i = 4; i >= 0; i-=4) {
1123 for(channel = 0; channel < avctx->
channels; channel++) {
1127 c->
status[channel].
sample2 * coeff[channel][1] + 0x80) >> 8;
1145 int previous_sample, current_sample, next_sample;
1148 unsigned int channel;
1153 for (channel=0; channel<avctx->
channels; channel++)
1154 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1155 bytestream2_get_le32(&gb)) +
1158 for (channel=0; channel<avctx->
channels; channel++) {
1160 samplesC = samples_p[channel];
1163 current_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1164 previous_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1170 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1171 int byte = bytestream2_get_byte(&gb);
1173 current_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1174 previous_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1176 for (count2=0; count2<28; count2++)
1177 *samplesC++ =
sign_extend(bytestream2_get_be16(&gb), 16);
1181 shift = 20 - (byte & 0x0F);
1183 for (count2=0; count2<28; count2++) {
1187 byte = bytestream2_get_byte(&gb);
1191 next_sample += (current_sample * coeff1) +
1192 (previous_sample * coeff2);
1193 next_sample = av_clip_int16(next_sample >> 8);
1195 previous_sample = current_sample;
1196 current_sample = next_sample;
1197 *samplesC++ = current_sample;
1203 }
else if (count != count1) {
1205 count =
FFMAX(count, count1);
1219 for (channel=0; channel<avctx->
channels; channel++) {
1221 int16_t *
s = samples_p[channel];
1222 for (n = 0; n < 4; n++, s += 32) {
1228 val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1229 shift[
n] = 20 - (val & 0x0F);
1233 for (m=2; m<32; m+=2) {
1234 s = &samples_p[channel][
m];
1235 for (n = 0; n < 4; n++, s += 32) {
1237 int byte = bytestream2_get_byteu(&gb);
1240 pred = s[-1] * coeff[0][
n] + s[-2] * coeff[1][
n];
1241 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1244 pred = s[0] * coeff[0][
n] + s[-1] * coeff[1][
n];
1245 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1260 for (n = nb_samples >> (1 - st); n > 0; n--) {
1261 int v = bytestream2_get_byteu(&gb);
1268 for (i = 0; i < avctx->
channels; i++) {
1279 for (n = nb_samples >> (1 - st); n > 0; n--) {
1280 int v = bytestream2_get_byteu(&gb);
1287 for (n = nb_samples >> (1 - st); n > 0; n--) {
1288 int v = bytestream2_get_byteu(&gb);
1298 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1300 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1305 for (n = nb_samples >> (1 - st); n > 0; n--) {
1306 int byte = bytestream2_get_byteu(&gb);
1313 for (n = nb_samples / 3; n > 0; n--) {
1314 int byte = bytestream2_get_byteu(&gb);
1318 (byte >> 2) & 0x07, 3, 0);
1323 for (n = nb_samples >> (2 - st); n > 0; n--) {
1324 int byte = bytestream2_get_byteu(&gb);
1328 (byte >> 4) & 0x03, 2, 2);
1330 (byte >> 2) & 0x03, 2, 2);
1341 for (n = nb_samples >> (1 - st); n > 0; n--) {
1342 int v = bytestream2_get_byteu(&gb);
1349 int samples_per_block;
1353 samples_per_block = avctx->
extradata[0] / 16;
1354 blocks = nb_samples / avctx->
extradata[0];
1356 samples_per_block = nb_samples / 16;
1360 for (m = 0; m < blocks; m++) {
1361 for (channel = 0; channel < avctx->
channels; channel++) {
1365 samples = samples_p[channel] + m * 16;
1367 for (i = 0; i < samples_per_block; i++) {
1368 int byte = bytestream2_get_byteu(&gb);
1369 int scale = 1 << (byte >> 4);
1370 int index = byte & 0xf;
1375 for (n = 0; n < 16; n++) {
1381 byte = bytestream2_get_byteu(&gb);
1385 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1386 ((sampledat * scale) << 11)) >> 11;
1387 *samples = av_clip_int16(sampledat);
1413 for (i = 0; i < avctx->
channels; i++)
1414 for (n = 0; n < 16; n++)
1415 table[i][n] =
sign_extend(bytestream2_get_be16u(&tb), 16);
1417 for (i = 0; i < avctx->
channels; i++)
1418 for (n = 0; n < 16; n++)
1419 table[i][n] =
sign_extend(bytestream2_get_be16u(&gb), 16);
1422 for (i = 0; i < avctx->
channels; i++) {
1428 for (ch = 0; ch < avctx->
channels; ch++) {
1429 samples = samples_p[ch];
1432 for (i = 0; i < nb_samples / 14; i++) {
1433 int byte = bytestream2_get_byteu(&gb);
1434 int index = (byte >> 4) & 7;
1435 unsigned int exp = byte & 0x0F;
1436 int factor1 = table[ch][index * 2];
1437 int factor2 = table[ch][index * 2 + 1];
1440 for (n = 0; n < 14; n++) {
1446 byte = bytestream2_get_byteu(&gb);
1451 + c->
status[ch].
sample2 * factor2) >> 11) + (sampledat << exp);
1452 *samples = av_clip_int16(sampledat);
1461 for (channel = 0; channel < avctx->
channels; channel++) {
1462 samples = samples_p[channel];
1465 for (i = 0; i < nb_samples / 28; i++) {
1469 header = bytestream2_get_byteu(&gb);
1473 for (n = 0; n < 28; n++) {
1476 switch (header >> 4) {
1490 prev = av_clip((prev + 0x20) >> 6, -0x200000, 0x1fffff);
1492 byte = bytestream2_get_byteu(&gb);
1498 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1499 *samples++ = av_clip_int16(sampledat >> 6);
1532 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1533 AVCodec ff_ ## name_ ## _decoder = { \
1535 .type = AVMEDIA_TYPE_AUDIO, \
1537 .priv_data_size = sizeof(ADPCMDecodeContext), \
1538 .init = adpcm_decode_init, \
1539 .decode = adpcm_decode_frame, \
1540 .capabilities = CODEC_CAP_DR1, \
1541 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1542 .sample_fmts = sample_fmts_, \