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