Statistics
| Branch: | Revision:

ffmpeg / libavcodec / adpcm.c @ 35d3d44a

History | View | Annotate | Download (66.4 KB)

1
/*
2
 * ADPCM codecs
3
 * Copyright (c) 2001-2003 The ffmpeg Project
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avcodec.h"
22
#include "get_bits.h"
23
#include "put_bits.h"
24
#include "bytestream.h"
25

    
26
/**
27
 * @file
28
 * ADPCM codecs.
29
 * First version by Francois Revol (revol@free.fr)
30
 * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
31
 *   by Mike Melanson (melanson@pcisys.net)
32
 * CD-ROM XA ADPCM codec by BERO
33
 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
34
 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
35
 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
36
 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
37
 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
38
 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
39
 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
40
 *
41
 * Features and limitations:
42
 *
43
 * Reference documents:
44
 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
45
 * http://www.geocities.com/SiliconValley/8682/aud3.txt
46
 * http://openquicktime.sourceforge.net/plugins.htm
47
 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
48
 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
49
 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
50
 *
51
 * CD-ROM XA:
52
 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
53
 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
54
 * readstr http://www.geocities.co.jp/Playtown/2004/
55
 */
56

    
57
#define BLKSIZE 1024
58

    
59
/* step_table[] and index_table[] are from the ADPCM reference source */
60
/* This is the index table: */
61
static const int index_table[16] = {
62
    -1, -1, -1, -1, 2, 4, 6, 8,
63
    -1, -1, -1, -1, 2, 4, 6, 8,
64
};
65

    
66
/**
67
 * This is the step table. Note that many programs use slight deviations from
68
 * this table, but such deviations are negligible:
69
 */
70
static const int step_table[89] = {
71
    7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
72
    19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
73
    50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
74
    130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
75
    337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
76
    876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
77
    2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
78
    5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
79
    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
80
};
81

    
82
/* These are for MS-ADPCM */
83
/* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
84
static const int AdaptationTable[] = {
85
        230, 230, 230, 230, 307, 409, 512, 614,
86
        768, 614, 512, 409, 307, 230, 230, 230
87
};
88

    
89
/** Divided by 4 to fit in 8-bit integers */
90
static const uint8_t AdaptCoeff1[] = {
91
        64, 128, 0, 48, 60, 115, 98
92
};
93

    
94
/** Divided by 4 to fit in 8-bit integers */
95
static const int8_t AdaptCoeff2[] = {
96
        0, -64, 0, 16, 0, -52, -58
97
};
98

    
99
/* These are for CD-ROM XA ADPCM */
100
static const int xa_adpcm_table[5][2] = {
101
   {   0,   0 },
102
   {  60,   0 },
103
   { 115, -52 },
104
   {  98, -55 },
105
   { 122, -60 }
106
};
107

    
108
static const int ea_adpcm_table[] = {
109
    0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
110
    3, 4, 7, 8, 10, 11, 0, -1, -3, -4
111
};
112

    
113
// padded to zero where table size is less then 16
114
static const int swf_index_tables[4][16] = {
115
    /*2*/ { -1, 2 },
116
    /*3*/ { -1, -1, 2, 4 },
117
    /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
118
    /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
119
};
120

    
121
static const int yamaha_indexscale[] = {
122
    230, 230, 230, 230, 307, 409, 512, 614,
123
    230, 230, 230, 230, 307, 409, 512, 614
124
};
125

    
126
static const int yamaha_difflookup[] = {
127
    1, 3, 5, 7, 9, 11, 13, 15,
128
    -1, -3, -5, -7, -9, -11, -13, -15
129
};
130

    
131
/* end of tables */
132

    
133
typedef struct ADPCMChannelStatus {
134
    int predictor;
135
    short int step_index;
136
    int step;
137
    /* for encoding */
138
    int prev_sample;
139

    
140
    /* MS version */
141
    short sample1;
142
    short sample2;
143
    int coeff1;
144
    int coeff2;
145
    int idelta;
146
} ADPCMChannelStatus;
147

    
148
typedef struct TrellisPath {
149
    int nibble;
150
    int prev;
151
} TrellisPath;
152

    
153
typedef struct TrellisNode {
154
    uint32_t ssd;
155
    int path;
156
    int sample1;
157
    int sample2;
158
    int step;
159
} TrellisNode;
160

    
161
typedef struct ADPCMContext {
162
    ADPCMChannelStatus status[6];
163
    TrellisPath *paths;
164
    TrellisNode *node_buf;
165
    TrellisNode **nodep_buf;
166
    uint8_t *trellis_hash;
167
} ADPCMContext;
168

    
169
#define FREEZE_INTERVAL 128
170

    
171
/* XXX: implement encoding */
172

    
173
#if CONFIG_ENCODERS
174
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
175
{
176
    ADPCMContext *s = avctx->priv_data;
177
    uint8_t *extradata;
178
    int i;
179
    if (avctx->channels > 2)
180
        return -1; /* only stereo or mono =) */
181

    
182
    if(avctx->trellis && (unsigned)avctx->trellis > 16U){
183
        av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
184
        return -1;
185
    }
186

    
187
    if (avctx->trellis) {
188
        int frontier = 1 << avctx->trellis;
189
        int max_paths =  frontier * FREEZE_INTERVAL;
190
        FF_ALLOC_OR_GOTO(avctx, s->paths,     max_paths * sizeof(*s->paths), error);
191
        FF_ALLOC_OR_GOTO(avctx, s->node_buf,  2 * frontier * sizeof(*s->node_buf), error);
192
        FF_ALLOC_OR_GOTO(avctx, s->nodep_buf, 2 * frontier * sizeof(*s->nodep_buf), error);
193
        FF_ALLOC_OR_GOTO(avctx, s->trellis_hash, 65536 * sizeof(*s->trellis_hash), error);
194
    }
195

    
196
    switch(avctx->codec->id) {
197
    case CODEC_ID_ADPCM_IMA_WAV:
198
        avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
199
                                                             /* and we have 4 bytes per channel overhead */
200
        avctx->block_align = BLKSIZE;
201
        /* seems frame_size isn't taken into account... have to buffer the samples :-( */
202
        break;
203
    case CODEC_ID_ADPCM_IMA_QT:
204
        avctx->frame_size = 64;
205
        avctx->block_align = 34 * avctx->channels;
206
        break;
207
    case CODEC_ID_ADPCM_MS:
208
        avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
209
                                                             /* and we have 7 bytes per channel overhead */
210
        avctx->block_align = BLKSIZE;
211
        avctx->extradata_size = 32;
212
        extradata = avctx->extradata = av_malloc(avctx->extradata_size);
213
        if (!extradata)
214
            return AVERROR(ENOMEM);
215
        bytestream_put_le16(&extradata, avctx->frame_size);
216
        bytestream_put_le16(&extradata, 7); /* wNumCoef */
217
        for (i = 0; i < 7; i++) {
218
            bytestream_put_le16(&extradata, AdaptCoeff1[i] * 4);
219
            bytestream_put_le16(&extradata, AdaptCoeff2[i] * 4);
220
        }
221
        break;
222
    case CODEC_ID_ADPCM_YAMAHA:
223
        avctx->frame_size = BLKSIZE * avctx->channels;
224
        avctx->block_align = BLKSIZE;
225
        break;
226
    case CODEC_ID_ADPCM_SWF:
227
        if (avctx->sample_rate != 11025 &&
228
            avctx->sample_rate != 22050 &&
229
            avctx->sample_rate != 44100) {
230
            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
231
            goto error;
232
        }
233
        avctx->frame_size = 512 * (avctx->sample_rate / 11025);
234
        break;
235
    default:
236
        goto error;
237
    }
238

    
239
    avctx->coded_frame= avcodec_alloc_frame();
240
    avctx->coded_frame->key_frame= 1;
241

    
242
    return 0;
243
error:
244
    av_freep(&s->paths);
245
    av_freep(&s->node_buf);
246
    av_freep(&s->nodep_buf);
247
    av_freep(&s->trellis_hash);
248
    return -1;
249
}
250

    
251
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
252
{
253
    ADPCMContext *s = avctx->priv_data;
254
    av_freep(&avctx->coded_frame);
255
    av_freep(&s->paths);
256
    av_freep(&s->node_buf);
257
    av_freep(&s->nodep_buf);
258
    av_freep(&s->trellis_hash);
259

    
260
    return 0;
261
}
262

    
263

    
264
static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
265
{
266
    int delta = sample - c->prev_sample;
267
    int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
268
    c->prev_sample += ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
269
    c->prev_sample = av_clip_int16(c->prev_sample);
270
    c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
271
    return nibble;
272
}
273

    
274
static inline unsigned char adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, short sample)
275
{
276
    int delta = sample - c->prev_sample;
277
    int mask, step = step_table[c->step_index];
278
    int diff = step >> 3;
279
    int nibble = 0;
280

    
281
    if (delta < 0) {
282
        nibble = 8;
283
        delta = -delta;
284
    }
285

    
286
    for (mask = 4; mask;) {
287
        if (delta >= step) {
288
            nibble |= mask;
289
            delta -= step;
290
            diff += step;
291
        }
292
        step >>= 1;
293
        mask >>= 1;
294
    }
295

    
296
    if (nibble & 8)
297
        c->prev_sample -= diff;
298
    else
299
        c->prev_sample += diff;
300

    
301
    c->prev_sample = av_clip_int16(c->prev_sample);
302
    c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
303

    
304
    return nibble;
305
}
306

    
307
static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
308
{
309
    int predictor, nibble, bias;
310

    
311
    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
312

    
313
    nibble= sample - predictor;
314
    if(nibble>=0) bias= c->idelta/2;
315
    else          bias=-c->idelta/2;
316

    
317
    nibble= (nibble + bias) / c->idelta;
318
    nibble= av_clip(nibble, -8, 7)&0x0F;
319

    
320
    predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
321

    
322
    c->sample2 = c->sample1;
323
    c->sample1 = av_clip_int16(predictor);
324

    
325
    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
326
    if (c->idelta < 16) c->idelta = 16;
327

    
328
    return nibble;
329
}
330

    
331
static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
332
{
333
    int nibble, delta;
334

    
335
    if(!c->step) {
336
        c->predictor = 0;
337
        c->step = 127;
338
    }
339

    
340
    delta = sample - c->predictor;
341

    
342
    nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
343

    
344
    c->predictor += ((c->step * yamaha_difflookup[nibble]) / 8);
345
    c->predictor = av_clip_int16(c->predictor);
346
    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
347
    c->step = av_clip(c->step, 127, 24567);
348

    
349
    return nibble;
350
}
351

    
352
static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
353
                                   uint8_t *dst, ADPCMChannelStatus *c, int n)
