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 ||
98 avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_WS) {
100 * The current trellis implementation doesn't work for extended
101 * runs of samples without periodic resets. Disallow it.
103 av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
104 return AVERROR_PATCHWELCOME;
107 frontier = 1 << avctx->trellis;
108 max_paths = frontier * FREEZE_INTERVAL;
109 if (!FF_ALLOC_TYPED_ARRAY(s->paths, max_paths) ||
110 !FF_ALLOC_TYPED_ARRAY(s->node_buf, 2 * frontier) ||
111 !FF_ALLOC_TYPED_ARRAY(s->nodep_buf, 2 * frontier) ||
112 !FF_ALLOC_TYPED_ARRAY(s->trellis_hash, 65536))
113 return AVERROR(ENOMEM);
116 avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
118 switch (avctx->codec->id) {
119 case AV_CODEC_ID_ADPCM_IMA_WAV:
120 /* each 16 bits sample gives one nibble
121 and we have 4 bytes per channel overhead */
122 avctx->frame_size = (s->block_size - 4 * avctx->channels) * 8 /
123 (4 * avctx->channels) + 1;
124 /* seems frame_size isn't taken into account...
125 have to buffer the samples :-( */
126 avctx->block_align = s->block_size;
127 avctx->bits_per_coded_sample = 4;
129 case AV_CODEC_ID_ADPCM_IMA_QT:
130 avctx->frame_size = 64;
131 avctx->block_align = 34 * avctx->channels;
133 case AV_CODEC_ID_ADPCM_MS:
134 /* each 16 bits sample gives one nibble
135 and we have 7 bytes per channel overhead */
136 avctx->frame_size = (s->block_size - 7 * avctx->channels) * 2 / avctx->channels + 2;
137 avctx->bits_per_coded_sample = 4;
138 avctx->block_align = s->block_size;
139 if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
140 return AVERROR(ENOMEM);
141 avctx->extradata_size = 32;
142 extradata = avctx->extradata;
143 bytestream_put_le16(&extradata, avctx->frame_size);
144 bytestream_put_le16(&extradata, 7); /* wNumCoef */
145 for (i = 0; i < 7; i++) {
146 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
147 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
150 case AV_CODEC_ID_ADPCM_YAMAHA:
151 avctx->frame_size = s->block_size * 2 / avctx->channels;
152 avctx->block_align = s->block_size;
154 case AV_CODEC_ID_ADPCM_SWF:
155 if (avctx->sample_rate != 11025 &&
156 avctx->sample_rate != 22050 &&
157 avctx->sample_rate != 44100) {
158 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
160 return AVERROR(EINVAL);
162 avctx->frame_size = 4096; /* Hardcoded according to the SWF spec. */
163 avctx->block_align = (2 + avctx->channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
165 case AV_CODEC_ID_ADPCM_IMA_SSI:
166 case AV_CODEC_ID_ADPCM_IMA_ALP:
167 avctx->frame_size = s->block_size * 2 / avctx->channels;
168 avctx->block_align = s->block_size;
170 case AV_CODEC_ID_ADPCM_IMA_AMV:
171 if (avctx->sample_rate != 22050) {
172 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
173 return AVERROR(EINVAL);
176 if (avctx->channels != 1) {
177 av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
178 return AVERROR(EINVAL);
181 avctx->frame_size = s->block_size;
182 avctx->block_align = 8 + (FFALIGN(avctx->frame_size, 2) / 2);
184 case AV_CODEC_ID_ADPCM_IMA_APM:
185 avctx->frame_size = s->block_size * 2 / avctx->channels;
186 avctx->block_align = s->block_size;
188 if (!(avctx->extradata = av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
189 return AVERROR(ENOMEM);
190 avctx->extradata_size = 28;
192 case AV_CODEC_ID_ADPCM_ARGO:
193 avctx->frame_size = 32;
194 avctx->block_align = 17 * avctx->channels;
196 case AV_CODEC_ID_ADPCM_IMA_WS:
197 /* each 16 bits sample gives one nibble */
198 avctx->frame_size = s->block_size * 2 / avctx->channels;
199 avctx->block_align = s->block_size;
202 return AVERROR(EINVAL);
208 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
210 ADPCMEncodeContext *s = avctx->priv_data;
212 av_freep(&s->node_buf);
213 av_freep(&s->nodep_buf);
214 av_freep(&s->trellis_hash);
220 static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
223 int delta = sample - c->prev_sample;
224 int nibble = FFMIN(7, abs(delta) * 4 /
225 ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
226 c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
227 ff_adpcm_yamaha_difflookup[nibble]) / 8);
228 c->prev_sample = av_clip_int16(c->prev_sample);
229 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
233 static inline uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)
235 const int delta = sample - c->prev_sample;
236 const int step = ff_adpcm_step_table[c->step_index];
237 const int sign = (delta < 0) * 8;
239 int nibble = FFMIN(abs(delta) * 4 / step, 7);
240 int diff = (step * nibble) >> 2;
244 nibble = sign | nibble;
246 c->prev_sample += diff;
247 c->prev_sample = av_clip_int16(c->prev_sample);
248 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
252 static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
255 int delta = sample - c->prev_sample;
256 int diff, step = ff_adpcm_step_table[c->step_index];
257 int nibble = 8*(delta < 0);
260 diff = delta + (step >> 3);
279 c->prev_sample -= diff;
281 c->prev_sample += diff;
283 c->prev_sample = av_clip_int16(c->prev_sample);
284 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
289 static inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
292 int predictor, nibble, bias;
294 predictor = (((c->sample1) * (c->coeff1)) +
295 (( c->sample2) * (c->coeff2))) / 64;
297 nibble = sample - predictor;
299 bias = c->idelta / 2;
301 bias = -c->idelta / 2;
303 nibble = (nibble + bias) / c->idelta;
304 nibble = av_clip_intp2(nibble, 3) & 0x0F;
306 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
308 c->sample2 = c->sample1;
309 c->sample1 = av_clip_int16(predictor);
311 c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
318 static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
328 delta = sample - c->predictor;
330 nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
332 c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
333 c->predictor = av_clip_int16(c->predictor);
334 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
335 c->step = av_clip(c->step, 127, 24576);
340 static void adpcm_compress_trellis(AVCodecContext *avctx,
341 const int16_t *samples, uint8_t *dst,
342 ADPCMChannelStatus *c, int n, int stride)
344 //FIXME 6% faster if frontier is a compile-time constant
345 ADPCMEncodeContext *s = avctx->priv_data;
346 const int frontier = 1 << avctx->trellis;
347 const int version = avctx->codec->id;
348 TrellisPath *paths = s->paths, *p;
349 TrellisNode *node_buf = s->node_buf;
350 TrellisNode **nodep_buf = s->nodep_buf;
351 TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
352 TrellisNode **nodes_next = nodep_buf + frontier;
353 int pathn = 0, froze = -1, i, j, k, generation = 0;
354 uint8_t *hash = s->trellis_hash;
355 memset(hash, 0xff, 65536 * sizeof(*hash));
357 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
358 nodes[0] = node_buf + frontier;
361 nodes[0]->step = c->step_index;
362 nodes[0]->sample1 = c->sample1;
363 nodes[0]->sample2 = c->sample2;
364 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
365 version == AV_CODEC_ID_ADPCM_IMA_QT ||
366 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
367 version == AV_CODEC_ID_ADPCM_SWF)
368 nodes[0]->sample1 = c->prev_sample;
369 if (version == AV_CODEC_ID_ADPCM_MS)
370 nodes[0]->step = c->idelta;
371 if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
373 nodes[0]->step = 127;
374 nodes[0]->sample1 = 0;
376 nodes[0]->step = c->step;
377 nodes[0]->sample1 = c->predictor;
381 for (i = 0; i < n; i++) {
382 TrellisNode *t = node_buf + frontier*(i&1);
384 int sample = samples[i * stride];
386 memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
387 for (j = 0; j < frontier && nodes[j]; j++) {
388 // higher j have higher ssd already, so they're likely
389 // to yield a suboptimal next sample too
390 const int range = (j < frontier / 2) ? 1 : 0;
391 const int step = nodes[j]->step;
393 if (version == AV_CODEC_ID_ADPCM_MS) {
394 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
395 (nodes[j]->sample2 * c->coeff2)) / 64;
396 const int div = (sample - predictor) / step;
397 const int nmin = av_clip(div-range, -8, 6);
398 const int nmax = av_clip(div+range, -7, 7);
399 for (nidx = nmin; nidx <= nmax; nidx++) {
400 const int nibble = nidx & 0xf;
401 int dec_sample = predictor + nidx * step;
402 #define STORE_NODE(NAME, STEP_INDEX)\
408 dec_sample = av_clip_int16(dec_sample);\
409 d = sample - dec_sample;\
410 ssd = nodes[j]->ssd + d*(unsigned)d;\
411 /* Check for wraparound, skip such samples completely. \
412 * Note, changing ssd to a 64 bit variable would be \
413 * simpler, avoiding this check, but it's slower on \
414 * x86 32 bit at the moment. */\
415 if (ssd < nodes[j]->ssd)\
417 /* Collapse any two states with the same previous sample value. \
418 * One could also distinguish states by step and by 2nd to last
419 * sample, but the effects of that are negligible.
420 * Since nodes in the previous generation are iterated
421 * through a heap, they're roughly ordered from better to
422 * worse, but not strictly ordered. Therefore, an earlier
423 * node with the same sample value is better in most cases
424 * (and thus the current is skipped), but not strictly
425 * in all cases. Only skipping samples where ssd >=
426 * ssd of the earlier node with the same sample gives
427 * slightly worse quality, though, for some reason. */ \
428 h = &hash[(uint16_t) dec_sample];\
429 if (*h == generation)\
431 if (heap_pos < frontier) {\
434 /* Try to replace one of the leaf nodes with the new \
435 * one, but try a different slot each time. */\
436 pos = (frontier >> 1) +\
437 (heap_pos & ((frontier >> 1) - 1));\
438 if (ssd > nodes_next[pos]->ssd)\
443 u = nodes_next[pos];\
445 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
447 nodes_next[pos] = u;\
451 u->step = STEP_INDEX;\
452 u->sample2 = nodes[j]->sample1;\
453 u->sample1 = dec_sample;\
454 paths[u->path].nibble = nibble;\
455 paths[u->path].prev = nodes[j]->path;\
456 /* Sift the newly inserted node up in the heap to \
457 * restore the heap property. */\
459 int parent = (pos - 1) >> 1;\
460 if (nodes_next[parent]->ssd <= ssd)\
462 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
466 STORE_NODE(ms, FFMAX(16,
467 (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
469 } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
470 version == AV_CODEC_ID_ADPCM_IMA_QT ||
471 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
472 version == AV_CODEC_ID_ADPCM_SWF) {
473 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
474 const int predictor = nodes[j]->sample1;\
475 const int div = (sample - predictor) * 4 / STEP_TABLE;\
476 int nmin = av_clip(div - range, -7, 6);\
477 int nmax = av_clip(div + range, -6, 7);\
479 nmin--; /* distinguish -0 from +0 */\
482 for (nidx = nmin; nidx <= nmax; nidx++) {\
483 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
484 int dec_sample = predictor +\
486 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
487 STORE_NODE(NAME, STEP_INDEX);\
489 LOOP_NODES(ima, ff_adpcm_step_table[step],
490 av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
491 } else { //AV_CODEC_ID_ADPCM_YAMAHA
492 LOOP_NODES(yamaha, step,
493 av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
505 if (generation == 255) {
506 memset(hash, 0xff, 65536 * sizeof(*hash));
511 if (nodes[0]->ssd > (1 << 28)) {
512 for (j = 1; j < frontier && nodes[j]; j++)
513 nodes[j]->ssd -= nodes[0]->ssd;
517 // merge old paths to save memory
518 if (i == froze + FREEZE_INTERVAL) {
519 p = &paths[nodes[0]->path];
520 for (k = i; k > froze; k--) {
526 // other nodes might use paths that don't coincide with the frozen one.
527 // checking which nodes do so is too slow, so just kill them all.
528 // this also slightly improves quality, but I don't know why.
529 memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
533 p = &paths[nodes[0]->path];
534 for (i = n - 1; i > froze; i--) {
539 c->predictor = nodes[0]->sample1;
540 c->sample1 = nodes[0]->sample1;
541 c->sample2 = nodes[0]->sample2;
542 c->step_index = nodes[0]->step;
543 c->step = nodes[0]->step;
544 c->idelta = nodes[0]->step;
547 static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
553 nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
555 nibble = 4 * s - 4 * cs->sample1;
557 return (nibble >> shift) & 0x0F;
560 static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
561 const int16_t *samples, int nsamples,
567 put_bits(pb, 4, shift - 2);
569 put_bits(pb, 1, !!flag);
573 for (int n = 0; n < nsamples; n++) {
574 /* Compress the nibble, then expand it to see how much precision we've lost. */
575 int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
576 int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
578 error += abs(samples[n] - sample);
581 put_bits(pb, 4, nibble);
587 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
588 const AVFrame *frame, int *got_packet_ptr)
590 int n, i, ch, st, pkt_size, ret;
591 const int16_t *samples;
594 ADPCMEncodeContext *c = avctx->priv_data;
597 samples = (const int16_t *)frame->data[0];
598 samples_p = (int16_t **)frame->extended_data;
599 st = avctx->channels == 2;
601 if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
602 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
603 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
604 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_WS)
605 pkt_size = (frame->nb_samples * avctx->channels) / 2;
607 pkt_size = avctx->block_align;
608 if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
612 switch(avctx->codec->id) {
613 case AV_CODEC_ID_ADPCM_IMA_WAV:
617 blocks = (frame->nb_samples - 1) / 8;
619 for (ch = 0; ch < avctx->channels; ch++) {
620 ADPCMChannelStatus *status = &c->status[ch];
621 status->prev_sample = samples_p[ch][0];
622 /* status->step_index = 0;
623 XXX: not sure how to init the state machine */
624 bytestream_put_le16(&dst, status->prev_sample);
625 *dst++ = status->step_index;
626 *dst++ = 0; /* unknown */
629 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
630 if (avctx->trellis > 0) {
631 if (!FF_ALLOC_TYPED_ARRAY(buf, avctx->channels * blocks * 8))
632 return AVERROR(ENOMEM);
633 for (ch = 0; ch < avctx->channels; ch++) {
634 adpcm_compress_trellis(avctx, &samples_p[ch][1],
635 buf + ch * blocks * 8, &c->status[ch],
638 for (i = 0; i < blocks; i++) {
639 for (ch = 0; ch < avctx->channels; ch++) {
640 uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
641 for (j = 0; j < 8; j += 2)
642 *dst++ = buf1[j] | (buf1[j + 1] << 4);
647 for (i = 0; i < blocks; i++) {
648 for (ch = 0; ch < avctx->channels; ch++) {
649 ADPCMChannelStatus *status = &c->status[ch];
650 const int16_t *smp = &samples_p[ch][1 + i * 8];
651 for (j = 0; j < 8; j += 2) {
652 uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
653 v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
661 case AV_CODEC_ID_ADPCM_IMA_QT:
664 init_put_bits(&pb, dst, pkt_size);
666 for (ch = 0; ch < avctx->channels; ch++) {
667 ADPCMChannelStatus *status = &c->status[ch];
668 put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
669 put_bits(&pb, 7, status->step_index);
670 if (avctx->trellis > 0) {
672 adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
674 for (i = 0; i < 64; i++)
675 put_bits(&pb, 4, buf[i ^ 1]);
676 status->prev_sample = status->predictor;
678 for (i = 0; i < 64; i += 2) {
680 t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
681 t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
682 put_bits(&pb, 4, t2);
683 put_bits(&pb, 4, t1);
691 case AV_CODEC_ID_ADPCM_IMA_SSI:
694 init_put_bits(&pb, dst, pkt_size);
696 av_assert0(avctx->trellis == 0);
698 for (i = 0; i < frame->nb_samples; i++) {
699 for (ch = 0; ch < avctx->channels; ch++) {
700 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
707 case AV_CODEC_ID_ADPCM_IMA_ALP:
710 init_put_bits(&pb, dst, pkt_size);
712 av_assert0(avctx->trellis == 0);
714 for (n = frame->nb_samples / 2; n > 0; n--) {
715 for (ch = 0; ch < avctx->channels; ch++) {
716 put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
717 put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
719 samples += avctx->channels;
725 case AV_CODEC_ID_ADPCM_SWF:
728 init_put_bits(&pb, dst, pkt_size);
730 n = frame->nb_samples - 1;
732 /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
733 av_assert0(n == 4095);
735 // store AdpcmCodeSize
736 put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
738 // init the encoder state
739 for (i = 0; i < avctx->channels; i++) {
740 // clip step so it fits 6 bits
741 c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
742 put_sbits(&pb, 16, samples[i]);
743 put_bits(&pb, 6, c->status[i].step_index);
744 c->status[i].prev_sample = samples[i];
747 if (avctx->trellis > 0) {
748 uint8_t buf[8190 /* = 2 * n */];
749 adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
750 &c->status[0], n, avctx->channels);
751 if (avctx->channels == 2)
752 adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
753 buf + n, &c->status[1], n,
755 for (i = 0; i < n; i++) {
756 put_bits(&pb, 4, buf[i]);
757 if (avctx->channels == 2)
758 put_bits(&pb, 4, buf[n + i]);
761 for (i = 1; i < frame->nb_samples; i++) {
762 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
763 samples[avctx->channels * i]));
764 if (avctx->channels == 2)
765 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
766 samples[2 * i + 1]));
772 case AV_CODEC_ID_ADPCM_MS:
773 for (i = 0; i < avctx->channels; i++) {
776 c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
777 c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
779 for (i = 0; i < avctx->channels; i++) {
780 if (c->status[i].idelta < 16)
781 c->status[i].idelta = 16;
782 bytestream_put_le16(&dst, c->status[i].idelta);
784 for (i = 0; i < avctx->channels; i++)
785 c->status[i].sample2= *samples++;
786 for (i = 0; i < avctx->channels; i++) {
787 c->status[i].sample1 = *samples++;
788 bytestream_put_le16(&dst, c->status[i].sample1);
790 for (i = 0; i < avctx->channels; i++)
791 bytestream_put_le16(&dst, c->status[i].sample2);
793 if (avctx->trellis > 0) {
794 n = avctx->block_align - 7 * avctx->channels;
795 if (!(buf = av_malloc(2 * n)))
796 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] << 4) | buf[i + 1];
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] << 4) | buf[n + i];
812 for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
814 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
815 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
820 case AV_CODEC_ID_ADPCM_YAMAHA:
821 n = frame->nb_samples / 2;
822 if (avctx->trellis > 0) {
823 if (!(buf = av_malloc(2 * n * 2)))
824 return AVERROR(ENOMEM);
826 if (avctx->channels == 1) {
827 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
829 for (i = 0; i < n; i += 2)
830 *dst++ = buf[i] | (buf[i + 1] << 4);
832 adpcm_compress_trellis(avctx, samples, buf,
833 &c->status[0], n, avctx->channels);
834 adpcm_compress_trellis(avctx, samples + 1, buf + n,
835 &c->status[1], n, avctx->channels);
836 for (i = 0; i < n; i++)
837 *dst++ = buf[i] | (buf[n + i] << 4);
841 for (n *= avctx->channels; n > 0; n--) {
843 nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
844 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
848 case AV_CODEC_ID_ADPCM_IMA_APM:
851 init_put_bits(&pb, dst, pkt_size);
853 av_assert0(avctx->trellis == 0);
855 for (n = frame->nb_samples / 2; n > 0; n--) {
856 for (ch = 0; ch < avctx->channels; ch++) {
857 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
858 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
860 samples += avctx->channels;
866 case AV_CODEC_ID_ADPCM_IMA_AMV:
868 av_assert0(avctx->channels == 1);
870 c->status[0].prev_sample = *samples;
871 bytestream_put_le16(&dst, c->status[0].prev_sample);
872 bytestream_put_byte(&dst, c->status[0].step_index);
873 bytestream_put_byte(&dst, 0);
874 bytestream_put_le32(&dst, avctx->frame_size);
876 if (avctx->trellis > 0) {
877 n = frame->nb_samples >> 1;
879 if (!(buf = av_malloc(2 * n)))
880 return AVERROR(ENOMEM);
882 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, avctx->channels);
883 for (i = 0; i < n; i++)
884 bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
888 } else for (n = frame->nb_samples >> 1; n > 0; n--) {
890 nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
891 nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
892 bytestream_put_byte(&dst, nibble);
895 if (avctx->frame_size & 1) {
896 int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
897 bytestream_put_byte(&dst, nibble);
901 case AV_CODEC_ID_ADPCM_ARGO:
904 init_put_bits(&pb, dst, pkt_size);
906 av_assert0(frame->nb_samples == 32);
908 for (ch = 0; ch < avctx->channels; ch++) {
909 int64_t error = INT64_MAX, tmperr = INT64_MAX;
910 int shift = 2, flag = 0;
911 int saved1 = c->status[ch].sample1;
912 int saved2 = c->status[ch].sample2;
914 /* Find the optimal coefficients, bail early if we find a perfect result. */
915 for (int s = 2; s < 18 && tmperr != 0; s++) {
916 for (int f = 0; f < 2 && tmperr != 0; f++) {
917 c->status[ch].sample1 = saved1;
918 c->status[ch].sample2 = saved2;
919 tmperr = adpcm_argo_compress_block(c->status + ch, NULL, samples_p[ch],
920 frame->nb_samples, s, f);
921 if (tmperr < error) {
929 /* Now actually do the encode. */
930 c->status[ch].sample1 = saved1;
931 c->status[ch].sample2 = saved2;
932 adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
933 frame->nb_samples, shift, flag);
939 case AV_CODEC_ID_ADPCM_IMA_WS:
942 init_put_bits(&pb, dst, pkt_size);
944 av_assert0(avctx->trellis == 0);
945 for (n = frame->nb_samples / 2; n > 0; n--) {
946 /* stereo: 1 byte (2 samples) for left, 1 byte for right */
947 for (ch = 0; ch < avctx->channels; ch++) {
949 t1 = adpcm_ima_compress_sample(&c->status[ch], *samples++);
950 t2 = adpcm_ima_compress_sample(&c->status[ch], samples[st]);
951 put_bits(&pb, 4, t2);
952 put_bits(&pb, 4, t1);
954 samples += avctx->channels;
960 return AVERROR(EINVAL);
963 avpkt->size = pkt_size;
968 static const enum AVSampleFormat sample_fmts[] = {
969 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
972 static const enum AVSampleFormat sample_fmts_p[] = {
973 AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
976 static const AVOption options[] = {
978 .name = "block_size",
979 .help = "set the block size",
980 .offset = offsetof(ADPCMEncodeContext, block_size),
981 .type = AV_OPT_TYPE_INT,
982 .default_val = {.i64 = 1024},
984 .max = 8192, /* Is this a reasonable upper limit? */
985 .flags = AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
990 #define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_) \
991 static const AVClass name_ ## _encoder_class = { \
992 .class_name = #name_, \
993 .item_name = av_default_item_name, \
995 .version = LIBAVUTIL_VERSION_INT, \
998 AVCodec ff_ ## name_ ## _encoder = { \
1000 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1001 .type = AVMEDIA_TYPE_AUDIO, \
1003 .priv_data_size = sizeof(ADPCMEncodeContext), \
1004 .init = adpcm_encode_init, \
1005 .encode2 = adpcm_encode_frame, \
1006 .close = adpcm_encode_close, \
1007 .sample_fmts = sample_fmts_, \
1008 .capabilities = capabilities_, \
1009 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE, \
1010 .priv_class = &name_ ## _encoder_class, \
1013 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_ARGO, adpcm_argo, sample_fmts_p, 0, "ADPCM Argonaut Games");
1014 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, sample_fmts, 0, "ADPCM IMA AMV");
1015 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM");
1016 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");
1017 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, 0, "ADPCM IMA QuickTime");
1018 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive");
1019 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0, "ADPCM IMA WAV");
1020 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Westwood");
1021 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, sample_fmts, 0, "ADPCM Microsoft");
1022 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, sample_fmts, 0, "ADPCM Shockwave Flash");
1023 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, 0, "ADPCM Yamaha");