~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/adpcm.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * ADPCM codecs
3
3
 * Copyright (c) 2001-2003 The ffmpeg Project
4
4
 *
5
 
 * This library is free software; you can redistribute it and/or
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or
6
8
 * modify it under the terms of the GNU Lesser General Public
7
9
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
 
10
 * version 2.1 of the License, or (at your option) any later version.
9
11
 *
10
 
 * This library is distributed in the hope that it will be useful,
 
12
 * FFmpeg is distributed in the hope that it will be useful,
11
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
15
 * Lesser General Public License for more details.
14
16
 *
15
17
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
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
18
20
 */
19
21
#include "avcodec.h"
 
22
#include "bitstream.h"
20
23
 
21
24
/**
22
25
 * @file adpcm.c
23
26
 * ADPCM codecs.
24
 
 * First version by Francois Revol revol@free.fr
25
 
 * Fringe ADPCM codecs (e.g., DK3 and DK4) 
 
27
 * First version by Francois Revol (revol@free.fr)
 
28
 * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
26
29
 *   by Mike Melanson (melanson@pcisys.net)
 
30
 * CD-ROM XA ADPCM codec by BERO
 
31
 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
27
32
 *
28
33
 * Features and limitations:
29
34
 *
34
39
 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
35
40
 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
36
41
 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
 
42
 *
 
43
 * CD-ROM XA:
 
44
 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
 
45
 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
 
46
 * readstr http://www.geocities.co.jp/Playtown/2004/
37
47
 */
38
48
 
39
49
#define BLKSIZE 1024
51
61
    -1, -1, -1, -1, 2, 4, 6, 8,
52
62
};
53
63
 
54
 
/** 
 
64
/**
55
65
 * This is the step table. Note that many programs use slight deviations from
56
66
 * this table, but such deviations are negligible:
57
67
 */
67
77
    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
68
78
};
69
79
 
70
 
/* Those are for MS-ADPCM */
 
80
/* These are for MS-ADPCM */
71
81
/* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
72
82
static const int AdaptationTable[] = {
73
83
        230, 230, 230, 230, 307, 409, 512, 614,
82
92
        0, -256, 0, 64, 0, -208, -232
83
93
};
84
94
 
 
95
/* These are for CD-ROM XA ADPCM */
 
96
static const int xa_adpcm_table[5][2] = {
 
97
   {   0,   0 },
 
98
   {  60,   0 },
 
99
   { 115, -52 },
 
100
   {  98, -55 },
 
101
   { 122, -60 }
 
102
};
 
103
 
 
104
static const int ea_adpcm_table[] = {
 
105
    0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
 
106
    3, 4, 7, 8, 10, 11, 0, -1, -3, -4
 
107
};
 
108
 
 
109
static const int ct_adpcm_table[8] = {
 
110
    0x00E6, 0x00E6, 0x00E6, 0x00E6,
 
111
    0x0133, 0x0199, 0x0200, 0x0266
 
112
};
 
113
 
 
114
// padded to zero where table size is less then 16
 
115
static const int swf_index_tables[4][16] = {
 
116
    /*2*/ { -1, 2 },
 
117
    /*3*/ { -1, -1, 2, 4 },
 
118
    /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
 
119
    /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
 
120
};
 
121
 
 
122
static const int yamaha_indexscale[] = {
 
123
    230, 230, 230, 230, 307, 409, 512, 614,
 
124
    230, 230, 230, 230, 307, 409, 512, 614
 
125
};
 
126
 
 
127
static const int yamaha_difflookup[] = {
 
128
    1, 3, 5, 7, 9, 11, 13, 15,
 
129
    -1, -3, -5, -7, -9, -11, -13, -15
 
130
};
 
131
 
85
132
/* end of tables */
86
133
 