354
{
355
    //FIXME 6% faster if frontier is a compile-time constant
356
    ADPCMContext *s = avctx->priv_data;
357
    const int frontier = 1 << avctx->trellis;
358
    const int stride = avctx->channels;
359
    const int version = avctx->codec->id;
360
    TrellisPath *paths = s->paths, *p;
361
    TrellisNode *node_buf = s->node_buf;
362
    TrellisNode **nodep_buf = s->nodep_buf;
363
    TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
364
    TrellisNode **nodes_next = nodep_buf + frontier;
365
    int pathn = 0, froze = -1, i, j, k, generation = 0;
366
    uint8_t *hash = s->trellis_hash;
367
    memset(hash, 0xff, 65536 * sizeof(*hash));
368

    
369
    memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
370
    nodes[0] = node_buf + frontier;
371
    nodes[0]->ssd = 0;
372
    nodes[0]->path = 0;
373
    nodes[0]->step = c->step_index;
374
    nodes[0]->sample1 = c->sample1;
375
    nodes[0]->sample2 = c->sample2;
376
    if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_IMA_QT) || (version == CODEC_ID_ADPCM_SWF))
377
        nodes[0]->sample1 = c->prev_sample;
378
    if(version == CODEC_ID_ADPCM_MS)
379
        nodes[0]->step = c->idelta;
380
    if(version == CODEC_ID_ADPCM_YAMAHA) {
381
        if(c->step == 0) {
382
            nodes[0]->step = 127;
383
            nodes[0]->sample1 = 0;
384
        } else {
385
            nodes[0]->step = c->step;
386
            nodes[0]->sample1 = c->predictor;
387
        }
388
    }
389

    
390
    for(i=0; i<n; i++) {
391
        TrellisNode *t = node_buf + frontier*(i&1);
392
        TrellisNode **u;
393
        int sample = samples[i*stride];
394
        int heap_pos = 0;
395
        memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
396
        for(j=0; j<frontier && nodes[j]; j++) {
397
            // higher j have higher ssd already, so they're likely to yield a suboptimal next sample too
398
            const int range = (j < frontier/2) ? 1 : 0;
399
            const int step = nodes[j]->step;
400
            int nidx;
401
            if(version == CODEC_ID_ADPCM_MS) {
402
                const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 64;
403
                const int div = (sample - predictor) / step;
404
                const int nmin = av_clip(div-range, -8, 6);
405
                const int nmax = av_clip(div+range, -7, 7);
406
                for(nidx=nmin; nidx<=nmax; nidx++) {
407
                    const int nibble = nidx & 0xf;
408
                    int dec_sample = predictor + nidx * step;
409
#define STORE_NODE(NAME, STEP_INDEX)\
410
                    int d;\
411
                    uint32_t ssd;\
412
                    int pos;\
413
                    TrellisNode *u;\
414
                    uint8_t *h;\
415
                    dec_sample = av_clip_int16(dec_sample);\
416
                    d = sample - dec_sample;\
417
                    ssd = nodes[j]->ssd + d*d;\
418
                    /* Check for wraparound, skip such samples completely. \
419
                     * Note, changing ssd to a 64 bit variable would be \
420
                     * simpler, avoiding this check, but it's slower on \
421
                     * x86 32 bit at the moment. */\
422
                    if (ssd < nodes[j]->ssd)\
423
                        goto next_##NAME;\
424
                    /* Collapse any two states with the same previous sample value. \
425
                     * One could also distinguish states by step and by 2nd to last
426
                     * sample, but the effects of that are negligible.
427
                     * Since nodes in the previous generation are iterated
428
                     * through a heap, they're roughly ordered from better to
429
                     * worse, but not strictly ordered. Therefore, an earlier
430
                     * node with the same sample value is better in most cases
431
                     * (and thus the current is skipped), but not strictly
432
                     * in all cases. Only skipping samples where ssd >=
433
                     * ssd of the earlier node with the same sample gives
434
                     * slightly worse quality, though, for some reason. */ \
435
                    h = &hash[(uint16_t) dec_sample];\
436
                    if (*h == generation)\
437
                        goto next_##NAME;\
438
                    if (heap_pos < frontier) {\
439
                        pos = heap_pos++;\
440
                    } else {\
441
                        /* Try to replace one of the leaf nodes with the new \
442
                         * one, but try a different slot each time. */\
443
                        pos = (frontier >> 1) + (heap_pos & ((frontier >> 1) - 1));\
444
                        if (ssd > nodes_next[pos]->ssd)\
445
                            goto next_##NAME;\
446
                        heap_pos++;\
447
                    }\
448
                    *h = generation;\
449
                    u = nodes_next[pos];\
450
                    if(!u) {\
451
                        assert(pathn < FREEZE_INTERVAL<<avctx->trellis);\
452
                        u = t++;\
453
                        nodes_next[pos] = u;\
454
                        u->path = pathn++;\
455
                    }\
456
                    u->ssd = ssd;\
457
                    u->step = STEP_INDEX;\
458
                    u->sample2 = nodes[j]->sample1;\
459
                    u->sample1 = dec_sample;\
460
                    paths[u->path].nibble = nibble;\
461
                    paths[u->path].prev = nodes[j]->path;\
462
                    /* Sift the newly inserted node up in the heap to \
463
                     * restore the heap property. */\
464
                    while (pos > 0) {\
465
                        int parent = (pos - 1) >> 1;\
466
                        if (nodes_next[parent]->ssd <= ssd)\
467
                            break;\
468
                        FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
469
                        pos = parent;\
470
                    }\
471
                    next_##NAME:;
472
                    STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
473
                }
474
            } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_IMA_QT)|| (version == CODEC_ID_ADPCM_SWF)) {
475
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
476
                const int predictor = nodes[j]->sample1;\
477
                const int div = (sample - predictor) * 4 / STEP_TABLE;\
478
                int nmin = av_clip(div-range, -7, 6);\
479
                int nmax = av_clip(div+range, -6, 7);\
480
                if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
481
                if(nmax<0) nmax--;\
482
                for(nidx=nmin; nidx<=nmax; nidx++) {\
483
                    const int nibble = nidx<0 ? 7-nidx : nidx;\
484
                    int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
485
                    STORE_NODE(NAME, STEP_INDEX);\
486
                }
487
                LOOP_NODES(ima, step_table[step], av_clip(step + index_table[nibble], 0, 88));
488
            } else { //CODEC_ID_ADPCM_YAMAHA
489
                LOOP_NODES(yamaha, step, av_clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
490
#undef LOOP_NODES
491
#undef STORE_NODE
492
            }
493
        }
