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
25 #include "libavutil/opt.h"
29 #include "bytestream.h"
31 #include "adpcm_data.h"
37 * See ADPCM decoder reference documents for codec information.
40 typedef struct TrellisPath {
45 typedef struct TrellisNode {
53 typedef struct ADPCMEncodeContext {
57 ADPCMChannelStatus status[6];
59 TrellisNode *node_buf;
60 TrellisNode **nodep_buf;
61 uint8_t *trellis_hash;
64 #define FREEZE_INTERVAL 128
66 static av_cold int adpcm_encode_init(AVCodecContext *avctx)
68 ADPCMEncodeContext *s = avctx->priv_data;
72 if (avctx->channels > 2) {
73 av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
74 return AVERROR(EINVAL);
78 * AMV's block size has to match that of the corresponding video
79 * stream. Relax the POT requirement.
81 if (avctx->codec->id != AV_CODEC_ID_ADPCM_IMA_AMV &&
82 (s->block_size & (s->block_size - 1))) {
83 av_log(avctx, AV_LOG_ERROR, "block size must be power of 2\n");
84 return AVERROR(EINVAL);
88 int frontier, max_paths;
90 if ((unsigned)avctx->trellis > 16U) {
91 av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
92 return AVERROR(EINVAL);
95 if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
96 avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
97 avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO) {
99 * The current trellis implementation doesn't work for extended
100 * runs of samples without periodic resets. Disallow it.
102 av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
103 return AVERROR_PATCHWELCOME;
106 frontier = 1 << avctx->trellis;
107 max_paths = frontier * FREEZE_INTERVAL;
108 if (!FF_ALLOC_TYPED_ARRAY(s->paths, max_paths) ||
109 !FF_ALLOC_TYPED_ARRAY(s->node_buf, 2 * frontier) ||
110 !FF_ALLOC_TYPED_ARRAY(s->nodep_buf, 2 * frontier) ||
111 !FF_ALLOC_TYPED_ARRAY(s->trellis_hash, 65536))
112 return AVERROR(ENOMEM);
115 avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
117 switch (avctx->codec->id) {
118 case AV_CODEC_ID_ADPCM_IMA_WAV:
119 /* each 16 bits sample gives one nibble
120 and we have 4 bytes per channel overhead */
121 avctx->frame_size = (s->block_size - 4 * avctx->channels) * 8 /
122 (4 * avctx->channels) + 1;
123 /* seems frame_size isn't taken into account...
124 have to buffer the samples :-( */
125 avctx->block_align = s->block_size;
126 avctx->bits_per_coded_sample = 4;
128 case AV_CODEC_ID_ADPCM_IMA_QT:
129 avctx->frame_size = 64;
130 avctx->block_align = 34 * avctx->channels;
132 case AV_CODEC_ID_ADPCM_MS:
133 /* each 16 bits sample gives one nibble
134 and we have 7 bytes per channel overhead */
135 avctx->frame_size = (s->block_size - 7 * avctx->channels) * 2 / avctx->channels + 2;
136 avctx->bits_per_coded_sample = 4;
137 avctx->block_align = s->block_size;
138 if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
139 return AVERROR(ENOMEM);
140 avctx->extradata_size = 32;
141 extradata = avctx->extradata;
142 bytestream_put_le16(&extradata, avctx->frame_size);
143 bytestream_put_le16(&extradata, 7); /* wNumCoef */
144 for (i = 0; i < 7; i++) {
145 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
146 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
149 case AV_CODEC_ID_ADPCM_YAMAHA:
150 avctx->frame_size = s->block_size * 2 / avctx->channels;
151 avctx->block_align = s->block_size;
153 case AV_CODEC_ID_ADPCM_SWF:
154 if (avctx->sample_rate != 11025 &&
155 avctx->sample_rate != 22050 &&
156 avctx->sample_rate != 44100) {
157 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
159 return AVERROR(EINVAL);
161 avctx->frame_size = 4096; /* Hardcoded according to the SWF spec. */
162 avctx->block_align = (2 + avctx->channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
164 case AV_CODEC_ID_ADPCM_IMA_SSI:
165 case AV_CODEC_ID_ADPCM_IMA_ALP:
166 avctx->frame_size = s->block_size * 2 / avctx->channels;
167 avctx->block_align = s->block_size;
169 case AV_CODEC_ID_ADPCM_IMA_AMV:
170 if (avctx->sample_rate != 22050) {
171 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
172 return AVERROR(EINVAL);
175 if (avctx->channels != 1) {
176 av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
177 return AVERROR(EINVAL);
180 avctx->frame_size = s->block_size;
181 avctx->block_align = 8 + (FFALIGN(avctx->frame_size, 2) / 2);
183 case AV_CODEC_ID_ADPCM_IMA_APM:
184 avctx->frame_size = s->block_size * 2 / avctx->channels;
185 avctx->block_align = s->block_size;
187 if (!(avctx->extradata = av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
188 return AVERROR(ENOMEM);
189 avctx->extradata_size = 28;
191 case AV_CODEC_ID_ADPCM_ARGO:
192 avctx->frame_size = 32;
193 avctx->block_align = 17 * avctx->channels;
196 return AVERROR(EINVAL);
202 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
204 ADPCMEncodeContext *s = avctx->priv_data;
206 av_freep(&s->node_buf);
207 av_freep(&s->nodep_buf);
208 av_freep(&s->trellis_hash);
214 static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
217 int delta = sample - c->prev_sample;
218 int nibble = FFMIN(7, abs(delta) * 4 /
219 ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
220 c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
221 ff_adpcm_yamaha_difflookup[nibble]) / 8);
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);
227 static inline uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)
229 const int delta = sample - c->prev_sample;
230 const int step = ff_adpcm_step_table[c->step_index];
231 const int sign = (delta < 0) * 8;
233 int nibble = FFMIN(abs(delta) * 4 / step, 7);
234 int diff = (step * nibble) >> 2;
238 nibble = sign | nibble;
240 c->prev_sample += diff;
241 c->prev_sample = av_clip_int16(c->prev_sample);
242 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
246 static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
249 int delta = sample - c->prev_sample;
250 int diff, step = ff_adpcm_step_table[c->step_index];
251 int nibble = 8*(delta < 0);
254 diff = delta + (step >> 3);
273 c->prev_sample -= diff;
275 c->prev_sample += diff;
277 c->prev_sample = av_clip_int16(c->prev_sample);
278 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
283 static inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
286 int predictor, nibble, bias;
288 predictor = (((c->sample1) * (c->coeff1)) +
289 (( c->sample2) * (c->coeff2))) / 64;
291 nibble = sample - predictor;
293 bias = c->idelta / 2;
295 bias = -c->idelta / 2;
297 nibble = (nibble + bias) / c->idelta;
298 nibble = av_clip_intp2(nibble, 3) & 0x0F;
300 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
302 c->sample2 = c->sample1;
303 c->sample1 = av_clip_int16(predictor);
305 c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
312 static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
322 delta = sample - c->predictor;
324 nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
326 c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
327 c->predictor = av_clip_int16(c->predictor);
328 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
329 c->step = av_clip(c->step, 127, 24576);
334 static void adpcm_compress_trellis(AVCodecContext *avctx,
335 const int16_t *samples, uint8_t *dst,
336 ADPCMChannelStatus *c, int n, int stride)
338 //FIXME 6% faster if frontier is a compile-time constant
339 ADPCMEncodeContext *s = avctx->priv_data;
340 const int frontier = 1 << avctx->trellis;
341 const int version = avctx->codec->id;
342 TrellisPath *paths = s->paths, *p;
343 TrellisNode *node_buf = s->node_buf;
344 TrellisNode **nodep_buf = s->nodep_buf;
345 TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
346 TrellisNode **nodes_next = nodep_buf + frontier;
347 int pathn = 0, froze = -1, i, j, k, generation = 0;
348 uint8_t *hash = s->trellis_hash;
349 memset(hash, 0xff, 65536 * sizeof(*hash));
351 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
352 nodes[0] = node_buf + frontier;
355 nodes[0]->step = c->step_index;
356 nodes[0]->sample1 = c->sample1;
357 nodes[0]->sample2 = c->sample2;
358 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
359 version == AV_CODEC_ID_ADPCM_IMA_QT ||
360 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
361 version == AV_CODEC_ID_ADPCM_SWF)
362 nodes[0]->sample1 = c->prev_sample;
363 if (version == AV_CODEC_ID_ADPCM_MS)
364 nodes[0]->step = c->idelta;
365 if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
367 nodes[0]->step = 127;
368 nodes[0]->sample1 = 0;
370 nodes[0]->step = c->step;
371 nodes[0]->sample1 = c->predictor;
375 for (i = 0; i < n; i++) {
376 TrellisNode *t = node_buf + frontier*(i&1);
378 int sample = samples[i * stride];
380 memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
381 for (j = 0; j < frontier && nodes[j]; j++) {
382 // higher j have higher ssd already, so they're likely
383 // to yield a suboptimal next sample too
384 const int range = (j < frontier / 2) ? 1 : 0;
385 const int step = nodes[j]->step;
387 if (version == AV_CODEC_ID_ADPCM_MS) {
388 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
389 (nodes[j]->sample2 * c->coeff2)) / 64;
390 const int div = (sample - predictor) / step;
391 const int nmin = av_clip(div-range, -8, 6);
392 const int nmax = av_clip(div+range, -7, 7);
393 for (nidx = nmin; nidx <= nmax; nidx++) {
394 const int nibble = nidx & 0xf;
395 int dec_sample = predictor + nidx * step;
396 #define STORE_NODE(NAME, STEP_INDEX)\
402 dec_sample = av_clip_int16(dec_sample);\
403 d = sample - dec_sample;\
404 ssd = nodes[j]->ssd + d*(unsigned)d;\
405 /* Check for wraparound, skip such samples completely. \
406 * Note, changing ssd to a 64 bit variable would be \
407 * simpler, avoiding this check, but it's slower on \
408 * x86 32 bit at the moment. */\
409 if (ssd < nodes[j]->ssd)\
411 /* Collapse any two states with the same previous sample value. \
412 * One could also distinguish states by step and by 2nd to last
413 * sample, but the effects of that are negligible.
414 * Since nodes in the previous generation are iterated
415 * through a heap, they're roughly ordered from better to
416 * worse, but not strictly ordered. Therefore, an earlier
417 * node with the same sample value is better in most cases
418 * (and thus the current is skipped), but not strictly
419 * in all cases. Only skipping samples where ssd >=
420 * ssd of the earlier node with the same sample gives
421 * slightly worse quality, though, for some reason. */ \
422 h = &hash[(uint16_t) dec_sample];\
423 if (*h == generation)\
425 if (heap_pos < frontier) {\
428 /* Try to replace one of the leaf nodes with the new \
429 * one, but try a different slot each time. */\
430 pos = (frontier >> 1) +\
431 (heap_pos & ((frontier >> 1) - 1));\
432 if (ssd > nodes_next[pos]->ssd)\
437 u = nodes_next[pos];\
439 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
441 nodes_next[pos] = u;\
445 u->step = STEP_INDEX;\
446 u->sample2 = nodes[j]->sample1;\
447 u->sample1 = dec_sample;\
448 paths[u->path].nibble = nibble;\
449 paths[u->path].prev = nodes[j]->path;\
450 /* Sift the newly inserted node up in the heap to \
451 * restore the heap property. */\
453 int parent = (pos - 1) >> 1;\
454 if (nodes_next[parent]->ssd <= ssd)\
456 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
460 STORE_NODE(ms, FFMAX(16,
461 (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
463 } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
464 version == AV_CODEC_ID_ADPCM_IMA_QT ||
465 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
466 version == AV_CODEC_ID_ADPCM_SWF) {
467 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
468 const int predictor = nodes[j]->sample1;\
469 const int div = (sample - predictor) * 4 / STEP_TABLE;\
470 int nmin = av_clip(div - range, -7, 6);\
471 int nmax = av_clip(div + range, -6, 7);\
473 nmin--; /* distinguish -0 from +0 */\
476 for (nidx = nmin; nidx <= nmax; nidx++) {\
477 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
478 int dec_sample = predictor +\
480 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
481 STORE_NODE(NAME, STEP_INDEX);\
483 LOOP_NODES(ima, ff_adpcm_step_table[step],
484 av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
485 } else { //AV_CODEC_ID_ADPCM_YAMAHA
486 LOOP_NODES(yamaha, step,
487 av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
499 if (generation == 255) {
500 memset(hash, 0xff, 65536 * sizeof(*hash));
505 if (nodes[0]->ssd > (1 << 28)) {
506 for (j = 1; j < frontier && nodes[j]; j++)
507 nodes[j]->ssd -= nodes[0]->ssd;
511 // merge old paths to save memory
512 if (i == froze + FREEZE_INTERVAL) {
513 p = &paths[nodes[0]->path];
514 for (k = i; k > froze; k--) {
520 // other nodes might use paths that don't coincide with the frozen one.
521 // checking which nodes do so is too slow, so just kill them all.
522 // this also slightly improves quality, but I don't know why.
523 memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
527 p = &paths[nodes[0]->path];
528 for (i = n - 1; i > froze; i--) {
533 c->predictor = nodes[0]->sample1;
534 c->sample1 = nodes[0]->sample1;
535 c->sample2 = nodes[0]->sample2;
536 c->step_index = nodes[0]->step;
537 c->step = nodes[0]->step;
538 c->idelta = nodes[0]->step;
541 static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
547 nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
549 nibble = 4 * s - 4 * cs->sample1;
551 return (nibble >> shift) & 0x0F;
554 static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
555 const int16_t *samples, int nsamples,
561 put_bits(pb, 4, shift - 2);
563 put_bits(pb, 1, !!flag);
567 for (int n = 0; n < nsamples; n++) {
568 /* Compress the nibble, then expand it to see how much precision we've lost. */
569 int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
570 int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
572 error += abs(samples[n] - sample);
575 put_bits(pb, 4, nibble);
581 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
582 const AVFrame *frame, int *got_packet_ptr)
584 int n, i, ch, st, pkt_size, ret;
585 const int16_t *samples;
588 ADPCMEncodeContext *c = avctx->priv_data;
591 samples = (const int16_t *)frame->data[0];
592 samples_p = (int16_t **)frame->extended_data;
593 st = avctx->channels == 2;
595 if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
596 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
597 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM)
598 pkt_size = (frame->nb_samples * avctx->channels) / 2;
600 pkt_size = avctx->block_align;
601 if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
605 switch(avctx->codec->id) {
606 case AV_CODEC_ID_ADPCM_IMA_WAV:
610 blocks = (frame->nb_samples - 1) / 8;
612 for (ch = 0; ch < avctx->channels; ch++) {
613 ADPCMChannelStatus *status = &c->status[ch];
614 status->prev_sample = samples_p[ch][0];
615 /* status->step_index = 0;
616 XXX: not sure how to init the state machine */
617 bytestream_put_le16(&dst, status->prev_sample);
618 *dst++ = status->step_index;
619 *dst++ = 0; /* unknown */
622 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
623 if (avctx->trellis > 0) {
624 if (!FF_ALLOC_TYPED_ARRAY(buf, avctx->channels * blocks * 8))
625 return AVERROR(ENOMEM);
626 for (ch = 0; ch < avctx->channels; ch++) {
627 adpcm_compress_trellis(avctx, &samples_p[ch][1],
628 buf + ch * blocks * 8, &c->status[ch],
631 for (i = 0; i < blocks; i++) {
632 for (ch = 0; ch < avctx->channels; ch++) {
633 uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
634 for (j = 0; j < 8; j += 2)
635 *dst++ = buf1[j] | (buf1[j + 1] << 4);
640 for (i = 0; i < blocks; i++) {
641 for (ch = 0; ch < avctx->channels; ch++) {
642 ADPCMChannelStatus *status = &c->status[ch];
643 const int16_t *smp = &samples_p[ch][1 + i * 8];
644 for (j = 0; j < 8; j += 2) {
645 uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
646 v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
654 case AV_CODEC_ID_ADPCM_IMA_QT:
657 init_put_bits(&pb, dst, pkt_size);
659 for (ch = 0; ch < avctx->channels; ch++) {
660 ADPCMChannelStatus *status = &c->status[ch];
661 put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
662 put_bits(&pb, 7, status->step_index);
663 if (avctx->trellis > 0) {
665 adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
667 for (i = 0; i < 64; i++)
668 put_bits(&pb, 4, buf[i ^ 1]);
669 status->prev_sample = status->predictor;
671 for (i = 0; i < 64; i += 2) {
673 t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
674 t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
675 put_bits(&pb, 4, t2);
676 put_bits(&pb, 4, t1);
684 case AV_CODEC_ID_ADPCM_IMA_SSI:
687 init_put_bits(&pb, dst, pkt_size);
689 av_assert0(avctx->trellis == 0);
691 for (i = 0; i < frame->nb_samples; i++) {
692 for (ch = 0; ch < avctx->channels; ch++) {
693 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
700 case AV_CODEC_ID_ADPCM_IMA_ALP:
703 init_put_bits(&pb, dst, pkt_size);
705 av_assert0(avctx->trellis == 0);
707 for (n = frame->nb_samples / 2; n > 0; n--) {
708 for (ch = 0; ch < avctx->channels; ch++) {
709 put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
710 put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
712 samples += avctx->channels;
718 case AV_CODEC_ID_ADPCM_SWF:
721 init_put_bits(&pb, dst, pkt_size);
723 n = frame->nb_samples - 1;
725 // store AdpcmCodeSize
726 put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
728 // init the encoder state
729 for (i = 0; i < avctx->channels; i++) {
730 // clip step so it fits 6 bits
731 c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
732 put_sbits(&pb, 16, samples[i]);
733 put_bits(&pb, 6, c->status[i].step_index);
734 c->status[i].prev_sample = samples[i];
737 if (avctx->trellis > 0) {
738 if (!(buf = av_malloc(2 * n)))
739 return AVERROR(ENOMEM);
740 adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
741 &c->status[0], n, avctx->channels);
742 if (avctx->channels == 2)
743 adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
744 buf + n, &c->status[1], n,
746 for (i = 0; i < n; i++) {
747 put_bits(&pb, 4, buf[i]);
748 if (avctx->channels == 2)
749 put_bits(&pb, 4, buf[n + i]);
753 for (i = 1; i < frame->nb_samples; i++) {
754 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
755 samples[avctx->channels * i]));
756 if (avctx->channels == 2)
757 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
758 samples[2 * i + 1]));
764 case AV_CODEC_ID_ADPCM_MS:
765 for (i = 0; i < avctx->channels; i++) {
768 c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
769 c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
771 for (i = 0; i < avctx->channels; i++) {
772 if (c->status[i].idelta < 16)
773 c->status[i].idelta = 16;
774 bytestream_put_le16(&dst, c->status[i].idelta);
776 for (i = 0; i < avctx->channels; i++)
777 c->status[i].sample2= *samples++;
778 for (i = 0; i < avctx->channels; i++) {
779 c->status[i].sample1 = *samples++;
780 bytestream_put_le16(&dst, c->status[i].sample1);
782 for (i = 0; i < avctx->channels; i++)
783 bytestream_put_le16(&dst, c->status[i].sample2);
785 if (avctx->trellis > 0) {
786 n = avctx->block_align - 7 * avctx->channels;
787 if (!(buf = av_malloc(2 * n)))
788 return AVERROR(ENOMEM);
789 if (avctx->channels == 1) {
790 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
792 for (i = 0; i < n; i += 2)
793 *dst++ = (buf[i] << 4) | buf[i + 1];
795 adpcm_compress_trellis(avctx, samples, buf,
796 &c->status[0], n, avctx->channels);
797 adpcm_compress_trellis(avctx, samples + 1, buf + n,
798 &c->status[1], n, avctx->channels);
799 for (i = 0; i < n; i++)
800 *dst++ = (buf[i] << 4) | buf[n + i];
804 for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
806 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
807 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
812 case AV_CODEC_ID_ADPCM_YAMAHA:
813 n = frame->nb_samples / 2;
814 if (avctx->trellis > 0) {
815 if (!(buf = av_malloc(2 * n * 2)))
816 return AVERROR(ENOMEM);
818 if (avctx->channels == 1) {
819 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
821 for (i = 0; i < n; i += 2)
822 *dst++ = buf[i] | (buf[i + 1] << 4);
824 adpcm_compress_trellis(avctx, samples, buf,
825 &c->status[0], n, avctx->channels);
826 adpcm_compress_trellis(avctx, samples + 1, buf + n,
827 &c->status[1], n, avctx->channels);
828 for (i = 0; i < n; i++)
829 *dst++ = buf[i] | (buf[n + i] << 4);
833 for (n *= avctx->channels; n > 0; n--) {
835 nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
836 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
840 case AV_CODEC_ID_ADPCM_IMA_APM:
843 init_put_bits(&pb, dst, pkt_size);
845 av_assert0(avctx->trellis == 0);
847 for (n = frame->nb_samples / 2; n > 0; n--) {
848 for (ch = 0; ch < avctx->channels; ch++) {
849 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
850 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
852 samples += avctx->channels;
858 case AV_CODEC_ID_ADPCM_IMA_AMV:
860 av_assert0(avctx->channels == 1);
862 c->status[0].prev_sample = *samples;
863 bytestream_put_le16(&dst, c->status[0].prev_sample);
864 bytestream_put_byte(&dst, c->status[0].step_index);
865 bytestream_put_byte(&dst, 0);
866 bytestream_put_le32(&dst, avctx->frame_size);
868 if (avctx->trellis > 0) {
869 n = frame->nb_samples >> 1;
871 if (!(buf = av_malloc(2 * n)))
872 return AVERROR(ENOMEM);
874 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, avctx->channels);
875 for (i = 0; i < n; i++)
876 bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
879 } else for (n = frame->nb_samples >> 1; n > 0; n--) {
881 nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
882 nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
883 bytestream_put_byte(&dst, nibble);
886 if (avctx->frame_size & 1) {
887 int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
888 bytestream_put_byte(&dst, nibble);
892 case AV_CODEC_ID_ADPCM_ARGO:
895 init_put_bits(&pb, dst, pkt_size);
897 av_assert0(frame->nb_samples == 32);
899 for (ch = 0; ch < avctx->channels; ch++) {
900 int64_t error = INT64_MAX, tmperr = INT64_MAX;
901 int shift = 2, flag = 0;
902 int saved1 = c->status[ch].sample1;
903 int saved2 = c->status[ch].sample2;
905 /* Find the optimal coefficients, bail early if we find a perfect result. */
906 for (int s = 2; s < 18 && tmperr != 0; s++) {
907 for (int f = 0; f < 2 && tmperr != 0; f++) {
908 c->status[ch].sample1 = saved1;
909 c->status[ch].sample2 = saved2;
910 tmperr = adpcm_argo_compress_block(c->status + ch, NULL, samples_p[ch],
911 frame->nb_samples, s, f);
912 if (tmperr < error) {
920 /* Now actually do the encode. */
921 c->status[ch].sample1 = saved1;
922 c->status[ch].sample2 = saved2;
923 adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
924 frame->nb_samples, shift, flag);
931 return AVERROR(EINVAL);
934 avpkt->size = pkt_size;
939 static const enum AVSampleFormat sample_fmts[] = {
940 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
943 static const enum AVSampleFormat sample_fmts_p[] = {
944 AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
947 static const AVOption options[] = {
949 .name = "block_size",
950 .help = "set the block size",
951 .offset = offsetof(ADPCMEncodeContext, block_size),
952 .type = AV_OPT_TYPE_INT,
953 .default_val = {.i64 = 1024},
955 .max = 8192, /* Is this a reasonable upper limit? */
956 .flags = AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
961 static const AVClass adpcm_encoder_class = {
962 .class_name = "ADPCM Encoder",
963 .item_name = av_default_item_name,
965 .version = LIBAVUTIL_VERSION_INT,
968 #define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_) \
969 AVCodec ff_ ## name_ ## _encoder = { \
971 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
972 .type = AVMEDIA_TYPE_AUDIO, \
974 .priv_data_size = sizeof(ADPCMEncodeContext), \
975 .init = adpcm_encode_init, \
976 .encode2 = adpcm_encode_frame, \
977 .close = adpcm_encode_close, \
978 .sample_fmts = sample_fmts_, \
979 .capabilities = capabilities_, \
980 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
981 .priv_class = &adpcm_encoder_class, \
984 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_ARGO, adpcm_argo, sample_fmts_p, 0, "ADPCM Argonaut Games");
985 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, sample_fmts, 0, "ADPCM IMA AMV");
986 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM");
987 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_ALP, adpcm_ima_alp, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA High Voltage Software ALP");
988 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, 0, "ADPCM IMA QuickTime");
989 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive");
990 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0, "ADPCM IMA WAV");
991 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, sample_fmts, 0, "ADPCM Microsoft");
992 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, sample_fmts, 0, "ADPCM Shockwave Flash");
993 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, 0, "ADPCM Yamaha");