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