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