Go to the documentation of this file.
59 #define FREEZE_INTERVAL 128
90 int frontier = 1 << avctx->
trellis;
93 max_paths *
sizeof(*
s->paths),
error);
95 2 * frontier *
sizeof(*
s->node_buf),
error);
97 2 * frontier *
sizeof(*
s->nodep_buf),
error);
99 65536 *
sizeof(*
s->trellis_hash),
error);
129 bytestream_put_le16(&extradata, avctx->
frame_size);
130 bytestream_put_le16(&extradata, 7);
131 for (
i = 0;
i < 7;
i++) {
185 c->prev_sample = av_clip_int16(
c->prev_sample);
195 int nibble = 8*(
delta < 0);
217 c->prev_sample -=
diff;
219 c->prev_sample +=
diff;
221 c->prev_sample = av_clip_int16(
c->prev_sample);
230 int predictor, nibble, bias;
232 predictor = (((
c->sample1) * (
c->coeff1)) +
233 ((
c->sample2) * (
c->coeff2))) / 64;
235 nibble =
sample - predictor;
237 bias =
c->idelta / 2;
239 bias = -
c->idelta / 2;
241 nibble = (nibble + bias) /
c->idelta;
242 nibble = av_clip_intp2(nibble, 3) & 0x0F;
244 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) *
c->idelta;
246 c->sample2 =
c->sample1;
247 c->sample1 = av_clip_int16(predictor);
271 c->predictor = av_clip_int16(
c->predictor);
273 c->step = av_clip(
c->step, 127, 24576);
284 const int frontier = 1 << avctx->
trellis;
291 int pathn = 0, froze = -1,
i, j, k, generation = 0;
293 memset(
hash, 0xff, 65536 *
sizeof(*
hash));
295 memset(nodep_buf, 0, 2 * frontier *
sizeof(*nodep_buf));
296 nodes[0] = node_buf + frontier;
299 nodes[0]->
step =
c->step_index;
307 nodes[0]->
step =
c->idelta;
310 nodes[0]->
step = 127;
313 nodes[0]->
step =
c->step;
318 for (
i = 0;
i < n;
i++) {
323 memset(nodes_next, 0, frontier *
sizeof(
TrellisNode*));
324 for (j = 0; j < frontier && nodes[j]; j++) {
327 const int range = (j < frontier / 2) ? 1 : 0;
328 const int step = nodes[j]->step;
331 const int predictor = ((nodes[j]->sample1 *
c->coeff1) +
332 (nodes[j]->sample2 *
c->coeff2)) / 64;
334 const int nmin = av_clip(div-range, -8, 6);
335 const int nmax = av_clip(div+range, -7, 7);
336 for (nidx = nmin; nidx <= nmax; nidx++) {
337 const int nibble = nidx & 0xf;
338 int dec_sample = predictor + nidx *
step;
339 #define STORE_NODE(NAME, STEP_INDEX)\
345 dec_sample = av_clip_int16(dec_sample);\
346 d = sample - dec_sample;\
347 ssd = nodes[j]->ssd + d*(unsigned)d;\
352 if (ssd < nodes[j]->ssd)\
365 h = &hash[(uint16_t) dec_sample];\
366 if (*h == generation)\
368 if (heap_pos < frontier) {\
373 pos = (frontier >> 1) +\
374 (heap_pos & ((frontier >> 1) - 1));\
375 if (ssd > nodes_next[pos]->ssd)\
380 u = nodes_next[pos];\
382 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
384 nodes_next[pos] = u;\
388 u->step = STEP_INDEX;\
389 u->sample2 = nodes[j]->sample1;\
390 u->sample1 = dec_sample;\
391 paths[u->path].nibble = nibble;\
392 paths[u->path].prev = nodes[j]->path;\
396 int parent = (pos - 1) >> 1;\
397 if (nodes_next[parent]->ssd <= ssd)\
399 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
409 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
410 const int predictor = nodes[j]->sample1;\
411 const int div = (sample - predictor) * 4 / STEP_TABLE;\
412 int nmin = av_clip(div - range, -7, 6);\
413 int nmax = av_clip(div + range, -6, 7);\
418 for (nidx = nmin; nidx <= nmax; nidx++) {\
419 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
420 int dec_sample = predictor +\
422 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
423 STORE_NODE(NAME, STEP_INDEX);\
441 if (generation == 255) {
442 memset(
hash, 0xff, 65536 *
sizeof(*
hash));
447 if (nodes[0]->ssd > (1 << 28)) {
448 for (j = 1; j < frontier && nodes[j]; j++)
449 nodes[j]->ssd -= nodes[0]->ssd;
455 p = &paths[nodes[0]->path];
456 for (k =
i; k > froze; k--) {
465 memset(nodes + 1, 0, (frontier - 1) *
sizeof(
TrellisNode*));
469 p = &paths[nodes[0]->
path];
470 for (
i = n - 1;
i > froze;
i--) {
475 c->predictor = nodes[0]->sample1;
476 c->sample1 = nodes[0]->sample1;
477 c->sample2 = nodes[0]->sample2;
478 c->step_index = nodes[0]->step;
479 c->step = nodes[0]->step;
480 c->idelta = nodes[0]->step;
486 int n,
i, ch, st, pkt_size,
ret;
494 samples_p = (int16_t **)
frame->extended_data;
498 pkt_size = (2 + avctx->
channels * (22 + 4 * (
frame->nb_samples - 1)) + 7) / 8;
512 blocks = (
frame->nb_samples - 1) / 8;
514 for (ch = 0; ch < avctx->
channels; ch++) {
516 status->prev_sample = samples_p[ch][0];
519 bytestream_put_le16(&dst,
status->prev_sample);
520 *dst++ =
status->step_index;
527 for (ch = 0; ch < avctx->
channels; ch++) {
529 buf + ch * blocks * 8, &
c->status[ch],
532 for (
i = 0;
i < blocks;
i++) {
533 for (ch = 0; ch < avctx->
channels; ch++) {
534 uint8_t *buf1 = buf + ch * blocks * 8 +
i * 8;
535 for (j = 0; j < 8; j += 2)
536 *dst++ = buf1[j] | (buf1[j + 1] << 4);
541 for (
i = 0;
i < blocks;
i++) {
542 for (ch = 0; ch < avctx->
channels; ch++) {
544 const int16_t *smp = &samples_p[ch][1 +
i * 8];
545 for (j = 0; j < 8; j += 2) {
560 for (ch = 0; ch < avctx->
channels; ch++) {
568 for (
i = 0;
i < 64;
i++)
572 for (
i = 0;
i < 64;
i += 2) {
592 for (
i = 0;
i <
frame->nb_samples;
i++) {
593 for (ch = 0; ch < avctx->
channels; ch++) {
606 n =
frame->nb_samples - 1;
614 c->status[
i].step_index = av_clip_uintp2(
c->status[
i].step_index, 6);
626 buf + n, &
c->status[1], n,
628 for (
i = 0;
i < n;
i++) {
635 for (
i = 1;
i <
frame->nb_samples;
i++) {
654 if (
c->status[
i].idelta < 16)
655 c->status[
i].idelta = 16;
656 bytestream_put_le16(&dst,
c->status[
i].idelta);
662 bytestream_put_le16(&dst,
c->status[
i].sample1);
665 bytestream_put_le16(&dst,
c->status[
i].sample2);
673 for (
i = 0;
i < n;
i += 2)
674 *dst++ = (buf[
i] << 4) | buf[
i + 1];
680 for (
i = 0;
i < n;
i++)
681 *dst++ = (buf[
i] << 4) | buf[n +
i];
685 for (
i = 7 * avctx->
channels; i < avctx->block_align;
i++) {
694 n =
frame->nb_samples / 2;
701 for (
i = 0;
i < n;
i += 2)
702 *dst++ = buf[
i] | (buf[
i + 1] << 4);
708 for (
i = 0;
i < n;
i++)
709 *dst++ = buf[
i] | (buf[n +
i] << 4);
713 for (n *= avctx->
channels; n > 0; n--) {
739 #define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_) \
740 AVCodec ff_ ## name_ ## _encoder = { \
742 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
743 .type = AVMEDIA_TYPE_AUDIO, \
745 .priv_data_size = sizeof(ADPCMEncodeContext), \
746 .init = adpcm_encode_init, \
747 .encode2 = adpcm_encode_frame, \
748 .close = adpcm_encode_close, \
749 .sample_fmts = sample_fmts_, \
750 .capabilities = capabilities_, \
751 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
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
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
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)
int sample_rate
samples per second
#define u(width, name, range_min, range_max)
static enum AVSampleFormat sample_fmts[]
const int16_t ff_adpcm_AdaptationTable[]
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
#define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_)
const struct AVCodec * codec
#define STORE_NODE(NAME, STEP_INDEX)
ADPCMChannelStatus status[6]
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#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 AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
@ AV_CODEC_ID_ADPCM_YAMAHA
int trellis
trellis RD quantization
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
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
const int8_t ff_adpcm_yamaha_difflookup[]
const int16_t ff_adpcm_step_table[89]
This is the step table.
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
int channels
number of audio channels
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)
@ AV_SAMPLE_FMT_S16
signed 16 bits
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.
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.
const int16_t ff_adpcm_yamaha_indexscale[]
#define FF_ALLOC_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)
Filter the word “frame” indicates either a video frame or a group of audio samples
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
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)
static av_always_inline int diff(const uint32_t a, const uint32_t b)
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.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
static uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c, int16_t sample)
static av_cold int adpcm_encode_close(AVCodecContext *avctx)