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);
77 if (s->block_size & (s->block_size - 1)) {
78 av_log(avctx, AV_LOG_ERROR, "block size must be power of 2\n");
79 return AVERROR(EINVAL);
83 int frontier, max_paths;
85 if ((unsigned)avctx->trellis > 16U) {
86 av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
87 return AVERROR(EINVAL);
90 if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
91 avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
92 avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO) {
94 * The current trellis implementation doesn't work for extended
95 * runs of samples without periodic resets. Disallow it.
97 av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
98 return AVERROR_PATCHWELCOME;
101 frontier = 1 << avctx->trellis;
102 max_paths = frontier * FREEZE_INTERVAL;
103 if (!FF_ALLOC_TYPED_ARRAY(s->paths, max_paths) ||
104 !FF_ALLOC_TYPED_ARRAY(s->node_buf, 2 * frontier) ||
105 !FF_ALLOC_TYPED_ARRAY(s->nodep_buf, 2 * frontier) ||
106 !FF_ALLOC_TYPED_ARRAY(s->trellis_hash, 65536))
107 return AVERROR(ENOMEM);
110 avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
112 switch (avctx->codec->id) {
113 case AV_CODEC_ID_ADPCM_IMA_WAV:
114 /* each 16 bits sample gives one nibble
115 and we have 4 bytes per channel overhead */
116 avctx->frame_size = (s->block_size - 4 * avctx->channels) * 8 /
117 (4 * avctx->channels) + 1;
118 /* seems frame_size isn't taken into account...
119 have to buffer the samples :-( */
120 avctx->block_align = s->block_size;
121 avctx->bits_per_coded_sample = 4;
123 case AV_CODEC_ID_ADPCM_IMA_QT:
124 avctx->frame_size = 64;
125 avctx->block_align = 34 * avctx->channels;
127 case AV_CODEC_ID_ADPCM_MS:
128 /* each 16 bits sample gives one nibble
129 and we have 7 bytes per channel overhead */
130 avctx->frame_size = (s->block_size - 7 * avctx->channels) * 2 / avctx->channels + 2;
131 avctx->bits_per_coded_sample = 4;
132 avctx->block_align = s->block_size;
133 if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
134 return AVERROR(ENOMEM);
135 avctx->extradata_size = 32;
136 extradata = avctx->extradata;
137 bytestream_put_le16(&extradata, avctx->frame_size);
138 bytestream_put_le16(&extradata, 7); /* wNumCoef */
139 for (i = 0; i < 7; i++) {
140 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
141 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
144 case AV_CODEC_ID_ADPCM_YAMAHA:
145 avctx->frame_size = s->block_size * 2 / avctx->channels;
146 avctx->block_align = s->block_size;
148 case AV_CODEC_ID_ADPCM_SWF:
149 if (avctx->sample_rate != 11025 &&
150 avctx->sample_rate != 22050 &&
151 avctx->sample_rate != 44100) {
152 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
154 return AVERROR(EINVAL);
156 avctx->frame_size = (s->block_size / 2) * (avctx->sample_rate / 11025);
157 avctx->block_align = (2 + avctx->channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
159 case AV_CODEC_ID_ADPCM_IMA_SSI:
160 case AV_CODEC_ID_ADPCM_IMA_ALP:
161 avctx->frame_size = s->block_size * 2 / avctx->channels;
162 avctx->block_align = s->block_size;
164 case AV_CODEC_ID_ADPCM_IMA_APM:
165 avctx->frame_size = s->block_size * 2 / avctx->channels;
166 avctx->block_align = s->block_size;
168 if (!(avctx->extradata = av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
169 return AVERROR(ENOMEM);
170 avctx->extradata_size = 28;
172 case AV_CODEC_ID_ADPCM_ARGO:
173 avctx->frame_size = 32;
174 avctx->block_align = 17 * avctx->channels;
177 return AVERROR(EINVAL);
183 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
185 ADPCMEncodeContext *s = avctx->priv_data;
187 av_freep(&s->node_buf);
188 av_freep(&s->nodep_buf);
189 av_freep(&s->trellis_hash);
195 static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
198 int delta = sample - c->prev_sample;
199 int nibble = FFMIN(7, abs(delta) * 4 /
200 ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
201 c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
202 ff_adpcm_yamaha_difflookup[nibble]) / 8);
203 c->prev_sample = av_clip_int16(c->prev_sample);
204 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
208 static inline uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)
210 const int delta = sample - c->prev_sample;
211 const int step = ff_adpcm_step_table[c->step_index];
212 const int sign = (delta < 0) * 8;
214 int nibble = FFMIN(abs(delta) * 4 / step, 7);
215 int diff = (step * nibble) >> 2;
219 nibble = sign | nibble;
221 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);
227 static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
230 int delta = sample - c->prev_sample;
231 int diff, step = ff_adpcm_step_table[c->step_index];
232 int nibble = 8*(delta < 0);
235 diff = delta + (step >> 3);
254 c->prev_sample -= diff;
256 c->prev_sample += diff;
258 c->prev_sample = av_clip_int16(c->prev_sample);
259 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
264 static inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
267 int predictor, nibble, bias;
269 predictor = (((c->sample1) * (c->coeff1)) +
270 (( c->sample2) * (c->coeff2))) / 64;
272 nibble = sample - predictor;
274 bias = c->idelta / 2;
276 bias = -c->idelta / 2;
278 nibble = (nibble + bias) / c->idelta;
279 nibble = av_clip_intp2(nibble, 3) & 0x0F;
281 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
283 c->sample2 = c->sample1;
284 c->sample1 = av_clip_int16(predictor);
286 c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
293 static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
303 delta = sample - c->predictor;
305 nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
307 c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
308 c->predictor = av_clip_int16(c->predictor);
309 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
310 c->step = av_clip(c->step, 127, 24576);
315 static void adpcm_compress_trellis(AVCodecContext *avctx,
316 const int16_t *samples, uint8_t *dst,
317 ADPCMChannelStatus *c, int n, int stride)
319 //FIXME 6% faster if frontier is a compile-time constant
320 ADPCMEncodeContext *s = avctx->priv_data;
321 const int frontier = 1 << avctx->trellis;
322 const int version = avctx->codec->id;
323 TrellisPath *paths = s->paths, *p;
324 TrellisNode *node_buf = s->node_buf;
325 TrellisNode **nodep_buf = s->nodep_buf;
326 TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
327 TrellisNode **nodes_next = nodep_buf + frontier;
328 int pathn = 0, froze = -1, i, j, k, generation = 0;
329 uint8_t *hash = s->trellis_hash;
330 memset(hash, 0xff, 65536 * sizeof(*hash));
332 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
333 nodes[0] = node_buf + frontier;
336 nodes[0]->step = c->step_index;
337 nodes[0]->sample1 = c->sample1;
338 nodes[0]->sample2 = c->sample2;
339 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
340 version == AV_CODEC_ID_ADPCM_IMA_QT ||
341 version == AV_CODEC_ID_ADPCM_SWF)
342 nodes[0]->sample1 = c->prev_sample;
343 if (version == AV_CODEC_ID_ADPCM_MS)
344 nodes[0]->step = c->idelta;
345 if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
347 nodes[0]->step = 127;
348 nodes[0]->sample1 = 0;
350 nodes[0]->step = c->step;
351 nodes[0]->sample1 = c->predictor;
355 for (i = 0; i < n; i++) {
356 TrellisNode *t = node_buf + frontier*(i&1);
358 int sample = samples[i * stride];
360 memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
361 for (j = 0; j < frontier && nodes[j]; j++) {
362 // higher j have higher ssd already, so they're likely
363 // to yield a suboptimal next sample too
364 const int range = (j < frontier / 2) ? 1 : 0;
365 const int step = nodes[j]->step;
367 if (version == AV_CODEC_ID_ADPCM_MS) {
368 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
369 (nodes[j]->sample2 * c->coeff2)) / 64;
370 const int div = (sample - predictor) / step;
371 const int nmin = av_clip(div-range, -8, 6);
372 const int nmax = av_clip(div+range, -7, 7);
373 for (nidx = nmin; nidx <= nmax; nidx++) {
374 const int nibble = nidx & 0xf;
375 int dec_sample = predictor + nidx * step;
376 #define STORE_NODE(NAME, STEP_INDEX)\
382 dec_sample = av_clip_int16(dec_sample);\
383 d = sample - dec_sample;\
384 ssd = nodes[j]->ssd + d*(unsigned)d;\
385 /* Check for wraparound, skip such samples completely. \
386 * Note, changing ssd to a 64 bit variable would be \
387 * simpler, avoiding this check, but it's slower on \
388 * x86 32 bit at the moment. */\
389 if (ssd < nodes[j]->ssd)\
391 /* Collapse any two states with the same previous sample value. \
392 * One could also distinguish states by step and by 2nd to last
393 * sample, but the effects of that are negligible.
394 * Since nodes in the previous generation are iterated
395 * through a heap, they're roughly ordered from better to
396 * worse, but not strictly ordered. Therefore, an earlier
397 * node with the same sample value is better in most cases
398 * (and thus the current is skipped), but not strictly
399 * in all cases. Only skipping samples where ssd >=
400 * ssd of the earlier node with the same sample gives
401 * slightly worse quality, though, for some reason. */ \
402 h = &hash[(uint16_t) dec_sample];\
403 if (*h == generation)\
405 if (heap_pos < frontier) {\
408 /* Try to replace one of the leaf nodes with the new \
409 * one, but try a different slot each time. */\
410 pos = (frontier >> 1) +\
411 (heap_pos & ((frontier >> 1) - 1));\
412 if (ssd > nodes_next[pos]->ssd)\
417 u = nodes_next[pos];\
419 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
421 nodes_next[pos] = u;\
425 u->step = STEP_INDEX;\
426 u->sample2 = nodes[j]->sample1;\
427 u->sample1 = dec_sample;\
428 paths[u->path].nibble = nibble;\
429 paths[u->path].prev = nodes[j]->path;\
430 /* Sift the newly inserted node up in the heap to \
431 * restore the heap property. */\
433 int parent = (pos - 1) >> 1;\
434 if (nodes_next[parent]->ssd <= ssd)\
436 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
440 STORE_NODE(ms, FFMAX(16,
441 (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
443 } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
444 version == AV_CODEC_ID_ADPCM_IMA_QT ||
445 version == AV_CODEC_ID_ADPCM_SWF) {
446 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
447 const int predictor = nodes[j]->sample1;\
448 const int div = (sample - predictor) * 4 / STEP_TABLE;\
449 int nmin = av_clip(div - range, -7, 6);\
450 int nmax = av_clip(div + range, -6, 7);\
452 nmin--; /* distinguish -0 from +0 */\
455 for (nidx = nmin; nidx <= nmax; nidx++) {\
456 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
457 int dec_sample = predictor +\
459 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
460 STORE_NODE(NAME, STEP_INDEX);\
462 LOOP_NODES(ima, ff_adpcm_step_table[step],
463 av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
464 } else { //AV_CODEC_ID_ADPCM_YAMAHA
465 LOOP_NODES(yamaha, step,
466 av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
478 if (generation == 255) {
479 memset(hash, 0xff, 65536 * sizeof(*hash));
484 if (nodes[0]->ssd > (1 << 28)) {
485 for (j = 1; j < frontier && nodes[j]; j++)
486 nodes[j]->ssd -= nodes[0]->ssd;
490 // merge old paths to save memory
491 if (i == froze + FREEZE_INTERVAL) {
492 p = &paths[nodes[0]->path];
493 for (k = i; k > froze; k--) {
499 // other nodes might use paths that don't coincide with the frozen one.
500 // checking which nodes do so is too slow, so just kill them all.
501 // this also slightly improves quality, but I don't know why.
502 memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
506 p = &paths[nodes[0]->path];
507 for (i = n - 1; i > froze; i--) {
512 c->predictor = nodes[0]->sample1;
513 c->sample1 = nodes[0]->sample1;
514 c->sample2 = nodes[0]->sample2;
515 c->step_index = nodes[0]->step;
516 c->step = nodes[0]->step;
517 c->idelta = nodes[0]->step;
520 static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
526 nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
528 nibble = 4 * s - 4 * cs->sample1;
530 return (nibble >> shift) & 0x0F;
533 static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
534 const int16_t *samples, int nsamples,
540 put_bits(pb, 4, shift - 2);
542 put_bits(pb, 1, !!flag);
546 for (int n = 0; n < nsamples; n++) {
547 /* Compress the nibble, then expand it to see how much precision we've lost. */
548 int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
549 int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
551 error += abs(samples[n] - sample);
554 put_bits(pb, 4, nibble);
560 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
561 const AVFrame *frame, int *got_packet_ptr)
563 int n, i, ch, st, pkt_size, ret;
564 const int16_t *samples;
567 ADPCMEncodeContext *c = avctx->priv_data;
570 samples = (const int16_t *)frame->data[0];
571 samples_p = (int16_t **)frame->extended_data;
572 st = avctx->channels == 2;
574 if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
575 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
576 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM)
577 pkt_size = (frame->nb_samples * avctx->channels) / 2;
579 pkt_size = avctx->block_align;
580 if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
584 switch(avctx->codec->id) {
585 case AV_CODEC_ID_ADPCM_IMA_WAV:
589 blocks = (frame->nb_samples - 1) / 8;
591 for (ch = 0; ch < avctx->channels; ch++) {
592 ADPCMChannelStatus *status = &c->status[ch];
593 status->prev_sample = samples_p[ch][0];
594 /* status->step_index = 0;
595 XXX: not sure how to init the state machine */
596 bytestream_put_le16(&dst, status->prev_sample);
597 *dst++ = status->step_index;
598 *dst++ = 0; /* unknown */
601 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
602 if (avctx->trellis > 0) {
603 if (!FF_ALLOC_TYPED_ARRAY(buf, avctx->channels * blocks * 8))
604 return AVERROR(ENOMEM);
605 for (ch = 0; ch < avctx->channels; ch++) {
606 adpcm_compress_trellis(avctx, &samples_p[ch][1],
607 buf + ch * blocks * 8, &c->status[ch],
610 for (i = 0; i < blocks; i++) {
611 for (ch = 0; ch < avctx->channels; ch++) {
612 uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
613 for (j = 0; j < 8; j += 2)
614 *dst++ = buf1[j] | (buf1[j + 1] << 4);
619 for (i = 0; i < blocks; i++) {
620 for (ch = 0; ch < avctx->channels; ch++) {
621 ADPCMChannelStatus *status = &c->status[ch];
622 const int16_t *smp = &samples_p[ch][1 + i * 8];
623 for (j = 0; j < 8; j += 2) {
624 uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
625 v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
633 case AV_CODEC_ID_ADPCM_IMA_QT:
636 init_put_bits(&pb, dst, pkt_size);
638 for (ch = 0; ch < avctx->channels; ch++) {
639 ADPCMChannelStatus *status = &c->status[ch];
640 put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
641 put_bits(&pb, 7, status->step_index);
642 if (avctx->trellis > 0) {
644 adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
646 for (i = 0; i < 64; i++)
647 put_bits(&pb, 4, buf[i ^ 1]);
648 status->prev_sample = status->predictor;
650 for (i = 0; i < 64; i += 2) {
652 t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
653 t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
654 put_bits(&pb, 4, t2);
655 put_bits(&pb, 4, t1);
663 case AV_CODEC_ID_ADPCM_IMA_SSI:
666 init_put_bits(&pb, dst, pkt_size);
668 av_assert0(avctx->trellis == 0);
670 for (i = 0; i < frame->nb_samples; i++) {
671 for (ch = 0; ch < avctx->channels; ch++) {
672 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
679 case AV_CODEC_ID_ADPCM_IMA_ALP:
682 init_put_bits(&pb, dst, pkt_size);
684 av_assert0(avctx->trellis == 0);
686 for (n = frame->nb_samples / 2; n > 0; n--) {
687 for (ch = 0; ch < avctx->channels; ch++) {
688 put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
689 put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
691 samples += avctx->channels;
697 case AV_CODEC_ID_ADPCM_SWF:
700 init_put_bits(&pb, dst, pkt_size);
702 n = frame->nb_samples - 1;
704 // store AdpcmCodeSize
705 put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
707 // init the encoder state
708 for (i = 0; i < avctx->channels; i++) {
709 // clip step so it fits 6 bits
710 c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
711 put_sbits(&pb, 16, samples[i]);
712 put_bits(&pb, 6, c->status[i].step_index);
713 c->status[i].prev_sample = samples[i];
716 if (avctx->trellis > 0) {
717 if (!(buf = av_malloc(2 * n)))
718 return AVERROR(ENOMEM);
719 adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
720 &c->status[0], n, avctx->channels);
721 if (avctx->channels == 2)
722 adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
723 buf + n, &c->status[1], n,
725 for (i = 0; i < n; i++) {
726 put_bits(&pb, 4, buf[i]);
727 if (avctx->channels == 2)
728 put_bits(&pb, 4, buf[n + i]);
732 for (i = 1; i < frame->nb_samples; i++) {
733 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
734 samples[avctx->channels * i]));
735 if (avctx->channels == 2)
736 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
737 samples[2 * i + 1]));
743 case AV_CODEC_ID_ADPCM_MS:
744 for (i = 0; i < avctx->channels; i++) {
747 c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
748 c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
750 for (i = 0; i < avctx->channels; i++) {
751 if (c->status[i].idelta < 16)
752 c->status[i].idelta = 16;
753 bytestream_put_le16(&dst, c->status[i].idelta);
755 for (i = 0; i < avctx->channels; i++)
756 c->status[i].sample2= *samples++;
757 for (i = 0; i < avctx->channels; i++) {
758 c->status[i].sample1 = *samples++;
759 bytestream_put_le16(&dst, c->status[i].sample1);
761 for (i = 0; i < avctx->channels; i++)
762 bytestream_put_le16(&dst, c->status[i].sample2);
764 if (avctx->trellis > 0) {
765 n = avctx->block_align - 7 * avctx->channels;
766 if (!(buf = av_malloc(2 * n)))
767 return AVERROR(ENOMEM);
768 if (avctx->channels == 1) {
769 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
771 for (i = 0; i < n; i += 2)
772 *dst++ = (buf[i] << 4) | buf[i + 1];
774 adpcm_compress_trellis(avctx, samples, buf,
775 &c->status[0], n, avctx->channels);
776 adpcm_compress_trellis(avctx, samples + 1, buf + n,
777 &c->status[1], n, avctx->channels);
778 for (i = 0; i < n; i++)
779 *dst++ = (buf[i] << 4) | buf[n + i];
783 for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
785 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
786 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
791 case AV_CODEC_ID_ADPCM_YAMAHA:
792 n = frame->nb_samples / 2;
793 if (avctx->trellis > 0) {
794 if (!(buf = av_malloc(2 * n * 2)))
795 return AVERROR(ENOMEM);
797 if (avctx->channels == 1) {
798 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
800 for (i = 0; i < n; i += 2)
801 *dst++ = buf[i] | (buf[i + 1] << 4);
803 adpcm_compress_trellis(avctx, samples, buf,
804 &c->status[0], n, avctx->channels);
805 adpcm_compress_trellis(avctx, samples + 1, buf + n,
806 &c->status[1], n, avctx->channels);
807 for (i = 0; i < n; i++)
808 *dst++ = buf[i] | (buf[n + i] << 4);
812 for (n *= avctx->channels; n > 0; n--) {
814 nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
815 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
819 case AV_CODEC_ID_ADPCM_IMA_APM:
822 init_put_bits(&pb, dst, pkt_size);
824 av_assert0(avctx->trellis == 0);
826 for (n = frame->nb_samples / 2; n > 0; n--) {
827 for (ch = 0; ch < avctx->channels; ch++) {
828 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
829 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
831 samples += avctx->channels;
837 case AV_CODEC_ID_ADPCM_ARGO:
840 init_put_bits(&pb, dst, pkt_size);
842 av_assert0(frame->nb_samples == 32);
844 for (ch = 0; ch < avctx->channels; ch++) {
845 int64_t error = INT64_MAX, tmperr = INT64_MAX;
846 int shift = 2, flag = 0;
847 int saved1 = c->status[ch].sample1;
848 int saved2 = c->status[ch].sample2;
850 /* Find the optimal coefficients, bail early if we find a perfect result. */
851 for (int s = 2; s < 18 && tmperr != 0; s++) {
852 for (int f = 0; f < 2 && tmperr != 0; f++) {
853 c->status[ch].sample1 = saved1;
854 c->status[ch].sample2 = saved2;
855 tmperr = adpcm_argo_compress_block(c->status + ch, NULL, samples_p[ch],
856 frame->nb_samples, s, f);
857 if (tmperr < error) {
865 /* Now actually do the encode. */
866 c->status[ch].sample1 = saved1;
867 c->status[ch].sample2 = saved2;
868 adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
869 frame->nb_samples, shift, flag);
876 return AVERROR(EINVAL);
879 avpkt->size = pkt_size;
884 static const enum AVSampleFormat sample_fmts[] = {
885 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
888 static const enum AVSampleFormat sample_fmts_p[] = {
889 AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
892 static const AVOption options[] = {
894 .name = "block_size",
895 .help = "set the block size",
896 .offset = offsetof(ADPCMEncodeContext, block_size),
897 .type = AV_OPT_TYPE_INT,
898 .default_val = {.i64 = 1024},
900 .max = 8192, /* Is this a reasonable upper limit? */
901 .flags = AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
906 static const AVClass adpcm_encoder_class = {
907 .class_name = "ADPCM Encoder",
908 .item_name = av_default_item_name,
910 .version = LIBAVUTIL_VERSION_INT,
913 #define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_) \
914 AVCodec ff_ ## name_ ## _encoder = { \
916 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
917 .type = AVMEDIA_TYPE_AUDIO, \
919 .priv_data_size = sizeof(ADPCMEncodeContext), \
920 .init = adpcm_encode_init, \
921 .encode2 = adpcm_encode_frame, \
922 .close = adpcm_encode_close, \
923 .sample_fmts = sample_fmts_, \
924 .capabilities = capabilities_, \
925 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
926 .priv_class = &adpcm_encoder_class, \
929 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_ARGO, adpcm_argo, sample_fmts_p, 0, "ADPCM Argonaut Games");
930 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM");
931 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");
932 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, 0, "ADPCM IMA QuickTime");
933 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive");
934 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0, "ADPCM IMA WAV");
935 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, sample_fmts, 0, "ADPCM Microsoft");
936 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, sample_fmts, 0, "ADPCM Shockwave Flash");
937 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, 0, "ADPCM Yamaha");