00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #include "avcodec.h"
00021 #include "get_bits.h"
00022 #include "put_bits.h"
00023 #include "bytestream.h"
00024 #include "adpcm.h"
00025 #include "adpcm_data.h"
00026 
00059 
00060 static const int xa_adpcm_table[5][2] = {
00061     {   0,   0 },
00062     {  60,   0 },
00063     { 115, -52 },
00064     {  98, -55 },
00065     { 122, -60 }
00066 };
00067 
00068 static const int ea_adpcm_table[] = {
00069     0,  240,  460,  392,
00070     0,    0, -208, -220,
00071     0,    1,    3,    4,
00072     7,    8,   10,   11,
00073     0,   -1,   -3,   -4
00074 };
00075 
00076 
00077 static const int swf_index_tables[4][16] = {
00078      { -1, 2 },
00079      { -1, -1, 2, 4 },
00080      { -1, -1, -1, -1, 2, 4, 6, 8 },
00081      { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
00082 };
00083 
00084 
00085 
00086 typedef struct ADPCMDecodeContext {
00087     AVFrame frame;
00088     ADPCMChannelStatus status[6];
00089     int vqa_version;                
00090 } ADPCMDecodeContext;
00091 
00092 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
00093 {
00094     ADPCMDecodeContext *c = avctx->priv_data;
00095     unsigned int min_channels = 1;
00096     unsigned int max_channels = 2;
00097 
00098     switch(avctx->codec->id) {
00099     case AV_CODEC_ID_ADPCM_EA:
00100         min_channels = 2;
00101         break;
00102     case AV_CODEC_ID_ADPCM_EA_R1:
00103     case AV_CODEC_ID_ADPCM_EA_R2:
00104     case AV_CODEC_ID_ADPCM_EA_R3:
00105     case AV_CODEC_ID_ADPCM_EA_XAS:
00106         max_channels = 6;
00107         break;
00108     }
00109     if (avctx->channels < min_channels || avctx->channels > max_channels) {
00110         av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
00111         return AVERROR(EINVAL);
00112     }
00113 
00114     switch(avctx->codec->id) {
00115     case AV_CODEC_ID_ADPCM_CT:
00116         c->status[0].step = c->status[1].step = 511;
00117         break;
00118     case AV_CODEC_ID_ADPCM_IMA_WAV:
00119         if (avctx->bits_per_coded_sample != 4) {
00120             av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
00121             return -1;
00122         }
00123         break;
00124     case AV_CODEC_ID_ADPCM_IMA_APC:
00125         if (avctx->extradata && avctx->extradata_size >= 8) {
00126             c->status[0].predictor = AV_RL32(avctx->extradata);
00127             c->status[1].predictor = AV_RL32(avctx->extradata + 4);
00128         }
00129         break;
00130     case AV_CODEC_ID_ADPCM_IMA_WS:
00131         if (avctx->extradata && avctx->extradata_size >= 2)
00132             c->vqa_version = AV_RL16(avctx->extradata);
00133         break;
00134     default:
00135         break;
00136     }
00137     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00138 
00139     avcodec_get_frame_defaults(&c->frame);
00140     avctx->coded_frame = &c->frame;
00141 
00142     return 0;
00143 }
00144 
00145 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
00146 {
00147     int step_index;
00148     int predictor;
00149     int sign, delta, diff, step;
00150 
00151     step = ff_adpcm_step_table[c->step_index];
00152     step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
00153     step_index = av_clip(step_index, 0, 88);
00154 
00155     sign = nibble & 8;
00156     delta = nibble & 7;
00157     
00158 
00159 
00160     diff = ((2 * delta + 1) * step) >> shift;
00161     predictor = c->predictor;
00162     if (sign) predictor -= diff;
00163     else predictor += diff;
00164 
00165     c->predictor = av_clip_int16(predictor);
00166     c->step_index = step_index;
00167 
00168     return (short)c->predictor;
00169 }
00170 
00171 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
00172 {
00173     int step_index;
00174     int predictor;
00175     int diff, step;
00176 
00177     step = ff_adpcm_step_table[c->step_index];
00178     step_index = c->step_index + ff_adpcm_index_table[nibble];
00179     step_index = av_clip(step_index, 0, 88);
00180 
00181     diff = step >> 3;
00182     if (nibble & 4) diff += step;
00183     if (nibble & 2) diff += step >> 1;
00184     if (nibble & 1) diff += step >> 2;
00185 
00186     if (nibble & 8)
00187         predictor = c->predictor - diff;
00188     else
00189         predictor = c->predictor + diff;
00190 
00191     c->predictor = av_clip_int16(predictor);
00192     c->step_index = step_index;
00193 
00194     return c->predictor;
00195 }
00196 
00197 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
00198 {
00199     int predictor;
00200 
00201     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
00202     predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
00203 
00204     c->sample2 = c->sample1;
00205     c->sample1 = av_clip_int16(predictor);
00206     c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
00207     if (c->idelta < 16) c->idelta = 16;
00208 
00209     return c->sample1;
00210 }
00211 
00212 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
00213 {
00214     int sign, delta, diff;
00215     int new_step;
00216 
00217     sign = nibble & 8;
00218     delta = nibble & 7;
00219     
00220 
00221 
00222     diff = ((2 * delta + 1) * c->step) >> 3;
00223     
00224     c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
00225     c->predictor = av_clip_int16(c->predictor);
00226     
00227     new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
00228     c->step = av_clip(new_step, 511, 32767);
00229 
00230     return (short)c->predictor;
00231 }
00232 
00233 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
00234 {
00235     int sign, delta, diff;
00236 
00237     sign = nibble & (1<<(size-1));
00238     delta = nibble & ((1<<(size-1))-1);
00239     diff = delta << (7 + c->step + shift);
00240 
00241     
00242     c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
00243 
00244     
00245     if (delta >= (2*size - 3) && c->step < 3)
00246         c->step++;
00247     else if (delta == 0 && c->step > 0)
00248         c->step--;
00249 
00250     return (short) c->predictor;
00251 }
00252 
00253 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
00254 {
00255     if(!c->step) {
00256         c->predictor = 0;
00257         c->step = 127;
00258     }
00259 
00260     c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
00261     c->predictor = av_clip_int16(c->predictor);
00262     c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
00263     c->step = av_clip(c->step, 127, 24567);
00264     return c->predictor;
00265 }
00266 
00267 static int xa_decode(AVCodecContext *avctx,
00268                      short *out, const unsigned char *in,
00269                      ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
00270 {
00271     int i, j;
00272     int shift,filter,f0,f1;
00273     int s_1,s_2;
00274     int d,s,t;
00275 
00276     for(i=0;i<4;i++) {
00277 
00278         shift  = 12 - (in[4+i*2] & 15);
00279         filter = in[4+i*2] >> 4;
00280         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
00281             av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
00282             filter=0;
00283         }
00284         f0 = xa_adpcm_table[filter][0];
00285         f1 = xa_adpcm_table[filter][1];
00286 
00287         s_1 = left->sample1;
00288         s_2 = left->sample2;
00289 
00290         for(j=0;j<28;j++) {
00291             d = in[16+i+j*4];
00292 
00293             t = sign_extend(d, 4);
00294             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
00295             s_2 = s_1;
00296             s_1 = av_clip_int16(s);
00297             *out = s_1;
00298             out += inc;
00299         }
00300 
00301         if (inc==2) { 
00302             left->sample1 = s_1;
00303             left->sample2 = s_2;
00304             s_1 = right->sample1;
00305             s_2 = right->sample2;
00306             out = out + 1 - 28*2;
00307         }
00308 
00309         shift  = 12 - (in[5+i*2] & 15);
00310         filter = in[5+i*2] >> 4;
00311         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
00312             av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
00313             filter=0;
00314         }
00315 
00316         f0 = xa_adpcm_table[filter][0];
00317         f1 = xa_adpcm_table[filter][1];
00318 
00319         for(j=0;j<28;j++) {
00320             d = in[16+i+j*4];
00321 
00322             t = sign_extend(d >> 4, 4);
00323             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
00324             s_2 = s_1;
00325             s_1 = av_clip_int16(s);
00326             *out = s_1;
00327             out += inc;
00328         }
00329 
00330         if (inc==2) { 
00331             right->sample1 = s_1;
00332             right->sample2 = s_2;
00333             out -= 1;
00334         } else {
00335             left->sample1 = s_1;
00336             left->sample2 = s_2;
00337         }
00338     }
00339 
00340     return 0;
00341 }
00342 
00343 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
00344 {
00345     ADPCMDecodeContext *c = avctx->priv_data;
00346     GetBitContext gb;
00347     const int *table;
00348     int k0, signmask, nb_bits, count;
00349     int size = buf_size*8;
00350     int i;
00351 
00352     init_get_bits(&gb, buf, size);
00353 
00354     
00355     nb_bits = get_bits(&gb, 2)+2;
00356     
00357     table = swf_index_tables[nb_bits-2];
00358     k0 = 1 << (nb_bits-2);
00359     signmask = 1 << (nb_bits-1);
00360 
00361     while (get_bits_count(&gb) <= size - 22*avctx->channels) {
00362         for (i = 0; i < avctx->channels; i++) {
00363             *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
00364             c->status[i].step_index = get_bits(&gb, 6);
00365         }
00366 
00367         for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
00368             int i;
00369 
00370             for (i = 0; i < avctx->channels; i++) {
00371                 
00372                 int delta = get_bits(&gb, nb_bits);
00373                 int step = ff_adpcm_step_table[c->status[i].step_index];
00374                 long vpdiff = 0; 
00375                 int k = k0;
00376 
00377                 do {
00378                     if (delta & k)
00379                         vpdiff += step;
00380                     step >>= 1;
00381                     k >>= 1;
00382                 } while(k);
00383                 vpdiff += step;
00384 
00385                 if (delta & signmask)
00386                     c->status[i].predictor -= vpdiff;
00387                 else
00388                     c->status[i].predictor += vpdiff;
00389 
00390                 c->status[i].step_index += table[delta & (~signmask)];
00391 
00392                 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
00393                 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
00394 
00395                 *samples++ = c->status[i].predictor;
00396             }
00397         }
00398     }
00399 }
00400 
00410 static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
00411                           int buf_size, int *coded_samples)
00412 {
00413     ADPCMDecodeContext *s = avctx->priv_data;
00414     int nb_samples        = 0;
00415     int ch                = avctx->channels;
00416     int has_coded_samples = 0;
00417     int header_size;
00418 
00419     *coded_samples = 0;
00420 
00421     if(ch <= 0)
00422         return 0;
00423 
00424     switch (avctx->codec->id) {
00425     
00426     case AV_CODEC_ID_ADPCM_EA_XAS:
00427         if (buf_size < 76 * ch)
00428             return 0;
00429         nb_samples = 128;
00430         break;
00431     case AV_CODEC_ID_ADPCM_IMA_QT:
00432         if (buf_size < 34 * ch)
00433             return 0;
00434         nb_samples = 64;
00435         break;
00436     
00437     case AV_CODEC_ID_ADPCM_CT:
00438     case AV_CODEC_ID_ADPCM_IMA_APC:
00439     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
00440     case AV_CODEC_ID_ADPCM_IMA_WS:
00441     case AV_CODEC_ID_ADPCM_YAMAHA:
00442         nb_samples = buf_size * 2 / ch;
00443         break;
00444     }
00445     if (nb_samples)
00446         return nb_samples;
00447 
00448     
00449     header_size = 0;
00450     switch (avctx->codec->id) {
00451         case AV_CODEC_ID_ADPCM_4XM:
00452         case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
00453         case AV_CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
00454         case AV_CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4;           break;
00455     }
00456     if (header_size > 0)
00457         return (buf_size - header_size) * 2 / ch;
00458 
00459     
00460     switch (avctx->codec->id) {
00461     case AV_CODEC_ID_ADPCM_EA:
00462         has_coded_samples = 1;
00463         *coded_samples  = bytestream2_get_le32(gb);
00464         *coded_samples -= *coded_samples % 28;
00465         nb_samples      = (buf_size - 12) / 30 * 28;
00466         break;
00467     case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
00468         has_coded_samples = 1;
00469         *coded_samples = bytestream2_get_le32(gb);
00470         nb_samples     = (buf_size - (4 + 8 * ch)) * 2 / ch;
00471         break;
00472     case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
00473         nb_samples = (buf_size - ch) / ch * 2;
00474         break;
00475     case AV_CODEC_ID_ADPCM_EA_R1:
00476     case AV_CODEC_ID_ADPCM_EA_R2:
00477     case AV_CODEC_ID_ADPCM_EA_R3:
00478         
00479         
00480         has_coded_samples = 1;
00481         switch (avctx->codec->id) {
00482         case AV_CODEC_ID_ADPCM_EA_R1:
00483             header_size    = 4 + 9 * ch;
00484             *coded_samples = bytestream2_get_le32(gb);
00485             break;
00486         case AV_CODEC_ID_ADPCM_EA_R2:
00487             header_size    = 4 + 5 * ch;
00488             *coded_samples = bytestream2_get_le32(gb);
00489             break;
00490         case AV_CODEC_ID_ADPCM_EA_R3:
00491             header_size    = 4 + 5 * ch;
00492             *coded_samples = bytestream2_get_be32(gb);
00493             break;
00494         }
00495         *coded_samples -= *coded_samples % 28;
00496         nb_samples      = (buf_size - header_size) * 2 / ch;
00497         nb_samples     -= nb_samples % 28;
00498         break;
00499     case AV_CODEC_ID_ADPCM_IMA_DK3:
00500         if (avctx->block_align > 0)
00501             buf_size = FFMIN(buf_size, avctx->block_align);
00502         nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
00503         break;
00504     case AV_CODEC_ID_ADPCM_IMA_DK4:
00505         if (avctx->block_align > 0)
00506             buf_size = FFMIN(buf_size, avctx->block_align);
00507         nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
00508         break;
00509     case AV_CODEC_ID_ADPCM_IMA_WAV:
00510         if (avctx->block_align > 0)
00511             buf_size = FFMIN(buf_size, avctx->block_align);
00512         nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
00513         break;
00514     case AV_CODEC_ID_ADPCM_MS:
00515         if (avctx->block_align > 0)
00516             buf_size = FFMIN(buf_size, avctx->block_align);
00517         nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
00518         break;
00519     case AV_CODEC_ID_ADPCM_SBPRO_2:
00520     case AV_CODEC_ID_ADPCM_SBPRO_3:
00521     case AV_CODEC_ID_ADPCM_SBPRO_4:
00522     {
00523         int samples_per_byte;
00524         switch (avctx->codec->id) {
00525         case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
00526         case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
00527         case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
00528         }
00529         if (!s->status[0].step_index) {
00530             nb_samples++;
00531             buf_size -= ch;
00532         }
00533         nb_samples += buf_size * samples_per_byte / ch;
00534         break;
00535     }
00536     case AV_CODEC_ID_ADPCM_SWF:
00537     {
00538         int buf_bits       = buf_size * 8 - 2;
00539         int nbits          = (bytestream2_get_byte(gb) >> 6) + 2;
00540         int block_hdr_size = 22 * ch;
00541         int block_size     = block_hdr_size + nbits * ch * 4095;
00542         int nblocks        = buf_bits / block_size;
00543         int bits_left      = buf_bits - nblocks * block_size;
00544         nb_samples         = nblocks * 4096;
00545         if (bits_left >= block_hdr_size)
00546             nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
00547         break;
00548     }
00549     case AV_CODEC_ID_ADPCM_THP:
00550         has_coded_samples = 1;
00551         bytestream2_skip(gb, 4); 
00552         *coded_samples  = bytestream2_get_be32(gb);
00553         *coded_samples -= *coded_samples % 14;
00554         nb_samples      = (buf_size - 80) / (8 * ch) * 14;
00555         break;
00556     case AV_CODEC_ID_ADPCM_XA:
00557         nb_samples = (buf_size / 128) * 224 / ch;
00558         break;
00559     }
00560 
00561     
00562     if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
00563         return AVERROR_INVALIDDATA;
00564 
00565     return nb_samples;
00566 }
00567 
00568 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
00569                               int *got_frame_ptr, AVPacket *avpkt)
00570 {
00571     const uint8_t *buf = avpkt->data;
00572     int buf_size = avpkt->size;
00573     ADPCMDecodeContext *c = avctx->priv_data;
00574     ADPCMChannelStatus *cs;
00575     int n, m, channel, i;
00576     short *samples;
00577     int st; 
00578     int count1, count2;
00579     int nb_samples, coded_samples, ret;
00580     GetByteContext gb;
00581 
00582     bytestream2_init(&gb, buf, buf_size);
00583     nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
00584     if (nb_samples <= 0) {
00585         av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
00586         return AVERROR_INVALIDDATA;
00587     }
00588 
00589     
00590     c->frame.nb_samples = nb_samples;
00591     if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
00592         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00593         return ret;
00594     }
00595     samples = (short *)c->frame.data[0];
00596 
00597     
00598     
00599     if (coded_samples) {
00600         if (coded_samples != nb_samples)
00601             av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
00602         c->frame.nb_samples = nb_samples = coded_samples;
00603     }
00604 
00605     st = avctx->channels == 2 ? 1 : 0;
00606 
00607     switch(avctx->codec->id) {
00608     case AV_CODEC_ID_ADPCM_IMA_QT:
00609         
00610 
00611         for (channel = 0; channel < avctx->channels; channel++) {
00612             int predictor;
00613             int step_index;
00614             cs = &(c->status[channel]);
00615             
00616 
00617             
00618             predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
00619             step_index = predictor & 0x7F;
00620             predictor &= ~0x7F;
00621 
00622             if (cs->step_index == step_index) {
00623                 int diff = predictor - cs->predictor;
00624                 if (diff < 0)
00625                     diff = - diff;
00626                 if (diff > 0x7f)
00627                     goto update;
00628             } else {
00629             update:
00630                 cs->step_index = step_index;
00631                 cs->predictor = predictor;
00632             }
00633 
00634             if (cs->step_index > 88u){
00635                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
00636                        channel, cs->step_index);
00637                 return AVERROR_INVALIDDATA;
00638             }
00639 
00640             samples = (short *)c->frame.data[0] + channel;
00641 
00642             for (m = 0; m < 32; m++) {
00643                 int byte = bytestream2_get_byteu(&gb);
00644                 *samples = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
00645                 samples += avctx->channels;
00646                 *samples = adpcm_ima_qt_expand_nibble(cs, byte >> 4  , 3);
00647                 samples += avctx->channels;
00648             }
00649         }
00650         break;
00651     case AV_CODEC_ID_ADPCM_IMA_WAV:
00652         for(i=0; i<avctx->channels; i++){
00653             cs = &(c->status[i]);
00654             cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
00655 
00656             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
00657             if (cs->step_index > 88u){
00658                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
00659                        i, cs->step_index);
00660                 return AVERROR_INVALIDDATA;
00661             }
00662         }
00663 
00664         for (n = (nb_samples - 1) / 8; n > 0; n--) {
00665             for (i = 0; i < avctx->channels; i++) {
00666                 cs = &c->status[i];
00667                 for (m = 0; m < 4; m++) {
00668                     int v = bytestream2_get_byteu(&gb);
00669                     *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
00670                     samples += avctx->channels;
00671                     *samples = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
00672                     samples += avctx->channels;
00673                 }
00674                 samples -= 8 * avctx->channels - 1;
00675             }
00676             samples += 7 * avctx->channels;
00677         }
00678         break;
00679     case AV_CODEC_ID_ADPCM_4XM:
00680         for (i = 0; i < avctx->channels; i++)
00681             c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
00682 
00683         for (i = 0; i < avctx->channels; i++) {
00684             c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
00685             if (c->status[i].step_index > 88u) {
00686                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
00687                        i, c->status[i].step_index);
00688                 return AVERROR_INVALIDDATA;
00689             }
00690         }
00691 
00692         for (i = 0; i < avctx->channels; i++) {
00693             samples = (short *)c->frame.data[0] + i;
00694             cs = &c->status[i];
00695             for (n = nb_samples >> 1; n > 0; n--) {
00696                 int v = bytestream2_get_byteu(&gb);
00697                 *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
00698                 samples += avctx->channels;
00699                 *samples = adpcm_ima_expand_nibble(cs, v >> 4  , 4);
00700                 samples += avctx->channels;
00701             }
00702         }
00703         break;
00704     case AV_CODEC_ID_ADPCM_MS:
00705     {
00706         int block_predictor;
00707 
00708         block_predictor = bytestream2_get_byteu(&gb);
00709         if (block_predictor > 6) {
00710             av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
00711                    block_predictor);
00712             return AVERROR_INVALIDDATA;
00713         }
00714         c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
00715         c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
00716         if (st) {
00717             block_predictor = bytestream2_get_byteu(&gb);
00718             if (block_predictor > 6) {
00719                 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
00720                        block_predictor);
00721                 return AVERROR_INVALIDDATA;
00722             }
00723             c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
00724             c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
00725         }
00726         c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
00727         if (st){
00728             c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
00729         }
00730 
00731         c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
00732         if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
00733         c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
00734         if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
00735 
00736         *samples++ = c->status[0].sample2;
00737         if (st) *samples++ = c->status[1].sample2;
00738         *samples++ = c->status[0].sample1;
00739         if (st) *samples++ = c->status[1].sample1;
00740         for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
00741             int byte = bytestream2_get_byteu(&gb);
00742             *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4  );
00743             *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
00744         }
00745         break;
00746     }
00747     case AV_CODEC_ID_ADPCM_IMA_DK4:
00748         for (channel = 0; channel < avctx->channels; channel++) {
00749             cs = &c->status[channel];
00750             cs->predictor  = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
00751             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
00752             if (cs->step_index > 88u){
00753                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
00754                        channel, cs->step_index);
00755                 return AVERROR_INVALIDDATA;
00756             }
00757         }
00758         for (n = nb_samples >> (1 - st); n > 0; n--) {
00759             int v = bytestream2_get_byteu(&gb);
00760             *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4  , 3);
00761             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
00762         }
00763         break;
00764     case AV_CODEC_ID_ADPCM_IMA_DK3:
00765     {
00766         int last_byte = 0;
00767         int nibble;
00768         int decode_top_nibble_next = 0;
00769         int diff_channel;
00770         const int16_t *samples_end = samples + avctx->channels * nb_samples;
00771 
00772         bytestream2_skipu(&gb, 10);
00773         c->status[0].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
00774         c->status[1].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
00775         c->status[0].step_index = bytestream2_get_byteu(&gb);
00776         c->status[1].step_index = bytestream2_get_byteu(&gb);
00777         if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
00778             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
00779                    c->status[0].step_index, c->status[1].step_index);
00780             return AVERROR_INVALIDDATA;
00781         }
00782         
00783         diff_channel = c->status[1].predictor;
00784 
00785         
00786 #define DK3_GET_NEXT_NIBBLE() \
00787     if (decode_top_nibble_next) { \
00788         nibble = last_byte >> 4; \
00789         decode_top_nibble_next = 0; \
00790     } else { \
00791         last_byte = bytestream2_get_byteu(&gb); \
00792         nibble = last_byte & 0x0F; \
00793         decode_top_nibble_next = 1; \
00794     }
00795 
00796         while (samples < samples_end) {
00797 
00798             
00799 
00800 
00801             
00802             DK3_GET_NEXT_NIBBLE();
00803             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
00804 
00805             
00806             DK3_GET_NEXT_NIBBLE();
00807             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
00808 
00809             
00810             diff_channel = (diff_channel + c->status[1].predictor) / 2;
00811             *samples++ = c->status[0].predictor + c->status[1].predictor;
00812             *samples++ = c->status[0].predictor - c->status[1].predictor;
00813 
00814             
00815             DK3_GET_NEXT_NIBBLE();
00816             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
00817 
00818             
00819             diff_channel = (diff_channel + c->status[1].predictor) / 2;
00820             *samples++ = c->status[0].predictor + c->status[1].predictor;
00821             *samples++ = c->status[0].predictor - c->status[1].predictor;
00822         }
00823         break;
00824     }
00825     case AV_CODEC_ID_ADPCM_IMA_ISS:
00826         for (channel = 0; channel < avctx->channels; channel++) {
00827             cs = &c->status[channel];
00828             cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
00829             cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
00830             if (cs->step_index > 88u){
00831                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
00832                        channel, cs->step_index);
00833                 return AVERROR_INVALIDDATA;
00834             }
00835         }
00836 
00837         for (n = nb_samples >> (1 - st); n > 0; n--) {
00838             int v1, v2;
00839             int v = bytestream2_get_byteu(&gb);
00840             
00841             if (st) {
00842                 v1 = v >> 4;
00843                 v2 = v & 0x0F;
00844             } else {
00845                 v2 = v >> 4;
00846                 v1 = v & 0x0F;
00847             }
00848             *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
00849             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
00850         }
00851         break;
00852     case AV_CODEC_ID_ADPCM_IMA_APC:
00853         while (bytestream2_get_bytes_left(&gb) > 0) {
00854             int v = bytestream2_get_byteu(&gb);
00855             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  v >> 4  , 3);
00856             *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
00857         }
00858         break;
00859     case AV_CODEC_ID_ADPCM_IMA_WS:
00860         if (c->vqa_version == 3) {
00861             for (channel = 0; channel < avctx->channels; channel++) {
00862                 int16_t *smp = samples + channel;
00863 
00864                 for (n = nb_samples / 2; n > 0; n--) {
00865                     int v = bytestream2_get_byteu(&gb);
00866                     *smp = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
00867                     smp += avctx->channels;
00868                     *smp = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
00869                     smp += avctx->channels;
00870                 }
00871             }
00872         } else {
00873             for (n = nb_samples / 2; n > 0; n--) {
00874                 for (channel = 0; channel < avctx->channels; channel++) {
00875                     int v = bytestream2_get_byteu(&gb);
00876                     *samples++  = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
00877                     samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
00878                 }
00879                 samples += avctx->channels;
00880             }
00881         }
00882         bytestream2_seek(&gb, 0, SEEK_END);
00883         break;
00884     case AV_CODEC_ID_ADPCM_XA:
00885         while (bytestream2_get_bytes_left(&gb) >= 128) {
00886             if ((ret = xa_decode(avctx, samples, buf + bytestream2_tell(&gb), &c->status[0],
00887                                  &c->status[1], avctx->channels)) < 0)
00888                 return ret;
00889             bytestream2_skipu(&gb, 128);
00890             samples += 28 * 8;
00891         }
00892         break;
00893     case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
00894         for (i=0; i<=st; i++) {
00895             c->status[i].step_index = bytestream2_get_le32u(&gb);
00896             if (c->status[i].step_index > 88u) {
00897                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
00898                        i, c->status[i].step_index);
00899                 return AVERROR_INVALIDDATA;
00900             }
00901         }
00902         for (i=0; i<=st; i++)
00903             c->status[i].predictor  = bytestream2_get_le32u(&gb);
00904 
00905         for (n = nb_samples >> (1 - st); n > 0; n--) {
00906             int byte   = bytestream2_get_byteu(&gb);
00907             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   3);
00908             *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
00909         }
00910         break;
00911     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
00912         for (n = nb_samples >> (1 - st); n > 0; n--) {
00913             int byte = bytestream2_get_byteu(&gb);
00914             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   6);
00915             *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
00916         }
00917         break;
00918     case AV_CODEC_ID_ADPCM_EA:
00919     {
00920         int previous_left_sample, previous_right_sample;
00921         int current_left_sample, current_right_sample;
00922         int next_left_sample, next_right_sample;
00923         int coeff1l, coeff2l, coeff1r, coeff2r;
00924         int shift_left, shift_right;
00925 
00926         
00927 
00928 
00929         if(avctx->channels != 2)
00930             return AVERROR_INVALIDDATA;
00931 
00932         current_left_sample   = sign_extend(bytestream2_get_le16u(&gb), 16);
00933         previous_left_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
00934         current_right_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
00935         previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
00936 
00937         for (count1 = 0; count1 < nb_samples / 28; count1++) {
00938             int byte = bytestream2_get_byteu(&gb);
00939             coeff1l = ea_adpcm_table[ byte >> 4       ];
00940             coeff2l = ea_adpcm_table[(byte >> 4  ) + 4];
00941             coeff1r = ea_adpcm_table[ byte & 0x0F];
00942             coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
00943 
00944             byte = bytestream2_get_byteu(&gb);
00945             shift_left  = 20 - (byte >> 4);
00946             shift_right = 20 - (byte & 0x0F);
00947 
00948             for (count2 = 0; count2 < 28; count2++) {
00949                 byte = bytestream2_get_byteu(&gb);
00950                 next_left_sample  = sign_extend(byte >> 4, 4) << shift_left;
00951                 next_right_sample = sign_extend(byte,      4) << shift_right;
00952 
00953                 next_left_sample = (next_left_sample +
00954                     (current_left_sample * coeff1l) +
00955                     (previous_left_sample * coeff2l) + 0x80) >> 8;
00956                 next_right_sample = (next_right_sample +
00957                     (current_right_sample * coeff1r) +
00958                     (previous_right_sample * coeff2r) + 0x80) >> 8;
00959 
00960                 previous_left_sample = current_left_sample;
00961                 current_left_sample = av_clip_int16(next_left_sample);
00962                 previous_right_sample = current_right_sample;
00963                 current_right_sample = av_clip_int16(next_right_sample);
00964                 *samples++ = current_left_sample;
00965                 *samples++ = current_right_sample;
00966             }
00967         }
00968 
00969         bytestream2_skip(&gb, 2); 
00970 
00971         break;
00972     }
00973     case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
00974     {
00975         int coeff[2][2], shift[2];
00976 
00977         for(channel = 0; channel < avctx->channels; channel++) {
00978             int byte = bytestream2_get_byteu(&gb);
00979             for (i=0; i<2; i++)
00980                 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
00981             shift[channel] = 20 - (byte & 0x0F);
00982         }
00983         for (count1 = 0; count1 < nb_samples / 2; count1++) {
00984             int byte[2];
00985 
00986             byte[0] = bytestream2_get_byteu(&gb);
00987             if (st) byte[1] = bytestream2_get_byteu(&gb);
00988             for(i = 4; i >= 0; i-=4) { 
00989                 for(channel = 0; channel < avctx->channels; channel++) {
00990                     int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
00991                     sample = (sample +
00992                              c->status[channel].sample1 * coeff[channel][0] +
00993                              c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
00994                     c->status[channel].sample2 = c->status[channel].sample1;
00995                     c->status[channel].sample1 = av_clip_int16(sample);
00996                     *samples++ = c->status[channel].sample1;
00997                 }
00998             }
00999         }
01000         bytestream2_seek(&gb, 0, SEEK_END);
01001         break;
01002     }
01003     case AV_CODEC_ID_ADPCM_EA_R1:
01004     case AV_CODEC_ID_ADPCM_EA_R2:
01005     case AV_CODEC_ID_ADPCM_EA_R3: {
01006         
01007 
01008 
01009 
01010         const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
01011         int previous_sample, current_sample, next_sample;
01012         int coeff1, coeff2;
01013         int shift;
01014         unsigned int channel;
01015         uint16_t *samplesC;
01016         int count = 0;
01017         int offsets[6];
01018 
01019         for (channel=0; channel<avctx->channels; channel++)
01020             offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
01021                                              bytestream2_get_le32(&gb)) +
01022                                (avctx->channels + 1) * 4;
01023 
01024         for (channel=0; channel<avctx->channels; channel++) {
01025             bytestream2_seek(&gb, offsets[channel], SEEK_SET);
01026             samplesC = samples + channel;
01027 
01028             if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
01029                 current_sample  = sign_extend(bytestream2_get_le16(&gb), 16);
01030                 previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
01031             } else {
01032                 current_sample  = c->status[channel].predictor;
01033                 previous_sample = c->status[channel].prev_sample;
01034             }
01035 
01036             for (count1 = 0; count1 < nb_samples / 28; count1++) {
01037                 int byte = bytestream2_get_byte(&gb);
01038                 if (byte == 0xEE) {  
01039                     current_sample  = sign_extend(bytestream2_get_be16(&gb), 16);
01040                     previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
01041 
01042                     for (count2=0; count2<28; count2++) {
01043                         *samplesC = sign_extend(bytestream2_get_be16(&gb), 16);
01044                         samplesC += avctx->channels;
01045                     }
01046                 } else {
01047                     coeff1 = ea_adpcm_table[ byte >> 4     ];
01048                     coeff2 = ea_adpcm_table[(byte >> 4) + 4];
01049                     shift = 20 - (byte & 0x0F);
01050 
01051                     for (count2=0; count2<28; count2++) {
01052                         if (count2 & 1)
01053                             next_sample = sign_extend(byte,    4) << shift;
01054                         else {
01055                             byte = bytestream2_get_byte(&gb);
01056                             next_sample = sign_extend(byte >> 4, 4) << shift;
01057                         }
01058 
01059                         next_sample += (current_sample  * coeff1) +
01060                                        (previous_sample * coeff2);
01061                         next_sample = av_clip_int16(next_sample >> 8);
01062 
01063                         previous_sample = current_sample;
01064                         current_sample  = next_sample;
01065                         *samplesC = current_sample;
01066                         samplesC += avctx->channels;
01067                     }
01068                 }
01069             }
01070             if (!count) {
01071                 count = count1;
01072             } else if (count != count1) {
01073                 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
01074                 count = FFMAX(count, count1);
01075             }
01076 
01077             if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
01078                 c->status[channel].predictor   = current_sample;
01079                 c->status[channel].prev_sample = previous_sample;
01080             }
01081         }
01082 
01083         c->frame.nb_samples = count * 28;
01084         bytestream2_seek(&gb, 0, SEEK_END);
01085         break;
01086     }
01087     case AV_CODEC_ID_ADPCM_EA_XAS:
01088         for (channel=0; channel<avctx->channels; channel++) {
01089             int coeff[2][4], shift[4];
01090             short *s2, *s = &samples[channel];
01091             for (n=0; n<4; n++, s+=32*avctx->channels) {
01092                 int val = sign_extend(bytestream2_get_le16u(&gb), 16);
01093                 for (i=0; i<2; i++)
01094                     coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
01095                 s[0] = val & ~0x0F;
01096 
01097                 val = sign_extend(bytestream2_get_le16u(&gb), 16);
01098                 shift[n] = 20 - (val & 0x0F);
01099                 s[avctx->channels] = val & ~0x0F;
01100             }
01101 
01102             for (m=2; m<32; m+=2) {
01103                 s = &samples[m*avctx->channels + channel];
01104                 for (n=0; n<4; n++, s+=32*avctx->channels) {
01105                     int byte = bytestream2_get_byteu(&gb);
01106                     for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
01107                         int level = sign_extend(byte >> (4 - i), 4) << shift[n];
01108                         int pred  = s2[-1*avctx->channels] * coeff[0][n]
01109                                   + s2[-2*avctx->channels] * coeff[1][n];
01110                         s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
01111                     }
01112                 }
01113             }
01114         }
01115         break;
01116     case AV_CODEC_ID_ADPCM_IMA_AMV:
01117     case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
01118         if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_AMV) {
01119             c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
01120             c->status[0].step_index = bytestream2_get_le16u(&gb);
01121             bytestream2_skipu(&gb, 4);
01122         } else {
01123             c->status[0].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
01124             c->status[0].step_index = bytestream2_get_byteu(&gb);
01125             bytestream2_skipu(&gb, 1);
01126         }
01127         if (c->status[0].step_index > 88u) {
01128             av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
01129                    c->status[0].step_index);
01130             return AVERROR_INVALIDDATA;
01131         }
01132 
01133         for (n = nb_samples >> (1 - st); n > 0; n--) {
01134             int hi, lo, v = bytestream2_get_byteu(&gb);
01135 
01136             if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_AMV) {
01137                 hi = v & 0x0F;
01138                 lo = v >> 4;
01139             } else {
01140                 lo = v & 0x0F;
01141                 hi = v >> 4;
01142             }
01143 
01144             *samples++ = adpcm_ima_expand_nibble(&c->status[0], lo, 3);
01145             *samples++ = adpcm_ima_expand_nibble(&c->status[0], hi, 3);
01146         }
01147         break;
01148     case AV_CODEC_ID_ADPCM_CT:
01149         for (n = nb_samples >> (1 - st); n > 0; n--) {
01150             int v = bytestream2_get_byteu(&gb);
01151             *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4  );
01152             *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
01153         }
01154         break;
01155     case AV_CODEC_ID_ADPCM_SBPRO_4:
01156     case AV_CODEC_ID_ADPCM_SBPRO_3:
01157     case AV_CODEC_ID_ADPCM_SBPRO_2:
01158         if (!c->status[0].step_index) {
01159             
01160             *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
01161             if (st)
01162                 *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
01163             c->status[0].step_index = 1;
01164             nb_samples--;
01165         }
01166         if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
01167             for (n = nb_samples >> (1 - st); n > 0; n--) {
01168                 int byte = bytestream2_get_byteu(&gb);
01169                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01170                                                        byte >> 4,   4, 0);
01171                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
01172                                                        byte & 0x0F, 4, 0);
01173             }
01174         } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
01175             for (n = nb_samples / 3; n > 0; n--) {
01176                 int byte = bytestream2_get_byteu(&gb);
01177                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01178                                                         byte >> 5        , 3, 0);
01179                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01180                                                        (byte >> 2) & 0x07, 3, 0);
01181                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01182                                                         byte & 0x03,       2, 0);
01183             }
01184         } else {
01185             for (n = nb_samples >> (2 - st); n > 0; n--) {
01186                 int byte = bytestream2_get_byteu(&gb);
01187                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01188                                                         byte >> 6        , 2, 2);
01189                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
01190                                                        (byte >> 4) & 0x03, 2, 2);
01191                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01192                                                        (byte >> 2) & 0x03, 2, 2);
01193                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
01194                                                         byte & 0x03,       2, 2);
01195             }
01196         }
01197         break;
01198     case AV_CODEC_ID_ADPCM_SWF:
01199         adpcm_swf_decode(avctx, buf, buf_size, samples);
01200         bytestream2_seek(&gb, 0, SEEK_END);
01201         break;
01202     case AV_CODEC_ID_ADPCM_YAMAHA:
01203         for (n = nb_samples >> (1 - st); n > 0; n--) {
01204             int v = bytestream2_get_byteu(&gb);
01205             *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
01206             *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4  );
01207         }
01208         break;
01209     case AV_CODEC_ID_ADPCM_THP:
01210     {
01211         int table[2][16];
01212         int prev[2][2];
01213         int ch;
01214 
01215         for (i = 0; i < 2; i++)
01216             for (n = 0; n < 16; n++)
01217                 table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
01218 
01219         
01220         for (i = 0; i < 2; i++)
01221             for (n = 0; n < 2; n++)
01222                 prev[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
01223 
01224         for (ch = 0; ch <= st; ch++) {
01225             samples = (short *)c->frame.data[0] + ch;
01226 
01227             
01228             for (i = 0; i < nb_samples / 14; i++) {
01229                 int byte = bytestream2_get_byteu(&gb);
01230                 int index = (byte >> 4) & 7;
01231                 unsigned int exp = byte & 0x0F;
01232                 int factor1 = table[ch][index * 2];
01233                 int factor2 = table[ch][index * 2 + 1];
01234 
01235                 
01236                 for (n = 0; n < 14; n++) {
01237                     int32_t sampledat;
01238 
01239                     if (n & 1) {
01240                         sampledat = sign_extend(byte, 4);
01241                     } else {
01242                         byte = bytestream2_get_byteu(&gb);
01243                         sampledat = sign_extend(byte >> 4, 4);
01244                     }
01245 
01246                     sampledat = ((prev[ch][0]*factor1
01247                                 + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
01248                     *samples = av_clip_int16(sampledat);
01249                     prev[ch][1] = prev[ch][0];
01250                     prev[ch][0] = *samples++;
01251 
01252                     
01253 
01254                     samples += st;
01255                 }
01256             }
01257         }
01258         break;
01259     }
01260 
01261     default:
01262         return -1;
01263     }
01264 
01265     *got_frame_ptr   = 1;
01266     *(AVFrame *)data = c->frame;
01267 
01268     return bytestream2_tell(&gb);
01269 }
01270 
01271 
01272 #define ADPCM_DECODER(id_, name_, long_name_)               \
01273 AVCodec ff_ ## name_ ## _decoder = {                        \
01274     .name           = #name_,                               \
01275     .type           = AVMEDIA_TYPE_AUDIO,                   \
01276     .id             = id_,                                  \
01277     .priv_data_size = sizeof(ADPCMDecodeContext),           \
01278     .init           = adpcm_decode_init,                    \
01279     .decode         = adpcm_decode_frame,                   \
01280     .capabilities   = CODEC_CAP_DR1,                        \
01281     .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
01282 }
01283 
01284 
01285 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
01286 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
01287 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
01288 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
01289 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
01290 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
01291 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
01292 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
01293 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
01294 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, adpcm_ima_apc, "ADPCM IMA CRYO APC");
01295 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
01296 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
01297 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
01298 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
01299 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
01300 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
01301 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
01302 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
01303 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
01304 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
01305 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
01306 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
01307 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
01308 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
01309 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
01310 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
01311 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");