1 /*
2 * Copyright (c) 2001-2003 The FFmpeg Project
3 *
4 * first version by Francois Revol (revol@free.fr)
5 * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6 * by Mike Melanson (melanson@pcisys.net)
7 * CD-ROM XA ADPCM codec by BERO
8 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
9 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
10 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
11 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
12 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
13 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
14 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
15 *
16 * This file is part of FFmpeg.
17 *
18 * FFmpeg is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
22 *
23 * FFmpeg is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with FFmpeg; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 */
38
39 /**
40 * @file
41 * ADPCM decoders
42 * Features and limitations:
43 *
44 * Reference documents:
45 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
46 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
47 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
48 * http://openquicktime.sourceforge.net/
49 * XAnim sources (xa_codec.c) http://xanim.polter.net/
50 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
51 * SoX source code http://sox.sourceforge.net/
52 *
53 * CD-ROM XA:
54 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
55 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
56 * readstr http://www.geocities.co.jp/Playtown/2004/
57 */
58
59 /* These are for CD-ROM XA ADPCM */
61 { 0, 0 },
62 { 60, 0 },
63 { 115, -52 },
64 { 98, -55 },
65 { 122, -60 }
66 };
67
69 0, 240, 460, 392,
70 0, 0, -208, -220,
71 0, 1, 3, 4,
72 7, 8, 10, 11,
73 0, -1, -3, -4
74 };
75
76 // padded to zero where table size is less then 16
78 /*2*/ { -1, 2 },
79 /*3*/ { -1, -1, 2, 4 },
80 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
81 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
82 };
83
84 /* end of tables */
85
90
92 {
94 unsigned int min_channels = 1;
95 unsigned int max_channels = 2;
96
100 min_channels = 2;
101 break;
108 max_channels = 6;
109 break;
110 }
114 }
115
119 break;
123 break;
128 }
129 break;
133 break;
134 default:
135 break;
136 }
137
151 break;
155 break;
156 default:
158 }
159
160 return 0;
161 }
162
164 {
165 int step_index;
168
171 step_index = av_clip(step_index, 0, 88);
172
173 sign = nibble & 8;
174 delta = nibble & 7;
175 /* perform direct multiplication instead of series of jumps proposed by
176 * the reference ADPCM implementation since modern CPUs can do the mults
177 * quickly enough */
178 diff = ((2 * delta + 1) * step) >>
shift;
180 if (sign) predictor -=
diff;
181 else predictor +=
diff;
182
185
187 }
188
190 {
192
193 shift = bps - 1;
197 step_index = av_clip(step_index, 0, 88);
198
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;
205
208
210 }
211
213 {
214 int step_index;
217
220 step_index = av_clip(step_index, 0, 88);
221
222 diff = step >> 3;
223 if (nibble & 4) diff += step;
224 if (nibble & 2) diff += step >> 1;
225 if (nibble & 1) diff += step >> 2;
226
227 if (nibble & 8)
229 else
231
234
236 }
237
239 {
241
243 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->
idelta;
244
246 c->
sample1 = av_clip_int16(predictor);
249 if (c->
idelta > INT_MAX/768) {
252 }
253
255 }
256
258 {
260
263 step_index = av_clip(step_index, 0, 48);
264
265 sign = nibble & 8;
266 delta = nibble & 7;
267 diff = ((2 * delta + 1) * step) >> 3;
269 if (sign) predictor -=
diff;
270 else predictor +=
diff;
271
272 c->
predictor = av_clip_intp2(predictor, 11);
274
276 }
277
279 {
281 int new_step;
282
283 sign = nibble & 8;
284 delta = nibble & 7;
285 /* perform direct multiplication instead of series of jumps proposed by
286 * the reference ADPCM implementation since modern CPUs can do the mults
287 * quickly enough */
288 diff = ((2 * delta + 1) * c->
step) >> 3;
289 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
292 /* calculate new step and clamp it to range 511..32767 */
294 c->
step = av_clip(new_step, 511, 32767);
295
297 }
298
300 {
302
303 sign = nibble & (1<<(size-1));
304 delta = nibble & ((1<<(size-1))-1);
306
307 /* clamp result */
309
310 /* calculate new step */
311 if (delta >= (2*size - 3) && c->
step < 3)
313 else if (delta == 0 && c->
step > 0)
315
317 }
318
320 {
324 }
325
329 c->
step = av_clip(c->
step, 127, 24567);
331 }
332
336 {
337 int i, j;
339 int s_1,s_2;
341
342 out0 += sample_offset;
343 if (channels == 1)
344 out1 = out0 + 28;
345 else
346 out1 += sample_offset;
347
348 for(i=0;i<4;i++) {
349 shift = 12 - (in[4+i*2] & 15);
350 filter = in[4+i*2] >> 4;
353 filter=0;
354 }
357
360
361 for(j=0;j<28;j++) {
362 d = in[16+i+j*4];
363
365 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
366 s_2 = s_1;
367 s_1 = av_clip_int16(s);
368 out0[j] = s_1;
369 }
370
371 if (channels == 2) {
376 }
377
378 shift = 12 - (in[5+i*2] & 15);
379 filter = in[5+i*2] >> 4;
382 filter=0;
383 }
384
387
388 for(j=0;j<28;j++) {
389 d = in[16+i+j*4];
390
392 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
393 s_2 = s_1;
394 s_1 = av_clip_int16(s);
395 out1[j] = s_1;
396 }
397
398 if (channels == 2) {
401 } else {
404 }
405
406 out0 += 28 * (3 - channels);
407 out1 += 28 * (3 - channels);
408 }
409
410 return 0;
411 }
412
414 {
418 int k0, signmask, nb_bits,
count;
419 int size = buf_size*8;
420 int i;
421
423
424 //read bits & initial values
427 k0 = 1 << (nb_bits-2);
428 signmask = 1 << (nb_bits-1);
429
431 for (i = 0; i < avctx->
channels; i++) {
434 }
435
437 int i;
438
439 for (i = 0; i < avctx->
channels; i++) {
440 // similar to IMA adpcm
443 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
444 int k = k0;
445
446 do {
447 if (delta & k)
448 vpdiff += step;
449 step >>= 1;
450 k >>= 1;
451 } while(k);
452 vpdiff += step;
453
454 if (delta & signmask)
456 else
458
460
463
465 }
466 }
467 }
468 }
469
470 /**
471 * Get the number of samples that will be decoded from the packet.
472 * In one case, this is actually the maximum number of samples possible to
473 * decode with the given buf_size.
474 *
475 * @param[out] coded_samples set to the number of samples as coded in the
476 * packet, or 0 if the codec does not encode the
477 * number of samples in each frame.
478 * @param[out] approx_nb_samples set to non-zero if the number of samples
479 * returned is an approximation.
480 */
482 int buf_size, int *coded_samples, int *approx_nb_samples)
483 {
485 int nb_samples = 0;
487 int has_coded_samples = 0;
488 int header_size;
489
490 *coded_samples = 0;
491 *approx_nb_samples = 0;
492
493 if(ch <= 0)
494 return 0;
495
497 /* constant, only check buf_size */
499 if (buf_size < 76 * ch)
500 return 0;
501 nb_samples = 128;
502 break;
504 if (buf_size < 34 * ch)
505 return 0;
506 nb_samples = 64;
507 break;
508 /* simple 4-bit adpcm */
515 nb_samples = buf_size * 2 / ch;
516 break;
517 }
518 if (nb_samples)
519 return nb_samples;
520
521 /* simple 4-bit adpcm, with header */
522 header_size = 0;
528 }
529 if (header_size > 0)
530 return (buf_size - header_size) * 2 / ch;
531
532 /* more complex formats */
535 has_coded_samples = 1;
536 *coded_samples = bytestream2_get_le32(gb);
537 *coded_samples -= *coded_samples % 28;
538 nb_samples = (buf_size - 12) / 30 * 28;
539 break;
541 has_coded_samples = 1;
542 *coded_samples = bytestream2_get_le32(gb);
543 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
544 break;
546 nb_samples = (buf_size - ch) / ch * 2;
547 break;
551 /* maximum number of samples */
552 /* has internal offsets and a per-frame switch to signal raw 16-bit */
553 has_coded_samples = 1;
556 header_size = 4 + 9 * ch;
557 *coded_samples = bytestream2_get_le32(gb);
558 break;
560 header_size = 4 + 5 * ch;
561 *coded_samples = bytestream2_get_le32(gb);
562 break;
564 header_size = 4 + 5 * ch;
565 *coded_samples = bytestream2_get_be32(gb);
566 break;
567 }
568 *coded_samples -= *coded_samples % 28;
569 nb_samples = (buf_size - header_size) * 2 / ch;
570 nb_samples -= nb_samples % 28;
571 *approx_nb_samples = 1;
572 break;
576 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
577 break;
581 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
582 break;
586 nb_samples = (buf_size - 4 * ch) * 2 / ch;
587 break;
589 {
594 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
595 break;
596 }
600 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
601 break;
605 {
606 int samples_per_byte;
611 }
613 nb_samples++;
614 buf_size -= ch;
615 }
616 nb_samples += buf_size * samples_per_byte / ch;
617 break;
618 }
620 {
621 int buf_bits = buf_size * 8 - 2;
622 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
623 int block_hdr_size = 22 * ch;
624 int block_size = block_hdr_size + nbits * ch * 4095;
625 int nblocks = buf_bits / block_size;
626 int bits_left = buf_bits - nblocks * block_size;
627 nb_samples = nblocks * 4096;
628 if (bits_left >= block_hdr_size)
629 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
630 break;
631 }
634 nb_samples = buf_size / (8 * ch) * 14;
635 break;
636 }
637 has_coded_samples = 1;
639 *coded_samples = bytestream2_get_be32(gb);
640 *coded_samples -= *coded_samples % 14;
641 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
642 break;
644 nb_samples = buf_size / (9 * ch) * 16;
645 break;
647 nb_samples = (buf_size / 128) * 224 / ch;
648 break;
650 nb_samples = buf_size / (16 * ch) * 28;
651 break;
652 }
653
654 /* validate coded sample count */
655 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
657
658 return nb_samples;
659 }
660
662 int *got_frame_ptr,
AVPacket *avpkt)
663 {
666 int buf_size = avpkt->
size;
669 int n,
m, channel, i;
670 short *samples;
671 int16_t **samples_p;
672 int st; /* stereo */
673 int count1, count2;
674 int nb_samples, coded_samples, approx_nb_samples,
ret;
676
678 nb_samples =
get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
679 if (nb_samples <= 0) {
682 }
683
684 /* get output buffer */
688 samples = (
short *)frame->
data[0];
690
691 /* use coded_samples when applicable */
692 /* it is always <= nb_samples, so the output buffer will be large enough */
693 if (coded_samples) {
694 if (!approx_nb_samples && coded_samples != nb_samples)
696 frame->
nb_samples = nb_samples = coded_samples;
697 }
698
700
703 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
704 Channel data is interleaved per-chunk. */
705 for (channel = 0; channel < avctx->
channels; channel++) {
707 int step_index;
708 cs = &(c->
status[channel]);
709 /* (pppppp) (piiiiiii) */
710
711 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
712 predictor =
sign_extend(bytestream2_get_be16u(&gb), 16);
713 step_index = predictor & 0x7F;
714 predictor &= ~0x7F;
715
718 if (diff < 0)
720 if (diff > 0x7f)
722 } else {
726 }
727
732 }
733
734 samples = samples_p[channel];
735
736 for (m = 0; m < 64; m += 2) {
737 int byte = bytestream2_get_byteu(&gb);
740 }
741 }
742 break;
747
753 }
754 }
755
759
761 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
762 for (i = 0; i < avctx->
channels; i++) {
764 samples = &samples_p[i][1 + n * samples_per_block];
765 for (m = 0; m < samples_per_block; m++) {
768 }
769 }
770 }
772 } else {
773 for (n = 0; n < (nb_samples - 1) / 8; n++) {
774 for (i = 0; i < avctx->
channels; i++) {
776 samples = &samples_p[i][1 + n * 8];
777 for (m = 0; m < 8; m += 2) {
778 int v = bytestream2_get_byteu(&gb);
781 }
782 }
783 }
784 }
785 break;
787 for (i = 0; i < avctx->
channels; i++)
789
790 for (i = 0; i < avctx->
channels; i++) {
796 }
797 }
798
799 for (i = 0; i < avctx->
channels; i++) {
800 samples = (int16_t *)frame->
data[i];
802 for (n = nb_samples >> 1; n > 0; n--) {
803 int v = bytestream2_get_byteu(&gb);
806 }
807 }
808 break;
810 {
811 int block_predictor;
812
813 block_predictor = bytestream2_get_byteu(&gb);
814 if (block_predictor > 6) {
816 block_predictor);
818 }
821 if (st) {
822 block_predictor = bytestream2_get_byteu(&gb);
823 if (block_predictor > 6) {
825 block_predictor);
827 }
830 }
832 if (st){
834 }
835
840
845 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
846 int byte = bytestream2_get_byteu(&gb);
849 }
850 break;
851 }
853 for (channel = 0; channel < avctx->
channels; channel++) {
861 }
862 }
863 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
864 int v = bytestream2_get_byteu(&gb);
867 }
868 break;
870 {
871 int last_byte = 0;
872 int nibble;
873 int decode_top_nibble_next = 0;
874 int diff_channel;
875 const int16_t *samples_end = samples + avctx->
channels * nb_samples;
876
886 }
887 /* sign extend the predictors */
889
890 /* DK3 ADPCM support macro */
891 #define DK3_GET_NEXT_NIBBLE() \
892 if (decode_top_nibble_next) { \
893 nibble = last_byte >> 4; \
894 decode_top_nibble_next = 0; \
895 } else { \
896 last_byte = bytestream2_get_byteu(&gb); \
897 nibble = last_byte & 0x0F; \
898 decode_top_nibble_next = 1; \
899 }
900
901 while (samples < samples_end) {
902
903 /* for this algorithm, c->status[0] is the sum channel and
904 * c->status[1] is the diff channel */
905
906 /* process the first predictor of the sum channel */
909
910 /* process the diff channel predictor */
913
914 /* process the first pair of stereo PCM samples */
918
919 /* process the second predictor of the sum channel */
922
923 /* process the second pair of stereo PCM samples */
927 }
928
931 break;
932 }
934 for (channel = 0; channel < avctx->
channels; channel++) {
942 }
943 }
944
945 for (n = nb_samples >> (1 - st); n > 0; n--) {
946 int v1, v2;
947 int v = bytestream2_get_byteu(&gb);
948 /* nibbles are swapped for mono */
949 if (st) {
950 v1 = v >> 4;
951 v2 = v & 0x0F;
952 } else {
953 v2 = v >> 4;
954 v1 = v & 0x0F;
955 }
958 }
959 break;
962 int v = bytestream2_get_byteu(&gb);
965 }
966 break;
969 int v = bytestream2_get_byteu(&gb);
972 }
973 break;
975 for (channel = 0; channel < avctx->
channels; channel++) {
983 }
984 }
985 for (n = 0; n < nb_samples / 2; n++) {
987
988 byte[0] = bytestream2_get_byteu(&gb);
989 if (st)
990 byte[1] = bytestream2_get_byteu(&gb);
991 for(channel = 0; channel < avctx->
channels; channel++) {
993 }
994 for(channel = 0; channel < avctx->
channels; channel++) {
996 }
997 }
998 break;
1001 for (channel = 0; channel < avctx->
channels; channel++) {
1002 int16_t *smp = samples_p[channel];
1003
1004 for (n = nb_samples / 2; n > 0; n--) {
1005 int v = bytestream2_get_byteu(&gb);
1008 }
1009 }
1010 } else {
1011 for (n = nb_samples / 2; n > 0; n--) {
1012 for (channel = 0; channel < avctx->
channels; channel++) {
1013 int v = bytestream2_get_byteu(&gb);
1016 }
1018 }
1019 }
1021 break;
1023 {
1024 int16_t *out0 = samples_p[0];
1025 int16_t *out1 = samples_p[1];
1026 int samples_per_block = 28 * (3 - avctx->
channels) * 4;
1027 int sample_offset = 0;
1031 avctx->
channels, sample_offset)) < 0)
1032 return ret;
1034 sample_offset += samples_per_block;
1035 }
1036 break;
1037 }
1039 for (i=0; i<=st; i++) {
1045 }
1046 }
1047 for (i=0; i<=st; i++)
1049
1050 for (n = nb_samples >> (1 - st); n > 0; n--) {
1051 int byte = bytestream2_get_byteu(&gb);
1054 }
1055 break;
1057 for (n = nb_samples >> (1 - st); n > 0; n--) {
1058 int byte = bytestream2_get_byteu(&gb);
1061 }
1062 break;
1064 {
1065 int previous_left_sample, previous_right_sample;
1066 int current_left_sample, current_right_sample;
1067 int next_left_sample, next_right_sample;
1068 int coeff1l, coeff2l, coeff1r, coeff2r;
1069 int shift_left, shift_right;
1070
1071 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1072 each coding 28 stereo samples. */
1073
1076
1077 current_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1078 previous_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1079 current_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1080 previous_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1081
1082 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1083 int byte = bytestream2_get_byteu(&gb);
1088
1089 byte = bytestream2_get_byteu(&gb);
1090 shift_left = 20 - (byte >> 4);
1091 shift_right = 20 - (byte & 0x0F);
1092
1093 for (count2 = 0; count2 < 28; count2++) {
1094 byte = bytestream2_get_byteu(&gb);
1095 next_left_sample =
sign_extend(byte >> 4, 4) << shift_left;
1096 next_right_sample =
sign_extend(byte, 4) << shift_right;
1097
1098 next_left_sample = (next_left_sample +
1099 (current_left_sample * coeff1l) +
1100 (previous_left_sample * coeff2l) + 0x80) >> 8;
1101 next_right_sample = (next_right_sample +
1102 (current_right_sample * coeff1r) +
1103 (previous_right_sample * coeff2r) + 0x80) >> 8;
1104
1105 previous_left_sample = current_left_sample;
1106 current_left_sample = av_clip_int16(next_left_sample);
1107 previous_right_sample = current_right_sample;
1108 current_right_sample = av_clip_int16(next_right_sample);
1109 *samples++ = current_left_sample;
1110 *samples++ = current_right_sample;
1111 }
1112 }
1113
1115
1116 break;
1117 }
1119 {
1121
1122 for(channel = 0; channel < avctx->
channels; channel++) {
1123 int byte = bytestream2_get_byteu(&gb);
1124 for (i=0; i<2; i++)
1126 shift[channel] = 20 - (byte & 0x0F);
1127 }
1128 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1130
1131 byte[0] = bytestream2_get_byteu(&gb);
1132 if (st) byte[1] = bytestream2_get_byteu(&gb);
1133 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1134 for(channel = 0; channel < avctx->
channels; channel++) {
1136 sample = (sample +
1138 c->
status[channel].
sample2 * coeff[channel][1] + 0x80) >> 8;
1142 }
1143 }
1144 }
1146 break;
1147 }
1151 /* channel numbering
1152 2chan: 0=fl, 1=fr
1153 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1154 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1156 int previous_sample, current_sample, next_sample;
1157 int coeff1, coeff2;
1159 unsigned int channel;
1160 uint16_t *samplesC;
1162 int offsets[6];
1163
1164 for (channel=0; channel<avctx->
channels; channel++)
1165 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1166 bytestream2_get_le32(&gb)) +
1168
1169 for (channel=0; channel<avctx->
channels; channel++) {
1171 samplesC = samples_p[channel];
1172
1174 current_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1175 previous_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1176 } else {
1179 }
1180
1181 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1182 int byte = bytestream2_get_byte(&gb);
1183 if (byte == 0xEE) { /* only seen in R2 and R3 */
1184 current_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1185 previous_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1186
1187 for (count2=0; count2<28; count2++)
1188 *samplesC++ =
sign_extend(bytestream2_get_be16(&gb), 16);
1189 } else {
1192 shift = 20 - (byte & 0x0F);
1193
1194 for (count2=0; count2<28; count2++) {
1195 if (count2 & 1)
1197 else {
1198 byte = bytestream2_get_byte(&gb);
1200 }
1201
1202 next_sample += (current_sample * coeff1) +
1203 (previous_sample * coeff2);
1204 next_sample = av_clip_int16(next_sample >> 8);
1205
1206 previous_sample = current_sample;
1207 current_sample = next_sample;
1208 *samplesC++ = current_sample;
1209 }
1210 }
1211 }
1212 if (!count) {
1213 count = count1;
1214 } else if (count != count1) {
1216 count =
FFMAX(count, count1);
1217 }
1218
1222 }
1223 }
1224
1227 break;
1228 }
1230 for (channel=0; channel<avctx->
channels; channel++) {
1232 int16_t *
s = samples_p[channel];
1233 for (n = 0; n < 4; n++, s += 32) {
1235 for (i=0; i<2; i++)
1237 s[0] = val & ~0x0F;
1238
1239 val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1240 shift[
n] = 20 - (val & 0x0F);
1241 s[1] = val & ~0x0F;
1242 }
1243
1244 for (m=2; m<32; m+=2) {
1245 s = &samples_p[channel][
m];
1246 for (n = 0; n < 4; n++, s += 32) {
1248 int byte = bytestream2_get_byteu(&gb);
1249
1251 pred = s[-1] * coeff[0][
n] + s[-2] * coeff[1][
n];
1252 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1253
1255 pred = s[0] * coeff[0][
n] + s[-1] * coeff[1][
n];
1256 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1257 }
1258 }
1259 }
1260 break;
1269 }
1270
1271 for (n = nb_samples >> (1 - st); n > 0; n--) {
1272 int v = bytestream2_get_byteu(&gb);
1273
1276 }
1277 break;
1279 for (i = 0; i < avctx->
channels; i++) {
1287 }
1288 }
1289
1290 for (n = nb_samples >> (1 - st); n > 0; n--) {
1291 int v = bytestream2_get_byteu(&gb);
1292
1295 }
1296 break;
1298 for (n = nb_samples >> (1 - st); n > 0; n--) {
1299 int v = bytestream2_get_byteu(&gb);
1302 }
1303 break;
1308 /* the first byte is a raw sample */
1309 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1310 if (st)
1311 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1313 nb_samples--;
1314 }
1316 for (n = nb_samples >> (1 - st); n > 0; n--) {
1317 int byte = bytestream2_get_byteu(&gb);
1319 byte >> 4, 4, 0);
1321 byte & 0x0F, 4, 0);
1322 }
1324 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1325 int byte = bytestream2_get_byteu(&gb);
1327 byte >> 5 , 3, 0);
1329 (byte >> 2) & 0x07, 3, 0);
1331 byte & 0x03, 2, 0);
1332 }
1333 } else {
1334 for (n = nb_samples >> (2 - st); n > 0; n--) {
1335 int byte = bytestream2_get_byteu(&gb);
1337 byte >> 6 , 2, 2);
1339 (byte >> 4) & 0x03, 2, 2);
1341 (byte >> 2) & 0x03, 2, 2);
1343 byte & 0x03, 2, 2);
1344 }
1345 }
1346 break;
1350 break;
1352 for (n = nb_samples >> (1 - st); n > 0; n--) {
1353 int v = bytestream2_get_byteu(&gb);
1356 }
1357 break;
1359 {
1360 int samples_per_block;
1361 int blocks;
1362
1364 samples_per_block = avctx->
extradata[0] / 16;
1365 blocks = nb_samples / avctx->
extradata[0];
1366 } else {
1367 samples_per_block = nb_samples / 16;
1368 blocks = 1;
1369 }
1370
1371 for (m = 0; m < blocks; m++) {
1372 for (channel = 0; channel < avctx->
channels; channel++) {
1375
1376 samples = samples_p[channel] + m * 16;
1377 /* Read in every sample for this channel. */
1378 for (i = 0; i < samples_per_block; i++) {
1379 int byte = bytestream2_get_byteu(&gb);
1380 int scale = 1 << (byte >> 4);
1381 int index = byte & 0xf;
1384
1385 /* Decode 16 samples. */
1386 for (n = 0; n < 16; n++) {
1388
1389 if (n & 1) {
1391 } else {
1392 byte = bytestream2_get_byteu(&gb);
1394 }
1395
1396 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1397 ((sampledat * scale) << 11)) >> 11;
1398 *samples = av_clip_int16(sampledat);
1399 prev2 = prev1;
1400 prev1 = *samples++;
1401 }
1402 }
1403
1406 }
1407 }
1409 break;
1410 }
1412 {
1414 int ch;
1415
1421 }
1422
1424 for (i = 0; i < avctx->
channels; i++)
1425 for (n = 0; n < 16; n++)
1426 table[i][n] =
sign_extend(bytestream2_get_be16u(&tb), 16);
1427 } else {
1428 for (i = 0; i < avctx->
channels; i++)
1429 for (n = 0; n < 16; n++)
1430 table[i][n] =
sign_extend(bytestream2_get_be16u(&gb), 16);
1431
1432 /* Initialize the previous sample. */
1433 for (i = 0; i < avctx->
channels; i++) {
1436 }
1437 }
1438
1439 for (ch = 0; ch < avctx->
channels; ch++) {
1440 samples = samples_p[ch];
1441
1442 /* Read in every sample for this channel. */
1443 for (i = 0; i < nb_samples / 14; i++) {
1444 int byte = bytestream2_get_byteu(&gb);
1445 int index = (byte >> 4) & 7;
1446 unsigned int exp = byte & 0x0F;
1447 int factor1 = table[ch][index * 2];
1448 int factor2 = table[ch][index * 2 + 1];
1449
1450 /* Decode 14 samples. */
1451 for (n = 0; n < 14; n++) {
1453
1454 if (n & 1) {
1456 } else {
1457 byte = bytestream2_get_byteu(&gb);
1459 }
1460
1462 + c->
status[ch].
sample2 * factor2) >> 11) + (sampledat << exp);
1463 *samples = av_clip_int16(sampledat);
1466 }
1467 }
1468 }
1469 break;
1470 }
1472 for (channel = 0; channel < avctx->
channels; channel++) {
1473 samples = samples_p[channel];
1474
1475 /* Read in every sample for this channel. */
1476 for (i = 0; i < nb_samples / 28; i++) {
1478 if (channel)
1480 header = bytestream2_get_byteu(&gb);
1482
1483 /* Decode 28 samples. */
1484 for (n = 0; n < 28; n++) {
1486
1487 switch (header >> 4) {
1488 case 1:
1490 break;
1491 case 2:
1493 break;
1494 case 3:
1496 break;
1497 default:
1498 prev = 0;
1499 }
1500
1501 prev = av_clip((prev + 0x20) >> 6, -0x200000, 0x1fffff);
1502
1503 byte = bytestream2_get_byteu(&gb);
1504 if (!channel)
1506 else
1508
1509 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1510 *samples++ = av_clip_int16(sampledat >> 6);
1513 }
1514 }
1515 if (!channel)
1517 }
1518 break;
1519
1520 default:
1521 return -1;
1522 }
1523
1527 }
1528
1529 *got_frame_ptr = 1;
1530
1532 }
1533
1534
1542
1543 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1544 AVCodec ff_ ## name_ ## _decoder = { \
1545 .name = #name_, \
1546 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1547 .type = AVMEDIA_TYPE_AUDIO, \
1548 .id = id_, \
1549 .priv_data_size = sizeof(ADPCMDecodeContext), \
1550 .init = adpcm_decode_init, \
1551 .decode = adpcm_decode_frame, \
1552 .capabilities = CODEC_CAP_DR1, \
1553 .sample_fmts = sample_fmts_, \
1554 }
1555
1556 /* Note: Do not forget to add new entries to the Makefile as well. */