• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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

Generated on Fri Feb 22 2013 07:24:25 for FFmpeg by  doxygen 1.7.1