GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/adpcm.c Lines: 948 1300 72.9 %
Date: 2021-04-20 15:25:36 Branches: 441 663 66.5 %

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
 * Simon & Schuster Interactive ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
17
 * Ubisoft ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
18
 * High Voltage Software ALP decoder by Zane van Iperen (zane@zanevaniperen.com)
19
 * Cunning Developments decoder by Zane van Iperen (zane@zanevaniperen.com)
20
 *
21
 * This file is part of FFmpeg.
22
 *
23
 * FFmpeg is free software; you can redistribute it and/or
24
 * modify it under the terms of the GNU Lesser General Public
25
 * License as published by the Free Software Foundation; either
26
 * version 2.1 of the License, or (at your option) any later version.
27
 *
28
 * FFmpeg is distributed in the hope that it will be useful,
29
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31
 * Lesser General Public License for more details.
32
 *
33
 * You should have received a copy of the GNU Lesser General Public
34
 * License along with FFmpeg; if not, write to the Free Software
35
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36
 */
37
#include "avcodec.h"
38
#include "get_bits.h"
39
#include "bytestream.h"
40
#include "adpcm.h"
41
#include "adpcm_data.h"
42
#include "internal.h"
43
44
/**
45
 * @file
46
 * ADPCM decoders
47
 * Features and limitations:
48
 *
49
 * Reference documents:
50
 * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
51
 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
52
 * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
53
 * http://openquicktime.sourceforge.net/
54
 * XAnim sources (xa_codec.c) http://xanim.polter.net/
55
 * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
56
 * SoX source code http://sox.sourceforge.net/
57
 *
58
 * CD-ROM XA:
59
 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
60
 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
61
 * readstr http://www.geocities.co.jp/Playtown/2004/
62
 */
63
64
/* These are for CD-ROM XA ADPCM */
65
static const int8_t xa_adpcm_table[5][2] = {
66
    {   0,   0 },
67
    {  60,   0 },
68
    { 115, -52 },
69
    {  98, -55 },
70
    { 122, -60 }
71
};
72
73
static const int16_t ea_adpcm_table[] = {
74
    0,  240,  460,  392,
75
    0,    0, -208, -220,
76
    0,    1,    3,    4,
77
    7,    8,   10,   11,
78
    0,   -1,   -3,   -4
79
};
80
81
// padded to zero where table size is less then 16
82
static const int8_t swf_index_tables[4][16] = {
83
    /*2*/ { -1, 2 },
84
    /*3*/ { -1, -1, 2, 4 },
85
    /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
86
    /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
87
};
88
89
static const int8_t zork_index_table[8] = {
90
    -1, -1, -1, 1, 4, 7, 10, 12,
91
};
92
93
static const int8_t mtf_index_table[16] = {
94
     8,  6,  4,  2, -1, -1, -1, -1,
95
    -1, -1, -1, -1,  2,  4,  6,  8,
96
};
97
98
/* end of tables */
99
100
typedef struct ADPCMDecodeContext {
101
    ADPCMChannelStatus status[14];
102
    int vqa_version;                /**< VQA version. Used for ADPCM_IMA_WS */
103
    int has_status;                 /**< Status flag. Reset to 0 after a flush. */
104
} ADPCMDecodeContext;
105
106
static void adpcm_flush(AVCodecContext *avctx);
107
108
183
static av_cold int adpcm_decode_init(AVCodecContext * avctx)
109
{
110
183
    ADPCMDecodeContext *c = avctx->priv_data;
111
183
    unsigned int min_channels = 1;
112
183
    unsigned int max_channels = 2;
113
114

183
    switch(avctx->codec->id) {
115
3
    case AV_CODEC_ID_ADPCM_IMA_AMV:
116
3
        max_channels = 1;
117
3
        break;
118
8
    case AV_CODEC_ID_ADPCM_DTK:
119
    case AV_CODEC_ID_ADPCM_EA:
120
8
        min_channels = 2;
121
8
        break;
122
23
    case AV_CODEC_ID_ADPCM_AFC:
123
    case AV_CODEC_ID_ADPCM_EA_R1:
124
    case AV_CODEC_ID_ADPCM_EA_R2:
125
    case AV_CODEC_ID_ADPCM_EA_R3:
126
    case AV_CODEC_ID_ADPCM_EA_XAS:
127
    case AV_CODEC_ID_ADPCM_MS:
128
23
        max_channels = 6;
129
23
        break;
130
    case AV_CODEC_ID_ADPCM_MTAF:
131
        min_channels = 2;
132
        max_channels = 8;
133
        if (avctx->channels & 1) {
134
            avpriv_request_sample(avctx, "channel count %d", avctx->channels);
135
            return AVERROR_PATCHWELCOME;
136
        }
137
        break;
138
    case AV_CODEC_ID_ADPCM_PSX:
139
        max_channels = 8;
140
        if (avctx->channels <= 0 || avctx->block_align % (16 * avctx->channels))
141
            return AVERROR_INVALIDDATA;
142
        break;
143
8
    case AV_CODEC_ID_ADPCM_IMA_DAT4:
144
    case AV_CODEC_ID_ADPCM_THP:
145
    case AV_CODEC_ID_ADPCM_THP_LE:
146
8
        max_channels = 14;
147
8
        break;
148
    }
149

183
    if (avctx->channels < min_channels || avctx->channels > max_channels) {
150
        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
151
        return AVERROR(EINVAL);
152
    }
153
154

183
    switch(avctx->codec->id) {
155
10
    case AV_CODEC_ID_ADPCM_IMA_WAV:
156

10
        if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
157
            return AVERROR_INVALIDDATA;
158
10
        break;
159
6
    case AV_CODEC_ID_ADPCM_ARGO:
160

6
        if (avctx->bits_per_coded_sample != 4 || avctx->block_align != 17 * avctx->channels)
161
            return AVERROR_INVALIDDATA;
162
6
        break;
163
    case AV_CODEC_ID_ADPCM_ZORK:
164
        if (avctx->bits_per_coded_sample != 8)
165
            return AVERROR_INVALIDDATA;
166
        break;
167
167
    default:
168
167
        break;
169
    }
170
171

183
    switch (avctx->codec->id) {
172
87
    case AV_CODEC_ID_ADPCM_AICA:
173
    case AV_CODEC_ID_ADPCM_IMA_CUNNING:
174
    case AV_CODEC_ID_ADPCM_IMA_DAT4:
175
    case AV_CODEC_ID_ADPCM_IMA_QT:
176
    case AV_CODEC_ID_ADPCM_IMA_WAV:
177
    case AV_CODEC_ID_ADPCM_4XM:
178
    case AV_CODEC_ID_ADPCM_XA:
179
    case AV_CODEC_ID_ADPCM_EA_R1:
180
    case AV_CODEC_ID_ADPCM_EA_R2:
181
    case AV_CODEC_ID_ADPCM_EA_R3:
182
    case AV_CODEC_ID_ADPCM_EA_XAS:
183
    case AV_CODEC_ID_ADPCM_THP:
184
    case AV_CODEC_ID_ADPCM_THP_LE:
185
    case AV_CODEC_ID_ADPCM_AFC:
186
    case AV_CODEC_ID_ADPCM_DTK:
187
    case AV_CODEC_ID_ADPCM_PSX:
188
    case AV_CODEC_ID_ADPCM_MTAF:
189
    case AV_CODEC_ID_ADPCM_ARGO:
190
    case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
191
87
        avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
192
87
        break;
193
4
    case AV_CODEC_ID_ADPCM_IMA_WS:
194
4
        avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
195
                                                  AV_SAMPLE_FMT_S16;
196
4
        break;
197
11
    case AV_CODEC_ID_ADPCM_MS:
198
11
        avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
199
                                                  AV_SAMPLE_FMT_S16;
200
11
        break;
201
81
    default:
202
81
        avctx->sample_fmt = AV_SAMPLE_FMT_S16;
203
    }
204
205
183
    adpcm_flush(avctx);
206
183
    return 0;
207
}
208
209
static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
210
{
211
    int delta, pred, step, add;
212
213
    pred = c->predictor;
214
    delta = nibble & 7;
215
    step = c->step;
216
    add = (delta * 2 + 1) * step;
217
    if (add < 0)
218
        add = add + 7;
219
220
    if ((nibble & 8) == 0)
221
        pred = av_clip(pred + (add >> 3), -32767, 32767);
222
    else
223
        pred = av_clip(pred - (add >> 3), -32767, 32767);
224
225
    switch (delta) {
226
    case 7:
227
        step *= 0x99;
228
        break;
229
    case 6:
230
        c->step = av_clip(c->step * 2, 127, 24576);
231
        c->predictor = pred;
232
        return pred;
233
    case 5:
234
        step *= 0x66;
235
        break;
236
    case 4:
237
        step *= 0x4d;
238
        break;
239
    default:
240
        step *= 0x39;
241
        break;
242
    }
243
244
    if (step < 0)
245
        step += 0x3f;
246
247
    c->step = step >> 6;
248
    c->step = av_clip(c->step, 127, 24576);
249
    c->predictor = pred;
250
    return pred;
251
}
252
253
9768078
static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
254
{
255
    int step_index;
256
    int predictor;
257
    int sign, delta, diff, step;
258
259
9768078
    step = ff_adpcm_step_table[c->step_index];
260
9768078
    step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
261
9768078
    step_index = av_clip(step_index, 0, 88);
262
263
9768078
    sign = nibble & 8;
264
9768078
    delta = nibble & 7;
265
    /* perform direct multiplication instead of series of jumps proposed by
266
     * the reference ADPCM implementation since modern CPUs can do the mults
267
     * quickly enough */
268
9768078
    diff = ((2 * delta + 1) * step) >> shift;
269
9768078
    predictor = c->predictor;
270
9768078
    if (sign) predictor -= diff;
271
4930668
    else predictor += diff;
272
273
9768078
    c->predictor = av_clip_int16(predictor);
274
9768078
    c->step_index = step_index;
275
276
9768078
    return (int16_t)c->predictor;
277
}
278
279
679344
static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
280
{
281
    int step_index;
282
    int predictor;
283
    int sign, delta, diff, step;
284
285
679344
    step = ff_adpcm_step_table[c->step_index];
286
679344
    step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
287
679344
    step_index = av_clip(step_index, 0, 88);
288
289
679344
    sign = nibble & 8;
290
679344
    delta = nibble & 7;
291
679344
    diff = (delta * step) >> shift;
292
679344
    predictor = c->predictor;
293
679344
    if (sign) predictor -= diff;
294
339502
    else predictor += diff;
295
296
679344
    c->predictor = av_clip_int16(predictor);
297
679344
    c->step_index = step_index;
298
299
679344
    return (int16_t)c->predictor;
300
}
301
302
static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
303
{
304
    int step_index, step, delta, predictor;
305
306
    step = ff_adpcm_step_table[c->step_index];
307
308
    delta = step * (2 * nibble - 15);
309
    predictor = c->predictor + delta;
310
311
    step_index = c->step_index + mtf_index_table[(unsigned)nibble];
312
    c->predictor = av_clip_int16(predictor >> 4);
313
    c->step_index = av_clip(step_index, 0, 88);
314
315
    return (int16_t)c->predictor;
316
}
317
318
386028
static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
319
{
320
    int step_index;
321
    int predictor;
322
    int step;
323
324
386028
    nibble = sign_extend(nibble & 0xF, 4);
325
326
386028
    step = ff_adpcm_ima_cunning_step_table[c->step_index];
327
386028
    step_index = c->step_index + ff_adpcm_ima_cunning_index_table[abs(nibble)];
328
386028
    step_index = av_clip(step_index, 0, 60);
329
330
386028
    predictor = c->predictor + step * nibble;
331
332
386028
    c->predictor = av_clip_int16(predictor);
333
386028
    c->step_index = step_index;
334
335
386028
    return c->predictor;
336
}
337
338
static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
339
{
340
    int nibble, step_index, predictor, sign, delta, diff, step, shift;
341
342
    shift = bps - 1;
343
    nibble = get_bits_le(gb, bps),
344
    step = ff_adpcm_step_table[c->step_index];
345
    step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
346
    step_index = av_clip(step_index, 0, 88);
347
348
    sign = nibble & (1 << shift);
349
    delta = av_mod_uintp2(nibble, shift);
350
    diff = ((2 * delta + 1) * step) >> shift;
351
    predictor = c->predictor;
352
    if (sign) predictor -= diff;
353
    else predictor += diff;
354
355
    c->predictor = av_clip_int16(predictor);
356
    c->step_index = step_index;
357
358
    return (int16_t)c->predictor;
359
}
360
361
4696090
static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
362
{
363
    int step_index;
364
    int predictor;
365
    int diff, step;
366
367
4696090
    step = ff_adpcm_step_table[c->step_index];
368
4696090
    step_index = c->step_index + ff_adpcm_index_table[nibble];
369
4696090
    step_index = av_clip(step_index, 0, 88);
370
371
4696090
    diff = step >> 3;
372
4696090
    if (nibble & 4) diff += step;
373
4696090
    if (nibble & 2) diff += step >> 1;
374
4696090
    if (nibble & 1) diff += step >> 2;
375
376
4696090
    if (nibble & 8)
377
2338238
        predictor = c->predictor - diff;
378
    else
379
2357852
        predictor = c->predictor + diff;
380
381
4696090
    c->predictor = av_clip_int16(predictor);
382
4696090
    c->step_index = step_index;
383
384
4696090
    return c->predictor;
385
}
386
387
2229948
static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
388
{
389
    int predictor;
390
391
2229948
    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
392
2229948
    predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
393
394
2229948
    c->sample2 = c->sample1;
395
2229948
    c->sample1 = av_clip_int16(predictor);
396
2229948
    c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
397
2229948
    if (c->idelta < 16) c->idelta = 16;
398
2229948
    if (c->idelta > INT_MAX/768) {
399
        av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
400
        c->idelta = INT_MAX/768;
401
    }
402
403
2229948
    return c->sample1;
404
}
405
406
55124
static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
407
{
408
    int step_index, predictor, sign, delta, diff, step;
409
410
55124
    step = ff_adpcm_oki_step_table[c->step_index];
411
55124
    step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
412
55124
    step_index = av_clip(step_index, 0, 48);
413
414
55124
    sign = nibble & 8;
415
55124
    delta = nibble & 7;
416
55124
    diff = ((2 * delta + 1) * step) >> 3;
417
55124
    predictor = c->predictor;
418
55124
    if (sign) predictor -= diff;
419
29772
    else predictor += diff;
420
421
55124
    c->predictor = av_clip_intp2(predictor, 11);
422
55124
    c->step_index = step_index;
423
424
55124
    return c->predictor * 16;
425
}
426
427
524192
static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
428
{
429
    int sign, delta, diff;
430
    int new_step;
431
432
524192
    sign = nibble & 8;
433
524192
    delta = nibble & 7;
434
    /* perform direct multiplication instead of series of jumps proposed by
435
     * the reference ADPCM implementation since modern CPUs can do the mults
436
     * quickly enough */
437
524192
    diff = ((2 * delta + 1) * c->step) >> 3;
438
    /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
439
524192
    c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
440
524192
    c->predictor = av_clip_int16(c->predictor);
441
    /* calculate new step and clamp it to range 511..32767 */
442
524192
    new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
443
524192
    c->step = av_clip(new_step, 511, 32767);
444
445
524192
    return (int16_t)c->predictor;
446
}
447
448
313380
static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
449
{
450
    int sign, delta, diff;
451
452
313380
    sign = nibble & (1<<(size-1));
453
313380
    delta = nibble & ((1<<(size-1))-1);
454
313380
    diff = delta << (7 + c->step + shift);
455
456
    /* clamp result */
457
313380
    c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
458
459
    /* calculate new step */
460

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

238300
    else if (delta == 0 && c->step > 0)
463
75080
        c->step--;
464
465
313380
    return (int16_t) c->predictor;
466
}
467
468
1105920
static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
469
{
470
1105920
    if(!c->step) {
471
5
        c->predictor = 0;
472
5
        c->step = 127;
473
    }
474
475
1105920
    c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
476
1105920
    c->predictor = av_clip_int16(c->predictor);
477
1105920
    c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
478
1105920
    c->step = av_clip(c->step, 127, 24576);
479
1105920
    return c->predictor;
480
}
481
482
static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
483
{
484
    c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];
