GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/adpcmenc.c Lines: 346 371 93.3 %
Date: 2020-07-11 02:49:52 Branches: 221 252 87.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 "avcodec.h"
26
#include "put_bits.h"
27
#include "bytestream.h"
28
#include "adpcm.h"
29
#include "adpcm_data.h"
30
#include "internal.h"
31
32
/**
33
 * @file
34
 * ADPCM encoders
35
 * See ADPCM decoder reference documents for codec information.
36
 */
37
38
typedef struct TrellisPath {
39
    int nibble;
40
    int prev;
41
} TrellisPath;
42
43
typedef struct TrellisNode {
44
    uint32_t ssd;
45
    int path;
46
    int sample1;
47
    int sample2;
48
    int step;
49
} TrellisNode;
50
51
typedef struct ADPCMEncodeContext {
52
    ADPCMChannelStatus status[6];
53
    TrellisPath *paths;
54
    TrellisNode *node_buf;
55
    TrellisNode **nodep_buf;
56
    uint8_t *trellis_hash;
57
} ADPCMEncodeContext;
58
59
#define FREEZE_INTERVAL 128
60
61
12
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
62
{
63
12
    ADPCMEncodeContext *s = avctx->priv_data;
64
    uint8_t *extradata;
65
    int i;
66
67
12
    if (avctx->channels > 2) {
68
        av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
69
        return AVERROR(EINVAL);
70
    }
71
72

12
    if (avctx->trellis && (unsigned)avctx->trellis > 16U) {
73
        av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
74
        return AVERROR(EINVAL);
75
    }
76
77

12
    if (avctx->trellis && avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI) {
78
        /*
79
         * The current trellis implementation doesn't work for extended
80
         * runs of samples without periodic resets. Disallow it.
81
         */
82
        av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
83
        return AVERROR_PATCHWELCOME;
84
    }
85
86
12
    if (avctx->trellis) {
87
5
        int frontier  = 1 << avctx->trellis;
88
5
        int max_paths =  frontier * FREEZE_INTERVAL;
89
5
        if (!FF_ALLOC_TYPED_ARRAY(s->paths,        max_paths)    ||
90
5
            !FF_ALLOC_TYPED_ARRAY(s->node_buf,     2 * frontier) ||
91
5
            !FF_ALLOC_TYPED_ARRAY(s->nodep_buf,    2 * frontier) ||
92
5
            !FF_ALLOC_TYPED_ARRAY(s->trellis_hash, 65536))
93
            return AVERROR(ENOMEM);
94
    }
95
96
12
    avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
97
98

12
    switch (avctx->codec->id) {
99
2
    case AV_CODEC_ID_ADPCM_IMA_WAV:
100
        /* each 16 bits sample gives one nibble
101
           and we have 4 bytes per channel overhead */
102
2
        avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 /
103
2
                            (4 * avctx->channels) + 1;
104
        /* seems frame_size isn't taken into account...
105
           have to buffer the samples :-( */
106
2
        avctx->block_align = BLKSIZE;
107
2
        avctx->bits_per_coded_sample = 4;
108
2
        break;
109
2
    case AV_CODEC_ID_ADPCM_IMA_QT:
110
2
        avctx->frame_size  = 64;
111
2
        avctx->block_align = 34 * avctx->channels;
112
2
        break;
113
2
    case AV_CODEC_ID_ADPCM_MS:
114
        /* each 16 bits sample gives one nibble
115
           and we have 7 bytes per channel overhead */
116
2
        avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2;
117
2
        avctx->bits_per_coded_sample = 4;
118
2
        avctx->block_align    = BLKSIZE;
119
2
        if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
120
            return AVERROR(ENOMEM);
121
2
        avctx->extradata_size = 32;
122
2
        extradata = avctx->extradata;
123
2
        bytestream_put_le16(&extradata, avctx->frame_size);
124
2
        bytestream_put_le16(&extradata, 7); /* wNumCoef */
125
16
        for (i = 0; i < 7; i++) {
126
14
            bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
127
14
            bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
128
        }
129
2
        break;
130
3
    case AV_CODEC_ID_ADPCM_YAMAHA:
131
3
        avctx->frame_size  = BLKSIZE * 2 / avctx->channels;
132
3
        avctx->block_align = BLKSIZE;
133
3
        break;
134
2
    case AV_CODEC_ID_ADPCM_SWF:
135
2
        if (avctx->sample_rate != 11025 &&
136
2
            avctx->sample_rate != 22050 &&
137
2
            avctx->sample_rate != 44100) {
138
            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
139
                   "22050 or 44100\n");
140
            return AVERROR(EINVAL);
141
        }
142
2
        avctx->frame_size = 512 * (avctx->sample_rate / 11025);
143
2
        break;
144
1
    case AV_CODEC_ID_ADPCM_IMA_SSI:
145
1
        avctx->frame_size = BLKSIZE * 2 / avctx->channels;
146
1
        avctx->block_align = BLKSIZE;
147
1
        break;
148
    default:
149
        return AVERROR(EINVAL);
150
    }
