Libav 0.7.1
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 file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "avcodec.h"
00022 #include "get_bits.h"
00023 #include "put_bits.h"
00024 #include "bytestream.h"
00025 
00057 #define BLKSIZE 1024
00058 
00059 /* step_table[] and index_table[] are from the ADPCM reference source */
00060 /* This is the index table: */
00061 static const int index_table[16] = {
00062     -1, -1, -1, -1, 2, 4, 6, 8,
00063     -1, -1, -1, -1, 2, 4, 6, 8,
00064 };
00065 
00070 static const int step_table[89] = {
00071     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
00072     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
00073     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
00074     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
00075     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
00076     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
00077     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
00078     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
00079     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
00080 };
00081 
00082 /* These are for MS-ADPCM */
00083 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
00084 static const int AdaptationTable[] = {
00085         230, 230, 230, 230, 307, 409, 512, 614,
00086         768, 614, 512, 409, 307, 230, 230, 230
00087 };
00088 
00090 static const uint8_t AdaptCoeff1[] = {
00091         64, 128, 0, 48, 60, 115, 98
00092 };
00093 
00095 static const int8_t AdaptCoeff2[] = {
00096         0, -64, 0, 16, 0, -52, -58
00097 };
00098 
00099 /* These are for CD-ROM XA ADPCM */
00100 static const int xa_adpcm_table[5][2] = {
00101    {   0,   0 },
00102    {  60,   0 },
00103    { 115, -52 },
00104    {  98, -55 },
00105    { 122, -60 }
00106 };
00107 
00108 static const int ea_adpcm_table[] = {
00109     0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
00110     3, 4, 7, 8, 10, 11, 0, -1, -3, -4
00111 };
00112 
00113 // padded to zero where table size is less then 16
00114 static const int swf_index_tables[4][16] = {
00115     /*2*/ { -1, 2 },
00116     /*3*/ { -1, -1, 2, 4 },
00117     /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
00118     /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
00119 };
00120 
00121 static const int yamaha_indexscale[] = {
00122     230, 230, 230, 230, 307, 409, 512, 614,
00123     230, 230, 230, 230, 307, 409, 512, 614
00124 };
00125 
00126 static const int yamaha_difflookup[] = {
00127     1, 3, 5, 7, 9, 11, 13, 15,
00128     -1, -3, -5, -7, -9, -11, -13, -15
00129 };
00130 
00131 /* end of tables */
00132 
00133 typedef struct ADPCMChannelStatus {
00134     int predictor;
00135     short int step_index;
00136     int step;
00137     /* for encoding */
00138     int prev_sample;
00139 
00140     /* MS version */
00141     short sample1;
00142     short sample2;
00143     int coeff1;
00144     int coeff2;
00145     int idelta;
00146 } ADPCMChannelStatus;
00147 
00148 typedef struct TrellisPath {
00149     int nibble;
00150     int prev;
00151 } TrellisPath;
00152 
00153 typedef struct TrellisNode {
00154     uint32_t ssd;
00155     int path;
00156     int sample1;
00157     int sample2;
00158     int step;
00159 } TrellisNode;
00160 
00161 typedef struct ADPCMContext {
00162     ADPCMChannelStatus status[6];
00163     TrellisPath *paths;
00164     TrellisNode *node_buf;
00165     TrellisNode **nodep_buf;
00166     uint8_t *trellis_hash;
00167 } ADPCMContext;
00168 
00169 #define FREEZE_INTERVAL 128
00170 
00171 /* XXX: implement encoding */
00172 
00173 #if CONFIG_ENCODERS
00174 static av_cold int adpcm_encode_init(AVCodecContext *avctx)
00175 {
00176     ADPCMContext *s = avctx->priv_data;
00177     uint8_t *extradata;
00178     int i;
00179     if (avctx->channels > 2)
00180         return -1; /* only stereo or mono =) */
00181 
00182     if(avctx->trellis && (unsigned)avctx->trellis > 16U){
00183         av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
00184         return -1;
00185     }
00186 
00187     if (avctx->trellis) {
00188         int frontier = 1 << avctx->trellis;
00189         int max_paths =  frontier * FREEZE_INTERVAL;
00190         FF_ALLOC_OR_GOTO(avctx, s->paths,     max_paths * sizeof(*s->paths), error);
00191         FF_ALLOC_OR_GOTO(avctx, s->node_buf,  2 * frontier * sizeof(*s->node_buf), error);
00192         FF_ALLOC_OR_GOTO(avctx, s->nodep_buf, 2 * frontier * sizeof(*s->nodep_buf), error);
00193         FF_ALLOC_OR_GOTO(avctx, s->trellis_hash, 65536 * sizeof(*s->trellis_hash), error);
00194     }
00195 
00196     switch(avctx->codec->id) {
00197     case CODEC_ID_ADPCM_IMA_WAV:
00198         avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
00199                                                              /* and we have 4 bytes per channel overhead */
00200         avctx->block_align = BLKSIZE;
00201         /* seems frame_size isn't taken into account... have to buffer the samples :-( */
00202         break;
00203     case CODEC_ID_ADPCM_IMA_QT:
00204         avctx->frame_size = 64;
00205         avctx->block_align = 34 * avctx->channels;
00206         break;
00207     case CODEC_ID_ADPCM_MS:
00208         avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
00209                                                              /* and we have 7 bytes per channel overhead */
00210         avctx->block_align = BLKSIZE;
00211         avctx->extradata_size = 32;
00212         extradata = avctx->extradata = av_malloc(avctx->extradata_size);
00213         if (!extradata)
00214             return AVERROR(ENOMEM);
00215         bytestream_put_le16(&extradata, avctx->frame_size);
00216         bytestream_put_le16(&extradata, 7); /* wNumCoef */
00217         for (i = 0; i < 7; i++) {
00218             bytestream_put_le16(&extradata, AdaptCoeff1[i] * 4);
00219             bytestream_put_le16(&extradata, AdaptCoeff2[i] * 4);
00220         }
00221         break;
00222     case CODEC_ID_ADPCM_YAMAHA:
00223         avctx->frame_size = BLKSIZE * avctx->channels;
00224         avctx->block_align = BLKSIZE;
00225         break;
00226     case CODEC_ID_ADPCM_SWF:
00227         if (avctx->sample_rate != 11025 &&
00228             avctx->sample_rate != 22050 &&
00229             avctx->sample_rate != 44100) {
00230             av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
00231             goto error;
00232         }
00233         avctx->frame_size = 512 * (avctx->sample_rate / 11025);
00234         break;
00235     default:
00236         goto error;
00237     }
00238 
00239     avctx->coded_frame= avcodec_alloc_frame();
00240     avctx->coded_frame->key_frame= 1;
00241 
00242     return 0;
00243 error:
00244     av_freep(&s->paths);
00245     av_freep(&s->node_buf);
00246     av_freep(&s->nodep_buf);
00247     av_freep(&s->trellis_hash);
00248     return -1;
00249 }
00250 
00251 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
00252 {
00253     ADPCMContext *s = avctx->priv_data;
00254     av_freep(&avctx->coded_frame);
00255     av_freep(&s->paths);
00256     av_freep(&s->node_buf);
00257     av_freep(&s->nodep_buf);
00258     av_freep(&s->trellis_hash);
00259 
00260     return 0;
00261 }
00262 
00263 
00264 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
00265 {
00266     int delta = sample - c->prev_sample;
00267     int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
00268     c->prev_sample += ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
00269     c->prev_sample = av_clip_int16(c->prev_sample);
00270     c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
00271     return nibble;
00272 }
00273 
00274 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
00275 {
00276     int predictor, nibble, bias;
00277 
00278     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
00279 
00280     nibble= sample - predictor;
00281     if(nibble>=0) bias= c->idelta/2;
00282     else          bias=-c->idelta/2;
00283 
00284     nibble= (nibble + bias) / c->idelta;
00285     nibble= av_clip(nibble, -8, 7)&0x0F;
00286 
00287     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
00288 
00289     c->sample2 = c->sample1;
00290     c->sample1 = av_clip_int16(predictor);
00291 
00292     c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
00293     if (c->idelta < 16) c->idelta = 16;
00294 
00295     return nibble;
00296 }
00297 
00298 static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
00299 {
00300     int nibble, delta;
00301 
00302     if(!c->step) {
00303         c->predictor = 0;
00304         c->step = 127;
00305     }
00306 
00307     delta = sample - c->predictor;
00308 
00309     nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
00310 
00311     c->predictor += ((c->step * yamaha_difflookup[nibble]) / 8);
00312     c->predictor = av_clip_int16(c->predictor);
00313     c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
00314     c->step = av_clip(c->step, 127, 24567);
00315 
00316     return nibble;
00317 }
00318 
00319 static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
00320                                    uint8_t *dst, ADPCMChannelStatus *c, int n)
00321 {
00322     //FIXME 6% faster if frontier is a compile-time constant
00323     ADPCMContext *s = avctx->priv_data;
00324     const int frontier = 1 << avctx->trellis;
00325     const int stride = avctx->channels;
00326     const int version = avctx->codec->id;
00327     TrellisPath *paths = s->paths, *p;
00328     TrellisNode *node_buf = s->node_buf;
00329     TrellisNode **nodep_buf = s->nodep_buf;
00330     TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
00331     TrellisNode **nodes_next = nodep_buf + frontier;
00332     int pathn = 0, froze = -1, i, j, k, generation = 0;
00333     uint8_t *hash = s->trellis_hash;
00334     memset(hash, 0xff, 65536 * sizeof(*hash));
00335 
00336     memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
00337     nodes[0] = node_buf + frontier;
00338     nodes[0]->ssd = 0;
00339     nodes[0]->path = 0;
00340     nodes[0]->step = c->step_index;
00341     nodes[0]->sample1 = c->sample1;
00342     nodes[0]->sample2 = c->sample2;
00343     if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_IMA_QT) || (version == CODEC_ID_ADPCM_SWF))
00344         nodes[0]->sample1 = c->prev_sample;
00345     if(version == CODEC_ID_ADPCM_MS)
00346         nodes[0]->step = c->idelta;
00347     if(version == CODEC_ID_ADPCM_YAMAHA) {
00348         if(c->step == 0) {
00349             nodes[0]->step = 127;
00350             nodes[0]->sample1 = 0;
00351         } else {
00352             nodes[0]->step = c->step;
00353             nodes[0]->sample1 = c->predictor;
00354         }
00355     }
00356 
00357     for(i=0; i<n; i++) {
00358         TrellisNode *t = node_buf + frontier*(i&1);
00359         TrellisNode **u;
00360         int sample = samples[i*stride];
00361         int heap_pos = 0;
00362         memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
00363         for(j=0; j<frontier && nodes[j]; j++) {
00364             // higher j have higher ssd already, so they're likely to yield a suboptimal next sample too
00365             const int range = (j < frontier/2) ? 1 : 0;
00366             const int step = nodes[j]->step;
00367             int nidx;
00368             if(version == CODEC_ID_ADPCM_MS) {
00369                 const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 64;
00370                 const int div = (sample - predictor) / step;
00371                 const int nmin = av_clip(div-range, -8, 6);
00372                 const int nmax = av_clip(div+range, -7, 7);
00373                 for(nidx=nmin; nidx<=nmax; nidx++) {
00374                     const int nibble = nidx & 0xf;
00375                     int dec_sample = predictor + nidx * step;
00376 #define STORE_NODE(NAME, STEP_INDEX)\
00377                     int d;\
00378                     uint32_t ssd;\
00379                     int pos;\
00380                     TrellisNode *u;\
00381                     uint8_t *h;\
00382                     dec_sample = av_clip_int16(dec_sample);\
00383                     d = sample - dec_sample;\
00384                     ssd = nodes[j]->ssd + d*d;\
00385                     /* Check for wraparound, skip such samples completely. \
00386                      * Note, changing ssd to a 64 bit variable would be \
00387                      * simpler, avoiding this check, but it's slower on \
00388                      * x86 32 bit at the moment. */\
00389                     if (ssd < nodes[j]->ssd)\
00390                         goto next_##NAME;\
00391                     /* Collapse any two states with the same previous sample value. \
00392                      * One could also distinguish states by step and by 2nd to last
00393                      * sample, but the effects of that are negligible.
00394                      * Since nodes in the previous generation are iterated
00395                      * through a heap, they're roughly ordered from better to
00396                      * worse, but not strictly ordered. Therefore, an earlier
00397                      * node with the same sample value is better in most cases
00398                      * (and thus the current is skipped), but not strictly
00399                      * in all cases. Only skipping samples where ssd >=
00400                      * ssd of the earlier node with the same sample gives
00401                      * slightly worse quality, though, for some reason. */ \
00402                     h = &hash[(uint16_t) dec_sample];\
00403                     if (*h == generation)\
00404                         goto next_##NAME;\
00405                     if (heap_pos < frontier) {\
00406                         pos = heap_pos++;\
00407                     } else {\
00408                         /* Try to replace one of the leaf nodes with the new \
00409                          * one, but try a different slot each time. */\
00410                         pos = (frontier >> 1) + (heap_pos & ((frontier >> 1) - 1));\
00411                         if (ssd > nodes_next[pos]->ssd)\
00412                             goto next_##NAME;\
00413                         heap_pos++;\
00414                     }\
00415                     *h = generation;\
00416                     u = nodes_next[pos];\
00417                     if(!u) {\
00418                         assert(pathn < FREEZE_INTERVAL<<avctx->trellis);\
00419                         u = t++;\
00420                         nodes_next[pos] = u;\
00421                         u->path = pathn++;\
00422                     }\
00423                     u->ssd = ssd;\
00424                     u->step = STEP_INDEX;\
00425                     u->sample2 = nodes[j]->sample1;\
00426                     u->sample1 = dec_sample;\
00427                     paths[u->path].nibble = nibble;\
00428                     paths[u->path].prev = nodes[j]->path;\
00429                     /* Sift the newly inserted node up in the heap to \
00430                      * restore the heap property. */\
00431                     while (pos > 0) {\
00432                         int parent = (pos - 1) >> 1;\
00433                         if (nodes_next[parent]->ssd <= ssd)\
00434                             break;\
00435                         FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
00436                         pos = parent;\
00437                     }\
00438                     next_##NAME:;
00439                     STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
00440                 }
00441             } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_IMA_QT)|| (version == CODEC_ID_ADPCM_SWF)) {
00442 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
00443                 const int predictor = nodes[j]->sample1;\
00444                 const int div = (sample - predictor) * 4 / STEP_TABLE;\
00445                 int nmin = av_clip(div-range, -7, 6);\
00446                 int nmax = av_clip(div+range, -6, 7);\
00447                 if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
00448                 if(nmax<0) nmax--;\
00449                 for(nidx=nmin; nidx<=nmax; nidx++) {\
00450                     const int nibble = nidx<0 ? 7-nidx : nidx;\
00451                     int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
00452                     STORE_NODE(NAME, STEP_INDEX);\
00453                 }
00454                 LOOP_NODES(ima, step_table[step], av_clip(step + index_table[nibble], 0, 88));
00455             } else { //CODEC_ID_ADPCM_YAMAHA
00456                 LOOP_NODES(yamaha, step, av_clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
00457 #undef LOOP_NODES
00458 #undef STORE_NODE
00459             }
00460         }
00461 
00462         u = nodes;
00463         nodes = nodes_next;
00464         nodes_next = u;
00465 
00466         generation++;
00467         if (generation == 255) {
00468             memset(hash, 0xff, 65536 * sizeof(*hash));
00469             generation = 0;
00470         }
00471 
00472         // prevent overflow
00473         if(nodes[0]->ssd > (1<<28)) {
00474             for(j=1; j<frontier && nodes[j]; j++)
00475                 nodes[j]->ssd -= nodes[0]->ssd;
00476             nodes[0]->ssd = 0;
00477         }
00478 
00479         // merge old paths to save memory
00480         if(i == froze + FREEZE_INTERVAL) {
00481             p = &paths[nodes[0]->path];
00482             for(k=i; k>froze; k--) {
00483                 dst[k] = p->nibble;
00484                 p = &paths[p->prev];
00485             }
00486             froze = i;
00487             pathn = 0;
00488             // other nodes might use paths that don't coincide with the frozen one.
00489             // checking which nodes do so is too slow, so just kill them all.
00490             // this also slightly improves quality, but I don't know why.
00491             memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
00492         }
00493     }
00494 
00495     p = &paths[nodes[0]->path];
00496     for(i=n-1; i>froze; i--) {
00497         dst[i] = p->nibble;
00498         p = &paths[p->prev];
00499     }
00500 
00501     c->predictor = nodes[0]->sample1;
00502     c->sample1 = nodes[0]->sample1;
00503     c->sample2 = nodes[0]->sample2;
00504     c->step_index = nodes[0]->step;
00505     c->step = nodes[0]->step;
00506     c->idelta = nodes[0]->step;
00507 }
00508 
00509 static int adpcm_encode_frame(AVCodecContext *avctx,
00510                             unsigned char *frame, int buf_size, void *data)
00511 {
00512     int n, i, st;
00513     short *samples;
00514     unsigned char *dst;
00515     ADPCMContext *c = avctx->priv_data;
00516     uint8_t *buf;
00517 
00518     dst = frame;
00519     samples = (short *)data;
00520     st= avctx->channels == 2;
00521 /*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
00522 
00523     switch(avctx->codec->id) {
00524     case CODEC_ID_ADPCM_IMA_WAV:
00525         n = avctx->frame_size / 8;
00526             c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
00527 /*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
00528             bytestream_put_le16(&dst, c->status[0].prev_sample);
00529             *dst++ = (unsigned char)c->status[0].step_index;
00530             *dst++ = 0; /* unknown */
00531             samples++;
00532             if (avctx->channels == 2) {
00533                 c->status[1].prev_sample = (signed short)samples[0];
00534 /*                c->status[1].step_index = 0; */
00535                 bytestream_put_le16(&dst, c->status[1].prev_sample);
00536                 *dst++ = (unsigned char)c->status[1].step_index;
00537                 *dst++ = 0;
00538                 samples++;
00539             }
00540 
00541             /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
00542             if(avctx->trellis > 0) {
00543                 FF_ALLOC_OR_GOTO(avctx, buf, 2*n*8, error);
00544                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n*8);
00545                 if(avctx->channels == 2)
00546                     adpcm_compress_trellis(avctx, samples+1, buf + n*8, &c->status[1], n*8);
00547                 for(i=0; i<n; i++) {
00548                     *dst++ = buf[8*i+0] | (buf[8*i+1] << 4);
00549                     *dst++ = buf[8*i+2] | (buf[8*i+3] << 4);
00550                     *dst++ = buf[8*i+4] | (buf[8*i+5] << 4);
00551                     *dst++ = buf[8*i+6] | (buf[8*i+7] << 4);
00552                     if (avctx->channels == 2) {
00553                         uint8_t *buf1 = buf + n*8;
00554                         *dst++ = buf1[8*i+0] | (buf1[8*i+1] << 4);
00555                         *dst++ = buf1[8*i+2] | (buf1[8*i+3] << 4);
00556                         *dst++ = buf1[8*i+4] | (buf1[8*i+5] << 4);
00557                         *dst++ = buf1[8*i+6] | (buf1[8*i+7] << 4);
00558                     }
00559                 }
00560                 av_free(buf);
00561             } else
00562             for (; n>0; n--) {
00563                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
00564                 *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
00565                 dst++;
00566                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
00567                 *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
00568                 dst++;
00569                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
00570                 *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
00571                 dst++;
00572                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
00573                 *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
00574                 dst++;
00575                 /* right channel */
00576                 if (avctx->channels == 2) {
00577                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
00578                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
00579                     dst++;
00580                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
00581                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
00582                     dst++;
00583                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
00584                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
00585                     dst++;
00586                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
00587                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
00588                     dst++;
00589                 }
00590                 samples += 8 * avctx->channels;
00591             }
00592         break;
00593     case CODEC_ID_ADPCM_IMA_QT:
00594     {
00595         int ch, i;
00596         PutBitContext pb;
00597         init_put_bits(&pb, dst, buf_size*8);
00598 
00599         for(ch=0; ch<avctx->channels; ch++){
00600             put_bits(&pb, 9, (c->status[ch].prev_sample + 0x10000) >> 7);
00601             put_bits(&pb, 7, c->status[ch].step_index);
00602             if(avctx->trellis > 0) {
00603                 uint8_t buf[64];
00604                 adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
00605                 for(i=0; i<64; i++)
00606                     put_bits(&pb, 4, buf[i^1]);
00607                 c->status[ch].prev_sample = c->status[ch].predictor & ~0x7F;
00608             } else {
00609                 for (i=0; i<64; i+=2){
00610                     int t1, t2;
00611                     t1 = adpcm_ima_compress_sample(&c->status[ch], samples[avctx->channels*(i+0)+ch]);
00612                     t2 = adpcm_ima_compress_sample(&c->status[ch], samples[avctx->channels*(i+1)+ch]);
00613                     put_bits(&pb, 4, t2);
00614                     put_bits(&pb, 4, t1);
00615                 }
00616                 c->status[ch].prev_sample &= ~0x7F;
00617             }
00618         }
00619 
00620         flush_put_bits(&pb);
00621         dst += put_bits_count(&pb)>>3;
00622         break;
00623     }
00624     case CODEC_ID_ADPCM_SWF:
00625     {
00626         int i;
00627         PutBitContext pb;
00628         init_put_bits(&pb, dst, buf_size*8);
00629 
00630         n = avctx->frame_size-1;
00631 
00632         //Store AdpcmCodeSize
00633         put_bits(&pb, 2, 2);                //Set 4bits flash adpcm format
00634 
00635         //Init the encoder state
00636         for(i=0; i<avctx->channels; i++){
00637             c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
00638             put_sbits(&pb, 16, samples[i]);
00639             put_bits(&pb, 6, c->status[i].step_index);
00640             c->status[i].prev_sample = (signed short)samples[i];
00641         }
00642 
00643         if(avctx->trellis > 0) {
00644             FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
00645             adpcm_compress_trellis(avctx, samples+2, buf, &c->status[0], n);
00646             if (avctx->channels == 2)
00647                 adpcm_compress_trellis(avctx, samples+3, buf+n, &c->status[1], n);
00648             for(i=0; i<n; i++) {
00649                 put_bits(&pb, 4, buf[i]);
00650                 if (avctx->channels == 2)
00651                     put_bits(&pb, 4, buf[n+i]);
00652             }
00653             av_free(buf);
00654         } else {
00655             for (i=1; i<avctx->frame_size; i++) {
00656                 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
00657                 if (avctx->channels == 2)
00658                     put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
00659             }
00660         }
00661         flush_put_bits(&pb);
00662         dst += put_bits_count(&pb)>>3;
00663         break;
00664     }
00665     case CODEC_ID_ADPCM_MS:
00666         for(i=0; i<avctx->channels; i++){
00667             int predictor=0;
00668 
00669             *dst++ = predictor;
00670             c->status[i].coeff1 = AdaptCoeff1[predictor];
00671             c->status[i].coeff2 = AdaptCoeff2[predictor];
00672         }
00673         for(i=0; i<avctx->channels; i++){
00674             if (c->status[i].idelta < 16)
00675                 c->status[i].idelta = 16;
00676 
00677             bytestream_put_le16(&dst, c->status[i].idelta);
00678         }
00679         for(i=0; i<avctx->channels; i++){
00680             c->status[i].sample2= *samples++;
00681         }
00682         for(i=0; i<avctx->channels; i++){
00683             c->status[i].sample1= *samples++;
00684 
00685             bytestream_put_le16(&dst, c->status[i].sample1);
00686         }
00687         for(i=0; i<avctx->channels; i++)
00688             bytestream_put_le16(&dst, c->status[i].sample2);
00689 
00690         if(avctx->trellis > 0) {
00691             int n = avctx->block_align - 7*avctx->channels;
00692             FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
00693             if(avctx->channels == 1) {
00694                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00695                 for(i=0; i<n; i+=2)
00696                     *dst++ = (buf[i] << 4) | buf[i+1];
00697             } else {
00698                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00699                 adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
00700                 for(i=0; i<n; i++)
00701                     *dst++ = (buf[i] << 4) | buf[n+i];
00702             }
00703             av_free(buf);
00704         } else
00705         for(i=7*avctx->channels; i<avctx->block_align; i++) {
00706             int nibble;
00707             nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
00708             nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
00709             *dst++ = nibble;
00710         }
00711         break;
00712     case CODEC_ID_ADPCM_YAMAHA:
00713         n = avctx->frame_size / 2;
00714         if(avctx->trellis > 0) {
00715             FF_ALLOC_OR_GOTO(avctx, buf, 2*n*2, error);
00716             n *= 2;
00717             if(avctx->channels == 1) {
00718                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00719                 for(i=0; i<n; i+=2)
00720                     *dst++ = buf[i] | (buf[i+1] << 4);
00721             } else {
00722                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00723                 adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
00724                 for(i=0; i<n; i++)
00725                     *dst++ = buf[i] | (buf[n+i] << 4);
00726             }
00727             av_free(buf);
00728         } else
00729             for (n *= avctx->channels; n>0; n--) {
00730                 int nibble;
00731                 nibble  = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
00732                 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
00733                 *dst++ = nibble;
00734             }
00735         break;
00736     default:
00737     error:
00738         return -1;
00739     }
00740     return dst - frame;
00741 }
00742 #endif //CONFIG_ENCODERS
00743 
00744 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
00745 {
00746     ADPCMContext *c = avctx->priv_data;
00747     unsigned int min_channels = 1;
00748     unsigned int max_channels = 2;
00749 
00750     switch(avctx->codec->id) {
00751     case CODEC_ID_ADPCM_EA:
00752         min_channels = 2;
00753         break;
00754     case CODEC_ID_ADPCM_EA_R1:
00755     case CODEC_ID_ADPCM_EA_R2:
00756     case CODEC_ID_ADPCM_EA_R3:
00757     case CODEC_ID_ADPCM_EA_XAS:
00758         max_channels = 6;
00759         break;
00760     }
00761 
00762     if (avctx->channels < min_channels || avctx->channels > max_channels) {
00763         av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
00764         return AVERROR(EINVAL);
00765     }
00766 
00767     switch(avctx->codec->id) {
00768     case CODEC_ID_ADPCM_CT:
00769         c->status[0].step = c->status[1].step = 511;
00770         break;
00771     case CODEC_ID_ADPCM_IMA_WAV:
00772         if (avctx->bits_per_coded_sample != 4) {
00773             av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
00774             return -1;
00775         }
00776         break;
00777     case CODEC_ID_ADPCM_IMA_WS:
00778         if (avctx->extradata && avctx->extradata_size == 2 * 4) {
00779             c->status[0].predictor = AV_RL32(avctx->extradata);
00780             c->status[1].predictor = AV_RL32(avctx->extradata + 4);
00781         }
00782         break;
00783     default:
00784         break;
00785     }
00786     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00787     return 0;
00788 }
00789 
00790 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
00791 {
00792     int step_index;
00793     int predictor;
00794     int sign, delta, diff, step;
00795 
00796     step = step_table[c->step_index];
00797     step_index = c->step_index + index_table[(unsigned)nibble];
00798     if (step_index < 0) step_index = 0;
00799     else if (step_index > 88) step_index = 88;
00800 
00801     sign = nibble & 8;
00802     delta = nibble & 7;
00803     /* perform direct multiplication instead of series of jumps proposed by
00804      * the reference ADPCM implementation since modern CPUs can do the mults
00805      * quickly enough */
00806     diff = ((2 * delta + 1) * step) >> shift;
00807     predictor = c->predictor;
00808     if (sign) predictor -= diff;
00809     else predictor += diff;
00810 
00811     c->predictor = av_clip_int16(predictor);
00812     c->step_index = step_index;
00813 
00814     return (short)c->predictor;
00815 }
00816 
00817 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
00818 {
00819     int predictor;
00820 
00821     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
00822     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
00823 
00824     c->sample2 = c->sample1;
00825     c->sample1 = av_clip_int16(predictor);
00826     c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
00827     if (c->idelta < 16) c->idelta = 16;
00828 
00829     return c->sample1;
00830 }
00831 
00832 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
00833 {
00834     int sign, delta, diff;
00835     int new_step;
00836 
00837     sign = nibble & 8;
00838     delta = nibble & 7;
00839     /* perform direct multiplication instead of series of jumps proposed by
00840      * the reference ADPCM implementation since modern CPUs can do the mults
00841      * quickly enough */
00842     diff = ((2 * delta + 1) * c->step) >> 3;
00843     /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
00844     c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
00845     c->predictor = av_clip_int16(c->predictor);
00846     /* calculate new step and clamp it to range 511..32767 */
00847     new_step = (AdaptationTable[nibble & 7] * c->step) >> 8;
00848     c->step = av_clip(new_step, 511, 32767);
00849 
00850     return (short)c->predictor;
00851 }
00852 
00853 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
00854 {
00855     int sign, delta, diff;
00856 
00857     sign = nibble & (1<<(size-1));
00858     delta = nibble & ((1<<(size-1))-1);
00859     diff = delta << (7 + c->step + shift);
00860 
00861     /* clamp result */
00862     c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
00863 
00864     /* calculate new step */
00865     if (delta >= (2*size - 3) && c->step < 3)
00866         c->step++;
00867     else if (delta == 0 && c->step > 0)
00868         c->step--;
00869 
00870     return (short) c->predictor;
00871 }
00872 
00873 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
00874 {
00875     if(!c->step) {
00876         c->predictor = 0;
00877         c->step = 127;
00878     }
00879 
00880     c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
00881     c->predictor = av_clip_int16(c->predictor);
00882     c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
00883     c->step = av_clip(c->step, 127, 24567);
00884     return c->predictor;
00885 }
00886 
00887 static void xa_decode(short *out, const unsigned char *in,
00888     ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
00889 {
00890     int i, j;
00891     int shift,filter,f0,f1;
00892     int s_1,s_2;
00893     int d,s,t;
00894 
00895     for(i=0;i<4;i++) {
00896 
00897         shift  = 12 - (in[4+i*2] & 15);
00898         filter = in[4+i*2] >> 4;
00899         f0 = xa_adpcm_table[filter][0];
00900         f1 = xa_adpcm_table[filter][1];
00901 
00902         s_1 = left->sample1;
00903         s_2 = left->sample2;
00904 
00905         for(j=0;j<28;j++) {
00906             d = in[16+i+j*4];
00907 
00908             t = (signed char)(d<<4)>>4;
00909             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
00910             s_2 = s_1;
00911             s_1 = av_clip_int16(s);
00912             *out = s_1;
00913             out += inc;
00914         }
00915 
00916         if (inc==2) { /* stereo */
00917             left->sample1 = s_1;
00918             left->sample2 = s_2;
00919             s_1 = right->sample1;
00920             s_2 = right->sample2;
00921             out = out + 1 - 28*2;
00922         }
00923 
00924         shift  = 12 - (in[5+i*2] & 15);
00925         filter = in[5+i*2] >> 4;
00926 
00927         f0 = xa_adpcm_table[filter][0];
00928         f1 = xa_adpcm_table[filter][1];
00929 
00930         for(j=0;j<28;j++) {
00931             d = in[16+i+j*4];
00932 
00933             t = (signed char)d >> 4;
00934             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
00935             s_2 = s_1;
00936             s_1 = av_clip_int16(s);
00937             *out = s_1;
00938             out += inc;
00939         }
00940 
00941         if (inc==2) { /* stereo */
00942             right->sample1 = s_1;
00943             right->sample2 = s_2;
00944             out -= 1;
00945         } else {
00946             left->sample1 = s_1;
00947             left->sample2 = s_2;
00948         }
00949     }
00950 }
00951 
00952 
00953 /* DK3 ADPCM support macro */
00954 #define DK3_GET_NEXT_NIBBLE() \
00955     if (decode_top_nibble_next) \
00956     { \
00957         nibble = last_byte >> 4; \
00958         decode_top_nibble_next = 0; \
00959     } \
00960     else \
00961     { \
00962         last_byte = *src++; \
00963         if (src >= buf + buf_size) break; \
00964         nibble = last_byte & 0x0F; \
00965         decode_top_nibble_next = 1; \
00966     }
00967 
00968 static int adpcm_decode_frame(AVCodecContext *avctx,
00969                             void *data, int *data_size,
00970                             AVPacket *avpkt)
00971 {
00972     const uint8_t *buf = avpkt->data;
00973     int buf_size = avpkt->size;
00974     ADPCMContext *c = avctx->priv_data;
00975     ADPCMChannelStatus *cs;
00976     int n, m, channel, i;
00977     int block_predictor[2];
00978     short *samples;
00979     short *samples_end;
00980     const uint8_t *src;
00981     int st; /* stereo */
00982 
00983     /* DK3 ADPCM accounting variables */
00984     unsigned char last_byte = 0;
00985     unsigned char nibble;
00986     int decode_top_nibble_next = 0;
00987     int diff_channel;
00988 
00989     /* EA ADPCM state variables */
00990     uint32_t samples_in_chunk;
00991     int32_t previous_left_sample, previous_right_sample;
00992     int32_t current_left_sample, current_right_sample;
00993     int32_t next_left_sample, next_right_sample;
00994     int32_t coeff1l, coeff2l, coeff1r, coeff2r;
00995     uint8_t shift_left, shift_right;
00996     int count1, count2;
00997     int coeff[2][2], shift[2];//used in EA MAXIS ADPCM
00998 
00999     if (!buf_size)
01000         return 0;
01001 
01002     //should protect all 4bit ADPCM variants
01003     //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
01004     //
01005     if(*data_size/4 < buf_size + 8)
01006         return -1;
01007 
01008     samples = data;
01009     samples_end= samples + *data_size/2;
01010     *data_size= 0;
01011     src = buf;
01012 
01013     st = avctx->channels == 2 ? 1 : 0;
01014 
01015     switch(avctx->codec->id) {
01016     case CODEC_ID_ADPCM_IMA_QT:
01017         n = buf_size - 2*avctx->channels;
01018         for (channel = 0; channel < avctx->channels; channel++) {
01019             cs = &(c->status[channel]);
01020             /* (pppppp) (piiiiiii) */
01021 
01022             /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
01023             cs->predictor = (*src++) << 8;
01024             cs->predictor |= (*src & 0x80);
01025             cs->predictor &= 0xFF80;
01026 
01027             /* sign extension */
01028             if(cs->predictor & 0x8000)
01029                 cs->predictor -= 0x10000;
01030 
01031             cs->predictor = av_clip_int16(cs->predictor);
01032 
01033             cs->step_index = (*src++) & 0x7F;
01034 
01035             if (cs->step_index > 88){
01036                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
01037                 cs->step_index = 88;
01038             }
01039 
01040             cs->step = step_table[cs->step_index];
01041 
01042             samples = (short*)data + channel;
01043 
01044             for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
01045                 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
01046                 samples += avctx->channels;
01047                 *samples = adpcm_ima_expand_nibble(cs, src[0] >> 4  , 3);
01048                 samples += avctx->channels;
01049                 src ++;
01050             }
01051         }
01052         if (st)
01053             samples--;
01054         break;
01055     case CODEC_ID_ADPCM_IMA_WAV:
01056         if (avctx->block_align != 0 && buf_size > avctx->block_align)
01057             buf_size = avctx->block_align;
01058 
01059 //        samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
01060 
01061         for(i=0; i<avctx->channels; i++){
01062             cs = &(c->status[i]);
01063             cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
01064 
01065             cs->step_index = *src++;
01066             if (cs->step_index > 88){
01067                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
01068                 cs->step_index = 88;
01069             }
01070             if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
01071         }
01072 
01073         while(src < buf + buf_size){
01074             for(m=0; m<4; m++){
01075                 for(i=0; i<=st; i++)
01076                     *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
01077                 for(i=0; i<=st; i++)
01078                     *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4  , 3);
01079                 src++;
01080             }
01081             src += 4*st;
01082         }
01083         break;
01084     case CODEC_ID_ADPCM_4XM:
01085         cs = &(c->status[0]);
01086         c->status[0].predictor= (int16_t)bytestream_get_le16(&src);
01087         if(st){
01088             c->status[1].predictor= (int16_t)bytestream_get_le16(&src);
01089         }
01090         c->status[0].step_index= (int16_t)bytestream_get_le16(&src);
01091         if(st){
01092             c->status[1].step_index= (int16_t)bytestream_get_le16(&src);
01093         }
01094         if (cs->step_index < 0) cs->step_index = 0;
01095         if (cs->step_index > 88) cs->step_index = 88;
01096 
01097         m= (buf_size - (src - buf))>>st;
01098         for(i=0; i<m; i++) {
01099             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
01100             if (st)
01101                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
01102             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
01103             if (st)
01104                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
01105         }
01106 
01107         src += m<<st;
01108 
01109         break;
01110     case CODEC_ID_ADPCM_MS:
01111         if (avctx->block_align != 0 && buf_size > avctx->block_align)
01112             buf_size = avctx->block_align;
01113         n = buf_size - 7 * avctx->channels;
01114         if (n < 0)
01115             return -1;
01116         block_predictor[0] = av_clip(*src++, 0, 6);
01117         block_predictor[1] = 0;
01118         if (st)
01119             block_predictor[1] = av_clip(*src++, 0, 6);
01120         c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
01121         if (st){
01122             c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
01123         }
01124         c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
01125         c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
01126         c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
01127         c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
01128 
01129         c->status[0].sample1 = bytestream_get_le16(&src);
01130         if (st) c->status[1].sample1 = bytestream_get_le16(&src);
01131         c->status[0].sample2 = bytestream_get_le16(&src);
01132         if (st) c->status[1].sample2 = bytestream_get_le16(&src);
01133 
01134         *samples++ = c->status[0].sample2;
01135         if (st) *samples++ = c->status[1].sample2;
01136         *samples++ = c->status[0].sample1;
01137         if (st) *samples++ = c->status[1].sample1;
01138         for(;n>0;n--) {
01139             *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4  );
01140             *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
01141             src ++;
01142         }
01143         break;
01144     case CODEC_ID_ADPCM_IMA_DK4:
01145         if (avctx->block_align != 0 && buf_size > avctx->block_align)
01146             buf_size = avctx->block_align;
01147 
01148         c->status[0].predictor  = (int16_t)bytestream_get_le16(&src);
01149         c->status[0].step_index = *src++;
01150         src++;
01151         *samples++ = c->status[0].predictor;
01152         if (st) {
01153             c->status[1].predictor  = (int16_t)bytestream_get_le16(&src);
01154             c->status[1].step_index = *src++;
01155             src++;
01156             *samples++ = c->status[1].predictor;
01157         }
01158         while (src < buf + buf_size) {
01159 
01160             /* take care of the top nibble (always left or mono channel) */
01161             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01162                 src[0] >> 4, 3);
01163 
01164             /* take care of the bottom nibble, which is right sample for
01165              * stereo, or another mono sample */
01166             if (st)
01167                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
01168                     src[0] & 0x0F, 3);
01169             else
01170                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01171                     src[0] & 0x0F, 3);
01172 
01173             src++;
01174         }
01175         break;
01176     case CODEC_ID_ADPCM_IMA_DK3:
01177         if (avctx->block_align != 0 && buf_size > avctx->block_align)
01178             buf_size = avctx->block_align;
01179 
01180         if(buf_size + 16 > (samples_end - samples)*3/8)
01181             return -1;
01182 
01183         c->status[0].predictor  = (int16_t)AV_RL16(src + 10);
01184         c->status[1].predictor  = (int16_t)AV_RL16(src + 12);
01185         c->status[0].step_index = src[14];
01186         c->status[1].step_index = src[15];
01187         /* sign extend the predictors */
01188         src += 16;
01189         diff_channel = c->status[1].predictor;
01190 
01191         /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
01192          * the buffer is consumed */
01193         while (1) {
01194 
01195             /* for this algorithm, c->status[0] is the sum channel and
01196              * c->status[1] is the diff channel */
01197 
01198             /* process the first predictor of the sum channel */
01199             DK3_GET_NEXT_NIBBLE();
01200             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
01201 
01202             /* process the diff channel predictor */
01203             DK3_GET_NEXT_NIBBLE();
01204             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
01205 
01206             /* process the first pair of stereo PCM samples */
01207             diff_channel = (diff_channel + c->status[1].predictor) / 2;
01208             *samples++ = c->status[0].predictor + c->status[1].predictor;
01209             *samples++ = c->status[0].predictor - c->status[1].predictor;
01210 
01211             /* process the second predictor of the sum channel */
01212             DK3_GET_NEXT_NIBBLE();
01213             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
01214 
01215             /* process the second pair of stereo PCM samples */
01216             diff_channel = (diff_channel + c->status[1].predictor) / 2;
01217             *samples++ = c->status[0].predictor + c->status[1].predictor;
01218             *samples++ = c->status[0].predictor - c->status[1].predictor;
01219         }
01220         break;
01221     case CODEC_ID_ADPCM_IMA_ISS:
01222         c->status[0].predictor  = (int16_t)AV_RL16(src + 0);
01223         c->status[0].step_index = src[2];
01224         src += 4;
01225         if(st) {
01226             c->status[1].predictor  = (int16_t)AV_RL16(src + 0);
01227             c->status[1].step_index = src[2];
01228             src += 4;
01229         }
01230 
01231         while (src < buf + buf_size) {
01232 
01233             if (st) {
01234                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01235                     src[0] >> 4  , 3);
01236                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
01237                     src[0] & 0x0F, 3);
01238             } else {
01239                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01240                     src[0] & 0x0F, 3);
01241                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01242                     src[0] >> 4  , 3);
01243             }
01244 
01245             src++;
01246         }
01247         break;
01248     case CODEC_ID_ADPCM_IMA_WS:
01249         /* no per-block initialization; just start decoding the data */
01250         while (src < buf + buf_size) {
01251 
01252             if (st) {
01253                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01254                     src[0] >> 4  , 3);
01255                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
01256                     src[0] & 0x0F, 3);
01257             } else {
01258                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01259                     src[0] >> 4  , 3);
01260                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01261                     src[0] & 0x0F, 3);
01262             }
01263 
01264             src++;
01265         }
01266         break;
01267     case CODEC_ID_ADPCM_XA:
01268         while (buf_size >= 128) {
01269             xa_decode(samples, src, &c->status[0], &c->status[1],
01270                 avctx->channels);
01271             src += 128;
01272             samples += 28 * 8;
01273             buf_size -= 128;
01274         }
01275         break;
01276     case CODEC_ID_ADPCM_IMA_EA_EACS: {
01277         unsigned header_size = 4 + (8<<st);
01278         samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
01279 
01280         if (buf_size < header_size || samples_in_chunk > buf_size - header_size) {
01281             src += buf_size - 4;
01282             break;
01283         }
01284 
01285         for (i=0; i<=st; i++)
01286             c->status[i].step_index = bytestream_get_le32(&src);
01287         for (i=0; i<=st; i++)
01288             c->status[i].predictor  = bytestream_get_le32(&src);
01289 
01290         for (; samples_in_chunk; samples_in_chunk--, src++) {
01291             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  *src>>4,   3);
01292             *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
01293         }
01294         break;
01295     }
01296     case CODEC_ID_ADPCM_IMA_EA_SEAD:
01297         for (; src < buf+buf_size; src++) {
01298             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
01299             *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
01300         }
01301         break;
01302     case CODEC_ID_ADPCM_EA:
01303         /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
01304            each coding 28 stereo samples. */
01305         if (buf_size < 12) {
01306             av_log(avctx, AV_LOG_ERROR, "frame too small\n");
01307             return AVERROR(EINVAL);
01308         }
01309         samples_in_chunk = AV_RL32(src);
01310         if (samples_in_chunk / 28 > (buf_size - 12) / 30) {
01311             av_log(avctx, AV_LOG_ERROR, "invalid frame\n");
01312             return AVERROR(EINVAL);
01313         }
01314         src += 4;
01315         current_left_sample   = (int16_t)bytestream_get_le16(&src);
01316         previous_left_sample  = (int16_t)bytestream_get_le16(&src);
01317         current_right_sample  = (int16_t)bytestream_get_le16(&src);
01318         previous_right_sample = (int16_t)bytestream_get_le16(&src);
01319 
01320         for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
01321             coeff1l = ea_adpcm_table[ *src >> 4       ];
01322             coeff2l = ea_adpcm_table[(*src >> 4  ) + 4];
01323             coeff1r = ea_adpcm_table[*src & 0x0F];
01324             coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
01325             src++;
01326 
01327             shift_left  = (*src >> 4  ) + 8;
01328             shift_right = (*src & 0x0F) + 8;
01329             src++;
01330 
01331             for (count2 = 0; count2 < 28; count2++) {
01332                 next_left_sample  = (int32_t)((*src & 0xF0) << 24) >> shift_left;
01333                 next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
01334                 src++;
01335 
01336                 next_left_sample = (next_left_sample +
01337                     (current_left_sample * coeff1l) +
01338                     (previous_left_sample * coeff2l) + 0x80) >> 8;
01339                 next_right_sample = (next_right_sample +
01340                     (current_right_sample * coeff1r) +
01341                     (previous_right_sample * coeff2r) + 0x80) >> 8;
01342 
01343                 previous_left_sample = current_left_sample;
01344                 current_left_sample = av_clip_int16(next_left_sample);
01345                 previous_right_sample = current_right_sample;
01346                 current_right_sample = av_clip_int16(next_right_sample);
01347                 *samples++ = (unsigned short)current_left_sample;
01348                 *samples++ = (unsigned short)current_right_sample;
01349             }
01350         }
01351 
01352         if (src - buf == buf_size - 2)
01353             src += 2; // Skip terminating 0x0000
01354 
01355         break;
01356     case CODEC_ID_ADPCM_EA_MAXIS_XA:
01357         for(channel = 0; channel < avctx->channels; channel++) {
01358             for (i=0; i<2; i++)
01359                 coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
01360             shift[channel] = (*src & 0x0F) + 8;
01361             src++;
01362         }
01363         for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
01364             for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
01365                 for(channel = 0; channel < avctx->channels; channel++) {
01366                     int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
01367                     sample = (sample +
01368                              c->status[channel].sample1 * coeff[channel][0] +
01369                              c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
01370                     c->status[channel].sample2 = c->status[channel].sample1;
01371                     c->status[channel].sample1 = av_clip_int16(sample);
01372                     *samples++ = c->status[channel].sample1;
01373                 }
01374             }
01375             src+=avctx->channels;
01376         }
01377         break;
01378     case CODEC_ID_ADPCM_EA_R1:
01379     case CODEC_ID_ADPCM_EA_R2:
01380     case CODEC_ID_ADPCM_EA_R3: {
01381         /* channel numbering
01382            2chan: 0=fl, 1=fr
01383            4chan: 0=fl, 1=rl, 2=fr, 3=rr
01384            6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
01385         const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
01386         int32_t previous_sample, current_sample, next_sample;
01387         int32_t coeff1, coeff2;
01388         uint8_t shift;
01389         unsigned int channel;
01390         uint16_t *samplesC;
01391         const uint8_t *srcC;
01392         const uint8_t *src_end = buf + buf_size;
01393 
01394         samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
01395                                        : bytestream_get_le32(&src)) / 28;
01396         if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
01397             28*samples_in_chunk*avctx->channels > samples_end-samples) {
01398             src += buf_size - 4;
01399             break;
01400         }
01401 
01402         for (channel=0; channel<avctx->channels; channel++) {
01403             int32_t offset = (big_endian ? bytestream_get_be32(&src)
01404                                          : bytestream_get_le32(&src))
01405                            + (avctx->channels-channel-1) * 4;
01406 
01407             if ((offset < 0) || (offset >= src_end - src - 4)) break;
01408             srcC  = src + offset;
01409             samplesC = samples + channel;
01410 
01411             if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
01412                 current_sample  = (int16_t)bytestream_get_le16(&srcC);
01413                 previous_sample = (int16_t)bytestream_get_le16(&srcC);
01414             } else {
01415                 current_sample  = c->status[channel].predictor;
01416                 previous_sample = c->status[channel].prev_sample;
01417             }
01418 
01419             for (count1=0; count1<samples_in_chunk; count1++) {
01420                 if (*srcC == 0xEE) {  /* only seen in R2 and R3 */
01421                     srcC++;
01422                     if (srcC > src_end - 30*2) break;
01423                     current_sample  = (int16_t)bytestream_get_be16(&srcC);
01424                     previous_sample = (int16_t)bytestream_get_be16(&srcC);
01425 
01426                     for (count2=0; count2<28; count2++) {
01427                         *samplesC = (int16_t)bytestream_get_be16(&srcC);
01428                         samplesC += avctx->channels;
01429                     }
01430                 } else {
01431                     coeff1 = ea_adpcm_table[ *srcC>>4     ];
01432                     coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
01433                     shift = (*srcC++ & 0x0F) + 8;
01434 
01435                     if (srcC > src_end - 14) break;
01436                     for (count2=0; count2<28; count2++) {
01437                         if (count2 & 1)
01438                             next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
01439                         else
01440                             next_sample = (int32_t)((*srcC   & 0xF0) << 24) >> shift;
01441 
01442                         next_sample += (current_sample  * coeff1) +
01443                                        (previous_sample * coeff2);
01444                         next_sample = av_clip_int16(next_sample >> 8);
01445 
01446                         previous_sample = current_sample;
01447                         current_sample  = next_sample;
01448                         *samplesC = current_sample;
01449                         samplesC += avctx->channels;
01450                     }
01451                 }
01452             }
01453 
01454             if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
01455                 c->status[channel].predictor   = current_sample;
01456                 c->status[channel].prev_sample = previous_sample;
01457             }
01458         }
01459 
01460         src = src + buf_size - (4 + 4*avctx->channels);
01461         samples += 28 * samples_in_chunk * avctx->channels;
01462         break;
01463     }
01464     case CODEC_ID_ADPCM_EA_XAS:
01465         if (samples_end-samples < 32*4*avctx->channels
01466             || buf_size < (4+15)*4*avctx->channels) {
01467             src += buf_size;
01468             break;
01469         }
01470         for (channel=0; channel<avctx->channels; channel++) {
01471             int coeff[2][4], shift[4];
01472             short *s2, *s = &samples[channel];
01473             for (n=0; n<4; n++, s+=32*avctx->channels) {
01474                 for (i=0; i<2; i++)
01475                     coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
01476                 shift[n] = (src[2]&0x0F) + 8;
01477                 for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
01478                     s2[0] = (src[0]&0xF0) + (src[1]<<8);
01479             }
01480 
01481             for (m=2; m<32; m+=2) {
01482                 s = &samples[m*avctx->channels + channel];
01483                 for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
01484                     for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
01485                         int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
01486                         int pred  = s2[-1*avctx->channels] * coeff[0][n]
01487                                   + s2[-2*avctx->channels] * coeff[1][n];
01488                         s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
01489                     }
01490                 }
01491             }
01492         }
01493         samples += 32*4*avctx->channels;
01494         break;
01495     case CODEC_ID_ADPCM_IMA_AMV:
01496     case CODEC_ID_ADPCM_IMA_SMJPEG:
01497         c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
01498         c->status[0].step_index = bytestream_get_le16(&src);
01499 
01500         if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
01501             src+=4;
01502 
01503         while (src < buf + buf_size) {
01504             char hi, lo;
01505             lo = *src & 0x0F;
01506             hi = *src >> 4;
01507 
01508             if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
01509                 FFSWAP(char, hi, lo);
01510 
01511             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01512                 lo, 3);
01513             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01514                 hi, 3);
01515             src++;
01516         }
01517         break;
01518     case CODEC_ID_ADPCM_CT:
01519         while (src < buf + buf_size) {
01520             if (st) {
01521                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
01522                     src[0] >> 4);
01523                 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
01524                     src[0] & 0x0F);
01525             } else {
01526                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
01527                     src[0] >> 4);
01528                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
01529                     src[0] & 0x0F);
01530             }
01531             src++;
01532         }
01533         break;
01534     case CODEC_ID_ADPCM_SBPRO_4:
01535     case CODEC_ID_ADPCM_SBPRO_3:
01536     case CODEC_ID_ADPCM_SBPRO_2:
01537         if (!c->status[0].step_index) {
01538             /* the first byte is a raw sample */
01539             *samples++ = 128 * (*src++ - 0x80);
01540             if (st)
01541               *samples++ = 128 * (*src++ - 0x80);
01542             c->status[0].step_index = 1;
01543         }
01544         if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
01545             while (src < buf + buf_size) {
01546                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01547                     src[0] >> 4, 4, 0);
01548                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
01549                     src[0] & 0x0F, 4, 0);
01550                 src++;
01551             }
01552         } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
01553             while (src < buf + buf_size && samples + 2 < samples_end) {
01554                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01555                      src[0] >> 5        , 3, 0);
01556                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01557                     (src[0] >> 2) & 0x07, 3, 0);
01558                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01559                     src[0] & 0x03, 2, 0);
01560                 src++;
01561             }
01562         } else {
01563             while (src < buf + buf_size && samples + 3 < samples_end) {
01564                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01565                      src[0] >> 6        , 2, 2);
01566                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
01567                     (src[0] >> 4) & 0x03, 2, 2);
01568                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01569                     (src[0] >> 2) & 0x03, 2, 2);
01570                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
01571                     src[0] & 0x03, 2, 2);
01572                 src++;
01573             }
01574         }
01575         break;
01576     case CODEC_ID_ADPCM_SWF:
01577     {
01578         GetBitContext gb;
01579         const int *table;
01580         int k0, signmask, nb_bits, count;
01581         int size = buf_size*8;
01582 
01583         init_get_bits(&gb, buf, size);
01584 
01585         //read bits & initial values
01586         nb_bits = get_bits(&gb, 2)+2;
01587         //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
01588         table = swf_index_tables[nb_bits-2];
01589         k0 = 1 << (nb_bits-2);
01590         signmask = 1 << (nb_bits-1);
01591 
01592         while (get_bits_count(&gb) <= size - 22*avctx->channels) {
01593             for (i = 0; i < avctx->channels; i++) {
01594                 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
01595                 c->status[i].step_index = get_bits(&gb, 6);
01596             }
01597 
01598             for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
01599                 int i;
01600 
01601                 for (i = 0; i < avctx->channels; i++) {
01602                     // similar to IMA adpcm
01603                     int delta = get_bits(&gb, nb_bits);
01604                     int step = step_table[c->status[i].step_index];
01605                     long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
01606                     int k = k0;
01607 
01608                     do {
01609                         if (delta & k)
01610                             vpdiff += step;
01611                         step >>= 1;
01612                         k >>= 1;
01613                     } while(k);
01614                     vpdiff += step;
01615 
01616                     if (delta & signmask)
01617                         c->status[i].predictor -= vpdiff;
01618                     else
01619                         c->status[i].predictor += vpdiff;
01620 
01621                     c->status[i].step_index += table[delta & (~signmask)];
01622 
01623                     c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
01624                     c->status[i].predictor = av_clip_int16(c->status[i].predictor);
01625 
01626                     *samples++ = c->status[i].predictor;
01627                     if (samples >= samples_end) {
01628                         av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
01629                         return -1;
01630                     }
01631                 }
01632             }
01633         }
01634         src += buf_size;
01635         break;
01636     }
01637     case CODEC_ID_ADPCM_YAMAHA:
01638         while (src < buf + buf_size) {
01639             if (st) {
01640                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
01641                         src[0] & 0x0F);
01642                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
01643                         src[0] >> 4  );
01644             } else {
01645                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
01646                         src[0] & 0x0F);
01647                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
01648                         src[0] >> 4  );
01649             }
01650             src++;
01651         }
01652         break;
01653     case CODEC_ID_ADPCM_THP:
01654     {
01655         int table[2][16];
01656         unsigned int samplecnt;
01657         int prev[2][2];
01658         int ch;
01659 
01660         if (buf_size < 80) {
01661             av_log(avctx, AV_LOG_ERROR, "frame too small\n");
01662             return -1;
01663         }
01664 
01665         src+=4;
01666         samplecnt = bytestream_get_be32(&src);
01667 
01668         for (i = 0; i < 32; i++)
01669             table[0][i] = (int16_t)bytestream_get_be16(&src);
01670 
01671         /* Initialize the previous sample.  */
01672         for (i = 0; i < 4; i++)
01673             prev[0][i] = (int16_t)bytestream_get_be16(&src);
01674 
01675         if (samplecnt >= (samples_end - samples) /  (st + 1)) {
01676             av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
01677             return -1;
01678         }
01679 
01680         for (ch = 0; ch <= st; ch++) {
01681             samples = (unsigned short *) data + ch;
01682 
01683             /* Read in every sample for this channel.  */
01684             for (i = 0; i < samplecnt / 14; i++) {
01685                 int index = (*src >> 4) & 7;
01686                 unsigned int exp = 28 - (*src++ & 15);
01687                 int factor1 = table[ch][index * 2];
01688                 int factor2 = table[ch][index * 2 + 1];
01689 
01690                 /* Decode 14 samples.  */
01691                 for (n = 0; n < 14; n++) {
01692                     int32_t sampledat;
01693                     if(n&1) sampledat=  *src++    <<28;
01694                     else    sampledat= (*src&0xF0)<<24;
01695 
01696                     sampledat = ((prev[ch][0]*factor1
01697                                 + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
01698                     *samples = av_clip_int16(sampledat);
01699                     prev[ch][1] = prev[ch][0];
01700                     prev[ch][0] = *samples++;
01701 
01702                     /* In case of stereo, skip one sample, this sample
01703                        is for the other channel.  */
01704                     samples += st;
01705                 }
01706             }
01707         }
01708 
01709         /* In the previous loop, in case stereo is used, samples is
01710            increased exactly one time too often.  */
01711         samples -= st;
01712         break;
01713     }
01714 
01715     default:
01716         return -1;
01717     }
01718     *data_size = (uint8_t *)samples - (uint8_t *)data;
01719     return src - buf;
01720 }
01721 
01722 
01723 
01724 #if CONFIG_ENCODERS
01725 #define ADPCM_ENCODER(id,name,long_name_)       \
01726 AVCodec ff_ ## name ## _encoder = {             \
01727     #name,                                      \
01728     AVMEDIA_TYPE_AUDIO,                         \
01729     id,                                         \
01730     sizeof(ADPCMContext),                       \
01731     adpcm_encode_init,                          \
01732     adpcm_encode_frame,                         \
01733     adpcm_encode_close,                         \
01734     NULL,                                       \
01735     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE}, \
01736     .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
01737 }
01738 #else
01739 #define ADPCM_ENCODER(id,name,long_name_)
01740 #endif
01741 
01742 #if CONFIG_DECODERS
01743 #define ADPCM_DECODER(id,name,long_name_)       \
01744 AVCodec ff_ ## name ## _decoder = {             \
01745     #name,                                      \
01746     AVMEDIA_TYPE_AUDIO,                         \
01747     id,                                         \
01748     sizeof(ADPCMContext),                       \
01749     adpcm_decode_init,                          \
01750     NULL,                                       \
01751     NULL,                                       \
01752     adpcm_decode_frame,                         \
01753     .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
01754 }
01755 #else
01756 #define ADPCM_DECODER(id,name,long_name_)
01757 #endif
01758 
01759 #define ADPCM_CODEC(id,name,long_name_)         \
01760     ADPCM_ENCODER(id,name,long_name_); ADPCM_DECODER(id,name,long_name_)
01761 
01762 /* Note: Do not forget to add new entries to the Makefile as well. */
01763 ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
01764 ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
01765 ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
01766 ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
01767 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
01768 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
01769 ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
01770 ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
01771 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
01772 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
01773 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
01774 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
01775 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
01776 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
01777 ADPCM_CODEC  (CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
01778 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
01779 ADPCM_CODEC  (CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
01780 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
01781 ADPCM_CODEC  (CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
01782 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
01783 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
01784 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
01785 ADPCM_CODEC  (CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
01786 ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
01787 ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
01788 ADPCM_CODEC  (CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");