87
134
typedef struct ADPCMChannelStatus {
107
154
 
108
155
/* XXX: implement encoding */
109
156
 
 
157
#ifdef CONFIG_ENCODERS
110
158
static int adpcm_encode_init(AVCodecContext *avctx)
111
159
{
112
160
    if (avctx->channels > 2)
113
161
        return -1; /* only stereo or mono =) */
114
162
    switch(avctx->codec->id) {
115
163
    case CODEC_ID_ADPCM_IMA_QT:
116
 
        fprintf(stderr, "ADPCM: codec admcp_ima_qt unsupported for encoding !\n");
 
164
        av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
117
165
        avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
118
166
        return -1;
119
167
        break;
124
172
        /* seems frame_size isn't taken into account... have to buffer the samples :-( */
125
173
        break;
126
174
    case CODEC_ID_ADPCM_MS:
127
 
        fprintf(stderr, "ADPCM: codec admcp_ms unsupported for encoding !\n");
128
 
        return -1;
 
175
        avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
 
176
                                                             /* and we have 7 bytes per channel overhead */
 
177
        avctx->block_align = BLKSIZE;
 
178
        break;
 
179
    case CODEC_ID_ADPCM_YAMAHA:
 
180
        avctx->frame_size = BLKSIZE * avctx->channels;
 
181
        avctx->block_align = BLKSIZE;
129
182
        break;
130
183
    default:
131
184
        return -1;
148
201
 
149
202
static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
150
203
{
151
 
    int step_index;
152
 
    unsigned char nibble;
153
 
    
154
 
    int sign = 0; /* sign bit of the nibble (MSB) */
155
 
    int delta, predicted_delta;
156
 
 
157
 
    delta = sample - c->prev_sample;
158
 
 
159
 
    if (delta < 0) {
160
 
        sign = 1;
161
 
        delta = -delta;
162
 
    }
163
 
 
164
 
    step_index = c->step_index;
165
 
 
166
 
    /* nibble = 4 * delta / step_table[step_index]; */
167
 
    nibble = (delta << 2) / step_table[step_index];
168
 
 
169
 
    if (nibble > 7)
170
 
        nibble = 7;
171
 
 
172
 
    step_index += index_table[nibble];
173
 
    if (step_index < 0)
174
 
        step_index = 0;
175
 
    if (step_index > 88)
176
 
        step_index = 88;
177
 
 
178
 
    /* what the decoder will find */
179
 
    predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
180
 
 
181
 
    if (sign)
182
 
        c->prev_sample -= predicted_delta;
183
 
    else
184
 
        c->prev_sample += predicted_delta;
185
 
 
 
204
    int delta = sample - c->prev_sample;
 
205
    int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
 
206
    c->prev_sample = c->prev_sample + ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
186
207
    CLAMP_TO_SHORT(c->prev_sample);
187
 
 
188
 
 
189
 
    nibble += sign << 3; /* sign * 8 */   
190
 
 
191
 
    /* save back */
192
 
    c->step_index = step_index;
193
 
 
194
 
    return nibble;
 
208
    c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
 
209
    return nibble;
 
210
}
 
211
 
 
212
static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
 
213
{
 
214
    int predictor, nibble, bias;
 
215
 
 
216
    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
 
217
 
 
218
    nibble= sample - predictor;
 
219
    if(nibble>=0) bias= c->idelta/2;
 
220
    else          bias=-c->idelta/2;
 
221
 
 
222
    nibble= (nibble + bias) / c->idelta;
 
223
    nibble= av_clip(nibble, -8, 7)&0x0F;
 
224
 
 
225
    predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
 
226
    CLAMP_TO_SHORT(predictor);
 
227
 
 
228
    c->sample2 = c->sample1;
 
229
    c->sample1 = predictor;
 
230
 
 
231
    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
 
232
    if (c->idelta < 16) c->idelta = 16;
 
233
 
 
234
    return nibble;
 
235
}
 
236
 
 
237
static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
 
238
{
 
239
    int nibble, delta;
 
240
 
 
241
    if(!c->step) {
 
242
        c->predictor = 0;
 
243
        c->step = 127;
 
244
    }
 
245
 
 
246
    delta = sample - c->predictor;
 
247
 
 
248
    nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
 
249
 
 
250
    c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8);
 
251
    CLAMP_TO_SHORT(c->predictor);
 
252
    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
 
253
    c->step = av_clip(c->step, 127, 24567);
 
254
 
 
255
    return nibble;
 
256
}
 
257
 
 
258
typedef struct TrellisPath {
 
259
    int nibble;
 
260
    int prev;
 
261
} TrellisPath;
 
262
 
 
263
typedef struct TrellisNode {
 
264
    uint32_t ssd;
 
265
    int path;
 
266
    int sample1;
 
267
    int sample2;
 
268
    int step;
 
269
} TrellisNode;
 
270
 
 
271
static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
 
272
                                   uint8_t *dst, ADPCMChannelStatus *c, int n)
 
273
{
 
274
#define FREEZE_INTERVAL 128
 
275
    //FIXME 6% faster if frontier is a compile-time constant
 
276
    const int frontier = 1 << avctx->trellis;
 
277
    const int stride = avctx->channels;
 
278
    const int version = avctx->codec->id;
 
279
    const int max_paths = frontier*FREEZE_INTERVAL;
 
280
    TrellisPath paths[max_paths], *p;
 
281
    TrellisNode node_buf[2][frontier];
 
282
    TrellisNode *nodep_buf[2][frontier];
 
283
    TrellisNode **nodes = nodep_buf[0]; // nodes[] is always sorted by .ssd
 
284
    TrellisNode **nodes_next = nodep_buf[1];
 
285
    int pathn = 0, froze = -1, i, j, k;
 
286
 
 
287
    assert(!(max_paths&(max_paths-1)));
 
288
 
 
289
    memset(nodep_buf, 0, sizeof(nodep_buf));
 
290
    nodes[0] = &node_buf[1][0];
 
291
    nodes[0]->ssd = 0;
 
292
    nodes[0]->path = 0;
 
293
    nodes[0]->step = c->step_index;
 
294
    nodes[0]->sample1 = c->sample1;
 
295
    nodes[0]->sample2 = c->sample2;
 
296
    if(version == CODEC_ID_ADPCM_IMA_WAV)
 
297
        nodes[0]->sample1 = c->prev_sample;
 
298
    if(version == CODEC_ID_ADPCM_MS)
 
299
        nodes[0]->step = c->idelta;
 
300
    if(version == CODEC_ID_ADPCM_YAMAHA) {
 
301
        if(c->step == 0) {
 
302
            nodes[0]->step = 127;
 
303
            nodes[0]->sample1 = 0;
 
304
        } else {
 
305
            nodes[0]->step = c->step;
 
306
            nodes[0]->sample1 = c->predictor;
 
307
        }
 
308
    }
 
309
 
 
310
    for(i=0; i<n; i++) {
 
311
        TrellisNode *t = node_buf[i&1];
 
312
        TrellisNode **u;
 
313
        int sample = samples[i*stride];
 
314
        memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
 
315
        for(j=0; j<frontier && nodes[j]; j++) {
 
316
            // higher j have higher ssd already, so they're unlikely to use a suboptimal next sample too
 
317
            const int range = (j < frontier/2) ? 1 : 0;
 
318
            const int step = nodes[j]->step;
 
319
            int nidx;
 
320
            if(version == CODEC_ID_ADPCM_MS) {
 
321
                const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 256;
 
322
                const int div = (sample - predictor) / step;
 
323
                const int nmin = av_clip(div-range, -8, 6);
 
324
                const int nmax = av_clip(div+range, -7, 7);
 
325
                for(nidx=nmin; nidx<=nmax; nidx++) {
 
326
                    const int nibble = nidx & 0xf;
 
327
                    int dec_sample = predictor + nidx * step;
 
328
#define STORE_NODE(NAME, STEP_INDEX)\
 
329
                    int d;\
 
330
                    uint32_t ssd;\
 
331
                    CLAMP_TO_SHORT(dec_sample);\
 
332
                    d = sample - dec_sample;\
 
333
                    ssd = nodes[j]->ssd + d*d;\
 
334
                    if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\
 
335
                        continue;\
 
336
                    /* Collapse any two states with the same previous sample value. \
 
337
                     * One could also distinguish states by step and by 2nd to last
 
338
                     * sample, but the effects of that are negligible. */\
 
339
                    for(k=0; k<frontier && nodes_next[k]; k++) {\
 
340
                        if(dec_sample == nodes_next[k]->sample1) {\
 
341
                            assert(ssd >= nodes_next[k]->ssd);\
 
342
                            goto next_##NAME;\
 
343
                        }\
 
344
                    }\
 
345
                    for(k=0; k<frontier; k++) {\
 
346
                        if(!nodes_next[k] || ssd < nodes_next[k]->ssd) {\
 
347
                            TrellisNode *u = nodes_next[frontier-1];\
 
348
                            if(!u) {\
 
349
                                assert(pathn < max_paths);\
 
350
                                u = t++;\
 
351
                                u->path = pathn++;\
 
352
                            }\
 
353
                            u->ssd = ssd;\
 
354
                            u->step = STEP_INDEX;\
 
355
                            u->sample2 = nodes[j]->sample1;\
 
356
                            u->sample1 = dec_sample;\
 
357
                            paths[u->path].nibble = nibble;\
 
358
                            paths[u->path].prev = nodes[j]->path;\
 
359
                            memmove(&nodes_next[k+1], &nodes_next[k], (frontier-k-1)*sizeof(TrellisNode*));\
 
360
                            nodes_next[k] = u;\
 
361
                            break;\
 
362
                        }\
 
363
                    }\
 
364
                    next_##NAME:;
 
365
                    STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
 
366
                }
 
367
            } else if(version == CODEC_ID_ADPCM_IMA_WAV) {
 
368
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
 
369
                const int predictor = nodes[j]->sample1;\
 
370
                const int div = (sample - predictor) * 4 / STEP_TABLE;\
 
371
                int nmin = av_clip(div-range, -7, 6);\
 
372
                int nmax = av_clip(div+range, -6, 7);\
 
373
                if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
 
374
                if(nmax<0) nmax--;\
 
375
                for(nidx=nmin; nidx<=nmax; nidx++) {\
 
376
                    const int nibble = nidx<0 ? 7-nidx : nidx;\
 
377
                    int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
 
378
                    STORE_NODE(NAME, STEP_INDEX);\
 
379
                }
 
380
                LOOP_NODES(ima, step_table[step], av_clip(step + index_table[nibble], 0, 88));
 
381
            } else { //CODEC_ID_ADPCM_YAMAHA
 
382
                LOOP_NODES(yamaha, step, av_clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
 
383
#undef LOOP_NODES
 
384
#undef STORE_NODE
 
385
            }
 
386
        }
 
387
 
 
388
        u = nodes;
 
389
        nodes = nodes_next;
 
390
        nodes_next = u;
 
391
 
 
392
        // prevent overflow
 
393
        if(nodes[0]->ssd > (1<<28)) {
 
394
            for(j=1; j<frontier && nodes[j]; j++)
 
395
                nodes[j]->ssd -= nodes[0]->ssd;
 
396
            nodes[0]->ssd = 0;
 
397
        }
 
398
 
 
399
        // merge old paths to save memory
 
400
        if(i == froze + FREEZE_INTERVAL) {
 
401
            p = &paths[nodes[0]->path];
 
402
            for(k=i; k>froze; k--) {
 
403
                dst[k] = p->nibble;
 
404
                p = &paths[p->prev];
 
405
            }
 
406
            froze = i;
 
407
            pathn = 0;
 
408
            // other nodes might use paths that don't coincide with the frozen one.
 
409
            // checking which nodes do so is too slow, so just kill them all.
 
410
            // this also slightly improves quality, but I don't know why.
 
411
            memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
 
412
        }
 
413
    }
 
414
 
 
415
    p = &paths[nodes[0]->path];
 
416
    for(i=n-1; i>froze; i--) {
 
417
        dst[i] = p->nibble;
 
418
        p = &paths[p->prev];
 
419
    }
 
420
 
 
421
    c->predictor = nodes[0]->sample1;
 
422
    c->sample1 = nodes[0]->sample1;
 
423
    c->sample2 = nodes[0]->sample2;
 
424
    c->step_index = nodes[0]->step;
 
425
    c->step = nodes[0]->step;
 
426
    c->idelta = nodes[0]->step;
195
427
}
196
428
 
197
429
static int adpcm_encode_frame(AVCodecContext *avctx,
198
 
                            unsigned char *frame, int buf_size, void *data)
 
430
                            unsigned char *frame, int buf_size, void *data)
199
431
{
200
 
    int n;
 
432
    int n, i, st;
201
433
    short *samples;
202
434
    unsigned char *dst;
203
435
    ADPCMContext *c = avctx->priv_data;
204
436
 
205
437
    dst = frame;
206
438
    samples = (short *)data;
 
439
    st= avctx->channels == 2;
207
440
/*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
208
441
 
209
442
    switch(avctx->codec->id) {
219
452
            *dst++ = 0; /* unknown */
220
453
            samples++;
221
454
            if (avctx->channels == 2) {
222
 
                c->status[1].prev_sample = (signed short)samples[0];
 
455
                c->status[1].prev_sample = (signed short)samples[1];
223
456
/*                c->status[1].step_index = 0; */
224
457
                *dst++ = (c->status[1].prev_sample) & 0xFF;
225
458
                *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
227
460
                *dst++ = 0;
228
461
                samples++;
229
462
            }
230
 
        
 
463
 
231
464
            /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
 
465
            if(avctx->trellis > 0) {
 
466
                uint8_t buf[2][n*8];
 
467
                adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n*8);
 
468
                if(avctx->channels == 2)
 
469
                    adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n*8);
 
470
                for(i=0; i<n; i++) {
 
471
                    *dst++ = buf[0][8*i+0] | (buf[0][8*i+1] << 4);
 
472
                    *dst++ = buf[0][8*i+2] | (buf[0][8*i+3] << 4);
 
473
                    *dst++ = buf[0][8*i+4] | (buf[0][8*i+5] << 4);
 
474
                    *dst++ = buf[0][8*i+6] | (buf[0][8*i+7] << 4);
 
475
                    if (avctx->channels == 2) {
 
476
                        *dst++ = buf[1][8*i+0] | (buf[1][8*i+1] << 4);
 
477
                        *dst++ = buf[1][8*i+2] | (buf[1][8*i+3] << 4);
 
478
                        *dst++ = buf[1][8*i+4] | (buf[1][8*i+5] << 4);
 
479
                        *dst++ = buf[1][8*i+6] | (buf[1][8*i+7] << 4);
 
480
                    }
 
481
                }
 
482
            } else