151
152
12
    return 0;
153
}
154
155
12
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
156
{
157
12
    ADPCMEncodeContext *s = avctx->priv_data;
158
12
    av_freep(&s->paths);
159
12
    av_freep(&s->node_buf);
160
12
    av_freep(&s->nodep_buf);
161
12
    av_freep(&s->trellis_hash);
162
163
12
    return 0;
164
}
165
166
167
1062572
static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
168
                                                int16_t sample)
169
{
170
1062572
    int delta  = sample - c->prev_sample;
171
1062572
    int nibble = FFMIN(7, abs(delta) * 4 /
172
1062572
                       ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
173
1062572
    c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
174
1062572
                        ff_adpcm_yamaha_difflookup[nibble]) / 8);
175
1062572
    c->prev_sample = av_clip_int16(c->prev_sample);
176
1062572
    c->step_index  = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
177
1062572
    return nibble;
178
}
179
180
1058480
static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
181
                                                   int16_t sample)
182
{
183
1058480
    int delta  = sample - c->prev_sample;
184
1058480
    int diff, step = ff_adpcm_step_table[c->step_index];
185
1058480
    int nibble = 8*(delta < 0);
186
187
1058480
    delta= abs(delta);
188
1058480
    diff = delta + (step >> 3);
189
190
1058480
    if (delta >= step) {
191
290588
        nibble |= 4;
192
290588
        delta  -= step;
193
    }
194
1058480
    step >>= 1;
195
1058480
    if (delta >= step) {
196
506840
        nibble |= 2;
197
506840
        delta  -= step;
198
    }
199
1058480
    step >>= 1;
200
1058480
    if (delta >= step) {
201
513439
        nibble |= 1;
202
513439
        delta  -= step;
203
    }
204
1058480
    diff -= delta;
205
206
1058480
    if (nibble & 8)
207
526026
        c->prev_sample -= diff;
208
    else
209
532454
        c->prev_sample += diff;
210
211
1058480
    c->prev_sample = av_clip_int16(c->prev_sample);
212
1058480
    c->step_index  = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
213
214
1058480
    return nibble;
215
}
216
217
529240
static inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
218
                                               int16_t sample)
219
{
220
    int predictor, nibble, bias;
221
222
529240
    predictor = (((c->sample1) * (c->coeff1)) +
223
529240
                (( c->sample2) * (c->coeff2))) / 64;
224
225
529240
    nibble = sample - predictor;
226
529240
    if (nibble >= 0)
227
265224
        bias =  c->idelta / 2;
228
    else
229
264016
        bias = -c->idelta / 2;
230
231
529240
    nibble = (nibble + bias) / c->idelta;
232
529240
    nibble = av_clip_intp2(nibble, 3) & 0x0F;
233
234
529240
    predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
235
236
529240
    c->sample2 = c->sample1;
237
529240
    c->sample1 = av_clip_int16(predictor);
238
239
529240
    c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
240
529240
    if (c->idelta < 16)
241
28193
        c->idelta = 16;
242
243
529240
    return nibble;
244
}
245
246
575488
static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
247
                                                   int16_t sample)
248
{
249
    int nibble, delta;
250
251
575488
    if (!c->step) {
252
3
        c->predictor = 0;
253
3
        c->step      = 127;
254
    }
255
256
575488
    delta = sample - c->predictor;
257
258
575488
    nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
259
260
575488
    c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
261
575488
    c->predictor = av_clip_int16(c->predictor);
262
575488
    c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
263
575488
    c->step = av_clip(c->step, 127, 24576);
264
265
575488
    return nibble;
266
}
267
268
10094
static void adpcm_compress_trellis(AVCodecContext *avctx,
269
                                   const int16_t *samples, uint8_t *dst,
270
                                   ADPCMChannelStatus *c, int n, int stride)