485
    c->predictor = av_clip_int16(c->predictor);
486
    c->step += ff_adpcm_index_table[nibble];
487
    c->step = av_clip_uintp2(c->step, 5);
488
    return c->predictor;
489
}
490
491
static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
492
{
493
    int16_t index = c->step_index;
494
    uint32_t lookup_sample = ff_adpcm_step_table[index];
495
    int32_t sample = 0;
496
497
    if (nibble & 0x40)
498
        sample += lookup_sample;
499
    if (nibble & 0x20)
500
        sample += lookup_sample >> 1;
501
    if (nibble & 0x10)
502
        sample += lookup_sample >> 2;
503
    if (nibble & 0x08)
504
        sample += lookup_sample >> 3;
505
    if (nibble & 0x04)
506
        sample += lookup_sample >> 4;
507
    if (nibble & 0x02)
508
        sample += lookup_sample >> 5;
509
    if (nibble & 0x01)
510
        sample += lookup_sample >> 6;
511
    if (nibble & 0x80)
512
        sample = -sample;
513
514
    sample += c->predictor;
515
    sample = av_clip_int16(sample);
516
517
    index += zork_index_table[(nibble >> 4) & 7];
518
    index = av_clip(index, 0, 88);
519
520
    c->predictor = sample;
521
    c->step_index = index;
522
523
    return sample;
524
}
525
526
666
static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
527
                     const uint8_t *in, ADPCMChannelStatus *left,
528
                     ADPCMChannelStatus *right, int channels, int sample_offset)
529
{
530
    int i, j;
531
    int shift,filter,f0,f1;
532
    int s_1,s_2;
533
    int d,s,t;
534
535
666
    out0 += sample_offset;
536
666
    if (channels == 1)
537
        out1 = out0 + 28;
538
    else
539
666
        out1 += sample_offset;
540
541
3330
    for(i=0;i<4;i++) {
542
2664
        shift  = 12 - (in[4+i*2] & 15);
543
2664
        filter = in[4+i*2] >> 4;
544
2664
        if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
545
            avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
546
            filter=0;
547
        }
548
2664
        if (shift < 0) {
549
            avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
550
            shift = 0;
551
        }
552
2664
        f0 = xa_adpcm_table[filter][0];
553
2664
        f1 = xa_adpcm_table[filter][1];
554
555
2664
        s_1 = left->sample1;
556
2664
        s_2 = left->sample2;
557
558
77256
        for(j=0;j<28;j++) {
559
74592
            d = in[16+i+j*4];
560
561
74592
            t = sign_extend(d, 4);
562
74592
            s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
563
74592
            s_2 = s_1;
564
74592
            s_1 = av_clip_int16(s);
565
74592
            out0[j] = s_1;
566
        }
567
568
2664
        if (channels == 2) {
569
2664
            left->sample1 = s_1;
570
2664
            left->sample2 = s_2;
571
2664
            s_1 = right->sample1;
572
2664
            s_2 = right->sample2;
573
        }
574
575
2664
        shift  = 12 - (in[5+i*2] & 15);
576
2664
        filter = in[5+i*2] >> 4;
577

2664
        if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
578
            avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
579
            filter=0;
580
        }
581
2664
        if (shift < 0) {
582
            avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
583
            shift = 0;
584
        }
585
586
2664
        f0 = xa_adpcm_table[filter][0];
587
2664
        f1 = xa_adpcm_table[filter][1];
588
589
77256
        for(j=0;j<28;j++) {
590
74592
            d = in[16+i+j*4];
591
592
74592
            t = sign_extend(d >> 4, 4);
593
74592
            s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
594
74592
            s_2 = s_1;
595
74592
            s_1 = av_clip_int16(s);
596
74592
            out1[j] = s_1;
597
        }
598
599
2664
        if (channels == 2) {
600
2664
            right->sample1 = s_1;
601
2664
            right->sample2 = s_2;
602
        } else {
603
            left->sample1 = s_1;
604
            left->sample2 = s_2;
605
        }
606
607
2664
        out0 += 28 * (3 - channels);
608
2664
        out1 += 28 * (3 - channels);
609
    }
610
611
666
    return 0;
612
}
613
614
195
static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
615
{
616
195
    ADPCMDecodeContext *c = avctx->priv_data;
617
    GetBitContext gb;
618
    const int8_t *table;
619
    int k0, signmask, nb_bits, count;
620
195
    int size = buf_size*8;
621
    int i;
622
623
195
    init_get_bits(&gb, buf, size);
624
625
    //read bits & initial values
626
195
    nb_bits = get_bits(&gb, 2)+2;
627
195
    table = swf_index_tables[nb_bits-2];
628
195
    k0 = 1 << (nb_bits-2);
629
195
    signmask = 1 << (nb_bits-1);
630
631
390
    while (get_bits_count(&gb) <= size - 22*avctx->channels) {
632
585
        for (i = 0; i < avctx->channels; i++) {
633
390
            *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
634
390
            c->status[i].step_index = get_bits(&gb, 6);
635
        }
636
637

798720
        for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
638
            int i;
639
640
2395575
            for (i = 0; i < avctx->channels; i++) {
641
                // similar to IMA adpcm
642
1597050
                int delta = get_bits(&gb, nb_bits);
643
1597050
                int step = ff_adpcm_step_table[c->status[i].step_index];
644
1597050
                int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
645
1597050
                int k = k0;
646
647
                do {
648
4791150
                    if (delta & k)
649
1979561
                        vpdiff += step;
650
4791150
                    step >>= 1;
651
4791150
                    k >>= 1;
652
4791150
                } while(k);
653
1597050
                vpdiff += step;
654
655
1597050
                if (delta & signmask)
656
803527
                    c->status[i].predictor -= vpdiff;
657
                else
658
793523
                    c->status[i].predictor += vpdiff;
659
660
1597050
                c->status[i].step_index += table[delta & (~signmask)];
661
662
1597050
                c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
663
1597050
                c->status[i].predictor = av_clip_int16(c->status[i].predictor);
664
665
1597050
                *samples++ = c->status[i].predictor;
666
            }
667
        }
668
    }