232
483
            for (; n>0; n--) {
233
484
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
234
485
                *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
260
511
                samples += 8 * avctx->channels;
261
512
            }
262
513
        break;
 
514
    case CODEC_ID_ADPCM_MS:
 
515
        for(i=0; i<avctx->channels; i++){
 
516
            int predictor=0;
 
517
 
 
518
            *dst++ = predictor;
 
519
            c->status[i].coeff1 = AdaptCoeff1[predictor];
 
520
            c->status[i].coeff2 = AdaptCoeff2[predictor];
 
521
        }
 
522
        for(i=0; i<avctx->channels; i++){
 
523
            if (c->status[i].idelta < 16)
 
524
                c->status[i].idelta = 16;
 
525
 
 
526
            *dst++ = c->status[i].idelta & 0xFF;
 
527
            *dst++ = c->status[i].idelta >> 8;
 
528
        }
 
529
        for(i=0; i<avctx->channels; i++){
 
530
            c->status[i].sample1= *samples++;
 
531
 
 
532
            *dst++ = c->status[i].sample1 & 0xFF;
 
533
            *dst++ = c->status[i].sample1 >> 8;
 
534
        }
 
535
        for(i=0; i<avctx->channels; i++){
 
536
            c->status[i].sample2= *samples++;
 
537
 
 
538
            *dst++ = c->status[i].sample2 & 0xFF;
 
539
            *dst++ = c->status[i].sample2 >> 8;
 
540
        }
 
541
 
 
542
        if(avctx->trellis > 0) {
 
543
            int n = avctx->block_align - 7*avctx->channels;
 
544
            uint8_t buf[2][n];
 
545
            if(avctx->channels == 1) {
 
546
                n *= 2;
 
547
                adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
 
548
                for(i=0; i<n; i+=2)
 
549
                    *dst++ = (buf[0][i] << 4) | buf[0][i+1];
 
550
            } else {
 
551
                adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
 
552
                adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
 
553
                for(i=0; i<n; i++)
 
554
                    *dst++ = (buf[0][i] << 4) | buf[1][i];
 
555
            }
 
556
        } else
 