271
{
272
    //FIXME 6% faster if frontier is a compile-time constant
273
10094
    ADPCMEncodeContext *s = avctx->priv_data;
274
10094
    const int frontier = 1 << avctx->trellis;
275
10094
    const int version  = avctx->codec->id;
276
10094
    TrellisPath *paths       = s->paths, *p;
277
10094
    TrellisNode *node_buf    = s->node_buf;
278
10094
    TrellisNode **nodep_buf  = s->nodep_buf;
279
10094
    TrellisNode **nodes      = nodep_buf; // nodes[] is always sorted by .ssd
280
10094
    TrellisNode **nodes_next = nodep_buf + frontier;
281
10094
    int pathn = 0, froze = -1, i, j, k, generation = 0;
282
10094
    uint8_t *hash = s->trellis_hash;
283
10094
    memset(hash, 0xff, 65536 * sizeof(*hash));
284
285
10094
    memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
286
10094
    nodes[0]          = node_buf + frontier;
287
10094
    nodes[0]->ssd     = 0;
288
10094
    nodes[0]->path    = 0;
289
10094
    nodes[0]->step    = c->step_index;
290
10094
    nodes[0]->sample1 = c->sample1;
291
10094
    nodes[0]->sample2 = c->sample2;
292

10094
    if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
293
1302
        version == AV_CODEC_ID_ADPCM_IMA_QT  ||
294
        version == AV_CODEC_ID_ADPCM_SWF)
295
9052
        nodes[0]->sample1 = c->prev_sample;
296
10094
    if (version == AV_CODEC_ID_ADPCM_MS)
297
524
        nodes[0]->step = c->idelta;
298
10094
    if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
299
518
        if (c->step == 0) {
300
2
            nodes[0]->step    = 127;
301
2
            nodes[0]->sample1 = 0;
302
        } else {
303
516
            nodes[0]->step    = c->step;
304
516
            nodes[0]->sample1 = c->predictor;
305
        }
306
    }
