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