Statistics
| Branch: | Revision:

ffmpeg / libavcodec / adpcm.c @ 764ef400

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

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

    
139
    return 0;
140
}
141

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

    
146
    return 0;
147
}
148

    
149

    
150
static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
151
{
152
    int step_index;
153
    unsigned char nibble;
154
    
155
    int sign = 0; /* sign bit of the nibble (MSB) */
156
    int delta, predicted_delta;
157

    
158
    delta = sample - c->prev_sample;
159

    
160
    if (delta < 0) {
161
        sign = 1;
162
        delta = -delta;
163
    }
164

    
165
    step_index = c->step_index;
166

    
167
    /* nibble = 4 * delta / step_table[step_index]; */
168
    nibble = (delta << 2) / step_table[step_index];
169

    
170
    if (nibble > 7)
171
        nibble = 7;
172

    
173
    step_index += index_table[nibble];
174
    if (step_index < 0)
175
        step_index = 0;
176
    if (step_index > 88)
177
        step_index = 88;
178

    
179
    /* what the decoder will find */
180
    predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
181

    
182
    if (sign)
183
        c->prev_sample -= predicted_delta;
184
    else
185
        c->prev_sample += predicted_delta;
186

    
187
    CLAMP_TO_SHORT(c->prev_sample);
188

    
189

    
190
    nibble += sign << 3; /* sign * 8 */   
191

    
192
    /* save back */
193
    c->step_index = step_index;
194

    
195
    return nibble;
196
}
197

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

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

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

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

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

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

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

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

    
298
    sign = nibble & 8;
299
    delta = nibble & 7;
300
    /* perform direct multiplication instead of series of jumps proposed by
301
     * the reference ADPCM implementation since modern CPUs can do the mults
302
     * quickly enough */
303
    diff = ((2 * delta + 1) * step) >> 3;
304
    predictor = c->predictor;
305
    if (sign) predictor -= diff;
306
    else predictor += diff;
307

    
308
    CLAMP_TO_SHORT(predictor);
309
    c->predictor = predictor;
310
    c->step_index = step_index;
311

    
312
    return (short)predictor;
313
}
314

    
315
static inline short adpcm_4xa_expand_nibble(ADPCMChannelStatus *c, char nibble)
316
{
317
    int step_index;
318
    int predictor;
319
    int sign, delta, diff, step;
320

    
321
    step = step_table[c->step_index];
322
    step_index = c->step_index + index_table[(unsigned)nibble];
323
    if (step_index < 0) step_index = 0;
324
    else if (step_index > 88) step_index = 88;
325

    
326
    sign = nibble & 8;
327
    delta = nibble & 7;
328
    
329
    diff = (delta*step + (step>>1))>>3; // difference to code above
330
    
331
    predictor = c->predictor;
332
    if (sign) predictor -= diff;
333
    else predictor += diff;
334

    
335
    CLAMP_TO_SHORT(predictor);
336
    c->predictor = predictor;
337
    c->step_index = step_index;
338

    
339
    return (short)predictor;
340
}
341

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

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

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

    
355
    return (short)predictor;
356
}
357

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

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

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

    
391
    samples = data;
392
    src = buf;
393

    
394
    st = avctx->channels == 2;
395

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

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

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

    
412
        CLAMP_TO_SHORT(cs->predictor);
413

    
414
        cs->step_index = (*src++) & 0x7F;
415

    
416
        if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
417
        if (cs->step_index > 88) cs->step_index = 88;
418

    
419
        cs->step = step_table[cs->step_index];
420

    
421
        if (st && channel)
422
            samples++;
423

    
424
        *samples++ = cs->predictor;
425
        samples += st;
426

    
427
        for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
428
            *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
429
            samples += avctx->channels;
430
            *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
431
            samples += avctx->channels;
432
            src ++;
433
        }
434

    
435
        if(st) { /* handle stereo interlacing */
436
            c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
437
            if(channel == 0) { /* wait for the other packet before outputing anything */
438
                *data_size = 0;
439
                return src - buf;
440
            }
441
        }
442
        break;
443
    case CODEC_ID_ADPCM_IMA_WAV:
444
        if (buf_size > BLKSIZE) {
445
            if (avctx->block_align != 0)
446
                buf_size = avctx->block_align;
447
            else
448
                buf_size = BLKSIZE;
449
        }
