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  avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate / avctx->frame_size;
139  ) /* End of CASE */
140  CASE(ADPCM_IMA_QT,
141  avctx->frame_size = 64;
142  avctx->block_align = 34 * channels;
143  ) /* End of CASE */
144  CASE(ADPCM_MS,
145  uint8_t *extradata;
146  /* each 16 bits sample gives one nibble
147  and we have 7 bytes per channel overhead */
148  avctx->frame_size = (s->block_size - 7 * channels) * 2 / channels + 2;
149  avctx->bits_per_coded_sample = 4;
150  avctx->block_align = s->block_size;
151  avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate / avctx->frame_size;
152  if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
153  return AVERROR(ENOMEM);
154  avctx->extradata_size = 32;
155  extradata = avctx->extradata;
156  bytestream_put_le16(&extradata, avctx->frame_size);
157  bytestream_put_le16(&extradata, 7); /* wNumCoef */
158  for (int i = 0; i < 7; i++) {
159  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
160  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
161  }
162  ) /* End of CASE */
163  CASE(ADPCM_YAMAHA,
164  avctx->frame_size = s->block_size * 2 / channels;
165  avctx->block_align = s->block_size;
166  ) /* End of CASE */
167  CASE(ADPCM_SWF,
168  avctx->frame_size = 4096; /* Hardcoded according to the SWF spec. */
169  avctx->block_align = (2 + channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
170  ) /* End of CASE */
173  avctx->frame_size = s->block_size * 2 / channels;
174  avctx->block_align = s->block_size;
175  break;
176  CASE(ADPCM_IMA_AMV,
177  avctx->frame_size = s->block_size;
178  avctx->block_align = 8 + (FFALIGN(avctx->frame_size, 2) / 2);
179  ) /* End of CASE */
180  CASE(ADPCM_IMA_APM,
181  avctx->frame_size = s->block_size * 2 / channels;
182  avctx->block_align = s->block_size;
183 
185  return AVERROR(ENOMEM);
186  avctx->extradata_size = 28;
187  ) /* End of CASE */
188  CASE(ADPCM_ARGO,
189  avctx->frame_size = 32;
190  avctx->block_align = 17 * channels;
191  ) /* End of CASE */
192  CASE(ADPCM_IMA_WS,
193  /* each 16 bits sample gives one nibble */
194  avctx->frame_size = s->block_size * 2 / channels;
195  avctx->block_align = s->block_size;
196  ) /* End of CASE */
197  default:
198  av_unreachable("there is a case for every codec using adpcm_encode_init()");
199  }
200 
201  return 0;
202 }
203 
205 {
206  ADPCMEncodeContext *s = avctx->priv_data;
207  av_freep(&s->paths);
208  av_freep(&s->node_buf);
209  av_freep(&s->nodep_buf);
210  av_freep(&s->trellis_hash);
211 
212  return 0;
213 }
214 
215 
217  int16_t sample)
218 {
219  int delta = sample - c->prev_sample;
220  int nibble = FFMIN(7, abs(delta) * 4 /
221  ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
222  c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
223  ff_adpcm_yamaha_difflookup[nibble]) / 8);
224  c->prev_sample = av_clip_int16(c->prev_sample);
225  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
226  return nibble;
227 }
228 
230 {
231  const int delta = sample - c->prev_sample;
232  const int step = ff_adpcm_step_table[c->step_index];
233  const int sign = (delta < 0) * 8;
234 
235  int nibble = FFMIN(abs(delta) * 4 / step, 7);
236  int diff = (step * nibble) >> 2;
237  if (sign)
238  diff = -diff;
239 
240  nibble = sign | nibble;
241 
242  c->prev_sample += diff;
243  c->prev_sample = av_clip_int16(c->prev_sample);
244  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
245  return nibble;
246 }
247 
249  int16_t sample)
250 {
251  int delta = sample - c->prev_sample;
252  int diff, step = ff_adpcm_step_table[c->step_index];
253  int nibble = 8*(delta < 0);
254 
255  delta= abs(delta);
256  diff = delta + (step >> 3);
257 
258  if (delta >= step) {
259  nibble |= 4;
260  delta -= step;
261  }
262  step >>= 1;
263  if (delta >= step) {
264  nibble |= 2;
265  delta -= step;
266  }
267  step >>= 1;
268  if (delta >= step) {
269  nibble |= 1;
270  delta -= step;
271  }
272  diff -= delta;
273 
274  if (nibble & 8)
275  c->prev_sample -= diff;
276  else
277  c->prev_sample += diff;
278 
279  c->prev_sample = av_clip_int16(c->prev_sample);
280  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
281 
282  return nibble;
283 }
284 
286  int16_t sample)
287 {
288  int predictor, nibble, bias;
289 
290  predictor = (((c->sample1) * (c->coeff1)) +
291  (( c->sample2) * (c->coeff2))) / 64;
292 
293  nibble = sample - predictor;
294  if (nibble >= 0)
295  bias = c->idelta / 2;
296  else
297  bias = -c->idelta / 2;
298 
299  nibble = (nibble + bias) / c->idelta;
300  nibble = av_clip_intp2(nibble, 3) & 0x0F;
301 
302  predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
303 
304  c->sample2 = c->sample1;
305  c->sample1 = av_clip_int16(predictor);
306 
307  c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
308  if (c->idelta < 16)
309  c->idelta = 16;
310 
311  return nibble;
312 }
313 
315  int16_t sample)
316 {
317  int nibble, delta;
318 
319  if (!c->step) {
320  c->predictor = 0;
321  c->step = 127;
322  }
323 
324  delta = sample - c->predictor;
325 
326  nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
327 
328  c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
329  c->predictor = av_clip_int16(c->predictor);
330  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
331  c->step = av_clip(c->step, 127, 24576);
332 
333  return nibble;
334 }
335 
337  const int16_t *samples, uint8_t *dst,
338  ADPCMChannelStatus *c, int n, int stride)
339 {
340  //FIXME 6% faster if frontier is a compile-time constant
341  ADPCMEncodeContext *s = avctx->priv_data;
342  const int frontier = 1 << avctx->trellis;
343  const int version = avctx->codec->id;
344  TrellisPath *paths = s->paths, *p;
345  TrellisNode *node_buf = s->node_buf;
346  TrellisNode **nodep_buf = s->nodep_buf;
347  TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
348  TrellisNode **nodes_next = nodep_buf + frontier;
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));
352 
353  memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
354  nodes[0] = node_buf + frontier;
355  nodes[0]->ssd = 0;
356  nodes[0]->path = 0;
357  nodes[0]->step = c->step_index;
358  nodes[0]->sample1 = c->sample1;
359  nodes[0]->sample2 = c->sample2;
364  nodes[0]->sample1 = c->prev_sample;
366  nodes[0]->step = c->idelta;
368  if (c->step == 0) {
369  nodes[0]->step = 127;
370  nodes[0]->sample1 = 0;
371  } else {
372  nodes[0]->step = c->step;
373  nodes[0]->sample1 = c->predictor;
374  }
375  }
376 
377  for (i = 0; i < n; i++) {
378  TrellisNode *t = node_buf + frontier*(i&1);
379  TrellisNode **u;
380  int sample = samples[i * stride];
381  int heap_pos = 0;
382  memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
383  for (j = 0; j < frontier && nodes[j]; j++) {
384  // higher j have higher ssd already, so they're likely
385  // to yield a suboptimal next sample too
386  const int range = (j < frontier / 2) ? 1 : 0;
387  const int step = nodes[j]->step;
388  int nidx;
389  if (version == AV_CODEC_ID_ADPCM_MS) {
390  const int predictor = ((nodes[j]->sample1 * c->coeff1) +
391  (nodes[j]->sample2 * c->coeff2)) / 64;
392  const int div = (sample - predictor) / step;
393  const int nmin = av_clip(div-range, -8, 6);
394  const int nmax = av_clip(div+range, -7, 7);
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)\
399  int d;\
400  uint32_t ssd;\
401  int pos;\
402  TrellisNode *u;\
403  uint8_t *h;\
404  dec_sample = av_clip_int16(dec_sample);\
405  d = sample - dec_sample;\
406  ssd = nodes[j]->ssd + d*(unsigned)d;\
407  /* Check for wraparound, skip such samples completely. \
408  * Note, changing ssd to a 64 bit variable would be \
409  * simpler, avoiding this check, but it's slower on \
410  * x86 32 bit at the moment. */\
411  if (ssd < nodes[j]->ssd)\
412  goto next_##NAME;\
413  /* Collapse any two states with the same previous sample value. \
414  * One could also distinguish states by step and by 2nd to last
415  * sample, but the effects of that are negligible.
416  * Since nodes in the previous generation are iterated
417  * through a heap, they're roughly ordered from better to
418  * worse, but not strictly ordered. Therefore, an earlier
419  * node with the same sample value is better in most cases
420  * (and thus the current is skipped), but not strictly
421  * in all cases. Only skipping samples where ssd >=
422  * ssd of the earlier node with the same sample gives
423  * slightly worse quality, though, for some reason. */ \
424  h = &hash[(uint16_t) dec_sample];\
425  if (*h == generation)\
426  goto next_##NAME;\
427  if (heap_pos < frontier) {\
428  pos = heap_pos++;\
429  } else {\
430  /* Try to replace one of the leaf nodes with the new \
431  * one, but try a different slot each time. */\
432  pos = (frontier >> 1) +\
433  (heap_pos & ((frontier >> 1) - 1));\
434  if (ssd > nodes_next[pos]->ssd)\
435  goto next_##NAME;\
436  heap_pos++;\
437  }\
438  *h = generation;\
439  u = nodes_next[pos];\
440  if (!u) {\
441  av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
442  u = t++;\
443  nodes_next[pos] = u;\
444  u->path = pathn++;\
445  }\
446  u->ssd = ssd;\
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;\
452  /* Sift the newly inserted node up in the heap to \
453  * restore the heap property. */\
454  while (pos > 0) {\
455  int parent = (pos - 1) >> 1;\
456  if (nodes_next[parent]->ssd <= ssd)\
457  break;\
458  FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
459  pos = parent;\
460  }\
461  next_##NAME:;
462  STORE_NODE(ms, FFMAX(16,
463  (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
464  }
465  } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
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);\
474  if (nmin <= 0)\
475  nmin--; /* distinguish -0 from +0 */\
476  if (nmax < 0)\
477  nmax--;\
478  for (nidx = nmin; nidx <= nmax; nidx++) {\
479  const int nibble = nidx < 0 ? 7 - nidx : nidx;\
480  int dec_sample = predictor +\
481  (STEP_TABLE *\
482  ff_adpcm_yamaha_difflookup[nibble]) / 8;\
483  STORE_NODE(NAME, STEP_INDEX);\
484  }
486  av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
487  } else { //AV_CODEC_ID_ADPCM_YAMAHA
488  LOOP_NODES(yamaha, step,
489  av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
490  127, 24576));
491 #undef LOOP_NODES
492 #undef STORE_NODE
493  }
494  }
495 
496  u = nodes;
497  nodes = nodes_next;
498  nodes_next = u;
499 
500  generation++;
501  if (generation == 255) {
502  memset(hash, 0xff, 65536 * sizeof(*hash));
503  generation = 0;
504  }
505 
506  // prevent overflow
507  if (nodes[0]->ssd > (1 << 28)) {
508  for (j = 1; j < frontier && nodes[j]; j++)
509  nodes[j]->ssd -= nodes[0]->ssd;
510  nodes[0]->ssd = 0;
511  }
512 
513  // merge old paths to save memory
514  if (i == froze + FREEZE_INTERVAL) {
515  p = &paths[nodes[0]->path];
516  for (k = i; k > froze; k--) {
517  dst[k] = p->nibble;
518  p = &paths[p->prev];
519  }
520  froze = i;
521  pathn = 0;
522  // other nodes might use paths that don't coincide with the frozen one.
523  // checking which nodes do so is too slow, so just kill them all.
524  // this also slightly improves quality, but I don't know why.
525  memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
526  }
527  }
528 
529  p = &paths[nodes[0]->path];
530  for (i = n - 1; i > froze; i--) {
531  dst[i] = p->nibble;
532  p = &paths[p->prev];
533  }
534 
535  c->predictor = nodes[0]->sample1;
536  c->sample1 = nodes[0]->sample1;
537  c->sample2 = nodes[0]->sample2;
538  c->step_index = nodes[0]->step;
539  c->step = nodes[0]->step;
540  c->idelta = nodes[0]->step;
541 }
542 
543 #if CONFIG_ADPCM_ARGO_ENCODER
544 static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
545  int shift, int flag)
546 {
547  int nibble;
548 
549  if (flag)
550  nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
551  else
552  nibble = 4 * s - 4 * cs->sample1;
553 
554  return (nibble >> shift) & 0x0F;
555 }
556 
557 static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
558  const int16_t *samples, int nsamples,
559  int shift, int flag)
560 {
561  int64_t error = 0;
562 
563  if (pb) {
564  put_bits(pb, 4, shift - 2);
565  put_bits(pb, 1, 0);
566  put_bits(pb, 1, !!flag);
567  put_bits(pb, 2, 0);
568  }
569 
570  for (int n = 0; n < nsamples; n++) {
571  /* Compress the nibble, then expand it to see how much precision we've lost. */
572  int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
573  int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
574 
575  error += abs(samples[n] - sample);
576 
577  if (pb)
578  put_bits(pb, 4, nibble);
579  }
580 
581  return error;
582 }
583 #endif
584 
585 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
586  const AVFrame *frame, int *got_packet_ptr)
587 {
588  int st, pkt_size, ret;
589  const int16_t *samples;
590  const int16_t *const *samples_p;
591  uint8_t *dst;
592  ADPCMEncodeContext *c = avctx->priv_data;
593  int channels = avctx->ch_layout.nb_channels;
594 
595  samples = (const int16_t *)frame->data[0];
596  samples_p = (const int16_t *const *)frame->extended_data;
597  st = channels == 2;
598 
599  if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
603  pkt_size = (frame->nb_samples * channels + 1) / 2;
604  else
605  pkt_size = avctx->block_align;
606  if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
607  return ret;
608  dst = avpkt->data;
609 
610  switch(avctx->codec->id) {
611  CASE(ADPCM_IMA_WAV,
612  int blocks = (frame->nb_samples - 1) / 8;
613 
614  for (int ch = 0; ch < channels; ch++) {
615  ADPCMChannelStatus *status = &c->status[ch];
616  status->prev_sample = samples_p[ch][0];
617  /* status->step_index = 0;
618  XXX: not sure how to init the state machine */
619  bytestream_put_le16(&dst, status->prev_sample);
620  *dst++ = status->step_index;
621  *dst++ = 0; /* unknown */
622  }
623 
624  /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
625  if (avctx->trellis > 0) {
626  uint8_t *buf;
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],
632  blocks * 8, 1);
633  }
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);
639  }
640  }
641  av_free(buf);
642  } else {
643  for (int i = 0; i < blocks; i++) {
644  for (int ch = 0; ch < channels; ch++) {
645  ADPCMChannelStatus *status = &c->status[ch];
646  const int16_t *smp = &samples_p[ch][1 + i * 8];
647  for (int j = 0; j < 8; j += 2) {
648  uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
649  v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
650  *dst++ = v;
651  }
652  }
653  }
654  }
655  ) /* End of CASE */
656  CASE(ADPCM_IMA_QT,
657  PutBitContext pb;
658  init_put_bits(&pb, dst, pkt_size);
659 
660  for (int ch = 0; ch < channels; ch++) {
661  ADPCMChannelStatus *status = &c->status[ch];
662  put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
663  put_bits(&pb, 7, status->step_index);
664  if (avctx->trellis > 0) {
665  uint8_t buf[64];
666  adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
667  64, 1);
668  for (int i = 0; i < 64; i++)
669  put_bits(&pb, 4, buf[i ^ 1]);
670  status->prev_sample = status->predictor;
671  } else {
672  for (int i = 0; i < 64; i += 2) {
673  int t1, t2;
674  t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
675  t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
676  put_bits(&pb, 4, t2);
677  put_bits(&pb, 4, t1);
678  }
679  }
680  }
681 
682  flush_put_bits(&pb);
683  ) /* End of CASE */
684  CASE(ADPCM_IMA_SSI,
685  PutBitContext pb;
686  init_put_bits(&pb, dst, pkt_size);
687 
688  av_assert0(avctx->trellis == 0);
689 
690  for (int i = 0; i < frame->nb_samples; i++) {
691  for (int ch = 0; ch < channels; ch++) {
692  put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
693  }
694  }
695 
696  flush_put_bits(&pb);
697  ) /* End of CASE */
698  CASE(ADPCM_IMA_ALP,
699  PutBitContext pb;
700  init_put_bits(&pb, dst, pkt_size);
701 
702  av_assert0(avctx->trellis == 0);
703 
704  for (int n = frame->nb_samples / 2; n > 0; n--) {
705  for (int ch = 0; ch < channels; ch++) {
706  put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
707  put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
708  }
709  samples += channels;
710  }
711 
712  flush_put_bits(&pb);
713  ) /* End of CASE */
714  CASE(ADPCM_SWF,
715  const int n = frame->nb_samples - 1;
716  PutBitContext pb;
717  init_put_bits(&pb, dst, pkt_size);
718 
719  /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
720  av_assert0(n == 4095);
721 
722  // store AdpcmCodeSize
723  put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
724 
725  // init the encoder state
726  for (int i = 0; i < channels; i++) {
727  // clip step so it fits 6 bits
728  c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
729  put_sbits(&pb, 16, samples[i]);
730  put_bits(&pb, 6, c->status[i].step_index);
731  c->status[i].prev_sample = samples[i];
732  }
733 
734  if (avctx->trellis > 0) {
735  uint8_t buf[8190 /* = 2 * n */];
737  &c->status[0], n, channels);
738  if (channels == 2)
740  buf + n, &c->status[1], n,
741  channels);
742  for (int i = 0; i < n; i++) {
743  put_bits(&pb, 4, buf[i]);
744  if (channels == 2)
745  put_bits(&pb, 4, buf[n + i]);
746  }
747  } else {
748  for (int i = 1; i < frame->nb_samples; i++) {
749  put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
750  samples[channels * i]));
751  if (channels == 2)
752  put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
753  samples[2 * i + 1]));
754  }
755  }
756  flush_put_bits(&pb);
757  ) /* End of CASE */
758  CASE(ADPCM_MS,
759  for (int i = 0; i < channels; i++) {
760  int predictor = 0;
761  *dst++ = predictor;
762  c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
763  c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
764  }
765  for (int i = 0; i < channels; i++) {
766  if (c->status[i].idelta < 16)
767  c->status[i].idelta = 16;
768  bytestream_put_le16(&dst, c->status[i].idelta);
769  }
770  for (int i = 0; i < channels; i++)
771  c->status[i].sample2= *samples++;
772  for (int i = 0; i < channels; i++) {
773  c->status[i].sample1 = *samples++;
774  bytestream_put_le16(&dst, c->status[i].sample1);
775  }
776  for (int i = 0; i < channels; i++)
777  bytestream_put_le16(&dst, c->status[i].sample2);
778 
779  if (avctx->trellis > 0) {
780  const int n = avctx->block_align - 7 * channels;
781  uint8_t *buf = av_malloc(2 * n);
782  if (!buf)
783  return AVERROR(ENOMEM);
784  if (channels == 1) {
785  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
786  channels);
787  for (int i = 0; i < n; i += 2)
788  *dst++ = (buf[i] << 4) | buf[i + 1];
789  } else {
790  adpcm_compress_trellis(avctx, samples, buf,
791  &c->status[0], n, channels);
792  adpcm_compress_trellis(avctx, samples + 1, buf + n,
793  &c->status[1], n, channels);
794  for (int i = 0; i < n; i++)
795  *dst++ = (buf[i] << 4) | buf[n + i];
796  }
797  av_free(buf);
798  } else {
799  for (int i = 7 * channels; i < avctx->block_align; i++) {
800  int nibble;
801  nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
802  nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
803  *dst++ = nibble;
804  }
805  }
806  ) /* End of CASE */
807  CASE(ADPCM_YAMAHA,
808  int n = frame->nb_samples / 2;
809  if (avctx->trellis > 0) {
810  uint8_t *buf = av_malloc(2 * n * 2);
811  if (!buf)
812  return AVERROR(ENOMEM);
813  n *= 2;
814  if (channels == 1) {
815  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
816  channels);
817  for (int i = 0; i < n; i += 2)
818  *dst++ = buf[i] | (buf[i + 1] << 4);
819  } else {
820  adpcm_compress_trellis(avctx, samples, buf,
821  &c->status[0], n, channels);
822  adpcm_compress_trellis(avctx, samples + 1, buf + n,
823  &c->status[1], n, channels);
824  for (int i = 0; i < n; i++)
825  *dst++ = buf[i] | (buf[n + i] << 4);
826  }
827  av_free(buf);
828  } else
829  for (n *= channels; n > 0; n--) {
830  int nibble;
831  nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
832  nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
833  *dst++ = nibble;
834  }
835  ) /* End of CASE */
836  CASE(ADPCM_IMA_APM,
837  PutBitContext pb;
838  init_put_bits(&pb, dst, pkt_size);
839 
840  av_assert0(avctx->trellis == 0);
841 
842  for (int n = frame->nb_samples / 2; n > 0; n--) {
843  for (int ch = 0; ch < channels; ch++) {
844  put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
845  put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
846  }
847  samples += channels;
848  }
849 
850  flush_put_bits(&pb);
851  ) /* End of CASE */
852  CASE(ADPCM_IMA_AMV,
853  av_assert0(channels == 1);
854 
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);
860 
861  if (avctx->trellis > 0) {
862  const int n = frame->nb_samples >> 1;
863  uint8_t *buf = av_malloc(2 * n);
864 
865  if (!buf)
866  return AVERROR(ENOMEM);
867 
868  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, channels);
869  for (int i = 0; i < n; i++)
870  bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
871 
872  samples += 2 * n;
873  av_free(buf);
874  } else for (int n = frame->nb_samples >> 1; n > 0; n--) {
875  int nibble;
876  nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
877  nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
878  bytestream_put_byte(&dst, nibble);
879  }
880 
881  if (avctx->frame_size & 1) {
882  int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
883  bytestream_put_byte(&dst, nibble);
884  }
885  ) /* End of CASE */
886  CASE(ADPCM_ARGO,
887  PutBitContext pb;
888  init_put_bits(&pb, dst, pkt_size);
889 
890  av_assert0(frame->nb_samples == 32);
891 
892  for (int ch = 0; ch < channels; ch++) {
893  int64_t error = INT64_MAX, tmperr = INT64_MAX;
894  int shift = 2, flag = 0;
895  int saved1 = c->status[ch].sample1;
896  int saved2 = c->status[ch].sample2;
897 
898  /* Find the optimal coefficients, bail early if we find a perfect result. */
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],
904  frame->nb_samples, s, f);
905  if (tmperr < error) {
906  shift = s;
907  flag = f;
908  error = tmperr;
909  }
910  }
911  }
912 
913  /* Now actually do the encode. */
914  c->status[ch].sample1 = saved1;
915  c->status[ch].sample2 = saved2;
916  adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
917  frame->nb_samples, shift, flag);
918  }
919 
920  flush_put_bits(&pb);
921  ) /* End of CASE */
922  CASE(ADPCM_IMA_WS,
923  PutBitContext pb;
924  init_put_bits(&pb, dst, pkt_size);
925 
926  av_assert0(avctx->trellis == 0);
927  for (int n = frame->nb_samples / 2; n > 0; n--) {
928  /* stereo: 1 byte (2 samples) for left, 1 byte for right */
929  for (int ch = 0; ch < channels; ch++) {
930  int t1, t2;
931  t1 = adpcm_ima_compress_sample(&c->status[ch], *samples++);
932  t2 = adpcm_ima_compress_sample(&c->status[ch], samples[st]);
933  put_bits(&pb, 4, t2);
934  put_bits(&pb, 4, t1);
935  }
936  samples += channels;
937  }
938  flush_put_bits(&pb);
939  ) /* End of CASE */
940  default:
941  return AVERROR(EINVAL);
942  }
943 
944  *got_packet_ptr = 1;
945  return 0;
946 }
947 
948 static const enum AVSampleFormat sample_fmts[] = {
950 };
951 
952 static const enum AVSampleFormat sample_fmts_p[] = {
954 };
955 
956 static const AVChannelLayout ch_layouts_mono_stereo[] = {
959  { 0 },
960 };
961 
962 static const AVOption options[] = {
963  {
964  .name = "block_size",
965  .help = "set the block size",
966  .offset = offsetof(ADPCMEncodeContext, block_size),
967  .type = AV_OPT_TYPE_INT,
968  .default_val = {.i64 = 1024},
969  .min = 32,
970  .max = 8192, /* Is this a reasonable upper limit? */
972  },
973  { NULL }
974 };
975 
976 static const AVClass adpcm_encoder_class = {
977  .class_name = "ADPCM encoder",
978  .item_name = av_default_item_name,
979  .option = options,
980  .version = LIBAVUTIL_VERSION_INT,
981 };
982 
983 #define ADPCM_ENCODER_0(id_, name_, sample_fmts_, capabilities_, long_name_, ...)
984 #define ADPCM_ENCODER_1(id_, name_, sample_fmts_, capabilities_, long_name_, ...) \
985 const FFCodec ff_ ## name_ ## _encoder = { \
986  .p.name = #name_, \
987  CODEC_LONG_NAME(long_name_), \
988  .p.type = AVMEDIA_TYPE_AUDIO, \
989  .p.id = id_, \
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, \
998  __VA_ARGS__, \
999 };
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__)
1007 
1008 #define MONO_STEREO CODEC_CH_LAYOUTS_ARRAY(ch_layouts_mono_stereo)
1009 #define AVCLASS .p.priv_class = &adpcm_encoder_class
1010 
1011 ADPCM_ENCODER(ADPCM_ARGO, adpcm_argo, sample_fmts_p, 0, "ADPCM Argonaut Games", MONO_STEREO)
1012 ADPCM_ENCODER(ADPCM_IMA_AMV, adpcm_ima_amv, sample_fmts, 0, "ADPCM IMA AMV", CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO), CODEC_SAMPLERATES(22050), AVCLASS)
1013 ADPCM_ENCODER(ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM", MONO_STEREO, AVCLASS)
1014 ADPCM_ENCODER(ADPCM_IMA_ALP, adpcm_ima_alp, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA High Voltage Software ALP", MONO_STEREO, AVCLASS)
1015 ADPCM_ENCODER(ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, 0, "ADPCM IMA QuickTime", MONO_STEREO)
1016 ADPCM_ENCODER(ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive", MONO_STEREO, AVCLASS)
1017 ADPCM_ENCODER(ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0, "ADPCM IMA WAV", MONO_STEREO, AVCLASS)
1018 ADPCM_ENCODER(ADPCM_IMA_WS, adpcm_ima_ws, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Westwood", MONO_STEREO, AVCLASS)
1019 ADPCM_ENCODER(ADPCM_MS, adpcm_ms, sample_fmts, 0, "ADPCM Microsoft", MONO_STEREO, AVCLASS)
1020 ADPCM_ENCODER(ADPCM_SWF, adpcm_swf, sample_fmts, 0, "ADPCM Shockwave Flash", MONO_STEREO, CODEC_SAMPLERATES(11025, 22050, 44100))
1021 ADPCM_ENCODER(ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, 0, "ADPCM Yamaha", MONO_STEREO, AVCLASS)
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:1068
adpcm_yamaha_compress_sample
static uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:314
AV_CODEC_ID_ADPCM_MS
@ AV_CODEC_ID_ADPCM_MS
Definition: codec_id.h:384
AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition: codec_id.h:378
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
flag
int flag
Definition: cpu.c:40
LOOP_NODES
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:395
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1040
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:933
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:124
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:961
TrellisNode::path
int path
Definition: adpcmenc.c:63
av_cold
#define av_cold
Definition: attributes.h:119
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:291
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:466
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:154
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
u
#define u(width, name, range_min, range_max)
Definition: cbs_apv.c:68
AVPacket::data
uint8_t * data
Definition: packet.h:603
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:329
hash
static uint8_t hash[HASH_SIZE]
Definition: movenc.c:58
ADPCMEncodeContext::nodep_buf
TrellisNode ** nodep_buf
Definition: adpcmenc.c:76
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:452
STORE_NODE
#define STORE_NODE(NAME, STEP_INDEX)
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1055
AVCLASS
#define AVCLASS
Definition: adpcmenc.c:994
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:541
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:210
ch_layouts_mono_stereo
static const AVChannelLayout ch_layouts_mono_stereo[]
Definition: adpcmenc.c:941
options
static const AVOption options[]
Definition: adpcmenc.c:947
ADPCMChannelStatus::sample1
int sample1
Definition: adpcm.h:39
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:527
adpcm_data.h
MONO_STEREO
#define MONO_STEREO
Definition: adpcmenc.c:993
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:42
channels
channels
Definition: aptx.h:31
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.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:336
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:453
ADPCM_ENCODER
#define ADPCM_ENCODER(codec, name, sample_fmts, capabilities, long_name,...)
Definition: adpcmenc.c:989
if
if(ret)
Definition: filter_design.txt:179
TrellisPath
Definition: aaccoder.c:295
CODEC_CH_LAYOUTS
#define CODEC_CH_LAYOUTS(...)
Definition: codec_internal.h:380
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
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:392
AV_CODEC_ID_ADPCM_IMA_WS
@ AV_CODEC_ID_ADPCM_IMA_WS
Definition: codec_id.h:382
bias
static int bias(int x, int c)
Definition: vqcdec.c:115
av_unreachable
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition: avassert.h:116
AV_CODEC_ID_ADPCM_ARGO
@ AV_CODEC_ID_ADPCM_ARGO
Definition: codec_id.h:420
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:493
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:242
options
Definition: swscale.c:50
AV_CODEC_ID_ADPCM_IMA_AMV
@ AV_CODEC_ID_ADPCM_IMA_AMV
Definition: codec_id.h:397
abs
#define abs(x)
Definition: cuda_runtime.h:35
AVCodecContext::trellis
int trellis
trellis RD quantization
Definition: avcodec.h:1323
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:229
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:319
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:87
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
AV_CODEC_ID_ADPCM_IMA_ALP
@ AV_CODEC_ID_ADPCM_IMA_ALP
Definition: codec_id.h:424
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
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:391
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:313
predictor
static void predictor(uint8_t *src, ptrdiff_t size)
Definition: exrenc.c:170
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:186
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1564
ff_adpcm_AdaptCoeff1
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:60
av_malloc
#define av_malloc(s)
Definition: ops_asmgen.c:44
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
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:526
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:216
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_CODEC_ID_ADPCM_IMA_APM
@ AV_CODEC_ID_ADPCM_IMA_APM
Definition: codec_id.h:423
TrellisPath::prev
int prev
Definition: aaccoder.c:297
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
ff_adpcm_argo_expand_nibble
int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
Definition: adpcm.c:969
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:570
avcodec.h
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:1075
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:81
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:265
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:937
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:443
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:105
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
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
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:153
AV_CODEC_ID_ADPCM_IMA_SSI
@ AV_CODEC_ID_ADPCM_IMA_SSI
Definition: codec_id.h:421
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:394
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:580
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:470
AV_CODEC_ID_ADPCM_IMA_WAV
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition: codec_id.h:379
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
adpcm_ima_qt_compress_sample
static uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:248
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
stride
#define stride
Definition: h264pred_template.c:536
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:285
CODEC_SAMPLERATES
#define CODEC_SAMPLERATES(...)
Definition: codec_internal.h:383
adpcm_encode_close
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
Definition: adpcmenc.c:204