669
195
}
670
671
18335520
int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
672
{
673
18335520
    int sample = sign_extend(nibble, 4) * (1 << shift);
674
675
18335520
    if (flag)
676
9806208
        sample += (8 * cs->sample1) - (4 * cs->sample2);
677
    else
678
8529312
        sample += 4 * cs->sample1;
679
680
18335520
    sample = av_clip_int16(sample >> 2);
681
682
18335520
    cs->sample2 = cs->sample1;
683
18335520
    cs->sample1 = sample;
684
685
18335520
    return sample;
686
}
687
688
/**
689
 * Get the number of samples (per channel) that will be decoded from the packet.
690
 * In one case, this is actually the maximum number of samples possible to
691
 * decode with the given buf_size.
692
 *
693
 * @param[out] coded_samples set to the number of samples as coded in the
694
 *                           packet, or 0 if the codec does not encode the
695
 *                           number of samples in each frame.
696
 * @param[out] approx_nb_samples set to non-zero if the number of samples
697
 *                               returned is an approximation.
698
 */
699
37298
static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
700
                          int buf_size, int *coded_samples, int *approx_nb_samples)
701
{
702
37298
    ADPCMDecodeContext *s = avctx->priv_data;
703
37298
    int nb_samples        = 0;
704
37298
    int ch                = avctx->channels;
705
37298
    int has_coded_samples = 0;
706
    int header_size;
707
708
37298
    *coded_samples = 0;
709
37298
    *approx_nb_samples = 0;
710
711
37298
    if(ch <= 0)
712
        return 0;
713
714

37298
    switch (avctx->codec->id) {
715
    /* constant, only check buf_size */
716
600
    case AV_CODEC_ID_ADPCM_EA_XAS:
717
600
        if (buf_size < 76 * ch)
718
            return 0;
719
600
        nb_samples = 128;
720
600
        break;
721
26342
    case AV_CODEC_ID_ADPCM_IMA_QT:
722
26342
        if (buf_size < 34 * ch)
723
            return 0;
724
26342
        nb_samples = 64;
725
26342
        break;
726
    /* simple 4-bit adpcm */
727
813
    case AV_CODEC_ID_ADPCM_CT:
728
    case AV_CODEC_ID_ADPCM_IMA_APC:
729
    case AV_CODEC_ID_ADPCM_IMA_CUNNING:
730
    case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
731
    case AV_CODEC_ID_ADPCM_IMA_OKI:
732
    case AV_CODEC_ID_ADPCM_IMA_WS:
733
    case AV_CODEC_ID_ADPCM_YAMAHA:
734
    case AV_CODEC_ID_ADPCM_AICA:
735
    case AV_CODEC_ID_ADPCM_IMA_SSI:
736
    case AV_CODEC_ID_ADPCM_IMA_APM:
737
    case AV_CODEC_ID_ADPCM_IMA_ALP:
738
    case AV_CODEC_ID_ADPCM_IMA_MTF:
739
813
        nb_samples = buf_size * 2 / ch;
740
813
        break;
741
    }
742
37298
    if (nb_samples)
743
27755
        return nb_samples;
744
745
    /* simple 4-bit adpcm, with header */
746
9543
    header_size = 0;
747
9543
    switch (avctx->codec->id) {
748
145
        case AV_CODEC_ID_ADPCM_4XM:
749
        case AV_CODEC_ID_ADPCM_AGM:
750
        case AV_CODEC_ID_ADPCM_IMA_DAT4:
751
        case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
752
145
        case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
753
350
        case AV_CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4 * ch;      break;
754
    }
755
9543
    if (header_size > 0)
756
495
        return (buf_size - header_size) * 2 / ch;
757
758
    /* more complex formats */
759





9048
    switch (avctx->codec->id) {
760
161
    case AV_CODEC_ID_ADPCM_IMA_AMV:
761
161
        bytestream2_skip(gb, 4);
762
161
        has_coded_samples  = 1;
763
161
        *coded_samples     = bytestream2_get_le32u(gb);
764
161
        nb_samples         = FFMIN((buf_size - 8) * 2, *coded_samples);
765
161
        bytestream2_seek(gb, -8, SEEK_CUR);
766
161
        break;
767
158
    case AV_CODEC_ID_ADPCM_EA:
768
158
        has_coded_samples = 1;
769
158
        *coded_samples  = bytestream2_get_le32(gb);
770
158
        *coded_samples -= *coded_samples % 28;
771
158
        nb_samples      = (buf_size - 12) / 30 * 28;
772
158
        break;
773
47
    case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
774
47
        has_coded_samples = 1;
775
47
        *coded_samples = bytestream2_get_le32(gb);
776
47
        nb_samples     = (buf_size - (4 + 8 * ch)) * 2 / ch;
777
47
        break;
778
30
    case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
779
30
        nb_samples = (buf_size - ch) / ch * 2;
780
30
        break;
781
400
    case AV_CODEC_ID_ADPCM_EA_R1:
782
    case AV_CODEC_ID_ADPCM_EA_R2:
783
    case AV_CODEC_ID_ADPCM_EA_R3:
784
        /* maximum number of samples */
785
        /* has internal offsets and a per-frame switch to signal raw 16-bit */
786
400
        has_coded_samples = 1;
787

400
        switch (avctx->codec->id) {
788
95
        case AV_CODEC_ID_ADPCM_EA_R1:
789
95
            header_size    = 4 + 9 * ch;
790
95
            *coded_samples = bytestream2_get_le32(gb);
791
95
            break;
792
180
        case AV_CODEC_ID_ADPCM_EA_R2:
793
180
            header_size    = 4 + 5 * ch;
794
180
            *coded_samples = bytestream2_get_le32(gb);
795
180
            break;
796
125
        case AV_CODEC_ID_ADPCM_EA_R3:
797
125
            header_size    = 4 + 5 * ch;
798
125
            *coded_samples = bytestream2_get_be32(gb);
799
125
            break;
800
        }
801
400
        *coded_samples -= *coded_samples % 28;
802
400
        nb_samples      = (buf_size - header_size) * 2 / ch;
803
400
        nb_samples     -= nb_samples % 28;
804
400
        *approx_nb_samples = 1;
805
400
        break;
806
642
    case AV_CODEC_ID_ADPCM_IMA_DK3:
807
642
        if (avctx->block_align > 0)
808
642
            buf_size = FFMIN(buf_size, avctx->block_align);
809
642
        nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
810
642
        break;
811
649
    case AV_CODEC_ID_ADPCM_IMA_DK4:
812
649
        if (avctx->block_align > 0)
813
649
            buf_size = FFMIN(buf_size, avctx->block_align);
814
649
        if (buf_size < 4 * ch)
815
            return AVERROR_INVALIDDATA;
816
649
        nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
817
649
        break;
818
1000
    case AV_CODEC_ID_ADPCM_IMA_RAD:
819
1000
        if (avctx->block_align > 0)
820
1000
            buf_size = FFMIN(buf_size, avctx->block_align);
821
1000
        nb_samples = (buf_size - 4 * ch) * 2 / ch;
822
1000
        break;
823
1668
    case AV_CODEC_ID_ADPCM_IMA_WAV:
824
    {
825
1668
        int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
826
1668
        int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
827
1668
        if (avctx->block_align > 0)
828
1668
            buf_size = FFMIN(buf_size, avctx->block_align);
829
1668
        if (buf_size < 4 * ch)
830
            return AVERROR_INVALIDDATA;
831
1668
        nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
832
1668
        break;
833
    }
834
2943
    case AV_CODEC_ID_ADPCM_MS:
835
2943
        if (avctx->block_align > 0)
836
2943
            buf_size = FFMIN(buf_size, avctx->block_align);
837
2943
        nb_samples = (buf_size - 6 * ch) * 2 / ch;
838
2943
        break;
839
    case AV_CODEC_ID_ADPCM_MTAF:
840
        if (avctx->block_align > 0)
841
            buf_size = FFMIN(buf_size, avctx->block_align);
842
        nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
843
        break;
844
57
    case AV_CODEC_ID_ADPCM_SBPRO_2:
845
    case AV_CODEC_ID_ADPCM_SBPRO_3:
846
    case AV_CODEC_ID_ADPCM_SBPRO_4:
847
    {
848
        int samples_per_byte;
849

57
        switch (avctx->codec->id) {
850
13
        case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
851
18
        case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
852
26
        case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
853
        }
854
57
        if (!s->status[0].step_index) {
855
3
            if (buf_size < ch)
856
                return AVERROR_INVALIDDATA;
857
3
            nb_samples++;
858
3
            buf_size -= ch;
859
        }
860
57
        nb_samples += buf_size * samples_per_byte / ch;
861
57
        break;
862
    }
863
195
    case AV_CODEC_ID_ADPCM_SWF:
864
    {
865
195
        int buf_bits       = buf_size * 8 - 2;
866
195
        int nbits          = (bytestream2_get_byte(gb) >> 6) + 2;
867
195
        int block_hdr_size = 22 * ch;
868
195
        int block_size     = block_hdr_size + nbits * ch * 4095;
869
195
        int nblocks        = buf_bits / block_size;
870
195
        int bits_left      = buf_bits - nblocks * block_size;
871
195
        nb_samples         = nblocks * 4096;
872
195
        if (bits_left >= block_hdr_size)
873
            nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
874
195
        break;
875
    }
876
71
    case AV_CODEC_ID_ADPCM_THP:
877
    case AV_CODEC_ID_ADPCM_THP_LE:
878
71
        if (avctx->extradata) {
879
            nb_samples = buf_size * 14 / (8 * ch);
880
            break;
881
        }
882
71
        has_coded_samples = 1;
883
71
        bytestream2_skip(gb, 4); // channel size
884
142
        *coded_samples  = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
885
142
                          bytestream2_get_le32(gb) :
886
71
                          bytestream2_get_be32(gb);
887
71
        buf_size       -= 8 + 36 * ch;
888
71
        buf_size       /= ch;
889
71
        nb_samples      = buf_size / 8 * 14;
890
71
        if (buf_size % 8 > 1)
891
            nb_samples     += (buf_size % 8 - 1) * 2;
892
71
        *approx_nb_samples = 1;
893
71
        break;
894
12
    case AV_CODEC_ID_ADPCM_AFC:
895
12
        nb_samples = buf_size / (9 * ch) * 16;
896
12
        break;
897
37
    case AV_CODEC_ID_ADPCM_XA:
898
37
        nb_samples = (buf_size / 128) * 224 / ch;
899
37
        break;
900
32
    case AV_CODEC_ID_ADPCM_DTK:
901
    case AV_CODEC_ID_ADPCM_PSX:
902
32
        nb_samples = buf_size / (16 * ch) * 28;
903
32
        break;
904
946
    case AV_CODEC_ID_ADPCM_ARGO:
905
946
        nb_samples = buf_size / avctx->block_align * 32;
906
946
        break;
907
    case AV_CODEC_ID_ADPCM_ZORK:
908
        nb_samples = buf_size / ch;
909
        break;
910
    }
911
912
    /* validate coded sample count */
913

9048
    if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
914
        return AVERROR_INVALIDDATA;
915
916
9048
    return nb_samples;
917
}
918
919
37298
static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
920
                              int *got_frame_ptr, AVPacket *avpkt)
