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