494

    
495
        u = nodes;
496
        nodes = nodes_next;
497
        nodes_next = u;
498

    
499
        generation++;
500
        if (generation == 255) {
501
            memset(hash, 0xff, 65536 * sizeof(*hash));
502
            generation = 0;
503
        }
504

    
505
        // prevent overflow
506
        if(nodes[0]->ssd > (1<<28)) {
507
            for(j=1; j<frontier && nodes[j]; j++)
508
                nodes[j]->ssd -= nodes[0]->ssd;
509
            nodes[0]->ssd = 0;
510
        }
511

    
512
        // merge old paths to save memory
513
        if(i == froze + FREEZE_INTERVAL) {
514
            p = &paths[nodes[0]->path];
515
            for(k=i; k>froze; k--) {
516
                dst[k] = p->nibble;
517
                p = &paths[p->prev];
518
            }
519
            froze = i;
520
            pathn = 0;
521
            // other nodes might use paths that don't coincide with the frozen one.
522
            // checking which nodes do so is too slow, so just kill them all.
523
            // this also slightly improves quality, but I don't know why.
524
            memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
525
        }
526
    }
527

    
528
    p = &paths[nodes[0]->path];
529
    for(i=n-1; i>froze; i--) {
530
        dst[i] = p->nibble;
531
        p = &paths[p->prev];
532
    }
533

    
534
    c->predictor = nodes[0]->sample1;
535
    c->sample1 = nodes[0]->sample1;
536
    c->sample2 = nodes[0]->sample2;
537
    c->step_index = nodes[0]->step;
538
    c->step = nodes[0]->step;
539
    c->idelta = nodes[0]->step;
540
}
541

    
542
static int adpcm_encode_frame(AVCodecContext *avctx,
543
                            unsigned char *frame, int buf_size, void *data)
544
{
545
    int n, i, st;
546
    short *samples;
547
    unsigned char *dst;
548
    ADPCMContext *c = avctx->priv_data;
549
    uint8_t *buf;
550

    
551
    dst = frame;
552
    samples = (short *)data;
553
    st= avctx->channels == 2;
554
/*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
555

    
556
    switch(avctx->codec->id) {
557
    case CODEC_ID_ADPCM_IMA_WAV:
558
        n = avctx->frame_size / 8;
559
            c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
560
/*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
561
            bytestream_put_le16(&dst, c->status[0].prev_sample);
562
            *dst++ = (unsigned char)c->status[0].step_index;
563
            *dst++ = 0; /* unknown */
564
            samples++;
565
            if (avctx->channels == 2) {
566
                c->status[1].prev_sample = (signed short)samples[0];
567
/*                c->status[1].step_index = 0; */
568
                bytestream_put_le16(&dst, c->status[1].prev_sample);
569
                *dst++ = (unsigned char)c->status[1].step_index;
570
                *dst++ = 0;
571
                samples++;
572
            }
573

    
574
            /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
575
            if(avctx->trellis > 0) {
576
                FF_ALLOC_OR_GOTO(avctx, buf, 2*n*8, error);
577
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n*8);
578
                if(avctx->channels == 2)
579
                    adpcm_compress_trellis(avctx, samples+1, buf + n*8, &c->status[1], n*8);
580
                for(i=0; i<n; i++) {
581
                    *dst++ = buf[8*i+0] | (buf[8*i+1] << 4);
582
                    *dst++ = buf[8*i+2] | (buf[8*i+3] << 4);
583
                    *dst++ = buf[8*i+4] | (buf[8*i+5] << 4);
584
                    *dst++ = buf[8*i+6] | (buf[8*i+7] << 4);
585
                    if (avctx->channels == 2) {
586
                        uint8_t *buf1 = buf + n*8;
587
                        *dst++ = buf1[8*i+0] | (buf1[8*i+1] << 4);
588
                        *dst++ = buf1[8*i+2] | (buf1[8*i+3] << 4);
589
                        *dst++ = buf1[8*i+4] | (buf1[8*i+5] << 4);
590
                        *dst++ = buf1[8*i+6] | (buf1[8*i+7] << 4);
591
                    }
592
                }
593
                av_free(buf);
594
            } else
595
            for (; n>0; n--) {
596
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
597
                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
598
                dst++;
599
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
600
                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
601
                dst++;
602
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
603
                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
604
                dst++;
605
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
606
                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
607
                dst++;
608
                /* right channel */
609
                if (avctx->channels == 2) {
610
                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
611
                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
612
                    dst++;
613
                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
614
                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
615
                    dst++;
616
                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
617
                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
618
                    dst++;
619
                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
620
                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
621
                    dst++;
622
                }
623
                samples += 8 * avctx->channels;
624
            }
625
        break;
626
    case CODEC_ID_ADPCM_IMA_QT:
627
    {
628
        int ch, i;
629
        PutBitContext pb;
630
        init_put_bits(&pb, dst, buf_size*8);
631

    
632
        for(ch=0; ch<avctx->channels; ch++){
633
            put_bits(&pb, 9, (c->status[ch].prev_sample + 0x10000) >> 7);
634
            put_bits(&pb, 7, c->status[ch].step_index);
635
            if(avctx->trellis > 0) {
636
                uint8_t buf[64];
637
                adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
638
                for(i=0; i<64; i++)
639
                    put_bits(&pb, 4, buf[i^1]);
640
            } else {
641
                for (i=0; i<64; i+=2){
642
                    int t1, t2;
643
                    t1 = adpcm_ima_qt_compress_sample(&c->status[ch], samples[avctx->channels*(i+0)+ch]);
644
                    t2 = adpcm_ima_qt_compress_sample(&c->status[ch], samples[avctx->channels*(i+1)+ch]);
645
                    put_bits(&pb, 4, t2);
646
                    put_bits(&pb, 4, t1);
647
                }
648
            }
649
        }
650

    
651
        flush_put_bits(&pb);
652
        dst += put_bits_count(&pb)>>3;
653
        break;
654
    }
655
    case CODEC_ID_ADPCM_SWF:
656
    {
657
        int i;
658
        PutBitContext pb;
659
        init_put_bits(&pb, dst, buf_size*8);
660

    
661
        n = avctx->frame_size-1;
662

    
663
        //Store AdpcmCodeSize
664
        put_bits(&pb, 2, 2);                //Set 4bits flash adpcm format
665

    
666
        //Init the encoder state
667
        for(i=0; i<avctx->channels; i++){
668
            c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
669
            put_sbits(&pb, 16, samples[i]);
670
            put_bits(&pb, 6, c->status[i].step_index);
671
            c->status[i].prev_sample = (signed short)samples[i];
672
        }
673

    
674
        if(avctx->trellis > 0) {
675
            FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
676
            adpcm_compress_trellis(avctx, samples+2, buf, &c->status[0], n);
677
            if (avctx->channels == 2)
678
                adpcm_compress_trellis(avctx, samples+3, buf+n, &c->status[1], n);
679
            for(i=0; i<n; i++) {
680
                put_bits(&pb, 4, buf[i]);
681
                if (avctx->channels == 2)
682
                    put_bits(&pb, 4, buf[n+i]);
683
            }
684
            av_free(buf);
685
        } else {
686
            for (i=1; i<avctx->frame_size; i++) {
687
                put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
688
                if (avctx->channels == 2)
689
                    put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
690
            }
691
        }
692
        flush_put_bits(&pb);
693
        dst += put_bits_count(&pb)>>3;
694
        break;
695
    }
