]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/adpcm.c
adpcm_ima & adpcm_yamaha: improved quantization
[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 delta = sample - c->prev_sample;
207     int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
208     c->prev_sample = c->prev_sample + ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
209     CLAMP_TO_SHORT(c->prev_sample);
210     c->step_index = clip(c->step_index + index_table[nibble], 0, 88);
211     return nibble;
212 }
213
214 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
215 {
216     int predictor, nibble, bias;
217
218     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
219
220     nibble= sample - predictor;
221     if(nibble>=0) bias= c->idelta/2;
222     else          bias=-c->idelta/2;
223
224     nibble= (nibble + bias) / c->idelta;
225     nibble= clip(nibble, -8, 7)&0x0F;
226
227     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
228     CLAMP_TO_SHORT(predictor);
229
230     c->sample2 = c->sample1;
231     c->sample1 = predictor;
232
233     c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
234     if (c->idelta < 16) c->idelta = 16;
235
236     return nibble;
237 }
238
239 static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
240 {
241     int nibble, delta;
242
243     if(!c->step) {
244         c->predictor = 0;
245         c->step = 127;
246     }
247
248     delta = sample - c->predictor;
249
250     nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
251
252     c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8);
253     CLAMP_TO_SHORT(c->predictor);
254     c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
255     c->step = clip(c->step, 127, 24567);
256
257     return nibble;
258 }
259
260 static int adpcm_encode_frame(AVCodecContext *avctx,
261                             unsigned char *frame, int buf_size, void *data)
262 {
263     int n, i, st;
264     short *samples;
265     unsigned char *dst;
266     ADPCMContext *c = avctx->priv_data;
267
268     dst = frame;
269     samples = (short *)data;
270     st= avctx->channels == 2;
271 /*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
272
273     switch(avctx->codec->id) {
274     case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
275         break;
276     case CODEC_ID_ADPCM_IMA_WAV:
277         n = avctx->frame_size / 8;
278             c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
279 /*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
280             *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
281             *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
282             *dst++ = (unsigned char)c->status[0].step_index;
283             *dst++ = 0; /* unknown */
284             samples++;
285             if (avctx->channels == 2) {
286                 c->status[1].prev_sample = (signed short)samples[1];
287 /*                c->status[1].step_index = 0; */
288                 *dst++ = (c->status[1].prev_sample) & 0xFF;
289                 *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
290                 *dst++ = (unsigned char)c->status[1].step_index;
291                 *dst++ = 0;
292                 samples++;
293             }
294
295             /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
296             for (; n>0; n--) {
297                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
298                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
299                 dst++;
300                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
301                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
302                 dst++;
303                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
304                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
305                 dst++;
306                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
307                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
308                 dst++;
309                 /* right channel */
310                 if (avctx->channels == 2) {
311                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
312                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
313                     dst++;
314                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
315                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
316                     dst++;
317                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
318                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
319                     dst++;
320                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
321                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
322                     dst++;
323                 }
324                 samples += 8 * avctx->channels;
325             }
326         break;
327     case CODEC_ID_ADPCM_MS:
328         for(i=0; i<avctx->channels; i++){
329             int predictor=0;
330
331             *dst++ = predictor;
332             c->status[i].coeff1 = AdaptCoeff1[predictor];
333             c->status[i].coeff2 = AdaptCoeff2[predictor];
334         }
335         for(i=0; i<avctx->channels; i++){
336             if (c->status[i].idelta < 16)
337                 c->status[i].idelta = 16;
338
339             *dst++ = c->status[i].idelta & 0xFF;
340             *dst++ = c->status[i].idelta >> 8;
341         }
342         for(i=0; i<avctx->channels; i++){
343             c->status[i].sample1= *samples++;
344
345             *dst++ = c->status[i].sample1 & 0xFF;
346             *dst++ = c->status[i].sample1 >> 8;
347         }
348         for(i=0; i<avctx->channels; i++){
349             c->status[i].sample2= *samples++;
350
351             *dst++ = c->status[i].sample2 & 0xFF;
352             *dst++ = c->status[i].sample2 >> 8;
353         }
354
355         for(i=7*avctx->channels; i<avctx->block_align; i++) {
356             int nibble;
357             nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
358             nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
359             *dst++ = nibble;
360         }
361         break;
362     case CODEC_ID_ADPCM_YAMAHA:
363         n = avctx->frame_size / 2;
364         for (; n>0; n--) {
365             for(i = 0; i < avctx->channels; i++) {
366                 int nibble;
367                 nibble  = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
368                 nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
369                 *dst++ = nibble;
370             }
371             samples += 2 * avctx->channels;
372         }
373         break;
374     default:
375         return -1;
376     }
377     return dst - frame;
378 }
379 #endif //CONFIG_ENCODERS
380
381 static int adpcm_decode_init(AVCodecContext * avctx)
382 {
383     ADPCMContext *c = avctx->priv_data;
384
385     c->channel = 0;
386     c->status[0].predictor = c->status[1].predictor = 0;
387     c->status[0].step_index = c->status[1].step_index = 0;
388     c->status[0].step = c->status[1].step = 0;
389
390     switch(avctx->codec->id) {
391     case CODEC_ID_ADPCM_CT:
392         c->status[0].step = c->status[1].step = 511;
393         break;
394     default:
395         break;
396     }
397     return 0;
398 }
399
400 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
401 {
402     int step_index;
403     int predictor;
404     int sign, delta, diff, step;
405
406     step = step_table[c->step_index];
407     step_index = c->step_index + index_table[(unsigned)nibble];
408     if (step_index < 0) step_index = 0;
409     else if (step_index > 88) step_index = 88;
410
411     sign = nibble & 8;
412     delta = nibble & 7;
413     /* perform direct multiplication instead of series of jumps proposed by
414      * the reference ADPCM implementation since modern CPUs can do the mults
415      * quickly enough */
416     diff = ((2 * delta + 1) * step) >> shift;
417     predictor = c->predictor;
418     if (sign) predictor -= diff;
419     else predictor += diff;
420
421     CLAMP_TO_SHORT(predictor);
422     c->predictor = predictor;
423     c->step_index = step_index;
424
425     return (short)predictor;
426 }
427
428 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
429 {
430     int predictor;
431
432     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
433     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
434     CLAMP_TO_SHORT(predictor);
435
436     c->sample2 = c->sample1;
437     c->sample1 = predictor;
438     c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
439     if (c->idelta < 16) c->idelta = 16;
440
441     return (short)predictor;
442 }
443
444 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
445 {
446     int predictor;
447     int sign, delta, diff;
448     int new_step;
449
450     sign = nibble & 8;
451     delta = nibble & 7;
452     /* perform direct multiplication instead of series of jumps proposed by
453      * the reference ADPCM implementation since modern CPUs can do the mults
454      * quickly enough */
455     diff = ((2 * delta + 1) * c->step) >> 3;
456     predictor = c->predictor;
457     /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
458     if(sign)
459         predictor = ((predictor * 254) >> 8) - diff;
460     else
461             predictor = ((predictor * 254) >> 8) + diff;
462     /* calculate new step and clamp it to range 511..32767 */
463     new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
464     c->step = new_step;
465     if(c->step < 511)
466         c->step = 511;
467     if(c->step > 32767)
468         c->step = 32767;
469
470     CLAMP_TO_SHORT(predictor);
471     c->predictor = predictor;
472     return (short)predictor;
473 }
474
475 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
476 {
477     int sign, delta, diff;
478
479     sign = nibble & (1<<(size-1));
480     delta = nibble & ((1<<(size-1))-1);
481     diff = delta << (7 + c->step + shift);
482
483     if (sign)
484         c->predictor -= diff;
485     else
486         c->predictor += diff;
487
488     /* clamp result */
489     if (c->predictor > 16256)
490         c->predictor = 16256;
491     else if (c->predictor < -16384)
492         c->predictor = -16384;
493
494     /* calculate new step */
495     if (delta >= (2*size - 3) && c->step < 3)
496         c->step++;
497     else if (delta == 0 && c->step > 0)
498         c->step--;
499
500     return (short) c->predictor;
501 }
502
503 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
504 {
505     if(!c->step) {
506         c->predictor = 0;
507         c->step = 127;
508     }
509
510     c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
511     CLAMP_TO_SHORT(c->predictor);
512     c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
513     c->step = clip(c->step, 127, 24567);
514     return c->predictor;
515 }
516
517 static void xa_decode(short *out, const unsigned char *in,
518     ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
519 {
520     int i, j;
521     int shift,filter,f0,f1;
522     int s_1,s_2;
523     int d,s,t;
524
525     for(i=0;i<4;i++) {
526
527         shift  = 12 - (in[4+i*2] & 15);
528         filter = in[4+i*2] >> 4;
529         f0 = xa_adpcm_table[filter][0];
530         f1 = xa_adpcm_table[filter][1];
531
532         s_1 = left->sample1;
533         s_2 = left->sample2;
534
535         for(j=0;j<28;j++) {
536             d = in[16+i+j*4];
537
538             t = (signed char)(d<<4)>>4;
539             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
540             CLAMP_TO_SHORT(s);
541             *out = s;
542             out += inc;
543             s_2 = s_1;
544             s_1 = s;
545         }
546
547         if (inc==2) { /* stereo */
548             left->sample1 = s_1;
549             left->sample2 = s_2;
550             s_1 = right->sample1;
551             s_2 = right->sample2;
552             out = out + 1 - 28*2;
553         }
554
555         shift  = 12 - (in[5+i*2] & 15);
556         filter = in[5+i*2] >> 4;
557
558         f0 = xa_adpcm_table[filter][0];
559         f1 = xa_adpcm_table[filter][1];
560
561         for(j=0;j<28;j++) {
562             d = in[16+i+j*4];
563
564             t = (signed char)d >> 4;
565             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
566             CLAMP_TO_SHORT(s);
567             *out = s;
568             out += inc;
569             s_2 = s_1;
570             s_1 = s;
571         }
572
573         if (inc==2) { /* stereo */
574             right->sample1 = s_1;
575             right->sample2 = s_2;
576             out -= 1;
577         } else {
578             left->sample1 = s_1;
579             left->sample2 = s_2;
580         }
581     }
582 }
583
584
585 /* DK3 ADPCM support macro */
586 #define DK3_GET_NEXT_NIBBLE() \
587     if (decode_top_nibble_next) \
588     { \
589         nibble = (last_byte >> 4) & 0x0F; \
590         decode_top_nibble_next = 0; \
591     } \
592     else \
593     { \
594         last_byte = *src++; \
595         if (src >= buf + buf_size) break; \
596         nibble = last_byte & 0x0F; \
597         decode_top_nibble_next = 1; \
598     }
599
600 static int adpcm_decode_frame(AVCodecContext *avctx,
601                             void *data, int *data_size,
602                             uint8_t *buf, int buf_size)
603 {
604     ADPCMContext *c = avctx->priv_data;
605     ADPCMChannelStatus *cs;
606     int n, m, channel, i;
607     int block_predictor[2];
608     short *samples;
609     uint8_t *src;
610     int st; /* stereo */
611
612     /* DK3 ADPCM accounting variables */
613     unsigned char last_byte = 0;
614     unsigned char nibble;
615     int decode_top_nibble_next = 0;
616     int diff_channel;
617
618     /* EA ADPCM state variables */
619     uint32_t samples_in_chunk;
620     int32_t previous_left_sample, previous_right_sample;
621     int32_t current_left_sample, current_right_sample;
622     int32_t next_left_sample, next_right_sample;
623     int32_t coeff1l, coeff2l, coeff1r, coeff2r;
624     uint8_t shift_left, shift_right;
625     int count1, count2;
626
627     if (!buf_size)
628         return 0;
629
630     samples = data;
631     src = buf;
632
633     st = avctx->channels == 2 ? 1 : 0;
634
635     switch(avctx->codec->id) {
636     case CODEC_ID_ADPCM_IMA_QT:
637         n = (buf_size - 2);/* >> 2*avctx->channels;*/
638         channel = c->channel;
639         cs = &(c->status[channel]);
640         /* (pppppp) (piiiiiii) */
641
642         /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
643         cs->predictor = (*src++) << 8;
644         cs->predictor |= (*src & 0x80);
645         cs->predictor &= 0xFF80;
646
647         /* sign extension */
648         if(cs->predictor & 0x8000)
649             cs->predictor -= 0x10000;
650
651         CLAMP_TO_SHORT(cs->predictor);
652
653         cs->step_index = (*src++) & 0x7F;
654
655         if (cs->step_index > 88){
656             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
657             cs->step_index = 88;
658         }
659
660         cs->step = step_table[cs->step_index];
661
662         if (st && channel)
663             samples++;
664
665         for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
666             *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
667             samples += avctx->channels;
668             *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
669             samples += avctx->channels;
670             src ++;
671         }
672
673         if(st) { /* handle stereo interlacing */
674             c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
675             if(channel == 1) { /* wait for the other packet before outputing anything */
676                 return src - buf;
677             }
678         }
679         break;
680     case CODEC_ID_ADPCM_IMA_WAV:
681         if (avctx->block_align != 0 && buf_size > avctx->block_align)
682             buf_size = avctx->block_align;
683
684 //        samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
685
686         for(i=0; i<avctx->channels; i++){
687             cs = &(c->status[i]);
688             cs->predictor = (int16_t)(src[0] + (src[1]<<8));
689             src+=2;
690
691         // XXX: is this correct ??: *samples++ = cs->predictor;
692
693             cs->step_index = *src++;
694             if (cs->step_index > 88){
695                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
696                 cs->step_index = 88;
697             }
698             if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
699         }
700
701         while(src < buf + buf_size){
702             for(m=0; m<4; m++){
703                 for(i=0; i<=st; i++)
704                     *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
705                 for(i=0; i<=st; i++)
706                     *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4  , 3);
707                 src++;
708             }
709             src += 4*st;
710         }
711         break;
712     case CODEC_ID_ADPCM_4XM:
713         cs = &(c->status[0]);
714         c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
715         if(st){
716             c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
717         }
718         c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
719         if(st){
720             c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
721         }
722         if (cs->step_index < 0) cs->step_index = 0;
723         if (cs->step_index > 88) cs->step_index = 88;
724
725         m= (buf_size - (src - buf))>>st;
726         for(i=0; i<m; i++) {
727             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
728             if (st)
729                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
730             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
731             if (st)
732                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
733         }
734
735         src += m<<st;
736
737         break;
738     case CODEC_ID_ADPCM_MS:
739         if (avctx->block_align != 0 && buf_size > avctx->block_align)
740             buf_size = avctx->block_align;
741         n = buf_size - 7 * avctx->channels;
742         if (n < 0)
743             return -1;
744         block_predictor[0] = clip(*src++, 0, 7);
745         block_predictor[1] = 0;
746         if (st)
747             block_predictor[1] = clip(*src++, 0, 7);
748         c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
749         src+=2;
750         if (st){
751             c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
752             src+=2;
753         }
754         c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
755         c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
756         c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
757         c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
758
759         c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
760         src+=2;
761         if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
762         if (st) src+=2;
763         c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
764         src+=2;
765         if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
766         if (st) src+=2;
767
768         *samples++ = c->status[0].sample1;
769         if (st) *samples++ = c->status[1].sample1;
770         *samples++ = c->status[0].sample2;
771         if (st) *samples++ = c->status[1].sample2;
772         for(;n>0;n--) {
773             *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
774             *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
775             src ++;
776         }
777         break;
778     case CODEC_ID_ADPCM_IMA_DK4:
779         if (avctx->block_align != 0 && buf_size > avctx->block_align)
780             buf_size = avctx->block_align;
781
782         c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
783         c->status[0].step_index = src[2];
784         src += 4;
785         *samples++ = c->status[0].predictor;
786         if (st) {
787             c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
788             c->status[1].step_index = src[2];
789             src += 4;
790             *samples++ = c->status[1].predictor;
791         }
792         while (src < buf + buf_size) {
793
794             /* take care of the top nibble (always left or mono channel) */
795             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
796                 (src[0] >> 4) & 0x0F, 3);
797
798             /* take care of the bottom nibble, which is right sample for
799              * stereo, or another mono sample */
800             if (st)
801                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
802                     src[0] & 0x0F, 3);
803             else
804                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
805                     src[0] & 0x0F, 3);
806
807             src++;
808         }
809         break;
810     case CODEC_ID_ADPCM_IMA_DK3:
811         if (avctx->block_align != 0 && buf_size > avctx->block_align)
812             buf_size = avctx->block_align;
813
814         c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
815         c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
816         c->status[0].step_index = src[14];
817         c->status[1].step_index = src[15];
818         /* sign extend the predictors */
819         src += 16;
820         diff_channel = c->status[1].predictor;
821
822         /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
823          * the buffer is consumed */
824         while (1) {
825
826             /* for this algorithm, c->status[0] is the sum channel and
827              * c->status[1] is the diff channel */
828
829             /* process the first predictor of the sum channel */
830             DK3_GET_NEXT_NIBBLE();
831             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
832
833             /* process the diff channel predictor */
834             DK3_GET_NEXT_NIBBLE();
835             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
836
837             /* process the first pair of stereo PCM samples */
838             diff_channel = (diff_channel + c->status[1].predictor) / 2;
839             *samples++ = c->status[0].predictor + c->status[1].predictor;
840             *samples++ = c->status[0].predictor - c->status[1].predictor;
841
842             /* process the second predictor of the sum channel */
843             DK3_GET_NEXT_NIBBLE();
844             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
845
846             /* process the second pair of stereo PCM samples */
847             diff_channel = (diff_channel + c->status[1].predictor) / 2;
848             *samples++ = c->status[0].predictor + c->status[1].predictor;
849             *samples++ = c->status[0].predictor - c->status[1].predictor;
850         }
851         break;
852     case CODEC_ID_ADPCM_IMA_WS:
853         /* no per-block initialization; just start decoding the data */
854         while (src < buf + buf_size) {
855
856             if (st) {
857                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
858                     (src[0] >> 4) & 0x0F, 3);
859                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
860                     src[0] & 0x0F, 3);
861             } else {
862                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
863                     (src[0] >> 4) & 0x0F, 3);
864                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
865                     src[0] & 0x0F, 3);
866             }
867
868             src++;
869         }
870         break;
871     case CODEC_ID_ADPCM_XA:
872         c->status[0].sample1 = c->status[0].sample2 =
873         c->status[1].sample1 = c->status[1].sample2 = 0;
874         while (buf_size >= 128) {
875             xa_decode(samples, src, &c->status[0], &c->status[1],
876                 avctx->channels);
877             src += 128;
878             samples += 28 * 8;
879             buf_size -= 128;
880         }
881         break;
882     case CODEC_ID_ADPCM_EA:
883         samples_in_chunk = LE_32(src);
884         if (samples_in_chunk >= ((buf_size - 12) * 2)) {
885             src += buf_size;
886             break;
887         }
888         src += 4;
889         current_left_sample = (int16_t)LE_16(src);
890         src += 2;
891         previous_left_sample = (int16_t)LE_16(src);
892         src += 2;
893         current_right_sample = (int16_t)LE_16(src);
894         src += 2;
895         previous_right_sample = (int16_t)LE_16(src);
896         src += 2;
897
898         for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
899             coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
900             coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
901             coeff1r = ea_adpcm_table[*src & 0x0F];
902             coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
903             src++;
904
905             shift_left = ((*src >> 4) & 0x0F) + 8;
906             shift_right = (*src & 0x0F) + 8;
907             src++;
908
909             for (count2 = 0; count2 < 28; count2++) {
910                 next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
911                 next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
912                 src++;
913
914                 next_left_sample = (next_left_sample +
915                     (current_left_sample * coeff1l) +
916                     (previous_left_sample * coeff2l) + 0x80) >> 8;
917                 next_right_sample = (next_right_sample +
918                     (current_right_sample * coeff1r) +
919                     (previous_right_sample * coeff2r) + 0x80) >> 8;
920                 CLAMP_TO_SHORT(next_left_sample);
921                 CLAMP_TO_SHORT(next_right_sample);
922
923                 previous_left_sample = current_left_sample;
924                 current_left_sample = next_left_sample;
925                 previous_right_sample = current_right_sample;
926                 current_right_sample = next_right_sample;
927                 *samples++ = (unsigned short)current_left_sample;
928                 *samples++ = (unsigned short)current_right_sample;
929             }
930         }
931         break;
932     case CODEC_ID_ADPCM_IMA_SMJPEG:
933         c->status[0].predictor = *src;
934         src += 2;
935         c->status[0].step_index = *src++;
936         src++;  /* skip another byte before getting to the meat */
937         while (src < buf + buf_size) {
938             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
939                 *src & 0x0F, 3);
940             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
941                 (*src >> 4) & 0x0F, 3);
942             src++;
943         }
944         break;
945     case CODEC_ID_ADPCM_CT:
946         while (src < buf + buf_size) {
947             if (st) {
948                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
949                     (src[0] >> 4) & 0x0F);
950                 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
951                     src[0] & 0x0F);
952             } else {
953                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
954                     (src[0] >> 4) & 0x0F);
955                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
956                     src[0] & 0x0F);
957             }
958             src++;
959         }
960         break;
961     case CODEC_ID_ADPCM_SBPRO_4:
962     case CODEC_ID_ADPCM_SBPRO_3:
963     case CODEC_ID_ADPCM_SBPRO_2:
964         if (!c->status[0].step_index) {
965             /* the first byte is a raw sample */
966             *samples++ = 128 * (*src++ - 0x80);
967             if (st)
968               *samples++ = 128 * (*src++ - 0x80);
969             c->status[0].step_index = 1;
970         }
971         if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
972             while (src < buf + buf_size) {
973                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
974                     (src[0] >> 4) & 0x0F, 4, 0);
975                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
976                     src[0] & 0x0F, 4, 0);
977                 src++;
978             }
979         } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
980             while (src < buf + buf_size) {
981                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
982                     (src[0] >> 5) & 0x07, 3, 0);
983                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
984                     (src[0] >> 2) & 0x07, 3, 0);
985                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
986                     src[0] & 0x03, 2, 0);
987                 src++;
988             }
989         } else {
990             while (src < buf + buf_size) {
991                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
992                     (src[0] >> 6) & 0x03, 2, 2);
993                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
994                     (src[0] >> 4) & 0x03, 2, 2);
995                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
996                     (src[0] >> 2) & 0x03, 2, 2);
997                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
998                     src[0] & 0x03, 2, 2);
999                 src++;
1000             }
1001         }
1002         break;
1003     case CODEC_ID_ADPCM_SWF:
1004     {
1005         GetBitContext gb;
1006         const int *table;
1007         int k0, signmask;
1008         int size = buf_size*8;
1009
1010         init_get_bits(&gb, buf, size);
1011
1012         // first frame, read bits & inital values
1013         if (!c->nb_bits)
1014         {
1015             c->nb_bits = get_bits(&gb, 2)+2;
1016 //            av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
1017         }
1018
1019         table = swf_index_tables[c->nb_bits-2];
1020         k0 = 1 << (c->nb_bits-2);
1021         signmask = 1 << (c->nb_bits-1);
1022
1023         while (get_bits_count(&gb) <= size)
1024         {
1025             int i;
1026
1027             c->nb_samples++;
1028             // wrap around at every 4096 samples...
1029             if ((c->nb_samples & 0xfff) == 1)
1030             {
1031                 for (i = 0; i <= st; i++)
1032                 {
1033                     *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1034                     c->status[i].step_index = get_bits(&gb, 6);
1035                 }
1036             }
1037
1038             // similar to IMA adpcm
1039             for (i = 0; i <= st; i++)
1040             {
1041                 int delta = get_bits(&gb, c->nb_bits);
1042                 int step = step_table[c->status[i].step_index];
1043                 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1044                 int k = k0;
1045
1046                 do {
1047                     if (delta & k)
1048                         vpdiff += step;
1049                     step >>= 1;
1050                     k >>= 1;
1051                 } while(k);
1052                 vpdiff += step;
1053
1054                 if (delta & signmask)
1055                     c->status[i].predictor -= vpdiff;
1056                 else
1057                     c->status[i].predictor += vpdiff;
1058
1059                 c->status[i].step_index += table[delta & (~signmask)];
1060
1061                 c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
1062                 c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
1063
1064                 *samples++ = c->status[i].predictor;
1065             }
1066         }
1067
1068 //        src += get_bits_count(&gb)*8;
1069         src += size;
1070
1071         break;
1072     }
1073     case CODEC_ID_ADPCM_YAMAHA:
1074         while (src < buf + buf_size) {
1075             if (st) {
1076                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1077                         src[0] & 0x0F);
1078                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
1079                         (src[0] >> 4) & 0x0F);
1080             } else {
1081                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1082                         src[0] & 0x0F);
1083                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1084                         (src[0] >> 4) & 0x0F);
1085             }
1086             src++;
1087         }
1088         break;
1089     default:
1090         return -1;
1091     }
1092     *data_size = (uint8_t *)samples - (uint8_t *)data;
1093     return src - buf;
1094 }
1095
1096
1097
1098 #ifdef CONFIG_ENCODERS
1099 #define ADPCM_ENCODER(id,name)                  \
1100 AVCodec name ## _encoder = {                    \
1101     #name,                                      \
1102     CODEC_TYPE_AUDIO,                           \
1103     id,                                         \
1104     sizeof(ADPCMContext),                       \
1105     adpcm_encode_init,                          \
1106     adpcm_encode_frame,                         \
1107     adpcm_encode_close,                         \
1108     NULL,                                       \
1109 };
1110 #else
1111 #define ADPCM_ENCODER(id,name)
1112 #endif
1113
1114 #ifdef CONFIG_DECODERS
1115 #define ADPCM_DECODER(id,name)                  \
1116 AVCodec name ## _decoder = {                    \
1117     #name,                                      \
1118     CODEC_TYPE_AUDIO,                           \
1119     id,                                         \
1120     sizeof(ADPCMContext),                       \
1121     adpcm_decode_init,                          \
1122     NULL,                                       \
1123     NULL,                                       \
1124     adpcm_decode_frame,                         \
1125 };
1126 #else
1127 #define ADPCM_DECODER(id,name)
1128 #endif
1129
1130 #define ADPCM_CODEC(id, name)                   \
1131 ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
1132
1133 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
1134 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
1135 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
1136 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
1137 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
1138 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
1139 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
1140 ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
1141 ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
1142 ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
1143 ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
1144 ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
1145 ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
1146 ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
1147 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
1148 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
1149 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
1150
1151 #undef ADPCM_CODEC