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