81 { -1, -1, -1, -1, 2, 4, 6, 8 },
82 { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
95 unsigned int min_channels = 1;
96 unsigned int max_channels = 2;
169 step_index = av_clip(step_index, 0, 88);
176 diff = ((2 * delta + 1) * step) >> shift;
178 if (sign) predictor -= diff;
179 else predictor += diff;
195 step_index = av_clip(step_index, 0, 88);
198 if (nibble & 4) diff +=
step;
199 if (nibble & 2) diff += step >> 1;
200 if (nibble & 1) diff += step >> 2;
218 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->
idelta;
221 c->
sample1 = av_clip_int16(predictor);
230 int sign,
delta, diff;
238 diff = ((2 * delta + 1) * c->
step) >> 3;
244 c->
step = av_clip(new_step, 511, 32767);
251 int sign,
delta, diff;
253 sign = nibble & (1<<(size-1));
254 delta = nibble & ((1<<(size-1))-1);
255 diff = delta << (7 + c->
step + shift);
261 if (delta >= (2*size - 3) && c->
step < 3)
263 else if (delta == 0 && c->
step > 0)
279 c->
step = av_clip(c->
step, 127, 24567);
292 out0 += sample_offset;
296 out1 += sample_offset;
299 shift = 12 - (in[4+i*2] & 15);
300 filter = in[4+i*2] >> 4;
303 "Invalid XA-ADPCM filter %d (max. allowed is 4)\n",
317 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
319 s_1 = av_clip_int16(s);
330 shift = 12 - (in[5+i*2] & 15);
331 filter = in[5+i*2] >> 4;
334 "Invalid XA-ADPCM filter %d (max. allowed is 4)\n",
345 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
347 s_1 = av_clip_int16(s);
359 out0 += 28 * (3 - channels);
360 out1 += 28 * (3 - channels);
371 int k0, signmask, nb_bits, count;
372 int size = buf_size*8;
380 k0 = 1 << (nb_bits-2);
381 signmask = 1 << (nb_bits-1);
384 for (i = 0; i < avctx->
channels; i++) {
392 for (i = 0; i < avctx->
channels; i++) {
407 if (delta & signmask)
433 int buf_size,
int *coded_samples)
438 int has_coded_samples = 0;
446 if (buf_size < 76 * ch)
451 if (buf_size < 34 * ch)
461 nb_samples = buf_size * 2 / ch;
476 return (buf_size - header_size) * 2 / ch;
481 has_coded_samples = 1;
482 *coded_samples = bytestream2_get_le32(gb);
483 *coded_samples -= *coded_samples % 28;
484 nb_samples = (buf_size - 12) / 30 * 28;
487 has_coded_samples = 1;
488 *coded_samples = bytestream2_get_le32(gb);
489 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
492 nb_samples = (buf_size - ch) / ch * 2;
499 has_coded_samples = 1;
502 header_size = 4 + 9 * ch;
503 *coded_samples = bytestream2_get_le32(gb);
506 header_size = 4 + 5 * ch;
507 *coded_samples = bytestream2_get_le32(gb);
510 header_size = 4 + 5 * ch;
511 *coded_samples = bytestream2_get_be32(gb);
514 *coded_samples -= *coded_samples % 28;
515 nb_samples = (buf_size - header_size) * 2 / ch;
516 nb_samples -= nb_samples % 28;
521 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
526 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
531 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
536 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
542 int samples_per_byte;
552 nb_samples += buf_size * samples_per_byte / ch;
557 int buf_bits = buf_size * 8 - 2;
558 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
559 int block_hdr_size = 22 * ch;
560 int block_size = block_hdr_size + nbits * ch * 4095;
561 int nblocks = buf_bits / block_size;
562 int bits_left = buf_bits - nblocks * block_size;
563 nb_samples = nblocks * 4096;
564 if (bits_left >= block_hdr_size)
565 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
569 has_coded_samples = 1;
571 *coded_samples = bytestream2_get_be32(gb);
572 *coded_samples -= *coded_samples % 14;
573 nb_samples = (buf_size - 80) / (8 * ch) * 14;
576 nb_samples = (buf_size / 128) * 224 / ch;
581 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
588 int *got_frame_ptr,
AVPacket *avpkt)
592 int buf_size = avpkt->
size;
595 int n, m, channel, i;
600 int nb_samples, coded_samples, ret;
604 nb_samples =
get_nb_samples(avctx, &gb, buf_size, &coded_samples);
605 if (nb_samples <= 0) {
616 samples = (
short *)frame->
data[0];
622 if (coded_samples != nb_samples)
624 frame->
nb_samples = nb_samples = coded_samples;
633 for (channel = 0; channel < avctx->
channels; channel++) {
636 cs = &(c->
status[channel]);
640 predictor =
sign_extend(bytestream2_get_be16u(&gb), 16);
641 step_index = predictor & 0x7F;
662 samples = samples_p[channel];
664 for (m = 0; m < 64; m += 2) {
665 int byte = bytestream2_get_byteu(&gb);
684 for (n = 0; n < (nb_samples - 1) / 8; n++) {
685 for (i = 0; i < avctx->
channels; i++) {
687 samples = &samples_p[i][1 + n * 8];
688 for (m = 0; m < 8; m += 2) {
689 int v = bytestream2_get_byteu(&gb);
697 for (i = 0; i < avctx->
channels; i++)
700 for (i = 0; i < avctx->
channels; i++) {
709 for (i = 0; i < avctx->
channels; i++) {
710 samples = (int16_t *)frame->
data[i];
712 for (n = nb_samples >> 1; n > 0; n--) {
713 int v = bytestream2_get_byteu(&gb);
723 block_predictor = bytestream2_get_byteu(&gb);
724 if (block_predictor > 6) {
732 block_predictor = bytestream2_get_byteu(&gb);
733 if (block_predictor > 6) {
755 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
756 int byte = bytestream2_get_byteu(&gb);
763 for (channel = 0; channel < avctx->
channels; channel++) {
773 for (n = (nb_samples >> (1 - st)) - 1; n > 0; n--) {
774 int v = bytestream2_get_byteu(&gb);
783 int decode_top_nibble_next = 0;
785 const int16_t *samples_end = samples + avctx->
channels * nb_samples;
801 #define DK3_GET_NEXT_NIBBLE() \
802 if (decode_top_nibble_next) { \
803 nibble = last_byte >> 4; \
804 decode_top_nibble_next = 0; \
806 last_byte = bytestream2_get_byteu(&gb); \
807 nibble = last_byte & 0x0F; \
808 decode_top_nibble_next = 1; \
811 while (samples < samples_end) {
841 for (channel = 0; channel < avctx->
channels; channel++) {
852 for (n = nb_samples >> (1 - st); n > 0; n--) {
854 int v = bytestream2_get_byteu(&gb);
869 int v = bytestream2_get_byteu(&gb);
876 for (channel = 0; channel < avctx->
channels; channel++) {
877 int16_t *smp = samples_p[channel];
879 for (n = nb_samples / 2; n > 0; n--) {
880 int v = bytestream2_get_byteu(&gb);
886 for (n = nb_samples / 2; n > 0; n--) {
887 for (channel = 0; channel < avctx->
channels; channel++) {
888 int v = bytestream2_get_byteu(&gb);
899 int16_t *out0 = samples_p[0];
900 int16_t *out1 = samples_p[1];
901 int samples_per_block = 28 * (3 - avctx->
channels) * 4;
902 int sample_offset = 0;
906 avctx->
channels, sample_offset)) < 0)
909 sample_offset += samples_per_block;
914 for (i=0; i<=st; i++) {
922 for (i=0; i<=st; i++)
925 for (n = nb_samples >> (1 - st); n > 0; n--) {
926 int byte = bytestream2_get_byteu(&gb);
932 for (n = nb_samples >> (1 - st); n > 0; n--) {
933 int byte = bytestream2_get_byteu(&gb);
940 int previous_left_sample, previous_right_sample;
941 int current_left_sample, current_right_sample;
942 int next_left_sample, next_right_sample;
943 int coeff1l, coeff2l, coeff1r, coeff2r;
944 int shift_left, shift_right;
949 current_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
950 previous_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
951 current_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
952 previous_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
954 for (count1 = 0; count1 < nb_samples / 28; count1++) {
955 int byte = bytestream2_get_byteu(&gb);
961 byte = bytestream2_get_byteu(&gb);
962 shift_left = 20 - (byte >> 4);
963 shift_right = 20 - (byte & 0x0F);
965 for (count2 = 0; count2 < 28; count2++) {
966 byte = bytestream2_get_byteu(&gb);
967 next_left_sample =
sign_extend(byte >> 4, 4) << shift_left;
968 next_right_sample =
sign_extend(byte, 4) << shift_right;
970 next_left_sample = (next_left_sample +
971 (current_left_sample * coeff1l) +
972 (previous_left_sample * coeff2l) + 0x80) >> 8;
973 next_right_sample = (next_right_sample +
974 (current_right_sample * coeff1r) +
975 (previous_right_sample * coeff2r) + 0x80) >> 8;
977 previous_left_sample = current_left_sample;
978 current_left_sample = av_clip_int16(next_left_sample);
979 previous_right_sample = current_right_sample;
980 current_right_sample = av_clip_int16(next_right_sample);
981 *samples++ = current_left_sample;
982 *samples++ = current_right_sample;
992 int coeff[2][2], shift[2];
994 for(channel = 0; channel < avctx->
channels; channel++) {
995 int byte = bytestream2_get_byteu(&gb);
998 shift[channel] = 20 - (byte & 0x0F);
1000 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1003 byte[0] = bytestream2_get_byteu(&gb);
1004 if (st) byte[1] = bytestream2_get_byteu(&gb);
1005 for(i = 4; i >= 0; i-=4) {
1006 for(channel = 0; channel < avctx->
channels; channel++) {
1010 c->
status[channel].
sample2 * coeff[channel][1] + 0x80) >> 8;
1028 int previous_sample, current_sample, next_sample;
1031 unsigned int channel;
1036 for (channel=0; channel<avctx->
channels; channel++)
1037 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1038 bytestream2_get_le32(&gb)) +
1041 for (channel=0; channel<avctx->
channels; channel++) {
1043 samplesC = samples_p[channel];
1046 current_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1047 previous_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1053 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1054 int byte = bytestream2_get_byte(&gb);
1056 current_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1057 previous_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1059 for (count2=0; count2<28; count2++)
1060 *samplesC++ =
sign_extend(bytestream2_get_be16(&gb), 16);
1064 shift = 20 - (byte & 0x0F);
1066 for (count2=0; count2<28; count2++) {
1070 byte = bytestream2_get_byte(&gb);
1074 next_sample += (current_sample * coeff1) +
1075 (previous_sample * coeff2);
1076 next_sample = av_clip_int16(next_sample >> 8);
1078 previous_sample = current_sample;
1079 current_sample = next_sample;
1080 *samplesC++ = current_sample;
1086 }
else if (count != count1) {
1088 count =
FFMAX(count, count1);
1102 for (channel=0; channel<avctx->
channels; channel++) {
1103 int coeff[2][4], shift[4];
1104 int16_t *s = samples_p[channel];
1105 for (n = 0; n < 4; n++, s += 32) {
1106 int val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1111 val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1112 shift[n] = 20 - (val & 0x0F);
1116 for (m=2; m<32; m+=2) {
1117 s = &samples_p[channel][m];
1118 for (n = 0; n < 4; n++, s += 32) {
1120 int byte = bytestream2_get_byteu(&gb);
1123 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1124 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1127 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1128 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1150 for (n = nb_samples >> (1 - st); n > 0; n--) {
1151 int hi, lo, v = bytestream2_get_byteu(&gb);
1166 for (n = nb_samples >> (1 - st); n > 0; n--) {
1167 int v = bytestream2_get_byteu(&gb);
1177 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1179 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1184 for (n = nb_samples >> (1 - st); n > 0; n--) {
1185 int byte = bytestream2_get_byteu(&gb);
1192 for (n = nb_samples / 3; n > 0; n--) {
1193 int byte = bytestream2_get_byteu(&gb);
1197 (byte >> 2) & 0x07, 3, 0);
1202 for (n = nb_samples >> (2 - st); n > 0; n--) {
1203 int byte = bytestream2_get_byteu(&gb);
1207 (byte >> 4) & 0x03, 2, 2);
1209 (byte >> 2) & 0x03, 2, 2);
1220 for (n = nb_samples >> (1 - st); n > 0; n--) {
1221 int v = bytestream2_get_byteu(&gb);
1232 for (i = 0; i < 2; i++)
1233 for (n = 0; n < 16; n++)
1234 table[i][n] =
sign_extend(bytestream2_get_be16u(&gb), 16);
1237 for (i = 0; i < 2; i++)
1238 for (n = 0; n < 2; n++)
1239 prev[i][n] =
sign_extend(bytestream2_get_be16u(&gb), 16);
1241 for (ch = 0; ch <= st; ch++) {
1242 samples = samples_p[ch];
1245 for (i = 0; i < nb_samples / 14; i++) {
1246 int byte = bytestream2_get_byteu(&gb);
1247 int index = (byte >> 4) & 7;
1248 unsigned int exp = byte & 0x0F;
1249 int factor1 = table[ch][index * 2];
1250 int factor2 = table[ch][index * 2 + 1];
1253 for (n = 0; n < 14; n++) {
1259 byte = bytestream2_get_byteu(&gb);
1263 sampledat = ((prev[ch][0]*factor1
1264 + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
1265 *samples = av_clip_int16(sampledat);
1266 prev[ch][1] = prev[ch][0];
1267 prev[ch][0] = *samples++;
1292 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1293 AVCodec ff_ ## name_ ## _decoder = { \
1295 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1296 .type = AVMEDIA_TYPE_AUDIO, \
1298 .priv_data_size = sizeof(ADPCMDecodeContext), \
1299 .init = adpcm_decode_init, \
1300 .decode = adpcm_decode_frame, \
1301 .capabilities = CODEC_CAP_DR1, \
1302 .sample_fmts = sample_fmts_, \