GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/adpcm.c Lines: 927 1285 72.1 %
Date: 2020-09-25 23:16:12 Branches: 434 656 66.2 %

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;
104
} ADPCMDecodeContext;
105
106
177
static av_cold int adpcm_decode_init(AVCodecContext * avctx)
107
{
108
177
    ADPCMDecodeContext *c = avctx->priv_data;
109
177
    unsigned int min_channels = 1;
110
177
    unsigned int max_channels = 2;
111
112

177
    switch(avctx->codec->id) {
113
22
    case AV_CODEC_ID_ADPCM_IMA_CUNNING:
114
22
        max_channels = 1;
115
22
        break;
116
8
    case AV_CODEC_ID_ADPCM_DTK:
117
    case AV_CODEC_ID_ADPCM_EA:
118
8
        min_channels = 2;
119
8
        break;
120
23
    case AV_CODEC_ID_ADPCM_AFC:
121
    case AV_CODEC_ID_ADPCM_EA_R1:
122
    case AV_CODEC_ID_ADPCM_EA_R2:
123
    case AV_CODEC_ID_ADPCM_EA_R3:
124
    case AV_CODEC_ID_ADPCM_EA_XAS:
125
    case AV_CODEC_ID_ADPCM_MS:
126
23
        max_channels = 6;
127
23
        break;
128
    case AV_CODEC_ID_ADPCM_MTAF:
129
        min_channels = 2;
130
        max_channels = 8;
131
        if (avctx->channels & 1) {
132
            avpriv_request_sample(avctx, "channel count %d\n", avctx->channels);
133
            return AVERROR_PATCHWELCOME;
134
        }
135
        break;
136
    case AV_CODEC_ID_ADPCM_PSX:
137
        max_channels = 8;
138
        break;
139
8
    case AV_CODEC_ID_ADPCM_IMA_DAT4:
140
    case AV_CODEC_ID_ADPCM_THP:
141
    case AV_CODEC_ID_ADPCM_THP_LE:
142
8
        max_channels = 14;
143
8
        break;
144
    }
145

177
    if (avctx->channels < min_channels || avctx->channels > max_channels) {
146
        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
147
        return AVERROR(EINVAL);
148
    }
149
150


177
    switch(avctx->codec->id) {
151
2
    case AV_CODEC_ID_ADPCM_CT:
152
2
        c->status[0].step = c->status[1].step = 511;
153
2
        break;
154
10
    case AV_CODEC_ID_ADPCM_IMA_WAV:
155

10
        if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
156
            return AVERROR_INVALIDDATA;
157
10
        break;
158
3
    case AV_CODEC_ID_ADPCM_IMA_APC:
159

3
        if (avctx->extradata && avctx->extradata_size >= 8) {
160
2
            c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata    ), 18);
161
2
            c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
162
        }
163
3
        break;
164
6
    case AV_CODEC_ID_ADPCM_IMA_APM:
165
6
        if (avctx->extradata) {
166
6
            if (avctx->extradata_size >= 28) {
167
6
                c->status[0].predictor  = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
168
6
                c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
169
6
                c->status[1].predictor  = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
170
6
                c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
171
            } else if (avctx->extradata_size >= 16) {
172
                c->status[0].predictor  = av_clip_intp2(AV_RL32(avctx->extradata +  0), 18);
173
                c->status[0].step_index = av_clip(AV_RL32(avctx->extradata +  4), 0, 88);
174
                c->status[1].predictor  = av_clip_intp2(AV_RL32(avctx->extradata +  8), 18);
175
                c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 12), 0, 88);
176
            }
177
        }
178
6
        break;
179
4
    case AV_CODEC_ID_ADPCM_IMA_WS:
180

4
        if (avctx->extradata && avctx->extradata_size >= 2)
181
3
            c->vqa_version = AV_RL16(avctx->extradata);
182
4
        break;
183
6
    case AV_CODEC_ID_ADPCM_ARGO:
184

6
        if (avctx->bits_per_coded_sample != 4 || avctx->block_align != 17 * avctx->channels)
