Statistics
| Branch: | Revision:

ffmpeg / libavcodec / adpcm.c @ 186447f8

History | View | Annotate | Download (23.6 KB)

1
/*
2
 * ADPCM codecs
3
 * Copyright (c) 2001-2003 The ffmpeg Project
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * 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
 */
19
#include "avcodec.h"
20

    
21
/**
22
 * @file adpcm.c
23
 * ADPCM codecs.
24
 * First version by Francois Revol revol@free.fr
25
 * Fringe ADPCM codecs (e.g., DK3 and DK4) 
26
 *   by Mike Melanson (melanson@pcisys.net)
27
 *
28
 * Features and limitations:
29
 *
30
 * Reference documents:
31
 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
32
 * http://www.geocities.com/SiliconValley/8682/aud3.txt
33
 * http://openquicktime.sourceforge.net/plugins.htm
34
 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
35
 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
36
 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
37
 */
38

    
39
#define BLKSIZE 1024
40

    
41
#define CLAMP_TO_SHORT(value) \
42
if (value > 32767) \
43
    value = 32767; \
44
else if (value < -32768) \
45
    value = -32768; \
46

    
47
/* step_table[] and index_table[] are from the ADPCM reference source */
48
/* This is the index table: */
49
static const int index_table[16] = {
50
    -1, -1, -1, -1, 2, 4, 6, 8,
51
    -1, -1, -1, -1, 2, 4, 6, 8,
52
};
53

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

    
70
/* Those are for MS-ADPCM */
71
/* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
72
static const int AdaptationTable[] = {
73
        230, 230, 230, 230, 307, 409, 512, 614,
74
        768, 614, 512, 409, 307, 230, 230, 230
75
};
76

    
77
static const int AdaptCoeff1[] = {
78
        256, 512, 0, 192, 240, 460, 392
79
};
80

    
81
static const int AdaptCoeff2[] = {
82
        0, -256, 0, 64, 0, -208, -232
83
};
84

    
85
/* end of tables */
86

    
87
typedef struct ADPCMChannelStatus {
88
    int predictor;
89
    short int step_index;
90
    int step;
91
    /* for encoding */
92
    int prev_sample;
93

    
94
    /* MS version */
95
    short sample1;
96
    short sample2;
97
    int coeff1;
98
    int coeff2;
99
    int idelta;
100
} ADPCMChannelStatus;
101

    
102
typedef struct ADPCMContext {
103
    int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
104
    ADPCMChannelStatus status[2];
105
    short sample_buffer[32]; /* hold left samples while waiting for right samples */
106
} ADPCMContext;
107

    
108
/* XXX: implement encoding */
109

    
110
static int adpcm_encode_init(AVCodecContext *avctx)
111
{
112
    if (avctx->channels > 2)
113
        return -1; /* only stereo or mono =) */
114
    switch(avctx->codec->id) {
115
    case CODEC_ID_ADPCM_IMA_QT:
116
        fprintf(stderr, "ADPCM: codec admcp_ima_qt unsupported for encoding !\n");
117
        avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
118
        return -1;
119
        break;
120
    case CODEC_ID_ADPCM_IMA_WAV:
121
        avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
122
                                                             /* and we have 4 bytes per channel overhead */
123
        avctx->block_align = BLKSIZE;
124
        /* seems frame_size isn't taken into account... have to buffer the samples :-( */
125
        break;
126
    case CODEC_ID_ADPCM_MS:
127
        fprintf(stderr, "ADPCM: codec admcp_ms unsupported for encoding !\n");
128
        return -1;
129
        break;
130
    default:
131
        return -1;
132
        break;
133
    }
134

    
135
    avctx->coded_frame= avcodec_alloc_frame();
136
    avctx->coded_frame->key_frame= 1;
137

    
138
    return 0;
139
}
140

    
141
static int adpcm_encode_close(AVCodecContext *avctx)
142
{
143
    av_freep(&avctx->coded_frame);
144

    
145
    return 0;
146
}
147

    
148

    
149
static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
150
{
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

    
186
    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;
195
}
196

    
197
static int adpcm_encode_frame(AVCodecContext *avctx,
198
                            unsigned char *frame, int buf_size, void *data)