307
308
2661618
    for (i = 0; i < n; i++) {
309
2651524
        TrellisNode *t = node_buf + frontier*(i&1);
310
        TrellisNode **u;
311
2651524
        int sample   = samples[i * stride];
312
2651524
        int heap_pos = 0;
313
2651524
        memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
314

83552779
        for (j = 0; j < frontier && nodes[j]; j++) {
315
            // higher j have higher ssd already, so they're likely
316
            // to yield a suboptimal next sample too
317
80901255
            const int range = (j < frontier / 2) ? 1 : 0;
318
80901255
            const int step  = nodes[j]->step;
319
            int nidx;
320
80901255
            if (version == AV_CODEC_ID_ADPCM_MS) {
321
15997120
                const int predictor = ((nodes[j]->sample1 * c->coeff1) +
322
15997120
                                       (nodes[j]->sample2 * c->coeff2)) / 64;
323
15997120
                const int div  = (sample - predictor) / step;
324
15997120
                const int nmin = av_clip(div-range, -8, 6);
325
15997120
                const int nmax = av_clip(div+range, -7, 7);
326
48245314
                for (nidx = nmin; nidx <= nmax; nidx++) {
327
32248194
                    const int nibble = nidx & 0xf;
328
32248194
                    int dec_sample   = predictor + nidx * step;
329
#define STORE_NODE(NAME, STEP_INDEX)\
330
                    int d;\
331
                    uint32_t ssd;\
332
                    int pos;\
333
                    TrellisNode *u;\
334
                    uint8_t *h;\
335
                    dec_sample = av_clip_int16(dec_sample);\
336
                    d = sample - dec_sample;\
337
                    ssd = nodes[j]->ssd + d*(unsigned)d;\
338
                    /* Check for wraparound, skip such samples completely. \
339
                     * Note, changing ssd to a 64 bit variable would be \
340
                     * simpler, avoiding this check, but it's slower on \
341
                     * x86 32 bit at the moment. */\
342
                    if (ssd < nodes[j]->ssd)\
343
                        goto next_##NAME;\
344
                    /* Collapse any two states with the same previous sample value. \
345
                     * One could also distinguish states by step and by 2nd to last
346
                     * sample, but the effects of that are negligible.
347
                     * Since nodes in the previous generation are iterated
348
                     * through a heap, they're roughly ordered from better to
349
                     * worse, but not strictly ordered. Therefore, an earlier
350
                     * node with the same sample value is better in most cases
351
                     * (and thus the current is skipped), but not strictly
352
                     * in all cases. Only skipping samples where ssd >=
353
                     * ssd of the earlier node with the same sample gives
354
                     * slightly worse quality, though, for some reason. */ \
355
                    h = &hash[(uint16_t) dec_sample];\
356
                    if (*h == generation)\
357
                        goto next_##NAME;\
358
                    if (heap_pos < frontier) {\
359
                        pos = heap_pos++;\
360
                    } else {\
361
                        /* Try to replace one of the leaf nodes with the new \
362
                         * one, but try a different slot each time. */\
363
                        pos = (frontier >> 1) +\
364
                              (heap_pos & ((frontier >> 1) - 1));\
365
                        if (ssd > nodes_next[pos]->ssd)\
366
                            goto next_##NAME;\
367
                        heap_pos++;\
368
                    }\
369
                    *h = generation;\
370
                    u  = nodes_next[pos];\
371
                    if (!u) {\
372
                        av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
373
                        u = t++;\
374
                        nodes_next[pos] = u;\
375
                        u->path = pathn++;\
376
                    }\
377
                    u->ssd  = ssd;\
378
                    u->step = STEP_INDEX;\
379
                    u->sample2 = nodes[j]->sample1;\
380
                    u->sample1 = dec_sample;\
381
                    paths[u->path].nibble = nibble;\
382
                    paths[u->path].prev   = nodes[j]->path;\
383
                    /* Sift the newly inserted node up in the heap to \
384
                     * restore the heap property. */\
385
                    while (pos > 0) {\
386
                        int parent = (pos - 1) >> 1;\
387
                        if (nodes_next[parent]->ssd <= ssd)\
388
                            break;\
389
                        FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
390
                        pos = parent;\
391
                    }\
392
                    next_##NAME:;
393



44471491
                    STORE_NODE(ms, FFMAX(16,
394
                               (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
395
                }
396

64904135
            } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
397
32822107
                       version == AV_CODEC_ID_ADPCM_IMA_QT  ||
398
48302932
                       version == AV_CODEC_ID_ADPCM_SWF) {
399
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
400
                const int predictor = nodes[j]->sample1;\
401
                const int div = (sample - predictor) * 4 / STEP_TABLE;\
402
                int nmin = av_clip(div - range, -7, 6);\
403
                int nmax = av_clip(div + range, -6, 7);\
404
                if (nmin <= 0)\
405
                    nmin--; /* distinguish -0 from +0 */\
406
                if (nmax < 0)\
407
                    nmax--;\
408
                for (nidx = nmin; nidx <= nmax; nidx++) {\
409
                    const int nibble = nidx < 0 ? 7 - nidx : nidx;\
410
                    int dec_sample = predictor +\
411
                                    (STEP_TABLE *\
412
                                     ff_adpcm_yamaha_difflookup[nibble]) / 8;\
413
                    STORE_NODE(NAME, STEP_INDEX);\
414
                }
415





196413839
                LOOP_NODES(ima, ff_adpcm_step_table[step],
416
                           av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
417
            } else { //AV_CODEC_ID_ADPCM_YAMAHA
418





67079772
                LOOP_NODES(yamaha, step,
419
                           av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
420
                                   127, 24576));
421
#undef LOOP_NODES
422
#undef STORE_NODE
423
            }
424
        }
425
426
2651524
        u = nodes;
427
2651524
        nodes = nodes_next;
428
2651524
        nodes_next = u;
429
430
2651524
        generation++;
431
2651524
        if (generation == 255) {
432
7290
            memset(hash, 0xff, 65536 * sizeof(*hash));
433
7290
            generation = 0;
434
        }
435
436
        // prevent overflow
437
2651524
        if (nodes[0]->ssd > (1 << 28)) {
438

151896
            for (j = 1; j < frontier && nodes[j]; j++)
439
147051
                nodes[j]->ssd -= nodes[0]->ssd;
440
4845
            nodes[0]->ssd = 0;
441
        }
442
443
        // merge old paths to save memory
444
2651524
        if (i == froze + FREEZE_INTERVAL) {
445
15366
            p = &paths[nodes[0]->path];
446
1982214
            for (k = i; k > froze; k--) {
447
1966848
                dst[k] = p->nibble;
448
1966848
                p = &paths[p->prev];
449
            }
450
15366
            froze = i;
451
15366
            pathn = 0;
452
            // other nodes might use paths that don't coincide with the frozen one.
453
            // checking which nodes do so is too slow, so just kill them all.
454
            // this also slightly improves quality, but I don't know why.
455
15366
            memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
456
        }
457
    }
458
459
10094
    p = &paths[nodes[0]->path];
460
694770
    for (i = n - 1; i > froze; i--) {
461
684676
        dst[i] = p->nibble;
462
684676
        p = &paths[p->prev];
463
    }
464
465
10094
    c->predictor  = nodes[0]->sample1;
466
10094
    c->sample1    = nodes[0]->sample1;
467
10094
    c->sample2    = nodes[0]->sample2;
468
10094
    c->step_index = nodes[0]->step;
469
10094
    c->step       = nodes[0]->step;
470
10094
    c->idelta     = nodes[0]->step;
471
10094
}
472
473
10375
static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
474
                              const AVFrame *frame, int *got_packet_ptr)
