GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/adpcm.c Lines: 937 1301 72.0 %
Date: 2021-01-21 21:11:50 Branches: 438 668 65.6 %

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

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

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


181
    switch(avctx->codec->id) {
154
2
    case AV_CODEC_ID_ADPCM_CT:
155
2
        c->status[0].step = c->status[1].step = 511;
156
2
        break;
157
10
    case AV_CODEC_ID_ADPCM_IMA_WAV:
158

10
        if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
159
            return AVERROR_INVALIDDATA;
160
10
        break;
161
3
    case AV_CODEC_ID_ADPCM_IMA_APC:
162

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

4
        if (avctx->extradata && avctx->extradata_size >= 2)
184
3
            c->vqa_version = AV_RL16(avctx->extradata);
185
4
        break;
186
6
    case AV_CODEC_ID_ADPCM_ARGO:
187

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

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

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

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

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

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

37286
    switch (avctx->codec->id) {
740
    /* constant, only check buf_size */
741
600
    case AV_CODEC_ID_ADPCM_EA_XAS:
742
600
        if (buf_size < 76 * ch)
743
            return 0;
744
600
        nb_samples = 128;
745
600
        break;
746
26342
    case AV_CODEC_ID_ADPCM_IMA_QT:
747
26342
        if (buf_size < 34 * ch)
748
            return 0;
749
26342
        nb_samples = 64;
750
26342
        break;
751
    /* simple 4-bit adpcm */
752
801
    case AV_CODEC_ID_ADPCM_CT:
753
    case AV_CODEC_ID_ADPCM_IMA_APC:
754
    case AV_CODEC_ID_ADPCM_IMA_CUNNING:
755
    case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
756
    case AV_CODEC_ID_ADPCM_IMA_OKI:
757
    case AV_CODEC_ID_ADPCM_IMA_WS:
758
    case AV_CODEC_ID_ADPCM_YAMAHA:
759
    case AV_CODEC_ID_ADPCM_AICA:
760
    case AV_CODEC_ID_ADPCM_IMA_SSI:
761
    case AV_CODEC_ID_ADPCM_IMA_APM:
762
    case AV_CODEC_ID_ADPCM_IMA_ALP:
763
    case AV_CODEC_ID_ADPCM_IMA_MTF:
764
801
        nb_samples = buf_size * 2 / ch;
765
801
        break;
766
    }
767
37286
    if (nb_samples)
768
27743
        return nb_samples;
769
770
    /* simple 4-bit adpcm, with header */
771
9543
    header_size = 0;
772
9543
    switch (avctx->codec->id) {
773
145
        case AV_CODEC_ID_ADPCM_4XM:
774
        case AV_CODEC_ID_ADPCM_AGM:
775
        case AV_CODEC_ID_ADPCM_IMA_DAT4:
776
        case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
777
145
        case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
778
350
        case AV_CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4 * ch;      break;
779
    }
780
9543
    if (header_size > 0)
781
495
        return (buf_size - header_size) * 2 / ch;
782
783
    /* more complex formats */
784





9048
    switch (avctx->codec->id) {
785
161
    case AV_CODEC_ID_ADPCM_IMA_AMV:
786
161
        bytestream2_skip(gb, 4);
787
161
        has_coded_samples  = 1;
788
161
        *coded_samples     = bytestream2_get_le32u(gb);
789
161
        nb_samples         = FFMIN((buf_size - 8) * 2, *coded_samples);
790
161
        bytestream2_seek(gb, -8, SEEK_CUR);
791
161
        break;
792
158
    case AV_CODEC_ID_ADPCM_EA:
793
158
        has_coded_samples = 1;
794
158
        *coded_samples  = bytestream2_get_le32(gb);
795
158
        *coded_samples -= *coded_samples % 28;
796
158
        nb_samples      = (buf_size - 12) / 30 * 28;
797
158
        break;
798
47
    case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
799
47
        has_coded_samples = 1;
800
47
        *coded_samples = bytestream2_get_le32(gb);
801
47
        nb_samples     = (buf_size - (4 + 8 * ch)) * 2 / ch;
802
47
        break;
803
30
    case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
804
30
        nb_samples = (buf_size - ch) / ch * 2;
805
30
        break;
806
400
    case AV_CODEC_ID_ADPCM_EA_R1:
807
    case AV_CODEC_ID_ADPCM_EA_R2:
808
    case AV_CODEC_ID_ADPCM_EA_R3:
809
        /* maximum number of samples */
810
        /* has internal offsets and a per-frame switch to signal raw 16-bit */
811
400
        has_coded_samples = 1;
812

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

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

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

837
        if (!approx_nb_samples && coded_samples != nb_samples)
978
            av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
979
837
        frame->nb_samples = nb_samples = coded_samples;
980
    }
981
982
37286
    st = avctx->channels == 2 ? 1 : 0;
983
984










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

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

12
        if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1831
            samples_per_block = avctx->extradata[0] / 16;
1832
            blocks = nb_samples / avctx->extradata[0];
1833
        } else {
1834
12
            samples_per_block = nb_samples / 16;
1835
12
            blocks = 1;
1836
        }
