GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/adpcm.c Lines: 837 1138 73.6 %
Date: 2020-01-28 04:56:05 Branches: 396 579 68.4 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2001-2003 The FFmpeg project
3
 *
4
 * first version by Francois Revol (revol@free.fr)
5
 * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6
 *   by Mike Melanson (melanson@pcisys.net)
7
 * CD-ROM XA ADPCM codec by BERO
8
 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
9
 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
10
 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
11
 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
12
 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
13
 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
14
 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
15
 * Argonaut Games ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
16
 *
17
 * This file is part of FFmpeg.
18
 *
19
 * FFmpeg is free software; you can redistribute it and/or
20
 * modify it under the terms of the GNU Lesser General Public
21
 * License as published by the Free Software Foundation; either
22
 * version 2.1 of the License, or (at your option) any later version.
23
 *
24
 * FFmpeg is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
 * Lesser General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Lesser General Public
30
 * License along with FFmpeg; if not, write to the Free Software
31
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32
 */
33
#include "avcodec.h"
34
#include "get_bits.h"
35
#include "bytestream.h"
36
#include "adpcm.h"
37
#include "adpcm_data.h"
38
#include "internal.h"
39
40
/**
41
 * @file
42
 * ADPCM decoders
43
 * Features and limitations:
44
 *
45
 * Reference documents:
46
 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
47
 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
48
 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
49
 * http://openquicktime.sourceforge.net/
50
 * XAnim sources (xa_codec.c) http://xanim.polter.net/
51
 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
52
 * SoX source code http://sox.sourceforge.net/
53
 *
54
 * CD-ROM XA:
55
 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
56
 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
57
 * readstr http://www.geocities.co.jp/Playtown/2004/
58
 */
59
60
/* These are for CD-ROM XA ADPCM */
61
static const int8_t xa_adpcm_table[5][2] = {
62
    {   0,   0 },
63
    {  60,   0 },
64
    { 115, -52 },
65
    {  98, -55 },
66
    { 122, -60 }
67
};
68
69
static const int16_t ea_adpcm_table[] = {
70
    0,  240,  460,  392,
71
    0,    0, -208, -220,
72
    0,    1,    3,    4,
73
    7,    8,   10,   11,
74
    0,   -1,   -3,   -4
75
};
76
77
// padded to zero where table size is less then 16
78
static const int8_t swf_index_tables[4][16] = {
79
    /*2*/ { -1, 2 },
80
    /*3*/ { -1, -1, 2, 4 },
81
    /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
82
    /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
83
};
84
85
/* end of tables */
86
87
typedef struct ADPCMDecodeContext {
88
    ADPCMChannelStatus status[14];
89
    int vqa_version;                /**< VQA version. Used for ADPCM_IMA_WS */
90
    int has_status;
91
} ADPCMDecodeContext;
92
93
132
static av_cold int adpcm_decode_init(AVCodecContext * avctx)
94
{
95
132
    ADPCMDecodeContext *c = avctx->priv_data;
96
132
    unsigned int min_channels = 1;
97
132
    unsigned int max_channels = 2;
98
99

132
    switch(avctx->codec->id) {
100
8
    case AV_CODEC_ID_ADPCM_DTK:
101
    case AV_CODEC_ID_ADPCM_EA:
102
8
        min_channels = 2;
103
8
        break;
104
23
    case AV_CODEC_ID_ADPCM_AFC:
105
    case AV_CODEC_ID_ADPCM_EA_R1:
106
    case AV_CODEC_ID_ADPCM_EA_R2:
107
    case AV_CODEC_ID_ADPCM_EA_R3:
108
    case AV_CODEC_ID_ADPCM_EA_XAS:
109
    case AV_CODEC_ID_ADPCM_MS:
110
23
        max_channels = 6;
111
23
        break;
112
    case AV_CODEC_ID_ADPCM_MTAF:
113
        min_channels = 2;
114
        max_channels = 8;
115
        if (avctx->channels & 1) {
116
            avpriv_request_sample(avctx, "channel count %d\n", avctx->channels);
117
            return AVERROR_PATCHWELCOME;
118
        }
119
        break;
120
    case AV_CODEC_ID_ADPCM_PSX:
121
        max_channels = 8;
122
        break;
123
8
    case AV_CODEC_ID_ADPCM_IMA_DAT4:
124
    case AV_CODEC_ID_ADPCM_THP:
125
    case AV_CODEC_ID_ADPCM_THP_LE:
126
8
        max_channels = 14;
127
8
        break;
128
    }
129

132
    if (avctx->channels < min_channels || avctx->channels > max_channels) {
130
        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
131
        return AVERROR(EINVAL);
132
    }
133
134

132
    switch(avctx->codec->id) {
135
2
    case AV_CODEC_ID_ADPCM_CT:
136
2
        c->status[0].step = c->status[1].step = 511;
137
2
        break;
138
10
    case AV_CODEC_ID_ADPCM_IMA_WAV:
139

10
        if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
140
            return AVERROR_INVALIDDATA;
141
10
        break;
142
3
    case AV_CODEC_ID_ADPCM_IMA_APC:
143

3
        if (avctx->extradata && avctx->extradata_size >= 8) {
144
2
            c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata    ), 18);
145
2
            c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
146
        }
147
3
        break;
148
4
    case AV_CODEC_ID_ADPCM_IMA_WS:
149

4
        if (avctx->extradata && avctx->extradata_size >= 2)
150
3
            c->vqa_version = AV_RL16(avctx->extradata);
151
4
        break;
152
    case AV_CODEC_ID_ADPCM_ARGO:
153
        if (avctx->bits_per_coded_sample != 4)
154
            return AVERROR_INVALIDDATA;
155
        break;
156
113
    default:
157
113
        break;
158
    }
159
160

132
    switch(avctx->codec->id) {
161
56
        case AV_CODEC_ID_ADPCM_AICA:
162
        case AV_CODEC_ID_ADPCM_IMA_DAT4:
163
        case AV_CODEC_ID_ADPCM_IMA_QT:
164
        case AV_CODEC_ID_ADPCM_IMA_WAV:
165
        case AV_CODEC_ID_ADPCM_4XM:
166
        case AV_CODEC_ID_ADPCM_XA:
167
        case AV_CODEC_ID_ADPCM_EA_R1:
168
        case AV_CODEC_ID_ADPCM_EA_R2:
169
        case AV_CODEC_ID_ADPCM_EA_R3:
170
        case AV_CODEC_ID_ADPCM_EA_XAS:
171
        case AV_CODEC_ID_ADPCM_THP:
172
        case AV_CODEC_ID_ADPCM_THP_LE:
173
        case AV_CODEC_ID_ADPCM_AFC:
174
        case AV_CODEC_ID_ADPCM_DTK:
175
        case AV_CODEC_ID_ADPCM_PSX:
176
        case AV_CODEC_ID_ADPCM_MTAF:
177
        case AV_CODEC_ID_ADPCM_ARGO:
178
56
            avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
179
56
            break;
180
4
        case AV_CODEC_ID_ADPCM_IMA_WS:
181
4
            avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
182
                                                      AV_SAMPLE_FMT_S16;
183
4
            break;
184
11
        case AV_CODEC_ID_ADPCM_MS:
185
11
            avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
186
                                                      AV_SAMPLE_FMT_S16;
187
11
            break;
188
61
        default:
189
61
            avctx->sample_fmt = AV_SAMPLE_FMT_S16;
190
    }
191
192
132
    return 0;
