1 /*
2 * Copyright (c) 2001-2003 The ffmpeg Project
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
27
28 /**
29 * @file
30 * ADPCM decoders
31 * First version by Francois Revol (revol@free.fr)
32 * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
33 * by Mike Melanson (melanson@pcisys.net)
34 * CD-ROM XA ADPCM codec by BERO
35 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
36 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
37 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
38 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
39 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
40 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
41 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
42 *
43 * Features and limitations:
44 *
45 * Reference documents:
46 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
47 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
48 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
49 * http://openquicktime.sourceforge.net/
50 * XAnim sources (xa_codec.c) http://xanim.polter.net/
51 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
52 * SoX source code http://sox.sourceforge.net/
53 *
54 * CD-ROM XA:
55 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
56 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
57 * readstr http://www.geocities.co.jp/Playtown/2004/
58 */
59
60 /* These are for CD-ROM XA ADPCM */
62 { 0, 0 },
63 { 60, 0 },
64 { 115, -52 },
65 { 98, -55 },
66 { 122, -60 }
67 };
68
70 0, 240, 460, 392,
71 0, 0, -208, -220,
72 0, 1, 3, 4,
73 7, 8, 10, 11,
74 0, -1, -3, -4
75 };
76
77 // padded to zero where table size is less then 16
79 /*2*/ { -1, 2 },
80 /*3*/ { -1, -1, 2, 4 },
81 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
82 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
83 };
84
85 /* end of tables */
86
92
94 {
96 unsigned int min_channels = 1;
97 unsigned int max_channels = 2;
98
101 min_channels = 2;
102 break;
109 max_channels = 6;
110 break;
111 }
115 }
116
120 break;
124 return -1;
125 }
126 break;
131 }
132 break;
136 break;
137 default:
138 break;
139 }
140
153 break;
157 break;
158 default:
160 }
161
164
165 return 0;
166 }
167
169 {
170 int step_index;
173
176 step_index = av_clip(step_index, 0, 88);
177
178 sign = nibble & 8;
179 delta = nibble & 7;
180 /* perform direct multiplication instead of series of jumps proposed by
181 * the reference ADPCM implementation since modern CPUs can do the mults
182 * quickly enough */
183 diff = ((2 * delta + 1) * step) >>
shift;
185 if (sign) predictor -=
diff;
186 else predictor +=
diff;
187
190
192 }
193
195 {
196 int step_index;
199
202 step_index = av_clip(step_index, 0, 88);
203
204 diff = step >> 3;
205 if (nibble & 4) diff += step;
206 if (nibble & 2) diff += step >> 1;
207 if (nibble & 1) diff += step >> 2;
208
209 if (nibble & 8)
211 else
213
216
218 }
219
221 {
223
225 predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->
idelta;
226
228 c->
sample1 = av_clip_int16(predictor);
231
233 }
234
236 {
238
241 step_index = av_clip(step_index, 0, 48);
242
243 sign = nibble & 8;
244 delta = nibble & 7;
245 diff = ((2 * delta + 1) * step) >> 3;
247 if (sign) predictor -=
diff;
248 else predictor +=
diff;
249
250 c->
predictor = av_clip(predictor, -2048, 2047);
252
254 }
255
257 {
259 int new_step;
260
261 sign = nibble & 8;
262 delta = nibble & 7;
263 /* perform direct multiplication instead of series of jumps proposed by
264 * the reference ADPCM implementation since modern CPUs can do the mults
265 * quickly enough */
266 diff = ((2 * delta + 1) * c->
step) >> 3;
267 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
270 /* calculate new step and clamp it to range 511..32767 */
272 c->
step = av_clip(new_step, 511, 32767);
273
275 }
276
278 {
280
281 sign = nibble & (1<<(size-1));
282 delta = nibble & ((1<<(size-1))-1);
284
285 /* clamp result */
287
288 /* calculate new step */
289 if (delta >= (2*size - 3) && c->
step < 3)
291 else if (delta == 0 && c->
step > 0)
293
295 }
296
298 {
302 }
303
307 c->
step = av_clip(c->
step, 127, 24567);
309 }
310
314 {
315 int i, j;
317 int s_1,s_2;
319
320 out0 += sample_offset;
321 if (channels == 1)
322 out1 = out0 + 28;
323 else
324 out1 += sample_offset;
325
326 for(i=0;i<4;i++) {
327 shift = 12 - (in[4+i*2] & 15);
328 filter = in[4+i*2] >> 4;
331 filter=0;
332 }
335
338
339 for(j=0;j<28;j++) {
340 d = in[16+i+j*4];
341
343 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
344 s_2 = s_1;
345 s_1 = av_clip_int16(s);
346 out0[j] = s_1;
347 }
348
349 if (channels == 2) {
354 }
355
356 shift = 12 - (in[5+i*2] & 15);
357 filter = in[5+i*2] >> 4;
360 filter=0;
361 }
362
365
366 for(j=0;j<28;j++) {
367 d = in[16+i+j*4];
368
370 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
371 s_2 = s_1;
372 s_1 = av_clip_int16(s);
373 out1[j] = s_1;
374 }
375
376 if (channels == 2) {
379 } else {
382 }
383
384 out0 += 28 * (3 - channels);
385 out1 += 28 * (3 - channels);
386 }
387
388 return 0;
389 }
390
392 {
396 int k0, signmask, nb_bits, count;
397 int size = buf_size*8;
398 int i;
399
401
402 //read bits & initial values
405 k0 = 1 << (nb_bits-2);
406 signmask = 1 << (nb_bits-1);
407
409 for (i = 0; i < avctx->
channels; i++) {
412 }
413
415 int i;
416
417 for (i = 0; i < avctx->
channels; i++) {
418 // similar to IMA adpcm
421 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
422 int k = k0;
423
424 do {
425 if (delta & k)
426 vpdiff += step;
427 step >>= 1;
428 k >>= 1;
429 } while(k);
430 vpdiff += step;
431
432 if (delta & signmask)
434 else
436
438
441
443 }
444 }
445 }
446 }
447
448 /**
449 * Get the number of samples that will be decoded from the packet.
450 * In one case, this is actually the maximum number of samples possible to
451 * decode with the given buf_size.
452 *
453 * @param[out] coded_samples set to the number of samples as coded in the
454 * packet, or 0 if the codec does not encode the
455 * number of samples in each frame.
456 */
458 int buf_size, int *coded_samples)
459 {
463 int has_coded_samples = 0;
464 int header_size;
465
466 *coded_samples = 0;
467
468 if(ch <= 0)
469 return 0;
470
472 /* constant, only check buf_size */
474 if (buf_size < 76 * ch)
475 return 0;
476 nb_samples = 128;
477 break;
479 if (buf_size < 34 * ch)
480 return 0;
481 nb_samples = 64;
482 break;
483 /* simple 4-bit adpcm */
490 nb_samples = buf_size * 2 / ch;
491 break;
492 }
493 if (nb_samples)
495
496 /* simple 4-bit adpcm, with header */
497 header_size = 0;
503 }
504 if (header_size > 0)
505 return (buf_size - header_size) * 2 / ch;
506
507 /* more complex formats */
510 has_coded_samples = 1;
511 *coded_samples = bytestream2_get_le32(gb);
512 *coded_samples -= *coded_samples % 28;
513 nb_samples = (buf_size - 12) / 30 * 28;
514 break;
516 has_coded_samples = 1;
517 *coded_samples = bytestream2_get_le32(gb);
518 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
519 break;
521 nb_samples = (buf_size - ch) / ch * 2;
522 break;
526 /* maximum number of samples */
527 /* has internal offsets and a per-frame switch to signal raw 16-bit */
528 has_coded_samples = 1;
531 header_size = 4 + 9 * ch;
532 *coded_samples = bytestream2_get_le32(gb);
533 break;
535 header_size = 4 + 5 * ch;
536 *coded_samples = bytestream2_get_le32(gb);
537 break;
539 header_size = 4 + 5 * ch;
540 *coded_samples = bytestream2_get_be32(gb);
541 break;
542 }
543 *coded_samples -= *coded_samples % 28;
544 nb_samples = (buf_size - header_size) * 2 / ch;
545 nb_samples -= nb_samples % 28;
546 break;
550 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
551 break;
555 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
556 break;
560 nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
561 break;
565 nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
566 break;
570 {
571 int samples_per_byte;
576 }
578 nb_samples++;
579 buf_size -= ch;
580 }
581 nb_samples += buf_size * samples_per_byte / ch;
582 break;
583 }
585 {
586 int buf_bits = buf_size * 8 - 2;
587 int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
588 int block_hdr_size = 22 * ch;
589 int block_size = block_hdr_size + nbits * ch * 4095;
590 int nblocks = buf_bits / block_size;
591 int bits_left = buf_bits - nblocks * block_size;
592 nb_samples = nblocks * 4096;
593 if (bits_left >= block_hdr_size)
594 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
595 break;
596 }
598 has_coded_samples = 1;
600 *coded_samples = bytestream2_get_be32(gb);
601 *coded_samples -= *coded_samples % 14;
602 nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
603 break;
605 nb_samples = buf_size / (9 * ch) * 16;
606 break;
608 nb_samples = (buf_size / 128) * 224 / ch;
609 break;
610 }
611
612 /* validate coded sample count */
613 if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
615
617 }
618
620 int *got_frame_ptr,
AVPacket *avpkt)
621 {
623 int buf_size = avpkt->
size;
626 int n,
m, channel, i;
628 int16_t **samples_p;
629 int st; /* stereo */
630 int count1, count2;
633
635 nb_samples =
get_nb_samples(avctx, &gb, buf_size, &coded_samples);
636 if (nb_samples <= 0) {
639 }
640
641 /* get output buffer */
645 return ret;
646 }
649
650 /* use coded_samples when applicable */
651 /* it is always <= nb_samples, so the output buffer will be large enough */
652 if (coded_samples) {
653 if (coded_samples != nb_samples)
656 }
657
659
662 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
663 Channel data is interleaved per-chunk. */
664 for (channel = 0; channel < avctx->
channels; channel++) {
666 int step_index;
667 cs = &(c->
status[channel]);
668 /* (pppppp) (piiiiiii) */
669
670 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
671 predictor =
sign_extend(bytestream2_get_be16u(&gb), 16);
672 step_index = predictor & 0x7F;
673 predictor &= ~0x7F;
674
677 if (diff < 0)
679 if (diff > 0x7f)
680 goto update;
681 } else {
682 update:
685 }
686
691 }
692
693 samples = samples_p[channel];
694
695 for (m = 0; m < 64; m += 2) {
696 int byte = bytestream2_get_byteu(&gb);
699 }
700 }
701 break;
706
712 }
713 }
714
715 for (n = 0; n < (nb_samples - 1) / 8; n++) {
716 for (i = 0; i < avctx->
channels; i++) {
718 samples = &samples_p[i][1 + n * 8];
719 for (m = 0; m < 8; m += 2) {
720 int v = bytestream2_get_byteu(&gb);
723 }
724 }
725 }
726 break;
728 for (i = 0; i < avctx->
channels; i++)
730
731 for (i = 0; i < avctx->
channels; i++) {
737 }
738 }
739
740 for (i = 0; i < avctx->
channels; i++) {
743 for (n = nb_samples >> 1; n > 0; n--) {
744 int v = bytestream2_get_byteu(&gb);
747 }
748 }
749 break;
751 {
752 int block_predictor;
753
754 block_predictor = bytestream2_get_byteu(&gb);
755 if (block_predictor > 6) {
757 block_predictor);
759 }
762 if (st) {
763 block_predictor = bytestream2_get_byteu(&gb);
764 if (block_predictor > 6) {
766 block_predictor);
768 }
771 }
773 if (st){
775 }
776
781
786 for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
787 int byte = bytestream2_get_byteu(&gb);
790 }
791 break;
792 }
794 for (channel = 0; channel < avctx->
channels; channel++) {
802 }
803 }
804 for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
805 int v = bytestream2_get_byteu(&gb);
808 }
809 break;
811 {
812 int last_byte = 0;
813 int nibble;
814 int decode_top_nibble_next = 0;
815 int diff_channel;
817
827 }
828 /* sign extend the predictors */
830
831 /* DK3 ADPCM support macro */
832 #define DK3_GET_NEXT_NIBBLE() \
833 if (decode_top_nibble_next) { \
834 nibble = last_byte >> 4; \
835 decode_top_nibble_next = 0; \
836 } else { \
837 last_byte = bytestream2_get_byteu(&gb); \
838 nibble = last_byte & 0x0F; \
839 decode_top_nibble_next = 1; \
840 }
841
842 while (samples < samples_end) {
843
844 /* for this algorithm, c->status[0] is the sum channel and
845 * c->status[1] is the diff channel */
846
847 /* process the first predictor of the sum channel */
850
851 /* process the diff channel predictor */
854
855 /* process the first pair of stereo PCM samples */
859
860 /* process the second predictor of the sum channel */
863
864 /* process the second pair of stereo PCM samples */
868 }
869 break;
870 }
872 for (channel = 0; channel < avctx->
channels; channel++) {
880 }
881 }
882
883 for (n = nb_samples >> (1 - st); n > 0; n--) {
884 int v1, v2;
885 int v = bytestream2_get_byteu(&gb);
886 /* nibbles are swapped for mono */
887 if (st) {
888 v1 = v >> 4;
889 v2 = v & 0x0F;
890 } else {
891 v2 = v >> 4;
892 v1 = v & 0x0F;
893 }
896 }
897 break;
900 int v = bytestream2_get_byteu(&gb);
903 }
904 break;
907 int v = bytestream2_get_byteu(&gb);
910 }
911 break;
914 for (channel = 0; channel < avctx->
channels; channel++) {
915 int16_t *smp = samples_p[channel];
916
917 for (n = nb_samples / 2; n > 0; n--) {
918 int v = bytestream2_get_byteu(&gb);
921 }
922 }
923 } else {
924 for (n = nb_samples / 2; n > 0; n--) {
925 for (channel = 0; channel < avctx->
channels; channel++) {
926 int v = bytestream2_get_byteu(&gb);
929 }
931 }
932 }
934 break;
936 {
937 int16_t *out0 = samples_p[0];
938 int16_t *out1 = samples_p[1];
939 int samples_per_block = 28 * (3 - avctx->
channels) * 4;
940 int sample_offset = 0;
944 avctx->
channels, sample_offset)) < 0)
945 return ret;
947 sample_offset += samples_per_block;
948 }
949 break;
950 }
952 for (i=0; i<=st; i++) {
958 }
959 }
960 for (i=0; i<=st; i++)
962
963 for (n = nb_samples >> (1 - st); n > 0; n--) {
964 int byte = bytestream2_get_byteu(&gb);
967 }
968 break;
970 for (n = nb_samples >> (1 - st); n > 0; n--) {
971 int byte = bytestream2_get_byteu(&gb);
974 }
975 break;
977 {
978 int previous_left_sample, previous_right_sample;
979 int current_left_sample, current_right_sample;
980 int next_left_sample, next_right_sample;
981 int coeff1l, coeff2l, coeff1r, coeff2r;
982 int shift_left, shift_right;
983
984 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
985 each coding 28 stereo samples. */
986
989
990 current_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
991 previous_left_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
992 current_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
993 previous_right_sample =
sign_extend(bytestream2_get_le16u(&gb), 16);
994
995 for (count1 = 0; count1 < nb_samples / 28; count1++) {
996 int byte = bytestream2_get_byteu(&gb);
1001
1002 byte = bytestream2_get_byteu(&gb);
1003 shift_left = 20 - (byte >> 4);
1004 shift_right = 20 - (byte & 0x0F);
1005
1006 for (count2 = 0; count2 < 28; count2++) {
1007 byte = bytestream2_get_byteu(&gb);
1008 next_left_sample =
sign_extend(byte >> 4, 4) << shift_left;
1009 next_right_sample =
sign_extend(byte, 4) << shift_right;
1010
1011 next_left_sample = (next_left_sample +
1012 (current_left_sample * coeff1l) +
1013 (previous_left_sample * coeff2l) + 0x80) >> 8;
1014 next_right_sample = (next_right_sample +
1015 (current_right_sample * coeff1r) +
1016 (previous_right_sample * coeff2r) + 0x80) >> 8;
1017
1018 previous_left_sample = current_left_sample;
1019 current_left_sample = av_clip_int16(next_left_sample);
1020 previous_right_sample = current_right_sample;
1021 current_right_sample = av_clip_int16(next_right_sample);
1022 *samples++ = current_left_sample;
1023 *samples++ = current_right_sample;
1024 }
1025 }
1026
1028
1029 break;
1030 }
1032 {
1034
1035 for(channel = 0; channel < avctx->
channels; channel++) {
1036 int byte = bytestream2_get_byteu(&gb);
1037 for (i=0; i<2; i++)
1039 shift[channel] = 20 - (byte & 0x0F);
1040 }
1041 for (count1 = 0; count1 < nb_samples / 2; count1++) {
1043
1044 byte[0] = bytestream2_get_byteu(&gb);
1045 if (st) byte[1] = bytestream2_get_byteu(&gb);
1046 for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1047 for(channel = 0; channel < avctx->
channels; channel++) {
1049 sample = (sample +
1051 c->
status[channel].
sample2 * coeff[channel][1] + 0x80) >> 8;
1055 }
1056 }
1057 }
1059 break;
1060 }
1064 /* channel numbering
1065 2chan: 0=fl, 1=fr
1066 4chan: 0=fl, 1=rl, 2=fr, 3=rr
1067 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1069 int previous_sample, current_sample, next_sample;
1070 int coeff1, coeff2;
1072 unsigned int channel;
1073 uint16_t *samplesC;
1074 int count = 0;
1075 int offsets[6];
1076
1077 for (channel=0; channel<avctx->
channels; channel++)
1078 offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1079 bytestream2_get_le32(&gb)) +
1081
1082 for (channel=0; channel<avctx->
channels; channel++) {
1084 samplesC = samples_p[channel];
1085
1087 current_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1088 previous_sample =
sign_extend(bytestream2_get_le16(&gb), 16);
1089 } else {
1092 }
1093
1094 for (count1 = 0; count1 < nb_samples / 28; count1++) {
1095 int byte = bytestream2_get_byte(&gb);
1096 if (byte == 0xEE) { /* only seen in R2 and R3 */
1097 current_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1098 previous_sample =
sign_extend(bytestream2_get_be16(&gb), 16);
1099
1100 for (count2=0; count2<28; count2++)
1101 *samplesC++ =
sign_extend(bytestream2_get_be16(&gb), 16);
1102 } else {
1105 shift = 20 - (byte & 0x0F);
1106
1107 for (count2=0; count2<28; count2++) {
1108 if (count2 & 1)
1110 else {
1111 byte = bytestream2_get_byte(&gb);
1113 }
1114
1115 next_sample += (current_sample * coeff1) +
1116 (previous_sample * coeff2);
1117 next_sample = av_clip_int16(next_sample >> 8);
1118
1119 previous_sample = current_sample;
1120 current_sample = next_sample;
1121 *samplesC++ = current_sample;
1122 }
1123 }
1124 }
1125 if (!count) {
1126 count = count1;
1127 } else if (count != count1) {
1129 count =
FFMAX(count, count1);
1130 }
1131
1135 }
1136 }
1137
1140 break;
1141 }
1143 for (channel=0; channel<avctx->
channels; channel++) {
1145 int16_t *s = samples_p[channel];
1146 for (n = 0; n < 4; n++, s += 32) {
1147 int val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1148 for (i=0; i<2; i++)
1150 s[0] = val & ~0x0F;
1151
1152 val =
sign_extend(bytestream2_get_le16u(&gb), 16);
1153 shift[n] = 20 - (val & 0x0F);
1154 s[1] = val & ~0x0F;
1155 }
1156
1157 for (m=2; m<32; m+=2) {
1158 s = &samples_p[channel][
m];
1159 for (n = 0; n < 4; n++, s += 32) {
1161 int byte = bytestream2_get_byteu(&gb);
1162
1164 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1165 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1166
1168 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1169 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1170 }
1171 }
1172 }
1173 break;
1182 }
1183
1184 for (n = nb_samples >> (1 - st); n > 0; n--) {
1185 int v = bytestream2_get_byteu(&gb);
1186
1189 }
1190 break;
1192 for (i = 0; i < avctx->
channels; i++) {
1200 }
1201 }
1202
1203 for (n = nb_samples >> (1 - st); n > 0; n--) {
1204 int v = bytestream2_get_byteu(&gb);
1205
1208 }
1209 break;
1211 for (n = nb_samples >> (1 - st); n > 0; n--) {
1212 int v = bytestream2_get_byteu(&gb);
1215 }
1216 break;
1221 /* the first byte is a raw sample */
1222 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1223 if (st)
1224 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1226 nb_samples--;
1227 }
1229 for (n = nb_samples >> (1 - st); n > 0; n--) {
1230 int byte = bytestream2_get_byteu(&gb);
1232 byte >> 4, 4, 0);
1234 byte & 0x0F, 4, 0);
1235 }
1237 for (n = nb_samples / 3; n > 0; n--) {
1238 int byte = bytestream2_get_byteu(&gb);
1240 byte >> 5 , 3, 0);
1242 (byte >> 2) & 0x07, 3, 0);
1244 byte & 0x03, 2, 0);
1245 }
1246 } else {
1247 for (n = nb_samples >> (2 - st); n > 0; n--) {
1248 int byte = bytestream2_get_byteu(&gb);
1250 byte >> 6 , 2, 2);
1252 (byte >> 4) & 0x03, 2, 2);
1254 (byte >> 2) & 0x03, 2, 2);
1256 byte & 0x03, 2, 2);
1257 }
1258 }
1259 break;
1263 break;
1265 for (n = nb_samples >> (1 - st); n > 0; n--) {
1266 int v = bytestream2_get_byteu(&gb);
1269 }
1270 break;
1272 {
1273 int samples_per_block;
1274 int blocks;
1275
1277 samples_per_block = avctx->
extradata[0] / 16;
1278 blocks = nb_samples / avctx->
extradata[0];
1279 } else {
1280 samples_per_block = nb_samples / 16;
1281 blocks = 1;
1282 }
1283
1284 for (m = 0; m < blocks; m++) {
1285 for (channel = 0; channel < avctx->
channels; channel++) {
1288
1289 samples = samples_p[channel] + m * 16;
1290 /* Read in every sample for this channel. */
1291 for (i = 0; i < samples_per_block; i++) {
1292 int byte = bytestream2_get_byteu(&gb);
1293 int scale = 1 << (byte >> 4);
1294 int index = byte & 0xf;
1297
1298 /* Decode 16 samples. */
1299 for (n = 0; n < 16; n++) {
1301
1302 if (n & 1) {
1304 } else {
1305 byte = bytestream2_get_byteu(&gb);
1307 }
1308
1309 sampledat = ((prev1 * factor1 + prev2 * factor2) +
1310 ((sampledat * scale) << 11)) >> 11;
1311 *samples = av_clip_int16(sampledat);
1312 prev2 = prev1;
1313 prev1 = *samples++;
1314 }
1315 }
1316
1319 }
1320 }
1322 break;
1323 }
1325 {
1327 int ch;
1328
1329 for (i = 0; i < avctx->
channels; i++)
1330 for (n = 0; n < 16; n++)
1331 table[i][n] =
sign_extend(bytestream2_get_be16u(&gb), 16);
1332
1333 /* Initialize the previous sample. */
1334 for (i = 0; i < avctx->
channels; i++) {
1337 }
1338
1339 for (ch = 0; ch < avctx->
channels; ch++) {
1340 samples = samples_p[ch];
1341
1342 /* Read in every sample for this channel. */
1343 for (i = 0; i < nb_samples / 14; i++) {
1344 int byte = bytestream2_get_byteu(&gb);
1345 int index = (byte >> 4) & 7;
1346 unsigned int exp = byte & 0x0F;
1347 int factor1 = table[ch][index * 2];
1348 int factor2 = table[ch][index * 2 + 1];
1349
1350 /* Decode 14 samples. */
1351 for (n = 0; n < 14; n++) {
1353
1354 if (n & 1) {
1356 } else {
1357 byte = bytestream2_get_byteu(&gb);
1359 }
1360
1362 + c->
status[ch].
sample2 * factor2) >> 11) + (sampledat << exp);
1363 *samples = av_clip_int16(sampledat);
1366 }
1367 }
1368 }
1369 break;
1370 }
1371
1372 default:
1373 return -1;
1374 }
1375
1379 }
1380
1381 *got_frame_ptr = 1;
1383
1385 }
1386
1387
1395
1396 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1397 AVCodec ff_ ## name_ ## _decoder = { \
1398 .name = #name_, \
1399 .type = AVMEDIA_TYPE_AUDIO, \
1400 .id = id_, \
1401 .priv_data_size = sizeof(ADPCMDecodeContext), \
1402 .init = adpcm_decode_init, \
1403 .decode = adpcm_decode_frame, \
1404 .capabilities = CODEC_CAP_DR1, \
1405 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1406 .sample_fmts = sample_fmts_, \
1407 }
1408
1409 /* Note: Do not forget to add new entries to the Makefile as well. */