FFmpeg  1.2.12
adpcm.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 #include "avcodec.h"
21 #include "get_bits.h"
22 #include "put_bits.h"
23 #include "bytestream.h"
24 #include "adpcm.h"
25 #include "adpcm_data.h"
26 #include "internal.h"
27 
60 /* These are for CD-ROM XA ADPCM */
61 static const int xa_adpcm_table[5][2] = {
62  { 0, 0 },
63  { 60, 0 },
64  { 115, -52 },
65  { 98, -55 },
66  { 122, -60 }
67 };
68 
69 static const int ea_adpcm_table[] = {
70  0, 240, 460, 392,
71  0, 0, -208, -220,
72  0, 1, 3, 4,
73  7, 8, 10, 11,
74  0, -1, -3, -4
75 };
76 
77 // padded to zero where table size is less then 16
78 static const int swf_index_tables[4][16] = {
79  /*2*/ { -1, 2 },
80  /*3*/ { -1, -1, 2, 4 },
81  /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
82  /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
83 };
84 
85 /* end of tables */
86 
87 typedef struct ADPCMDecodeContext {
91 
93 {
94  ADPCMDecodeContext *c = avctx->priv_data;
95  unsigned int min_channels = 1;
96  unsigned int max_channels = 2;
97 
98  switch(avctx->codec->id) {
100  min_channels = 2;
101  break;
108  max_channels = 6;
109  break;
110  }
111  if (avctx->channels < min_channels || avctx->channels > max_channels) {
112  av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
113  return AVERROR(EINVAL);
114  }
115 
116  switch(avctx->codec->id) {
118  c->status[0].step = c->status[1].step = 511;
119  break;
121  if (avctx->bits_per_coded_sample != 4) {
122  av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
123  return -1;
124  }
125  break;
127  if (avctx->extradata && avctx->extradata_size >= 8) {
128  c->status[0].predictor = AV_RL32(avctx->extradata);
129  c->status[1].predictor = AV_RL32(avctx->extradata + 4);
130  }
131  break;
133  if (avctx->extradata && avctx->extradata_size >= 2)
134  c->vqa_version = AV_RL16(avctx->extradata);
135  break;
136  default:
137  break;
138  }
139 
140  switch(avctx->codec->id) {
152  break;
154  avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
156  break;
157  default:
158  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
159  }
160 
161  return 0;
162 }
163 
164 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
165 {
166  int step_index;
167  int predictor;
168  int sign, delta, diff, step;
169 
170  step = ff_adpcm_step_table[c->step_index];
171  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
172  step_index = av_clip(step_index, 0, 88);
173 
174  sign = nibble & 8;
175  delta = nibble & 7;
176  /* perform direct multiplication instead of series of jumps proposed by
177  * the reference ADPCM implementation since modern CPUs can do the mults
178  * quickly enough */
179  diff = ((2 * delta + 1) * step) >> shift;
180  predictor = c->predictor;
181  if (sign) predictor -= diff;
182  else predictor += diff;
183 
184  c->predictor = av_clip_int16(predictor);
185  c->step_index = step_index;
186 
187  return (short)c->predictor;
188 }
189 
190 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
191 {
192  int step_index;
193  int predictor;
194  int diff, step;
195 
196  step = ff_adpcm_step_table[c->step_index];
197  step_index = c->step_index + ff_adpcm_index_table[nibble];
198  step_index = av_clip(step_index, 0, 88);
199 
200  diff = step >> 3;
201  if (nibble & 4) diff += step;
202  if (nibble & 2) diff += step >> 1;
203  if (nibble & 1) diff += step >> 2;
204 
205  if (nibble & 8)
206  predictor = c->predictor - diff;
207  else
208  predictor = c->predictor + diff;
209 
210  c->predictor = av_clip_int16(predictor);
211  c->step_index = step_index;
212 
213  return c->predictor;
214 }
215 
216 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
217 {
218  int predictor;
219 
220  predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
221  predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
222 
223  c->sample2 = c->sample1;
224  c->sample1 = av_clip_int16(predictor);
225  c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
226  if (c->idelta < 16) c->idelta = 16;
227 
228  return c->sample1;
229 }
230 
231 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
232 {
233  int step_index, predictor, sign, delta, diff, step;
234 
236  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
237  step_index = av_clip(step_index, 0, 48);
238 
239  sign = nibble & 8;
240  delta = nibble & 7;
241  diff = ((2 * delta + 1) * step) >> 3;
242  predictor = c->predictor;
243  if (sign) predictor -= diff;
244  else predictor += diff;
245 
246  c->predictor = av_clip(predictor, -2048, 2047);
247  c->step_index = step_index;
248 
249  return c->predictor << 4;
250 }
251 
252 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
253 {
254  int sign, delta, diff;
255  int new_step;
256 
257  sign = nibble & 8;
258  delta = nibble & 7;
259  /* perform direct multiplication instead of series of jumps proposed by
260  * the reference ADPCM implementation since modern CPUs can do the mults
261  * quickly enough */
262  diff = ((2 * delta + 1) * c->step) >> 3;
263  /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
264  c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
265  c->predictor = av_clip_int16(c->predictor);
266  /* calculate new step and clamp it to range 511..32767 */
267  new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
268  c->step = av_clip(new_step, 511, 32767);
269 
270  return (short)c->predictor;
271 }
272 
273 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
274 {
275  int sign, delta, diff;
276 
277  sign = nibble & (1<<(size-1));
278  delta = nibble & ((1<<(size-1))-1);
279  diff = delta << (7 + c->step + shift);
280 
281  /* clamp result */
282  c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
283 
284  /* calculate new step */
285  if (delta >= (2*size - 3) && c->step < 3)
286  c->step++;
287  else if (delta == 0 && c->step > 0)
288  c->step--;
289 
290  return (short) c->predictor;
291 }
292 
293 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
294 {
295  if(!c->step) {
296  c->predictor = 0;
297  c->step = 127;
298  }
299 
300  c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
301  c->predictor = av_clip_int16(c->predictor);
302  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
303  c->step = av_clip(c->step, 127, 24567);
304  return c->predictor;
305 }
306 
307 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
308  const uint8_t *in, ADPCMChannelStatus *left,
309  ADPCMChannelStatus *right, int channels, int sample_offset)
310 {
311  int i, j;
312  int shift,filter,f0,f1;
313  int s_1,s_2;
314  int d,s,t;
315 
316  out0 += sample_offset;
317  if (channels == 1)
318  out1 = out0 + 28;
319  else
320  out1 += sample_offset;
321 
322  for(i=0;i<4;i++) {
323  shift = 12 - (in[4+i*2] & 15);
324  filter = in[4+i*2] >> 4;
325  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
326  av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
327  filter=0;
328  }
329  f0 = xa_adpcm_table[filter][0];
330  f1 = xa_adpcm_table[filter][1];
331 
332  s_1 = left->sample1;
333  s_2 = left->sample2;
334 
335  for(j=0;j<28;j++) {
336  d = in[16+i+j*4];
337 
338  t = sign_extend(d, 4);
339  s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
340  s_2 = s_1;
341  s_1 = av_clip_int16(s);
342  out0[j] = s_1;
343  }
344 
345  if (channels == 2) {
346  left->sample1 = s_1;
347  left->sample2 = s_2;
348  s_1 = right->sample1;
349  s_2 = right->sample2;
350  }
351 
352  shift = 12 - (in[5+i*2] & 15);
353  filter = in[5+i*2] >> 4;
354  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
355  av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
356  filter=0;
357  }
358 
359  f0 = xa_adpcm_table[filter][0];
360  f1 = xa_adpcm_table[filter][1];
361 
362  for(j=0;j<28;j++) {
363  d = in[16+i+j*4];
364 
365  t = sign_extend(d >> 4, 4);
366  s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
367  s_2 = s_1;
368  s_1 = av_clip_int16(s);
369  out1[j] = s_1;
370  }
371 
372  if (channels == 2) {
373  right->sample1 = s_1;
374  right->sample2 = s_2;
375  } else {
376  left->sample1 = s_1;
377  left->sample2 = s_2;
378  }
379 
380  out0 += 28 * (3 - channels);
381  out1 += 28 * (3 - channels);
382  }
383 
384  return 0;
385 }
386 
387 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
388 {
389  ADPCMDecodeContext *c = avctx->priv_data;
390  GetBitContext gb;
391  const int *table;
392  int k0, signmask, nb_bits, count;
393  int size = buf_size*8;
394  int i;
395 
396  init_get_bits(&gb, buf, size);
397 
398  //read bits & initial values
399  nb_bits = get_bits(&gb, 2)+2;
400  table = swf_index_tables[nb_bits-2];
401  k0 = 1 << (nb_bits-2);
402  signmask = 1 << (nb_bits-1);
403 
404  while (get_bits_count(&gb) <= size - 22*avctx->channels) {
405  for (i = 0; i < avctx->channels; i++) {
406  *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
407  c->status[i].step_index = get_bits(&gb, 6);
408  }
409 
410  for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
411  int i;
412 
413  for (i = 0; i < avctx->channels; i++) {
414  // similar to IMA adpcm
415  int delta = get_bits(&gb, nb_bits);
416  int step = ff_adpcm_step_table[c->status[i].step_index];
417  long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
418  int k = k0;
419 
420  do {
421  if (delta & k)
422  vpdiff += step;
423  step >>= 1;
424  k >>= 1;
425  } while(k);
426  vpdiff += step;
427 
428  if (delta & signmask)
429  c->status[i].predictor -= vpdiff;
430  else
431  c->status[i].predictor += vpdiff;
432 
433  c->status[i].step_index += table[delta & (~signmask)];
434 
435  c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
436  c->status[i].predictor = av_clip_int16(c->status[i].predictor);
437 
438  *samples++ = c->status[i].predictor;
439  }
440  }
441  }
442 }
443 
456  int buf_size, int *coded_samples, int *approx_nb_samples)
457 {
458  ADPCMDecodeContext *s = avctx->priv_data;
459  int nb_samples = 0;
460  int ch = avctx->channels;
461  int has_coded_samples = 0;
462  int header_size;
463 
464  *coded_samples = 0;
465  *approx_nb_samples = 0;
466 
467  if(ch <= 0)
468  return 0;
469 
470  switch (avctx->codec->id) {
471  /* constant, only check buf_size */
473  if (buf_size < 76 * ch)
474  return 0;
475  nb_samples = 128;
476  break;
478  if (buf_size < 34 * ch)
479  return 0;
480  nb_samples = 64;
481  break;
482  /* simple 4-bit adpcm */
489  nb_samples = buf_size * 2 / ch;
490  break;
491  }
492  if (nb_samples)
493  return nb_samples;
494 
495  /* simple 4-bit adpcm, with header */
496  header_size = 0;
497  switch (avctx->codec->id) {
499  case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
500  case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
501  case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
502  }
503  if (header_size > 0)
504  return (buf_size - header_size) * 2 / ch;
505 
506  /* more complex formats */
507  switch (avctx->codec->id) {
509  has_coded_samples = 1;
510  *coded_samples = bytestream2_get_le32(gb);
511  *coded_samples -= *coded_samples % 28;
512  nb_samples = (buf_size - 12) / 30 * 28;
513  break;
515  has_coded_samples = 1;
516  *coded_samples = bytestream2_get_le32(gb);
517  nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
518  break;
520  nb_samples = (buf_size - ch) / ch * 2;
521  break;
525  /* maximum number of samples */
526  /* has internal offsets and a per-frame switch to signal raw 16-bit */
527  has_coded_samples = 1;
528  switch (avctx->codec->id) {
530  header_size = 4 + 9 * ch;
531  *coded_samples = bytestream2_get_le32(gb);
532  break;
534  header_size = 4 + 5 * ch;
535  *coded_samples = bytestream2_get_le32(gb);
536  *approx_nb_samples = 1;
537  break;
539  header_size = 4 + 5 * ch;
540  *coded_samples = bytestream2_get_be32(gb);
541  *approx_nb_samples = 1;
542  break;
543  }
544  *coded_samples -= *coded_samples % 28;
545  nb_samples = (buf_size - header_size) * 2 / ch;
546  nb_samples -= nb_samples % 28;
547  break;
549  if (avctx->block_align > 0)
550  buf_size = FFMIN(buf_size, avctx->block_align);
551  nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
552  break;
554  if (avctx->block_align > 0)
555  buf_size = FFMIN(buf_size, avctx->block_align);
556  nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
557  break;
559  if (avctx->block_align > 0)
560  buf_size = FFMIN(buf_size, avctx->block_align);
561  nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
562  break;
564  if (avctx->block_align > 0)
565  buf_size = FFMIN(buf_size, avctx->block_align);
566  nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
567  break;
571  {
572  int samples_per_byte;
573  switch (avctx->codec->id) {
574  case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
575  case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
576  case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
577  }
578  if (!s->status[0].step_index) {
579  nb_samples++;
580  buf_size -= ch;
581  }
582  nb_samples += buf_size * samples_per_byte / ch;
583  break;
584  }
586  {
587  int buf_bits = buf_size * 8 - 2;
588  int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
589  int block_hdr_size = 22 * ch;
590  int block_size = block_hdr_size + nbits * ch * 4095;
591  int nblocks = buf_bits / block_size;
592  int bits_left = buf_bits - nblocks * block_size;
593  nb_samples = nblocks * 4096;
594  if (bits_left >= block_hdr_size)
595  nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
596  break;
597  }
599  has_coded_samples = 1;
600  bytestream2_skip(gb, 4); // channel size
601  *coded_samples = bytestream2_get_be32(gb);
602  *coded_samples -= *coded_samples % 14;
603  nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
604  break;
606  nb_samples = buf_size / (9 * ch) * 16;
607  break;
609  nb_samples = (buf_size / 128) * 224 / ch;
610  break;
611  }
612 
613  /* validate coded sample count */
614  if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
615  return AVERROR_INVALIDDATA;
616 
617  return nb_samples;
618 }
619 
620 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
621  int *got_frame_ptr, AVPacket *avpkt)
622 {
623  AVFrame *frame = data;
624  const uint8_t *buf = avpkt->data;
625  int buf_size = avpkt->size;
626  ADPCMDecodeContext *c = avctx->priv_data;
627  ADPCMChannelStatus *cs;
628  int n, m, channel, i;
629  short *samples;
630  int16_t **samples_p;
631  int st; /* stereo */
632  int count1, count2;
633  int nb_samples, coded_samples, approx_nb_samples, ret;
634  GetByteContext gb;
635 
636  bytestream2_init(&gb, buf, buf_size);
637  nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
638  if (nb_samples <= 0) {
639  av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
640  return AVERROR_INVALIDDATA;
641  }
642 
643  /* get output buffer */
644  frame->nb_samples = nb_samples;
645  if ((ret = ff_get_buffer(avctx, frame)) < 0) {
646  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
647  return ret;
648  }
649  samples = (short *)frame->data[0];
650  samples_p = (int16_t **)frame->extended_data;
651 
652  /* use coded_samples when applicable */
653  /* it is always <= nb_samples, so the output buffer will be large enough */
654  if (coded_samples) {
655  if (!approx_nb_samples && coded_samples != nb_samples)
656  av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
657  frame->nb_samples = nb_samples = coded_samples;
658  }
659 
660  st = avctx->channels == 2 ? 1 : 0;
661 
662  switch(avctx->codec->id) {
664  /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
665  Channel data is interleaved per-chunk. */
666  for (channel = 0; channel < avctx->channels; channel++) {
667  int predictor;
668  int step_index;
669  cs = &(c->status[channel]);
670  /* (pppppp) (piiiiiii) */
671 
672  /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
673  predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
674  step_index = predictor & 0x7F;
675  predictor &= ~0x7F;
676 
677  if (cs->step_index == step_index) {
678  int diff = predictor - cs->predictor;
679  if (diff < 0)
680  diff = - diff;
681  if (diff > 0x7f)
682  goto update;
683  } else {
684  update:
685  cs->step_index = step_index;
686  cs->predictor = predictor;
687  }
688 
689  if (cs->step_index > 88u){
690  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
691  channel, cs->step_index);
692  return AVERROR_INVALIDDATA;
693  }
694 
695  samples = samples_p[channel];
696 
697  for (m = 0; m < 64; m += 2) {
698  int byte = bytestream2_get_byteu(&gb);
699  samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
700  samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
701  }
702  }
703  break;
705  for(i=0; i<avctx->channels; i++){
706  cs = &(c->status[i]);
707  cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
708 
709  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
710  if (cs->step_index > 88u){
711  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
712  i, cs->step_index);
713  return AVERROR_INVALIDDATA;
714  }
715  }
716 
717  for (n = 0; n < (nb_samples - 1) / 8; n++) {
718  for (i = 0; i < avctx->channels; i++) {
719  cs = &c->status[i];
720  samples = &samples_p[i][1 + n * 8];
721  for (m = 0; m < 8; m += 2) {
722  int v = bytestream2_get_byteu(&gb);
723  samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
724  samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
725  }
726  }
727  }
728  break;
730  for (i = 0; i < avctx->channels; i++)
731  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
732 
733  for (i = 0; i < avctx->channels; i++) {
734  c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
735  if (c->status[i].step_index > 88u) {
736  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
737  i, c->status[i].step_index);
738  return AVERROR_INVALIDDATA;
739  }
740  }
741 
742  for (i = 0; i < avctx->channels; i++) {
743  samples = (int16_t *)frame->data[i];
744  cs = &c->status[i];
745  for (n = nb_samples >> 1; n > 0; n--) {
746  int v = bytestream2_get_byteu(&gb);
747  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
748  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
749  }
750  }
751  break;
753  {
754  int block_predictor;
755 
756  block_predictor = bytestream2_get_byteu(&gb);
757  if (block_predictor > 6) {
758  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
759  block_predictor);
760  return AVERROR_INVALIDDATA;
761  }
762  c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
763  c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
764  if (st) {
765  block_predictor = bytestream2_get_byteu(&gb);
766  if (block_predictor > 6) {
767  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
768  block_predictor);
769  return AVERROR_INVALIDDATA;
770  }
771  c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
772  c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
773  }
774  c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
775  if (st){
776  c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
777  }
778 
779  c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
780  if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
781  c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
782  if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
783 
784  *samples++ = c->status[0].sample2;
785  if (st) *samples++ = c->status[1].sample2;
786  *samples++ = c->status[0].sample1;
787  if (st) *samples++ = c->status[1].sample1;
788  for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
789  int byte = bytestream2_get_byteu(&gb);
790  *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
791  *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
792  }
793  break;
794  }
796  for (channel = 0; channel < avctx->channels; channel++) {
797  cs = &c->status[channel];
798  cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
799  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
800  if (cs->step_index > 88u){
801  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
802  channel, cs->step_index);
803  return AVERROR_INVALIDDATA;
804  }
805  }
806  for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
807  int v = bytestream2_get_byteu(&gb);
808  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
809  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
810  }
811  break;
813  {
814  int last_byte = 0;
815  int nibble;
816  int decode_top_nibble_next = 0;
817  int diff_channel;
818  const int16_t *samples_end = samples + avctx->channels * nb_samples;
819 
820  bytestream2_skipu(&gb, 10);
821  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
822  c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
823  c->status[0].step_index = bytestream2_get_byteu(&gb);
824  c->status[1].step_index = bytestream2_get_byteu(&gb);
825  if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
826  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
827  c->status[0].step_index, c->status[1].step_index);
828  return AVERROR_INVALIDDATA;
829  }
830  /* sign extend the predictors */
831  diff_channel = c->status[1].predictor;
832 
833  /* DK3 ADPCM support macro */
834 #define DK3_GET_NEXT_NIBBLE() \
835  if (decode_top_nibble_next) { \
836  nibble = last_byte >> 4; \
837  decode_top_nibble_next = 0; \
838  } else { \
839  last_byte = bytestream2_get_byteu(&gb); \
840  nibble = last_byte & 0x0F; \
841  decode_top_nibble_next = 1; \
842  }
843 
844  while (samples < samples_end) {
845 
846  /* for this algorithm, c->status[0] is the sum channel and
847  * c->status[1] is the diff channel */
848 
849  /* process the first predictor of the sum channel */
851  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
852 
853  /* process the diff channel predictor */
855  adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
856 
857  /* process the first pair of stereo PCM samples */
858  diff_channel = (diff_channel + c->status[1].predictor) / 2;
859  *samples++ = c->status[0].predictor + c->status[1].predictor;
860  *samples++ = c->status[0].predictor - c->status[1].predictor;
861 
862  /* process the second predictor of the sum channel */
864  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
865 
866  /* process the second pair of stereo PCM samples */
867  diff_channel = (diff_channel + c->status[1].predictor) / 2;
868  *samples++ = c->status[0].predictor + c->status[1].predictor;
869  *samples++ = c->status[0].predictor - c->status[1].predictor;
870  }
871 
872  if ((bytestream2_tell(&gb) & 1))
873  bytestream2_skip(&gb, 1);
874  break;
875  }
877  for (channel = 0; channel < avctx->channels; channel++) {
878  cs = &c->status[channel];
879  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
880  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
881  if (cs->step_index > 88u){
882  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
883  channel, cs->step_index);
884  return AVERROR_INVALIDDATA;
885  }
886  }
887 
888  for (n = nb_samples >> (1 - st); n > 0; n--) {
889  int v1, v2;
890  int v = bytestream2_get_byteu(&gb);
891  /* nibbles are swapped for mono */
892  if (st) {
893  v1 = v >> 4;
894  v2 = v & 0x0F;
895  } else {
896  v2 = v >> 4;
897  v1 = v & 0x0F;
898  }
899  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
900  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
901  }
902  break;
904  while (bytestream2_get_bytes_left(&gb) > 0) {
905  int v = bytestream2_get_byteu(&gb);
906  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
907  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
908  }
909  break;
911  while (bytestream2_get_bytes_left(&gb) > 0) {
912  int v = bytestream2_get_byteu(&gb);
913  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
914  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
915  }
916  break;
918  if (c->vqa_version == 3) {
919  for (channel = 0; channel < avctx->channels; channel++) {
920  int16_t *smp = samples_p[channel];
921 
922  for (n = nb_samples / 2; n > 0; n--) {
923  int v = bytestream2_get_byteu(&gb);
924  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
925  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
926  }
927  }
928  } else {
929  for (n = nb_samples / 2; n > 0; n--) {
930  for (channel = 0; channel < avctx->channels; channel++) {
931  int v = bytestream2_get_byteu(&gb);
932  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
933  samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
934  }
935  samples += avctx->channels;
936  }
937  }
938  bytestream2_seek(&gb, 0, SEEK_END);
939  break;
941  {
942  int16_t *out0 = samples_p[0];
943  int16_t *out1 = samples_p[1];
944  int samples_per_block = 28 * (3 - avctx->channels) * 4;
945  int sample_offset = 0;
946  while (bytestream2_get_bytes_left(&gb) >= 128) {
947  if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
948  &c->status[0], &c->status[1],
949  avctx->channels, sample_offset)) < 0)
950  return ret;
951  bytestream2_skipu(&gb, 128);
952  sample_offset += samples_per_block;
953  }
954  break;
955  }
957  for (i=0; i<=st; i++) {
958  c->status[i].step_index = bytestream2_get_le32u(&gb);
959  if (c->status[i].step_index > 88u) {
960  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
961  i, c->status[i].step_index);
962  return AVERROR_INVALIDDATA;
963  }
964  }
965  for (i=0; i<=st; i++)
966  c->status[i].predictor = bytestream2_get_le32u(&gb);
967 
968  for (n = nb_samples >> (1 - st); n > 0; n--) {
969  int byte = bytestream2_get_byteu(&gb);
970  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
971  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
972  }
973  break;
975  for (n = nb_samples >> (1 - st); n > 0; n--) {
976  int byte = bytestream2_get_byteu(&gb);
977  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
978  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
979  }
980  break;
982  {
983  int previous_left_sample, previous_right_sample;
984  int current_left_sample, current_right_sample;
985  int next_left_sample, next_right_sample;
986  int coeff1l, coeff2l, coeff1r, coeff2r;
987  int shift_left, shift_right;
988 
989  /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
990  each coding 28 stereo samples. */
991 
992  if(avctx->channels != 2)
993  return AVERROR_INVALIDDATA;
994 
995  current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
996  previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
997  current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
998  previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
999 
1000  for (count1 = 0; count1 < nb_samples / 28; count1++) {
1001  int byte = bytestream2_get_byteu(&gb);
1002  coeff1l = ea_adpcm_table[ byte >> 4 ];
1003  coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1004  coeff1r = ea_adpcm_table[ byte & 0x0F];
1005  coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1006 
1007  byte = bytestream2_get_byteu(&gb);
1008  shift_left = 20 - (byte >> 4);
1009  shift_right = 20 - (byte & 0x0F);
1010 
1011  for (count2 = 0; count2 < 28; count2++) {
1012  byte = bytestream2_get_byteu(&gb);
1013  next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1014  next_right_sample = sign_extend(byte, 4) << shift_right;
1015 
1016  next_left_sample = (next_left_sample +
1017  (current_left_sample * coeff1l) +
1018  (previous_left_sample * coeff2l) + 0x80) >> 8;
1019  next_right_sample = (next_right_sample +
1020  (current_right_sample * coeff1r) +
1021  (previous_right_sample * coeff2r) + 0x80) >> 8;
1022 
1023  previous_left_sample = current_left_sample;
1024  current_left_sample = av_clip_int16(next_left_sample);
1025  previous_right_sample = current_right_sample;
1026  current_right_sample = av_clip_int16(next_right_sample);
1027  *samples++ = current_left_sample;
1028  *samples++ = current_right_sample;
1029  }
1030  }
1031 
1032  bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1033 
1034  break;
1035  }
1037  {
1038  int coeff[2][2], shift[2];
1039 
1040  for(channel = 0; channel < avctx->channels; channel++) {
1041  int byte = bytestream2_get_byteu(&gb);
1042  for (i=0; i<2; i++)
1043  coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1044  shift[channel] = 20 - (byte & 0x0F);
1045  }
1046  for (count1 = 0; count1 < nb_samples / 2; count1++) {
1047  int byte[2];
1048 
1049  byte[0] = bytestream2_get_byteu(&gb);
1050  if (st) byte[1] = bytestream2_get_byteu(&gb);
1051  for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1052  for(channel = 0; channel < avctx->channels; channel++) {
1053  int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1054  sample = (sample +
1055  c->status[channel].sample1 * coeff[channel][0] +
1056  c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1057  c->status[channel].sample2 = c->status[channel].sample1;
1058  c->status[channel].sample1 = av_clip_int16(sample);
1059  *samples++ = c->status[channel].sample1;
1060  }
1061  }
1062  }
1063  bytestream2_seek(&gb, 0, SEEK_END);
1064  break;
1065  }
1068  case AV_CODEC_ID_ADPCM_EA_R3: {
1069  /* channel numbering
1070  2chan: 0=fl, 1=fr
1071  4chan: 0=fl, 1=rl, 2=fr, 3=rr
1072  6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1073  const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1074  int previous_sample, current_sample, next_sample;
1075  int coeff1, coeff2;
1076  int shift;
1077  unsigned int channel;
1078  uint16_t *samplesC;
1079  int count = 0;
1080  int offsets[6];
1081 
1082  for (channel=0; channel<avctx->channels; channel++)
1083  offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1084  bytestream2_get_le32(&gb)) +
1085  (avctx->channels + 1) * 4;
1086 
1087  for (channel=0; channel<avctx->channels; channel++) {
1088  bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1089  samplesC = samples_p[channel];
1090 
1091  if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1092  current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1093  previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1094  } else {
1095  current_sample = c->status[channel].predictor;
1096  previous_sample = c->status[channel].prev_sample;
1097  }
1098 
1099  for (count1 = 0; count1 < nb_samples / 28; count1++) {
1100  int byte = bytestream2_get_byte(&gb);
1101  if (byte == 0xEE) { /* only seen in R2 and R3 */
1102  current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1103  previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1104 
1105  for (count2=0; count2<28; count2++)
1106  *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1107  } else {
1108  coeff1 = ea_adpcm_table[ byte >> 4 ];
1109  coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1110  shift = 20 - (byte & 0x0F);
1111 
1112  for (count2=0; count2<28; count2++) {
1113  if (count2 & 1)
1114  next_sample = sign_extend(byte, 4) << shift;
1115  else {
1116  byte = bytestream2_get_byte(&gb);
1117  next_sample = sign_extend(byte >> 4, 4) << shift;
1118  }
1119 
1120  next_sample += (current_sample * coeff1) +
1121  (previous_sample * coeff2);
1122  next_sample = av_clip_int16(next_sample >> 8);
1123 
1124  previous_sample = current_sample;
1125  current_sample = next_sample;
1126  *samplesC++ = current_sample;
1127  }
1128  }
1129  }
1130  if (!count) {
1131  count = count1;
1132  } else if (count != count1) {
1133  av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1134  count = FFMAX(count, count1);
1135  }
1136 
1137  if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1138  c->status[channel].predictor = current_sample;
1139  c->status[channel].prev_sample = previous_sample;
1140  }
1141  }
1142 
1143  frame->nb_samples = count * 28;
1144  bytestream2_seek(&gb, 0, SEEK_END);
1145  break;
1146  }
1148  for (channel=0; channel<avctx->channels; channel++) {
1149  int coeff[2][4], shift[4];
1150  int16_t *s = samples_p[channel];
1151  for (n = 0; n < 4; n++, s += 32) {
1152  int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1153  for (i=0; i<2; i++)
1154  coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1155  s[0] = val & ~0x0F;
1156 
1157  val = sign_extend(bytestream2_get_le16u(&gb), 16);
1158  shift[n] = 20 - (val & 0x0F);
1159  s[1] = val & ~0x0F;
1160  }
1161 
1162  for (m=2; m<32; m+=2) {
1163  s = &samples_p[channel][m];
1164  for (n = 0; n < 4; n++, s += 32) {
1165  int level, pred;
1166  int byte = bytestream2_get_byteu(&gb);
1167 
1168  level = sign_extend(byte >> 4, 4) << shift[n];
1169  pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1170  s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1171 
1172  level = sign_extend(byte, 4) << shift[n];
1173  pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1174  s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1175  }
1176  }
1177  }
1178  break;
1180  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1181  c->status[0].step_index = bytestream2_get_le16u(&gb);
1182  bytestream2_skipu(&gb, 4);
1183  if (c->status[0].step_index > 88u) {
1184  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1185  c->status[0].step_index);
1186  return AVERROR_INVALIDDATA;
1187  }
1188 
1189  for (n = nb_samples >> (1 - st); n > 0; n--) {
1190  int v = bytestream2_get_byteu(&gb);
1191 
1192  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1193  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1194  }
1195  break;
1197  for (i = 0; i < avctx->channels; i++) {
1198  c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1199  c->status[i].step_index = bytestream2_get_byteu(&gb);
1200  bytestream2_skipu(&gb, 1);
1201  if (c->status[i].step_index > 88u) {
1202  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1203  c->status[i].step_index);
1204  return AVERROR_INVALIDDATA;
1205  }
1206  }
1207 
1208  for (n = nb_samples >> (1 - st); n > 0; n--) {
1209  int v = bytestream2_get_byteu(&gb);
1210 
1211  *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1212  *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1213  }
1214  break;
1215  case AV_CODEC_ID_ADPCM_CT:
1216  for (n = nb_samples >> (1 - st); n > 0; n--) {
1217  int v = bytestream2_get_byteu(&gb);
1218  *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1219  *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1220  }
1221  break;
1225  if (!c->status[0].step_index) {
1226  /* the first byte is a raw sample */
1227  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1228  if (st)
1229  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1230  c->status[0].step_index = 1;
1231  nb_samples--;
1232  }
1233  if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1234  for (n = nb_samples >> (1 - st); n > 0; n--) {
1235  int byte = bytestream2_get_byteu(&gb);
1236  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1237  byte >> 4, 4, 0);
1238  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1239  byte & 0x0F, 4, 0);
1240  }
1241  } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1242  for (n = nb_samples / 3; n > 0; n--) {
1243  int byte = bytestream2_get_byteu(&gb);
1244  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1245  byte >> 5 , 3, 0);
1246  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1247  (byte >> 2) & 0x07, 3, 0);
1248  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1249  byte & 0x03, 2, 0);
1250  }
1251  } else {
1252  for (n = nb_samples >> (2 - st); n > 0; n--) {
1253  int byte = bytestream2_get_byteu(&gb);
1254  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1255  byte >> 6 , 2, 2);
1256  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1257  (byte >> 4) & 0x03, 2, 2);
1258  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1259  (byte >> 2) & 0x03, 2, 2);
1260  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1261  byte & 0x03, 2, 2);
1262  }
1263  }
1264  break;
1265  case AV_CODEC_ID_ADPCM_SWF:
1266  adpcm_swf_decode(avctx, buf, buf_size, samples);
1267  bytestream2_seek(&gb, 0, SEEK_END);
1268  break;
1270  for (n = nb_samples >> (1 - st); n > 0; n--) {
1271  int v = bytestream2_get_byteu(&gb);
1272  *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1273  *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1274  }
1275  break;
1276  case AV_CODEC_ID_ADPCM_AFC:
1277  {
1278  int samples_per_block;
1279  int blocks;
1280 
1281  if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1282  samples_per_block = avctx->extradata[0] / 16;
1283  blocks = nb_samples / avctx->extradata[0];
1284  } else {
1285  samples_per_block = nb_samples / 16;
1286  blocks = 1;
1287  }
1288 
1289  for (m = 0; m < blocks; m++) {
1290  for (channel = 0; channel < avctx->channels; channel++) {
1291  int prev1 = c->status[channel].sample1;
1292  int prev2 = c->status[channel].sample2;
1293 
1294  samples = samples_p[channel] + m * 16;
1295  /* Read in every sample for this channel. */
1296  for (i = 0; i < samples_per_block; i++) {
1297  int byte = bytestream2_get_byteu(&gb);
1298  int scale = 1 << (byte >> 4);
1299  int index = byte & 0xf;
1300  int factor1 = ff_adpcm_afc_coeffs[0][index];
1301  int factor2 = ff_adpcm_afc_coeffs[1][index];
1302 
1303  /* Decode 16 samples. */
1304  for (n = 0; n < 16; n++) {
1305  int32_t sampledat;
1306 
1307  if (n & 1) {
1308  sampledat = sign_extend(byte, 4);
1309  } else {
1310  byte = bytestream2_get_byteu(&gb);
1311  sampledat = sign_extend(byte >> 4, 4);
1312  }
1313 
1314  sampledat = ((prev1 * factor1 + prev2 * factor2) +
1315  ((sampledat * scale) << 11)) >> 11;
1316  *samples = av_clip_int16(sampledat);
1317  prev2 = prev1;
1318  prev1 = *samples++;
1319  }
1320  }
1321 
1322  c->status[channel].sample1 = prev1;
1323  c->status[channel].sample2 = prev2;
1324  }
1325  }
1326  bytestream2_seek(&gb, 0, SEEK_END);
1327  break;
1328  }
1329  case AV_CODEC_ID_ADPCM_THP:
1330  {
1331  int table[6][16];
1332  int ch;
1333 
1334  for (i = 0; i < avctx->channels; i++)
1335  for (n = 0; n < 16; n++)
1336  table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
1337 
1338  /* Initialize the previous sample. */
1339  for (i = 0; i < avctx->channels; i++) {
1340  c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
1341  c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
1342  }
1343 
1344  for (ch = 0; ch < avctx->channels; ch++) {
1345  samples = samples_p[ch];
1346 
1347  /* Read in every sample for this channel. */
1348  for (i = 0; i < nb_samples / 14; i++) {
1349  int byte = bytestream2_get_byteu(&gb);
1350  int index = (byte >> 4) & 7;
1351  unsigned int exp = byte & 0x0F;
1352  int factor1 = table[ch][index * 2];
1353  int factor2 = table[ch][index * 2 + 1];
1354 
1355  /* Decode 14 samples. */
1356  for (n = 0; n < 14; n++) {
1357  int32_t sampledat;
1358 
1359  if (n & 1) {
1360  sampledat = sign_extend(byte, 4);
1361  } else {
1362  byte = bytestream2_get_byteu(&gb);
1363  sampledat = sign_extend(byte >> 4, 4);
1364  }
1365 
1366  sampledat = ((c->status[ch].sample1 * factor1
1367  + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1368  *samples = av_clip_int16(sampledat);
1369  c->status[ch].sample2 = c->status[ch].sample1;
1370  c->status[ch].sample1 = *samples++;
1371  }
1372  }
1373  }
1374  break;
1375  }
1376 
1377  default:
1378  return -1;
1379  }
1380 
1381  if (avpkt->size && bytestream2_tell(&gb) == 0) {
1382  av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1383  return AVERROR_INVALIDDATA;
1384  }
1385 
1386  *got_frame_ptr = 1;
1387 
1388  return bytestream2_tell(&gb);
1389 }
1390 
1391 
1399 
1400 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1401 AVCodec ff_ ## name_ ## _decoder = { \
1402  .name = #name_, \
1403  .type = AVMEDIA_TYPE_AUDIO, \
1404  .id = id_, \
1405  .priv_data_size = sizeof(ADPCMDecodeContext), \
1406  .init = adpcm_decode_init, \
1407  .decode = adpcm_decode_frame, \
1408  .capabilities = CODEC_CAP_DR1, \
1409  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1410  .sample_fmts = sample_fmts_, \
1411 }
1412 
1413 /* Note: Do not forget to add new entries to the Makefile as well. */
1414 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1415 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1416 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1417 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1418 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1419 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1420 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1421 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1422 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1423 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1424 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1425 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1426 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1427 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1428 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1429 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1430 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1431 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1432 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1433 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1434 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1435 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1436 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1437 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1438 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1439 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1440 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1441 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1442 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");