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;
167 int sign,
delta, diff, step;
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;
216 int diff, step;
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
251 }
252
254 {
256
259 step_index = av_clip(step_index, 0, 48);
260
261 sign = nibble & 8;
262 delta = nibble & 7;
263 diff = ((2 * delta + 1) * step) >> 3;
265 if (sign) predictor -= diff;
266 else predictor += diff;
267
268 c->
predictor = av_clip(predictor, -2048, 2047);
270
272 }
273
275 {
276 int sign,
delta, diff;
277 int new_step;
278
279 sign = nibble & 8;
280 delta = nibble & 7;
281 /* perform direct multiplication instead of series of jumps proposed by
282 * the reference ADPCM implementation since modern CPUs can do the mults
283 * quickly enough */
284 diff = ((2 * delta + 1) * c->
step) >> 3;
285 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
288 /* calculate new step and clamp it to range 511..32767 */
290 c->
step = av_clip(new_step, 511, 32767);
291
293 }
294
296 {
297 int sign,
delta, diff;
298
299 sign = nibble & (1<<(size-1));
300 delta = nibble & ((1<<(size-1))-1);
302
303 /* clamp result */
305
306 /* calculate new step */
307 if (delta >= (2*size - 3) && c->
step < 3)
309 else if (delta == 0 && c->
step > 0)
311
313 }
314
316 {
320 }
321
325 c->
step = av_clip(c->
step, 127, 24567);
327 }
328
332 {
333 int i, j;
335 int s_1,s_2;
337
338 out0 += sample_offset;
339 if (channels == 1)
340 out1 = out0 + 28;
341 else
342 out1 += sample_offset;
343
344 for(i=0;i<4;i++) {
345 shift = 12 - (in[4+i*2] & 15);
346 filter = in[4+i*2] >> 4;
349 filter=0;
350 }
353
356
357 for(j=0;j<28;j++) {
358 d = in[16+i+j*4];
359
361 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
362 s_2 = s_1;
363 s_1 = av_clip_int16(s);
364 out0[j] = s_1;
365 }
366
367 if (channels == 2) {
372 }
373
374 shift = 12 - (in[5+i*2] & 15);
375 filter = in[5+i*2] >> 4;
378 filter=0;
379 }
380
383
384 for(j=0;j<28;j++) {
385 d = in[16+i+j*4];
386
388 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
389 s_2 = s_1;
390 s_1 = av_clip_int16(s);
391 out1[j] = s_1;
392 }
393
394 if (channels == 2) {
397 } else {
400 }
401
402 out0 += 28 * (3 - channels);
403 out1 += 28 * (3 - channels);
404 }
405
406 return 0;
407 }
408
410 {
414 int k0, signmask, nb_bits,
count;
415 int size = buf_size*8;
416 int i;
417
419
420 //read bits & initial values
423 k0 = 1 << (nb_bits-2);
424 signmask = 1 << (nb_bits-1);
425
427 for (i = 0; i < avctx->
channels; i++) {
430 }
431
433 int i;
434
435 for (i = 0; i < avctx->
channels; i++) {
436 // similar to IMA adpcm
439 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
440 int k = k0;
441
442 do {
443 if (delta & k)
444 vpdiff += step;
445 step >>= 1;
446 k >>= 1;
447 } while(k);
448 vpdiff += step;
449
450 if (delta & signmask)
452 else
454
456
459
461 }
462 }
463 }
464 }
465
466 /**
467 * Get the number of samples that will be decoded from the packet.
468 * In one case, this is actually the maximum number of samples possible to
469 * decode with the given buf_size.
470 *
471 * @param[out] coded_samples set to the number of samples as coded in the
472 * packet, or 0 if the codec does not encode the
473 * number of samples in each frame.
474 * @param[out] approx_nb_samples set to non-zero if the number of samples
475 * returned is an approximation.
476 */
478 int buf_size, int *coded_samples, int *approx_nb_samples)
479 {
481 int nb_samples = 0;
483 int has_coded_samples = 0;
484 int header_size;
485
486 *coded_samples = 0;
487 *approx_nb_samples = 0;
488
489 if(ch <= 0)
490 return 0;
491
493 /* constant, only check buf_size */
495 if (buf_size < 76 * ch)
496 return 0;
497 nb_samples = 128;
498 break;
500 if (buf_size < 34 * ch)
501 return 0;
502 nb_samples = 64;
503 break;
504 /* simple 4-bit adpcm */
511 nb_samples = buf_size * 2 / ch;
512 break;
513 }
514 if (nb_samples)
515 return nb_samples;
516
517 /* simple 4-bit adpcm, with header */
518 header_size = 0;
524 }
525 if (header_size > 0)
526 return (buf_size - header_size) * 2 / ch;
527
528 /* more complex formats */
531 has_coded_samples = 1;
532 *coded_samples = bytestream2_get_le32(gb);
533 *coded_samples -= *coded_samples % 28;
534 nb_samples = (buf_size - 12) / 30 * 28;
535 break;
537 has_coded_samples = 1;
538 *coded_samples = bytestream2_get_le32(gb);
539 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
540 break;
542 nb_samples = (buf_size - ch) / ch * 2;
543 break;
547 /* maximum number of samples */
548 /* has internal offsets and a per-frame switch to signal raw 16-bit */
549 has_coded_samples = 1;
552 header_size = 4 + 9 * ch;
553 *coded_samples = bytestream2_get_le32(gb);
554 break;
556 header_size = 4 + 5 * ch;
557 *coded_samples = bytestream2_get_le32(gb);
558 break;
560 header_size = 4 + 5 * ch;
561 *coded_samples = bytestream2_get_be32(gb);
562 break;
563 }
564 *coded_samples -= *coded_samples % 28;
565 nb_samples = (buf_size - header_size) * 2 / ch;
566 nb_samples -= nb_samples % 28;
567 *approx_nb_samples = 1;
568 break;
572 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
573 break;
577 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
578 break;
582 nb_samples = (buf_size - 4 * ch) * 2 / ch;
583 break;
585 {
590 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
591 break;
592 }
596 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
597 break;
601 {
602 int samples_per_byte;
607 }
609 nb_samples++;
610 buf_size -= ch;
611 }
612 nb_samples += buf_size * samples_per_byte / ch;
613 break;
614 }
616 {
617 int buf_bits = buf_size * 8 - 2;
618 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
619 int block_hdr_size = 22 * ch;
620 int block_size = block_hdr_size + nbits * ch * 4095;
621 int nblocks = buf_bits / block_size;
622 int bits_left = buf_bits - nblocks * block_size;
623 nb_samples = nblocks * 4096;
624 if (bits_left >= block_hdr_size)
625 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
626 break;
627 }
630 nb_samples = buf_size / (8 * ch) * 14;
631 break;
632 }
633 has_coded_samples = 1;
635 *coded_samples = bytestream2_get_be32(gb);
636 *coded_samples -= *coded_samples % 14;
637 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
638 break;
640 nb_samples = buf_size / (9 * ch) * 16;
641 break;
643 nb_samples = (buf_size / 128) * 224 / ch;
644 break;
646 nb_samples = buf_size / (16 * ch) * 28;
647 break;
648 }
649
650 /* validate coded sample count */
651 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
653
654 return nb_samples;
655 }
656
658 int *got_frame_ptr,
AVPacket *avpkt)
659 {
662 int buf_size = avpkt->
size;
665 int n,
m, channel, i;
666 short *samples;
667 int16_t **samples_p;
668 int st; /* stereo */
669 int count1, count2;
670 int nb_samples, coded_samples, approx_nb_samples,
ret;
672
674 nb_samples =
get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
675 if (nb_samples <= 0) {
678 }
679
680 /* get output buffer */
684 samples = (
short *)frame->
data[0];
686
687 /* use coded_samples when applicable */
688 /* it is always <= nb_samples, so the output buffer will be large enough */
689 if (coded_samples) {
690 if (!approx_nb_samples && coded_samples != nb_samples)
692 frame->
nb_samples = nb_samples = coded_samples;
693 }
694
696
699 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
700 Channel data is interleaved per-chunk. */
701 for (channel = 0; channel < avctx->
channels; channel++) {
703 int step_index;
704 cs = &(c->
status[channel]);
705 /* (pppppp) (piiiiiii) */
706
707 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
708 predictor =
sign_extend(bytestream2_get_be16u(&gb), 16);
709 step_index = predictor & 0x7F;
710 predictor &= ~0x7F;
711
714 if (diff < 0)
715 diff = - diff;
716 if (diff > 0x7f)
718 } else {
722 }
723
728 }
729
730 samples = samples_p[channel];
731
732 for (m = 0; m < 64; m += 2) {
733 int byte = bytestream2_get_byteu(&gb);
736 }
737 }
738 break;
743
749 }
750 }
751
755
757 for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
758 for (i = 0; i < avctx->
channels; i++) {
760 samples = &samples_p[i][1 + n * samples_per_block];
761 for (m = 0; m < samples_per_block; m++) {
764 }
765 }
766 }
768 } else {
769 for (n = 0; n < (nb_samples - 1) / 8; n++) {
770 for (i = 0; i < avctx->
channels; i++) {
772 samples = &samples_p[i][1 + n * 8];
773 for (m = 0; m < 8; m += 2) {
774 int v = bytestream2_get_byteu(&gb);
777 }
778 }
779 }
780 }
781 break;
783 for (i = 0; i < avctx->
channels; i++)
785
786 for (i = 0; i < avctx->
channels; i++) {
792 }
793 }
794
795 for (i = 0; i < avctx->
channels; i++) {
796 samples = (int16_t *)frame->
data[i];
798 for (n = nb_samples >> 1; n > 0; n--) {
799 int v = bytestream2_get_byteu(&gb);
802 }
803 }
804 break;
806 {
807 int block_predictor;
808
809 block_predictor = bytestream2_get_byteu(&gb);
810 if (block_predictor > 6) {
812 block_predictor);
814 }
817 if (st) {
818 block_predictor = bytestream2_get_byteu(&gb);
819 if (block_predictor > 6) {
821 block_predictor);
823 }
826 }
828 if (st){
830 }
831
836
841 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
842 int byte = bytestream2_get_byteu(&gb);
845 }
846 break;
847 }
849 for (channel = 0; channel < avctx->
channels; channel++) {
857 }
858 }
859 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
860 int v = bytestream2_get_byteu(&gb);
863 }
864 break;
866 {
867 int last_byte = 0;
868 int nibble;
869 int decode_top_nibble_next = 0;
870 int diff_channel;
871 const int16_t *samples_end = samples + avctx->
channels * nb_samples;
872
882 }
883 /* sign extend the predictors */
885
886 /* DK3 ADPCM support macro */
887 #define DK3_GET_NEXT_NIBBLE() \
888 if (decode_top_nibble_next) { \
889 nibble = last_byte >> 4; \
890 decode_top_nibble_next = 0; \
891 } else { \
892 last_byte = bytestream2_get_byteu(&gb); \
893 nibble = last_byte & 0x0F; \
894 decode_top_nibble_next = 1; \
895 }
896
897 while (samples < samples_end) {
898
899 /* for this algorithm, c->status[0] is the sum channel and
900 * c->status[1] is the diff channel */
901
902 /* process the first predictor of the sum channel */
905
906 /* process the diff channel predictor */
909
910 /* process the first pair of stereo PCM samples */
914
915 /* process the second predictor of the sum channel */
918
919 /* process the second pair of stereo PCM samples */
923 }
924
927 break;
928 }
930 for (channel = 0; channel < avctx->
channels; channel++) {
938 }
939 }
940
941 for (n = nb_samples >> (1 - st); n > 0; n--) {
942 int v1, v2;
943 int v = bytestream2_get_byteu(&gb);
944 /* nibbles are swapped for mono */
945 if (st) {
946 v1 = v >> 4;
947 v2 = v & 0x0F;
948 } else {
949 v2 = v >> 4;
950 v1 = v & 0x0F;
951 }
954 }
955 break;
958 int v = bytestream2_get_byteu(&gb);
961 }
962 break;
965 int v = bytestream2_get_byteu(&gb);
968 }
969 break;
971 for (channel = 0; channel < avctx->
channels; channel++) {
979 }
980 }
981 for (n = 0; n < nb_samples / 2; n++) {
983
984 byte[0] = bytestream2_get_byteu(&gb);
985 if (st)
986 byte[1] = bytestream2_get_byteu(&gb);
987 for(channel = 0; channel < avctx->
channels; channel++) {
989 }
990 for(channel = 0; channel < avctx->
channels; channel++) {
992 }
993 }
994 break;
997 for (channel = 0; channel < avctx->
channels; channel++) {
998 int16_t *smp = samples_p[channel];
999
1000 for (n = nb_samples / 2; n > 0; n--) {
1001 int v = bytestream2_get_byteu(&gb);
1004 }
1005 }
1006 } else {
1007 for (n = nb_samples / 2; n > 0; n--) {
1008 for (channel = 0; channel < avctx->
channels; channel++) {
1009 int v = bytestream2_get_byteu(&gb);
1012 }
1014 }
1015 }
1017 break;
1019 {
1020 int16_t *out0 = samples_p[0];
1021 int16_t *out1 = samples_p[1];
1022 int samples_per_block = 28 * (3 - avctx->
channels) * 4;
1023 int sample_offset = 0;
1027 avctx->
channels, sample_offset)) < 0)
1028 return ret;
1030 sample_offset += samples_per_block;
1031 }
1032 break;
1033 }
1035 for (i=0; i<=st; i++) {
1041 }
1042 }
1043 for (i=0; i<=st; i++)
1045
1046 for (n = nb_samples >> (1 - st); n > 0; n--) {
1047 int byte = bytestream2_get_byteu(&gb);
1050 }
1051 break;
1053 for (n = nb_samples >> (1 - st); n > 0; n--) {
1054 int byte = bytestream2_get_byteu(&gb);
1057 }
1058 break;
1060 {
1061 int previous_left_sample, previous_right_sample;
1062 int current_left_sample, current_right_sample;
1063 int next_left_sample, next_right_sample;
1064 int coeff1l, coeff2l, coeff1r, coeff2r;
1065 int shift_left, shift_right;
1066
1067 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1068 each coding 28 stereo samples. */
1069
1072
1073 current_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1074 previous_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1075 current_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1076 previous_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
1077
1078 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1079 int byte = bytestream2_get_byteu(&gb);
1084
1085 byte = bytestream2_get_byteu(&gb);
1086 shift_left = 20 - (byte >> 4);
1087 shift_right = 20 - (byte & 0x0F);
1088
1089 for (count2 = 0; count2 < 28; count2++) {
1090 byte = bytestream2_get_byteu(&gb);
1091 next_left_sample =
sign_extend(byte >> 4, 4) << shift_left;
1092 next_right_sample =
sign_extend(byte, 4) << shift_right;
1093
1094 next_left_sample = (next_left_sample +
1095 (current_left_sample * coeff1l) +
1096 (previous_left_sample * coeff2l) + 0x80) >> 8;
1097 next_right_sample = (next_right_sample +
1098 (current_right_sample * coeff1r) +
1099 (previous_right_sample * coeff2r) + 0x80) >> 8;
1100
1101 previous_left_sample = current_left_sample;
1102 current_left_sample = av_clip_int16(next_left_sample);
1103 previous_right_sample = current_right_sample;
1104 current_right_sample = av_clip_int16(next_right_sample);
1105 *samples++ = current_left_sample;
1106 *samples++ = current_right_sample;
1107 }
1108 }
1109
1111
1112 break;
1113 }
1115 {
1117
1118 for(channel = 0; channel < avctx->
channels; channel++) {
1119 int byte = bytestream2_get_byteu(&gb);
1120 for (i=0; i<2; i++)
1122 shift[channel] = 20 - (byte & 0x0F);
1123 }
1124 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1126
1127 byte[0] = bytestream2_get_byteu(&gb);
1128 if (st) byte[1] = bytestream2_get_byteu(&gb);
1129 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1130 for(channel = 0; channel < avctx->
channels; channel++) {
1132 sample = (sample +
1134 c->
status[channel].
sample2 * coeff[channel][1] + 0x80) >> 8;
1138 }
1139 }
1140 }
1142 break;
1143 }
1147 /* channel numbering
1148 2chan: 0=fl, 1=fr
1149 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1150 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1152 int previous_sample, current_sample, next_sample;
1153 int coeff1, coeff2;
1155 unsigned int channel;
1156 uint16_t *samplesC;
1158 int offsets[6];
1159
1160 for (channel=0; channel<avctx->
channels; channel++)
1161 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1162 bytestream2_get_le32(&gb)) +
1164
1165 for (channel=0; channel<avctx->
channels; channel++) {
1167 samplesC = samples_p[channel];
1168
1170 current_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1171 previous_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1172 } else {
1175 }
1176
1177 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1178 int byte = bytestream2_get_byte(&gb);
1179 if (byte == 0xEE) { /* only seen in R2 and R3 */
1180 current_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1181 previous_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1182
1183 for (count2=0; count2<28; count2++)
1184 *samplesC++ =
sign_extend(bytestream2_get_be16(&gb), 16);
1185 } else {
1188 shift = 20 - (byte & 0x0F);
1189
1190 for (count2=0; count2<28; count2++) {
1191 if (count2 & 1)
1193 else {
1194 byte = bytestream2_get_byte(&gb);
1196 }
1197
1198 next_sample += (current_sample * coeff1) +
1199 (previous_sample * coeff2);
1200 next_sample = av_clip_int16(next_sample >> 8);
1201
1202 previous_sample = current_sample;
1203 current_sample = next_sample;
1204 *samplesC++ = current_sample;
1205 }
1206 }
1207 }
1208 if (!count) {
1209 count = count1;
1210 } else if (count != count1) {
1212 count =
FFMAX(count, count1);
1213 }
1214
1218 }
1219 }
1220
1223 break;
1224 }
1226 for (channel=0; channel<avctx->
channels; channel++) {
1228 int16_t *
s = samples_p[channel];
1229 for (n = 0; n < 4; n++, s += 32) {
1231 for (i=0; i<2; i++)
1233 s[0] = val & ~0x0F;
1234
1235 val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1236 shift[
n] = 20 - (val & 0x0F);
1237 s[1] = val & ~0x0F;
1238 }
1239
1240 for (m=2; m<32; m+=2) {
1241 s = &samples_p[channel][
m];
1242 for (n = 0; n < 4; n++, s += 32) {
1244 int byte = bytestream2_get_byteu(&gb);
1245
1247 pred = s[-1] * coeff[0][
n] + s[-2] * coeff[1][
n];
1248 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1249
1251 pred = s[0] * coeff[0][
n] + s[-1] * coeff[1][
n];
1252 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1253 }
1254 }
1255 }
1256 break;
1265 }
1266
1267 for (n = nb_samples >> (1 - st); n > 0; n--) {
1268 int v = bytestream2_get_byteu(&gb);
1269
1272 }
1273 break;
1275 for (i = 0; i < avctx->
channels; i++) {
1283 }
1284 }
1285
1286 for (n = nb_samples >> (1 - st); n > 0; n--) {
1287 int v = bytestream2_get_byteu(&gb);
1288
1291 }
1292 break;
1294 for (n = nb_samples >> (1 - st); n > 0; n--) {
1295 int v = bytestream2_get_byteu(&gb);
1298 }
1299 break;
1304 /* the first byte is a raw sample */
1305 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1306 if (st)
1307 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1309 nb_samples--;
1310 }
1312 for (n = nb_samples >> (1 - st); n > 0; n--) {
1313 int byte = bytestream2_get_byteu(&gb);
1315 byte >> 4, 4, 0);
1317 byte & 0x0F, 4, 0);
1318 }
1320 for (n = (nb_samples<<st) / 3; n > 0; n--) {
1321 int byte = bytestream2_get_byteu(&gb);
1323 byte >> 5 , 3, 0);
1325 (byte >> 2) & 0x07, 3, 0);
1327 byte & 0x03, 2, 0);
1328 }
1329 } else {
1330 for (n = nb_samples >> (2 - st); n > 0; n--) {
1331 int byte = bytestream2_get_byteu(&gb);
1333 byte >> 6 , 2, 2);
1335 (byte >> 4) & 0x03, 2, 2);
1337 (byte >> 2) & 0x03, 2, 2);
1339 byte & 0x03, 2, 2);
1340 }
1341 }
1342 break;
1346 break;
1348 for (n = nb_samples >> (1 - st); n > 0; n--) {
1349 int v = bytestream2_get_byteu(&gb);
1352 }
1353 break;
1355 {
1356 int samples_per_block;
1357 int blocks;
1358
1360 samples_per_block = avctx->
extradata[0] / 16;
1361 blocks = nb_samples / avctx->
extradata[0];
1362 } else {
1363 samples_per_block = nb_samples / 16;
1364 blocks = 1;
1365 }
1366
1367 for (m = 0; m < blocks; m++) {
1368 for (channel = 0; channel < avctx->
channels; channel++) {
1371
1372 samples = samples_p[channel] + m * 16;
1373 /* Read in every sample for this channel. */
1374 for (i = 0; i < samples_per_block; i++) {
1375 int byte = bytestream2_get_byteu(&gb);
1376 int scale = 1 << (byte >> 4);
1377 int index = byte & 0xf;
1380
1381 /* Decode 16 samples. */
1382 for (n = 0; n < 16; n++) {
1384
1385 if (n & 1) {
1387 } else {
1388 byte = bytestream2_get_byteu(&gb);
1390 }
1391
1392 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1393 ((sampledat * scale) << 11)) >> 11;
1394 *samples = av_clip_int16(sampledat);
1395 prev2 = prev1;
1396 prev1 = *samples++;
1397 }
1398 }
1399
1402 }
1403 }
1405 break;
1406 }
1408 {
1410 int ch;
1411
1417 }
1418
1420 for (i = 0; i < avctx->
channels; i++)
1421 for (n = 0; n < 16; n++)
1422 table[i][n] =
sign_extend(bytestream2_get_be16u(&tb), 16);
1423 } else {
1424 for (i = 0; i < avctx->
channels; i++)
1425 for (n = 0; n < 16; n++)
1426 table[i][n] =
sign_extend(bytestream2_get_be16u(&gb), 16);
1427
1428 /* Initialize the previous sample. */
1429 for (i = 0; i < avctx->
channels; i++) {
1432 }
1433 }
1434
1435 for (ch = 0; ch < avctx->
channels; ch++) {
1436 samples = samples_p[ch];
1437
1438 /* Read in every sample for this channel. */
1439 for (i = 0; i < nb_samples / 14; i++) {
1440 int byte = bytestream2_get_byteu(&gb);
1441 int index = (byte >> 4) & 7;
1442 unsigned int exp = byte & 0x0F;
1443 int factor1 = table[ch][index * 2];
1444 int factor2 = table[ch][index * 2 + 1];
1445
1446 /* Decode 14 samples. */
1447 for (n = 0; n < 14; n++) {
1449
1450 if (n & 1) {
1452 } else {
1453 byte = bytestream2_get_byteu(&gb);
1455 }
1456
1458 + c->
status[ch].
sample2 * factor2) >> 11) + (sampledat << exp);
1459 *samples = av_clip_int16(sampledat);
1462 }
1463 }
1464 }
1465 break;
1466 }
1468 for (channel = 0; channel < avctx->
channels; channel++) {
1469 samples = samples_p[channel];
1470
1471 /* Read in every sample for this channel. */
1472 for (i = 0; i < nb_samples / 28; i++) {
1474 if (channel)
1476 header = bytestream2_get_byteu(&gb);
1478
1479 /* Decode 28 samples. */
1480 for (n = 0; n < 28; n++) {
1482
1483 switch (header >> 4) {
1484 case 1:
1486 break;
1487 case 2:
1489 break;
1490 case 3:
1492 break;
1493 default:
1494 prev = 0;
1495 }
1496
1497 prev = av_clip((prev + 0x20) >> 6, -0x200000, 0x1fffff);
1498
1499 byte = bytestream2_get_byteu(&gb);
1500 if (!channel)
1502 else
1504
1505 sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1506 *samples++ = av_clip_int16(sampledat >> 6);
1509 }
1510 }
1511 if (!channel)
1513 }
1514 break;
1515
1516 default:
1517 return -1;
1518 }
1519
1523 }
1524
1525 *got_frame_ptr = 1;
1526
1528 }
1529
1530
1538
1539 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1540 AVCodec ff_ ## name_ ## _decoder = { \
1541 .name = #name_, \
1542 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1543 .type = AVMEDIA_TYPE_AUDIO, \
1544 .id = id_, \
1545 .priv_data_size = sizeof(ADPCMDecodeContext), \
1546 .init = adpcm_decode_init, \
1547 .decode = adpcm_decode_frame, \
1548 .capabilities = CODEC_CAP_DR1, \
1549 .sample_fmts = sample_fmts_, \
1550 }
1551
1552 /* Note: Do not forget to add new entries to the Makefile as well. */