FFmpeg  4.0
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 "avcodec.h"
26 #include "put_bits.h"
27 #include "bytestream.h"
28 #include "adpcm.h"
29 #include "adpcm_data.h"
30 #include "internal.h"
31 
32 /**
33  * @file
34  * ADPCM encoders
35  * See ADPCM decoder reference documents for codec information.
36  */
37 
38 typedef struct TrellisPath {
39  int nibble;
40  int prev;
41 } TrellisPath;
42 
43 typedef struct TrellisNode {
44  uint32_t ssd;
45  int path;
46  int sample1;
47  int sample2;
48  int step;
49 } TrellisNode;
50 
51 typedef struct ADPCMEncodeContext {
52  ADPCMChannelStatus status[6];
58 
59 #define FREEZE_INTERVAL 128
60 
61 static av_cold int adpcm_encode_close(AVCodecContext *avctx);
62 
64 {
65  ADPCMEncodeContext *s = avctx->priv_data;
66  uint8_t *extradata;
67  int i;
68  int ret = AVERROR(ENOMEM);
69 
70  if (avctx->channels > 2) {
71  av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
72  return AVERROR(EINVAL);
73  }
74 
75  if (avctx->trellis && (unsigned)avctx->trellis > 16U) {
76  av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
77  return AVERROR(EINVAL);
78  }
79 
80  if (avctx->trellis) {
81  int frontier = 1 << avctx->trellis;
82  int max_paths = frontier * FREEZE_INTERVAL;
83  FF_ALLOC_OR_GOTO(avctx, s->paths,
84  max_paths * sizeof(*s->paths), error);
85  FF_ALLOC_OR_GOTO(avctx, s->node_buf,
86  2 * frontier * sizeof(*s->node_buf), error);
87  FF_ALLOC_OR_GOTO(avctx, s->nodep_buf,
88  2 * frontier * sizeof(*s->nodep_buf), error);
90  65536 * sizeof(*s->trellis_hash), error);
91  }
92 
94 
95  switch (avctx->codec->id) {
97  /* each 16 bits sample gives one nibble
98  and we have 4 bytes per channel overhead */
99  avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 /
100  (4 * avctx->channels) + 1;
101  /* seems frame_size isn't taken into account...
102  have to buffer the samples :-( */
103  avctx->block_align = BLKSIZE;
104  avctx->bits_per_coded_sample = 4;
105  break;
107  avctx->frame_size = 64;
108  avctx->block_align = 34 * avctx->channels;
109  break;
111  /* each 16 bits sample gives one nibble
112  and we have 7 bytes per channel overhead */
113  avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2;
114  avctx->bits_per_coded_sample = 4;
115  avctx->block_align = BLKSIZE;
117  goto error;
118  avctx->extradata_size = 32;
119  extradata = avctx->extradata;
120  bytestream_put_le16(&extradata, avctx->frame_size);
121  bytestream_put_le16(&extradata, 7); /* wNumCoef */
122  for (i = 0; i < 7; i++) {
123  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
124  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
125  }
126  break;
128  avctx->frame_size = BLKSIZE * 2 / avctx->channels;
129  avctx->block_align = BLKSIZE;
130  break;
132  if (avctx->sample_rate != 11025 &&
133  avctx->sample_rate != 22050 &&
134  avctx->sample_rate != 44100) {
135  av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
136  "22050 or 44100\n");
137  ret = AVERROR(EINVAL);
138  goto error;
139  }
140  avctx->frame_size = 512 * (avctx->sample_rate / 11025);
141  break;
142  default:
143  ret = AVERROR(EINVAL);
144  goto error;
145  }
146 
147  return 0;
148 error:
149  adpcm_encode_close(avctx);
150  return ret;
151 }
152 
154 {
155  ADPCMEncodeContext *s = avctx->priv_data;
156  av_freep(&s->paths);
157  av_freep(&s->node_buf);
158  av_freep(&s->nodep_buf);
159  av_freep(&s->trellis_hash);
160 
161  return 0;
162 }
163 
164 
166  int16_t sample)
167 {
168  int delta = sample - c->prev_sample;
169  int nibble = FFMIN(7, abs(delta) * 4 /
170  ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
173  c->prev_sample = av_clip_int16(c->prev_sample);
174  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
175  return nibble;
176 }
177 
179  int16_t sample)
180 {
181  int delta = sample - c->prev_sample;
182  int diff, step = ff_adpcm_step_table[c->step_index];
183  int nibble = 8*(delta < 0);
184 
185  delta= abs(delta);
186  diff = delta + (step >> 3);
187 
188  if (delta >= step) {
189  nibble |= 4;
190  delta -= step;
191  }
192  step >>= 1;
193  if (delta >= step) {
194  nibble |= 2;
195  delta -= step;
196  }
197  step >>= 1;
198  if (delta >= step) {
199  nibble |= 1;
200  delta -= step;
201  }
202  diff -= delta;
203 
204  if (nibble & 8)
205  c->prev_sample -= diff;
206  else
207  c->prev_sample += diff;
208 
209  c->prev_sample = av_clip_int16(c->prev_sample);
210  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
211 
212  return nibble;
213 }
214 
216  int16_t sample)
217 {
218  int predictor, nibble, bias;
219 
220  predictor = (((c->sample1) * (c->coeff1)) +
221  (( c->sample2) * (c->coeff2))) / 64;
222 
223  nibble = sample - predictor;
224  if (nibble >= 0)
225  bias = c->idelta / 2;
226  else
227  bias = -c->idelta / 2;
228 
229  nibble = (nibble + bias) / c->idelta;
230  nibble = av_clip_intp2(nibble, 3) & 0x0F;
231 
232  predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
233 
234  c->sample2 = c->sample1;
235  c->sample1 = av_clip_int16(predictor);
236 
237  c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
238  if (c->idelta < 16)
239  c->idelta = 16;
240 
241  return nibble;
242 }
243 
245  int16_t sample)
246 {
247  int nibble, delta;
248 
249  if (!c->step) {
250  c->predictor = 0;
251  c->step = 127;
252  }
253 
254  delta = sample - c->predictor;
255 
256  nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
257 
258  c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
259  c->predictor = av_clip_int16(c->predictor);
260  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
261  c->step = av_clip(c->step, 127, 24576);
262 
263  return nibble;
264 }
265 
267  const int16_t *samples, uint8_t *dst,
268  ADPCMChannelStatus *c, int n, int stride)
269 {
270  //FIXME 6% faster if frontier is a compile-time constant
271  ADPCMEncodeContext *s = avctx->priv_data;
272  const int frontier = 1 << avctx->trellis;
273  const int version = avctx->codec->id;
274  TrellisPath *paths = s->paths, *p;
275  TrellisNode *node_buf = s->node_buf;
276  TrellisNode **nodep_buf = s->nodep_buf;
277  TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
278  TrellisNode **nodes_next = nodep_buf + frontier;
279  int pathn = 0, froze = -1, i, j, k, generation = 0;
280  uint8_t *hash = s->trellis_hash;
281  memset(hash, 0xff, 65536 * sizeof(*hash));
282 
283  memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
284  nodes[0] = node_buf + frontier;
285  nodes[0]->ssd = 0;
286  nodes[0]->path = 0;
287  nodes[0]->step = c->step_index;
288  nodes[0]->sample1 = c->sample1;
289  nodes[0]->sample2 = c->sample2;
290  if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
291  version == AV_CODEC_ID_ADPCM_IMA_QT ||
292  version == AV_CODEC_ID_ADPCM_SWF)
293  nodes[0]->sample1 = c->prev_sample;
294  if (version == AV_CODEC_ID_ADPCM_MS)
295  nodes[0]->step = c->idelta;
296  if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
297  if (c->step == 0) {
298  nodes[0]->step = 127;
299  nodes[0]->sample1 = 0;
300  } else {
301  nodes[0]->step = c->step;
302  nodes[0]->sample1 = c->predictor;
303  }
304  }
305 
306  for (i = 0; i < n; i++) {
307  TrellisNode *t = node_buf + frontier*(i&1);
308  TrellisNode **u;
309  int sample = samples[i * stride];
310  int heap_pos = 0;
311  memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
312  for (j = 0; j < frontier && nodes[j]; j++) {
313  // higher j have higher ssd already, so they're likely
314  // to yield a suboptimal next sample too
315  const int range = (j < frontier / 2) ? 1 : 0;
316  const int step = nodes[j]->step;
317  int nidx;
318  if (version == AV_CODEC_ID_ADPCM_MS) {
319  const int predictor = ((nodes[j]->sample1 * c->coeff1) +
320  (nodes[j]->sample2 * c->coeff2)) / 64;
321  const int div = (sample - predictor) / step;
322  const int nmin = av_clip(div-range, -8, 6);
323  const int nmax = av_clip(div+range, -7, 7);
324  for (nidx = nmin; nidx <= nmax; nidx++) {
325  const int nibble = nidx & 0xf;
326  int dec_sample = predictor + nidx * step;
327 #define STORE_NODE(NAME, STEP_INDEX)\
328  int d;\
329  uint32_t ssd;\
330  int pos;\
331  TrellisNode *u;\
332  uint8_t *h;\
333  dec_sample = av_clip_int16(dec_sample);\
334  d = sample - dec_sample;\
335  ssd = nodes[j]->ssd + d*(unsigned)d;\
336  /* Check for wraparound, skip such samples completely. \
337  * Note, changing ssd to a 64 bit variable would be \
338  * simpler, avoiding this check, but it's slower on \
339  * x86 32 bit at the moment. */\
340  if (ssd < nodes[j]->ssd)\
341  goto next_##NAME;\
342  /* Collapse any two states with the same previous sample value. \
343  * One could also distinguish states by step and by 2nd to last
344  * sample, but the effects of that are negligible.
345  * Since nodes in the previous generation are iterated
346  * through a heap, they're roughly ordered from better to
347  * worse, but not strictly ordered. Therefore, an earlier
348  * node with the same sample value is better in most cases
349  * (and thus the current is skipped), but not strictly
350  * in all cases. Only skipping samples where ssd >=
351  * ssd of the earlier node with the same sample gives
352  * slightly worse quality, though, for some reason. */ \
353  h = &hash[(uint16_t) dec_sample];\
354  if (*h == generation)\
355  goto next_##NAME;\
356  if (heap_pos < frontier) {\
357  pos = heap_pos++;\
358  } else {\
359  /* Try to replace one of the leaf nodes with the new \
360  * one, but try a different slot each time. */\
361  pos = (frontier >> 1) +\
362  (heap_pos & ((frontier >> 1) - 1));\
363  if (ssd > nodes_next[pos]->ssd)\
364  goto next_##NAME;\
365  heap_pos++;\
366  }\
367  *h = generation;\
368  u = nodes_next[pos];\
369  if (!u) {\
370  av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
371  u = t++;\
372  nodes_next[pos] = u;\
373  u->path = pathn++;\
374  }\
375  u->ssd = ssd;\
376  u->step = STEP_INDEX;\
377  u->sample2 = nodes[j]->sample1;\
378  u->sample1 = dec_sample;\
379  paths[u->path].nibble = nibble;\
380  paths[u->path].prev = nodes[j]->path;\
381  /* Sift the newly inserted node up in the heap to \
382  * restore the heap property. */\
383  while (pos > 0) {\
384  int parent = (pos - 1) >> 1;\
385  if (nodes_next[parent]->ssd <= ssd)\
386  break;\
387  FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
388  pos = parent;\
389  }\
390  next_##NAME:;
391  STORE_NODE(ms, FFMAX(16,
392  (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
393  }
394  } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
395  version == AV_CODEC_ID_ADPCM_IMA_QT ||
396  version == AV_CODEC_ID_ADPCM_SWF) {
397 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
398  const int predictor = nodes[j]->sample1;\
399  const int div = (sample - predictor) * 4 / STEP_TABLE;\
400  int nmin = av_clip(div - range, -7, 6);\
401  int nmax = av_clip(div + range, -6, 7);\
402  if (nmin <= 0)\
403  nmin--; /* distinguish -0 from +0 */\
404  if (nmax < 0)\
405  nmax--;\
406  for (nidx = nmin; nidx <= nmax; nidx++) {\
407  const int nibble = nidx < 0 ? 7 - nidx : nidx;\
408  int dec_sample = predictor +\
409  (STEP_TABLE *\
410  ff_adpcm_yamaha_difflookup[nibble]) / 8;\
411  STORE_NODE(NAME, STEP_INDEX);\
412  }
414  av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
415  } else { //AV_CODEC_ID_ADPCM_YAMAHA
416  LOOP_NODES(yamaha, step,
417  av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
418  127, 24576));
419 #undef LOOP_NODES
420 #undef STORE_NODE
421  }
422  }
423 
424  u = nodes;
425  nodes = nodes_next;
426  nodes_next = u;
427 
428  generation++;
429  if (generation == 255) {
430  memset(hash, 0xff, 65536 * sizeof(*hash));
431  generation = 0;
432  }
433 
434  // prevent overflow
435  if (nodes[0]->ssd > (1 << 28)) {
436  for (j = 1; j < frontier && nodes[j]; j++)
437  nodes[j]->ssd -= nodes[0]->ssd;
438  nodes[0]->ssd = 0;
439  }
440 
441  // merge old paths to save memory
442  if (i == froze + FREEZE_INTERVAL) {
443  p = &paths[nodes[0]->path];
444  for (k = i; k > froze; k--) {
445  dst[k] = p->nibble;
446  p = &paths[p->prev];
447  }
448  froze = i;
449  pathn = 0;
450  // other nodes might use paths that don't coincide with the frozen one.
451  // checking which nodes do so is too slow, so just kill them all.
452  // this also slightly improves quality, but I don't know why.
453  memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
454  }
455  }
456 
457  p = &paths[nodes[0]->path];
458  for (i = n - 1; i > froze; i--) {
459  dst[i] = p->nibble;
460  p = &paths[p->prev];
461  }
462 
463  c->predictor = nodes[0]->sample1;
464  c->sample1 = nodes[0]->sample1;
465  c->sample2 = nodes[0]->sample2;
466  c->step_index = nodes[0]->step;
467  c->step = nodes[0]->step;
468  c->idelta = nodes[0]->step;
469 }
470 
471 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
472  const AVFrame *frame, int *got_packet_ptr)
473 {
474  int n, i, ch, st, pkt_size, ret;
475  const int16_t *samples;
476  int16_t **samples_p;
477  uint8_t *dst;
478  ADPCMEncodeContext *c = avctx->priv_data;
479  uint8_t *buf;
480 
481  samples = (const int16_t *)frame->data[0];
482  samples_p = (int16_t **)frame->extended_data;
483  st = avctx->channels == 2;
484 
485  if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
486  pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
487  else
488  pkt_size = avctx->block_align;
489  if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
490  return ret;
491  dst = avpkt->data;
492 
493  switch(avctx->codec->id) {
495  {
496  int blocks, j;
497 
498  blocks = (frame->nb_samples - 1) / 8;
499 
500  for (ch = 0; ch < avctx->channels; ch++) {
501  ADPCMChannelStatus *status = &c->status[ch];
502  status->prev_sample = samples_p[ch][0];
503  /* status->step_index = 0;
504  XXX: not sure how to init the state machine */
505  bytestream_put_le16(&dst, status->prev_sample);
506  *dst++ = status->step_index;
507  *dst++ = 0; /* unknown */
508  }
509 
510  /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
511  if (avctx->trellis > 0) {
512  FF_ALLOC_ARRAY_OR_GOTO(avctx, buf, avctx->channels, blocks * 8, error);
513  for (ch = 0; ch < avctx->channels; ch++) {
514  adpcm_compress_trellis(avctx, &samples_p[ch][1],
515  buf + ch * blocks * 8, &c->status[ch],
516  blocks * 8, 1);
517  }
518  for (i = 0; i < blocks; i++) {
519  for (ch = 0; ch < avctx->channels; ch++) {
520  uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
521  for (j = 0; j < 8; j += 2)
522  *dst++ = buf1[j] | (buf1[j + 1] << 4);
523  }
524  }
525  av_free(buf);
526  } else {
527  for (i = 0; i < blocks; i++) {
528  for (ch = 0; ch < avctx->channels; ch++) {
529  ADPCMChannelStatus *status = &c->status[ch];
530  const int16_t *smp = &samples_p[ch][1 + i * 8];
531  for (j = 0; j < 8; j += 2) {
532  uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
533  v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
534  *dst++ = v;
535  }
536  }
537  }
538  }
539  break;
540  }
542  {
543  PutBitContext pb;
544  init_put_bits(&pb, dst, pkt_size);
545 
546  for (ch = 0; ch < avctx->channels; ch++) {
547  ADPCMChannelStatus *status = &c->status[ch];
548  put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
549  put_bits(&pb, 7, status->step_index);
550  if (avctx->trellis > 0) {
551  uint8_t buf[64];
552  adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
553  64, 1);
554  for (i = 0; i < 64; i++)
555  put_bits(&pb, 4, buf[i ^ 1]);
556  status->prev_sample = status->predictor;
557  } else {
558  for (i = 0; i < 64; i += 2) {
559  int t1, t2;
560  t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
561  t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
562  put_bits(&pb, 4, t2);
563  put_bits(&pb, 4, t1);
564  }
565  }
566  }
567 
568  flush_put_bits(&pb);
569  break;
570  }
572  {
573  PutBitContext pb;
574  init_put_bits(&pb, dst, pkt_size);
575 
576  n = frame->nb_samples - 1;
577 
578  // store AdpcmCodeSize
579  put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
580 
581  // init the encoder state
582  for (i = 0; i < avctx->channels; i++) {
583  // clip step so it fits 6 bits
584  c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
585  put_sbits(&pb, 16, samples[i]);
586  put_bits(&pb, 6, c->status[i].step_index);
587  c->status[i].prev_sample = samples[i];
588  }
589 
590  if (avctx->trellis > 0) {
591  FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
592  adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
593  &c->status[0], n, avctx->channels);
594  if (avctx->channels == 2)
595  adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
596  buf + n, &c->status[1], n,
597  avctx->channels);
598  for (i = 0; i < n; i++) {
599  put_bits(&pb, 4, buf[i]);
600  if (avctx->channels == 2)
601  put_bits(&pb, 4, buf[n + i]);
602  }
603  av_free(buf);
604  } else {
605  for (i = 1; i < frame->nb_samples; i++) {
607  samples[avctx->channels * i]));
608  if (avctx->channels == 2)
610  samples[2 * i + 1]));
611  }
612  }
613  flush_put_bits(&pb);
614  break;
615  }
617  for (i = 0; i < avctx->channels; i++) {
618  int predictor = 0;
619  *dst++ = predictor;
620  c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
621  c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
622  }
623  for (i = 0; i < avctx->channels; i++) {
624  if (c->status[i].idelta < 16)
625  c->status[i].idelta = 16;
626  bytestream_put_le16(&dst, c->status[i].idelta);
627  }
628  for (i = 0; i < avctx->channels; i++)
629  c->status[i].sample2= *samples++;
630  for (i = 0; i < avctx->channels; i++) {
631  c->status[i].sample1 = *samples++;
632  bytestream_put_le16(&dst, c->status[i].sample1);
633  }
634  for (i = 0; i < avctx->channels; i++)
635  bytestream_put_le16(&dst, c->status[i].sample2);
636 
637  if (avctx->trellis > 0) {
638  n = avctx->block_align - 7 * avctx->channels;
639  FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
640  if (avctx->channels == 1) {
641  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
642  avctx->channels);
643  for (i = 0; i < n; i += 2)
644  *dst++ = (buf[i] << 4) | buf[i + 1];
645  } else {
646  adpcm_compress_trellis(avctx, samples, buf,
647  &c->status[0], n, avctx->channels);
648  adpcm_compress_trellis(avctx, samples + 1, buf + n,
649  &c->status[1], n, avctx->channels);
650  for (i = 0; i < n; i++)
651  *dst++ = (buf[i] << 4) | buf[n + i];
652  }
653  av_free(buf);
654  } else {
655  for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
656  int nibble;
657  nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
658  nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
659  *dst++ = nibble;
660  }
661  }
662  break;
664  n = frame->nb_samples / 2;
665  if (avctx->trellis > 0) {
666  FF_ALLOC_OR_GOTO(avctx, buf, 2 * n * 2, error);
667  n *= 2;
668  if (avctx->channels == 1) {
669  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
670  avctx->channels);
671  for (i = 0; i < n; i += 2)
672  *dst++ = buf[i] | (buf[i + 1] << 4);
673  } else {
674  adpcm_compress_trellis(avctx, samples, buf,
675  &c->status[0], n, avctx->channels);
676  adpcm_compress_trellis(avctx, samples + 1, buf + n,
677  &c->status[1], n, avctx->channels);
678  for (i = 0; i < n; i++)
679  *dst++ = buf[i] | (buf[n + i] << 4);
680  }
681  av_free(buf);
682  } else
683  for (n *= avctx->channels; n > 0; n--) {
684  int nibble;
685  nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
686  nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
687  *dst++ = nibble;
688  }
689  break;
690  default:
691  return AVERROR(EINVAL);
692  }
693 
694  avpkt->size = pkt_size;
695  *got_packet_ptr = 1;
696  return 0;
697 error:
698  return AVERROR(ENOMEM);
699 }
700 
701 static const enum AVSampleFormat sample_fmts[] = {
703 };
704 
705 static const enum AVSampleFormat sample_fmts_p[] = {
707 };
708 
709 #define ADPCM_ENCODER(id_, name_, sample_fmts_, long_name_) \
710 AVCodec ff_ ## name_ ## _encoder = { \
711  .name = #name_, \
712  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
713  .type = AVMEDIA_TYPE_AUDIO, \
714  .id = id_, \
715  .priv_data_size = sizeof(ADPCMEncodeContext), \
716  .init = adpcm_encode_init, \
717  .encode2 = adpcm_encode_frame, \
718  .close = adpcm_encode_close, \
719  .sample_fmts = sample_fmts_, \
720 }
721 
722 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, "ADPCM IMA QuickTime");
723 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, "ADPCM IMA WAV");
724 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, sample_fmts, "ADPCM Microsoft");
725 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, sample_fmts, "ADPCM Shockwave Flash");
726 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, "ADPCM Yamaha");
const struct AVCodec * codec
Definition: avcodec.h:1527
const char * s
Definition: avisynth_c.h:768
int sample1
Definition: adpcmenc.c:46
int path
Definition: adpcmenc.c:45
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
Definition: adpcmenc.c:63
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:240
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:207
static uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:215
#define BLKSIZE
Definition: adpcm.h:31
#define ima
int size
Definition: avcodec.h:1431
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&HAVE_MMX) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
static uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:178
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
Definition: adpcmenc.c:153
int version
Definition: avisynth_c.h:766
#define sample
int stride
Definition: mace.c:144
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2210
uint8_t * trellis_hash
Definition: adpcmenc.c:56
static uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:244
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:32
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:82
#define av_malloc(s)
float delta
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1618
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:344
ADPCM tables.
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1430
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2734
#define av_log(a,...)
uint8_t hash[HASH_SIZE]
Definition: movenc.c:57
#define U(x)
Definition: vp56_arith.h:37
uint32_t ssd
Definition: adpcmenc.c:44
enum AVCodecID id
Definition: avcodec.h:3422
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1495
ADPCM encoder/decoder common header.
#define AVERROR(e)
Definition: error.h:43
#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:59
static uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:165
#define FFMIN(a, b)
Definition: common.h:96
TrellisNode ** nodep_buf
Definition: adpcmenc.c:55
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:266
static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: adpcmenc.c:471
int n
Definition: avisynth_c.h:684
TrellisPath * paths
Definition: adpcmenc.c:53
if(ret< 0)
Definition: vf_mcdeint.c:279
int sample2
Definition: adpcmenc.c:47
static void error(const char *err)
TrellisNode * node_buf
Definition: adpcmenc.c:54
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2193
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:1528
int sample_rate
samples per second
Definition: avcodec.h:2173
main external API structure.
Definition: avcodec.h:1518
int nibble
Definition: adpcmenc.c:39
void * buf
Definition: avisynth_c.h:690
int extradata_size
Definition: avcodec.h:1619
int step
Definition: adpcmenc.c:48
#define ADPCM_ENCODER(id_, name_, sample_fmts_, long_name_)
Definition: adpcmenc.c:709
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
#define FF_ALLOC_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)
Definition: internal.h:158
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:101
const int16_t ff_adpcm_yamaha_indexscale[]
Definition: adpcm_data.c:99
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
Definition: internal.h:140
signed 16 bits
Definition: samplefmt.h:61
static double c[64]
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int trellis
trellis RD quantization
Definition: avcodec.h:2462
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:773
void * priv_data
Definition: avcodec.h:1545
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:2174
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
int16_t step_index
Definition: adpcm.h:35
signed 16 bits, planar
Definition: samplefmt.h:67
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:265
ADPCMChannelStatus status[6]
Definition: adpcmenc.c:52
This structure stores compressed data.
Definition: avcodec.h:1407
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:284
for(j=16;j >0;--j)
static enum AVSampleFormat sample_fmts_p[]
Definition: adpcmenc.c:705
#define t2
Definition: regdef.h:30
bitstream writer API