475
{
476
    int n, i, ch, st, pkt_size, ret;
477
    const int16_t *samples;
478
    int16_t **samples_p;
479
    uint8_t *dst;
480
10375
    ADPCMEncodeContext *c = avctx->priv_data;
481
    uint8_t *buf;
482
483
10375
    samples = (const int16_t *)frame->data[0];
484
10375
    samples_p = (int16_t **)frame->extended_data;
485
10375
    st = avctx->channels == 2;
486
487
10375
    if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
488
260
        pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
489
10115
    else if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI)
490
259
        pkt_size = (frame->nb_samples * avctx->channels) / 2;
491
    else
492
9856
        pkt_size = avctx->block_align;
493
10375
    if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
494
        return ret;
495
10375
    dst = avpkt->data;
496
497

10375
    switch(avctx->codec->id) {
498
522
    case AV_CODEC_ID_ADPCM_IMA_WAV:
499
    {
500
        int blocks, j;
501
502
522
        blocks = (frame->nb_samples - 1) / 8;
503
504
1566
        for (ch = 0; ch < avctx->channels; ch++) {
505
1044
            ADPCMChannelStatus *status = &c->status[ch];
506
1044
            status->prev_sample = samples_p[ch][0];
507
            /* status->step_index = 0;
508
               XXX: not sure how to init the state machine */
509
1044
            bytestream_put_le16(&dst, status->prev_sample);
510
1044
            *dst++ = status->step_index;
511
1044
            *dst++ = 0; /* unknown */
512
        }
513
514
        /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
515
522
        if (avctx->trellis > 0) {
516
261
            if (!FF_ALLOC_TYPED_ARRAY(buf, avctx->channels * blocks * 8))
517
                return AVERROR(ENOMEM);
518
783
            for (ch = 0; ch < avctx->channels; ch++) {
519
522
                adpcm_compress_trellis(avctx, &samples_p[ch][1],
520
522
                                       buf + ch * blocks * 8, &c->status[ch],
521
                                       blocks * 8, 1);
522
            }
523
33408
            for (i = 0; i < blocks; i++) {
524
99441
                for (ch = 0; ch < avctx->channels; ch++) {
525
66294
                    uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
526
331470
                    for (j = 0; j < 8; j += 2)
527
265176
                        *dst++ = buf1[j] | (buf1[j + 1] << 4);
528
                }
529
            }
530
261
            av_free(buf);
531
        } else {
532
33408
            for (i = 0; i < blocks; i++) {
533
99441
                for (ch = 0; ch < avctx->channels; ch++) {
534
66294
                    ADPCMChannelStatus *status = &c->status[ch];
535
66294
                    const int16_t *smp = &samples_p[ch][1 + i * 8];
536
331470
                    for (j = 0; j < 8; j += 2) {
537
265176
                        uint8_t v = adpcm_ima_compress_sample(status, smp[j    ]);
538
265176
                        v        |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
539
265176
                        *dst++ = v;
540
                    }
541
                }
542
            }
543
        }
544
522
        break;
545
    }
546
8270
    case AV_CODEC_ID_ADPCM_IMA_QT:
547
    {
548
        PutBitContext pb;
549
8270
        init_put_bits(&pb, dst, pkt_size);
550
551
24810
        for (ch = 0; ch < avctx->channels; ch++) {
552
16540
            ADPCMChannelStatus *status = &c->status[ch];
553
16540
            put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
554
16540
            put_bits(&pb, 7,  status->step_index);
555
16540
            if (avctx->trellis > 0) {
556
                uint8_t buf[64];
557
8270
                adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
558
                                       64, 1);
559
537550
                for (i = 0; i < 64; i++)
560
529280
                    put_bits(&pb, 4, buf[i ^ 1]);
561
8270
                status->prev_sample = status->predictor;
562
            } else {
563
272910
                for (i = 0; i < 64; i += 2) {
564
                    int t1, t2;
565
264640
                    t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i    ]);
566
264640
                    t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
567
264640
                    put_bits(&pb, 4, t2);
568
264640
                    put_bits(&pb, 4, t1);
569
                }
570
            }
