GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/adpcmenc.c Lines: 437 499 87.6 %
Date: 2020-11-28 20:53:16 Branches: 264 323 81.7 %

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
 *
8
 * This file is part of FFmpeg.
9
 *
10
 * FFmpeg is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * FFmpeg is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with FFmpeg; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24
25
#include "libavutil/opt.h"
26
27
#include "avcodec.h"
28
#include "put_bits.h"
29
#include "bytestream.h"
30
#include "adpcm.h"
31
#include "adpcm_data.h"
32
#include "internal.h"
33
34
/**
35
 * @file
36
 * ADPCM encoders
37
 * See ADPCM decoder reference documents for codec information.
38
 */
39
40
typedef struct TrellisPath {
41
    int nibble;
42
    int prev;
43
} TrellisPath;
44
45
typedef struct TrellisNode {
46
    uint32_t ssd;
47
    int path;
48
    int sample1;
49
    int sample2;
50
    int step;
51
} TrellisNode;
52
53
typedef struct ADPCMEncodeContext {
54
    AVClass *class;
55
    int block_size;
56
57
    ADPCMChannelStatus status[6];
58
    TrellisPath *paths;
59
    TrellisNode *node_buf;
60
    TrellisNode **nodep_buf;
61
    uint8_t *trellis_hash;
62
} ADPCMEncodeContext;
63
64
#define FREEZE_INTERVAL 128
65
66
16
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
67
{
68
16
    ADPCMEncodeContext *s = avctx->priv_data;
69
    uint8_t *extradata;
70
    int i;
71
72
16
    if (avctx->channels > 2) {
73
        av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
74
        return AVERROR(EINVAL);
75
    }
76
77
    /*
78
     * AMV's block size has to match that of the corresponding video
79
     * stream. Relax the POT requirement.
80
     */
81
16
    if (avctx->codec->id != AV_CODEC_ID_ADPCM_IMA_AMV &&
82
16
        (s->block_size & (s->block_size - 1))) {
83
        av_log(avctx, AV_LOG_ERROR, "block size must be power of 2\n");
84
        return AVERROR(EINVAL);
85
    }
86
87
16
    if (avctx->trellis) {
88
        int frontier, max_paths;
89
90
5
        if ((unsigned)avctx->trellis > 16U) {
91
            av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
92
            return AVERROR(EINVAL);
93
        }
94
95
5
        if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
96
5
            avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
97
5
            avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO) {
98
            /*
99
             * The current trellis implementation doesn't work for extended
100
             * runs of samples without periodic resets. Disallow it.
101
             */
102
            av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
103
            return AVERROR_PATCHWELCOME;
104
        }
105
106
5
        frontier  = 1 << avctx->trellis;
107
5
        max_paths =  frontier * FREEZE_INTERVAL;
108
5
        if (!FF_ALLOC_TYPED_ARRAY(s->paths,        max_paths)    ||
109
5
            !FF_ALLOC_TYPED_ARRAY(s->node_buf,     2 * frontier) ||
110
5
            !FF_ALLOC_TYPED_ARRAY(s->nodep_buf,    2 * frontier) ||
111
5
            !FF_ALLOC_TYPED_ARRAY(s->trellis_hash, 65536))
112
            return AVERROR(ENOMEM);
113
    }
114
115
16
    avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
116
117


16
    switch (avctx->codec->id) {
118
2
    case AV_CODEC_ID_ADPCM_IMA_WAV:
119
        /* each 16 bits sample gives one nibble
120
           and we have 4 bytes per channel overhead */
121
2
        avctx->frame_size = (s->block_size - 4 * avctx->channels) * 8 /
122
2
                            (4 * avctx->channels) + 1;
123
        /* seems frame_size isn't taken into account...
124
           have to buffer the samples :-( */
125
2
        avctx->block_align = s->block_size;
126
2
        avctx->bits_per_coded_sample = 4;
127
2
        break;
128
2
    case AV_CODEC_ID_ADPCM_IMA_QT:
129
2
        avctx->frame_size  = 64;
130
2
        avctx->block_align = 34 * avctx->channels;
131
2
        break;
132
2
    case AV_CODEC_ID_ADPCM_MS:
133
        /* each 16 bits sample gives one nibble
134
           and we have 7 bytes per channel overhead */
135
2
        avctx->frame_size = (s->block_size - 7 * avctx->channels) * 2 / avctx->channels + 2;
136
2
        avctx->bits_per_coded_sample = 4;
137
2
        avctx->block_align     = s->block_size;
138
2
        if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
139
            return AVERROR(ENOMEM);
140
2
        avctx->extradata_size = 32;
141
2
        extradata = avctx->extradata;
142
2
        bytestream_put_le16(&extradata, avctx->frame_size);
143
2
        bytestream_put_le16(&extradata, 7); /* wNumCoef */
144
16
        for (i = 0; i < 7; i++) {
145
14
            bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
146
14
            bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
147
        }
148
2
        break;
149
3
    case AV_CODEC_ID_ADPCM_YAMAHA:
150
3
        avctx->frame_size  = s->block_size * 2 / avctx->channels;
151
3
        avctx->block_align = s->block_size;
152
3
        break;
153
3
    case AV_CODEC_ID_ADPCM_SWF:
154
3
        if (avctx->sample_rate != 11025 &&
155
3
            avctx->sample_rate != 22050 &&
156
3
            avctx->sample_rate != 44100) {
157
            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
158
                   "22050 or 44100\n");
159
            return AVERROR(EINVAL);
160
        }