557
        for(i=7*avctx->channels; i<avctx->block_align; i++) {
 
558
            int nibble;
 
559
            nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
 
560
            nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
 
561
            *dst++ = nibble;
 
562
        }
 
563
        break;
 
564
    case CODEC_ID_ADPCM_YAMAHA:
 
565
        n = avctx->frame_size / 2;
 
566
        if(avctx->trellis > 0) {
 
567
            uint8_t buf[2][n*2];
 
568
            n *= 2;
 
569
            if(avctx->channels == 1) {
 
570
                adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
 
571
                for(i=0; i<n; i+=2)
 
572
                    *dst++ = buf[0][i] | (buf[0][i+1] << 4);
 
573
            } else {
 
574
                adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
 
575
                adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
 
576
                for(i=0; i<n; i++)
 
577
                    *dst++ = buf[0][i] | (buf[1][i] << 4);
 
578
            }
 
579
        } else
 
580
        for (; n>0; n--) {
 
581
            for(i = 0; i < avctx->channels; i++) {
 
582
                int nibble;
 
583
                nibble  = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
 
584
                nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
 
585
                *dst++ = nibble;
 
586
            }
 
587
            samples += 2 * avctx->channels;
 
588
        }
 
589
        break;
263
590
    default:
264
591
        return -1;
265
592
    }
266
593
    return dst - frame;
267
594
}
 
595
#endif //CONFIG_ENCODERS
268
596
 
269
597
static int adpcm_decode_init(AVCodecContext * avctx)
270
598
{
271
599
    ADPCMContext *c = avctx->priv_data;
272
600
 
 
601
    if(avctx->channels > 2U){
 
602
        return -1;
 
603
    }
 
604
 
273
605
    c->channel = 0;
274
606
    c->status[0].predictor = c->status[1].predictor = 0;
275
607
    c->status[0].step_index = c->status[1].step_index = 0;
276
608
    c->status[0].step = c->status[1].step = 0;
277
609
 
278
610
    switch(avctx->codec->id) {
 
611
    case CODEC_ID_ADPCM_CT:
 
612
        c->status[0].step = c->status[1].step = 511;
 
613
        break;
279
614
    default:
280
615
        break;
281
616
    }
282
617
    return 0;
283
618
}
284
619
 
285
 
static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
 
620
static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
286
621
{
287
622
    int step_index;
288
623
    int predictor;
298
633
    /* perform direct multiplication instead of series of jumps proposed by
299
634
     * the reference ADPCM implementation since modern CPUs can do the mults
300
635
     * quickly enough */
301
 
    diff = ((2 * delta + 1) * step) >> 3;
302
 
    predictor = c->predictor;
303
 
    if (sign) predictor -= diff;
304
 
    else predictor += diff;
305
 
 
306
 
    CLAMP_TO_SHORT(predictor);
307
 
    c->predictor = predictor;
308
 
    c->step_index = step_index;
309
 
 
310
 
    return (short)predictor;
311
 
}
312
 
 
313
 
static inline short adpcm_4xa_expand_nibble(ADPCMChannelStatus *c, char nibble)
314
 
{
315
 
    int step_index;
316
 
    int predictor;
317
 
    int sign, delta, diff, step;
318
 
 
319
 
    step = step_table[c->step_index];
320
 
    step_index = c->step_index + index_table[(unsigned)nibble];
321
 
    if (step_index < 0) step_index = 0;
322
 
    else if (step_index > 88) step_index = 88;
323
 
 
324
 
    sign = nibble & 8;
325
 
    delta = nibble & 7;
326
 
    
327
 
    diff = (delta*step + (step>>1))>>3; // difference to code above
328
 
    
 
636
    diff = ((2 * delta + 1) * step) >> shift;
329
637
    predictor = c->predictor;
330
638
    if (sign) predictor -= diff;
331
639
    else predictor += diff;
347
655
 
348
656
    c->sample2 = c->sample1;
349
657
    c->sample1 = predictor;
350
 
    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
 
658
    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
351
659
    if (c->idelta < 16) c->idelta = 16;
352
660
 
353
661
    return (short)predictor;
354
662
}
355
663
 
 
664
static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
 
665
{
 
666
    int predictor;
 
667
    int sign, delta, diff;
 
668
    int new_step;
 
669
 
 
670
    sign = nibble & 8;
 
671
    delta = nibble & 7;
 
672
    /* perform direct multiplication instead of series of jumps proposed by
 
673
     * the reference ADPCM implementation since modern CPUs can do the mults
 
674
     * quickly enough */
 
675
    diff = ((2 * delta + 1) * c->step) >> 3;
 
676
    predictor = c->predictor;
 
677
    /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
 
678
    if(sign)
 
679
        predictor = ((predictor * 254) >> 8) - diff;
 
680
    else
 
681
            predictor = ((predictor * 254) >> 8) + diff;
 
682
    /* calculate new step and clamp it to range 511..32767 */
 
683
    new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
 
684
    c->step = new_step;
 
685
    if(c->step < 511)
 
686
        c->step = 511;
 
687
    if(c->step > 32767)
 
688
        c->step = 32767;
 
689
 
 
690
    CLAMP_TO_SHORT(predictor);
 
691
    c->predictor = predictor;
 
692
    return (short)predictor;
 
693
}
 
694
 
 
695
static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
 
696
{
 
697
    int sign, delta, diff;
 
698
 
 
699
    sign = nibble & (1<<(size-1));
 
700
    delta = nibble & ((1<<(size-1))-1);
 
701
    diff = delta << (7 + c->step + shift);
 
702
 
 
703
    if (sign)
 
704
        c->predictor -= diff;
 
705
    else
 
706
        c->predictor += diff;
 
707
 
 
708
    /* clamp result */
 
709
    if (c->predictor > 16256)
 
710
        c->predictor = 16256;
 
711
    else if (c->predictor < -16384)
 
712
        c->predictor = -16384;
 
713
 
 
714
    /* calculate new step */
 
715
    if (delta >= (2*size - 3) && c->step < 3)
 
716
        c->step++;
 
717
    else if (delta == 0 && c->step > 0)
 
718
        c->step--;
 
719
 
 
720
    return (short) c->predictor;
 
721
}
 
722
 
 
723
static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
 
724
{
 
725
    if(!c->step) {
 
726
        c->predictor = 0;
 
727
        c->step = 127;
 
728
    }
 
729
 
 
730
    c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
 
731
    CLAMP_TO_SHORT(c->predictor);
 
732
    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
 
733
    c->step = av_clip(c->step, 127, 24567);
 
734
    return c->predictor;
 
735
}
 