185
            return AVERROR_INVALIDDATA;
186
6
        break;
187
    case AV_CODEC_ID_ADPCM_ZORK:
188
        if (avctx->bits_per_coded_sample != 8)
189
            return AVERROR_INVALIDDATA;
190
        break;
191
146
    default:
192
146
        break;
193
    }
194
195

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

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

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

2664
        if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
600
            avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
601
            filter=0;
602
        }
603
2664
        if (shift < 0) {
604
            avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
605
            shift = 0;
606
        }
607
608
2664
        f0 = xa_adpcm_table[filter][0];
609
2664
        f1 = xa_adpcm_table[filter][1];
610
611
77256
        for(j=0;j<28;j++) {
612
74592
            d = in[16+i+j*4];
613
614
74592
            t = sign_extend(d >> 4, 4);
615
74592
            s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
616
74592
            s_2 = s_1;
617
74592
            s_1 = av_clip_int16(s);
618
74592
            out1[j] = s_1;
619
        }
620
621
2664
        if (channels == 2) {
622
2664
            right->sample1 = s_1;
623
2664
            right->sample2 = s_2;
624
        } else {
625
            left->sample1 = s_1;
626
            left->sample2 = s_2;
627
        }
628
629
2664
        out0 += 28 * (3 - channels);
630
2664
        out1 += 28 * (3 - channels);
631
    }
632
633
666
    return 0;
634
}
635
636
260
static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
637
{
638
260
    ADPCMDecodeContext *c = avctx->priv_data;
639
    GetBitContext gb;
640
    const int8_t *table;
641
    int k0, signmask, nb_bits, count;
642
260
    int size = buf_size*8;
643
    int i;
644
645
260
    init_get_bits(&gb, buf, size);
646
647
    //read bits & initial values
648
260
    nb_bits = get_bits(&gb, 2)+2;
649
260
    table = swf_index_tables[nb_bits-2];
650
260
    k0 = 1 << (nb_bits-2);
651
260
    signmask = 1 << (nb_bits-1);
652
653
520
    while (get_bits_count(&gb) <= size - 22*avctx->channels) {
654
780
        for (i = 0; i < avctx->channels; i++) {
655
520
            *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
656
520
            c->status[i].step_index = get_bits(&gb, 6);
657
        }
658
659

532480
        for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
660
            int i;
661
662
1596660
            for (i = 0; i < avctx->channels; i++) {
663
                // similar to IMA adpcm
664
1064440
                int delta = get_bits(&gb, nb_bits);
665
1064440
                int step = ff_adpcm_step_table[c->status[i].step_index];
666
1064440
                int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
667
1064440
                int k = k0;
668
669
                do {
670
3193320
                    if (delta & k)
671
1328304
                        vpdiff += step;
672
3193320
                    step >>= 1;
673
3193320
                    k >>= 1;
674
3193320
                } while(k);
675
1064440
                vpdiff += step;
676
677
1064440
                if (delta & signmask)
678
535145
                    c->status[i].predictor -= vpdiff;
679
                else
680
529295
                    c->status[i].predictor += vpdiff;
681
682
1064440
                c->status[i].step_index += table[delta & (~signmask)];
683
684
1064440
                c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
685
1064440
                c->status[i].predictor = av_clip_int16(c->status[i].predictor);
686
687
1064440
                *samples++ = c->status[i].predictor;
688
            }
689
        }
690
    }
691
260
}
692
693
18335520
int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
694
{
695
18335520
    int sample = sign_extend(nibble, 4) * (1 << shift);
696
697
18335520
    if (flag)
698
9806208
        sample += (8 * cs->sample1) - (4 * cs->sample2);
699
    else
700
8529312
        sample += 4 * cs->sample1;
701
702
18335520
    sample = av_clip_int16(sample >> 2);
703
704
18335520
    cs->sample2 = cs->sample1;
705
18335520
    cs->sample1 = sample;
706
707
18335520
    return sample;
708
}
709
710
/**
711
 * Get the number of samples (per channel) that will be decoded from the packet.
712
 * In one case, this is actually the maximum number of samples possible to
713
 * decode with the given buf_size.
714
 *
715
 * @param[out] coded_samples set to the number of samples as coded in the
716
 *                           packet, or 0 if the codec does not encode the
717
 *                           number of samples in each frame.
718
 * @param[out] approx_nb_samples set to non-zero if the number of samples
719
 *                               returned is an approximation.
720
 */