571
        }
572
573
8270
        flush_put_bits(&pb);
574
8270
        break;
575
    }
576
259
    case AV_CODEC_ID_ADPCM_IMA_SSI:
577
    {
578
        PutBitContext pb;
579
259
        init_put_bits(&pb, dst, pkt_size);
580
581
259
        av_assert0(avctx->trellis == 0);
582
583
264859
        for (i = 0; i < frame->nb_samples; i++) {
584
793800
            for (ch = 0; ch < avctx->channels; ch++) {
585
529200
                put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
586
            }
587
        }
588
589
259
        flush_put_bits(&pb);
590
259
        break;
591
    }
592
260
    case AV_CODEC_ID_ADPCM_SWF:
593
    {
594
        PutBitContext pb;
595
260
        init_put_bits(&pb, dst, pkt_size);
596
597
260
        n = frame->nb_samples - 1;
598
599
        // store AdpcmCodeSize
600
260
        put_bits(&pb, 2, 2);    // set 4-bit flash adpcm format
601
602
        // init the encoder state
603
780
        for (i = 0; i < avctx->channels; i++) {
604
            // clip step so it fits 6 bits
605
520
            c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
606
520
            put_sbits(&pb, 16, samples[i]);
607
520
            put_bits(&pb, 6, c->status[i].step_index);
608
520
            c->status[i].prev_sample = samples[i];
609
        }
610
611
260
        if (avctx->trellis > 0) {
612
130
            if (!(buf = av_malloc(2 * n)))
613
                return AVERROR(ENOMEM);
614
130
            adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
615
                                   &c->status[0], n, avctx->channels);
616
130
            if (avctx->channels == 2)
617
130
                adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
618
                                       buf + n, &c->status[1], n,
619
                                       avctx->channels);
620
266240
            for (i = 0; i < n; i++) {
621
266110
                put_bits(&pb, 4, buf[i]);
622
266110
                if (avctx->channels == 2)
623
266110
                    put_bits(&pb, 4, buf[n + i]);
624
            }
625
130
            av_free(buf);
626
        } else {
627
266240
            for (i = 1; i < frame->nb_samples; i++) {
628
266110
                put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
629
266110
                         samples[avctx->channels * i]));
630
266110
                if (avctx->channels == 2)
631
266110
                    put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
632
266110
                             samples[2 * i + 1]));
633
            }
634
        }
635
260
        flush_put_bits(&pb);
636
260
        break;
637
    }
638
524
    case AV_CODEC_ID_ADPCM_MS:
639
1572
        for (i = 0; i < avctx->channels; i++) {
640
1048
            int predictor = 0;
641
1048
            *dst++ = predictor;
642
1048
            c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
643
1048
            c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
644
        }
645
1572
        for (i = 0; i < avctx->channels; i++) {
646
1048
            if (c->status[i].idelta < 16)
647
4
                c->status[i].idelta = 16;
648
1048
            bytestream_put_le16(&dst, c->status[i].idelta);
649
        }
650
1572
        for (i = 0; i < avctx->channels; i++)
651
1048
            c->status[i].sample2= *samples++;