696
    case CODEC_ID_ADPCM_MS:
697
        for(i=0; i<avctx->channels; i++){
698
            int predictor=0;
699

    
700
            *dst++ = predictor;
701
            c->status[i].coeff1 = AdaptCoeff1[predictor];
702
            c->status[i].coeff2 = AdaptCoeff2[predictor];
703
        }
704
        for(i=0; i<avctx->channels; i++){
705
            if (c->status[i].idelta < 16)
706
                c->status[i].idelta = 16;
707

    
708
            bytestream_put_le16(&dst, c->status[i].idelta);
709
        }
710
        for(i=0; i<avctx->channels; i++){
711
            c->status[i].sample2= *samples++;
712
        }
713
        for(i=0; i<avctx->channels; i++){
714
            c->status[i].sample1= *samples++;
715

    
716
            bytestream_put_le16(&dst, c->status[i].sample1);
717
        }
718
        for(i=0; i<avctx->channels; i++)
719
            bytestream_put_le16(&dst, c->status[i].sample2);
720

    
721
        if(avctx->trellis > 0) {
722
            int n = avctx->block_align - 7*avctx->channels;
723
            FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
724
            if(avctx->channels == 1) {
725
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
726
                for(i=0; i<n; i+=2)
727
                    *dst++ = (buf[i] << 4) | buf[i+1];
728
            } else {
729
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
730
                adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
731
                for(i=0; i<n; i++)
732
                    *dst++ = (buf[i] << 4) | buf[n+i];
733
            }
734
            av_free(buf);
735
        } else
736
        for(i=7*avctx->channels; i<avctx->block_align; i++) {
737
            int nibble;
738
            nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
739
            nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
740
            *dst++ = nibble;
741
        }
742
        break;
743
    case CODEC_ID_ADPCM_YAMAHA:
744
        n = avctx->frame_size / 2;
745
        if(avctx->trellis > 0) {
746
            FF_ALLOC_OR_GOTO(avctx, buf, 2*n*2, error);
747
            n *= 2;
748
            if(avctx->channels == 1) {
749
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
750
                for(i=0; i<n; i+=2)
751
                    *dst++ = buf[i] | (buf[i+1] << 4);
752
            } else {
753
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
754
                adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
755
                for(i=0; i<n; i++)
756
                    *dst++ = buf[i] | (buf[n+i] << 4);
757
            }
758
            av_free(buf);
759
        } else
760
            for (n *= avctx->channels; n>0; n--) {
761
                int nibble;
762
                nibble  = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
763
                nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
764
                *dst++ = nibble;
765
            }
766
        break;
767
    default:
768
    error:
769
        return -1;
770
    }
771
    return dst - frame;