161
3
        avctx->frame_size  = 4096; /* Hardcoded according to the SWF spec. */
162
3
        avctx->block_align = (2 + avctx->channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
163
3
        break;
164
2
    case AV_CODEC_ID_ADPCM_IMA_SSI:
165
    case AV_CODEC_ID_ADPCM_IMA_ALP:
166
2
        avctx->frame_size  = s->block_size * 2 / avctx->channels;
167
2
        avctx->block_align = s->block_size;
168
2
        break;
169
    case AV_CODEC_ID_ADPCM_IMA_AMV:
170
        if (avctx->sample_rate != 22050) {
171
            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
172
            return AVERROR(EINVAL);
173
        }
174
175
        if (avctx->channels != 1) {
176
            av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
177
            return AVERROR(EINVAL);
178
        }
179
180
        avctx->frame_size  = s->block_size;
181
        avctx->block_align = 8 + (FFALIGN(avctx->frame_size, 2) / 2);
182
        break;
183
1
    case AV_CODEC_ID_ADPCM_IMA_APM:
184
1
        avctx->frame_size  = s->block_size * 2 / avctx->channels;
185
1
        avctx->block_align = s->block_size;
186
187
1
        if (!(avctx->extradata = av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
188
            return AVERROR(ENOMEM);
189
1
        avctx->extradata_size = 28;
190
1
        break;
191
1
    case AV_CODEC_ID_ADPCM_ARGO:
192
1
        avctx->frame_size = 32;
193
1
        avctx->block_align = 17 * avctx->channels;
194
1
        break;
195
    default:
196
        return AVERROR(EINVAL);
197
    }
198
199
16
    return 0;
200
}
201
202
16
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
203
{
204
16
    ADPCMEncodeContext *s = avctx->priv_data;
205
16
    av_freep(&s->paths);
206
16
    av_freep(&s->node_buf);
207
16
    av_freep(&s->nodep_buf);
208
16
    av_freep(&s->trellis_hash);
209
210
16
    return 0;
211
}
212
213
214
1595052
static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
215
                                                int16_t sample)
216
{
217
1595052
    int delta  = sample - c->prev_sample;
218
1595052
    int nibble = FFMIN(7, abs(delta) * 4 /
219
1595052
                       ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
220
1595052
    c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
221
1595052
                        ff_adpcm_yamaha_difflookup[nibble]) / 8);
222
1595052
    c->prev_sample = av_clip_int16(c->prev_sample);
223
1595052
    c->step_index  = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
224
1595052
    return nibble;
225
}
226
227
529200
static inline uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)
228
{
229
529200
    const int delta  = sample - c->prev_sample;
230
529200
    const int step   = ff_adpcm_step_table[c->step_index];
231
529200
    const int sign   = (delta < 0) * 8;
232
233
529200
    int nibble = FFMIN(abs(delta) * 4 / step, 7);
234
529200
    int diff   = (step * nibble) >> 2;
235
529200
    if (sign)
236
263832
        diff = -diff;
237
238
529200
    nibble = sign | nibble;
239
240
529200
    c->prev_sample += diff;
241
529200
    c->prev_sample  = av_clip_int16(c->prev_sample);
242
529200
    c->step_index   = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
243
529200
    return nibble;
244
}
245
246
1587680
static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
247
                                                   int16_t sample)
248
{
249
1587680
    int delta  = sample - c->prev_sample;
250
1587680
    int diff, step = ff_adpcm_step_table[c->step_index];
251
1587680
    int nibble = 8*(delta < 0);
252
253
1587680
    delta= abs(delta);
254
1587680
    diff = delta + (step >> 3);
255
256
1587680
    if (delta >= step) {
257
435880
        nibble |= 4;
258
435880
        delta  -= step;
259
    }
260
1587680
    step >>= 1;
261
1587680
    if (delta >= step) {
262
760258
        nibble |= 2;
263
760258
        delta  -= step;
264
    }
265
1587680
    step >>= 1;
266
1587680
    if (delta >= step) {
267
770157
        nibble |= 1;
268
770157
        delta  -= step;
269
    }
270
1587680
    diff -= delta;
271
272
1587680
    if (nibble & 8)
273
789018
        c->prev_sample -= diff;
274
    else
275
798662
        c->prev_sample += diff;
276
277
1587680
    c->prev_sample = av_clip_int16(c->prev_sample);
278
1587680
    c->step_index  = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
279
280
1587680
    return nibble;
281
}
282
283
529240
static inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
284
                                               int16_t sample)
