]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/adpcm.c
cleanup IMA-ADPCM WAV decoder
[frescor/ffmpeg.git] / libavcodec / adpcm.c
1 /*
2  * ADPCM codecs
3  * Copyright (c) 2001-2003 The ffmpeg Project
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 #include "avcodec.h"
20 #include "bitstream.h"
21
22 /**
23  * @file adpcm.c
24  * ADPCM codecs.
25  * First version by Francois Revol (revol@free.fr)
26  * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
27  *   by Mike Melanson (melanson@pcisys.net)
28  * CD-ROM XA ADPCM codec by BERO
29  * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
30  *
31  * Features and limitations:
32  *
33  * Reference documents:
34  * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
35  * http://www.geocities.com/SiliconValley/8682/aud3.txt
36  * http://openquicktime.sourceforge.net/plugins.htm
37  * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
38  * http://www.cs.ucla.edu/~leec/mediabench/applications.html
39  * SoX source code http://home.sprynet.com/~cbagwell/sox.html
40  *
41  * CD-ROM XA:
42  * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
43  * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
44  * readstr http://www.geocities.co.jp/Playtown/2004/
45  */
46
47 #define BLKSIZE 1024
48
49 #define CLAMP_TO_SHORT(value) \
50 if (value > 32767) \
51     value = 32767; \
52 else if (value < -32768) \
53     value = -32768; \
54
55 /* step_table[] and index_table[] are from the ADPCM reference source */
56 /* This is the index table: */
57 static const int index_table[16] = {
58     -1, -1, -1, -1, 2, 4, 6, 8,
59     -1, -1, -1, -1, 2, 4, 6, 8,
60 };
61
62 /**
63  * This is the step table. Note that many programs use slight deviations from
64  * this table, but such deviations are negligible:
65  */
66 static const int step_table[89] = {
67     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
68     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
69     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
70     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
71     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
72     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
73     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
74     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
75     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
76 };
77
78 /* These are for MS-ADPCM */
79 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
80 static const int AdaptationTable[] = {
81         230, 230, 230, 230, 307, 409, 512, 614,
82         768, 614, 512, 409, 307, 230, 230, 230
83 };
84
85 static const int AdaptCoeff1[] = {
86         256, 512, 0, 192, 240, 460, 392
87 };
88
89 static const int AdaptCoeff2[] = {
90         0, -256, 0, 64, 0, -208, -232
91 };
92
93 /* These are for CD-ROM XA ADPCM */
94 static const int xa_adpcm_table[5][2] = {
95    {   0,   0 },
96    {  60,   0 },
97    { 115, -52 },
98    {  98, -55 },
99    { 122, -60 }
100 };
101
102 static const int ea_adpcm_table[] = {
103     0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
104     3, 4, 7, 8, 10, 11, 0, -1, -3, -4
105 };
106
107 static const int ct_adpcm_table[8] = {
108     0x00E6, 0x00E6, 0x00E6, 0x00E6,
109     0x0133, 0x0199, 0x0200, 0x0266
110 };
111
112 // padded to zero where table size is less then 16
113 static const int swf_index_tables[4][16] = {
114     /*2*/ { -1, 2 },
115     /*3*/ { -1, -1, 2, 4 },
116     /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
117     /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
118 };
119
120 static const int yamaha_indexscale[] = {
121     230, 230, 230, 230, 307, 409, 512, 614,
122     230, 230, 230, 230, 307, 409, 512, 614
123 };
124
125 static const int yamaha_difflookup[] = {
126     1, 3, 5, 7, 9, 11, 13, 15,
127     -1, -3, -5, -7, -9, -11, -13, -15
128 };
129
130 /* end of tables */
131
132 typedef struct ADPCMChannelStatus {
133     int predictor;
134     short int step_index;
135     int step;
136     /* for encoding */
137     int prev_sample;
138
139     /* MS version */
140     short sample1;
141     short sample2;
142     int coeff1;
143     int coeff2;
144     int idelta;
145 } ADPCMChannelStatus;
146
147 typedef struct ADPCMContext {
148     int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
149     ADPCMChannelStatus status[2];
150     short sample_buffer[32]; /* hold left samples while waiting for right samples */
151
152     /* SWF only */
153     int nb_bits;
154     int nb_samples;
155 } ADPCMContext;
156
157 /* XXX: implement encoding */
158
159 #ifdef CONFIG_ENCODERS
160 static int adpcm_encode_init(AVCodecContext *avctx)
161 {
162     if (avctx->channels > 2)
163         return -1; /* only stereo or mono =) */
164     switch(avctx->codec->id) {
165     case CODEC_ID_ADPCM_IMA_QT:
166         av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
167         avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
168         return -1;
169         break;
170     case CODEC_ID_ADPCM_IMA_WAV:
171         avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
172                                                              /* and we have 4 bytes per channel overhead */
173         avctx->block_align = BLKSIZE;
174         /* seems frame_size isn't taken into account... have to buffer the samples :-( */
175         break;
176     case CODEC_ID_ADPCM_MS:
177         avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
178                                                              /* and we have 7 bytes per channel overhead */
179         avctx->block_align = BLKSIZE;
180         break;
181     case CODEC_ID_ADPCM_YAMAHA:
182         avctx->frame_size = BLKSIZE * avctx->channels;
183         avctx->block_align = BLKSIZE;
184         break;
185     default:
186         return -1;
187         break;
188     }
189
190     avctx->coded_frame= avcodec_alloc_frame();
191     avctx->coded_frame->key_frame= 1;
192
193     return 0;
194 }
195
196 static int adpcm_encode_close(AVCodecContext *avctx)
197 {
198     av_freep(&avctx->coded_frame);
199
200     return 0;
201 }
202
203
204 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
205 {
206     int step_index;
207     unsigned char nibble;
208
209     int sign = 0; /* sign bit of the nibble (MSB) */
210     int delta, predicted_delta;
211
212     delta = sample - c->prev_sample;
213
214     if (delta < 0) {
215         sign = 1;
216         delta = -delta;
217     }
218
219     step_index = c->step_index;
220
221     /* nibble = 4 * delta / step_table[step_index]; */
222     nibble = (delta << 2) / step_table[step_index];
223
224     if (nibble > 7)
225         nibble = 7;
226
227     step_index += index_table[nibble];
228     if (step_index < 0)
229         step_index = 0;
230     if (step_index > 88)
231         step_index = 88;
232
233     /* what the decoder will find */
234     predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
235
236     if (sign)
237         c->prev_sample -= predicted_delta;
238     else
239         c->prev_sample += predicted_delta;
240
241     CLAMP_TO_SHORT(c->prev_sample);
242
243
244     nibble += sign << 3; /* sign * 8 */
245
246     /* save back */
247     c->step_index = step_index;
248
249     return nibble;
250 }
251
252 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
253 {
254     int predictor, nibble, bias;
255
256     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
257
258     nibble= sample - predictor;
259     if(nibble>=0) bias= c->idelta/2;
260     else          bias=-c->idelta/2;
261
262     nibble= (nibble + bias) / c->idelta;
263     nibble= clip(nibble, -8, 7)&0x0F;
264
265     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
266     CLAMP_TO_SHORT(predictor);
267
268     c->sample2 = c->sample1;
269     c->sample1 = predictor;
270
271     c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
272     if (c->idelta < 16) c->idelta = 16;
273
274     return nibble;
275 }
276
277 static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
278 {
279     int i1 = 0, j1;
280
281     if(!c->step) {
282         c->predictor = 0;
283         c->step = 127;
284     }
285     j1 = sample - c->predictor;
286
287     j1 = (j1 * 8) / c->step;
288     i1 = abs(j1) / 2;
289     if (i1 > 7)
290         i1 = 7;
291     if (j1 < 0)
292         i1 += 8;
293
294     c->predictor = c->predictor + ((c->step * yamaha_difflookup[i1]) / 8);
295     CLAMP_TO_SHORT(c->predictor);
296     c->step = (c->step * yamaha_indexscale[i1]) >> 8;
297     c->step = clip(c->step, 127, 24567);
298
299     return i1;
300 }
301
302 static int adpcm_encode_frame(AVCodecContext *avctx,
303                             unsigned char *frame, int buf_size, void *data)
304 {
305     int n, i, st;
306     short *samples;
307     unsigned char *dst;
308     ADPCMContext *c = avctx->priv_data;
309
310     dst = frame;
311     samples = (short *)data;
312     st= avctx->channels == 2;
313 /*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
314
315     switch(avctx->codec->id) {
316     case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
317         break;
318     case CODEC_ID_ADPCM_IMA_WAV:
319         n = avctx->frame_size / 8;
320             c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
321 /*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
322             *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
323             *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
324             *dst++ = (unsigned char)c->status[0].step_index;
325             *dst++ = 0; /* unknown */
326             samples++;
327             if (avctx->channels == 2) {
328                 c->status[1].prev_sample = (signed short)samples[1];
329 /*                c->status[1].step_index = 0; */
330                 *dst++ = (c->status[1].prev_sample) & 0xFF;
331                 *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
332                 *dst++ = (unsigned char)c->status[1].step_index;
333                 *dst++ = 0;
334                 samples++;
335             }
336
337             /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
338             for (; n>0; n--) {
339                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
340                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
341                 dst++;
342                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
343                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
344                 dst++;
345                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
346                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
347                 dst++;
348                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
349                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
350                 dst++;
351                 /* right channel */
352                 if (avctx->channels == 2) {
353                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
354                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
355                     dst++;
356                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
357                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
358                     dst++;
359                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
360                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
361                     dst++;
362                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
363                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
364                     dst++;
365                 }
366                 samples += 8 * avctx->channels;
367             }
368         break;
369     case CODEC_ID_ADPCM_MS:
370         for(i=0; i<avctx->channels; i++){
371             int predictor=0;
372
373             *dst++ = predictor;
374             c->status[i].coeff1 = AdaptCoeff1[predictor];
375             c->status[i].coeff2 = AdaptCoeff2[predictor];
376         }
377         for(i=0; i<avctx->channels; i++){
378             if (c->status[i].idelta < 16)
379                 c->status[i].idelta = 16;
380
381             *dst++ = c->status[i].idelta & 0xFF;
382             *dst++ = c->status[i].idelta >> 8;
383         }
384         for(i=0; i<avctx->channels; i++){
385             c->status[i].sample1= *samples++;
386
387             *dst++ = c->status[i].sample1 & 0xFF;
388             *dst++ = c->status[i].sample1 >> 8;
389         }
390         for(i=0; i<avctx->channels; i++){
391             c->status[i].sample2= *samples++;
392
393             *dst++ = c->status[i].sample2 & 0xFF;
394             *dst++ = c->status[i].sample2 >> 8;
395         }
396
397         for(i=7*avctx->channels; i<avctx->block_align; i++) {
398             int nibble;
399             nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
400             nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
401             *dst++ = nibble;
402         }
403         break;
404     case CODEC_ID_ADPCM_YAMAHA:
405         n = avctx->frame_size / 2;
406         for (; n>0; n--) {
407             for(i = 0; i < avctx->channels; i++) {
408                 int nibble;
409                 nibble  = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
410                 nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
411                 *dst++ = nibble;
412             }
413             samples += 2 * avctx->channels;
414         }
415         break;
416     default:
417         return -1;
418     }
419     return dst - frame;
420 }
421 #endif //CONFIG_ENCODERS
422
423 static int adpcm_decode_init(AVCodecContext * avctx)
424 {
425     ADPCMContext *c = avctx->priv_data;
426
427     c->channel = 0;
428     c->status[0].predictor = c->status[1].predictor = 0;
429     c->status[0].step_index = c->status[1].step_index = 0;
430     c->status[0].step = c->status[1].step = 0;
431
432     switch(avctx->codec->id) {
433     case CODEC_ID_ADPCM_CT:
434         c->status[0].step = c->status[1].step = 511;
435         break;
436     default:
437         break;
438     }
439     return 0;
440 }
441
442 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
443 {
444     int step_index;
445     int predictor;
446     int sign, delta, diff, step;
447
448     step = step_table[c->step_index];
449     step_index = c->step_index + index_table[(unsigned)nibble];
450     if (step_index < 0) step_index = 0;
451     else if (step_index > 88) step_index = 88;
452
453     sign = nibble & 8;
454     delta = nibble & 7;
455     /* perform direct multiplication instead of series of jumps proposed by
456      * the reference ADPCM implementation since modern CPUs can do the mults
457      * quickly enough */
458     diff = ((2 * delta + 1) * step) >> shift;
459     predictor = c->predictor;
460     if (sign) predictor -= diff;
461     else predictor += diff;
462
463     CLAMP_TO_SHORT(predictor);
464     c->predictor = predictor;
465     c->step_index = step_index;
466
467     return (short)predictor;
468 }
469
470 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
471 {
472     int predictor;
473
474     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
475     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
476     CLAMP_TO_SHORT(predictor);
477
478     c->sample2 = c->sample1;
479     c->sample1 = predictor;
480     c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
481     if (c->idelta < 16) c->idelta = 16;
482
483     return (short)predictor;
484 }
485
486 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
487 {
488     int predictor;
489     int sign, delta, diff;
490     int new_step;
491
492     sign = nibble & 8;
493     delta = nibble & 7;
494     /* perform direct multiplication instead of series of jumps proposed by
495      * the reference ADPCM implementation since modern CPUs can do the mults
496      * quickly enough */
497     diff = ((2 * delta + 1) * c->step) >> 3;
498     predictor = c->predictor;
499     /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
500     if(sign)
501         predictor = ((predictor * 254) >> 8) - diff;
502     else
503             predictor = ((predictor * 254) >> 8) + diff;
504     /* calculate new step and clamp it to range 511..32767 */
505     new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
506     c->step = new_step;
507     if(c->step < 511)
508         c->step = 511;
509     if(c->step > 32767)
510         c->step = 32767;
511
512     CLAMP_TO_SHORT(predictor);
513     c->predictor = predictor;
514     return (short)predictor;
515 }
516
517 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
518 {
519     int sign, delta, diff;
520
521     sign = nibble & (1<<(size-1));
522     delta = nibble & ((1<<(size-1))-1);
523     diff = delta << (7 + c->step + shift);
524
525     if (sign)
526         c->predictor -= diff;
527     else
528         c->predictor += diff;
529
530     /* clamp result */
531     if (c->predictor > 16256)
532         c->predictor = 16256;
533     else if (c->predictor < -16384)
534         c->predictor = -16384;
535
536     /* calculate new step */
537     if (delta >= (2*size - 3) && c->step < 3)
538         c->step++;
539     else if (delta == 0 && c->step > 0)
540         c->step--;
541
542     return (short) c->predictor;
543 }
544
545 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
546 {
547     if(!c->step) {
548         c->predictor = 0;
549         c->step = 127;
550     }
551
552     c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
553     CLAMP_TO_SHORT(c->predictor);
554     c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
555     c->step = clip(c->step, 127, 24567);
556     return c->predictor;
557 }
558
559 static void xa_decode(short *out, const unsigned char *in,
560     ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
561 {
562     int i, j;
563     int shift,filter,f0,f1;
564     int s_1,s_2;
565     int d,s,t;
566
567     for(i=0;i<4;i++) {
568
569         shift  = 12 - (in[4+i*2] & 15);
570         filter = in[4+i*2] >> 4;
571         f0 = xa_adpcm_table[filter][0];
572         f1 = xa_adpcm_table[filter][1];
573
574         s_1 = left->sample1;
575         s_2 = left->sample2;
576
577         for(j=0;j<28;j++) {
578             d = in[16+i+j*4];
579
580             t = (signed char)(d<<4)>>4;
581             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
582             CLAMP_TO_SHORT(s);
583             *out = s;
584             out += inc;
585             s_2 = s_1;
586             s_1 = s;
587         }
588
589         if (inc==2) { /* stereo */
590             left->sample1 = s_1;
591             left->sample2 = s_2;
592             s_1 = right->sample1;
593             s_2 = right->sample2;
594             out = out + 1 - 28*2;
595         }
596
597         shift  = 12 - (in[5+i*2] & 15);
598         filter = in[5+i*2] >> 4;
599
600         f0 = xa_adpcm_table[filter][0];
601         f1 = xa_adpcm_table[filter][1];
602
603         for(j=0;j<28;j++) {
604             d = in[16+i+j*4];
605
606             t = (signed char)d >> 4;
607             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
608             CLAMP_TO_SHORT(s);
609             *out = s;
610             out += inc;
611             s_2 = s_1;
612             s_1 = s;
613         }
614
615         if (inc==2) { /* stereo */
616             right->sample1 = s_1;
617             right->sample2 = s_2;
618             out -= 1;
619         } else {
620             left->sample1 = s_1;
621             left->sample2 = s_2;
622         }
623     }
624 }
625
626
627 /* DK3 ADPCM support macro */
628 #define DK3_GET_NEXT_NIBBLE() \
629     if (decode_top_nibble_next) \
630     { \
631         nibble = (last_byte >> 4) & 0x0F; \
632         decode_top_nibble_next = 0; \
633     } \
634     else \
635     { \
636         last_byte = *src++; \
637         if (src >= buf + buf_size) break; \
638         nibble = last_byte & 0x0F; \
639         decode_top_nibble_next = 1; \
640     }
641
642 static int adpcm_decode_frame(AVCodecContext *avctx,
643                             void *data, int *data_size,
644                             uint8_t *buf, int buf_size)
645 {
646     ADPCMContext *c = avctx->priv_data;
647     ADPCMChannelStatus *cs;
648     int n, m, channel, i;
649     int block_predictor[2];
650     short *samples;
651     uint8_t *src;
652     int st; /* stereo */
653
654     /* DK3 ADPCM accounting variables */
655     unsigned char last_byte = 0;
656     unsigned char nibble;
657     int decode_top_nibble_next = 0;
658     int diff_channel;
659
660     /* EA ADPCM state variables */
661     uint32_t samples_in_chunk;
662     int32_t previous_left_sample, previous_right_sample;
663     int32_t current_left_sample, current_right_sample;
664     int32_t next_left_sample, next_right_sample;
665     int32_t coeff1l, coeff2l, coeff1r, coeff2r;
666     uint8_t shift_left, shift_right;
667     int count1, count2;
668
669     if (!buf_size)
670         return 0;
671
672     samples = data;
673     src = buf;
674
675     st = avctx->channels == 2 ? 1 : 0;
676
677     switch(avctx->codec->id) {
678     case CODEC_ID_ADPCM_IMA_QT:
679         n = (buf_size - 2);/* >> 2*avctx->channels;*/
680         channel = c->channel;
681         cs = &(c->status[channel]);
682         /* (pppppp) (piiiiiii) */
683
684         /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
685         cs->predictor = (*src++) << 8;
686         cs->predictor |= (*src & 0x80);
687         cs->predictor &= 0xFF80;
688
689         /* sign extension */
690         if(cs->predictor & 0x8000)
691             cs->predictor -= 0x10000;
692
693         CLAMP_TO_SHORT(cs->predictor);
694
695         cs->step_index = (*src++) & 0x7F;
696
697         if (cs->step_index > 88){
698             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
699             cs->step_index = 88;
700         }
701
702         cs->step = step_table[cs->step_index];
703
704         if (st && channel)
705             samples++;
706
707         for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
708             *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
709             samples += avctx->channels;
710             *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
711             samples += avctx->channels;
712             src ++;
713         }
714
715         if(st) { /* handle stereo interlacing */
716             c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
717             if(channel == 1) { /* wait for the other packet before outputing anything */
718                 return src - buf;
719             }
720         }
721         break;
722     case CODEC_ID_ADPCM_IMA_WAV:
723         if (avctx->block_align != 0 && buf_size > avctx->block_align)
724             buf_size = avctx->block_align;
725
726 //        samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
727
728         for(i=0; i<avctx->channels; i++){
729             cs = &(c->status[i]);
730             cs->predictor = (int16_t)(src[0] + (src[1]<<8));
731             src+=2;
732
733         // XXX: is this correct ??: *samples++ = cs->predictor;
734
735             cs->step_index = *src++;
736             if (cs->step_index > 88){
737                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
738                 cs->step_index = 88;
739             }
740             if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
741         }
742
743         while(src < buf + buf_size){
744             for(m=0; m<4; m++){
745                 for(i=0; i<=st; i++)
746                     *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
747                 for(i=0; i<=st; i++)
748                     *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4  , 3);
749                 src++;
750             }
751             src += 4*st;
752         }
753         break;
754     case CODEC_ID_ADPCM_4XM:
755         cs = &(c->status[0]);
756         c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
757         if(st){
758             c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
759         }
760         c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
761         if(st){
762             c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
763         }
764         if (cs->step_index < 0) cs->step_index = 0;
765         if (cs->step_index > 88) cs->step_index = 88;
766
767         m= (buf_size - (src - buf))>>st;
768         for(i=0; i<m; i++) {
769             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
770             if (st)
771                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
772             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
773             if (st)
774                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
775         }
776
777         src += m<<st;
778
779         break;
780     case CODEC_ID_ADPCM_MS:
781         if (avctx->block_align != 0 && buf_size > avctx->block_align)
782             buf_size = avctx->block_align;
783         n = buf_size - 7 * avctx->channels;
784         if (n < 0)
785             return -1;
786         block_predictor[0] = clip(*src++, 0, 7);
787         block_predictor[1] = 0;
788         if (st)
789             block_predictor[1] = clip(*src++, 0, 7);
790         c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
791         src+=2;
792         if (st){
793             c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
794             src+=2;
795         }
796         c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
797         c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
798         c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
799         c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
800
801         c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
802         src+=2;
803         if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
804         if (st) src+=2;
805         c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
806         src+=2;
807         if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
808         if (st) src+=2;
809
810         *samples++ = c->status[0].sample1;
811         if (st) *samples++ = c->status[1].sample1;
812         *samples++ = c->status[0].sample2;
813         if (st) *samples++ = c->status[1].sample2;
814         for(;n>0;n--) {
815             *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
816             *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
817             src ++;
818         }
819         break;
820     case CODEC_ID_ADPCM_IMA_DK4:
821         if (avctx->block_align != 0 && buf_size > avctx->block_align)
822             buf_size = avctx->block_align;
823
824         c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
825         c->status[0].step_index = src[2];
826         src += 4;
827         *samples++ = c->status[0].predictor;
828         if (st) {
829             c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
830             c->status[1].step_index = src[2];
831             src += 4;
832             *samples++ = c->status[1].predictor;
833         }
834         while (src < buf + buf_size) {
835
836             /* take care of the top nibble (always left or mono channel) */
837             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
838                 (src[0] >> 4) & 0x0F, 3);
839
840             /* take care of the bottom nibble, which is right sample for
841              * stereo, or another mono sample */
842             if (st)
843                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
844                     src[0] & 0x0F, 3);
845             else
846                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
847                     src[0] & 0x0F, 3);
848
849             src++;
850         }
851         break;
852     case CODEC_ID_ADPCM_IMA_DK3:
853         if (avctx->block_align != 0 && buf_size > avctx->block_align)
854             buf_size = avctx->block_align;
855
856         c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
857         c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
858         c->status[0].step_index = src[14];
859         c->status[1].step_index = src[15];
860         /* sign extend the predictors */
861         src += 16;
862         diff_channel = c->status[1].predictor;
863
864         /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
865          * the buffer is consumed */
866         while (1) {
867
868             /* for this algorithm, c->status[0] is the sum channel and
869              * c->status[1] is the diff channel */
870
871             /* process the first predictor of the sum channel */
872             DK3_GET_NEXT_NIBBLE();
873             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
874
875             /* process the diff channel predictor */
876             DK3_GET_NEXT_NIBBLE();
877             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
878
879             /* process the first pair of stereo PCM samples */
880             diff_channel = (diff_channel + c->status[1].predictor) / 2;
881             *samples++ = c->status[0].predictor + c->status[1].predictor;
882             *samples++ = c->status[0].predictor - c->status[1].predictor;
883
884             /* process the second predictor of the sum channel */
885             DK3_GET_NEXT_NIBBLE();
886             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
887
888             /* process the second pair of stereo PCM samples */
889             diff_channel = (diff_channel + c->status[1].predictor) / 2;
890             *samples++ = c->status[0].predictor + c->status[1].predictor;
891             *samples++ = c->status[0].predictor - c->status[1].predictor;
892         }
893         break;
894     case CODEC_ID_ADPCM_IMA_WS:
895         /* no per-block initialization; just start decoding the data */
896         while (src < buf + buf_size) {
897
898             if (st) {
899                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
900                     (src[0] >> 4) & 0x0F, 3);
901                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
902                     src[0] & 0x0F, 3);
903             } else {
904                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
905                     (src[0] >> 4) & 0x0F, 3);
906                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
907                     src[0] & 0x0F, 3);
908             }
909
910             src++;
911         }
912         break;
913     case CODEC_ID_ADPCM_XA:
914         c->status[0].sample1 = c->status[0].sample2 =
915         c->status[1].sample1 = c->status[1].sample2 = 0;
916         while (buf_size >= 128) {
917             xa_decode(samples, src, &c->status[0], &c->status[1],
918                 avctx->channels);
919             src += 128;
920             samples += 28 * 8;
921             buf_size -= 128;
922         }
923         break;
924     case CODEC_ID_ADPCM_EA:
925         samples_in_chunk = LE_32(src);
926         if (samples_in_chunk >= ((buf_size - 12) * 2)) {
927             src += buf_size;
928             break;
929         }
930         src += 4;
931         current_left_sample = (int16_t)LE_16(src);
932         src += 2;
933         previous_left_sample = (int16_t)LE_16(src);
934         src += 2;
935         current_right_sample = (int16_t)LE_16(src);
936         src += 2;
937         previous_right_sample = (int16_t)LE_16(src);
938         src += 2;
939
940         for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
941             coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
942             coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
943             coeff1r = ea_adpcm_table[*src & 0x0F];
944             coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
945             src++;
946
947             shift_left = ((*src >> 4) & 0x0F) + 8;
948             shift_right = (*src & 0x0F) + 8;
949             src++;
950
951             for (count2 = 0; count2 < 28; count2++) {
952                 next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
953                 next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
954                 src++;
955
956                 next_left_sample = (next_left_sample +
957                     (current_left_sample * coeff1l) +
958                     (previous_left_sample * coeff2l) + 0x80) >> 8;
959                 next_right_sample = (next_right_sample +
960                     (current_right_sample * coeff1r) +
961                     (previous_right_sample * coeff2r) + 0x80) >> 8;
962                 CLAMP_TO_SHORT(next_left_sample);
963                 CLAMP_TO_SHORT(next_right_sample);
964
965                 previous_left_sample = current_left_sample;
966                 current_left_sample = next_left_sample;
967                 previous_right_sample = current_right_sample;
968                 current_right_sample = next_right_sample;
969                 *samples++ = (unsigned short)current_left_sample;
970                 *samples++ = (unsigned short)current_right_sample;
971             }
972         }
973         break;
974     case CODEC_ID_ADPCM_IMA_SMJPEG:
975         c->status[0].predictor = *src;
976         src += 2;
977         c->status[0].step_index = *src++;
978         src++;  /* skip another byte before getting to the meat */
979         while (src < buf + buf_size) {
980             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
981                 *src & 0x0F, 3);
982             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
983                 (*src >> 4) & 0x0F, 3);
984             src++;
985         }
986         break;
987     case CODEC_ID_ADPCM_CT:
988         while (src < buf + buf_size) {
989             if (st) {
990                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
991                     (src[0] >> 4) & 0x0F);
992                 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
993                     src[0] & 0x0F);
994             } else {
995                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
996                     (src[0] >> 4) & 0x0F);
997                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
998                     src[0] & 0x0F);
999             }
1000             src++;
1001         }
1002         break;
1003     case CODEC_ID_ADPCM_SBPRO_4:
1004     case CODEC_ID_ADPCM_SBPRO_3:
1005     case CODEC_ID_ADPCM_SBPRO_2:
1006         if (!c->status[0].step_index) {
1007             /* the first byte is a raw sample */
1008             *samples++ = 128 * (*src++ - 0x80);
1009             if (st)
1010               *samples++ = 128 * (*src++ - 0x80);
1011             c->status[0].step_index = 1;
1012         }
1013         if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1014             while (src < buf + buf_size) {
1015                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1016                     (src[0] >> 4) & 0x0F, 4, 0);
1017                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1018                     src[0] & 0x0F, 4, 0);
1019                 src++;
1020             }
1021         } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
1022             while (src < buf + buf_size) {
1023                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1024                     (src[0] >> 5) & 0x07, 3, 0);
1025                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1026                     (src[0] >> 2) & 0x07, 3, 0);
1027                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1028                     src[0] & 0x03, 2, 0);
1029                 src++;
1030             }
1031         } else {
1032             while (src < buf + buf_size) {
1033                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1034                     (src[0] >> 6) & 0x03, 2, 2);
1035                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1036                     (src[0] >> 4) & 0x03, 2, 2);
1037                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1038                     (src[0] >> 2) & 0x03, 2, 2);
1039                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1040                     src[0] & 0x03, 2, 2);
1041                 src++;
1042             }
1043         }
1044         break;
1045     case CODEC_ID_ADPCM_SWF:
1046     {
1047         GetBitContext gb;
1048         const int *table;
1049         int k0, signmask;
1050         int size = buf_size*8;
1051
1052         init_get_bits(&gb, buf, size);
1053
1054         // first frame, read bits & inital values
1055         if (!c->nb_bits)
1056         {
1057             c->nb_bits = get_bits(&gb, 2)+2;
1058 //            av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
1059         }
1060
1061         table = swf_index_tables[c->nb_bits-2];
1062         k0 = 1 << (c->nb_bits-2);
1063         signmask = 1 << (c->nb_bits-1);
1064
1065         while (get_bits_count(&gb) <= size)
1066         {
1067             int i;
1068
1069             c->nb_samples++;
1070             // wrap around at every 4096 samples...
1071             if ((c->nb_samples & 0xfff) == 1)
1072             {
1073                 for (i = 0; i <= st; i++)
1074                 {
1075                     *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1076                     c->status[i].step_index = get_bits(&gb, 6);
1077                 }
1078             }
1079
1080             // similar to IMA adpcm
1081             for (i = 0; i <= st; i++)
1082             {
1083                 int delta = get_bits(&gb, c->nb_bits);
1084                 int step = step_table[c->status[i].step_index];
1085                 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1086                 int k = k0;
1087
1088                 do {
1089                     if (delta & k)
1090                         vpdiff += step;
1091                     step >>= 1;
1092                     k >>= 1;
1093                 } while(k);
1094                 vpdiff += step;
1095
1096                 if (delta & signmask)
1097                     c->status[i].predictor -= vpdiff;
1098                 else
1099                     c->status[i].predictor += vpdiff;
1100
1101                 c->status[i].step_index += table[delta & (~signmask)];
1102
1103                 c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
1104                 c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
1105
1106                 *samples++ = c->status[i].predictor;
1107             }
1108         }
1109
1110 //        src += get_bits_count(&gb)*8;
1111         src += size;
1112
1113         break;
1114     }
1115     case CODEC_ID_ADPCM_YAMAHA:
1116         while (src < buf + buf_size) {
1117             if (st) {
1118                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1119                         src[0] & 0x0F);
1120                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
1121                         (src[0] >> 4) & 0x0F);
1122             } else {
1123                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1124                         src[0] & 0x0F);
1125                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1126                         (src[0] >> 4) & 0x0F);
1127             }
1128             src++;
1129         }
1130         break;
1131     default:
1132         return -1;
1133     }
1134     *data_size = (uint8_t *)samples - (uint8_t *)data;
1135     return src - buf;
1136 }
1137
1138
1139
1140 #ifdef CONFIG_ENCODERS
1141 #define ADPCM_ENCODER(id,name)                  \
1142 AVCodec name ## _encoder = {                    \
1143     #name,                                      \
1144     CODEC_TYPE_AUDIO,                           \
1145     id,                                         \
1146     sizeof(ADPCMContext),                       \
1147     adpcm_encode_init,                          \
1148     adpcm_encode_frame,                         \
1149     adpcm_encode_close,                         \
1150     NULL,                                       \
1151 };
1152 #else
1153 #define ADPCM_ENCODER(id,name)
1154 #endif
1155
1156 #ifdef CONFIG_DECODERS
1157 #define ADPCM_DECODER(id,name)                  \
1158 AVCodec name ## _decoder = {                    \
1159     #name,                                      \
1160     CODEC_TYPE_AUDIO,                           \
1161     id,                                         \
1162     sizeof(ADPCMContext),                       \
1163     adpcm_decode_init,                          \
1164     NULL,                                       \
1165     NULL,                                       \
1166     adpcm_decode_frame,                         \
1167 };
1168 #else
1169 #define ADPCM_DECODER(id,name)
1170 #endif
1171
1172 #define ADPCM_CODEC(id, name)                   \
1173 ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
1174
1175 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
1176 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
1177 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
1178 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
1179 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
1180 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
1181 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
1182 ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
1183 ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
1184 ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
1185 ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
1186 ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
1187 ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
1188 ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
1189 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
1190 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
1191 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
1192
1193 #undef ADPCM_CODEC