FFmpeg
 All Data Structures Namespaces 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  * first version by Francois Revol (revol@free.fr)
5  * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6  * by Mike Melanson (melanson@pcisys.net)
7  * CD-ROM XA ADPCM codec by BERO
8  * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
9  * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
10  * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
11  * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
12  * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
13  * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
14  * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
15  *
16  * This file is part of FFmpeg.
17  *
18  * FFmpeg is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU Lesser General Public
20  * License as published by the Free Software Foundation; either
21  * version 2.1 of the License, or (at your option) any later version.
22  *
23  * FFmpeg is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26  * Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public
29  * License along with FFmpeg; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31  */
32 #include "avcodec.h"
33 #include "get_bits.h"
34 #include "bytestream.h"
35 #include "adpcm.h"
36 #include "adpcm_data.h"
37 #include "internal.h"
38 
39 /**
40  * @file
41  * ADPCM decoders
42  * Features and limitations:
43  *
44  * Reference documents:
45  * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
46  * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
47  * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
48  * http://openquicktime.sourceforge.net/
49  * XAnim sources (xa_codec.c) http://xanim.polter.net/
50  * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
51  * SoX source code http://sox.sourceforge.net/
52  *
53  * CD-ROM XA:
54  * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
55  * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
56  * readstr http://www.geocities.co.jp/Playtown/2004/
57  */
58 
59 /* These are for CD-ROM XA ADPCM */
60 static const int xa_adpcm_table[5][2] = {
61  { 0, 0 },
62  { 60, 0 },
63  { 115, -52 },
64  { 98, -55 },
65  { 122, -60 }
66 };
67 
68 static const int ea_adpcm_table[] = {
69  0, 240, 460, 392,
70  0, 0, -208, -220,
71  0, 1, 3, 4,
72  7, 8, 10, 11,
73  0, -1, -3, -4
74 };
75 
76 // padded to zero where table size is less then 16
77 static const int swf_index_tables[4][16] = {
78  /*2*/ { -1, 2 },
79  /*3*/ { -1, -1, 2, 4 },
80  /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
81  /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
82 };
83 
84 /* end of tables */
85 
86 typedef struct ADPCMDecodeContext {
88  int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
90 
92 {
93  ADPCMDecodeContext *c = avctx->priv_data;
94  unsigned int min_channels = 1;
95  unsigned int max_channels = 2;
96 
97  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 < 2 || avctx->bits_per_coded_sample > 5)
122  return AVERROR_INVALIDDATA;
123  break;
125  if (avctx->extradata && avctx->extradata_size >= 8) {
126  c->status[0].predictor = AV_RL32(avctx->extradata);
127  c->status[1].predictor = AV_RL32(avctx->extradata + 4);
128  }
129  break;
131  if (avctx->extradata && avctx->extradata_size >= 2)
132  c->vqa_version = AV_RL16(avctx->extradata);
133  break;
134  default:
135  break;
136  }
137 
138  switch(avctx->codec->id) {
151  break;
153  avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
155  break;
156  default:
157  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
158  }
159 
160  return 0;
161 }
162 
163 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
164 {
165  int step_index;
166  int predictor;
167  int sign, delta, diff, step;
168 
169  step = ff_adpcm_step_table[c->step_index];
170  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
171  step_index = av_clip(step_index, 0, 88);
172 
173  sign = nibble & 8;
174  delta = nibble & 7;
175  /* perform direct multiplication instead of series of jumps proposed by
176  * the reference ADPCM implementation since modern CPUs can do the mults
177  * quickly enough */
178  diff = ((2 * delta + 1) * step) >> shift;
179  predictor = c->predictor;
180  if (sign) predictor -= diff;
181  else predictor += diff;
182 
183  c->predictor = av_clip_int16(predictor);
184  c->step_index = step_index;
185 
186  return (short)c->predictor;
187 }
188 
190 {
191  int nibble, step_index, predictor, sign, delta, diff, step, shift;
192 
193  shift = bps - 1;
194  nibble = get_bits_le(gb, bps),
195  step = ff_adpcm_step_table[c->step_index];
196  step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
197  step_index = av_clip(step_index, 0, 88);
198 
199  sign = nibble & (1 << shift);
200  delta = nibble & ((1 << shift) - 1);
201  diff = ((2 * delta + 1) * step) >> shift;
202  predictor = c->predictor;
203  if (sign) predictor -= diff;
204  else predictor += diff;
205 
206  c->predictor = av_clip_int16(predictor);
207  c->step_index = step_index;
208 
209  return (int16_t)c->predictor;
210 }
211 
212 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
213 {
214  int step_index;
215  int predictor;
216  int diff, step;
217 
218  step = ff_adpcm_step_table[c->step_index];
219  step_index = c->step_index + ff_adpcm_index_table[nibble];
220  step_index = av_clip(step_index, 0, 88);
221 
222  diff = step >> 3;
223  if (nibble & 4) diff += step;
224  if (nibble & 2) diff += step >> 1;
225  if (nibble & 1) diff += step >> 2;
226 
227  if (nibble & 8)
228  predictor = c->predictor - diff;
229  else
230  predictor = c->predictor + diff;
231 
232  c->predictor = av_clip_int16(predictor);
233  c->step_index = step_index;
234 
235  return c->predictor;
236 }
237 
238 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
239 {
240  int predictor;
241 
242  predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
243  predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
244 
245  c->sample2 = c->sample1;
246  c->sample1 = av_clip_int16(predictor);
247  c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
248  if (c->idelta < 16) c->idelta = 16;
249 
250  return c->sample1;
251 }
252 
253 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
254 {
255  int step_index, predictor, sign, delta, diff, step;
256 
258  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
259  step_index = av_clip(step_index, 0, 48);
260 
261  sign = nibble & 8;
262  delta = nibble & 7;
263  diff = ((2 * delta + 1) * step) >> 3;
264  predictor = c->predictor;
265  if (sign) predictor -= diff;
266  else predictor += diff;
267 
268  c->predictor = av_clip(predictor, -2048, 2047);
269  c->step_index = step_index;
270 
271  return c->predictor << 4;
272 }
273 
274 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
275 {
276  int sign, delta, diff;
277  int new_step;
278 
279  sign = nibble & 8;
280  delta = nibble & 7;
281  /* perform direct multiplication instead of series of jumps proposed by
282  * the reference ADPCM implementation since modern CPUs can do the mults
283  * quickly enough */
284  diff = ((2 * delta + 1) * c->step) >> 3;
285  /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
286  c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
287  c->predictor = av_clip_int16(c->predictor);
288  /* calculate new step and clamp it to range 511..32767 */
289  new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
290  c->step = av_clip(new_step, 511, 32767);
291 
292  return (short)c->predictor;
293 }
294 
295 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
296 {
297  int sign, delta, diff;
298 
299  sign = nibble & (1<<(size-1));
300  delta = nibble & ((1<<(size-1))-1);
301  diff = delta << (7 + c->step + shift);
302 
303  /* clamp result */
304  c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
305 
306  /* calculate new step */
307  if (delta >= (2*size - 3) && c->step < 3)
308  c->step++;
309  else if (delta == 0 && c->step > 0)
310  c->step--;
311 
312  return (short) c->predictor;
313 }
314 
315 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
316 {
317  if(!c->step) {
318  c->predictor = 0;
319  c->step = 127;
320  }
321 
322  c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
323  c->predictor = av_clip_int16(c->predictor);
324  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
325  c->step = av_clip(c->step, 127, 24567);
326  return c->predictor;
327 }
328 
329 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
330  const uint8_t *in, ADPCMChannelStatus *left,
331  ADPCMChannelStatus *right, int channels, int sample_offset)
332 {
333  int i, j;
334  int shift,filter,f0,f1;
335  int s_1,s_2;
336  int d,s,t;
337 
338  out0 += sample_offset;
339  if (channels == 1)
340  out1 = out0 + 28;
341  else
342  out1 += sample_offset;
343 
344  for(i=0;i<4;i++) {
345  shift = 12 - (in[4+i*2] & 15);
346  filter = in[4+i*2] >> 4;
347  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
348  avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
349  filter=0;
350  }
351  f0 = xa_adpcm_table[filter][0];
352  f1 = xa_adpcm_table[filter][1];
353 
354  s_1 = left->sample1;
355  s_2 = left->sample2;
356 
357  for(j=0;j<28;j++) {
358  d = in[16+i+j*4];
359 
360  t = sign_extend(d, 4);
361  s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
362  s_2 = s_1;
363  s_1 = av_clip_int16(s);
364  out0[j] = s_1;
365  }
366 
367  if (channels == 2) {
368  left->sample1 = s_1;
369  left->sample2 = s_2;
370  s_1 = right->sample1;
371  s_2 = right->sample2;
372  }
373 
374  shift = 12 - (in[5+i*2] & 15);
375  filter = in[5+i*2] >> 4;
376  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
377  avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
378  filter=0;
379  }
380 
381  f0 = xa_adpcm_table[filter][0];
382  f1 = xa_adpcm_table[filter][1];
383 
384  for(j=0;j<28;j++) {
385  d = in[16+i+j*4];
386 
387  t = sign_extend(d >> 4, 4);
388  s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
389  s_2 = s_1;
390  s_1 = av_clip_int16(s);
391  out1[j] = s_1;
392  }
393 
394  if (channels == 2) {
395  right->sample1 = s_1;
396  right->sample2 = s_2;
397  } else {
398  left->sample1 = s_1;
399  left->sample2 = s_2;
400  }
401 
402  out0 += 28 * (3 - channels);
403  out1 += 28 * (3 - channels);
404  }
405 
406  return 0;
407 }
408 
409 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
410 {
411  ADPCMDecodeContext *c = avctx->priv_data;
412  GetBitContext gb;
413  const int *table;
414  int k0, signmask, nb_bits, count;
415  int size = buf_size*8;
416  int i;
417 
418  init_get_bits(&gb, buf, size);
419 
420  //read bits & initial values
421  nb_bits = get_bits(&gb, 2)+2;
422  table = swf_index_tables[nb_bits-2];
423  k0 = 1 << (nb_bits-2);
424  signmask = 1 << (nb_bits-1);
425 
426  while (get_bits_count(&gb) <= size - 22*avctx->channels) {
427  for (i = 0; i < avctx->channels; i++) {
428  *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
429  c->status[i].step_index = get_bits(&gb, 6);
430  }
431 
432  for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
433  int i;
434 
435  for (i = 0; i < avctx->channels; i++) {
436  // similar to IMA adpcm
437  int delta = get_bits(&gb, nb_bits);
438  int step = ff_adpcm_step_table[c->status[i].step_index];
439  long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
440  int k = k0;
441 
442  do {
443  if (delta & k)
444  vpdiff += step;
445  step >>= 1;
446  k >>= 1;
447  } while(k);
448  vpdiff += step;
449 
450  if (delta & signmask)
451  c->status[i].predictor -= vpdiff;
452  else
453  c->status[i].predictor += vpdiff;
454 
455  c->status[i].step_index += table[delta & (~signmask)];
456 
457  c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
458  c->status[i].predictor = av_clip_int16(c->status[i].predictor);
459 
460  *samples++ = c->status[i].predictor;
461  }
462  }
463  }
464 }
465 
466 /**
467  * Get the number of samples that will be decoded from the packet.
468  * In one case, this is actually the maximum number of samples possible to
469  * decode with the given buf_size.
470  *
471  * @param[out] coded_samples set to the number of samples as coded in the
472  * packet, or 0 if the codec does not encode the
473  * number of samples in each frame.
474  */
476  int buf_size, int *coded_samples)
477 {
478  ADPCMDecodeContext *s = avctx->priv_data;
479  int nb_samples = 0;
480  int ch = avctx->channels;
481  int has_coded_samples = 0;
482  int header_size;
483 
484  *coded_samples = 0;
485 
486  if(ch <= 0)
487  return 0;
488 
489  switch (avctx->codec->id) {
490  /* constant, only check buf_size */
492  if (buf_size < 76 * ch)
493  return 0;
494  nb_samples = 128;
495  break;
497  if (buf_size < 34 * ch)
498  return 0;
499  nb_samples = 64;
500  break;
501  /* simple 4-bit adpcm */
508  nb_samples = buf_size * 2 / ch;
509  break;
510  }
511  if (nb_samples)
512  return nb_samples;
513 
514  /* simple 4-bit adpcm, with header */
515  header_size = 0;
516  switch (avctx->codec->id) {
518  case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
519  case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
520  case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
521  }
522  if (header_size > 0)
523  return (buf_size - header_size) * 2 / ch;
524 
525  /* more complex formats */
526  switch (avctx->codec->id) {
528  has_coded_samples = 1;
529  *coded_samples = bytestream2_get_le32(gb);
530  *coded_samples -= *coded_samples % 28;
531  nb_samples = (buf_size - 12) / 30 * 28;
532  break;
534  has_coded_samples = 1;
535  *coded_samples = bytestream2_get_le32(gb);
536  nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
537  break;
539  nb_samples = (buf_size - ch) / ch * 2;
540  break;
544  /* maximum number of samples */
545  /* has internal offsets and a per-frame switch to signal raw 16-bit */
546  has_coded_samples = 1;
547  switch (avctx->codec->id) {
549  header_size = 4 + 9 * ch;
550  *coded_samples = bytestream2_get_le32(gb);
551  break;
553  header_size = 4 + 5 * ch;
554  *coded_samples = bytestream2_get_le32(gb);
555  break;
557  header_size = 4 + 5 * ch;
558  *coded_samples = bytestream2_get_be32(gb);
559  break;
560  }
561  *coded_samples -= *coded_samples % 28;
562  nb_samples = (buf_size - header_size) * 2 / ch;
563  nb_samples -= nb_samples % 28;
564  break;
566  if (avctx->block_align > 0)
567  buf_size = FFMIN(buf_size, avctx->block_align);
568  nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
569  break;
571  if (avctx->block_align > 0)
572  buf_size = FFMIN(buf_size, avctx->block_align);
573  nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
574  break;
576  if (avctx->block_align > 0)
577  buf_size = FFMIN(buf_size, avctx->block_align);
578  nb_samples = (buf_size - 4 * ch) * 2 / ch;
579  break;
581  {
582  int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
583  int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
584  if (avctx->block_align > 0)
585  buf_size = FFMIN(buf_size, avctx->block_align);
586  nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
587  break;
588  }
590  if (avctx->block_align > 0)
591  buf_size = FFMIN(buf_size, avctx->block_align);
592  nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
593  break;
597  {
598  int samples_per_byte;
599  switch (avctx->codec->id) {
600  case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
601  case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
602  case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
603  }
604  if (!s->status[0].step_index) {
605  nb_samples++;
606  buf_size -= ch;
607  }
608  nb_samples += buf_size * samples_per_byte / ch;
609  break;
610  }
612  {
613  int buf_bits = buf_size * 8 - 2;
614  int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
615  int block_hdr_size = 22 * ch;
616  int block_size = block_hdr_size + nbits * ch * 4095;
617  int nblocks = buf_bits / block_size;
618  int bits_left = buf_bits - nblocks * block_size;
619  nb_samples = nblocks * 4096;
620  if (bits_left >= block_hdr_size)
621  nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
622  break;
623  }
625  if (avctx->extradata) {
626  nb_samples = buf_size / (8 * ch) * 14;
627  break;
628  }
629  has_coded_samples = 1;
630  bytestream2_skip(gb, 4); // channel size
631  *coded_samples = bytestream2_get_be32(gb);
632  *coded_samples -= *coded_samples % 14;
633  nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
634  break;
636  nb_samples = buf_size / (9 * ch) * 16;
637  break;
639  nb_samples = (buf_size / 128) * 224 / ch;
640  break;
642  nb_samples = buf_size / (16 * ch) * 28;
643  break;
644  }
645 
646  /* validate coded sample count */
647  if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
648  return AVERROR_INVALIDDATA;
649 
650  return nb_samples;
651 }
652 
653 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
654  int *got_frame_ptr, AVPacket *avpkt)
655 {
656  AVFrame *frame = data;
657  const uint8_t *buf = avpkt->data;
658  int buf_size = avpkt->size;
659  ADPCMDecodeContext *c = avctx->priv_data;
660  ADPCMChannelStatus *cs;
661  int n, m, channel, i;
662  short *samples;
663  int16_t **samples_p;
664  int st; /* stereo */
665  int count1, count2;
666  int nb_samples, coded_samples, ret;
667  GetByteContext gb;
668 
669  bytestream2_init(&gb, buf, buf_size);
670  nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
671  if (nb_samples <= 0) {
672  av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
673  return AVERROR_INVALIDDATA;
674  }
675 
676  /* get output buffer */
677  frame->nb_samples = nb_samples;
678  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
679  return ret;
680  samples = (short *)frame->data[0];
681  samples_p = (int16_t **)frame->extended_data;
682 
683  /* use coded_samples when applicable */
684  /* it is always <= nb_samples, so the output buffer will be large enough */
685  if (coded_samples) {
686  if (coded_samples != nb_samples)
687  av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
688  frame->nb_samples = nb_samples = coded_samples;
689  }
690 
691  st = avctx->channels == 2 ? 1 : 0;
692 
693  switch(avctx->codec->id) {
695  /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
696  Channel data is interleaved per-chunk. */
697  for (channel = 0; channel < avctx->channels; channel++) {
698  int predictor;
699  int step_index;
700  cs = &(c->status[channel]);
701  /* (pppppp) (piiiiiii) */
702 
703  /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
704  predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
705  step_index = predictor & 0x7F;
706  predictor &= ~0x7F;
707 
708  if (cs->step_index == step_index) {
709  int diff = predictor - cs->predictor;
710  if (diff < 0)
711  diff = - diff;
712  if (diff > 0x7f)
713  goto update;
714  } else {
715  update:
716  cs->step_index = step_index;
717  cs->predictor = predictor;
718  }
719 
720  if (cs->step_index > 88u){
721  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
722  channel, cs->step_index);
723  return AVERROR_INVALIDDATA;
724  }
725 
726  samples = samples_p[channel];
727 
728  for (m = 0; m < 64; m += 2) {
729  int byte = bytestream2_get_byteu(&gb);
730  samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
731  samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
732  }
733  }
734  break;
736  for(i=0; i<avctx->channels; i++){
737  cs = &(c->status[i]);
738  cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
739 
740  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
741  if (cs->step_index > 88u){
742  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
743  i, cs->step_index);
744  return AVERROR_INVALIDDATA;
745  }
746  }
747 
748  if (avctx->bits_per_coded_sample != 4) {
749  int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
751 
753  for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
754  for (i = 0; i < avctx->channels; i++) {
755  cs = &c->status[i];
756  samples = &samples_p[i][1 + n * samples_per_block];
757  for (m = 0; m < samples_per_block; m++) {
758  samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
759  avctx->bits_per_coded_sample);
760  }
761  }
762  }
763  bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
764  } else {
765  for (n = 0; n < (nb_samples - 1) / 8; n++) {
766  for (i = 0; i < avctx->channels; i++) {
767  cs = &c->status[i];
768  samples = &samples_p[i][1 + n * 8];
769  for (m = 0; m < 8; m += 2) {
770  int v = bytestream2_get_byteu(&gb);
771  samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
772  samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
773  }
774  }
775  }
776  }
777  break;
779  for (i = 0; i < avctx->channels; i++)
780  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
781 
782  for (i = 0; i < avctx->channels; i++) {
783  c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
784  if (c->status[i].step_index > 88u) {
785  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
786  i, c->status[i].step_index);
787  return AVERROR_INVALIDDATA;
788  }
789  }
790 
791  for (i = 0; i < avctx->channels; i++) {
792  samples = (int16_t *)frame->data[i];
793  cs = &c->status[i];
794  for (n = nb_samples >> 1; n > 0; n--) {
795  int v = bytestream2_get_byteu(&gb);
796  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
797  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
798  }
799  }
800  break;
802  {
803  int block_predictor;
804 
805  block_predictor = bytestream2_get_byteu(&gb);
806  if (block_predictor > 6) {
807  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
808  block_predictor);
809  return AVERROR_INVALIDDATA;
810  }
811  c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
812  c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
813  if (st) {
814  block_predictor = bytestream2_get_byteu(&gb);
815  if (block_predictor > 6) {
816  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
817  block_predictor);
818  return AVERROR_INVALIDDATA;
819  }
820  c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
821  c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
822  }
823  c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
824  if (st){
825  c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
826  }
827 
828  c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
829  if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
830  c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
831  if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
832 
833  *samples++ = c->status[0].sample2;
834  if (st) *samples++ = c->status[1].sample2;
835  *samples++ = c->status[0].sample1;
836  if (st) *samples++ = c->status[1].sample1;
837  for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
838  int byte = bytestream2_get_byteu(&gb);
839  *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
840  *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
841  }
842  break;
843  }
845  for (channel = 0; channel < avctx->channels; channel++) {
846  cs = &c->status[channel];
847  cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
848  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
849  if (cs->step_index > 88u){
850  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
851  channel, cs->step_index);
852  return AVERROR_INVALIDDATA;
853  }
854  }
855  for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
856  int v = bytestream2_get_byteu(&gb);
857  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
858  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
859  }
860  break;
862  {
863  int last_byte = 0;
864  int nibble;
865  int decode_top_nibble_next = 0;
866  int diff_channel;
867  const int16_t *samples_end = samples + avctx->channels * nb_samples;
868 
869  bytestream2_skipu(&gb, 10);
870  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
871  c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
872  c->status[0].step_index = bytestream2_get_byteu(&gb);
873  c->status[1].step_index = bytestream2_get_byteu(&gb);
874  if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
875  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
876  c->status[0].step_index, c->status[1].step_index);
877  return AVERROR_INVALIDDATA;
878  }
879  /* sign extend the predictors */
880  diff_channel = c->status[1].predictor;
881 
882  /* DK3 ADPCM support macro */
883 #define DK3_GET_NEXT_NIBBLE() \
884  if (decode_top_nibble_next) { \
885  nibble = last_byte >> 4; \
886  decode_top_nibble_next = 0; \
887  } else { \
888  last_byte = bytestream2_get_byteu(&gb); \
889  nibble = last_byte & 0x0F; \
890  decode_top_nibble_next = 1; \
891  }
892 
893  while (samples < samples_end) {
894 
895  /* for this algorithm, c->status[0] is the sum channel and
896  * c->status[1] is the diff channel */
897 
898  /* process the first predictor of the sum channel */
900  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
901 
902  /* process the diff channel predictor */
904  adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
905 
906  /* process the first pair of stereo PCM samples */
907  diff_channel = (diff_channel + c->status[1].predictor) / 2;
908  *samples++ = c->status[0].predictor + c->status[1].predictor;
909  *samples++ = c->status[0].predictor - c->status[1].predictor;
910 
911  /* process the second predictor of the sum channel */
913  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
914 
915  /* process the second pair of stereo PCM samples */
916  diff_channel = (diff_channel + c->status[1].predictor) / 2;
917  *samples++ = c->status[0].predictor + c->status[1].predictor;
918  *samples++ = c->status[0].predictor - c->status[1].predictor;
919  }
920  break;
921  }
923  for (channel = 0; channel < avctx->channels; channel++) {
924  cs = &c->status[channel];
925  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
926  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
927  if (cs->step_index > 88u){
928  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
929  channel, cs->step_index);
930  return AVERROR_INVALIDDATA;
931  }
932  }
933 
934  for (n = nb_samples >> (1 - st); n > 0; n--) {
935  int v1, v2;
936  int v = bytestream2_get_byteu(&gb);
937  /* nibbles are swapped for mono */
938  if (st) {
939  v1 = v >> 4;
940  v2 = v & 0x0F;
941  } else {
942  v2 = v >> 4;
943  v1 = v & 0x0F;
944  }
945  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
946  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
947  }
948  break;
950  while (bytestream2_get_bytes_left(&gb) > 0) {
951  int v = bytestream2_get_byteu(&gb);
952  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
953  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
954  }
955  break;
957  while (bytestream2_get_bytes_left(&gb) > 0) {
958  int v = bytestream2_get_byteu(&gb);
959  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
960  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
961  }
962  break;
964  for (channel = 0; channel < avctx->channels; channel++) {
965  cs = &c->status[channel];
966  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
967  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
968  if (cs->step_index > 88u){
969  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
970  channel, cs->step_index);
971  return AVERROR_INVALIDDATA;
972  }
973  }
974  for (n = 0; n < nb_samples / 2; n++) {
975  int byte[2];
976 
977  byte[0] = bytestream2_get_byteu(&gb);
978  if (st)
979  byte[1] = bytestream2_get_byteu(&gb);
980  for(channel = 0; channel < avctx->channels; channel++) {
981  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
982  }
983  for(channel = 0; channel < avctx->channels; channel++) {
984  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
985  }
986  }
987  break;
989  if (c->vqa_version == 3) {
990  for (channel = 0; channel < avctx->channels; channel++) {
991  int16_t *smp = samples_p[channel];
992 
993  for (n = nb_samples / 2; n > 0; n--) {
994  int v = bytestream2_get_byteu(&gb);
995  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
996  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
997  }
998  }
999  } else {
1000  for (n = nb_samples / 2; n > 0; n--) {
1001  for (channel = 0; channel < avctx->channels; channel++) {
1002  int v = bytestream2_get_byteu(&gb);
1003  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1004  samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1005  }
1006  samples += avctx->channels;
1007  }
1008  }
1009  bytestream2_seek(&gb, 0, SEEK_END);
1010  break;
1011  case AV_CODEC_ID_ADPCM_XA:
1012  {
1013  int16_t *out0 = samples_p[0];
1014  int16_t *out1 = samples_p[1];
1015  int samples_per_block = 28 * (3 - avctx->channels) * 4;
1016  int sample_offset = 0;
1017  while (bytestream2_get_bytes_left(&gb) >= 128) {
1018  if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1019  &c->status[0], &c->status[1],
1020  avctx->channels, sample_offset)) < 0)
1021  return ret;
1022  bytestream2_skipu(&gb, 128);
1023  sample_offset += samples_per_block;
1024  }
1025  break;
1026  }
1028  for (i=0; i<=st; i++) {
1029  c->status[i].step_index = bytestream2_get_le32u(&gb);
1030  if (c->status[i].step_index > 88u) {
1031  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1032  i, c->status[i].step_index);
1033  return AVERROR_INVALIDDATA;
1034  }
1035  }
1036  for (i=0; i<=st; i++)
1037  c->status[i].predictor = bytestream2_get_le32u(&gb);
1038 
1039  for (n = nb_samples >> (1 - st); n > 0; n--) {
1040  int byte = bytestream2_get_byteu(&gb);
1041  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1042  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1043  }
1044  break;
1046  for (n = nb_samples >> (1 - st); n > 0; n--) {
1047  int byte = bytestream2_get_byteu(&gb);
1048  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1049  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1050  }
1051  break;
1052  case AV_CODEC_ID_ADPCM_EA:
1053  {
1054  int previous_left_sample, previous_right_sample;
1055  int current_left_sample, current_right_sample;
1056  int next_left_sample, next_right_sample;
1057  int coeff1l, coeff2l, coeff1r, coeff2r;
1058  int shift_left, shift_right;
1059 
1060  /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1061  each coding 28 stereo samples. */
1062 
1063  if(avctx->channels != 2)
1064  return AVERROR_INVALIDDATA;
1065 
1066  current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1067  previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1068  current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1069  previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1070 
1071  for (count1 = 0; count1 < nb_samples / 28; count1++) {
1072  int byte = bytestream2_get_byteu(&gb);
1073  coeff1l = ea_adpcm_table[ byte >> 4 ];
1074  coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1075  coeff1r = ea_adpcm_table[ byte & 0x0F];
1076  coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1077 
1078  byte = bytestream2_get_byteu(&gb);
1079  shift_left = 20 - (byte >> 4);
1080  shift_right = 20 - (byte & 0x0F);
1081 
1082  for (count2 = 0; count2 < 28; count2++) {
1083  byte = bytestream2_get_byteu(&gb);
1084  next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1085  next_right_sample = sign_extend(byte, 4) << shift_right;
1086 
1087  next_left_sample = (next_left_sample +
1088  (current_left_sample * coeff1l) +
1089  (previous_left_sample * coeff2l) + 0x80) >> 8;
1090  next_right_sample = (next_right_sample +
1091  (current_right_sample * coeff1r) +
1092  (previous_right_sample * coeff2r) + 0x80) >> 8;
1093 
1094  previous_left_sample = current_left_sample;
1095  current_left_sample = av_clip_int16(next_left_sample);
1096  previous_right_sample = current_right_sample;
1097  current_right_sample = av_clip_int16(next_right_sample);
1098  *samples++ = current_left_sample;
1099  *samples++ = current_right_sample;
1100  }
1101  }
1102 
1103  bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1104 
1105  break;
1106  }
1108  {
1109  int coeff[2][2], shift[2];
1110 
1111  for(channel = 0; channel < avctx->channels; channel++) {
1112  int byte = bytestream2_get_byteu(&gb);
1113  for (i=0; i<2; i++)
1114  coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1115  shift[channel] = 20 - (byte & 0x0F);
1116  }
1117  for (count1 = 0; count1 < nb_samples / 2; count1++) {
1118  int byte[2];
1119 
1120  byte[0] = bytestream2_get_byteu(&gb);
1121  if (st) byte[1] = bytestream2_get_byteu(&gb);
1122  for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1123  for(channel = 0; channel < avctx->channels; channel++) {
1124  int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1125  sample = (sample +
1126  c->status[channel].sample1 * coeff[channel][0] +
1127  c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1128  c->status[channel].sample2 = c->status[channel].sample1;
1129  c->status[channel].sample1 = av_clip_int16(sample);
1130  *samples++ = c->status[channel].sample1;
1131  }
1132  }
1133  }
1134  bytestream2_seek(&gb, 0, SEEK_END);
1135  break;
1136  }
1139  case AV_CODEC_ID_ADPCM_EA_R3: {
1140  /* channel numbering
1141  2chan: 0=fl, 1=fr
1142  4chan: 0=fl, 1=rl, 2=fr, 3=rr
1143  6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1144  const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1145  int previous_sample, current_sample, next_sample;
1146  int coeff1, coeff2;
1147  int shift;
1148  unsigned int channel;
1149  uint16_t *samplesC;
1150  int count = 0;
1151  int offsets[6];
1152 
1153  for (channel=0; channel<avctx->channels; channel++)
1154  offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1155  bytestream2_get_le32(&gb)) +
1156  (avctx->channels + 1) * 4;
1157 
1158  for (channel=0; channel<avctx->channels; channel++) {
1159  bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1160  samplesC = samples_p[channel];
1161 
1162  if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1163  current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1164  previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1165  } else {
1166  current_sample = c->status[channel].predictor;
1167  previous_sample = c->status[channel].prev_sample;
1168  }
1169 
1170  for (count1 = 0; count1 < nb_samples / 28; count1++) {
1171  int byte = bytestream2_get_byte(&gb);
1172  if (byte == 0xEE) { /* only seen in R2 and R3 */
1173  current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1174  previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1175 
1176  for (count2=0; count2<28; count2++)
1177  *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1178  } else {
1179  coeff1 = ea_adpcm_table[ byte >> 4 ];
1180  coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1181  shift = 20 - (byte & 0x0F);
1182 
1183  for (count2=0; count2<28; count2++) {
1184  if (count2 & 1)
1185  next_sample = sign_extend(byte, 4) << shift;
1186  else {
1187  byte = bytestream2_get_byte(&gb);
1188  next_sample = sign_extend(byte >> 4, 4) << shift;
1189  }
1190 
1191  next_sample += (current_sample * coeff1) +
1192  (previous_sample * coeff2);
1193  next_sample = av_clip_int16(next_sample >> 8);
1194 
1195  previous_sample = current_sample;
1196  current_sample = next_sample;
1197  *samplesC++ = current_sample;
1198  }
1199  }
1200  }
1201  if (!count) {
1202  count = count1;
1203  } else if (count != count1) {
1204  av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1205  count = FFMAX(count, count1);
1206  }
1207 
1208  if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1209  c->status[channel].predictor = current_sample;
1210  c->status[channel].prev_sample = previous_sample;
1211  }
1212  }
1213 
1214  frame->nb_samples = count * 28;
1215  bytestream2_seek(&gb, 0, SEEK_END);
1216  break;
1217  }
1219  for (channel=0; channel<avctx->channels; channel++) {
1220  int coeff[2][4], shift[4];
1221  int16_t *s = samples_p[channel];
1222  for (n = 0; n < 4; n++, s += 32) {
1223  int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1224  for (i=0; i<2; i++)
1225  coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1226  s[0] = val & ~0x0F;
1227 
1228  val = sign_extend(bytestream2_get_le16u(&gb), 16);
1229  shift[n] = 20 - (val & 0x0F);
1230  s[1] = val & ~0x0F;
1231  }
1232 
1233  for (m=2; m<32; m+=2) {
1234  s = &samples_p[channel][m];
1235  for (n = 0; n < 4; n++, s += 32) {
1236  int level, pred;
1237  int byte = bytestream2_get_byteu(&gb);
1238 
1239  level = sign_extend(byte >> 4, 4) << shift[n];
1240  pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1241  s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1242 
1243  level = sign_extend(byte, 4) << shift[n];
1244  pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1245  s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1246  }
1247  }
1248  }
1249  break;
1251  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1252  c->status[0].step_index = bytestream2_get_le16u(&gb);
1253  bytestream2_skipu(&gb, 4);
1254  if (c->status[0].step_index > 88u) {
1255  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1256  c->status[0].step_index);
1257  return AVERROR_INVALIDDATA;
1258  }
1259 
1260  for (n = nb_samples >> (1 - st); n > 0; n--) {
1261  int v = bytestream2_get_byteu(&gb);
1262 
1263  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1264  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1265  }
1266  break;
1268  for (i = 0; i < avctx->channels; i++) {
1269  c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1270  c->status[i].step_index = bytestream2_get_byteu(&gb);
1271  bytestream2_skipu(&gb, 1);
1272  if (c->status[i].step_index > 88u) {
1273  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1274  c->status[i].step_index);
1275  return AVERROR_INVALIDDATA;
1276  }
1277  }
1278 
1279  for (n = nb_samples >> (1 - st); n > 0; n--) {
1280  int v = bytestream2_get_byteu(&gb);
1281 
1282  *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1283  *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1284  }
1285  break;
1286  case AV_CODEC_ID_ADPCM_CT:
1287  for (n = nb_samples >> (1 - st); n > 0; n--) {
1288  int v = bytestream2_get_byteu(&gb);
1289  *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1290  *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1291  }
1292  break;
1296  if (!c->status[0].step_index) {
1297  /* the first byte is a raw sample */
1298  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1299  if (st)
1300  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1301  c->status[0].step_index = 1;
1302  nb_samples--;
1303  }
1304  if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1305  for (n = nb_samples >> (1 - st); n > 0; n--) {
1306  int byte = bytestream2_get_byteu(&gb);
1307  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1308  byte >> 4, 4, 0);
1309  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1310  byte & 0x0F, 4, 0);
1311  }
1312  } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1313  for (n = nb_samples / 3; n > 0; n--) {
1314  int byte = bytestream2_get_byteu(&gb);
1315  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1316  byte >> 5 , 3, 0);
1317  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1318  (byte >> 2) & 0x07, 3, 0);
1319  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1320  byte & 0x03, 2, 0);
1321  }
1322  } else {
1323  for (n = nb_samples >> (2 - st); n > 0; n--) {
1324  int byte = bytestream2_get_byteu(&gb);
1325  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1326  byte >> 6 , 2, 2);
1327  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1328  (byte >> 4) & 0x03, 2, 2);
1329  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1330  (byte >> 2) & 0x03, 2, 2);
1331  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1332  byte & 0x03, 2, 2);
1333  }
1334  }
1335  break;
1336  case AV_CODEC_ID_ADPCM_SWF:
1337  adpcm_swf_decode(avctx, buf, buf_size, samples);
1338  bytestream2_seek(&gb, 0, SEEK_END);
1339  break;
1341  for (n = nb_samples >> (1 - st); n > 0; n--) {
1342  int v = bytestream2_get_byteu(&gb);
1343  *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1344  *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1345  }
1346  break;
1347  case AV_CODEC_ID_ADPCM_AFC:
1348  {
1349  int samples_per_block;
1350  int blocks;
1351 
1352  if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1353  samples_per_block = avctx->extradata[0] / 16;
1354  blocks = nb_samples / avctx->extradata[0];
1355  } else {
1356  samples_per_block = nb_samples / 16;
1357  blocks = 1;
1358  }
1359 
1360  for (m = 0; m < blocks; m++) {
1361  for (channel = 0; channel < avctx->channels; channel++) {
1362  int prev1 = c->status[channel].sample1;
1363  int prev2 = c->status[channel].sample2;
1364 
1365  samples = samples_p[channel] + m * 16;
1366  /* Read in every sample for this channel. */
1367  for (i = 0; i < samples_per_block; i++) {
1368  int byte = bytestream2_get_byteu(&gb);
1369  int scale = 1 << (byte >> 4);
1370  int index = byte & 0xf;
1371  int factor1 = ff_adpcm_afc_coeffs[0][index];
1372  int factor2 = ff_adpcm_afc_coeffs[1][index];
1373 
1374  /* Decode 16 samples. */
1375  for (n = 0; n < 16; n++) {
1376  int32_t sampledat;
1377 
1378  if (n & 1) {
1379  sampledat = sign_extend(byte, 4);
1380  } else {
1381  byte = bytestream2_get_byteu(&gb);
1382  sampledat = sign_extend(byte >> 4, 4);
1383  }
1384 
1385  sampledat = ((prev1 * factor1 + prev2 * factor2) +
1386  ((sampledat * scale) << 11)) >> 11;
1387  *samples = av_clip_int16(sampledat);
1388  prev2 = prev1;
1389  prev1 = *samples++;
1390  }
1391  }
1392 
1393  c->status[channel].sample1 = prev1;
1394  c->status[channel].sample2 = prev2;
1395  }
1396  }
1397  bytestream2_seek(&gb, 0, SEEK_END);
1398  break;
1399  }
1400  case AV_CODEC_ID_ADPCM_THP:
1401  {
1402  int table[6][16];
1403  int ch;
1404 
1405  if (avctx->extradata) {
1407  if (avctx->extradata_size < 32 * avctx->channels) {
1408  av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1409  return AVERROR_INVALIDDATA;
1410  }
1411 
1412  bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1413  for (i = 0; i < avctx->channels; i++)
1414  for (n = 0; n < 16; n++)
1415  table[i][n] = sign_extend(bytestream2_get_be16u(&tb), 16);
1416  } else {
1417  for (i = 0; i < avctx->channels; i++)
1418  for (n = 0; n < 16; n++)
1419  table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
1420 
1421  /* Initialize the previous sample. */
1422  for (i = 0; i < avctx->channels; i++) {
1423  c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
1424  c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
1425  }
1426  }
1427 
1428  for (ch = 0; ch < avctx->channels; ch++) {
1429  samples = samples_p[ch];
1430 
1431  /* Read in every sample for this channel. */
1432  for (i = 0; i < nb_samples / 14; i++) {
1433  int byte = bytestream2_get_byteu(&gb);
1434  int index = (byte >> 4) & 7;
1435  unsigned int exp = byte & 0x0F;
1436  int factor1 = table[ch][index * 2];
1437  int factor2 = table[ch][index * 2 + 1];
1438 
1439  /* Decode 14 samples. */
1440  for (n = 0; n < 14; n++) {
1441  int32_t sampledat;
1442 
1443  if (n & 1) {
1444  sampledat = sign_extend(byte, 4);
1445  } else {
1446  byte = bytestream2_get_byteu(&gb);
1447  sampledat = sign_extend(byte >> 4, 4);
1448  }
1449 
1450  sampledat = ((c->status[ch].sample1 * factor1
1451  + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1452  *samples = av_clip_int16(sampledat);
1453  c->status[ch].sample2 = c->status[ch].sample1;
1454  c->status[ch].sample1 = *samples++;
1455  }
1456  }
1457  }
1458  break;
1459  }
1460  case AV_CODEC_ID_ADPCM_DTK:
1461  for (channel = 0; channel < avctx->channels; channel++) {
1462  samples = samples_p[channel];
1463 
1464  /* Read in every sample for this channel. */
1465  for (i = 0; i < nb_samples / 28; i++) {
1466  int byte, header;
1467  if (channel)
1468  bytestream2_skipu(&gb, 1);
1469  header = bytestream2_get_byteu(&gb);
1470  bytestream2_skipu(&gb, 3 - channel);
1471 
1472  /* Decode 28 samples. */
1473  for (n = 0; n < 28; n++) {
1474  int32_t sampledat, prev;
1475 
1476  switch (header >> 4) {
1477  case 1:
1478  prev = (c->status[channel].sample1 * 0x3c);
1479  break;
1480  case 2:
1481  prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1482  break;
1483  case 3:
1484  prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1485  break;
1486  default:
1487  prev = 0;
1488  }
1489 
1490  prev = av_clip((prev + 0x20) >> 6, -0x200000, 0x1fffff);
1491 
1492  byte = bytestream2_get_byteu(&gb);
1493  if (!channel)
1494  sampledat = sign_extend(byte, 4);
1495  else
1496  sampledat = sign_extend(byte >> 4, 4);
1497 
1498  sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1499  *samples++ = av_clip_int16(sampledat >> 6);
1500  c->status[channel].sample2 = c->status[channel].sample1;
1501  c->status[channel].sample1 = sampledat;
1502  }
1503  }
1504  if (!channel)
1505  bytestream2_seek(&gb, 0, SEEK_SET);
1506  }
1507  break;
1508 
1509  default:
1510  return -1;
1511  }
1512 
1513  if (avpkt->size && bytestream2_tell(&gb) == 0) {
1514  av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1515  return AVERROR_INVALIDDATA;
1516  }
1517 
1518  *got_frame_ptr = 1;
1519 
1520  return bytestream2_tell(&gb);
1521 }
1522 
1523 
1531 
1532 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1533 AVCodec ff_ ## name_ ## _decoder = { \
1534  .name = #name_, \
1535  .type = AVMEDIA_TYPE_AUDIO, \
1536  .id = id_, \
1537  .priv_data_size = sizeof(ADPCMDecodeContext), \
1538  .init = adpcm_decode_init, \
1539  .decode = adpcm_decode_frame, \
1540  .capabilities = CODEC_CAP_DR1, \
1541  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1542  .sample_fmts = sample_fmts_, \
1543 }
1544 
1545 /* Note: Do not forget to add new entries to the Makefile as well. */
1546 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1547 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1548 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1549 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1550 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1551 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1552 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1553 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1554 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1555 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1556 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1557 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1558 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1559 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1560 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1561 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1562 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1563 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1564 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1565 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1566 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1567 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1568 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1569 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1570 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1571 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1572 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1573 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1574 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1575 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1576 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");