4#include "adpcm/BitWriter.h"
5#include "adpcm/ByteWriter.h"
8namespace adpcm_ffmpeg {
23 void put(
int nibble) {
25 out[pos >> 1] |= nibble & 0x0F;
27 out[pos >> 1] = (
uint8_t)((nibble & 0x0F) << 4);
36 setBlockSize(ADAPCM_DEFAULT_BLOCK_SIZE);
37 avctx.priv_data = (
uint8_t *)&enc_ctx;
47 avctx.nb_channels = channels;
48 avctx.sample_fmt = sample_formats[0];
49 bool rc = adpcm_encode_init() == 0;
53 void end() { adpcm_encode_close(); }
62 if (channels() == 1 || !isPlanar()) {
63 extended_data[0] = data;
67 frame_extended_data_vectors.resize(channels());
68 for (
int ch=0;
ch<channels();
ch++){
69 frame_extended_data_vectors[
ch].resize(
sampleCount/channels());
70 extended_data[
ch] = &frame_extended_data_vectors[
ch][0];
74 for (
int j = 0; j <
sampleCount / channels(); j++) {
75 for (
int ch=0;
ch<channels();
ch++){
76 frame_extended_data_vectors[
ch][j] = data[(j * channels()) +
ch];
83 av_packet_data.resize(buf_size);
84 result.data = &av_packet_data[0];
94 virtual bool isTrellis() {
return false; }
117 for (
int index = 0; index <
npairs; index++) {
123 int s1 =
cs->sample1,
s2 =
cs->sample2;
125 for (
int i = 0; i <
n; i++) {
159 for (
int k = 0; k < (
n + 1) / 2; k++) {
161 const int lo = (2 * k + 1 <
n) ? (
best_nib[2 * k + 1] & 0x0F) : 0;
169 int16_t *extended_data[ADPCM_MAX_CHANNELS] = {0};
170 ADPCMVector<uint8_t> av_packet_data;
171 ADPCMVector<ADPCMVector<int16_t>> frame_extended_data_vectors;
173 int st, pkt_size, ret;
174 const int16_t *samples;
175 const int16_t *
const *samples_p;
177 ADPCMEncodeContext *c;
178 ADPCMEncodeContext *s;
180 virtual int adpcm_encode_init_impl() = 0;
185 if (avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
186 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
187 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
188 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_WS)
189 return (frame->
nb_samples * channels() + 1) / 2;
193 virtual int adpcm_encode_init() {
204 if (avctx.codec_id != AV_CODEC_ID_ADPCM_IMA_AMV &&
205 (s->block_size & (s->block_size - 1))) {
206 av_log(avctx, AV_LOG_ERROR,
"block size must be power of 2: %d\n",
208 return AVERROR(AVERROR_INVALID);
212 int frontier, max_paths;
214 if ((
unsigned)avctx.trellis > 16U) {
215 av_log(avctx, AV_LOG_ERROR,
"invalid trellis size\n");
216 return AVERROR(AVERROR_INVALID);
219 if (avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
220 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
221 avctx.codec_id == AV_CODEC_ID_ADPCM_ARGO ||
222 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_WS) {
227 av_log(avctx, AV_LOG_ERROR,
"trellis not supported\n");
228 return AVERROR_PATCHWELCOME;
231 frontier = 1 << avctx.trellis;
232 max_paths = frontier * FREEZE_INTERVAL;
234 freeArray(s->node_buf);
235 freeArray(s->nodep_buf);
236 freeArray(s->trellis_hash);
237 if (!allocArray(s->paths, max_paths) ||
238 !allocArray(s->node_buf, 2 * frontier) ||
239 !allocArray(s->nodep_buf, 2 * frontier) ||
240 !allocArray(s->trellis_hash, 65536))
241 return AVERROR(AVERROR_MEMORY);
244 avctx.bits_per_coded_sample = av_get_bits_per_sample();
246 return adpcm_encode_init_impl();
249 int adpcm_encode_close() {
250 ADPCMEncodeContext *s = (ADPCMEncodeContext *)avctx.priv_data;
252 freeArray(s->node_buf);
253 freeArray(s->nodep_buf);
254 freeArray(s->trellis_hash);
256 freeArray(avctx.extradata);
257 avctx.extradata_size = 0;
262 inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
264 int delta = sample - c->prev_sample;
265 int nibble = FFMIN(7, abs(delta) * 4 / ff_adpcm_step_table[c->step_index]) +
267 c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
268 ff_adpcm_yamaha_difflookup[nibble]) /
270 c->prev_sample = av_clip_int16(c->prev_sample);
272 av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
280 int step = ff_adpcm_step_table[
cs->step_index];
285 for (
int d = 0;
d < 8;
d++) {
286 int rec = ((2 *
d + 1) * step) >> shift;
293 int nibble =
best | (
diff < 0 ? 8 : 0);
295 int rec = ((2 *
best + 1) * step) >> shift;
296 cs->predictor = av_clip_int16(
cs->predictor + (nibble & 8 ? -
rec :
rec));
298 av_clip(
cs->step_index + ff_adpcm_index_table[nibble], 0, 88);
304 int delta = sample - c->prev_sample;
305 int diff, step = ff_adpcm_step_table[c->step_index];
306 int nibble = 8 * (
delta < 0);
328 c->prev_sample -= diff;
330 c->prev_sample += diff;
332 c->prev_sample = av_clip_int16(c->prev_sample);
334 av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
339 inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
348 delta = sample - c->predictor;
350 nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
352 c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
353 c->predictor = av_clip_int16(c->predictor);
354 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
355 c->step = av_clip(c->step, 127, 24576);
360 virtual int adpcm_encode_frame_impl(AVPacket *avpkt,
const AVFrame *frame,
361 int *got_packet_ptr) = 0;
363 virtual int adpcm_encode_frame(AVPacket *avpkt,
const AVFrame *frame,
364 int *got_packet_ptr) {
365 c = (ADPCMEncodeContext *)avctx.priv_data;
367 samples = (
const int16_t *)frame->
data[0];
369 assert(samples_p != NULL);
370 assert(samples != NULL);
371 st = channels() == 2;
374 if ((ret = ff_get_encode_buffer(&avctx, avpkt, pkt_size, 0)) < 0)
378 int rc = adpcm_encode_frame_impl(avpkt, frame, got_packet_ptr);
379 if (rc != AV_OK)
return rc;
388 bool isTrellis() {
return avctx.trellis; }
389 void setTrellis(
bool flag) { avctx.trellis =
flag;}
398 dec_sample = av_clip_int16(dec_sample);
399 d = sample - dec_sample;
400 ssd = nodes[j]->ssd +
406 if (ssd < nodes[j]->ssd) {
410 if (*
h == generation)
return;
411 if (heap_pos < frontier) {
415 pos = (frontier >> 1) + (heap_pos & ((frontier >> 1) - 1));
416 if (ssd > nodes_next[pos]->ssd)
return;
422 av_assert(pathn < FREEZE_INTERVAL << avctx.trellis);
429 u->sample2 = nodes[j]->sample1;
430 u->sample1 = dec_sample;
432 paths[u->path].prev =
436 int parent = (pos - 1) >> 1;
437 if (nodes_next[
parent]->ssd <= ssd)
break;
444 const int predictor = nodes[j]->sample1;
446 int nmin = av_clip(
div - range, -7, 6);
447 int nmax = av_clip(
div + range, -6, 7);
450 for (nidx =
nmin; nidx <=
nmax; nidx++) {
451 nibble = nidx < 0 ? 7 - nidx : nidx;
453 predictor + (
STEP_TABLE * ff_adpcm_yamaha_difflookup[nibble]) / 8;
454 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
455 version == AV_CODEC_ID_ADPCM_IMA_QT ||
456 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
457 version == AV_CODEC_ID_ADPCM_SWF) {
458 store_node(av_clip(step + ff_adpcm_index_table[nibble], 0, 88), nibble);
460 store_node(av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
461 127, 24576), nibble);
470 frontier = 1 << avctx.trellis;
471 version = avctx.codec_id;
473 node_buf = s->node_buf;
474 nodep_buf = s->nodep_buf;
476 nodes_next = nodep_buf + frontier;
480 hash = s->trellis_hash;
481 memset(hash, 0xff, 65536 *
sizeof(*hash));
483 memset(nodep_buf, 0, 2 * frontier *
sizeof(*nodep_buf));
484 nodes[0] = node_buf + frontier;
487 nodes[0]->step = c->step_index;
488 nodes[0]->sample1 = c->sample1;
489 nodes[0]->sample2 = c->sample2;
490 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
491 version == AV_CODEC_ID_ADPCM_IMA_QT ||
492 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
493 version == AV_CODEC_ID_ADPCM_SWF)
494 nodes[0]->sample1 = c->prev_sample;
495 if (version == AV_CODEC_ID_ADPCM_MS) nodes[0]->step = c->idelta;
496 if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
498 nodes[0]->step = 127;
499 nodes[0]->sample1 = 0;
501 nodes[0]->step = c->step;
502 nodes[0]->sample1 = c->predictor;
506 for (i = 0; i <
n; i++) {
507 t = node_buf + frontier * (i & 1);
508 sample = samples[i *
stride];
511 for (j = 0; j < frontier && nodes[j]; j++) {
514 range = (j < frontier / 2) ? 1 : 0;
515 const int step = nodes[j]->step;
516 if (version == AV_CODEC_ID_ADPCM_MS) {
517 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
518 (nodes[j]->sample2 * c->coeff2)) /
520 const int div = (sample - predictor) / step;
521 const int nmin = av_clip(
div - range, -8, 6);
522 const int nmax = av_clip(
div + range, -7, 7);
523 for (nidx =
nmin; nidx <=
nmax; nidx++) {
525 dec_sample = predictor + nidx * step;
527 FFMAX(16, (ff_adpcm_AdaptationTable[nibble] * step) >> 8), nibble);
529 }
else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
530 version == AV_CODEC_ID_ADPCM_IMA_QT ||
531 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
532 version == AV_CODEC_ID_ADPCM_SWF) {
533 loop_nodes(ff_adpcm_step_table[step], step);
535 loop_nodes(step, step);
544 if (generation == 255) {
545 memset(hash, 0xff, 65536 *
sizeof(*hash));
550 if (nodes[0]->ssd > (1 << 28)) {
551 for (j = 1; j < frontier && nodes[j]; j++)
552 nodes[j]->ssd -= nodes[0]->ssd;
557 if (i == froze + FREEZE_INTERVAL) {
558 p = &paths[nodes[0]->path];
559 for (k = i; k > froze; k--) {
572 p = &paths[nodes[0]->path];
573 for (i =
n - 1; i > froze; i--) {
578 c->predictor = nodes[0]->sample1;
579 c->sample1 = nodes[0]->sample1;
580 c->sample2 = nodes[0]->sample2;
581 c->step_index = nodes[0]->step;
582 c->step = nodes[0]->step;
583 c->idelta = nodes[0]->step;
596 int pathn = 0, froze = -1, i, j, k, generation = 0;
609 setCodecID(AV_CODEC_ID_ADPCM_IMA_WAV);
610 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
613 int adpcm_encode_init_impl() {
616 avctx.
frame_size = (s->block_size - 4 * channels()) * 8 / (4 * channels()) + 1;
620 avctx.bits_per_coded_sample = 4;
628 for (
int ch = 0;
ch < channels();
ch++) {
630 status->prev_sample = samples_p[
ch][0];
634 *dst++ = status->step_index;
639 if (avctx.trellis > 0) {
641 if (!allocArray(buf, channels() *
blocks * 8))
642 return AVERROR(AVERROR_MEMORY);
643 for (
int ch = 0;
ch < channels();
ch++) {
644 adpcm_compress_trellis(&samples_p[
ch][1], buf +
ch *
blocks * 8,
647 for (
int i = 0; i <
blocks; i++) {
648 for (
int ch = 0;
ch < channels();
ch++) {
650 for (
int j = 0; j < 8; j += 2) *dst++ =
buf1[j] | (
buf1[j + 1] << 4);
655 for (
int i = 0; i <
blocks; i++) {
656 for (
int ch = 0;
ch < channels();
ch++) {
659 for (
int j = 0; j < 8; j += 2) {
660 uint8_t v = adpcm_ima_compress_sample(status,
smp[j]);
661 v |= adpcm_ima_compress_sample(status,
smp[j + 1]) << 4;
681 setCodecID(AV_CODEC_ID_ADPCM_IMA_QT);
682 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
687 int adpcm_encode_init_impl() {
696 for (
int ch = 0;
ch < channels();
ch++) {
702 (status->step_index & 0x7F));
706 if (avctx.trellis > 0) {
708 adpcm_compress_trellis(&samples_p[
ch][0],
nibbles, status, 64, 1);
709 for (
int i = 0; i < 64; i += 2)
711 status->prev_sample = status->predictor;
713 for (
int i = 0; i < 64; i += 2) {
714 int t1 = adpcm_ima_qt_compress_sample(status, samples_p[
ch][i]);
715 int t2 = adpcm_ima_qt_compress_sample(status, samples_p[
ch][i + 1]);
716 *out++ = (
uint8_t)((
t1 & 0x0F) | ((
t2 & 0x0F) << 4));
720 assert(out - dst == pkt_size);
728 setCodecID(AV_CODEC_ID_ADPCM_IMA_SSI);
729 sample_formats.push_back(AV_SAMPLE_FMT_S16);
733 int adpcm_encode_init_impl() {
734 avctx.
frame_size = s->block_size * 2 / channels();
743 av_assert(avctx.trellis == 0);
746 for (
int ch = 0;
ch < channels();
ch++) {
748 adpcm_ima_qt_compress_sample(c->status +
ch, *samples++));
759 setCodecID(AV_CODEC_ID_ADPCM_IMA_ALP);
760 sample_formats.push_back(AV_SAMPLE_FMT_S16);
765 const int delta = sample - c->prev_sample;
766 const int step = ff_adpcm_step_table[c->step_index];
769 int nibble = FFMIN(
abs(
delta) * 4 / step, 7);
770 int diff = (step * nibble) >> 2;
773 nibble =
sign | nibble;
775 c->prev_sample +=
diff;
776 c->prev_sample = av_clip_int16(c->prev_sample);
778 av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
783 int adpcm_encode_init_impl() {
784 avctx.
frame_size = s->block_size * 2 / channels();
793 av_assert(avctx.trellis == 0);
796 for (
int ch = 0;
ch < channels();
ch++) {
798 adpcm_ima_alp_compress_sample(c->status +
ch, *samples++));
800 adpcm_ima_alp_compress_sample(c->status +
ch, samples[st]));
802 samples += channels();
812 setCodecID(AV_CODEC_ID_ADPCM_MS);
813 sample_formats.push_back(AV_SAMPLE_FMT_S16);
818 int predictor, nibble,
bias;
821 (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
823 nibble = sample - predictor;
825 bias = c->idelta / 2;
827 bias = -c->idelta / 2;
829 nibble = (nibble +
bias) / c->idelta;
830 nibble = av_clip_intp2(nibble, 3) & 0x0F;
832 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
834 c->sample2 = c->sample1;
835 c->sample1 = av_clip_int16(predictor);
837 c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
838 if (c->idelta < 16) c->idelta = 16;
844 int adpcm_encode_init_impl() {
848 avctx.
frame_size = (s->block_size - 7 * channels()) * 2 / channels() + 2;
849 avctx.bits_per_coded_sample = 4;
851 if (!(avctx.extradata =
852 new uint8_t[32 + AV_INPUT_BUFFER_PADDING_SIZE]))
853 return AVERROR(AVERROR_MEMORY);
854 avctx.extradata_size = 32;
855 extradata = avctx.extradata;
858 for (
int i = 0; i < 7; i++) {
859 ByteWriter(extradata).putLe16(ff_adpcm_AdaptCoeff1[i] * 4);
860 ByteWriter(extradata).putLe16(ff_adpcm_AdaptCoeff2[i] * 4);
867 for (
int i = 0; i < channels(); i++) {
870 c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
871 c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
873 for (
int i = 0; i < channels(); i++) {
874 if (c->status[i].idelta < 16) c->status[i].idelta = 16;
877 for (
int i = 0; i < channels(); i++) c->status[i].sample2 = *samples++;
878 for (
int i = 0; i < channels(); i++) {
879 c->status[i].sample1 = *samples++;
880 ByteWriter(dst).putLe16(c->status[i].sample1);
882 for (
int i = 0; i < channels(); i++)
883 ByteWriter(dst).putLe16(c->status[i].sample2);
885 if (avctx.trellis > 0) {
888 const int n = (avctx.
block_align - 7 * channels()) * 2 / channels();
890 if (!buf)
return AVERROR(AVERROR_MEMORY);
891 if (channels() == 1) {
892 adpcm_compress_trellis(samples, buf, &c->status[0],
n, channels());
893 for (
int i = 0; i <
n; i += 2) *dst++ = (buf[i] << 4) | buf[i + 1];
895 adpcm_compress_trellis(samples, buf, &c->status[0],
n, channels());
896 adpcm_compress_trellis(samples + 1, buf +
n, &c->status[1],
n,
898 for (
int i = 0; i <
n; i++) *dst++ = (buf[i] << 4) | buf[
n + i];
902 for (
int i = 7 * channels(); i < avctx.
block_align; i++) {
904 nibble = adpcm_ms_compress_sample(&c->status[0], *samples++) << 4;
905 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
916 setCodecID(AV_CODEC_ID_ADPCM_SWF);
917 sample_formats.push_back(AV_SAMPLE_FMT_S16);
921 int adpcm_encode_init_impl() {
922 if (avctx.sample_rate != 11025 && avctx.sample_rate != 22050 &&
923 avctx.sample_rate != 44100) {
924 av_log(avctx, AV_LOG_ERROR,
925 "Sample rate must be 11025, "
927 return AVERROR(AVERROR_INVALID);
931 (2 + channels() * (22 + 4 * (avctx.
frame_size - 1)) + 7) / 8;
939 pb.setMsbFirst(
true);
942 av_assert(
n == 4095);
948 for (
int i = 0; i < channels(); i++) {
950 c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
951 pb.putSigned(16, samples[i]);
952 pb.put(6, c->status[i].step_index);
953 c->status[i].prev_sample = samples[i];
956 if (avctx.trellis > 0) {
958 adpcm_compress_trellis(samples + channels(), buf, &c->status[0],
n,
961 adpcm_compress_trellis(samples + channels() + 1, buf +
n, &c->status[1],
963 for (
int i = 0; i <
n; i++) {
965 if (channels() == 2)
pb.put(4, buf[
n + i]);
969 pb.put(4, adpcm_ima_compress_sample(&c->status[0], samples[channels() * i]));
971 pb.put(4, adpcm_ima_compress_sample(&c->status[1], samples[2 * i + 1]));
982 setCodecID(AV_CODEC_ID_ADPCM_YAMAHA);
983 sample_formats.push_back(AV_SAMPLE_FMT_S16);
987 int adpcm_encode_init_impl() {
988 avctx.
frame_size = s->block_size * 2 / channels();
996 if (avctx.trellis > 0) {
998 if (!buf)
return AVERROR(AVERROR_MEMORY);
1000 if (channels() == 1) {
1001 adpcm_compress_trellis(samples, buf, &c->status[0],
n, channels());
1002 for (
int i = 0; i <
n; i += 2) *dst++ = buf[i] | (buf[i + 1] << 4);
1004 adpcm_compress_trellis(samples, buf, &c->status[0],
n, channels());
1005 adpcm_compress_trellis(samples + 1, buf +
n, &c->status[1],
n,
1007 for (
int i = 0; i <
n; i++) *dst++ = buf[i] | (buf[
n + i] << 4);
1011 for (
n *= channels();
n > 0;
n--) {
1013 nibble = adpcm_yamaha_compress_sample(&c->status[0], *samples++);
1014 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
1024 setCodecID(AV_CODEC_ID_ADPCM_IMA_APM);
1025 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1029 int adpcm_encode_init_impl() {
1030 avctx.
frame_size = s->block_size * 2 / channels();
1033 if (!(avctx.extradata =
1034 new uint8_t[28 + AV_INPUT_BUFFER_PADDING_SIZE]()))
1035 return AVERROR(AVERROR_MEMORY);
1036 avctx.extradata_size = 28;
1044 av_assert(avctx.trellis == 0);
1047 for (
int ch = 0;
ch < channels();
ch++) {
1049 adpcm_ima_qt_compress_sample(c->status +
ch, *samples++));
1051 adpcm_ima_qt_compress_sample(c->status +
ch, samples[st]));
1053 samples += channels();
1063 setCodecID(AV_CODEC_ID_ADPCM_IMA_AMV);
1064 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1068 int adpcm_encode_init_impl() {
1069 if (avctx.sample_rate != 22050) {
1070 av_log(avctx, AV_LOG_ERROR,
"Sample rate must be 22050\n");
1071 return AVERROR(AVERROR_INVALID);
1074 if (channels() != 1) {
1075 av_log(avctx, AV_LOG_ERROR,
"Only mono is supported\n");
1076 return AVERROR(AVERROR_INVALID);
1086 av_assert(channels() == 1);
1088 c->status[0].prev_sample = *samples;
1089 ByteWriter(dst).putLe16(c->status[0].prev_sample);
1090 ByteWriter(dst).putByte(c->status[0].step_index);
1094 if (avctx.trellis > 0) {
1098 if (!buf)
return AVERROR(AVERROR_MEMORY);
1100 adpcm_compress_trellis(samples, buf, &c->status[0], 2 *
n, channels());
1101 for (
int i = 0; i <
n; i++)
1102 ByteWriter(dst).putByte((buf[2 * i] << 4) | buf[2 * i + 1]);
1109 nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
1110 nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
1115 int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
1125 setCodecID(AV_CODEC_ID_ADPCM_ARGO);
1126 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1130 int shift,
int flag) {
1134 nibble = 4 * s - 8 *
cs->sample1 + 4 *
cs->sample2;
1136 nibble = 4 * s - 4 *
cs->sample1;
1138 return (nibble >> shift) & 0x0F;
1142 int shift,
int flag) {
1143 int sample = sign_extend(nibble, 4) * (1 << shift);
1146 sample += (8 *
cs->sample1) - (4 *
cs->sample2);
1148 sample += 4 *
cs->sample1;
1150 sample = av_clip_int16(sample >> 2);
1152 cs->sample2 =
cs->sample1;
1153 cs->sample1 = sample;
1165 int shift,
int flag) {
1172 int nibble = adpcm_argo_compress_nibble(
cs, samples[
n], shift,
flag);
1173 int16_t sample = ff_adpcm_argo_expand_nibble(
cs, nibble, shift,
flag);
1181 out[0] = (
uint8_t)(((shift - 2) << 4) | (!!
flag << 2));
1182 for (
int n = 0;
n < 16;
n++) {
1191 int adpcm_encode_init_impl() {
1197 int adpcm_encode_frame_impl(AVPacket *avpkt,
const AVFrame *frame,
1198 int *got_packet_ptr) {
1199 av_assert(frame->nb_samples == 32);
1201 for (
int ch = 0; ch < channels(); ch++) {
1202 int64_t error = INT64_MAX, tmperr = INT64_MAX;
1203 int shift = 2, flag = 0;
1204 int saved1 = c->status[ch].sample1;
1205 int saved2 = c->status[ch].sample2;
1209 for (
int s = 2; s < 18 && tmperr != 0; s++) {
1210 for (
int f = 0; f < 2 && tmperr != 0; f++) {
1211 c->status[ch].sample1 = saved1;
1212 c->status[ch].sample2 = saved2;
1214 c->status + ch, NULL, samples_p[ch], frame->nb_samples, s, f);
1215 if (tmperr < error) {
1224 c->status[ch].sample1 = saved1;
1225 c->status[ch].sample2 = saved2;
1227 samples_p[ch], frame->nb_samples, shift, flag);
1237 setCodecID(AV_CODEC_ID_ADPCM_IMA_WS);
1238 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1242 int adpcm_encode_init_impl() {
1244 avctx.
frame_size = s->block_size * 2 / channels();
1253 av_assert(avctx.trellis == 0);
1256 for (
int ch = 0;
ch < channels();
ch++) {
1258 t1 = adpcm_ima_compress_sample(&c->status[
ch], *samples++);
1259 t2 = adpcm_ima_compress_sample(&c->status[
ch], samples[st]);
1263 samples += channels();
1278 setCodecID(AV_CODEC_ID_ADPCM_PSX);
1279 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1286 int adpcm_encode_init_impl() {
1287 if (channels() < 1 || channels() > 6)
return AVERROR(AVERROR_INVALID);
1288 memset(&s->status, 0,
sizeof(s->status));
1296 if (frame->
nb_samples != 28)
return AVERROR(AVERROR_INVALID);
1297 for (
int ch = 0;
ch < channels();
ch++) {
1298 encodeBlock(&c->status[
ch], samples_p[
ch], dst +
ch * 16);
1308 for (
int shift = 0; shift <= 12; shift++) {
1310 int s1 =
cs->sample1,
s2 =
cs->sample2;
1312 for (
int n = 0;
n < 28;
n++) {
1314 s2 * xa_adpcm_table[
filter][1]) / 64;
1315 int base = ((
in[
n] -
pred) * (1 << shift)) / 4096;
1347 for (
int k = 0; k < 14; k++) {
1349 ((
best_nib[2 * k + 1] & 0x0F) << 4));
1363 static const int FRAME_SIZE = 1025;
1366 setCodecID(AV_CODEC_ID_ADPCM_IMA_DK4);
1367 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1372 int adpcm_encode_init_impl() {
1374 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
1375 memset(&s->status, 0,
sizeof(s->status));
1377 avctx.
block_align = 4 * channels() + (FRAME_SIZE - 1) * channels() / 2;
1383 const int nch = channels();
1384 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
1393 *out++ =
first & 0xFF;
1394 *out++ = (
first >> 8) & 0xFF;
1401 for (
int t = 1; t < FRAME_SIZE; t++) {
1402 uint8_t left = adpcm_ima_compress_sample(&c->status[0], samples[t * 2]);
1404 adpcm_ima_compress_sample(&c->status[1], samples[t * 2 + 1]);
1408 for (
int t = 1; t < FRAME_SIZE; t += 2) {
1409 uint8_t hi = adpcm_ima_compress_sample(&c->status[0], samples[t]);
1410 uint8_t lo = adpcm_ima_compress_sample(&c->status[0], samples[t + 1]);
1411 *out++ = (
hi << 4) |
lo;
1424 static const int FRAME_SIZE = 1024;
1427 setCodecID(AV_CODEC_ID_ADPCM_AICA);
1428 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1433 int adpcm_encode_init_impl() {
1434 if (channels() < 1 || channels() > 6)
return AVERROR(AVERROR_INVALID);
1435 memset(&s->status, 0,
sizeof(s->status));
1437 avctx.
block_align = (FRAME_SIZE / 2) * channels();
1443 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
1444 for (
int ch = 0;
ch < channels();
ch++) {
1445 uint8_t *out = dst +
ch * (FRAME_SIZE / 2);
1446 for (
int k = 0; k < FRAME_SIZE / 2; k++) {
1447 uint8_t lo = adpcm_yamaha_compress_sample(&c->status[
ch],
1448 samples_p[
ch][2 * k]);
1449 uint8_t hi = adpcm_yamaha_compress_sample(&c->status[
ch],
1450 samples_p[
ch][2 * k + 1]);
1451 out[k] = (
hi << 4) |
lo;
1464 static const int FRAME_SIZE = 1024;
1467 setCodecID(AV_CODEC_ID_ADPCM_IMA_OKI);
1468 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1475 int adpcm_encode_init_impl() {
1476 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
1477 memset(&s->status, 0,
sizeof(s->status));
1479 avctx.
block_align = (FRAME_SIZE / 2) * channels();
1485 const int nch = channels();
1486 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
1489 for (
int t = 0; t < FRAME_SIZE; t++) {
1495 for (
int t = 0; t < FRAME_SIZE; t += 2) {
1497 uint8_t lo = quantize(0, samples[t + 1]);
1498 *out++ = (
hi << 4) |
lo;
1507 int target = sample >> 4;
1509 int step = oki_step_table[
cs->step_index];
1513 for (
int d = 0;
d < 8;
d++) {
1514 int rec = ((2 *
d + 1) * step) >> 3;
1521 int nibble =
best | (
diff < 0 ? 8 : 0);
1523 int rec = ((2 *
best + 1) * step) >> 3;
1524 cs->predictor = av_clip_intp2(
cs->predictor + (nibble & 8 ? -
rec :
rec), 11);
1526 av_clip(
cs->step_index + ff_adpcm_index_table[nibble], 0, 48);
1538 static const int FRAME_SIZE = 1024;
1541 setCodecID(AV_CODEC_ID_ADPCM_IMA_EA_SEAD);
1542 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1549 int adpcm_encode_init_impl() {
1550 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
1551 memset(&s->status, 0,
sizeof(s->status));
1553 avctx.
block_align = (FRAME_SIZE / 2) * channels();
1559 const int nch = channels();
1560 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
1563 for (
int t = 0; t < FRAME_SIZE; t++) {
1569 for (
int t = 0; t < FRAME_SIZE; t += 2) {
1571 uint8_t lo = quantize(0, samples[t + 1]);
1572 *out++ = (
hi << 4) |
lo;
1588 static const int FRAME_SIZE = 1024;
1591 setCodecID(AV_CODEC_ID_ADPCM_IMA_ACORN);
1592 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1599 int adpcm_encode_init_impl() {
1600 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
1601 memset(&s->status, 0,
sizeof(s->status));
1603 avctx.
block_align = 4 * channels() + (FRAME_SIZE / 2) * channels();
1609 const int nch = channels();
1610 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
1616 s->status[
ch].predictor =
first;
1617 s->status[
ch].step_index = 0;
1618 dst[
ch * 4 + 0] =
first & 0xFF;
1619 dst[
ch * 4 + 1] = (
first >> 8) & 0xFF;
1620 dst[
ch * 4 + 2] = 0;
1621 dst[
ch * 4 + 3] = 0;
1625 for (
int t = 0; t < FRAME_SIZE; t++) {
1631 for (
int t = 0; t < FRAME_SIZE; t += 2) {
1651 static const int FRAME_SIZE = 1024;
1654 setCodecID(AV_CODEC_ID_ADPCM_IMA_DAT4);
1655 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1662 int adpcm_encode_init_impl() {
1663 if (channels() < 1 || channels() > ADPCM_MAX_CHANNELS)
1664 return AVERROR(AVERROR_INVALID);
1665 memset(&s->status, 0,
sizeof(s->status));
1667 avctx.
block_align = channels() * (4 + FRAME_SIZE / 2);
1673 const int nch = channels();
1674 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
1676 const int chunk = 4 + FRAME_SIZE / 2;
1679 for (
int t = 0; t < FRAME_SIZE; t += 2) {
1682 *out++ = (
hi << 4) |
lo;
1700 static const int FRAME_SIZE = 1024;
1703 setCodecID(AV_CODEC_ID_ADPCM_AGM);
1704 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1711 int adpcm_encode_init_impl() {
1712 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
1713 memset(&s->status, 0,
sizeof(s->status));
1715 avctx.
block_align = channels() * (4 + FRAME_SIZE / 2);
1721 const int nch = channels();
1722 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
1728 s->status[
ch].predictor =
first;
1729 s->status[
ch].step = 127;
1732 dst[2 *
nch +
ch * 2] = 127;
1733 dst[2 *
nch +
ch * 2 + 1] = 0;
1738 for (
int t = 0; t < FRAME_SIZE; t++) {
1740 uint8_t ch1 = quantize(1, samples[t * 2 + 1]);
1741 *out++ =
ch0 | (
ch1 << 4);
1744 for (
int t = 0; t < FRAME_SIZE; t += 2) {
1754 int delta = nibble & 7;
1758 if ((nibble & 8) == 0)
1780 if (step < 0) step += 0x3f;
1781 *
next_step = av_clip(step >> 6, 127, 24576);
1788 for (
int nibble = 0; nibble < 16; nibble++) {
1814 static const int FRAME_SIZE = 1024;
1815 static const int HEADER = 16;
1818 setCodecID(AV_CODEC_ID_ADPCM_MTAF);
1819 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1826 int adpcm_encode_init_impl() {
1828 if (channels() < 2 || channels() > 8 || (channels() & 1))
1829 return AVERROR(AVERROR_INVALID);
1830 memset(&s->status, 0,
sizeof(s->status));
1832 avctx.
block_align = (channels() / 2) * (HEADER + FRAME_SIZE);
1838 const int nch = channels();
1839 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
1848 s->status[
ch].step = 0;
1849 s->status[
ch + 1].predictor =
first1;
1850 s->status[
ch + 1].step = 0;
1862 for (
int c =
ch; c <
ch + 2; c++) {
1863 for (
int t = 0; t < FRAME_SIZE; t += 2) {
1866 *out++ =
lo | (
hi << 4);
1869 block += HEADER + FRAME_SIZE;
1877 for (
int nibble = 0; nibble < 16; nibble++) {
1878 int pred = av_clip_int16(
cs->predictor + mtaf_stepsize[
cs->step][nibble]);
1879 int next_step = av_clip_uintp2(
cs->step + ff_adpcm_index_table[nibble], 5);
1904 static const int UNIT_SAMPLES = 28;
1905 static const int UNIT_BYTES = 32;
1906 static const int UNITS = 36;
1907 static const int FRAME_SIZE = UNIT_SAMPLES * UNITS;
1910 setCodecID(AV_CODEC_ID_ADPCM_DTK);
1911 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1918 int adpcm_encode_init_impl() {
1919 if (channels() != 2)
return AVERROR(AVERROR_INVALID);
1920 memset(&s->status, 0,
sizeof(s->status));
1928 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
1932 for (
int ch = 0;
ch < 2;
ch++) {
1935 for (
int k = 0; k < UNIT_SAMPLES; k++) {
1937 u[4 + k] |= (
uint8_t)((unit_nibbles_[k] & 0x0F) << (
ch * 4));
1943 int unit_nibbles_[UNIT_SAMPLES];
1946 static int predict(
int p,
int s1,
int s2) {
1951 return s1 * 0x73 -
s2 * 0x34;
1953 return s1 * 0x62 -
s2 * 0x37;
1961 int encodeUnit(
int ch,
int start) {
1967 for (
int p = 0; p < 4; p++) {
1968 for (
int sh = 0;
sh <= 12;
sh++) {
1969 int s1 =
cs->sample1,
s2 =
cs->sample2;
1970 int nib[UNIT_SAMPLES];
1972 for (
int k = 0; k < UNIT_SAMPLES; k++) {
1974 int prev = av_clip_intp2((predict(p,
s1,
s2) + 0x20) >> 6, 21);
1976 for (
int n = -8;
n <= 7;
n++) {
1977 int sampledat = ((
n * (1 << 12)) >>
sh) * (1 << 6) + prev;
1978 int out = av_clip_int16(
sampledat >> 6);
2000 for (
int k = 0; k < UNIT_SAMPLES; k++) unit_nibbles_[k] =
best_nib[k];
2014 static const int FRAME_SIZE = 1024;
2017 setCodecID(AV_CODEC_ID_ADPCM_ZORK);
2018 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2025 int adpcm_encode_init_impl() {
2026 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
2027 memset(&s->status, 0,
sizeof(s->status));
2035 const int nch = channels();
2036 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
2037 for (
int t = 0; t < FRAME_SIZE *
nch; t++) {
2038 dst[t] = quantize(t %
nch, samples[t]);
2044 static int zork_reconstruct(
int predictor,
int step_index,
uint8_t nibble,
2055 if (nibble & 0x80) sample = -sample;
2056 sample += predictor;
2057 *
next_index = av_clip(step_index + zork_index_table[(nibble >> 4) & 7], 0, 88);
2058 return av_clip_int16(sample);
2064 for (
int b = 0;
b < 256;
b++) {
2090 static const int FRAME_SIZE = 1024;
2091 static const int BLOCK = 32;
2092 static const int BLOCK_BYTES = 21;
2095 setCodecID(AV_CODEC_ID_ADPCM_XMD);
2096 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2103 int adpcm_encode_init_impl() {
2104 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
2106 avctx.
block_align = (FRAME_SIZE / BLOCK) * BLOCK_BYTES * channels();
2112 const int nch = channels();
2113 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
2126 for (
int k = 0; k < BLOCK; k++)
x[k] = samples[(
start + k) *
nch +
ch];
2130 for (
int k = 2; k < BLOCK; k++) {
2131 int pred = (
x[k - 1] * 3667 -
x[k - 2] * 1642) >> 11;
2146 out[4] =
scale & 0xFF;
2147 out[5] = (
scale >> 8) & 0xFF;
2151 for (
int n = 0;
n < 15;
n++) {
2153 for (
int j = 0; j < 2; j++) {
2154 int k = 2 +
n * 2 + j;
2155 int pred = (
h0 * 3667 -
h1 * 1642) >> 11;
2177 static const int FRAME_SIZE = 1024;
2180 setCodecID(AV_CODEC_ID_ADPCM_IMA_CUNNING);
2181 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2188 int adpcm_encode_init_impl() {
2189 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
2190 memset(&s->status, 0,
sizeof(s->status));
2192 avctx.
block_align = (FRAME_SIZE / 2) * channels();
2198 const int nch = channels();
2199 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
2202 for (
int t = 0; t < FRAME_SIZE; t += 2) {
2205 *out++ = (
lo & 0x0F) | ((
hi & 0x0F) << 4);
2211 static int cunning_reconstruct(
int predictor,
int step_index,
int nibble,
2213 nibble = sign_extend(nibble & 0xF, 4);
2214 const int step = ima_cunning_step_table[step_index];
2215 *
next_index = av_clip(step_index + ima_cunning_index_table[
abs(nibble)], 0, 60);
2216 return av_clip_int16(predictor + step * nibble);
2222 for (
int v = 0;
v < 16;
v++) {
2247 static const int FRAME_SIZE = 1024;
2250 setCodecID(AV_CODEC_ID_ADPCM_IMA_MTF);
2251 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2258 int adpcm_encode_init_impl() {
2259 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
2260 memset(&s->status, 0,
sizeof(s->status));
2262 avctx.
block_align = (FRAME_SIZE / 2) * channels();
2268 const int nch = channels();
2269 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
2271 for (
int t = 0; t < FRAME_SIZE; t += 2) {
2281 static int mtf_reconstruct(
int predictor,
int step_index,
int nibble,
2283 const int step = ff_adpcm_step_table[step_index];
2284 const int delta = step * (2 * nibble - 15);
2285 *
next_index = av_clip(step_index + mtf_index_table[nibble], 0, 88);
2286 return av_clip_int16((predictor +
delta) >> 4);
2292 for (
int v = 0;
v < 16;
v++) {
2324 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2329 return started() ? avctx.
block_align : firstPacketBytes();
2335 int adpcm_encode_init_impl() {
2336 const int ch = channels();
2337 if (!(
ch == 1 || (
ch == 2 && codecID() != AV_CODEC_ID_ADPCM_SBPRO_3)))
2338 return AVERROR(AVERROR_INVALID);
2339 frame_ = frameSizeFor(codecID());
2340 if ((frame_ *
ch) % samplesPerByte() != 0)
2341 return AVERROR(AVERROR_INVALID);
2342 memset(&s->status, 0,
sizeof(s->status));
2349 int adpcm_encode_frame_impl(AVPacket *avpkt,
const AVFrame *frame,
2350 int *got_packet_ptr) {
2351 if (frame->nb_samples != frame_)
return AVERROR(AVERROR_INVALID);
2352 const int ch = channels();
2353 const int spb = samplesPerByte();
2354 const bool first = !started();
2356 const int emit = first ? firstPacketSamples() : frame_;
2360 for (
int c = 0; c < ch; c++) {
2361 int b = ((sampleAt(c, 0) + 64) >> 7) + 0x80;
2362 *out++ = (uint8_t)(b < 0 ? 0 : b > 255 ? 255 : b);
2363 s->status[c].predictor = 0;
2364 s->status[c].step = 0;
2366 s->status[0].step_index = 1;
2373 for (
int t = t0; t < emit; t++) {
2374 for (
int c = 0; c < ch; c++, v++) {
2375 const int pos = v % spb;
2376 const int size = valueSize(pos);
2377 uint8_t nib = quantize(c, sampleAt(c, t), size, shift());
2378 acc = (acc << size) | nib;
2379 if (pos == spb - 1) {
2380 *out++ = (uint8_t)acc;
2385 assert(out - dst == pkt_size);
2388 const int carry_new = carry_ + frame_ - emit;
2389 assert(carry_new >= 0 && carry_new <= 3);
2391 for (
int c = 0; c < ch; c++)
2392 for (
int j = 0; j < carry_new; j++) next[c][j] = sampleAt(c, emit + j);
2393 for (
int c = 0; c < ch; c++)
2394 for (
int j = 0; j < carry_new; j++) carry_buf_[c][j] = next[c][j];
2400 int16_t carry_buf_[2][3] = {{0}};
2402 static int frameSizeFor(AVCodecID
id) {
2403 return id == AV_CODEC_ID_ADPCM_SBPRO_3 ? 1023 : 1024;
2406 int samplesPerByte() {
2407 switch (codecID()) {
2408 case AV_CODEC_ID_ADPCM_SBPRO_2:
2410 case AV_CODEC_ID_ADPCM_SBPRO_3:
2418 int valueSize(
int pos) {
2419 switch (codecID()) {
2420 case AV_CODEC_ID_ADPCM_SBPRO_2:
2422 case AV_CODEC_ID_ADPCM_SBPRO_3:
2423 return pos < 2 ? 3 : 2;
2430 int shift() {
return codecID() == AV_CODEC_ID_ADPCM_SBPRO_2 ? 2 : 0; }
2433 bool started() {
return s->status[0].step_index != 0; }
2438 int firstPacketSamples() {
2439 const int ch = channels();
2440 const int spb = samplesPerByte();
2441 const int step = spb / ((ch == 2 && spb % 2 == 0) ? 2 : 1);
2442 const int body = ((frame_ - 1) / step) * step;
2446 int firstPacketBytes() {
2447 const int ch = channels();
2448 return ch + (firstPacketSamples() - 1) * ch / samplesPerByte();
2452 int16_t sampleAt(
int c,
int t) {
2453 if (t < carry_)
return carry_buf_[c][t];
2454 return samples[(t - carry_) * channels() + c];
2458 static void reconstruct(
int predictor,
int step,
int nibble,
int size,
2459 int shift,
int *next_pred,
int *next_step) {
2460 const int sign = nibble & (1 << (size - 1));
2461 const int delta = nibble & ((1 << (size - 1)) - 1);
2462 const int diff = delta << (7 + step + shift);
2463 *next_pred = av_clip(predictor + (sign ? -diff : diff), -16384, 16256);
2465 if (delta >= 2 * size - 3 && step < 3)
2466 *next_step = step + 1;
2467 else if (delta == 0 && step > 0)
2468 *next_step = step - 1;
2472 uint8_t quantize(
int ch,
int target,
int size,
int shift) {
2473 ADPCMChannelStatus *cs = &s->status[ch];
2474 int best = 0, best_e = 0x7fffffff, best_pred = 0, best_step = 0;
2475 for (
int nib = 0; nib < (1 << size); nib++) {
2476 int pred = 0, step = 0;
2477 reconstruct(cs->predictor, cs->step, nib, size, shift, &pred, &step);
2478 int e = pred > target ? pred - target : target - pred;
2486 cs->predictor = best_pred;
2487 cs->step = best_step;
2488 return (uint8_t)best;
2504 static const int FRAME_SIZE = 224;
2505 static const int BLOCK = 128;
2508 setCodecID(AV_CODEC_ID_ADPCM_XA);
2509 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2516 int adpcm_encode_init_impl() {
2517 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
2518 memset(&s->status, 0,
sizeof(s->status));
2526 const int nch = channels();
2527 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
2532 for (
int b = 0;
b <
nch;
b++) {
2535 for (
int i = 0; i < 4; i++) {
2536 const int base = i * 56;
2537 for (
int j = 0; j < 28; j++)
x[j] = samples[
base + j];
2538 blk[4 + 2 * i] = encodeSegment(0,
x,
nl);
2539 for (
int j = 0; j < 28; j++)
x[j] = samples[
base + 28 + j];
2540 blk[5 + 2 * i] = encodeSegment(0,
x,
nr);
2541 for (
int j = 0; j < 28; j++) {
2543 blk[16 + i + 4 * j] = (
uint8_t)((
nl[j] & 0x0F) | ((
nr[j] & 0x0F) << 4));
2547 for (
int i = 0; i < 4; i++) {
2548 const int base =
b * 112 + i * 28;
2550 for (
int ch = 0;
ch < 2;
ch++) {
2551 for (
int j = 0; j < 28; j++)
x[j] = samples[(
base + j) * 2 +
ch];
2555 for (
int j = 0; j < 28; j++) {
2556 blk[16 + i + 4 * j] = (
uint8_t)((
nl[j] & 0x0F) | ((
nr[j] & 0x0F) << 4));
2572 for (
int f = 0;
f < 5;
f++) {
2573 for (
int sf = 0;
sf <= 12;
sf++) {
2574 const int f0 = xa_adpcm_table[
f][0];
2575 const int f1 = xa_adpcm_table[
f][1];
2576 const int shift = 12 -
sf;
2577 int s1 =
cs->sample1,
s2 =
cs->sample2;
2580 for (
int j = 0; j < 28; j++) {
2583 for (
int n = -8;
n <= 7;
n++) {
2584 int v = av_clip_int16(
n * (1 << shift) +
pred);
2585 int e =
v >
x[j] ?
v -
x[j] :
x[j] -
v;
2624 static const int FRAME_SIZE = 1008;
2625 static const int PIECES = FRAME_SIZE / 28;
2628 setCodecID(AV_CODEC_ID_ADPCM_EA);
2629 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2636 int adpcm_encode_init_impl() {
2637 if (channels() != 2)
return AVERROR(AVERROR_INVALID);
2638 memset(&s->status, 0,
sizeof(s->status));
2646 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
2649 dst[0] = FRAME_SIZE & 0xFF;
2650 dst[1] = (FRAME_SIZE >> 8) & 0xFF;
2651 for (
int ch = 0;
ch < 2;
ch++) {
2652 put16(dst + 4 + 4 *
ch, s->status[
ch].predictor);
2653 put16(dst + 6 + 4 *
ch, s->status[
ch].prev_sample);
2657 for (
int p = 0; p < PIECES; p++,
piece += 30) {
2660 for (
int ch = 0;
ch < 2;
ch++) {
2662 for (
int k = 0; k < 28; k++)
x[k] = samples[(p * 28 + k) * 2 +
ch];
2669 for (
int k = 0; k < 28; k++) {
2671 (
nibs[1][k] & 0x0F));
2677 static void put16(
uint8_t *p,
int v) {
2693 for (
int sf = 0;
sf < 16;
sf++) {
2694 const int c1 = ea_adpcm_table[
idx];
2695 const int c2 = ea_adpcm_table[
idx + 4];
2696 const int shift = 20 -
sf;
2697 int cu =
cs->predictor,
pv =
cs->prev_sample;
2700 for (
int k = 0; k < 28; k++) {
2702 for (
int n = -8;
n <= 7;
n++) {
2703 int v = av_clip_int16(((
n * (1 << shift)) +
cu *
c1 +
pv *
c2 + 0x80) >> 8);
2704 int e =
v >
x[k] ?
v -
x[k] :
x[k] -
v;
2744 static const int FRAME_SIZE = 1008;
2745 static const int BLOCKS = FRAME_SIZE / 28;
2746 static const int BLOCK_BYTES = 15;
2750 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2757 int adpcm_encode_init_impl() {
2758 if (channels() < 1 || channels() > 6)
return AVERROR(AVERROR_INVALID);
2759 memset(&s->status, 0,
sizeof(s->status));
2761 avctx.
block_align = headerSize() + channels() * channelBytes();
2767 const int nch = channels();
2768 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
2770 const bool be = variant_ == AV_CODEC_ID_ADPCM_EA_R3;
2771 put32(dst, FRAME_SIZE,
be);
2773 int pos = headerSize();
2775 put32(dst + 4 + 4 *
ch, pos - headerSize(),
be);
2778 int cur = 0, prev = 0;
2779 if (variant_ == AV_CODEC_ID_ADPCM_EA_R1) {
2786 cur =
cs->predictor;
2787 prev =
cs->prev_sample;
2790 for (
int b = 0;
b < BLOCKS;
b++) {
2791 for (
int k = 0; k < 28; k++)
x[k] = samples[(
b * 28 + k) *
nch +
ch];
2792 encodeBlock(
x, &
cur, &prev,
d +
b * BLOCK_BYTES);
2794 if (variant_ != AV_CODEC_ID_ADPCM_EA_R1) {
2795 cs->predictor =
cur;
2796 cs->prev_sample = prev;
2798 pos += channelBytes();
2804 int headerSize() {
return 4 + 4 * channels(); }
2806 int channelBytes() {
2807 return BLOCKS * BLOCK_BYTES +
2808 (variant_ == AV_CODEC_ID_ADPCM_EA_R1 ? 4 : 0);
2812 for (
int i = 0; i < 4; i++) {
2813 int shift =
be ? (3 - i) * 8 : i * 8;
2814 p[i] = (
v >> shift) & 0xFF;
2819 void encodeBlock(
const int *
x,
int *
cur,
int *prev,
uint8_t *out) {
2825 for (
int h = 0;
h < 256;
h++) {
2826 if (
h == 0xEE)
continue;
2827 const int c1 = ea_adpcm_table[
h >> 4];
2828 const int c2 = ea_adpcm_table[(
h >> 4) + 4];
2829 const int shift = 20 - (
h & 0x0F);
2833 for (
int k = 0; k < 28; k++) {
2835 for (
int n = -8;
n <= 7;
n++) {
2836 int v = av_clip_int16(((
n * (1 << shift)) +
cu *
c1 +
pv *
c2) >> 8);
2837 int e =
v >
x[k] ?
v -
x[k] :
x[k] -
v;
2852 for (
int k = 0; k < 14; k++) {
2854 (
nib[2 * k + 1] & 0x0F));
2877 static const int FRAME_SIZE = 128;
2878 static const int GROUP = 32;
2879 static const int CHUNK = 76;
2882 setCodecID(AV_CODEC_ID_ADPCM_EA_XAS);
2883 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2890 int adpcm_encode_init_impl() {
2891 if (channels() < 1 || channels() > 6)
return AVERROR(AVERROR_INVALID);
2899 const int nch = channels();
2900 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
2904 for (
int t = 0; t < FRAME_SIZE; t++)
x[t] = samples[t *
nch +
ch];
2905 encodeChunk(
x, dst +
ch * CHUNK);
2909 void encodeChunk(
const int *
x,
uint8_t *out) {
2911 for (
int g = 0;
g < 4;
g++) {
2913 encodeGroup(
x +
g * GROUP, &
w0, &
w1, data[
g]);
2914 out[
g * 4 + 0] =
w0 & 0xFF;
2915 out[
g * 4 + 1] = (
w0 >> 8) & 0xFF;
2916 out[
g * 4 + 2] =
w1 & 0xFF;
2917 out[
g * 4 + 3] = (
w1 >> 8) & 0xFF;
2920 for (
int k = 0; k < 15; k++) {
2921 for (
int g = 0;
g < 4;
g++)
d[k * 4 +
g] = data[
g][k];
2927 const int raw0 =
x[0] & ~0x0F;
2928 const int raw1 =
x[1] & ~0x0F;
2934 for (
int sf = 0;
sf < 16;
sf++) {
2935 const int c0 = ea_adpcm_table[
idx];
2936 const int c1 = ea_adpcm_table[
idx + 4];
2937 const int shift = 20 -
sf;
2942 for (
int k = 0; k < 15; k++) {
2943 const int m = 2 + 2 * k;
2945 int n0 = 0,
e0 = 0x7fffffff,
out0 = 0;
2946 for (
int n = -8;
n <= 7;
n++) {
2947 int v = av_clip_int16((
n * (1 << shift) +
pred0) >> 8);
2956 int n1 = 0,
e1 = 0x7fffffff,
out1 = 0;
2957 for (
int n = -8;
n <= 7;
n++) {
2958 int v = av_clip_int16((
n * (1 << shift) +
pred1) >> 8);
2959 int e =
v >
x[
m + 1] ?
v -
x[
m + 1] :
x[
m + 1] -
v;
2995 static const int FRAME_SIZE = 1024;
2998 setCodecID(AV_CODEC_ID_ADPCM_EA_MAXIS_XA);
2999 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3006 int adpcm_encode_init_impl() {
3007 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
3008 memset(&s->status, 0,
sizeof(s->status));
3010 avctx.
block_align = channels() * (1 + FRAME_SIZE / 2);
3016 const int nch = channels();
3017 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
3025 for (
int k = 0; k < FRAME_SIZE / 2; k++) {
3034 int encodeChannel(
int ch,
int *
nibbles) {
3042 int c0 = ea_adpcm_table[(
header >> 4)];
3043 int c1 = ea_adpcm_table[(
header >> 4) + 4];
3044 int shift = 20 - (
header & 0x0F);
3045 int s1 =
cs->sample1,
s2 =
cs->sample2;
3047 int nib[FRAME_SIZE];
3048 for (
int t = 0; t < FRAME_SIZE; t++) {
3049 int target = samples[t * channels() +
ch];
3051 for (
int n = -8;
n <= 7;
n++) {
3052 int level =
n * (1 << shift);
3053 int out = av_clip_int16((
level +
s1 *
c0 +
s2 *
c1 + 0x80) >> 8);
3089 static const int FRAME_SIZE = 1024;
3092 setCodecID(AV_CODEC_ID_ADPCM_4XM);
3093 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3100 int adpcm_encode_init_impl() {
3101 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
3102 memset(&s->status, 0,
sizeof(s->status));
3104 avctx.
block_align = channels() * (4 + FRAME_SIZE / 2);
3110 const int nch = channels();
3111 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
3116 s->status[
ch].predictor =
first;
3117 s->status[
ch].step_index = 0;
3125 for (
int t = 0; t < FRAME_SIZE; t += 2) {
3128 *out++ =
lo | (
hi << 4);
3146 static const int FRAME_SIZE = 1024;
3149 setCodecID(AV_CODEC_ID_ADPCM_IMA_EA_EACS);
3150 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3157 int adpcm_encode_init_impl() {
3158 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
3159 memset(&s->status, 0,
sizeof(s->status));
3161 avctx.
block_align = 4 + 8 * channels() + (FRAME_SIZE / 2) * channels();
3167 const int nch = channels();
3168 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
3169 const int hdr = 4 + 8 *
nch;
3173 dst[0] = FRAME_SIZE & 0xFF;
3174 dst[1] = (FRAME_SIZE >> 8) & 0xFF;
3179 s->status[
ch].predictor =
first;
3180 s->status[
ch].step_index = 0;
3190 for (
int t = 0; t < FRAME_SIZE; t++) {
3196 for (
int t = 0; t < FRAME_SIZE; t += 2) {
3198 uint8_t lo = quantize(0, samples[t + 1]);
3199 *out++ = (
hi << 4) |
lo;
3217 static const int FRAME_SIZE = 1024;
3220 setCodecID(AV_CODEC_ID_ADPCM_IMA_APC);
3221 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3228 int adpcm_encode_init_impl() {
3229 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
3230 memset(&s->status, 0,
sizeof(s->status));
3232 avctx.
block_align = (FRAME_SIZE / 2) * channels();
3238 const int nch = channels();
3239 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
3242 for (
int t = 0; t < FRAME_SIZE; t++) {
3248 for (
int t = 0; t < FRAME_SIZE; t += 2) {
3250 uint8_t lo = quantize(0, samples[t + 1]);
3251 *out++ = (
hi << 4) |
lo;
3269 static const int FRAME_SIZE = 1024;
3272 setCodecID(AV_CODEC_ID_ADPCM_IMA_SMJPEG);
3273 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3280 int adpcm_encode_init_impl() {
3281 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
3282 memset(&s->status, 0,
sizeof(s->status));
3284 avctx.
block_align = channels() * (4 + FRAME_SIZE / 2);
3290 const int nch = channels();
3291 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
3296 s->status[
ch].predictor =
first;
3297 s->status[
ch].step_index = 0;
3298 dst[
ch * 4 + 0] = (
first >> 8) & 0xFF;
3299 dst[
ch * 4 + 1] =
first & 0xFF;
3300 dst[
ch * 4 + 2] = 0;
3301 dst[
ch * 4 + 3] = 0;
3306 for (
int t = 0; t < FRAME_SIZE; t++) {
3312 for (
int t = 0; t < FRAME_SIZE; t += 2) {
3314 uint8_t lo = quantize(0, samples[t + 1]);
3315 *out++ = (
hi << 4) |
lo;
3324 int step = ff_adpcm_step_table[
cs->step_index];
3326 for (
int nibble = 0; nibble < 16; nibble++) {
3327 int diff = step >> 3;
3328 if (nibble & 4)
diff += step;
3329 if (nibble & 2)
diff += step >> 1;
3330 if (nibble & 1)
diff += step >> 2;
3331 int pred = av_clip_int16((nibble & 8) ?
cs->predictor -
diff
3342 av_clip(
cs->step_index + ff_adpcm_index_table[
best], 0, 88);
3355 static const int FRAME_SIZE = 1024;
3356 static const int SUBFRAME = 256;
3359 setCodecID(AV_CODEC_ID_ADPCM_IMA_MOFLEX);
3360 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3367 int adpcm_encode_init_impl() {
3368 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
3369 memset(&s->status, 0,
sizeof(s->status));
3371 avctx.
block_align = channels() * (4 + FRAME_SIZE / 2);
3377 const int nch = channels();
3378 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
3383 s->status[
ch].predictor =
first;
3384 s->status[
ch].step_index = 0;
3386 dst[
ch * 4 + 0] = 0;
3387 dst[
ch * 4 + 1] = 0;
3388 dst[
ch * 4 + 2] =
first & 0xFF;
3389 dst[
ch * 4 + 3] = (
first >> 8) & 0xFF;
3393 for (
int sub = 0;
sub < FRAME_SIZE / SUBFRAME;
sub++) {
3395 for (
int n = 0;
n < SUBFRAME;
n += 2) {
3396 int t =
sub * SUBFRAME +
n;
3399 *out++ =
lo | (
hi << 4);
3418 static const int FRAME_SIZE = 1024;
3421 setCodecID(AV_CODEC_ID_ADPCM_IMA_RAD);
3422 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3429 int adpcm_encode_init_impl() {
3430 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
3431 memset(&s->status, 0,
sizeof(s->status));
3433 avctx.
block_align = channels() * (4 + FRAME_SIZE / 2);
3439 const int nch = channels();
3440 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
3445 s->status[
ch].predictor =
first;
3446 s->status[
ch].step_index = 0;
3447 dst[
ch * 4 + 0] = 0;
3448 dst[
ch * 4 + 1] = 0;
3449 dst[
ch * 4 + 2] =
first & 0xFF;
3450 dst[
ch * 4 + 3] = (
first >> 8) & 0xFF;
3454 for (
int n = 0;
n < FRAME_SIZE;
n += 2) {
3458 *out++ =
lo | (
hi << 4);
3476 static const int FRAME_SIZE = 1024;
3479 setCodecID(AV_CODEC_ID_ADPCM_IMA_ISS);
3480 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3487 int adpcm_encode_init_impl() {
3488 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
3489 memset(&s->status, 0,
sizeof(s->status));
3491 avctx.
block_align = 4 * channels() + (FRAME_SIZE / 2) * channels();
3497 const int nch = channels();
3498 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
3505 s->status[
ch].predictor =
first;
3506 s->status[
ch].step_index = 0;
3507 dst[
ch * 4 + 0] =
first & 0xFF;
3508 dst[
ch * 4 + 1] = (
first >> 8) & 0xFF;
3509 dst[
ch * 4 + 2] = 0;
3510 dst[
ch * 4 + 3] = 0;
3514 for (
int t = 0; t < FRAME_SIZE; t++) {
3520 for (
int t = 0; t < FRAME_SIZE; t += 2) {
3541 static const int FRAME_SIZE = 1024;
3542 static const int HEADER_SIZE = 16;
3545 setCodecID(AV_CODEC_ID_ADPCM_IMA_DK3);
3546 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3553 int adpcm_encode_init_impl() {
3555 if (channels() != 2)
return AVERROR(AVERROR_INVALID);
3556 memset(&s->status, 0,
sizeof(s->status));
3558 avctx.
block_align = HEADER_SIZE + FRAME_SIZE * 3 / 4;
3564 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
3571 int sum0 = (samples[0] + samples[1]) >> 1;
3572 int diff0 = (samples[0] - samples[1]) >> 1;
3573 s->status[0].predictor =
sum0;
3574 s->status[1].predictor =
diff0;
3575 s->status[0].step_index = 0;
3576 s->status[1].step_index = 0;
3586 auto put = [&](
int nibble) {
3588 nib[nidx >> 1] |= nibble << 4;
3590 nib[nidx >> 1] = nibble;
3595 for (
int t = 0; t < FRAME_SIZE; t += 2) {
3601 put(quantize(0,
sum_a));
3602 put(quantize(1,
diff));
3603 put(quantize(0,
sum_c));
3621 static const int FRAME_SIZE = 1024;
3624 setCodecID(AV_CODEC_ID_ADPCM_CT);
3625 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3632 int adpcm_encode_init_impl() {
3633 if (channels() < 1 || channels() > 2)
return AVERROR(AVERROR_INVALID);
3634 memset(&s->status, 0,
sizeof(s->status));
3635 s->status[0].step = s->status[1].step = 511;
3637 avctx.
block_align = (FRAME_SIZE / 2) * channels();
3643 const int nch = channels();
3644 if (frame->
nb_samples != FRAME_SIZE)
return AVERROR(AVERROR_INVALID);
3647 for (
int t = 0; t < FRAME_SIZE; t++) {
3653 for (
int t = 0; t < FRAME_SIZE; t += 2) {
3655 uint8_t lo = quantize(0, samples[t + 1]);
3656 *out++ = (
hi << 4) |
lo;
3667 for (
int nibble = 0; nibble < 16; nibble++) {
3668 int diff = ((2 * (nibble & 7) + 1) *
cs->step) >> 3;
3669 int pred = ((
cs->predictor * 254) >> 8) + (nibble & 8 ? -
diff :
diff);
3678 int new_step = (ff_adpcm_AdaptationTable[
best & 7] *
cs->step) >> 8;
3692 static const int SUBFRAME_SAMPLES = 16;
3695 setCodecID(AV_CODEC_ID_ADPCM_AFC);
3696 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
3703 int adpcm_encode_init_impl() {
3704 if (channels() < 1 || channels() > 6)
return AVERROR(AVERROR_INVALID);
3705 memset(&s->status, 0,
sizeof(s->status));
3713 if (frame->
nb_samples != SUBFRAME_SAMPLES)
return AVERROR(AVERROR_INVALID);
3715 for (
int ch = 0;
ch < channels();
ch++) {
3716 encodeSubframe(&c->status[
ch], samples_p[
ch], dst +
ch * 9);
3721 int index = 0,
exp = 0;
3723 16, out + 1, index,
exp);
3736 static const int SUBFRAMES = 32;
3737 static const int FRAME_SAMPLES = 14 * SUBFRAMES;
3741 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
3748 int adpcm_encode_init_impl() {
3749 if (channels() < 1 || channels() > 6)
return AVERROR(AVERROR_INVALID);
3750 memset(&s->status, 0,
sizeof(s->status));
3752 avctx.
block_align = 8 + 36 * channels() + 8 * SUBFRAMES * channels();
3758 if (frame->
nb_samples != FRAME_SAMPLES)
return AVERROR(AVERROR_INVALID);
3759 const int nch = channels();
3764 put32(out + 4, FRAME_SAMPLES);
3769 for (
int i = 0; i < 8; i++) {
3770 put16(out, afc_coeffs[0][i]);
3771 put16(out + 2, afc_coeffs[1][i]);
3778 put16(out, c->status[
ch].sample1);
3779 put16(out + 2, c->status[
ch].sample2);
3785 for (
int k = 0; k < SUBFRAMES; k++) {
3786 int index = 0,
exp = 0;
3788 afc_coeffs[0], afc_coeffs[1], 8, out + 1, index,
3796 bool isLE() {
return codecID() == AV_CODEC_ID_ADPCM_THP_LE; }
3801 p[1] = (
v >> 8) & 0xFF;
3803 p[0] = (
v >> 8) & 0xFF;
3810 for (
int i = 0; i < 4; i++) p[i] = (
v >> (8 * i)) & 0xFF;
3812 for (
int i = 0; i < 4; i++) p[i] = (
v >> (24 - 8 * i)) & 0xFF;
3821 case AV_CODEC_ID_ADPCM_IMA_WAV:
3823 case AV_CODEC_ID_ADPCM_IMA_QT:
3825 case AV_CODEC_ID_ADPCM_IMA_SSI:
3827 case AV_CODEC_ID_ADPCM_IMA_ALP:
3829 case AV_CODEC_ID_ADPCM_MS:
3831 case AV_CODEC_ID_ADPCM_SWF:
3833 case AV_CODEC_ID_ADPCM_YAMAHA:
3835 case AV_CODEC_ID_ADPCM_IMA_APM:
3837 case AV_CODEC_ID_ADPCM_IMA_AMV:
3839 case AV_CODEC_ID_ADPCM_ARGO:
3841 case AV_CODEC_ID_ADPCM_IMA_WS:
3843 case AV_CODEC_ID_ADPCM_PSX:
3845 case AV_CODEC_ID_ADPCM_IMA_DK4:
3847 case AV_CODEC_ID_ADPCM_AICA:
3849 case AV_CODEC_ID_ADPCM_IMA_OKI:
3851 case AV_CODEC_ID_ADPCM_AFC:
3853 case AV_CODEC_ID_ADPCM_THP:
3855 case AV_CODEC_ID_ADPCM_THP_LE:
3857 case AV_CODEC_ID_ADPCM_CT:
3859 case AV_CODEC_ID_ADPCM_IMA_DK3:
3861 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
3863 case AV_CODEC_ID_ADPCM_IMA_ISS:
3865 case AV_CODEC_ID_ADPCM_IMA_ACORN:
3867 case AV_CODEC_ID_ADPCM_IMA_DAT4:
3869 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
3871 case AV_CODEC_ID_ADPCM_IMA_RAD:
3873 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
3875 case AV_CODEC_ID_ADPCM_IMA_APC:
3877 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
3879 case AV_CODEC_ID_ADPCM_ZORK:
3881 case AV_CODEC_ID_ADPCM_XMD:
3883 case AV_CODEC_ID_ADPCM_AGM:
3885 case AV_CODEC_ID_ADPCM_MTAF:
3887 case AV_CODEC_ID_ADPCM_DTK:
3889 case AV_CODEC_ID_ADPCM_4XM:
3891 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
3893 case AV_CODEC_ID_ADPCM_EA_XAS:
3895 case AV_CODEC_ID_ADPCM_EA_R1:
3897 case AV_CODEC_ID_ADPCM_EA_R2:
3899 case AV_CODEC_ID_ADPCM_EA_R3:
3901 case AV_CODEC_ID_ADPCM_EA:
3903 case AV_CODEC_ID_ADPCM_XA:
3905 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
3907 case AV_CODEC_ID_ADPCM_IMA_MTF:
3909 case AV_CODEC_ID_ADPCM_SBPRO_2:
3910 case AV_CODEC_ID_ADPCM_SBPRO_3:
3911 case AV_CODEC_ID_ADPCM_SBPRO_4:
3915 av_log(avctx, AV_LOG_ERROR,
"ERROR: encoder [%d] not implemented\n",
id);
Common ADPCM Functionality.
Definition ADPCMCodec.h:23
Definition ADPCMEncoder.h:3817
Definition ADPCMEncoder.h:386
Definition ADPCMEncoder.h:33
void encodeDSPSubframe(ADPCMChannelStatus *cs, const int16_t *in, int n, const int16_t *coef1, const int16_t *coef2, int npairs, uint8_t *nibbles_out, int &best_index, int &best_exp)
Definition ADPCMEncoder.h:107
virtual int packetSize(const AVFrame *frame)
Definition ADPCMEncoder.h:184
uint8_t adpcm_ima_quantize(ADPCMChannelStatus *cs, int target, int shift)
Definition ADPCMEncoder.h:278
Vector implementation which provides the most important methods as defined by std::vector....
Definition ADPCMVector.h:16
Writes bits into a buffer. By default the first bit written is the least significant bit of the first...
Definition BitWriter.h:27
Writer for a memory buffer. The writer advances the caller's pointer, so it can be combined with dire...
Definition ByteWriter.h:26
4XM ADPCM encoder. Header: le16 predictor per channel, then le16 step index per channel....
Definition ADPCMEncoder.h:3087
GameCube AFC ADPCM encoder. Each channel holds one 9 byte subframe per 16 samples: a scale/coefficien...
Definition ADPCMEncoder.h:3690
AGM encoder. Header per packet: le16 predictor and le16 step per channel. Each byte holds one time st...
Definition ADPCMEncoder.h:1698
Yamaha ADPCM with planar output as used by AICA (Dreamcast). Each channel is stored as a contiguous r...
Definition ADPCMEncoder.h:1422
Definition ADPCMEncoder.h:1122
int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, uint8_t *out, const int16_t *samples, int nsamples, int shift, int flag)
Definition ADPCMEncoder.h:1163
Creative (CT) 4-bit ADPCM encoder. Two nibbles per byte; mono packs two consecutive samples (high nib...
Definition ADPCMEncoder.h:3619
DTK encoder (stereo only). A 32 byte unit holds 28 samples per channel: byte 0 is the channel 0 heade...
Definition ADPCMEncoder.h:1902
EA MAXIS XA encoder (mono or stereo). Each packet starts with one header byte per channel: high nibbl...
Definition ADPCMEncoder.h:2993
EA R1/R2/R3 encoder. Packet: a 4 byte sample count, one 4 byte offset per channel (relative to the en...
Definition ADPCMEncoder.h:2742
EA XAS encoder. Each packet holds 128 samples per channel, one 76 byte chunk per channel: four groups...
Definition ADPCMEncoder.h:2875
EA (stereo only) encoder. Packet: le32 sample count, then four le16 state values (current and previou...
Definition ADPCMEncoder.h:2622
IMA ACORN encoder. Same 4 byte per channel header as ISS, but the stereo nibbles are swapped: the lef...
Definition ADPCMEncoder.h:1586
Definition ADPCMEncoder.h:756
Definition ADPCMEncoder.h:1060
IMA APC encoder. No per-packet header: the decoder keeps its state across packets (starting from zero...
Definition ADPCMEncoder.h:3215
Definition ADPCMEncoder.h:1021
IMA CUNNING encoder (planar). No header: channel 0 has FRAME_SIZE/2 bytes (low nibble first),...
Definition ADPCMEncoder.h:2175
IMA DAT4 encoder. Planar layout: each channel has a 4 byte unused header followed by FRAME_SIZE/2 byt...
Definition ADPCMEncoder.h:1649
IMA DK3 encoder (stereo only). The 16 byte block header holds 10 unused bytes, the sum and diff predi...
Definition ADPCMEncoder.h:3539
IMA ADPCM with a 4 byte per channel header (DK4). The header holds the first sample and the step inde...
Definition ADPCMEncoder.h:1361
IMA EACS encoder. Header: le32 sample count, then per channel an le32 step index followed by an le32 ...
Definition ADPCMEncoder.h:3144
EA SEAD IMA encoder: stateful 4-bit IMA with a shift-6 reconstruction (see DecoderADPCM_IMA_EA_SEAD)....
Definition ADPCMEncoder.h:1536
IMA ISS encoder. Each packet starts with a 4 byte header per channel (le16 predictor,...
Definition ADPCMEncoder.h:3474
IMA MOFLEX encoder. Header per channel (le16 step index, le16 predictor) resets the decoder for each ...
Definition ADPCMEncoder.h:3353
IMA MTF encoder. Each byte holds one nibble per channel for a pair of samples: the high nibble is the...
Definition ADPCMEncoder.h:2245
OKI / MSM6258 ADPCM. The predictor works on 12 bit values and the step table is the OKI table,...
Definition ADPCMEncoder.h:1462
QuickTime IMA ADPCM encoder (mono or stereo). Each channel is a 34 byte block: a big-endian 16-bit he...
Definition ADPCMEncoder.h:678
IMA RAD encoder. Header per channel (le16 step index, le16 predictor). Each byte holds the low nibble...
Definition ADPCMEncoder.h:3416
IMA SMJPEG encoder. Header per channel: be16 predictor, step index byte, one unused byte....
Definition ADPCMEncoder.h:3267
Definition ADPCMEncoder.h:725
Definition ADPCMEncoder.h:606
Definition ADPCMEncoder.h:1234
Definition ADPCMEncoder.h:809
MTAF encoder (even channel counts). Each channel pair starts with a 16 byte header: 4 unused bytes,...
Definition ADPCMEncoder.h:1812
PlayStation ADPCM (VAG style) encoder. Each packet holds one 16 byte block per channel,...
Definition ADPCMEncoder.h:1275
Sound Blaster Pro ADPCM encoder for SBPRO_2 (mono or stereo), SBPRO_3 (mono only) and SBPRO_4 (mono o...
Definition ADPCMEncoder.h:2320
int packetSize(const AVFrame *frame)
Definition ADPCMEncoder.h:2328
Definition ADPCMEncoder.h:913
GameCube THP ADPCM encoder (big and little endian variants). A packet holds an 8 byte header (unused ...
Definition ADPCMEncoder.h:3734
CD-XA ADPCM encoder (mono or stereo). A packet holds 224 samples per channel in ch 128 byte blocks....
Definition ADPCMEncoder.h:2502
Microsoft XMD encoder. Blocks of 32 samples per channel, 21 bytes each: le16 history[1],...
Definition ADPCMEncoder.h:2088
Definition ADPCMEncoder.h:979
LucasArts ZORK encoder. One byte per sample, interleaved, with the predictor and step index carried a...
Definition ADPCMEncoder.h:2012
int frame_size
Number of samples per channel in an audio frame.
Definition adpcm.h:218
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition adpcm.h:221
This structure provides the uncompressed PCM data.
Definition adpcm.h:130
int16_t ** extended_data
Definition adpcm.h:164
int nb_samples
Definition adpcm.h:148
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition adpcm.h:143
This structure stores compressed data. It is typically exported by demuxers and then passed as input ...
Definition adpcm.h:122
ADPCM Encoder.
Definition ADPCMEncoder.h:20