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