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