Go to the documentation of this file.
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 > 16
U) {
117 frontier = 1 << avctx->
trellis;
154 bytestream_put_le16(&extradata, avctx->
frame_size);
155 bytestream_put_le16(&extradata, 7);
156 for (
int i = 0;
i < 7;
i++) {
169 av_log(avctx, AV_LOG_ERROR,
"Sample rate must be 11025, "
171 return AVERROR(EINVAL);
183 av_log(avctx, AV_LOG_ERROR,
"Sample rate must be 22050\n");
184 return AVERROR(EINVAL);
248 const int sign = (
delta < 0) * 8;
255 nibble = sign | nibble;
257 c->prev_sample +=
diff;
268 int nibble = 8*(
delta < 0);
290 c->prev_sample -=
diff;
292 c->prev_sample +=
diff;
306 ((
c->sample2) * (
c->coeff2))) / 64;
310 bias =
c->idelta / 2;
312 bias = -
c->idelta / 2;
314 nibble = (nibble +
bias) /
c->idelta;
317 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) *
c->idelta;
319 c->sample2 =
c->sample1;
357 const int frontier = 1 << avctx->
trellis;
364 int pathn = 0, froze = -1,
i, j, k, generation = 0;
365 uint8_t *
hash =
s->trellis_hash;
366 memset(
hash, 0xff, 65536 *
sizeof(*
hash));
368 memset(nodep_buf, 0, 2 * frontier *
sizeof(*nodep_buf));
369 nodes[0] = node_buf + frontier;
372 nodes[0]->
step =
c->step_index;
381 nodes[0]->
step =
c->idelta;
384 nodes[0]->
step = 127;
387 nodes[0]->
step =
c->step;
392 for (
i = 0;
i < n;
i++) {
397 memset(nodes_next, 0, frontier *
sizeof(
TrellisNode*));
398 for (j = 0; j < frontier && nodes[j]; j++) {
401 const int range = (j < frontier / 2) ? 1 : 0;
402 const int step = nodes[j]->step;
405 const int predictor = ((nodes[j]->sample1 *
c->coeff1) +
406 (nodes[j]->sample2 *
c->coeff2)) / 64;
410 for (nidx = nmin; nidx <= nmax; nidx++) {
411 const int nibble = nidx & 0xf;
413 #define STORE_NODE(NAME, STEP_INDEX)\
419 dec_sample = av_clip_int16(dec_sample);\
420 d = sample - dec_sample;\
421 ssd = nodes[j]->ssd + d*(unsigned)d;\
426 if (ssd < nodes[j]->ssd)\
439 h = &hash[(uint16_t) dec_sample];\
440 if (*h == generation)\
442 if (heap_pos < frontier) {\
447 pos = (frontier >> 1) +\
448 (heap_pos & ((frontier >> 1) - 1));\
449 if (ssd > nodes_next[pos]->ssd)\
454 u = nodes_next[pos];\
456 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
458 nodes_next[pos] = u;\
462 u->step = STEP_INDEX;\
463 u->sample2 = nodes[j]->sample1;\
464 u->sample1 = dec_sample;\
465 paths[u->path].nibble = nibble;\
466 paths[u->path].prev = nodes[j]->path;\
470 int parent = (pos - 1) >> 1;\
471 if (nodes_next[parent]->ssd <= ssd)\
473 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
484 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
485 const int predictor = nodes[j]->sample1;\
486 const int div = (sample - predictor) * 4 / STEP_TABLE;\
487 int nmin = av_clip(div - range, -7, 6);\
488 int nmax = av_clip(div + range, -6, 7);\
493 for (nidx = nmin; nidx <= nmax; nidx++) {\
494 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
495 int dec_sample = predictor +\
497 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
498 STORE_NODE(NAME, STEP_INDEX);\
516 if (generation == 255) {
517 memset(
hash, 0xff, 65536 *
sizeof(*
hash));
522 if (nodes[0]->ssd > (1 << 28)) {
523 for (j = 1; j < frontier && nodes[j]; j++)
524 nodes[j]->ssd -= nodes[0]->ssd;
530 p = &paths[nodes[0]->path];
531 for (k =
i; k > froze; k--) {
540 memset(nodes + 1, 0, (frontier - 1) *
sizeof(
TrellisNode*));
544 p = &paths[nodes[0]->
path];
545 for (
i = n - 1;
i > froze;
i--) {
550 c->predictor = nodes[0]->sample1;
551 c->sample1 = nodes[0]->sample1;
552 c->sample2 = nodes[0]->sample2;
553 c->step_index = nodes[0]->step;
554 c->step = nodes[0]->step;
555 c->idelta = nodes[0]->step;
558 #if CONFIG_ADPCM_ARGO_ENCODER
569 return (nibble >>
shift) & 0x0F;
573 const int16_t *
samples,
int nsamples,
585 for (
int n = 0; n < nsamples; n++) {
603 int st, pkt_size,
ret;
605 const int16_t *
const *samples_p;
611 samples_p = (
const int16_t *
const *)
frame->extended_data;
627 int blocks = (
frame->nb_samples - 1) / 8;
631 status->prev_sample = samples_p[ch][0];
634 bytestream_put_le16(&
dst,
status->prev_sample);
644 for (
int ch = 0; ch <
channels; ch++) {
646 buf + ch * blocks * 8, &
c->status[ch],
649 for (
int i = 0;
i < blocks;
i++) {
650 for (
int ch = 0; ch <
channels; ch++) {
651 uint8_t *buf1 = buf + ch * blocks * 8 +
i * 8;
652 for (
int j = 0; j < 8; j += 2)
653 *
dst++ = buf1[j] | (buf1[j + 1] << 4);
658 for (
int i = 0;
i < blocks;
i++) {
659 for (
int ch = 0; ch <
channels; ch++) {
661 const int16_t *smp = &samples_p[ch][1 +
i * 8];
662 for (
int j = 0; j < 8; j += 2) {
675 for (
int ch = 0; ch <
channels; ch++) {
683 for (
int i = 0;
i < 64;
i++)
687 for (
int i = 0;
i < 64;
i += 2) {
705 for (
int i = 0;
i <
frame->nb_samples;
i++) {
706 for (
int ch = 0; ch <
channels; ch++) {
719 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
720 for (
int ch = 0; ch <
channels; ch++) {
730 const int n =
frame->nb_samples - 1;
755 buf + n, &
c->status[1], n,
757 for (
int i = 0;
i < n;
i++) {
763 for (
int i = 1;
i <
frame->nb_samples;
i++) {
781 if (
c->status[
i].idelta < 16)
782 c->status[
i].idelta = 16;
783 bytestream_put_le16(&
dst,
c->status[
i].idelta);
789 bytestream_put_le16(&
dst,
c->status[
i].sample1);
792 bytestream_put_le16(&
dst,
c->status[
i].sample2);
802 for (
int i = 0;
i < n;
i += 2)
803 *
dst++ = (buf[
i] << 4) | buf[
i + 1];
809 for (
int i = 0;
i < n;
i++)
810 *
dst++ = (buf[
i] << 4) | buf[n +
i];
823 int n =
frame->nb_samples / 2;
832 for (
int i = 0;
i < n;
i += 2)
833 *
dst++ = buf[
i] | (buf[
i + 1] << 4);
839 for (
int i = 0;
i < n;
i++)
840 *
dst++ = buf[
i] | (buf[n +
i] << 4);
857 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
858 for (
int ch = 0; ch <
channels; ch++) {
870 c->status[0].prev_sample = *
samples;
871 bytestream_put_le16(&
dst,
c->status[0].prev_sample);
872 bytestream_put_byte(&
dst,
c->status[0].step_index);
873 bytestream_put_byte(&
dst, 0);
877 const int n =
frame->nb_samples >> 1;
884 for (
int i = 0;
i < n;
i++)
885 bytestream_put_byte(&
dst, (buf[2 *
i] << 4) | buf[2 *
i + 1]);
889 }
else for (
int n =
frame->nb_samples >> 1; n > 0; n--) {
893 bytestream_put_byte(&
dst, nibble);
898 bytestream_put_byte(&
dst, nibble);
907 for (
int ch = 0; ch <
channels; ch++) {
910 int saved1 =
c->status[ch].sample1;
911 int saved2 =
c->status[ch].sample2;
914 for (
int s = 2;
s < 18 && tmperr != 0;
s++) {
915 for (
int f = 0;
f < 2 && tmperr != 0;
f++) {
916 c->status[ch].sample1 = saved1;
917 c->status[ch].sample2 = saved2;
918 tmperr = adpcm_argo_compress_block(
c->status + ch,
NULL, samples_p[ch],
920 if (tmperr <
error) {
929 c->status[ch].sample1 = saved1;
930 c->status[ch].sample2 = saved2;
931 adpcm_argo_compress_block(
c->status + ch, &pb, samples_p[ch],
942 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
944 for (
int ch = 0; ch <
channels; ch++) {
979 .
name =
"block_size",
980 .help =
"set the block size",
983 .default_val = {.i64 = 1024},
998 #define ADPCM_ENCODER_0(id_, name_, sample_fmts_, capabilities_, long_name_)
999 #define ADPCM_ENCODER_1(id_, name_, sample_fmts_, capabilities_, long_name_) \
1000 const FFCodec ff_ ## name_ ## _encoder = { \
1002 CODEC_LONG_NAME(long_name_), \
1003 .p.type = AVMEDIA_TYPE_AUDIO, \
1005 .p.sample_fmts = sample_fmts_, \
1006 .p.ch_layouts = ch_layouts, \
1007 .p.capabilities = capabilities_ | AV_CODEC_CAP_DR1 | \
1008 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, \
1009 .p.priv_class = &adpcm_encoder_class, \
1010 .priv_data_size = sizeof(ADPCMEncodeContext), \
1011 .init = adpcm_encode_init, \
1012 FF_CODEC_ENCODE_CB(adpcm_encode_frame), \
1013 .close = adpcm_encode_close, \
1014 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
1016 #define ADPCM_ENCODER_2(enabled, codec_id, name, sample_fmts, capabilities, long_name) \
1017 ADPCM_ENCODER_ ## enabled(codec_id, name, sample_fmts, capabilities, long_name)
1018 #define ADPCM_ENCODER_3(config, codec_id, name, sample_fmts, capabilities, long_name) \
1019 ADPCM_ENCODER_2(config, codec_id, name, sample_fmts, capabilities, long_name)
1020 #define ADPCM_ENCODER(codec, name, sample_fmts, capabilities, long_name) \
1021 ADPCM_ENCODER_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, \
1022 name, sample_fmts, capabilities, long_name)
static void error(const char *err)
int frame_size
Number of samples per channel in an audio frame.
static uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, int16_t sample)
@ AV_CODEC_ID_ADPCM_IMA_QT
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)
#define AV_CHANNEL_LAYOUT_STEREO
int sample_rate
samples per second
#define u(width, name, range_min, range_max)
static enum AVSampleFormat sample_fmts[]
#define ADPCM_ENCODER(codec, name, sample_fmts, capabilities, long_name)
const int16_t ff_adpcm_AdaptationTable[]
static const AVClass adpcm_encoder_class
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.
This structure describes decoded (raw) audio or video data.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
int nb_channels
Number of channels in this layout.
const struct AVCodec * codec
#define STORE_NODE(NAME, STEP_INDEX)
AVChannelLayout ch_layout
Audio channel layout.
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
ADPCMChannelStatus status[6]
#define AV_OPT_FLAG_AUDIO_PARAM
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static const AVOption options[]
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static void adpcm_compress_trellis(AVCodecContext *avctx, const int16_t *samples, uint8_t *dst, ADPCMChannelStatus *c, int n, int stride)
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
@ AV_CODEC_ID_ADPCM_YAMAHA
@ AV_CODEC_ID_ADPCM_IMA_WS
static int bias(int x, int c)
const char * av_default_item_name(void *ptr)
Return the context name.
@ AV_CODEC_ID_ADPCM_IMA_AMV
int trellis
trellis RD quantization
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)
const int8_t ff_adpcm_yamaha_difflookup[]
An AVChannelLayout holds information about the channel layout of audio data.
static int shift(int a, int b)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
@ AV_CODEC_ID_ADPCM_IMA_ALP
const int16_t ff_adpcm_step_table[89]
This is the step table.
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static void predictor(uint8_t *src, ptrdiff_t size)
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
#define i(width, name, range_min, range_max)
const int8_t ff_adpcm_AdaptCoeff2[]
Divided by 4 to fit in 8-bit integers.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVSampleFormat
Audio sample formats.
static uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c, int16_t sample)
static const AVChannelLayout ch_layouts[]
@ AV_CODEC_ID_ADPCM_IMA_APM
@ AV_SAMPLE_FMT_S16
signed 16 bits
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
const int8_t ff_adpcm_index_table[16]
static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
static enum AVSampleFormat sample_fmts_p[]
#define AV_INPUT_BUFFER_PADDING_SIZE
main external API structure.
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
@ AV_OPT_TYPE_INT
Underlying C type is int.
const int16_t ff_adpcm_yamaha_indexscale[]
Filter the word “frame” indicates either a video frame or a group of audio samples
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
@ AV_CODEC_ID_ADPCM_IMA_SSI
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
#define AV_CHANNEL_LAYOUT_MONO
This structure stores compressed data.
@ AV_CODEC_ID_ADPCM_IMA_WAV
static uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, int16_t sample)
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
static uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c, int16_t sample)
static av_cold int adpcm_encode_close(AVCodecContext *avctx)