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 */
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) {
101  min_channels = 2;
102  break;
108  max_channels = 6;
109  break;
111  max_channels = 8;
112  break;
116  max_channels = 14;
117  break;
118  }
119  if (avctx->channels < min_channels || avctx->channels > max_channels) {
120  av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
121  return AVERROR(EINVAL);
122  }
123 
124  switch(avctx->codec->id) {
126  c->status[0].step = c->status[1].step = 511;
127  break;
129  if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
130  return AVERROR_INVALIDDATA;
131  break;
133  if (avctx->extradata && avctx->extradata_size >= 8) {
134  c->status[0].predictor = AV_RL32(avctx->extradata);
135  c->status[1].predictor = AV_RL32(avctx->extradata + 4);
136  }
137  break;
139  if (avctx->extradata && avctx->extradata_size >= 2)
140  c->vqa_version = AV_RL16(avctx->extradata);
141  break;
142  default:
143  break;
144  }
145 
146  switch(avctx->codec->id) {
163  break;
165  avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
167  break;
168  default:
169  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
170  }
171 
172  return 0;
173 }
174 
175 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
176 {
177  int step_index;
178  int predictor;
179  int sign, delta, diff, step;
180 
181  step = ff_adpcm_step_table[c->step_index];
182  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
183  step_index = av_clip(step_index, 0, 88);
184 
185  sign = nibble & 8;
186  delta = nibble & 7;
187  /* perform direct multiplication instead of series of jumps proposed by
188  * the reference ADPCM implementation since modern CPUs can do the mults
189  * quickly enough */
190  diff = ((2 * delta + 1) * step) >> shift;
191  predictor = c->predictor;
192  if (sign) predictor -= diff;
193  else predictor += diff;
194 
195  c->predictor = av_clip_int16(predictor);
196  c->step_index = step_index;
197 
198  return (short)c->predictor;
199 }
200 
202 {
203  int nibble, step_index, predictor, sign, delta, diff, step, shift;
204 
205  shift = bps - 1;
206  nibble = get_bits_le(gb, bps),
207  step = ff_adpcm_step_table[c->step_index];
208  step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
209  step_index = av_clip(step_index, 0, 88);
210 
211  sign = nibble & (1 << shift);
212  delta = av_mod_uintp2(nibble, shift);
213  diff = ((2 * delta + 1) * step) >> shift;
214  predictor = c->predictor;
215  if (sign) predictor -= diff;
216  else predictor += diff;
217 
218  c->predictor = av_clip_int16(predictor);
219  c->step_index = step_index;
220 
221  return (int16_t)c->predictor;
222 }
223 
224 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
225 {
226  int step_index;
227  int predictor;
228  int diff, step;
229 
230  step = ff_adpcm_step_table[c->step_index];
231  step_index = c->step_index + ff_adpcm_index_table[nibble];
232  step_index = av_clip(step_index, 0, 88);
233 
234  diff = step >> 3;
235  if (nibble & 4) diff += step;
236  if (nibble & 2) diff += step >> 1;
237  if (nibble & 1) diff += step >> 2;
238 
239  if (nibble & 8)
240  predictor = c->predictor - diff;
241  else
242  predictor = c->predictor + diff;
243 
244  c->predictor = av_clip_int16(predictor);
245  c->step_index = step_index;
246 
247  return c->predictor;
248 }
249 
250 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
251 {
252  int predictor;
253 
254  predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
255  predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
256 
257  c->sample2 = c->sample1;
258  c->sample1 = av_clip_int16(predictor);
259  c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
260  if (c->idelta < 16) c->idelta = 16;
261  if (c->idelta > INT_MAX/768) {
262  av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
263  c->idelta = INT_MAX/768;
264  }
265 
266  return c->sample1;
267 }
268 
269 static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
270 {
271  int step_index, predictor, sign, delta, diff, step;
272 
274  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
275  step_index = av_clip(step_index, 0, 48);
276 
277  sign = nibble & 8;
278  delta = nibble & 7;
279  diff = ((2 * delta + 1) * step) >> 3;
280  predictor = c->predictor;
281  if (sign) predictor -= diff;
282  else predictor += diff;
283 
284  c->predictor = av_clip_intp2(predictor, 11);
285  c->step_index = step_index;
286 
287  return c->predictor << 4;
288 }
289 
290 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
291 {
292  int sign, delta, diff;
293  int new_step;
294 
295  sign = nibble & 8;
296  delta = nibble & 7;
297  /* perform direct multiplication instead of series of jumps proposed by
298  * the reference ADPCM implementation since modern CPUs can do the mults
299  * quickly enough */
300  diff = ((2 * delta + 1) * c->step) >> 3;
301  /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
302  c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
303  c->predictor = av_clip_int16(c->predictor);
304  /* calculate new step and clamp it to range 511..32767 */
305  new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
306  c->step = av_clip(new_step, 511, 32767);
307 
308  return (short)c->predictor;
309 }
310 
311 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
312 {
313  int sign, delta, diff;
314 
315  sign = nibble & (1<<(size-1));
316  delta = nibble & ((1<<(size-1))-1);
317  diff = delta << (7 + c->step + shift);
318 
319  /* clamp result */
320  c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
321 
322  /* calculate new step */
323  if (delta >= (2*size - 3) && c->step < 3)
324  c->step++;
325  else if (delta == 0 && c->step > 0)
326  c->step--;
327 
328  return (short) c->predictor;
329 }
330 
331 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
332 {
333  if(!c->step) {
334  c->predictor = 0;
335  c->step = 127;
336  }
337 
338  c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
339  c->predictor = av_clip_int16(c->predictor);
340  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
341  c->step = av_clip(c->step, 127, 24567);
342  return c->predictor;
343 }
344 
345 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
346  const uint8_t *in, ADPCMChannelStatus *left,
347  ADPCMChannelStatus *right, int channels, int sample_offset)
348 {
349  int i, j;
350  int shift,filter,f0,f1;
351  int s_1,s_2;
352  int d,s,t;
353 
354  out0 += sample_offset;
355  if (channels == 1)
356  out1 = out0 + 28;
357  else
358  out1 += sample_offset;
359 
360  for(i=0;i<4;i++) {
361  shift = 12 - (in[4+i*2] & 15);
362  filter = in[4+i*2] >> 4;
363  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
364  avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
365  filter=0;
366  }
367  f0 = xa_adpcm_table[filter][0];
368  f1 = xa_adpcm_table[filter][1];
369 
370  s_1 = left->sample1;
371  s_2 = left->sample2;
372 
373  for(j=0;j<28;j++) {
374  d = in[16+i+j*4];
375 
376  t = sign_extend(d, 4);
377  s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
378  s_2 = s_1;
379  s_1 = av_clip_int16(s);
380  out0[j] = s_1;
381  }
382 
383  if (channels == 2) {
384  left->sample1 = s_1;
385  left->sample2 = s_2;
386  s_1 = right->sample1;
387  s_2 = right->sample2;
388  }
389 
390  shift = 12 - (in[5+i*2] & 15);
391  filter = in[5+i*2] >> 4;
392  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
393  avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
394  filter=0;
395  }
396 
397  f0 = xa_adpcm_table[filter][0];
398  f1 = xa_adpcm_table[filter][1];
399 
400  for(j=0;j<28;j++) {
401  d = in[16+i+j*4];
402 
403  t = sign_extend(d >> 4, 4);
404  s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
405  s_2 = s_1;
406  s_1 = av_clip_int16(s);
407  out1[j] = s_1;
408  }
409 
410  if (channels == 2) {
411  right->sample1 = s_1;
412  right->sample2 = s_2;
413  } else {
414  left->sample1 = s_1;
415  left->sample2 = s_2;
416  }
417 
418  out0 += 28 * (3 - channels);
419  out1 += 28 * (3 - channels);
420  }
421 
422  return 0;
423 }
424 
425 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
426 {
427  ADPCMDecodeContext *c = avctx->priv_data;
428  GetBitContext gb;
429  const int *table;
430  int k0, signmask, nb_bits, count;
431  int size = buf_size*8;
432  int i;
433 
434  init_get_bits(&gb, buf, size);
435 
436  //read bits & initial values
437  nb_bits = get_bits(&gb, 2)+2;
438  table = swf_index_tables[nb_bits-2];
439  k0 = 1 << (nb_bits-2);
440  signmask = 1 << (nb_bits-1);
441 
442  while (get_bits_count(&gb) <= size - 22*avctx->channels) {
443  for (i = 0; i < avctx->channels; i++) {
444  *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
445  c->status[i].step_index = get_bits(&gb, 6);
446  }
447 
448  for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
449  int i;
450 
451  for (i = 0; i < avctx->channels; i++) {
452  // similar to IMA adpcm
453  int delta = get_bits(&gb, nb_bits);
454  int step = ff_adpcm_step_table[c->status[i].step_index];
455  long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
456  int k = k0;
457 
458  do {
459  if (delta & k)
460  vpdiff += step;
461  step >>= 1;
462  k >>= 1;
463  } while(k);
464  vpdiff += step;
465 
466  if (delta & signmask)
467  c->status[i].predictor -= vpdiff;
468  else
469  c->status[i].predictor += vpdiff;
470 
471  c->status[i].step_index += table[delta & (~signmask)];
472 
473  c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
474  c->status[i].predictor = av_clip_int16(c->status[i].predictor);
475 
476  *samples++ = c->status[i].predictor;
477  }
478  }
479  }
480 }
481 
482 /**
483  * Get the number of samples that will be decoded from the packet.
484  * In one case, this is actually the maximum number of samples possible to
485  * decode with the given buf_size.
486  *
487  * @param[out] coded_samples set to the number of samples as coded in the
488  * packet, or 0 if the codec does not encode the
489  * number of samples in each frame.
490  * @param[out] approx_nb_samples set to non-zero if the number of samples
491  * returned is an approximation.
492  */
494  int buf_size, int *coded_samples, int *approx_nb_samples)
495 {
496  ADPCMDecodeContext *s = avctx->priv_data;
497  int nb_samples = 0;
498  int ch = avctx->channels;
499  int has_coded_samples = 0;
500  int header_size;
501 
502  *coded_samples = 0;
503  *approx_nb_samples = 0;
504 
505  if(ch <= 0)
506  return 0;
507 
508  switch (avctx->codec->id) {
509  /* constant, only check buf_size */
511  if (buf_size < 76 * ch)
512  return 0;
513  nb_samples = 128;
514  break;
516  if (buf_size < 34 * ch)
517  return 0;
518  nb_samples = 64;
519  break;
520  /* simple 4-bit adpcm */
528  nb_samples = buf_size * 2 / ch;
529  break;
530  }
531  if (nb_samples)
532  return nb_samples;
533 
534  /* simple 4-bit adpcm, with header */
535  header_size = 0;
536  switch (avctx->codec->id) {
539  case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
540  case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
541  case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
542  }
543  if (header_size > 0)
544  return (buf_size - header_size) * 2 / ch;
545 
546  /* more complex formats */
547  switch (avctx->codec->id) {
549  has_coded_samples = 1;
550  *coded_samples = bytestream2_get_le32(gb);
551  *coded_samples -= *coded_samples % 28;
552  nb_samples = (buf_size - 12) / 30 * 28;
553  break;
555  has_coded_samples = 1;
556  *coded_samples = bytestream2_get_le32(gb);
557  nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
558  break;
560  nb_samples = (buf_size - ch) / ch * 2;
561  break;
565  /* maximum number of samples */
566  /* has internal offsets and a per-frame switch to signal raw 16-bit */
567  has_coded_samples = 1;
568  switch (avctx->codec->id) {
570  header_size = 4 + 9 * ch;
571  *coded_samples = bytestream2_get_le32(gb);
572  break;
574  header_size = 4 + 5 * ch;
575  *coded_samples = bytestream2_get_le32(gb);
576  break;
578  header_size = 4 + 5 * ch;
579  *coded_samples = bytestream2_get_be32(gb);
580  break;
581  }
582  *coded_samples -= *coded_samples % 28;
583  nb_samples = (buf_size - header_size) * 2 / ch;
584  nb_samples -= nb_samples % 28;
585  *approx_nb_samples = 1;
586  break;
588  if (avctx->block_align > 0)
589  buf_size = FFMIN(buf_size, avctx->block_align);
590  nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
591  break;
593  if (avctx->block_align > 0)
594  buf_size = FFMIN(buf_size, avctx->block_align);
595  if (buf_size < 4 * ch)
596  return AVERROR_INVALIDDATA;
597  nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
598  break;
600  if (avctx->block_align > 0)
601  buf_size = FFMIN(buf_size, avctx->block_align);
602  nb_samples = (buf_size - 4 * ch) * 2 / ch;
603  break;
605  {
606  int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
607  int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
608  if (avctx->block_align > 0)
609  buf_size = FFMIN(buf_size, avctx->block_align);
610  if (buf_size < 4 * ch)
611  return AVERROR_INVALIDDATA;
612  nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
613  break;
614  }
616  if (avctx->block_align > 0)
617  buf_size = FFMIN(buf_size, avctx->block_align);
618  nb_samples = (buf_size - 6 * ch) * 2 / ch;
619  break;
623  {
624  int samples_per_byte;
625  switch (avctx->codec->id) {
626  case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
627  case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
628  case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
629  }
630  if (!s->status[0].step_index) {
631  if (buf_size < ch)
632  return AVERROR_INVALIDDATA;
633  nb_samples++;
634  buf_size -= ch;
635  }
636  nb_samples += buf_size * samples_per_byte / ch;
637  break;
638  }
640  {
641  int buf_bits = buf_size * 8 - 2;
642  int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
643  int block_hdr_size = 22 * ch;
644  int block_size = block_hdr_size + nbits * ch * 4095;
645  int nblocks = buf_bits / block_size;
646  int bits_left = buf_bits - nblocks * block_size;
647  nb_samples = nblocks * 4096;
648  if (bits_left >= block_hdr_size)
649  nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
650  break;
651  }
654  if (avctx->extradata) {
655  nb_samples = buf_size * 14 / (8 * ch);
656  break;
657  }
658  has_coded_samples = 1;
659  bytestream2_skip(gb, 4); // channel size
660  *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
661  bytestream2_get_le32(gb) :
662  bytestream2_get_be32(gb);
663  buf_size -= 8 + 36 * ch;
664  buf_size /= ch;
665  nb_samples = buf_size / 8 * 14;
666  if (buf_size % 8 > 1)
667  nb_samples += (buf_size % 8 - 1) * 2;
668  *approx_nb_samples = 1;
669  break;
671  nb_samples = buf_size / (9 * ch) * 16;
672  break;
674  nb_samples = (buf_size / 128) * 224 / ch;
675  break;
678  nb_samples = buf_size / (16 * ch) * 28;
679  break;
680  }
681 
682  /* validate coded sample count */
683  if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
684  return AVERROR_INVALIDDATA;
685 
686  return nb_samples;
687 }
688 
689 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
690  int *got_frame_ptr, AVPacket *avpkt)
691 {
692  AVFrame *frame = data;
693  const uint8_t *buf = avpkt->data;
694  int buf_size = avpkt->size;
695  ADPCMDecodeContext *c = avctx->priv_data;
696  ADPCMChannelStatus *cs;
697  int n, m, channel, i;
698  short *samples;
699  int16_t **samples_p;
700  int st; /* stereo */
701  int count1, count2;
702  int nb_samples, coded_samples, approx_nb_samples, ret;
703  GetByteContext gb;
704 
705  bytestream2_init(&gb, buf, buf_size);
706  nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
707  if (nb_samples <= 0) {
708  av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
709  return AVERROR_INVALIDDATA;
710  }
711 
712  /* get output buffer */
713  frame->nb_samples = nb_samples;
714  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
715  return ret;
716  samples = (short *)frame->data[0];
717  samples_p = (int16_t **)frame->extended_data;
718 
719  /* use coded_samples when applicable */
720  /* it is always <= nb_samples, so the output buffer will be large enough */
721  if (coded_samples) {
722  if (!approx_nb_samples && coded_samples != nb_samples)
723  av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
724  frame->nb_samples = nb_samples = coded_samples;
725  }
726 
727  st = avctx->channels == 2 ? 1 : 0;
728 
729  switch(avctx->codec->id) {
731  /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
732  Channel data is interleaved per-chunk. */
733  for (channel = 0; channel < avctx->channels; channel++) {
734  int predictor;
735  int step_index;
736  cs = &(c->status[channel]);
737  /* (pppppp) (piiiiiii) */
738 
739  /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
740  predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
741  step_index = predictor & 0x7F;
742  predictor &= ~0x7F;
743 
744  if (cs->step_index == step_index) {
745  int diff = predictor - cs->predictor;
746  if (diff < 0)
747  diff = - diff;
748  if (diff > 0x7f)
749  goto update;
750  } else {
751  update:
752  cs->step_index = step_index;
753  cs->predictor = predictor;
754  }
755 
756  if (cs->step_index > 88u){
757  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
758  channel, cs->step_index);
759  return AVERROR_INVALIDDATA;
760  }
761 
762  samples = samples_p[channel];
763 
764  for (m = 0; m < 64; m += 2) {
765  int byte = bytestream2_get_byteu(&gb);
766  samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
767  samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
768  }
769  }
770  break;
772  for(i=0; i<avctx->channels; i++){
773  cs = &(c->status[i]);
774  cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
775 
776  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
777  if (cs->step_index > 88u){
778  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
779  i, cs->step_index);
780  return AVERROR_INVALIDDATA;
781  }
782  }
783 
784  if (avctx->bits_per_coded_sample != 4) {
785  int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
787 
789  if (ret < 0)
790  return ret;
791  for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
792  for (i = 0; i < avctx->channels; i++) {
793  cs = &c->status[i];
794  samples = &samples_p[i][1 + n * samples_per_block];
795  for (m = 0; m < samples_per_block; m++) {
796  samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
797  avctx->bits_per_coded_sample);
798  }
799  }
800  }
801  bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
802  } else {
803  for (n = 0; n < (nb_samples - 1) / 8; n++) {
804  for (i = 0; i < avctx->channels; i++) {
805  cs = &c->status[i];
806  samples = &samples_p[i][1 + n * 8];
807  for (m = 0; m < 8; m += 2) {
808  int v = bytestream2_get_byteu(&gb);
809  samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
810  samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
811  }
812  }
813  }
814  }
815  break;
817  for (i = 0; i < avctx->channels; i++)
818  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
819 
820  for (i = 0; i < avctx->channels; i++) {
821  c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
822  if (c->status[i].step_index > 88u) {
823  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
824  i, c->status[i].step_index);
825  return AVERROR_INVALIDDATA;
826  }
827  }
828 
829  for (i = 0; i < avctx->channels; i++) {
830  samples = (int16_t *)frame->data[i];
831  cs = &c->status[i];
832  for (n = nb_samples >> 1; n > 0; n--) {
833  int v = bytestream2_get_byteu(&gb);
834  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
835  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
836  }
837  }
838  break;
840  {
841  int block_predictor;
842 
843  block_predictor = bytestream2_get_byteu(&gb);
844  if (block_predictor > 6) {
845  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
846  block_predictor);
847  return AVERROR_INVALIDDATA;
848  }
849  c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
850  c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
851  if (st) {
852  block_predictor = bytestream2_get_byteu(&gb);
853  if (block_predictor > 6) {
854  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
855  block_predictor);
856  return AVERROR_INVALIDDATA;
857  }
858  c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
859  c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
860  }
861  c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
862  if (st){
863  c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
864  }
865 
866  c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
867  if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
868  c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
869  if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
870 
871  *samples++ = c->status[0].sample2;
872  if (st) *samples++ = c->status[1].sample2;
873  *samples++ = c->status[0].sample1;
874  if (st) *samples++ = c->status[1].sample1;
875  for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
876  int byte = bytestream2_get_byteu(&gb);
877  *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
878  *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
879  }
880  break;
881  }
883  for (channel = 0; channel < avctx->channels; channel++) {
884  cs = &c->status[channel];
885  cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
886  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
887  if (cs->step_index > 88u){
888  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
889  channel, cs->step_index);
890  return AVERROR_INVALIDDATA;
891  }
892  }
893  for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
894  int v = bytestream2_get_byteu(&gb);
895  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
896  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
897  }
898  break;
900  {
901  int last_byte = 0;
902  int nibble;
903  int decode_top_nibble_next = 0;
904  int diff_channel;
905  const int16_t *samples_end = samples + avctx->channels * nb_samples;
906 
907  bytestream2_skipu(&gb, 10);
908  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
909  c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
910  c->status[0].step_index = bytestream2_get_byteu(&gb);
911  c->status[1].step_index = bytestream2_get_byteu(&gb);
912  if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
913  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
914  c->status[0].step_index, c->status[1].step_index);
915  return AVERROR_INVALIDDATA;
916  }
917  /* sign extend the predictors */
918  diff_channel = c->status[1].predictor;
919 
920  /* DK3 ADPCM support macro */
921 #define DK3_GET_NEXT_NIBBLE() \
922  if (decode_top_nibble_next) { \
923  nibble = last_byte >> 4; \
924  decode_top_nibble_next = 0; \
925  } else { \
926  last_byte = bytestream2_get_byteu(&gb); \
927  nibble = last_byte & 0x0F; \
928  decode_top_nibble_next = 1; \
929  }
930 
931  while (samples < samples_end) {
932 
933  /* for this algorithm, c->status[0] is the sum channel and
934  * c->status[1] is the diff channel */
935 
936  /* process the first predictor of the sum channel */
938  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
939 
940  /* process the diff channel predictor */
942  adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
943 
944  /* process the first pair of stereo PCM samples */
945  diff_channel = (diff_channel + c->status[1].predictor) / 2;
946  *samples++ = c->status[0].predictor + c->status[1].predictor;
947  *samples++ = c->status[0].predictor - c->status[1].predictor;
948 
949  /* process the second predictor of the sum channel */
951  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
952 
953  /* process the second pair of stereo PCM samples */
954  diff_channel = (diff_channel + c->status[1].predictor) / 2;
955  *samples++ = c->status[0].predictor + c->status[1].predictor;
956  *samples++ = c->status[0].predictor - c->status[1].predictor;
957  }
958 
959  if ((bytestream2_tell(&gb) & 1))
960  bytestream2_skip(&gb, 1);
961  break;
962  }
964  for (channel = 0; channel < avctx->channels; channel++) {
965  cs = &c->status[channel];
966  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
967  cs->step_index = 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 
975  for (n = nb_samples >> (1 - st); n > 0; n--) {
976  int v1, v2;
977  int v = bytestream2_get_byteu(&gb);
978  /* nibbles are swapped for mono */
979  if (st) {
980  v1 = v >> 4;
981  v2 = v & 0x0F;
982  } else {
983  v2 = v >> 4;
984  v1 = v & 0x0F;
985  }
986  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
987  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
988  }
989  break;
991  for (channel = 0; channel < avctx->channels; channel++) {
992  cs = &c->status[channel];
993  samples = samples_p[channel];
994  bytestream2_skip(&gb, 4);
995  for (n = 0; n < nb_samples; n += 2) {
996  int v = bytestream2_get_byteu(&gb);
997  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
998  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
999  }
1000  }
1001  break;
1003  while (bytestream2_get_bytes_left(&gb) > 0) {
1004  int v = bytestream2_get_byteu(&gb);
1005  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1006  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1007  }
1008  break;
1010  while (bytestream2_get_bytes_left(&gb) > 0) {
1011  int v = bytestream2_get_byteu(&gb);
1012  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1013  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1014  }
1015  break;
1017  for (channel = 0; channel < avctx->channels; channel++) {
1018  cs = &c->status[channel];
1019  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1020  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1021  if (cs->step_index > 88u){
1022  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1023  channel, cs->step_index);
1024  return AVERROR_INVALIDDATA;
1025  }
1026  }
1027  for (n = 0; n < nb_samples / 2; n++) {
1028  int byte[2];
1029 
1030  byte[0] = bytestream2_get_byteu(&gb);
1031  if (st)
1032  byte[1] = bytestream2_get_byteu(&gb);
1033  for(channel = 0; channel < avctx->channels; channel++) {
1034  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1035  }
1036  for(channel = 0; channel < avctx->channels; channel++) {
1037  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1038  }
1039  }
1040  break;
1042  if (c->vqa_version == 3) {
1043  for (channel = 0; channel < avctx->channels; channel++) {
1044  int16_t *smp = samples_p[channel];
1045 
1046  for (n = nb_samples / 2; n > 0; n--) {
1047  int v = bytestream2_get_byteu(&gb);
1048  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1049  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1050  }
1051  }
1052  } else {
1053  for (n = nb_samples / 2; n > 0; n--) {
1054  for (channel = 0; channel < avctx->channels; channel++) {
1055  int v = bytestream2_get_byteu(&gb);
1056  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1057  samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1058  }
1059  samples += avctx->channels;
1060  }
1061  }
1062  bytestream2_seek(&gb, 0, SEEK_END);
1063  break;
1064  case AV_CODEC_ID_ADPCM_XA:
1065  {
1066  int16_t *out0 = samples_p[0];
1067  int16_t *out1 = samples_p[1];
1068  int samples_per_block = 28 * (3 - avctx->channels) * 4;
1069  int sample_offset = 0;
1070  while (bytestream2_get_bytes_left(&gb) >= 128) {
1071  if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1072  &c->status[0], &c->status[1],
1073  avctx->channels, sample_offset)) < 0)
1074  return ret;
1075  bytestream2_skipu(&gb, 128);
1076  sample_offset += samples_per_block;
1077  }
1078  break;
1079  }
1081  for (i=0; i<=st; i++) {
1082  c->status[i].step_index = bytestream2_get_le32u(&gb);
1083  if (c->status[i].step_index > 88u) {
1084  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1085  i, c->status[i].step_index);
1086  return AVERROR_INVALIDDATA;
1087  }
1088  }
1089  for (i=0; i<=st; i++)
1090  c->status[i].predictor = bytestream2_get_le32u(&gb);
1091 
1092  for (n = nb_samples >> (1 - st); n > 0; n--) {
1093  int byte = bytestream2_get_byteu(&gb);
1094  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1095  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1096  }
1097  break;
1099  for (n = nb_samples >> (1 - st); n > 0; n--) {
1100  int byte = bytestream2_get_byteu(&gb);
1101  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1102  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1103  }
1104  break;
1105  case AV_CODEC_ID_ADPCM_EA:
1106  {
1107  int previous_left_sample, previous_right_sample;
1108  int current_left_sample, current_right_sample;
1109  int next_left_sample, next_right_sample;
1110  int coeff1l, coeff2l, coeff1r, coeff2r;
1111  int shift_left, shift_right;
1112 
1113  /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1114  each coding 28 stereo samples. */
1115 
1116  if(avctx->channels != 2)
1117  return AVERROR_INVALIDDATA;
1118 
1119  current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1120  previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1121  current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1122  previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1123 
1124  for (count1 = 0; count1 < nb_samples / 28; count1++) {
1125  int byte = bytestream2_get_byteu(&gb);
1126  coeff1l = ea_adpcm_table[ byte >> 4 ];
1127  coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1128  coeff1r = ea_adpcm_table[ byte & 0x0F];
1129  coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1130 
1131  byte = bytestream2_get_byteu(&gb);
1132  shift_left = 20 - (byte >> 4);
1133  shift_right = 20 - (byte & 0x0F);
1134 
1135  for (count2 = 0; count2 < 28; count2++) {
1136  byte = bytestream2_get_byteu(&gb);
1137  next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1138  next_right_sample = sign_extend(byte, 4) << shift_right;
1139 
1140  next_left_sample = (next_left_sample +
1141  (current_left_sample * coeff1l) +
1142  (previous_left_sample * coeff2l) + 0x80) >> 8;
1143  next_right_sample = (next_right_sample +
1144  (current_right_sample * coeff1r) +
1145  (previous_right_sample * coeff2r) + 0x80) >> 8;
1146 
1147  previous_left_sample = current_left_sample;
1148  current_left_sample = av_clip_int16(next_left_sample);
1149  previous_right_sample = current_right_sample;
1150  current_right_sample = av_clip_int16(next_right_sample);
1151  *samples++ = current_left_sample;
1152  *samples++ = current_right_sample;
1153  }
1154  }
1155 
1156  bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1157 
1158  break;
1159  }
1161  {
1162  int coeff[2][2], shift[2];
1163 
1164  for(channel = 0; channel < avctx->channels; channel++) {
1165  int byte = bytestream2_get_byteu(&gb);
1166  for (i=0; i<2; i++)
1167  coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1168  shift[channel] = 20 - (byte & 0x0F);
1169  }
1170  for (count1 = 0; count1 < nb_samples / 2; count1++) {
1171  int byte[2];
1172 
1173  byte[0] = bytestream2_get_byteu(&gb);
1174  if (st) byte[1] = bytestream2_get_byteu(&gb);
1175  for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1176  for(channel = 0; channel < avctx->channels; channel++) {
1177  int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1178  sample = (sample +
1179  c->status[channel].sample1 * coeff[channel][0] +
1180  c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1181  c->status[channel].sample2 = c->status[channel].sample1;
1182  c->status[channel].sample1 = av_clip_int16(sample);
1183  *samples++ = c->status[channel].sample1;
1184  }
1185  }
1186  }
1187  bytestream2_seek(&gb, 0, SEEK_END);
1188  break;
1189  }
1192  case AV_CODEC_ID_ADPCM_EA_R3: {
1193  /* channel numbering
1194  2chan: 0=fl, 1=fr
1195  4chan: 0=fl, 1=rl, 2=fr, 3=rr
1196  6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1197  const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1198  int previous_sample, current_sample, next_sample;
1199  int coeff1, coeff2;
1200  int shift;
1201  unsigned int channel;
1202  uint16_t *samplesC;
1203  int count = 0;
1204  int offsets[6];
1205 
1206  for (channel=0; channel<avctx->channels; channel++)
1207  offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1208  bytestream2_get_le32(&gb)) +
1209  (avctx->channels + 1) * 4;
1210 
1211  for (channel=0; channel<avctx->channels; channel++) {
1212  bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1213  samplesC = samples_p[channel];
1214 
1215  if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1216  current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1217  previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1218  } else {
1219  current_sample = c->status[channel].predictor;
1220  previous_sample = c->status[channel].prev_sample;
1221  }
1222 
1223  for (count1 = 0; count1 < nb_samples / 28; count1++) {
1224  int byte = bytestream2_get_byte(&gb);
1225  if (byte == 0xEE) { /* only seen in R2 and R3 */
1226  current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1227  previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1228 
1229  for (count2=0; count2<28; count2++)
1230  *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1231  } else {
1232  coeff1 = ea_adpcm_table[ byte >> 4 ];
1233  coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1234  shift = 20 - (byte & 0x0F);
1235 
1236  for (count2=0; count2<28; count2++) {
1237  if (count2 & 1)
1238  next_sample = sign_extend(byte, 4) << shift;
1239  else {
1240  byte = bytestream2_get_byte(&gb);
1241  next_sample = sign_extend(byte >> 4, 4) << shift;
1242  }
1243 
1244  next_sample += (current_sample * coeff1) +
1245  (previous_sample * coeff2);
1246  next_sample = av_clip_int16(next_sample >> 8);
1247 
1248  previous_sample = current_sample;
1249  current_sample = next_sample;
1250  *samplesC++ = current_sample;
1251  }
1252  }
1253  }
1254  if (!count) {
1255  count = count1;
1256  } else if (count != count1) {
1257  av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1258  count = FFMAX(count, count1);
1259  }
1260 
1261  if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1262  c->status[channel].predictor = current_sample;
1263  c->status[channel].prev_sample = previous_sample;
1264  }
1265  }
1266 
1267  frame->nb_samples = count * 28;
1268  bytestream2_seek(&gb, 0, SEEK_END);
1269  break;
1270  }
1272  for (channel=0; channel<avctx->channels; channel++) {
1273  int coeff[2][4], shift[4];
1274  int16_t *s = samples_p[channel];
1275  for (n = 0; n < 4; n++, s += 32) {
1276  int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1277  for (i=0; i<2; i++)
1278  coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1279  s[0] = val & ~0x0F;
1280 
1281  val = sign_extend(bytestream2_get_le16u(&gb), 16);
1282  shift[n] = 20 - (val & 0x0F);
1283  s[1] = val & ~0x0F;
1284  }
1285 
1286  for (m=2; m<32; m+=2) {
1287  s = &samples_p[channel][m];
1288  for (n = 0; n < 4; n++, s += 32) {
1289  int level, pred;
1290  int byte = bytestream2_get_byteu(&gb);
1291 
1292  level = sign_extend(byte >> 4, 4) << shift[n];
1293  pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1294  s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1295 
1296  level = sign_extend(byte, 4) << shift[n];
1297  pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1298  s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1299  }
1300  }
1301  }
1302  break;
1304  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1305  c->status[0].step_index = bytestream2_get_le16u(&gb);
1306  bytestream2_skipu(&gb, 4);
1307  if (c->status[0].step_index > 88u) {
1308  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1309  c->status[0].step_index);
1310  return AVERROR_INVALIDDATA;
1311  }
1312 
1313  for (n = nb_samples >> (1 - st); n > 0; n--) {
1314  int v = bytestream2_get_byteu(&gb);
1315 
1316  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1317  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1318  }
1319  break;
1321  for (i = 0; i < avctx->channels; i++) {
1322  c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1323  c->status[i].step_index = bytestream2_get_byteu(&gb);
1324  bytestream2_skipu(&gb, 1);
1325  if (c->status[i].step_index > 88u) {
1326  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1327  c->status[i].step_index);
1328  return AVERROR_INVALIDDATA;
1329  }
1330  }
1331 
1332  for (n = nb_samples >> (1 - st); n > 0; n--) {
1333  int v = bytestream2_get_byteu(&gb);
1334 
1335  *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1336  *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1337  }
1338  break;
1339  case AV_CODEC_ID_ADPCM_CT:
1340  for (n = nb_samples >> (1 - st); n > 0; n--) {
1341  int v = bytestream2_get_byteu(&gb);
1342  *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1343  *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1344  }
1345  break;
1349  if (!c->status[0].step_index) {
1350  /* the first byte is a raw sample */
1351  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1352  if (st)
1353  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1354  c->status[0].step_index = 1;
1355  nb_samples--;
1356  }
1357  if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1358  for (n = nb_samples >> (1 - st); n > 0; n--) {
1359  int byte = bytestream2_get_byteu(&gb);
1360  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1361  byte >> 4, 4, 0);
1362  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1363  byte & 0x0F, 4, 0);
1364  }
1365  } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1366  for (n = (nb_samples<<st) / 3; n > 0; n--) {
1367  int byte = bytestream2_get_byteu(&gb);
1368  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1369  byte >> 5 , 3, 0);
1370  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1371  (byte >> 2) & 0x07, 3, 0);
1372  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1373  byte & 0x03, 2, 0);
1374  }
1375  } else {
1376  for (n = nb_samples >> (2 - st); n > 0; n--) {
1377  int byte = bytestream2_get_byteu(&gb);
1378  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1379  byte >> 6 , 2, 2);
1380  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1381  (byte >> 4) & 0x03, 2, 2);
1382  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1383  (byte >> 2) & 0x03, 2, 2);
1384  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1385  byte & 0x03, 2, 2);
1386  }
1387  }
1388  break;
1389  case AV_CODEC_ID_ADPCM_SWF:
1390  adpcm_swf_decode(avctx, buf, buf_size, samples);
1391  bytestream2_seek(&gb, 0, SEEK_END);
1392  break;
1394  for (n = nb_samples >> (1 - st); n > 0; n--) {
1395  int v = bytestream2_get_byteu(&gb);
1396  *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1397  *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1398  }
1399  break;
1401  if (!c->has_status) {
1402  for (channel = 0; channel < avctx->channels; channel++)
1403  c->status[channel].step = 0;
1404  c->has_status = 1;
1405  }
1406  for (channel = 0; channel < avctx->channels; channel++) {
1407  samples = samples_p[channel];
1408  for (n = nb_samples >> 1; n > 0; n--) {
1409  int v = bytestream2_get_byteu(&gb);
1410  *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1411  *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
1412  }
1413  }
1414  break;
1415  case AV_CODEC_ID_ADPCM_AFC:
1416  {
1417  int samples_per_block;
1418  int blocks;
1419 
1420  if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1421  samples_per_block = avctx->extradata[0] / 16;
1422  blocks = nb_samples / avctx->extradata[0];
1423  } else {
1424  samples_per_block = nb_samples / 16;
1425  blocks = 1;
1426  }
1427 
1428  for (m = 0; m < blocks; m++) {
1429  for (channel = 0; channel < avctx->channels; channel++) {
1430  int prev1 = c->status[channel].sample1;
1431  int prev2 = c->status[channel].sample2;
1432 
1433  samples = samples_p[channel] + m * 16;
1434  /* Read in every sample for this channel. */
1435  for (i = 0; i < samples_per_block; i++) {
1436  int byte = bytestream2_get_byteu(&gb);
1437  int scale = 1 << (byte >> 4);
1438  int index = byte & 0xf;
1439  int factor1 = ff_adpcm_afc_coeffs[0][index];
1440  int factor2 = ff_adpcm_afc_coeffs[1][index];
1441 
1442  /* Decode 16 samples. */
1443  for (n = 0; n < 16; n++) {
1444  int32_t sampledat;
1445 
1446  if (n & 1) {
1447  sampledat = sign_extend(byte, 4);
1448  } else {
1449  byte = bytestream2_get_byteu(&gb);
1450  sampledat = sign_extend(byte >> 4, 4);
1451  }
1452 
1453  sampledat = ((prev1 * factor1 + prev2 * factor2) +
1454  ((sampledat * scale) << 11)) >> 11;
1455  *samples = av_clip_int16(sampledat);
1456  prev2 = prev1;
1457  prev1 = *samples++;
1458  }
1459  }
1460 
1461  c->status[channel].sample1 = prev1;
1462  c->status[channel].sample2 = prev2;
1463  }
1464  }
1465  bytestream2_seek(&gb, 0, SEEK_END);
1466  break;
1467  }
1468  case AV_CODEC_ID_ADPCM_THP:
1470  {
1471  int table[14][16];
1472  int ch;
1473 
1474 #define THP_GET16(g) \
1475  sign_extend( \
1476  avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1477  bytestream2_get_le16u(&(g)) : \
1478  bytestream2_get_be16u(&(g)), 16)
1479 
1480  if (avctx->extradata) {
1482  if (avctx->extradata_size < 32 * avctx->channels) {
1483  av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1484  return AVERROR_INVALIDDATA;
1485  }
1486 
1487  bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1488  for (i = 0; i < avctx->channels; i++)
1489  for (n = 0; n < 16; n++)
1490  table[i][n] = THP_GET16(tb);
1491  } else {
1492  for (i = 0; i < avctx->channels; i++)
1493  for (n = 0; n < 16; n++)
1494  table[i][n] = THP_GET16(gb);
1495 
1496  if (!c->has_status) {
1497  /* Initialize the previous sample. */
1498  for (i = 0; i < avctx->channels; i++) {
1499  c->status[i].sample1 = THP_GET16(gb);
1500  c->status[i].sample2 = THP_GET16(gb);
1501  }
1502  c->has_status = 1;
1503  } else {
1504  bytestream2_skip(&gb, avctx->channels * 4);
1505  }
1506  }
1507 
1508  for (ch = 0; ch < avctx->channels; ch++) {
1509  samples = samples_p[ch];
1510 
1511  /* Read in every sample for this channel. */
1512  for (i = 0; i < (nb_samples + 13) / 14; i++) {
1513  int byte = bytestream2_get_byteu(&gb);
1514  int index = (byte >> 4) & 7;
1515  unsigned int exp = byte & 0x0F;
1516  int factor1 = table[ch][index * 2];
1517  int factor2 = table[ch][index * 2 + 1];
1518 
1519  /* Decode 14 samples. */
1520  for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1521  int32_t sampledat;
1522 
1523  if (n & 1) {
1524  sampledat = sign_extend(byte, 4);
1525  } else {
1526  byte = bytestream2_get_byteu(&gb);
1527  sampledat = sign_extend(byte >> 4, 4);
1528  }
1529 
1530  sampledat = ((c->status[ch].sample1 * factor1
1531  + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1532  *samples = av_clip_int16(sampledat);
1533  c->status[ch].sample2 = c->status[ch].sample1;
1534  c->status[ch].sample1 = *samples++;
1535  }
1536  }
1537  }
1538  break;
1539  }
1540  case AV_CODEC_ID_ADPCM_DTK:
1541  for (channel = 0; channel < avctx->channels; channel++) {
1542  samples = samples_p[channel];
1543 
1544  /* Read in every sample for this channel. */
1545  for (i = 0; i < nb_samples / 28; i++) {
1546  int byte, header;
1547  if (channel)
1548  bytestream2_skipu(&gb, 1);
1549  header = bytestream2_get_byteu(&gb);
1550  bytestream2_skipu(&gb, 3 - channel);
1551 
1552  /* Decode 28 samples. */
1553  for (n = 0; n < 28; n++) {
1554  int32_t sampledat, prev;
1555 
1556  switch (header >> 4) {
1557  case 1:
1558  prev = (c->status[channel].sample1 * 0x3c);
1559  break;
1560  case 2:
1561  prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1562  break;
1563  case 3:
1564  prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1565  break;
1566  default:
1567  prev = 0;
1568  }
1569 
1570  prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1571 
1572  byte = bytestream2_get_byteu(&gb);
1573  if (!channel)
1574  sampledat = sign_extend(byte, 4);
1575  else
1576  sampledat = sign_extend(byte >> 4, 4);
1577 
1578  sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1579  *samples++ = av_clip_int16(sampledat >> 6);
1580  c->status[channel].sample2 = c->status[channel].sample1;
1581  c->status[channel].sample1 = sampledat;
1582  }
1583  }
1584  if (!channel)
1585  bytestream2_seek(&gb, 0, SEEK_SET);
1586  }
1587  break;
1588  case AV_CODEC_ID_ADPCM_PSX:
1589  for (channel = 0; channel < avctx->channels; channel++) {
1590  samples = samples_p[channel];
1591 
1592  /* Read in every sample for this channel. */
1593  for (i = 0; i < nb_samples / 28; i++) {
1594  int filter, shift, flag, byte;
1595 
1596  filter = bytestream2_get_byteu(&gb);
1597  shift = filter & 0xf;
1598  filter = filter >> 4;
1599  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1600  return AVERROR_INVALIDDATA;
1601  flag = bytestream2_get_byteu(&gb);
1602 
1603  /* Decode 28 samples. */
1604  for (n = 0; n < 28; n++) {
1605  int sample = 0, scale;
1606 
1607  if (flag < 0x07) {
1608  if (n & 1) {
1609  scale = sign_extend(byte >> 4, 4);
1610  } else {
1611  byte = bytestream2_get_byteu(&gb);
1612  scale = sign_extend(byte, 4);
1613  }
1614 
1615  scale = scale << 12;
1616  sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1617  }
1618  *samples++ = av_clip_int16(sample);
1619  c->status[channel].sample2 = c->status[channel].sample1;
1620  c->status[channel].sample1 = sample;
1621  }
1622  }
1623  }
1624  break;
1625 
1626  default:
1627  return -1;
1628  }
1629 
1630  if (avpkt->size && bytestream2_tell(&gb) == 0) {
1631  av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1632  return AVERROR_INVALIDDATA;
1633  }
1634 
1635  *got_frame_ptr = 1;
1636 
1637  if (avpkt->size < bytestream2_tell(&gb)) {
1638  av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1639  return avpkt->size;
1640  }
1641 
1642  return bytestream2_tell(&gb);
1643 }
1644 
1645 static void adpcm_flush(AVCodecContext *avctx)
1646 {
1647  ADPCMDecodeContext *c = avctx->priv_data;
1648  c->has_status = 0;
1649 }
1650 
1651 
1659 
1660 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1661 AVCodec ff_ ## name_ ## _decoder = { \
1662  .name = #name_, \
1663  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1664  .type = AVMEDIA_TYPE_AUDIO, \
1665  .id = id_, \
1666  .priv_data_size = sizeof(ADPCMDecodeContext), \
1667  .init = adpcm_decode_init, \
1668  .decode = adpcm_decode_frame, \
1669  .flush = adpcm_flush, \
1670  .capabilities = AV_CODEC_CAP_DR1, \
1671  .sample_fmts = sample_fmts_, \
1672 }
1673 
1674 /* Note: Do not forget to add new entries to the Makefile as well. */
1675 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1676 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1677 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
1678 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1679 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1680 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1681 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1682 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1683 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1684 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1685 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1686 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1687 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1688 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
1689 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1690 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1691 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1692 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1693 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1694 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1695 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1696 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1697 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1698 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1699 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1700 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
1701 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
1702 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1703 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1704 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1705 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1706 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
1707 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
1708 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1709 ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1648
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
Definition: adpcm.c:311
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:180
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define THP_GET16(g)
const int16_t ff_adpcm_afc_coeffs[2][16]
Definition: adpcm_data.c:109
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
const char * g
Definition: vf_curves.c:108
static short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
Definition: adpcm.c:290
int size
Definition: avcodec.h:1575
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
static enum AVSampleFormat sample_fmts_s16[]
Definition: adpcm.c:1652
#define sample
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2437
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:245
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:90
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2408
uint8_t
#define av_cold
Definition: attributes.h:82
static av_cold int adpcm_decode_init(AVCodecContext *avctx)
Definition: adpcm.c:92
float delta
static void adpcm_flush(AVCodecContext *avctx)
Definition: adpcm.c:1645
static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
Definition: adpcm.c:425
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1754
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
Definition: cfhd.c:80
static const int xa_adpcm_table[5][2]
Definition: adpcm.c:60
ADPCM tables.
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1574
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
const uint8_t * buffer
Definition: bytestream.h:34
static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb, int buf_size, int *coded_samples, int *approx_nb_samples)
Get the number of samples that will be decoded from the packet.
Definition: adpcm.c:493
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:170
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
static const uint8_t header[24]
Definition: sdr2.c:67
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:3032
#define av_log(a,...)
unsigned m
Definition: audioconvert.c:187
int flag
Definition: checkasm.c:120
static void predictor(uint8_t *src, int size)
Definition: exr.c:251
enum AVCodecID id
Definition: avcodec.h:3545
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
ADPCM encoder/decoder common header.
static short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
Definition: adpcm.c:331
static const int ea_adpcm_table[]
Definition: adpcm.c:68
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
const int8_t *const ff_adpcm_index_tables[4]
Definition: adpcm_data.c:50
static const struct endianess table[]
const int16_t ff_adpcm_step_table[89]
This is the step table.
Definition: adpcm_data.c:61
static int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
Definition: adpcm.c:224
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
static int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
Definition: adpcm.c:201
int8_t exp
Definition: eval.c:64
const int8_t ff_adpcm_index_table[16]
Definition: adpcm_data.c:40
static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples, int is_silence, int64_t nb_samples_notify, AVRational time_base)
static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1, const uint8_t *in, ADPCMChannelStatus *left, ADPCMChannelStatus *right, int channels, int sample_offset)
Definition: adpcm.c:345
#define FFMIN(a, b)
Definition: common.h:96
const int8_t ff_adpcm_AdaptCoeff2[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:95
int vqa_version
VQA version.
Definition: adpcm.c:88
int32_t
static const uint8_t ff_adpcm_ima_block_sizes[4]
Definition: adpcm_data.h:31
static enum AVSampleFormat sample_fmts_s16p[]
Definition: adpcm.c:1654
int n
Definition: avisynth_c.h:547
const int16_t ff_adpcm_oki_step_table[49]
Definition: adpcm_data.c:73
#define FF_ARRAY_ELEMS(a)
static short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
Definition: adpcm.c:175
static const float pred[4]
Definition: siprdata.h:259
static const int swf_index_tables[4][16]
Definition: adpcm.c:77
static const uint8_t ff_adpcm_ima_block_samples[4]
Definition: adpcm_data.h:32
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
const int16_t ff_adpcm_AdaptationTable[]
Definition: adpcm_data.c:84
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:87
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:449
static short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition: adpcm.c:250
main external API structure.
Definition: avcodec.h:1639
#define DK3_GET_NEXT_NIBBLE()
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:928
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1755
int index
Definition: gxfenc.c:89
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:418
static short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition: adpcm.c:269
ADPCMChannelStatus status[14]
Definition: adpcm.c:87
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:139
static unsigned int get_bits_le(GetBitContext *s, int n)
Definition: get_bits.h:280
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:194
uint8_t level
Definition: svq3.c:193
const int8_t ff_adpcm_yamaha_difflookup[]
Definition: adpcm_data.c:104
common internal api header.
const int16_t ff_adpcm_yamaha_indexscale[]
Definition: adpcm_data.c:99
static int adpcm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: adpcm.c:689
signed 16 bits
Definition: samplefmt.h:61
static double c[64]
unsigned bps
Definition: movenc.c:1354
void * priv_data
Definition: avcodec.h:1681
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int channels
number of audio channels
Definition: avcodec.h:2401
static const double coeff[2][5]
Definition: vf_owdenoise.c:71
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
static enum AVSampleFormat sample_fmts_both[]
Definition: adpcm.c:1656
int16_t step_index
Definition: adpcm.h:35
signed 16 bits, planar
Definition: samplefmt.h:67
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:227
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1551
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:237
for(j=16;j >0;--j)
#define tb
Definition: regdef.h:68
#define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_)
Definition: adpcm.c:1660