]> git.sesse.net Git - ffmpeg/blob - libavcodec/adpcm.c
tta decoder, tested with 44khz mono and stereo (for the latter use ffplay or change...
[ffmpeg] / 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_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
518 {
519     if(!c->step) {
520         c->predictor = 0;
521         c->step = 127;
522     }
523
524     c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
525     CLAMP_TO_SHORT(c->predictor);
526     c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
527     c->step = clip(c->step, 127, 24567);
528     return c->predictor;
529 }
530
531 static void xa_decode(short *out, const unsigned char *in,
532     ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
533 {
534     int i, j;
535     int shift,filter,f0,f1;
536     int s_1,s_2;
537     int d,s,t;
538
539     for(i=0;i<4;i++) {
540
541         shift  = 12 - (in[4+i*2] & 15);
542         filter = in[4+i*2] >> 4;
543         f0 = xa_adpcm_table[filter][0];
544         f1 = xa_adpcm_table[filter][1];
545
546         s_1 = left->sample1;
547         s_2 = left->sample2;
548
549         for(j=0;j<28;j++) {
550             d = in[16+i+j*4];
551
552             t = (signed char)(d<<4)>>4;
553             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
554             CLAMP_TO_SHORT(s);
555             *out = s;
556             out += inc;
557             s_2 = s_1;
558             s_1 = s;
559         }
560
561         if (inc==2) { /* stereo */
562             left->sample1 = s_1;
563             left->sample2 = s_2;
564             s_1 = right->sample1;
565             s_2 = right->sample2;
566             out = out + 1 - 28*2;
567         }
568
569         shift  = 12 - (in[5+i*2] & 15);
570         filter = in[5+i*2] >> 4;
571
572         f0 = xa_adpcm_table[filter][0];
573         f1 = xa_adpcm_table[filter][1];
574
575         for(j=0;j<28;j++) {
576             d = in[16+i+j*4];
577
578             t = (signed char)d >> 4;
579             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
580             CLAMP_TO_SHORT(s);
581             *out = s;
582             out += inc;
583             s_2 = s_1;
584             s_1 = s;
585         }
586
587         if (inc==2) { /* stereo */
588             right->sample1 = s_1;
589             right->sample2 = s_2;
590             out -= 1;
591         } else {
592             left->sample1 = s_1;
593             left->sample2 = s_2;
594         }
595     }
596 }
597
598
599 /* DK3 ADPCM support macro */
600 #define DK3_GET_NEXT_NIBBLE() \
601     if (decode_top_nibble_next) \
602     { \
603         nibble = (last_byte >> 4) & 0x0F; \
604         decode_top_nibble_next = 0; \
605     } \
606     else \
607     { \
608         last_byte = *src++; \
609         if (src >= buf + buf_size) break; \
610         nibble = last_byte & 0x0F; \
611         decode_top_nibble_next = 1; \
612     }
613
614 static int adpcm_decode_frame(AVCodecContext *avctx,
615                             void *data, int *data_size,
616                             uint8_t *buf, int buf_size)
617 {
618     ADPCMContext *c = avctx->priv_data;
619     ADPCMChannelStatus *cs;
620     int n, m, channel, i;
621     int block_predictor[2];
622     short *samples;
623     uint8_t *src;
624     int st; /* stereo */
625
626     /* DK3 ADPCM accounting variables */
627     unsigned char last_byte = 0;
628     unsigned char nibble;
629     int decode_top_nibble_next = 0;
630     int diff_channel;
631
632     /* EA ADPCM state variables */
633     uint32_t samples_in_chunk;
634     int32_t previous_left_sample, previous_right_sample;
635     int32_t current_left_sample, current_right_sample;
636     int32_t next_left_sample, next_right_sample;
637     int32_t coeff1l, coeff2l, coeff1r, coeff2r;
638     uint8_t shift_left, shift_right;
639     int count1, count2;
640
641     if (!buf_size)
642         return 0;
643
644     samples = data;
645     src = buf;
646
647     st = avctx->channels == 2;
648
649     switch(avctx->codec->id) {
650     case CODEC_ID_ADPCM_IMA_QT:
651         n = (buf_size - 2);/* >> 2*avctx->channels;*/
652         channel = c->channel;
653         cs = &(c->status[channel]);
654         /* (pppppp) (piiiiiii) */
655
656         /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
657         cs->predictor = (*src++) << 8;
658         cs->predictor |= (*src & 0x80);
659         cs->predictor &= 0xFF80;
660
661         /* sign extension */
662         if(cs->predictor & 0x8000)
663             cs->predictor -= 0x10000;
664
665         CLAMP_TO_SHORT(cs->predictor);
666
667         cs->step_index = (*src++) & 0x7F;
668
669         if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
670         if (cs->step_index > 88) cs->step_index = 88;
671
672         cs->step = step_table[cs->step_index];
673
674         if (st && channel)
675             samples++;
676
677         for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
678             *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
679             samples += avctx->channels;
680             *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
681             samples += avctx->channels;
682             src ++;
683         }
684
685         if(st) { /* handle stereo interlacing */
686             c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
687             if(channel == 1) { /* wait for the other packet before outputing anything */
688                 return src - buf;
689             }
690         }
691         break;
692     case CODEC_ID_ADPCM_IMA_WAV:
693         if (avctx->block_align != 0 && buf_size > avctx->block_align)
694             buf_size = avctx->block_align;
695
696         for(i=0; i<avctx->channels; i++){
697             cs = &(c->status[i]);
698             cs->predictor = *src++;
699             cs->predictor |= (*src++) << 8;
700             if(cs->predictor & 0x8000)
701                 cs->predictor -= 0x10000;
702             CLAMP_TO_SHORT(cs->predictor);
703
704         // XXX: is this correct ??: *samples++ = cs->predictor;
705
706             cs->step_index = *src++;
707             if (cs->step_index < 0) cs->step_index = 0;
708             if (cs->step_index > 88) cs->step_index = 88;
709             if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
710         }
711
712         for(m=4; src < (buf + buf_size);) {
713             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
714             if (st)
715                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
716             *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
717             if (st) {
718                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
719                 if (!--m) {
720                     m=4;
721                     src+=4;
722                 }
723             }
724             src++;
725         }
726         break;
727     case CODEC_ID_ADPCM_4XM:
728         cs = &(c->status[0]);
729         c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
730         if(st){
731             c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
732         }
733         c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
734         if(st){
735             c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
736         }
737         if (cs->step_index < 0) cs->step_index = 0;
738         if (cs->step_index > 88) cs->step_index = 88;
739
740         m= (buf_size - (src - buf))>>st;
741         for(i=0; i<m; i++) {
742             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
743             if (st)
744                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
745             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
746             if (st)
747                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
748         }
749
750         src += m<<st;
751
752         break;
753     case CODEC_ID_ADPCM_MS:
754         if (avctx->block_align != 0 && buf_size > avctx->block_align)
755             buf_size = avctx->block_align;
756         n = buf_size - 7 * avctx->channels;
757         if (n < 0)
758             return -1;
759         block_predictor[0] = clip(*src++, 0, 7);
760         block_predictor[1] = 0;
761         if (st)
762             block_predictor[1] = clip(*src++, 0, 7);
763         c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
764         src+=2;
765         if (st){
766             c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
767             src+=2;
768         }
769         c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
770         c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
771         c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
772         c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
773
774         c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
775         src+=2;
776         if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
777         if (st) src+=2;
778         c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
779         src+=2;
780         if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
781         if (st) src+=2;
782
783         *samples++ = c->status[0].sample1;
784         if (st) *samples++ = c->status[1].sample1;
785         *samples++ = c->status[0].sample2;
786         if (st) *samples++ = c->status[1].sample2;
787         for(;n>0;n--) {
788             *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
789             *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
790             src ++;
791         }
792         break;
793     case CODEC_ID_ADPCM_IMA_DK4:
794         if (avctx->block_align != 0 && buf_size > avctx->block_align)
795             buf_size = avctx->block_align;
796
797         c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
798         c->status[0].step_index = src[2];
799         src += 4;
800         *samples++ = c->status[0].predictor;
801         if (st) {
802             c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
803             c->status[1].step_index = src[2];
804             src += 4;
805             *samples++ = c->status[1].predictor;
806         }
807         while (src < buf + buf_size) {
808
809             /* take care of the top nibble (always left or mono channel) */
810             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
811                 (src[0] >> 4) & 0x0F, 3);
812
813             /* take care of the bottom nibble, which is right sample for
814              * stereo, or another mono sample */
815             if (st)
816                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
817                     src[0] & 0x0F, 3);
818             else
819                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
820                     src[0] & 0x0F, 3);
821
822             src++;
823         }
824         break;
825     case CODEC_ID_ADPCM_IMA_DK3:
826         if (avctx->block_align != 0 && buf_size > avctx->block_align)
827             buf_size = avctx->block_align;
828
829         c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
830         c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
831         c->status[0].step_index = src[14];
832         c->status[1].step_index = src[15];
833         /* sign extend the predictors */
834         src += 16;
835         diff_channel = c->status[1].predictor;
836
837         /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
838          * the buffer is consumed */
839         while (1) {
840
841             /* for this algorithm, c->status[0] is the sum channel and
842              * c->status[1] is the diff channel */
843
844             /* process the first predictor of the sum channel */
845             DK3_GET_NEXT_NIBBLE();
846             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
847
848             /* process the diff channel predictor */
849             DK3_GET_NEXT_NIBBLE();
850             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
851
852             /* process the first pair of stereo PCM samples */
853             diff_channel = (diff_channel + c->status[1].predictor) / 2;
854             *samples++ = c->status[0].predictor + c->status[1].predictor;
855             *samples++ = c->status[0].predictor - c->status[1].predictor;
856
857             /* process the second predictor of the sum channel */
858             DK3_GET_NEXT_NIBBLE();
859             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
860
861             /* process the second pair of stereo PCM samples */
862             diff_channel = (diff_channel + c->status[1].predictor) / 2;
863             *samples++ = c->status[0].predictor + c->status[1].predictor;
864             *samples++ = c->status[0].predictor - c->status[1].predictor;
865         }
866         break;
867     case CODEC_ID_ADPCM_IMA_WS:
868         /* no per-block initialization; just start decoding the data */
869         while (src < buf + buf_size) {
870
871             if (st) {
872                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
873                     (src[0] >> 4) & 0x0F, 3);
874                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
875                     src[0] & 0x0F, 3);
876             } else {
877                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
878                     (src[0] >> 4) & 0x0F, 3);
879                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
880                     src[0] & 0x0F, 3);
881             }
882
883             src++;
884         }
885         break;
886     case CODEC_ID_ADPCM_XA:
887         c->status[0].sample1 = c->status[0].sample2 =
888         c->status[1].sample1 = c->status[1].sample2 = 0;
889         while (buf_size >= 128) {
890             xa_decode(samples, src, &c->status[0], &c->status[1],
891                 avctx->channels);
892             src += 128;
893             samples += 28 * 8;
894             buf_size -= 128;
895         }
896         break;
897     case CODEC_ID_ADPCM_EA:
898         samples_in_chunk = LE_32(src);
899         if (samples_in_chunk >= ((buf_size - 12) * 2)) {
900             src += buf_size;
901             break;
902         }
903         src += 4;
904         current_left_sample = (int16_t)LE_16(src);
905         src += 2;
906         previous_left_sample = (int16_t)LE_16(src);
907         src += 2;
908         current_right_sample = (int16_t)LE_16(src);
909         src += 2;
910         previous_right_sample = (int16_t)LE_16(src);
911         src += 2;
912
913         for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
914             coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
915             coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
916             coeff1r = ea_adpcm_table[*src & 0x0F];
917             coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
918             src++;
919
920             shift_left = ((*src >> 4) & 0x0F) + 8;
921             shift_right = (*src & 0x0F) + 8;
922             src++;
923
924             for (count2 = 0; count2 < 28; count2++) {
925                 next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
926                 next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
927                 src++;
928
929                 next_left_sample = (next_left_sample +
930                     (current_left_sample * coeff1l) +
931                     (previous_left_sample * coeff2l) + 0x80) >> 8;
932                 next_right_sample = (next_right_sample +
933                     (current_right_sample * coeff1r) +
934                     (previous_right_sample * coeff2r) + 0x80) >> 8;
935                 CLAMP_TO_SHORT(next_left_sample);
936                 CLAMP_TO_SHORT(next_right_sample);
937
938                 previous_left_sample = current_left_sample;
939                 current_left_sample = next_left_sample;
940                 previous_right_sample = current_right_sample;
941                 current_right_sample = next_right_sample;
942                 *samples++ = (unsigned short)current_left_sample;
943                 *samples++ = (unsigned short)current_right_sample;
944             }
945         }
946         break;
947     case CODEC_ID_ADPCM_IMA_SMJPEG:
948         c->status[0].predictor = *src;
949         src += 2;
950         c->status[0].step_index = *src++;
951         src++;  /* skip another byte before getting to the meat */
952         while (src < buf + buf_size) {
953             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
954                 *src & 0x0F, 3);
955             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
956                 (*src >> 4) & 0x0F, 3);
957             src++;
958         }
959         break;
960     case CODEC_ID_ADPCM_CT:
961         while (src < buf + buf_size) {
962             if (st) {
963                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
964                     (src[0] >> 4) & 0x0F);
965                 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
966                     src[0] & 0x0F);
967             } else {
968                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
969                     (src[0] >> 4) & 0x0F);
970                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
971                     src[0] & 0x0F);
972             }
973             src++;
974         }
975         break;
976     case CODEC_ID_ADPCM_SWF:
977     {
978         GetBitContext gb;
979         const int *table;
980         int k0, signmask;
981         int size = buf_size*8;
982
983         init_get_bits(&gb, buf, size);
984
985         // first frame, read bits & inital values
986         if (!c->nb_bits)
987         {
988             c->nb_bits = get_bits(&gb, 2)+2;
989 //            av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
990         }
991
992         table = swf_index_tables[c->nb_bits-2];
993         k0 = 1 << (c->nb_bits-2);
994         signmask = 1 << (c->nb_bits-1);
995
996         while (get_bits_count(&gb) <= size)
997         {
998             int i;
999
1000             c->nb_samples++;
1001             // wrap around at every 4096 samples...
1002             if ((c->nb_samples & 0xfff) == 1)
1003             {
1004                 for (i = 0; i <= st; i++)
1005                 {
1006                     *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1007                     c->status[i].step_index = get_bits(&gb, 6);
1008                 }
1009             }
1010
1011             // similar to IMA adpcm
1012             for (i = 0; i <= st; i++)
1013             {
1014                 int delta = get_bits(&gb, c->nb_bits);
1015                 int step = step_table[c->status[i].step_index];
1016                 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1017                 int k = k0;
1018
1019                 do {
1020                     if (delta & k)
1021                         vpdiff += step;
1022                     step >>= 1;
1023                     k >>= 1;
1024                 } while(k);
1025                 vpdiff += step;
1026
1027                 if (delta & signmask)
1028                     c->status[i].predictor -= vpdiff;
1029                 else
1030                     c->status[i].predictor += vpdiff;
1031
1032                 c->status[i].step_index += table[delta & (~signmask)];
1033
1034                 c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
1035                 c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
1036
1037                 *samples++ = c->status[i].predictor;
1038             }
1039         }
1040
1041 //        src += get_bits_count(&gb)*8;
1042         src += size;
1043
1044         break;
1045     }
1046     case CODEC_ID_ADPCM_YAMAHA:
1047         while (src < buf + buf_size) {
1048             if (st) {
1049                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1050                         src[0] & 0x0F);
1051                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
1052                         (src[0] >> 4) & 0x0F);
1053             } else {
1054                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1055                         src[0] & 0x0F);
1056                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1057                         (src[0] >> 4) & 0x0F);
1058             }
1059             src++;
1060         }
1061         break;
1062     default:
1063         return -1;
1064     }
1065     *data_size = (uint8_t *)samples - (uint8_t *)data;
1066     return src - buf;
1067 }
1068
1069
1070
1071 #ifdef CONFIG_ENCODERS
1072 #define ADPCM_ENCODER(id,name)                  \
1073 AVCodec name ## _encoder = {                    \
1074     #name,                                      \
1075     CODEC_TYPE_AUDIO,                           \
1076     id,                                         \
1077     sizeof(ADPCMContext),                       \
1078     adpcm_encode_init,                          \
1079     adpcm_encode_frame,                         \
1080     adpcm_encode_close,                         \
1081     NULL,                                       \
1082 };
1083 #else
1084 #define ADPCM_ENCODER(id,name)
1085 #endif
1086
1087 #ifdef CONFIG_DECODERS
1088 #define ADPCM_DECODER(id,name)                  \
1089 AVCodec name ## _decoder = {                    \
1090     #name,                                      \
1091     CODEC_TYPE_AUDIO,                           \
1092     id,                                         \
1093     sizeof(ADPCMContext),                       \
1094     adpcm_decode_init,                          \
1095     NULL,                                       \
1096     NULL,                                       \
1097     adpcm_decode_frame,                         \
1098 };
1099 #else
1100 #define ADPCM_DECODER(id,name)
1101 #endif
1102
1103 #define ADPCM_CODEC(id, name)                   \
1104 ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
1105
1106 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
1107 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
1108 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
1109 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
1110 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
1111 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
1112 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
1113 ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
1114 ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
1115 ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
1116 ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
1117 ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
1118 ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
1119 ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
1120
1121 #undef ADPCM_CODEC