199
{
200
    int n;
201
    short *samples;
202
    unsigned char *dst;
203
    ADPCMContext *c = avctx->priv_data;
204

    
205
    dst = frame;
206
    samples = (short *)data;
207
/*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
208

    
209
    switch(avctx->codec->id) {
210
    case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
211
        break;
212
    case CODEC_ID_ADPCM_IMA_WAV:
213
        n = avctx->frame_size / 8;
214
            c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
215
/*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
216
            *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
217
            *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
218
            *dst++ = (unsigned char)c->status[0].step_index;
219
            *dst++ = 0; /* unknown */
220
            samples++;
221
            if (avctx->channels == 2) {
222
                c->status[1].prev_sample = (signed short)samples[0];
223
/*                c->status[1].step_index = 0; */
224
                *dst++ = (c->status[1].prev_sample) & 0xFF;
225
                *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
226
                *dst++ = (unsigned char)c->status[1].step_index;
227
                *dst++ = 0;
228
                samples++;
229
            }
230
        
231
            /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
232
            for (; n>0; n--) {
233
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
234
                *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
235
                dst++;
236
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
237
                *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
238
                dst++;
239
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
240
                *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
241
                dst++;
242
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
243
                *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
244
                dst++;
245
                /* right channel */
246
                if (avctx->channels == 2) {
247
                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
248
                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
249
                    dst++;
250
                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
251
                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
252
                    dst++;
253
                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
254
                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
255
                    dst++;
256
                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
257
                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
258
                    dst++;
259
                }
260
                samples += 8 * avctx->channels;
261
            }
262
        break;
263
    default:
264
        return -1;
265
    }
266
    return dst - frame;
267
}
268

    
269
static int adpcm_decode_init(AVCodecContext * avctx)
270
{
271
    ADPCMContext *c = avctx->priv_data;
272

    
273
    c->channel = 0;
274
    c->status[0].predictor = c->status[1].predictor = 0;
275
    c->status[0].step_index = c->status[1].step_index = 0;
276
    c->status[0].step = c->status[1].step = 0;
277

    
278
    switch(avctx->codec->id) {
279
    default:
280
        break;
281
    }
282
    return 0;
283
}
284

    
285
static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
286
{
287
    int step_index;
288
    int predictor;
289
    int sign, delta, diff, step;
290

    
291
    step = step_table[c->step_index];
292
    step_index = c->step_index + index_table[(unsigned)nibble];
293
    if (step_index < 0) step_index = 0;
294
    else if (step_index > 88) step_index = 88;
295

    
296
    sign = nibble & 8;
297
    delta = nibble & 7;
298
    /* perform direct multiplication instead of series of jumps proposed by
299
     * the reference ADPCM implementation since modern CPUs can do the mults
300
     * 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
    
329
    predictor = c->predictor;
330
    if (sign) predictor -= diff;
331
    else predictor += diff;
332

    
333
    CLAMP_TO_SHORT(predictor);
334
    c->predictor = predictor;
335
    c->step_index = step_index;
336

    
337
    return (short)predictor;
338
}
339

    
340
static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
341
{
342
    int predictor;
343

    
344
    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
345
    predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
346
    CLAMP_TO_SHORT(predictor);
347

    
348
    c->sample2 = c->sample1;
349
    c->sample1 = predictor;
350
    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
351
    if (c->idelta < 16) c->idelta = 16;
352

    
353
    return (short)predictor;
354
}
355

    
356
/* DK3 ADPCM support macro */
357
#define DK3_GET_NEXT_NIBBLE() \
358
    if (decode_top_nibble_next) \
359
    { \
360
        nibble = (last_byte >> 4) & 0x0F; \
361
        decode_top_nibble_next = 0; \
362
    } \
363
    else \
364
    { \
365
        last_byte = *src++; \
366
        if (src >= buf + buf_size) break; \
367
        nibble = last_byte & 0x0F; \
368
        decode_top_nibble_next = 1; \
369
    }
370

    
371
static int adpcm_decode_frame(AVCodecContext *avctx,
372
                            void *data, int *data_size,
373
                            uint8_t *buf, int buf_size)
374
{
375
    ADPCMContext *c = avctx->priv_data;
376
    ADPCMChannelStatus *cs;
377
    int n, m, channel, i;
378
    int block_predictor[2];
379
    short *samples;
380
    uint8_t *src;
381
    int st; /* stereo */
382

    
383
    /* DK3 ADPCM accounting variables */
384
    unsigned char last_byte = 0;
385
    unsigned char nibble;
386
    int decode_top_nibble_next = 0;
387
    int diff_channel;
388

    
389
    samples = data;
390
    src = buf;
391

    
392
    st = avctx->channels == 2;
393

    
394
    switch(avctx->codec->id) {
395
    case CODEC_ID_ADPCM_IMA_QT:
396
        n = (buf_size - 2);/* >> 2*avctx->channels;*/
397
        channel = c->channel;
398
        cs = &(c->status[channel]);
399
        /* (pppppp) (piiiiiii) */
400

    
401
        /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
402
        cs->predictor = (*src++) << 8;
403
        cs->predictor |= (*src & 0x80);
404
        cs->predictor &= 0xFF80;
405

    
406
        /* sign extension */
407
        if(cs->predictor & 0x8000)
408
            cs->predictor -= 0x10000;
409

    
410
        CLAMP_TO_SHORT(cs->predictor);
411

    
412
        cs->step_index = (*src++) & 0x7F;
413

    
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;
416

    
417
        cs->step = step_table[cs->step_index];
418

    
419
        if (st && channel)
420
            samples++;
421

    
422
        *samples++ = cs->predictor;
423
        samples += st;
424

    
425
        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);
427
            samples += avctx->channels;
428
            *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
429
            samples += avctx->channels;
430
            src ++;
431
        }
432

    
433
        if(st) { /* handle stereo interlacing */
434
            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;
437
                return src - buf;
438
            }
439
        }
