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 "config_components.h"
26 
27 #include "libavutil/opt.h"
28 
29 #include "avcodec.h"
30 #include "put_bits.h"
31 #include "bytestream.h"
32 #include "adpcm.h"
33 #include "adpcm_data.h"
34 #include "codec_internal.h"
35 #include "encode.h"
36 
37 /**
38  * @file
39  * ADPCM encoders
40  * See ADPCM decoder reference documents for codec information.
41  */
42 
43 #define CASE_0(codec_id, ...)
44 #define CASE_1(codec_id, ...) \
45     case codec_id:            \
46     { __VA_ARGS__ }           \
47     break;
48 #define CASE_2(enabled, codec_id, ...) \
49         CASE_ ## enabled(codec_id, __VA_ARGS__)
50 #define CASE_3(config, codec_id, ...) \
51         CASE_2(config, codec_id, __VA_ARGS__)
52 #define CASE(codec, ...) \
53         CASE_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, __VA_ARGS__)
54 
55 typedef struct TrellisPath {
56     int nibble;
57     int prev;
58 } TrellisPath;
59 
60 typedef struct TrellisNode {
61     uint32_t ssd;
62     int path;
63     int sample1;
64     int sample2;
65     int step;
66 } TrellisNode;
67 
68 typedef struct ADPCMEncodeContext {
69     AVClass *class;
70     int block_size;
71 
72     ADPCMChannelStatus status[6];
73     TrellisPath *paths;
74     TrellisNode *node_buf;
75     TrellisNode **nodep_buf;
76     uint8_t *trellis_hash;
77 } ADPCMEncodeContext;
78 
79 #define FREEZE_INTERVAL 128
80 
adpcm_encode_init(AVCodecContext *avctx)81 static av_cold int adpcm_encode_init(AVCodecContext *avctx)
82 {
83     ADPCMEncodeContext *s = avctx->priv_data;
84     int channels = avctx->ch_layout.nb_channels;
85 
86     /*
87      * AMV's block size has to match that of the corresponding video
88      * stream. Relax the POT requirement.
89      */
90     if (avctx->codec->id != AV_CODEC_ID_ADPCM_IMA_AMV &&
91         (s->block_size & (s->block_size - 1))) {
92         av_log(avctx, AV_LOG_ERROR, "block size must be power of 2\n");
93         return AVERROR(EINVAL);
94     }
95 
96     if (avctx->trellis) {
97         int frontier, max_paths;
98 
99         if ((unsigned)avctx->trellis > 16U) {
100             av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
101             return AVERROR(EINVAL);
102         }
103 
104         if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
105             avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
106             avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO    ||
107             avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_WS) {
108             /*
109              * The current trellis implementation doesn't work for extended
110              * runs of samples without periodic resets. Disallow it.
111              */
112             av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
113             return AVERROR_PATCHWELCOME;
114         }
115 
116         frontier  = 1 << avctx->trellis;
117         max_paths =  frontier * FREEZE_INTERVAL;
118         if (!FF_ALLOC_TYPED_ARRAY(s->paths,        max_paths)    ||
119             !FF_ALLOC_TYPED_ARRAY(s->node_buf,     2 * frontier) ||
120             !FF_ALLOC_TYPED_ARRAY(s->nodep_buf,    2 * frontier) ||
121             !FF_ALLOC_TYPED_ARRAY(s->trellis_hash, 65536))
122             return AVERROR(ENOMEM);
123     }
124 
125     avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
126 
127     switch (avctx->codec->id) {
128     CASE(ADPCM_IMA_WAV,
129         /* each 16 bits sample gives one nibble
130            and we have 4 bytes per channel overhead */
131         avctx->frame_size = (s->block_size - 4 * channels) * 8 /
132                             (4 * channels) + 1;
133         /* seems frame_size isn't taken into account...
134            have to buffer the samples :-( */
135         avctx->block_align = s->block_size;
136         avctx->bits_per_coded_sample = 4;
137         ) /* End of CASE */
138     CASE(ADPCM_IMA_QT,
139         avctx->frame_size  = 64;
140         avctx->block_align = 34 * channels;
141         ) /* End of CASE */
142     CASE(ADPCM_MS,
143         uint8_t *extradata;
144         /* each 16 bits sample gives one nibble
145            and we have 7 bytes per channel overhead */
146         avctx->frame_size = (s->block_size - 7 * channels) * 2 / channels + 2;
147         avctx->bits_per_coded_sample = 4;
148         avctx->block_align     = s->block_size;
149         if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
150             return AVERROR(ENOMEM);
151         avctx->extradata_size = 32;
152         extradata = avctx->extradata;
153         bytestream_put_le16(&extradata, avctx->frame_size);
154         bytestream_put_le16(&extradata, 7); /* wNumCoef */
155         for (int i = 0; i < 7; i++) {
156             bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
157             bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
158         }
159         ) /* End of CASE */
160     CASE(ADPCM_YAMAHA,
161         avctx->frame_size  = s->block_size * 2 / channels;
162         avctx->block_align = s->block_size;
163         ) /* End of CASE */
164     CASE(ADPCM_SWF,
165         if (avctx->sample_rate != 11025 &&
166             avctx->sample_rate != 22050 &&
167             avctx->sample_rate != 44100) {
168             av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
169                    "22050 or 44100\n");
170             return AVERROR(EINVAL);
171         }
172         avctx->frame_size  = 4096; /* Hardcoded according to the SWF spec. */
173         avctx->block_align = (2 + channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
174         ) /* End of CASE */
175     case AV_CODEC_ID_ADPCM_IMA_SSI:
176     case AV_CODEC_ID_ADPCM_IMA_ALP:
177         avctx->frame_size  = s->block_size * 2 / channels;
178         avctx->block_align = s->block_size;
179         break;
180     CASE(ADPCM_IMA_AMV,
181         if (avctx->sample_rate != 22050) {
182             av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
183             return AVERROR(EINVAL);
184         }
185 
186         if (channels != 1) {
187             av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
188             return AVERROR(EINVAL);
189         }
190 
191         avctx->frame_size  = s->block_size;
192         avctx->block_align = 8 + (FFALIGN(avctx->frame_size, 2) / 2);
193         ) /* End of CASE */
194     CASE(ADPCM_IMA_APM,
195         avctx->frame_size  = s->block_size * 2 / channels;
196         avctx->block_align = s->block_size;
197 
198         if (!(avctx->extradata = av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
199             return AVERROR(ENOMEM);
200         avctx->extradata_size = 28;
201         ) /* End of CASE */
202     CASE(ADPCM_ARGO,
203         avctx->frame_size = 32;
204         avctx->block_align = 17 * channels;
205         ) /* End of CASE */
206     CASE(ADPCM_IMA_WS,
207         /* each 16 bits sample gives one nibble */
208         avctx->frame_size = s->block_size * 2 / channels;
209         avctx->block_align = s->block_size;
210         ) /* End of CASE */
211     default:
212         return AVERROR(EINVAL);
213     }
214 
215     return 0;
216 }
217 
adpcm_encode_close(AVCodecContext *avctx)218 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
219 {
220     ADPCMEncodeContext *s = avctx->priv_data;
221     av_freep(&s->paths);
222     av_freep(&s->node_buf);
223     av_freep(&s->nodep_buf);
224     av_freep(&s->trellis_hash);
225 
226     return 0;
227 }
228 
229 
adpcm_ima_compress_sample(ADPCMChannelStatus *c, int16_t sample)230 static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
231                                                 int16_t sample)
232 {
233     int delta  = sample - c->prev_sample;
234     int nibble = FFMIN(7, abs(delta) * 4 /
235                        ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
236     c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
237                         ff_adpcm_yamaha_difflookup[nibble]) / 8);
238     c->prev_sample = av_clip_int16(c->prev_sample);
239     c->step_index  = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
240     return nibble;
241 }
242 
adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)243 static inline uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)
244 {
245     const int delta  = sample - c->prev_sample;
246     const int step   = ff_adpcm_step_table[c->step_index];
247     const int sign   = (delta < 0) * 8;
248 
249     int nibble = FFMIN(abs(delta) * 4 / step, 7);
250     int diff   = (step * nibble) >> 2;
251     if (sign)
252         diff = -diff;
253 
254     nibble = sign | nibble;
255 
256     c->prev_sample += diff;
257     c->prev_sample  = av_clip_int16(c->prev_sample);
258     c->step_index   = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
259     return nibble;
260 }
261 
adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, int16_t sample)262 static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
263                                                    int16_t sample)
264 {
265     int delta  = sample - c->prev_sample;
266     int diff, step = ff_adpcm_step_table[c->step_index];
267     int nibble = 8*(delta < 0);
268 
269     delta= abs(delta);
270     diff = delta + (step >> 3);
271 
272     if (delta >= step) {
273         nibble |= 4;
274         delta  -= step;
275     }
276     step >>= 1;
277     if (delta >= step) {
278         nibble |= 2;
279         delta  -= step;
280     }
281     step >>= 1;
282     if (delta >= step) {
283         nibble |= 1;
284         delta  -= step;
285     }
286     diff -= delta;
287 
288     if (nibble & 8)
289         c->prev_sample -= diff;
290     else
291         c->prev_sample += diff;
292 
293     c->prev_sample = av_clip_int16(c->prev_sample);
294     c->step_index  = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
295 
296     return nibble;
297 }
298 
adpcm_ms_compress_sample(ADPCMChannelStatus *c, int16_t sample)299 static inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
300                                                int16_t sample)
301 {
302     int predictor, nibble, bias;
303 
304     predictor = (((c->sample1) * (c->coeff1)) +
305                 (( c->sample2) * (c->coeff2))) / 64;
306 
307     nibble = sample - predictor;
308     if (nibble >= 0)
309         bias =  c->idelta / 2;
310     else
311         bias = -c->idelta / 2;
312 
313     nibble = (nibble + bias) / c->idelta;
314     nibble = av_clip_intp2(nibble, 3) & 0x0F;
315 
316     predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
317 
318     c->sample2 = c->sample1;
319     c->sample1 = av_clip_int16(predictor);
320 
321     c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
322     if (c->idelta < 16)
323         c->idelta = 16;
324 
325     return nibble;
326 }
327 
adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, int16_t sample)328 static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
329                                                    int16_t sample)
330 {
331     int nibble, delta;
332 
333     if (!c->step) {
334         c->predictor = 0;
335         c->step      = 127;
336     }
337 
338     delta = sample - c->predictor;
339 
340     nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
341 
342     c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
343     c->predictor = av_clip_int16(c->predictor);
344     c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
345     c->step = av_clip(c->step, 127, 24576);
346 
347     return nibble;
348 }
349 
adpcm_compress_trellis(AVCodecContext *avctx, const int16_t *samples, uint8_t *dst, ADPCMChannelStatus *c, int n, int stride)350 static void adpcm_compress_trellis(AVCodecContext *avctx,
351                                    const int16_t *samples, uint8_t *dst,
352                                    ADPCMChannelStatus *c, int n, int stride)
353 {
354     //FIXME 6% faster if frontier is a compile-time constant
355     ADPCMEncodeContext *s = avctx->priv_data;
356     const int frontier = 1 << avctx->trellis;
357     const int version  = avctx->codec->id;
358     TrellisPath *paths       = s->paths, *p;
359     TrellisNode *node_buf    = s->node_buf;
360     TrellisNode **nodep_buf  = s->nodep_buf;
361     TrellisNode **nodes      = nodep_buf; // nodes[] is always sorted by .ssd
362     TrellisNode **nodes_next = nodep_buf + frontier;
363     int pathn = 0, froze = -1, i, j, k, generation = 0;
364     uint8_t *hash = s->trellis_hash;
365     memset(hash, 0xff, 65536 * sizeof(*hash));
366 
367     memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
368     nodes[0]          = node_buf + frontier;
369     nodes[0]->ssd     = 0;
370     nodes[0]->path    = 0;
371     nodes[0]->step    = c->step_index;
372     nodes[0]->sample1 = c->sample1;
373     nodes[0]->sample2 = c->sample2;
374     if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
375         version == AV_CODEC_ID_ADPCM_IMA_QT  ||
376         version == AV_CODEC_ID_ADPCM_IMA_AMV ||
377         version == AV_CODEC_ID_ADPCM_SWF)
378         nodes[0]->sample1 = c->prev_sample;
379     if (version == AV_CODEC_ID_ADPCM_MS)
380         nodes[0]->step = c->idelta;
381     if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
382         if (c->step == 0) {
383             nodes[0]->step    = 127;
384             nodes[0]->sample1 = 0;
385         } else {
386             nodes[0]->step    = c->step;
387             nodes[0]->sample1 = c->predictor;
388         }
389     }
390 
391     for (i = 0; i < n; i++) {
392         TrellisNode *t = node_buf + frontier*(i&1);
393         TrellisNode **u;
394         int sample   = samples[i * stride];
395         int heap_pos = 0;
396         memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
397         for (j = 0; j < frontier && nodes[j]; j++) {
398             // higher j have higher ssd already, so they're likely
399             // to yield a suboptimal next sample too
400             const int range = (j < frontier / 2) ? 1 : 0;
401             const int step  = nodes[j]->step;
402             int nidx;
403             if (version == AV_CODEC_ID_ADPCM_MS) {
404                 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
405                                        (nodes[j]->sample2 * c->coeff2)) / 64;
406                 const int div  = (sample - predictor) / step;
407                 const int nmin = av_clip(div-range, -8, 6);
408                 const int nmax = av_clip(div+range, -7, 7);
409                 for (nidx = nmin; nidx <= nmax; nidx++) {
410                     const int nibble = nidx & 0xf;
411                     int dec_sample   = predictor + nidx * step;
412 #define STORE_NODE(NAME, STEP_INDEX)\
413                     int d;\
414                     uint32_t ssd;\
415                     int pos;\
416                     TrellisNode *u;\
417                     uint8_t *h;\
418                     dec_sample = av_clip_int16(dec_sample);\
419                     d = sample - dec_sample;\
420                     ssd = nodes[j]->ssd + d*(unsigned)d;\
421                     /* Check for wraparound, skip such samples completely. \
422                      * Note, changing ssd to a 64 bit variable would be \
423                      * simpler, avoiding this check, but it's slower on \
424                      * x86 32 bit at the moment. */\
425                     if (ssd < nodes[j]->ssd)\
426                         goto next_##NAME;\
427                     /* Collapse any two states with the same previous sample value. \
428                      * One could also distinguish states by step and by 2nd to last
429                      * sample, but the effects of that are negligible.
430                      * Since nodes in the previous generation are iterated
431                      * through a heap, they're roughly ordered from better to
432                      * worse, but not strictly ordered. Therefore, an earlier
433                      * node with the same sample value is better in most cases
434                      * (and thus the current is skipped), but not strictly
435                      * in all cases. Only skipping samples where ssd >=
436                      * ssd of the earlier node with the same sample gives
437                      * slightly worse quality, though, for some reason. */ \
438                     h = &hash[(uint16_t) dec_sample];\
439                     if (*h == generation)\
440                         goto next_##NAME;\
441                     if (heap_pos < frontier) {\
442                         pos = heap_pos++;\
443                     } else {\
444                         /* Try to replace one of the leaf nodes with the new \
445                          * one, but try a different slot each time. */\
446                         pos = (frontier >> 1) +\
447                               (heap_pos & ((frontier >> 1) - 1));\
448                         if (ssd > nodes_next[pos]->ssd)\
449                             goto next_##NAME;\
450                         heap_pos++;\
451                     }\
452                     *h = generation;\
453                     u  = nodes_next[pos];\
454                     if (!u) {\
455                         av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
456                         u = t++;\
457                         nodes_next[pos] = u;\
458                         u->path = pathn++;\
459                     }\
460                     u->ssd  = ssd;\
461                     u->step = STEP_INDEX;\
462                     u->sample2 = nodes[j]->sample1;\
463                     u->sample1 = dec_sample;\
464                     paths[u->path].nibble = nibble;\
465                     paths[u->path].prev   = nodes[j]->path;\
466                     /* Sift the newly inserted node up in the heap to \
467                      * restore the heap property. */\
468                     while (pos > 0) {\
469                         int parent = (pos - 1) >> 1;\
470                         if (nodes_next[parent]->ssd <= ssd)\
471                             break;\
472                         FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
473                         pos = parent;\
474                     }\
475                     next_##NAME:;
476                     STORE_NODE(ms, FFMAX(16,
477                                (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
478                 }
479             } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
480                        version == AV_CODEC_ID_ADPCM_IMA_QT  ||
481                        version == AV_CODEC_ID_ADPCM_IMA_AMV ||
482                        version == AV_CODEC_ID_ADPCM_SWF) {
483 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
484                 const int predictor = nodes[j]->sample1;\
485                 const int div = (sample - predictor) * 4 / STEP_TABLE;\
486                 int nmin = av_clip(div - range, -7, 6);\
487                 int nmax = av_clip(div + range, -6, 7);\
488                 if (nmin <= 0)\
489                     nmin--; /* distinguish -0 from +0 */\
490                 if (nmax < 0)\
491                     nmax--;\
492                 for (nidx = nmin; nidx <= nmax; nidx++) {\
493                     const int nibble = nidx < 0 ? 7 - nidx : nidx;\
494                     int dec_sample = predictor +\
495                                     (STEP_TABLE *\
496                                      ff_adpcm_yamaha_difflookup[nibble]) / 8;\
497                     STORE_NODE(NAME, STEP_INDEX);\
498                 }
499                 LOOP_NODES(ima, ff_adpcm_step_table[step],
500                            av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
501             } else { //AV_CODEC_ID_ADPCM_YAMAHA
502                 LOOP_NODES(yamaha, step,
503                            av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
504                                    127, 24576));
505 #undef LOOP_NODES
506 #undef STORE_NODE
507             }
508         }
509 
510         u = nodes;
511         nodes = nodes_next;
512         nodes_next = u;
513 
514         generation++;
515         if (generation == 255) {
516             memset(hash, 0xff, 65536 * sizeof(*hash));
517             generation = 0;
518         }
519 
520         // prevent overflow
521         if (nodes[0]->ssd > (1 << 28)) {
522             for (j = 1; j < frontier && nodes[j]; j++)
523                 nodes[j]->ssd -= nodes[0]->ssd;
524             nodes[0]->ssd = 0;
525         }
526 
527         // merge old paths to save memory
528         if (i == froze + FREEZE_INTERVAL) {
529             p = &paths[nodes[0]->path];
530             for (k = i; k > froze; k--) {
531                 dst[k] = p->nibble;
532                 p = &paths[p->prev];
533             }
534             froze = i;
535             pathn = 0;
536             // other nodes might use paths that don't coincide with the frozen one.
537             // checking which nodes do so is too slow, so just kill them all.
538             // this also slightly improves quality, but I don't know why.
539             memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
540         }
541     }
542 
543     p = &paths[nodes[0]->path];
544     for (i = n - 1; i > froze; i--) {
545         dst[i] = p->nibble;
546         p = &paths[p->prev];
547     }
548 
549     c->predictor  = nodes[0]->sample1;
550     c->sample1    = nodes[0]->sample1;
551     c->sample2    = nodes[0]->sample2;
552     c->step_index = nodes[0]->step;
553     c->step       = nodes[0]->step;
554     c->idelta     = nodes[0]->step;
555 }
556 
557 #if CONFIG_ADPCM_ARGO_ENCODER
adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s, int shift, int flag)558 static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
559                                              int shift, int flag)
560 {
561     int nibble;
562 
563     if (flag)
564         nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
565     else
566         nibble = 4 * s - 4 * cs->sample1;
567 
568     return (nibble >> shift) & 0x0F;
569 }
570 
adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb, const int16_t *samples, int nsamples, int shift, int flag)571 static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
572                                          const int16_t *samples, int nsamples,
573                                          int shift, int flag)
574 {
575     int64_t error = 0;
576 
577     if (pb) {
578         put_bits(pb, 4, shift - 2);
579         put_bits(pb, 1, 0);
580         put_bits(pb, 1, !!flag);
581         put_bits(pb, 2, 0);
582     }
583 
584     for (int n = 0; n < nsamples; n++) {
585         /* Compress the nibble, then expand it to see how much precision we've lost. */
586         int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
587         int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
588 
589         error += abs(samples[n] - sample);
590 
591         if (pb)
592             put_bits(pb, 4, nibble);
593     }
594 
595     return error;
596 }
597 #endif
598 
adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)599 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
600                               const AVFrame *frame, int *got_packet_ptr)
601 {
602     int st, pkt_size, ret;
603     const int16_t *samples;
604     int16_t **samples_p;
605     uint8_t *dst;
606     ADPCMEncodeContext *c = avctx->priv_data;
607     int channels = avctx->ch_layout.nb_channels;
608 
609     samples = (const int16_t *)frame->data[0];
610     samples_p = (int16_t **)frame->extended_data;
611     st = channels == 2;
612 
613     if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
614         avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
615         avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
616         avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_WS)
617         pkt_size = (frame->nb_samples * channels) / 2;
618     else
619         pkt_size = avctx->block_align;
620     if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
621         return ret;
622     dst = avpkt->data;
623 
624     switch(avctx->codec->id) {
625     CASE(ADPCM_IMA_WAV,
626         int blocks = (frame->nb_samples - 1) / 8;
627 
628         for (int ch = 0; ch < channels; ch++) {
629             ADPCMChannelStatus *status = &c->status[ch];
630             status->prev_sample = samples_p[ch][0];
631             /* status->step_index = 0;
632                XXX: not sure how to init the state machine */
633             bytestream_put_le16(&dst, status->prev_sample);
634             *dst++ = status->step_index;
635             *dst++ = 0; /* unknown */
636         }
637 
638         /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
639         if (avctx->trellis > 0) {
640             uint8_t *buf;
641             if (!FF_ALLOC_TYPED_ARRAY(buf, channels * blocks * 8))
642                 return AVERROR(ENOMEM);
643             for (int ch = 0; ch < channels; ch++) {
644                 adpcm_compress_trellis(avctx, &samples_p[ch][1],
645                                        buf + ch * blocks * 8, &c->status[ch],
646                                        blocks * 8, 1);
647             }
648             for (int i = 0; i < blocks; i++) {
649                 for (int ch = 0; ch < channels; ch++) {
650                     uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
651                     for (int j = 0; j < 8; j += 2)
652                         *dst++ = buf1[j] | (buf1[j + 1] << 4);
653                 }
654             }
655             av_free(buf);
656         } else {
657             for (int i = 0; i < blocks; i++) {
658                 for (int ch = 0; ch < channels; ch++) {
659                     ADPCMChannelStatus *status = &c->status[ch];
660                     const int16_t *smp = &samples_p[ch][1 + i * 8];
661                     for (int j = 0; j < 8; j += 2) {
662                         uint8_t v = adpcm_ima_compress_sample(status, smp[j    ]);
663                         v        |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
664                         *dst++ = v;
665                     }
666                 }
667             }
668         }
669         ) /* End of CASE */
670     CASE(ADPCM_IMA_QT,
671         PutBitContext pb;
672         init_put_bits(&pb, dst, pkt_size);
673 
674         for (int ch = 0; ch < channels; ch++) {
675             ADPCMChannelStatus *status = &c->status[ch];
676             put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
677             put_bits(&pb, 7,  status->step_index);
678             if (avctx->trellis > 0) {
679                 uint8_t buf[64];
680                 adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
681                                        64, 1);
682                 for (int i = 0; i < 64; i++)
683                     put_bits(&pb, 4, buf[i ^ 1]);
684                 status->prev_sample = status->predictor;
685             } else {
686                 for (int i = 0; i < 64; i += 2) {
687                     int t1, t2;
688                     t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i    ]);
689                     t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
690                     put_bits(&pb, 4, t2);
691                     put_bits(&pb, 4, t1);
692                 }
693             }
694         }
695 
696         flush_put_bits(&pb);
697         ) /* End of CASE */
698     CASE(ADPCM_IMA_SSI,
699         PutBitContext pb;
700         init_put_bits(&pb, dst, pkt_size);
701 
702         av_assert0(avctx->trellis == 0);
703 
704         for (int i = 0; i < frame->nb_samples; i++) {
705             for (int ch = 0; ch < channels; ch++) {
706                 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
707             }
708         }
709 
710         flush_put_bits(&pb);
711         ) /* End of CASE */
712     CASE(ADPCM_IMA_ALP,
713         PutBitContext pb;
714         init_put_bits(&pb, dst, pkt_size);
715 
716         av_assert0(avctx->trellis == 0);
717 
718         for (int n = frame->nb_samples / 2; n > 0; n--) {
719             for (int ch = 0; ch < channels; ch++) {
720                 put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
721                 put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
722             }
723             samples += channels;
724         }
725 
726         flush_put_bits(&pb);
727         ) /* End of CASE */
728     CASE(ADPCM_SWF,
729         const int n = frame->nb_samples - 1;
730         PutBitContext pb;
731         init_put_bits(&pb, dst, pkt_size);
732 
733         /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
734         av_assert0(n == 4095);
735 
736         // store AdpcmCodeSize
737         put_bits(&pb, 2, 2);    // set 4-bit flash adpcm format
738 
739         // init the encoder state
740         for (int i = 0; i < channels; i++) {
741             // clip step so it fits 6 bits
742             c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
743             put_sbits(&pb, 16, samples[i]);
744             put_bits(&pb, 6, c->status[i].step_index);
745             c->status[i].prev_sample = samples[i];
746         }
747 
748         if (avctx->trellis > 0) {
749             uint8_t buf[8190 /* = 2 * n */];
750             adpcm_compress_trellis(avctx, samples + channels, buf,
751                                    &c->status[0], n, channels);
752             if (channels == 2)
753                 adpcm_compress_trellis(avctx, samples + channels + 1,
754                                        buf + n, &c->status[1], n,
755                                        channels);
756             for (int i = 0; i < n; i++) {
757                 put_bits(&pb, 4, buf[i]);
758                 if (channels == 2)
759                     put_bits(&pb, 4, buf[n + i]);
760             }
761         } else {
762             for (int i = 1; i < frame->nb_samples; i++) {
763                 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
764                          samples[channels * i]));
765                 if (channels == 2)
766                     put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
767                              samples[2 * i + 1]));
768             }
769         }
770         flush_put_bits(&pb);
771         ) /* End of CASE */
772     CASE(ADPCM_MS,
773         for (int i = 0; i < channels; i++) {
774             int predictor = 0;
775             *dst++ = predictor;
776             c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
777             c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
778         }
779         for (int i = 0; i < channels; i++) {
780             if (c->status[i].idelta < 16)
781                 c->status[i].idelta = 16;
782             bytestream_put_le16(&dst, c->status[i].idelta);
783         }
784         for (int i = 0; i < channels; i++)
785             c->status[i].sample2= *samples++;
786         for (int i = 0; i < channels; i++) {
787             c->status[i].sample1 = *samples++;
788             bytestream_put_le16(&dst, c->status[i].sample1);
789         }
790         for (int i = 0; i < channels; i++)
791             bytestream_put_le16(&dst, c->status[i].sample2);
792 
793         if (avctx->trellis > 0) {
794             const int n  = avctx->block_align - 7 * channels;
795             uint8_t *buf = av_malloc(2 * n);
796             if (!buf)
797                 return AVERROR(ENOMEM);
798             if (channels == 1) {
799                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
800                                        channels);
801                 for (int i = 0; i < n; i += 2)
802                     *dst++ = (buf[i] << 4) | buf[i + 1];
803             } else {
804                 adpcm_compress_trellis(avctx, samples,     buf,
805                                        &c->status[0], n, channels);
806                 adpcm_compress_trellis(avctx, samples + 1, buf + n,
807                                        &c->status[1], n, channels);
808                 for (int i = 0; i < n; i++)
809                     *dst++ = (buf[i] << 4) | buf[n + i];
810             }
811             av_free(buf);
812         } else {
813             for (int i = 7 * channels; i < avctx->block_align; i++) {
814                 int nibble;
815                 nibble  = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
816                 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
817                 *dst++  = nibble;
818             }
819         }
820         ) /* End of CASE */
821     CASE(ADPCM_YAMAHA,
822         int n = frame->nb_samples / 2;
823         if (avctx->trellis > 0) {
824             uint8_t *buf = av_malloc(2 * n * 2);
825             if (!buf)
826                 return AVERROR(ENOMEM);
827             n *= 2;
828             if (channels == 1) {
829                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
830                                        channels);
831                 for (int i = 0; i < n; i += 2)
832                     *dst++ = buf[i] | (buf[i + 1] << 4);
833             } else {
834                 adpcm_compress_trellis(avctx, samples,     buf,
835                                        &c->status[0], n, channels);
836                 adpcm_compress_trellis(avctx, samples + 1, buf + n,
837                                        &c->status[1], n, channels);
838                 for (int i = 0; i < n; i++)
839                     *dst++ = buf[i] | (buf[n + i] << 4);
840             }
841             av_free(buf);
842         } else
843             for (n *= channels; n > 0; n--) {
844                 int nibble;
845                 nibble  = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
846                 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
847                 *dst++  = nibble;
848             }
849         ) /* End of CASE */
850     CASE(ADPCM_IMA_APM,
851         PutBitContext pb;
852         init_put_bits(&pb, dst, pkt_size);
853 
854         av_assert0(avctx->trellis == 0);
855 
856         for (int n = frame->nb_samples / 2; n > 0; n--) {
857             for (int ch = 0; ch < channels; ch++) {
858                 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
859                 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
860             }
861             samples += channels;
862         }
863 
864         flush_put_bits(&pb);
865         ) /* End of CASE */
866     CASE(ADPCM_IMA_AMV,
867         av_assert0(channels == 1);
868 
869         c->status[0].prev_sample = *samples;
870         bytestream_put_le16(&dst, c->status[0].prev_sample);
871         bytestream_put_byte(&dst, c->status[0].step_index);
872         bytestream_put_byte(&dst, 0);
873         bytestream_put_le32(&dst, avctx->frame_size);
874 
875         if (avctx->trellis > 0) {
876             const int n  = frame->nb_samples >> 1;
877             uint8_t *buf = av_malloc(2 * n);
878 
879             if (!buf)
880                 return AVERROR(ENOMEM);
881 
882             adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, channels);
883             for (int i = 0; i < n; i++)
884                 bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
885 
886             samples += 2 * n;
887             av_free(buf);
888         } else for (int n = frame->nb_samples >> 1; n > 0; n--) {
889             int nibble;
890             nibble  = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
891             nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
892             bytestream_put_byte(&dst, nibble);
893         }
894 
895         if (avctx->frame_size & 1) {
896             int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
897             bytestream_put_byte(&dst, nibble);
898         }
899         ) /* End of CASE */
900     CASE(ADPCM_ARGO,
901         PutBitContext pb;
902         init_put_bits(&pb, dst, pkt_size);
903 
904         av_assert0(frame->nb_samples == 32);
905 
906         for (int ch = 0; ch < channels; ch++) {
907             int64_t error  = INT64_MAX, tmperr = INT64_MAX;
908             int     shift  = 2, flag = 0;
909             int     saved1 = c->status[ch].sample1;
910             int     saved2 = c->status[ch].sample2;
911 
912             /* Find the optimal coefficients, bail early if we find a perfect result. */
913             for (int s = 2; s < 18 && tmperr != 0; s++) {
914                 for (int f = 0; f < 2 && tmperr != 0; f++) {
915                     c->status[ch].sample1 = saved1;
916                     c->status[ch].sample2 = saved2;
917                     tmperr = adpcm_argo_compress_block(c->status + ch, NULL, samples_p[ch],
918                                                        frame->nb_samples, s, f);
919                     if (tmperr < error) {
920                         shift = s;
921                         flag  = f;
922                         error = tmperr;
923                     }
924                 }
925             }
926 
927             /* Now actually do the encode. */
928             c->status[ch].sample1 = saved1;
929             c->status[ch].sample2 = saved2;
930             adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
931                                       frame->nb_samples, shift, flag);
932         }
933 
934         flush_put_bits(&pb);
935         ) /* End of CASE */
936     CASE(ADPCM_IMA_WS,
937         PutBitContext pb;
938         init_put_bits(&pb, dst, pkt_size);
939 
940         av_assert0(avctx->trellis == 0);
941         for (int n = frame->nb_samples / 2; n > 0; n--) {
942             /* stereo: 1 byte (2 samples) for left, 1 byte for right */
943             for (int ch = 0; ch < channels; ch++) {
944                 int t1, t2;
945                 t1 = adpcm_ima_compress_sample(&c->status[ch], *samples++);
946                 t2 = adpcm_ima_compress_sample(&c->status[ch], samples[st]);
947                 put_bits(&pb, 4, t2);
948                 put_bits(&pb, 4, t1);
949             }
950             samples += channels;
951         }
952         flush_put_bits(&pb);
953         ) /* End of CASE */
954     default:
955         return AVERROR(EINVAL);
956     }
957 
958     *got_packet_ptr = 1;
959     return 0;
960 }
961 
962 static const enum AVSampleFormat sample_fmts[] = {
963     AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
964 };
965 
966 static const enum AVSampleFormat sample_fmts_p[] = {
967     AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
968 };
969 
970 static const AVChannelLayout ch_layouts[] = {
971     AV_CHANNEL_LAYOUT_MONO,
972     AV_CHANNEL_LAYOUT_STEREO,
973     { 0 },
974 };
975 
976 static const AVOption options[] = {
977     {
978         .name        = "block_size",
979         .help        = "set the block size",
980         .offset      = offsetof(ADPCMEncodeContext, block_size),
981         .type        = AV_OPT_TYPE_INT,
982         .default_val = {.i64 = 1024},
983         .min         = 32,
984         .max         = 8192, /* Is this a reasonable upper limit? */
985         .flags       = AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
986     },
987     { NULL }
988 };
989 
990 static const AVClass adpcm_encoder_class = {
991     .class_name = "ADPCM encoder",
992     .item_name  = av_default_item_name,
993     .option     = options,
994     .version    = LIBAVUTIL_VERSION_INT,
995 };
996 
997 #define ADPCM_ENCODER_0(id_, name_, sample_fmts_, capabilities_, long_name_)
998 #define ADPCM_ENCODER_1(id_, name_, sample_fmts_, capabilities_, long_name_) \
999 const FFCodec ff_ ## name_ ## _encoder = {                                 \
1000     .p.name         = #name_,                                              \
1001     .p.long_name    = NULL_IF_CONFIG_SMALL(long_name_),                    \
1002     .p.type         = AVMEDIA_TYPE_AUDIO,                                  \
1003     .p.id           = id_,                                                 \
1004     .p.sample_fmts  = sample_fmts_,                                        \
1005     .p.ch_layouts   = ch_layouts,                                          \
1006     .p.capabilities = capabilities_ | AV_CODEC_CAP_DR1,                    \
1007     .p.priv_class   = &adpcm_encoder_class,                                \
1008     .priv_data_size = sizeof(ADPCMEncodeContext),                          \
1009     .init           = adpcm_encode_init,                                   \
1010     FF_CODEC_ENCODE_CB(adpcm_encode_frame),                                \
1011     .close          = adpcm_encode_close,                                  \
1012     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE, \
1013 };
1014 #define ADPCM_ENCODER_2(enabled, codec_id, name, sample_fmts, capabilities, long_name) \
1015     ADPCM_ENCODER_ ## enabled(codec_id, name, sample_fmts, capabilities, long_name)
1016 #define ADPCM_ENCODER_3(config, codec_id, name, sample_fmts, capabilities, long_name) \
1017     ADPCM_ENCODER_2(config, codec_id, name, sample_fmts, capabilities, long_name)
1018 #define ADPCM_ENCODER(codec, name, sample_fmts, capabilities, long_name) \
1019     ADPCM_ENCODER_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, \
1020                     name, sample_fmts, capabilities, long_name)
1021 
1022 ADPCM_ENCODER(ADPCM_ARGO,    adpcm_argo,    sample_fmts_p, 0,                             "ADPCM Argonaut Games")
1023 ADPCM_ENCODER(ADPCM_IMA_AMV, adpcm_ima_amv, sample_fmts,   0,                             "ADPCM IMA AMV")
1024 ADPCM_ENCODER(ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM")
1025 ADPCM_ENCODER(ADPCM_IMA_ALP, adpcm_ima_alp, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA High Voltage Software ALP")
1026 ADPCM_ENCODER(ADPCM_IMA_QT,  adpcm_ima_qt,  sample_fmts_p, 0,                             "ADPCM IMA QuickTime")
1027 ADPCM_ENCODER(ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive")
1028 ADPCM_ENCODER(ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0,                             "ADPCM IMA WAV")
1029 ADPCM_ENCODER(ADPCM_IMA_WS,  adpcm_ima_ws,  sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Westwood")
1030 ADPCM_ENCODER(ADPCM_MS,      adpcm_ms,      sample_fmts,   0,                             "ADPCM Microsoft")
1031 ADPCM_ENCODER(ADPCM_SWF,     adpcm_swf,     sample_fmts,   0,                             "ADPCM Shockwave Flash")
1032 ADPCM_ENCODER(ADPCM_YAMAHA,  adpcm_yamaha,  sample_fmts,   0,                             "ADPCM Yamaha")
1033