• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files

hvirtual/quicktime/ffmpeg/libavcodec/adpcm.c

Go to the documentation of this file.
00001 /*
00002  * ADPCM codecs
00003  * Copyright (c) 2001-2003 The ffmpeg Project
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 #include "avcodec.h"
00020 #include "bitstream.h"
00021 
00047 #define BLKSIZE 1024
00048 
00049 #define CLAMP_TO_SHORT(value) \
00050 if (value > 32767) \
00051     value = 32767; \
00052 else if (value < -32768) \
00053     value = -32768; \
00054 
00055 /* step_table[] and index_table[] are from the ADPCM reference source */
00056 /* This is the index table: */
00057 static const int index_table[16] = {
00058     -1, -1, -1, -1, 2, 4, 6, 8,
00059     -1, -1, -1, -1, 2, 4, 6, 8,
00060 };
00061 
00066 static const int step_table[89] = {
00067     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
00068     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
00069     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
00070     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
00071     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
00072     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
00073     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
00074     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
00075     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
00076 };
00077 
00078 /* These are for MS-ADPCM */
00079 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
00080 static const int AdaptationTable[] = {
00081         230, 230, 230, 230, 307, 409, 512, 614,
00082         768, 614, 512, 409, 307, 230, 230, 230
00083 };
00084 
00085 static const int AdaptCoeff1[] = {
00086         256, 512, 0, 192, 240, 460, 392
00087 };
00088 
00089 static const int AdaptCoeff2[] = {
00090         0, -256, 0, 64, 0, -208, -232
00091 };
00092 
00093 /* These are for CD-ROM XA ADPCM */
00094 static const int xa_adpcm_table[5][2] = {
00095    {   0,   0 },
00096    {  60,   0 },
00097    { 115, -52 },
00098    {  98, -55 },
00099    { 122, -60 }
00100 };
00101 
00102 static const int ea_adpcm_table[] = {
00103     0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
00104     3, 4, 7, 8, 10, 11, 0, -1, -3, -4
00105 };
00106 
00107 static const int ct_adpcm_table[8] = {
00108     0x00E6, 0x00E6, 0x00E6, 0x00E6,
00109     0x0133, 0x0199, 0x0200, 0x0266
00110 };
00111 
00112 // padded to zero where table size is less then 16
00113 static const int swf_index_tables[4][16] = {
00114     /*2*/ { -1, 2 },
00115     /*3*/ { -1, -1, 2, 4 },
00116     /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
00117     /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
00118 };
00119 
00120 static const int yamaha_indexscale[] = {
00121     230, 230, 230, 230, 307, 409, 512, 614,
00122     230, 230, 230, 230, 307, 409, 512, 614
00123 };
00124 
00125 static const int yamaha_difflookup[] = {
00126     1, 3, 5, 7, 9, 11, 13, 15,
00127     -1, -3, -5, -7, -9, -11, -13, -15
00128 };
00129 
00130 /* end of tables */
00131 
00132 typedef struct ADPCMChannelStatus {
00133     int predictor;
00134     short int step_index;
00135     int step;
00136     /* for encoding */
00137     int prev_sample;
00138 
00139     /* MS version */
00140     short sample1;
00141     short sample2;
00142     int coeff1;
00143     int coeff2;
00144     int idelta;
00145 } ADPCMChannelStatus;
00146 
00147 typedef struct ADPCMContext {
00148     int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
00149     ADPCMChannelStatus status[2];
00150     short sample_buffer[32]; /* hold left samples while waiting for right samples */
00151 
00152     /* SWF only */
00153     int nb_bits;
00154     int nb_samples;
00155 } ADPCMContext;
00156 
00157 /* XXX: implement encoding */
00158 
00159 #ifdef CONFIG_ENCODERS
00160 static int adpcm_encode_init(AVCodecContext *avctx)
00161 {
00162     if (avctx->channels > 2)
00163         return -1; /* only stereo or mono =) */
00164     switch(avctx->codec->id) {
00165     case CODEC_ID_ADPCM_IMA_QT:
00166         av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
00167         avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
00168         return -1;
00169         break;
00170     case CODEC_ID_ADPCM_IMA_WAV:
00171         avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
00172                                                              /* and we have 4 bytes per channel overhead */
00173         avctx->block_align = BLKSIZE;
00174         /* seems frame_size isn't taken into account... have to buffer the samples :-( */
00175         break;
00176     case CODEC_ID_ADPCM_MS:
00177         avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
00178                                                              /* and we have 7 bytes per channel overhead */
00179         avctx->block_align = BLKSIZE;
00180         break;
00181     case CODEC_ID_ADPCM_YAMAHA:
00182         avctx->frame_size = BLKSIZE * avctx->channels;
00183         avctx->block_align = BLKSIZE;
00184         break;
00185     default:
00186         return -1;
00187         break;
00188     }
00189 
00190     avctx->coded_frame= avcodec_alloc_frame();
00191     avctx->coded_frame->key_frame= 1;
00192 
00193     return 0;
00194 }
00195 
00196 static int adpcm_encode_close(AVCodecContext *avctx)
00197 {
00198     av_freep(&avctx->coded_frame);
00199 
00200     return 0;
00201 }
00202 
00203 
00204 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
00205 {
00206     int step_index;
00207     unsigned char nibble;
00208     
00209     int sign = 0; /* sign bit of the nibble (MSB) */
00210     int delta, predicted_delta;
00211 
00212     delta = sample - c->prev_sample;
00213 
00214     if (delta < 0) {
00215         sign = 1;
00216         delta = -delta;
00217     }
00218 
00219     step_index = c->step_index;
00220 
00221     /* nibble = 4 * delta / step_table[step_index]; */
00222     nibble = (delta << 2) / step_table[step_index];
00223 
00224     if (nibble > 7)
00225         nibble = 7;
00226 
00227     step_index += index_table[nibble];
00228     if (step_index < 0)
00229         step_index = 0;
00230     if (step_index > 88)
00231         step_index = 88;
00232 
00233     /* what the decoder will find */
00234     predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
00235 
00236     if (sign)
00237         c->prev_sample -= predicted_delta;
00238     else
00239         c->prev_sample += predicted_delta;
00240 
00241     CLAMP_TO_SHORT(c->prev_sample);
00242 
00243 
00244     nibble += sign << 3; /* sign * 8 */   
00245 
00246     /* save back */
00247     c->step_index = step_index;
00248 
00249     return nibble;
00250 }
00251 
00252 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
00253 {
00254     int predictor, nibble, bias;
00255 
00256     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
00257     
00258     nibble= sample - predictor;
00259     if(nibble>=0) bias= c->idelta/2;
00260     else          bias=-c->idelta/2;
00261         
00262     nibble= (nibble + bias) / c->idelta;
00263     nibble= clip(nibble, -8, 7)&0x0F;
00264     
00265     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
00266     CLAMP_TO_SHORT(predictor);
00267 
00268     c->sample2 = c->sample1;
00269     c->sample1 = predictor;
00270 
00271     c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
00272     if (c->idelta < 16) c->idelta = 16;
00273 
00274     return nibble;
00275 }
00276 
00277 static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
00278 {
00279     int i1 = 0, j1;
00280 
00281     if(!c->step) {
00282         c->predictor = 0;
00283         c->step = 127;
00284     }
00285     j1 = sample - c->predictor;
00286 
00287     j1 = (j1 * 8) / c->step;
00288     i1 = abs(j1) / 2;
00289     if (i1 > 7)
00290         i1 = 7;
00291     if (j1 < 0)
00292         i1 += 8;
00293 
00294     c->predictor = c->predictor + ((c->step * yamaha_difflookup[i1]) / 8);
00295     CLAMP_TO_SHORT(c->predictor);
00296     c->step = (c->step * yamaha_indexscale[i1]) >> 8;
00297     c->step = clip(c->step, 127, 24567);
00298 
00299     return i1;
00300 }
00301 
00302 static int adpcm_encode_frame(AVCodecContext *avctx,
00303                             unsigned char *frame, int buf_size, void *data)
00304 {
00305     int n, i, st;
00306     short *samples;
00307     unsigned char *dst;
00308     ADPCMContext *c = avctx->priv_data;
00309 
00310     dst = frame;
00311     samples = (short *)data;
00312     st= avctx->channels == 2;
00313 /*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
00314 
00315     switch(avctx->codec->id) {
00316     case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
00317         break;
00318     case CODEC_ID_ADPCM_IMA_WAV:
00319         n = avctx->frame_size / 8;
00320             c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
00321 /*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
00322             *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
00323             *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
00324             *dst++ = (unsigned char)c->status[0].step_index;
00325             *dst++ = 0; /* unknown */
00326             samples++;
00327             if (avctx->channels == 2) {
00328                 c->status[1].prev_sample = (signed short)samples[1];
00329 /*                c->status[1].step_index = 0; */
00330                 *dst++ = (c->status[1].prev_sample) & 0xFF;
00331                 *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
00332                 *dst++ = (unsigned char)c->status[1].step_index;
00333                 *dst++ = 0;
00334                 samples++;
00335             }
00336         
00337             /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
00338             for (; n>0; n--) {
00339                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
00340                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
00341                 dst++;
00342                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
00343                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
00344                 dst++;
00345                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
00346                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
00347                 dst++;
00348                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
00349                 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
00350                 dst++;
00351                 /* right channel */
00352                 if (avctx->channels == 2) {
00353                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
00354                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
00355                     dst++;
00356                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
00357                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
00358                     dst++;
00359                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
00360                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
00361                     dst++;
00362                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
00363                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
00364                     dst++;
00365                 }
00366                 samples += 8 * avctx->channels;
00367             }
00368         break;
00369     case CODEC_ID_ADPCM_MS:
00370         for(i=0; i<avctx->channels; i++){
00371             int predictor=0;
00372 
00373             *dst++ = predictor;
00374             c->status[i].coeff1 = AdaptCoeff1[predictor];
00375             c->status[i].coeff2 = AdaptCoeff2[predictor];
00376         }
00377         for(i=0; i<avctx->channels; i++){
00378             if (c->status[i].idelta < 16) 
00379                 c->status[i].idelta = 16;
00380             
00381             *dst++ = c->status[i].idelta & 0xFF;
00382             *dst++ = c->status[i].idelta >> 8;
00383         }
00384         for(i=0; i<avctx->channels; i++){
00385             c->status[i].sample1= *samples++;
00386 
00387             *dst++ = c->status[i].sample1 & 0xFF;
00388             *dst++ = c->status[i].sample1 >> 8;
00389         }
00390         for(i=0; i<avctx->channels; i++){
00391             c->status[i].sample2= *samples++;
00392 
00393             *dst++ = c->status[i].sample2 & 0xFF;
00394             *dst++ = c->status[i].sample2 >> 8;
00395         }
00396 
00397         for(i=7*avctx->channels; i<avctx->block_align; i++) {
00398             int nibble;
00399             nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
00400             nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
00401             *dst++ = nibble;
00402         }
00403         break;
00404     case CODEC_ID_ADPCM_YAMAHA:
00405         n = avctx->frame_size / 2;
00406         for (; n>0; n--) {
00407             for(i = 0; i < avctx->channels; i++) {
00408                 int nibble;
00409                 nibble  = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
00410                 nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
00411                 *dst++ = nibble;
00412             }
00413             samples += 2 * avctx->channels;
00414         }
00415         break;
00416     default:
00417         return -1;
00418     }
00419     return dst - frame;
00420 }
00421 #endif //CONFIG_ENCODERS
00422 
00423 static int adpcm_decode_init(AVCodecContext * avctx)
00424 {
00425     ADPCMContext *c = avctx->priv_data;
00426 
00427     c->channel = 0;
00428     c->status[0].predictor = c->status[1].predictor = 0;
00429     c->status[0].step_index = c->status[1].step_index = 0;
00430     c->status[0].step = c->status[1].step = 0;
00431 
00432     switch(avctx->codec->id) {
00433     case CODEC_ID_ADPCM_CT:
00434         c->status[0].step = c->status[1].step = 511;
00435         break;
00436     default:
00437         break;
00438     }
00439     return 0;
00440 }
00441 
00442 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
00443 {
00444     int step_index;
00445     int predictor;
00446     int sign, delta, diff, step;
00447 
00448     step = step_table[c->step_index];
00449     step_index = c->step_index + index_table[(unsigned)nibble];
00450     if (step_index < 0) step_index = 0;
00451     else if (step_index > 88) step_index = 88;
00452 
00453     sign = nibble & 8;
00454     delta = nibble & 7;
00455     /* perform direct multiplication instead of series of jumps proposed by
00456      * the reference ADPCM implementation since modern CPUs can do the mults
00457      * quickly enough */
00458     diff = ((2 * delta + 1) * step) >> shift;
00459     predictor = c->predictor;
00460     if (sign) predictor -= diff;
00461     else predictor += diff;
00462 
00463     CLAMP_TO_SHORT(predictor);
00464     c->predictor = predictor;
00465     c->step_index = step_index;
00466 
00467     return (short)predictor;
00468 }
00469 
00470 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
00471 {
00472     int predictor;
00473 
00474     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
00475     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
00476     CLAMP_TO_SHORT(predictor);
00477 
00478     c->sample2 = c->sample1;
00479     c->sample1 = predictor;
00480     c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
00481     if (c->idelta < 16) c->idelta = 16;
00482 
00483     return (short)predictor;
00484 }
00485 
00486 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
00487 {
00488     int predictor;
00489     int sign, delta, diff;
00490     int new_step;
00491 
00492     sign = nibble & 8;
00493     delta = nibble & 7;
00494     /* perform direct multiplication instead of series of jumps proposed by
00495      * the reference ADPCM implementation since modern CPUs can do the mults
00496      * quickly enough */
00497     diff = ((2 * delta + 1) * c->step) >> 3;
00498     predictor = c->predictor;
00499     /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
00500     if(sign)
00501         predictor = ((predictor * 254) >> 8) - diff;
00502     else
00503         predictor = ((predictor * 254) >> 8) + diff;
00504     /* calculate new step and clamp it to range 511..32767 */
00505     new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
00506     c->step = new_step;
00507     if(c->step < 511)
00508         c->step = 511;
00509     if(c->step > 32767)
00510         c->step = 32767;
00511 
00512     CLAMP_TO_SHORT(predictor);
00513     c->predictor = predictor;
00514     return (short)predictor;
00515 }
00516 
00517 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
00518 {
00519     if(!c->step) {
00520         c->predictor = 0;
00521         c->step = 127;
00522     }
00523 
00524     c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
00525     CLAMP_TO_SHORT(c->predictor);
00526     c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
00527     c->step = clip(c->step, 127, 24567);
00528     return c->predictor;
00529 }
00530 
00531 static void xa_decode(short *out, const unsigned char *in, 
00532     ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
00533 {
00534     int i, j;
00535     int shift,filter,f0,f1;
00536     int s_1,s_2;
00537     int d,s,t;
00538 
00539     for(i=0;i<4;i++) {
00540 
00541         shift  = 12 - (in[4+i*2] & 15);
00542         filter = in[4+i*2] >> 4;
00543         f0 = xa_adpcm_table[filter][0];
00544         f1 = xa_adpcm_table[filter][1];
00545 
00546         s_1 = left->sample1;
00547         s_2 = left->sample2;
00548 
00549         for(j=0;j<28;j++) {
00550             d = in[16+i+j*4];
00551 
00552             t = (signed char)(d<<4)>>4;
00553             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
00554             CLAMP_TO_SHORT(s);
00555             *out = s;
00556             out += inc;
00557             s_2 = s_1;
00558             s_1 = s;
00559         }
00560 
00561         if (inc==2) { /* stereo */
00562             left->sample1 = s_1;
00563             left->sample2 = s_2;
00564             s_1 = right->sample1;
00565             s_2 = right->sample2;
00566             out = out + 1 - 28*2;
00567         }
00568 
00569         shift  = 12 - (in[5+i*2] & 15);
00570         filter = in[5+i*2] >> 4;
00571 
00572         f0 = xa_adpcm_table[filter][0];
00573         f1 = xa_adpcm_table[filter][1];
00574 
00575         for(j=0;j<28;j++) {
00576             d = in[16+i+j*4];
00577 
00578             t = (signed char)d >> 4;
00579             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
00580             CLAMP_TO_SHORT(s);
00581             *out = s;
00582             out += inc;
00583             s_2 = s_1;
00584             s_1 = s;
00585         }
00586 
00587         if (inc==2) { /* stereo */
00588             right->sample1 = s_1;
00589             right->sample2 = s_2;
00590             out -= 1;
00591         } else {
00592             left->sample1 = s_1;
00593             left->sample2 = s_2;
00594         }
00595     }
00596 }
00597 
00598 
00599 /* DK3 ADPCM support macro */
00600 #define DK3_GET_NEXT_NIBBLE() \
00601     if (decode_top_nibble_next) \
00602     { \
00603         nibble = (last_byte >> 4) & 0x0F; \
00604         decode_top_nibble_next = 0; \
00605     } \
00606     else \
00607     { \
00608         last_byte = *src++; \
00609         if (src >= buf + buf_size) break; \
00610         nibble = last_byte & 0x0F; \
00611         decode_top_nibble_next = 1; \
00612     }
00613 
00614 static int adpcm_decode_frame(AVCodecContext *avctx,
00615                             void *data, int *data_size,
00616                             uint8_t *buf, int buf_size)
00617 {
00618     ADPCMContext *c = avctx->priv_data;
00619     ADPCMChannelStatus *cs;
00620     int n, m, channel, i;
00621     int block_predictor[2];
00622     short *samples;
00623     uint8_t *src;
00624     int st; /* stereo */
00625 
00626     /* DK3 ADPCM accounting variables */
00627     unsigned char last_byte = 0;
00628     unsigned char nibble;
00629     int decode_top_nibble_next = 0;
00630     int diff_channel;
00631 
00632     /* EA ADPCM state variables */
00633     uint32_t samples_in_chunk;
00634     int32_t previous_left_sample, previous_right_sample;
00635     int32_t current_left_sample, current_right_sample;
00636     int32_t next_left_sample, next_right_sample;
00637     int32_t coeff1l, coeff2l, coeff1r, coeff2r;
00638     uint8_t shift_left, shift_right;
00639     int count1, count2;
00640 
00641     if (!buf_size)
00642         return 0;
00643 
00644     samples = data;
00645     src = buf;
00646 
00647     st = avctx->channels == 2;
00648 
00649     switch(avctx->codec->id) {
00650     case CODEC_ID_ADPCM_IMA_QT:
00651         n = (buf_size - 2);/* >> 2*avctx->channels;*/
00652         channel = c->channel;
00653         cs = &(c->status[channel]);
00654         /* (pppppp) (piiiiiii) */
00655 
00656         /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
00657         cs->predictor = (*src++) << 8;
00658         cs->predictor |= (*src & 0x80);
00659         cs->predictor &= 0xFF80;
00660 
00661         /* sign extension */
00662         if(cs->predictor & 0x8000)
00663             cs->predictor -= 0x10000;
00664 
00665         CLAMP_TO_SHORT(cs->predictor);
00666 
00667         cs->step_index = (*src++) & 0x7F;
00668 
00669         if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
00670         if (cs->step_index > 88) cs->step_index = 88;
00671 
00672         cs->step = step_table[cs->step_index];
00673 
00674         if (st && channel)
00675             samples++;
00676 
00677         for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
00678             *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
00679             samples += avctx->channels;
00680             *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
00681             samples += avctx->channels;
00682             src ++;
00683         }
00684 
00685         if(st) { /* handle stereo interlacing */
00686             c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
00687             if(channel == 1) { /* wait for the other packet before outputing anything */
00688                 return src - buf;
00689             }
00690         }
00691         break;
00692     case CODEC_ID_ADPCM_IMA_WAV:
00693         if (avctx->block_align != 0 && buf_size > avctx->block_align)
00694             buf_size = avctx->block_align;
00695 
00696         for(i=0; i<avctx->channels; i++){
00697             cs = &(c->status[i]);
00698             cs->predictor = *src++;
00699             cs->predictor |= (*src++) << 8;
00700             if(cs->predictor & 0x8000)
00701                 cs->predictor -= 0x10000;
00702             CLAMP_TO_SHORT(cs->predictor);
00703 
00704         // XXX: is this correct ??: *samples++ = cs->predictor;
00705 
00706             cs->step_index = *src++;
00707             if (cs->step_index < 0) cs->step_index = 0;
00708             if (cs->step_index > 88) cs->step_index = 88;
00709             if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
00710         }
00711 
00712         for(m=4; src < (buf + buf_size);) {
00713             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
00714             if (st)
00715                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
00716             *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
00717             if (st) {
00718                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
00719                 if (!--m) {
00720                     m=4;
00721                     src+=4;
00722                 }
00723             }
00724             src++;
00725         }
00726         break;
00727     case CODEC_ID_ADPCM_4XM:
00728         cs = &(c->status[0]);
00729         c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
00730         if(st){
00731             c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
00732         }
00733         c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
00734         if(st){
00735             c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
00736         }
00737         if (cs->step_index < 0) cs->step_index = 0;
00738         if (cs->step_index > 88) cs->step_index = 88;
00739 
00740         m= (buf_size - (src - buf))>>st;
00741         for(i=0; i<m; i++) {
00742             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
00743             if (st)
00744                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
00745             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
00746             if (st)
00747                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
00748         }
00749 
00750         src += m<<st;
00751 
00752         break;
00753     case CODEC_ID_ADPCM_MS:
00754         if (avctx->block_align != 0 && buf_size > avctx->block_align)
00755             buf_size = avctx->block_align;
00756         n = buf_size - 7 * avctx->channels;
00757         if (n < 0)
00758             return -1;
00759         block_predictor[0] = clip(*src++, 0, 7);
00760         block_predictor[1] = 0;
00761         if (st)
00762             block_predictor[1] = clip(*src++, 0, 7);
00763         c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
00764         src+=2;
00765         if (st){
00766             c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
00767             src+=2;
00768         }
00769         c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
00770         c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
00771         c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
00772         c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
00773         
00774         c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
00775         src+=2;
00776         if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
00777         if (st) src+=2;
00778         c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
00779         src+=2;
00780         if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
00781         if (st) src+=2;
00782 
00783         *samples++ = c->status[0].sample1;
00784         if (st) *samples++ = c->status[1].sample1;
00785         *samples++ = c->status[0].sample2;
00786         if (st) *samples++ = c->status[1].sample2;
00787         for(;n>0;n--) {
00788             *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
00789             *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
00790             src ++;
00791         }
00792         break;
00793     case CODEC_ID_ADPCM_IMA_DK4:
00794         if (avctx->block_align != 0 && buf_size > avctx->block_align)
00795             buf_size = avctx->block_align;
00796 
00797         c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
00798         c->status[0].step_index = src[2];
00799         src += 4;
00800         *samples++ = c->status[0].predictor;
00801         if (st) {
00802             c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
00803             c->status[1].step_index = src[2];
00804             src += 4;
00805             *samples++ = c->status[1].predictor;
00806         }
00807         while (src < buf + buf_size) {
00808 
00809             /* take care of the top nibble (always left or mono channel) */
00810             *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
00811                 (src[0] >> 4) & 0x0F, 3);
00812 
00813             /* take care of the bottom nibble, which is right sample for
00814              * stereo, or another mono sample */
00815             if (st)
00816                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], 
00817                     src[0] & 0x0F, 3);
00818             else
00819                 *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
00820                     src[0] & 0x0F, 3);
00821 
00822             src++;
00823         }
00824         break;
00825     case CODEC_ID_ADPCM_IMA_DK3:
00826         if (avctx->block_align != 0 && buf_size > avctx->block_align)
00827             buf_size = avctx->block_align;
00828 
00829         c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
00830         c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
00831         c->status[0].step_index = src[14];
00832         c->status[1].step_index = src[15];
00833         /* sign extend the predictors */
00834         src += 16;
00835         diff_channel = c->status[1].predictor;
00836 
00837         /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
00838          * the buffer is consumed */
00839         while (1) {
00840 
00841             /* for this algorithm, c->status[0] is the sum channel and
00842              * c->status[1] is the diff channel */
00843 
00844             /* process the first predictor of the sum channel */
00845             DK3_GET_NEXT_NIBBLE();
00846             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
00847 
00848             /* process the diff channel predictor */
00849             DK3_GET_NEXT_NIBBLE();
00850             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
00851 
00852             /* process the first pair of stereo PCM samples */
00853             diff_channel = (diff_channel + c->status[1].predictor) / 2;
00854             *samples++ = c->status[0].predictor + c->status[1].predictor;
00855             *samples++ = c->status[0].predictor - c->status[1].predictor;
00856 
00857             /* process the second predictor of the sum channel */
00858             DK3_GET_NEXT_NIBBLE();
00859             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
00860 
00861             /* process the second pair of stereo PCM samples */
00862             diff_channel = (diff_channel + c->status[1].predictor) / 2;
00863             *samples++ = c->status[0].predictor + c->status[1].predictor;
00864             *samples++ = c->status[0].predictor - c->status[1].predictor;
00865         }
00866         break;
00867     case CODEC_ID_ADPCM_IMA_WS:
00868         /* no per-block initialization; just start decoding the data */
00869         while (src < buf + buf_size) {
00870 
00871             if (st) {
00872                 *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
00873                     (src[0] >> 4) & 0x0F, 3);
00874                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], 
00875                     src[0] & 0x0F, 3);
00876             } else {
00877                 *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
00878                     (src[0] >> 4) & 0x0F, 3);
00879                 *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
00880                     src[0] & 0x0F, 3);
00881             }
00882 
00883             src++;
00884         }
00885         break;
00886     case CODEC_ID_ADPCM_XA:
00887         c->status[0].sample1 = c->status[0].sample2 = 
00888         c->status[1].sample1 = c->status[1].sample2 = 0;
00889         while (buf_size >= 128) {
00890             xa_decode(samples, src, &c->status[0], &c->status[1], 
00891                 avctx->channels);
00892             src += 128;
00893             samples += 28 * 8;
00894             buf_size -= 128;
00895         }
00896         break;
00897     case CODEC_ID_ADPCM_EA:
00898         samples_in_chunk = LE_32(src);
00899         if (samples_in_chunk >= ((buf_size - 12) * 2)) {
00900             src += buf_size;
00901             break;
00902         }
00903         src += 4;
00904         current_left_sample = (int16_t)LE_16(src);
00905         src += 2;
00906         previous_left_sample = (int16_t)LE_16(src);
00907         src += 2;
00908         current_right_sample = (int16_t)LE_16(src);
00909         src += 2;
00910         previous_right_sample = (int16_t)LE_16(src);
00911         src += 2;
00912 
00913         for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
00914             coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
00915             coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
00916             coeff1r = ea_adpcm_table[*src & 0x0F];
00917             coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
00918             src++;
00919 
00920             shift_left = ((*src >> 4) & 0x0F) + 8;
00921             shift_right = (*src & 0x0F) + 8;
00922             src++;
00923 
00924             for (count2 = 0; count2 < 28; count2++) {
00925                 next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
00926                 next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
00927                 src++;
00928 
00929                 next_left_sample = (next_left_sample + 
00930                     (current_left_sample * coeff1l) + 
00931                     (previous_left_sample * coeff2l) + 0x80) >> 8;
00932                 next_right_sample = (next_right_sample + 
00933                     (current_right_sample * coeff1r) + 
00934                     (previous_right_sample * coeff2r) + 0x80) >> 8;
00935                 CLAMP_TO_SHORT(next_left_sample);
00936                 CLAMP_TO_SHORT(next_right_sample);
00937 
00938                 previous_left_sample = current_left_sample;
00939                 current_left_sample = next_left_sample;
00940                 previous_right_sample = current_right_sample;
00941                 current_right_sample = next_right_sample;
00942                 *samples++ = (unsigned short)current_left_sample;
00943                 *samples++ = (unsigned short)current_right_sample;
00944             }
00945         }
00946         break;
00947     case CODEC_ID_ADPCM_IMA_SMJPEG:
00948         c->status[0].predictor = *src;
00949         src += 2;
00950         c->status[0].step_index = *src++;
00951         src++;  /* skip another byte before getting to the meat */
00952         while (src < buf + buf_size) {
00953             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
00954                 *src & 0x0F, 3);
00955             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
00956                 (*src >> 4) & 0x0F, 3);
00957             src++;
00958         }
00959         break;
00960     case CODEC_ID_ADPCM_CT:
00961         while (src < buf + buf_size) {
00962             if (st) {
00963                 *samples++ = adpcm_ct_expand_nibble(&c->status[0], 
00964                     (src[0] >> 4) & 0x0F);
00965                 *samples++ = adpcm_ct_expand_nibble(&c->status[1], 
00966                     src[0] & 0x0F);
00967             } else {
00968                 *samples++ = adpcm_ct_expand_nibble(&c->status[0], 
00969                     (src[0] >> 4) & 0x0F);
00970                 *samples++ = adpcm_ct_expand_nibble(&c->status[0], 
00971                     src[0] & 0x0F);
00972             }
00973             src++;
00974         }
00975         break;
00976     case CODEC_ID_ADPCM_SWF:
00977     {
00978         GetBitContext gb;
00979         const int *table;
00980         int k0, signmask;
00981         int size = buf_size*8;
00982         
00983         init_get_bits(&gb, buf, size);
00984 
00985         // first frame, read bits & inital values
00986         if (!c->nb_bits)
00987         {
00988             c->nb_bits = get_bits(&gb, 2)+2;
00989 //          av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
00990         }
00991         
00992         table = swf_index_tables[c->nb_bits-2];
00993         k0 = 1 << (c->nb_bits-2);
00994         signmask = 1 << (c->nb_bits-1);
00995         
00996         while (get_bits_count(&gb) <= size)
00997         {
00998             int i;
00999 
01000             c->nb_samples++;
01001             // wrap around at every 4096 samples...
01002             if ((c->nb_samples & 0xfff) == 1)
01003             {
01004                 for (i = 0; i <= st; i++)
01005                 {
01006                     *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
01007                     c->status[i].step_index = get_bits(&gb, 6);
01008                 }
01009             }
01010 
01011             // similar to IMA adpcm
01012             for (i = 0; i <= st; i++)
01013             {
01014                 int delta = get_bits(&gb, c->nb_bits);
01015                 int step = step_table[c->status[i].step_index];
01016                 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
01017                 int k = k0;
01018                 
01019                 do {
01020                     if (delta & k)
01021                         vpdiff += step;
01022                     step >>= 1;
01023                     k >>= 1;
01024                 } while(k);
01025                 vpdiff += step;
01026                 
01027                 if (delta & signmask)
01028                     c->status[i].predictor -= vpdiff;
01029                 else
01030                     c->status[i].predictor += vpdiff;
01031