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 "libavutil/opt.h"
26 
27 #include "avcodec.h"
28 #include "put_bits.h"
29 #include "bytestream.h"
30 #include "adpcm.h"
31 #include "adpcm_data.h"
32 #include "internal.h"
33 
34 /**
35  * @file
36  * ADPCM encoders
37  * See ADPCM decoder reference documents for codec information.
38  */
39 
40 typedef struct TrellisPath {
41  int nibble;
42  int prev;
43 } TrellisPath;
44 
45 typedef struct TrellisNode {
46  uint32_t ssd;
47  int path;
48  int sample1;
49  int sample2;
50  int step;
51 } TrellisNode;
52 
53 typedef struct ADPCMEncodeContext {
54  AVClass *class;
56 
63 
64 #define FREEZE_INTERVAL 128
65 
67 {
68  ADPCMEncodeContext *s = avctx->priv_data;
69  uint8_t *extradata;
70  int i;
71 
72  if (avctx->channels > 2) {
73  av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
74  return AVERROR(EINVAL);
75  }
76 
77  if (s->block_size & (s->block_size - 1)) {
78  av_log(avctx, AV_LOG_ERROR, "block size must be power of 2\n");
79  return AVERROR(EINVAL);
80  }
81 
82  if (avctx->trellis) {
83  int frontier, max_paths;
84 
85  if ((unsigned)avctx->trellis > 16U) {
86  av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
87  return AVERROR(EINVAL);
88  }
89 
90  if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
91  avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
92  avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO) {
93  /*
94  * The current trellis implementation doesn't work for extended
95  * runs of samples without periodic resets. Disallow it.
96  */
97  av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
98  return AVERROR_PATCHWELCOME;
99  }
100 
101  frontier = 1 << avctx->trellis;
102  max_paths = frontier * FREEZE_INTERVAL;
103  if (!FF_ALLOC_TYPED_ARRAY(s->paths, max_paths) ||
104  !FF_ALLOC_TYPED_ARRAY(s->node_buf, 2 * frontier) ||
105  !FF_ALLOC_TYPED_ARRAY(s->nodep_buf, 2 * frontier) ||
107  return AVERROR(ENOMEM);
108  }
109 
111 
112  switch (avctx->codec->id) {
114  /* each 16 bits sample gives one nibble
115  and we have 4 bytes per channel overhead */
116  avctx->frame_size = (s->block_size - 4 * avctx->channels) * 8 /
117  (4 * avctx->channels) + 1;
118  /* seems frame_size isn't taken into account...
119  have to buffer the samples :-( */
120  avctx->block_align = s->block_size;
121  avctx->bits_per_coded_sample = 4;
122  break;
124  avctx->frame_size = 64;
125  avctx->block_align = 34 * avctx->channels;
126  break;
128  /* each 16 bits sample gives one nibble
129  and we have 7 bytes per channel overhead */
130  avctx->frame_size = (s->block_size - 7 * avctx->channels) * 2 / avctx->channels + 2;
131  avctx->bits_per_coded_sample = 4;
132  avctx->block_align = s->block_size;
134  return AVERROR(ENOMEM);
135  avctx->extradata_size = 32;
136  extradata = avctx->extradata;
137  bytestream_put_le16(&extradata, avctx->frame_size);
138  bytestream_put_le16(&extradata, 7); /* wNumCoef */
139  for (i = 0; i < 7; i++) {
140  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
141  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
142  }
143  break;
145  avctx->frame_size = s->block_size * 2 / avctx->channels;
146  avctx->block_align = s->block_size;
147  break;
149  if (avctx->sample_rate != 11025 &&
150  avctx->sample_rate != 22050 &&
151  avctx->sample_rate != 44100) {
152  av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
153  "22050 or 44100\n");
154  return AVERROR(EINVAL);
155  }
156  avctx->frame_size = (s->block_size / 2) * (avctx->sample_rate / 11025);
157  avctx->block_align = (2 + avctx->channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
158  break;
160  avctx->frame_size = s->block_size * 2 / avctx->channels;
161  avctx->block_align = s->block_size;
162  break;
164  avctx->frame_size = s->block_size * 2 / avctx->channels;
165  avctx->block_align = s->block_size;
166 
167  if (!(avctx->extradata = av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
168  return AVERROR(ENOMEM);
169  avctx->extradata_size = 28;
170  break;
172  avctx->frame_size = 32;
173  avctx->block_align = 17 * avctx->channels;
174  break;
175  default:
176  return AVERROR(EINVAL);
177  }
178 
179  return 0;
180 }
181 
183 {
184  ADPCMEncodeContext *s = avctx->priv_data;
185  av_freep(&s->paths);
186  av_freep(&s->node_buf);
187  av_freep(&s->nodep_buf);
188  av_freep(&s->trellis_hash);
189 
190  return 0;
191 }
192 
193 
195  int16_t sample)
196 {
197  int delta = sample - c->prev_sample;
198  int nibble = FFMIN(7, abs(delta) * 4 /
199  ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
202  c->prev_sample = av_clip_int16(c->prev_sample);
203  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
204  return nibble;
205 }
206 
208  int16_t sample)
209 {
210  int delta = sample - c->prev_sample;
212  int nibble = 8*(delta < 0);
213 
214  delta= abs(delta);
215  diff = delta + (step >> 3);
216 
217  if (delta >= step) {
218  nibble |= 4;
219  delta -= step;
220  }
221  step >>= 1;
222  if (delta >= step) {
223  nibble |= 2;
224  delta -= step;
225  }
226  step >>= 1;
227  if (delta >= step) {
228  nibble |= 1;
229  delta -= step;
230  }
231  diff -= delta;
232 
233  if (nibble & 8)
234  c->prev_sample -= diff;
235  else
236  c->prev_sample += diff;
237 
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 
241  return nibble;
242 }
243 
245  int16_t sample)
246 {
247  int predictor, nibble, bias;
248 
249  predictor = (((c->sample1) * (c->coeff1)) +
250  (( c->sample2) * (c->coeff2))) / 64;
251 
252  nibble = sample - predictor;
253  if (nibble >= 0)
254  bias = c->idelta / 2;
255  else
256  bias = -c->idelta / 2;
257 
258  nibble = (nibble + bias) / c->idelta;
259  nibble = av_clip_intp2(nibble, 3) & 0x0F;
260 
261  predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
262 
263  c->sample2 = c->sample1;
264  c->sample1 = av_clip_int16(predictor);
265 
266  c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
267  if (c->idelta < 16)
268  c->idelta = 16;
269 
270  return nibble;
271 }
272 
274  int16_t sample)
275 {
276  int nibble, delta;
277 
278  if (!c->step) {
279  c->predictor = 0;
280  c->step = 127;
281  }
282 
283  delta = sample - c->predictor;
284 
285  nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
286 
287  c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
288  c->predictor = av_clip_int16(c->predictor);
289  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
290  c->step = av_clip(c->step, 127, 24576);
291 
292  return nibble;
293 }
294 
296  const int16_t *samples, uint8_t *dst,
297  ADPCMChannelStatus *c, int n, int stride)
298 {
299  //FIXME 6% faster if frontier is a compile-time constant
300  ADPCMEncodeContext *s = avctx->priv_data;
301  const int frontier = 1 << avctx->trellis;
302  const int version = avctx->codec->id;
303  TrellisPath *paths = s->paths, *p;
304  TrellisNode *node_buf = s->node_buf;
305  TrellisNode **nodep_buf = s->nodep_buf;
306  TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
307  TrellisNode **nodes_next = nodep_buf + frontier;
308  int pathn = 0, froze = -1, i, j, k, generation = 0;
309  uint8_t *hash = s->trellis_hash;
310  memset(hash, 0xff, 65536 * sizeof(*hash));
311 
312  memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
313  nodes[0] = node_buf + frontier;
314  nodes[0]->ssd = 0;
315  nodes[0]->path = 0;
316  nodes[0]->step = c->step_index;
317  nodes[0]->sample1 = c->sample1;
318  nodes[0]->sample2 = c->sample2;
319  if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
320  version == AV_CODEC_ID_ADPCM_IMA_QT ||
321  version == AV_CODEC_ID_ADPCM_SWF)
322  nodes[0]->sample1 = c->prev_sample;
323  if (version == AV_CODEC_ID_ADPCM_MS)
324  nodes[0]->step = c->idelta;
325  if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
326  if (c->step == 0) {
327  nodes[0]->step = 127;
328  nodes[0]->sample1 = 0;
329  } else {
330  nodes[0]->step = c->step;
331  nodes[0]->sample1 = c->predictor;
332  }
333  }
334 
335  for (i = 0; i < n; i++) {
336  TrellisNode *t = node_buf + frontier*(i&1);
337  TrellisNode **u;
338  int sample = samples[i * stride];
339  int heap_pos = 0;
340  memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
341  for (j = 0; j < frontier && nodes[j]; j++) {
342  // higher j have higher ssd already, so they're likely
343  // to yield a suboptimal next sample too
344  const int range = (j < frontier / 2) ? 1 : 0;
345  const int step = nodes[j]->step;
346  int nidx;
347  if (version == AV_CODEC_ID_ADPCM_MS) {
348  const int predictor = ((nodes[j]->sample1 * c->coeff1) +
349  (nodes[j]->sample2 * c->coeff2)) / 64;
350  const int div = (sample - predictor) / step;
351  const int nmin = av_clip(div-range, -8, 6);
352  const int nmax = av_clip(div+range, -7, 7);
353  for (nidx = nmin; nidx <= nmax; nidx++) {
354  const int nibble = nidx & 0xf;
355  int dec_sample = predictor + nidx * step;
356 #define STORE_NODE(NAME, STEP_INDEX)\
357  int d;\
358  uint32_t ssd;\
359  int pos;\
360  TrellisNode *u;\
361  uint8_t *h;\
362  dec_sample = av_clip_int16(dec_sample);\
363  d = sample - dec_sample;\
364  ssd = nodes[j]->ssd + d*(unsigned)d;\
365  /* Check for wraparound, skip such samples completely. \
366  * Note, changing ssd to a 64 bit variable would be \
367  * simpler, avoiding this check, but it's slower on \
368  * x86 32 bit at the moment. */\
369  if (ssd < nodes[j]->ssd)\
370  goto next_##NAME;\
371  /* Collapse any two states with the same previous sample value. \
372  * One could also distinguish states by step and by 2nd to last
373  * sample, but the effects of that are negligible.
374  * Since nodes in the previous generation are iterated
375  * through a heap, they're roughly ordered from better to
376  * worse, but not strictly ordered. Therefore, an earlier
377  * node with the same sample value is better in most cases
378  * (and thus the current is skipped), but not strictly
379  * in all cases. Only skipping samples where ssd >=
380  * ssd of the earlier node with the same sample gives
381  * slightly worse quality, though, for some reason. */ \
382  h = &hash[(uint16_t) dec_sample];\
383  if (*h == generation)\
384  goto next_##NAME;\
385  if (heap_pos < frontier) {\
386  pos = heap_pos++;\
387  } else {\
388  /* Try to replace one of the leaf nodes with the new \
389  * one, but try a different slot each time. */\
390  pos = (frontier >> 1) +\
391  (heap_pos & ((frontier >> 1) - 1));\
392  if (ssd > nodes_next[pos]->ssd)\
393  goto next_##NAME;\
394  heap_pos++;\
395  }\
396  *h = generation;\
397  u = nodes_next[pos];\
398  if (!u) {\
399  av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
400  u = t++;\
401  nodes_next[pos] = u;\
402  u->path = pathn++;\
403  }\
404  u->ssd = ssd;\
405  u->step = STEP_INDEX;\
406  u->sample2 = nodes[j]->sample1;\
407  u->sample1 = dec_sample;\
408  paths[u->path].nibble = nibble;\
409  paths[u->path].prev = nodes[j]->path;\
410  /* Sift the newly inserted node up in the heap to \
411  * restore the heap property. */\
412  while (pos > 0) {\
413  int parent = (pos - 1) >> 1;\
414  if (nodes_next[parent]->ssd <= ssd)\
415  break;\
416  FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
417  pos = parent;\
418  }\
419  next_##NAME:;
420  STORE_NODE(ms, FFMAX(16,
421  (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
422  }
423  } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
424  version == AV_CODEC_ID_ADPCM_IMA_QT ||
425  version == AV_CODEC_ID_ADPCM_SWF) {
426 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
427  const int predictor = nodes[j]->sample1;\
428  const int div = (sample - predictor) * 4 / STEP_TABLE;\
429  int nmin = av_clip(div - range, -7, 6);\
430  int nmax = av_clip(div + range, -6, 7);\
431  if (nmin <= 0)\
432  nmin--; /* distinguish -0 from +0 */\
433  if (nmax < 0)\
434  nmax--;\
435  for (nidx = nmin; nidx <= nmax; nidx++) {\
436  const int nibble = nidx < 0 ? 7 - nidx : nidx;\
437  int dec_sample = predictor +\
438  (STEP_TABLE *\
439  ff_adpcm_yamaha_difflookup[nibble]) / 8;\
440  STORE_NODE(NAME, STEP_INDEX);\
441  }
443  av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
444  } else { //AV_CODEC_ID_ADPCM_YAMAHA
445  LOOP_NODES(yamaha, step,
446  av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
447  127, 24576));
448 #undef LOOP_NODES
449 #undef STORE_NODE
450  }
451  }
452 
453  u = nodes;
454  nodes = nodes_next;
455  nodes_next = u;
456 
457  generation++;
458  if (generation == 255) {
459  memset(hash, 0xff, 65536 * sizeof(*hash));
460  generation = 0;
461  }
462 
463  // prevent overflow
464  if (nodes[0]->ssd > (1 << 28)) {
465  for (j = 1; j < frontier && nodes[j]; j++)
466  nodes[j]->ssd -= nodes[0]->ssd;
467  nodes[0]->ssd = 0;
468  }
469 
470  // merge old paths to save memory
471  if (i == froze + FREEZE_INTERVAL) {
472  p = &paths[nodes[0]->path];
473  for (k = i; k > froze; k--) {
474  dst[k] = p->nibble;
475  p = &paths[p->prev];
476  }
477  froze = i;
478  pathn = 0;
479  // other nodes might use paths that don't coincide with the frozen one.
480  // checking which nodes do so is too slow, so just kill them all.
481  // this also slightly improves quality, but I don't know why.
482  memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
483  }
484  }
485 
486  p = &paths[nodes[0]->path];
487  for (i = n - 1; i > froze; i--) {
488  dst[i] = p->nibble;
489  p = &paths[p->prev];
490  }
491 
492  c->predictor = nodes[0]->sample1;
493  c->sample1 = nodes[0]->sample1;
494  c->sample2 = nodes[0]->sample2;
495  c->step_index = nodes[0]->step;
496  c->step = nodes[0]->step;
497  c->idelta = nodes[0]->step;
498 }
499 
500 static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
501  int shift, int flag)
502 {
503  int nibble;
504 
505  if (flag)
506  nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
507  else
508  nibble = 4 * s - 4 * cs->sample1;
509 
510  return (nibble >> shift) & 0x0F;
511 }
512 
514  const int16_t *samples, int nsamples,
515  int shift, int flag)
516 {
517  int64_t error = 0;
518 
519  if (pb) {
520  put_bits(pb, 4, shift - 2);
521  put_bits(pb, 1, 0);
522  put_bits(pb, 1, !!flag);
523  put_bits(pb, 2, 0);
524  }
525 
526  for (int n = 0; n < nsamples; n++) {
527  /* Compress the nibble, then expand it to see how much precision we've lost. */
528  int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
529  int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
530 
531  error += abs(samples[n] - sample);
532 
533  if (pb)
534  put_bits(pb, 4, nibble);
535  }
536 
537  return error;
538 }
539 
540 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
541  const AVFrame *frame, int *got_packet_ptr)
542 {
543  int n, i, ch, st, pkt_size, ret;
544  const int16_t *samples;
545  int16_t **samples_p;
546  uint8_t *dst;
547  ADPCMEncodeContext *c = avctx->priv_data;
548  uint8_t *buf;
549 
550  samples = (const int16_t *)frame->data[0];
551  samples_p = (int16_t **)frame->extended_data;
552  st = avctx->channels == 2;
553 
554  if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
556  pkt_size = (frame->nb_samples * avctx->channels) / 2;
557  else
558  pkt_size = avctx->block_align;
559  if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
560  return ret;
561  dst = avpkt->data;
562 
563  switch(avctx->codec->id) {
565  {
566  int blocks, j;
567 
568  blocks = (frame->nb_samples - 1) / 8;
569 
570  for (ch = 0; ch < avctx->channels; ch++) {
571  ADPCMChannelStatus *status = &c->status[ch];
572  status->prev_sample = samples_p[ch][0];
573  /* status->step_index = 0;
574  XXX: not sure how to init the state machine */
575  bytestream_put_le16(&dst, status->prev_sample);
576  *dst++ = status->step_index;
577  *dst++ = 0; /* unknown */
578  }
579 
580  /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
581  if (avctx->trellis > 0) {
582  if (!FF_ALLOC_TYPED_ARRAY(buf, avctx->channels * blocks * 8))
583  return AVERROR(ENOMEM);
584  for (ch = 0; ch < avctx->channels; ch++) {
585  adpcm_compress_trellis(avctx, &samples_p[ch][1],
586  buf + ch * blocks * 8, &c->status[ch],
587  blocks * 8, 1);
588  }
589  for (i = 0; i < blocks; i++) {
590  for (ch = 0; ch < avctx->channels; ch++) {
591  uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
592  for (j = 0; j < 8; j += 2)
593  *dst++ = buf1[j] | (buf1[j + 1] << 4);
594  }
595  }
596  av_free(buf);
597  } else {
598  for (i = 0; i < blocks; i++) {
599  for (ch = 0; ch < avctx->channels; ch++) {
600  ADPCMChannelStatus *status = &c->status[ch];
601  const int16_t *smp = &samples_p[ch][1 + i * 8];
602  for (j = 0; j < 8; j += 2) {
603  uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
604  v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
605  *dst++ = v;
606  }
607  }
608  }
609  }
610  break;
611  }
613  {
614  PutBitContext pb;
615  init_put_bits(&pb, dst, pkt_size);
616 
617  for (ch = 0; ch < avctx->channels; ch++) {
618  ADPCMChannelStatus *status = &c->status[ch];
619  put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
620  put_bits(&pb, 7, status->step_index);
621  if (avctx->trellis > 0) {
622  uint8_t buf[64];
623  adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
624  64, 1);
625  for (i = 0; i < 64; i++)
626  put_bits(&pb, 4, buf[i ^ 1]);
627  status->prev_sample = status->predictor;
628  } else {
629  for (i = 0; i < 64; i += 2) {
630  int t1, t2;
631  t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
632  t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
633  put_bits(&pb, 4, t2);
634  put_bits(&pb, 4, t1);
635  }
636  }
637  }
638 
639  flush_put_bits(&pb);
640  break;
641  }
643  {
644  PutBitContext pb;
645  init_put_bits(&pb, dst, pkt_size);
646 
647  av_assert0(avctx->trellis == 0);
648 
649  for (i = 0; i < frame->nb_samples; i++) {
650  for (ch = 0; ch < avctx->channels; ch++) {
651  put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
652  }
653  }
654 
655  flush_put_bits(&pb);
656  break;
657  }
659  {
660  PutBitContext pb;
661  init_put_bits(&pb, dst, pkt_size);
662 
663  n = frame->nb_samples - 1;
664 
665  // store AdpcmCodeSize
666  put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
667 
668  // init the encoder state
669  for (i = 0; i < avctx->channels; i++) {
670  // clip step so it fits 6 bits
671  c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
672  put_sbits(&pb, 16, samples[i]);
673  put_bits(&pb, 6, c->status[i].step_index);
674  c->status[i].prev_sample = samples[i];
675  }
676 
677  if (avctx->trellis > 0) {
678  if (!(buf = av_malloc(2 * n)))
679  return AVERROR(ENOMEM);
680  adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
681  &c->status[0], n, avctx->channels);
682  if (avctx->channels == 2)
683  adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
684  buf + n, &c->status[1], n,
685  avctx->channels);
686  for (i = 0; i < n; i++) {
687  put_bits(&pb, 4, buf[i]);
688  if (avctx->channels == 2)
689  put_bits(&pb, 4, buf[n + i]);
690  }
691  av_free(buf);
692  } else {
693  for (i = 1; i < frame->nb_samples; i++) {
695  samples[avctx->channels * i]));
696  if (avctx->channels == 2)
698  samples[2 * i + 1]));
699  }
700  }
701  flush_put_bits(&pb);
702  break;
703  }
705  for (i = 0; i < avctx->channels; i++) {
706  int predictor = 0;
707  *dst++ = predictor;
708  c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
709  c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
710  }
711  for (i = 0; i < avctx->channels; i++) {
712  if (c->status[i].idelta < 16)
713  c->status[i].idelta = 16;
714  bytestream_put_le16(&dst, c->status[i].idelta);
715  }
716  for (i = 0; i < avctx->channels; i++)
717  c->status[i].sample2= *samples++;
718  for (i = 0; i < avctx->channels; i++) {
719  c->status[i].sample1 = *samples++;
720  bytestream_put_le16(&dst, c->status[i].sample1);
721  }
722  for (i = 0; i < avctx->channels; i++)
723  bytestream_put_le16(&dst, c->status[i].sample2);
724 
725  if (avctx->trellis > 0) {
726  n = avctx->block_align - 7 * avctx->channels;
727  if (!(buf = av_malloc(2 * n)))
728  return AVERROR(ENOMEM);
729  if (avctx->channels == 1) {
730  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
731  avctx->channels);
732  for (i = 0; i < n; i += 2)
733  *dst++ = (buf[i] << 4) | buf[i + 1];
734  } else {
735  adpcm_compress_trellis(avctx, samples, buf,
736  &c->status[0], n, avctx->channels);
737  adpcm_compress_trellis(avctx, samples + 1, buf + n,
738  &c->status[1], n, avctx->channels);
739  for (i = 0; i < n; i++)
740  *dst++ = (buf[i] << 4) | buf[n + i];
741  }
742  av_free(buf);
743  } else {
744  for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
745  int nibble;
746  nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
747  nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
748  *dst++ = nibble;
749  }
750  }
751  break;
753  n = frame->nb_samples / 2;
754  if (avctx->trellis > 0) {
755  if (!(buf = av_malloc(2 * n * 2)))
756  return AVERROR(ENOMEM);
757  n *= 2;
758  if (avctx->channels == 1) {
759  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
760  avctx->channels);
761  for (i = 0; i < n; i += 2)
762  *dst++ = buf[i] | (buf[i + 1] << 4);
763  } else {
764  adpcm_compress_trellis(avctx, samples, buf,
765  &c->status[0], n, avctx->channels);
766  adpcm_compress_trellis(avctx, samples + 1, buf + n,
767  &c->status[1], n, avctx->channels);
768  for (i = 0; i < n; i++)
769  *dst++ = buf[i] | (buf[n + i] << 4);
770  }
771  av_free(buf);
772  } else
773  for (n *= avctx->channels; n > 0; n--) {
774  int nibble;
775  nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
776  nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
777  *dst++ = nibble;
778  }
779  break;
781  {
782  PutBitContext pb;
783  init_put_bits(&pb, dst, pkt_size);
784 
785  av_assert0(avctx->trellis == 0);
786 
787  for (n = frame->nb_samples / 2; n > 0; n--) {
788  for (ch = 0; ch < avctx->channels; ch++) {
789  put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
790  put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
791  }
792  samples += avctx->channels;
793  }
794 
795  flush_put_bits(&pb);
796  break;
797  }
799  {
800  PutBitContext pb;
801  init_put_bits(&pb, dst, pkt_size);
802 
803  av_assert0(frame->nb_samples == 32);
804 
805  for (ch = 0; ch < avctx->channels; ch++) {
806  int64_t error = INT64_MAX, tmperr = INT64_MAX;
807  int shift = 2, flag = 0;
808  int saved1 = c->status[ch].sample1;
809  int saved2 = c->status[ch].sample2;
810 
811  /* Find the optimal coefficients, bail early if we find a perfect result. */
812  for (int s = 2; s < 18 && tmperr != 0; s++) {
813  for (int f = 0; f < 2 && tmperr != 0; f++) {
814  c->status[ch].sample1 = saved1;
815  c->status[ch].sample2 = saved2;
816  tmperr = adpcm_argo_compress_block(c->status + ch, NULL, samples_p[ch],
817  frame->nb_samples, s, f);
818  if (tmperr < error) {
819  shift = s;
820  flag = f;
821  error = tmperr;
822  }
823  }
824  }
825 
826  /* Now actually do the encode. */
827  c->status[ch].sample1 = saved1;
828  c->status[ch].sample2 = saved2;
829  adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
830  frame->nb_samples, shift, flag);
831  }
832 
833  flush_put_bits(&pb);
834  break;
835  }
836  default:
837  return AVERROR(EINVAL);
838  }
839 
840  avpkt->size = pkt_size;
841  *got_packet_ptr = 1;
842  return 0;
843 }
844 
845 static const enum AVSampleFormat sample_fmts[] = {
847 };
848 
849 static const enum AVSampleFormat sample_fmts_p[] = {
851 };
852 
853 static const AVOption options[] = {
854  {
855  .name = "block_size",
856  .help = "set the block size",
857  .offset = offsetof(ADPCMEncodeContext, block_size),
858  .type = AV_OPT_TYPE_INT,
859  .default_val = {.i64 = 1024},
860  .min = 32,
861  .max = 8192, /* Is this a reasonable upper limit? */
863  },
864  { NULL }
865 };
866 
867 static const AVClass adpcm_encoder_class = {
868  .class_name = "ADPCM Encoder",
869  .item_name = av_default_item_name,
870  .option = options,
871  .version = LIBAVUTIL_VERSION_INT,
872 };
873 
874 #define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_) \
875 AVCodec ff_ ## name_ ## _encoder = { \
876  .name = #name_, \
877  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
878  .type = AVMEDIA_TYPE_AUDIO, \
879  .id = id_, \
880  .priv_data_size = sizeof(ADPCMEncodeContext), \
881  .init = adpcm_encode_init, \
882  .encode2 = adpcm_encode_frame, \
883  .close = adpcm_encode_close, \
884  .sample_fmts = sample_fmts_, \
885  .capabilities = capabilities_, \
886  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
887  .priv_class = &adpcm_encoder_class, \
888 }
889 
890 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_ARGO, adpcm_argo, sample_fmts_p, 0, "ADPCM Argonaut Games");
891 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM");
892 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, 0, "ADPCM IMA QuickTime");
893 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive");
894 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0, "ADPCM IMA WAV");
895 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, sample_fmts, 0, "ADPCM Microsoft");
896 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, sample_fmts, 0, "ADPCM Shockwave Flash");
897 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, 0, "ADPCM Yamaha");
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:535
#define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_)
Definition: adpcmenc.c:874
int sample1
Definition: adpcmenc.c:48
int path
Definition: adpcmenc.c:47
version
Definition: libkvazaar.c:317
static int shift(int a, int b)
Definition: sonic.c:82
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
Definition: adpcmenc.c:66
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
AVOption.
Definition: opt.h:248
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:258
static const AVOption options[]
Definition: adpcmenc.c:853
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
static uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:244
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
#define ima
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:280
int size
Definition: packet.h:364
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
static uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:207
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
Definition: internal.h:140
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
Definition: adpcmenc.c:182
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
static void error(const char *err)
#define sample
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1223
uint8_t * trellis_hash
Definition: adpcmenc.c:61
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:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:273
const char * name
Definition: opt.h:249
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:90
uint8_t
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
float delta
AVOptions.
#define f(width, name)
Definition: cbs_vp9.c:255
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
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:262
ADPCM tables.
uint8_t * data
Definition: packet.h:363
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1750
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:278
uint8_t hash[HASH_SIZE]
Definition: movenc.c:57
#define U(x)
Definition: vp56_arith.h:37
uint32_t ssd
Definition: adpcmenc.c:46
enum AVCodecID id
Definition: codec.h:204
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1572
ADPCM encoder/decoder common header.
int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
Definition: adpcm.c:695
#define STORE_NODE(NAME, STEP_INDEX)
const int16_t ff_adpcm_step_table[89]
This is the step table.
Definition: adpcm_data.c:61
#define t1
Definition: regdef.h:29
#define FFMAX(a, b)
Definition: common.h:94
const int8_t ff_adpcm_index_table[16]
Definition: adpcm_data.c:40
#define FREEZE_INTERVAL
Definition: adpcmenc.c:64
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:80
static uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:194
#define FFMIN(a, b)
Definition: common.h:96
TrellisNode ** nodep_buf
Definition: adpcmenc.c:60
const int8_t ff_adpcm_AdaptCoeff2[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:95
static void adpcm_compress_trellis(AVCodecContext *avctx, const int16_t *samples, uint8_t *dst, ADPCMChannelStatus *c, int n, int stride)
Definition: adpcmenc.c:295
static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: adpcmenc.c:540
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
#define s(width, name)
Definition: cbs_vp9.c:257
TrellisPath * paths
Definition: adpcmenc.c:58
int sample2
Definition: adpcmenc.c:49
if(ret)
TrellisNode * node_buf
Definition: adpcmenc.c:59
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1206
const int16_t ff_adpcm_AdaptationTable[]
Definition: adpcm_data.c:84
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
enum AVCodecID codec_id
Definition: avcodec.h:536
int sample_rate
samples per second
Definition: avcodec.h:1186
#define abs(x)
Definition: cuda_runtime.h:35
main external API structure.
Definition: avcodec.h:526
int nibble
Definition: adpcmenc.c:41
int extradata_size
Definition: avcodec.h:628
static int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s, int shift, int flag)
Definition: adpcmenc.c:500
Describe the class of an AVClass context structure.
Definition: log.h:67
int step
Definition: adpcmenc.c:50
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:322
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:104
const int8_t ff_adpcm_yamaha_difflookup[]
Definition: adpcm_data.c:104
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:115
const int16_t ff_adpcm_yamaha_indexscale[]
Definition: adpcm_data.c:99
signed 16 bits
Definition: samplefmt.h:61
#define flag(name)
Definition: cbs_av1.c:552
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)
static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb, const int16_t *samples, int nsamples, int shift, int flag)
Definition: adpcmenc.c:513
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
int trellis
trellis RD quantization
Definition: avcodec.h:1475
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
void * priv_data
Definition: avcodec.h:553
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:1187
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:845
Filter the word “frame” indicates either a video frame or a group of audio samples
#define av_freep(p)
int16_t step_index
Definition: adpcm.h:33
signed 16 bits, planar
Definition: samplefmt.h:67
#define stride
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
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:355
ADPCMChannelStatus status[6]
Definition: adpcmenc.c:57
This structure stores compressed data.
Definition: packet.h:340
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:374
for(j=16;j >0;--j)
static enum AVSampleFormat sample_fmts_p[]
Definition: adpcmenc.c:849
#define t2
Definition: regdef.h:30
int i
Definition: input.c:407
static const AVClass adpcm_encoder_class
Definition: adpcmenc.c:867
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
bitstream writer API