3 #include "ADPCMCodec.h"
6 #define FREEZE_INTERVAL 128
8 namespace adpcm_ffmpeg {
18 setBlockSize(ADAPCM_DEFAULT_BLOCK_SIZE);
19 avctx.priv_data = (uint8_t *)&enc_ctx;
22 bool begin(
int sampleRate,
int channels) {
23 avctx.sample_rate = sampleRate;
24 avctx.nb_channels = channels;
25 avctx.sample_fmt = sample_formats[0];
26 bool rc = adpcm_encode_init() == 0;
27 printf(
"frame_size: %d", frameSize());
31 void end() { adpcm_encode_close(); }
33 AVPacket &encode(int16_t *data,
size_t sampleCount) {
34 frame.
nb_samples = sampleCount / avctx.nb_channels;
36 frame.
data[0] = (uint8_t *)data;
40 if (channels() == 1 || !isPlanar()) {
41 extended_data[0] = data;
42 }
else if (channels() == 2) {
45 frame_extended_data_vectors.resize(channels());
46 for (
int ch=0;ch<channels();ch++){
47 frame_extended_data_vectors[ch].resize(sampleCount/channels());
48 extended_data[ch] = &frame_extended_data_vectors[ch][0];
52 for (
int j = 0; j < sampleCount / channels(); j++) {
53 for (
int ch=0;ch<channels();ch++){
54 frame_extended_data_vectors[ch][j] = data[(j * channels()) + ch];
59 int got_packet_ptr = 0;
60 av_packet_data.resize(sampleCount);
61 result.data = &av_packet_data[0];
63 int rc = adpcm_encode_frame(&result, &frame, &got_packet_ptr);
64 if (rc != 0 || !got_packet_ptr) {
70 virtual bool is_trellis() {
return false; }
77 int16_t *extended_data[2] = {0};
78 std::vector<uint8_t> av_packet_data;
79 std::vector<std::vector<int16_t>> frame_extended_data_vectors;
81 int st, pkt_size, ret;
82 const int16_t *samples;
83 const int16_t *
const *samples_p;
88 virtual int adpcm_encode_init_impl() = 0;
90 virtual int adpcm_encode_init() {
101 if (avctx.codec_id != AV_CODEC_ID_ADPCM_IMA_AMV &&
102 (s->block_size & (s->block_size - 1))) {
103 av_log(avctx, AV_LOG_ERROR,
"block size must be power of 2: %d\n",
105 return AVERROR(AVERROR_INVALID);
109 int frontier, max_paths;
111 if ((
unsigned)avctx.trellis > 16U) {
112 av_log(avctx, AV_LOG_ERROR,
"invalid trellis size\n");
113 return AVERROR(AVERROR_INVALID);
116 if (avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
117 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
118 avctx.codec_id == AV_CODEC_ID_ADPCM_ARGO ||
119 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_WS) {
124 av_log(avctx, AV_LOG_ERROR,
"trellis not supported\n");
125 return AVERROR_PATCHWELCOME;
128 frontier = 1 << avctx.trellis;
129 max_paths = frontier * FREEZE_INTERVAL;
130 if (!FF_ALLOC_TYPED_ARRAY(
TrellisPath *, s->paths, max_paths) ||
131 !FF_ALLOC_TYPED_ARRAY(
TrellisNode *, s->node_buf, 2 * frontier) ||
132 !FF_ALLOC_TYPED_ARRAY(
TrellisNode **, s->nodep_buf, 2 * frontier) ||
133 !FF_ALLOC_TYPED_ARRAY(uint8_t *, s->trellis_hash, 65536))
134 return AVERROR(AVERROR_MEMORY);
137 avctx.bits_per_coded_sample = av_get_bits_per_sample();
139 return adpcm_encode_init_impl();
142 int adpcm_encode_close() {
145 av_freep(&s->node_buf);
146 av_freep(&s->nodep_buf);
147 av_freep(&s->trellis_hash);
154 int delta = sample - c->prev_sample;
155 int nibble = FFMIN(7, abs(delta) * 4 / ff_adpcm_step_table[c->step_index]) +
157 c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
158 ff_adpcm_yamaha_difflookup[nibble]) /
160 c->prev_sample = av_clip_int16(c->prev_sample);
162 av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
168 int delta = sample - c->prev_sample;
169 int diff, step = ff_adpcm_step_table[c->step_index];
170 int nibble = 8 * (delta < 0);
173 diff = delta + (step >> 3);
192 c->prev_sample -= diff;
194 c->prev_sample += diff;
196 c->prev_sample = av_clip_int16(c->prev_sample);
198 av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
212 delta = sample - c->predictor;
214 nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
216 c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
217 c->predictor = av_clip_int16(c->predictor);
218 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
219 c->step = av_clip(c->step, 127, 24576);
224 virtual int adpcm_encode_frame_impl(
AVPacket *avpkt,
const AVFrame *frame,
225 int *got_packet_ptr) = 0;
228 int *got_packet_ptr) {
231 samples = (
const int16_t *)frame->
data[0];
233 assert(samples_p != NULL);
234 assert(samples != NULL);
235 st = channels() == 2;
237 if (avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
238 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
239 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
240 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_WS)
241 pkt_size = (frame->
nb_samples * channels() + 1) / 2;
244 if ((ret = ff_get_encode_buffer(&avctx, avpkt, pkt_size, 0)) < 0)
248 int rc = adpcm_encode_frame_impl(avpkt, frame, got_packet_ptr);
249 if (rc != AV_OK)
return rc;
258 bool is_trellis() {
return avctx.trellis; }
259 void set_trellis(
bool flag) { avctx.trellis = flag;}
260 bool store_node(
int STEP_INDEX) {
266 dec_sample = av_clip_int16(dec_sample);
267 d = sample - dec_sample;
268 ssd = nodes[j]->ssd +
274 if (ssd < nodes[j]->ssd) {
287 h = &hash[(uint16_t)dec_sample];
288 if (*h == generation)
return true;
289 if (heap_pos < frontier) {
293 pos = (frontier >> 1) + (heap_pos & ((frontier >> 1) - 1));
294 if (ssd > nodes_next[pos]->ssd)
return true;
300 av_assert(pathn < FREEZE_INTERVAL << avctx.trellis);
306 u->step = STEP_INDEX;
307 u->sample2 = nodes[j]->sample1;
308 u->sample1 = dec_sample;
309 paths[u->path].nibble = nibble;
310 paths[u->path].prev =
314 int parent = (pos - 1) >> 1;
315 if (nodes_next[parent]->ssd <= ssd)
break;
316 FFSWAP(
TrellisNode *, nodes_next[parent], nodes_next[pos]);
322 void loop_nodes(int16_t STEP_TABLE,
int STEP_INDEX) {
323 const int predictor = nodes[j]->sample1;
324 const int div = (sample - predictor) * 4 / STEP_TABLE;
325 int nmin = av_clip(div - range, -7, 6);
326 int nmax = av_clip(div + range, -6, 7);
327 if (nmin <= 0) nmin--;
328 if (nmax < 0) nmax--;
329 for (nidx = nmin; nidx <= nmax; nidx++) {
330 const int nibble = nidx < 0 ? 7 - nidx : nidx;
332 predictor + (STEP_TABLE * ff_adpcm_yamaha_difflookup[nibble]) / 8;
333 store_node(STEP_INDEX);
337 void adpcm_compress_trellis(
const int16_t *samples, uint8_t *dst,
341 frontier = 1 << avctx.trellis;
342 version = avctx.codec_id;
343 paths = s->paths, *p;
344 node_buf = s->node_buf;
345 nodep_buf = s->nodep_buf;
347 nodes_next = nodep_buf + frontier;
348 pathn = 0, froze = -1, i, j, k, generation = 0;
349 hash = s->trellis_hash;
350 memset(hash, 0xff, 65536 *
sizeof(*hash));
352 memset(nodep_buf, 0, 2 * frontier *
sizeof(*nodep_buf));
353 nodes[0] = node_buf + frontier;
356 nodes[0]->step = c->step_index;
357 nodes[0]->sample1 = c->sample1;
358 nodes[0]->sample2 = c->sample2;
359 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
360 version == AV_CODEC_ID_ADPCM_IMA_QT ||
361 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
362 version == AV_CODEC_ID_ADPCM_SWF)
363 nodes[0]->sample1 = c->prev_sample;
364 if (version == AV_CODEC_ID_ADPCM_MS) 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 t = node_buf + frontier * (i & 1);
377 sample = samples[i * stride];
379 memset(nodes_next, 0, frontier *
sizeof(
TrellisNode *));
380 for (j = 0; j < frontier && nodes[j]; j++) {
383 range = (j < frontier / 2) ? 1 : 0;
384 const int step = nodes[j]->step;
385 if (version == AV_CODEC_ID_ADPCM_MS) {
386 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
387 (nodes[j]->sample2 * c->coeff2)) /
389 const int div = (sample - predictor) / step;
390 const int nmin = av_clip(div - range, -8, 6);
391 const int nmax = av_clip(div + range, -7, 7);
392 for (nidx = nmin; nidx <= nmax; nidx++) {
394 dec_sample = predictor + nidx * step;
397 FFMAX(16, (ff_adpcm_AdaptationTable[nibble] * step) >> 8)));
399 }
else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
400 version == AV_CODEC_ID_ADPCM_IMA_QT ||
401 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
402 version == AV_CODEC_ID_ADPCM_SWF) {
403 loop_nodes(ff_adpcm_step_table[step],
404 av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
407 av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
417 if (generation == 255) {
418 memset(hash, 0xff, 65536 *
sizeof(*hash));
423 if (nodes[0]->ssd > (1 << 28)) {
424 for (j = 1; j < frontier && nodes[j]; j++)
425 nodes[j]->ssd -= nodes[0]->ssd;
430 if (i == froze + FREEZE_INTERVAL) {
431 p = &paths[nodes[0]->path];
432 for (k = i; k > froze; k--) {
441 memset(nodes + 1, 0, (frontier - 1) *
sizeof(
TrellisNode *));
445 p = &paths[nodes[0]->path];
446 for (i = n - 1; i > froze; i--) {
451 c->predictor = nodes[0]->sample1;
452 c->sample1 = nodes[0]->sample1;
453 c->sample2 = nodes[0]->sample2;
454 c->step_index = nodes[0]->step;
455 c->step = nodes[0]->step;
456 c->idelta = nodes[0]->step;
470 int pathn = 0, froze = -1, i, j, k, generation = 0;
483 setCodecID(AV_CODEC_ID_ADPCM_IMA_WAV);
484 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
486 int adpcm_encode_init_impl() {
489 avctx.
frame_size = (s->block_size - 4 * channels()) * 8 / (4 * channels()) + 1;
493 avctx.bits_per_coded_sample = 4;
497 int *got_packet_ptr) {
500 for (
int ch = 0; ch < channels(); ch++) {
502 status->prev_sample = samples_p[ch][0];
505 bytestream_put_le16(&dst, status->prev_sample);
506 *dst++ = status->step_index;
511 if (avctx.trellis > 0) {
513 if (!FF_ALLOC_TYPED_ARRAY(uint8_t *, buf, channels() *blocks * 8))
514 return AVERROR(AVERROR_MEMORY);
515 for (
int ch = 0; ch < channels(); ch++) {
516 adpcm_compress_trellis(&samples_p[ch][1], buf + ch * blocks * 8,
517 &c->status[ch], blocks * 8, 1);
519 for (
int i = 0; i < blocks; i++) {
520 for (
int ch = 0; ch < channels(); ch++) {
521 uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
522 for (
int j = 0; j < 8; j += 2) *dst++ = buf1[j] | (buf1[j + 1] << 4);
527 for (
int i = 0; i < blocks; i++) {
528 for (
int ch = 0; ch < channels(); ch++) {
530 const int16_t *smp = &samples_p[ch][1 + i * 8];
531 for (
int j = 0; j < 8; j += 2) {
532 uint8_t v = adpcm_ima_compress_sample(status, smp[j]);
533 v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
546 setCodecID(AV_CODEC_ID_ADPCM_IMA_QT);
547 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
549 int adpcm_encode_init_impl() {
556 int *got_packet_ptr) {
558 init_put_bits(&pb, dst, pkt_size);
560 for (
int ch = 0; ch < channels(); ch++) {
562 put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
563 put_bits(&pb, 7, status->step_index);
564 if (avctx.trellis > 0) {
566 adpcm_compress_trellis(&samples_p[ch][0], buf, status, 64, 1);
567 for (
int i = 0; i < 64; i++) put_bits(&pb, 4, buf[i ^ 1]);
568 status->prev_sample = status->predictor;
570 for (
int i = 0; i < 64; i += 2) {
572 t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i]);
573 t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
574 put_bits(&pb, 4, t2);
575 put_bits(&pb, 4, t1);
588 setCodecID(AV_CODEC_ID_ADPCM_IMA_SSI);
589 sample_formats.push_back(AV_SAMPLE_FMT_S16);
591 int adpcm_encode_init_impl() {
592 avctx.
frame_size = s->block_size * 2 / channels();
598 int *got_packet_ptr) {
600 init_put_bits(&pb, dst, pkt_size);
602 av_assert(avctx.trellis == 0);
605 for (
int ch = 0; ch < channels(); ch++) {
607 adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
619 setCodecID(AV_CODEC_ID_ADPCM_IMA_ALP);
620 sample_formats.push_back(AV_SAMPLE_FMT_S16);
622 int adpcm_encode_init_impl() {
623 avctx.
frame_size = s->block_size * 2 / channels();
630 const int delta = sample - c->prev_sample;
631 const int step = ff_adpcm_step_table[c->step_index];
632 const int sign = (delta < 0) * 8;
634 int nibble = FFMIN(abs(delta) * 4 / step, 7);
635 int diff = (step * nibble) >> 2;
636 if (sign) diff = -diff;
638 nibble = sign | nibble;
640 c->prev_sample += diff;
641 c->prev_sample = av_clip_int16(c->prev_sample);
643 av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
648 int *got_packet_ptr) {
650 init_put_bits(&pb, dst, pkt_size);
652 av_assert(avctx.trellis == 0);
654 for (
int n = frame->
nb_samples / 2; n > 0; n--) {
655 for (
int ch = 0; ch < channels(); ch++) {
657 adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
659 adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
661 samples += channels();
672 setCodecID(AV_CODEC_ID_ADPCM_MS);
673 sample_formats.push_back(AV_SAMPLE_FMT_S16);
675 int adpcm_encode_init_impl() {
679 avctx.
frame_size = (s->block_size - 7 * channels()) * 2 / channels() + 2;
680 avctx.bits_per_coded_sample = 4;
682 if (!(avctx.extradata =
683 (uint8_t *)av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
684 return AVERROR(AVERROR_MEMORY);
685 avctx.extradata_size = 32;
686 extradata = avctx.extradata;
687 bytestream_put_le16(&extradata, avctx.
frame_size);
688 bytestream_put_le16(&extradata, 7);
689 for (
int i = 0; i < 7; i++) {
690 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
691 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
698 int predictor, nibble, bias;
701 (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
703 nibble = sample - predictor;
705 bias = c->idelta / 2;
707 bias = -c->idelta / 2;
709 nibble = (nibble + bias) / c->idelta;
710 nibble = av_clip_intp2(nibble, 3) & 0x0F;
712 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
714 c->sample2 = c->sample1;
715 c->sample1 = av_clip_int16(predictor);
717 c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
718 if (c->idelta < 16) c->idelta = 16;
724 int *got_packet_ptr) {
725 for (
int i = 0; i < channels(); i++) {
728 c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
729 c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
731 for (
int i = 0; i < channels(); i++) {
732 if (c->status[i].idelta < 16) c->status[i].idelta = 16;
733 bytestream_put_le16(&dst, c->status[i].idelta);
735 for (
int i = 0; i < channels(); i++) c->status[i].sample2 = *samples++;
736 for (
int i = 0; i < channels(); i++) {
737 c->status[i].sample1 = *samples++;
738 bytestream_put_le16(&dst, c->status[i].sample1);
740 for (
int i = 0; i < channels(); i++)
741 bytestream_put_le16(&dst, c->status[i].sample2);
743 if (avctx.trellis > 0) {
745 uint8_t *buf = (uint8_t *)av_malloc(2 * n);
746 if (!buf)
return AVERROR(AVERROR_MEMORY);
747 if (channels() == 1) {
748 adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
749 for (
int i = 0; i < n; i += 2) *dst++ = (buf[i] << 4) | buf[i + 1];
751 adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
752 adpcm_compress_trellis(samples + 1, buf + n, &c->status[1], n,
754 for (
int i = 0; i < n; i++) *dst++ = (buf[i] << 4) | buf[n + i];
758 for (
int i = 7 * channels(); i < avctx.
block_align; i++) {
760 nibble = adpcm_ms_compress_sample(&c->status[0], *samples++) << 4;
761 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
772 setCodecID(AV_CODEC_ID_ADPCM_SWF);
773 sample_formats.push_back(AV_SAMPLE_FMT_S16);
775 int adpcm_encode_init_impl() {
776 if (avctx.sample_rate != 11025 && avctx.sample_rate != 22050 &&
777 avctx.sample_rate != 44100) {
778 av_log(avctx, AV_LOG_ERROR,
779 "Sample rate must be 11025, "
781 return AVERROR(AVERROR_INVALID);
785 (2 + channels() * (22 + 4 * (avctx.
frame_size - 1)) + 7) / 8;
790 int *got_packet_ptr) {
793 init_put_bits(&pb, dst, pkt_size);
796 av_assert(n == 4095);
802 for (
int i = 0; i < channels(); i++) {
804 c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
805 put_sbits(&pb, 16, samples[i]);
806 put_bits(&pb, 6, c->status[i].step_index);
807 c->status[i].prev_sample = samples[i];
810 if (avctx.trellis > 0) {
812 adpcm_compress_trellis(samples + channels(), buf, &c->status[0], n,
815 adpcm_compress_trellis(samples + channels() + 1, buf + n, &c->status[1],
817 for (
int i = 0; i < n; i++) {
818 put_bits(&pb, 4, buf[i]);
819 if (channels() == 2) put_bits(&pb, 4, buf[n + i]);
825 adpcm_ima_compress_sample(&c->status[0], samples[channels() * i]));
829 adpcm_ima_compress_sample(&c->status[1], samples[2 * i + 1]));
840 setCodecID(AV_CODEC_ID_ADPCM_YAMAHA);
841 sample_formats.push_back(AV_SAMPLE_FMT_S16);
843 int adpcm_encode_init_impl() {
844 avctx.
frame_size = s->block_size * 2 / channels();
850 int *got_packet_ptr) {
852 if (avctx.trellis > 0) {
853 uint8_t *buf = (uint8_t *)av_malloc(2 * n * 2);
854 if (!buf)
return AVERROR(AVERROR_MEMORY);
856 if (channels() == 1) {
857 adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
858 for (
int i = 0; i < n; i += 2) *dst++ = buf[i] | (buf[i + 1] << 4);
860 adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
861 adpcm_compress_trellis(samples + 1, buf + n, &c->status[1], n,
863 for (
int i = 0; i < n; i++) *dst++ = buf[i] | (buf[n + i] << 4);
867 for (n *= channels(); n > 0; n--) {
869 nibble = adpcm_yamaha_compress_sample(&c->status[0], *samples++);
870 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
880 setCodecID(AV_CODEC_ID_ADPCM_IMA_APM);
881 sample_formats.push_back(AV_SAMPLE_FMT_S16);
883 int adpcm_encode_init_impl() {
884 avctx.
frame_size = s->block_size * 2 / channels();
887 if (!(avctx.extradata =
888 (uint8_t *)av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
889 return AVERROR(AVERROR_MEMORY);
890 avctx.extradata_size = 28;
895 int *got_packet_ptr) {
897 init_put_bits(&pb, dst, pkt_size);
899 av_assert(avctx.trellis == 0);
901 for (
int n = frame->
nb_samples / 2; n > 0; n--) {
902 for (
int ch = 0; ch < channels(); ch++) {
904 adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
906 adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
908 samples += channels();
919 setCodecID(AV_CODEC_ID_ADPCM_IMA_AMV);
920 sample_formats.push_back(AV_SAMPLE_FMT_S16);
922 int adpcm_encode_init_impl() {
923 if (avctx.sample_rate != 22050) {
924 av_log(avctx, AV_LOG_ERROR,
"Sample rate must be 22050\n");
925 return AVERROR(AVERROR_INVALID);
928 if (channels() != 1) {
929 av_log(avctx, AV_LOG_ERROR,
"Only mono is supported\n");
930 return AVERROR(AVERROR_INVALID);
939 int *got_packet_ptr) {
940 av_assert(channels() == 1);
942 c->status[0].prev_sample = *samples;
943 bytestream_put_le16(&dst, c->status[0].prev_sample);
944 bytestream_put_byte(&dst, c->status[0].step_index);
945 bytestream_put_byte(&dst, 0);
948 if (avctx.trellis > 0) {
950 uint8_t *buf = (uint8_t *)av_malloc(2 * n);
952 if (!buf)
return AVERROR(AVERROR_MEMORY);
954 adpcm_compress_trellis(samples, buf, &c->status[0], 2 * n, channels());
955 for (
int i = 0; i < n; i++)
956 bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
961 for (
int n = frame->
nb_samples >> 1; n > 0; n--) {
963 nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
964 nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
965 bytestream_put_byte(&dst, nibble);
969 int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
970 bytestream_put_byte(&dst, nibble);
979 setCodecID(AV_CODEC_ID_ADPCM_ARGO);
980 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
982 int adpcm_encode_init_impl() {
989 int shift,
int flag) {
993 nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
995 nibble = 4 * s - 4 * cs->sample1;
997 return (nibble >> shift) & 0x0F;
1001 int shift,
int flag) {
1002 int sample = sign_extend(nibble, 4) * (1 << shift);
1005 sample += (8 * cs->sample1) - (4 * cs->sample2);
1007 sample += 4 * cs->sample1;
1009 sample = av_clip_int16(sample >> 2);
1011 cs->sample2 = cs->sample1;
1012 cs->sample1 = sample;
1018 const int16_t *samples,
int nsamples,
1019 int shift,
int flag) {
1023 put_bits(pb, 4, shift - 2);
1025 put_bits(pb, 1, !!flag);
1029 for (
int n = 0; n < nsamples; n++) {
1032 int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
1033 int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
1035 error += abs(samples[n] - sample);
1037 if (pb) put_bits(pb, 4, nibble);
1044 int *got_packet_ptr) {
1046 init_put_bits(&pb, dst, pkt_size);
1050 for (
int ch = 0; ch < channels(); ch++) {
1051 int64_t error = INT64_MAX, tmperr = INT64_MAX;
1052 int shift = 2, flag = 0;
1053 int saved1 = c->status[ch].sample1;
1054 int saved2 = c->status[ch].sample2;
1058 for (
int s = 2; s < 18 && tmperr != 0; s++) {
1059 for (
int f = 0; f < 2 && tmperr != 0; f++) {
1060 c->status[ch].sample1 = saved1;
1061 c->status[ch].sample2 = saved2;
1062 tmperr = adpcm_argo_compress_block(
1063 c->status + ch, NULL, samples_p[ch], frame->
nb_samples, s, f);
1064 if (tmperr < error) {
1073 c->status[ch].sample1 = saved1;
1074 c->status[ch].sample2 = saved2;
1075 adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
1079 flush_put_bits(&pb);
1087 setCodecID(AV_CODEC_ID_ADPCM_IMA_WS);
1088 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1090 int adpcm_encode_init_impl() {
1092 avctx.
frame_size = s->block_size * 2 / channels();
1098 int *got_packet_ptr) {
1100 init_put_bits(&pb, dst, pkt_size);
1102 av_assert(avctx.trellis == 0);
1103 for (
int n = frame->
nb_samples / 2; n > 0; n--) {
1105 for (
int ch = 0; ch < channels(); ch++) {
1107 t1 = adpcm_ima_compress_sample(&c->status[ch], *samples++);
1108 t2 = adpcm_ima_compress_sample(&c->status[ch], samples[st]);
1109 put_bits(&pb, 4, t2);
1110 put_bits(&pb, 4, t1);
1112 samples += channels();
1114 flush_put_bits(&pb);
1123 case AV_CODEC_ID_ADPCM_IMA_WAV:
1125 #if ENABLE_BROKEN_CODECS
1126 case AV_CODEC_ID_ADPCM_IMA_QT:
1129 case AV_CODEC_ID_ADPCM_IMA_SSI:
1131 case AV_CODEC_ID_ADPCM_IMA_ALP:
1133 case AV_CODEC_ID_ADPCM_MS:
1135 case AV_CODEC_ID_ADPCM_SWF:
1137 case AV_CODEC_ID_ADPCM_YAMAHA:
1139 case AV_CODEC_ID_ADPCM_IMA_APM:
1141 case AV_CODEC_ID_ADPCM_IMA_AMV:
1143 case AV_CODEC_ID_ADPCM_ARGO:
1145 case AV_CODEC_ID_ADPCM_IMA_WS:
1149 av_log(avctx, AV_LOG_ERROR,
"ERROR: encoder [%d] not implemented\n",
id);
Common ADPCM Functionality.
Definition: ADPCMCodec.h:21
Definition: ADPCMEncoder.h:1119
Definition: ADPCMEncoder.h:256
ADPCM Encoder.
Definition: ADPCMEncoder.h:15
Definition: ADPCMEncoder.h:976
Definition: ADPCMEncoder.h:616
Definition: ADPCMEncoder.h:916
Definition: ADPCMEncoder.h:877
Definition: ADPCMEncoder.h:543
Definition: ADPCMEncoder.h:585
Definition: ADPCMEncoder.h:480
Definition: ADPCMEncoder.h:1084
Definition: ADPCMEncoder.h:669
Definition: ADPCMEncoder.h:769
Definition: ADPCMEncoder.h:837
int frame_size
Number of samples per channel in an audio frame.
Definition: adpcm.h:239
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: adpcm.h:242
This structure provides the uncompressed PCM data.
Definition: adpcm.h:151
int16_t ** extended_data
Definition: adpcm.h:185
int nb_samples
Definition: adpcm.h:169
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: adpcm.h:164
This structure stores compressed data. It is typically exported by demuxers and then passed as input ...
Definition: adpcm.h:143
Definition: put_bits.h:51