adpcmenc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2003 The ffmpeg Project
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "avcodec.h"
22 #include "get_bits.h"
23 #include "put_bits.h"
24 #include "bytestream.h"
25 #include "adpcm.h"
26 #include "adpcm_data.h"
27 #include "internal.h"
28 
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 #if FF_API_OLD_ENCODE_AUDIO
146  if (!(avctx->coded_frame = avcodec_alloc_frame()))
147  goto error;
148 #endif
149 
150  return 0;
151 error:
152  av_freep(&s->paths);
153  av_freep(&s->node_buf);
154  av_freep(&s->nodep_buf);
155  av_freep(&s->trellis_hash);
156  return ret;
157 }
158 
160 {
161  ADPCMEncodeContext *s = avctx->priv_data;
162 #if FF_API_OLD_ENCODE_AUDIO
163  av_freep(&avctx->coded_frame);
164 #endif
165  av_freep(&s->paths);
166  av_freep(&s->node_buf);
167  av_freep(&s->nodep_buf);
168  av_freep(&s->trellis_hash);
169 
170  return 0;
171 }
172 
173 
175  int16_t sample)
176 {
177  int delta = sample - c->prev_sample;
178  int nibble = FFMIN(7, abs(delta) * 4 /
179  ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
181  ff_adpcm_yamaha_difflookup[nibble]) / 8);
182  c->prev_sample = av_clip_int16(c->prev_sample);
183  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
184  return nibble;
185 }
186 
188  int16_t sample)
189 {
190  int delta = sample - c->prev_sample;
192  int diff = step >> 3;
193  int nibble = 0;
194 
195  if (delta < 0) {
196  nibble = 8;
197  delta = -delta;
198  }
199 
200  for (mask = 4; mask;) {
201  if (delta >= step) {
202  nibble |= mask;
203  delta -= step;
204  diff += step;
205  }
206  step >>= 1;
207  mask >>= 1;
208  }
209 
210  if (nibble & 8)
211  c->prev_sample -= diff;
212  else
213  c->prev_sample += diff;
214 
215  c->prev_sample = av_clip_int16(c->prev_sample);
216  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
217 
218  return nibble;
219 }
220 
222  int16_t sample)
223 {
224  int predictor, nibble, bias;
225 
226  predictor = (((c->sample1) * (c->coeff1)) +
227  (( c->sample2) * (c->coeff2))) / 64;
228 
229  nibble = sample - predictor;
230  if (nibble >= 0)
231  bias = c->idelta / 2;
232  else
233  bias = -c->idelta / 2;
234 
235  nibble = (nibble + bias) / c->idelta;
236  nibble = av_clip(nibble, -8, 7) & 0x0F;
237 
238  predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
239 
240  c->sample2 = c->sample1;
241  c->sample1 = av_clip_int16(predictor);
242 
243  c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
244  if (c->idelta < 16)
245  c->idelta = 16;
246 
247  return nibble;
248 }
249 
251  int16_t sample)
252 {
253  int nibble, delta;
254 
255  if (!c->step) {
256  c->predictor = 0;
257  c->step = 127;
258  }
259 
260  delta = sample - c->predictor;
261 
262  nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
263 
264  c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
265  c->predictor = av_clip_int16(c->predictor);
266  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
267  c->step = av_clip(c->step, 127, 24567);
268 
269  return nibble;
270 }
271 
273  const int16_t *samples, uint8_t *dst,
274  ADPCMChannelStatus *c, int n, int stride)
275 {
276  //FIXME 6% faster if frontier is a compile-time constant
277  ADPCMEncodeContext *s = avctx->priv_data;
278  const int frontier = 1 << avctx->trellis;
279  const int version = avctx->codec->id;
280  TrellisPath *paths = s->paths, *p;
281  TrellisNode *node_buf = s->node_buf;
282  TrellisNode **nodep_buf = s->nodep_buf;
283  TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
284  TrellisNode **nodes_next = nodep_buf + frontier;
285  int pathn = 0, froze = -1, i, j, k, generation = 0;
286  uint8_t *hash = s->trellis_hash;
287  memset(hash, 0xff, 65536 * sizeof(*hash));
288 
289  memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
290  nodes[0] = node_buf + frontier;
291  nodes[0]->ssd = 0;
292  nodes[0]->path = 0;
293  nodes[0]->step = c->step_index;
294  nodes[0]->sample1 = c->sample1;
295  nodes[0]->sample2 = c->sample2;
296  if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
297  version == AV_CODEC_ID_ADPCM_IMA_QT ||
298  version == AV_CODEC_ID_ADPCM_SWF)
299  nodes[0]->sample1 = c->prev_sample;
300  if (version == AV_CODEC_ID_ADPCM_MS)
301  nodes[0]->step = c->idelta;
302  if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
303  if (c->step == 0) {
304  nodes[0]->step = 127;
305  nodes[0]->sample1 = 0;
306  } else {
307  nodes[0]->step = c->step;
308  nodes[0]->sample1 = c->predictor;
309  }
310  }
311 
312  for (i = 0; i < n; i++) {
313  TrellisNode *t = node_buf + frontier*(i&1);
314  TrellisNode **u;
315  int sample = samples[i * stride];
316  int heap_pos = 0;
317  memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
318  for (j = 0; j < frontier && nodes[j]; j++) {
319  // higher j have higher ssd already, so they're likely
320  // to yield a suboptimal next sample too
321  const int range = (j < frontier / 2) ? 1 : 0;
322  const int step = nodes[j]->step;
323  int nidx;
324  if (version == AV_CODEC_ID_ADPCM_MS) {
325  const int predictor = ((nodes[j]->sample1 * c->coeff1) +
326  (nodes[j]->sample2 * c->coeff2)) / 64;
327  const int div = (sample - predictor) / step;
328  const int nmin = av_clip(div-range, -8, 6);
329  const int nmax = av_clip(div+range, -7, 7);
330  for (nidx = nmin; nidx <= nmax; nidx++) {
331  const int nibble = nidx & 0xf;
332  int dec_sample = predictor + nidx * step;
333 #define STORE_NODE(NAME, STEP_INDEX)\
334  int d;\
335  uint32_t ssd;\
336  int pos;\
337  TrellisNode *u;\
338  uint8_t *h;\
339  dec_sample = av_clip_int16(dec_sample);\
340  d = sample - dec_sample;\
341  ssd = nodes[j]->ssd + d*d;\
342  /* Check for wraparound, skip such samples completely. \
343  * Note, changing ssd to a 64 bit variable would be \
344  * simpler, avoiding this check, but it's slower on \
345  * x86 32 bit at the moment. */\
346  if (ssd < nodes[j]->ssd)\
347  goto next_##NAME;\
348  /* Collapse any two states with the same previous sample value. \
349  * One could also distinguish states by step and by 2nd to last
350  * sample, but the effects of that are negligible.
351  * Since nodes in the previous generation are iterated
352  * through a heap, they're roughly ordered from better to
353  * worse, but not strictly ordered. Therefore, an earlier
354  * node with the same sample value is better in most cases
355  * (and thus the current is skipped), but not strictly
356  * in all cases. Only skipping samples where ssd >=
357  * ssd of the earlier node with the same sample gives
358  * slightly worse quality, though, for some reason. */ \
359  h = &hash[(uint16_t) dec_sample];\
360  if (*h == generation)\
361  goto next_##NAME;\
362  if (heap_pos < frontier) {\
363  pos = heap_pos++;\
364  } else {\
365  /* Try to replace one of the leaf nodes with the new \
366  * one, but try a different slot each time. */\
367  pos = (frontier >> 1) +\
368  (heap_pos & ((frontier >> 1) - 1));\
369  if (ssd > nodes_next[pos]->ssd)\
370  goto next_##NAME;\
371  heap_pos++;\
372  }\
373  *h = generation;\
374  u = nodes_next[pos];\
375  if (!u) {\
376  assert(pathn < FREEZE_INTERVAL << avctx->trellis);\
377  u = t++;\
378  nodes_next[pos] = u;\
379  u->path = pathn++;\
380  }\
381  u->ssd = ssd;\
382  u->step = STEP_INDEX;\
383  u->sample2 = nodes[j]->sample1;\
384  u->sample1 = dec_sample;\
385  paths[u->path].nibble = nibble;\
386  paths[u->path].prev = nodes[j]->path;\
387  /* Sift the newly inserted node up in the heap to \
388  * restore the heap property. */\
389  while (pos > 0) {\
390  int parent = (pos - 1) >> 1;\
391  if (nodes_next[parent]->ssd <= ssd)\
392  break;\
393  FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
394  pos = parent;\
395  }\
396  next_##NAME:;
397  STORE_NODE(ms, FFMAX(16,
398  (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
399  }
400  } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
401  version == AV_CODEC_ID_ADPCM_IMA_QT ||
402  version == AV_CODEC_ID_ADPCM_SWF) {
403 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
404  const int predictor = nodes[j]->sample1;\
405  const int div = (sample - predictor) * 4 / STEP_TABLE;\
406  int nmin = av_clip(div - range, -7, 6);\
407  int nmax = av_clip(div + range, -6, 7);\
408  if (nmin <= 0)\
409  nmin--; /* distinguish -0 from +0 */\
410  if (nmax < 0)\
411  nmax--;\
412  for (nidx = nmin; nidx <= nmax; nidx++) {\
413  const int nibble = nidx < 0 ? 7 - nidx : nidx;\
414  int dec_sample = predictor +\
415  (STEP_TABLE *\
416  ff_adpcm_yamaha_difflookup[nibble]) / 8;\
417  STORE_NODE(NAME, STEP_INDEX);\
418  }
419  LOOP_NODES(ima, ff_adpcm_step_table[step],
420  av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
421  } else { //AV_CODEC_ID_ADPCM_YAMAHA
422  LOOP_NODES(yamaha, step,
423  av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
424  127, 24567));
425 #undef LOOP_NODES
426 #undef STORE_NODE
427  }
428  }
429 
430  u = nodes;
431  nodes = nodes_next;
432  nodes_next = u;
433 
434  generation++;
435  if (generation == 255) {
436  memset(hash, 0xff, 65536 * sizeof(*hash));
437  generation = 0;
438  }
439 
440  // prevent overflow
441  if (nodes[0]->ssd > (1 << 28)) {
442  for (j = 1; j < frontier && nodes[j]; j++)
443  nodes[j]->ssd -= nodes[0]->ssd;
444  nodes[0]->ssd = 0;
445  }
446 
447  // merge old paths to save memory
448  if (i == froze + FREEZE_INTERVAL) {
449  p = &paths[nodes[0]->path];
450  for (k = i; k > froze; k--) {
451  dst[k] = p->nibble;
452  p = &paths[p->prev];
453  }
454  froze = i;
455  pathn = 0;
456  // other nodes might use paths that don't coincide with the frozen one.
457  // checking which nodes do so is too slow, so just kill them all.
458  // this also slightly improves quality, but I don't know why.
459  memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
460  }
461  }
462 
463  p = &paths[nodes[0]->path];
464  for (i = n - 1; i > froze; i--) {
465  dst[i] = p->nibble;
466  p = &paths[p->prev];
467  }
468 
469  c->predictor = nodes[0]->sample1;
470  c->sample1 = nodes[0]->sample1;
471  c->sample2 = nodes[0]->sample2;
472  c->step_index = nodes[0]->step;
473  c->step = nodes[0]->step;
474  c->idelta = nodes[0]->step;
475 }
476 
477 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
478  const AVFrame *frame, int *got_packet_ptr)
479 {
480  int n, i, ch, st, pkt_size, ret;
481  const int16_t *samples;
482  int16_t **samples_p;
483  uint8_t *dst;
484  ADPCMEncodeContext *c = avctx->priv_data;
485  uint8_t *buf;
486 
487  samples = (const int16_t *)frame->data[0];
488  samples_p = (int16_t **)frame->extended_data;
489  st = avctx->channels == 2;
490 
491  if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
492  pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
493  else
494  pkt_size = avctx->block_align;
495  if ((ret = ff_alloc_packet(avpkt, pkt_size))) {
496  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
497  return ret;
498  }
499  dst = avpkt->data;
500 
501  switch(avctx->codec->id) {
503  {
504  int blocks, j;
505 
506  blocks = (frame->nb_samples - 1) / 8;
507 
508  for (ch = 0; ch < avctx->channels; ch++) {
509  ADPCMChannelStatus *status = &c->status[ch];
510  status->prev_sample = samples_p[ch][0];
511  /* status->step_index = 0;
512  XXX: not sure how to init the state machine */
513  bytestream_put_le16(&dst, status->prev_sample);
514  *dst++ = status->step_index;
515  *dst++ = 0; /* unknown */
516  }
517 
518  /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
519  if (avctx->trellis > 0) {
520  FF_ALLOC_OR_GOTO(avctx, buf, avctx->channels * blocks * 8, error);
521  for (ch = 0; ch < avctx->channels; ch++) {
522  adpcm_compress_trellis(avctx, &samples_p[ch][1],
523  buf + ch * blocks * 8, &c->status[ch],
524  blocks * 8, 1);
525  }
526  for (i = 0; i < blocks; i++) {
527  for (ch = 0; ch < avctx->channels; ch++) {
528  uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
529  for (j = 0; j < 8; j += 2)
530  *dst++ = buf1[j] | (buf1[j + 1] << 4);
531  }
532  }
533  av_free(buf);
534  } else {
535  for (i = 0; i < blocks; i++) {
536  for (ch = 0; ch < avctx->channels; ch++) {
537  ADPCMChannelStatus *status = &c->status[ch];
538  const int16_t *smp = &samples_p[ch][1 + i * 8];
539  for (j = 0; j < 8; j += 2) {
540  uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
541  v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
542  *dst++ = v;
543  }
544  }
545  }
546  }
547  break;
548  }
550  {
551  PutBitContext pb;
552  init_put_bits(&pb, dst, pkt_size * 8);
553 
554  for (ch = 0; ch < avctx->channels; ch++) {
555  ADPCMChannelStatus *status = &c->status[ch];
556  put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
557  put_bits(&pb, 7, status->step_index);
558  if (avctx->trellis > 0) {
559  uint8_t buf[64];
560  adpcm_compress_trellis(avctx, &samples_p[ch][1], buf, status,
561  64, 1);
562  for (i = 0; i < 64; i++)
563  put_bits(&pb, 4, buf[i ^ 1]);
564  } else {
565  for (i = 0; i < 64; i += 2) {
566  int t1, t2;
567  t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
568  t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
569  put_bits(&pb, 4, t2);
570  put_bits(&pb, 4, t1);
571  }
572  }
573  }
574 
575  flush_put_bits(&pb);
576  break;
577  }
579  {
580  PutBitContext pb;
581  init_put_bits(&pb, dst, pkt_size * 8);
582 
583  n = frame->nb_samples - 1;
584 
585  // store AdpcmCodeSize
586  put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
587 
588  // init the encoder state
589  for (i = 0; i < avctx->channels; i++) {
590  // clip step so it fits 6 bits
591  c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63);
592  put_sbits(&pb, 16, samples[i]);
593  put_bits(&pb, 6, c->status[i].step_index);
594  c->status[i].prev_sample = samples[i];
595  }
596 
597  if (avctx->trellis > 0) {
598  FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
599  adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
600  &c->status[0], n, avctx->channels);
601  if (avctx->channels == 2)
602  adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
603  buf + n, &c->status[1], n,
604  avctx->channels);
605  for (i = 0; i < n; i++) {
606  put_bits(&pb, 4, buf[i]);
607  if (avctx->channels == 2)
608  put_bits(&pb, 4, buf[n + i]);
609  }
610  av_free(buf);
611  } else {
612  for (i = 1; i < frame->nb_samples; i++) {
614  samples[avctx->channels * i]));
615  if (avctx->channels == 2)
617  samples[2 * i + 1]));
618  }
619  }
620  flush_put_bits(&pb);
621  break;
622  }
624  for (i = 0; i < avctx->channels; i++) {
625  int predictor = 0;
626  *dst++ = predictor;
627  c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
628  c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
629  }
630  for (i = 0; i < avctx->channels; i++) {
631  if (c->status[i].idelta < 16)
632  c->status[i].idelta = 16;
633  bytestream_put_le16(&dst, c->status[i].idelta);
634  }
635  for (i = 0; i < avctx->channels; i++)
636  c->status[i].sample2= *samples++;
637  for (i = 0; i < avctx->channels; i++) {
638  c->status[i].sample1 = *samples++;
639  bytestream_put_le16(&dst, c->status[i].sample1);
640  }
641  for (i = 0; i < avctx->channels; i++)
642  bytestream_put_le16(&dst, c->status[i].sample2);
643 
644  if (avctx->trellis > 0) {
645  n = avctx->block_align - 7 * avctx->channels;
646  FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
647  if (avctx->channels == 1) {
648  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
649  avctx->channels);
650  for (i = 0; i < n; i += 2)
651  *dst++ = (buf[i] << 4) | buf[i + 1];
652  } else {
653  adpcm_compress_trellis(avctx, samples, buf,
654  &c->status[0], n, avctx->channels);
655  adpcm_compress_trellis(avctx, samples + 1, buf + n,
656  &c->status[1], n, avctx->channels);
657  for (i = 0; i < n; i++)
658  *dst++ = (buf[i] << 4) | buf[n + i];
659  }
660  av_free(buf);
661  } else {
662  for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
663  int nibble;
664  nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
665  nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
666  *dst++ = nibble;
667  }
668  }
669  break;
671  n = frame->nb_samples / 2;
672  if (avctx->trellis > 0) {
673  FF_ALLOC_OR_GOTO(avctx, buf, 2 * n * 2, error);
674  n *= 2;
675  if (avctx->channels == 1) {
676  adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
677  avctx->channels);
678  for (i = 0; i < n; i += 2)
679  *dst++ = buf[i] | (buf[i + 1] << 4);
680  } else {
681  adpcm_compress_trellis(avctx, samples, buf,
682  &c->status[0], n, avctx->channels);
683  adpcm_compress_trellis(avctx, samples + 1, buf + n,
684  &c->status[1], n, avctx->channels);
685  for (i = 0; i < n; i++)
686  *dst++ = buf[i] | (buf[n + i] << 4);
687  }
688  av_free(buf);
689  } else
690  for (n *= avctx->channels; n > 0; n--) {
691  int nibble;
692  nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
693  nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
694  *dst++ = nibble;
695  }
696  break;
697  default:
698  return AVERROR(EINVAL);
699  }
700 
701  avpkt->size = pkt_size;
702  *got_packet_ptr = 1;
703  return 0;
704 error:
705  return AVERROR(ENOMEM);
706 }
707 
708 static const enum AVSampleFormat sample_fmts[] = {
710 };
711 
712 static const enum AVSampleFormat sample_fmts_p[] = {
714 };
715 
716 #define ADPCM_ENCODER(id_, name_, sample_fmts_, long_name_) \
717 AVCodec ff_ ## name_ ## _encoder = { \
718  .name = #name_, \
719  .type = AVMEDIA_TYPE_AUDIO, \
720  .id = id_, \
721  .priv_data_size = sizeof(ADPCMEncodeContext), \
722  .init = adpcm_encode_init, \
723  .encode2 = adpcm_encode_frame, \
724  .close = adpcm_encode_close, \
725  .sample_fmts = sample_fmts_, \
726  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
727 }
728 
729 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, "ADPCM IMA QuickTime");
730 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, "ADPCM IMA WAV");
731 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, sample_fmts, "ADPCM Microsoft");
732 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, sample_fmts, "ADPCM Shockwave Flash");
733 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, "ADPCM Yamaha");
const struct AVCodec * codec
Definition: avcodec.h:1348
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:61
int sample1
Definition: adpcmenc.c:47
int path
Definition: adpcmenc.c:46
static int16_t * samples
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
Definition: adpcmenc.c:62
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:174
static uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:221
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
#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:916
static uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:187
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
Definition: adpcmenc.c:159
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:2141
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:151
uint8_t * trellis_hash
Definition: adpcmenc.c:57
static uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, int16_t sample)
Definition: adpcmenc.c:250
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:61
uint8_t
float delta
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
ADPCM tables.
uint8_t * data
Definition: avcodec.h:915
bitstream reader API header.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2704
static float t
uint32_t ssd
Definition: adpcmenc.c:45
enum AVCodecID id
Definition: avcodec.h:2974
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1811
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
ADPCM encoder/decoder common header.
static const uint16_t mask[17]
Definition: lzw.c:38
#define STORE_NODE(NAME, STEP_INDEX)
const int16_t ff_adpcm_step_table[89]
This is the step table.
Definition: adpcm_data.c:40
#define t1
Definition: regdef.h:29
int16_t sample2
Definition: adpcm.h:42
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:136
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:616
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:174
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:272
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:477
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:878
TrellisPath * paths
Definition: adpcmenc.c:54
int sample2
Definition: adpcmenc.c:48
TrellisNode * node_buf
Definition: adpcmenc.c:55
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2124
const int16_t ff_adpcm_AdaptationTable[]
Definition: adpcm_data.c:55
external API header
version
Definition: ffv1enc.c:1069
enum AVCodecID codec_id
Definition: avcodec.h:1350
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:63
int sample_rate
samples per second
Definition: avcodec.h:2104
main external API structure.
Definition: avcodec.h:1339
int nibble
Definition: adpcmenc.c:40
int extradata_size
Definition: avcodec.h:1455
int step
Definition: adpcmenc.c:49
static int step
Definition: avplay.c:252
#define ADPCM_ENCODER(id_, name_, sample_fmts_, long_name_)
Definition: adpcmenc.c:716
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
Definition: vf_drawbox.c:36
const int8_t ff_adpcm_yamaha_difflookup[]
Definition: adpcm_data.c:75
struct TrellisNode TrellisNode
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:86
const int16_t ff_adpcm_yamaha_indexscale[]
Definition: adpcm_data.c:70
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
Definition: internal.h:60
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:52
int trellis
trellis RD quantization
Definition: avcodec.h:2444
void * priv_data
Definition: avcodec.h:1382
int channels
number of audio channels
Definition: avcodec.h:2105
struct TrellisPath TrellisPath
signed 16 bits, planar
Definition: samplefmt.h:58
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:708
struct ADPCMEncodeContext ADPCMEncodeContext
int16_t step_index
Definition: adpcm.h:35
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avcodec.h:1028
ADPCMChannelStatus status[6]
Definition: adpcmenc.c:53
This structure stores compressed data.
Definition: avcodec.h:898
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042
for(j=16;j >0;--j)
static enum AVSampleFormat sample_fmts_p[]
Definition: adpcmenc.c:712
#define t2
Definition: regdef.h:30
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)
bitstream writer API