450
        // XXX: do as per-channel loop
451
        cs = &(c->status[0]);
452
        cs->predictor = (*src++) & 0x0FF;
453
        cs->predictor |= ((*src++) << 8) & 0x0FF00;
454
        if(cs->predictor & 0x8000)
455
            cs->predictor -= 0x10000;
456
        CLAMP_TO_SHORT(cs->predictor);
457

    
458
        // XXX: is this correct ??: *samples++ = cs->predictor;
459

    
460
        cs->step_index = *src++;
461
        if (cs->step_index < 0) cs->step_index = 0;
462
        if (cs->step_index > 88) cs->step_index = 88;
463
        if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
464

    
465
        if (st) {
466
            cs = &(c->status[1]);
467
            cs->predictor = (*src++) & 0x0FF;
468
            cs->predictor |= ((*src++) << 8) & 0x0FF00;
469
            if(cs->predictor & 0x8000)
470
                cs->predictor -= 0x10000;
471
            CLAMP_TO_SHORT(cs->predictor);
472

    
473
            // XXX: is this correct ??: *samples++ = cs->predictor;
474

    
475
            cs->step_index = *src++;
476
            if (cs->step_index < 0) cs->step_index = 0;
477
            if (cs->step_index > 88) cs->step_index = 88;
478
            src++; /* if != 0  -> out-of-sync */
479
        }
480

    
481
        for(m=4; src < (buf + buf_size);) {
482
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
483
            if (st)
484
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
485
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
486
            if (st) {
487
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
488
                if (!--m) {
489
                    m=4;
490
                    src+=4;
491
                }
492
            }
493
            src++;
494
        }
495
        break;
496
    case CODEC_ID_ADPCM_4XM:
497
        cs = &(c->status[0]);
498
        c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
499
        if(st){
500
            c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
501
        }
502
        c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
503
        if(st){
504
            c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
505
        }
506
//            if (cs->step_index < 0) cs->step_index = 0;
507
//            if (cs->step_index > 88) cs->step_index = 88;
508

    
509
        m= (buf_size - (src - buf))>>st;
510
//printf("%d %d %d %d\n", st, m, c->status[0].predictor, c->status[0].step_index);
511
        //FIXME / XXX decode chanels individual & interleave samples
512
        for(i=0; i<m; i++) {
513
            *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] & 0x0F);
514
            if (st)
515
                *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F);
516
            *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4);
517
            if (st)
518
                *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4);
519
        }
520

    
521
        src += m<<st;
522

    
523
        break;
524
    case CODEC_ID_ADPCM_MS:
525

    
526
        if (buf_size > BLKSIZE) {
527
            if (avctx->block_align != 0)
528
                buf_size = avctx->block_align;
529
            else
530
                buf_size = BLKSIZE;
531
        }
532
        n = buf_size - 7 * avctx->channels;
533
        if (n < 0)
534
            return -1;
535
        block_predictor[0] = (*src++); /* should be bound */
536
        block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
537
        block_predictor[1] = 0;
538
        if (st)
539
            block_predictor[1] = (*src++);
540
        block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
541
        c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
542
        if (c->status[0].idelta & 0x08000)
543
            c->status[0].idelta -= 0x10000;
544
        src+=2;
545
        if (st)
546
            c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
547
        if (st && c->status[1].idelta & 0x08000)
548
            c->status[1].idelta |= 0xFFFF0000;
549
        if (st)
550
            src+=2;
551
        c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
552
        c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
553
        c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
554
        c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
555
        
556
        c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
557
        src+=2;
558
        if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
559
        if (st) src+=2;
560
        c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
561
        src+=2;
562
        if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
563
        if (st) src+=2;
564

    
565
        *samples++ = c->status[0].sample1;
566
        if (st) *samples++ = c->status[1].sample1;
567
        *samples++ = c->status[0].sample2;
568
        if (st) *samples++ = c->status[1].sample2;
569
        for(;n>0;n--) {
570
            *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
571
            *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
572
            src ++;
573
        }
574
        break;
575
    case CODEC_ID_ADPCM_IMA_DK4:
576
        if (buf_size > BLKSIZE) {
577
            if (avctx->block_align != 0)
578
                buf_size = avctx->block_align;
579
            else
580
                buf_size = BLKSIZE;
581
        }