921
{
922
37298
    AVFrame *frame     = data;
923
37298
    const uint8_t *buf = avpkt->data;
924
37298
    int buf_size = avpkt->size;
925
37298
    ADPCMDecodeContext *c = avctx->priv_data;
926
    ADPCMChannelStatus *cs;
927
    int n, m, channel, i;
928
    int16_t *samples;
929
    int16_t **samples_p;
930
    int st; /* stereo */
931
    int count1, count2;
932
    int nb_samples, coded_samples, approx_nb_samples, ret;
933
    GetByteContext gb;
934
935
37298
    bytestream2_init(&gb, buf, buf_size);
936
37298
    nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
937
37298
    if (nb_samples <= 0) {
938
        av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
939
        return AVERROR_INVALIDDATA;
940
    }
941
942
    /* get output buffer */
943
37298
    frame->nb_samples = nb_samples;
944
37298
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
945
        return ret;
946
37298
    samples = (int16_t *)frame->data[0];
947
37298
    samples_p = (int16_t **)frame->extended_data;
948
949
    /* use coded_samples when applicable */
950
    /* it is always <= nb_samples, so the output buffer will be large enough */
951
37298
    if (coded_samples) {
952

837
        if (!approx_nb_samples && coded_samples != nb_samples)
953
            av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
954
837
        frame->nb_samples = nb_samples = coded_samples;
955
    }
956
957
37298
    st = avctx->channels == 2 ? 1 : 0;
958
959










37298
    switch(avctx->codec->id) {
960
26342
    case AV_CODEC_ID_ADPCM_IMA_QT:
961
        /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
962
           Channel data is interleaved per-chunk. */
963
69990
        for (channel = 0; channel < avctx->channels; channel++) {
964
            int predictor;
965
            int step_index;
966
43648
            cs = &(c->status[channel]);
967
            /* (pppppp) (piiiiiii) */
968
969
            /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
970
43648
            predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
971
43648
            step_index = predictor & 0x7F;
972
43648
            predictor &= ~0x7F;
973
974
43648
            if (cs->step_index == step_index) {
975
43648
                int diff = predictor - cs->predictor;
976
43648
                if (diff < 0)
977
39464
                    diff = - diff;
978
43648
                if (diff > 0x7f)
979
30
                    goto update;
980
            } else {
981
            update:
982
30
                cs->step_index = step_index;
983
30
                cs->predictor = predictor;
984
            }
985
986
43648
            if (cs->step_index > 88u){
987
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
988
                       channel, cs->step_index);
989
                return AVERROR_INVALIDDATA;
990
            }
991
992
43648
            samples = samples_p[channel];
993
994
1440384
            for (m = 0; m < 64; m += 2) {
995
1396736
                int byte = bytestream2_get_byteu(&gb);
996
1396736
                samples[m    ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
997
1396736
                samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4  );
998
            }
999
        }
1000
26342
        break;
1001
1668
    case AV_CODEC_ID_ADPCM_IMA_WAV:
1002
5004
        for(i=0; i<avctx->channels; i++){
1003
3336
            cs = &(c->status[i]);
1004
3336
            cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1005
1006
3336
            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1007
3336
            if (cs->step_index > 88u){
1008
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1009
                       i, cs->step_index);
1010
                return AVERROR_INVALIDDATA;
1011
            }
1012
        }
1013
1014
1668
        if (avctx->bits_per_coded_sample != 4) {
1015
            int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1016
            int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1017
            uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1018
            GetBitContext g;
1019
1020
            for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1021
                for (i = 0; i < avctx->channels; i++) {
1022
                    int j;
1023
1024
                    cs = &c->status[i];
1025
                    samples = &samples_p[i][1 + n * samples_per_block];
1026
                    for (j = 0; j < block_size; j++) {
1027
                        temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
1028
                                        (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
1029
                    }
1030
                    ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1031
                    if (ret < 0)
1032
                        return ret;
1033
                    for (m = 0; m < samples_per_block; m++) {
1034
                        samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
1035
                                          avctx->bits_per_coded_sample);
1036
                    }
1037
                }
1038
            }
1039
            bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
1040
        } else {
1041
140160
        for (n = 0; n < (nb_samples - 1) / 8; n++) {
1042
415476
            for (i = 0; i < avctx->channels; i++) {
1043
276984
                cs = &c->status[i];
1044
276984
                samples = &samples_p[i][1 + n * 8];
1045
1384920
                for (m = 0; m < 8; m += 2) {
1046
1107936
                    int v = bytestream2_get_byteu(&gb);
1047
1107936
                    samples[m    ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1048
1107936
                    samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
1049
                }
1050
            }
1051
        }
1052
        }
1053
1668
        break;
1054
26
    case AV_CODEC_ID_ADPCM_4XM:
1055
78
        for (i = 0; i < avctx->channels; i++)
1056
52
            c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1057
1058
78
        for (i = 0; i < avctx->channels; i++) {
1059
52
            c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1060
52
            if (c->status[i].step_index > 88u) {
1061
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1062
                       i, c->status[i].step_index);
1063
                return AVERROR_INVALIDDATA;
1064
            }
1065
        }
1066
1067
78
        for (i = 0; i < avctx->channels; i++) {
1068
52
            samples = (int16_t *)frame->data[i];
1069
52
            cs = &c->status[i];
1070
38276
            for (n = nb_samples >> 1; n > 0; n--) {
1071
38224
                int v = bytestream2_get_byteu(&gb);
1072
38224
                *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1073
38224
                *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 4);
1074
            }
1075
        }
1076
26
        break;
1077
    case AV_CODEC_ID_ADPCM_AGM:
1078
        for (i = 0; i < avctx->channels; i++)
1079
            c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1080
        for (i = 0; i < avctx->channels; i++)
1081
            c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1082
1083
        for (n = 0; n < nb_samples >> (1 - st); n++) {
1084
            int v = bytestream2_get_byteu(&gb);
1085
            *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1086
            *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1087
        }
1088
        break;
1089
2943
    case AV_CODEC_ID_ADPCM_MS:
1090
    {
1091
        int block_predictor;
1092
1093
2943
        if (avctx->channels > 2) {
1094
            for (channel = 0; channel < avctx->channels; channel++) {
1095
                samples = samples_p[channel];
1096
                block_predictor = bytestream2_get_byteu(&gb);
1097
                if (block_predictor > 6) {
1098
                    av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1099
                           channel, block_predictor);
1100
                    return AVERROR_INVALIDDATA;
1101
                }
1102
                c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1103
                c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1104
                c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1105
                c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1106
                c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1107
                *samples++ = c->status[channel].sample2;
1108
                *samples++ = c->status[channel].sample1;
1109
                for(n = (nb_samples - 2) >> 1; n > 0; n--) {
1110
                    int byte = bytestream2_get_byteu(&gb);
1111
                    *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4  );
1112
                    *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1113
                }
1114
            }
1115
        } else {
1116
2943
            block_predictor = bytestream2_get_byteu(&gb);
1117
2943
            if (block_predictor > 6) {
1118
                av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1119
                       block_predictor);
1120
                return AVERROR_INVALIDDATA;
1121
            }
1122
2943
            c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1123
2943
            c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1124
2943
            if (st) {
1125
2895
                block_predictor = bytestream2_get_byteu(&gb);
1126
2895
                if (block_predictor > 6) {
1127
                    av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1128
                           block_predictor);
1129
                    return AVERROR_INVALIDDATA;
1130
                }
1131
2895
                c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1132
2895
                c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1133
            }
1134
2943
            c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1135
2943
            if (st){
1136
2895
                c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1137
            }
1138
1139
2943
            c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1140
2943
            if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1141
2943
            c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1142
2943
            if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1143
1144
2943
            *samples++ = c->status[0].sample2;
1145
2943
            if (st) *samples++ = c->status[1].sample2;
1146
2943
            *samples++ = c->status[0].sample1;
1147
2943
            if (st) *samples++ = c->status[1].sample1;
1148
1117917
            for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1149
1114974
                int byte = bytestream2_get_byteu(&gb);
1150
1114974
                *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4  );
1151
1114974
                *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1152
            }
1153
        }
1154
2943
        break;
1155
    }
1156
    case AV_CODEC_ID_ADPCM_MTAF:
1157
        for (channel = 0; channel < avctx->channels; channel+=2) {
1158
            bytestream2_skipu(&gb, 4);
1159
            c->status[channel    ].step      = bytestream2_get_le16u(&gb) & 0x1f;
1160
            c->status[channel + 1].step      = bytestream2_get_le16u(&gb) & 0x1f;
1161
            c->status[channel    ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1162
            bytestream2_skipu(&gb, 2);
1163
            c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1164
            bytestream2_skipu(&gb, 2);
1165
            for (n = 0; n < nb_samples; n+=2) {
1166
                int v = bytestream2_get_byteu(&gb);
1167
                samples_p[channel][n    ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1168
                samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4  );
1169
            }
1170
            for (n = 0; n < nb_samples; n+=2) {
1171
                int v = bytestream2_get_byteu(&gb);
1172
                samples_p[channel + 1][n    ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1173
                samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4  );
1174
            }
1175
        }
1176
        break;
1177
649
    case AV_CODEC_ID_ADPCM_IMA_DK4:
1178
1947
        for (channel = 0; channel < avctx->channels; channel++) {
1179
1298
            cs = &c->status[channel];
1180
1298
            cs->predictor  = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1181
1298
            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1182
1298
            if (cs->step_index > 88u){
1183
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1184
                       channel, cs->step_index);
1185
                return AVERROR_INVALIDDATA;
1186
            }
1187
        }
1188
1324609
        for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1189
1323960
            int v = bytestream2_get_byteu(&gb);
1190
1323960
            *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4  , 3);
1191
1323960
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1192
        }
1193
649
        break;
1194
642
    case AV_CODEC_ID_ADPCM_IMA_DK3:
1195
    {
1196
642
        int last_byte = 0;
1197
        int nibble;
1198
642
        int decode_top_nibble_next = 0;
1199
        int diff_channel;
1200
642
        const int16_t *samples_end = samples + avctx->channels * nb_samples;
1201
1202
642
        bytestream2_skipu(&gb, 10);
1203
642
        c->status[0].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1204
642
        c->status[1].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1205
642
        c->status[0].step_index = bytestream2_get_byteu(&gb);
1206
642
        c->status[1].step_index = bytestream2_get_byteu(&gb);
1207

642
        if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1208
            av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1209
                   c->status[0].step_index, c->status[1].step_index);
