25#include "config_components.h"
44#define CASE_0(codec_id, ...)
45#define CASE_1(codec_id, ...) \
49#define CASE_2(enabled, codec_id, ...) \
50 CASE_ ## enabled(codec_id, __VA_ARGS__)
51#define CASE_3(config, codec_id, ...) \
52 CASE_2(config, codec_id, __VA_ARGS__)
53#define CASE(codec, ...) \
54 CASE_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, __VA_ARGS__)
80#define FREEZE_INTERVAL 128
92 (
s->block_size & (
s->block_size - 1))) {
98 int frontier, max_paths;
100 if ((
unsigned)avctx->
trellis > 16U) {
117 frontier = 1 << avctx->
trellis;
156 bytestream_put_le16(&extradata, avctx->
frame_size);
157 bytestream_put_le16(&extradata, 7);
158 for (
int i = 0;
i < 7;
i++) {
198 av_unreachable(
"there is a case for every codec using adpcm_encode_init()");
233 const int sign = (
delta < 0) * 8;
236 int diff = (step * nibble) >> 2;
240 nibble = sign | nibble;
242 c->prev_sample +=
diff;
253 int nibble = 8*(
delta < 0);
275 c->prev_sample -=
diff;
277 c->prev_sample +=
diff;
291 ((
c->sample2) * (
c->coeff2))) / 64;
295 bias =
c->idelta / 2;
297 bias = -
c->idelta / 2;
299 nibble = (nibble +
bias) /
c->idelta;
302 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) *
c->idelta;
304 c->sample2 =
c->sample1;
337 const int16_t *samples, uint8_t *
dst,
342 const int frontier = 1 << avctx->
trellis;
349 int pathn = 0, froze = -1,
i, j, k, generation = 0;
350 uint8_t *
hash =
s->trellis_hash;
351 memset(
hash, 0xff, 65536 *
sizeof(*
hash));
353 memset(nodep_buf, 0, 2 * frontier *
sizeof(*nodep_buf));
354 nodes[0] = node_buf + frontier;
357 nodes[0]->
step =
c->step_index;
366 nodes[0]->
step =
c->idelta;
369 nodes[0]->
step = 127;
372 nodes[0]->
step =
c->step;
377 for (
i = 0;
i < n;
i++) {
382 memset(nodes_next, 0, frontier *
sizeof(
TrellisNode*));
383 for (j = 0; j < frontier && nodes[j]; j++) {
386 const int range = (j < frontier / 2) ? 1 : 0;
387 const int step = nodes[j]->step;
390 const int predictor = ((nodes[j]->sample1 *
c->coeff1) +
391 (nodes[j]->sample2 *
c->coeff2)) / 64;
395 for (nidx = nmin; nidx <= nmax; nidx++) {
396 const int nibble = nidx & 0xf;
397 int dec_sample =
predictor + nidx * step;
398#define STORE_NODE(NAME, STEP_INDEX)\
404 dec_sample = av_clip_int16(dec_sample);\
405 d = sample - dec_sample;\
406 ssd = nodes[j]->ssd + d*(unsigned)d;\
411 if (ssd < nodes[j]->ssd)\
424 h = &hash[(uint16_t) dec_sample];\
425 if (*h == generation)\
427 if (heap_pos < frontier) {\
432 pos = (frontier >> 1) +\
433 (heap_pos & ((frontier >> 1) - 1));\
434 if (ssd > nodes_next[pos]->ssd)\
439 u = nodes_next[pos];\
441 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
443 nodes_next[pos] = u;\
447 u->step = STEP_INDEX;\
448 u->sample2 = nodes[j]->sample1;\
449 u->sample1 = dec_sample;\
450 paths[u->path].nibble = nibble;\
451 paths[u->path].prev = nodes[j]->path;\
455 int parent = (pos - 1) >> 1;\
456 if (nodes_next[parent]->ssd <= ssd)\
458 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
469#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
470 const int predictor = nodes[j]->sample1;\
471 const int div = (sample - predictor) * 4 / STEP_TABLE;\
472 int nmin = av_clip(div - range, -7, 6);\
473 int nmax = av_clip(div + range, -6, 7);\
478 for (nidx = nmin; nidx <= nmax; nidx++) {\
479 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
480 int dec_sample = predictor +\
482 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
483 STORE_NODE(NAME, STEP_INDEX);\
501 if (generation == 255) {
502 memset(
hash, 0xff, 65536 *
sizeof(*
hash));
507 if (nodes[0]->ssd > (1 << 28)) {
508 for (j = 1; j < frontier && nodes[j]; j++)
509 nodes[j]->ssd -= nodes[0]->ssd;
515 p = &paths[nodes[0]->path];
516 for (k =
i; k > froze; k--) {
525 memset(nodes + 1, 0, (frontier - 1) *
sizeof(
TrellisNode*));
529 p = &paths[nodes[0]->
path];
530 for (
i = n - 1;
i > froze;
i--) {
538 c->step_index = nodes[0]->
step;
539 c->step = nodes[0]->
step;
540 c->idelta = nodes[0]->
step;
543#if CONFIG_ADPCM_ARGO_ENCODER
554 return (nibble >>
shift) & 0x0F;
558 const int16_t *samples,
int nsamples,
570 for (
int n = 0; n < nsamples; n++) {
572 int nibble = adpcm_argo_compress_nibble(cs, samples[n],
shift,
flag);
588 int st, pkt_size, ret;
589 const int16_t *samples;
590 const int16_t *
const *samples_p;
595 samples = (
const int16_t *)
frame->data[0];
596 samples_p = (
const int16_t *
const *)
frame->extended_data;
605 pkt_size = avctx->block_align;
610 switch(avctx->codec->id) {
612 int blocks = (
frame->nb_samples - 1) / 8;
625 if (avctx->trellis > 0) {
627 if (!FF_ALLOC_TYPED_ARRAY(buf, channels * blocks * 8))
628 return AVERROR(ENOMEM);
629 for (int ch = 0; ch < channels; ch++) {
630 adpcm_compress_trellis(avctx, &samples_p[ch][1],
631 buf + ch * blocks * 8, &c->status[ch],
634 for (
int i = 0;
i < blocks;
i++) {
635 for (
int ch = 0; ch <
channels; ch++) {
636 uint8_t *buf1 = buf + ch * blocks * 8 +
i * 8;
637 for (
int j = 0; j < 8; j += 2)
638 *
dst++ = buf1[j] | (buf1[j + 1] << 4);
643 for (
int i = 0;
i < blocks;
i++) {
644 for (
int ch = 0; ch <
channels; ch++) {
646 const int16_t *smp = &samples_p[ch][1 +
i * 8];
647 for (
int j = 0; j < 8; j += 2) {
660 for (
int ch = 0; ch <
channels; ch++) {
664 if (avctx->trellis > 0) {
668 for (
int i = 0;
i < 64;
i++)
672 for (
int i = 0;
i < 64;
i += 2) {
690 for (
int i = 0;
i <
frame->nb_samples;
i++) {
691 for (
int ch = 0; ch <
channels; ch++) {
704 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
705 for (
int ch = 0; ch <
channels; ch++) {
715 const int n =
frame->nb_samples - 1;
731 c->status[
i].prev_sample = samples[
i];
734 if (avctx->trellis > 0) {
740 buf + n, &
c->status[1], n,
742 for (
int i = 0;
i < n;
i++) {
748 for (
int i = 1;
i <
frame->nb_samples;
i++) {
753 samples[2 *
i + 1]));
766 if (
c->status[
i].idelta < 16)
767 c->status[
i].idelta = 16;
768 bytestream_put_le16(&
dst,
c->status[
i].idelta);
771 c->status[
i].sample2= *samples++;
773 c->status[
i].sample1 = *samples++;
774 bytestream_put_le16(&
dst,
c->status[
i].sample1);
777 bytestream_put_le16(&
dst,
c->status[
i].sample2);
779 if (avctx->trellis > 0) {
780 const int n = avctx->block_align - 7 *
channels;
787 for (
int i = 0;
i < n;
i += 2)
788 *
dst++ = (buf[
i] << 4) | buf[
i + 1];
794 for (
int i = 0;
i < n;
i++)
795 *
dst++ = (buf[
i] << 4) | buf[n +
i];
799 for (
int i = 7 *
channels;
i < avctx->block_align;
i++) {
808 int n =
frame->nb_samples / 2;
809 if (avctx->trellis > 0) {
817 for (
int i = 0;
i < n;
i += 2)
818 *
dst++ = buf[
i] | (buf[
i + 1] << 4);
824 for (
int i = 0;
i < n;
i++)
825 *
dst++ = buf[
i] | (buf[n +
i] << 4);
842 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
843 for (
int ch = 0; ch <
channels; ch++) {
855 c->status[0].prev_sample = *samples;
856 bytestream_put_le16(&
dst,
c->status[0].prev_sample);
857 bytestream_put_byte(&
dst,
c->status[0].step_index);
858 bytestream_put_byte(&
dst, 0);
859 bytestream_put_le32(&
dst, avctx->frame_size);
861 if (avctx->trellis > 0) {
862 const int n =
frame->nb_samples >> 1;
869 for (
int i = 0;
i < n;
i++)
870 bytestream_put_byte(&
dst, (buf[2 *
i] << 4) | buf[2 *
i + 1]);
874 }
else for (
int n =
frame->nb_samples >> 1; n > 0; n--) {
878 bytestream_put_byte(&
dst, nibble);
881 if (avctx->frame_size & 1) {
883 bytestream_put_byte(&
dst, nibble);
892 for (
int ch = 0; ch <
channels; ch++) {
895 int saved1 =
c->status[ch].sample1;
896 int saved2 =
c->status[ch].sample2;
899 for (
int s = 2;
s < 18 && tmperr != 0;
s++) {
900 for (
int f = 0;
f < 2 && tmperr != 0;
f++) {
901 c->status[ch].sample1 = saved1;
902 c->status[ch].sample2 = saved2;
903 tmperr = adpcm_argo_compress_block(
c->status + ch,
NULL, samples_p[ch],
905 if (tmperr <
error) {
914 c->status[ch].sample1 = saved1;
915 c->status[ch].sample2 = saved2;
916 adpcm_argo_compress_block(
c->status + ch, &pb, samples_p[ch],
927 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
929 for (
int ch = 0; ch <
channels; ch++) {
964 .name =
"block_size",
965 .help =
"set the block size",
968 .default_val = {.i64 = 1024},
977 .class_name =
"ADPCM encoder",
983#define ADPCM_ENCODER_0(id_, name_, sample_fmts_, capabilities_, long_name_, ...)
984#define ADPCM_ENCODER_1(id_, name_, sample_fmts_, capabilities_, long_name_, ...) \
985const FFCodec ff_ ## name_ ## _encoder = { \
987 CODEC_LONG_NAME(long_name_), \
988 .p.type = AVMEDIA_TYPE_AUDIO, \
990 .p.capabilities = capabilities_ | AV_CODEC_CAP_DR1 | \
991 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, \
992 CODEC_SAMPLEFMTS_ARRAY(sample_fmts_), \
993 .priv_data_size = sizeof(ADPCMEncodeContext), \
994 .init = adpcm_encode_init, \
995 FF_CODEC_ENCODE_CB(adpcm_encode_frame), \
996 .close = adpcm_encode_close, \
997 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
1000#define ADPCM_ENCODER_2(enabled, codec_id, name, sample_fmts, capabilities, long_name, ...) \
1001 ADPCM_ENCODER_ ## enabled(codec_id, name, sample_fmts, capabilities, long_name, __VA_ARGS__)
1002#define ADPCM_ENCODER_3(config, codec_id, name, sample_fmts, capabilities, long_name, ...) \
1003 ADPCM_ENCODER_2(config, codec_id, name, sample_fmts, capabilities, long_name, __VA_ARGS__)
1004#define ADPCM_ENCODER(codec, name, sample_fmts, capabilities, long_name, ...) \
1005 ADPCM_ENCODER_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, \
1006 name, sample_fmts, capabilities, long_name, __VA_ARGS__)
1008#define MONO_STEREO CODEC_CH_LAYOUTS_ARRAY(ch_layouts_mono_stereo)
1009#define AVCLASS .p.priv_class = &adpcm_encoder_class
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
ADPCM encoder/decoder common header.
const int8_t ff_adpcm_AdaptCoeff2[]
Divided by 4 to fit in 8-bit integers.
const int8_t ff_adpcm_index_table[16]
const int8_t ff_adpcm_yamaha_difflookup[]
const int16_t ff_adpcm_step_table[89]
This is the step table.
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
const int16_t ff_adpcm_yamaha_indexscale[]
const int16_t ff_adpcm_AdaptationTable[]
static enum AVSampleFormat sample_fmts[]
static const AVChannelLayout ch_layouts_mono_stereo[]
#define STORE_NODE(NAME, STEP_INDEX)
static uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)
static uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c, int16_t sample)
static uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, int16_t sample)
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
static const AVClass adpcm_encoder_class
static void adpcm_compress_trellis(AVCodecContext *avctx, const int16_t *samples, uint8_t *dst, ADPCMChannelStatus *c, int n, int stride)
#define ADPCM_ENCODER(codec, name, sample_fmts, capabilities, long_name,...)
static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
static uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c, int16_t sample)
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)
static enum AVSampleFormat sample_fmts_p[]
static uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, int16_t sample)
static uint8_t hash[HASH_SIZE]
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define i(width, name, range_min, range_max)
#define CODEC_CH_LAYOUTS(...)
#define CODEC_SAMPLERATES(...)
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
static void predictor(uint8_t *src, ptrdiff_t size)
#define AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
@ AV_OPT_TYPE_INT
Underlying C type is int.
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
@ AV_CODEC_ID_ADPCM_IMA_WS
@ AV_CODEC_ID_ADPCM_YAMAHA
@ AV_CODEC_ID_ADPCM_IMA_AMV
@ AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_ADPCM_IMA_APM
@ AV_CODEC_ID_ADPCM_IMA_WAV
@ AV_CODEC_ID_ADPCM_IMA_ALP
@ AV_CODEC_ID_ADPCM_IMA_SSI
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
@ AV_SAMPLE_FMT_S16
signed 16 bits
#define LIBAVUTIL_VERSION_INT
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static int shift(int a, int b)
#define u(width, name, range_min, range_max)
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
Memory handling functions.
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
ADPCMChannelStatus status[6]
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
main external API structure.
int trellis
trellis RD quantization
AVChannelLayout ch_layout
Audio channel layout.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
int64_t bit_rate
the average bitrate
const struct AVCodec * codec
int sample_rate
samples per second
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
int frame_size
Number of samples per channel in an audio frame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
static void error(const char *err)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static int bias(int x, int c)