ADPCM Codecs
ADPCMEncoder.h
1 #pragma once
2 #include "ADPCM.h"
3 #include "ADPCMCodec.h"
5 
6 #define FREEZE_INTERVAL 128
7 
8 namespace adpcm_ffmpeg {
9 
15 class ADPCMEncoder : public ADPCMCodec {
16  public:
17  ADPCMEncoder() : ADPCMCodec() {
18  setBlockSize(ADAPCM_DEFAULT_BLOCK_SIZE);
19  avctx.priv_data = (uint8_t *)&enc_ctx;
20  }
21 
22  bool begin(int sampleRate, int channels) {
23  avctx.sample_rate = sampleRate;
24  avctx.nb_channels = channels;
25  avctx.sample_fmt = sample_formats[0];
26  bool rc = adpcm_encode_init() == 0;
27  printf("frame_size: %d", frameSize());
28  return rc;
29  }
30 
31  void end() { adpcm_encode_close(); }
32 
33  AVPacket &encode(int16_t *data, size_t sampleCount) {
34  frame.nb_samples = sampleCount / avctx.nb_channels;
35  // fill data
36  frame.data[0] = (uint8_t *)data;
37 
38  // fill extended_data
39  frame.extended_data = extended_data;
40  if (channels() == 1 || !isPlanar()) {
41  extended_data[0] = data;
42  } else if (channels() == 2) {
43  // if channels() is 2 we need to split up the stereo data
44  // into separate frame_extended_data_vector2 arrays
45  frame_extended_data_vectors.resize(channels());
46  for (int ch=0;ch<channels();ch++){
47  frame_extended_data_vectors[ch].resize(sampleCount/channels());
48  extended_data[ch] = &frame_extended_data_vectors[ch][0];
49  }
50 
51  // fill with data
52  for (int j = 0; j < sampleCount / channels(); j++) {
53  for (int ch=0;ch<channels();ch++){
54  frame_extended_data_vectors[ch][j] = data[(j * channels()) + ch];
55  }
56  }
57  }
58 
59  int got_packet_ptr = 0;
60  av_packet_data.resize(sampleCount);
61  result.data = &av_packet_data[0];
62 
63  int rc = adpcm_encode_frame(&result, &frame, &got_packet_ptr);
64  if (rc != 0 || !got_packet_ptr) {
65  result.size = 0;
66  }
67  return result;
68  }
69 
70  virtual bool is_trellis() { return false; }
71 
72  int blockAlign() { return avctx.block_align;}
73 
74  protected:
75  AVPacket result;
76  AVFrame frame;
77  int16_t *extended_data[2] = {0};
78  std::vector<uint8_t> av_packet_data;
79  std::vector<std::vector<int16_t>> frame_extended_data_vectors;
80  // encoding data
81  int st, pkt_size, ret;
82  const int16_t *samples;
83  const int16_t *const *samples_p;
84  uint8_t *dst;
87 
88  virtual int adpcm_encode_init_impl() = 0;
89 
90  virtual int adpcm_encode_init() {
91  s = (ADPCMEncodeContext *)avctx.priv_data;
92 
93  if (s == NULL) {
94  return -1;
95  }
96 
97  /*
98  * AMV's block size has to match that of the corresponding video
99  * stream. Relax the POT requirement.
100  */
101  if (avctx.codec_id != AV_CODEC_ID_ADPCM_IMA_AMV &&
102  (s->block_size & (s->block_size - 1))) {
103  av_log(avctx, AV_LOG_ERROR, "block size must be power of 2: %d\n",
104  s->block_size);
105  return AVERROR(AVERROR_INVALID);
106  }
107 
108  if (avctx.trellis) {
109  int frontier, max_paths;
110 
111  if ((unsigned)avctx.trellis > 16U) {
112  av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
113  return AVERROR(AVERROR_INVALID);
114  }
115 
116  if (avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
117  avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
118  avctx.codec_id == AV_CODEC_ID_ADPCM_ARGO ||
119  avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_WS) {
120  /*
121  * The current trellis implementation doesn't work for extended
122  * runs of samples without periodic resets. Disallow it.
123  */
124  av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
125  return AVERROR_PATCHWELCOME;
126  }
127 
128  frontier = 1 << avctx.trellis;
129  max_paths = frontier * FREEZE_INTERVAL;
130  if (!FF_ALLOC_TYPED_ARRAY(TrellisPath *, s->paths, max_paths) ||
131  !FF_ALLOC_TYPED_ARRAY(TrellisNode *, s->node_buf, 2 * frontier) ||
132  !FF_ALLOC_TYPED_ARRAY(TrellisNode **, s->nodep_buf, 2 * frontier) ||
133  !FF_ALLOC_TYPED_ARRAY(uint8_t *, s->trellis_hash, 65536))
134  return AVERROR(AVERROR_MEMORY);
135  }
136 
137  avctx.bits_per_coded_sample = av_get_bits_per_sample();
138 
139  return adpcm_encode_init_impl();
140  }
141 
142  int adpcm_encode_close() {
143  ADPCMEncodeContext *s = (ADPCMEncodeContext *)avctx.priv_data;
144  av_freep(&s->paths);
145  av_freep(&s->node_buf);
146  av_freep(&s->nodep_buf);
147  av_freep(&s->trellis_hash);
148 
149  return 0;
150  }
151 
152  inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
153  int16_t sample) {
154  int delta = sample - c->prev_sample;
155  int nibble = FFMIN(7, abs(delta) * 4 / ff_adpcm_step_table[c->step_index]) +
156  (delta < 0) * 8;
157  c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
158  ff_adpcm_yamaha_difflookup[nibble]) /
159  8);
160  c->prev_sample = av_clip_int16(c->prev_sample);
161  c->step_index =
162  av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
163  return nibble;
164  }
165 
166  inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
167  int16_t sample) {
168  int delta = sample - c->prev_sample;
169  int diff, step = ff_adpcm_step_table[c->step_index];
170  int nibble = 8 * (delta < 0);
171 
172  delta = abs(delta);
173  diff = delta + (step >> 3);
174 
175  if (delta >= step) {
176  nibble |= 4;
177  delta -= step;
178  }
179  step >>= 1;
180  if (delta >= step) {
181  nibble |= 2;
182  delta -= step;
183  }
184  step >>= 1;
185  if (delta >= step) {
186  nibble |= 1;
187  delta -= step;
188  }
189  diff -= delta;
190 
191  if (nibble & 8)
192  c->prev_sample -= diff;
193  else
194  c->prev_sample += diff;
195 
196  c->prev_sample = av_clip_int16(c->prev_sample);
197  c->step_index =
198  av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
199 
200  return nibble;
201  }
202 
203  inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
204  int16_t sample) {
205  int nibble, delta;
206 
207  if (!c->step) {
208  c->predictor = 0;
209  c->step = 127;
210  }
211 
212  delta = sample - c->predictor;
213 
214  nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
215 
216  c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
217  c->predictor = av_clip_int16(c->predictor);
218  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
219  c->step = av_clip(c->step, 127, 24576);
220 
221  return nibble;
222  }
223 
224  virtual int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
225  int *got_packet_ptr) = 0;
226 
227  virtual int adpcm_encode_frame(AVPacket *avpkt, const AVFrame *frame,
228  int *got_packet_ptr) {
229  c = (ADPCMEncodeContext *)avctx.priv_data;
230 
231  samples = (const int16_t *)frame->data[0];
232  samples_p = (const int16_t *const *)frame->extended_data;
233  assert(samples_p != NULL);
234  assert(samples != NULL);
235  st = channels() == 2;
236 
237  if (avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
238  avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
239  avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
240  avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_WS)
241  pkt_size = (frame->nb_samples * channels() + 1) / 2;
242  else
243  pkt_size = avctx.block_align;
244  if ((ret = ff_get_encode_buffer(&avctx, avpkt, pkt_size, 0)) < 0)
245  return ret;
246  dst = avpkt->data;
247 
248  int rc = adpcm_encode_frame_impl(avpkt, frame, got_packet_ptr);
249  if (rc != AV_OK) return rc;
250 
251  *got_packet_ptr = 1;
252  return 0;
253  }
254 };
255 
257  public:
258  bool is_trellis() { return avctx.trellis; }
259  void set_trellis(bool flag) { avctx.trellis = flag;}
260  bool store_node(int STEP_INDEX) {
261  int d;
262  uint32_t ssd;
263  int pos;
264  TrellisNode *u;
265  uint8_t *h;
266  dec_sample = av_clip_int16(dec_sample);
267  d = sample - dec_sample;
268  ssd = nodes[j]->ssd +
269  d * (unsigned)d; /* Check for wraparound, skip such samples
270  * completely. \
271  * Note, changing ssd to a 64 bit variable would be \
272  * simpler, avoiding this check, but it's slower on \
273  * x86 32 bit at the moment. */
274  if (ssd < nodes[j]->ssd) {
275  /* Collapse any two states with the same previous
276  * sample value. One could also distinguish states by step and by 2nd
277  * to last sample, but the effects of that are negligible.
278  * Since nodes in the previous generation are iterated through a heap,
279  * they're roughly ordered from better to worse, but not strictly ordered.
280  * Therefore, an earlier node with the same sample value is better in most
281  * cases (and thus the current is skipped), but not strictly
282  * in all cases. Only skipping samples where ssd >= ssd of the earlier
283  * node with the same sample gives slightly worse quality, though, for
284  * some reason. */
285  return true;
286  }
287  h = &hash[(uint16_t)dec_sample];
288  if (*h == generation) return true;
289  if (heap_pos < frontier) {
290  pos = heap_pos++;
291  } else { /* Try to replace one of the leaf nodes with the new \
292  * one, but try a different slot each time. */
293  pos = (frontier >> 1) + (heap_pos & ((frontier >> 1) - 1));
294  if (ssd > nodes_next[pos]->ssd) return true;
295  heap_pos++;
296  }
297  *h = generation;
298  u = nodes_next[pos];
299  if (!u) {
300  av_assert(pathn < FREEZE_INTERVAL << avctx.trellis);
301  u = t++;
302  nodes_next[pos] = u;
303  u->path = pathn++;
304  }
305  u->ssd = ssd;
306  u->step = STEP_INDEX;
307  u->sample2 = nodes[j]->sample1;
308  u->sample1 = dec_sample;
309  paths[u->path].nibble = nibble;
310  paths[u->path].prev =
311  nodes[j]->path; /* Sift the newly inserted node up in the heap to \
312  * restore the heap property. */
313  while (pos > 0) {
314  int parent = (pos - 1) >> 1;
315  if (nodes_next[parent]->ssd <= ssd) break;
316  FFSWAP(TrellisNode *, nodes_next[parent], nodes_next[pos]);
317  pos = parent;
318  }
319  return false;
320  }
321 
322  void loop_nodes(int16_t STEP_TABLE, int STEP_INDEX) {
323  const int predictor = nodes[j]->sample1;
324  const int div = (sample - predictor) * 4 / STEP_TABLE;
325  int nmin = av_clip(div - range, -7, 6);
326  int nmax = av_clip(div + range, -6, 7);
327  if (nmin <= 0) nmin--; /* distinguish -0 from +0 */
328  if (nmax < 0) nmax--;
329  for (nidx = nmin; nidx <= nmax; nidx++) {
330  const int nibble = nidx < 0 ? 7 - nidx : nidx;
331  dec_sample =
332  predictor + (STEP_TABLE * ff_adpcm_yamaha_difflookup[nibble]) / 8;
333  store_node(STEP_INDEX);
334  }
335  }
336 
337  void adpcm_compress_trellis(const int16_t *samples, uint8_t *dst,
338  ADPCMChannelStatus *c, int n, int stride) {
339  // FIXME 6% faster if frontier is a compile-time constant
340  s = (ADPCMEncodeContext *)avctx.priv_data;
341  frontier = 1 << avctx.trellis;
342  version = avctx.codec_id;
343  paths = s->paths, *p;
344  node_buf = s->node_buf;
345  nodep_buf = s->nodep_buf;
346  nodes = nodep_buf; // nodes[] is always sorted by .ssd
347  nodes_next = nodep_buf + frontier;
348  pathn = 0, froze = -1, i, j, k, generation = 0;
349  hash = s->trellis_hash;
350  memset(hash, 0xff, 65536 * sizeof(*hash));
351 
352  memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
353  nodes[0] = node_buf + frontier;
354  nodes[0]->ssd = 0;
355  nodes[0]->path = 0;
356  nodes[0]->step = c->step_index;
357  nodes[0]->sample1 = c->sample1;
358  nodes[0]->sample2 = c->sample2;
359  if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
360  version == AV_CODEC_ID_ADPCM_IMA_QT ||
361  version == AV_CODEC_ID_ADPCM_IMA_AMV ||
362  version == AV_CODEC_ID_ADPCM_SWF)
363  nodes[0]->sample1 = c->prev_sample;
364  if (version == AV_CODEC_ID_ADPCM_MS) nodes[0]->step = c->idelta;
365  if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
366  if (c->step == 0) {
367  nodes[0]->step = 127;
368  nodes[0]->sample1 = 0;
369  } else {
370  nodes[0]->step = c->step;
371  nodes[0]->sample1 = c->predictor;
372  }
373  }
374 
375  for (i = 0; i < n; i++) {
376  t = node_buf + frontier * (i & 1);
377  sample = samples[i * stride];
378  heap_pos = 0;
379  memset(nodes_next, 0, frontier * sizeof(TrellisNode *));
380  for (j = 0; j < frontier && nodes[j]; j++) {
381  // higher j have higher ssd already, so they're likely
382  // to yield a suboptimal next sample too
383  range = (j < frontier / 2) ? 1 : 0;
384  const int step = nodes[j]->step;
385  if (version == AV_CODEC_ID_ADPCM_MS) {
386  const int predictor = ((nodes[j]->sample1 * c->coeff1) +
387  (nodes[j]->sample2 * c->coeff2)) /
388  64;
389  const int div = (sample - predictor) / step;
390  const int nmin = av_clip(div - range, -8, 6);
391  const int nmax = av_clip(div + range, -7, 7);
392  for (nidx = nmin; nidx <= nmax; nidx++) {
393  nibble = nidx & 0xf;
394  dec_sample = predictor + nidx * step;
395 
396  while (store_node(
397  FFMAX(16, (ff_adpcm_AdaptationTable[nibble] * step) >> 8)));
398  }
399  } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
400  version == AV_CODEC_ID_ADPCM_IMA_QT ||
401  version == AV_CODEC_ID_ADPCM_IMA_AMV ||
402  version == AV_CODEC_ID_ADPCM_SWF) {
403  loop_nodes(ff_adpcm_step_table[step],
404  av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
405  } else { // AV_CODEC_ID_ADPCM_YAMAHA
406  loop_nodes(step,
407  av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
408  127, 24576));
409  }
410  }
411 
412  u = nodes;
413  nodes = nodes_next;
414  nodes_next = u;
415 
416  generation++;
417  if (generation == 255) {
418  memset(hash, 0xff, 65536 * sizeof(*hash));
419  generation = 0;
420  }
421 
422  // prevent overflow
423  if (nodes[0]->ssd > (1 << 28)) {
424  for (j = 1; j < frontier && nodes[j]; j++)
425  nodes[j]->ssd -= nodes[0]->ssd;
426  nodes[0]->ssd = 0;
427  }
428 
429  // merge old paths to save memory
430  if (i == froze + FREEZE_INTERVAL) {
431  p = &paths[nodes[0]->path];
432  for (k = i; k > froze; k--) {
433  dst[k] = p->nibble;
434  p = &paths[p->prev];
435  }
436  froze = i;
437  pathn = 0;
438  // other nodes might use paths that don't coincide with the frozen one.
439  // checking which nodes do so is too slow, so just kill them all.
440  // this also slightly improves quality, but I don't know why.
441  memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode *));
442  }
443  }
444 
445  p = &paths[nodes[0]->path];
446  for (i = n - 1; i > froze; i--) {
447  dst[i] = p->nibble;
448  p = &paths[p->prev];
449  }
450 
451  c->predictor = nodes[0]->sample1;
452  c->sample1 = nodes[0]->sample1;
453  c->sample2 = nodes[0]->sample2;
454  c->step_index = nodes[0]->step;
455  c->step = nodes[0]->step;
456  c->idelta = nodes[0]->step;
457  }
458 
459  protected:
460  int frontier;
461  int version;
462  TrellisPath *paths, *p;
463  TrellisNode *node_buf;
464  TrellisNode **nodep_buf;
465  TrellisNode **nodes; // nodes[] is always sorted by .ssd
466  TrellisNode **nodes_next;
467  TrellisNode **u;
468  TrellisNode *t;
469 
470  int pathn = 0, froze = -1, i, j, k, generation = 0;
471  uint8_t *hash;
472  int dec_sample = 0;
473  int sample;
474  int nibble;
475  int nidx;
476  int range;
477  int heap_pos;
478 };
479 
481  public:
483  setCodecID(AV_CODEC_ID_ADPCM_IMA_WAV);
484  sample_formats.push_back(AV_SAMPLE_FMT_S16P);
485  }
486  int adpcm_encode_init_impl() {
487  /* each 16 bits sample gives one nibble
488  and we have 4 bytes per channel overhead */
489  avctx.frame_size = (s->block_size - 4 * channels()) * 8 / (4 * channels()) + 1;
490  /* seems frame_size isn't taken into account...
491  have to buffer the samples :-( */
492  avctx.block_align = s->block_size;
493  avctx.bits_per_coded_sample = 4;
494  return AV_OK;
495  }
496  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
497  int *got_packet_ptr) {
498  int blocks = (frame->nb_samples - 1) / 8;
499 
500  for (int ch = 0; ch < 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  uint8_t *buf;
513  if (!FF_ALLOC_TYPED_ARRAY(uint8_t *, buf, channels() *blocks * 8))
514  return AVERROR(AVERROR_MEMORY);
515  for (int ch = 0; ch < channels(); ch++) {
516  adpcm_compress_trellis(&samples_p[ch][1], buf + ch * blocks * 8,
517  &c->status[ch], blocks * 8, 1);
518  }
519  for (int i = 0; i < blocks; i++) {
520  for (int ch = 0; ch < channels(); ch++) {
521  uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
522  for (int j = 0; j < 8; j += 2) *dst++ = buf1[j] | (buf1[j + 1] << 4);
523  }
524  }
525  av_free(buf);
526  } else {
527  for (int i = 0; i < blocks; i++) {
528  for (int ch = 0; ch < channels(); ch++) {
529  ADPCMChannelStatus *status = &c->status[ch];
530  const int16_t *smp = &samples_p[ch][1 + i * 8];
531  for (int 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  return AV_OK;
540  }
541 };
542 
544  public:
546  setCodecID(AV_CODEC_ID_ADPCM_IMA_QT);
547  sample_formats.push_back(AV_SAMPLE_FMT_S16P);
548  }
549  int adpcm_encode_init_impl() {
550  avctx.frame_size = 64 ;
551  avctx.block_align = 34 * channels(); /* End of CASE */
552  return AV_OK;
553  }
554 
555  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
556  int *got_packet_ptr) {
557  PutBitContext pb;
558  init_put_bits(&pb, dst, pkt_size);
559 
560  for (int ch = 0; ch < channels(); ch++) {
561  ADPCMChannelStatus *status = &c->status[ch];
562  put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
563  put_bits(&pb, 7, status->step_index);
564  if (avctx.trellis > 0) {
565  uint8_t buf[64];
566  adpcm_compress_trellis(&samples_p[ch][0], buf, status, 64, 1);
567  for (int i = 0; i < 64; i++) put_bits(&pb, 4, buf[i ^ 1]);
568  status->prev_sample = status->predictor;
569  } else {
570  for (int i = 0; i < 64; i += 2) {
571  int t1, t2;
572  t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i]);
573  t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
574  put_bits(&pb, 4, t2);
575  put_bits(&pb, 4, t1);
576  }
577  }
578  }
579 
580  flush_put_bits(&pb);
581  return AV_OK;
582  }
583 };
584 
586  public:
588  setCodecID(AV_CODEC_ID_ADPCM_IMA_SSI);
589  sample_formats.push_back(AV_SAMPLE_FMT_S16);
590  }
591  int adpcm_encode_init_impl() {
592  avctx.frame_size = s->block_size * 2 / channels();
593  avctx.block_align = s->block_size;
594  return AV_OK;
595  }
596 
597  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
598  int *got_packet_ptr) {
599  PutBitContext pb;
600  init_put_bits(&pb, dst, pkt_size);
601 
602  av_assert(avctx.trellis == 0);
603 
604  for (int i = 0; i < frame->nb_samples; i++) {
605  for (int ch = 0; ch < channels(); ch++) {
606  put_bits(&pb, 4,
607  adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
608  }
609  }
610 
611  flush_put_bits(&pb);
612  return AV_OK;
613  }
614 };
615 
617  public:
619  setCodecID(AV_CODEC_ID_ADPCM_IMA_ALP);
620  sample_formats.push_back(AV_SAMPLE_FMT_S16);
621  }
622  int adpcm_encode_init_impl() {
623  avctx.frame_size = s->block_size * 2 / channels();
624  avctx.block_align = s->block_size;
625  return AV_OK;
626  }
627 
628  inline uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c,
629  int16_t sample) {
630  const int delta = sample - c->prev_sample;
631  const int step = ff_adpcm_step_table[c->step_index];
632  const int sign = (delta < 0) * 8;
633 
634  int nibble = FFMIN(abs(delta) * 4 / step, 7);
635  int diff = (step * nibble) >> 2;
636  if (sign) diff = -diff;
637 
638  nibble = sign | nibble;
639 
640  c->prev_sample += diff;
641  c->prev_sample = av_clip_int16(c->prev_sample);
642  c->step_index =
643  av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
644  return nibble;
645  }
646 
647  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
648  int *got_packet_ptr) {
649  PutBitContext pb;
650  init_put_bits(&pb, dst, pkt_size);
651 
652  av_assert(avctx.trellis == 0);
653 
654  for (int n = frame->nb_samples / 2; n > 0; n--) {
655  for (int ch = 0; ch < channels(); ch++) {
656  put_bits(&pb, 4,
657  adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
658  put_bits(&pb, 4,
659  adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
660  }
661  samples += channels();
662  }
663 
664  flush_put_bits(&pb);
665  return AV_OK;
666  }
667 };
668 
670  public:
671  EncoderADPCM_MS() {
672  setCodecID(AV_CODEC_ID_ADPCM_MS);
673  sample_formats.push_back(AV_SAMPLE_FMT_S16);
674  }
675  int adpcm_encode_init_impl() {
676  uint8_t *extradata;
677  /* each 16 bits sample gives one nibble
678  and we have 7 bytes per channel overhead */
679  avctx.frame_size = (s->block_size - 7 * channels()) * 2 / channels() + 2;
680  avctx.bits_per_coded_sample = 4;
681  avctx.block_align = s->block_size;
682  if (!(avctx.extradata =
683  (uint8_t *)av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
684  return AVERROR(AVERROR_MEMORY);
685  avctx.extradata_size = 32;
686  extradata = avctx.extradata;
687  bytestream_put_le16(&extradata, avctx.frame_size);
688  bytestream_put_le16(&extradata, 7); /* wNumCoef */
689  for (int i = 0; i < 7; i++) {
690  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
691  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
692  }
693  return AV_OK;
694  }
695 
696  inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
697  int16_t sample) {
698  int predictor, nibble, bias;
699 
700  predictor =
701  (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
702 
703  nibble = sample - predictor;
704  if (nibble >= 0)
705  bias = c->idelta / 2;
706  else
707  bias = -c->idelta / 2;
708 
709  nibble = (nibble + bias) / c->idelta;
710  nibble = av_clip_intp2(nibble, 3) & 0x0F;
711 
712  predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
713 
714  c->sample2 = c->sample1;
715  c->sample1 = av_clip_int16(predictor);
716 
717  c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
718  if (c->idelta < 16) c->idelta = 16;
719 
720  return nibble;
721  }
722 
723  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
724  int *got_packet_ptr) {
725  for (int i = 0; i < channels(); i++) {
726  int predictor = 0;
727  *dst++ = predictor;
728  c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
729  c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
730  }
731  for (int i = 0; i < channels(); i++) {
732  if (c->status[i].idelta < 16) c->status[i].idelta = 16;
733  bytestream_put_le16(&dst, c->status[i].idelta);
734  }
735  for (int i = 0; i < channels(); i++) c->status[i].sample2 = *samples++;
736  for (int i = 0; i < channels(); i++) {
737  c->status[i].sample1 = *samples++;
738  bytestream_put_le16(&dst, c->status[i].sample1);
739  }
740  for (int i = 0; i < channels(); i++)
741  bytestream_put_le16(&dst, c->status[i].sample2);
742 
743  if (avctx.trellis > 0) {
744  const int n = avctx.block_align - 7 * channels();
745  uint8_t *buf = (uint8_t *)av_malloc(2 * n);
746  if (!buf) return AVERROR(AVERROR_MEMORY);
747  if (channels() == 1) {
748  adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
749  for (int i = 0; i < n; i += 2) *dst++ = (buf[i] << 4) | buf[i + 1];
750  } else {
751  adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
752  adpcm_compress_trellis(samples + 1, buf + n, &c->status[1], n,
753  channels());
754  for (int i = 0; i < n; i++) *dst++ = (buf[i] << 4) | buf[n + i];
755  }
756  av_free(buf);
757  } else {
758  for (int i = 7 * channels(); i < avctx.block_align; i++) {
759  int nibble;
760  nibble = adpcm_ms_compress_sample(&c->status[0], *samples++) << 4;
761  nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
762  *dst++ = nibble;
763  }
764  } /* End of CASE */
765  return AV_OK;
766  }
767 };
768 
770  public:
771  EncoderADPCM_SWF() {
772  setCodecID(AV_CODEC_ID_ADPCM_SWF);
773  sample_formats.push_back(AV_SAMPLE_FMT_S16);
774  }
775  int adpcm_encode_init_impl() {
776  if (avctx.sample_rate != 11025 && avctx.sample_rate != 22050 &&
777  avctx.sample_rate != 44100) {
778  av_log(avctx, AV_LOG_ERROR,
779  "Sample rate must be 11025, "
780  "22050 or 44100\n");
781  return AVERROR(AVERROR_INVALID);
782  }
783  avctx.frame_size = 4096; /* Hardcoded according to the SWF spec. */
784  avctx.block_align =
785  (2 + channels() * (22 + 4 * (avctx.frame_size - 1)) + 7) / 8;
786  return AV_OK;
787  }
788 
789  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
790  int *got_packet_ptr) {
791  const int n = frame->nb_samples - 1;
792  PutBitContext pb;
793  init_put_bits(&pb, dst, pkt_size);
794 
795  /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
796  av_assert(n == 4095);
797 
798  // store AdpcmCodeSize
799  put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
800 
801  // init the encoder state
802  for (int i = 0; i < channels(); i++) {
803  // clip step so it fits 6 bits
804  c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
805  put_sbits(&pb, 16, samples[i]);
806  put_bits(&pb, 6, c->status[i].step_index);
807  c->status[i].prev_sample = samples[i];
808  }
809 
810  if (avctx.trellis > 0) {
811  uint8_t buf[8190 /* = 2 * n */];
812  adpcm_compress_trellis(samples + channels(), buf, &c->status[0], n,
813  channels());
814  if (channels() == 2)
815  adpcm_compress_trellis(samples + channels() + 1, buf + n, &c->status[1],
816  n, channels());
817  for (int i = 0; i < n; i++) {
818  put_bits(&pb, 4, buf[i]);
819  if (channels() == 2) put_bits(&pb, 4, buf[n + i]);
820  }
821  } else {
822  for (int i = 1; i < frame->nb_samples; i++) {
823  put_bits(
824  &pb, 4,
825  adpcm_ima_compress_sample(&c->status[0], samples[channels() * i]));
826  if (channels() == 2)
827  put_bits(
828  &pb, 4,
829  adpcm_ima_compress_sample(&c->status[1], samples[2 * i + 1]));
830  }
831  }
832  flush_put_bits(&pb);
833  return AV_OK;
834  }
835 };
836 
838  public:
840  setCodecID(AV_CODEC_ID_ADPCM_YAMAHA);
841  sample_formats.push_back(AV_SAMPLE_FMT_S16);
842  }
843  int adpcm_encode_init_impl() {
844  avctx.frame_size = s->block_size * 2 / channels();
845  avctx.block_align = s->block_size;
846  return AV_OK;
847  }
848 
849  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
850  int *got_packet_ptr) {
851  int n = frame->nb_samples / 2;
852  if (avctx.trellis > 0) {
853  uint8_t *buf = (uint8_t *)av_malloc(2 * n * 2);
854  if (!buf) return AVERROR(AVERROR_MEMORY);
855  n *= 2;
856  if (channels() == 1) {
857  adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
858  for (int i = 0; i < n; i += 2) *dst++ = buf[i] | (buf[i + 1] << 4);
859  } else {
860  adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
861  adpcm_compress_trellis(samples + 1, buf + n, &c->status[1], n,
862  channels());
863  for (int i = 0; i < n; i++) *dst++ = buf[i] | (buf[n + i] << 4);
864  }
865  av_free(buf);
866  } else
867  for (n *= channels(); n > 0; n--) {
868  int nibble;
869  nibble = adpcm_yamaha_compress_sample(&c->status[0], *samples++);
870  nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
871  *dst++ = nibble;
872  }
873  return AV_OK;
874  }
875 };
876 
878  public:
880  setCodecID(AV_CODEC_ID_ADPCM_IMA_APM);
881  sample_formats.push_back(AV_SAMPLE_FMT_S16);
882  }
883  int adpcm_encode_init_impl() {
884  avctx.frame_size = s->block_size * 2 / channels();
885  avctx.block_align = s->block_size;
886 
887  if (!(avctx.extradata =
888  (uint8_t *)av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
889  return AVERROR(AVERROR_MEMORY);
890  avctx.extradata_size = 28;
891  return AV_OK;
892  }
893 
894  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
895  int *got_packet_ptr) {
896  PutBitContext pb;
897  init_put_bits(&pb, dst, pkt_size);
898 
899  av_assert(avctx.trellis == 0);
900 
901  for (int n = frame->nb_samples / 2; n > 0; n--) {
902  for (int ch = 0; ch < channels(); ch++) {
903  put_bits(&pb, 4,
904  adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
905  put_bits(&pb, 4,
906  adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
907  }
908  samples += channels();
909  }
910 
911  flush_put_bits(&pb);
912  return AV_OK;
913  }
914 };
915 
917  public:
919  setCodecID(AV_CODEC_ID_ADPCM_IMA_AMV);
920  sample_formats.push_back(AV_SAMPLE_FMT_S16);
921  }
922  int adpcm_encode_init_impl() {
923  if (avctx.sample_rate != 22050) {
924  av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
925  return AVERROR(AVERROR_INVALID);
926  }
927 
928  if (channels() != 1) {
929  av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
930  return AVERROR(AVERROR_INVALID);
931  }
932 
933  avctx.frame_size = s->block_size;
934  avctx.block_align = 8 + (FFALIGN(avctx.frame_size, 2) / 2);
935  return AV_OK;
936  }
937 
938  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
939  int *got_packet_ptr) {
940  av_assert(channels() == 1);
941 
942  c->status[0].prev_sample = *samples;
943  bytestream_put_le16(&dst, c->status[0].prev_sample);
944  bytestream_put_byte(&dst, c->status[0].step_index);
945  bytestream_put_byte(&dst, 0);
946  bytestream_put_le32(&dst, avctx.frame_size);
947 
948  if (avctx.trellis > 0) {
949  const int n = frame->nb_samples >> 1;
950  uint8_t *buf = (uint8_t *)av_malloc(2 * n);
951 
952  if (!buf) return AVERROR(AVERROR_MEMORY);
953 
954  adpcm_compress_trellis(samples, buf, &c->status[0], 2 * n, channels());
955  for (int i = 0; i < n; i++)
956  bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
957 
958  samples += 2 * n;
959  av_free(buf);
960  } else
961  for (int n = frame->nb_samples >> 1; n > 0; n--) {
962  int nibble;
963  nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
964  nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
965  bytestream_put_byte(&dst, nibble);
966  }
967 
968  if (avctx.frame_size & 1) {
969  int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
970  bytestream_put_byte(&dst, nibble);
971  }
972  return AV_OK;
973  }
974 };
975 
977  public:
979  setCodecID(AV_CODEC_ID_ADPCM_ARGO);
980  sample_formats.push_back(AV_SAMPLE_FMT_S16P);
981  }
982  int adpcm_encode_init_impl() {
983  avctx.frame_size = 32;
984  avctx.block_align = 17 * channels();
985  return AV_OK;
986  }
987 
988  int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
989  int shift, int flag) {
990  int nibble;
991 
992  if (flag)
993  nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
994  else
995  nibble = 4 * s - 4 * cs->sample1;
996 
997  return (nibble >> shift) & 0x0F;
998  }
999 
1000  int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble,
1001  int shift, int flag) {
1002  int sample = sign_extend(nibble, 4) * (1 << shift);
1003 
1004  if (flag)
1005  sample += (8 * cs->sample1) - (4 * cs->sample2);
1006  else
1007  sample += 4 * cs->sample1;
1008 
1009  sample = av_clip_int16(sample >> 2);
1010 
1011  cs->sample2 = cs->sample1;
1012  cs->sample1 = sample;
1013 
1014  return sample;
1015  }
1016 
1017  int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
1018  const int16_t *samples, int nsamples,
1019  int shift, int flag) {
1020  int64_t error = 0;
1021 
1022  if (pb) {
1023  put_bits(pb, 4, shift - 2);
1024  put_bits(pb, 1, 0);
1025  put_bits(pb, 1, !!flag);
1026  put_bits(pb, 2, 0);
1027  }
1028 
1029  for (int n = 0; n < nsamples; n++) {
1030  /* Compress the nibble, then expand it to see how much precision we've
1031  * lost. */
1032  int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
1033  int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
1034 
1035  error += abs(samples[n] - sample);
1036 
1037  if (pb) put_bits(pb, 4, nibble);
1038  }
1039 
1040  return error;
1041  }
1042 
1043  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1044  int *got_packet_ptr) {
1045  PutBitContext pb;
1046  init_put_bits(&pb, dst, pkt_size);
1047 
1048  av_assert(frame->nb_samples == 32);
1049 
1050  for (int ch = 0; ch < channels(); ch++) {
1051  int64_t error = INT64_MAX, tmperr = INT64_MAX;
1052  int shift = 2, flag = 0;
1053  int saved1 = c->status[ch].sample1;
1054  int saved2 = c->status[ch].sample2;
1055 
1056  /* Find the optimal coefficients, bail early if we find a perfect
1057  * result. */
1058  for (int s = 2; s < 18 && tmperr != 0; s++) {
1059  for (int f = 0; f < 2 && tmperr != 0; f++) {
1060  c->status[ch].sample1 = saved1;
1061  c->status[ch].sample2 = saved2;
1062  tmperr = adpcm_argo_compress_block(
1063  c->status + ch, NULL, samples_p[ch], frame->nb_samples, s, f);
1064  if (tmperr < error) {
1065  shift = s;
1066  flag = f;
1067  error = tmperr;
1068  }
1069  }
1070  }
1071 
1072  /* Now actually do the encode. */
1073  c->status[ch].sample1 = saved1;
1074  c->status[ch].sample2 = saved2;
1075  adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
1076  frame->nb_samples, shift, flag);
1077  }
1078 
1079  flush_put_bits(&pb);
1080  return AV_OK;
1081  }
1082 };
1083 
1085  public:
1087  setCodecID(AV_CODEC_ID_ADPCM_IMA_WS);
1088  sample_formats.push_back(AV_SAMPLE_FMT_S16);
1089  }
1090  int adpcm_encode_init_impl() {
1091  /* each 16 bits sample gives one nibble */
1092  avctx.frame_size = s->block_size * 2 / channels();
1093  avctx.block_align = s->block_size;
1094  return AV_OK;
1095  }
1096 
1097  int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1098  int *got_packet_ptr) {
1099  PutBitContext pb;
1100  init_put_bits(&pb, dst, pkt_size);
1101 
1102  av_assert(avctx.trellis == 0);
1103  for (int n = frame->nb_samples / 2; n > 0; n--) {
1104  /* stereo: 1 byte (2 samples) for left, 1 byte for right */
1105  for (int ch = 0; ch < channels(); ch++) {
1106  int t1, t2;
1107  t1 = adpcm_ima_compress_sample(&c->status[ch], *samples++);
1108  t2 = adpcm_ima_compress_sample(&c->status[ch], samples[st]);
1109  put_bits(&pb, 4, t2);
1110  put_bits(&pb, 4, t1);
1111  }
1112  samples += channels();
1113  }
1114  flush_put_bits(&pb);
1115  return AV_OK;
1116  }
1117 };
1118 
1120  public:
1121  static ADPCMEncoder *create(AVCodecID id) {
1122  switch (id) {
1123  case AV_CODEC_ID_ADPCM_IMA_WAV:
1124  return new EncoderADPCM_IMA_WAV();
1125 #if ENABLE_BROKEN_CODECS
1126  case AV_CODEC_ID_ADPCM_IMA_QT:
1127  return new EncoderADPCM_IMA_QT();
1128 #endif
1129  case AV_CODEC_ID_ADPCM_IMA_SSI:
1130  return new EncoderADPCM_IMA_SSI();
1131  case AV_CODEC_ID_ADPCM_IMA_ALP:
1132  return new EncoderADPCM_IMA_ALP();
1133  case AV_CODEC_ID_ADPCM_MS:
1134  return new EncoderADPCM_MS();
1135  case AV_CODEC_ID_ADPCM_SWF:
1136  return new EncoderADPCM_SWF();
1137  case AV_CODEC_ID_ADPCM_YAMAHA:
1138  return new EncoderADPCM_YAMAHA();
1139  case AV_CODEC_ID_ADPCM_IMA_APM:
1140  return new EncoderADPCM_IMA_APM();
1141  case AV_CODEC_ID_ADPCM_IMA_AMV:
1142  return new EncoderADPCM_IMA_AMV();
1143  case AV_CODEC_ID_ADPCM_ARGO:
1144  return new EncoderADPCM_ARGO();
1145  case AV_CODEC_ID_ADPCM_IMA_WS:
1146  return new EncoderADPCM_IMA_WS();
1147 
1148  default:
1149  av_log(avctx, AV_LOG_ERROR, "ERROR: encoder [%d] not implemented\n", id);
1150  return nullptr;
1151  };
1152  }
1153 };
1154 
1155 } // namespace adpcm_ffmpeg
Common ADPCM Functionality.
Definition: ADPCMCodec.h:21
Definition: ADPCMEncoder.h:1119
Definition: ADPCMEncoder.h:256
ADPCM Encoder.
Definition: ADPCMEncoder.h:15
Definition: ADPCMEncoder.h:976
Definition: ADPCMEncoder.h:616
Definition: ADPCMEncoder.h:916
Definition: ADPCMEncoder.h:877
Definition: ADPCMEncoder.h:543
Definition: ADPCMEncoder.h:585
Definition: ADPCMEncoder.h:480
Definition: ADPCMEncoder.h:1084
Definition: ADPCMEncoder.h:669
Definition: ADPCMEncoder.h:769
Definition: ADPCMEncoder.h:837
Definition: adpcm.h:206
Definition: adpcm.h:221
int frame_size
Number of samples per channel in an audio frame.
Definition: adpcm.h:239
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: adpcm.h:242
This structure provides the uncompressed PCM data.
Definition: adpcm.h:151
int16_t ** extended_data
Definition: adpcm.h:185
int nb_samples
Definition: adpcm.h:169
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: adpcm.h:164
This structure stores compressed data. It is typically exported by demuxers and then passed as input ...
Definition: adpcm.h:143
Definition: put_bits.h:51
Definition: adpcm.h:193
Definition: adpcm.h:188