772
}
773
#endif //CONFIG_ENCODERS
774

    
775
static av_cold int adpcm_decode_init(AVCodecContext * avctx)
776
{
777
    ADPCMContext *c = avctx->priv_data;
778
    unsigned int max_channels = 2;
779

    
780
    switch(avctx->codec->id) {
781
    case CODEC_ID_ADPCM_EA_R1:
782
    case CODEC_ID_ADPCM_EA_R2:
783
    case CODEC_ID_ADPCM_EA_R3:
784
    case CODEC_ID_ADPCM_EA_XAS:
785
        max_channels = 6;
786
        break;
787
    }
788
    if(avctx->channels > max_channels){
789
        return -1;
790
    }
791

    
792
    switch(avctx->codec->id) {
793
    case CODEC_ID_ADPCM_CT:
794
        c->status[0].step = c->status[1].step = 511;
795
        break;
796
    case CODEC_ID_ADPCM_IMA_WAV:
797
        if (avctx->bits_per_coded_sample != 4) {
798
            av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
799
            return -1;
800
        }
801
        break;
802
    case CODEC_ID_ADPCM_IMA_WS:
803
        if (avctx->extradata && avctx->extradata_size == 2 * 4) {
804
            c->status[0].predictor = AV_RL32(avctx->extradata);
805
            c->status[1].predictor = AV_RL32(avctx->extradata + 4);
806
        }
807
        break;
808
    default:
809
        break;
810
    }
811
    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
812
    return 0;
813
}
814

    
815
static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
816
{
817
    int step_index;
818
    int predictor;
819
    int sign, delta, diff, step;
820

    
821
    step = step_table[c->step_index];
822
    step_index = c->step_index + index_table[(unsigned)nibble];
823
    if (step_index < 0) step_index = 0;
824
    else if (step_index > 88) step_index = 88;
825

    
826
    sign = nibble & 8;
827
    delta = nibble & 7;
828
    /* perform direct multiplication instead of series of jumps proposed by
829
     * the reference ADPCM implementation since modern CPUs can do the mults
830
     * quickly enough */
831
    diff = ((2 * delta + 1) * step) >> shift;
832
    predictor = c->predictor;
833
    if (sign) predictor -= diff;
834
    else predictor += diff;
835

    
836
    c->predictor = av_clip_int16(predictor);
837
    c->step_index = step_index;
838

    
839
    return (short)c->predictor;
840
}
841

    
842
static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
843
{
844
    int step_index;
845
    int predictor;
846
    int diff, step;
847

    
848
    step = step_table[c->step_index];
849
    step_index = c->step_index + index_table[nibble];
850
    step_index = av_clip(step_index, 0, 88);
851

    
852
    diff = step >> 3;
853
    if (nibble & 4) diff += step;
854
    if (nibble & 2) diff += step >> 1;
855
    if (nibble & 1) diff += step >> 2;
856

    
857
    if (nibble & 8)
858
        predictor = c->predictor - diff;
859
    else
860
        predictor = c->predictor + diff;
861

    
862
    c->predictor = av_clip_int16(predictor);
863
    c->step_index = step_index;
864

    
865
    return c->predictor;
866
}
867

    
868
static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
869
{
870
    int predictor;
871

    
872
    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
873
    predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
874

    
875
    c->sample2 = c->sample1;
876
    c->sample1 = av_clip_int16(predictor);
877
    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
878
    if (c->idelta < 16) c->idelta = 16;
879

    
880
    return c->sample1;
881
}
882

    
883
static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
884
{
885
    int sign, delta, diff;
886
    int new_step;
887

    
888
    sign = nibble & 8;
889
    delta = nibble & 7;
890
    /* perform direct multiplication instead of series of jumps proposed by
891
     * the reference ADPCM implementation since modern CPUs can do the mults
892
     * quickly enough */
893
    diff = ((2 * delta + 1) * c->step) >> 3;
894
    /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
895
    c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
896
    c->predictor = av_clip_int16(c->predictor);
897
    /* calculate new step and clamp it to range 511..32767 */
898
    new_step = (AdaptationTable[nibble & 7] * c->step) >> 8;
899
    c->step = av_clip(new_step, 511, 32767);
900

    
901
    return (short)c->predictor;
902
}
903

    
904
static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
905
{
906
    int sign, delta, diff;
907

    
908
    sign = nibble & (1<<(size-1));
909
    delta = nibble & ((1<<(size-1))-1);
910
    diff = delta << (7 + c->step + shift);
911

    
912
    /* clamp result */
913
    c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
914

    
915
    /* calculate new step */
916
    if (delta >= (2*size - 3) && c->step < 3)
917
        c->step++;
918
    else if (delta == 0 && c->step > 0)
919
        c->step--;
920

    
921
    return (short) c->predictor;
922
}
923

    
924
static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
925
{
926
    if(!c->step) {
927
        c->predictor = 0;
928
        c->step = 127;
929
    }
930

    
931
    c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
932
    c->predictor = av_clip_int16(c->predictor);
933
    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
934
    c->step = av_clip(c->step, 127, 24567);
935
    return c->predictor;
936
}
937

    
938
static void xa_decode(short *out, const unsigned char *in,
939
    ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
940
{
941
    int i, j;
942
    int shift,filter,f0,f1;
943
    int s_1,s_2;
944
    int d,s,t;
945

    
946
    for(i=0;i<4;i++) {
947

    
948
        shift  = 12 - (in[4+i*2] & 15);
949
        filter = in[4+i*2] >> 4;
950
        f0 = xa_adpcm_table[filter][0];
951
        f1 = xa_adpcm_table[filter][1];
952

    
953
        s_1 = left->sample1;
954
        s_2 = left->sample2;
955

    
956
        for(j=0;j<28;j++) {
957
            d = in[16+i+j*4];
958

    
959
            t = (signed char)(d<<4)>>4;
960
            s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
961
            s_2 = s_1;
962
            s_1 = av_clip_int16(s);
963
            *out = s_1;
964
            out += inc;
965
        }
966

    
967
        if (inc==2) { /* stereo */
968
            left->sample1 = s_1;
969
            left->sample2 = s_2;
970
            s_1 = right->sample1;
971
            s_2 = right->sample2;
972
            out = out + 1 - 28*2;
973
        }
974

    
975
        shift  = 12 - (in[5+i*2] & 15);
976
        filter = in[5+i*2] >> 4;
977

    
978
        f0 = xa_adpcm_table[filter][0];
979
        f1 = xa_adpcm_table[filter][1];
980

    
981
        for(j=0;j<28;j++) {
982
            d = in[16+i+j*4];
983

    
984
            t = (signed char)d >> 4;
985
            s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
986
            s_2 = s_1;
987
            s_1 = av_clip_int16(s);
988
            *out = s_1;
989
            out += inc;
990
        }
991

    
992
        if (inc==2) { /* stereo */
993
            right->sample1 = s_1;
994
            right->sample2 = s_2;
995
            out -= 1;
996
        } else {
997
            left->sample1 = s_1;
998
            left->sample2 = s_2;
999
        }
1000
    }
1001
}
1002

    
1003

    
1004
/* DK3 ADPCM support macro */
1005
#define DK3_GET_NEXT_NIBBLE() \
1006
    if (decode_top_nibble_next) \
1007
    { \
1008
        nibble = last_byte >> 4; \
1009
        decode_top_nibble_next = 0; \
1010
    } \
1011
    else \
1012
    { \
1013
        last_byte = *src++; \
1014
        if (src >= buf + buf_size) break; \
1015
        nibble = last_byte & 0x0F; \
1016
        decode_top_nibble_next = 1; \
1017
    }
1018

    
1019
static int adpcm_decode_frame(AVCodecContext *avctx,
1020
                            void *data, int *data_size,
1021
                            AVPacket *avpkt)
1022
{
1023
    const uint8_t *buf = avpkt->data;
1024
    int buf_size = avpkt->size;
1025
    ADPCMContext *c = avctx->priv_data;
1026
    ADPCMChannelStatus *cs;
1027
    int n, m, channel, i;
1028
    int block_predictor[2];
1029
    short *samples;
1030
    short *samples_end;
1031
    const uint8_t *src;
1032
    int st; /* stereo */
1033

    
1034
    /* DK3 ADPCM accounting variables */
1035
    unsigned char last_byte = 0;
1036
    unsigned char nibble;
1037
    int decode_top_nibble_next = 0;
1038
    int diff_channel;
1039

    
1040
    /* EA ADPCM state variables */
1041
    uint32_t samples_in_chunk;
1042
    int32_t previous_left_sample, previous_right_sample;
1043
    int32_t current_left_sample, current_right_sample;
1044
    int32_t next_left_sample, next_right_sample;
1045
    int32_t coeff1l, coeff2l, coeff1r, coeff2r;
1046
    uint8_t shift_left, shift_right;
1047
    int count1, count2;
1048
    int coeff[2][2], shift[2];//used in EA MAXIS ADPCM
1049

    
1050
    if (!buf_size)
1051
        return 0;
1052

    
1053
    //should protect all 4bit ADPCM variants
1054
    //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
1055
    //
1056
    if(*data_size/4 < buf_size + 8)
1057
        return -1;
1058

    
1059
    samples = data;
1060
    samples_end= samples + *data_size/2;
1061
    *data_size= 0;
1062
    src = buf;
1063

    
1064
    st = avctx->channels == 2 ? 1 : 0;
1065

    
1066
    switch(avctx->codec->id) {
1067
    case CODEC_ID_ADPCM_IMA_QT:
1068
        n = buf_size - 2*avctx->channels;
1069
        for (channel = 0; channel < avctx->channels; channel++) {
1070
            int16_t predictor;
1071
            int step_index;
1072
            cs = &(c->status[channel]);
1073
            /* (pppppp) (piiiiiii) */
1074

    
1075
            /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
1076
            predictor = AV_RB16(src);
1077
            step_index = predictor & 0x7F;
1078
            predictor &= 0xFF80;
1079

    
1080
            src += 2;
1081

    
1082
            if (cs->step_index == step_index) {
1083
                int diff = (int)predictor - cs->predictor;
1084
                if (diff < 0)
1085
                    diff = - diff;
1086
                if (diff > 0x7f)
1087
                    goto update;
1088
            } else {
1089
            update:
1090
                cs->step_index = step_index;
1091
                cs->predictor = predictor;
1092
            }
1093

    
1094
            if (cs->step_index > 88){
1095
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
1096
                cs->step_index = 88;
1097
            }
1098

    
1099
            samples = (short*)data + channel;
1100

    
1101
            for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
1102
                *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3);
1103
                samples += avctx->channels;
1104
                *samples = adpcm_ima_qt_expand_nibble(cs, src[0] >> 4  , 3);
1105
                samples += avctx->channels;
1106
                src ++;
1107
            }
1108
        }
1109
        if (st)
1110
            samples--;
1111
        break;
1112
    case CODEC_ID_ADPCM_IMA_WAV:
1113
        if (avctx->block_align != 0 && buf_size > avctx->block_align)
1114
            buf_size = avctx->block_align;
1115

    
1116
//        samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
1117

    
1118
        for(i=0; i<avctx->channels; i++){
1119
            cs = &(c->status[i]);
1120
            cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
1121

    
1122
            cs->step_index = *src++;
1123
            if (cs->step_index > 88){
1124
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
1125
                cs->step_index = 88;
1126
            }
1127
            if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
1128
        }
1129

    
1130
        while(src < buf + buf_size){
1131
            for(m=0; m<4; m++){
1132
                for(i=0; i<=st; i++)
1133
                    *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
1134
                for(i=0; i<=st; i++)
1135
                    *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4  , 3);
1136
                src++;
1137
            }
1138
            src += 4*st;
1139
        }
1140
        break;
1141
    case CODEC_ID_ADPCM_4XM:
1142
        cs = &(c->status[0]);
1143
        c->status[0].predictor= (int16_t)bytestream_get_le16(&src);
1144
        if(st){
1145
            c->status[1].predictor= (int16_t)bytestream_get_le16(&src);
1146
        }
1147
        c->status[0].step_index= (int16_t)bytestream_get_le16(&src);
1148
        if(st){
1149
            c->status[1].step_index= (int16_t)bytestream_get_le16(&src);
1150
        }
1151
        if (cs->step_index < 0) cs->step_index = 0;
1152
        if (cs->step_index > 88) cs->step_index = 88;
1153

    
1154
        m= (buf_size - (src - buf))>>st;
1155
        for(i=0; i<m; i++) {
1156
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
1157
            if (st)
1158
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
1159
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
1160
            if (st)
1161
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
1162
        }
1163

    
1164
        src += m<<st;
1165

    
1166
        break;
1167
    case CODEC_ID_ADPCM_MS:
1168
        if (avctx->block_align != 0 && buf_size > avctx->block_align)
1169
            buf_size = avctx->block_align;
1170
        n = buf_size - 7 * avctx->channels;
1171
        if (n < 0)
1172
            return -1;
1173
        block_predictor[0] = av_clip(*src++, 0, 6);
1174
        block_predictor[1] = 0;
1175
        if (st)
1176
            block_predictor[1] = av_clip(*src++, 0, 6);
1177
        c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
1178
        if (st){
1179
            c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
1180
        }