193
}
194
195
static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
196
{
197
    int delta, pred, step, add;
198
199
    pred = c->predictor;
200
    delta = nibble & 7;
201
    step = c->step;
202
    add = (delta * 2 + 1) * step;
203
    if (add < 0)
204
        add = add + 7;
205
206
    if ((nibble & 8) == 0)
207
        pred = av_clip(pred + (add >> 3), -32767, 32767);
208
    else
209
        pred = av_clip(pred - (add >> 3), -32767, 32767);
210
211
    switch (delta) {
212
    case 7:
213
        step *= 0x99;
214
        break;
215
    case 6:
216
        c->step = av_clip(c->step * 2, 127, 24576);
217
        c->predictor = pred;
218
        return pred;
219
    case 5:
220
        step *= 0x66;
221
        break;
222
    case 4:
223
        step *= 0x4d;
224
        break;
225
    default:
226
        step *= 0x39;
227
        break;
228
    }
229
230
    if (step < 0)
231
        step += 0x3f;
232
233
    c->step = step >> 6;
234
    c->step = av_clip(c->step, 127, 24576);
235
    c->predictor = pred;
236
    return pred;
237
}
238
239
9768100
static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
240
{
241
    int step_index;
242
    int predictor;
243
    int sign, delta, diff, step;
244
245
9768100
    step = ff_adpcm_step_table[c->step_index];
246
9768100
    step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
247
9768100
    step_index = av_clip(step_index, 0, 88);
248
249
9768100
    sign = nibble & 8;
250
9768100
    delta = nibble & 7;
251
    /* perform direct multiplication instead of series of jumps proposed by
252
     * the reference ADPCM implementation since modern CPUs can do the mults
253
     * quickly enough */
254
9768100
    diff = ((2 * delta + 1) * step) >> shift;
255
9768100
    predictor = c->predictor;
256
9768100
    if (sign) predictor -= diff;
257
4930690
    else predictor += diff;
258
259
9768100
    c->predictor = av_clip_int16(predictor);
260
9768100
    c->step_index = step_index;
261
262
9768100
    return (int16_t)c->predictor;
263
}
264
265
static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
266
{
267
    int nibble, step_index, predictor, sign, delta, diff, step, shift;
268
269
    shift = bps - 1;
270
    nibble = get_bits_le(gb, bps),
271
    step = ff_adpcm_step_table[c->step_index];
272
    step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
273
    step_index = av_clip(step_index, 0, 88);
274
275
    sign = nibble & (1 << shift);
276
    delta = av_mod_uintp2(nibble, shift);
277
    diff = ((2 * delta + 1) * step) >> shift;
278
    predictor = c->predictor;
279
    if (sign) predictor -= diff;
280
    else predictor += diff;
281
282
    c->predictor = av_clip_int16(predictor);
283
    c->step_index = step_index;
284
285
    return (int16_t)c->predictor;
286
}
287
288
2972672
static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
289
{
290
    int step_index;
291
    int predictor;
292
    int diff, step;
293
294
2972672
    step = ff_adpcm_step_table[c->step_index];
295
2972672
    step_index = c->step_index + ff_adpcm_index_table[nibble];
296
2972672
    step_index = av_clip(step_index, 0, 88);
297
298
2972672
    diff = step >> 3;
299
2972672
    if (nibble & 4) diff += step;
300
2972672
    if (nibble & 2) diff += step >> 1;
301
2972672
    if (nibble & 1) diff += step >> 2;
302
303
2972672
    if (nibble & 8)
304
1480232
        predictor = c->predictor - diff;
305
    else
306
1492440
        predictor = c->predictor + diff;
307
308
2972672
    c->predictor = av_clip_int16(predictor);
309
2972672
    c->step_index = step_index;
310
311
2972672
    return c->predictor;
312
}
313
314
2229948
static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
315
{
316
    int predictor;
317
318
2229948
    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
319
2229948
    predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
320
321
2229948
    c->sample2 = c->sample1;
322
2229948
    c->sample1 = av_clip_int16(predictor);
323
2229948
    c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
324
2229948
    if (c->idelta < 16) c->idelta = 16;
325
2229948
    if (c->idelta > INT_MAX/768) {
326
        av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
327
        c->idelta = INT_MAX/768;
328
    }
329
330
2229948
    return c->sample1;
331
}
332
333
55124
static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
334
{
335
    int step_index, predictor, sign, delta, diff, step;
336
337
55124
    step = ff_adpcm_oki_step_table[c->step_index];
338
55124
    step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
339
55124
    step_index = av_clip(step_index, 0, 48);
340
341
55124
    sign = nibble & 8;
342
55124
    delta = nibble & 7;
343
55124
    diff = ((2 * delta + 1) * step) >> 3;
344
55124
    predictor = c->predictor;
345
55124
    if (sign) predictor -= diff;
346
29772
    else predictor += diff;
347
348
55124
    c->predictor = av_clip_intp2(predictor, 11);
349
55124
    c->step_index = step_index;
350
351
55124
    return c->predictor * 16;
352
}
353
354
524192
static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
355
{
356
    int sign, delta, diff;
357
    int new_step;
358
359
524192
    sign = nibble & 8;
360
524192
    delta = nibble & 7;
361
    /* perform direct multiplication instead of series of jumps proposed by
362
     * the reference ADPCM implementation since modern CPUs can do the mults
363
     * quickly enough */
364
524192
    diff = ((2 * delta + 1) * c->step) >> 3;
365
    /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
366
524192
    c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
367
524192
    c->predictor = av_clip_int16(c->predictor);
368
    /* calculate new step and clamp it to range 511..32767 */
369
524192
    new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
370
524192
    c->step = av_clip(new_step, 511, 32767);
371
372
524192
    return (int16_t)c->predictor;
373
}
374
375
313380
static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
376
{
377
    int sign, delta, diff;
378
379
313380
    sign = nibble & (1<<(size-1));
380
313380
    delta = nibble & ((1<<(size-1))-1);
381
313380
    diff = delta << (7 + c->step + shift);
382
383
    /* clamp result */
384
313380
    c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
385
386
    /* calculate new step */
387

313380
    if (delta >= (2*size - 3) && c->step < 3)
388
75080
        c->step++;
389

238300
    else if (delta == 0 && c->step > 0)
390
75080
        c->step--;
391
392
313380
    return (int16_t) c->predictor;
393
}
394
395
1105920
static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
396
{
397
1105920
    if(!c->step) {
398
5
        c->predictor = 0;
399
5
        c->step = 127;
400
    }
401
402
1105920
    c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
403
1105920
    c->predictor = av_clip_int16(c->predictor);
404
1105920
    c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
405
1105920
    c->step = av_clip(c->step, 127, 24576);
406
1105920
    return c->predictor;
407
}
408
409
static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
410
{
411
    c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];
412
    c->predictor = av_clip_int16(c->predictor);
413
    c->step += ff_adpcm_index_table[nibble];
414
    c->step = av_clip_uintp2(c->step, 5);
415
    return c->predictor;
416
}
417
418
666
static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
419
                     const uint8_t *in, ADPCMChannelStatus *left,
420
                     ADPCMChannelStatus *right, int channels, int sample_offset)
421
{
422
    int i, j;
423
    int shift,filter,f0,f1;
424
    int s_1,s_2;
425
    int d,s,t;
426
427
666
    out0 += sample_offset;
428
666
    if (channels == 1)
429
        out1 = out0 + 28;
430
    else
431
666
        out1 += sample_offset;
432
433
3330
    for(i=0;i<4;i++) {
434
2664
        shift  = 12 - (in[4+i*2] & 15);
435
2664
        filter = in[4+i*2] >> 4;
436
2664
        if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
437
            avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
438
            filter=0;
439
        }
440
2664
        f0 = xa_adpcm_table[filter][0];
441
2664
        f1 = xa_adpcm_table[filter][1];
442
443
2664
        s_1 = left->sample1;
444
2664
        s_2 = left->sample2;
445
446
77256
        for(j=0;j<28;j++) {
447
74592
            d = in[16+i+j*4];
448
449
74592
            t = sign_extend(d, 4);
450
74592
            s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
451
74592
            s_2 = s_1;
452
74592
            s_1 = av_clip_int16(s);
453
74592
            out0[j] = s_1;
454
        }
455
456
2664
        if (channels == 2) {
457
2664
            left->sample1 = s_1;
458
2664
            left->sample2 = s_2;
459
2664
            s_1 = right->sample1;
460
2664
            s_2 = right->sample2;
461
        }
462
463
2664
        shift  = 12 - (in[5+i*2] & 15);
464
2664
        filter = in[5+i*2] >> 4;
465
2664
        if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
466
            avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
467
            filter=0;
468
        }
469
470
2664
        f0 = xa_adpcm_table[filter][0];
471
2664
        f1 = xa_adpcm_table[filter][1];
472
473
77256
        for(j=0;j<28;j++) {
474
74592
            d = in[16+i+j*4];
475
476
74592
            t = sign_extend(d >> 4, 4);
477
74592
            s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
478
74592
            s_2 = s_1;
479
74592
            s_1 = av_clip_int16(s);
480
74592
            out1[j] = s_1;
481
        }
482
483
2664
        if (channels == 2) {
484
2664
            right->sample1 = s_1;
485
2664
            right->sample2 = s_2;
486
        } else {
487
            left->sample1 = s_1;
488
            left->sample2 = s_2;
489
        }
490
491
2664
        out0 += 28 * (3 - channels);
492
2664
        out1 += 28 * (3 - channels);
493
    }
494
495
666
    return 0;
496
}
497
498
260
static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
499
{
500
260
    ADPCMDecodeContext *c = avctx->priv_data;
501
    GetBitContext gb;
502
    const int8_t *table;
503
    int k0, signmask, nb_bits, count;
504
260
    int size = buf_size*8;
505
    int i;
506
507
260
    init_get_bits(&gb, buf, size);
508
509
    //read bits & initial values
510
260
    nb_bits = get_bits(&gb, 2)+2;
511
260
    table = swf_index_tables[nb_bits-2];
512
260
    k0 = 1 << (nb_bits-2);
513
260
    signmask = 1 << (nb_bits-1);
514
515
520
    while (get_bits_count(&gb) <= size - 22*avctx->channels) {
516
780
        for (i = 0; i < avctx->channels; i++) {
517
520
            *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
518
520
            c->status[i].step_index = get_bits(&gb, 6);
519
        }
520
521

532480
        for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
522
            int i;
523
524
1596660
            for (i = 0; i < avctx->channels; i++) {
525
                // similar to IMA adpcm
526
1064440
                int delta = get_bits(&gb, nb_bits);
527
1064440
                int step = ff_adpcm_step_table[c->status[i].step_index];
528
1064440
                int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
529
1064440
                int k = k0;
530
531
                do {
532
3193320
                    if (delta & k)
533
1328304
                        vpdiff += step;
534
3193320
                    step >>= 1;
535
3193320
                    k >>= 1;
536
3193320
                } while(k);
537
1064440
                vpdiff += step;
538
539
1064440
                if (delta & signmask)
540
535145
                    c->status[i].predictor -= vpdiff;
541
                else
542
529295
                    c->status[i].predictor += vpdiff;
543
544
1064440
                c->status[i].step_index += table[delta & (~signmask)];
545
546
1064440
                c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
547
1064440
                c->status[i].predictor = av_clip_int16(c->status[i].predictor);
548
549
1064440
                *samples++ = c->status[i].predictor;
550
            }
551
        }
552
    }