721
37286
static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
722
                          int buf_size, int *coded_samples, int *approx_nb_samples)
723
{
724
37286
    ADPCMDecodeContext *s = avctx->priv_data;
725
37286
    int nb_samples        = 0;
726
37286
    int ch                = avctx->channels;
727
37286
    int has_coded_samples = 0;
728
    int header_size;
729
730
37286
    *coded_samples = 0;
731
37286
    *approx_nb_samples = 0;
732
733
37286
    if(ch <= 0)
734
        return 0;
735
736

37286
    switch (avctx->codec->id) {
737
    /* constant, only check buf_size */
738
600
    case AV_CODEC_ID_ADPCM_EA_XAS:
739
600
        if (buf_size < 76 * ch)
740
            return 0;
741
600
        nb_samples = 128;
742
600
        break;
743
26342
    case AV_CODEC_ID_ADPCM_IMA_QT:
744
26342
        if (buf_size < 34 * ch)
745
            return 0;
746
26342
        nb_samples = 64;
747
26342
        break;
748
    /* simple 4-bit adpcm */
749
736
    case AV_CODEC_ID_ADPCM_CT:
750
    case AV_CODEC_ID_ADPCM_IMA_APC:
751
    case AV_CODEC_ID_ADPCM_IMA_CUNNING:
752
    case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
753
    case AV_CODEC_ID_ADPCM_IMA_OKI:
754
    case AV_CODEC_ID_ADPCM_IMA_WS:
755
    case AV_CODEC_ID_ADPCM_YAMAHA:
756
    case AV_CODEC_ID_ADPCM_AICA:
757
    case AV_CODEC_ID_ADPCM_IMA_SSI:
758
    case AV_CODEC_ID_ADPCM_IMA_APM:
759
    case AV_CODEC_ID_ADPCM_IMA_ALP:
760
    case AV_CODEC_ID_ADPCM_IMA_MTF:
761
736
        nb_samples = buf_size * 2 / ch;
762
736
        break;
763
    }
764
37286
    if (nb_samples)
765
27678
        return nb_samples;
766
767
    /* simple 4-bit adpcm, with header */
768
9608
    header_size = 0;
769

9608
    switch (avctx->codec->id) {
770
145
        case AV_CODEC_ID_ADPCM_4XM:
771
        case AV_CODEC_ID_ADPCM_AGM:
772
        case AV_CODEC_ID_ADPCM_IMA_DAT4:
773
        case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
774
145
        case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
775
161
        case AV_CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
776
350
        case AV_CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4 * ch;      break;
777
    }
778
9608
    if (header_size > 0)
779
656
        return (buf_size - header_size) * 2 / ch;
780
781
    /* more complex formats */
782




8952
    switch (avctx->codec->id) {
783
158
    case AV_CODEC_ID_ADPCM_EA:
784
158
        has_coded_samples = 1;
785
158
        *coded_samples  = bytestream2_get_le32(gb);
786
158
        *coded_samples -= *coded_samples % 28;
787
158
        nb_samples      = (buf_size - 12) / 30 * 28;
788
158
        break;
789
47
    case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
790
47
        has_coded_samples = 1;
791
47
        *coded_samples = bytestream2_get_le32(gb);
792
47
        nb_samples     = (buf_size - (4 + 8 * ch)) * 2 / ch;
793
47
        break;
794
30
    case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
795
30
        nb_samples = (buf_size - ch) / ch * 2;
796
30
        break;
797
400
    case AV_CODEC_ID_ADPCM_EA_R1:
798
    case AV_CODEC_ID_ADPCM_EA_R2:
799
    case AV_CODEC_ID_ADPCM_EA_R3:
800
        /* maximum number of samples */
801
        /* has internal offsets and a per-frame switch to signal raw 16-bit */
802
400
        has_coded_samples = 1;
803

400
        switch (avctx->codec->id) {
804
95
        case AV_CODEC_ID_ADPCM_EA_R1:
805
95
            header_size    = 4 + 9 * ch;
806
95
            *coded_samples = bytestream2_get_le32(gb);
807
95
            break;
808
180
        case AV_CODEC_ID_ADPCM_EA_R2:
809
180
            header_size    = 4 + 5 * ch;
810
180
            *coded_samples = bytestream2_get_le32(gb);
811
180
            break;
812
125
        case AV_CODEC_ID_ADPCM_EA_R3:
813
125
            header_size    = 4 + 5 * ch;
814
125
            *coded_samples = bytestream2_get_be32(gb);
815
125
            break;
816
        }
817
400
        *coded_samples -= *coded_samples % 28;
818
400
        nb_samples      = (buf_size - header_size) * 2 / ch;
819
400
        nb_samples     -= nb_samples % 28;
820
400
        *approx_nb_samples = 1;
821
400
        break;
822
642
    case AV_CODEC_ID_ADPCM_IMA_DK3:
823
642
        if (avctx->block_align > 0)
824
642
            buf_size = FFMIN(buf_size, avctx->block_align);
825
642
        nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
826
642
        break;
827
649
    case AV_CODEC_ID_ADPCM_IMA_DK4:
828
649
        if (avctx->block_align > 0)
829
649
            buf_size = FFMIN(buf_size, avctx->block_align);
830
649
        if (buf_size < 4 * ch)
831
            return AVERROR_INVALIDDATA;
832
649
        nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
833
649
        break;
834
1000
    case AV_CODEC_ID_ADPCM_IMA_RAD:
835
1000
        if (avctx->block_align > 0)
836
1000
            buf_size = FFMIN(buf_size, avctx->block_align);
837
1000
        nb_samples = (buf_size - 4 * ch) * 2 / ch;
838
1000
        break;
839
1668
    case AV_CODEC_ID_ADPCM_IMA_WAV:
840
    {
841
1668
        int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
842
1668
        int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
843
1668
        if (avctx->block_align > 0)
844
1668
            buf_size = FFMIN(buf_size, avctx->block_align);
845
1668
        if (buf_size < 4 * ch)
846
            return AVERROR_INVALIDDATA;
847
1668
        nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
848
1668
        break;
849
    }
850
2943
    case AV_CODEC_ID_ADPCM_MS:
851
2943
        if (avctx->block_align > 0)
852
2943
            buf_size = FFMIN(buf_size, avctx->block_align);
853
2943
        nb_samples = (buf_size - 6 * ch) * 2 / ch;
854
2943
        break;
855
    case AV_CODEC_ID_ADPCM_MTAF:
856
        if (avctx->block_align > 0)
857
            buf_size = FFMIN(buf_size, avctx->block_align);
858
        nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
859
        break;
860
57
    case AV_CODEC_ID_ADPCM_SBPRO_2:
861
    case AV_CODEC_ID_ADPCM_SBPRO_3:
862
    case AV_CODEC_ID_ADPCM_SBPRO_4:
863
    {
864
        int samples_per_byte;
865

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

8952
    if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
930
        return AVERROR_INVALIDDATA;
931
932
8952
    return nb_samples;
933
}
934
935
37286
static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
936
                              int *got_frame_ptr, AVPacket *avpkt)
937
{
938
37286
    AVFrame *frame     = data;
939
37286
    const uint8_t *buf = avpkt->data;
940
37286
    int buf_size = avpkt->size;
941
37286
    ADPCMDecodeContext *c = avctx->priv_data;
942
    ADPCMChannelStatus *cs;
943
    int n, m, channel, i;
944
    int16_t *samples;
945
    int16_t **samples_p;
946
    int st; /* stereo */
947
    int count1, count2;
948
    int nb_samples, coded_samples, approx_nb_samples, ret;
949
    GetByteContext gb;
950
951
37286
    bytestream2_init(&gb, buf, buf_size);
952
37286
    nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
953
37286
    if (nb_samples <= 0) {
954
        av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
955
        return AVERROR_INVALIDDATA;
956
    }
957
958
    /* get output buffer */
959
37286
    frame->nb_samples = nb_samples;
960
37286
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
961
        return ret;
962
37286
    samples = (int16_t *)frame->data[0];
963
37286
    samples_p = (int16_t **)frame->extended_data;
964
965
    /* use coded_samples when applicable */
966
    /* it is always <= nb_samples, so the output buffer will be large enough */
967
37286
    if (coded_samples) {
968

676
        if (!approx_nb_samples && coded_samples != nb_samples)
969
            av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
970
676
        frame->nb_samples = nb_samples = coded_samples;
971
    }
972
973
37286
    st = avctx->channels == 2 ? 1 : 0;
974
975










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

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

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

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

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

37286
    if (avpkt->size && bytestream2_tell(&gb) == 0) {
2071
        av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2072
        return AVERROR_INVALIDDATA;
2073
    }
2074
2075
37286
    *got_frame_ptr = 1;
2076
2077
37286
    if (avpkt->size < bytestream2_tell(&gb)) {
2078
        av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2079
        return avpkt->size;
2080
    }
2081
2082
37286
    return bytestream2_tell(&gb);
2083
}
2084
2085
static void adpcm_flush(AVCodecContext *avctx)
2086
{
2087
    ADPCMDecodeContext *c = avctx->priv_data;
2088
    c->has_status = 0;
2089
}
2090
2091
2092
static const enum AVSampleFormat sample_fmts_s16[]  = { AV_SAMPLE_FMT_S16,
2093
                                                        AV_SAMPLE_FMT_NONE };
2094
static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
2095
                                                        AV_SAMPLE_FMT_NONE };