440
        break;
441
    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
        }
493
        break;
494
    case CODEC_ID_ADPCM_4XM:
495
        cs = &(c->status[0]);
496
        c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
497
        if(st){
498
            c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
499
        }
500
        c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
501
        if(st){
502
            c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
503
        }
504
//            if (cs->step_index < 0) cs->step_index = 0;
505
//            if (cs->step_index > 88) cs->step_index = 88;
506

    
507
        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
        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
        }
518

    
519
        src += m<<st;
520

    
521
        break;
522
    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
        }
530
        n = buf_size - 7 * avctx->channels;
531
        if (n < 0)
532
            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]));
535
        block_predictor[1] = 0;
536
        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;
542
        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)
548
            src+=2;
549
        c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
550
        c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
551
        c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
552
        c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
553
        
554
        c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
555
        src+=2;
556
        if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
557
        if (st) src+=2;
558
        c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
559
        src+=2;
560
        if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
561
        if (st) src+=2;
562

    
563
        *samples++ = c->status[0].sample1;
564
        if (st) *samples++ = c->status[1].sample1;
565
        *samples++ = c->status[0].sample2;
566
        if (st) *samples++ = c->status[1].sample2;
567
        for(;n>0;n--) {
568
            *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
569
            *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
570
            src ++;
571
        }
572
        break;
573
    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));
581
        c->status[0].step_index = src[2];
582
        src += 4;
583
        if(c->status[0].predictor & 0x8000)
584
            c->status[0].predictor -= 0x10000;
585
        *samples++ = c->status[0].predictor;
586
        if (st) {
587
            c->status[1].predictor = (src[0] | (src[1] << 8));
588
            c->status[1].step_index = src[2];
589
            src += 4;
590
            if(c->status[1].predictor & 0x8000)
591
                c->status[1].predictor -= 0x10000;
592
            *samples++ = c->status[1].predictor;
593
        }
594
        while (src < buf + buf_size) {
595

    
596
            /* 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);
599

    
600
            /* take care of the bottom nibble, which is right sample for
601
             * stereo, or another mono sample */
602
            if (st)
603
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], 
604
                    src[0] & 0x0F);
605
            else
606
                *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
607
                    src[0] & 0x0F);
608

    
609
            src++;
610
        }
611
        break;
612
    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));
621
        c->status[0].step_index = src[14];
622
        c->status[1].step_index = src[15];
623
        /* 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
        src += 16;
629
        diff_channel = c->status[1].predictor;
630

    
631
        /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
632
         * the buffer is consumed */
633
        while (1) {
634

    
635
            /* for this algorithm, c->status[0] is the sum channel and
636
             * c->status[1] is the diff channel */
637

    
638
            /* process the first predictor of the sum channel */
639
            DK3_GET_NEXT_NIBBLE();
640
            adpcm_ima_expand_nibble(&c->status[0], nibble);
641

    
642
            /* process the diff channel predictor */
643
            DK3_GET_NEXT_NIBBLE();
644
            adpcm_ima_expand_nibble(&c->status[1], nibble);
645

    
646
            /* process the first pair of stereo PCM samples */
647
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
648
            *samples++ = c->status[0].predictor + c->status[1].predictor;
649
            *samples++ = c->status[0].predictor - c->status[1].predictor;
650

    
651
            /* process the second predictor of the sum channel */
652
            DK3_GET_NEXT_NIBBLE();
653
            adpcm_ima_expand_nibble(&c->status[0], nibble);
654

    
655
            /* process the second pair of stereo PCM samples */
656
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
657
            *samples++ = c->status[0].predictor + c->status[1].predictor;
658
            *samples++ = c->status[0].predictor - c->status[1].predictor;
659
        }
660
        break;
661
    default:
662
        *data_size = 0;
663
        return -1;
664
    }
665
    *data_size = (uint8_t *)samples - (uint8_t *)data;
666
    return src - buf;
667
}
668

    
669
#define ADPCM_CODEC(id, name)                   \
670
AVCodec name ## _encoder = {                    \
671
    #name,                                      \
672
    CODEC_TYPE_AUDIO,                           \
673
    id,                                         \
674
    sizeof(ADPCMContext),                       \
675
    adpcm_encode_init,                          \
676
    adpcm_encode_frame,                         \
677
    adpcm_encode_close,                         \
678
    NULL,                                       \
679
};                                              \
680
AVCodec name ## _decoder = {                    \
681
    #name,                                      \
682
    CODEC_TYPE_AUDIO,                           \
683
    id,                                         \
684
    sizeof(ADPCMContext),                       \
685
    adpcm_decode_init,                          \
686
    NULL,                                       \
687
    NULL,                                       \
688
    adpcm_decode_frame,                         \
689
};
690

    
691
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
692
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
693
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
694
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
695
ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
696
ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
697

    
698
#undef ADPCM_CODEC