1181
        c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
1182
        c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
1183
        c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
1184
        c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
1185

    
1186
        c->status[0].sample1 = bytestream_get_le16(&src);
1187
        if (st) c->status[1].sample1 = bytestream_get_le16(&src);
1188
        c->status[0].sample2 = bytestream_get_le16(&src);
1189
        if (st) c->status[1].sample2 = bytestream_get_le16(&src);
1190

    
1191
        *samples++ = c->status[0].sample2;
1192
        if (st) *samples++ = c->status[1].sample2;
1193
        *samples++ = c->status[0].sample1;
1194
        if (st) *samples++ = c->status[1].sample1;
1195
        for(;n>0;n--) {
1196
            *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4  );
1197
            *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
1198
            src ++;
1199
        }
1200
        break;
1201
    case CODEC_ID_ADPCM_IMA_DK4:
1202
        if (avctx->block_align != 0 && buf_size > avctx->block_align)
1203
            buf_size = avctx->block_align;
1204

    
1205
        c->status[0].predictor  = (int16_t)bytestream_get_le16(&src);
1206
        c->status[0].step_index = *src++;
1207
        src++;
1208
        *samples++ = c->status[0].predictor;
1209
        if (st) {
1210
            c->status[1].predictor  = (int16_t)bytestream_get_le16(&src);
1211
            c->status[1].step_index = *src++;
1212
            src++;
1213
            *samples++ = c->status[1].predictor;
1214
        }
1215
        while (src < buf + buf_size) {
1216

    
1217
            /* take care of the top nibble (always left or mono channel) */
1218
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1219
                src[0] >> 4, 3);
1220

    
1221
            /* take care of the bottom nibble, which is right sample for
1222
             * stereo, or another mono sample */
1223
            if (st)
1224
                *samples++ = adpcm_ima_expand_nibble(&c->status[1],
1225
                    src[0] & 0x0F, 3);
1226
            else
1227
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1228
                    src[0] & 0x0F, 3);
1229

    
1230
            src++;
1231
        }
1232
        break;
1233
    case CODEC_ID_ADPCM_IMA_DK3:
1234
        if (avctx->block_align != 0 && buf_size > avctx->block_align)
1235
            buf_size = avctx->block_align;
1236

    
1237
        if(buf_size + 16 > (samples_end - samples)*3/8)
1238
            return -1;
1239

    
1240
        c->status[0].predictor  = (int16_t)AV_RL16(src + 10);
1241
        c->status[1].predictor  = (int16_t)AV_RL16(src + 12);
1242
        c->status[0].step_index = src[14];
1243
        c->status[1].step_index = src[15];
1244
        /* sign extend the predictors */
1245
        src += 16;
1246
        diff_channel = c->status[1].predictor;
1247

    
1248
        /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
1249
         * the buffer is consumed */
1250
        while (1) {
1251

    
1252
            /* for this algorithm, c->status[0] is the sum channel and
1253
             * c->status[1] is the diff channel */
1254

    
1255
            /* process the first predictor of the sum channel */
1256
            DK3_GET_NEXT_NIBBLE();
1257
            adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1258

    
1259
            /* process the diff channel predictor */
1260
            DK3_GET_NEXT_NIBBLE();
1261
            adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1262

    
1263
            /* process the first pair of stereo PCM samples */
1264
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
1265
            *samples++ = c->status[0].predictor + c->status[1].predictor;
1266
            *samples++ = c->status[0].predictor - c->status[1].predictor;
1267

    
1268
            /* process the second predictor of the sum channel */
1269
            DK3_GET_NEXT_NIBBLE();
1270
            adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1271

    
1272
            /* process the second pair of stereo PCM samples */
1273
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
1274
            *samples++ = c->status[0].predictor + c->status[1].predictor;
1275
            *samples++ = c->status[0].predictor - c->status[1].predictor;
1276
        }
1277
        break;
1278
    case CODEC_ID_ADPCM_IMA_ISS:
1279
        c->status[0].predictor  = (int16_t)AV_RL16(src + 0);
1280
        c->status[0].step_index = src[2];
1281
        src += 4;
1282
        if(st) {
1283
            c->status[1].predictor  = (int16_t)AV_RL16(src + 0);
1284
            c->status[1].step_index = src[2];
1285
            src += 4;
1286
        }
1287

    
1288
        while (src < buf + buf_size) {
1289

    
1290
            if (st) {
1291
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1292
                    src[0] >> 4  , 3);
1293
                *samples++ = adpcm_ima_expand_nibble(&c->status[1],
1294
                    src[0] & 0x0F, 3);
1295
            } else {
1296
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1297
                    src[0] & 0x0F, 3);
1298
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1299
                    src[0] >> 4  , 3);
1300
            }
1301

    
1302
            src++;
1303
        }
1304
        break;
1305
    case CODEC_ID_ADPCM_IMA_WS:
1306
        /* no per-block initialization; just start decoding the data */
1307
        while (src < buf + buf_size) {
1308

    
1309
            if (st) {
1310
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1311
                    src[0] >> 4  , 3);
1312
                *samples++ = adpcm_ima_expand_nibble(&c->status[1],
1313
                    src[0] & 0x0F, 3);
1314
            } else {
1315
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1316
                    src[0] >> 4  , 3);
1317
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1318
                    src[0] & 0x0F, 3);
1319
            }
1320

    
1321
            src++;
1322
        }
1323
        break;
1324
    case CODEC_ID_ADPCM_XA:
1325
        while (buf_size >= 128) {
1326
            xa_decode(samples, src, &c->status[0], &c->status[1],
1327
                avctx->channels);
1328
            src += 128;
1329
            samples += 28 * 8;
1330
            buf_size -= 128;
1331
        }
1332
        break;
1333
    case CODEC_ID_ADPCM_IMA_EA_EACS:
1334
        samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
1335

    
1336
        if (samples_in_chunk > buf_size-4-(8<<st)) {
1337
            src += buf_size - 4;
1338
            break;
1339
        }
1340

    
1341
        for (i=0; i<=st; i++)
1342
            c->status[i].step_index = bytestream_get_le32(&src);
1343
        for (i=0; i<=st; i++)
1344
            c->status[i].predictor  = bytestream_get_le32(&src);
1345

    
1346
        for (; samples_in_chunk; samples_in_chunk--, src++) {
1347
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  *src>>4,   3);
1348
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
1349
        }
1350
        break;
1351
    case CODEC_ID_ADPCM_IMA_EA_SEAD:
1352
        for (; src < buf+buf_size; src++) {
1353
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
1354
            *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
1355
        }
1356
        break;
1357
    case CODEC_ID_ADPCM_EA:
1358
        if (buf_size < 12 || AV_RL32(src) > (buf_size - 12)/30*28) {
1359
            src += buf_size;
1360
            break;
1361
        }
1362
        samples_in_chunk = AV_RL32(src);
1363
        src += 4;
1364
        current_left_sample   = (int16_t)bytestream_get_le16(&src);
1365
        previous_left_sample  = (int16_t)bytestream_get_le16(&src);
1366
        current_right_sample  = (int16_t)bytestream_get_le16(&src);
1367
        previous_right_sample = (int16_t)bytestream_get_le16(&src);
1368

    
1369
        for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
1370
            coeff1l = ea_adpcm_table[ *src >> 4       ];
1371
            coeff2l = ea_adpcm_table[(*src >> 4  ) + 4];
1372
            coeff1r = ea_adpcm_table[*src & 0x0F];
1373
            coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
1374
            src++;
1375

    
1376
            shift_left  = (*src >> 4  ) + 8;
1377
            shift_right = (*src & 0x0F) + 8;
1378
            src++;
1379

    
1380
            for (count2 = 0; count2 < 28; count2++) {
1381
                next_left_sample  = (int32_t)((*src & 0xF0) << 24) >> shift_left;
1382
                next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
1383
                src++;
1384

    
1385
                next_left_sample = (next_left_sample +
1386
                    (current_left_sample * coeff1l) +
1387
                    (previous_left_sample * coeff2l) + 0x80) >> 8;
1388
                next_right_sample = (next_right_sample +
1389
                    (current_right_sample * coeff1r) +
1390
                    (previous_right_sample * coeff2r) + 0x80) >> 8;
1391

    
1392
                previous_left_sample = current_left_sample;
1393
                current_left_sample = av_clip_int16(next_left_sample);
1394
                previous_right_sample = current_right_sample;
1395
                current_right_sample = av_clip_int16(next_right_sample);
1396
                *samples++ = (unsigned short)current_left_sample;
1397
                *samples++ = (unsigned short)current_right_sample;
1398
            }