1210
            return AVERROR_INVALIDDATA;
1211
        }
1212
        /* sign extend the predictors */
1213
642
        diff_channel = c->status[1].predictor;
1214
1215
        /* DK3 ADPCM support macro */
1216
#define DK3_GET_NEXT_NIBBLE() \
1217
    if (decode_top_nibble_next) { \
1218
        nibble = last_byte >> 4; \
1219
        decode_top_nibble_next = 0; \
1220
    } else { \
1221
        last_byte = bytestream2_get_byteu(&gb); \
1222
        nibble = last_byte & 0x0F; \
1223
        decode_top_nibble_next = 1; \
1224
    }
1225
1226
869910
        while (samples < samples_end) {
1227
1228
            /* for this algorithm, c->status[0] is the sum channel and
1229
             * c->status[1] is the diff channel */
1230
1231
            /* process the first predictor of the sum channel */
1232
869268
            DK3_GET_NEXT_NIBBLE();
1233
869268
            adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1234
1235
            /* process the diff channel predictor */
1236
869268
            DK3_GET_NEXT_NIBBLE();
1237
869268
            adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1238
1239
            /* process the first pair of stereo PCM samples */
1240
869268
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
1241
869268
            *samples++ = c->status[0].predictor + c->status[1].predictor;
1242
869268
            *samples++ = c->status[0].predictor - c->status[1].predictor;
1243
1244
            /* process the second predictor of the sum channel */
1245
869268
            DK3_GET_NEXT_NIBBLE();
1246
869268
            adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1247
1248
            /* process the second pair of stereo PCM samples */
1249
869268
            diff_channel = (diff_channel + c->status[1].predictor) / 2;
1250
869268
            *samples++ = c->status[0].predictor + c->status[1].predictor;
1251
869268
            *samples++ = c->status[0].predictor - c->status[1].predictor;
1252
        }
1253
1254
642
        if ((bytestream2_tell(&gb) & 1))
1255
642
            bytestream2_skip(&gb, 1);
1256
642
        break;
1257
    }
1258
119
    case AV_CODEC_ID_ADPCM_IMA_ISS:
1259
238
        for (channel = 0; channel < avctx->channels; channel++) {
1260
119
            cs = &c->status[channel];
1261
119
            cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1262
119
            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1263
119
            if (cs->step_index > 88u){
1264
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1265
                       channel, cs->step_index);
1266
                return AVERROR_INVALIDDATA;
1267
            }
1268
        }
1269
1270
60571
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1271
            int v1, v2;
1272
60452
            int v = bytestream2_get_byteu(&gb);
1273
            /* nibbles are swapped for mono */
1274
60452
            if (st) {
1275
                v1 = v >> 4;
1276
                v2 = v & 0x0F;
1277
            } else {
1278
60452
                v2 = v >> 4;
1279
60452
                v1 = v & 0x0F;
1280
            }
1281
60452
            *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1282
60452
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1283
        }
1284
119
        break;
1285
    case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
1286
        for (channel = 0; channel < avctx->channels; channel++) {
1287
            cs = &c->status[channel];
1288
            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1289
            cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1290
            if (cs->step_index > 88u){
1291
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1292
                       channel, cs->step_index);
1293
                return AVERROR_INVALIDDATA;
1294
            }
1295
        }
1296
1297
        for (int subframe = 0; subframe < nb_samples / 256; subframe++) {
1298
            for (channel = 0; channel < avctx->channels; channel++) {
1299
                samples = samples_p[channel] + 256 * subframe;
1300
                for (n = 0; n < 256; n += 2) {
1301
                    int v = bytestream2_get_byteu(&gb);
1302
                    *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1303
                    *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1304
                }
1305
            }
1306
        }
1307
        break;
1308
    case AV_CODEC_ID_ADPCM_IMA_DAT4:
1309
        for (channel = 0; channel < avctx->channels; channel++) {
1310
            cs = &c->status[channel];
1311
            samples = samples_p[channel];
1312
            bytestream2_skip(&gb, 4);
1313
            for (n = 0; n < nb_samples; n += 2) {
1314
                int v = bytestream2_get_byteu(&gb);
1315
                *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
1316
                *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1317
            }
1318
        }
1319
        break;
1320
179
    case AV_CODEC_ID_ADPCM_IMA_APC:
1321
732239
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1322
732060
            int v = bytestream2_get_byteu(&gb);
1323
732060
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  v >> 4  , 3);
1324
732060
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1325
        }
1326
179
        break;
1327
127
    case AV_CODEC_ID_ADPCM_IMA_SSI:
1328
514727
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1329
514600
            int v = bytestream2_get_byteu(&gb);
1330
514600
            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0],  v >> 4  );
1331
514600
            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1332
        }
1333
127
        break;
1334
87
    case AV_CODEC_ID_ADPCM_IMA_APM:
1335
181917
        for (n = nb_samples / 2; n > 0; n--) {
1336
528939
            for (channel = 0; channel < avctx->channels; channel++) {
1337
347109
                int v = bytestream2_get_byteu(&gb);
1338
347109
                *samples++  = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4  );
1339
347109
                samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1340
            }
1341
181830
            samples += avctx->channels;
1342
        }
1343
87
        break;
1344
85
    case AV_CODEC_ID_ADPCM_IMA_ALP:
1345
172457
        for (n = nb_samples / 2; n > 0; n--) {
1346
512044
            for (channel = 0; channel < avctx->channels; channel++) {
1347
339672
                int v = bytestream2_get_byteu(&gb);
1348
339672
                *samples++  = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4  , 2);
1349
339672
                samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1350
            }
1351
172372
            samples += avctx->channels;
1352
        }
1353
85
        break;
1354
39
    case AV_CODEC_ID_ADPCM_IMA_CUNNING:
1355
90
        for (channel = 0; channel < avctx->channels; channel++) {
1356
51
            int16_t *smp = samples_p[channel];
1357
193065
            for (n = 0; n < nb_samples / 2; n++) {
1358
193014
                int v = bytestream2_get_byteu(&gb);
1359
193014
                *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v & 0x0F);
1360
193014
                *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v >> 4);
1361
            }
1362
        }
1363
39
        break;
1364
7
    case AV_CODEC_ID_ADPCM_IMA_OKI:
1365
27569
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1366
27562
            int v = bytestream2_get_byteu(&gb);
1367
27562
            *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0],  v >> 4  );
1368
27562
            *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1369
        }
1370
7
        break;
1371
1000
    case AV_CODEC_ID_ADPCM_IMA_RAD:
1372
3000
        for (channel = 0; channel < avctx->channels; channel++) {
1373
2000
            cs = &c->status[channel];
1374
2000
            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1375
2000
            cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);
1376
2000
            if (cs->step_index > 88u){
1377
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1378
                       channel, cs->step_index);
1379
                return AVERROR_INVALIDDATA;
1380
            }
1381
        }
1382
17000
        for (n = 0; n < nb_samples / 2; n++) {
1383
            int byte[2];
1384
1385
16000
            byte[0] = bytestream2_get_byteu(&gb);
1386
16000
            if (st)
1387
16000
                byte[1] = bytestream2_get_byteu(&gb);
1388
48000
            for(channel = 0; channel < avctx->channels; channel++) {
1389
32000
                *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1390
            }
1391
48000
            for(channel = 0; channel < avctx->channels; channel++) {
1392
32000
                *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4  , 3);
1393
            }
1394
        }
1395
1000
        break;
1396
40
    case AV_CODEC_ID_ADPCM_IMA_WS:
1397
40
        if (c->vqa_version == 3) {
1398
            for (channel = 0; channel < avctx->channels; channel++) {
1399
                int16_t *smp = samples_p[channel];
1400
1401
                for (n = nb_samples / 2; n > 0; n--) {
1402
                    int v = bytestream2_get_byteu(&gb);
1403
                    *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1404
                    *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1405
                }
1406
            }
1407
        } else {
1408
34217
            for (n = nb_samples / 2; n > 0; n--) {
1409
68354
                for (channel = 0; channel < avctx->channels; channel++) {
1410
34177
                    int v = bytestream2_get_byteu(&gb);
1411
34177
                    *samples++  = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);
1412
34177
                    samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1413
                }
1414
34177
                samples += avctx->channels;
1415
            }
1416
        }
1417
40
        bytestream2_seek(&gb, 0, SEEK_END);
1418
40
        break;
1419
37
    case AV_CODEC_ID_ADPCM_XA:
1420
    {
1421
37
        int16_t *out0 = samples_p[0];
1422
37
        int16_t *out1 = samples_p[1];
1423
37
        int samples_per_block = 28 * (3 - avctx->channels) * 4;
1424
37
        int sample_offset = 0;
1425
        int bytes_remaining;
1426
703
        while (bytestream2_get_bytes_left(&gb) >= 128) {
1427
666
            if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1428
                                 &c->status[0], &c->status[1],
1429
                                 avctx->channels, sample_offset)) < 0)
1430
                return ret;
1431
666
            bytestream2_skipu(&gb, 128);
1432
666
            sample_offset += samples_per_block;
1433
        }
1434
        /* Less than a full block of data left, e.g. when reading from
1435
         * 2324 byte per sector XA; the remainder is padding */
1436
37
        bytes_remaining = bytestream2_get_bytes_left(&gb);
1437
37
        if (bytes_remaining > 0) {
1438
            bytestream2_skip(&gb, bytes_remaining);
1439
        }
1440
37
        break;
1441
    }
1442
47
    case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
1443
141
        for (i=0; i<=st; i++) {
1444
94
            c->status[i].step_index = bytestream2_get_le32u(&gb);
1445
94
            if (c->status[i].step_index > 88u) {
1446
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1447
                       i, c->status[i].step_index);
1448
                return AVERROR_INVALIDDATA;
1449
            }
1450
        }
1451
141
        for (i=0; i<=st; i++) {
1452
94
            c->status[i].predictor  = bytestream2_get_le32u(&gb);
1453
94
            if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
1454
                return AVERROR_INVALIDDATA;
1455
        }
1456
1457
69043
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1458
68996
            int byte   = bytestream2_get_byteu(&gb);
1459
68996
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   3);
1460
68996
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1461
        }
1462
47
        break;
1463
49
    case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1464
71441
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1465
71392
            int byte = bytestream2_get_byteu(&gb);
1466
71392
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   6);
1467
71392
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1468
        }
1469
49
        break;
1470
158
    case AV_CODEC_ID_ADPCM_EA:
