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;
173 step_index = av_clip(step_index, 0, 88);
180 diff = ((2 * delta + 1) * step) >>
shift;
182 if (sign) predictor -=
diff;
183 else predictor +=
diff;
199 step_index = av_clip(step_index, 0, 88);
202 if (nibble & 4) diff += step;
203 if (nibble & 2) diff += step >> 1;
204 if (nibble & 1) diff += step >> 2;
222 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->
idelta;
225 c->
sample1 = av_clip_int16(predictor);
238 step_index = av_clip(step_index, 0, 48);
242 diff = ((2 * delta + 1) * step) >> 3;
244 if (sign) predictor -=
diff;
245 else predictor +=
diff;
247 c->
predictor = av_clip(predictor, -2048, 2047);
263 diff = ((2 * delta + 1) * c->
step) >> 3;
269 c->
step = av_clip(new_step, 511, 32767);
278 sign = nibble & (1<<(size-1));
279 delta = nibble & ((1<<(size-1))-1);
286 if (delta >= (2*size - 3) && c->
step < 3)
288 else if (delta == 0 && c->
step > 0)
304 c->
step = av_clip(c->
step, 127, 24567);
317 out0 += sample_offset;
321 out1 += sample_offset;
324 shift = 12 - (in[4+i*2] & 15);
325 filter = in[4+i*2] >> 4;
340 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
342 s_1 = av_clip_int16(s);
353 shift = 12 - (in[5+i*2] & 15);
354 filter = in[5+i*2] >> 4;
367 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
369 s_1 = av_clip_int16(s);
381 out0 += 28 * (3 - channels);
382 out1 += 28 * (3 - channels);
393 int k0, signmask, nb_bits,
count;
394 int size = buf_size*8;
402 k0 = 1 << (nb_bits-2);
403 signmask = 1 << (nb_bits-1);
406 for (i = 0; i < avctx->
channels; i++) {
414 for (i = 0; i < avctx->
channels; i++) {
429 if (delta & signmask)
455 int buf_size,
int *coded_samples)
460 int has_coded_samples = 0;
471 if (buf_size < 76 * ch)
476 if (buf_size < 34 * ch)
487 nb_samples = buf_size * 2 / ch;
502 return (buf_size - header_size) * 2 / ch;
507 has_coded_samples = 1;
508 *coded_samples = bytestream2_get_le32(gb);
509 *coded_samples -= *coded_samples % 28;
510 nb_samples = (buf_size - 12) / 30 * 28;
513 has_coded_samples = 1;
514 *coded_samples = bytestream2_get_le32(gb);
515 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
518 nb_samples = (buf_size - ch) / ch * 2;
525 has_coded_samples = 1;
528 header_size = 4 + 9 * ch;
529 *coded_samples = bytestream2_get_le32(gb);
532 header_size = 4 + 5 * ch;
533 *coded_samples = bytestream2_get_le32(gb);
536 header_size = 4 + 5 * ch;
537 *coded_samples = bytestream2_get_be32(gb);
540 *coded_samples -= *coded_samples % 28;
541 nb_samples = (buf_size - header_size) * 2 / ch;
542 nb_samples -= nb_samples % 28;
547 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
552 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
557 nb_samples = (buf_size - 4 * ch) * 2 / ch;
562 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
567 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
573 int samples_per_byte;
583 nb_samples += buf_size * samples_per_byte / ch;
588 int buf_bits = buf_size * 8 - 2;
589 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
590 int block_hdr_size = 22 * ch;
591 int block_size = block_hdr_size + nbits * ch * 4095;
592 int nblocks = buf_bits / block_size;
593 int bits_left = buf_bits - nblocks * block_size;
594 nb_samples = nblocks * 4096;
595 if (bits_left >= block_hdr_size)
596 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
601 nb_samples = buf_size / (8 * ch) * 14;
604 has_coded_samples = 1;
606 *coded_samples = bytestream2_get_be32(gb);
607 *coded_samples -= *coded_samples % 14;
608 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
611 nb_samples = buf_size / (9 * ch) * 16;
614 nb_samples = (buf_size / 128) * 224 / ch;
617 nb_samples = buf_size / (16 * ch) * 28;
622 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
629 int *got_frame_ptr,
AVPacket *avpkt)
633 int buf_size = avpkt->
size;
636 int n,
m, channel, i;
641 int nb_samples, coded_samples,
ret;
645 nb_samples =
get_nb_samples(avctx, &gb, buf_size, &coded_samples);
646 if (nb_samples <= 0) {
655 samples = (
short *)frame->
data[0];
661 if (coded_samples != nb_samples)
663 frame->
nb_samples = nb_samples = coded_samples;
672 for (channel = 0; channel < avctx->
channels; channel++) {
675 cs = &(c->
status[channel]);
679 predictor =
sign_extend(bytestream2_get_be16u(&gb), 16);
680 step_index = predictor & 0x7F;
701 samples = samples_p[channel];
703 for (m = 0; m < 64; m += 2) {
704 int byte = bytestream2_get_byteu(&gb);
723 for (n = 0; n < (nb_samples - 1) / 8; n++) {
724 for (i = 0; i < avctx->
channels; i++) {
726 samples = &samples_p[i][1 + n * 8];
727 for (m = 0; m < 8; m += 2) {
728 int v = bytestream2_get_byteu(&gb);
736 for (i = 0; i < avctx->
channels; i++)
739 for (i = 0; i < avctx->
channels; i++) {
748 for (i = 0; i < avctx->
channels; i++) {
749 samples = (int16_t *)frame->
data[i];
751 for (n = nb_samples >> 1; n > 0; n--) {
752 int v = bytestream2_get_byteu(&gb);
762 block_predictor = bytestream2_get_byteu(&gb);
763 if (block_predictor > 6) {
771 block_predictor = bytestream2_get_byteu(&gb);
772 if (block_predictor > 6) {
794 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
795 int byte = bytestream2_get_byteu(&gb);
802 for (channel = 0; channel < avctx->
channels; channel++) {
812 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
813 int v = bytestream2_get_byteu(&gb);
822 int decode_top_nibble_next = 0;
824 const int16_t *samples_end = samples + avctx->
channels * nb_samples;
840 #define DK3_GET_NEXT_NIBBLE() \
841 if (decode_top_nibble_next) { \
842 nibble = last_byte >> 4; \
843 decode_top_nibble_next = 0; \
845 last_byte = bytestream2_get_byteu(&gb); \
846 nibble = last_byte & 0x0F; \
847 decode_top_nibble_next = 1; \
850 while (samples < samples_end) {
880 for (channel = 0; channel < avctx->
channels; channel++) {
891 for (n = nb_samples >> (1 - st); n > 0; n--) {
893 int v = bytestream2_get_byteu(&gb);
908 int v = bytestream2_get_byteu(&gb);
915 int v = bytestream2_get_byteu(&gb);
921 for (channel = 0; channel < avctx->
channels; channel++) {
931 for (n = 0; n < nb_samples / 2; n++) {
934 byte[0] = bytestream2_get_byteu(&gb);
936 byte[1] = bytestream2_get_byteu(&gb);
937 for(channel = 0; channel < avctx->
channels; channel++) {
940 for(channel = 0; channel < avctx->
channels; channel++) {
947 for (channel = 0; channel < avctx->
channels; channel++) {
948 int16_t *smp = samples_p[channel];
950 for (n = nb_samples / 2; n > 0; n--) {
951 int v = bytestream2_get_byteu(&gb);
957 for (n = nb_samples / 2; n > 0; n--) {
958 for (channel = 0; channel < avctx->
channels; channel++) {
959 int v = bytestream2_get_byteu(&gb);
970 int16_t *out0 = samples_p[0];
971 int16_t *out1 = samples_p[1];
972 int samples_per_block = 28 * (3 - avctx->
channels) * 4;
973 int sample_offset = 0;
977 avctx->
channels, sample_offset)) < 0)
980 sample_offset += samples_per_block;
985 for (i=0; i<=st; i++) {
993 for (i=0; i<=st; i++)
996 for (n = nb_samples >> (1 - st); n > 0; n--) {
997 int byte = bytestream2_get_byteu(&gb);
1003 for (n = nb_samples >> (1 - st); n > 0; n--) {
1004 int byte = bytestream2_get_byteu(&gb);
1011 int previous_left_sample, previous_right_sample;
1012 int current_left_sample, current_right_sample;
1013 int next_left_sample, next_right_sample;
1014 int coeff1l, coeff2l, coeff1r, coeff2r;
1015 int shift_left, shift_right;
1023 current_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1024 previous_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1025 current_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1026 previous_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1028 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1029 int byte = bytestream2_get_byteu(&gb);
1035 byte = bytestream2_get_byteu(&gb);
1036 shift_left = 20 - (byte >> 4);
1037 shift_right = 20 - (byte & 0x0F);
1039 for (count2 = 0; count2 < 28; count2++) {
1040 byte = bytestream2_get_byteu(&gb);
1041 next_left_sample =
sign_extend(byte >> 4, 4) << shift_left;
1042 next_right_sample =
sign_extend(byte, 4) << shift_right;
1044 next_left_sample = (next_left_sample +
1045 (current_left_sample * coeff1l) +
1046 (previous_left_sample * coeff2l) + 0x80) >> 8;
1047 next_right_sample = (next_right_sample +
1048 (current_right_sample * coeff1r) +
1049 (previous_right_sample * coeff2r) + 0x80) >> 8;
1051 previous_left_sample = current_left_sample;
1052 current_left_sample = av_clip_int16(next_left_sample);
1053 previous_right_sample = current_right_sample;
1054 current_right_sample = av_clip_int16(next_right_sample);
1055 *samples++ = current_left_sample;
1056 *samples++ = current_right_sample;
1068 for(channel = 0; channel < avctx->
channels; channel++) {
1069 int byte = bytestream2_get_byteu(&gb);
1072 shift[channel] = 20 - (byte & 0x0F);
1074 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1077 byte[0] = bytestream2_get_byteu(&gb);
1078 if (st) byte[1] = bytestream2_get_byteu(&gb);
1079 for(i = 4; i >= 0; i-=4) {
1080 for(channel = 0; channel < avctx->
channels; channel++) {
1084 c->
status[channel].
sample2 * coeff[channel][1] + 0x80) >> 8;
1102 int previous_sample, current_sample, next_sample;
1105 unsigned int channel;
1110 for (channel=0; channel<avctx->
channels; channel++)
1111 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1112 bytestream2_get_le32(&gb)) +
1115 for (channel=0; channel<avctx->
channels; channel++) {
1117 samplesC = samples_p[channel];
1120 current_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1121 previous_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1127 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1128 int byte = bytestream2_get_byte(&gb);
1130 current_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1131 previous_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1133 for (count2=0; count2<28; count2++)
1134 *samplesC++ =
sign_extend(bytestream2_get_be16(&gb), 16);
1138 shift = 20 - (byte & 0x0F);
1140 for (count2=0; count2<28; count2++) {
1144 byte = bytestream2_get_byte(&gb);
1148 next_sample += (current_sample * coeff1) +
1149 (previous_sample * coeff2);
1150 next_sample = av_clip_int16(next_sample >> 8);
1152 previous_sample = current_sample;
1153 current_sample = next_sample;
1154 *samplesC++ = current_sample;
1160 }
else if (count != count1) {
1162 count =
FFMAX(count, count1);
1176 for (channel=0; channel<avctx->
channels; channel++) {
1178 int16_t *
s = samples_p[channel];
1179 for (n = 0; n < 4; n++, s += 32) {
1185 val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1186 shift[
n] = 20 - (val & 0x0F);
1190 for (m=2; m<32; m+=2) {
1191 s = &samples_p[channel][
m];
1192 for (n = 0; n < 4; n++, s += 32) {
1194 int byte = bytestream2_get_byteu(&gb);
1197 pred = s[-1] * coeff[0][
n] + s[-2] * coeff[1][
n];
1198 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1201 pred = s[0] * coeff[0][
n] + s[-1] * coeff[1][
n];
1202 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1217 for (n = nb_samples >> (1 - st); n > 0; n--) {
1218 int v = bytestream2_get_byteu(&gb);
1225 for (i = 0; i < avctx->
channels; i++) {
1236 for (n = nb_samples >> (1 - st); n > 0; n--) {
1237 int v = bytestream2_get_byteu(&gb);
1244 for (n = nb_samples >> (1 - st); n > 0; n--) {
1245 int v = bytestream2_get_byteu(&gb);
1255 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1257 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1262 for (n = nb_samples >> (1 - st); n > 0; n--) {
1263 int byte = bytestream2_get_byteu(&gb);
1270 for (n = nb_samples / 3; n > 0; n--) {
1271 int byte = bytestream2_get_byteu(&gb);
1275 (byte >> 2) & 0x07, 3, 0);
1280 for (n = nb_samples >> (2 - st); n > 0; n--) {
1281 int byte = bytestream2_get_byteu(&gb);
1285 (byte >> 4) & 0x03, 2, 2);
1287 (byte >> 2) & 0x03, 2, 2);
1298 for (n = nb_samples >> (1 - st); n > 0; n--) {
1299 int v = bytestream2_get_byteu(&gb);
1306 int samples_per_block;
1310 samples_per_block = avctx->
extradata[0] / 16;
1311 blocks = nb_samples / avctx->
extradata[0];
1313 samples_per_block = nb_samples / 16;
1317 for (m = 0; m < blocks; m++) {
1318 for (channel = 0; channel < avctx->
channels; channel++) {
1322 samples = samples_p[channel] + m * 16;
1324 for (i = 0; i < samples_per_block; i++) {
1325 int byte = bytestream2_get_byteu(&gb);
1326 int scale = 1 << (byte >> 4);
1327 int index = byte & 0xf;
1332 for (n = 0; n < 16; n++) {
1338 byte = bytestream2_get_byteu(&gb);
1342 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1343 ((sampledat * scale) << 11)) >> 11;
1344 *samples = av_clip_int16(sampledat);
1370 for (i = 0; i < avctx->
channels; i++)
1371 for (n = 0; n < 16; n++)
1372 table[i][n] =
sign_extend(bytestream2_get_be16u(&tb), 16);
1374 for (i = 0; i < avctx->
channels; i++)
1375 for (n = 0; n < 16; n++)
1376 table[i][n] =
sign_extend(bytestream2_get_be16u(&gb), 16);
1379 for (i = 0; i < avctx->
channels; i++) {
1385 for (ch = 0; ch < avctx->
channels; ch++) {
1386 samples = samples_p[ch];
1389 for (i = 0; i < nb_samples / 14; i++) {
1390 int byte = bytestream2_get_byteu(&gb);
1391 int index = (byte >> 4) & 7;
1392 unsigned int exp = byte & 0x0F;
1393 int factor1 = table[ch][index * 2];
1394 int factor2 = table[ch][index * 2 + 1];
1397 for (n = 0; n < 14; n++) {
1403 byte = bytestream2_get_byteu(&gb);
1408 + c->
status[ch].
sample2 * factor2) >> 11) + (sampledat << exp);
1409 *samples = av_clip_int16(sampledat);
1418 for (channel = 0; channel < avctx->
channels; channel++) {
1419 samples = samples_p[channel];
1422 for (i = 0; i < nb_samples / 28; i++) {
1426 header = bytestream2_get_byteu(&gb);
1430 for (n = 0; n < 28; n++) {
1433 switch (header >> 4) {
1447 prev = av_clip((prev + 0x20) >> 6, -0x200000, 0x1fffff);
1449 byte = bytestream2_get_byteu(&gb);
1455 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1456 *samples++ = av_clip_int16(sampledat >> 6);
1489 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1490 AVCodec ff_ ## name_ ## _decoder = { \
1492 .type = AVMEDIA_TYPE_AUDIO, \
1494 .priv_data_size = sizeof(ADPCMDecodeContext), \
1495 .init = adpcm_decode_init, \
1496 .decode = adpcm_decode_frame, \
1497 .capabilities = CODEC_CAP_DR1, \
1498 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1499 .sample_fmts = sample_fmts_, \