1399
        }
1400

    
1401
        if (src - buf == buf_size - 2)
1402
            src += 2; // Skip terminating 0x0000
1403

    
1404
        break;
1405
    case CODEC_ID_ADPCM_EA_MAXIS_XA:
1406
        for(channel = 0; channel < avctx->channels; channel++) {
1407
            for (i=0; i<2; i++)
1408
                coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
1409
            shift[channel] = (*src & 0x0F) + 8;
1410
            src++;
1411
        }
1412
        for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
1413
            for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1414
                for(channel = 0; channel < avctx->channels; channel++) {
1415
                    int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
1416
                    sample = (sample +
1417
                             c->status[channel].sample1 * coeff[channel][0] +
1418
                             c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1419
                    c->status[channel].sample2 = c->status[channel].sample1;
1420
                    c->status[channel].sample1 = av_clip_int16(sample);
1421
                    *samples++ = c->status[channel].sample1;
1422
                }
1423
            }
1424
            src+=avctx->channels;
1425
        }
1426
        break;
1427
    case CODEC_ID_ADPCM_EA_R1:
1428
    case CODEC_ID_ADPCM_EA_R2:
1429
    case CODEC_ID_ADPCM_EA_R3: {
1430
        /* channel numbering
1431
           2chan: 0=fl, 1=fr
1432
           4chan: 0=fl, 1=rl, 2=fr, 3=rr
1433
           6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
1434
        const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
1435
        int32_t previous_sample, current_sample, next_sample;
1436
        int32_t coeff1, coeff2;
1437
        uint8_t shift;
1438
        unsigned int channel;
1439
        uint16_t *samplesC;
1440
        const uint8_t *srcC;
1441
        const uint8_t *src_end = buf + buf_size;
1442

    
1443
        samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
1444
                                       : bytestream_get_le32(&src)) / 28;
1445
        if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
1446
            28*samples_in_chunk*avctx->channels > samples_end-samples) {
1447
            src += buf_size - 4;
1448
            break;
1449
        }
1450

    
1451
        for (channel=0; channel<avctx->channels; channel++) {
1452
            int32_t offset = (big_endian ? bytestream_get_be32(&src)
1453
                                         : bytestream_get_le32(&src))
1454
                           + (avctx->channels-channel-1) * 4;
1455

    
1456
            if ((offset < 0) || (offset >= src_end - src - 4)) break;
1457
            srcC  = src + offset;
1458
            samplesC = samples + channel;
1459

    
1460
            if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
1461
                current_sample  = (int16_t)bytestream_get_le16(&srcC);
1462
                previous_sample = (int16_t)bytestream_get_le16(&srcC);
1463
            } else {
1464
                current_sample  = c->status[channel].predictor;
1465
                previous_sample = c->status[channel].prev_sample;
1466
            }
1467

    
1468
            for (count1=0; count1<samples_in_chunk; count1++) {
1469
                if (*srcC == 0xEE) {  /* only seen in R2 and R3 */
1470
                    srcC++;
1471
                    if (srcC > src_end - 30*2) break;
1472
                    current_sample  = (int16_t)bytestream_get_be16(&srcC);
1473
                    previous_sample = (int16_t)bytestream_get_be16(&srcC);
1474

    
1475
                    for (count2=0; count2<28; count2++) {
1476
                        *samplesC = (int16_t)bytestream_get_be16(&srcC);
1477
                        samplesC += avctx->channels;
1478
                    }
1479
                } else {
1480
                    coeff1 = ea_adpcm_table[ *srcC>>4     ];
1481
                    coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
1482
                    shift = (*srcC++ & 0x0F) + 8;
1483

    
1484
                    if (srcC > src_end - 14) break;
1485
                    for (count2=0; count2<28; count2++) {
1486
                        if (count2 & 1)
1487
                            next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
1488
                        else
1489
                            next_sample = (int32_t)((*srcC   & 0xF0) << 24) >> shift;
1490

    
1491
                        next_sample += (current_sample  * coeff1) +
1492
                                       (previous_sample * coeff2);
1493
                        next_sample = av_clip_int16(next_sample >> 8);
1494

    
1495
                        previous_sample = current_sample;
1496
                        current_sample  = next_sample;
1497
                        *samplesC = current_sample;
1498
                        samplesC += avctx->channels;
1499
                    }
1500
                }
1501
            }
1502

    
1503
            if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
1504
                c->status[channel].predictor   = current_sample;
1505
                c->status[channel].prev_sample = previous_sample;
1506
            }
1507
        }
1508

    
1509
        src = src + buf_size - (4 + 4*avctx->channels);
1510
        samples += 28 * samples_in_chunk * avctx->channels;
1511
        break;
1512
    }
1513
    case CODEC_ID_ADPCM_EA_XAS:
1514
        if (samples_end-samples < 32*4*avctx->channels
1515
            || buf_size < (4+15)*4*avctx->channels) {
1516
            src += buf_size;
1517
            break;
1518
        }
1519
        for (channel=0; channel<avctx->channels; channel++) {
1520
            int coeff[2][4], shift[4];
1521
            short *s2, *s = &samples[channel];
1522
            for (n=0; n<4; n++, s+=32*avctx->channels) {
1523
                for (i=0; i<2; i++)
1524
                    coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
1525
                shift[n] = (src[2]&0x0F) + 8;
1526
                for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
1527
                    s2[0] = (src[0]&0xF0) + (src[1]<<8);
1528
            }
1529

    
1530
            for (m=2; m<32; m+=2) {
1531
                s = &samples[m*avctx->channels + channel];
1532
                for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
1533
                    for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
1534
                        int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
1535
                        int pred  = s2[-1*avctx->channels] * coeff[0][n]
1536
                                  + s2[-2*avctx->channels] * coeff[1][n];
1537
                        s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
1538
                    }
1539
                }
1540
            }
1541
        }
1542
        samples += 32*4*avctx->channels;
1543
        break;
1544
    case CODEC_ID_ADPCM_IMA_AMV:
1545
    case CODEC_ID_ADPCM_IMA_SMJPEG:
1546
        c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
1547
        c->status[0].step_index = bytestream_get_le16(&src);
1548

    
1549
        if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
1550
            src+=4;
1551

    
1552
        while (src < buf + buf_size) {
1553
            char hi, lo;
1554
            lo = *src & 0x0F;
1555
            hi = *src >> 4;
1556

    
1557
            if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
1558
                FFSWAP(char, hi, lo);
1559

    
1560
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1561
                lo, 3);
1562
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1563
                hi, 3);
1564
            src++;
1565
        }
1566
        break;
1567
    case CODEC_ID_ADPCM_CT:
1568
        while (src < buf + buf_size) {
1569
            if (st) {
1570
                *samples++ = adpcm_ct_expand_nibble(&c->status[0],
1571
                    src[0] >> 4);
1572
                *samples++ = adpcm_ct_expand_nibble(&c->status[1],
1573
                    src[0] & 0x0F);
1574
            } else {
1575
                *samples++ = adpcm_ct_expand_nibble(&c->status[0],
1576
                    src[0] >> 4);
1577
                *samples++ = adpcm_ct_expand_nibble(&c->status[0],
1578
                    src[0] & 0x0F);
1579
            }
1580
            src++;
1581
        }
1582
        break;
1583
    case CODEC_ID_ADPCM_SBPRO_4:
1584
    case CODEC_ID_ADPCM_SBPRO_3:
1585
    case CODEC_ID_ADPCM_SBPRO_2:
1586
        if (!c->status[0].step_index) {
1587
            /* the first byte is a raw sample */
1588
            *samples++ = 128 * (*src++ - 0x80);
1589
            if (st)
1590
              *samples++ = 128 * (*src++ - 0x80);
1591
            c->status[0].step_index = 1;
1592
        }