285
{
286
    int predictor, nibble, bias;
287
288
529240
    predictor = (((c->sample1) * (c->coeff1)) +
289
529240
                (( c->sample2) * (c->coeff2))) / 64;
290
291
529240
    nibble = sample - predictor;
292
529240
    if (nibble >= 0)
293
265224
        bias =  c->idelta / 2;
294
    else
295
264016
        bias = -c->idelta / 2;
296
297
529240
    nibble = (nibble + bias) / c->idelta;
298
529240
    nibble = av_clip_intp2(nibble, 3) & 0x0F;
299
300
529240
    predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
301
302
529240
    c->sample2 = c->sample1;
303
529240
    c->sample1 = av_clip_int16(predictor);
304
305
529240
    c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
306
529240
    if (c->idelta < 16)
307
28193
        c->idelta = 16;
308
309
529240
    return nibble;
310
}
311
312
575488
static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
313
                                                   int16_t sample)
314
{
315
    int nibble, delta;
316
317
575488
    if (!c->step) {
318
3
        c->predictor = 0;
319
3
        c->step      = 127;
320
    }
321
322
575488
    delta = sample - c->predictor;
323
324
575488
    nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
325
326
575488
    c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
327
575488
    c->predictor = av_clip_int16(c->predictor);
328
575488
    c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
329
575488
    c->step = av_clip(c->step, 127, 24576);
330
331
575488
    return nibble;
332
}
333
334
9964
static void adpcm_compress_trellis(AVCodecContext *avctx,
335
                                   const int16_t *samples, uint8_t *dst,
336
                                   ADPCMChannelStatus *c, int n, int stride)
337
{
338
    //FIXME 6% faster if frontier is a compile-time constant
339
9964
    ADPCMEncodeContext *s = avctx->priv_data;
340
9964
    const int frontier = 1 << avctx->trellis;
341
9964
    const int version  = avctx->codec->id;
342
9964
    TrellisPath *paths       = s->paths, *p;
343
9964
    TrellisNode *node_buf    = s->node_buf;
344
9964
    TrellisNode **nodep_buf  = s->nodep_buf;
345
9964
    TrellisNode **nodes      = nodep_buf; // nodes[] is always sorted by .ssd
346
9964
    TrellisNode **nodes_next = nodep_buf + frontier;
347
9964
    int pathn = 0, froze = -1, i, j, k, generation = 0;
348
9964
    uint8_t *hash = s->trellis_hash;
349
9964
    memset(hash, 0xff, 65536 * sizeof(*hash));
350
351
9964
    memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
352
9964
    nodes[0]          = node_buf + frontier;
353
9964
    nodes[0]->ssd     = 0;
354
9964
    nodes[0]->path    = 0;
355
9964
    nodes[0]->step    = c->step_index;
356
9964
    nodes[0]->sample1 = c->sample1;
357
9964
    nodes[0]->sample2 = c->sample2;
358

9964
    if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
359
1172
        version == AV_CODEC_ID_ADPCM_IMA_QT  ||
360
1172
        version == AV_CODEC_ID_ADPCM_IMA_AMV ||
361
        version == AV_CODEC_ID_ADPCM_SWF)
362
8922
        nodes[0]->sample1 = c->prev_sample;
363
9964
    if (version == AV_CODEC_ID_ADPCM_MS)
364
524
        nodes[0]->step = c->idelta;
365
9964
    if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
366
518
        if (c->step == 0) {
367
2
            nodes[0]->step    = 127;
368
2
            nodes[0]->sample1 = 0;
369
        } else {
370
516
            nodes[0]->step    = c->step;
371
516
            nodes[0]->sample1 = c->predictor;
372
        }
373
    }
