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 }
96 unsigned int min_channels = 1;
97 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);
234 int sign,
delta, diff;
242 diff = ((2 * delta + 1) * c->
step) >> 3;
248 c->
step = av_clip(new_step, 511, 32767);
255 int sign,
delta, diff;
257 sign = nibble & (1<<(size-1));
258 delta = nibble & ((1<<(size-1))-1);
259 diff = delta << (7 + c->
step + shift);
265 if (delta >= (2*size - 3) && c->
step < 3)
267 else if (delta == 0 && c->
step > 0)
283 c->
step = av_clip(c->
step, 127, 24567);
296 out0 += sample_offset;
300 out1 += sample_offset;
303 shift = 12 - (in[4+i*2] & 15);
304 filter = in[4+i*2] >> 4;
307 "Invalid XA-ADPCM filter %d (max. allowed is 4)\n",
321 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
323 s_1 = av_clip_int16(s);
334 shift = 12 - (in[5+i*2] & 15);
335 filter = in[5+i*2] >> 4;
338 "Invalid XA-ADPCM filter %d (max. allowed is 4)\n",
349 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
351 s_1 = av_clip_int16(s);
363 out0 += 28 * (3 - channels);
364 out1 += 28 * (3 - channels);
375 int k0, signmask, nb_bits, count;
376 int size = buf_size*8;
384 k0 = 1 << (nb_bits-2);
385 signmask = 1 << (nb_bits-1);
388 for (i = 0; i < avctx->
channels; i++) {
396 for (i = 0; i < avctx->
channels; i++) {
411 if (delta & signmask)
437 int buf_size,
int *coded_samples)
442 int has_coded_samples = 0;
450 if (buf_size < 76 * ch)
455 if (buf_size < 34 * ch)
465 nb_samples = buf_size * 2 / ch;
480 return (buf_size - header_size) * 2 / ch;
485 has_coded_samples = 1;
486 *coded_samples = bytestream2_get_le32(gb);
487 *coded_samples -= *coded_samples % 28;
488 nb_samples = (buf_size - 12) / 30 * 28;
491 has_coded_samples = 1;
492 *coded_samples = bytestream2_get_le32(gb);
493 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
496 nb_samples = (buf_size - ch) / ch * 2;
503 has_coded_samples = 1;
506 header_size = 4 + 9 * ch;
507 *coded_samples = bytestream2_get_le32(gb);
510 header_size = 4 + 5 * ch;
511 *coded_samples = bytestream2_get_le32(gb);
514 header_size = 4 + 5 * ch;
515 *coded_samples = bytestream2_get_be32(gb);
518 *coded_samples -= *coded_samples % 28;
519 nb_samples = (buf_size - header_size) * 2 / ch;
520 nb_samples -= nb_samples % 28;
525 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
530 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
535 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
540 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
546 int samples_per_byte;
556 nb_samples += buf_size * samples_per_byte / ch;
561 int buf_bits = buf_size * 8 - 2;
562 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
563 int block_hdr_size = 22 * ch;
564 int block_size = block_hdr_size + nbits * ch * 4095;
565 int nblocks = buf_bits / block_size;
566 int bits_left = buf_bits - nblocks * block_size;
567 nb_samples = nblocks * 4096;
568 if (bits_left >= block_hdr_size)
569 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
573 has_coded_samples = 1;
575 *coded_samples = bytestream2_get_be32(gb);
576 *coded_samples -= *coded_samples % 14;
577 nb_samples = (buf_size - 80) / (8 * ch) * 14;
580 nb_samples = (buf_size / 128) * 224 / ch;
585 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
592 int *got_frame_ptr,
AVPacket *avpkt)
595 int buf_size = avpkt->
size;
598 int n, m, channel, i;
607 nb_samples =
get_nb_samples(avctx, &gb, buf_size, &coded_samples);
608 if (nb_samples <= 0) {
625 if (coded_samples != nb_samples)
636 for (channel = 0; channel < avctx->
channels; channel++) {
639 cs = &(c->
status[channel]);
643 predictor =
sign_extend(bytestream2_get_be16u(&gb), 16);
644 step_index = predictor & 0x7F;
665 samples = samples_p[channel];
667 for (m = 0; m < 64; m += 2) {
668 int byte = bytestream2_get_byteu(&gb);
687 for (n = 0; n < (nb_samples - 1) / 8; n++) {
688 for (i = 0; i < avctx->
channels; i++) {
690 samples = &samples_p[i][1 + n * 8];
691 for (m = 0; m < 8; m += 2) {
692 int v = bytestream2_get_byteu(&gb);
700 for (i = 0; i < avctx->
channels; i++)
703 for (i = 0; i < avctx->
channels; i++) {
712 for (i = 0; i < avctx->
channels; i++) {
715 for (n = nb_samples >> 1; n > 0; n--) {
716 int v = bytestream2_get_byteu(&gb);
726 block_predictor = bytestream2_get_byteu(&gb);
727 if (block_predictor > 6) {
735 block_predictor = bytestream2_get_byteu(&gb);
736 if (block_predictor > 6) {
758 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
759 int byte = bytestream2_get_byteu(&gb);
766 for (channel = 0; channel < avctx->
channels; channel++) {
776 for (n = (nb_samples >> (1 - st)) - 1; n > 0; n--) {
777 int v = bytestream2_get_byteu(&gb);
786 int decode_top_nibble_next = 0;
804 #define DK3_GET_NEXT_NIBBLE() \
805 if (decode_top_nibble_next) { \
806 nibble = last_byte >> 4; \
807 decode_top_nibble_next = 0; \
809 last_byte = bytestream2_get_byteu(&gb); \
810 nibble = last_byte & 0x0F; \
811 decode_top_nibble_next = 1; \
814 while (samples < samples_end) {
844 for (channel = 0; channel < avctx->
channels; channel++) {
855 for (n = nb_samples >> (1 - st); n > 0; n--) {
857 int v = bytestream2_get_byteu(&gb);
872 int v = bytestream2_get_byteu(&gb);
879 for (channel = 0; channel < avctx->
channels; channel++) {
880 int16_t *smp = samples_p[channel];
882 for (n = nb_samples / 2; n > 0; n--) {
883 int v = bytestream2_get_byteu(&gb);
889 for (n = nb_samples / 2; n > 0; n--) {
890 for (channel = 0; channel < avctx->
channels; channel++) {
891 int v = bytestream2_get_byteu(&gb);
902 int16_t *out0 = samples_p[0];
903 int16_t *out1 = samples_p[1];
904 int samples_per_block = 28 * (3 - avctx->
channels) * 4;
905 int sample_offset = 0;
909 avctx->
channels, sample_offset)) < 0)
912 sample_offset += samples_per_block;
917 for (i=0; i<=st; i++) {
925 for (i=0; i<=st; i++)
928 for (n = nb_samples >> (1 - st); n > 0; n--) {
929 int byte = bytestream2_get_byteu(&gb);
935 for (n = nb_samples >> (1 - st); n > 0; n--) {
936 int byte = bytestream2_get_byteu(&gb);
943 int previous_left_sample, previous_right_sample;
944 int current_left_sample, current_right_sample;
945 int next_left_sample, next_right_sample;
946 int coeff1l, coeff2l, coeff1r, coeff2r;
947 int shift_left, shift_right;
952 current_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
953 previous_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
954 current_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
955 previous_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
957 for (count1 = 0; count1 < nb_samples / 28; count1++) {
958 int byte = bytestream2_get_byteu(&gb);
964 byte = bytestream2_get_byteu(&gb);
965 shift_left = 20 - (byte >> 4);
966 shift_right = 20 - (byte & 0x0F);
968 for (count2 = 0; count2 < 28; count2++) {
969 byte = bytestream2_get_byteu(&gb);
970 next_left_sample =
sign_extend(byte >> 4, 4) << shift_left;
971 next_right_sample =
sign_extend(byte, 4) << shift_right;
973 next_left_sample = (next_left_sample +
974 (current_left_sample * coeff1l) +
975 (previous_left_sample * coeff2l) + 0x80) >> 8;
976 next_right_sample = (next_right_sample +
977 (current_right_sample * coeff1r) +
978 (previous_right_sample * coeff2r) + 0x80) >> 8;
980 previous_left_sample = current_left_sample;
981 current_left_sample = av_clip_int16(next_left_sample);
982 previous_right_sample = current_right_sample;
983 current_right_sample = av_clip_int16(next_right_sample);
984 *samples++ = current_left_sample;
985 *samples++ = current_right_sample;
995 int coeff[2][2], shift[2];
997 for(channel = 0; channel < avctx->
channels; channel++) {
998 int byte = bytestream2_get_byteu(&gb);
1001 shift[channel] = 20 - (byte & 0x0F);
1003 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1006 byte[0] = bytestream2_get_byteu(&gb);
1007 if (st) byte[1] = bytestream2_get_byteu(&gb);
1008 for(i = 4; i >= 0; i-=4) {
1009 for(channel = 0; channel < avctx->
channels; channel++) {
1013 c->
status[channel].
sample2 * coeff[channel][1] + 0x80) >> 8;
1031 int previous_sample, current_sample, next_sample;
1034 unsigned int channel;
1039 for (channel=0; channel<avctx->
channels; channel++)
1040 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1041 bytestream2_get_le32(&gb)) +
1044 for (channel=0; channel<avctx->
channels; channel++) {
1046 samplesC = samples_p[channel];
1049 current_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1050 previous_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1056 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1057 int byte = bytestream2_get_byte(&gb);
1059 current_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1060 previous_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1062 for (count2=0; count2<28; count2++)
1063 *samplesC++ =
sign_extend(bytestream2_get_be16(&gb), 16);
1067 shift = 20 - (byte & 0x0F);
1069 for (count2=0; count2<28; count2++) {
1073 byte = bytestream2_get_byte(&gb);
1077 next_sample += (current_sample * coeff1) +
1078 (previous_sample * coeff2);
1079 next_sample = av_clip_int16(next_sample >> 8);
1081 previous_sample = current_sample;
1082 current_sample = next_sample;
1083 *samplesC++ = current_sample;
1089 }
else if (count != count1) {
1091 count =
FFMAX(count, count1);
1105 for (channel=0; channel<avctx->
channels; channel++) {
1106 int coeff[2][4], shift[4];
1107 int16_t *s = samples_p[channel];
1108 for (n = 0; n < 4; n++, s += 32) {
1109 int val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1114 val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1115 shift[n] = 20 - (val & 0x0F);
1119 for (m=2; m<32; m+=2) {
1120 s = &samples_p[channel][m];
1121 for (n = 0; n < 4; n++, s += 32) {
1123 int byte = bytestream2_get_byteu(&gb);
1126 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1127 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1130 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1131 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1153 for (n = nb_samples >> (1 - st); n > 0; n--) {
1154 int hi, lo, v = bytestream2_get_byteu(&gb);
1169 for (n = nb_samples >> (1 - st); n > 0; n--) {
1170 int v = bytestream2_get_byteu(&gb);
1180 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1182 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1187 for (n = nb_samples >> (1 - st); n > 0; n--) {
1188 int byte = bytestream2_get_byteu(&gb);
1195 for (n = nb_samples / 3; n > 0; n--) {
1196 int byte = bytestream2_get_byteu(&gb);
1200 (byte >> 2) & 0x07, 3, 0);
1205 for (n = nb_samples >> (2 - st); n > 0; n--) {
1206 int byte = bytestream2_get_byteu(&gb);
1210 (byte >> 4) & 0x03, 2, 2);
1212 (byte >> 2) & 0x03, 2, 2);
1223 for (n = nb_samples >> (1 - st); n > 0; n--) {
1224 int v = bytestream2_get_byteu(&gb);
1235 for (i = 0; i < 2; i++)
1236 for (n = 0; n < 16; n++)
1237 table[i][n] =
sign_extend(bytestream2_get_be16u(&gb), 16);
1240 for (i = 0; i < 2; i++)
1241 for (n = 0; n < 2; n++)
1242 prev[i][n] =
sign_extend(bytestream2_get_be16u(&gb), 16);
1244 for (ch = 0; ch <= st; ch++) {
1245 samples = samples_p[ch];
1248 for (i = 0; i < nb_samples / 14; i++) {
1249 int byte = bytestream2_get_byteu(&gb);
1250 int index = (byte >> 4) & 7;
1251 unsigned int exp = byte & 0x0F;
1252 int factor1 = table[ch][index * 2];
1253 int factor2 = table[ch][index * 2 + 1];
1256 for (n = 0; n < 14; n++) {
1262 byte = bytestream2_get_byteu(&gb);
1266 sampledat = ((prev[ch][0]*factor1
1267 + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
1268 *samples = av_clip_int16(sampledat);
1269 prev[ch][1] = prev[ch][0];
1270 prev[ch][0] = *samples++;
1296 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1297 AVCodec ff_ ## name_ ## _decoder = { \
1299 .type = AVMEDIA_TYPE_AUDIO, \
1301 .priv_data_size = sizeof(ADPCMDecodeContext), \
1302 .init = adpcm_decode_init, \
1303 .decode = adpcm_decode_frame, \
1304 .capabilities = CODEC_CAP_DR1, \
1305 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1306 .sample_fmts = sample_fmts_, \