736
 
 
737
static void xa_decode(short *out, const unsigned char *in,
 
738
    ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
 
739
{
 
740
    int i, j;
 
741
    int shift,filter,f0,f1;
 
742
    int s_1,s_2;
 
743
    int d,s,t;
 
744
 
 
745
    for(i=0;i<4;i++) {
 
746
 
 
747
        shift  = 12 - (in[4+i*2] & 15);
 
748
        filter = in[4+i*2] >> 4;
 
749
        f0 = xa_adpcm_table[filter][0];
 
750
        f1 = xa_adpcm_table[filter][1];
 
751
 
 
752
        s_1 = left->sample1;
 
753
        s_2 = left->sample2;
 
754
 
 
755
        for(j=0;j<28;j++) {
 
756
            d = in[16+i+j*4];
 
757
 
 
758
            t = (signed char)(d<<4)>>4;
 
759
            s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
 
760
            CLAMP_TO_SHORT(s);
 
761
            *out = s;
 
762
            out += inc;
 
763
            s_2 = s_1;
 
764
            s_1 = s;
 
765
        }
 
766
 
 
767
        if (inc==2) { /* stereo */
 
768
            left->sample1 = s_1;
 
769
            left->sample2 = s_2;
 
770
            s_1 = right->sample1;
 
771
            s_2 = right->sample2;
 
772
            out = out + 1 - 28*2;
 
773
        }
 
774
 
 
775
        shift  = 12 - (in[5+i*2] & 15);
 
776
        filter = in[5+i*2] >> 4;
 
777
 
 
778
        f0 = xa_adpcm_table[filter][0];
 
779
        f1 = xa_adpcm_table[filter][1];
 
780
 
 
781
        for(j=0;j<28;j++) {
 
782
            d = in[16+i+j*4];
 
783
 
 
784
            t = (signed char)d >> 4;
 
785
            s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
 
786
            CLAMP_TO_SHORT(s);
 
787
            *out = s;
 
788
            out += inc;
 
789
            s_2 = s_1;
 
790
            s_1 = s;
 
791
        }
 
792
 
 
793
        if (inc==2) { /* stereo */
 
794
            right->sample1 = s_1;
 
795
            right->sample2 = s_2;
 
796
            out -= 1;
 
797
        } else {
 
798
            left->sample1 = s_1;
 
799
            left->sample2 = s_2;
 
800
        }
 
801
    }
 
802
}
 
803
 
 
804
 
356
805
/* DK3 ADPCM support macro */
357
806
#define DK3_GET_NEXT_NIBBLE() \
358
807
    if (decode_top_nibble_next) \
369
818
    }
370
819
 
371
820
static int adpcm_decode_frame(AVCodecContext *avctx,
372
 
                            void *data, int *data_size,
373
 
                            uint8_t *buf, int buf_size)
 
821
                            void *data, int *data_size,
 
822
                            uint8_t *buf, int buf_size)
374
823
{
375
824
    ADPCMContext *c = avctx->priv_data;
376
825
    ADPCMChannelStatus *cs;
377
826
    int n, m, channel, i;
378
827
    int block_predictor[2];
379
828
    short *samples;
 
829
    short *samples_end;
380
830
    uint8_t *src;
381
831
    int st; /* stereo */
382
832
 
386
836
    int decode_top_nibble_next = 0;
387
837
    int diff_channel;
388
838
 
 
839
    /* EA ADPCM state variables */
 
840
    uint32_t samples_in_chunk;
 
841
    int32_t previous_left_sample, previous_right_sample;
 
842
    int32_t current_left_sample, current_right_sample;
 
843
    int32_t next_left_sample, next_right_sample;
 
844
    int32_t coeff1l, coeff2l, coeff1r, coeff2r;
 
845
    uint8_t shift_left, shift_right;
 
846
    int count1, count2;
 
847
 
 
848
    if (!buf_size)
 
849
        return 0;
 
850
 
 
851
    //should protect all 4bit ADPCM variants
 
852
    //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
 
853
    //
 
854
    if(*data_size/4 < buf_size + 8)
 
855
        return -1;
 
856
 
389
857
    samples = data;
 
858
    samples_end= samples + *data_size/2;
 
859
    *data_size= 0;
390
860
    src = buf;
391
861
 
392
 
    st = avctx->channels == 2;
 
862
    st = avctx->channels == 2 ? 1 : 0;
393
863
 
394
864
    switch(avctx->codec->id) {
395
865
    case CODEC_ID_ADPCM_IMA_QT:
411
881
 
412
882
        cs->step_index = (*src++) & 0x7F;
413
883
 
414
 
        if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
415
 
        if (cs->step_index > 88) cs->step_index = 88;
 
884
        if (cs->step_index > 88){
 
885
            av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
 
886
            cs->step_index = 88;
 
887
        }
416
888
 
417
889
        cs->step = step_table[cs->step_index];
418
890
 
419
891
        if (st && channel)
420
892
            samples++;
421
893
 
422
 
        *samples++ = cs->predictor;
423
 
        samples += st;
424
 
 
425
894
        for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
426
 
            *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
 
895
            *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
427
896
            samples += avctx->channels;
428
 
            *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
 
897
            *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
429
898
            samples += avctx->channels;
430
899
            src ++;
431
900
        }
432
901
 
433
902
        if(st) { /* handle stereo interlacing */
434
903
            c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
435
 
            if(channel == 0) { /* wait for the other packet before outputing anything */
436
 
                *data_size = 0;
 
904
            if(channel == 1) { /* wait for the other packet before outputing anything */
437
905
                return src - buf;
438
906
            }
439
907
        }
440
908
        break;
441
909
    case CODEC_ID_ADPCM_IMA_WAV:
442
 
        if (buf_size > BLKSIZE) {
443
 
            if (avctx->block_align != 0)
444
 
                buf_size = avctx->block_align;
445
 
            else
446
 
                buf_size = BLKSIZE;
447
 
        }
448
 
        // XXX: do as per-channel loop
449
 
        cs = &(c->status[0]);
450
 
        cs->predictor = (*src++) & 0x0FF;
451
 
        cs->predictor |= ((*src++) << 8) & 0x0FF00;
452
 
        if(cs->predictor & 0x8000)
453
 
            cs->predictor -= 0x10000;
454
 
        CLAMP_TO_SHORT(cs->predictor);
455
 
 
456
 
        // XXX: is this correct ??: *samples++ = cs->predictor;
457
 
 
458
 
        cs->step_index = *src++;
459
 
        if (cs->step_index < 0) cs->step_index = 0;
460
 
        if (cs->step_index > 88) cs->step_index = 88;
461
 
        if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
462
 
 
463
 
        if (st) {
464
 
            cs = &(c->status[1]);
465
 
            cs->predictor = (*src++) & 0x0FF;
466
 
            cs->predictor |= ((*src++) << 8) & 0x0FF00;
467
 
            if(cs->predictor & 0x8000)
468
 
                cs->predictor -= 0x10000;
469
 
            CLAMP_TO_SHORT(cs->predictor);
470
 
 
471
 
            // XXX: is this correct ??: *samples++ = cs->predictor;
472
 
 
473
 
            cs->step_index = *src++;
474
 
            if (cs->step_index < 0) cs->step_index = 0;
475
 
            if (cs->step_index > 88) cs->step_index = 88;
476
 
            src++; /* if != 0  -> out-of-sync */
477
 
        }
478
 
 
479
 
        for(m=4; src < (buf + buf_size);) {
480
 
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
481
 
            if (st)
482
 
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
483
 
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
484
 
            if (st) {
485
 
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
486
 
                if (!--m) {
487
 
                    m=4;
488
 
                    src+=4;
489
 
                }
490
 
            }
491
 
            src++;
492
 
        }
 
910
        if (avctx->block_align != 0 && buf_size > avctx->block_align)
 
911
            buf_size = avctx->block_align;
 
912
 
 
913
//        samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
 
914
 
 
915
        for(i=0; i<avctx->channels; i++){
 
916
            cs = &(c->status[i]);
 
917
            cs->predictor = (int16_t)(src[0] + (src[1]<<8));
 
918
            src+=2;
 
919
 
 
920
        // XXX: is this correct ??: *samples++ = cs->predictor;
 
921
 
 
922
            cs->step_index = *src++;
 
923
            if (cs->step_index > 88){
 
924
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
 
925
                cs->step_index = 88;
 
926
            }
 
927
            if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
 
928
        }
 
929
 
 
930
        while(src < buf + buf_size){
 
931
            for(m=0; m<4; m++){
 
932
                for(i=0; i<=st; i++)
 
933
                    *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
 
934
                for(i=0; i<=st; i++)
 
935
                    *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4  , 3);
 
936
                src++;
 
937
            }
 
938
            src += 4*st;
 
939
        }
493
940
        break;
494
941
    case CODEC_ID_ADPCM_4XM:
495
942
        cs = &(c->status[0]);
501
948
        if(st){
502
949
            c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
503
950
        }
504
 
//            if (cs->step_index < 0) cs->step_index = 0;
505
 
//            if (cs->step_index > 88) cs->step_index = 88;
 
951
        if (cs->step_index < 0) cs->step_index = 0;
 
952
        if (cs->step_index > 88) cs->step_index = 88;
506
953
 
507
954
        m= (buf_size - (src - buf))>>st;
508
 
//printf("%d %d %d %d\n", st, m, c->status[0].predictor, c->status[0].step_index);
509
 
        //FIXME / XXX decode chanels individual & interleave samples
510
955
        for(i=0; i<m; i++) {
511
 
            *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] & 0x0F);