553
260
}
554
555
static inline int16_t adpcm_argo_expand_nibble(int nibble, int shift, int16_t prev0, int16_t prev1)
556
{
557
    return ((8 * prev0) - (4 * prev1) + (nibble * (1 << shift))) >> 2;
558
}
559
560
/**
561
 * Get the number of samples that will be decoded from the packet.
562
 * In one case, this is actually the maximum number of samples possible to
563
 * decode with the given buf_size.
564
 *
565
 * @param[out] coded_samples set to the number of samples as coded in the
566
 *                           packet, or 0 if the codec does not encode the
567
 *                           number of samples in each frame.
568
 * @param[out] approx_nb_samples set to non-zero if the number of samples
569
 *                               returned is an approximation.
570
 */
571
36079
static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
572
                          int buf_size, int *coded_samples, int *approx_nb_samples)
573
{
574
36079
    ADPCMDecodeContext *s = avctx->priv_data;
575
36079
    int nb_samples        = 0;
576
36079
    int ch                = avctx->channels;
577
36079
    int has_coded_samples = 0;
578
    int header_size;
579
580
36079
    *coded_samples = 0;
581
36079
    *approx_nb_samples = 0;
582
583
36079
    if(ch <= 0)
584
        return 0;
585
586

36079
    switch (avctx->codec->id) {
587
    /* constant, only check buf_size */
588
600
    case AV_CODEC_ID_ADPCM_EA_XAS:
589
600
        if (buf_size < 76 * ch)
590
            return 0;
591
600
        nb_samples = 128;
592
600
        break;
593
26342
    case AV_CODEC_ID_ADPCM_IMA_QT:
594
26342
        if (buf_size < 34 * ch)
595
            return 0;
596
26342
        nb_samples = 64;
597
26342
        break;
598
    case AV_CODEC_ID_ADPCM_ARGO:
599
        if (buf_size < 17 * ch)
600
            return 0;
601
        nb_samples = 32;
602
        break;
603
    /* simple 4-bit adpcm */
604
475
    case AV_CODEC_ID_ADPCM_CT:
605
    case AV_CODEC_ID_ADPCM_IMA_APC:
606
    case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
607
    case AV_CODEC_ID_ADPCM_IMA_OKI:
608
    case AV_CODEC_ID_ADPCM_IMA_WS:
609
    case AV_CODEC_ID_ADPCM_YAMAHA:
610
    case AV_CODEC_ID_ADPCM_AICA:
611
475
        nb_samples = buf_size * 2 / ch;
612
475
        break;
613
    }
614
36079
    if (nb_samples)
615
27417
        return nb_samples;
616
617
    /* simple 4-bit adpcm, with header */
618
8662
    header_size = 0;
619

8662
    switch (avctx->codec->id) {
620
145
        case AV_CODEC_ID_ADPCM_4XM:
621
        case AV_CODEC_ID_ADPCM_AGM:
622
        case AV_CODEC_ID_ADPCM_IMA_DAT4:
623
145
        case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
624
161
        case AV_CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
625
350
        case AV_CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4 * ch;      break;
626
    }
627
8662
    if (header_size > 0)
628
656
        return (buf_size - header_size) * 2 / ch;
629
630
    /* more complex formats */
631




8006
    switch (avctx->codec->id) {
632
158
    case AV_CODEC_ID_ADPCM_EA:
633
158
        has_coded_samples = 1;
634
158
        *coded_samples  = bytestream2_get_le32(gb);
635
158
        *coded_samples -= *coded_samples % 28;
636
158
        nb_samples      = (buf_size - 12) / 30 * 28;
637
158
        break;
638
47
    case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
639
47
        has_coded_samples = 1;
640
47
        *coded_samples = bytestream2_get_le32(gb);
641
47
        nb_samples     = (buf_size - (4 + 8 * ch)) * 2 / ch;
642
47
        break;
643
30
    case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
644
30
        nb_samples = (buf_size - ch) / ch * 2;
645
30
        break;
646
400
    case AV_CODEC_ID_ADPCM_EA_R1:
647
    case AV_CODEC_ID_ADPCM_EA_R2:
648
    case AV_CODEC_ID_ADPCM_EA_R3:
649
        /* maximum number of samples */
650
        /* has internal offsets and a per-frame switch to signal raw 16-bit */
651
400
        has_coded_samples = 1;
652

400
        switch (avctx->codec->id) {
653
95
        case AV_CODEC_ID_ADPCM_EA_R1:
654
95
            header_size    = 4 + 9 * ch;
655
95
            *coded_samples = bytestream2_get_le32(gb);
656
95
            break;
657
180
        case AV_CODEC_ID_ADPCM_EA_R2:
658
180
            header_size    = 4 + 5 * ch;
659
180
            *coded_samples = bytestream2_get_le32(gb);
660
180
            break;
661
125
        case AV_CODEC_ID_ADPCM_EA_R3:
662
125
            header_size    = 4 + 5 * ch;
663
125
            *coded_samples = bytestream2_get_be32(gb);
664
125
            break;
665
        }
666
400
        *coded_samples -= *coded_samples % 28;
667
400
        nb_samples      = (buf_size - header_size) * 2 / ch;
668
400
        nb_samples     -= nb_samples % 28;
669
400
        *approx_nb_samples = 1;
670
400
        break;
671
642
    case AV_CODEC_ID_ADPCM_IMA_DK3:
672
642
        if (avctx->block_align > 0)
673
642
            buf_size = FFMIN(buf_size, avctx->block_align);
674
642
        nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
675
642
        break;
676
649
    case AV_CODEC_ID_ADPCM_IMA_DK4:
677
649
        if (avctx->block_align > 0)
678
649
            buf_size = FFMIN(buf_size, avctx->block_align);
679
649
        if (buf_size < 4 * ch)
680
            return AVERROR_INVALIDDATA;
681
649
        nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
682
649
        break;
683
1000
    case AV_CODEC_ID_ADPCM_IMA_RAD:
684
1000
        if (avctx->block_align > 0)
685
1000
            buf_size = FFMIN(buf_size, avctx->block_align);
686
1000
        nb_samples = (buf_size - 4 * ch) * 2 / ch;
687
1000
        break;
688
1668
    case AV_CODEC_ID_ADPCM_IMA_WAV:
689
    {
690
1668
        int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
691
1668
        int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
692
1668
        if (avctx->block_align > 0)
693
1668
            buf_size = FFMIN(buf_size, avctx->block_align);
694
1668
        if (buf_size < 4 * ch)
695
            return AVERROR_INVALIDDATA;
696
1668
        nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
697
1668
        break;
698
    }
699
2943
    case AV_CODEC_ID_ADPCM_MS:
700
2943
        if (avctx->block_align > 0)
701
2943
            buf_size = FFMIN(buf_size, avctx->block_align);
702
2943
        nb_samples = (buf_size - 6 * ch) * 2 / ch;
703
2943
        break;
704
    case AV_CODEC_ID_ADPCM_MTAF:
705
        if (avctx->block_align > 0)
706
            buf_size = FFMIN(buf_size, avctx->block_align);
707
        nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
708
        break;
709
57
    case AV_CODEC_ID_ADPCM_SBPRO_2:
710
    case AV_CODEC_ID_ADPCM_SBPRO_3:
711
    case AV_CODEC_ID_ADPCM_SBPRO_4:
712
    {
713
        int samples_per_byte;
714

57
        switch (avctx->codec->id) {
715
13
        case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
716
18
        case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
717
26
        case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
718
        }
719
57
        if (!s->status[0].step_index) {
720
3
            if (buf_size < ch)
721
                return AVERROR_INVALIDDATA;
722
3
            nb_samples++;
723
3
            buf_size -= ch;
724
        }
725
57
        nb_samples += buf_size * samples_per_byte / ch;
726
57
        break;
727
    }
728
260
    case AV_CODEC_ID_ADPCM_SWF:
729
    {
730
260
        int buf_bits       = buf_size * 8 - 2;
731
260
        int nbits          = (bytestream2_get_byte(gb) >> 6) + 2;
732
260
        int block_hdr_size = 22 * ch;
733
260
        int block_size     = block_hdr_size + nbits * ch * 4095;
734
260
        int nblocks        = buf_bits / block_size;
735
260
        int bits_left      = buf_bits - nblocks * block_size;
736
260
        nb_samples         = nblocks * 4096;
737
260
        if (bits_left >= block_hdr_size)
738
260
            nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
739
260
        break;
740
    }
741
71
    case AV_CODEC_ID_ADPCM_THP:
742
    case AV_CODEC_ID_ADPCM_THP_LE:
743
71
        if (avctx->extradata) {
744
            nb_samples = buf_size * 14 / (8 * ch);
745
            break;
746
        }
747
71
        has_coded_samples = 1;
748
71
        bytestream2_skip(gb, 4); // channel size
749
142
        *coded_samples  = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
750
142
                          bytestream2_get_le32(gb) :
751
71
                          bytestream2_get_be32(gb);
752
71
        buf_size       -= 8 + 36 * ch;
753
71
        buf_size       /= ch;
754
71
        nb_samples      = buf_size / 8 * 14;
755
71
        if (buf_size % 8 > 1)
756
            nb_samples     += (buf_size % 8 - 1) * 2;
757
71
        *approx_nb_samples = 1;
758
71
        break;
759
12
    case AV_CODEC_ID_ADPCM_AFC:
760
12
        nb_samples = buf_size / (9 * ch) * 16;
761
12
        break;
762
37
    case AV_CODEC_ID_ADPCM_XA:
763
37
        nb_samples = (buf_size / 128) * 224 / ch;
764
37
        break;
765
32
    case AV_CODEC_ID_ADPCM_DTK:
766
    case AV_CODEC_ID_ADPCM_PSX:
767
32
        nb_samples = buf_size / (16 * ch) * 28;
768
32
        break;
769
    }
770
771
    /* validate coded sample count */
772

8006
    if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
773
        return AVERROR_INVALIDDATA;
774
775
8006
    return nb_samples;
776
}
777
778
36079
static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
779
                              int *got_frame_ptr, AVPacket *avpkt)
