FFmpeg
adpcmenc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2003 The FFmpeg project
3  *
4  * first version by Francois Revol (revol@free.fr)
5  * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6  * by Mike Melanson (melanson@pcisys.net)
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include "config_components.h"
26 
27 #include "libavutil/mem.h"
28 #include "libavutil/opt.h"
29 
30 #include "avcodec.h"
31 #include "put_bits.h"
32 #include "bytestream.h"
33 #include "adpcm.h"
34 #include "adpcm_data.h"
35 #include "codec_internal.h"
36 #include "encode.h"
37 
38 /**
39  * @file
40  * ADPCM encoders
41  * See ADPCM decoder reference documents for codec information.
42  */
43 
44 #define CASE_0(codec_id, ...)
45 #define CASE_1(codec_id, ...) \
46  case codec_id: \
47  { __VA_ARGS__ } \
48  break;
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__)
55 
56 typedef struct TrellisPath {
57  int nibble;
58  int prev;
59 } TrellisPath;
60 
61 typedef struct TrellisNode {
62  uint32_t ssd;
63  int path;
64  int sample1;
65  int sample2;
66  int step;
67 } TrellisNode;
68 
69 typedef struct ADPCMEncodeContext {
70  AVClass *class;
72 
77  uint8_t *trellis_hash;
79 
80 #define FREEZE_INTERVAL 128
81 
83 {
84  ADPCMEncodeContext *s = avctx->priv_data;
85  int channels = avctx->ch_layout.nb_channels;
86 
87  /*
88  * AMV's block size has to match that of the corresponding video
89  * stream. Relax the POT requirement.
90  */
91  if (avctx->codec->id != AV_CODEC_ID_ADPCM_IMA_AMV &&
92  (s->block_size & (s->block_size - 1))) {
93  av_log(avctx, AV_LOG_ERROR, "block size must be power of 2\n");
94  return AVERROR(EINVAL);
95  }
96 
97  if (avctx->trellis) {
98  int frontier, max_paths;
99 
100  if ((unsigned)avctx->trellis > 16U) {
101  av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
102  return AVERROR(EINVAL);
103  }
104 
105  if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
106  avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
107  avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO ||
108  avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_WS) {
109  /*
110  * The current trellis implementation doesn't work for extended
111  * runs of samples without periodic resets. Disallow it.
112  */
113  av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
114  return AVERROR_PATCHWELCOME;
115  }
116 
117  frontier = 1 << avctx->trellis;
118  max_paths = frontier * FREEZE_INTERVAL;
119  if (!FF_ALLOC_TYPED_ARRAY(s->paths, max_paths) ||
120  !FF_ALLOC_TYPED_ARRAY(s->node_buf, 2 * frontier) ||
121  !FF_ALLOC_TYPED_ARRAY(s->nodep_buf, 2 * frontier) ||
122  !FF_ALLOC_TYPED_ARRAY(s->trellis_hash, 65536))
123  return AVERROR(ENOMEM);
124  }
125 
127 
128  switch (avctx->codec->id) {
129  CASE(ADPCM_IMA_WAV,
130  /* each 16 bits sample gives one nibble
131  and we have 4 bytes per channel overhead */
132  avctx->frame_size = (s->block_size - 4 * channels) * 8 /
133  (4 * channels) + 1;
134  /* seems frame_size isn't taken into account...
135  have to buffer the samples :-( */
136  avctx->block_align = s->block_size;
137  avctx->bits_per_coded_sample = 4;
138  ) /* End of CASE */
139  CASE(ADPCM_IMA_QT,
140  avctx->frame_size = 64;
141  avctx->block_align = 34 * channels;
142  ) /* End of CASE */
143  CASE(ADPCM_MS,
144  uint8_t *extradata;
145  /* each 16 bits sample gives one nibble
146  and we have 7 bytes per channel overhead */
147  avctx->frame_size = (s->block_size - 7 * channels) * 2 / channels + 2;
148  avctx->bits_per_coded_sample = 4;
149  avctx->block_align = s->block_size;
150  if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
151  return AVERROR(ENOMEM);
152  avctx->extradata_size = 32;
153  extradata = avctx->extradata;
154  bytestream_put_le16(&extradata, avctx->frame_size);
155  bytestream_put_le16(&extradata, 7); /* wNumCoef */
156  for (int i = 0; i < 7; i++) {
157  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
158  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
159  }
160  ) /* End of CASE */
161  CASE(ADPCM_YAMAHA,
162  avctx->frame_size = s->block_size * 2 / channels;
163  avctx->block_align = s->block_size;
164  ) /* End of CASE */
165  CASE(ADPCM_SWF,
166  if (avctx->sample_rate != 11025 &&
167  avctx->sample_rate != 22050 &&
168  avctx->sample_rate != 44100) {
169  av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
170  "22050 or 44100\n");
171  return AVERROR(EINVAL);
172  }
173  avctx->frame_size = 4096; /* Hardcoded according to the SWF spec. */
174  avctx->block_align = (2 + channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
175  ) /* End of CASE */
178  avctx->frame_size = s->block_size * 2 / channels;
179  avctx->block_align = s->block_size;
180  break;
181  CASE(ADPCM_IMA_AMV,
182  if (avctx->sample_rate != 22050) {
183  av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
184  return AVERROR(EINVAL);
185  }
186 
187  if (channels != 1) {
188  av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
189  return AVERROR(EINVAL);
190  }
191 
192  avctx->frame_size = s->block_size;
193  avctx->block_align = 8 + (FFALIGN(avctx->frame_size, 2) / 2);
194  ) /* End of CASE */
195  CASE(ADPCM_IMA_APM,
196  avctx->frame_size = s->block_size * 2 / channels;
197  avctx->block_align = s->block_size;
198 
200  return AVERROR(ENOMEM);
201  avctx->extradata_size = 28;
202  ) /* End of CASE */
203  CASE(ADPCM_ARGO,
204  avctx->frame_size = 32;
205  avctx->block_align = 17 * channels;
206  ) /* End of CASE */
207  CASE(ADPCM_IMA_WS,
208  /* each 16 bits sample gives one nibble */
209  avctx->frame_size = s->block_size * 2 / channels;
210  avctx->block_align = s->block_size;
211  ) /* End of CASE */
212  default:
213  return AVERROR(EINVAL);
214  }
215 
216  return 0;
217 }
218 
220 {
221  ADPCMEncodeContext *s = avctx->priv_data;
222  av_freep(&s->paths);
223  av_freep(&s->node_buf);
224  av_freep(&s->nodep_buf);
225  av_freep(&s->trellis_hash);
226 
227  return 0;
228 }
229 
230 
232  int16_t sample)
233 {
234  int delta = sample - c->prev_sample;
235  int nibble = FFMIN(7, abs(delta) * 4 /
236  ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
237  c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
238  ff_adpcm_yamaha_difflookup[nibble]) / 8);
239  c->prev_sample = av_clip_int16(c->prev_sample);
240  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
241  return nibble;
242 }
243 
245 {
246  const int delta = sample - c->prev_sample;
247  const int step = ff_adpcm_step_table[c->step_index];
248  const int sign = (delta < 0) * 8;
249 
250  int nibble = FFMIN(abs(delta) * 4 / step, 7);
251  int diff = (step * nibble) >> 2;
252  if (sign)
253  diff = -diff;
254 
255  nibble = sign | nibble;
256 
257  c->prev_sample += diff;
258  c->prev_sample = av_clip_int16(c->prev_sample);
259  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
260  return nibble;
261 }
262 
264  int16_t sample)
265 {
266  int delta = sample - c->prev_sample;
267  int diff, step = ff_adpcm_step_table[c->step_index];
268  int nibble = 8*(delta < 0);
269 
270  delta= abs(delta);
271  diff = delta + (step >> 3);
272 
273  if (delta >= step) {
274  nibble |= 4;
275  delta -= step;
276  }
277  step >>= 1;
278  if (delta >= step) {
279  nibble |= 2;
280  delta -= step;
281  }
282  step >>= 1;
283  if (delta >= step) {
284  nibble |= 1;
285  delta -= step;
286  }
287  diff -= delta;
288 
289  if (nibble & 8)
290  c->prev_sample -= diff;
291  else
292  c->prev_sample += diff;
293 
294  c->prev_sample = av_clip_int16(c->prev_sample);
295  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
296 
297  return nibble;
298 }
299 
301  int16_t sample)
302 {
303  int predictor, nibble, bias;
304 
305  predictor = (((c->sample1) * (c->coeff1)) +
306  (( c->sample2) * (c->coeff2))) / 64;
307 
308  nibble = sample - predictor;
309  if (nibble >= 0)
310  bias = c->idelta / 2;
311  else
312  bias = -c->idelta / 2;
313 
314  nibble = (nibble + bias) / c->idelta;
315  nibble = av_clip_intp2(nibble, 3) & 0x0F;
316 
317  predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
318 
319  c->sample2 = c->sample1;
320  c->sample1 = av_clip_int16(predictor);
321 
322  c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
323  if (c->idelta < 16)
324  c->idelta = 16;
325 
326  return nibble;
327 }
328 
330  int16_t sample)
331 {
332  int nibble, delta;
333 
334  if (!c->step) {
335  c->predictor = 0;
336  c->step = 127;
337  }
338 
339  delta = sample - c->predictor;
340 
341  nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
342 
343  c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
344  c->predictor = av_clip_int16(c->predictor);
345  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
346  c->step = av_clip(c->step, 127, 24576);
347 
348  return nibble;
349 }
350 
352  const int16_t *samples, uint8_t *dst,
353  ADPCMChannelStatus *c, int n, int stride)
354 {
355  //FIXME 6% faster if frontier is a compile-time constant
356  ADPCMEncodeContext *s = avctx->priv_data;
357  const int frontier = 1 << avctx->trellis;
358  const int version = avctx->codec->id;
359  TrellisPath *paths = s->paths, *p;
360  TrellisNode *node_buf = s->node_buf;
361  TrellisNode **nodep_buf = s->nodep_buf;
362  TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
363  TrellisNode **nodes_next = nodep_buf + frontier;
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));
367 
368  memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
369  nodes[0] = node_buf + frontier;
370  nodes[0]->ssd = 0;
371  nodes[0]->path = 0;
372  nodes[0]->step = c->step_index;
373  nodes[0]->sample1 = c->sample1;
374  nodes[0]->sample2 = c->sample2;
379  nodes[0]->sample1 = c->prev_sample;
381  nodes[0]->step = c->idelta;
383  if (c->step == 0) {
384  nodes[0]->step = 127;
385  nodes[0]->sample1 = 0;
386  } else {
387  nodes[0]->step = c->step;
388  nodes[0]->sample1 = c->predictor;
389  }
390  }
391 
392  for (i = 0; i < n; i++) {
393  TrellisNode *t = node_buf + frontier*(i&1);
394  TrellisNode **u;
395  int sample = samples[i * stride];
396  int heap_pos = 0;
397  memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
398  for (j = 0; j < frontier && nodes[j]; j++) {
399  // higher j have higher ssd already, so they're likely
400  // to yield a suboptimal next sample too
401  const int range = (j < frontier / 2) ? 1 : 0;
402  const int step = nodes[j]->step;
403  int nidx;
404  if (version == AV_CODEC_ID_ADPCM_MS) {
405  const int predictor = ((nodes[j]->sample1 * c->coeff1) +
406  (nodes[j]->sample2 * c->coeff2)) / 64;
407  const int div = (sample - predictor) / step;
408  const int nmin = av_clip(div-range, -8, 6);
409  const int nmax = av_clip(div+range, -7, 7);
410  for (nidx = nmin; nidx <= nmax; nidx++) {
411  const int nibble = nidx & 0xf;
412  int dec_sample = predictor + nidx * step;
413 #define STORE_NODE(NAME, STEP_INDEX)\
414  int d;\
415  uint32_t ssd;\
416  int pos;\
417  TrellisNode *u;\
418  uint8_t *h;\
419  dec_sample = av_clip_int16(dec_sample);\
420  d = sample - dec_sample;\
421  ssd = nodes[j]->ssd + d*(unsigned)d;\
422  /* Check for wraparound, skip such samples completely. \
423  * Note, changing ssd to a 64 bit variable would be \
424  * simpler, avoiding this check, but it's slower on \
425  * x86 32 bit at the moment. */\
426  if (ssd < nodes[j]->ssd)\
427  goto next_##NAME;\
428  /* Collapse any two states with the same previous sample value. \
429  * One could also distinguish states by step and by 2nd to last
430  * sample, but the effects of that are negligible.
431  * Since nodes in the previous generation are iterated
432  * through a heap, they're roughly ordered from better to
433  * worse, but not strictly ordered. Therefore, an earlier
434  * node with the same sample value is better in most cases
435  * (and thus the current is skipped), but not strictly
436  * in all cases. Only skipping samples where ssd >=
437  * ssd of the earlier node with the same sample gives
438  * slightly worse quality, though, for some reason. */ \
439  h = &hash[(uint16_t) dec_sample];\
440  if (*h == generation)\
441  goto next_##NAME;\
442  if (heap_pos < frontier) {\
443  pos = heap_pos++;\
444  } else {\
445  /* Try to replace one of the leaf nodes with the new \
446  * one, but try a different slot each time. */\
447  pos = (frontier >> 1) +\
448  (heap_pos & ((frontier >> 1) - 1));\
449  if (ssd > nodes_next[pos]->ssd)\
450  goto next_##NAME;\
451  heap_pos++;\
452  }\
453  *h = generation;\
454  u = nodes_next[pos];\
455  if (!u) {\
456  av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
457  u = t++;\
458  nodes_next[pos] = u;\
459  u->path = pathn++;\
460  }\
461  u->ssd = ssd;\
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;\
467  /* Sift the newly inserted node up in the heap to \
468  * restore the heap property. */\
469  while (pos > 0) {\
470  int parent = (pos - 1) >> 1;\
471  if (nodes_next[parent]->ssd <= ssd)\
472  break;\
473  FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
474  pos = parent;\
475  }\
476  next_##NAME:;
477  STORE_NODE(ms, FFMAX(16,
478  (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
479  }
480  } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
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);\
489  if (nmin <= 0)\
490  nmin--; /* distinguish -0 from +0 */\
491  if (nmax < 0)\
492  nmax--;\
493  for (nidx = nmin; nidx <= nmax; nidx++) {\
494  const int nibble = nidx < 0 ? 7 - nidx : nidx;\
495  int dec_sample = predictor +\
496  (STEP_TABLE *\
497  ff_adpcm_yamaha_difflookup[nibble]) / 8;\
498  STORE_NODE(NAME, STEP_INDEX);\
499  }
501  av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
502  } else { //AV_CODEC_ID_ADPCM_YAMAHA
503  LOOP_NODES(yamaha, step,
504  av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
505  127, 24576));
506 #undef LOOP_NODES
507 #undef STORE_NODE
508  }
509  }
510 
511  u = nodes;
512  nodes = nodes_next;
513  nodes_next = u;
514 
515  generation++;
516  if (generation == 255) {
517  memset(hash, 0xff, 65536 * sizeof(*hash));
518  generation = 0;
519  }
520 
521  // prevent overflow
522  if (nodes[0]->ssd > (1 << 28)) {
523  for (j = 1; j < frontier && nodes[j]; j++)
524  nodes[j]->ssd -= nodes[0]->ssd;
525  nodes[0]->ssd = 0;
526  }
527 
528  // merge old paths to save memory
529  if (i == froze + FREEZE_INTERVAL) {
530  p = &paths[nodes[0]->path];
531  for (k = i; k > froze; k--) {
532  dst[k] = p->nibble;
533  p = &paths[p->prev];
534  }
535  froze = i;
536  pathn = 0;
537  // other nodes might use paths that don't coincide with the frozen one.
538  // checking which nodes do so is too slow, so just kill them all.
539  // this also slightly improves quality, but I don't know why.
540  memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
541  }
542  }
543 
544  p = &paths[nodes[0]->path];
545  for (i = n - 1; i > froze; i--) {
546  dst[i] = p->nibble;
547  p = &paths[p->prev];
548  }
549 
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;
556 }
557 
558 #if CONFIG_ADPCM_ARGO_ENCODER
559 static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
560  int shift, int flag)
561 {
562  int nibble;
563 
564  if (flag)
565  nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
566  else
567  nibble = 4 * s - 4 * cs->sample1;
568 
569  return (nibble >> shift) & 0x0F;
570 }
571 
572 static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
573  const int16_t *samples, int nsamples,
574  int shift, int flag)
575 {
576  int64_t error = 0;
577 
578  if (pb) {
579  put_bits(pb, 4, shift - 2);
580  put_bits(pb, 1, 0);
581  put_bits(pb, 1, !!flag);
582  put_bits(pb, 2, 0);
583  }
584 
585  for (int n = 0; n < nsamples; n++) {
586  /* Compress the nibble, then expand it to see how much precision we've lost. */
587  int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
588  int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
589 
590  error += abs(samples[n] - sample);
591 
592  if (pb)
593  put_bits(pb, 4, nibble);
594  }
595 
596  return error;
597 }
598 #endif
599 
600 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
601  const AVFrame *frame, int *got_packet_ptr)
602 {
603  int st, pkt_size, ret;
604  const int16_t *samples;
605  const int16_t *const *samples_p;
606  uint8_t *dst;
607  ADPCMEncodeContext *c = avctx->priv_data;
608  int channels = avctx->ch_layout.nb_channels;
609 
610  samples = (const int16_t *)frame->data[0];
611  samples_p = (const int16_t *const *)frame->extended_data;
612  st = channels == 2;
613 
614  if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
618  pkt_size = (frame->nb_samples * channels + 1) / 2;
619  else
620  pkt_size = avctx->block_align;
621  if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
622  return ret;
623  dst = avpkt->data;
624 
625  switch(avctx->codec->id) {
626  CASE(ADPCM_IMA_WAV,
627  int blocks = (frame->nb_samples - 1) / 8;
628 
629  for (int ch = 0; ch < channels; ch++) {
630  ADPCMChannelStatus *status = &c->status[ch];
631  status->prev_sample = samples_p[ch][0];
632  /* status->step_index = 0;
633  XXX: not sure how to init the state machine */
634  bytestream_put_le16(&dst, status->prev_sample);
635  *dst++ = status->step_index;
636  *dst++ = 0; /* unknown */
637  }
638 
639  /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
640  if (avctx->trellis > 0) {
641  uint8_t *buf;
642  if (!FF_ALLOC_TYPED_ARRAY(buf, channels * blocks * 8))
643  return AVERROR(ENOMEM);
644  for (int ch = 0; ch < channels; ch++) {
645  adpcm_compress_trellis(avctx, &samples_p[ch][1],
646  buf + ch * blocks * 8, &c->status[ch],
647  blocks * 8, 1);
648  }
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);
654  }
655  }
656  av_free(buf);
657  } else {
658  for (int i = 0; i < blocks; i++) {
659  for (int ch = 0; ch < channels; ch++) {
660  ADPCMChannelStatus *status = &c->status[ch];
661  const int16_t *smp = &samples_p[ch][1 + i * 8];
662  for (int j = 0; j < 8; j += 2) {
663  uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
664  v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
665  *dst++ = v;
666  }
667  }
668  }
669  }
670  ) /* End of CASE */
671  CASE(ADPCM_IMA_QT,
672  PutBitContext pb;
673  init_put_bits(&pb, dst, pkt_size);
674 
675  for (int ch = 0; ch < channels; ch++) {
676  ADPCMChannelStatus *status = &c->status[ch];
677  put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
678  put_bits(&pb, 7, status->step_index);
679  if (avctx->trellis > 0) {
680  uint8_t buf[64];
681  adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
682  64, 1);
683  for (int i = 0; i < 64; i++)
684  put_bits(&pb, 4, buf[i ^ 1]);
685  status->prev_sample = status->predictor;
686  } else {
687  for (int i = 0; i < 64; i += 2) {
688  int t1, t2;
689  t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
690  t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
691  put_bits(&pb, 4, t2);
692  put_bits(&pb, 4, t1);
693  }
694  }
695  }
696 
697  flush_put_bits(&pb);
698  ) /* End of CASE */
699  CASE(ADPCM_IMA_SSI,
700  PutBitContext pb;
701  init_put_bits(&pb, dst, pkt_size);
702 
703  av_assert0(avctx->trellis == 0);
704 
705  for (int i = 0; i < frame->nb_samples; i++) {
706  for (int ch = 0; ch < channels; ch++) {
707  put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
708  }
709  }
710 
711  flush_put_bits(&pb);
712  ) /* End of CASE */
713  CASE(ADPCM_IMA_ALP,
714  PutBitContext pb;
715  init_put_bits(&pb, dst, pkt_size);
716 
717  av_assert0(avctx->trellis == 0);
718 
719  for (int n = frame->nb_samples / 2; n > 0; n--) {
720  for (int ch = 0; ch < channels; ch++) {
721  put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
722  put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
723  }
724  samples += channels;
725  }
726 
727  flush_put_bits(&pb);
728  ) /* End of CASE */
729  CASE(ADPCM_SWF,
730  const int n = frame->nb_samples - 1;
731  PutBitContext pb;
732  init_put_bits(&pb, dst, pkt_size);
733 
734  /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
735  av_assert0(n == 4095);
736 
737  // store AdpcmCodeSize
738  put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
739 
740  // init the encoder state
741  for (int i = 0; i < channels; i++) {
742  // clip step so it fits 6 bits
743  c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
744  put_sbits(&pb, 16, samples[i]);
745  put_bits(&pb, 6, c->status[i].step_index);
746  c->status[i].prev_sample = samples[i];
747  }
748 
749  if (avctx->trellis > 0) {
750  uint8_t buf[8190 /* = 2 * n */];
752  &c->status[0], n, channels);
753  if (channels == 2)
755  buf + n, &c->status[1], n,
756  channels);
757  for (int i = 0; i < n; i++) {
758  put_bits(&pb, 4, buf[i]);
759  if (channels == 2)
760  put_bits(&pb, 4, buf[n + i]);
761  }
762  } else {
763  for (int i = 1; i < frame->nb_samples; i++) {
764  put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
765  samples[channels * i]));
766  if (channels == 2)
767  put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
768  samples[2 * i + 1]));
769  }
770  }
771  flush_put_bits(&pb);
772  ) /* End of CASE */
773  CASE(ADPCM_MS,
774  for (int i = 0; i < channels; i++) {
775  int predictor = 0;
776  *dst++ = predictor;
777  c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
778  c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
779  }
780  for (int i = 0; i < channels; i++) {
781  if (c->status[i].idelta < 16)
782  c->status[i].idelta = 16;
783  bytestream_put_le16(&dst, c->status[i].idelta);
784  }
785  for (int i = 0; i < channels; i++)
786  c->status[i].sample2= *samples++;
787  for (int i = 0; i < channels; i++) {
788  c->status[i].sample1 = *samples++;
789  bytestream_put_le16(&dst, c->status[i].sample1);
790  }
791  for (int i = 0; i < channels; i++)
792  bytestream_put_le16(&dst, c->status[i].sample2);
793 
794  if (avctx->trellis > 0) {
795  const int n = avctx->block_align - 7 * channels;
796  uint8_t *buf = av_malloc(2 * n);
797  if (!buf)
798  return AVERROR(ENOMEM);
799  if (channels == 1) {
800  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
801  channels);
802  for (int i = 0; i < n; i += 2)
803  *dst++ = (buf[i] << 4) | buf[i + 1];
804  } else {
805  adpcm_compress_trellis(avctx, samples, buf,
806  &c->status[0], n, channels);
807  adpcm_compress_trellis(avctx, samples + 1, buf + n,
808  &c->status[1], n, channels);
809  for (int i = 0; i < n; i++)
810  *dst++ = (buf[i] << 4) | buf[n + i];
811  }
812  av_free(buf);
813  } else {
814  for (int i = 7 * channels; i < avctx->block_align; i++) {
815  int nibble;
816  nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
817  nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
818  *dst++ = nibble;
819  }
820  }
821  ) /* End of CASE */
822  CASE(ADPCM_YAMAHA,
823  int n = frame->nb_samples / 2;
824  if (avctx->trellis > 0) {
825  uint8_t *buf = av_malloc(2 * n * 2);
826  if (!buf)
827  return AVERROR(ENOMEM);
828  n *= 2;
829  if (channels == 1) {
830  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
831  channels);
832  for (int i = 0; i < n; i += 2)
833  *dst++ = buf[i] | (buf[i + 1] << 4);
834  } else {
835  adpcm_compress_trellis(avctx, samples, buf,
836  &c->status[0], n, channels);
837  adpcm_compress_trellis(avctx, samples + 1, buf + n,
838  &c->status[1], n, channels);
839  for (int i = 0; i < n; i++)
840  *dst++ = buf[i] | (buf[n + i] << 4);
841  }
842  av_free(buf);
843  } else
844  for (n *= channels; n > 0; n--) {
845  int nibble;
846  nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
847  nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
848  *dst++ = nibble;
849  }
850  ) /* End of CASE */
851  CASE(ADPCM_IMA_APM,
852  PutBitContext pb;
853  init_put_bits(&pb, dst, pkt_size);
854 
855  av_assert0(avctx->trellis == 0);
856 
857  for (int n = frame->nb_samples / 2; n > 0; n--) {
858  for (int ch = 0; ch < channels; ch++) {
859  put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
860  put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
861  }
862  samples += channels;
863  }
864 
865  flush_put_bits(&pb);
866  ) /* End of CASE */
867  CASE(ADPCM_IMA_AMV,
868  av_assert0(channels == 1);
869 
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);
874  bytestream_put_le32(&dst, avctx->frame_size);
875 
876  if (avctx->trellis > 0) {
877  const int n = frame->nb_samples >> 1;
878  uint8_t *buf = av_malloc(2 * n);
879 
880  if (!buf)
881  return AVERROR(ENOMEM);
882 
883  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, channels);
884  for (int i = 0; i < n; i++)
885  bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
886 
887  samples += 2 * n;
888  av_free(buf);
889  } else for (int n = frame->nb_samples >> 1; n > 0; n--) {
890  int nibble;
891  nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
892  nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
893  bytestream_put_byte(&dst, nibble);
894  }
895 
896  if (avctx->frame_size & 1) {
897  int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
898  bytestream_put_byte(&dst, nibble);
899  }
900  ) /* End of CASE */
901  CASE(ADPCM_ARGO,
902  PutBitContext pb;
903  init_put_bits(&pb, dst, pkt_size);
904 
905  av_assert0(frame->nb_samples == 32);
906 
907  for (int ch = 0; ch < channels; ch++) {
908  int64_t error = INT64_MAX, tmperr = INT64_MAX;
909  int shift = 2, flag = 0;
910  int saved1 = c->status[ch].sample1;
911  int saved2 = c->status[ch].sample2;
912 
913  /* Find the optimal coefficients, bail early if we find a perfect result. */
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],
919  frame->nb_samples, s, f);
920  if (tmperr < error) {
921  shift = s;
922  flag = f;
923  error = tmperr;
924  }
925  }
926  }
927 
928  /* Now actually do the encode. */
929  c->status[ch].sample1 = saved1;
930  c->status[ch].sample2 = saved2;
931  adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
932  frame->nb_samples, shift, flag);
933  }
934 
935  flush_put_bits(&pb);
936  ) /* End of CASE */
937  CASE(ADPCM_IMA_WS,
938  PutBitContext pb;
939  init_put_bits(&pb, dst, pkt_size);
940 
941  av_assert0(avctx->trellis == 0);
942  for (int n = frame->nb_samples / 2; n > 0; n--) {
943  /* stereo: 1 byte (2 samples) for left, 1 byte for right */
944  for (int ch = 0; ch < channels; ch++) {
945  int t1, t2;
946  t1 = adpcm_ima_compress_sample(&c->status[ch], *samples++);
947  t2 = adpcm_ima_compress_sample(&c->status[ch], samples[st]);
948  put_bits(&pb, 4, t2);
949  put_bits(&pb, 4, t1);
950  }
951  samples += channels;
952  }
953  flush_put_bits(&pb);
954  ) /* End of CASE */
955  default:
956  return AVERROR(EINVAL);
957  }
958 
959  *got_packet_ptr = 1;
960  return 0;
961 }
962 
963 static const enum AVSampleFormat sample_fmts[] = {
965 };
966 
967 static const enum AVSampleFormat sample_fmts_p[] = {
969 };
970 
971 static const AVChannelLayout ch_layouts[] = {
974  { 0 },
975 };
976 
977 static const AVOption options[] = {
978  {
979  .name = "block_size",
980  .help = "set the block size",
981  .offset = offsetof(ADPCMEncodeContext, block_size),
982  .type = AV_OPT_TYPE_INT,
983  .default_val = {.i64 = 1024},
984  .min = 32,
985  .max = 8192, /* Is this a reasonable upper limit? */
987  },
988  { NULL }
989 };
990 
991 static const AVClass adpcm_encoder_class = {
992  .class_name = "ADPCM encoder",
993  .item_name = av_default_item_name,
994  .option = options,
995  .version = LIBAVUTIL_VERSION_INT,
996 };
997 
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 = { \
1001  .p.name = #name_, \
1002  CODEC_LONG_NAME(long_name_), \
1003  .p.type = AVMEDIA_TYPE_AUDIO, \
1004  .p.id = id_, \
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, \
1015 };
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)
1023 
1024 ADPCM_ENCODER(ADPCM_ARGO, adpcm_argo, sample_fmts_p, 0, "ADPCM Argonaut Games")
1025 ADPCM_ENCODER(ADPCM_IMA_AMV, adpcm_ima_amv, sample_fmts, 0, "ADPCM IMA AMV")
1026 ADPCM_ENCODER(ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM")
1027 ADPCM_ENCODER(ADPCM_IMA_ALP, adpcm_ima_alp, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA High Voltage Software ALP")
1028 ADPCM_ENCODER(ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, 0, "ADPCM IMA QuickTime")
1029 ADPCM_ENCODER(ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive")
1030 ADPCM_ENCODER(ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0, "ADPCM IMA WAV")
1031 ADPCM_ENCODER(ADPCM_IMA_WS, adpcm_ima_ws, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Westwood")
1032 ADPCM_ENCODER(ADPCM_MS, adpcm_ms, sample_fmts, 0, "ADPCM Microsoft")
1033 ADPCM_ENCODER(ADPCM_SWF, adpcm_swf, sample_fmts, 0, "ADPCM Shockwave Flash")
1034 ADPCM_ENCODER(ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, 0, "ADPCM Yamaha")
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1083
adpcm_yamaha_compress_sample
static uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:329
AV_CODEC_ID_ADPCM_MS
@ AV_CODEC_ID_ADPCM_MS
Definition: codec_id.h:379
AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition: codec_id.h:373
av_clip
#define av_clip
Definition: common.h:100
TrellisNode::sample1
int sample1
Definition: adpcmenc.c:64
AVERROR
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
opt.h
LOOP_NODES
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:393
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1056
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:251
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:124
ADPCM_ENCODER
#define ADPCM_ENCODER(codec, name, sample_fmts, capabilities, long_name)
Definition: adpcmenc.c:1005
ff_adpcm_AdaptationTable
const int16_t ff_adpcm_AdaptationTable[]
Definition: adpcm_data.c:54
adpcm_encoder_class
static const AVClass adpcm_encoder_class
Definition: adpcmenc.c:976
TrellisNode::path
int path
Definition: adpcmenc.c:63
int64_t
long long int64_t
Definition: coverity.c:34
put_sbits
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:281
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
step
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
Definition: rate_distortion.txt:58
AVPacket::data
uint8_t * data
Definition: packet.h:539
TrellisNode::sample2
int sample2
Definition: adpcmenc.c:65
AVOption
AVOption.
Definition: opt.h:429
encode.h
TrellisNode::step
int step
Definition: adpcmenc.c:66
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
hash
uint8_t hash[HASH_SIZE]
Definition: movenc.c:58
ADPCMEncodeContext::nodep_buf
TrellisNode ** nodep_buf
Definition: adpcmenc.c:76
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:460
STORE_NODE
#define STORE_NODE(NAME, STEP_INDEX)
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1071
TrellisNode
Definition: adpcmenc.c:61
FF_ALLOC_TYPED_ARRAY
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
Definition: internal.h:77
ADPCMEncodeContext::status
ADPCMChannelStatus status[6]
Definition: adpcmenc.c:73
ADPCMEncodeContext::paths
TrellisPath * paths
Definition: adpcmenc.c:74
ADPCMEncodeContext::node_buf
TrellisNode * node_buf
Definition: adpcmenc.c:75
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:357
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:550
TrellisNode::ssd
uint32_t ssd
Definition: adpcmenc.c:62
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
options
static const AVOption options[]
Definition: adpcmenc.c:962
ADPCMChannelStatus::sample1
int sample1
Definition: adpcm.h:39
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:530
adpcm_data.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
TrellisPath::nibble
int nibble
Definition: adpcmenc.c:57
ADPCMEncodeContext::block_size
int block_size
Definition: adpcmenc.c:71
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
channels
channels
Definition: aptx.h:31
PutBitContext
Definition: put_bits.h:50
adpcm_compress_trellis
static void adpcm_compress_trellis(AVCodecContext *avctx, const int16_t *samples, uint8_t *dst, ADPCMChannelStatus *c, int n, int stride)
Definition: adpcmenc.c:351
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:461
if
if(ret)
Definition: filter_design.txt:179
TrellisPath
Definition: aaccoder.c:411
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
av_clip_int16
#define av_clip_int16
Definition: common.h:115
NULL
#define NULL
Definition: coverity.c:32
av_clip_intp2
#define av_clip_intp2
Definition: common.h:121
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AV_CODEC_ID_ADPCM_YAMAHA
@ AV_CODEC_ID_ADPCM_YAMAHA
Definition: codec_id.h:387
AV_CODEC_ID_ADPCM_IMA_WS
@ AV_CODEC_ID_ADPCM_IMA_WS
Definition: codec_id.h:377
bias
static int bias(int x, int c)
Definition: vqcdec.c:115
AV_CODEC_ID_ADPCM_ARGO
@ AV_CODEC_ID_ADPCM_ARGO
Definition: codec_id.h:415
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
options
Definition: swscale.c:42
AV_CODEC_ID_ADPCM_IMA_AMV
@ AV_CODEC_ID_ADPCM_IMA_AMV
Definition: codec_id.h:392
abs
#define abs(x)
Definition: cuda_runtime.h:35
AVCodecContext::trellis
int trellis
trellis RD quantization
Definition: avcodec.h:1337
ADPCMChannelStatus::sample2
int sample2
Definition: adpcm.h:40
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition: opt.h:352
c
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
Definition: undefined.txt:32
adpcm_ima_alp_compress_sample
static uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:244
adpcm.h
ff_adpcm_yamaha_difflookup
const int8_t ff_adpcm_yamaha_difflookup[]
Definition: adpcm_data.c:74
f
f
Definition: af_crystalizer.c:122
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:317
codec_internal.h
shift
static int shift(int a, int b)
Definition: bonk.c:261
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
AV_CODEC_ID_ADPCM_IMA_ALP
@ AV_CODEC_ID_ADPCM_IMA_ALP
Definition: codec_id.h:419
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
ff_adpcm_step_table
const int16_t ff_adpcm_step_table[89]
This is the step table.
Definition: adpcm_data.c:39
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
sample
#define sample
Definition: flacdsp_template.c:44
AV_CODEC_ID_ADPCM_SWF
@ AV_CODEC_ID_ADPCM_SWF
Definition: codec_id.h:386
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2594
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:166
version
version
Definition: libkvazaar.c:321
predictor
static void predictor(uint8_t *src, ptrdiff_t size)
Definition: exrenc.c:169
FREEZE_INTERVAL
#define FREEZE_INTERVAL
Definition: adpcmenc.c:80
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
flag
#define flag(name)
Definition: cbs_av1.c:474
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1578
ff_adpcm_AdaptCoeff1
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:60
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_adpcm_AdaptCoeff2
const int8_t ff_adpcm_AdaptCoeff2[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:65
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:529
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
ADPCMEncodeContext::trellis_hash
uint8_t * trellis_hash
Definition: adpcmenc.c:77
delta
float delta
Definition: vorbis_enc_data.h:430
adpcm_ima_compress_sample
static uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:231
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ch_layouts
static const AVChannelLayout ch_layouts[]
Definition: adpcmenc.c:956
AV_CODEC_ID_ADPCM_IMA_APM
@ AV_CODEC_ID_ADPCM_IMA_APM
Definition: codec_id.h:418
TrellisPath::prev
int prev
Definition: aaccoder.c:413
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
ff_adpcm_argo_expand_nibble
int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
Definition: adpcm.c:818
ff_adpcm_index_table
const int8_t ff_adpcm_index_table[16]
Definition: adpcm_data.c:30
adpcm_encode_frame
static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: adpcmenc.c:585
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
ret
ret
Definition: filter_design.txt:187
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:1089
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
frame
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
Definition: filter_design.txt:264
ADPCMEncodeContext
Definition: adpcmenc.c:69
CASE
#define CASE(codec,...)
Definition: adpcmenc.c:53
sample_fmts_p
static enum AVSampleFormat sample_fmts_p[]
Definition: adpcmenc.c:952
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:451
status
ov_status_e status
Definition: dnn_backend_openvino.c:100
ima
#define ima
Definition: vf_colormatrix.c:108
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
ff_adpcm_yamaha_indexscale
const int16_t ff_adpcm_yamaha_indexscale[]
Definition: adpcm_data.c:69
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
mem.h
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
AV_CODEC_ID_ADPCM_IMA_SSI
@ AV_CODEC_ID_ADPCM_IMA_SSI
Definition: codec_id.h:416
adpcm_encode_init
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
Definition: adpcmenc.c:82
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:392
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AV_CODEC_ID_ADPCM_IMA_WAV
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition: codec_id.h:374
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
adpcm_ima_qt_compress_sample
static uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:263
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:81
put_bits.h
ADPCMChannelStatus
Definition: adpcm.h:31
adpcm_ms_compress_sample
static uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:300
adpcm_encode_close
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
Definition: adpcmenc.c:219