2096
static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
2097
                                                        AV_SAMPLE_FMT_S16P,
2098
                                                        AV_SAMPLE_FMT_NONE };
2099
2100
#define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
2101
AVCodec ff_ ## name_ ## _decoder = {                        \
2102
    .name           = #name_,                               \
2103
    .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
2104
    .type           = AVMEDIA_TYPE_AUDIO,                   \
2105
    .id             = id_,                                  \
2106
    .priv_data_size = sizeof(ADPCMDecodeContext),           \
2107
    .init           = adpcm_decode_init,                    \
2108
    .decode         = adpcm_decode_frame,                   \
2109
    .flush          = adpcm_flush,                          \
2110
    .capabilities   = AV_CODEC_CAP_DR1,                     \
2111
    .sample_fmts    = sample_fmts_,                         \
2112
}
2113
2114
/* Note: Do not forget to add new entries to the Makefile as well. */
2115
ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM,         sample_fmts_s16p, adpcm_4xm,         "ADPCM 4X Movie");
2116
ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC,         sample_fmts_s16p, adpcm_afc,         "ADPCM Nintendo Gamecube AFC");
2117
ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM,         sample_fmts_s16,  adpcm_agm,         "ADPCM AmuseGraphics Movie");
2118
ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA,        sample_fmts_s16p, adpcm_aica,        "ADPCM Yamaha AICA");
2119
ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO,        sample_fmts_s16p, adpcm_argo,        "ADPCM Argonaut Games");
2120
ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT,          sample_fmts_s16,  adpcm_ct,          "ADPCM Creative Technology");
2121
ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK,         sample_fmts_s16p, adpcm_dtk,         "ADPCM Nintendo Gamecube DTK");
2122
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA,          sample_fmts_s16,  adpcm_ea,          "ADPCM Electronic Arts");
2123
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16,  adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
2124
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1,       sample_fmts_s16p, adpcm_ea_r1,       "ADPCM Electronic Arts R1");
2125
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2,       sample_fmts_s16p, adpcm_ea_r2,       "ADPCM Electronic Arts R2");
2126
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3,       sample_fmts_s16p, adpcm_ea_r3,       "ADPCM Electronic Arts R3");
2127
ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS,      sample_fmts_s16p, adpcm_ea_xas,      "ADPCM Electronic Arts XAS");
2128
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV,     sample_fmts_s16,  adpcm_ima_amv,     "ADPCM IMA AMV");
2129
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC,     sample_fmts_s16,  adpcm_ima_apc,     "ADPCM IMA CRYO APC");
2130
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM,     sample_fmts_s16,  adpcm_ima_apm,     "ADPCM IMA Ubisoft APM");
2131
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_CUNNING, sample_fmts_s16,  adpcm_ima_cunning, "ADPCM IMA Cunning Developments");
2132
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4,    sample_fmts_s16,  adpcm_ima_dat4,    "ADPCM IMA Eurocom DAT4");
2133
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3,     sample_fmts_s16,  adpcm_ima_dk3,     "ADPCM IMA Duck DK3");
2134
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4,     sample_fmts_s16,  adpcm_ima_dk4,     "ADPCM IMA Duck DK4");
2135
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16,  adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
2136
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16,  adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
2137
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS,     sample_fmts_s16,  adpcm_ima_iss,     "ADPCM IMA Funcom ISS");
2138
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MOFLEX,  sample_fmts_s16p, adpcm_ima_moflex,  "ADPCM IMA MobiClip MOFLEX");
2139
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MTF,     sample_fmts_s16,  adpcm_ima_mtf,     "ADPCM IMA Capcom's MT Framework");
2140
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI,     sample_fmts_s16,  adpcm_ima_oki,     "ADPCM IMA Dialogic OKI");
2141
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT,      sample_fmts_s16p, adpcm_ima_qt,      "ADPCM IMA QuickTime");
2142
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD,     sample_fmts_s16,  adpcm_ima_rad,     "ADPCM IMA Radical");
2143
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SSI,     sample_fmts_s16,  adpcm_ima_ssi,     "ADPCM IMA Simon & Schuster Interactive");
2144
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG,  sample_fmts_s16,  adpcm_ima_smjpeg,  "ADPCM IMA Loki SDL MJPEG");
2145
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ALP,     sample_fmts_s16,  adpcm_ima_alp,     "ADPCM IMA High Voltage Software ALP");
2146
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV,     sample_fmts_s16p, adpcm_ima_wav,     "ADPCM IMA WAV");
2147
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS,      sample_fmts_both, adpcm_ima_ws,      "ADPCM IMA Westwood");
2148
ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS,          sample_fmts_both, adpcm_ms,          "ADPCM Microsoft");
2149
ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF,        sample_fmts_s16p, adpcm_mtaf,        "ADPCM MTAF");
2150
ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX,         sample_fmts_s16p, adpcm_psx,         "ADPCM Playstation");
2151
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2,     sample_fmts_s16,  adpcm_sbpro_2,     "ADPCM Sound Blaster Pro 2-bit");
2152
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3,     sample_fmts_s16,  adpcm_sbpro_3,     "ADPCM Sound Blaster Pro 2.6-bit");
2153
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4,     sample_fmts_s16,  adpcm_sbpro_4,     "ADPCM Sound Blaster Pro 4-bit");
2154
ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF,         sample_fmts_s16,  adpcm_swf,         "ADPCM Shockwave Flash");
2155
ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE,      sample_fmts_s16p, adpcm_thp_le,      "ADPCM Nintendo THP (little-endian)");
2156
ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP,         sample_fmts_s16p, adpcm_thp,         "ADPCM Nintendo THP");
2157
ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA,          sample_fmts_s16p, adpcm_xa,          "ADPCM CDROM XA");
2158
ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA,      sample_fmts_s16,  adpcm_yamaha,      "ADPCM Yamaha");
2159
ADPCM_DECODER(AV_CODEC_ID_ADPCM_ZORK,        sample_fmts_s16,  adpcm_zork,        "ADPCM Zork");