780
{
781
36079
    AVFrame *frame     = data;
782
36079
    const uint8_t *buf = avpkt->data;
783
36079
    int buf_size = avpkt->size;
784
36079
    ADPCMDecodeContext *c = avctx->priv_data;
785
    ADPCMChannelStatus *cs;
786
    int n, m, channel, i;
787
    int16_t *samples;
788
    int16_t **samples_p;
789
    int st; /* stereo */
790
    int count1, count2;
791
    int nb_samples, coded_samples, approx_nb_samples, ret;
792
    GetByteContext gb;
793
794
36079
    bytestream2_init(&gb, buf, buf_size);
795
36079
    nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
796
36079
    if (nb_samples <= 0) {
797
        av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
798
        return AVERROR_INVALIDDATA;
799
    }
800
801
    /* get output buffer */
802
36079
    frame->nb_samples = nb_samples;
803
36079
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
804
        return ret;
805
36079
    samples = (int16_t *)frame->data[0];
806
36079
    samples_p = (int16_t **)frame->extended_data;
807
808
    /* use coded_samples when applicable */
809
    /* it is always <= nb_samples, so the output buffer will be large enough */
810
36079
    if (coded_samples) {
811

676
        if (!approx_nb_samples && coded_samples != nb_samples)
812
            av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
813
676
        frame->nb_samples = nb_samples = coded_samples;
814
    }
815
816
36079
    st = avctx->channels == 2 ? 1 : 0;
817
818








36079
    switch(avctx->codec->id) {
819
26342
    case AV_CODEC_ID_ADPCM_IMA_QT:
820
        /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
821
           Channel data is interleaved per-chunk. */
822
69990
        for (channel = 0; channel < avctx->channels; channel++) {
823
            int predictor;
824
            int step_index;
825
43648
            cs = &(c->status[channel]);
826
            /* (pppppp) (piiiiiii) */
827
828
            /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
829
43648
            predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
830
43648
            step_index = predictor & 0x7F;
831
43648
            predictor &= ~0x7F;
832
833
43648
            if (cs->step_index == step_index) {
834
43648
                int diff = predictor - cs->predictor;
835
43648
                if (diff < 0)
836
39464
                    diff = - diff;
837
43648
                if (diff > 0x7f)
838
30
                    goto update;
839
            } else {
840
            update:
841
30
                cs->step_index = step_index;
842
30
                cs->predictor = predictor;
843
            }
844
845
43648
            if (cs->step_index > 88u){
846
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
847
                       channel, cs->step_index);
848
                return AVERROR_INVALIDDATA;
849
            }
850
851
43648
            samples = samples_p[channel];
852
853
1440384
            for (m = 0; m < 64; m += 2) {
854
1396736
                int byte = bytestream2_get_byteu(&gb);
855
1396736
                samples[m    ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
856
1396736
                samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4  , 3);
857
            }
858
        }
859
26342
        break;
860
1668
    case AV_CODEC_ID_ADPCM_IMA_WAV:
861
5004
        for(i=0; i<avctx->channels; i++){
862
3336
            cs = &(c->status[i]);
863
3336
            cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
864
865
3336
            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
866
3336
            if (cs->step_index > 88u){
867
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
868
                       i, cs->step_index);
869
                return AVERROR_INVALIDDATA;
870
            }
871
        }
872
873
1668
        if (avctx->bits_per_coded_sample != 4) {
874
            int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
875
            int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
876
            uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
877
            GetBitContext g;
878
879
            for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
880
                for (i = 0; i < avctx->channels; i++) {
881
                    int j;
882
883
                    cs = &c->status[i];
884
                    samples = &samples_p[i][1 + n * samples_per_block];
885
                    for (j = 0; j < block_size; j++) {
886
                        temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
887
                                        (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
888
                    }
889
                    ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
890
                    if (ret < 0)
891
                        return ret;
892
                    for (m = 0; m < samples_per_block; m++) {
893
                        samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
894
                                          avctx->bits_per_coded_sample);
895
                    }
896
                }
897
            }
898
            bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
899
        } else {
900
140160
        for (n = 0; n < (nb_samples - 1) / 8; n++) {
901
415476
            for (i = 0; i < avctx->channels; i++) {
902
276984
                cs = &c->status[i];
903
276984
                samples = &samples_p[i][1 + n * 8];
904
1384920
                for (m = 0; m < 8; m += 2) {
905
1107936
                    int v = bytestream2_get_byteu(&gb);
906
1107936
                    samples[m    ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
907
1107936
                    samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
908
                }
909
            }
910
        }
911
        }
912
1668
        break;
913
26
    case AV_CODEC_ID_ADPCM_4XM:
914
78
        for (i = 0; i < avctx->channels; i++)
915
52
            c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
916
917
78
        for (i = 0; i < avctx->channels; i++) {
918
52
            c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
919
52
            if (c->status[i].step_index > 88u) {
920
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
921
                       i, c->status[i].step_index);
922
                return AVERROR_INVALIDDATA;
923
            }
924
        }
925
926
78
        for (i = 0; i < avctx->channels; i++) {
927
52
            samples = (int16_t *)frame->data[i];
928
52
            cs = &c->status[i];
929
38276
            for (n = nb_samples >> 1; n > 0; n--) {
930
38224
                int v = bytestream2_get_byteu(&gb);
931
38224
                *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
932
38224
                *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 4);
933
            }
934
        }
935
26
        break;
936
    case AV_CODEC_ID_ADPCM_AGM:
937
        for (i = 0; i < avctx->channels; i++)
938
            c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
939
        for (i = 0; i < avctx->channels; i++)
940
            c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
941
942
        for (n = 0; n < nb_samples >> (1 - st); n++) {
943
            int v = bytestream2_get_byteu(&gb);
944
            *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
945
            *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
946
        }
947
        break;
948
2943
    case AV_CODEC_ID_ADPCM_MS:
949
    {
950
        int block_predictor;
951
952
2943
        if (avctx->channels > 2) {
953
            for (channel = 0; channel < avctx->channels; channel++) {
954
                samples = samples_p[channel];
955
                block_predictor = bytestream2_get_byteu(&gb);
956
                if (block_predictor > 6) {
957
                    av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
958
                           channel, block_predictor);
959
                    return AVERROR_INVALIDDATA;
960
                }
961
                c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
962
                c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
963
                c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
964
                c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
965
                c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
966
                *samples++ = c->status[channel].sample2;
967
                *samples++ = c->status[channel].sample1;
968
                for(n = (nb_samples - 2) >> 1; n > 0; n--) {
969
                    int byte = bytestream2_get_byteu(&gb);
970
                    *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4  );
971
                    *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
972
                }
973
            }
974
        } else {
975
2943
            block_predictor = bytestream2_get_byteu(&gb);
976
2943
            if (block_predictor > 6) {
977
                av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
978
                       block_predictor);
979
                return AVERROR_INVALIDDATA;
980
            }
981
2943
            c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
982
2943
            c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
983
2943
            if (st) {
984
2895
                block_predictor = bytestream2_get_byteu(&gb);
985
2895
                if (block_predictor > 6) {
986
                    av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
987
                           block_predictor);
988
                    return AVERROR_INVALIDDATA;
989
                }
990
2895
                c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
991
2895
                c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
992
            }
993
2943
            c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
994
2943
            if (st){
995
2895
                c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
996
            }
997
998
2943
            c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
999
2943
            if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1000
2943
            c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1001
2943
            if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1002
1003
2943
            *samples++ = c->status[0].sample2;
1004
2943
            if (st) *samples++ = c->status[1].sample2;
1005
2943
            *samples++ = c->status[0].sample1;
1006
2943
            if (st) *samples++ = c->status[1].sample1;
1007
1117917
            for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1008
1114974
                int byte = bytestream2_get_byteu(&gb);
1009
1114974
                *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4  );
1010
1114974
                *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1011
            }
1012
        }
1013
2943
        break;
1014
    }
1015
    case AV_CODEC_ID_ADPCM_MTAF:
1016
        for (channel = 0; channel < avctx->channels; channel+=2) {
1017
            bytestream2_skipu(&gb, 4);
1018
            c->status[channel    ].step      = bytestream2_get_le16u(&gb) & 0x1f;
1019
            c->status[channel + 1].step      = bytestream2_get_le16u(&gb) & 0x1f;
1020
            c->status[channel    ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1021
            bytestream2_skipu(&gb, 2);
1022
            c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1023
            bytestream2_skipu(&gb, 2);
1024
            for (n = 0; n < nb_samples; n+=2) {
1025
                int v = bytestream2_get_byteu(&gb);
1026
                samples_p[channel][n    ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1027
                samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4  );
1028
            }
1029
            for (n = 0; n < nb_samples; n+=2) {
1030
                int v = bytestream2_get_byteu(&gb);
1031
                samples_p[channel + 1][n    ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1032
                samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4  );
1033
            }
1034
        }
1035
        break;
1036
649
    case AV_CODEC_ID_ADPCM_IMA_DK4:
1037
1947
        for (channel = 0; channel < avctx->channels; channel++) {
1038
1298
            cs = &c->status[channel];
1039
1298
            cs->predictor  = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1040
1298
            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1041
1298
            if (cs->step_index > 88u){
1042
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1043
                       channel, cs->step_index);
1044
                return AVERROR_INVALIDDATA;
1045
            }
1046
        }
1047
1324609
        for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1048