1471
    {
1472
        int previous_left_sample, previous_right_sample;
1473
        int current_left_sample, current_right_sample;
1474
        int next_left_sample, next_right_sample;
1475
        int coeff1l, coeff2l, coeff1r, coeff2r;
1476
        int shift_left, shift_right;
1477
1478
        /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1479
           each coding 28 stereo samples. */
1480
1481
158
        if(avctx->channels != 2)
1482
            return AVERROR_INVALIDDATA;
1483
1484
158
        current_left_sample   = sign_extend(bytestream2_get_le16u(&gb), 16);
1485
158
        previous_left_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
1486
158
        current_right_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
1487
158
        previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1488
1489
8454
        for (count1 = 0; count1 < nb_samples / 28; count1++) {
1490
8296
            int byte = bytestream2_get_byteu(&gb);
1491
8296
            coeff1l = ea_adpcm_table[ byte >> 4       ];
1492
8296
            coeff2l = ea_adpcm_table[(byte >> 4  ) + 4];
1493
8296
            coeff1r = ea_adpcm_table[ byte & 0x0F];
1494
8296
            coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1495
1496
8296
            byte = bytestream2_get_byteu(&gb);
1497
8296
            shift_left  = 20 - (byte >> 4);
1498
8296
            shift_right = 20 - (byte & 0x0F);
1499
1500
240584
            for (count2 = 0; count2 < 28; count2++) {
1501
232288
                byte = bytestream2_get_byteu(&gb);
1502
232288
                next_left_sample  = sign_extend(byte >> 4, 4) * (1 << shift_left);
1503
232288
                next_right_sample = sign_extend(byte,      4) * (1 << shift_right);
1504
1505
232288
                next_left_sample = (next_left_sample +
1506
232288
                    (current_left_sample * coeff1l) +
1507
232288
                    (previous_left_sample * coeff2l) + 0x80) >> 8;
1508
232288
                next_right_sample = (next_right_sample +
1509
232288
                    (current_right_sample * coeff1r) +
1510
232288
                    (previous_right_sample * coeff2r) + 0x80) >> 8;
1511
1512
232288
                previous_left_sample = current_left_sample;
1513
232288
                current_left_sample = av_clip_int16(next_left_sample);
1514
232288
                previous_right_sample = current_right_sample;
1515
232288
                current_right_sample = av_clip_int16(next_right_sample);
1516
232288
                *samples++ = current_left_sample;
1517
232288
                *samples++ = current_right_sample;
1518
            }
1519
        }
1520
1521
158
        bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1522
1523
158
        break;
1524
    }
1525
30
    case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
1526
    {
1527
        int coeff[2][2], shift[2];
1528
1529
90
        for(channel = 0; channel < avctx->channels; channel++) {
1530
60
            int byte = bytestream2_get_byteu(&gb);
1531
180
            for (i=0; i<2; i++)
1532
120
                coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1533
60
            shift[channel] = 20 - (byte & 0x0F);
1534
        }
1535
450
        for (count1 = 0; count1 < nb_samples / 2; count1++) {
1536
            int byte[2];
1537
1538
420
            byte[0] = bytestream2_get_byteu(&gb);
1539
420
            if (st) byte[1] = bytestream2_get_byteu(&gb);
1540
1260
            for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1541
2520
                for(channel = 0; channel < avctx->channels; channel++) {
1542
1680
                    int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1543
1680
                    sample = (sample +
1544
1680
                             c->status[channel].sample1 * coeff[channel][0] +
1545
1680
                             c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1546
1680
                    c->status[channel].sample2 = c->status[channel].sample1;
1547
1680
                    c->status[channel].sample1 = av_clip_int16(sample);
1548
1680
                    *samples++ = c->status[channel].sample1;
1549
                }
1550
            }
1551
        }
1552
30
        bytestream2_seek(&gb, 0, SEEK_END);
1553
30
        break;
1554
    }
1555
400
    case AV_CODEC_ID_ADPCM_EA_R1:
1556
    case AV_CODEC_ID_ADPCM_EA_R2:
1557
    case AV_CODEC_ID_ADPCM_EA_R3: {
1558
        /* channel numbering
1559
           2chan: 0=fl, 1=fr
1560
           4chan: 0=fl, 1=rl, 2=fr, 3=rr
1561
           6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
1562
400
        const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1563
        int previous_sample, current_sample, next_sample;
1564
        int coeff1, coeff2;
1565
        int shift;
1566
        unsigned int channel;
1567
        uint16_t *samplesC;
1568
400
        int count = 0;
1569
        int offsets[6];
1570
1571
1200
        for (channel=0; channel<avctx->channels; channel++)
1572
800
            offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1573
550
                                             bytestream2_get_le32(&gb)) +
1574
800
                               (avctx->channels + 1) * 4;
1575
1576
1200
        for (channel=0; channel<avctx->channels; channel++) {
1577
800
            bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1578
800
            samplesC = samples_p[channel];
1579
1580
800
            if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1581
190
                current_sample  = sign_extend(bytestream2_get_le16(&gb), 16);
1582
190
                previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1583
            } else {
1584
610
                current_sample  = c->status[channel].predictor;
1585
610
                previous_sample = c->status[channel].prev_sample;
1586
            }
1587
1588
41992
            for (count1 = 0; count1 < nb_samples / 28; count1++) {
1589
41192
                int byte = bytestream2_get_byte(&gb);
1590
41192
                if (byte == 0xEE) {  /* only seen in R2 and R3 */
1591
12
                    current_sample  = sign_extend(bytestream2_get_be16(&gb), 16);
1592
12
                    previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1593
1594
348
                    for (count2=0; count2<28; count2++)
1595
336
                        *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1596
                } else {
1597
41180
                    coeff1 = ea_adpcm_table[ byte >> 4     ];
1598
41180
                    coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1599
41180
                    shift = 20 - (byte & 0x0F);
1600
1601
1194220
                    for (count2=0; count2<28; count2++) {
1602
1153040
                        if (count2 & 1)
1603
576520
                            next_sample = (unsigned)sign_extend(byte,    4) << shift;
1604
                        else {
1605
576520
                            byte = bytestream2_get_byte(&gb);
1606
576520
                            next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
1607
                        }
1608
1609
1153040
                        next_sample += (current_sample  * coeff1) +
1610
1153040
                                       (previous_sample * coeff2);
1611
1153040
                        next_sample = av_clip_int16(next_sample >> 8);
1612
1613
1153040
                        previous_sample = current_sample;
1614
1153040
                        current_sample  = next_sample;
1615
1153040
                        *samplesC++ = current_sample;
1616
                    }
1617
                }
1618
            }
1619
800
            if (!count) {
1620
400
                count = count1;
1621
400
            } else if (count != count1) {
1622
                av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1623
                count = FFMAX(count, count1);
1624
            }
1625
1626
800
            if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1627
610
                c->status[channel].predictor   = current_sample;
1628
610
                c->status[channel].prev_sample = previous_sample;
1629
            }
1630
        }
1631
1632
400
        frame->nb_samples = count * 28;
1633
400
        bytestream2_seek(&gb, 0, SEEK_END);
1634
400
        break;
1635
    }
1636
600
    case AV_CODEC_ID_ADPCM_EA_XAS:
1637
1200
        for (channel=0; channel<avctx->channels; channel++) {
1638
            int coeff[2][4], shift[4];
1639
600
            int16_t *s = samples_p[channel];
1640
3000
            for (n = 0; n < 4; n++, s += 32) {
1641
2400
                int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1642
7200
                for (i=0; i<2; i++)
1643
4800
                    coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1644
2400
                s[0] = val & ~0x0F;
1645
1646
2400
                val = sign_extend(bytestream2_get_le16u(&gb), 16);
1647
2400
                shift[n] = 20 - (val & 0x0F);
1648
2400
                s[1] = val & ~0x0F;
1649
            }
1650
1651
9600
            for (m=2; m<32; m+=2) {
1652
9000
                s = &samples_p[channel][m];
1653
45000
                for (n = 0; n < 4; n++, s += 32) {
1654
                    int level, pred;
1655
36000
                    int byte = bytestream2_get_byteu(&gb);
1656
1657
36000
                    level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
1658
36000
                    pred  = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1659
36000
                    s[0]  = av_clip_int16((level + pred + 0x80) >> 8);
1660
1661
36000
                    level = sign_extend(byte, 4) * (1 << shift[n]);
1662
36000
                    pred  = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1663
36000
                    s[1]  = av_clip_int16((level + pred + 0x80) >> 8);
1664
                }
1665
            }
1666
        }
1667
600
        break;
1668
161
    case AV_CODEC_ID_ADPCM_IMA_AMV:
1669
161
        av_assert0(avctx->channels == 1);
1670
1671
        /*
1672
         * Header format:
1673
         *   int16_t  predictor;
1674
         *   uint8_t  step_index;
1675
         *   uint8_t  reserved;
1676
         *   uint32_t frame_size;
1677
         *
1678
         * Some implementations have step_index as 16-bits, but others
1679
         * only use the lower 8 and store garbage in the upper 8.
1680
         */
1681
161
        c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1682
161
        c->status[0].step_index = bytestream2_get_byteu(&gb);
1683
161
        bytestream2_skipu(&gb, 5);
1684
161
        if (c->status[0].step_index > 88u) {
1685
            av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1686
                   c->status[0].step_index);
1687
            return AVERROR_INVALIDDATA;
1688
        }
1689
1690
111090
        for (n = nb_samples >> 1; n > 0; n--) {
1691
110929
            int v = bytestream2_get_byteu(&gb);
1692
1693
110929
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1694
110929
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1695
        }
1696
1697
161
        if (nb_samples & 1) {
1698
22
            int v = bytestream2_get_byteu(&gb);
1699
22
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1700
1701
22
            if (v & 0x0F) {
1702
                /* Holds true on all the http://samples.mplayerhq.hu/amv samples. */
1703
                av_log(avctx, AV_LOG_WARNING, "Last nibble set on packet with odd sample count.\n");
1704
                av_log(avctx, AV_LOG_WARNING, "Sample will be skipped.\n");
1705
            }
1706
        }
1707
161
        break;
1708
350
    case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1709
700
        for (i = 0; i < avctx->channels; i++) {
1710
350
            c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1711
350
            c->status[i].step_index = bytestream2_get_byteu(&gb);
1712
350
            bytestream2_skipu(&gb, 1);
1713
350
            if (c->status[i].step_index > 88u) {
1714
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1715
                       c->status[i].step_index);
1716
                return AVERROR_INVALIDDATA;
1717
            }
1718
        }
1719
1720
89950
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1721
89600
            int v = bytestream2_get_byteu(&gb);
1722
1723
89600
            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
1724
89600
            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
1725
        }
1726
350
        break;
1727
64
    case AV_CODEC_ID_ADPCM_CT:
1728
262160
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1729
262096
            int v = bytestream2_get_byteu(&gb);
1730
262096
            *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4  );
1731
262096
            *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1732
        }
1733
64
        break;
1734
57
    case AV_CODEC_ID_ADPCM_SBPRO_4:
1735
    case AV_CODEC_ID_ADPCM_SBPRO_3:
1736
    case AV_CODEC_ID_ADPCM_SBPRO_2:
1737
57
        if (!c->status[0].step_index) {
1738
            /* the first byte is a raw sample */
1739
3
            *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1740
3
            if (st)
1741
                *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1742
3
            c->status[0].step_index = 1;
1743
3
            nb_samples--;
1744
        }
1745
57
        if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1746
