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