582
        c->status[0].predictor = (src[0] | (src[1] << 8));
583
        c->status[0].step_index = src[2];
584
        src += 4;
585
        if(c->status[0].predictor & 0x8000)
586
            c->status[0].predictor -= 0x10000;
587
        *samples++ = c->status[0].predictor;
588
        if (st) {
589
            c->status[1].predictor = (src[0] | (src[1] << 8));
590
            c->status[1].step_index = src[2];
591
            src += 4;
592
            if(c->status[1].predictor & 0x8000)
593
                c->status[1].predictor -= 0x10000;
594
            *samples++ = c->status[1].predictor;
595
        }
596
        while (src < buf + buf_size) {
597

    
598
            /* take care of the top nibble (always left or mono channel) */
599
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
600
                (src[0] >> 4) & 0x0F);
601

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

    
611
            src++;
612
        }
613
        break;
614
    case CODEC_ID_ADPCM_IMA_DK3:
615
        if (buf_size > BLKSIZE) {
616
            if (avctx->block_align != 0)
617
                buf_size = avctx->block_align;
618
            else
619
                buf_size = BLKSIZE;
620
        }
621
        c->status[0].predictor = (src[10] | (src[11] << 8));
622
        c->status[1].predictor = (src[12] | (src[13] << 8));
623
        c->status[0].step_index = src[14];
624
        c->status[1].step_index = src[15];
625
        /* sign extend the predictors */
626
        if(c->status[0].predictor & 0x8000)
627
            c->status[0].predictor -= 0x10000;
628
        if(c->status[1].predictor & 0x8000)
629
            c->status[1].predictor -= 0x10000;
630
        src += 16;
631
        diff_channel = c->status[1].predictor;
632

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

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

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

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

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

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

    
657
            /* process the second pair of stereo PCM samples */
658
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
659
            *samples++ = c->status[0].predictor + c->status[1].predictor;
660
            *samples++ = c->status[0].predictor - c->status[1].predictor;
661
        }
662
        break;
663
    case CODEC_ID_ADPCM_IMA_WS:
664
        /* no per-block initialization; just start decoding the data */
665
        while (src < buf + buf_size) {
666

    
667
            if (st) {
668
                *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
669
                    (src[0] >> 4) & 0x0F);
670
                *samples++ = adpcm_ima_expand_nibble(&c->status[1], 
671
                    src[0] & 0x0F);
672
            } else {
673
                *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
674
                    (src[0] >> 4) & 0x0F);
675
                *samples++ = adpcm_ima_expand_nibble(&c->status[0], 
676
                    src[0] & 0x0F);
677
            }
678

    
679
            src++;
680
        }
681
        break;
682
    default:
683
        *data_size = 0;
684
        return -1;
685
    }
686
    *data_size = (uint8_t *)samples - (uint8_t *)data;
687
    return src - buf;
688
}
689

    
690

    
691

    
692
#ifdef CONFIG_ENCODERS
693
#define ADPCM_ENCODER(id,name)                  \
694
AVCodec name ## _encoder = {                    \
695
    #name,                                      \
696
    CODEC_TYPE_AUDIO,                           \
697
    id,                                         \
698
    sizeof(ADPCMContext),                       \
699
    adpcm_encode_init,                          \
700
    adpcm_encode_frame,                         \
701
    adpcm_encode_close,                         \
702
    NULL,                                       \
703
};
704
#else
705
#define ADPCM_ENCODER(id,name)
706
#endif
707

    
708
#ifdef CONFIG_DECODERS
709
#define ADPCM_DECODER(id,name)                  \
710
AVCodec name ## _decoder = {                    \
711
    #name,                                      \
712
    CODEC_TYPE_AUDIO,                           \
713
    id,                                         \
714
    sizeof(ADPCMContext),                       \
715
    adpcm_decode_init,                          \
716
    NULL,                                       \
717
    NULL,                                       \
718
    adpcm_decode_frame,                         \
719
};
720
#else
721
#define ADPCM_DECODER(id,name)
722
#endif
723

    
724
#define ADPCM_CODEC(id, name)                   \
725
ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
726

    
727
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
728
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
729
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
730
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
731
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
732
ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
733
ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
734

    
735
#undef ADPCM_CODEC