39 #include "config_components.h"
71 #define CASE_0(codec_id, ...)
72 #define CASE_1(codec_id, ...) \
76 #define CASE_2(enabled, codec_id, ...) \
77 CASE_ ## enabled(codec_id, __VA_ARGS__)
78 #define CASE_3(config, codec_id, ...) \
79 CASE_2(config, codec_id, __VA_ARGS__)
80 #define CASE(codec, ...) \
81 CASE_3(CONFIG_ ## codec ## _DECODER, AV_CODEC_ID_ ## codec, __VA_ARGS__)
93 { 0, 2048, 0, 1024, 4096, 3584, 3072, 4608, 4200, 4800, 5120, 2048, 1024, -1024, -1024, -2048 },
94 { 0, 0, 2048, 1024, -2048, -1536, -1024, -2560, -2248, -2300, -3072, -2048, -1024, 1024, 0, 0 }
112 -1, -1, -1, -1, 1, 2, 3, 4, -1
122 1, 1, 1, 1, 2, 2, 3, 3, 4, 5,
123 6, 7, 8, 10, 12, 14, 16, 20, 24, 28,
124 32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
125 192, 224, 256, 320, 384, 448, 512, 640, 768, 896,
126 1024, 1280, 1536, 1792, 2048, 2560, 3072, 3584, 4096, 5120,
127 6144, 7168, 8192, 10240, 12288, 14336, 16384, 20480, 24576, 28672, 0
141 -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
142 -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
153 { 1, 5, 9, 13, 16, 20, 24, 28,
154 -1, -5, -9, -13, -16, -20, -24, -28, },
155 { 2, 6, 11, 15, 20, 24, 29, 33,
156 -2, -6, -11, -15, -20, -24, -29, -33, },
157 { 2, 7, 13, 18, 23, 28, 34, 39,
158 -2, -7, -13, -18, -23, -28, -34, -39, },
159 { 3, 9, 15, 21, 28, 34, 40, 46,
160 -3, -9, -15, -21, -28, -34, -40, -46, },
161 { 3, 11, 18, 26, 33, 41, 48, 56,
162 -3, -11, -18, -26, -33, -41, -48, -56, },
163 { 4, 13, 22, 31, 40, 49, 58, 67,
164 -4, -13, -22, -31, -40, -49, -58, -67, },
165 { 5, 16, 26, 37, 48, 59, 69, 80,
166 -5, -16, -26, -37, -48, -59, -69, -80, },
167 { 6, 19, 31, 44, 57, 70, 82, 95,
168 -6, -19, -31, -44, -57, -70, -82, -95, },
169 { 7, 22, 38, 53, 68, 83, 99, 114,
170 -7, -22, -38, -53, -68, -83, -99, -114, },
171 { 9, 27, 45, 63, 81, 99, 117, 135,
172 -9, -27, -45, -63, -81, -99, -117, -135, },
173 { 10, 32, 53, 75, 96, 118, 139, 161,
174 -10, -32, -53, -75, -96, -118, -139, -161, },
175 { 12, 38, 64, 90, 115, 141, 167, 193,
176 -12, -38, -64, -90, -115, -141, -167, -193, },
177 { 15, 45, 76, 106, 137, 167, 198, 228,
178 -15, -45, -76, -106, -137, -167, -198, -228, },
179 { 18, 54, 91, 127, 164, 200, 237, 273,
180 -18, -54, -91, -127, -164, -200, -237, -273, },
181 { 21, 65, 108, 152, 195, 239, 282, 326,
182 -21, -65, -108, -152, -195, -239, -282, -326, },
183 { 25, 77, 129, 181, 232, 284, 336, 388,
184 -25, -77, -129, -181, -232, -284, -336, -388, },
185 { 30, 92, 153, 215, 276, 338, 399, 461,
186 -30, -92, -153, -215, -276, -338, -399, -461, },
187 { 36, 109, 183, 256, 329, 402, 476, 549,
188 -36, -109, -183, -256, -329, -402, -476, -549, },
189 { 43, 130, 218, 305, 392, 479, 567, 654,
190 -43, -130, -218, -305, -392, -479, -567, -654, },
191 { 52, 156, 260, 364, 468, 572, 676, 780,
192 -52, -156, -260, -364, -468, -572, -676, -780, },
193 { 62, 186, 310, 434, 558, 682, 806, 930,
194 -62, -186, -310, -434, -558, -682, -806, -930, },
195 { 73, 221, 368, 516, 663, 811, 958, 1106,
196 -73, -221, -368, -516, -663, -811, -958, -1106, },
197 { 87, 263, 439, 615, 790, 966, 1142, 1318,
198 -87, -263, -439, -615, -790, -966, -1142, -1318, },
199 { 104, 314, 523, 733, 942, 1152, 1361, 1571,
200 -104, -314, -523, -733, -942, -1152, -1361, -1571, },
201 { 124, 374, 623, 873, 1122, 1372, 1621, 1871,
202 -124, -374, -623, -873, -1122, -1372, -1621, -1871, },
203 { 148, 445, 743, 1040, 1337, 1634, 1932, 2229,
204 -148, -445, -743, -1040, -1337, -1634, -1932, -2229, },
205 { 177, 531, 885, 1239, 1593, 1947, 2301, 2655,
206 -177, -531, -885, -1239, -1593, -1947, -2301, -2655, },
207 { 210, 632, 1053, 1475, 1896, 2318, 2739, 3161,
208 -210, -632, -1053, -1475, -1896, -2318, -2739, -3161, },
209 { 251, 753, 1255, 1757, 2260, 2762, 3264, 3766,
210 -251, -753, -1255, -1757, -2260, -2762, -3264, -3766, },
211 { 299, 897, 1495, 2093, 2692, 3290, 3888, 4486,
212 -299, -897, -1495, -2093, -2692, -3290, -3888, -4486, },
213 { 356, 1068, 1781, 2493, 3206, 3918, 4631, 5343,
214 -356, -1068, -1781, -2493, -3206, -3918, -4631, -5343, },
215 { 424, 1273, 2121, 2970, 3819, 4668, 5516, 6365,
216 -424, -1273, -2121, -2970, -3819, -4668, -5516, -6365, },
220 16, 17, 19, 21, 23, 25, 28, 31, 34, 37,
221 41, 45, 50, 55, 60, 66, 73, 80, 88, 97,
222 107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
223 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
224 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552
231 { -1, -1, -1, -1, 2, 4, 6, 8 },
232 { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
236 -1, -1, -1, 1, 4, 7, 10, 12,
240 8, 6, 4, 2, -1, -1, -1, -1,
241 -1, -1, -1, -1, 2, 4, 6, 8,
257 unsigned int min_channels = 1;
258 unsigned int max_channels = 2;
390 if ((nibble & 8) == 0)
400 c->step =
av_clip(
c->step * 2, 127, 24576);
431 step_index =
av_clip(step_index, 0, 88);
441 c->step_index = step_index;
443 return (int16_t)
c->predictor;
454 step_index =
av_clip(step_index, 0, 88);
467 c->step_index = step_index;
469 return (int16_t)
c->predictor;
480 step_index =
av_clip(step_index, 0, 88);
490 c->step_index = step_index;
492 return (int16_t)
c->predictor;
506 c->step_index =
av_clip(step_index, 0, 88);
508 return (int16_t)
c->predictor;
521 step_index =
av_clip(step_index, 0, 60);
526 c->step_index = step_index;
539 step_index =
av_clip(step_index, 0, 88);
541 sign = nibble & (1 <<
shift);
551 c->step_index = step_index;
553 return (int16_t)
c->predictor;
564 step_index =
av_clip(step_index, 0, 88);
577 c->step_index = step_index;
592 switch (frame_format) {
594 tmp = bytestream2_get_be16(gb);
596 c->status[ch].step_index =
tmp & 0x7f;
597 *outbuf++ =
c->status[ch].predictor;
604 c->status[ch].step_index =
av_clip(
c->status[ch].step_index, 0, 88);
607 for (
int i = 0;
i < samples_to_do;
i++) {
609 nibble = bytestream2_get_byte(gb);
627 switch (frame_format) {
629 tmp = bytestream2_get_be16(gb);
631 c->status[ch].step_index =
tmp & 0x7f;
637 tmp = bytestream2_get_be16(gb);
639 c->status[ch].step_index = bytestream2_get_byte(gb);
643 c->status[ch].step_index =
av_clip(
c->status[ch].step_index, 0, 88);
646 if (frame_format == 1 || frame_format == 3) {
648 *outbuf++ = (int16_t)
c->status[st - ch].predictor;
652 for (
int i = 0;
i < samples_to_do;
i += 1+(!st)) {
653 uint8_t nibble = bytestream2_get_byte(gb);
666 predictor = (((
c->sample1) * (
c->coeff1)) + ((
c->sample2) * (
c->coeff2))) / 64;
667 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) *
c->idelta;
669 c->sample2 =
c->sample1;
672 if (
c->idelta < 16)
c->idelta = 16;
673 if (
c->idelta > INT_MAX/768) {
675 c->idelta = INT_MAX/768;
687 step_index =
av_clip(step_index, 0, 48);
697 c->step_index = step_index;
699 return c->predictor * 16;
714 c->predictor = ((
c->predictor * 254) >> 8) + (sign ? -
diff :
diff);
718 c->step =
av_clip(new_step, 511, 32767);
720 return (int16_t)
c->predictor;
727 sign = nibble & (1<<(
size-1));
737 else if (
delta == 0 &&
c->step > 0)
740 return (int16_t)
c->predictor;
775 }
else if (
code == 127 ||
code == -128) {
789 int16_t
index =
c->step_index;
796 sample += lookup_sample >> 1;
798 sample += lookup_sample >> 2;
800 sample += lookup_sample >> 3;
802 sample += lookup_sample >> 4;
804 sample += lookup_sample >> 5;
806 sample += lookup_sample >> 6;
831 out0 += sample_offset;
835 out1 += sample_offset;
838 shift = 12 - (in[4+
i*2] & 15);
858 s = t*(1<<
shift) + ((s_1*f0 + s_2*f1+32)>>6);
871 shift = 12 - (in[5+
i*2] & 15);
889 s = t*(1<<
shift) + ((s_1*f0 + s_2*f1+32)>>6);
916 int k0, signmask, nb_bits, count;
917 int size = buf_size*8;
925 k0 = 1 << (nb_bits-2);
926 signmask = 1 << (nb_bits-1);
952 if (
delta & signmask)
953 c->status[
i].predictor -= vpdiff;
955 c->status[
i].predictor += vpdiff;
959 c->status[
i].step_index =
av_clip(
c->status[
i].step_index, 0, 88);
987 int sign,
delta, add;
998 c->step = (3 *
c->step) >> 2;
1002 c->step = (4 *
c->step - (
c->step >> 1)) >> 2;
1006 c->step = ((
c->step >> 1) + add) >> 1;
1009 add = 4 *
c->step - (
c->step >> 1);
1010 c->step = 2 *
c->step;
1013 add = (11 *
c->step) >> 1;
1014 c->step = 3 *
c->step;
1017 av_unreachable(
"There are cases for all control paths when bits is 3-bit");
1025 return c->predictor;
1030 int sign,
delta, add;
1041 c->step = (3 *
c->step) >> 2;
1045 c->step = (3 *
c->step) >> 2;
1057 add = (11 *
c->step) >> 1;
1058 c->step +=
c->step >> 2;
1061 add = (15 *
c->step) >> 1;
1062 c->step = 2 *
c->step;
1066 add = (19 *
c->step) >> 1;
1068 add = (21 *
c->step) >> 1;
1069 c->step = (
c->step >> 1) + 2 *
c->step;
1072 add = (25 *
c->step) >> 1;
1073 c->step = 5 *
c->step;
1076 av_unreachable(
"There are cases for all control paths when bits is 4-bit");
1084 return c->predictor;
1089 int sign,
delta, add;
1100 c->step += (
c->step >> 2) - (
c->step >> 1);
1105 c->step += (
c->step >> 3) - (
c->step >> 2);
1109 c->step += (
c->step >> 4) - (
c->step >> 3);
1114 c->step +=
c->step >> 3;
1117 c->step +=
c->step >> 2;
1120 c->step +=
c->step >> 1;
1123 c->step = 2 *
c->step - (
c->step >> 3);
1126 c->step = 2 *
c->step + (
c->step >> 3);
1129 c->step = 2 *
c->step + (
c->step >> 1) - (
c->step >> 3);
1132 c->step = 3 *
c->step - (
c->step >> 2);
1139 c->step = (7 *
c->step) >> 1;
1148 return c->predictor;
1163 int buf_size,
int *coded_samples,
int *approx_nb_samples)
1168 int has_coded_samples = 0;
1172 *approx_nb_samples = 0;
1180 if (buf_size < 76 * ch)
1185 if (buf_size < 34 * ch)
1190 nb_samples = (buf_size / 9) * 16;
1206 nb_samples = buf_size * 2 / ch;
1224 if (header_size > 0)
1225 return (buf_size - header_size) * 2 / ch;
1231 has_coded_samples = 1;
1232 *coded_samples = bytestream2_get_le32u(gb);
1233 nb_samples =
FFMIN((buf_size - 8) * 2, *coded_samples);
1239 has_coded_samples = 1;
1240 *coded_samples = bytestream2_get_le32(gb);
1241 *coded_samples -= *coded_samples % 28;
1242 nb_samples = (buf_size - 12) / (ch == 2 ? 30 : 15) * 28;
1245 nb_samples = ((bytestream2_peek_be64(gb) >> 16) & 0xFFFF);
1249 int frame_format = bytestream2_get_be16(gb);
1252 if (frame_format == 1)
1254 if (frame_format == 3)
1257 nb_samples = (buf_size -
skip) * 2 / ch;
1262 has_coded_samples = 1;
1263 *coded_samples = bytestream2_get_le32(gb);
1264 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
1267 nb_samples = (buf_size - ch) / ch * 2;
1274 has_coded_samples = 1;
1277 header_size = 4 + 9 * ch;
1278 *coded_samples = bytestream2_get_le32(gb);
1281 header_size = 4 + 5 * ch;
1282 *coded_samples = bytestream2_get_le32(gb);
1285 header_size = 4 + 5 * ch;
1286 *coded_samples = bytestream2_get_be32(gb);
1289 *coded_samples -= *coded_samples % 28;
1290 nb_samples = (buf_size - header_size) * 2 / ch;
1291 nb_samples -= nb_samples % 28;
1292 *approx_nb_samples = 1;
1297 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
1302 if (buf_size < 4 * ch)
1304 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
1309 nb_samples = (buf_size - 4 * ch) * 2 / ch;
1314 nb_samples = (buf_size - 4 * ch) * 2 / ch;
1321 if (buf_size < 4 * ch)
1323 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
1325 CASE(ADPCM_IMA_XBOX,
1330 if (buf_size < 4 * ch)
1332 nb_samples = (buf_size - 4 * ch) / (bsize * ch) * bsamples + 1;
1337 nb_samples = (buf_size - 6 * ch) * 2 / ch;
1342 nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
1348 int samples_per_byte;
1354 if (!
s->status[0].step_index) {
1360 nb_samples += buf_size * samples_per_byte / ch;
1365 int buf_bits = buf_size * 8 - 2;
1366 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
1367 int block_hdr_size = 22 * ch;
1368 int block_size = block_hdr_size + nbits * ch * 4095;
1369 int nblocks = buf_bits / block_size;
1370 int bits_left = buf_bits - nblocks * block_size;
1371 nb_samples = nblocks * 4096;
1373 nb_samples += 1 + (
bits_left - block_hdr_size) / (nbits * ch);
1379 nb_samples = buf_size * 14 / (8 * ch);
1382 has_coded_samples = 1;
1385 bytestream2_get_le32(gb) :
1386 bytestream2_get_be32(gb);
1387 buf_size -= 8 + 36 * ch;
1389 nb_samples = buf_size / 8 * 14;
1390 if (buf_size % 8 > 1)
1391 nb_samples += (buf_size % 8 - 1) * 2;
1392 *approx_nb_samples = 1;
1395 nb_samples = buf_size / (9 * ch) * 16;
1398 nb_samples = (buf_size / 128) * 224 / ch;
1401 nb_samples = buf_size / (21 * ch) * 32;
1405 nb_samples = buf_size / (16 * ch) * 28;
1408 nb_samples = ((buf_size - 1) / ch) * 2;
1415 nb_samples = buf_size / ch;
1425 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
1432 int *got_frame_ptr,
AVPacket *avpkt)
1434 const uint8_t *buf = avpkt->
data;
1435 int buf_size = avpkt->
size;
1439 int16_t **samples_p;
1441 int nb_samples, coded_samples, approx_nb_samples,
ret;
1445 nb_samples =
get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
1446 if (nb_samples <= 0) {
1452 frame->nb_samples = nb_samples;
1456 samples_p = (int16_t **)
frame->extended_data;
1460 if (coded_samples) {
1461 if (!approx_nb_samples && coded_samples != nb_samples)
1463 frame->nb_samples = nb_samples = coded_samples;
1503 for (
int m = 0; m < 64; m += 2) {
1504 int byte = bytestream2_get_byteu(&gb);
1529 for (
int n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1532 samples = &samples_p[
i][1 + n * samples_per_block];
1533 for (
int j = 0; j < block_size; j++) {
1535 (j % 4) + (j / 4) * (
channels * 4) +
i * 4];
1540 for (
int m = 0; m < samples_per_block; m++) {
1548 for (
int n = 0; n < (nb_samples - 1) / 8; n++) {
1551 samples = &samples_p[
i][1 + n * 8];
1552 for (
int m = 0; m < 8; m += 2) {
1553 int v = bytestream2_get_byteu(&gb);
1561 CASE(ADPCM_IMA_XBOX,
1574 for (
int n = 0; n < (nb_samples-1) / 8; n++) {
1577 samples = &samples_p[
i][1 + n * 8];
1578 for (
int m = 0; m < 8; m += 2) {
1579 int v = bytestream2_get_byteu(&gb);
1585 frame->nb_samples--;
1589 c->status[
i].predictor =
sign_extend(bytestream2_get_le16u(&gb), 16);
1592 c->status[
i].step_index =
sign_extend(bytestream2_get_le16u(&gb), 16);
1593 if (
c->status[
i].step_index > 88
u) {
1595 i,
c->status[
i].step_index);
1603 for (
int n = nb_samples >> 1; n > 0; n--) {
1604 int v = bytestream2_get_byteu(&gb);
1612 c->status[
i].predictor =
sign_extend(bytestream2_get_le16u(&gb), 16);
1614 c->status[
i].step =
sign_extend(bytestream2_get_le16u(&gb), 16);
1616 for (
int n = 0; n < nb_samples >> (1 - st); n++) {
1617 int v = bytestream2_get_byteu(&gb);
1623 int block_predictor;
1628 block_predictor = bytestream2_get_byteu(&gb);
1629 if (block_predictor > 6) {
1641 for (
int n = (nb_samples - 2) >> 1; n > 0; n--) {
1642 int byte = bytestream2_get_byteu(&gb);
1648 block_predictor = bytestream2_get_byteu(&gb);
1649 if (block_predictor > 6) {
1657 block_predictor = bytestream2_get_byteu(&gb);
1658 if (block_predictor > 6) {
1666 c->status[0].idelta =
sign_extend(bytestream2_get_le16u(&gb), 16);
1668 c->status[1].idelta =
sign_extend(bytestream2_get_le16u(&gb), 16);
1671 c->status[0].sample1 =
sign_extend(bytestream2_get_le16u(&gb), 16);
1672 if (st)
c->status[1].sample1 =
sign_extend(bytestream2_get_le16u(&gb), 16);
1673 c->status[0].sample2 =
sign_extend(bytestream2_get_le16u(&gb), 16);
1674 if (st)
c->status[1].sample2 =
sign_extend(bytestream2_get_le16u(&gb), 16);
1677 if (st) *
samples++ =
c->status[1].sample2;
1679 if (st) *
samples++ =
c->status[1].sample1;
1680 for (
int n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1681 int byte = bytestream2_get_byteu(&gb);
1690 c->status[
channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
1691 c->status[
channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1696 for (
int n = 0; n < nb_samples; n += 2) {
1697 int v = bytestream2_get_byteu(&gb);
1701 for (
int n = 0; n < nb_samples; n += 2) {
1702 int v = bytestream2_get_byteu(&gb);
1719 for (
int n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1720 int v = bytestream2_get_byteu(&gb);
1728 if (decode_top_nibble_next) { \
1729 nibble = last_byte >> 4; \
1730 decode_top_nibble_next = 0; \
1732 last_byte = bytestream2_get_byteu(&gb); \
1733 nibble = last_byte & 0x0F; \
1734 decode_top_nibble_next = 1; \
1739 int decode_top_nibble_next = 0;
1744 c->status[0].predictor =
sign_extend(bytestream2_get_le16u(&gb), 16);
1745 c->status[1].predictor =
sign_extend(bytestream2_get_le16u(&gb), 16);
1746 c->status[0].step_index = bytestream2_get_byteu(&gb);
1747 c->status[1].step_index = bytestream2_get_byteu(&gb);
1748 if (
c->status[0].step_index > 88
u ||
c->status[1].step_index > 88
u){
1750 c->status[0].step_index,
c->status[1].step_index);
1754 diff_channel =
c->status[1].predictor;
1756 while (
samples < samples_end) {
1770 diff_channel = (diff_channel +
c->status[1].predictor) / 2;
1771 *
samples++ =
c->status[0].predictor +
c->status[1].predictor;
1772 *
samples++ =
c->status[0].predictor -
c->status[1].predictor;
1779 diff_channel = (diff_channel +
c->status[1].predictor) / 2;
1780 *
samples++ =
c->status[0].predictor +
c->status[1].predictor;
1781 *
samples++ =
c->status[0].predictor -
c->status[1].predictor;
1787 CASE(ADPCM_IMA_MAGIX,
1799 for (
int m = 0; m < avctx->
block_align-8; m += 8) {
1800 uint32_t v0 = bytestream2_get_le32u(&gb);
1801 uint32_t v1 = bytestream2_get_le32u(&gb);
1803 for (
int n = 8; n > 0; n--, v0 >>= 4, v1 >>= 4,
samples += 2) {
1821 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
1823 int v = bytestream2_get_byteu(&gb);
1836 CASE(ADPCM_IMA_MOFLEX,
1848 for (
int subframe = 0; subframe < nb_samples / 256; subframe++) {
1851 for (
int n = 0; n < 256; n += 2) {
1852 int v = bytestream2_get_byteu(&gb);
1859 CASE(ADPCM_IMA_DAT4,
1864 for (
int n = 0; n < nb_samples; n += 2) {
1865 int v = bytestream2_get_byteu(&gb);
1872 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
1873 int v = bytestream2_get_byteu(&gb);
1878 CASE(ADPCM_IMA_HVQM2,
1879 int format = bytestream2_get_be16(&gb);
1884 CASE(ADPCM_IMA_HVQM4,
1885 int format = bytestream2_get_be16(&gb);
1891 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
1892 int v = bytestream2_get_byteu(&gb);
1898 for (
int n = nb_samples / 2; n > 0; n--) {
1900 int v = bytestream2_get_byteu(&gb);
1908 for (
int n = nb_samples / 2; n > 0; n--) {
1910 int v = bytestream2_get_byteu(&gb);
1917 CASE(ADPCM_IMA_CUNNING,
1919 int16_t *smp = samples_p[
channel];
1920 for (
int n = 0; n < nb_samples / 2; n++) {
1921 int v = bytestream2_get_byteu(&gb);
1928 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
1929 int v = bytestream2_get_byteu(&gb);
1945 for (
int n = 0; n < nb_samples / 2; n++) {
1948 byte[0] = bytestream2_get_byteu(&gb);
1950 byte[1] = bytestream2_get_byteu(&gb);
1960 if (
c->vqa_version == 3) {
1962 int16_t *smp = samples_p[
channel];
1964 for (
int n = nb_samples / 2; n > 0; n--) {
1965 int v = bytestream2_get_byteu(&gb);
1971 for (
int n = nb_samples / 2; n > 0; n--) {
1973 int v = bytestream2_get_byteu(&gb);
1983 int bytes_remaining,
block = 0;
1990 history[1] =
sign_extend(bytestream2_get_le16(&gb), 16);
1991 history[0] =
sign_extend(bytestream2_get_le16(&gb), 16);
1992 scale = bytestream2_get_le16(&gb);
1994 out[0] = history[1];
1995 out[1] = history[0];
1997 for (
int n = 0; n < 15; n++) {
1998 unsigned byte = bytestream2_get_byte(&gb);
2004 out[2+n*2] = nibble[0]*
scale + ((history[0]*3667 - history[1]*1642) >> 11);
2005 history[1] = history[0];
2006 history[0] =
out[2+n*2];
2008 out[2+n*2+1] = nibble[1]*
scale + ((history[0]*3667 - history[1]*1642) >> 11);
2009 history[1] = history[0];
2010 history[0] =
out[2+n*2+1];
2017 if (bytes_remaining > 0) {
2022 int16_t *out0 = samples_p[0];
2023 int16_t *out1 = samples_p[1];
2024 int samples_per_block = 28 * (3 -
channels) * 4;
2025 int sample_offset = 0;
2026 int bytes_remaining;
2029 &
c->status[0], &
c->status[1],
2033 sample_offset += samples_per_block;
2038 if (bytes_remaining > 0) {
2042 CASE(ADPCM_IMA_ESCAPE,
2043 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
2044 int byte = bytestream2_get_byteu(&gb);
2049 CASE(ADPCM_IMA_EA_EACS,
2050 for (
int i = 0;
i <= st;
i++) {
2051 c->status[
i].step_index = bytestream2_get_le32u(&gb);
2052 if (
c->status[
i].step_index > 88
u) {
2054 i,
c->status[
i].step_index);
2058 for (
int i = 0;
i <= st;
i++) {
2059 c->status[
i].predictor = bytestream2_get_le32u(&gb);
2064 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
2065 int byte = bytestream2_get_byteu(&gb);
2070 CASE(ADPCM_IMA_EA_SEAD,
2071 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
2072 int byte = bytestream2_get_byteu(&gb);
2078 int previous_left_sample, previous_right_sample;
2079 int current_left_sample, current_right_sample;
2080 int next_left_sample, next_right_sample;
2081 int coeff1l, coeff2l, coeff1r, coeff2r;
2082 int shift_left, shift_right;
2090 current_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
2091 previous_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
2092 current_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
2093 previous_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
2095 for (
int count1 = 0; count1 < nb_samples / 28; count1++) {
2096 int byte = bytestream2_get_byteu(&gb);
2103 byte = bytestream2_get_byteu(&gb);
2104 shift_left = 20 - (
byte >> 4);
2105 shift_right = 20 - (
byte & 0x0F);
2108 shift_left = 20 - (
byte & 0x0F);
2111 for (
int count2 = 0; count2 < (
channels == 2 ? 28 : 14); count2++) {
2112 byte = bytestream2_get_byteu(&gb);
2113 next_left_sample =
sign_extend(
byte >> 4, 4) * (1 << shift_left);
2115 next_left_sample = (next_left_sample +
2116 (current_left_sample * coeff1l) +
2117 (previous_left_sample * coeff2l) + 0x80) >> 8;
2119 previous_left_sample = current_left_sample;
2121 *
samples++ = current_left_sample;
2124 next_right_sample =
sign_extend(
byte, 4) * (1 << shift_right);
2126 next_right_sample = (next_right_sample +
2127 (current_right_sample * coeff1r) +
2128 (previous_right_sample * coeff2r) + 0x80) >> 8;
2130 previous_right_sample = current_right_sample;
2132 *
samples++ = current_right_sample;
2134 next_left_sample =
sign_extend(
byte, 4) * (1 << shift_left);
2136 next_left_sample = (next_left_sample +
2137 (current_left_sample * coeff1l) +
2138 (previous_left_sample * coeff2l) + 0x80) >> 8;
2140 previous_left_sample = current_left_sample;
2143 *
samples++ = current_left_sample;
2149 CASE(ADPCM_EA_MAXIS_XA,
2153 int byte = bytestream2_get_byteu(&gb);
2154 for (
int i = 0;
i < 2;
i++)
2158 for (
int count1 = 0; count1 < nb_samples / 2; count1++) {
2161 byte[0] = bytestream2_get_byteu(&gb);
2162 if (st)
byte[1] = bytestream2_get_byteu(&gb);
2163 for (
int i = 4;
i >= 0;
i-=4) {
2177 #
if CONFIG_ADPCM_EA_R1_DECODER || CONFIG_ADPCM_EA_R2_DECODER || CONFIG_ADPCM_EA_R3_DECODER
2186 int previous_sample, current_sample, next_sample;
2195 bytestream2_get_le32(&gb)) +
2202 samplesC = samples_p[
channel];
2205 current_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
2206 previous_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
2208 current_sample =
c->status[
channel].predictor;
2209 previous_sample =
c->status[
channel].prev_sample;
2212 for (count1 = 0; count1 < nb_samples / 28; count1++) {
2213 int byte = bytestream2_get_byte(&gb);
2215 current_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
2216 previous_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
2218 for (
int count2 = 0; count2 < 28; count2++)
2219 *samplesC++ =
sign_extend(bytestream2_get_be16(&gb), 16);
2223 shift = 20 - (
byte & 0x0F);
2225 for (
int count2 = 0; count2 < 28; count2++) {
2229 byte = bytestream2_get_byte(&gb);
2233 next_sample += (current_sample * coeff1) +
2234 (previous_sample * coeff2);
2237 previous_sample = current_sample;
2238 current_sample = next_sample;
2239 *samplesC++ = current_sample;
2245 }
else if (count != count1) {
2247 count =
FFMAX(count, count1);
2251 c->status[
channel].predictor = current_sample;
2252 c->status[
channel].prev_sample = previous_sample;
2256 frame->nb_samples = count * 28;
2265 for (
int n = 0; n < 4; n++,
s += 32) {
2267 for (
int i = 0;
i < 2;
i++)
2276 for (
int m = 2; m < 32; m += 2) {
2278 for (
int n = 0; n < 4; n++,
s += 32) {
2280 int byte = bytestream2_get_byteu(&gb);
2293 CASE(ADPCM_IMA_ACORN,
2297 cs->
step_index = bytestream2_get_le16u(&gb) & 0xFF;
2304 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
2305 int byte = bytestream2_get_byteu(&gb);
2323 c->status[0].predictor =
sign_extend(bytestream2_get_le16u(&gb), 16);
2324 c->status[0].step_index = bytestream2_get_byteu(&gb);
2326 if (
c->status[0].step_index > 88
u) {
2328 c->status[0].step_index);
2332 for (
int n = nb_samples >> 1; n > 0; n--) {
2333 int v = bytestream2_get_byteu(&gb);
2339 if (nb_samples & 1) {
2340 int v = bytestream2_get_byteu(&gb);
2352 c->status[
i].predictor =
sign_extend(bytestream2_get_le16u(&gb), 16);
2353 c->status[
i].step_index = bytestream2_get_byteu(&gb);
2355 if (
c->status[
i].step_index > 88
u) {
2357 c->status[
i].step_index);
2362 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
2363 int v = bytestream2_get_byteu(&gb);
2369 CASE(ADPCM_IMA_SMJPEG,
2371 c->status[
i].predictor =
sign_extend(bytestream2_get_be16u(&gb), 16);
2372 c->status[
i].step_index = bytestream2_get_byteu(&gb);
2374 if (
c->status[
i].step_index > 88
u) {
2376 c->status[
i].step_index);
2381 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
2382 int v = bytestream2_get_byteu(&gb);
2389 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
2390 int v = bytestream2_get_byteu(&gb);
2395 #
if CONFIG_ADPCM_SBPRO_2_DECODER || CONFIG_ADPCM_SBPRO_3_DECODER || \
2396 CONFIG_ADPCM_SBPRO_4_DECODER
2400 if (!
c->status[0].step_index) {
2402 *
samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2404 *
samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2405 c->status[0].step_index = 1;
2409 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
2410 int byte = bytestream2_get_byteu(&gb);
2417 for (
int n = (nb_samples<<st) / 3; n > 0; n--) {
2418 int byte = bytestream2_get_byteu(&gb);
2422 (
byte >> 2) & 0x07, 3, 0);
2427 for (
int n = nb_samples >> (2 - st); n > 0; n--) {
2428 int byte = bytestream2_get_byteu(&gb);
2432 (
byte >> 4) & 0x03, 2, 2);
2434 (
byte >> 2) & 0x03, 2, 2);
2446 for (
int n = nb_samples >> (1 - st); n > 0; n--) {
2447 int v = bytestream2_get_byteu(&gb);
2455 for (
int n = nb_samples >> 1; n > 0; n--) {
2456 int v = bytestream2_get_byteu(&gb);
2463 int samples_per_block;
2467 samples_per_block = avctx->
extradata[0] / 16;
2468 blocks = nb_samples / avctx->
extradata[0];
2470 samples_per_block = nb_samples / 16;
2474 for (
int m = 0; m < blocks; m++) {
2476 int prev1 =
c->status[
channel].sample1;
2477 int prev2 =
c->status[
channel].sample2;
2481 for (
int i = 0;
i < samples_per_block;
i++) {
2482 int byte = bytestream2_get_byteu(&gb);
2483 int scale = 1 << (
byte >> 4);
2484 int index =
byte & 0xf;
2489 for (
int n = 0; n < 16; n++) {
2495 byte = bytestream2_get_byteu(&gb);
2499 sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
2507 c->status[
channel].sample1 = prev1;
2508 c->status[
channel].sample2 = prev2;
2513 #
if CONFIG_ADPCM_THP_DECODER || CONFIG_ADPCM_THP_LE_DECODER
2519 #define THP_GET16(g) \
2521 avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
2522 bytestream2_get_le16u(&(g)) : \
2523 bytestream2_get_be16u(&(g)), 16)
2534 for (
int n = 0; n < 16; n++)
2535 table[
i][n] = THP_GET16(tb);
2538 for (
int n = 0; n < 16; n++)
2539 table[
i][n] = THP_GET16(gb);
2541 if (!
c->has_status) {
2544 c->status[
i].sample1 = THP_GET16(gb);
2545 c->status[
i].sample2 = THP_GET16(gb);
2553 for (
int ch = 0; ch <
channels; ch++) {
2557 for (
int i = 0;
i < (nb_samples + 13) / 14;
i++) {
2558 int byte = bytestream2_get_byteu(&gb);
2559 int index = (
byte >> 4) & 7;
2560 unsigned int exp =
byte & 0x0F;
2565 for (
int n = 0; n < 14 && (
i * 14 + n < nb_samples); n++) {
2571 byte = bytestream2_get_byteu(&gb);
2575 sampledat = ((
c->status[ch].sample1 * factor1
2576 +
c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 <<
exp);
2578 c->status[ch].sample2 =
c->status[ch].sample1;
2579 c->status[ch].sample1 = *
samples++;
2591 for (
int i = 0;
i < nb_samples / 28;
i++) {
2595 header = bytestream2_get_byteu(&gb);
2599 for (
int n = 0; n < 28; n++) {
2604 prev = (
c->status[
channel].sample1 * 0x3c);
2607 prev = (
c->status[
channel].sample1 * 0x73) - (
c->status[
channel].sample2 * 0x34);
2610 prev = (
c->status[
channel].sample1 * 0x62) - (
c->status[
channel].sample2 * 0x37);
2618 byte = bytestream2_get_byteu(&gb);
2624 sampledat = ((sampledat * (1 << 12)) >> (
header & 0xf)) * (1 << 6) + prev;
2627 c->status[
channel].sample1 = sampledat;
2636 int coefs[8*2*8] = { 0 };
2645 order = bytestream2_get_be16(&
cb);
2646 entries = bytestream2_get_be16(&
cb);
2647 if (
version != 1 || order != 2 || entries > 8)
2650 for (
int n = 0; n < order * entries * 8; n++)
2656 int16_t hist[8] = { 0 };
2657 const int order = 2;
2660 hist[6] = cs->sample2;
2661 hist[7] = cs->sample1;
2665 scale = (buf[0] >> 4) & 0xF;
2666 index = (buf[0] >> 0) & 0xF;
2670 for (
int i = 0, j = 0;
i < 16;
i += 2, j++) {
2671 int n0 = (buf[j+1] >> 4) & 0xF;
2672 int n1 = (buf[j+1] >> 0) & 0xF;
2683 for (
int j = 0; j < 2; j++) {
2684 int *sf_codes = &codes[j*8];
2685 int16_t *sf_out = &
out[j*8];
2687 for (
int i = 0;
i < 8;
i++) {
2690 for (
int o = 0; o < order; o++)
2691 delta += coefs[o*8 +
i] * hist[(8 - order) + o];
2693 for (
int k =
i-1; k > -1; k--) {
2694 for (
int o = 1; o < order; o++)
2695 delta += sf_codes[(
i-1) - k] * coefs[(o*8) + k];
2704 for (
int i = 8 - order;
i < 8;
i++)
2705 hist[
i] = sf_out[
i];
2710 cs->sample2 = hist[6];
2711 cs->sample1 = hist[7];
2725 for (
int i = 0;
i < nb_samples_per_block / 28;
i++) {
2728 filter = bytestream2_get_byteu(&gb);
2733 flag = bytestream2_get_byteu(&gb) & 0x7;
2736 for (
int n = 0; n < 28; n++) {
2742 byte = bytestream2_get_byteu(&gb);
2767 filter = bytestream2_get_byteu(&gb);
2773 for (
int n = 0; n < nb_samples_per_block; n++) {
2779 byte = bytestream2_get_byteu(&gb);
2802 for (
int ch = 0; ch <
channels; ch++) {
2803 c->status[ch].predictor =
sign_extend(bytestream2_get_le16(&gb), 16);
2804 c->status[ch].step =
sign_extend(bytestream2_get_le16(&gb), 16);
2808 for (
int i = 0;
i < nb_samples;
i++)
2809 for (
int ch = 0; ch <
channels; ch++)
2841 control = bytestream2_get_byteu(&gb);
2842 shift = (control >> 4) + 2;
2844 for (
int n = 0; n < 16; n++) {
2845 int sample = bytestream2_get_byteu(&gb);
2853 for (
int n = 0; n < nb_samples; n++) {
2854 for (
int ch = 0; ch <
channels; ch++) {
2855 int v = bytestream2_get_byteu(&gb);
2861 for (
int n = 0; n < nb_samples *
channels; n++) {
2862 int v = bytestream2_get_byteu(&gb);
2867 for (
int n = nb_samples / 2; n > 0; n--) {
2869 int v = bytestream2_get_byteu(&gb);
2877 av_unreachable(
"There are cases for all codec ids using adpcm_decode_frame");
2904 c->status[0].step =
c->status[1].step = 511;
2945 #define ADPCM_DECODER_0(id_, sample_fmts_, name_, long_name_)
2946 #define ADPCM_DECODER_1(id_, sample_fmts_, name_, long_name_) \
2947 const FFCodec ff_ ## name_ ## _decoder = { \
2949 CODEC_LONG_NAME(long_name_), \
2950 .p.type = AVMEDIA_TYPE_AUDIO, \
2952 .p.capabilities = AV_CODEC_CAP_DR1, \
2953 CODEC_SAMPLEFMTS_ARRAY(sample_fmts_), \
2954 .priv_data_size = sizeof(ADPCMDecodeContext), \
2955 .init = adpcm_decode_init, \
2956 FF_CODEC_DECODE_CB(adpcm_decode_frame), \
2957 .flush = adpcm_flush, \
2959 #define ADPCM_DECODER_2(enabled, codec_id, name, sample_fmts, long_name) \
2960 ADPCM_DECODER_ ## enabled(codec_id, name, sample_fmts, long_name)
2961 #define ADPCM_DECODER_3(config, codec_id, name, sample_fmts, long_name) \
2962 ADPCM_DECODER_2(config, codec_id, name, sample_fmts, long_name)
2963 #define ADPCM_DECODER(codec, name, sample_fmts, long_name) \
2964 ADPCM_DECODER_3(CONFIG_ ## codec ## _DECODER, AV_CODEC_ID_ ## codec, \
2965 name, sample_fmts, long_name)