374
375
2661618
    for (i = 0; i < n; i++) {
376
2651654
        TrellisNode *t = node_buf + frontier*(i&1);
377
        TrellisNode **u;
378
2651654
        int sample   = samples[i * stride];
379
2651654
        int heap_pos = 0;
380
2651654
        memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
381

83557580
        for (j = 0; j < frontier && nodes[j]; j++) {
382
            // higher j have higher ssd already, so they're likely
383
            // to yield a suboptimal next sample too
384
80905926
            const int range = (j < frontier / 2) ? 1 : 0;
385
80905926
            const int step  = nodes[j]->step;
386
            int nidx;
387
80905926
            if (version == AV_CODEC_ID_ADPCM_MS) {
388
15997120
                const int predictor = ((nodes[j]->sample1 * c->coeff1) +
389
15997120
                                       (nodes[j]->sample2 * c->coeff2)) / 64;
390
15997120
                const int div  = (sample - predictor) / step;
391
15997120
                const int nmin = av_clip(div-range, -8, 6);
392
15997120
                const int nmax = av_clip(div+range, -7, 7);
393
48245314
                for (nidx = nmin; nidx <= nmax; nidx++) {
394
32248194
                    const int nibble = nidx & 0xf;
395
32248194
                    int dec_sample   = predictor + nidx * step;
396
#define STORE_NODE(NAME, STEP_INDEX)\
397
                    int d;\
398
                    uint32_t ssd;\
399
                    int pos;\
400
                    TrellisNode *u;\
401
                    uint8_t *h;\
402
                    dec_sample = av_clip_int16(dec_sample);\
403
                    d = sample - dec_sample;\
404
                    ssd = nodes[j]->ssd + d*(unsigned)d;\
405
                    /* Check for wraparound, skip such samples completely. \
406
                     * Note, changing ssd to a 64 bit variable would be \
407
                     * simpler, avoiding this check, but it's slower on \
408
                     * x86 32 bit at the moment. */\
409
                    if (ssd < nodes[j]->ssd)\
410
                        goto next_##NAME;\
411
                    /* Collapse any two states with the same previous sample value. \
412
                     * One could also distinguish states by step and by 2nd to last
413
                     * sample, but the effects of that are negligible.
414
                     * Since nodes in the previous generation are iterated
415
                     * through a heap, they're roughly ordered from better to
416
                     * worse, but not strictly ordered. Therefore, an earlier
417
                     * node with the same sample value is better in most cases
418
                     * (and thus the current is skipped), but not strictly
419
                     * in all cases. Only skipping samples where ssd >=
420
                     * ssd of the earlier node with the same sample gives
421
                     * slightly worse quality, though, for some reason. */ \
422
                    h = &hash[(uint16_t) dec_sample];\
423
                    if (*h == generation)\
424
                        goto next_##NAME;\
425
                    if (heap_pos < frontier) {\
426
                        pos = heap_pos++;\
427
                    } else {\
428
                        /* Try to replace one of the leaf nodes with the new \
429
                         * one, but try a different slot each time. */\
430
                        pos = (frontier >> 1) +\
431
                              (heap_pos & ((frontier >> 1) - 1));\
432
                        if (ssd > nodes_next[pos]->ssd)\
433
                            goto next_##NAME;\
434
                        heap_pos++;\
435
                    }\
436
                    *h = generation;\
437
                    u  = nodes_next[pos];\
438
                    if (!u) {\
439
                        av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
440
                        u = t++;\
441
                        nodes_next[pos] = u;\
442
                        u->path = pathn++;\
443
                    }\
444
                    u->ssd  = ssd;\
445
                    u->step = STEP_INDEX;\
446
                    u->sample2 = nodes[j]->sample1;\
447
                    u->sample1 = dec_sample;\
448
                    paths[u->path].nibble = nibble;\
449
                    paths[u->path].prev   = nodes[j]->path;\
450
                    /* Sift the newly inserted node up in the heap to \
451
                     * restore the heap property. */\
452
                    while (pos > 0) {\
453
                        int parent = (pos - 1) >> 1;\
454
                        if (nodes_next[parent]->ssd <= ssd)\
455
                            break;\
456
                        FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
457
                        pos = parent;\
458
                    }\
459
                    next_##NAME:;
460



44471491
                    STORE_NODE(ms, FFMAX(16,
461
                               (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
462
                }
463

64908806
            } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
464
32826778
                       version == AV_CODEC_ID_ADPCM_IMA_QT  ||
465
32826778
                       version == AV_CODEC_ID_ADPCM_IMA_AMV ||
466
48307603
                       version == AV_CODEC_ID_ADPCM_SWF) {
467
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
468
                const int predictor = nodes[j]->sample1;\
469
                const int div = (sample - predictor) * 4 / STEP_TABLE;\
470
                int nmin = av_clip(div - range, -7, 6);\
471
                int nmax = av_clip(div + range, -6, 7);\
472
                if (nmin <= 0)\
473
                    nmin--; /* distinguish -0 from +0 */\
474
                if (nmax < 0)\
475
                    nmax--;\
476
                for (nidx = nmin; nidx <= nmax; nidx++) {\
477
                    const int nibble = nidx < 0 ? 7 - nidx : nidx;\
478
                    int dec_sample = predictor +\
479
                                    (STEP_TABLE *\
480
                                     ff_adpcm_yamaha_difflookup[nibble]) / 8;\
481
                    STORE_NODE(NAME, STEP_INDEX);\
482
                }
483





196436595
                LOOP_NODES(ima, ff_adpcm_step_table[step],
484
                           av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
485
            } else { //AV_CODEC_ID_ADPCM_YAMAHA
486





67079772
                LOOP_NODES(yamaha, step,
487
                           av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
488
                                   127, 24576));
489
#undef LOOP_NODES
490
#undef STORE_NODE
491
            }
492
        }
493
494
2651654
        u = nodes;
495
2651654
        nodes = nodes_next;
496
2651654
        nodes_next = u;
497
498
2651654
        generation++;
499
2651654
        if (generation == 255) {
500
7290
            memset(hash, 0xff, 65536 * sizeof(*hash));
501
7290
            generation = 0;
502
        }
503
504
        // prevent overflow
505
2651654
        if (nodes[0]->ssd > (1 << 28)) {
506

152412
            for (j = 1; j < frontier && nodes[j]; j++)
507
147555
                nodes[j]->ssd -= nodes[0]->ssd;
508
4857
            nodes[0]->ssd = 0;
509
        }
510
511
        // merge old paths to save memory
512
2651654
        if (i == froze + FREEZE_INTERVAL) {
513
15496
            p = &paths[nodes[0]->path];
514
1998984
            for (k = i; k > froze; k--) {
515
1983488
                dst[k] = p->nibble;
516
1983488
                p = &paths[p->prev];
517
            }
518
15496
            froze = i;
519
15496
            pathn = 0;
520
            // other nodes might use paths that don't coincide with the frozen one.
521
            // checking which nodes do so is too slow, so just kill them all.
522
            // this also slightly improves quality, but I don't know why.
523
15496
            memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
524
        }
525
    }
526
527
9964
    p = &paths[nodes[0]->path];
528
678130
    for (i = n - 1; i > froze; i--) {
529
668166
        dst[i] = p->nibble;
530
668166
        p = &paths[p->prev];
531
    }
532
533
9964
    c->predictor  = nodes[0]->sample1;
534
9964
    c->sample1    = nodes[0]->sample1;
535
9964
    c->sample2    = nodes[0]->sample2;
536
9964
    c->step_index = nodes[0]->step;
537
9964
    c->step       = nodes[0]->step;
538
9964
    c->idelta     = nodes[0]->step;
539
9964
}
540
541
16840928
static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
542
                                             int shift, int flag)