512
 
            if (st)
513
 
                *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F);
514
 
            *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4);
515
 
            if (st)
516
 
                *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4);
517
 
        }
 
956
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
 
957
            if (st)
 
958
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
 
959
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
 
960
            if (st)
 
961
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
 
962
        }
518
963
 
519
964
        src += m<<st;
520
965
 
521
966
        break;
522
967
    case CODEC_ID_ADPCM_MS:
523
 
 
524
 
        if (buf_size > BLKSIZE) {
525
 
            if (avctx->block_align != 0)
526
 
                buf_size = avctx->block_align;
527
 
            else
528
 
                buf_size = BLKSIZE;
529
 
        }
 
968
        if (avctx->block_align != 0 && buf_size > avctx->block_align)
 
969
            buf_size = avctx->block_align;
530
970
        n = buf_size - 7 * avctx->channels;
531
971
        if (n < 0)
532
972
            return -1;
533
 
        block_predictor[0] = (*src++); /* should be bound */
534
 
        block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
 
973
        block_predictor[0] = av_clip(*src++, 0, 7);
535
974
        block_predictor[1] = 0;
536
975
        if (st)
537
 
            block_predictor[1] = (*src++);
538
 
        block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
539
 
        c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
540
 
        if (c->status[0].idelta & 0x08000)
541
 
            c->status[0].idelta -= 0x10000;
 
976
            block_predictor[1] = av_clip(*src++, 0, 7);
 
977
        c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
542
978
        src+=2;
543
 
        if (st)
544
 
            c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
545
 
        if (st && c->status[1].idelta & 0x08000)
546
 
            c->status[1].idelta |= 0xFFFF0000;
547
 
        if (st)
 
979
        if (st){
 
980
            c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
548
981
            src+=2;
 
982
        }
549
983
        c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
550
984
        c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
551
985
        c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
552
986
        c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
553
 
        
 
987
 
554
988
        c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
555
989
        src+=2;
556
990
        if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
571
1005
        }
572
1006
        break;
573
1007
    case CODEC_ID_ADPCM_IMA_DK4:
574
 
        if (buf_size > BLKSIZE) {
575
 
            if (avctx->block_align != 0)
576
 
                buf_size = avctx->block_align;
577
 
            else
578
 
                buf_size = BLKSIZE;
579
 
        }
580
 
        c->status[0].predictor = (src[0] | (src[1] << 8));
 
1008
        if (avctx->block_align != 0 && buf_size > avctx->block_align)
 
1009
            buf_size = avctx->block_align;
 
1010
 
 
1011
        c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
581
1012
        c->status[0].step_index = src[2];
582
1013
        src += 4;
583
 
        if(c->status[0].predictor & 0x8000)
584
 
            c->status[0].predictor -= 0x10000;
585
1014
        *samples++ = c->status[0].predictor;
586
1015
        if (st) {
587
 
            c->status[1].predictor = (src[0] | (src[1] << 8));
 
1016
            c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
588
1017
            c->status[1].step_index = src[2];
589
1018
            src += 4;
590
 
            if(c->status[1].predictor & 0x8000)
591
 
                c->status[1].predictor -= 0x10000;
592
1019
            *samples++ = c->status[1].predictor;
593
1020
        }
594
1021
        while (src < buf + buf_size) {
595
1022
 
596
1023
            /* take care of the top nibble (always left or mono channel) */
597
 
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
598
 
                (src[0] >> 4) & 0x0F);
 
1024
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
 
1025
                (src[0] >> 4) & 0x0F, 3);
599
1026
 
600
1027
            /* take care of the bottom nibble, which is right sample for
601
1028
             * stereo, or another mono sample */
602
1029
            if (st)
603
 
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], 
604
 
                    src[0] & 0x0F);
 
1030
                *samples++ = adpcm_ima_expand_nibble(&c->status[1],
 
1031
                    src[0] & 0x0F, 3);
605
1032
            else
606
 
                *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
607
 
                    src[0] & 0x0F);
 
1033
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
 
1034
                    src[0] & 0x0F, 3);
608
1035
 
609
1036
            src++;
610
1037
        }
611
1038
        break;
