Libav
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 Libav.
9  *
10  * Libav 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  * Libav 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 Libav; 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 "get_bits.h"
27 #include "put_bits.h"
28 #include "bytestream.h"
29 #include "adpcm.h"
30 #include "adpcm_data.h"
31 #include "internal.h"
32 
39 typedef struct TrellisPath {
40  int nibble;
41  int prev;
42 } TrellisPath;
43 
44 typedef struct TrellisNode {
45  uint32_t ssd;
46  int path;
47  int sample1;
48  int sample2;
49  int step;
50 } TrellisNode;
51 
52 typedef struct ADPCMEncodeContext {
59 
60 #define FREEZE_INTERVAL 128
61 
63 {
64  ADPCMEncodeContext *s = avctx->priv_data;
65  uint8_t *extradata;
66  int i;
67  int ret = AVERROR(ENOMEM);
68 
69  if (avctx->channels > 2) {
70  av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
71  return AVERROR(EINVAL);
72  }
73 
74  if (avctx->trellis && (unsigned)avctx->trellis > 16U) {
75  av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
76  return AVERROR(EINVAL);
77  }
78 
79  if (avctx->trellis) {
80  int frontier = 1 << avctx->trellis;
81  int max_paths = frontier * FREEZE_INTERVAL;
82  FF_ALLOC_OR_GOTO(avctx, s->paths,
83  max_paths * sizeof(*s->paths), error);
84  FF_ALLOC_OR_GOTO(avctx, s->node_buf,
85  2 * frontier * sizeof(*s->node_buf), error);
86  FF_ALLOC_OR_GOTO(avctx, s->nodep_buf,
87  2 * frontier * sizeof(*s->nodep_buf), error);
89  65536 * sizeof(*s->trellis_hash), error);
90  }
91 
93 
94  switch (avctx->codec->id) {
96  /* each 16 bits sample gives one nibble
97  and we have 4 bytes per channel overhead */
98  avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 /
99  (4 * avctx->channels) + 1;
100  /* seems frame_size isn't taken into account...
101  have to buffer the samples :-( */
102  avctx->block_align = BLKSIZE;
103  break;
105  avctx->frame_size = 64;
106  avctx->block_align = 34 * avctx->channels;
107  break;
109  /* each 16 bits sample gives one nibble
110  and we have 7 bytes per channel overhead */
111  avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 /
112  avctx->channels + 2;
113  avctx->block_align = BLKSIZE;
115  goto error;
116  avctx->extradata_size = 32;
117  extradata = avctx->extradata;
118  bytestream_put_le16(&extradata, avctx->frame_size);
119  bytestream_put_le16(&extradata, 7); /* wNumCoef */
120  for (i = 0; i < 7; i++) {
121  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
122  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
123  }
124  break;
126  avctx->frame_size = BLKSIZE * 2 / avctx->channels;
127  avctx->block_align = BLKSIZE;
128  break;
130  if (avctx->sample_rate != 11025 &&
131  avctx->sample_rate != 22050 &&
132  avctx->sample_rate != 44100) {
133  av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
134  "22050 or 44100\n");
135  ret = AVERROR(EINVAL);
136  goto error;
137  }
138  avctx->frame_size = 512 * (avctx->sample_rate / 11025);
139  break;
140  default:
141  ret = AVERROR(EINVAL);
142  goto error;
143  }
144 
145  return 0;
146 error:
147  av_freep(&s->paths);
148  av_freep(&s->node_buf);
149  av_freep(&s->nodep_buf);
150  av_freep(&s->trellis_hash);
151  return ret;
152 }
153 
155 {
156  ADPCMEncodeContext *s = avctx->priv_data;
157  av_freep(&s->paths);
158  av_freep(&s->node_buf);
159  av_freep(&s->nodep_buf);
160  av_freep(&s->trellis_hash);
161 
162  return 0;
163 }
164 
165 
167  int16_t sample)
168 {
169  int delta = sample - c->prev_sample;
170  int nibble = FFMIN(7, abs(delta) * 4 /
171  ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
173  ff_adpcm_yamaha_difflookup[nibble]) / 8);
174  c->prev_sample = av_clip_int16(c->prev_sample);
175  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
176  return nibble;
177 }
178 
180  int16_t sample)
181 {
182  int delta = sample - c->prev_sample;
184  int diff = step >> 3;
185  int nibble = 0;
186 
187  if (delta < 0) {
188  nibble = 8;
189  delta = -delta;
190  }
191 
192  for (mask = 4; mask;) {
193  if (delta >= step) {
194  nibble |= mask;
195  delta -= step;
196  diff += step;
197  }
198  step >>= 1;
199  mask >>= 1;
200  }
201 
202  if (nibble & 8)
203  c->prev_sample -= diff;
204  else
205  c->prev_sample += diff;
206 
207  c->prev_sample = av_clip_int16(c->prev_sample);
208  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
209 
210  return nibble;
211 }
212 
214  int16_t sample)
215 {
216  int predictor, nibble, bias;
217 
218  predictor = (((c->sample1) * (c->coeff1)) +
219  (( c->sample2) * (c->coeff2))) / 64;
220 
221  nibble = sample - predictor;
222  if (nibble >= 0)
223  bias = c->idelta / 2;
224  else
225  bias = -c->idelta / 2;
226 
227  nibble = (nibble + bias) / c->idelta;
228  nibble = av_clip(nibble, -8, 7) & 0x0F;
229 
230  predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
231 
232  c->sample2 = c->sample1;
233  c->sample1 = av_clip_int16(predictor);
234 
235  c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
236  if (c->idelta < 16)
237  c->idelta = 16;
238 
239  return nibble;
240 }
241 
243  int16_t sample)
244 {
245  int nibble, delta;
246 
247  if (!c->step) {
248  c->predictor = 0;
249  c->step = 127;
250  }
251 
252  delta = sample - c->predictor;
253 
254  nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
255 
256  c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
257  c->predictor = av_clip_int16(c->predictor);
258  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
259  c->step = av_clip(c->step, 127, 24567);
260 
261  return nibble;
262 }
263 
265  const int16_t *samples, uint8_t *dst,
266  ADPCMChannelStatus *c, int n, int stride)
267 {
268  //FIXME 6% faster if frontier is a compile-time constant
269  ADPCMEncodeContext *s = avctx->priv_data;
270  const int frontier = 1 << avctx->trellis;
271  const int version = avctx->codec->id;
272  TrellisPath *paths = s->paths, *p;
273  TrellisNode *node_buf = s->node_buf;
274  TrellisNode **nodep_buf = s->nodep_buf;
275  TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
276  TrellisNode **nodes_next = nodep_buf + frontier;
277  int pathn = 0, froze = -1, i, j, k, generation = 0;
278  uint8_t *hash = s->trellis_hash;
279  memset(hash, 0xff, 65536 * sizeof(*hash));
280 
281  memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
282  nodes[0] = node_buf + frontier;
283  nodes[0]->ssd = 0;
284  nodes[0]->path = 0;
285  nodes[0]->step = c->step_index;
286  nodes[0]->sample1 = c->sample1;
287  nodes[0]->sample2 = c->sample2;
288  if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
289  version == AV_CODEC_ID_ADPCM_IMA_QT ||
290  version == AV_CODEC_ID_ADPCM_SWF)
291  nodes[0]->sample1 = c->prev_sample;
292  if (version == AV_CODEC_ID_ADPCM_MS)
293  nodes[0]->step = c->idelta;
294  if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
295  if (c->step == 0) {
296  nodes[0]->step = 127;
297  nodes[0]->sample1 = 0;
298  } else {
299  nodes[0]->step = c->step;
300  nodes[0]->sample1 = c->predictor;
301  }
302  }
303 
304  for (i = 0; i < n; i++) {
305  TrellisNode *t = node_buf + frontier*(i&1);
306  TrellisNode **u;
307  int sample = samples[i * stride];
308  int heap_pos = 0;
309  memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
310  for (j = 0; j < frontier && nodes[j]; j++) {
311  // higher j have higher ssd already, so they're likely
312  // to yield a suboptimal next sample too
313  const int range = (j < frontier / 2) ? 1 : 0;
314  const int step = nodes[j]->step;
315  int nidx;
316  if (version == AV_CODEC_ID_ADPCM_MS) {
317  const int predictor = ((nodes[j]->sample1 * c->coeff1) +
318  (nodes[j]->sample2 * c->coeff2)) / 64;
319  const int div = (sample - predictor) / step;
320  const int nmin = av_clip(div-range, -8, 6);
321  const int nmax = av_clip(div+range, -7, 7);
322  for (nidx = nmin; nidx <= nmax; nidx++) {
323  const int nibble = nidx & 0xf;
324  int dec_sample = predictor + nidx * step;
325 #define STORE_NODE(NAME, STEP_INDEX)\
326  int d;\
327  uint32_t ssd;\
328  int pos;\
329  TrellisNode *u;\
330  uint8_t *h;\
331  dec_sample = av_clip_int16(dec_sample);\
332  d = sample - dec_sample;\
333  ssd = nodes[j]->ssd + d*d;\
334  /* Check for wraparound, skip such samples completely. \
335  * Note, changing ssd to a 64 bit variable would be \
336  * simpler, avoiding this check, but it's slower on \
337  * x86 32 bit at the moment. */\
338  if (ssd < nodes[j]->ssd)\
339  goto next_##NAME;\
340  /* Collapse any two states with the same previous sample value. \
341  * One could also distinguish states by step and by 2nd to last
342  * sample, but the effects of that are negligible.
343  * Since nodes in the previous generation are iterated
344  * through a heap, they're roughly ordered from better to
345  * worse, but not strictly ordered. Therefore, an earlier
346  * node with the same sample value is better in most cases
347  * (and thus the current is skipped), but not strictly
348  * in all cases. Only skipping samples where ssd >=
349  * ssd of the earlier node with the same sample gives
350  * slightly worse quality, though, for some reason. */ \
351  h = &hash[(uint16_t) dec_sample];\
352  if (*h == generation)\
353  goto next_##NAME;\
354  if (heap_pos < frontier) {\
355  pos = heap_pos++;\
356  } else {\
357  /* Try to replace one of the leaf nodes with the new \
358  * one, but try a different slot each time. */\
359  pos = (frontier >> 1) +\
360  (heap_pos & ((frontier >> 1) - 1));\
361  if (ssd > nodes_next[pos]->ssd)\
362  goto next_##NAME;\
363  heap_pos++;\
364  }\
365  *h = generation;\
366  u = nodes_next[pos];\
367  if (!u) {\
368  assert(pathn < FREEZE_INTERVAL << avctx->trellis);\
369  u = t++;\
370  nodes_next[pos] = u;\
371  u->path = pathn++;\
372  }\
373  u->ssd = ssd;\
374  u->step = STEP_INDEX;\
375  u->sample2 = nodes[j]->sample1;\
376  u->sample1 = dec_sample;\
377  paths[u->path].nibble = nibble;\
378  paths[u->path].prev = nodes[j]->path;\
379  /* Sift the newly inserted node up in the heap to \
380  * restore the heap property. */\
381  while (pos > 0) {\
382  int parent = (pos - 1) >> 1;\
383  if (nodes_next[parent]->ssd <= ssd)\
384  break;\
385  FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
386  pos = parent;\
387  }\
388  next_##NAME:;
389  STORE_NODE(ms, FFMAX(16,
390  (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
391  }
392  } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
393  version == AV_CODEC_ID_ADPCM_IMA_QT ||
394  version == AV_CODEC_ID_ADPCM_SWF) {
395 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
396  const int predictor = nodes[j]->sample1;\
397  const int div = (sample - predictor) * 4 / STEP_TABLE;\
398  int nmin = av_clip(div - range, -7, 6);\
399  int nmax = av_clip(div + range, -6, 7);\
400  if (nmin <= 0)\
401  nmin--; /* distinguish -0 from +0 */\
402  if (nmax < 0)\
403  nmax--;\
404  for (nidx = nmin; nidx <= nmax; nidx++) {\
405  const int nibble = nidx < 0 ? 7 - nidx : nidx;\
406  int dec_sample = predictor +\
407  (STEP_TABLE *\
408  ff_adpcm_yamaha_difflookup[nibble]) / 8;\
409  STORE_NODE(NAME, STEP_INDEX);\
410  }
411  LOOP_NODES(ima, ff_adpcm_step_table[step],
412  av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
413  } else { //AV_CODEC_ID_ADPCM_YAMAHA
414  LOOP_NODES(yamaha, step,
415  av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
416  127, 24567));
417 #undef LOOP_NODES
418 #undef STORE_NODE
419  }
420  }
421 
422  u = nodes;
423  nodes = nodes_next;
424  nodes_next = u;
425 
426  generation++;
427  if (generation == 255) {
428  memset(hash, 0xff, 65536 * sizeof(*hash));
429  generation = 0;
430  }
431 
432  // prevent overflow
433  if (nodes[0]->ssd > (1 << 28)) {
434  for (j = 1; j < frontier && nodes[j]; j++)
435  nodes[j]->ssd -= nodes[0]->ssd;
436  nodes[0]->ssd = 0;
437  }
438 
439  // merge old paths to save memory
440  if (i == froze + FREEZE_INTERVAL) {
441  p = &paths[nodes[0]->path];
442  for (k = i; k > froze; k--) {
443  dst[k] = p->nibble;
444  p = &paths[p->prev];
445  }
446  froze = i;
447  pathn = 0;
448  // other nodes might use paths that don't coincide with the frozen one.
449  // checking which nodes do so is too slow, so just kill them all.
450  // this also slightly improves quality, but I don't know why.
451  memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
452  }
453  }
454 
455  p = &paths[nodes[0]->path];
456  for (i = n - 1; i > froze; i--) {
457  dst[i] = p->nibble;
458  p = &paths[p->prev];
459  }
460 
461  c->predictor = nodes[0]->sample1;
462  c->sample1 = nodes[0]->sample1;
463  c->sample2 = nodes[0]->sample2;
464  c->step_index = nodes[0]->step;
465  c->step = nodes[0]->step;
466  c->idelta = nodes[0]->step;
467 }
468 
469 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
470  const AVFrame *frame, int *got_packet_ptr)
471 {
472  int n, i, ch, st, pkt_size, ret;
473  const int16_t *samples;
474  int16_t **samples_p;
475  uint8_t *dst;
476  ADPCMEncodeContext *c = avctx->priv_data;
477  uint8_t *buf;
478 
479  samples = (const int16_t *)frame->data[0];
480  samples_p = (int16_t **)frame->extended_data;
481  st = avctx->channels == 2;
482 
483  if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
484  pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
485  else
486  pkt_size = avctx->block_align;
487  if ((ret = ff_alloc_packet(avpkt, pkt_size))) {
488  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
489  return ret;
490  }
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_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 * 8);
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][1], buf, status,
553  64, 1);
554  for (i = 0; i < 64; i++)
555  put_bits(&pb, 4, buf[i ^ 1]);
556  } else {
557  for (i = 0; i < 64; i += 2) {
558  int t1, t2;
559  t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
560  t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
561  put_bits(&pb, 4, t2);
562  put_bits(&pb, 4, t1);
563  }
564  }
565  }
566 
567  flush_put_bits(&pb);
568  break;
569  }
571  {
572  PutBitContext pb;
573  init_put_bits(&pb, dst, pkt_size * 8);
574 
575  n = frame->nb_samples - 1;
576 
577  // store AdpcmCodeSize
578  put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
579 
580  // init the encoder state
581  for (i = 0; i < avctx->channels; i++) {
582  // clip step so it fits 6 bits
583  c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63);
584  put_sbits(&pb, 16, samples[i]);
585  put_bits(&pb, 6, c->status[i].step_index);
586  c->status[i].prev_sample = samples[i];
587  }
588 
589  if (avctx->trellis > 0) {
590  FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
591  adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
592  &c->status[0], n, avctx->channels);
593  if (avctx->channels == 2)
594  adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
595  buf + n, &c->status[1], n,
596  avctx->channels);
597  for (i = 0; i < n; i++) {
598  put_bits(&pb, 4, buf[i]);
599  if (avctx->channels == 2)
600  put_bits(&pb, 4, buf[n + i]);
601  }
602  av_free(buf);
603  } else {
604  for (i = 1; i < frame->nb_samples; i++) {
606  samples[avctx->channels * i]));
607  if (avctx->channels == 2)
609  samples[2 * i + 1]));
610  }
611  }
612  flush_put_bits(&pb);
613  break;
614  }
616  for (i = 0; i < avctx->channels; i++) {
617  int predictor = 0;
618  *dst++ = predictor;
619  c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
620  c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
621  }
622  for (i = 0; i < avctx->channels; i++) {
623  if (c->status[i].idelta < 16)
624  c->status[i].idelta = 16;
625  bytestream_put_le16(&dst, c->status[i].idelta);
626  }
627  for (i = 0; i < avctx->channels; i++)
628  c->status[i].sample2= *samples++;
629  for (i = 0; i < avctx->channels; i++) {
630  c->status[i].sample1 = *samples++;
631  bytestream_put_le16(&dst, c->status[i].sample1);
632  }
633  for (i = 0; i < avctx->channels; i++)
634  bytestream_put_le16(&dst, c->status[i].sample2);
635 
636  if (avctx->trellis > 0) {
637  n = avctx->block_align - 7 * avctx->channels;
638  FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
639  if (avctx->channels == 1) {
640  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
641  avctx->channels);
642  for (i = 0; i < n; i += 2)
643  *dst++ = (buf[i] << 4) | buf[i + 1];
644  } else {
645  adpcm_compress_trellis(avctx, samples, buf,
646  &c->status[0], n, avctx->channels);
647  adpcm_compress_trellis(avctx, samples + 1, buf + n,
648  &c->status[1], n, avctx->channels);
649  for (i = 0; i < n; i++)
650  *dst++ = (buf[i] << 4) | buf[n + i];
651  }
652  av_free(buf);
653  } else {
654  for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
655  int nibble;
656  nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
657  nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
658  *dst++ = nibble;
659  }
660  }
661  break;
663  n = frame->nb_samples / 2;
664  if (avctx->trellis > 0) {
665  FF_ALLOC_OR_GOTO(avctx, buf, 2 * n * 2, error);
666  n *= 2;
667  if (avctx->channels == 1) {
668  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
669  avctx->channels);
670  for (i = 0; i < n; i += 2)
671  *dst++ = buf[i] | (buf[i + 1] << 4);
672  } else {
673  adpcm_compress_trellis(avctx, samples, buf,
674  &c->status[0], n, avctx->channels);
675  adpcm_compress_trellis(avctx, samples + 1, buf + n,
676  &c->status[1], n, avctx->channels);
677  for (i = 0; i < n; i++)
678  *dst++ = buf[i] | (buf[n + i] << 4);
679  }
680  av_free(buf);
681  } else
682  for (n *= avctx->channels; n > 0; n--) {
683  int nibble;
684  nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
685  nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
686  *dst++ = nibble;
687  }
688  break;
689  default:
690  return AVERROR(EINVAL);
691  }
692 
693  avpkt->size = pkt_size;
694  *got_packet_ptr = 1;
695  return 0;
696 error:
697  return AVERROR(ENOMEM);
698 }
699 
700 static const enum AVSampleFormat sample_fmts[] = {
702 };
703 
704 static const enum AVSampleFormat sample_fmts_p[] = {
706 };
707 
708 #define ADPCM_ENCODER(id_, name_, sample_fmts_, long_name_) \
709 AVCodec ff_ ## name_ ## _encoder = { \
710  .name = #name_, \
711  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
712  .type = AVMEDIA_TYPE_AUDIO, \
713  .id = id_, \
714  .priv_data_size = sizeof(ADPCMEncodeContext), \
715  .init = adpcm_encode_init, \
716  .encode2 = adpcm_encode_frame, \
717  .close = adpcm_encode_close, \
718  .sample_fmts = sample_fmts_, \
719 }
720 
721 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, "ADPCM IMA QuickTime");
722 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, "ADPCM IMA WAV");
723 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, sample_fmts, "ADPCM Microsoft");
724 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, sample_fmts, "ADPCM Shockwave Flash");
725 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, "ADPCM Yamaha");
const struct AVCodec * codec
Definition: avcodec.h:1063
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int sample1
Definition: adpcmenc.c:47
int path
Definition: adpcmenc.c:46
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
Definition: adpcmenc.c:62
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:177
static uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:213
#define BLKSIZE
Definition: adpcm.h:31
static int hash(int head, const int add)
Hash function adding character.
Definition: lzwenc.c:74
int size
Definition: avcodec.h:974
static uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:179
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
Definition: adpcmenc.c:154
static int16_t * samples
Definition: output.c:53
signed 16 bits
Definition: samplefmt.h:52
#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:1816
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Definition: vf_drawbox.c:37
uint8_t * trellis_hash
Definition: adpcmenc.c:57
static uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:242
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:61
uint8_t
#define av_cold
Definition: attributes.h:66
float delta
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1162
ADPCM tables.
uint8_t * data
Definition: avcodec.h:973
bitstream reader API header.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2481
static float t
Definition: output.c:52
uint32_t ssd
Definition: adpcmenc.c:45
enum AVCodecID id
Definition: avcodec.h:2769
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1938
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
ADPCM encoder/decoder common header.
static const uint16_t mask[17]
Definition: lzw.c:38
#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:40
int16_t sample2
Definition: adpcm.h:42
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:139
#define FFMAX(a, b)
Definition: common.h:55
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:509
const int8_t ff_adpcm_index_table[16]
Definition: adpcm_data.c:31
#define FREEZE_INTERVAL
Definition: adpcmenc.c:60
static uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:166
#define FFMIN(a, b)
Definition: common.h:57
TrellisNode ** nodep_buf
Definition: adpcmenc.c:56
const int8_t ff_adpcm_AdaptCoeff2[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:66
static void adpcm_compress_trellis(AVCodecContext *avctx, const int16_t *samples, uint8_t *dst, ADPCMChannelStatus *c, int n, int stride)
Definition: adpcmenc.c:264
int16_t sample1
Definition: adpcm.h:41
static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: adpcmenc.c:469
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1125
TrellisPath * paths
Definition: adpcmenc.c:54
int sample2
Definition: adpcmenc.c:48
if(ac->has_optimized_func)
TrellisNode * node_buf
Definition: adpcmenc.c:55
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1799
const int16_t ff_adpcm_AdaptationTable[]
Definition: adpcm_data.c:55
Libavcodec external API header.
version
Definition: ffv1enc.c:1080
enum AVCodecID codec_id
Definition: avcodec.h:1065
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
int sample_rate
samples per second
Definition: avcodec.h:1779
main external API structure.
Definition: avcodec.h:1054
int nibble
Definition: adpcmenc.c:40
int extradata_size
Definition: avcodec.h:1163
int step
Definition: adpcmenc.c:49
static int step
Definition: avplay.c:247
#define ADPCM_ENCODER(id_, name_, sample_fmts_, long_name_)
Definition: adpcmenc.c:708
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
const int8_t ff_adpcm_yamaha_difflookup[]
Definition: adpcm_data.c:75
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:88
const int16_t ff_adpcm_yamaha_indexscale[]
Definition: adpcm_data.c:70
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
Definition: internal.h:109
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
#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:53
int trellis
trellis RD quantization
Definition: avcodec.h:2220
void * priv_data
Definition: avcodec.h:1090
int channels
number of audio channels
Definition: avcodec.h:1780
signed 16 bits, planar
Definition: samplefmt.h:58
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
int16_t step_index
Definition: adpcm.h:35
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:141
ADPCMChannelStatus status[6]
Definition: adpcmenc.c:53
This structure stores compressed data.
Definition: avcodec.h:950
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:151
for(j=16;j >0;--j)
static enum AVSampleFormat sample_fmts_p[]
Definition: adpcmenc.c:704
bitstream writer API