543
{
544
    int nibble;
545
546
16840928
    if (flag)
547
8538976
        nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
548
    else
549
8301952
        nibble = 4 * s - 4 * cs->sample1;
550
551
16840928
    return (nibble >> shift) & 0x0F;
552
}
553
554
526279
static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
555
                                         const int16_t *samples, int nsamples,
556
                                         int shift, int flag)
557
{
558
526279
    int64_t error = 0;
559
560
526279
    if (pb) {
561
16538
        put_bits(pb, 4, shift - 2);
562
16538
        put_bits(pb, 1, 0);
563
16538
        put_bits(pb, 1, !!flag);
564
16538
        put_bits(pb, 2, 0);
565
    }
566
567
17367207
    for (int n = 0; n < nsamples; n++) {
568
        /* Compress the nibble, then expand it to see how much precision we've lost. */
569
16840928
        int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
570
16840928
        int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
571
572
16840928
        error += abs(samples[n] - sample);
573
574
16840928
        if (pb)
575
529216
            put_bits(pb, 4, nibble);
576
    }
577
578
526279
    return error;
579
}
580
581
19097
static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
582
                              const AVFrame *frame, int *got_packet_ptr)
583
{
584
    int n, i, ch, st, pkt_size, ret;
585
    const int16_t *samples;
586
    int16_t **samples_p;
587
    uint8_t *dst;
588
19097
    ADPCMEncodeContext *c = avctx->priv_data;
589
    uint8_t *buf;
590
591
19097
    samples = (const int16_t *)frame->data[0];
592
19097
    samples_p = (int16_t **)frame->extended_data;
593
19097
    st = avctx->channels == 2;
594
595
19097
    if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
596
18838
        avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
597
18579
        avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM)
598
777
        pkt_size = (frame->nb_samples * avctx->channels) / 2;
599
    else
600
18320
        pkt_size = avctx->block_align;
601
19097
    if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
602
        return ret;
603
19097
    dst = avpkt->data;
604
605