1593
        if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1594
            while (src < buf + buf_size) {
1595
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1596
                    src[0] >> 4, 4, 0);
1597
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1598
                    src[0] & 0x0F, 4, 0);
1599
                src++;
1600
            }
1601
        } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
1602
            while (src < buf + buf_size && samples + 2 < samples_end) {
1603
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1604
                     src[0] >> 5        , 3, 0);
1605
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1606
                    (src[0] >> 2) & 0x07, 3, 0);
1607
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1608
                    src[0] & 0x03, 2, 0);
1609
                src++;
1610
            }
1611
        } else {
1612
            while (src < buf + buf_size && samples + 3 < samples_end) {
1613
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1614
                     src[0] >> 6        , 2, 2);
1615
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1616
                    (src[0] >> 4) & 0x03, 2, 2);
1617
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1618
                    (src[0] >> 2) & 0x03, 2, 2);
1619
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1620
                    src[0] & 0x03, 2, 2);
1621
                src++;
1622
            }
1623
        }
1624
        break;
1625
    case CODEC_ID_ADPCM_SWF:
1626
    {
1627
        GetBitContext gb;
1628
        const int *table;
1629
        int k0, signmask, nb_bits, count;
1630
        int size = buf_size*8;
1631

    
1632
        init_get_bits(&gb, buf, size);
1633

    
1634
        //read bits & initial values
1635
        nb_bits = get_bits(&gb, 2)+2;
1636
        //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
1637
        table = swf_index_tables[nb_bits-2];
1638
        k0 = 1 << (nb_bits-2);
1639
        signmask = 1 << (nb_bits-1);
1640

    
1641
        while (get_bits_count(&gb) <= size - 22*avctx->channels) {
1642
            for (i = 0; i < avctx->channels; i++) {
1643
                *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1644
                c->status[i].step_index = get_bits(&gb, 6);
1645
            }
1646

    
1647
            for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
1648
                int i;
1649

    
1650
                for (i = 0; i < avctx->channels; i++) {
1651
                    // similar to IMA adpcm
1652
                    int delta = get_bits(&gb, nb_bits);
1653
                    int step = step_table[c->status[i].step_index];
1654
                    long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1655
                    int k = k0;
1656

    
1657
                    do {
1658
                        if (delta & k)
1659
                            vpdiff += step;
1660
                        step >>= 1;
1661
                        k >>= 1;
1662
                    } while(k);
1663
                    vpdiff += step;
1664

    
1665
                    if (delta & signmask)
1666
                        c->status[i].predictor -= vpdiff;
1667
                    else
1668
                        c->status[i].predictor += vpdiff;
1669

    
1670
                    c->status[i].step_index += table[delta & (~signmask)];
1671

    
1672
                    c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1673
                    c->status[i].predictor = av_clip_int16(c->status[i].predictor);
1674

    
1675
                    *samples++ = c->status[i].predictor;
1676
                    if (samples >= samples_end) {
1677
                        av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
1678
                        return -1;
1679
                    }
1680
                }
1681
            }
1682
        }
1683
        src += buf_size;
1684
        break;
1685
    }
1686
    case CODEC_ID_ADPCM_YAMAHA:
1687
        while (src < buf + buf_size) {
1688
            if (st) {
1689
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1690
                        src[0] & 0x0F);
1691
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
1692
                        src[0] >> 4  );
1693
            } else {
1694
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1695
                        src[0] & 0x0F);
1696
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1697
                        src[0] >> 4  );
1698
            }
1699
            src++;
1700
        }
1701
        break;
1702
    case CODEC_ID_ADPCM_THP:
1703
    {
1704
        int table[2][16];
1705
        unsigned int samplecnt;
1706
        int prev[2][2];
1707
        int ch;
1708

    
1709
        if (buf_size < 80) {
1710
            av_log(avctx, AV_LOG_ERROR, "frame too small\n");
1711
            return -1;
1712
        }
1713

    
1714
        src+=4;
1715
        samplecnt = bytestream_get_be32(&src);
1716

    
1717
        for (i = 0; i < 32; i++)
1718
            table[0][i] = (int16_t)bytestream_get_be16(&src);
1719

    
1720
        /* Initialize the previous sample.  */
1721
        for (i = 0; i < 4; i++)
1722
            prev[0][i] = (int16_t)bytestream_get_be16(&src);
1723

    
1724
        if (samplecnt >= (samples_end - samples) /  (st + 1)) {
1725
            av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
1726
            return -1;
1727
        }
1728

    
1729
        for (ch = 0; ch <= st; ch++) {
1730
            samples = (unsigned short *) data + ch;
1731

    
1732
            /* Read in every sample for this channel.  */
1733
            for (i = 0; i < samplecnt / 14; i++) {
1734
                int index = (*src >> 4) & 7;
1735
                unsigned int exp = 28 - (*src++ & 15);
1736
                int factor1 = table[ch][index * 2];
1737
                int factor2 = table[ch][index * 2 + 1];
1738

    
1739
                /* Decode 14 samples.  */
1740
                for (n = 0; n < 14; n++) {
1741
                    int32_t sampledat;
1742
                    if(n&1) sampledat=  *src++    <<28;
1743
                    else    sampledat= (*src&0xF0)<<24;
1744

    
1745
                    sampledat = ((prev[ch][0]*factor1
1746
                                + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
1747
                    *samples = av_clip_int16(sampledat);
1748
                    prev[ch][1] = prev[ch][0];
1749
                    prev[ch][0] = *samples++;
1750

    
1751
                    /* In case of stereo, skip one sample, this sample
1752
                       is for the other channel.  */
1753
                    samples += st;
1754
                }
1755
            }
1756
        }
1757

    
1758
        /* In the previous loop, in case stereo is used, samples is
1759
           increased exactly one time too often.  */
1760
        samples -= st;
1761
        break;
1762
    }
1763

    
1764
    default:
1765
        return -1;
1766
    }
1767
    *data_size = (uint8_t *)samples - (uint8_t *)data;
1768
    return src - buf;
1769
}
1770

    
1771

    
1772

    
1773
#if CONFIG_ENCODERS
1774
#define ADPCM_ENCODER(id,name,long_name_)       \
1775
AVCodec ff_ ## name ## _encoder = {             \
1776
    #name,                                      \
1777
    AVMEDIA_TYPE_AUDIO,                         \
1778
    id,                                         \
1779
    sizeof(ADPCMContext),                       \
1780
    adpcm_encode_init,                          \
1781
    adpcm_encode_frame,                         \
1782
    adpcm_encode_close,                         \
1783
    NULL,                                       \
1784
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE}, \
1785
    .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1786
}
1787
#else
1788
#define ADPCM_ENCODER(id,name,long_name_)
1789
#endif
1790

    
1791
#if CONFIG_DECODERS
1792
#define ADPCM_DECODER(id,name,long_name_)       \
1793
AVCodec ff_ ## name ## _decoder = {             \
1794
    #name,                                      \
1795
    AVMEDIA_TYPE_AUDIO,                         \
1796
    id,                                         \
1797
    sizeof(ADPCMContext),                       \
1798
    adpcm_decode_init,                          \
1799
    NULL,                                       \
1800
    NULL,                                       \
1801
    adpcm_decode_frame,                         \
1802
    .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1803
}
1804
#else
1805
#define ADPCM_DECODER(id,name,long_name_)
1806
#endif
1807

    
1808
#define ADPCM_CODEC(id,name,long_name_)         \
1809
    ADPCM_ENCODER(id,name,long_name_); ADPCM_DECODER(id,name,long_name_)
1810

    
1811
/* Note: Do not forget to add new entries to the Makefile as well. */
1812
ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
1813
ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
1814
ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
1815
ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1816
ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1817
ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1818
ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1819
ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1820
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
1821
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1822
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1823
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1824
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1825
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1826
ADPCM_CODEC  (CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
1827
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1828
ADPCM_CODEC  (CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
1829
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
1830
ADPCM_CODEC  (CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
1831
ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1832
ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1833
ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1834
ADPCM_CODEC  (CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
1835
ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1836
ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
1837
ADPCM_CODEC  (CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");