FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
28 /**
29  * @file
30  * ADPCM decoders
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  * CD-ROM XA ADPCM codec by BERO
35  * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
36  * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
37  * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
38  * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
39  * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
40  * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
41  * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
42  *
43  * Features and limitations:
44  *
45  * Reference documents:
46  * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
47  * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
48  * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
49  * http://openquicktime.sourceforge.net/
50  * XAnim sources (xa_codec.c) http://xanim.polter.net/
51  * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
52  * SoX source code http://sox.sourceforge.net/
53  *
54  * CD-ROM XA:
55  * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
56  * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
57  * readstr http://www.geocities.co.jp/Playtown/2004/
58  */
59 
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 {
89  int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
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 
444 /**
445  * Get the number of samples that will be decoded from the packet.
446  * In one case, this is actually the maximum number of samples possible to
447  * decode with the given buf_size.
448  *
449  * @param[out] coded_samples set to the number of samples as coded in the
450  * packet, or 0 if the codec does not encode the
451  * number of samples in each frame.
452  */
454  int buf_size, int *coded_samples)
455 {
456  ADPCMDecodeContext *s = avctx->priv_data;
457  int nb_samples = 0;
458  int ch = avctx->channels;
459  int has_coded_samples = 0;
460  int header_size;
461 
462  *coded_samples = 0;
463 
464  if(ch <= 0)
465  return 0;
466 
467  switch (avctx->codec->id) {
468  /* constant, only check buf_size */
470  if (buf_size < 76 * ch)
471  return 0;
472  nb_samples = 128;
473  break;
475  if (buf_size < 34 * ch)
476  return 0;
477  nb_samples = 64;
478  break;
479  /* simple 4-bit adpcm */
486  nb_samples = buf_size * 2 / ch;
487  break;
488  }
489  if (nb_samples)
490  return nb_samples;
491 
492  /* simple 4-bit adpcm, with header */
493  header_size = 0;
494  switch (avctx->codec->id) {
496  case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
497  case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
498  case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
499  }
500  if (header_size > 0)
501  return (buf_size - header_size) * 2 / ch;
502 
503  /* more complex formats */
504  switch (avctx->codec->id) {
506  has_coded_samples = 1;
507  *coded_samples = bytestream2_get_le32(gb);
508  *coded_samples -= *coded_samples % 28;
509  nb_samples = (buf_size - 12) / 30 * 28;
510  break;
512  has_coded_samples = 1;
513  *coded_samples = bytestream2_get_le32(gb);
514  nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
515  break;
517  nb_samples = (buf_size - ch) / ch * 2;
518  break;
522  /* maximum number of samples */
523  /* has internal offsets and a per-frame switch to signal raw 16-bit */
524  has_coded_samples = 1;
525  switch (avctx->codec->id) {
527  header_size = 4 + 9 * ch;
528  *coded_samples = bytestream2_get_le32(gb);
529  break;
531  header_size = 4 + 5 * ch;
532  *coded_samples = bytestream2_get_le32(gb);
533  break;
535  header_size = 4 + 5 * ch;
536  *coded_samples = bytestream2_get_be32(gb);
537  break;
538  }
539  *coded_samples -= *coded_samples % 28;
540  nb_samples = (buf_size - header_size) * 2 / ch;
541  nb_samples -= nb_samples % 28;
542  break;
544  if (avctx->block_align > 0)
545  buf_size = FFMIN(buf_size, avctx->block_align);
546  nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
547  break;
549  if (avctx->block_align > 0)
550  buf_size = FFMIN(buf_size, avctx->block_align);
551  nb_samples = 1 + (buf_size - 4 * ch) * 2 / 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) / (4 * ch) * 8;
557  break;
559  if (avctx->block_align > 0)
560  buf_size = FFMIN(buf_size, avctx->block_align);
561  nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
562  break;
566  {
567  int samples_per_byte;
568  switch (avctx->codec->id) {
569  case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
570  case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
571  case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
572  }
573  if (!s->status[0].step_index) {
574  nb_samples++;
575  buf_size -= ch;
576  }
577  nb_samples += buf_size * samples_per_byte / ch;
578  break;
579  }
581  {
582  int buf_bits = buf_size * 8 - 2;
583  int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
584  int block_hdr_size = 22 * ch;
585  int block_size = block_hdr_size + nbits * ch * 4095;
586  int nblocks = buf_bits / block_size;
587  int bits_left = buf_bits - nblocks * block_size;
588  nb_samples = nblocks * 4096;
589  if (bits_left >= block_hdr_size)
590  nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
591  break;
592  }
594  has_coded_samples = 1;
595  bytestream2_skip(gb, 4); // channel size
596  *coded_samples = bytestream2_get_be32(gb);
597  *coded_samples -= *coded_samples % 14;
598  nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
599  break;
601  nb_samples = buf_size / (9 * ch) * 16;
602  break;
604  nb_samples = (buf_size / 128) * 224 / ch;
605  break;
606  }
607 
608  /* validate coded sample count */
609  if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
610  return AVERROR_INVALIDDATA;
611 
612  return nb_samples;
613 }
614 
615 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
616  int *got_frame_ptr, AVPacket *avpkt)
617 {
618  AVFrame *frame = data;
619  const uint8_t *buf = avpkt->data;
620  int buf_size = avpkt->size;
621  ADPCMDecodeContext *c = avctx->priv_data;
622  ADPCMChannelStatus *cs;
623  int n, m, channel, i;
624  short *samples;
625  int16_t **samples_p;
626  int st; /* stereo */
627  int count1, count2;
628  int nb_samples, coded_samples, ret;
629  GetByteContext gb;
630 
631  bytestream2_init(&gb, buf, buf_size);
632  nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
633  if (nb_samples <= 0) {
634  av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
635  return AVERROR_INVALIDDATA;
636  }
637 
638  /* get output buffer */
639  frame->nb_samples = nb_samples;
640  if ((ret = ff_get_buffer(avctx, frame)) < 0) {
641  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
642  return ret;
643  }
644  samples = (short *)frame->data[0];
645  samples_p = (int16_t **)frame->extended_data;
646 
647  /* use coded_samples when applicable */
648  /* it is always <= nb_samples, so the output buffer will be large enough */
649  if (coded_samples) {
650  if (coded_samples != nb_samples)
651  av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
652  frame->nb_samples = nb_samples = coded_samples;
653  }
654 
655  st = avctx->channels == 2 ? 1 : 0;
656 
657  switch(avctx->codec->id) {
659  /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
660  Channel data is interleaved per-chunk. */
661  for (channel = 0; channel < avctx->channels; channel++) {
662  int predictor;
663  int step_index;
664  cs = &(c->status[channel]);
665  /* (pppppp) (piiiiiii) */
666 
667  /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
668  predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
669  step_index = predictor & 0x7F;
670  predictor &= ~0x7F;
671 
672  if (cs->step_index == step_index) {
673  int diff = predictor - cs->predictor;
674  if (diff < 0)
675  diff = - diff;
676  if (diff > 0x7f)
677  goto update;
678  } else {
679  update:
680  cs->step_index = step_index;
681  cs->predictor = predictor;
682  }
683 
684  if (cs->step_index > 88u){
685  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
686  channel, cs->step_index);
687  return AVERROR_INVALIDDATA;
688  }
689 
690  samples = samples_p[channel];
691 
692  for (m = 0; m < 64; m += 2) {
693  int byte = bytestream2_get_byteu(&gb);
694  samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
695  samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
696  }
697  }
698  break;
700  for(i=0; i<avctx->channels; i++){
701  cs = &(c->status[i]);
702  cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
703 
704  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
705  if (cs->step_index > 88u){
706  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
707  i, cs->step_index);
708  return AVERROR_INVALIDDATA;
709  }
710  }
711 
712  for (n = 0; n < (nb_samples - 1) / 8; n++) {
713  for (i = 0; i < avctx->channels; i++) {
714  cs = &c->status[i];
715  samples = &samples_p[i][1 + n * 8];
716  for (m = 0; m < 8; m += 2) {
717  int v = bytestream2_get_byteu(&gb);
718  samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
719  samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
720  }
721  }
722  }
723  break;
725  for (i = 0; i < avctx->channels; i++)
726  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
727 
728  for (i = 0; i < avctx->channels; i++) {
729  c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
730  if (c->status[i].step_index > 88u) {
731  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
732  i, c->status[i].step_index);
733  return AVERROR_INVALIDDATA;
734  }
735  }
736 
737  for (i = 0; i < avctx->channels; i++) {
738  samples = (int16_t *)frame->data[i];
739  cs = &c->status[i];
740  for (n = nb_samples >> 1; n > 0; n--) {
741  int v = bytestream2_get_byteu(&gb);
742  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
743  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
744  }
745  }
746  break;
748  {
749  int block_predictor;
750 
751  block_predictor = bytestream2_get_byteu(&gb);
752  if (block_predictor > 6) {
753  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
754  block_predictor);
755  return AVERROR_INVALIDDATA;
756  }
757  c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
758  c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
759  if (st) {
760  block_predictor = bytestream2_get_byteu(&gb);
761  if (block_predictor > 6) {
762  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
763  block_predictor);
764  return AVERROR_INVALIDDATA;
765  }
766  c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
767  c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
768  }
769  c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
770  if (st){
771  c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
772  }
773 
774  c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
775  if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
776  c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
777  if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
778 
779  *samples++ = c->status[0].sample2;
780  if (st) *samples++ = c->status[1].sample2;
781  *samples++ = c->status[0].sample1;
782  if (st) *samples++ = c->status[1].sample1;
783  for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
784  int byte = bytestream2_get_byteu(&gb);
785  *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
786  *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
787  }
788  break;
789  }
791  for (channel = 0; channel < avctx->channels; channel++) {
792  cs = &c->status[channel];
793  cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
794  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
795  if (cs->step_index > 88u){
796  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
797  channel, cs->step_index);
798  return AVERROR_INVALIDDATA;
799  }
800  }
801  for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
802  int v = bytestream2_get_byteu(&gb);
803  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
804  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
805  }
806  break;
808  {
809  int last_byte = 0;
810  int nibble;
811  int decode_top_nibble_next = 0;
812  int diff_channel;
813  const int16_t *samples_end = samples + avctx->channels * nb_samples;
814 
815  bytestream2_skipu(&gb, 10);
816  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
817  c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
818  c->status[0].step_index = bytestream2_get_byteu(&gb);
819  c->status[1].step_index = bytestream2_get_byteu(&gb);
820  if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
821  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
822  c->status[0].step_index, c->status[1].step_index);
823  return AVERROR_INVALIDDATA;
824  }
825  /* sign extend the predictors */
826  diff_channel = c->status[1].predictor;
827 
828  /* DK3 ADPCM support macro */
829 #define DK3_GET_NEXT_NIBBLE() \
830  if (decode_top_nibble_next) { \
831  nibble = last_byte >> 4; \
832  decode_top_nibble_next = 0; \
833  } else { \
834  last_byte = bytestream2_get_byteu(&gb); \
835  nibble = last_byte & 0x0F; \
836  decode_top_nibble_next = 1; \
837  }
838 
839  while (samples < samples_end) {
840 
841  /* for this algorithm, c->status[0] is the sum channel and
842  * c->status[1] is the diff channel */
843 
844  /* process the first predictor of the sum channel */
846  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
847 
848  /* process the diff channel predictor */
850  adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
851 
852  /* process the first pair of stereo PCM samples */
853  diff_channel = (diff_channel + c->status[1].predictor) / 2;
854  *samples++ = c->status[0].predictor + c->status[1].predictor;
855  *samples++ = c->status[0].predictor - c->status[1].predictor;
856 
857  /* process the second predictor of the sum channel */
859  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
860 
861  /* process the second pair of stereo PCM samples */
862  diff_channel = (diff_channel + c->status[1].predictor) / 2;
863  *samples++ = c->status[0].predictor + c->status[1].predictor;
864  *samples++ = c->status[0].predictor - c->status[1].predictor;
865  }
866  break;
867  }
869  for (channel = 0; channel < avctx->channels; channel++) {
870  cs = &c->status[channel];
871  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
872  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
873  if (cs->step_index > 88u){
874  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
875  channel, cs->step_index);
876  return AVERROR_INVALIDDATA;
877  }
878  }
879 
880  for (n = nb_samples >> (1 - st); n > 0; n--) {
881  int v1, v2;
882  int v = bytestream2_get_byteu(&gb);
883  /* nibbles are swapped for mono */
884  if (st) {
885  v1 = v >> 4;
886  v2 = v & 0x0F;
887  } else {
888  v2 = v >> 4;
889  v1 = v & 0x0F;
890  }
891  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
892  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
893  }
894  break;
896  while (bytestream2_get_bytes_left(&gb) > 0) {
897  int v = bytestream2_get_byteu(&gb);
898  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
899  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
900  }
901  break;
903  while (bytestream2_get_bytes_left(&gb) > 0) {
904  int v = bytestream2_get_byteu(&gb);
905  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
906  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
907  }
908  break;
910  if (c->vqa_version == 3) {
911  for (channel = 0; channel < avctx->channels; channel++) {
912  int16_t *smp = samples_p[channel];
913 
914  for (n = nb_samples / 2; n > 0; n--) {
915  int v = bytestream2_get_byteu(&gb);
916  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
917  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
918  }
919  }
920  } else {
921  for (n = nb_samples / 2; n > 0; n--) {
922  for (channel = 0; channel < avctx->channels; channel++) {
923  int v = bytestream2_get_byteu(&gb);
924  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
925  samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
926  }
927  samples += avctx->channels;
928  }
929  }
930  bytestream2_seek(&gb, 0, SEEK_END);
931  break;
933  {
934  int16_t *out0 = samples_p[0];
935  int16_t *out1 = samples_p[1];
936  int samples_per_block = 28 * (3 - avctx->channels) * 4;
937  int sample_offset = 0;
938  while (bytestream2_get_bytes_left(&gb) >= 128) {
939  if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
940  &c->status[0], &c->status[1],
941  avctx->channels, sample_offset)) < 0)
942  return ret;
943  bytestream2_skipu(&gb, 128);
944  sample_offset += samples_per_block;
945  }
946  break;
947  }
949  for (i=0; i<=st; i++) {
950  c->status[i].step_index = bytestream2_get_le32u(&gb);
951  if (c->status[i].step_index > 88u) {
952  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
953  i, c->status[i].step_index);
954  return AVERROR_INVALIDDATA;
955  }
956  }
957  for (i=0; i<=st; i++)
958  c->status[i].predictor = bytestream2_get_le32u(&gb);
959 
960  for (n = nb_samples >> (1 - st); n > 0; n--) {
961  int byte = bytestream2_get_byteu(&gb);
962  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
963  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
964  }
965  break;
967  for (n = nb_samples >> (1 - st); n > 0; n--) {
968  int byte = bytestream2_get_byteu(&gb);
969  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
970  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
971  }
972  break;
974  {
975  int previous_left_sample, previous_right_sample;
976  int current_left_sample, current_right_sample;
977  int next_left_sample, next_right_sample;
978  int coeff1l, coeff2l, coeff1r, coeff2r;
979  int shift_left, shift_right;
980 
981  /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
982  each coding 28 stereo samples. */
983 
984  if(avctx->channels != 2)
985  return AVERROR_INVALIDDATA;
986 
987  current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
988  previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
989  current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
990  previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
991 
992  for (count1 = 0; count1 < nb_samples / 28; count1++) {
993  int byte = bytestream2_get_byteu(&gb);
994  coeff1l = ea_adpcm_table[ byte >> 4 ];
995  coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
996  coeff1r = ea_adpcm_table[ byte & 0x0F];
997  coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
998 
999  byte = bytestream2_get_byteu(&gb);
1000  shift_left = 20 - (byte >> 4);
1001  shift_right = 20 - (byte & 0x0F);
1002 
1003  for (count2 = 0; count2 < 28; count2++) {
1004  byte = bytestream2_get_byteu(&gb);
1005  next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1006  next_right_sample = sign_extend(byte, 4) << shift_right;
1007 
1008  next_left_sample = (next_left_sample +
1009  (current_left_sample * coeff1l) +
1010  (previous_left_sample * coeff2l) + 0x80) >> 8;
1011  next_right_sample = (next_right_sample +
1012  (current_right_sample * coeff1r) +
1013  (previous_right_sample * coeff2r) + 0x80) >> 8;
1014 
1015  previous_left_sample = current_left_sample;
1016  current_left_sample = av_clip_int16(next_left_sample);
1017  previous_right_sample = current_right_sample;
1018  current_right_sample = av_clip_int16(next_right_sample);
1019  *samples++ = current_left_sample;
1020  *samples++ = current_right_sample;
1021  }
1022  }
1023 
1024  bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1025 
1026  break;
1027  }
1029  {
1030  int coeff[2][2], shift[2];
1031 
1032  for(channel = 0; channel < avctx->channels; channel++) {
1033  int byte = bytestream2_get_byteu(&gb);
1034  for (i=0; i<2; i++)
1035  coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1036  shift[channel] = 20 - (byte & 0x0F);
1037  }
1038  for (count1 = 0; count1 < nb_samples / 2; count1++) {
1039  int byte[2];
1040 
1041  byte[0] = bytestream2_get_byteu(&gb);
1042  if (st) byte[1] = bytestream2_get_byteu(&gb);
1043  for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1044  for(channel = 0; channel < avctx->channels; channel++) {
1045  int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1046  sample = (sample +
1047  c->status[channel].sample1 * coeff[channel][0] +
1048  c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1049  c->status[channel].sample2 = c->status[channel].sample1;
1050  c->status[channel].sample1 = av_clip_int16(sample);
1051  *samples++ = c->status[channel].sample1;
1052  }
1053  }
1054  }
1055  bytestream2_seek(&gb, 0, SEEK_END);
1056  break;
1057  }
1060  case AV_CODEC_ID_ADPCM_EA_R3: {
1061  /* channel numbering
1062  2chan: 0=fl, 1=fr
1063  4chan: 0=fl, 1=rl, 2=fr, 3=rr
1064  6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1065  const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1066  int previous_sample, current_sample, next_sample;
1067  int coeff1, coeff2;
1068  int shift;
1069  unsigned int channel;
1070  uint16_t *samplesC;
1071  int count = 0;
1072  int offsets[6];
1073 
1074  for (channel=0; channel<avctx->channels; channel++)
1075  offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1076  bytestream2_get_le32(&gb)) +
1077  (avctx->channels + 1) * 4;
1078 
1079  for (channel=0; channel<avctx->channels; channel++) {
1080  bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1081  samplesC = samples_p[channel];
1082 
1083  if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1084  current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1085  previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1086  } else {
1087  current_sample = c->status[channel].predictor;
1088  previous_sample = c->status[channel].prev_sample;
1089  }
1090 
1091  for (count1 = 0; count1 < nb_samples / 28; count1++) {
1092  int byte = bytestream2_get_byte(&gb);
1093  if (byte == 0xEE) { /* only seen in R2 and R3 */
1094  current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1095  previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1096 
1097  for (count2=0; count2<28; count2++)
1098  *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1099  } else {
1100  coeff1 = ea_adpcm_table[ byte >> 4 ];
1101  coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1102  shift = 20 - (byte & 0x0F);
1103 
1104  for (count2=0; count2<28; count2++) {
1105  if (count2 & 1)
1106  next_sample = sign_extend(byte, 4) << shift;
1107  else {
1108  byte = bytestream2_get_byte(&gb);
1109  next_sample = sign_extend(byte >> 4, 4) << shift;
1110  }
1111 
1112  next_sample += (current_sample * coeff1) +
1113  (previous_sample * coeff2);
1114  next_sample = av_clip_int16(next_sample >> 8);
1115 
1116  previous_sample = current_sample;
1117  current_sample = next_sample;
1118  *samplesC++ = current_sample;
1119  }
1120  }
1121  }
1122  if (!count) {
1123  count = count1;
1124  } else if (count != count1) {
1125  av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1126  count = FFMAX(count, count1);
1127  }
1128 
1129  if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1130  c->status[channel].predictor = current_sample;
1131  c->status[channel].prev_sample = previous_sample;
1132  }
1133  }
1134 
1135  frame->nb_samples = count * 28;
1136  bytestream2_seek(&gb, 0, SEEK_END);
1137  break;
1138  }
1140  for (channel=0; channel<avctx->channels; channel++) {
1141  int coeff[2][4], shift[4];
1142  int16_t *s = samples_p[channel];
1143  for (n = 0; n < 4; n++, s += 32) {
1144  int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1145  for (i=0; i<2; i++)
1146  coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1147  s[0] = val & ~0x0F;
1148 
1149  val = sign_extend(bytestream2_get_le16u(&gb), 16);
1150  shift[n] = 20 - (val & 0x0F);
1151  s[1] = val & ~0x0F;
1152  }
1153 
1154  for (m=2; m<32; m+=2) {
1155  s = &samples_p[channel][m];
1156  for (n = 0; n < 4; n++, s += 32) {
1157  int level, pred;
1158  int byte = bytestream2_get_byteu(&gb);
1159 
1160  level = sign_extend(byte >> 4, 4) << shift[n];
1161  pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1162  s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1163 
1164  level = sign_extend(byte, 4) << shift[n];
1165  pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1166  s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1167  }
1168  }
1169  }
1170  break;
1172  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1173  c->status[0].step_index = bytestream2_get_le16u(&gb);
1174  bytestream2_skipu(&gb, 4);
1175  if (c->status[0].step_index > 88u) {
1176  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1177  c->status[0].step_index);
1178  return AVERROR_INVALIDDATA;
1179  }
1180 
1181  for (n = nb_samples >> (1 - st); n > 0; n--) {
1182  int v = bytestream2_get_byteu(&gb);
1183 
1184  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1185  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1186  }
1187  break;
1189  for (i = 0; i < avctx->channels; i++) {
1190  c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1191  c->status[i].step_index = bytestream2_get_byteu(&gb);
1192  bytestream2_skipu(&gb, 1);
1193  if (c->status[i].step_index > 88u) {
1194  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1195  c->status[i].step_index);
1196  return AVERROR_INVALIDDATA;
1197  }
1198  }
1199 
1200  for (n = nb_samples >> (1 - st); n > 0; n--) {
1201  int v = bytestream2_get_byteu(&gb);
1202 
1203  *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1204  *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1205  }
1206  break;
1207  case AV_CODEC_ID_ADPCM_CT:
1208  for (n = nb_samples >> (1 - st); n > 0; n--) {
1209  int v = bytestream2_get_byteu(&gb);
1210  *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1211  *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1212  }
1213  break;
1217  if (!c->status[0].step_index) {
1218  /* the first byte is a raw sample */
1219  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1220  if (st)
1221  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1222  c->status[0].step_index = 1;
1223  nb_samples--;
1224  }
1225  if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1226  for (n = nb_samples >> (1 - st); n > 0; n--) {
1227  int byte = bytestream2_get_byteu(&gb);
1228  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1229  byte >> 4, 4, 0);
1230  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1231  byte & 0x0F, 4, 0);
1232  }
1233  } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1234  for (n = nb_samples / 3; n > 0; n--) {
1235  int byte = bytestream2_get_byteu(&gb);
1236  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1237  byte >> 5 , 3, 0);
1238  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1239  (byte >> 2) & 0x07, 3, 0);
1240  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1241  byte & 0x03, 2, 0);
1242  }
1243  } else {
1244  for (n = nb_samples >> (2 - st); n > 0; n--) {
1245  int byte = bytestream2_get_byteu(&gb);
1246  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1247  byte >> 6 , 2, 2);
1248  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1249  (byte >> 4) & 0x03, 2, 2);
1250  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1251  (byte >> 2) & 0x03, 2, 2);
1252  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1253  byte & 0x03, 2, 2);
1254  }
1255  }
1256  break;
1257  case AV_CODEC_ID_ADPCM_SWF:
1258  adpcm_swf_decode(avctx, buf, buf_size, samples);
1259  bytestream2_seek(&gb, 0, SEEK_END);
1260  break;
1262  for (n = nb_samples >> (1 - st); n > 0; n--) {
1263  int v = bytestream2_get_byteu(&gb);
1264  *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1265  *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1266  }
1267  break;
1268  case AV_CODEC_ID_ADPCM_AFC:
1269  {
1270  int samples_per_block;
1271  int blocks;
1272 
1273  if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1274  samples_per_block = avctx->extradata[0] / 16;
1275  blocks = nb_samples / avctx->extradata[0];
1276  } else {
1277  samples_per_block = nb_samples / 16;
1278  blocks = 1;
1279  }
1280 
1281  for (m = 0; m < blocks; m++) {
1282  for (channel = 0; channel < avctx->channels; channel++) {
1283  int prev1 = c->status[channel].sample1;
1284  int prev2 = c->status[channel].sample2;
1285 
1286  samples = samples_p[channel] + m * 16;
1287  /* Read in every sample for this channel. */
1288  for (i = 0; i < samples_per_block; i++) {
1289  int byte = bytestream2_get_byteu(&gb);
1290  int scale = 1 << (byte >> 4);
1291  int index = byte & 0xf;
1292  int factor1 = ff_adpcm_afc_coeffs[0][index];
1293  int factor2 = ff_adpcm_afc_coeffs[1][index];
1294 
1295  /* Decode 16 samples. */
1296  for (n = 0; n < 16; n++) {
1297  int32_t sampledat;
1298 
1299  if (n & 1) {
1300  sampledat = sign_extend(byte, 4);
1301  } else {
1302  byte = bytestream2_get_byteu(&gb);
1303  sampledat = sign_extend(byte >> 4, 4);
1304  }
1305 
1306  sampledat = ((prev1 * factor1 + prev2 * factor2) +
1307  ((sampledat * scale) << 11)) >> 11;
1308  *samples = av_clip_int16(sampledat);
1309  prev2 = prev1;
1310  prev1 = *samples++;
1311  }
1312  }
1313 
1314  c->status[channel].sample1 = prev1;
1315  c->status[channel].sample2 = prev2;
1316  }
1317  }
1318  bytestream2_seek(&gb, 0, SEEK_END);
1319  break;
1320  }
1321  case AV_CODEC_ID_ADPCM_THP:
1322  {
1323  int table[6][16];
1324  int ch;
1325 
1326  for (i = 0; i < avctx->channels; i++)
1327  for (n = 0; n < 16; n++)
1328  table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
1329 
1330  /* Initialize the previous sample. */
1331  for (i = 0; i < avctx->channels; i++) {
1332  c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
1333  c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
1334  }
1335 
1336  for (ch = 0; ch < avctx->channels; ch++) {
1337  samples = samples_p[ch];
1338 
1339  /* Read in every sample for this channel. */
1340  for (i = 0; i < nb_samples / 14; i++) {
1341  int byte = bytestream2_get_byteu(&gb);
1342  int index = (byte >> 4) & 7;
1343  unsigned int exp = byte & 0x0F;
1344  int factor1 = table[ch][index * 2];
1345  int factor2 = table[ch][index * 2 + 1];
1346 
1347  /* Decode 14 samples. */
1348  for (n = 0; n < 14; n++) {
1349  int32_t sampledat;
1350 
1351  if (n & 1) {
1352  sampledat = sign_extend(byte, 4);
1353  } else {
1354  byte = bytestream2_get_byteu(&gb);
1355  sampledat = sign_extend(byte >> 4, 4);
1356  }
1357 
1358  sampledat = ((c->status[ch].sample1 * factor1
1359  + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1360  *samples = av_clip_int16(sampledat);
1361  c->status[ch].sample2 = c->status[ch].sample1;
1362  c->status[ch].sample1 = *samples++;
1363  }
1364  }
1365  }
1366  break;
1367  }
1368 
1369  default:
1370  return -1;
1371  }
1372 
1373  if (avpkt->size && bytestream2_tell(&gb) == 0) {
1374  av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1375  return AVERROR_INVALIDDATA;
1376  }
1377 
1378  *got_frame_ptr = 1;
1379 
1380  return bytestream2_tell(&gb);
1381 }
1382 
1383 
1391 
1392 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1393 AVCodec ff_ ## name_ ## _decoder = { \
1394  .name = #name_, \
1395  .type = AVMEDIA_TYPE_AUDIO, \
1396  .id = id_, \
1397  .priv_data_size = sizeof(ADPCMDecodeContext), \
1398  .init = adpcm_decode_init, \
1399  .decode = adpcm_decode_frame, \
1400  .capabilities = CODEC_CAP_DR1, \
1401  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1402  .sample_fmts = sample_fmts_, \
1403 }
1404 
1405 /* Note: Do not forget to add new entries to the Makefile as well. */
1406 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1407 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1408 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1409 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1410 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1411 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1412 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1413 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1414 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1415 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1416 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1417 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1418 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1419 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1420 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1421 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1422 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1423 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1424 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1425 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1426 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1427 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1428 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1429 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1430 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1431 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1432 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1433 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1434 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");