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