612
1039
    case CODEC_ID_ADPCM_IMA_DK3:
613
 
        if (buf_size > BLKSIZE) {
614
 
            if (avctx->block_align != 0)
615
 
                buf_size = avctx->block_align;
616
 
            else
617
 
                buf_size = BLKSIZE;
618
 
        }
619
 
        c->status[0].predictor = (src[10] | (src[11] << 8));
620
 
        c->status[1].predictor = (src[12] | (src[13] << 8));
 
1040
        if (avctx->block_align != 0 && buf_size > avctx->block_align)
 
1041
            buf_size = avctx->block_align;
 
1042
 
 
1043
        if(buf_size + 16 > (samples_end - samples)*3/8)
 
1044
            return -1;
 
1045
 
 
1046
        c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
 
1047
        c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
621
1048
        c->status[0].step_index = src[14];
622
1049
        c->status[1].step_index = src[15];
623
1050
        /* sign extend the predictors */
624
 
        if(c->status[0].predictor & 0x8000)
625
 
            c->status[0].predictor -= 0x10000;
626
 
        if(c->status[1].predictor & 0x8000)
627
 
            c->status[1].predictor -= 0x10000;
628
1051
        src += 16;
629
1052
        diff_channel = c->status[1].predictor;
630
1053
 
637
1060
 
638
1061
            /* process the first predictor of the sum channel */
639
1062
            DK3_GET_NEXT_NIBBLE();
640
 
            adpcm_ima_expand_nibble(&c->status[0], nibble);
 
1063
            adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
641
1064
 
642
1065
            /* process the diff channel predictor */
643
1066
            DK3_GET_NEXT_NIBBLE();
644
 
            adpcm_ima_expand_nibble(&c->status[1], nibble);
 
1067
            adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
645
1068
 
646
1069
            /* process the first pair of stereo PCM samples */
647
1070
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
650
1073
 
651
1074
            /* process the second predictor of the sum channel */
652
1075
            DK3_GET_NEXT_NIBBLE();
653
 
            adpcm_ima_expand_nibble(&c->status[0], nibble);
 
1076
            adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
654
1077
 
655
1078
            /* process the second pair of stereo PCM samples */
656
1079
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
658
1081
            *samples++ = c->status[0].predictor - c->status[1].predictor;
659
1082
        }
660
1083
        break;
 
1084
    case CODEC_ID_ADPCM_IMA_WS:
 
1085
        /* no per-block initialization; just start decoding the data */
 
1086
        while (src < buf + buf_size) {
 
1087
 
 
1088
            if (st) {
 
1089
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
 
1090
                    (src[0] >> 4) & 0x0F, 3);
 
1091
                *samples++ = adpcm_ima_expand_nibble(&c->status[1],
 
1092
                    src[0] & 0x0F, 3);
 
1093
            } else {
 
1094
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
 
1095
                    (src[0] >> 4) & 0x0F, 3);
 
1096
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
 
1097
                    src[0] & 0x0F, 3);
 
1098
            }
 
1099
 
 
1100
            src++;
 
1101
        }
 
1102
        break;
 
1103
    case CODEC_ID_ADPCM_XA:
 
1104
        c->status[0].sample1 = c->status[0].sample2 =
 
1105
        c->status[1].sample1 = c->status[1].sample2 = 0;
 
1106
        while (buf_size >= 128) {
 
1107
            xa_decode(samples, src, &c->status[0], &c->status[1],
 
1108
                avctx->channels);
 
1109
            src += 128;
 
1110
            samples += 28 * 8;
 
1111
            buf_size -= 128;
 
1112
        }
 
1113
        break;
 
1114
    case CODEC_ID_ADPCM_EA:
 
1115
        samples_in_chunk = AV_RL32(src);
 
1116
        if (samples_in_chunk >= ((buf_size - 12) * 2)) {
 
1117
            src += buf_size;
 
1118
            break;
 
1119
        }
 
1120
        src += 4;
 
1121
        current_left_sample = (int16_t)AV_RL16(src);
 
1122
        src += 2;
 
1123
        previous_left_sample = (int16_t)AV_RL16(src);
 
1124
        src += 2;
 
1125
        current_right_sample = (int16_t)AV_RL16(src);
 
1126
        src += 2;
 
1127
        previous_right_sample = (int16_t)AV_RL16(src);
 
1128
        src += 2;
 
1129
 
 
1130
        for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
 
1131
            coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
 
1132
            coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
 
1133
            coeff1r = ea_adpcm_table[*src & 0x0F];
 
1134
            coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
 
1135
            src++;
 
1136
 
 
1137
            shift_left = ((*src >> 4) & 0x0F) + 8;
 
1138
            shift_right = (*src & 0x0F) + 8;
 
1139
            src++;
 
1140
 
 
1141
            for (count2 = 0; count2 < 28; count2++) {
 
1142
                next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
 
1143
                next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
 
1144
                src++;
 
1145
 
 
1146
                next_left_sample = (next_left_sample +
 
1147
                    (current_left_sample * coeff1l) +
 
1148
                    (previous_left_sample * coeff2l) + 0x80) >> 8;
 
1149
                next_right_sample = (next_right_sample +
 
1150
                    (current_right_sample * coeff1r) +
 
1151
                    (previous_right_sample * coeff2r) + 0x80) >> 8;
 
1152
                CLAMP_TO_SHORT(next_left_sample);
 
1153
                CLAMP_TO_SHORT(next_right_sample);
 
1154
 
 
1155
                previous_left_sample = current_left_sample;
 
1156
                current_left_sample = next_left_sample;
 
1157
                previous_right_sample = current_right_sample;
 
1158
                current_right_sample = next_right_sample;
 
1159
                *samples++ = (unsigned short)current_left_sample;
 
1160
                *samples++ = (unsigned short)current_right_sample;
 
1161
            }
 
1162
        }
 
1163
        break;
 
1164
    case CODEC_ID_ADPCM_IMA_SMJPEG:
 
1165
        c->status[0].predictor = *src;
 
1166
        src += 2;
 
1167
        c->status[0].step_index = *src++;
 
1168
        src++;  /* skip another byte before getting to the meat */
 
1169
        while (src < buf + buf_size) {
 
1170
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
 
1171
                *src & 0x0F, 3);
 
1172
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
 
1173
                (*src >> 4) & 0x0F, 3);
 
1174
            src++;
 
1175
        }
 
1176
        break;
 
1177
    case CODEC_ID_ADPCM_CT:
 
1178
        while (src < buf + buf_size) {
 
1179
            if (st) {
 
1180
                *samples++ = adpcm_ct_expand_nibble(&c->status[0],
 
1181
                    (src[0] >> 4) & 0x0F);
 
1182
                *samples++ = adpcm_ct_expand_nibble(&c->status[1],
 
1183
                    src[0] & 0x0F);
 
1184
            } else {
 
1185
                *samples++ = adpcm_ct_expand_nibble(&c->status[0],
 
1186
                    (src[0] >> 4) & 0x0F);
 
1187
                *samples++ = adpcm_ct_expand_nibble(&c->status[0],
 
1188
                    src[0] & 0x0F);
 
1189
            }
 
1190
            src++;
 
1191
        }
 