1323960
            int v = bytestream2_get_byteu(&gb);
1049
1323960
            *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4  , 3);
1050
1323960
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1051
        }
1052
649
        break;
1053
642
    case AV_CODEC_ID_ADPCM_IMA_DK3:
1054
    {
1055
642
        int last_byte = 0;
1056
        int nibble;
1057
642
        int decode_top_nibble_next = 0;
1058
        int diff_channel;
1059
642
        const int16_t *samples_end = samples + avctx->channels * nb_samples;
1060
1061
642
        bytestream2_skipu(&gb, 10);
1062
642
        c->status[0].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1063
642
        c->status[1].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1064
642
        c->status[0].step_index = bytestream2_get_byteu(&gb);
1065
642
        c->status[1].step_index = bytestream2_get_byteu(&gb);
1066

642
        if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1067
            av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1068
                   c->status[0].step_index, c->status[1].step_index);
1069
            return AVERROR_INVALIDDATA;
1070
        }
1071
        /* sign extend the predictors */
1072
642
        diff_channel = c->status[1].predictor;
1073
1074
        /* DK3 ADPCM support macro */
1075
#define DK3_GET_NEXT_NIBBLE() \
1076
    if (decode_top_nibble_next) { \
1077
        nibble = last_byte >> 4; \
1078
        decode_top_nibble_next = 0; \
1079
    } else { \
1080
        last_byte = bytestream2_get_byteu(&gb); \
1081
        nibble = last_byte & 0x0F; \
1082
        decode_top_nibble_next = 1; \
1083
    }
1084
1085
869910
        while (samples < samples_end) {
1086
1087
            /* for this algorithm, c->status[0] is the sum channel and
1088
             * c->status[1] is the diff channel */
1089
1090
            /* process the first predictor of the sum channel */
1091
869268
            DK3_GET_NEXT_NIBBLE();
1092
869268
            adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1093
1094
            /* process the diff channel predictor */
1095
869268
            DK3_GET_NEXT_NIBBLE();
1096
869268
            adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1097
1098
            /* process the first pair of stereo PCM samples */
1099
869268
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
1100
869268
            *samples++ = c->status[0].predictor + c->status[1].predictor;
1101
869268
            *samples++ = c->status[0].predictor - c->status[1].predictor;
1102
1103
            /* process the second predictor of the sum channel */
1104
869268
            DK3_GET_NEXT_NIBBLE();
1105
869268
            adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1106
1107
            /* process the second pair of stereo PCM samples */
1108
869268
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
1109
869268
            *samples++ = c->status[0].predictor + c->status[1].predictor;
1110
869268
            *samples++ = c->status[0].predictor - c->status[1].predictor;
1111
        }
1112
1113
642
        if ((bytestream2_tell(&gb) & 1))
1114
642
            bytestream2_skip(&gb, 1);
1115
642
        break;
1116
    }
1117
119
    case AV_CODEC_ID_ADPCM_IMA_ISS:
1118
238
        for (channel = 0; channel < avctx->channels; channel++) {
1119
119
            cs = &c->status[channel];
1120
119
            cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1121
119
            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1122
119
            if (cs->step_index > 88u){
1123
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1124
                       channel, cs->step_index);
1125
                return AVERROR_INVALIDDATA;
1126
            }
1127
        }
1128
1129
60571
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1130
            int v1, v2;
1131
60452
            int v = bytestream2_get_byteu(&gb);
1132
            /* nibbles are swapped for mono */
1133
60452
            if (st) {
1134
                v1 = v >> 4;
1135
                v2 = v & 0x0F;
1136
            } else {
1137
60452
                v2 = v >> 4;
1138
60452
                v1 = v & 0x0F;
1139
            }
1140
60452
            *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1141
60452
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1142
        }
1143
119
        break;
1144
    case AV_CODEC_ID_ADPCM_IMA_DAT4:
1145
        for (channel = 0; channel < avctx->channels; channel++) {
1146
            cs = &c->status[channel];
1147
            samples = samples_p[channel];
1148
            bytestream2_skip(&gb, 4);
1149
            for (n = 0; n < nb_samples; n += 2) {
1150
                int v = bytestream2_get_byteu(&gb);
1151
                *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
1152
                *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1153
            }
1154
        }
1155
        break;
1156
179
    case AV_CODEC_ID_ADPCM_IMA_APC:
1157
732239
        while (bytestream2_get_bytes_left(&gb) > 0) {
1158
732060
            int v = bytestream2_get_byteu(&gb);
1159
732060
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  v >> 4  , 3);
1160
732060
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1161
        }
1162
179
        break;
1163
7
    case AV_CODEC_ID_ADPCM_IMA_OKI:
1164
27569
        while (bytestream2_get_bytes_left(&gb) > 0) {
1165
27562
            int v = bytestream2_get_byteu(&gb);
1166
27562
            *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0],  v >> 4  );
1167
27562
            *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1168
        }
1169
7
        break;
1170
1000
    case AV_CODEC_ID_ADPCM_IMA_RAD:
1171
3000
        for (channel = 0; channel < avctx->channels; channel++) {
1172
2000
            cs = &c->status[channel];
1173
2000
            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1174
2000
            cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1175
2000
            if (cs->step_index > 88u){
1176
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1177
                       channel, cs->step_index);
1178
                return AVERROR_INVALIDDATA;
1179
            }
1180
        }
1181
17000
        for (n = 0; n < nb_samples / 2; n++) {
1182
            int byte[2];
1183
1184
16000
            byte[0] = bytestream2_get_byteu(&gb);
1185
16000
            if (st)
1186
16000
                byte[1] = bytestream2_get_byteu(&gb);
1187
48000
            for(channel = 0; channel < avctx->channels; channel++) {
1188
32000
                *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1189
            }
1190
48000
            for(channel = 0; channel < avctx->channels; channel++) {
1191
32000
                *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4  , 3);
1192
            }
1193
        }
1194
1000
        break;
1195
40
    case AV_CODEC_ID_ADPCM_IMA_WS:
1196
40
        if (c->vqa_version == 3) {
1197
            for (channel = 0; channel < avctx->channels; channel++) {
1198
                int16_t *smp = samples_p[channel];
1199
1200
                for (n = nb_samples / 2; n > 0; n--) {
1201
                    int v = bytestream2_get_byteu(&gb);
1202
                    *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1203
                    *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1204
                }
1205
            }
1206
        } else {
1207
34217
            for (n = nb_samples / 2; n > 0; n--) {
1208
68354
                for (channel = 0; channel < avctx->channels; channel++) {
1209
34177
                    int v = bytestream2_get_byteu(&gb);
1210
34177
                    *samples++  = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1211
34177
                    samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1212
                }
1213
34177
                samples += avctx->channels;
1214
            }
1215
        }
1216
40
        bytestream2_seek(&gb, 0, SEEK_END);
1217
40
        break;
1218
37
    case AV_CODEC_ID_ADPCM_XA:
1219
    {
1220
37
        int16_t *out0 = samples_p[0];
1221
37
        int16_t *out1 = samples_p[1];
1222
37
        int samples_per_block = 28 * (3 - avctx->channels) * 4;
1223
37
        int sample_offset = 0;
1224
        int bytes_remaining;
1225
703
        while (bytestream2_get_bytes_left(&gb) >= 128) {
1226
666
            if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1227
                                 &c->status[0], &c->status[1],
1228
                                 avctx->channels, sample_offset)) < 0)
1229
                return ret;
1230
666
            bytestream2_skipu(&gb, 128);
1231
666
            sample_offset += samples_per_block;
1232
        }
1233
        /* Less than a full block of data left, e.g. when reading from
1234
         * 2324 byte per sector XA; the remainder is padding */
1235
37
        bytes_remaining = bytestream2_get_bytes_left(&gb);
1236
37
        if (bytes_remaining > 0) {
1237
            bytestream2_skip(&gb, bytes_remaining);
1238
        }
1239
37
        break;
1240
    }
1241
47
    case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1242
141
        for (i=0; i<=st; i++) {
1243
94
            c->status[i].step_index = bytestream2_get_le32u(&gb);
1244
94
            if (c->status[i].step_index > 88u) {
1245
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1246
                       i, c->status[i].step_index);
1247
                return AVERROR_INVALIDDATA;
1248
            }
1249
        }
1250
141
        for (i=0; i<=st; i++) {
1251
94
            c->status[i].predictor  = bytestream2_get_le32u(&gb);
1252
94
            if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
1253
                return AVERROR_INVALIDDATA;
1254
        }
1255
1256
69043
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1257
68996
            int byte   = bytestream2_get_byteu(&gb);
1258
68996
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   3);
1259
68996
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1260
        }
1261
47
        break;
1262
49
    case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1263
71441
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1264
71392
            int byte = bytestream2_get_byteu(&gb);
1265
71392
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   6);
1266
71392
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1267
        }
1268
49
        break;
1269
158
    case AV_CODEC_ID_ADPCM_EA:
1270
    {
1271
        int previous_left_sample, previous_right_sample;
1272
        int current_left_sample, current_right_sample;
1273
        int next_left_sample, next_right_sample;
1274
        int coeff1l, coeff2l, coeff1r, coeff2r;
1275
        int shift_left, shift_right;
1276
1277
        /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1278
           each coding 28 stereo samples. */
1279
1280
158
        if(avctx->channels != 2)
1281
            return AVERROR_INVALIDDATA;
1282
1283
158
        current_left_sample   = sign_extend(bytestream2_get_le16u(&gb), 16);
1284
158
        previous_left_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
1285
158
        current_right_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
1286
158
        previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1287
1288
8454
        for (count1 = 0; count1 < nb_samples / 28; count1++) {
1289
8296
            int byte = bytestream2_get_byteu(&gb);
1290
8296
            coeff1l = ea_adpcm_table[ byte >> 4       ];
1291
8296
            coeff2l = ea_adpcm_table[(byte >> 4  ) + 4];
1292
8296
            coeff1r = ea_adpcm_table[ byte & 0x0F];
1293
8296
            coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1294
1295
8296
            byte = bytestream2_get_byteu(&gb);
1296
8296
            shift_left  = 20 - (byte >> 4);
1297
8296
            shift_right = 20 - (byte & 0x0F);
1298
1299
240584
            for (count2 = 0; count2 < 28; count2++) {
1300
232288
                byte = bytestream2_get_byteu(&gb);
1301
232288
                next_left_sample  = sign_extend(byte >> 4, 4) * (1 << shift_left);
1302
232288
                next_right_sample = sign_extend(byte,      4) * (1 << shift_right);
1303
1304
232288
                next_left_sample = (next_left_sample +
1305
232288
                    (current_left_sample * coeff1l) +
1306
232288
                    (previous_left_sample * coeff2l) + 0x80) >> 8;
1307
232288
                next_right_sample = (next_right_sample +
1308
232288
                    (current_right_sample * coeff1r) +
1309
232288
                    (previous_right_sample * coeff2r) + 0x80) >> 8;
1310
1311
232288
                previous_left_sample = current_left_sample;
1312
232288
                current_left_sample = av_clip_int16(next_left_sample);
1313
232288
                previous_right_sample = current_right_sample;
1314
232288
                current_right_sample = av_clip_int16(next_right_sample);
1315
232288
                *samples++ = current_left_sample;
1316
232288
                *samples++ = current_right_sample;
1317
            }
1318
        }
1319
1320
158
        bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1321
1322
158
        break;
1323
    }
1324
30
    case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1325
    {
1326
        int coeff[2][2], shift[2];
1327
1328
90
        for(channel = 0; channel < avctx->channels; channel++) {
1329
60
            int byte = bytestream2_get_byteu(&gb);
1330
180
            for (i=0; i<2; i++)
1331
120
                coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1332
60
            shift[channel] = 20 - (byte & 0x0F);
1333
        }
1334
450
        for (count1 = 0; count1 < nb_samples / 2; count1++) {
1335
            int byte[2];
1336
1337
420
            byte[0] = bytestream2_get_byteu(&gb);
1338
420
            if (st) byte[1] = bytestream2_get_byteu(&gb);
1339
1260
            for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1340
2520
                for(channel = 0; channel < avctx->channels; channel++) {
1341
1680
                    int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1342
1680
                    sample = (sample +
1343
1680
                             c->status[channel].sample1 * coeff[channel][0] +
1344
1680
                             c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1345
1680
                    c->status[channel].sample2 = c->status[channel].sample1;
1346
1680
                    c->status[channel].sample1 = av_clip_int16(sample);
1347
1680
                    *samples++ = c->status[channel].sample1;
1348
                }
1349
            }
1350
        }
1351
30
        bytestream2_seek(&gb, 0, SEEK_END);
1352
30
        break;
1353
    }
1354
400
    case AV_CODEC_ID_ADPCM_EA_R1:
1355
    case AV_CODEC_ID_ADPCM_EA_R2:
1356
    case AV_CODEC_ID_ADPCM_EA_R3: {
1357
        /* channel numbering
1358
           2chan: 0=fl, 1=fr
1359
           4chan: 0=fl, 1=rl, 2=fr, 3=rr
1360
           6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
1361
400
        const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1362
        int previous_sample, current_sample, next_sample;
1363
        int coeff1, coeff2;
1364
        int shift;
1365
        unsigned int channel;
1366
        uint16_t *samplesC;
1367
400
        int count = 0;
1368
        int offsets[6];
1369
1370
1200
        for (channel=0; channel<avctx->channels; channel++)
1371
800
            offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1372
550
                                             bytestream2_get_le32(&gb)) +
1373
800
                               (avctx->channels + 1) * 4;
1374
1375
1200
        for (channel=0; channel<avctx->channels; channel++) {
1376
800
            bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1377
800
            samplesC = samples_p[channel];
1378
1379
800
            if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1380
190
                current_sample  = sign_extend(bytestream2_get_le16(&gb), 16);
1381
190
                previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1382
            } else {
1383
610
                current_sample  = c->status[channel].predictor;
1384
610
                previous_sample = c->status[channel].prev_sample;
1385
            }
1386
1387
41992
            for (count1 = 0; count1 < nb_samples / 28; count1++) {
1388
41192
                int byte = bytestream2_get_byte(&gb);
1389
41192
                if (byte == 0xEE) {  /* only seen in R2 and R3 */
1390
12
                    current_sample  = sign_extend(bytestream2_get_be16(&gb), 16);
1391
12
                    previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1392
1393
348
                    for (count2=0; count2<28; count2++)
1394
336
                        *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1395
                } else {
1396
41180
                    coeff1 = ea_adpcm_table[ byte >> 4     ];
1397
41180
                    coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1398
41180
                    shift = 20 - (byte & 0x0F);
1399
1400
1194220
                    for (count2=0; count2<28; count2++) {
1401
1153040
                        if (count2 & 1)
1402
576520
                            next_sample = (unsigned)sign_extend(byte,    4) << shift;
1403
                        else {
1404
576520
                            byte = bytestream2_get_byte(&gb);
1405
576520
                            next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
1406
                        }
1407
1408
1153040
                        next_sample += (current_sample  * coeff1) +
1409
1153040
                                       (previous_sample * coeff2);
1410
1153040
                        next_sample = av_clip_int16(next_sample >> 8);
1411
1412
1153040
                        previous_sample = current_sample;
1413
1153040
                        current_sample  = next_sample;
1414
1153040
                        *samplesC++ = current_sample;
1415
                    }
1416
                }
1417
            }
1418
800
            if (!count) {
1419
400
                count = count1;
1420
400
            } else if (count != count1) {
1421
                av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1422
                count = FFMAX(count, count1);
1423
            }
1424
1425
800
            if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1426
610
                c->status[channel].predictor   = current_sample;
1427
610
                c->status[channel].prev_sample = previous_sample;
1428
            }
1429
        }
1430
1431
400
        frame->nb_samples = count * 28;
1432
400
        bytestream2_seek(&gb, 0, SEEK_END);
1433
400
        break;
1434
    }
1435
600
    case AV_CODEC_ID_ADPCM_EA_XAS:
1436
1200
        for (channel=0; channel<avctx->channels; channel++) {
1437
            int coeff[2][4], shift[4];
1438
600
            int16_t *s = samples_p[channel];
1439
3000
            for (n = 0; n < 4; n++, s += 32) {
1440
2400
                int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1441
7200
                for (i=0; i<2; i++)
1442
4800
                    coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1443
2400
                s[0] = val & ~0x0F;
1444
1445
2400
                val = sign_extend(bytestream2_get_le16u(&gb), 16);
1446
2400
                shift[n] = 20 - (val & 0x0F);
1447
2400
                s[1] = val & ~0x0F;
1448
            }
1449
1450
9600
            for (m=2; m<32; m+=2) {
1451
9000
                s = &samples_p[channel][m];
1452
45000
                for (n = 0; n < 4; n++, s += 32) {
1453
                    int level, pred;
1454
36000
                    int byte = bytestream2_get_byteu(&gb);
1455
1456
36000
                    level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
1457
36000
                    pred  = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1458
36000
                    s[0]  = av_clip_int16((level + pred + 0x80) >> 8);
1459
1460
36000
                    level = sign_extend(byte, 4) * (1 << shift[n]);
1461
36000
                    pred  = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1462
36000
                    s[1]  = av_clip_int16((level + pred + 0x80) >> 8);
1463
                }
1464
            }
1465
        }
1466
600
        break;
1467
161
    case AV_CODEC_ID_ADPCM_IMA_AMV:
1468
161
        c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1469
161
        c->status[0].step_index = bytestream2_get_byteu(&gb);
1470
161
        bytestream2_skipu(&gb, 5);
1471
161
        if (c->status[0].step_index > 88u) {
1472
            av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1473
                   c->status[0].step_index);
1474
            return AVERROR_INVALIDDATA;
1475
        }
1476
1477
111112
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1478
110951
            int v = bytestream2_get_byteu(&gb);
1479
1480
110951
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1481
110951
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1482
        }
1483
161
        break;
1484
350
    case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1485
700
        for (i = 0; i < avctx->channels; i++) {
1486
350
            c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1487
350
            c->status[i].step_index = bytestream2_get_byteu(&gb);
1488
350
            bytestream2_skipu(&gb, 1);
1489
350
            if (c->status[i].step_index > 88u) {
1490
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1491
                       c->status[i].step_index);
1492
                return AVERROR_INVALIDDATA;
1493
            }
1494
        }
1495
1496
89950
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1497
89600
            int v = bytestream2_get_byteu(&gb);
1498
1499
89600
            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1500
89600
            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1501
        }
1502
350
        break;
1503
64
    case AV_CODEC_ID_ADPCM_CT:
1504
262160
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1505
262096
            int v = bytestream2_get_byteu(&gb);
1506
262096
            *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4  );
1507
262096
            *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1508
        }
1509
64
        break;
1510
57
    case AV_CODEC_ID_ADPCM_SBPRO_4:
1511
    case AV_CODEC_ID_ADPCM_SBPRO_3:
1512
    case AV_CODEC_ID_ADPCM_SBPRO_2:
1513
57
        if (!c->status[0].step_index) {
1514
            /* the first byte is a raw sample */
1515
3
            *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1516
3
            if (st)
1517
                *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1518
3
            c->status[0].step_index = 1;
1519
3
            nb_samples--;
1520
        }
1521
57
        if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1522
52256
            for (n = nb_samples >> (1 - st); n > 0; n--) {
1523
52230
                int byte = bytestream2_get_byteu(&gb);
1524
104460
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1525
52230
                                                       byte >> 4,   4, 0);
1526
52230
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1527
52230
                                                       byte & 0x0F, 4, 0);
1528
            }
1529
31
        } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1530
34838
            for (n = (nb_samples<<st) / 3; n > 0; n--) {
1531
34820
                int byte = bytestream2_get_byteu(&gb);
1532
69640
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1533
34820
                                                        byte >> 5        , 3, 0);
1534
69640
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1535
34820
                                                       (byte >> 2) & 0x07, 3, 0);
1536
34820
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1537
34820
                                                        byte & 0x03,       2, 0);
1538
            }
1539
        } else {
1540
26128
            for (n = nb_samples >> (2 - st); n > 0; n--) {
1541
26115
                int byte = bytestream2_get_byteu(&gb);
1542
52230
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1543
26115
                                                        byte >> 6        , 2, 2);
1544
52230
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1545
26115
                                                       (byte >> 4) & 0x03, 2, 2);
1546
52230
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1547
26115
                                                       (byte >> 2) & 0x03, 2, 2);
1548
26115
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1549
26115
                                                        byte & 0x03,       2, 2);
1550
            }
1551
        }
1552
57
        break;
1553
260
    case AV_CODEC_ID_ADPCM_SWF:
1554
260
        adpcm_swf_decode(avctx, buf, buf_size, samples);
1555
260
        bytestream2_seek(&gb, 0, SEEK_END);
1556
260
        break;
1557
136
    case AV_CODEC_ID_ADPCM_YAMAHA:
1558
553096
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1559
552960
            int v = bytestream2_get_byteu(&gb);
1560
552960
            *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1561
552960
            *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4  );
1562
        }
1563
136
        break;
1564
    case AV_CODEC_ID_ADPCM_AICA:
1565
        if (!c->has_status) {
1566
            for (channel = 0; channel < avctx->channels; channel++)
1567
                c->status[channel].step = 0;
1568
            c->has_status = 1;
1569
        }
1570
        for (channel = 0; channel < avctx->channels; channel++) {
1571
            samples = samples_p[channel];
1572
            for (n = nb_samples >> 1; n > 0; n--) {
1573
                int v = bytestream2_get_byteu(&gb);
1574
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1575
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4  );
1576
            }
1577
        }
1578
        break;
1579
12
    case AV_CODEC_ID_ADPCM_AFC:
1580
    {
1581
        int samples_per_block;
1582
        int blocks;
1583
1584

12
        if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1585
            samples_per_block = avctx->extradata[0] / 16;
1586
            blocks = nb_samples / avctx->extradata[0];
1587
        } else {
1588
12
            samples_per_block = nb_samples / 16;
1589
12
            blocks = 1;
1590
        }
1591
1592
24
        for (m = 0; m < blocks; m++) {
1593
36
        for (channel = 0; channel < avctx->channels; channel++) {
1594
24
            int prev1 = c->status[channel].sample1;
1595
24
            int prev2 = c->status[channel].sample2;
1596
1597
24
            samples = samples_p[channel] + m * 16;
1598
            /* Read in every sample for this channel.  */
1599
26904
            for (i = 0; i < samples_per_block; i++) {
1600
26880
                int byte = bytestream2_get_byteu(&gb);
1601
26880
                int scale = 1 << (byte >> 4);
1602
26880
                int index = byte & 0xf;
1603
26880
                int factor1 = ff_adpcm_afc_coeffs[0][index];
1604
26880
                int factor2 = ff_adpcm_afc_coeffs[1][index];
1605
1606
                /* Decode 16 samples.  */
1607
456960
                for (n = 0; n < 16; n++) {
1608
                    int32_t sampledat;
1609
1610
430080
                    if (n & 1) {
1611
215040
                        sampledat = sign_extend(byte, 4);
1612
                    } else {
1613
215040
                        byte = bytestream2_get_byteu(&gb);
1614
215040
                        sampledat = sign_extend(byte >> 4, 4);
1615
                    }
1616
1617
430080
                    sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
1618
430080
                                sampledat * scale;
1619
430080
                    *samples = av_clip_int16(sampledat);
1620
430080
                    prev2 = prev1;
1621
430080
                    prev1 = *samples++;
1622
                }
1623
            }
1624
1625
24
            c->status[channel].sample1 = prev1;
1626
24
            c->status[channel].sample2 = prev2;
1627
        }
1628
        }
1629
12
        bytestream2_seek(&gb, 0, SEEK_END);
1630
12
        break;
1631
    }
1632
71
    case AV_CODEC_ID_ADPCM_THP:
1633
    case AV_CODEC_ID_ADPCM_THP_LE:
1634
    {
1635
        int table[14][16];
1636
        int ch;
1637
1638
#define THP_GET16(g) \
1639
    sign_extend( \
1640
        avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1641
        bytestream2_get_le16u(&(g)) : \
1642
        bytestream2_get_be16u(&(g)), 16)
1643
1644
71
        if (avctx->extradata) {
1645
            GetByteContext tb;
1646
            if (avctx->extradata_size < 32 * avctx->channels) {
1647
                av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1648
                return AVERROR_INVALIDDATA;
1649
            }
1650
1651
            bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1652
            for (i = 0; i < avctx->channels; i++)
1653
                for (n = 0; n < 16; n++)
1654
                    table[i][n] = THP_GET16(tb);
1655
        } else {
1656
213
            for (i = 0; i < avctx->channels; i++)
1657
2414
                for (n = 0; n < 16; n++)
1658
2272
                    table[i][n] = THP_GET16(gb);
1659
1660
71
            if (!c->has_status) {
1661
                /* Initialize the previous sample.  */
1662
3
                for (i = 0; i < avctx->channels; i++) {
1663
2
                    c->status[i].sample1 = THP_GET16(gb);
1664
2
                    c->status[i].sample2 = THP_GET16(gb);
1665
                }
1666
1
                c->has_status = 1;
1667
            } else {
1668
70
                bytestream2_skip(&gb, avctx->channels * 4);
1669
            }
1670
        }
1671
1672
213
        for (ch = 0; ch < avctx->channels; ch++) {
1673
142
            samples = samples_p[ch];
1674
1675
            /* Read in every sample for this channel.  */
1676
10982
            for (i = 0; i < (nb_samples + 13) / 14; i++) {
1677
10840
                int byte = bytestream2_get_byteu(&gb);
1678
10840
                int index = (byte >> 4) & 7;
1679
10840
                unsigned int exp = byte & 0x0F;
1680
10840
                int factor1 = table[ch][index * 2];
1681
10840
                int factor2 = table[ch][index * 2 + 1];
1682
1683
                /* Decode 14 samples.  */
1684

162600
                for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1685
                    int32_t sampledat;
1686
1687
151760
                    if (n & 1) {
1688
75880
                        sampledat = sign_extend(byte, 4);
1689
                    } else {
1690
75880
                        byte = bytestream2_get_byteu(&gb);
1691
75880
                        sampledat = sign_extend(byte >> 4, 4);
1692
                    }
1693
1694
151760
                    sampledat = ((c->status[ch].sample1 * factor1
1695
151760
                                + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
1696
151760
                    *samples = av_clip_int16(sampledat);
1697
151760
                    c->status[ch].sample2 = c->status[ch].sample1;
1698
151760
                    c->status[ch].sample1 = *samples++;
1699
                }
1700
            }
1701
        }
1702
71
        break;
1703
    }
1704
32
    case AV_CODEC_ID_ADPCM_DTK:
1705
96
        for (channel = 0; channel < avctx->channels; channel++) {
1706
64
            samples = samples_p[channel];
1707
1708
            /* Read in every sample for this channel.  */
1709
2112
            for (i = 0; i < nb_samples / 28; i++) {
1710
                int byte, header;
1711
2048
                if (channel)
1712
1024
                    bytestream2_skipu(&gb, 1);
1713
2048
                header = bytestream2_get_byteu(&gb);
1714
2048
                bytestream2_skipu(&gb, 3 - channel);
1715
1716
                /* Decode 28 samples.  */
1717
59392
                for (n = 0; n < 28; n++) {
1718
                    int32_t sampledat, prev;
1719
1720

57344
                    switch (header >> 4) {
1721
3388
                    case 1:
1722
3388
                        prev = (c->status[channel].sample1 * 0x3c);
1723
3388
                        break;
1724
50876
                    case 2:
1725
50876
                        prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1726
50876
                        break;
1727
2744
                    case 3:
1728
2744
                        prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1729
2744
                        break;
1730
336
                    default:
1731
336
                        prev = 0;
1732
                    }
1733
1734
57344
                    prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1735
1736
57344
                    byte = bytestream2_get_byteu(&gb);
1737
57344
                    if (!channel)
1738
28672
                        sampledat = sign_extend(byte, 4);
1739
                    else
1740
28672
                        sampledat = sign_extend(byte >> 4, 4);
1741
1742
57344
                    sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
1743
57344
                    *samples++ = av_clip_int16(sampledat >> 6);
1744
57344
                    c->status[channel].sample2 = c->status[channel].sample1;
1745
57344
                    c->status[channel].sample1 = sampledat;
1746
                }
1747
            }
1748
64
            if (!channel)
1749
32
                bytestream2_seek(&gb, 0, SEEK_SET);
1750
        }
1751
32
        break;
1752
    case AV_CODEC_ID_ADPCM_PSX:
1753
        for (channel = 0; channel < avctx->channels; channel++) {
1754
            samples = samples_p[channel];
1755
1756
            /* Read in every sample for this channel.  */
1757
            for (i = 0; i < nb_samples / 28; i++) {
1758
                int filter, shift, flag, byte;
1759
1760
                filter = bytestream2_get_byteu(&gb);
1761
                shift  = filter & 0xf;
1762
                filter = filter >> 4;
1763
                if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1764
                    return AVERROR_INVALIDDATA;
1765
                flag   = bytestream2_get_byteu(&gb);
1766
1767
                /* Decode 28 samples.  */
1768
                for (n = 0; n < 28; n++) {
1769
                    int sample = 0, scale;
1770
1771
                    if (flag < 0x07) {
1772
                        if (n & 1) {
1773
                            scale = sign_extend(byte >> 4, 4);
1774
                        } else {
1775
                            byte  = bytestream2_get_byteu(&gb);
1776
                            scale = sign_extend(byte, 4);
1777
                        }
1778
1779
                        scale  = scale << 12;
1780
                        sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1781
                    }
1782
                    *samples++ = av_clip_int16(sample);
1783
                    c->status[channel].sample2 = c->status[channel].sample1;
1784
                    c->status[channel].sample1 = sample;
1785
                }
1786
            }
1787
        }
1788
        break;
1789
    case AV_CODEC_ID_ADPCM_ARGO:
1790
        /*
1791
         * The format of each block:
1792
         *   uint8_t left_control;
1793
         *   uint4_t left_samples[nb_samples];
1794
         *   ---- and if stereo ----
1795
         *   uint8_t right_control;
1796
         *   uint4_t right_samples[nb_samples];
1797
         *
1798
         * Format of the control byte:
1799
         * MSB [SSSSDRRR] LSB
1800
         *   S = (Shift Amount - 2)
1801
         *   D = Decoder flag.
1802
         *   R = Reserved
1803
         *
1804
         * Each block relies on the previous two samples of each channel.
1805
         * They should be 0 initially.
1806
         */
1807
        for (channel = 0; channel < avctx->channels; channel++) {
1808
            int control, shift, sample, nibble;
1809
1810
            samples = samples_p[channel];
1811
            cs = c->status + channel;
1812
1813
            /* Get the control byte and decode the samples, 2 at a time. */
1814
            control = bytestream2_get_byteu(&gb);
1815
            shift = (control >> 4) + 2;
1816
1817
            for (n = 0; n < nb_samples / 2; n++) {
1818
                sample = bytestream2_get_byteu(&gb);
1819
1820
                nibble = sign_extend(sample >> 4, 4);
1821
                if (control & 0x04)
1822
                    *samples = adpcm_argo_expand_nibble(nibble, shift, cs->sample1, cs->sample2);
1823
                else
1824
                    *samples = adpcm_argo_expand_nibble(nibble, shift, cs->sample1, cs->sample1);
1825
1826
                cs->sample2 = cs->sample1;
1827
                cs->sample1 = *samples++;
1828
1829
                nibble = sign_extend(sample >> 0, 4);
1830
                if (control & 0x04)
1831
                    *samples = adpcm_argo_expand_nibble(nibble, shift, cs->sample1, cs->sample2);
1832
                else
1833
                    *samples = adpcm_argo_expand_nibble(nibble, shift, cs->sample1, cs->sample1);
1834
1835
                cs->sample2 = cs->sample1;
1836
                cs->sample1 = *samples++;
1837
            }
1838
        }
1839
        break;
1840
    default:
1841
        av_assert0(0); // unsupported codec_id should not happen
1842
    }
