FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "put_bits.h"
23 #include "bytestream.h"
24 #include "adpcm.h"
25 #include "adpcm_data.h"
26 #include "internal.h"
27 
28 /**
29  * @file
30  * ADPCM encoders
31  * First version by Francois Revol (revol@free.fr)
32  * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
33  * by Mike Melanson (melanson@pcisys.net)
34  *
35  * See ADPCM decoder reference documents for codec information.
36  */
37 
38 typedef struct TrellisPath {
39  int nibble;
40  int prev;
41 } TrellisPath;
42 
43 typedef struct TrellisNode {
44  uint32_t ssd;
45  int path;
46  int sample1;
47  int sample2;
48  int step;
49 } TrellisNode;
50 
51 typedef struct ADPCMEncodeContext {
58 
59 #define FREEZE_INTERVAL 128
60 
61 static av_cold int adpcm_encode_close(AVCodecContext *avctx);
62 
64 {
65  ADPCMEncodeContext *s = avctx->priv_data;
66  uint8_t *extradata;
67  int i;
68  int ret = AVERROR(ENOMEM);
69 
70  if (avctx->channels > 2) {
71  av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
72  return AVERROR(EINVAL);
73  }
74 
75  if (avctx->trellis && (unsigned)avctx->trellis > 16U) {
76  av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
77  return AVERROR(EINVAL);
78  }
79 
80  if (avctx->trellis) {
81  int frontier = 1 << avctx->trellis;
82  int max_paths = frontier * FREEZE_INTERVAL;
83  FF_ALLOC_OR_GOTO(avctx, s->paths,
84  max_paths * sizeof(*s->paths), error);
85  FF_ALLOC_OR_GOTO(avctx, s->node_buf,
86  2 * frontier * sizeof(*s->node_buf), error);
87  FF_ALLOC_OR_GOTO(avctx, s->nodep_buf,
88  2 * frontier * sizeof(*s->nodep_buf), error);
90  65536 * sizeof(*s->trellis_hash), error);
91  }
92 
94 
95  switch (avctx->codec->id) {
97  /* each 16 bits sample gives one nibble
98  and we have 4 bytes per channel overhead */
99  avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 /
100  (4 * avctx->channels) + 1;
101  /* seems frame_size isn't taken into account...
102  have to buffer the samples :-( */
103  avctx->block_align = BLKSIZE;
104  avctx->bits_per_coded_sample = 4;
105  break;
107  avctx->frame_size = 64;
108  avctx->block_align = 34 * avctx->channels;
109  break;
111  /* each 16 bits sample gives one nibble
112  and we have 7 bytes per channel overhead */
113  avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2;
114  avctx->bits_per_coded_sample = 4;
115  avctx->block_align = BLKSIZE;
117  goto error;
118  avctx->extradata_size = 32;
119  extradata = avctx->extradata;
120  bytestream_put_le16(&extradata, avctx->frame_size);
121  bytestream_put_le16(&extradata, 7); /* wNumCoef */
122  for (i = 0; i < 7; i++) {
123  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
124  bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
125  }
126  break;
128  avctx->frame_size = BLKSIZE * 2 / avctx->channels;
129  avctx->block_align = BLKSIZE;
130  break;
132  if (avctx->sample_rate != 11025 &&
133  avctx->sample_rate != 22050 &&
134  avctx->sample_rate != 44100) {
135  av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
136  "22050 or 44100\n");
137  ret = AVERROR(EINVAL);
138  goto error;
139  }
140  avctx->frame_size = 512 * (avctx->sample_rate / 11025);
141  break;
142  default:
143  ret = AVERROR(EINVAL);
144  goto error;
145  }
146 
147 #if FF_API_OLD_ENCODE_AUDIO
148  if (!(avctx->coded_frame = avcodec_alloc_frame()))
149  goto error;
150 #endif
151 
152  return 0;
153 error:
154  adpcm_encode_close(avctx);
155  return ret;
156 }
157 
159 {
160  ADPCMEncodeContext *s = avctx->priv_data;
161 #if FF_API_OLD_ENCODE_AUDIO
162  av_freep(&avctx->coded_frame);
163 #endif
164  av_freep(&s->paths);
165  av_freep(&s->node_buf);
166  av_freep(&s->nodep_buf);
167  av_freep(&s->trellis_hash);
168 
169  return 0;
170 }
171 
172 
174  int16_t sample)
175 {
176  int delta = sample - c->prev_sample;
177  int nibble = FFMIN(7, abs(delta) * 4 /
178  ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
180  ff_adpcm_yamaha_difflookup[nibble]) / 8);
181  c->prev_sample = av_clip_int16(c->prev_sample);
182  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
183  return nibble;
184 }
185 
187  int16_t sample)
188 {
189  int delta = sample - c->prev_sample;
190  int diff, step = ff_adpcm_step_table[c->step_index];
191  int nibble = 8*(delta < 0);
192 
193  delta= abs(delta);
194  diff = delta + (step >> 3);
195 
196  if (delta >= step) {
197  nibble |= 4;
198  delta -= step;
199  }
200  step >>= 1;
201  if (delta >= step) {
202  nibble |= 2;
203  delta -= step;
204  }
205  step >>= 1;
206  if (delta >= step) {
207  nibble |= 1;
208  delta -= step;
209  }
210  diff -= delta;
211 
212  if (nibble & 8)
213  c->prev_sample -= diff;
214  else
215  c->prev_sample += diff;
216 
217  c->prev_sample = av_clip_int16(c->prev_sample);
218  c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
219 
220  return nibble;
221 }
222 
224  int16_t sample)
225 {
226  int predictor, nibble, bias;
227 
228  predictor = (((c->sample1) * (c->coeff1)) +
229  (( c->sample2) * (c->coeff2))) / 64;
230 
231  nibble = sample - predictor;
232  if (nibble >= 0)
233  bias = c->idelta / 2;
234  else
235  bias = -c->idelta / 2;
236 
237  nibble = (nibble + bias) / c->idelta;
238  nibble = av_clip(nibble, -8, 7) & 0x0F;
239 
240  predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
241 
242  c->sample2 = c->sample1;
243  c->sample1 = av_clip_int16(predictor);
244 
245  c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
246  if (c->idelta < 16)
247  c->idelta = 16;
248 
249  return nibble;
250 }
251 
253  int16_t sample)
254 {
255  int nibble, delta;
256 
257  if (!c->step) {
258  c->predictor = 0;
259  c->step = 127;
260  }
261 
262  delta = sample - c->predictor;
263 
264  nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
265 
266  c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
267  c->predictor = av_clip_int16(c->predictor);
268  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
269  c->step = av_clip(c->step, 127, 24567);
270 
271  return nibble;
272 }
273 
275  const int16_t *samples, uint8_t *dst,
276  ADPCMChannelStatus *c, int n, int stride)
277 {
278  //FIXME 6% faster if frontier is a compile-time constant
279  ADPCMEncodeContext *s = avctx->priv_data;
280  const int frontier = 1 << avctx->trellis;
281  const int version = avctx->codec->id;
282  TrellisPath *paths = s->paths, *p;
283  TrellisNode *node_buf = s->node_buf;
284  TrellisNode **nodep_buf = s->nodep_buf;
285  TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
286  TrellisNode **nodes_next = nodep_buf + frontier;
287  int pathn = 0, froze = -1, i, j, k, generation = 0;
288  uint8_t *hash = s->trellis_hash;
289  memset(hash, 0xff, 65536 * sizeof(*hash));
290 
291  memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
292  nodes[0] = node_buf + frontier;
293  nodes[0]->ssd = 0;
294  nodes[0]->path = 0;
295  nodes[0]->step = c->step_index;
296  nodes[0]->sample1 = c->sample1;
297  nodes[0]->sample2 = c->sample2;
298  if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
299  version == AV_CODEC_ID_ADPCM_IMA_QT ||
300  version == AV_CODEC_ID_ADPCM_SWF)
301  nodes[0]->sample1 = c->prev_sample;
302  if (version == AV_CODEC_ID_ADPCM_MS)
303  nodes[0]->step = c->idelta;
304  if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
305  if (c->step == 0) {
306  nodes[0]->step = 127;
307  nodes[0]->sample1 = 0;
308  } else {
309  nodes[0]->step = c->step;
310  nodes[0]->sample1 = c->predictor;
311  }
312  }
313 
314  for (i = 0; i < n; i++) {
315  TrellisNode *t = node_buf + frontier*(i&1);
316  TrellisNode **u;
317  int sample = samples[i * stride];
318  int heap_pos = 0;
319  memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
320  for (j = 0; j < frontier && nodes[j]; j++) {
321  // higher j have higher ssd already, so they're likely
322  // to yield a suboptimal next sample too
323  const int range = (j < frontier / 2) ? 1 : 0;
324  const int step = nodes[j]->step;
325  int nidx;
326  if (version == AV_CODEC_ID_ADPCM_MS) {
327  const int predictor = ((nodes[j]->sample1 * c->coeff1) +
328  (nodes[j]->sample2 * c->coeff2)) / 64;
329  const int div = (sample - predictor) / step;
330  const int nmin = av_clip(div-range, -8, 6);
331  const int nmax = av_clip(div+range, -7, 7);
332  for (nidx = nmin; nidx <= nmax; nidx++) {
333  const int nibble = nidx & 0xf;
334  int dec_sample = predictor + nidx * step;
335 #define STORE_NODE(NAME, STEP_INDEX)\
336  int d;\
337  uint32_t ssd;\
338  int pos;\
339  TrellisNode *u;\
340  uint8_t *h;\
341  dec_sample = av_clip_int16(dec_sample);\
342  d = sample - dec_sample;\
343  ssd = nodes[j]->ssd + d*d;\
344  /* Check for wraparound, skip such samples completely. \
345  * Note, changing ssd to a 64 bit variable would be \
346  * simpler, avoiding this check, but it's slower on \
347  * x86 32 bit at the moment. */\
348  if (ssd < nodes[j]->ssd)\
349  goto next_##NAME;\
350  /* Collapse any two states with the same previous sample value. \
351  * One could also distinguish states by step and by 2nd to last
352  * sample, but the effects of that are negligible.
353  * Since nodes in the previous generation are iterated
354  * through a heap, they're roughly ordered from better to
355  * worse, but not strictly ordered. Therefore, an earlier
356  * node with the same sample value is better in most cases
357  * (and thus the current is skipped), but not strictly
358  * in all cases. Only skipping samples where ssd >=
359  * ssd of the earlier node with the same sample gives
360  * slightly worse quality, though, for some reason. */ \
361  h = &hash[(uint16_t) dec_sample];\
362  if (*h == generation)\
363  goto next_##NAME;\
364  if (heap_pos < frontier) {\
365  pos = heap_pos++;\
366  } else {\
367  /* Try to replace one of the leaf nodes with the new \
368  * one, but try a different slot each time. */\
369  pos = (frontier >> 1) +\
370  (heap_pos & ((frontier >> 1) - 1));\
371  if (ssd > nodes_next[pos]->ssd)\
372  goto next_##NAME;\
373  heap_pos++;\
374  }\
375  *h = generation;\
376  u = nodes_next[pos];\
377  if (!u) {\
378  av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
379  u = t++;\
380  nodes_next[pos] = u;\
381  u->path = pathn++;\
382  }\
383  u->ssd = ssd;\
384  u->step = STEP_INDEX;\
385  u->sample2 = nodes[j]->sample1;\
386  u->sample1 = dec_sample;\
387  paths[u->path].nibble = nibble;\
388  paths[u->path].prev = nodes[j]->path;\
389  /* Sift the newly inserted node up in the heap to \
390  * restore the heap property. */\
391  while (pos > 0) {\
392  int parent = (pos - 1) >> 1;\
393  if (nodes_next[parent]->ssd <= ssd)\
394  break;\
395  FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
396  pos = parent;\
397  }\
398  next_##NAME:;
399  STORE_NODE(ms, FFMAX(16,
400  (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
401  }
402  } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
403  version == AV_CODEC_ID_ADPCM_IMA_QT ||
404  version == AV_CODEC_ID_ADPCM_SWF) {
405 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
406  const int predictor = nodes[j]->sample1;\
407  const int div = (sample - predictor) * 4 / STEP_TABLE;\
408  int nmin = av_clip(div - range, -7, 6);\
409  int nmax = av_clip(div + range, -6, 7);\
410  if (nmin <= 0)\
411  nmin--; /* distinguish -0 from +0 */\
412  if (nmax < 0)\
413  nmax--;\
414  for (nidx = nmin; nidx <= nmax; nidx++) {\
415  const int nibble = nidx < 0 ? 7 - nidx : nidx;\
416  int dec_sample = predictor +\
417  (STEP_TABLE *\
418  ff_adpcm_yamaha_difflookup[nibble]) / 8;\
419  STORE_NODE(NAME, STEP_INDEX);\
420  }
422  av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
423  } else { //AV_CODEC_ID_ADPCM_YAMAHA
424  LOOP_NODES(yamaha, step,
425  av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
426  127, 24567));
427 #undef LOOP_NODES
428 #undef STORE_NODE
429  }
430  }
431 
432  u = nodes;
433  nodes = nodes_next;
434  nodes_next = u;
435 
436  generation++;
437  if (generation == 255) {
438  memset(hash, 0xff, 65536 * sizeof(*hash));
439  generation = 0;
440  }
441 
442  // prevent overflow
443  if (nodes[0]->ssd > (1 << 28)) {
444  for (j = 1; j < frontier && nodes[j]; j++)
445  nodes[j]->ssd -= nodes[0]->ssd;
446  nodes[0]->ssd = 0;
447  }
448 
449  // merge old paths to save memory
450  if (i == froze + FREEZE_INTERVAL) {
451  p = &paths[nodes[0]->path];
452  for (k = i; k > froze; k--) {
453  dst[k] = p->nibble;
454  p = &paths[p->prev];
455  }
456  froze = i;
457  pathn = 0;
458  // other nodes might use paths that don't coincide with the frozen one.
459  // checking which nodes do so is too slow, so just kill them all.
460  // this also slightly improves quality, but I don't know why.
461  memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
462  }
463  }
465  p = &paths[nodes[0]->path];
466  for (i = n - 1; i > froze; i--) {
467  dst[i] = p->nibble;
468  p = &paths[p->prev];
469  }
470 
471  c->predictor = nodes[0]->sample1;
472  c->sample1 = nodes[0]->sample1;
473  c->sample2 = nodes[0]->sample2;
474  c->step_index = nodes[0]->step;
475  c->step = nodes[0]->step;
476  c->idelta = nodes[0]->step;
477 }
478 
479 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
480  const AVFrame *frame, int *got_packet_ptr)
481 {
482  int n, i, ch, st, pkt_size, ret;
483  const int16_t *samples;
484  int16_t **samples_p;
485  uint8_t *dst;
486  ADPCMEncodeContext *c = avctx->priv_data;
487  uint8_t *buf;
488 
489  samples = (const int16_t *)frame->data[0];
490  samples_p = (int16_t **)frame->extended_data;
491  st = avctx->channels == 2;
492 
493  if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
494  pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
495  else
496  pkt_size = avctx->block_align;
497  if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size)) < 0)
498  return ret;
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;
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");