52256
            for (n = nb_samples >> (1 - st); n > 0; n--) {
1747
52230
                int byte = bytestream2_get_byteu(&gb);
1748
104460
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1749
52230
                                                       byte >> 4,   4, 0);
1750
52230
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1751
52230
                                                       byte & 0x0F, 4, 0);
1752
            }
1753
31
        } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1754
34838
            for (n = (nb_samples<<st) / 3; n > 0; n--) {
1755
34820
                int byte = bytestream2_get_byteu(&gb);
1756
69640
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1757
34820
                                                        byte >> 5        , 3, 0);
1758
69640
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1759
34820
                                                       (byte >> 2) & 0x07, 3, 0);
1760
34820
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1761
34820
                                                        byte & 0x03,       2, 0);
1762
            }
1763
        } else {
1764
26128
            for (n = nb_samples >> (2 - st); n > 0; n--) {
1765
26115
                int byte = bytestream2_get_byteu(&gb);
1766
52230
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1767
26115
                                                        byte >> 6        , 2, 2);
1768
52230
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1769
26115
                                                       (byte >> 4) & 0x03, 2, 2);
1770
52230
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1771
26115
                                                       (byte >> 2) & 0x03, 2, 2);
1772
26115
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1773
26115
                                                        byte & 0x03,       2, 2);
1774
            }
1775
        }
1776
57
        break;
1777
195
    case AV_CODEC_ID_ADPCM_SWF:
1778
195
        adpcm_swf_decode(avctx, buf, buf_size, samples);
1779
195
        bytestream2_seek(&gb, 0, SEEK_END);
1780
195
        break;
1781
136
    case AV_CODEC_ID_ADPCM_YAMAHA:
1782
553096
        for (n = nb_samples >> (1 - st); n > 0; n--) {
1783
552960
            int v = bytestream2_get_byteu(&gb);
1784
552960
            *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1785
552960
            *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4  );
1786
        }
1787
136
        break;
1788
    case AV_CODEC_ID_ADPCM_AICA:
1789
        for (channel = 0; channel < avctx->channels; channel++) {
1790
            samples = samples_p[channel];
1791
            for (n = nb_samples >> 1; n > 0; n--) {
1792
                int v = bytestream2_get_byteu(&gb);
1793
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1794
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4  );
1795
            }
1796
        }
1797
        break;
1798
12
    case AV_CODEC_ID_ADPCM_AFC:
1799
    {
1800
        int samples_per_block;
1801
        int blocks;
1802
1803

12
        if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1804
            samples_per_block = avctx->extradata[0] / 16;
1805
            blocks = nb_samples / avctx->extradata[0];
1806
        } else {
1807
12
            samples_per_block = nb_samples / 16;
1808
12
            blocks = 1;
1809
        }
1810
1811
24
        for (m = 0; m < blocks; m++) {
1812
36
        for (channel = 0; channel < avctx->channels; channel++) {
1813
24
            int prev1 = c->status[channel].sample1;
1814
24
            int prev2 = c->status[channel].sample2;
1815
1816
24
            samples = samples_p[channel] + m * 16;
1817
            /* Read in every sample for this channel.  */
1818
26904
            for (i = 0; i < samples_per_block; i++) {
1819
26880
                int byte = bytestream2_get_byteu(&gb);
1820
26880
                int scale = 1 << (byte >> 4);
1821
26880
                int index = byte & 0xf;
1822
26880
                int factor1 = ff_adpcm_afc_coeffs[0][index];
1823
26880
                int factor2 = ff_adpcm_afc_coeffs[1][index];
1824
1825
                /* Decode 16 samples.  */
1826
456960
                for (n = 0; n < 16; n++) {
1827
                    int32_t sampledat;
1828
1829
430080
                    if (n & 1) {
1830
215040
                        sampledat = sign_extend(byte, 4);
1831
                    } else {
1832
215040
                        byte = bytestream2_get_byteu(&gb);
1833
215040
                        sampledat = sign_extend(byte >> 4, 4);
1834
                    }
1835
1836
430080
                    sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
1837
430080
                                sampledat * scale;
1838
430080
                    *samples = av_clip_int16(sampledat);
1839
430080
                    prev2 = prev1;
1840
430080
                    prev1 = *samples++;
1841
                }
1842
            }
1843
1844
24
            c->status[channel].sample1 = prev1;
1845
24
            c->status[channel].sample2 = prev2;
1846
        }
1847
        }
1848
12
        bytestream2_seek(&gb, 0, SEEK_END);
1849
12
        break;
1850
    }
1851
71
    case AV_CODEC_ID_ADPCM_THP:
1852
    case AV_CODEC_ID_ADPCM_THP_LE:
1853
    {
1854
        int table[14][16];
1855
        int ch;
1856
1857
#define THP_GET16(g) \
1858
    sign_extend( \
1859
        avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1860
        bytestream2_get_le16u(&(g)) : \
1861
        bytestream2_get_be16u(&(g)), 16)
1862
1863
71
        if (avctx->extradata) {
1864
            GetByteContext tb;
1865
            if (avctx->extradata_size < 32 * avctx->channels) {
1866
                av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1867
                return AVERROR_INVALIDDATA;
1868
            }
1869
1870
            bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1871
            for (i = 0; i < avctx->channels; i++)
1872
                for (n = 0; n < 16; n++)
1873
                    table[i][n] = THP_GET16(tb);
1874
        } else {
1875
213
            for (i = 0; i < avctx->channels; i++)
1876
2414
                for (n = 0; n < 16; n++)
1877
2272
                    table[i][n] = THP_GET16(gb);
1878
1879
71
            if (!c->has_status) {
1880
                /* Initialize the previous sample.  */
1881
3
                for (i = 0; i < avctx->channels; i++) {
1882
2
                    c->status[i].sample1 = THP_GET16(gb);
1883
2
                    c->status[i].sample2 = THP_GET16(gb);
1884
                }
1885
1
                c->has_status = 1;
1886
            } else {
1887
70
                bytestream2_skip(&gb, avctx->channels * 4);
1888
            }
1889
        }
1890
1891
213
        for (ch = 0; ch < avctx->channels; ch++) {
1892
142
            samples = samples_p[ch];
1893
1894
            /* Read in every sample for this channel.  */
1895
10982
            for (i = 0; i < (nb_samples + 13) / 14; i++) {
1896
10840
                int byte = bytestream2_get_byteu(&gb);
1897
10840
                int index = (byte >> 4) & 7;
1898
10840
                unsigned int exp = byte & 0x0F;
1899
10840
                int64_t factor1 = table[ch][index * 2];
1900
10840
                int64_t factor2 = table[ch][index * 2 + 1];
1901
1902
                /* Decode 14 samples.  */
1903

162600
                for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1904
                    int32_t sampledat;
1905
1906
151760
                    if (n & 1) {
1907
75880
                        sampledat = sign_extend(byte, 4);
1908
                    } else {
1909
75880
                        byte = bytestream2_get_byteu(&gb);
1910
75880
                        sampledat = sign_extend(byte >> 4, 4);
1911
                    }
1912
1913
151760
                    sampledat = ((c->status[ch].sample1 * factor1
1914
151760
                                + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
1915
151760
                    *samples = av_clip_int16(sampledat);
1916
151760
                    c->status[ch].sample2 = c->status[ch].sample1;
1917
151760
                    c->status[ch].sample1 = *samples++;
1918
                }
1919
            }
1920
        }
1921
71
        break;
1922
    }
1923
32
    case AV_CODEC_ID_ADPCM_DTK:
1924
96
        for (channel = 0; channel < avctx->channels; channel++) {
1925
64
            samples = samples_p[channel];
1926
1927
            /* Read in every sample for this channel.  */
1928
2112
            for (i = 0; i < nb_samples / 28; i++) {
1929
                int byte, header;
1930
2048
                if (channel)
1931
1024
                    bytestream2_skipu(&gb, 1);
1932
2048
                header = bytestream2_get_byteu(&gb);
1933
2048
                bytestream2_skipu(&gb, 3 - channel);
1934
1935
                /* Decode 28 samples.  */
1936
59392
                for (n = 0; n < 28; n++) {
1937
                    int32_t sampledat, prev;
1938
1939

57344
                    switch (header >> 4) {
1940
3388
                    case 1:
1941
3388
                        prev = (c->status[channel].sample1 * 0x3c);
1942
3388
                        break;
1943
50876
                    case 2:
1944
50876
                        prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1945
50876
                        break;
1946
2744
                    case 3:
1947
2744
                        prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1948
2744
                        break;
1949
336
                    default:
1950
336
                        prev = 0;
1951
                    }
1952
1953
57344
                    prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1954
1955
57344
                    byte = bytestream2_get_byteu(&gb);
1956
57344
                    if (!channel)
1957
28672
                        sampledat = sign_extend(byte, 4);
1958
                    else
1959
28672
                        sampledat = sign_extend(byte >> 4, 4);
1960
1961
57344
                    sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
1962
57344
                    *samples++ = av_clip_int16(sampledat >> 6);
1963
57344
                    c->status[channel].sample2 = c->status[channel].sample1;
1964
57344
                    c->status[channel].sample1 = sampledat;
1965
                }
1966
            }
1967
64
            if (!channel)
1968
32
                bytestream2_seek(&gb, 0, SEEK_SET);
1969
        }
1970
32
        break;
1971
    case AV_CODEC_ID_ADPCM_PSX:
1972
        for (int block = 0; block < avpkt->size / FFMAX(avctx->block_align, 16 * avctx->channels); block++) {
1973
            int nb_samples_per_block = 28 * FFMAX(avctx->block_align, 16 * avctx->channels) / (16 * avctx->channels);
1974
            for (channel = 0; channel < avctx->channels; channel++) {
1975
                samples = samples_p[channel] + block * nb_samples_per_block;
1976
                av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
1977
1978
                /* Read in every sample for this channel.  */
1979
                for (i = 0; i < nb_samples_per_block / 28; i++) {
1980
                    int filter, shift, flag, byte;
1981
1982
                    filter = bytestream2_get_byteu(&gb);
1983
                    shift  = filter & 0xf;
1984
                    filter = filter >> 4;
1985
                    if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1986
                        return AVERROR_INVALIDDATA;
1987
                    flag   = bytestream2_get_byteu(&gb);
1988
1989
                    /* Decode 28 samples.  */
1990
                    for (n = 0; n < 28; n++) {
1991
                        int sample = 0, scale;
1992
1993
                        if (flag < 0x07) {
1994
                            if (n & 1) {
1995
                                scale = sign_extend(byte >> 4, 4);
1996
                            } else {
1997
                                byte  = bytestream2_get_byteu(&gb);
1998
                                scale = sign_extend(byte, 4);
1999
                            }
2000
2001
                            scale  = scale * (1 << 12);
2002
                            sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
2003
                        }
2004
                        *samples++ = av_clip_int16(sample);
2005
                        c->status[channel].sample2 = c->status[channel].sample1;
2006
                        c->status[channel].sample1 = sample;
2007
                    }
2008
                }
2009
            }
2010
        }
2011
        break;
2012
946
    case AV_CODEC_ID_ADPCM_ARGO:
2013
        /*
2014
         * The format of each block:
2015
         *   uint8_t left_control;
2016
         *   uint4_t left_samples[nb_samples];
2017
         *   ---- and if stereo ----
2018
         *   uint8_t right_control;
2019
         *   uint4_t right_samples[nb_samples];
2020
         *
2021
         * Format of the control byte:
2022
         * MSB [SSSSRDRR] LSB
2023
         *   S = (Shift Amount - 2)
2024
         *   D = Decoder flag.
2025
         *   R = Reserved
2026
         *
2027
         * Each block relies on the previous two samples of each channel.
2028
         * They should be 0 initially.
2029
         */
2030
31191
        for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
2031
76951
            for (channel = 0; channel < avctx->channels; channel++) {
2032
                int control, shift;
2033
2034
46706
                samples = samples_p[channel] + block * 32;
2035
46706
                cs = c->status + channel;
2036
2037
                /* Get the control byte and decode the samples, 2 at a time. */
2038
46706
                control = bytestream2_get_byteu(&gb);
2039
46706
                shift = (control >> 4) + 2;
2040
2041
794002
                for (n = 0; n < 16; n++) {
2042
747296
                    int sample = bytestream2_get_byteu(&gb);
2043
747296
                    *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift, control & 0x04);
2044
747296
                    *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift, control & 0x04);
2045
                }
