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