]> git.sesse.net Git - ffmpeg/blob - libavcodec/adpcmenc.c
wavpack: Reset internal state on corrupted blocks
[ffmpeg] / libavcodec / adpcmenc.c
1 /*
2  * Copyright (c) 2001-2003 The ffmpeg Project
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "avcodec.h"
22 #include "get_bits.h"
23 #include "put_bits.h"
24 #include "bytestream.h"
25 #include "adpcm.h"
26 #include "adpcm_data.h"
27
28 /**
29  * @file
30  * ADPCM encoders
31  * First version by Francois Revol (revol@free.fr)
32  * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
33  *   by Mike Melanson (melanson@pcisys.net)
34  *
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     if (avctx->channels > 2)
67         return -1; /* only stereo or mono =) */
68
69     if(avctx->trellis && (unsigned)avctx->trellis > 16U){
70         av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
71         return -1;
72     }
73
74     if (avctx->trellis) {
75         int frontier = 1 << avctx->trellis;
76         int max_paths =  frontier * FREEZE_INTERVAL;
77         FF_ALLOC_OR_GOTO(avctx, s->paths,     max_paths * sizeof(*s->paths), error);
78         FF_ALLOC_OR_GOTO(avctx, s->node_buf,  2 * frontier * sizeof(*s->node_buf), error);
79         FF_ALLOC_OR_GOTO(avctx, s->nodep_buf, 2 * frontier * sizeof(*s->nodep_buf), error);
80         FF_ALLOC_OR_GOTO(avctx, s->trellis_hash, 65536 * sizeof(*s->trellis_hash), error);
81     }
82
83     avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
84
85     switch(avctx->codec->id) {
86     case CODEC_ID_ADPCM_IMA_WAV:
87         avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
88                                                              /* and we have 4 bytes per channel overhead */
89         avctx->block_align = BLKSIZE;
90         /* seems frame_size isn't taken into account... have to buffer the samples :-( */
91         break;
92     case CODEC_ID_ADPCM_IMA_QT:
93         avctx->frame_size = 64;
94         avctx->block_align = 34 * avctx->channels;
95         break;
96     case CODEC_ID_ADPCM_MS:
97         avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
98                                                              /* and we have 7 bytes per channel overhead */
99         avctx->block_align = BLKSIZE;
100         avctx->extradata_size = 32;
101         extradata = avctx->extradata = av_malloc(avctx->extradata_size);
102         if (!extradata)
103             return AVERROR(ENOMEM);
104         bytestream_put_le16(&extradata, avctx->frame_size);
105         bytestream_put_le16(&extradata, 7); /* wNumCoef */
106         for (i = 0; i < 7; i++) {
107             bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
108             bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
109         }
110         break;
111     case CODEC_ID_ADPCM_YAMAHA:
112         avctx->frame_size = BLKSIZE * avctx->channels;
113         avctx->block_align = BLKSIZE;
114         break;
115     case CODEC_ID_ADPCM_SWF:
116         if (avctx->sample_rate != 11025 &&
117             avctx->sample_rate != 22050 &&
118             avctx->sample_rate != 44100) {
119             av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
120             goto error;
121         }
122         avctx->frame_size = 512 * (avctx->sample_rate / 11025);
123         break;
124     default:
125         goto error;
126     }
127
128     avctx->coded_frame= avcodec_alloc_frame();
129     avctx->coded_frame->key_frame= 1;
130
131     return 0;
132 error:
133     av_freep(&s->paths);
134     av_freep(&s->node_buf);
135     av_freep(&s->nodep_buf);
136     av_freep(&s->trellis_hash);
137     return -1;
138 }
139
140 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
141 {
142     ADPCMEncodeContext *s = avctx->priv_data;
143     av_freep(&avctx->coded_frame);
144     av_freep(&s->paths);
145     av_freep(&s->node_buf);
146     av_freep(&s->nodep_buf);
147     av_freep(&s->trellis_hash);
148
149     return 0;
150 }
151
152
153 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
154 {
155     int delta = sample - c->prev_sample;
156     int nibble = FFMIN(7, abs(delta)*4/ff_adpcm_step_table[c->step_index]) + (delta<0)*8;
157     c->prev_sample += ((ff_adpcm_step_table[c->step_index] * ff_adpcm_yamaha_difflookup[nibble]) / 8);
158     c->prev_sample = av_clip_int16(c->prev_sample);
159     c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
160     return nibble;
161 }
162
163 static inline unsigned char adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, short sample)
164 {
165     int delta = sample - c->prev_sample;
166     int mask, step = ff_adpcm_step_table[c->step_index];
167     int diff = step >> 3;
168     int nibble = 0;
169
170     if (delta < 0) {
171         nibble = 8;
172         delta = -delta;
173     }
174
175     for (mask = 4; mask;) {
176         if (delta >= step) {
177             nibble |= mask;
178             delta -= step;
179             diff += step;
180         }
181         step >>= 1;
182         mask >>= 1;
183     }
184
185     if (nibble & 8)
186         c->prev_sample -= diff;
187     else
188         c->prev_sample += diff;
189
190     c->prev_sample = av_clip_int16(c->prev_sample);
191     c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
192
193     return nibble;
194 }
195
196 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
197 {
198     int predictor, nibble, bias;
199
200     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
201
202     nibble= sample - predictor;
203     if(nibble>=0) bias= c->idelta/2;
204     else          bias=-c->idelta/2;
205
206     nibble= (nibble + bias) / c->idelta;
207     nibble= av_clip(nibble, -8, 7)&0x0F;
208
209     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
210
211     c->sample2 = c->sample1;
212     c->sample1 = av_clip_int16(predictor);
213
214     c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
215     if (c->idelta < 16) c->idelta = 16;
216
217     return nibble;
218 }
219
220 static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
221 {
222     int nibble, delta;
223
224     if(!c->step) {
225         c->predictor = 0;
226         c->step = 127;
227     }
228
229     delta = sample - c->predictor;
230
231     nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
232
233     c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
234     c->predictor = av_clip_int16(c->predictor);
235     c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
236     c->step = av_clip(c->step, 127, 24567);
237
238     return nibble;
239 }
240
241 static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
242                                    uint8_t *dst, ADPCMChannelStatus *c, int n)
243 {
244     //FIXME 6% faster if frontier is a compile-time constant
245     ADPCMEncodeContext *s = avctx->priv_data;
246     const int frontier = 1 << avctx->trellis;
247     const int stride = avctx->channels;
248     const int version = avctx->codec->id;
249     TrellisPath *paths = s->paths, *p;
250     TrellisNode *node_buf = s->node_buf;
251     TrellisNode **nodep_buf = s->nodep_buf;
252     TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
253     TrellisNode **nodes_next = nodep_buf + frontier;
254     int pathn = 0, froze = -1, i, j, k, generation = 0;
255     uint8_t *hash = s->trellis_hash;
256     memset(hash, 0xff, 65536 * sizeof(*hash));
257
258     memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
259     nodes[0] = node_buf + frontier;
260     nodes[0]->ssd = 0;
261     nodes[0]->path = 0;
262     nodes[0]->step = c->step_index;
263     nodes[0]->sample1 = c->sample1;
264     nodes[0]->sample2 = c->sample2;
265     if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_IMA_QT) || (version == CODEC_ID_ADPCM_SWF))
266         nodes[0]->sample1 = c->prev_sample;
267     if(version == CODEC_ID_ADPCM_MS)
268         nodes[0]->step = c->idelta;
269     if(version == CODEC_ID_ADPCM_YAMAHA) {
270         if(c->step == 0) {
271             nodes[0]->step = 127;
272             nodes[0]->sample1 = 0;
273         } else {
274             nodes[0]->step = c->step;
275             nodes[0]->sample1 = c->predictor;
276         }
277     }
278
279     for(i=0; i<n; i++) {
280         TrellisNode *t = node_buf + frontier*(i&1);
281         TrellisNode **u;
282         int sample = samples[i*stride];
283         int heap_pos = 0;
284         memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
285         for(j=0; j<frontier && nodes[j]; j++) {
286             // higher j have higher ssd already, so they're likely to yield a suboptimal next sample too
287             const int range = (j < frontier/2) ? 1 : 0;
288             const int step = nodes[j]->step;
289             int nidx;
290             if(version == CODEC_ID_ADPCM_MS) {
291                 const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 64;
292                 const int div = (sample - predictor) / step;
293                 const int nmin = av_clip(div-range, -8, 6);
294                 const int nmax = av_clip(div+range, -7, 7);
295                 for(nidx=nmin; nidx<=nmax; nidx++) {
296                     const int nibble = nidx & 0xf;
297                     int dec_sample = predictor + nidx * step;
298 #define STORE_NODE(NAME, STEP_INDEX)\
299                     int d;\
300                     uint32_t ssd;\
301                     int pos;\
302                     TrellisNode *u;\
303                     uint8_t *h;\
304                     dec_sample = av_clip_int16(dec_sample);\
305                     d = sample - dec_sample;\
306                     ssd = nodes[j]->ssd + d*d;\
307                     /* Check for wraparound, skip such samples completely. \
308                      * Note, changing ssd to a 64 bit variable would be \
309                      * simpler, avoiding this check, but it's slower on \
310                      * x86 32 bit at the moment. */\
311                     if (ssd < nodes[j]->ssd)\
312                         goto next_##NAME;\
313                     /* Collapse any two states with the same previous sample value. \
314                      * One could also distinguish states by step and by 2nd to last
315                      * sample, but the effects of that are negligible.
316                      * Since nodes in the previous generation are iterated
317                      * through a heap, they're roughly ordered from better to
318                      * worse, but not strictly ordered. Therefore, an earlier
319                      * node with the same sample value is better in most cases
320                      * (and thus the current is skipped), but not strictly
321                      * in all cases. Only skipping samples where ssd >=
322                      * ssd of the earlier node with the same sample gives
323                      * slightly worse quality, though, for some reason. */ \
324                     h = &hash[(uint16_t) dec_sample];\
325                     if (*h == generation)\
326                         goto next_##NAME;\
327                     if (heap_pos < frontier) {\
328                         pos = heap_pos++;\
329                     } else {\
330                         /* Try to replace one of the leaf nodes with the new \
331                          * one, but try a different slot each time. */\
332                         pos = (frontier >> 1) + (heap_pos & ((frontier >> 1) - 1));\
333                         if (ssd > nodes_next[pos]->ssd)\
334                             goto next_##NAME;\
335                         heap_pos++;\
336                     }\
337                     *h = generation;\
338                     u = nodes_next[pos];\
339                     if(!u) {\
340                         assert(pathn < FREEZE_INTERVAL<<avctx->trellis);\
341                         u = t++;\
342                         nodes_next[pos] = u;\
343                         u->path = pathn++;\
344                     }\
345                     u->ssd = ssd;\
346                     u->step = STEP_INDEX;\
347                     u->sample2 = nodes[j]->sample1;\
348                     u->sample1 = dec_sample;\
349                     paths[u->path].nibble = nibble;\
350                     paths[u->path].prev = nodes[j]->path;\
351                     /* Sift the newly inserted node up in the heap to \
352                      * restore the heap property. */\
353                     while (pos > 0) {\
354                         int parent = (pos - 1) >> 1;\
355                         if (nodes_next[parent]->ssd <= ssd)\
356                             break;\
357                         FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
358                         pos = parent;\
359                     }\
360                     next_##NAME:;
361                     STORE_NODE(ms, FFMAX(16, (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
362                 }
363             } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_IMA_QT)|| (version == CODEC_ID_ADPCM_SWF)) {
364 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
365                 const int predictor = nodes[j]->sample1;\
366                 const int div = (sample - predictor) * 4 / STEP_TABLE;\
367                 int nmin = av_clip(div-range, -7, 6);\
368                 int nmax = av_clip(div+range, -6, 7);\
369                 if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
370                 if(nmax<0) nmax--;\
371                 for(nidx=nmin; nidx<=nmax; nidx++) {\
372                     const int nibble = nidx<0 ? 7-nidx : nidx;\
373                     int dec_sample = predictor + (STEP_TABLE * ff_adpcm_yamaha_difflookup[nibble]) / 8;\
374                     STORE_NODE(NAME, STEP_INDEX);\
375                 }
376                 LOOP_NODES(ima, ff_adpcm_step_table[step], av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
377             } else { //CODEC_ID_ADPCM_YAMAHA
378                 LOOP_NODES(yamaha, step, av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8, 127, 24567));
379 #undef LOOP_NODES
380 #undef STORE_NODE
381             }
382         }
383
384         u = nodes;
385         nodes = nodes_next;
386         nodes_next = u;
387
388         generation++;
389         if (generation == 255) {
390             memset(hash, 0xff, 65536 * sizeof(*hash));
391             generation = 0;
392         }
393
394         // prevent overflow
395         if(nodes[0]->ssd > (1<<28)) {
396             for(j=1; j<frontier && nodes[j]; j++)
397                 nodes[j]->ssd -= nodes[0]->ssd;
398             nodes[0]->ssd = 0;
399         }
400
401         // merge old paths to save memory
402         if(i == froze + FREEZE_INTERVAL) {
403             p = &paths[nodes[0]->path];
404             for(k=i; k>froze; k--) {
405                 dst[k] = p->nibble;
406                 p = &paths[p->prev];
407             }
408             froze = i;
409             pathn = 0;
410             // other nodes might use paths that don't coincide with the frozen one.
411             // checking which nodes do so is too slow, so just kill them all.
412             // this also slightly improves quality, but I don't know why.
413             memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
414         }
415     }
416
417     p = &paths[nodes[0]->path];
418     for(i=n-1; i>froze; i--) {
419         dst[i] = p->nibble;
420         p = &paths[p->prev];
421     }
422
423     c->predictor = nodes[0]->sample1;
424     c->sample1 = nodes[0]->sample1;
425     c->sample2 = nodes[0]->sample2;
426     c->step_index = nodes[0]->step;
427     c->step = nodes[0]->step;
428     c->idelta = nodes[0]->step;
429 }
430
431 static int adpcm_encode_frame(AVCodecContext *avctx,
432                             unsigned char *frame, int buf_size, void *data)
433 {
434     int n, i, st;
435     short *samples;
436     unsigned char *dst;
437     ADPCMEncodeContext *c = avctx->priv_data;
438     uint8_t *buf;
439
440     dst = frame;
441     samples = (short *)data;
442     st= avctx->channels == 2;
443 /*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
444
445     switch(avctx->codec->id) {
446     case CODEC_ID_ADPCM_IMA_WAV:
447         n = avctx->frame_size / 8;
448             c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
449 /*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
450             bytestream_put_le16(&dst, c->status[0].prev_sample);
451             *dst++ = (unsigned char)c->status[0].step_index;
452             *dst++ = 0; /* unknown */
453             samples++;
454             if (avctx->channels == 2) {
455                 c->status[1].prev_sample = (signed short)samples[0];
456 /*                c->status[1].step_index = 0; */
457                 bytestream_put_le16(&dst, c->status[1].prev_sample);
458                 *dst++ = (unsigned char)c->status[1].step_index;
459                 *dst++ = 0;
460                 samples++;
461             }
462
463             /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
464             if(avctx->trellis > 0) {
465                 FF_ALLOC_OR_GOTO(avctx, buf, 2*n*8, error);
466                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n*8);
467                 if(avctx->channels == 2)
468                     adpcm_compress_trellis(avctx, samples+1, buf + n*8, &c->status[1], n*8);
469                 for(i=0; i<n; i++) {
470                     *dst++ = buf[8*i+0] | (buf[8*i+1] << 4);
471                     *dst++ = buf[8*i+2] | (buf[8*i+3] << 4);
472                     *dst++ = buf[8*i+4] | (buf[8*i+5] << 4);
473                     *dst++ = buf[8*i+6] | (buf[8*i+7] << 4);
474                     if (avctx->channels == 2) {
475                         uint8_t *buf1 = buf + n*8;
476                         *dst++ = buf1[8*i+0] | (buf1[8*i+1] << 4);
477                         *dst++ = buf1[8*i+2] | (buf1[8*i+3] << 4);
478                         *dst++ = buf1[8*i+4] | (buf1[8*i+5] << 4);
479                         *dst++ = buf1[8*i+6] | (buf1[8*i+7] << 4);
480                     }
481                 }
482                 av_free(buf);
483             } else
484             for (; n>0; n--) {
485                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
486                 *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
487                 dst++;
488                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
489                 *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
490                 dst++;
491                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
492                 *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
493                 dst++;
494                 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
495                 *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
496                 dst++;
497                 /* right channel */
498                 if (avctx->channels == 2) {
499                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
500                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
501                     dst++;
502                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
503                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
504                     dst++;
505                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
506                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
507                     dst++;
508                     *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
509                     *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
510                     dst++;
511                 }
512                 samples += 8 * avctx->channels;
513             }
514         break;
515     case CODEC_ID_ADPCM_IMA_QT:
516     {
517         int ch, i;
518         PutBitContext pb;
519         init_put_bits(&pb, dst, buf_size*8);
520
521         for(ch=0; ch<avctx->channels; ch++){
522             put_bits(&pb, 9, (c->status[ch].prev_sample + 0x10000) >> 7);
523             put_bits(&pb, 7, c->status[ch].step_index);
524             if(avctx->trellis > 0) {
525                 uint8_t buf[64];
526                 adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
527                 for(i=0; i<64; i++)
528                     put_bits(&pb, 4, buf[i^1]);
529             } else {
530                 for (i=0; i<64; i+=2){
531                     int t1, t2;
532                     t1 = adpcm_ima_qt_compress_sample(&c->status[ch], samples[avctx->channels*(i+0)+ch]);
533                     t2 = adpcm_ima_qt_compress_sample(&c->status[ch], samples[avctx->channels*(i+1)+ch]);
534                     put_bits(&pb, 4, t2);
535                     put_bits(&pb, 4, t1);
536                 }
537             }
538         }
539
540         flush_put_bits(&pb);
541         dst += put_bits_count(&pb)>>3;
542         break;
543     }
544     case CODEC_ID_ADPCM_SWF:
545     {
546         int i;
547         PutBitContext pb;
548         init_put_bits(&pb, dst, buf_size*8);
549
550         n = avctx->frame_size-1;
551
552         //Store AdpcmCodeSize
553         put_bits(&pb, 2, 2);                //Set 4bits flash adpcm format
554
555         //Init the encoder state
556         for(i=0; i<avctx->channels; i++){
557             c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
558             put_sbits(&pb, 16, samples[i]);
559             put_bits(&pb, 6, c->status[i].step_index);
560             c->status[i].prev_sample = (signed short)samples[i];
561         }
562
563         if(avctx->trellis > 0) {
564             FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
565             adpcm_compress_trellis(avctx, samples+2, buf, &c->status[0], n);
566             if (avctx->channels == 2)
567                 adpcm_compress_trellis(avctx, samples+3, buf+n, &c->status[1], n);
568             for(i=0; i<n; i++) {
569                 put_bits(&pb, 4, buf[i]);
570                 if (avctx->channels == 2)
571                     put_bits(&pb, 4, buf[n+i]);
572             }
573             av_free(buf);
574         } else {
575             for (i=1; i<avctx->frame_size; i++) {
576                 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
577                 if (avctx->channels == 2)
578                     put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
579             }
580         }
581         flush_put_bits(&pb);
582         dst += put_bits_count(&pb)>>3;
583         break;
584     }
585     case CODEC_ID_ADPCM_MS:
586         for(i=0; i<avctx->channels; i++){
587             int predictor=0;
588
589             *dst++ = predictor;
590             c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
591             c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
592         }
593         for(i=0; i<avctx->channels; i++){
594             if (c->status[i].idelta < 16)
595                 c->status[i].idelta = 16;
596
597             bytestream_put_le16(&dst, c->status[i].idelta);
598         }
599         for(i=0; i<avctx->channels; i++){
600             c->status[i].sample2= *samples++;
601         }
602         for(i=0; i<avctx->channels; i++){
603             c->status[i].sample1= *samples++;
604
605             bytestream_put_le16(&dst, c->status[i].sample1);
606         }
607         for(i=0; i<avctx->channels; i++)
608             bytestream_put_le16(&dst, c->status[i].sample2);
609
610         if(avctx->trellis > 0) {
611             int n = avctx->block_align - 7*avctx->channels;
612             FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
613             if(avctx->channels == 1) {
614                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
615                 for(i=0; i<n; i+=2)
616                     *dst++ = (buf[i] << 4) | buf[i+1];
617             } else {
618                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
619                 adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
620                 for(i=0; i<n; i++)
621                     *dst++ = (buf[i] << 4) | buf[n+i];
622             }
623             av_free(buf);
624         } else
625         for(i=7*avctx->channels; i<avctx->block_align; i++) {
626             int nibble;
627             nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
628             nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
629             *dst++ = nibble;
630         }
631         break;
632     case CODEC_ID_ADPCM_YAMAHA:
633         n = avctx->frame_size / 2;
634         if(avctx->trellis > 0) {
635             FF_ALLOC_OR_GOTO(avctx, buf, 2*n*2, error);
636             n *= 2;
637             if(avctx->channels == 1) {
638                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
639                 for(i=0; i<n; i+=2)
640                     *dst++ = buf[i] | (buf[i+1] << 4);
641             } else {
642                 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
643                 adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
644                 for(i=0; i<n; i++)
645                     *dst++ = buf[i] | (buf[n+i] << 4);
646             }
647             av_free(buf);
648         } else
649             for (n *= avctx->channels; n>0; n--) {
650                 int nibble;
651                 nibble  = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
652                 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
653                 *dst++ = nibble;
654             }
655         break;
656     default:
657     error:
658         return -1;
659     }
660     return dst - frame;
661 }
662
663
664 #define ADPCM_ENCODER(id_, name_, long_name_)               \
665 AVCodec ff_ ## name_ ## _encoder = {                        \
666     .name           = #name_,                               \
667     .type           = AVMEDIA_TYPE_AUDIO,                   \
668     .id             = id_,                                  \
669     .priv_data_size = sizeof(ADPCMEncodeContext),           \
670     .init           = adpcm_encode_init,                    \
671     .encode         = adpcm_encode_frame,                   \
672     .close          = adpcm_encode_close,                   \
673     .sample_fmts    = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE}, \
674     .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
675 }
676
677 ADPCM_ENCODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
678 ADPCM_ENCODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
679 ADPCM_ENCODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
680 ADPCM_ENCODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
681 ADPCM_ENCODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");