1843
1844

36079
    if (avpkt->size && bytestream2_tell(&gb) == 0) {
1845
        av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1846
        return AVERROR_INVALIDDATA;
1847
    }
1848
1849
36079
    *got_frame_ptr = 1;
1850
1851
36079
    if (avpkt->size < bytestream2_tell(&gb)) {
1852
        av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1853
        return avpkt->size;
1854
    }
1855
1856
36079
    return bytestream2_tell(&gb);
1857
}
1858
1859
static void adpcm_flush(AVCodecContext *avctx)
1860
{
1861
    ADPCMDecodeContext *c = avctx->priv_data;
1862
    c->has_status = 0;
1863
}
1864
1865
1866
static const enum AVSampleFormat sample_fmts_s16[]  = { AV_SAMPLE_FMT_S16,
1867
                                                        AV_SAMPLE_FMT_NONE };
1868
static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
1869
                                                        AV_SAMPLE_FMT_NONE };
1870
static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
1871
                                                        AV_SAMPLE_FMT_S16P,
1872
                                                        AV_SAMPLE_FMT_NONE };
1873
1874
#define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1875
AVCodec ff_ ## name_ ## _decoder = {                        \
1876
    .name           = #name_,                               \
1877
    .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
1878
    .type           = AVMEDIA_TYPE_AUDIO,                   \
1879
    .id             = id_,                                  \
1880
    .priv_data_size = sizeof(ADPCMDecodeContext),           \
1881
    .init           = adpcm_decode_init,                    \
1882
    .decode         = adpcm_decode_frame,                   \
1883
    .flush          = adpcm_flush,                          \
1884
    .capabilities   = AV_CODEC_CAP_DR1,                     \
1885
    .sample_fmts    = sample_fmts_,                         \
1886
}
1887
1888
/* Note: Do not forget to add new entries to the Makefile as well. */
1889
ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM,         sample_fmts_s16p, adpcm_4xm,         "ADPCM 4X Movie");
1890
ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC,         sample_fmts_s16p, adpcm_afc,         "ADPCM Nintendo Gamecube AFC");
1891
ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM,         sample_fmts_s16,  adpcm_agm,         "ADPCM AmuseGraphics Movie");
1892
ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA,        sample_fmts_s16p, adpcm_aica,        "ADPCM Yamaha AICA");
1893
ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO,        sample_fmts_s16p, adpcm_argo,        "ADPCM Argonaut Games");
1894
ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT,          sample_fmts_s16,  adpcm_ct,          "ADPCM Creative Technology");
1895
ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK,         sample_fmts_s16p, adpcm_dtk,         "ADPCM Nintendo Gamecube DTK");
1896
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA,          sample_fmts_s16,  adpcm_ea,          "ADPCM Electronic Arts");
1897
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16,  adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1898
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1,       sample_fmts_s16p, adpcm_ea_r1,       "ADPCM Electronic Arts R1");
1899
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2,       sample_fmts_s16p, adpcm_ea_r2,       "ADPCM Electronic Arts R2");
1900
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3,       sample_fmts_s16p, adpcm_ea_r3,       "ADPCM Electronic Arts R3");
1901
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS,      sample_fmts_s16p, adpcm_ea_xas,      "ADPCM Electronic Arts XAS");
1902
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV,     sample_fmts_s16,  adpcm_ima_amv,     "ADPCM IMA AMV");
1903
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC,     sample_fmts_s16,  adpcm_ima_apc,     "ADPCM IMA CRYO APC");
1904
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4,    sample_fmts_s16,  adpcm_ima_dat4,    "ADPCM IMA Eurocom DAT4");
1905
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3,     sample_fmts_s16,  adpcm_ima_dk3,     "ADPCM IMA Duck DK3");
1906
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4,     sample_fmts_s16,  adpcm_ima_dk4,     "ADPCM IMA Duck DK4");
1907
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16,  adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1908
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16,  adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1909
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS,     sample_fmts_s16,  adpcm_ima_iss,     "ADPCM IMA Funcom ISS");
1910
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI,     sample_fmts_s16,  adpcm_ima_oki,     "ADPCM IMA Dialogic OKI");
1911
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT,      sample_fmts_s16p, adpcm_ima_qt,      "ADPCM IMA QuickTime");
1912
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD,     sample_fmts_s16,  adpcm_ima_rad,     "ADPCM IMA Radical");
1913
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG,  sample_fmts_s16,  adpcm_ima_smjpeg,  "ADPCM IMA Loki SDL MJPEG");
1914
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV,     sample_fmts_s16p, adpcm_ima_wav,     "ADPCM IMA WAV");
1915
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS,      sample_fmts_both, adpcm_ima_ws,      "ADPCM IMA Westwood");
1916
ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS,          sample_fmts_both, adpcm_ms,          "ADPCM Microsoft");
1917
ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF,        sample_fmts_s16p, adpcm_mtaf,        "ADPCM MTAF");
1918
ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX,         sample_fmts_s16p, adpcm_psx,         "ADPCM Playstation");
1919
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2,     sample_fmts_s16,  adpcm_sbpro_2,     "ADPCM Sound Blaster Pro 2-bit");
1920
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3,     sample_fmts_s16,  adpcm_sbpro_3,     "ADPCM Sound Blaster Pro 2.6-bit");
1921
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4,     sample_fmts_s16,  adpcm_sbpro_4,     "ADPCM Sound Blaster Pro 4-bit");
1922
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF,         sample_fmts_s16,  adpcm_swf,         "ADPCM Shockwave Flash");
1923
ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE,      sample_fmts_s16p, adpcm_thp_le,      "ADPCM Nintendo THP (little-endian)");
1924
ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP,         sample_fmts_s16p, adpcm_thp,         "ADPCM Nintendo THP");
1925
ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA,          sample_fmts_s16p, adpcm_xa,          "ADPCM CDROM XA");
1926
ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA,      sample_fmts_s16,  adpcm_yamaha,      "ADPCM Yamaha");