19097
    switch(avctx->codec->id) {
606
522
    case AV_CODEC_ID_ADPCM_IMA_WAV:
607
    {
608
        int blocks, j;
609
610
522
        blocks = (frame->nb_samples - 1) / 8;
611
612
1566
        for (ch = 0; ch < avctx->channels; ch++) {
613
1044
            ADPCMChannelStatus *status = &c->status[ch];
614
1044
            status->prev_sample = samples_p[ch][0];
615
            /* status->step_index = 0;
616
               XXX: not sure how to init the state machine */
617
1044
            bytestream_put_le16(&dst, status->prev_sample);
618
1044
            *dst++ = status->step_index;
619
1044
            *dst++ = 0; /* unknown */
620
        }
621
622
        /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
623
522
        if (avctx->trellis > 0) {
624
261
            if (!FF_ALLOC_TYPED_ARRAY(buf, avctx->channels * blocks * 8))
625
                return AVERROR(ENOMEM);
626
783
            for (ch = 0; ch < avctx->channels; ch++) {
627
522
                adpcm_compress_trellis(avctx, &samples_p[ch][1],
628
522
                                       buf + ch * blocks * 8, &c->status[ch],
629
                                       blocks * 8, 1);
630
            }
631
33408
            for (i = 0; i < blocks; i++) {
632
99441
                for (ch = 0; ch < avctx->channels; ch++) {
633
66294
                    uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
634
331470
                    for (j = 0; j < 8; j += 2)
635
265176
                        *dst++ = buf1[j] | (buf1[j + 1] << 4);
636
                }
637
            }
638
261
            av_free(buf);
639
        } else {
640
33408
            for (i = 0; i < blocks; i++) {
641
99441
                for (ch = 0; ch < avctx->channels; ch++) {
642
66294
                    ADPCMChannelStatus *status = &c->status[ch];
643
66294
                    const int16_t *smp = &samples_p[ch][1 + i * 8];
644
331470
                    for (j = 0; j < 8; j += 2) {
645
265176
                        uint8_t v = adpcm_ima_compress_sample(status, smp[j    ]);
646
265176
                        v        |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
647
265176
                        *dst++ = v;
648
                    }
649
                }
650
            }
651
        }
652
522
        break;
653
    }
654
8270
    case AV_CODEC_ID_ADPCM_IMA_QT:
655
    {
656
        PutBitContext pb;
657
8270
        init_put_bits(&pb, dst, pkt_size);
658
659
24810
        for (ch = 0; ch < avctx->channels; ch++) {
660
16540
            ADPCMChannelStatus *status = &c->status[ch];
661
16540
            put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
662
16540
            put_bits(&pb, 7,  status->step_index);
663
16540
            if (avctx->trellis > 0) {
664
                uint8_t buf[64];
665
8270
                adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
666
                                       64, 1);
667
537550
                for (i = 0; i < 64; i++)
668
529280
                    put_bits(&pb, 4, buf[i ^ 1]);
669
8270
                status->prev_sample = status->predictor;
670
            } else {
671
272910
                for (i = 0; i < 64; i += 2) {
672
                    int t1, t2;
673
264640
                    t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i    ]);
674
264640
                    t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
675
264640
                    put_bits(&pb, 4, t2);
676
264640
                    put_bits(&pb, 4, t1);
677
                }
678
            }
679
        }
680
681
8270
        flush_put_bits(&pb);
682
8270
        break;
683
    }
684
259
    case AV_CODEC_ID_ADPCM_IMA_SSI:
685
    {
686
        PutBitContext pb;
687
259
        init_put_bits(&pb, dst, pkt_size);
688
689
259
        av_assert0(avctx->trellis == 0);
690
691
264859
        for (i = 0; i < frame->nb_samples; i++) {
692
793800
            for (ch = 0; ch < avctx->channels; ch++) {
693
529200
                put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
694
            }
695
        }
696
697
259
        flush_put_bits(&pb);
698
259
        break;
699
    }
700
259
    case AV_CODEC_ID_ADPCM_IMA_ALP:
701
    {
702
        PutBitContext pb;
703
259
        init_put_bits(&pb, dst, pkt_size);
704
705
259
        av_assert0(avctx->trellis == 0);
706
707
132559
        for (n = frame->nb_samples / 2; n > 0; n--) {
708
396900
            for (ch = 0; ch < avctx->channels; ch++) {
709
264600
                put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
710
264600
                put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
711
            }
712
132300
            samples += avctx->channels;
713
        }
714
715
259
        flush_put_bits(&pb);
716
259
        break;
717
    }
718
195
    case AV_CODEC_ID_ADPCM_SWF:
719
    {
720
        PutBitContext pb;
721
195
        init_put_bits(&pb, dst, pkt_size);
722
723
195
        n = frame->nb_samples - 1;
724
725
        // store AdpcmCodeSize
726
195
        put_bits(&pb, 2, 2);    // set 4-bit flash adpcm format
727
728
        // init the encoder state
729
585
        for (i = 0; i < avctx->channels; i++) {
730
            // clip step so it fits 6 bits
731
390
            c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
732
390
            put_sbits(&pb, 16, samples[i]);
733
390
            put_bits(&pb, 6, c->status[i].step_index);
734
390
            c->status[i].prev_sample = samples[i];
735
        }
736
737
195
        if (avctx->trellis > 0) {
738
65
            if (!(buf = av_malloc(2 * n)))
739
                return AVERROR(ENOMEM);
740
65
            adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
741
                                   &c->status[0], n, avctx->channels);
742
65
            if (avctx->channels == 2)
743
65
                adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
744
                                       buf + n, &c->status[1], n,
745
                                       avctx->channels);
746
266240
            for (i = 0; i < n; i++) {
747
266175
                put_bits(&pb, 4, buf[i]);
748
266175
                if (avctx->channels == 2)
749
266175
                    put_bits(&pb, 4, buf[n + i]);
750
            }
751
65
            av_free(buf);
752
        } else {
753
532480
            for (i = 1; i < frame->nb_samples; i++) {
754
532350
                put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
755
532350
                         samples[avctx->channels * i]));