1192
        break;
 
1193
    case CODEC_ID_ADPCM_SBPRO_4:
 
1194
    case CODEC_ID_ADPCM_SBPRO_3:
 
1195
    case CODEC_ID_ADPCM_SBPRO_2:
 
1196
        if (!c->status[0].step_index) {
 
1197
            /* the first byte is a raw sample */
 
1198
            *samples++ = 128 * (*src++ - 0x80);
 
1199
            if (st)
 
1200
              *samples++ = 128 * (*src++ - 0x80);
 
1201
            c->status[0].step_index = 1;
 
1202
        }
 
1203
        if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
 
1204
            while (src < buf + buf_size) {
 
1205
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
 
1206
                    (src[0] >> 4) & 0x0F, 4, 0);
 
1207
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
 
1208
                    src[0] & 0x0F, 4, 0);
 
1209
                src++;
 
1210
            }
 
1211
        } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
 
1212
            while (src < buf + buf_size && samples + 2 < samples_end) {
 
1213
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
 
1214
                    (src[0] >> 5) & 0x07, 3, 0);
 
1215
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
 
1216
                    (src[0] >> 2) & 0x07, 3, 0);
 
1217
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
 
1218
                    src[0] & 0x03, 2, 0);
 
1219
                src++;
 
1220
            }
 
1221
        } else {
 
1222
            while (src < buf + buf_size && samples + 3 < samples_end) {
 
1223
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
 
1224
                    (src[0] >> 6) & 0x03, 2, 2);
 
1225
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
 
1226
                    (src[0] >> 4) & 0x03, 2, 2);
 
1227
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
 
1228
                    (src[0] >> 2) & 0x03, 2, 2);
 
1229
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
 
1230
                    src[0] & 0x03, 2, 2);
 
1231
                src++;
 
1232
            }
 
1233
        }
 
1234
        break;
 
1235
    case CODEC_ID_ADPCM_SWF:
 
1236
    {
 
1237
        GetBitContext gb;
 
1238
        const int *table;
 
1239
        int k0, signmask, nb_bits;
 
1240
        int size = buf_size*8;
 
1241
 
 
1242
        init_get_bits(&gb, buf, size);
 
1243
 
 
1244
        //read bits & inital values
 
1245
        nb_bits = get_bits(&gb, 2)+2;
 
1246
        //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
 
1247
        table = swf_index_tables[nb_bits-2];
 
1248
        k0 = 1 << (nb_bits-2);
 
1249
        signmask = 1 << (nb_bits-1);
 
1250
 
 
1251
        for (i = 0; i < avctx->channels; i++) {
 
1252
            *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
 
1253
            c->status[i].step_index = get_bits(&gb, 6);
 
1254
        }
 
1255
 
 
1256
        while (get_bits_count(&gb) < size)
 
1257
        {
 
1258
            int i;
 
1259
 
 
1260
            for (i = 0; i < avctx->channels; i++) {
 
1261
                // similar to IMA adpcm
 
1262
                int delta = get_bits(&gb, nb_bits);
 
1263
                int step = step_table[c->status[i].step_index];
 
1264
                long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
 
1265
                int k = k0;
 
1266
 
 
1267
                do {
 
1268
                    if (delta & k)
 
1269
                        vpdiff += step;
 
1270
                    step >>= 1;
 
1271
                    k >>= 1;
 
1272
                } while(k);
 
1273
                vpdiff += step;
 
1274
 
 
1275
                if (delta & signmask)
 
1276
                    c->status[i].predictor -= vpdiff;
 
1277
                else
 
1278
                    c->status[i].predictor += vpdiff;
 
1279
 
 
1280
                c->status[i].step_index += table[delta & (~signmask)];
 
1281
 
 
1282
                c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
 
1283
                c->status[i].predictor = av_clip(c->status[i].predictor, -32768, 32767);
 
1284
 
 
1285
                *samples++ = c->status[i].predictor;
 
1286
                if (samples >= samples_end) {
 
1287
                    av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
 
1288
                    return -1;
 
1289
                }
 
1290
            }
 
1291
        }
 
1292
        src += buf_size;
 
1293
        break;
 
1294
    }
 
1295
    case CODEC_ID_ADPCM_YAMAHA:
 
1296
        while (src < buf + buf_size) {
 
1297
            if (st) {
 
1298
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
 
1299
                        src[0] & 0x0F);
 
1300
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
 
1301
                        (src[0] >> 4) & 0x0F);
 
1302
            } else {
 
1303
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
 
1304
                        src[0] & 0x0F);
 
1305
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
 
1306
                        (src[0] >> 4) & 0x0F);
 
1307
            }
 
1308
            src++;
 
1309
        }
 
1310
        break;
661
1311
    default:
662
 
        *data_size = 0;
663
1312
        return -1;
664
1313
    }
665
1314
    *data_size = (uint8_t *)samples - (uint8_t *)data;
666
1315
    return src - buf;
667
1316
}
668
1317
 
669
 
#define ADPCM_CODEC(id, name)                   \
 
1318
 
 
1319
 
 
1320
#ifdef CONFIG_ENCODERS
 
1321
#define ADPCM_ENCODER(id,name)                  \
670
1322
AVCodec name ## _encoder = {                    \
671
1323
    #name,                                      \
672
1324
    CODEC_TYPE_AUDIO,                           \
676
1328
    adpcm_encode_frame,                         \
677
1329
    adpcm_encode_close,                         \
678
1330
    NULL,                                       \
679
 
};                                              \
 
1331
};
 
1332
#else
 
1333
#define ADPCM_ENCODER(id,name)
 
1334
#endif
 
1335
 
 
1336
#ifdef CONFIG_DECODERS
 
1337
#define ADPCM_DECODER(id,name)                  \
680
1338
AVCodec name ## _decoder = {                    \
681
1339
    #name,                                      \
682
1340
    CODEC_TYPE_AUDIO,                           \
687
1345
    NULL,                                       \
688
1346
    adpcm_decode_frame,                         \
689
1347
};
 
1348
#else
 
1349
#define ADPCM_DECODER(id,name)
 
1350
#endif
 
1351
 
 
1352
#define ADPCM_CODEC(id, name)                   \
 
1353
ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
690
1354
 
691
1355
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
692
1356
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
693
1357
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
694
1358
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
 
1359
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
 
1360
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
695
1361
ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
696
1362
ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
 
1363
ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
 
1364
ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
 
1365
ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
 
1366
ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
 
1367
ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
 
1368
ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
 
1369
ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
 
1370
ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
697
1371
 
698
1372
#undef ADPCM_CODEC