2 * Copyright (c) 2001-2003 The FFmpeg project
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)
8 * This file is part of FFmpeg.
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.
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.
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
27 #include "bytestream.h"
29 #include "adpcm_data.h"
35 * See ADPCM decoder reference documents for codec information.
38 typedef struct TrellisPath {
43 typedef struct TrellisNode {
51 typedef struct ADPCMEncodeContext {
52 ADPCMChannelStatus status[6];
54 TrellisNode *node_buf;
55 TrellisNode **nodep_buf;
56 uint8_t *trellis_hash;
59 #define FREEZE_INTERVAL 128
61 static av_cold int adpcm_encode_init(AVCodecContext *avctx)
63 ADPCMEncodeContext *s = avctx->priv_data;
67 if (avctx->channels > 2) {
68 av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
69 return AVERROR(EINVAL);
73 int frontier, max_paths;
75 if ((unsigned)avctx->trellis > 16U) {
76 av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
77 return AVERROR(EINVAL);
80 if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
81 avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM) {
83 * The current trellis implementation doesn't work for extended
84 * runs of samples without periodic resets. Disallow it.
86 av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
87 return AVERROR_PATCHWELCOME;
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);
99 avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
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;
112 case AV_CODEC_ID_ADPCM_IMA_QT:
113 avctx->frame_size = 64;
114 avctx->block_align = 34 * avctx->channels;
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);
133 case AV_CODEC_ID_ADPCM_YAMAHA:
134 avctx->frame_size = BLKSIZE * 2 / avctx->channels;
135 avctx->block_align = BLKSIZE;
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, "
143 return AVERROR(EINVAL);
145 avctx->frame_size = 512 * (avctx->sample_rate / 11025);
147 case AV_CODEC_ID_ADPCM_IMA_SSI:
148 avctx->frame_size = BLKSIZE * 2 / avctx->channels;
149 avctx->block_align = BLKSIZE;
151 case AV_CODEC_ID_ADPCM_IMA_APM:
152 avctx->frame_size = BLKSIZE * 2 / avctx->channels;
153 avctx->block_align = BLKSIZE;
155 if (!(avctx->extradata = av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
156 return AVERROR(ENOMEM);
157 avctx->extradata_size = 28;
160 return AVERROR(EINVAL);
166 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
168 ADPCMEncodeContext *s = avctx->priv_data;
170 av_freep(&s->node_buf);
171 av_freep(&s->nodep_buf);
172 av_freep(&s->trellis_hash);
178 static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
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);
191 static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
194 int delta = sample - c->prev_sample;
195 int diff, step = ff_adpcm_step_table[c->step_index];
196 int nibble = 8*(delta < 0);
199 diff = delta + (step >> 3);
218 c->prev_sample -= diff;
220 c->prev_sample += diff;
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);
228 static inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
231 int predictor, nibble, bias;
233 predictor = (((c->sample1) * (c->coeff1)) +
234 (( c->sample2) * (c->coeff2))) / 64;
236 nibble = sample - predictor;
238 bias = c->idelta / 2;
240 bias = -c->idelta / 2;
242 nibble = (nibble + bias) / c->idelta;
243 nibble = av_clip_intp2(nibble, 3) & 0x0F;
245 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
247 c->sample2 = c->sample1;
248 c->sample1 = av_clip_int16(predictor);
250 c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
257 static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
267 delta = sample - c->predictor;
269 nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
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);
279 static void adpcm_compress_trellis(AVCodecContext *avctx,
280 const int16_t *samples, uint8_t *dst,
281 ADPCMChannelStatus *c, int n, int stride)
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));
296 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
297 nodes[0] = node_buf + frontier;
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) {
311 nodes[0]->step = 127;
312 nodes[0]->sample1 = 0;
314 nodes[0]->step = c->step;
315 nodes[0]->sample1 = c->predictor;
319 for (i = 0; i < n; i++) {
320 TrellisNode *t = node_buf + frontier*(i&1);
322 int sample = samples[i * stride];
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;
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)\
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)\
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)\
369 if (heap_pos < frontier) {\
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)\
381 u = nodes_next[pos];\
383 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
385 nodes_next[pos] = u;\
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. */\
397 int parent = (pos - 1) >> 1;\
398 if (nodes_next[parent]->ssd <= ssd)\
400 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
404 STORE_NODE(ms, FFMAX(16,
405 (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
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);\
416 nmin--; /* distinguish -0 from +0 */\
419 for (nidx = nmin; nidx <= nmax; nidx++) {\
420 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
421 int dec_sample = predictor +\
423 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
424 STORE_NODE(NAME, STEP_INDEX);\
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,
442 if (generation == 255) {
443 memset(hash, 0xff, 65536 * sizeof(*hash));
448 if (nodes[0]->ssd > (1 << 28)) {
449 for (j = 1; j < frontier && nodes[j]; j++)
450 nodes[j]->ssd -= nodes[0]->ssd;
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--) {
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*));
470 p = &paths[nodes[0]->path];
471 for (i = n - 1; i > froze; i--) {
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;
484 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
485 const AVFrame *frame, int *got_packet_ptr)
487 int n, i, ch, st, pkt_size, ret;
488 const int16_t *samples;
491 ADPCMEncodeContext *c = avctx->priv_data;
494 samples = (const int16_t *)frame->data[0];
495 samples_p = (int16_t **)frame->extended_data;
496 st = avctx->channels == 2;
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;
504 pkt_size = avctx->block_align;
505 if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
509 switch(avctx->codec->id) {
510 case AV_CODEC_ID_ADPCM_IMA_WAV:
514 blocks = (frame->nb_samples - 1) / 8;
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 */
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],
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);
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;
558 case AV_CODEC_ID_ADPCM_IMA_QT:
561 init_put_bits(&pb, dst, pkt_size);
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) {
569 adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
571 for (i = 0; i < 64; i++)
572 put_bits(&pb, 4, buf[i ^ 1]);
573 status->prev_sample = status->predictor;
575 for (i = 0; i < 64; i += 2) {
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);
588 case AV_CODEC_ID_ADPCM_IMA_SSI:
591 init_put_bits(&pb, dst, pkt_size);
593 av_assert0(avctx->trellis == 0);
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++));
604 case AV_CODEC_ID_ADPCM_SWF:
607 init_put_bits(&pb, dst, pkt_size);
609 n = frame->nb_samples - 1;
611 // store AdpcmCodeSize
612 put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
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];
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,
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]);
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]));
650 case AV_CODEC_ID_ADPCM_MS:
651 for (i = 0; i < avctx->channels; i++) {
654 c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
655 c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
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);
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);
668 for (i = 0; i < avctx->channels; i++)
669 bytestream_put_le16(&dst, c->status[i].sample2);
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,
678 for (i = 0; i < n; i += 2)
679 *dst++ = (buf[i] << 4) | buf[i + 1];
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];
690 for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
692 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
693 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
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);
704 if (avctx->channels == 1) {
705 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
707 for (i = 0; i < n; i += 2)
708 *dst++ = buf[i] | (buf[i + 1] << 4);
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);
719 for (n *= avctx->channels; n > 0; n--) {
721 nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
722 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
726 case AV_CODEC_ID_ADPCM_IMA_APM:
729 init_put_bits(&pb, dst, pkt_size);
731 av_assert0(avctx->trellis == 0);
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]));
738 samples += avctx->channels;
745 return AVERROR(EINVAL);
748 avpkt->size = pkt_size;
753 static const enum AVSampleFormat sample_fmts[] = {
754 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
757 static const enum AVSampleFormat sample_fmts_p[] = {
758 AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
761 #define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_) \
762 AVCodec ff_ ## name_ ## _encoder = { \
764 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
765 .type = AVMEDIA_TYPE_AUDIO, \
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, \
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");