756
532350
                if (avctx->channels == 2)
757
532350
                    put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
758
532350
                             samples[2 * i + 1]));
759
            }
760
        }
761
195
        flush_put_bits(&pb);
762
195
        break;
763
    }
764
524
    case AV_CODEC_ID_ADPCM_MS:
765
1572
        for (i = 0; i < avctx->channels; i++) {
766
1048
            int predictor = 0;
767
1048
            *dst++ = predictor;
768
1048
            c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
769
1048
            c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
770
        }
771
1572
        for (i = 0; i < avctx->channels; i++) {
772
1048
            if (c->status[i].idelta < 16)
773
4
                c->status[i].idelta = 16;
774
1048
            bytestream_put_le16(&dst, c->status[i].idelta);
775
        }
776
1572
        for (i = 0; i < avctx->channels; i++)
777
1048
            c->status[i].sample2= *samples++;
778
1572
        for (i = 0; i < avctx->channels; i++) {
779
1048
            c->status[i].sample1 = *samples++;
780
1048
            bytestream_put_le16(&dst, c->status[i].sample1);
781
        }
782
1572
        for (i = 0; i < avctx->channels; i++)
783
1048
            bytestream_put_le16(&dst, c->status[i].sample2);
784
785
524
        if (avctx->trellis > 0) {
786
262
            n = avctx->block_align - 7 * avctx->channels;
787
262
            if (!(buf = av_malloc(2 * n)))
788
                return AVERROR(ENOMEM);
789
262
            if (avctx->channels == 1) {
790
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
791
                                       avctx->channels);
792
                for (i = 0; i < n; i += 2)
793
                    *dst++ = (buf[i] << 4) | buf[i + 1];
794
            } else {
795
262
                adpcm_compress_trellis(avctx, samples,     buf,
796
                                       &c->status[0], n, avctx->channels);
797
262
                adpcm_compress_trellis(avctx, samples + 1, buf + n,
798
                                       &c->status[1], n, avctx->channels);
799
264882
                for (i = 0; i < n; i++)
800
264620
                    *dst++ = (buf[i] << 4) | buf[n + i];
801
            }
802
262
            av_free(buf);
803
        } else {
804
264882
            for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
805
                int nibble;
806
264620
                nibble  = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
807
264620
                nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
808
264620
                *dst++  = nibble;
809
            }
810
        }
811
524
        break;
812
540
    case AV_CODEC_ID_ADPCM_YAMAHA:
813
540
        n = frame->nb_samples / 2;
814
540
        if (avctx->trellis > 0) {
815
259
            if (!(buf = av_malloc(2 * n * 2)))
816
                return AVERROR(ENOMEM);
817
259
            n *= 2;
818
259
            if (avctx->channels == 1) {
819
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
820
                                       avctx->channels);
821
                for (i = 0; i < n; i += 2)
822
                    *dst++ = buf[i] | (buf[i + 1] << 4);
823
            } else {
824
259
                adpcm_compress_trellis(avctx, samples,     buf,
825
                                       &c->status[0], n, avctx->channels);
826
259
                adpcm_compress_trellis(avctx, samples + 1, buf + n,
827
                                       &c->status[1], n, avctx->channels);
828
265475
                for (i = 0; i < n; i++)
829
265216
                    *dst++ = buf[i] | (buf[n + i] << 4);
830
            }
831
259
            av_free(buf);
832
        } else
833
288025
            for (n *= avctx->channels; n > 0; n--) {
834
                int nibble;
835
287744
                nibble  = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
836
287744
                nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
837
287744
                *dst++  = nibble;
838
            }
839
540
        break;
840
259
    case AV_CODEC_ID_ADPCM_IMA_APM:
841
    {
842
        PutBitContext pb;
843
259
        init_put_bits(&pb, dst, pkt_size);
844
845
259
        av_assert0(avctx->trellis == 0);
846
847
132559
        for (n = frame->nb_samples / 2; n > 0; n--) {
848
396900
            for (ch = 0; ch < avctx->channels; ch++) {
849
264600
                put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
850
264600
                put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
851
            }
852
132300
            samples += avctx->channels;
853
        }
854
855
259
        flush_put_bits(&pb);
856
259
        break;
857
    }
858
    case AV_CODEC_ID_ADPCM_IMA_AMV:
859
    {
860
        av_assert0(avctx->channels == 1);
861
862
        c->status[0].prev_sample = *samples;
863
        bytestream_put_le16(&dst, c->status[0].prev_sample);
864
        bytestream_put_byte(&dst, c->status[0].step_index);
865
        bytestream_put_byte(&dst, 0);
866
        bytestream_put_le32(&dst, avctx->frame_size);
867
868
        if (avctx->trellis > 0) {
869
            n = frame->nb_samples >> 1;
870
871
            if (!(buf = av_malloc(2 * n)))
872
                return AVERROR(ENOMEM);
873
874
            adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, avctx->channels);
875
            for (i = 0; i < n; i++)
876
                bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
877
878
            samples += 2 * n;
879
            av_free(buf);
880
        } else for (n = frame->nb_samples >> 1; n > 0; n--) {
881
            int nibble;
882
            nibble  = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
883
            nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
884
            bytestream_put_byte(&dst, nibble);
885
        }