2046
            }
2047
        }
2048
946
        break;
2049
    case AV_CODEC_ID_ADPCM_ZORK:
2050
        for (n = 0; n < nb_samples * avctx->channels; n++) {
2051
            int v = bytestream2_get_byteu(&gb);
2052
            *samples++ = adpcm_zork_expand_nibble(&c->status[n % avctx->channels], v);
2053
        }
2054
        break;
2055
    case AV_CODEC_ID_ADPCM_IMA_MTF:
2056
        for (n = nb_samples / 2; n > 0; n--) {
2057
            for (channel = 0; channel < avctx->channels; channel++) {
2058
                int v = bytestream2_get_byteu(&gb);
2059
                *samples++  = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2060
                samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2061
            }
2062
            samples += avctx->channels;
2063
        }
2064
        break;
2065
    default:
2066
        av_assert0(0); // unsupported codec_id should not happen
2067
    }
2068
2069

37298
    if (avpkt->size && bytestream2_tell(&gb) == 0) {
2070
        av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2071
        return AVERROR_INVALIDDATA;
2072
    }
2073
2074
37298
    *got_frame_ptr = 1;
2075
2076
37298
    if (avpkt->size < bytestream2_tell(&gb)) {
2077
        av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2078
        return avpkt->size;
2079
    }
2080
2081
37298
    return bytestream2_tell(&gb);
2082
}
2083
2084
183
static void adpcm_flush(AVCodecContext *avctx)
2085
{
2086
183
    ADPCMDecodeContext *c = avctx->priv_data;
2087
2088
    /* Just nuke the entire state and re-init. */
2089
183
    memset(c, 0, sizeof(ADPCMDecodeContext));
2090
2091

183
    switch(avctx->codec_id) {
2092
2
    case AV_CODEC_ID_ADPCM_CT:
2093
2
        c->status[0].step = c->status[1].step = 511;
2094
2
        break;
2095
2096
3
    case AV_CODEC_ID_ADPCM_IMA_APC:
2097

3
        if (avctx->extradata && avctx->extradata_size >= 8) {
2098
2
            c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata    ), 18);
2099
2
            c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
2100
        }
2101
3
        break;
2102
2103
6
    case AV_CODEC_ID_ADPCM_IMA_APM:
2104
6
        if (avctx->extradata) {
2105
6
            if (avctx->extradata_size >= 28) {
2106
6
                c->status[0].predictor  = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
2107
6
                c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
2108
6
                c->status[1].predictor  = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
2109
6
                c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
2110
            } else if (avctx->extradata_size >= 16) {
2111
                c->status[0].predictor  = av_clip_intp2(AV_RL32(avctx->extradata +  0), 18);
2112
                c->status[0].step_index = av_clip(AV_RL32(avctx->extradata +  4), 0, 88);
2113
                c->status[1].predictor  = av_clip_intp2(AV_RL32(avctx->extradata +  8), 18);
2114
                c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 12), 0, 88);
2115
            }
2116
        }
2117
6
        break;
2118
2119
4
    case AV_CODEC_ID_ADPCM_IMA_WS:
2120

4
        if (avctx->extradata && avctx->extradata_size >= 2)
2121
3
            c->vqa_version = AV_RL16(avctx->extradata);
2122
4
        break;
2123
168
    default:
2124
        /* Other codecs may want to handle this during decoding. */
2125
168
        c->has_status = 0;
2126
168
        return;
2127
    }
2128
2129
15
    c->has_status = 1;
2130
}
2131
2132
2133
static const enum AVSampleFormat sample_fmts_s16[]  = { AV_SAMPLE_FMT_S16,
2134
                                                        AV_SAMPLE_FMT_NONE };
2135
static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
2136
                                                        AV_SAMPLE_FMT_NONE };
2137
static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
2138
                                                        AV_SAMPLE_FMT_S16P,
2139
                                                        AV_SAMPLE_FMT_NONE };
2140
2141
#define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
2142
AVCodec ff_ ## name_ ## _decoder = {                        \
2143
    .name           = #name_,                               \
2144
    .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
2145
    .type           = AVMEDIA_TYPE_AUDIO,                   \
2146
    .id             = id_,                                  \
2147
    .priv_data_size = sizeof(ADPCMDecodeContext),           \
2148
    .init           = adpcm_decode_init,                    \
2149
    .decode         = adpcm_decode_frame,                   \
2150
    .flush          = adpcm_flush,                          \
2151
    .capabilities   = AV_CODEC_CAP_DR1,                     \
2152
    .sample_fmts    = sample_fmts_,                         \
2153
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,         \
2154
}
2155
2156
/* Note: Do not forget to add new entries to the Makefile as well. */
2157
ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM,         sample_fmts_s16p, adpcm_4xm,         "ADPCM 4X Movie");
2158
ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC,         sample_fmts_s16p, adpcm_afc,         "ADPCM Nintendo Gamecube AFC");
2159
ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM,         sample_fmts_s16,  adpcm_agm,         "ADPCM AmuseGraphics Movie");
2160
ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA,        sample_fmts_s16p, adpcm_aica,        "ADPCM Yamaha AICA");
2161
ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO,        sample_fmts_s16p, adpcm_argo,        "ADPCM Argonaut Games");
2162
ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT,          sample_fmts_s16,  adpcm_ct,          "ADPCM Creative Technology");
2163
ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK,         sample_fmts_s16p, adpcm_dtk,         "ADPCM Nintendo Gamecube DTK");
2164
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA,          sample_fmts_s16,  adpcm_ea,          "ADPCM Electronic Arts");
2165
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16,  adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
2166
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1,       sample_fmts_s16p, adpcm_ea_r1,       "ADPCM Electronic Arts R1");
2167
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2,       sample_fmts_s16p, adpcm_ea_r2,       "ADPCM Electronic Arts R2");
2168
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3,       sample_fmts_s16p, adpcm_ea_r3,       "ADPCM Electronic Arts R3");
2169
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS,      sample_fmts_s16p, adpcm_ea_xas,      "ADPCM Electronic Arts XAS");
2170
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV,     sample_fmts_s16,  adpcm_ima_amv,     "ADPCM IMA AMV");
2171
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC,     sample_fmts_s16,  adpcm_ima_apc,     "ADPCM IMA CRYO APC");
2172
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM,     sample_fmts_s16,  adpcm_ima_apm,     "ADPCM IMA Ubisoft APM");
2173
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_CUNNING, sample_fmts_s16p, adpcm_ima_cunning, "ADPCM IMA Cunning Developments");
2174
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4,    sample_fmts_s16,  adpcm_ima_dat4,    "ADPCM IMA Eurocom DAT4");
2175
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3,     sample_fmts_s16,  adpcm_ima_dk3,     "ADPCM IMA Duck DK3");
2176
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4,     sample_fmts_s16,  adpcm_ima_dk4,     "ADPCM IMA Duck DK4");
2177
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16,  adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
2178
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16,  adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
2179
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS,     sample_fmts_s16,  adpcm_ima_iss,     "ADPCM IMA Funcom ISS");
2180
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MOFLEX,  sample_fmts_s16p, adpcm_ima_moflex,  "ADPCM IMA MobiClip MOFLEX");
2181
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MTF,     sample_fmts_s16,  adpcm_ima_mtf,     "ADPCM IMA Capcom's MT Framework");
2182
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI,     sample_fmts_s16,  adpcm_ima_oki,     "ADPCM IMA Dialogic OKI");
2183
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT,      sample_fmts_s16p, adpcm_ima_qt,      "ADPCM IMA QuickTime");
2184
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD,     sample_fmts_s16,  adpcm_ima_rad,     "ADPCM IMA Radical");
2185
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SSI,     sample_fmts_s16,  adpcm_ima_ssi,     "ADPCM IMA Simon & Schuster Interactive");
2186
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG,  sample_fmts_s16,  adpcm_ima_smjpeg,  "ADPCM IMA Loki SDL MJPEG");
2187
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ALP,     sample_fmts_s16,  adpcm_ima_alp,     "ADPCM IMA High Voltage Software ALP");
2188
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV,     sample_fmts_s16p, adpcm_ima_wav,     "ADPCM IMA WAV");
2189
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS,      sample_fmts_both, adpcm_ima_ws,      "ADPCM IMA Westwood");
2190
ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS,          sample_fmts_both, adpcm_ms,          "ADPCM Microsoft");
2191
ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF,        sample_fmts_s16p, adpcm_mtaf,        "ADPCM MTAF");
2192
ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX,         sample_fmts_s16p, adpcm_psx,         "ADPCM Playstation");
2193
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2,     sample_fmts_s16,  adpcm_sbpro_2,     "ADPCM Sound Blaster Pro 2-bit");
2194
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3,     sample_fmts_s16,  adpcm_sbpro_3,     "ADPCM Sound Blaster Pro 2.6-bit");
2195
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4,     sample_fmts_s16,  adpcm_sbpro_4,     "ADPCM Sound Blaster Pro 4-bit");
2196
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF,         sample_fmts_s16,  adpcm_swf,         "ADPCM Shockwave Flash");
2197
ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE,      sample_fmts_s16p, adpcm_thp_le,      "ADPCM Nintendo THP (little-endian)");
2198
ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP,         sample_fmts_s16p, adpcm_thp,         "ADPCM Nintendo THP");
2199
ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA,          sample_fmts_s16p, adpcm_xa,          "ADPCM CDROM XA");
2200
ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA,      sample_fmts_s16,  adpcm_yamaha,      "ADPCM Yamaha");
2201
ADPCM_DECODER(AV_CODEC_ID_ADPCM_ZORK,        sample_fmts_s16,  adpcm_zork,        "ADPCM Zork");