652
1572
        for (i = 0; i < avctx->channels; i++) {
653
1048
            c->status[i].sample1 = *samples++;
654
1048
            bytestream_put_le16(&dst, c->status[i].sample1);
655
        }
656
1572
        for (i = 0; i < avctx->channels; i++)
657
1048
            bytestream_put_le16(&dst, c->status[i].sample2);
658
659
524
        if (avctx->trellis > 0) {
660
262
            n = avctx->block_align - 7 * avctx->channels;
661
262
            if (!(buf = av_malloc(2 * n)))
662
                return AVERROR(ENOMEM);
663
262
            if (avctx->channels == 1) {
664
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
665
                                       avctx->channels);
666
                for (i = 0; i < n; i += 2)
667
                    *dst++ = (buf[i] << 4) | buf[i + 1];
668
            } else {
669
262
                adpcm_compress_trellis(avctx, samples,     buf,
670
                                       &c->status[0], n, avctx->channels);
671
262
                adpcm_compress_trellis(avctx, samples + 1, buf + n,
672
                                       &c->status[1], n, avctx->channels);
673
264882
                for (i = 0; i < n; i++)
674
264620
                    *dst++ = (buf[i] << 4) | buf[n + i];
675
            }
676
262
            av_free(buf);
677
        } else {
678
264882
            for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
679
                int nibble;
680
264620
                nibble  = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
681
264620
                nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
682
264620
                *dst++  = nibble;
683
            }
684
        }
685
524
        break;
686
540
    case AV_CODEC_ID_ADPCM_YAMAHA:
687
540
        n = frame->nb_samples / 2;
688
540
        if (avctx->trellis > 0) {
689
259
            if (!(buf = av_malloc(2 * n * 2)))
690
                return AVERROR(ENOMEM);
691
259
            n *= 2;
692
259
            if (avctx->channels == 1) {
693
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
694
                                       avctx->channels);
695
                for (i = 0; i < n; i += 2)
696
                    *dst++ = buf[i] | (buf[i + 1] << 4);
697
            } else {
698
259
                adpcm_compress_trellis(avctx, samples,     buf,
699
                                       &c->status[0], n, avctx->channels);
700
259
                adpcm_compress_trellis(avctx, samples + 1, buf + n,
701
                                       &c->status[1], n, avctx->channels);
702
265475
                for (i = 0; i < n; i++)
703
265216
                    *dst++ = buf[i] | (buf[n + i] << 4);
704
            }
705
259
            av_free(buf);
706
        } else
707
288025
            for (n *= avctx->channels; n > 0; n--) {
708
                int nibble;
709
287744
                nibble  = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
710
287744
                nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
711
287744
                *dst++  = nibble;
712
            }
713
540
        break;
714
    default:
715
        return AVERROR(EINVAL);
716
    }
717
718
10375
    avpkt->size = pkt_size;
719
10375
    *got_packet_ptr = 1;
720
10375
    return 0;
721
}
722
723
static const enum AVSampleFormat sample_fmts[] = {
724
    AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
725
};
726
727
static const enum AVSampleFormat sample_fmts_p[] = {
728
    AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
729
};
730
731
#define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_) \
732
AVCodec ff_ ## name_ ## _encoder = {                                       \
733
    .name           = #name_,                                              \
734
    .long_name      = NULL_IF_CONFIG_SMALL(long_name_),                    \
735
    .type           = AVMEDIA_TYPE_AUDIO,                                  \
736
    .id             = id_,                                                 \
737
    .priv_data_size = sizeof(ADPCMEncodeContext),                          \
738
    .init           = adpcm_encode_init,                                   \
739
    .encode2        = adpcm_encode_frame,                                  \
740
    .close          = adpcm_encode_close,                                  \
741
    .sample_fmts    = sample_fmts_,                                        \
742
    .capabilities   = capabilities_,                                       \
743
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,                           \
744
}
745
746
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT,  adpcm_ima_qt,  sample_fmts_p, 0,                             "ADPCM IMA QuickTime");
747
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive");
748
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0,                             "ADPCM IMA WAV");
749
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS,      adpcm_ms,      sample_fmts,   0,                             "ADPCM Microsoft");
750
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF,     adpcm_swf,     sample_fmts,   0,                             "ADPCM Shockwave Flash");
751
ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA,  adpcm_yamaha,  sample_fmts,   0,                             "ADPCM Yamaha");