886
887
        if (avctx->frame_size & 1) {
888
            int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
889
            bytestream_put_byte(&dst, nibble);
890
        }
891
        break;
892
    }
893
8269
    case AV_CODEC_ID_ADPCM_ARGO:
894
    {
895
        PutBitContext pb;
896
8269
        init_put_bits(&pb, dst, pkt_size);
897
898
8269
        av_assert0(frame->nb_samples == 32);
899
900
24807
        for (ch = 0; ch < avctx->channels; ch++) {
901
16538
            int64_t error  = INT64_MAX, tmperr = INT64_MAX;
902
16538
            int     shift  = 2, flag = 0;
903
16538
            int     saved1 = c->status[ch].sample1;
904
16538
            int     saved2 = c->status[ch].sample2;
905
906
            /* Find the optimal coefficients, bail early if we find a perfect result. */
907

271531
            for (int s = 2; s < 18 && tmperr != 0; s++) {
908

764734
                for (int f = 0; f < 2 && tmperr != 0; f++) {
909
509741
                    c->status[ch].sample1 = saved1;
910
509741
                    c->status[ch].sample2 = saved2;
911
509741
                    tmperr = adpcm_argo_compress_block(c->status + ch, NULL, samples_p[ch],
912
                                                       frame->nb_samples, s, f);
913
509741
                    if (tmperr < error) {
914
75397
                        shift = s;
915
75397
                        flag  = f;
916
75397
                        error = tmperr;
917
                    }
918
                }
919
            }
920
921
            /* Now actually do the encode. */
922
16538
            c->status[ch].sample1 = saved1;
923
16538
            c->status[ch].sample2 = saved2;
924
16538
            adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
925
                                      frame->nb_samples, shift, flag);
926
        }
927
928
8269
        flush_put_bits(&pb);
929
8269
        break;
930
    }
931
    default:
932
        return AVERROR(EINVAL);
933
    }
934
935
19097
    avpkt->size = pkt_size;
936
19097
    *got_packet_ptr = 1;
937
19097
    return 0;
938
}
939
940
static const enum AVSampleFormat sample_fmts[] = {
941
    AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
942
};
943
944
static const enum AVSampleFormat sample_fmts_p[] = {
945
    AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
946
};
947
948
static const AVOption options[] = {
949
    {
950
        .name        = "block_size",
951
        .help        = "set the block size",
952
        .offset      = offsetof(ADPCMEncodeContext, block_size),
953
        .type        = AV_OPT_TYPE_INT,
954
        .default_val = {.i64 = 1024},
955
        .min         = 32,
956
        .max         = 8192, /* Is this a reasonable upper limit? */
957
        .flags       = AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
958
    },
959
    { NULL }
960
};
961
962
static const AVClass adpcm_encoder_class = {
963
    .class_name = "ADPCM Encoder",
964
    .item_name  = av_default_item_name,
965
    .option     = options,
966
    .version    = LIBAVUTIL_VERSION_INT,
967
};
968
969
#define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_) \
970
AVCodec ff_ ## name_ ## _encoder = {                                       \
971
    .name           = #name_,                                              \
972
    .long_name      = NULL_IF_CONFIG_SMALL(long_name_),                    \
973
    .type           = AVMEDIA_TYPE_AUDIO,                                  \
974
    .id             = id_,                                                 \
975
    .priv_data_size = sizeof(ADPCMEncodeContext),                          \
976
    .init           = adpcm_encode_init,                                   \
977
    .encode2        = adpcm_encode_frame,                                  \
978
    .close          = adpcm_encode_close,                                  \
979
    .sample_fmts    = sample_fmts_,                                        \
980
    .capabilities   = capabilities_,                                       \
981
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,                           \
982
    .priv_class     = &adpcm_encoder_class,                                \
983
}
984
985
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_ARGO,    adpcm_argo,    sample_fmts_p, 0,                             "ADPCM Argonaut Games");
986
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, sample_fmts,   0,                             "ADPCM IMA AMV");
987
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM");
988
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_ALP, adpcm_ima_alp, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA High Voltage Software ALP");
989
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT,  adpcm_ima_qt,  sample_fmts_p, 0,                             "ADPCM IMA QuickTime");
990
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive");
991
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0,                             "ADPCM IMA WAV");
992
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS,      adpcm_ms,      sample_fmts,   0,                             "ADPCM Microsoft");
993
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF,     adpcm_swf,     sample_fmts,   0,                             "ADPCM Shockwave Flash");
994
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA,  adpcm_yamaha,  sample_fmts,   0,                             "ADPCM Yamaha");