1837
1838
24
        for (m = 0; m < blocks; m++) {
1839
36
        for (channel = 0; channel < avctx->channels; channel++) {
1840
24
            int prev1 = c->status[channel].sample1;
1841
24
            int prev2 = c->status[channel].sample2;
1842
1843
24
            samples = samples_p[channel] + m * 16;
1844
            /* Read in every sample for this channel.  */
1845
26904
            for (i = 0; i < samples_per_block; i++) {
1846
26880
                int byte = bytestream2_get_byteu(&gb);
1847
26880
                int scale = 1 << (byte >> 4);
1848
26880
                int index = byte & 0xf;
1849
26880
                int factor1 = ff_adpcm_afc_coeffs[0][index];
1850
26880
                int factor2 = ff_adpcm_afc_coeffs[1][index];
1851
1852
                /* Decode 16 samples.  */
1853
456960
                for (n = 0; n < 16; n++) {
1854
                    int32_t sampledat;
1855
1856
430080
                    if (n & 1) {
1857
215040
                        sampledat = sign_extend(byte, 4);
1858
                    } else {
1859
215040
                        byte = bytestream2_get_byteu(&gb);
1860
215040
                        sampledat = sign_extend(byte >> 4, 4);
1861
                    }
1862
1863
430080
                    sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
1864
430080
                                sampledat * scale;
1865
430080
                    *samples = av_clip_int16(sampledat);
1866
430080
                    prev2 = prev1;
1867
430080
                    prev1 = *samples++;
1868
                }
1869
            }
1870
1871
24
            c->status[channel].sample1 = prev1;
1872
24
            c->status[channel].sample2 = prev2;
1873
        }
1874
        }
1875
12
        bytestream2_seek(&gb, 0, SEEK_END);
1876
12
        break;
1877
    }
1878
71
    case AV_CODEC_ID_ADPCM_THP:
1879
    case AV_CODEC_ID_ADPCM_THP_LE:
1880
    {
1881
        int table[14][16];
1882
        int ch;
1883
1884
#define THP_GET16(g) \
1885
    sign_extend( \
1886
        avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1887
        bytestream2_get_le16u(&(g)) : \
1888
        bytestream2_get_be16u(&(g)), 16)
1889
1890
71
        if (avctx->extradata) {
1891
            GetByteContext tb;
1892
            if (avctx->extradata_size < 32 * avctx->channels) {
1893
                av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1894
                return AVERROR_INVALIDDATA;
1895
            }
1896
1897
            bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1898
            for (i = 0; i < avctx->channels; i++)
1899
                for (n = 0; n < 16; n++)
1900
                    table[i][n] = THP_GET16(tb);
1901
        } else {
1902
213
            for (i = 0; i < avctx->channels; i++)
1903
2414
                for (n = 0; n < 16; n++)
1904
2272
                    table[i][n] = THP_GET16(gb);
1905
1906
71
            if (!c->has_status) {
1907
                /* Initialize the previous sample.  */
1908
3
                for (i = 0; i < avctx->channels; i++) {
1909
2
                    c->status[i].sample1 = THP_GET16(gb);
1910
2
                    c->status[i].sample2 = THP_GET16(gb);
1911
                }
1912
1
                c->has_status = 1;
1913
            } else {
1914
70
                bytestream2_skip(&gb, avctx->channels * 4);
1915
            }
1916
        }
1917
1918
213
        for (ch = 0; ch < avctx->channels; ch++) {
1919
142
            samples = samples_p[ch];
1920
1921
            /* Read in every sample for this channel.  */
1922
10982
            for (i = 0; i < (nb_samples + 13) / 14; i++) {
1923
10840
                int byte = bytestream2_get_byteu(&gb);
1924
10840
                int index = (byte >> 4) & 7;
1925
10840
                unsigned int exp = byte & 0x0F;
1926
10840
                int64_t factor1 = table[ch][index * 2];
1927
10840
                int64_t factor2 = table[ch][index * 2 + 1];
1928
1929
                /* Decode 14 samples.  */
1930

162600
                for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1931
                    int32_t sampledat;
1932
1933
151760
                    if (n & 1) {
1934
75880
                        sampledat = sign_extend(byte, 4);
1935
                    } else {
1936
75880
                        byte = bytestream2_get_byteu(&gb);
1937
75880
                        sampledat = sign_extend(byte >> 4, 4);
1938
                    }
1939
1940
151760
                    sampledat = ((c->status[ch].sample1 * factor1
1941
151760
                                + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
1942
151760
                    *samples = av_clip_int16(sampledat);
1943
151760
                    c->status[ch].sample2 = c->status[ch].sample1;
1944
151760
                    c->status[ch].sample1 = *samples++;
1945
                }
1946
            }
1947
        }
1948
71
        break;
1949
    }
1950
32
    case AV_CODEC_ID_ADPCM_DTK:
1951
96
        for (channel = 0; channel < avctx->channels; channel++) {
1952
64
            samples = samples_p[channel];
1953
1954
            /* Read in every sample for this channel.  */
1955
2112
            for (i = 0; i < nb_samples / 28; i++) {
1956
                int byte, header;
1957
2048
                if (channel)
1958
1024
                    bytestream2_skipu(&gb, 1);
1959
2048
                header = bytestream2_get_byteu(&gb);
1960
2048
                bytestream2_skipu(&gb, 3 - channel);
1961
1962
                /* Decode 28 samples.  */
1963
59392
                for (n = 0; n < 28; n++) {
1964
                    int32_t sampledat, prev;
1965
1966

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

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