FFmpeg
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 int8_t 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 int16_t 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 int8_t 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;
109  max_channels = 6;
110  break;
112  min_channels = 2;
113  max_channels = 8;
114  break;
116  max_channels = 8;
117  break;
121  max_channels = 14;
122  break;
123  }
124  if (avctx->channels < min_channels || avctx->channels > max_channels) {
125  av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
126  return AVERROR(EINVAL);
127  }
128 
129  switch(avctx->codec->id) {
131  c->status[0].step = c->status[1].step = 511;
132  break;
134  if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
135  return AVERROR_INVALIDDATA;
136  break;
138  if (avctx->extradata && avctx->extradata_size >= 8) {
139  c->status[0].predictor = AV_RL32(avctx->extradata);
140  c->status[1].predictor = AV_RL32(avctx->extradata + 4);
141  }
142  break;
144  if (avctx->extradata && avctx->extradata_size >= 2)
145  c->vqa_version = AV_RL16(avctx->extradata);
146  break;
147  default:
148  break;
149  }
150 
151  switch(avctx->codec->id) {
169  break;
171  avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
173  break;
175  avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
177  break;
178  default:
179  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
180  }
181 
182  return 0;
183 }
184 
185 static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
186 {
187  int delta, pred, step, add;
188 
189  pred = c->predictor;
190  delta = nibble & 7;
191  step = c->step;
192  add = (delta * 2 + 1) * step;
193  if (add < 0)
194  add = add + 7;
195 
196  if ((nibble & 8) == 0)
197  pred = av_clip(pred + (add >> 3), -32767, 32767);
198  else
199  pred = av_clip(pred - (add >> 3), -32767, 32767);
200 
201  switch (delta) {
202  case 7:
203  step *= 0x99;
204  break;
205  case 6:
206  c->step = av_clip(c->step * 2, 127, 24576);
207  c->predictor = pred;
208  return pred;
209  case 5:
210  step *= 0x66;
211  break;
212  case 4:
213  step *= 0x4d;
214  break;
215  default:
216  step *= 0x39;
217  break;
218  }
219 
220  if (step < 0)
221  step += 0x3f;
222 
223  c->step = step >> 6;
224  c->step = av_clip(c->step, 127, 24576);
225  c->predictor = pred;
226  return pred;
227 }
228 
229 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
230 {
231  int step_index;
232  int predictor;
233  int sign, delta, diff, step;
234 
235  step = ff_adpcm_step_table[c->step_index];
236  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
237  step_index = av_clip(step_index, 0, 88);
238 
239  sign = nibble & 8;
240  delta = nibble & 7;
241  /* perform direct multiplication instead of series of jumps proposed by
242  * the reference ADPCM implementation since modern CPUs can do the mults
243  * quickly enough */
244  diff = ((2 * delta + 1) * step) >> shift;
245  predictor = c->predictor;
246  if (sign) predictor -= diff;
247  else predictor += diff;
248 
249  c->predictor = av_clip_int16(predictor);
250  c->step_index = step_index;
251 
252  return (int16_t)c->predictor;
253 }
254 
256 {
257  int nibble, step_index, predictor, sign, delta, diff, step, shift;
258 
259  shift = bps - 1;
260  nibble = get_bits_le(gb, bps),
261  step = ff_adpcm_step_table[c->step_index];
262  step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
263  step_index = av_clip(step_index, 0, 88);
264 
265  sign = nibble & (1 << shift);
266  delta = av_mod_uintp2(nibble, shift);
267  diff = ((2 * delta + 1) * step) >> shift;
268  predictor = c->predictor;
269  if (sign) predictor -= diff;
270  else predictor += diff;
271 
272  c->predictor = av_clip_int16(predictor);
273  c->step_index = step_index;
274 
275  return (int16_t)c->predictor;
276 }
277 
278 static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
279 {
280  int step_index;
281  int predictor;
282  int diff, step;
283 
284  step = ff_adpcm_step_table[c->step_index];
285  step_index = c->step_index + ff_adpcm_index_table[nibble];
286  step_index = av_clip(step_index, 0, 88);
287 
288  diff = step >> 3;
289  if (nibble & 4) diff += step;
290  if (nibble & 2) diff += step >> 1;
291  if (nibble & 1) diff += step >> 2;
292 
293  if (nibble & 8)
294  predictor = c->predictor - diff;
295  else
296  predictor = c->predictor + diff;
297 
298  c->predictor = av_clip_int16(predictor);
299  c->step_index = step_index;
300 
301  return c->predictor;
302 }
303 
304 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
305 {
306  int predictor;
307 
308  predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
309  predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
310 
311  c->sample2 = c->sample1;
312  c->sample1 = av_clip_int16(predictor);
313  c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
314  if (c->idelta < 16) c->idelta = 16;
315  if (c->idelta > INT_MAX/768) {
316  av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
317  c->idelta = INT_MAX/768;
318  }
319 
320  return c->sample1;
321 }
322 
323 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
324 {
325  int step_index, predictor, sign, delta, diff, step;
326 
328  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
329  step_index = av_clip(step_index, 0, 48);
330 
331  sign = nibble & 8;
332  delta = nibble & 7;
333  diff = ((2 * delta + 1) * step) >> 3;
334  predictor = c->predictor;
335  if (sign) predictor -= diff;
336  else predictor += diff;
337 
338  c->predictor = av_clip_intp2(predictor, 11);
339  c->step_index = step_index;
340 
341  return c->predictor << 4;
342 }
343 
344 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
345 {
346  int sign, delta, diff;
347  int new_step;
348 
349  sign = nibble & 8;
350  delta = nibble & 7;
351  /* perform direct multiplication instead of series of jumps proposed by
352  * the reference ADPCM implementation since modern CPUs can do the mults
353  * quickly enough */
354  diff = ((2 * delta + 1) * c->step) >> 3;
355  /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
356  c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
357  c->predictor = av_clip_int16(c->predictor);
358  /* calculate new step and clamp it to range 511..32767 */
359  new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
360  c->step = av_clip(new_step, 511, 32767);
361 
362  return (int16_t)c->predictor;
363 }
364 
365 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
366 {
367  int sign, delta, diff;
368 
369  sign = nibble & (1<<(size-1));
370  delta = nibble & ((1<<(size-1))-1);
371  diff = delta << (7 + c->step + shift);
372 
373  /* clamp result */
374  c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
375 
376  /* calculate new step */
377  if (delta >= (2*size - 3) && c->step < 3)
378  c->step++;
379  else if (delta == 0 && c->step > 0)
380  c->step--;
381 
382  return (int16_t) c->predictor;
383 }
384 
386 {
387  if(!c->step) {
388  c->predictor = 0;
389  c->step = 127;
390  }
391 
392  c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
393  c->predictor = av_clip_int16(c->predictor);
394  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
395  c->step = av_clip(c->step, 127, 24576);
396  return c->predictor;
397 }
398 
399 static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
400 {
401  c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];
402  c->predictor = av_clip_int16(c->predictor);
403  c->step += ff_adpcm_index_table[nibble];
404  c->step = av_clip_uintp2(c->step, 5);
405  return c->predictor;
406 }
407 
408 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
410  ADPCMChannelStatus *right, int channels, int sample_offset)
411 {
412  int i, j;
413  int shift,filter,f0,f1;
414  int s_1,s_2;
415  int d,s,t;
416 
417  out0 += sample_offset;
418  if (channels == 1)
419  out1 = out0 + 28;
420  else
421  out1 += sample_offset;
422 
423  for(i=0;i<4;i++) {
424  shift = 12 - (in[4+i*2] & 15);
425  filter = in[4+i*2] >> 4;
426  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
427  avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
428  filter=0;
429  }
430  f0 = xa_adpcm_table[filter][0];
431  f1 = xa_adpcm_table[filter][1];
432 
433  s_1 = left->sample1;
434  s_2 = left->sample2;
435 
436  for(j=0;j<28;j++) {
437  d = in[16+i+j*4];
438 
439  t = sign_extend(d, 4);
440  s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
441  s_2 = s_1;
442  s_1 = av_clip_int16(s);
443  out0[j] = s_1;
444  }
445 
446  if (channels == 2) {
447  left->sample1 = s_1;
448  left->sample2 = s_2;
449  s_1 = right->sample1;
450  s_2 = right->sample2;
451  }
452 
453  shift = 12 - (in[5+i*2] & 15);
454  filter = in[5+i*2] >> 4;
455  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
456  avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
457  filter=0;
458  }
459 
460  f0 = xa_adpcm_table[filter][0];
461  f1 = xa_adpcm_table[filter][1];
462 
463  for(j=0;j<28;j++) {
464  d = in[16+i+j*4];
465 
466  t = sign_extend(d >> 4, 4);
467  s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
468  s_2 = s_1;
469  s_1 = av_clip_int16(s);
470  out1[j] = s_1;
471  }
472 
473  if (channels == 2) {
474  right->sample1 = s_1;
475  right->sample2 = s_2;
476  } else {
477  left->sample1 = s_1;
478  left->sample2 = s_2;
479  }
480 
481  out0 += 28 * (3 - channels);
482  out1 += 28 * (3 - channels);
483  }
484 
485  return 0;
486 }
487 
488 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
489 {
490  ADPCMDecodeContext *c = avctx->priv_data;
491  GetBitContext gb;
492  const int8_t *table;
493  int k0, signmask, nb_bits, count;
494  int size = buf_size*8;
495  int i;
496 
497  init_get_bits(&gb, buf, size);
498 
499  //read bits & initial values
500  nb_bits = get_bits(&gb, 2)+2;
501  table = swf_index_tables[nb_bits-2];
502  k0 = 1 << (nb_bits-2);
503  signmask = 1 << (nb_bits-1);
504 
505  while (get_bits_count(&gb) <= size - 22*avctx->channels) {
506  for (i = 0; i < avctx->channels; i++) {
507  *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
508  c->status[i].step_index = get_bits(&gb, 6);
509  }
510 
511  for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
512  int i;
513 
514  for (i = 0; i < avctx->channels; i++) {
515  // similar to IMA adpcm
516  int delta = get_bits(&gb, nb_bits);
518  int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
519  int k = k0;
520 
521  do {
522  if (delta & k)
523  vpdiff += step;
524  step >>= 1;
525  k >>= 1;
526  } while(k);
527  vpdiff += step;
528 
529  if (delta & signmask)
530  c->status[i].predictor -= vpdiff;
531  else
532  c->status[i].predictor += vpdiff;
533 
534  c->status[i].step_index += table[delta & (~signmask)];
535 
536  c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
537  c->status[i].predictor = av_clip_int16(c->status[i].predictor);
538 
539  *samples++ = c->status[i].predictor;
540  }
541  }
542  }
543 }
544 
545 /**
546  * Get the number of samples that will be decoded from the packet.
547  * In one case, this is actually the maximum number of samples possible to
548  * decode with the given buf_size.
549  *
550  * @param[out] coded_samples set to the number of samples as coded in the
551  * packet, or 0 if the codec does not encode the
552  * number of samples in each frame.
553  * @param[out] approx_nb_samples set to non-zero if the number of samples
554  * returned is an approximation.
555  */
557  int buf_size, int *coded_samples, int *approx_nb_samples)
558 {
559  ADPCMDecodeContext *s = avctx->priv_data;
560  int nb_samples = 0;
561  int ch = avctx->channels;
562  int has_coded_samples = 0;
563  int header_size;
564 
565  *coded_samples = 0;
566  *approx_nb_samples = 0;
567 
568  if(ch <= 0)
569  return 0;
570 
571  switch (avctx->codec->id) {
572  /* constant, only check buf_size */
574  if (buf_size < 76 * ch)
575  return 0;
576  nb_samples = 128;
577  break;
579  if (buf_size < 34 * ch)
580  return 0;
581  nb_samples = 64;
582  break;
583  /* simple 4-bit adpcm */
591  nb_samples = buf_size * 2 / ch;
592  break;
593  }
594  if (nb_samples)
595  return nb_samples;
596 
597  /* simple 4-bit adpcm, with header */
598  header_size = 0;
599  switch (avctx->codec->id) {
603  case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
604  case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
605  case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
606  }
607  if (header_size > 0)
608  return (buf_size - header_size) * 2 / ch;
609 
610  /* more complex formats */
611  switch (avctx->codec->id) {
613  has_coded_samples = 1;
614  *coded_samples = bytestream2_get_le32(gb);
615  *coded_samples -= *coded_samples % 28;
616  nb_samples = (buf_size - 12) / 30 * 28;
617  break;
619  has_coded_samples = 1;
620  *coded_samples = bytestream2_get_le32(gb);
621  nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
622  break;
624  nb_samples = (buf_size - ch) / ch * 2;
625  break;
629  /* maximum number of samples */
630  /* has internal offsets and a per-frame switch to signal raw 16-bit */
631  has_coded_samples = 1;
632  switch (avctx->codec->id) {
634  header_size = 4 + 9 * ch;
635  *coded_samples = bytestream2_get_le32(gb);
636  break;
638  header_size = 4 + 5 * ch;
639  *coded_samples = bytestream2_get_le32(gb);
640  break;
642  header_size = 4 + 5 * ch;
643  *coded_samples = bytestream2_get_be32(gb);
644  break;
645  }
646  *coded_samples -= *coded_samples % 28;
647  nb_samples = (buf_size - header_size) * 2 / ch;
648  nb_samples -= nb_samples % 28;
649  *approx_nb_samples = 1;
650  break;
652  if (avctx->block_align > 0)
653  buf_size = FFMIN(buf_size, avctx->block_align);
654  nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
655  break;
657  if (avctx->block_align > 0)
658  buf_size = FFMIN(buf_size, avctx->block_align);
659  if (buf_size < 4 * ch)
660  return AVERROR_INVALIDDATA;
661  nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
662  break;
664  if (avctx->block_align > 0)
665  buf_size = FFMIN(buf_size, avctx->block_align);
666  nb_samples = (buf_size - 4 * ch) * 2 / ch;
667  break;
669  {
670  int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
671  int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
672  if (avctx->block_align > 0)
673  buf_size = FFMIN(buf_size, avctx->block_align);
674  if (buf_size < 4 * ch)
675  return AVERROR_INVALIDDATA;
676  nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
677  break;
678  }
680  if (avctx->block_align > 0)
681  buf_size = FFMIN(buf_size, avctx->block_align);
682  nb_samples = (buf_size - 6 * ch) * 2 / ch;
683  break;
685  if (avctx->block_align > 0)
686  buf_size = FFMIN(buf_size, avctx->block_align);
687  nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
688  break;
692  {
693  int samples_per_byte;
694  switch (avctx->codec->id) {
695  case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
696  case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
697  case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
698  }
699  if (!s->status[0].step_index) {
700  if (buf_size < ch)
701  return AVERROR_INVALIDDATA;
702  nb_samples++;
703  buf_size -= ch;
704  }
705  nb_samples += buf_size * samples_per_byte / ch;
706  break;
707  }
709  {
710  int buf_bits = buf_size * 8 - 2;
711  int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
712  int block_hdr_size = 22 * ch;
713  int block_size = block_hdr_size + nbits * ch * 4095;
714  int nblocks = buf_bits / block_size;
715  int bits_left = buf_bits - nblocks * block_size;
716  nb_samples = nblocks * 4096;
717  if (bits_left >= block_hdr_size)
718  nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
719  break;
720  }
723  if (avctx->extradata) {
724  nb_samples = buf_size * 14 / (8 * ch);
725  break;
726  }
727  has_coded_samples = 1;
728  bytestream2_skip(gb, 4); // channel size
729  *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
730  bytestream2_get_le32(gb) :
731  bytestream2_get_be32(gb);
732  buf_size -= 8 + 36 * ch;
733  buf_size /= ch;
734  nb_samples = buf_size / 8 * 14;
735  if (buf_size % 8 > 1)
736  nb_samples += (buf_size % 8 - 1) * 2;
737  *approx_nb_samples = 1;
738  break;
740  nb_samples = buf_size / (9 * ch) * 16;
741  break;
743  nb_samples = (buf_size / 128) * 224 / ch;
744  break;
747  nb_samples = buf_size / (16 * ch) * 28;
748  break;
749  }
750 
751  /* validate coded sample count */
752  if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
753  return AVERROR_INVALIDDATA;
754 
755  return nb_samples;
756 }
757 
758 static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
759  int *got_frame_ptr, AVPacket *avpkt)
760 {
761  AVFrame *frame = data;
762  const uint8_t *buf = avpkt->data;
763  int buf_size = avpkt->size;
764  ADPCMDecodeContext *c = avctx->priv_data;
765  ADPCMChannelStatus *cs;
766  int n, m, channel, i;
767  int16_t *samples;
768  int16_t **samples_p;
769  int st; /* stereo */
770  int count1, count2;
771  int nb_samples, coded_samples, approx_nb_samples, ret;
772  GetByteContext gb;
773 
774  bytestream2_init(&gb, buf, buf_size);
775  nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
776  if (nb_samples <= 0) {
777  av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
778  return AVERROR_INVALIDDATA;
779  }
780 
781  /* get output buffer */
782  frame->nb_samples = nb_samples;
783  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
784  return ret;
785  samples = (int16_t *)frame->data[0];
786  samples_p = (int16_t **)frame->extended_data;
787 
788  /* use coded_samples when applicable */
789  /* it is always <= nb_samples, so the output buffer will be large enough */
790  if (coded_samples) {
791  if (!approx_nb_samples && coded_samples != nb_samples)
792  av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
793  frame->nb_samples = nb_samples = coded_samples;
794  }
795 
796  st = avctx->channels == 2 ? 1 : 0;
797 
798  switch(avctx->codec->id) {
800  /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
801  Channel data is interleaved per-chunk. */
802  for (channel = 0; channel < avctx->channels; channel++) {
803  int predictor;
804  int step_index;
805  cs = &(c->status[channel]);
806  /* (pppppp) (piiiiiii) */
807 
808  /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
809  predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
810  step_index = predictor & 0x7F;
811  predictor &= ~0x7F;
812 
813  if (cs->step_index == step_index) {
814  int diff = predictor - cs->predictor;
815  if (diff < 0)
816  diff = - diff;
817  if (diff > 0x7f)
818  goto update;
819  } else {
820  update:
821  cs->step_index = step_index;
822  cs->predictor = predictor;
823  }
824 
825  if (cs->step_index > 88u){
826  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
827  channel, cs->step_index);
828  return AVERROR_INVALIDDATA;
829  }
830 
831  samples = samples_p[channel];
832 
833  for (m = 0; m < 64; m += 2) {
834  int byte = bytestream2_get_byteu(&gb);
835  samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
836  samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
837  }
838  }
839  break;
841  for(i=0; i<avctx->channels; i++){
842  cs = &(c->status[i]);
843  cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
844 
845  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
846  if (cs->step_index > 88u){
847  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
848  i, cs->step_index);
849  return AVERROR_INVALIDDATA;
850  }
851  }
852 
853  if (avctx->bits_per_coded_sample != 4) {
854  int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
855  int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
858 
859  for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
860  for (i = 0; i < avctx->channels; i++) {
861  int j;
862 
863  cs = &c->status[i];
864  samples = &samples_p[i][1 + n * samples_per_block];
865  for (j = 0; j < block_size; j++) {
866  temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
867  (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
868  }
869  ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
870  if (ret < 0)
871  return ret;
872  for (m = 0; m < samples_per_block; m++) {
873  samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
874  avctx->bits_per_coded_sample);
875  }
876  }
877  }
878  bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
879  } else {
880  for (n = 0; n < (nb_samples - 1) / 8; n++) {
881  for (i = 0; i < avctx->channels; i++) {
882  cs = &c->status[i];
883  samples = &samples_p[i][1 + n * 8];
884  for (m = 0; m < 8; m += 2) {
885  int v = bytestream2_get_byteu(&gb);
886  samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
887  samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
888  }
889  }
890  }
891  }
892  break;
894  for (i = 0; i < avctx->channels; i++)
895  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
896 
897  for (i = 0; i < avctx->channels; i++) {
898  c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
899  if (c->status[i].step_index > 88u) {
900  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
901  i, c->status[i].step_index);
902  return AVERROR_INVALIDDATA;
903  }
904  }
905 
906  for (i = 0; i < avctx->channels; i++) {
907  samples = (int16_t *)frame->data[i];
908  cs = &c->status[i];
909  for (n = nb_samples >> 1; n > 0; n--) {
910  int v = bytestream2_get_byteu(&gb);
911  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
912  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
913  }
914  }
915  break;
917  for (i = 0; i < avctx->channels; i++)
918  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
919  for (i = 0; i < avctx->channels; i++)
920  c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
921 
922  for (n = 0; n < nb_samples >> (1 - st); n++) {
923  int v = bytestream2_get_byteu(&gb);
924  *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
925  *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
926  }
927  break;
929  {
930  int block_predictor;
931 
932  if (avctx->channels > 2) {
933  for (channel = 0; channel < avctx->channels; channel++) {
934  samples = samples_p[channel];
935  block_predictor = bytestream2_get_byteu(&gb);
936  if (block_predictor > 6) {
937  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
938  channel, block_predictor);
939  return AVERROR_INVALIDDATA;
940  }
941  c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
942  c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
943  c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
944  c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
945  c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
946  *samples++ = c->status[channel].sample2;
947  *samples++ = c->status[channel].sample1;
948  for(n = (nb_samples - 2) >> 1; n > 0; n--) {
949  int byte = bytestream2_get_byteu(&gb);
950  *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
951  *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
952  }
953  }
954  } else {
955  block_predictor = bytestream2_get_byteu(&gb);
956  if (block_predictor > 6) {
957  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
958  block_predictor);
959  return AVERROR_INVALIDDATA;
960  }
961  c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
962  c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
963  if (st) {
964  block_predictor = bytestream2_get_byteu(&gb);
965  if (block_predictor > 6) {
966  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
967  block_predictor);
968  return AVERROR_INVALIDDATA;
969  }
970  c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
971  c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
972  }
973  c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
974  if (st){
975  c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
976  }
977 
978  c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
979  if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
980  c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
981  if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
982 
983  *samples++ = c->status[0].sample2;
984  if (st) *samples++ = c->status[1].sample2;
985  *samples++ = c->status[0].sample1;
986  if (st) *samples++ = c->status[1].sample1;
987  for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
988  int byte = bytestream2_get_byteu(&gb);
989  *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
990  *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
991  }
992  }
993  break;
994  }
996  for (channel = 0; channel < avctx->channels; channel+=2) {
997  bytestream2_skipu(&gb, 4);
998  c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
999  c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1000  c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1001  bytestream2_skipu(&gb, 2);
1002  c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1003  bytestream2_skipu(&gb, 2);
1004  for (n = 0; n < nb_samples; n+=2) {
1005  int v = bytestream2_get_byteu(&gb);
1006  samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1007  samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
1008  }
1009  for (n = 0; n < nb_samples; n+=2) {
1010  int v = bytestream2_get_byteu(&gb);
1011  samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1012  samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
1013  }
1014  }
1015  break;
1017  for (channel = 0; channel < avctx->channels; channel++) {
1018  cs = &c->status[channel];
1019  cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1020  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1021  if (cs->step_index > 88u){
1022  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1023  channel, cs->step_index);
1024  return AVERROR_INVALIDDATA;
1025  }
1026  }
1027  for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1028  int v = bytestream2_get_byteu(&gb);
1029  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1030  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1031  }
1032  break;
1034  {
1035  int last_byte = 0;
1036  int nibble;
1037  int decode_top_nibble_next = 0;
1038  int diff_channel;
1039  const int16_t *samples_end = samples + avctx->channels * nb_samples;
1040 
1041  bytestream2_skipu(&gb, 10);
1042  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1043  c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1044  c->status[0].step_index = bytestream2_get_byteu(&gb);
1045  c->status[1].step_index = bytestream2_get_byteu(&gb);
1046  if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1047  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1048  c->status[0].step_index, c->status[1].step_index);
1049  return AVERROR_INVALIDDATA;
1050  }
1051  /* sign extend the predictors */
1052  diff_channel = c->status[1].predictor;
1053 
1054  /* DK3 ADPCM support macro */
1055 #define DK3_GET_NEXT_NIBBLE() \
1056  if (decode_top_nibble_next) { \
1057  nibble = last_byte >> 4; \
1058  decode_top_nibble_next = 0; \
1059  } else { \
1060  last_byte = bytestream2_get_byteu(&gb); \
1061  nibble = last_byte & 0x0F; \
1062  decode_top_nibble_next = 1; \
1063  }
1064 
1065  while (samples < samples_end) {
1066 
1067  /* for this algorithm, c->status[0] is the sum channel and
1068  * c->status[1] is the diff channel */
1069 
1070  /* process the first predictor of the sum channel */
1072  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1073 
1074  /* process the diff channel predictor */
1076  adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1077 
1078  /* process the first pair of stereo PCM samples */
1079  diff_channel = (diff_channel + c->status[1].predictor) / 2;
1080  *samples++ = c->status[0].predictor + c->status[1].predictor;
1081  *samples++ = c->status[0].predictor - c->status[1].predictor;
1082 
1083  /* process the second predictor of the sum channel */
1085  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1086 
1087  /* process the second pair of stereo PCM samples */
1088  diff_channel = (diff_channel + c->status[1].predictor) / 2;
1089  *samples++ = c->status[0].predictor + c->status[1].predictor;
1090  *samples++ = c->status[0].predictor - c->status[1].predictor;
1091  }
1092 
1093  if ((bytestream2_tell(&gb) & 1))
1094  bytestream2_skip(&gb, 1);
1095  break;
1096  }
1098  for (channel = 0; channel < avctx->channels; channel++) {
1099  cs = &c->status[channel];
1100  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1101  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1102  if (cs->step_index > 88u){
1103  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1104  channel, cs->step_index);
1105  return AVERROR_INVALIDDATA;
1106  }
1107  }
1108 
1109  for (n = nb_samples >> (1 - st); n > 0; n--) {
1110  int v1, v2;
1111  int v = bytestream2_get_byteu(&gb);
1112  /* nibbles are swapped for mono */
1113  if (st) {
1114  v1 = v >> 4;
1115  v2 = v & 0x0F;
1116  } else {
1117  v2 = v >> 4;
1118  v1 = v & 0x0F;
1119  }
1120  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1121  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1122  }
1123  break;
1125  for (channel = 0; channel < avctx->channels; channel++) {
1126  cs = &c->status[channel];
1127  samples = samples_p[channel];
1128  bytestream2_skip(&gb, 4);
1129  for (n = 0; n < nb_samples; n += 2) {
1130  int v = bytestream2_get_byteu(&gb);
1131  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1132  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1133  }
1134  }
1135  break;
1137  while (bytestream2_get_bytes_left(&gb) > 0) {
1138  int v = bytestream2_get_byteu(&gb);
1139  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1140  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1141  }
1142  break;
1144  while (bytestream2_get_bytes_left(&gb) > 0) {
1145  int v = bytestream2_get_byteu(&gb);
1146  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1147  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1148  }
1149  break;
1151  for (channel = 0; channel < avctx->channels; channel++) {
1152  cs = &c->status[channel];
1153  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1154  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1155  if (cs->step_index > 88u){
1156  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1157  channel, cs->step_index);
1158  return AVERROR_INVALIDDATA;
1159  }
1160  }
1161  for (n = 0; n < nb_samples / 2; n++) {
1162  int byte[2];
1163 
1164  byte[0] = bytestream2_get_byteu(&gb);
1165  if (st)
1166  byte[1] = bytestream2_get_byteu(&gb);
1167  for(channel = 0; channel < avctx->channels; channel++) {
1168  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1169  }
1170  for(channel = 0; channel < avctx->channels; channel++) {
1171  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1172  }
1173  }
1174  break;
1176  if (c->vqa_version == 3) {
1177  for (channel = 0; channel < avctx->channels; channel++) {
1178  int16_t *smp = samples_p[channel];
1179 
1180  for (n = nb_samples / 2; n > 0; n--) {
1181  int v = bytestream2_get_byteu(&gb);
1182  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1183  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1184  }
1185  }
1186  } else {
1187  for (n = nb_samples / 2; n > 0; n--) {
1188  for (channel = 0; channel < avctx->channels; channel++) {
1189  int v = bytestream2_get_byteu(&gb);
1190  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1191  samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1192  }
1193  samples += avctx->channels;
1194  }
1195  }
1196  bytestream2_seek(&gb, 0, SEEK_END);
1197  break;
1198  case AV_CODEC_ID_ADPCM_XA:
1199  {
1200  int16_t *out0 = samples_p[0];
1201  int16_t *out1 = samples_p[1];
1202  int samples_per_block = 28 * (3 - avctx->channels) * 4;
1203  int sample_offset = 0;
1204  int bytes_remaining;
1205  while (bytestream2_get_bytes_left(&gb) >= 128) {
1206  if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1207  &c->status[0], &c->status[1],
1208  avctx->channels, sample_offset)) < 0)
1209  return ret;
1210  bytestream2_skipu(&gb, 128);
1211  sample_offset += samples_per_block;
1212  }
1213  /* Less than a full block of data left, e.g. when reading from
1214  * 2324 byte per sector XA; the remainder is padding */
1215  bytes_remaining = bytestream2_get_bytes_left(&gb);
1216  if (bytes_remaining > 0) {
1217  bytestream2_skip(&gb, bytes_remaining);
1218  }
1219  break;
1220  }
1222  for (i=0; i<=st; i++) {
1223  c->status[i].step_index = bytestream2_get_le32u(&gb);
1224  if (c->status[i].step_index > 88u) {
1225  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1226  i, c->status[i].step_index);
1227  return AVERROR_INVALIDDATA;
1228  }
1229  }
1230  for (i=0; i<=st; i++)
1231  c->status[i].predictor = bytestream2_get_le32u(&gb);
1232 
1233  for (n = nb_samples >> (1 - st); n > 0; n--) {
1234  int byte = bytestream2_get_byteu(&gb);
1235  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1236  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1237  }
1238  break;
1240  for (n = nb_samples >> (1 - st); n > 0; n--) {
1241  int byte = bytestream2_get_byteu(&gb);
1242  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1243  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1244  }
1245  break;
1246  case AV_CODEC_ID_ADPCM_EA:
1247  {
1248  int previous_left_sample, previous_right_sample;
1249  int current_left_sample, current_right_sample;
1250  int next_left_sample, next_right_sample;
1251  int coeff1l, coeff2l, coeff1r, coeff2r;
1252  int shift_left, shift_right;
1253 
1254  /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1255  each coding 28 stereo samples. */
1256 
1257  if(avctx->channels != 2)
1258  return AVERROR_INVALIDDATA;
1259 
1260  current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1261  previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1262  current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1263  previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1264 
1265  for (count1 = 0; count1 < nb_samples / 28; count1++) {
1266  int byte = bytestream2_get_byteu(&gb);
1267  coeff1l = ea_adpcm_table[ byte >> 4 ];
1268  coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1269  coeff1r = ea_adpcm_table[ byte & 0x0F];
1270  coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1271 
1272  byte = bytestream2_get_byteu(&gb);
1273  shift_left = 20 - (byte >> 4);
1274  shift_right = 20 - (byte & 0x0F);
1275 
1276  for (count2 = 0; count2 < 28; count2++) {
1277  byte = bytestream2_get_byteu(&gb);
1278  next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
1279  next_right_sample = sign_extend(byte, 4) << shift_right;
1280 
1281  next_left_sample = (next_left_sample +
1282  (current_left_sample * coeff1l) +
1283  (previous_left_sample * coeff2l) + 0x80) >> 8;
1284  next_right_sample = (next_right_sample +
1285  (current_right_sample * coeff1r) +
1286  (previous_right_sample * coeff2r) + 0x80) >> 8;
1287 
1288  previous_left_sample = current_left_sample;
1289  current_left_sample = av_clip_int16(next_left_sample);
1290  previous_right_sample = current_right_sample;
1291  current_right_sample = av_clip_int16(next_right_sample);
1292  *samples++ = current_left_sample;
1293  *samples++ = current_right_sample;
1294  }
1295  }
1296 
1297  bytestream2_skip(&gb, 2); // Skip terminating 0x0000
1298 
1299  break;
1300  }
1302  {
1303  int coeff[2][2], shift[2];
1304 
1305  for(channel = 0; channel < avctx->channels; channel++) {
1306  int byte = bytestream2_get_byteu(&gb);
1307  for (i=0; i<2; i++)
1308  coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1309  shift[channel] = 20 - (byte & 0x0F);
1310  }
1311  for (count1 = 0; count1 < nb_samples / 2; count1++) {
1312  int byte[2];
1313 
1314  byte[0] = bytestream2_get_byteu(&gb);
1315  if (st) byte[1] = bytestream2_get_byteu(&gb);
1316  for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1317  for(channel = 0; channel < avctx->channels; channel++) {
1318  int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
1319  sample = (sample +
1320  c->status[channel].sample1 * coeff[channel][0] +
1321  c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1323  c->status[channel].sample1 = av_clip_int16(sample);
1324  *samples++ = c->status[channel].sample1;
1325  }
1326  }
1327  }
1328  bytestream2_seek(&gb, 0, SEEK_END);
1329  break;
1330  }
1333  case AV_CODEC_ID_ADPCM_EA_R3: {
1334  /* channel numbering
1335  2chan: 0=fl, 1=fr
1336  4chan: 0=fl, 1=rl, 2=fr, 3=rr
1337  6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1338  const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1339  int previous_sample, current_sample, next_sample;
1340  int coeff1, coeff2;
1341  int shift;
1342  unsigned int channel;
1343  uint16_t *samplesC;
1344  int count = 0;
1345  int offsets[6];
1346 
1347  for (channel=0; channel<avctx->channels; channel++)
1348  offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1349  bytestream2_get_le32(&gb)) +
1350  (avctx->channels + 1) * 4;
1351 
1352  for (channel=0; channel<avctx->channels; channel++) {
1353  bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1354  samplesC = samples_p[channel];
1355 
1356  if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1357  current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1358  previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
1359  } else {
1360  current_sample = c->status[channel].predictor;
1361  previous_sample = c->status[channel].prev_sample;
1362  }
1363 
1364  for (count1 = 0; count1 < nb_samples / 28; count1++) {
1365  int byte = bytestream2_get_byte(&gb);
1366  if (byte == 0xEE) { /* only seen in R2 and R3 */
1367  current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1368  previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
1369 
1370  for (count2=0; count2<28; count2++)
1371  *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
1372  } else {
1373  coeff1 = ea_adpcm_table[ byte >> 4 ];
1374  coeff2 = ea_adpcm_table[(byte >> 4) + 4];
1375  shift = 20 - (byte & 0x0F);
1376 
1377  for (count2=0; count2<28; count2++) {
1378  if (count2 & 1)
1379  next_sample = sign_extend(byte, 4) << shift;
1380  else {
1381  byte = bytestream2_get_byte(&gb);
1382  next_sample = sign_extend(byte >> 4, 4) << shift;
1383  }
1384 
1385  next_sample += (current_sample * coeff1) +
1386  (previous_sample * coeff2);
1387  next_sample = av_clip_int16(next_sample >> 8);
1388 
1389  previous_sample = current_sample;
1390  current_sample = next_sample;
1391  *samplesC++ = current_sample;
1392  }
1393  }
1394  }
1395  if (!count) {
1396  count = count1;
1397  } else if (count != count1) {
1398  av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
1399  count = FFMAX(count, count1);
1400  }
1401 
1402  if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
1403  c->status[channel].predictor = current_sample;
1404  c->status[channel].prev_sample = previous_sample;
1405  }
1406  }
1407 
1408  frame->nb_samples = count * 28;
1409  bytestream2_seek(&gb, 0, SEEK_END);
1410  break;
1411  }
1413  for (channel=0; channel<avctx->channels; channel++) {
1414  int coeff[2][4], shift[4];
1415  int16_t *s = samples_p[channel];
1416  for (n = 0; n < 4; n++, s += 32) {
1417  int val = sign_extend(bytestream2_get_le16u(&gb), 16);
1418  for (i=0; i<2; i++)
1419  coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
1420  s[0] = val & ~0x0F;
1421 
1422  val = sign_extend(bytestream2_get_le16u(&gb), 16);
1423  shift[n] = 20 - (val & 0x0F);
1424  s[1] = val & ~0x0F;
1425  }
1426 
1427  for (m=2; m<32; m+=2) {
1428  s = &samples_p[channel][m];
1429  for (n = 0; n < 4; n++, s += 32) {
1430  int level, pred;
1431  int byte = bytestream2_get_byteu(&gb);
1432 
1433  level = sign_extend(byte >> 4, 4) << shift[n];
1434  pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1435  s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1436 
1437  level = sign_extend(byte, 4) << shift[n];
1438  pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1439  s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1440  }
1441  }
1442  }
1443  break;
1445  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1446  c->status[0].step_index = bytestream2_get_byteu(&gb);
1447  bytestream2_skipu(&gb, 5);
1448  if (c->status[0].step_index > 88u) {
1449  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1450  c->status[0].step_index);
1451  return AVERROR_INVALIDDATA;
1452  }
1453 
1454  for (n = nb_samples >> (1 - st); n > 0; n--) {
1455  int v = bytestream2_get_byteu(&gb);
1456 
1457  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1458  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1459  }
1460  break;
1462  for (i = 0; i < avctx->channels; i++) {
1463  c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1464  c->status[i].step_index = bytestream2_get_byteu(&gb);
1465  bytestream2_skipu(&gb, 1);
1466  if (c->status[i].step_index > 88u) {
1467  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1468  c->status[i].step_index);
1469  return AVERROR_INVALIDDATA;
1470  }
1471  }
1472 
1473  for (n = nb_samples >> (1 - st); n > 0; n--) {
1474  int v = bytestream2_get_byteu(&gb);
1475 
1476  *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
1477  *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
1478  }
1479  break;
1480  case AV_CODEC_ID_ADPCM_CT:
1481  for (n = nb_samples >> (1 - st); n > 0; n--) {
1482  int v = bytestream2_get_byteu(&gb);
1483  *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
1484  *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
1485  }
1486  break;
1490  if (!c->status[0].step_index) {
1491  /* the first byte is a raw sample */
1492  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1493  if (st)
1494  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
1495  c->status[0].step_index = 1;
1496  nb_samples--;
1497  }
1498  if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
1499  for (n = nb_samples >> (1 - st); n > 0; n--) {
1500  int byte = bytestream2_get_byteu(&gb);
1501  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1502  byte >> 4, 4, 0);
1503  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1504  byte & 0x0F, 4, 0);
1505  }
1506  } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
1507  for (n = (nb_samples<<st) / 3; n > 0; n--) {
1508  int byte = bytestream2_get_byteu(&gb);
1509  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1510  byte >> 5 , 3, 0);
1511  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1512  (byte >> 2) & 0x07, 3, 0);
1513  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1514  byte & 0x03, 2, 0);
1515  }
1516  } else {
1517  for (n = nb_samples >> (2 - st); n > 0; n--) {
1518  int byte = bytestream2_get_byteu(&gb);
1519  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1520  byte >> 6 , 2, 2);
1521  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1522  (byte >> 4) & 0x03, 2, 2);
1523  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1524  (byte >> 2) & 0x03, 2, 2);
1525  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1526  byte & 0x03, 2, 2);
1527  }
1528  }
1529  break;
1530  case AV_CODEC_ID_ADPCM_SWF:
1531  adpcm_swf_decode(avctx, buf, buf_size, samples);
1532  bytestream2_seek(&gb, 0, SEEK_END);
1533  break;
1535  for (n = nb_samples >> (1 - st); n > 0; n--) {
1536  int v = bytestream2_get_byteu(&gb);
1537  *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
1538  *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
1539  }
1540  break;
1542  if (!c->has_status) {
1543  for (channel = 0; channel < avctx->channels; channel++)
1544  c->status[channel].step = 0;
1545  c->has_status = 1;
1546  }
1547  for (channel = 0; channel < avctx->channels; channel++) {
1548  samples = samples_p[channel];
1549  for (n = nb_samples >> 1; n > 0; n--) {
1550  int v = bytestream2_get_byteu(&gb);
1551  *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
1552  *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
1553  }
1554  }
1555  break;
1556  case AV_CODEC_ID_ADPCM_AFC:
1557  {
1558  int samples_per_block;
1559  int blocks;
1560 
1561  if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
1562  samples_per_block = avctx->extradata[0] / 16;
1563  blocks = nb_samples / avctx->extradata[0];
1564  } else {
1565  samples_per_block = nb_samples / 16;
1566  blocks = 1;
1567  }
1568 
1569  for (m = 0; m < blocks; m++) {
1570  for (channel = 0; channel < avctx->channels; channel++) {
1571  int prev1 = c->status[channel].sample1;
1572  int prev2 = c->status[channel].sample2;
1573 
1574  samples = samples_p[channel] + m * 16;
1575  /* Read in every sample for this channel. */
1576  for (i = 0; i < samples_per_block; i++) {
1577  int byte = bytestream2_get_byteu(&gb);
1578  int scale = 1 << (byte >> 4);
1579  int index = byte & 0xf;
1580  int factor1 = ff_adpcm_afc_coeffs[0][index];
1581  int factor2 = ff_adpcm_afc_coeffs[1][index];
1582 
1583  /* Decode 16 samples. */
1584  for (n = 0; n < 16; n++) {
1585  int32_t sampledat;
1586 
1587  if (n & 1) {
1588  sampledat = sign_extend(byte, 4);
1589  } else {
1590  byte = bytestream2_get_byteu(&gb);
1591  sampledat = sign_extend(byte >> 4, 4);
1592  }
1593 
1594  sampledat = ((prev1 * factor1 + prev2 * factor2) +
1595  ((sampledat * scale) << 11)) >> 11;
1596  *samples = av_clip_int16(sampledat);
1597  prev2 = prev1;
1598  prev1 = *samples++;
1599  }
1600  }
1601 
1602  c->status[channel].sample1 = prev1;
1603  c->status[channel].sample2 = prev2;
1604  }
1605  }
1606  bytestream2_seek(&gb, 0, SEEK_END);
1607  break;
1608  }
1609  case AV_CODEC_ID_ADPCM_THP:
1611  {
1612  int table[14][16];
1613  int ch;
1614 
1615 #define THP_GET16(g) \
1616  sign_extend( \
1617  avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
1618  bytestream2_get_le16u(&(g)) : \
1619  bytestream2_get_be16u(&(g)), 16)
1620 
1621  if (avctx->extradata) {
1623  if (avctx->extradata_size < 32 * avctx->channels) {
1624  av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
1625  return AVERROR_INVALIDDATA;
1626  }
1627 
1628  bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
1629  for (i = 0; i < avctx->channels; i++)
1630  for (n = 0; n < 16; n++)
1631  table[i][n] = THP_GET16(tb);
1632  } else {
1633  for (i = 0; i < avctx->channels; i++)
1634  for (n = 0; n < 16; n++)
1635  table[i][n] = THP_GET16(gb);
1636 
1637  if (!c->has_status) {
1638  /* Initialize the previous sample. */
1639  for (i = 0; i < avctx->channels; i++) {
1640  c->status[i].sample1 = THP_GET16(gb);
1641  c->status[i].sample2 = THP_GET16(gb);
1642  }
1643  c->has_status = 1;
1644  } else {
1645  bytestream2_skip(&gb, avctx->channels * 4);
1646  }
1647  }
1648 
1649  for (ch = 0; ch < avctx->channels; ch++) {
1650  samples = samples_p[ch];
1651 
1652  /* Read in every sample for this channel. */
1653  for (i = 0; i < (nb_samples + 13) / 14; i++) {
1654  int byte = bytestream2_get_byteu(&gb);
1655  int index = (byte >> 4) & 7;
1656  unsigned int exp = byte & 0x0F;
1657  int factor1 = table[ch][index * 2];
1658  int factor2 = table[ch][index * 2 + 1];
1659 
1660  /* Decode 14 samples. */
1661  for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
1662  int32_t sampledat;
1663 
1664  if (n & 1) {
1665  sampledat = sign_extend(byte, 4);
1666  } else {
1667  byte = bytestream2_get_byteu(&gb);
1668  sampledat = sign_extend(byte >> 4, 4);
1669  }
1670 
1671  sampledat = ((c->status[ch].sample1 * factor1
1672  + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
1673  *samples = av_clip_int16(sampledat);
1674  c->status[ch].sample2 = c->status[ch].sample1;
1675  c->status[ch].sample1 = *samples++;
1676  }
1677  }
1678  }
1679  break;
1680  }
1681  case AV_CODEC_ID_ADPCM_DTK:
1682  for (channel = 0; channel < avctx->channels; channel++) {
1683  samples = samples_p[channel];
1684 
1685  /* Read in every sample for this channel. */
1686  for (i = 0; i < nb_samples / 28; i++) {
1687  int byte, header;
1688  if (channel)
1689  bytestream2_skipu(&gb, 1);
1690  header = bytestream2_get_byteu(&gb);
1691  bytestream2_skipu(&gb, 3 - channel);
1692 
1693  /* Decode 28 samples. */
1694  for (n = 0; n < 28; n++) {
1695  int32_t sampledat, prev;
1696 
1697  switch (header >> 4) {
1698  case 1:
1699  prev = (c->status[channel].sample1 * 0x3c);
1700  break;
1701  case 2:
1702  prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
1703  break;
1704  case 3:
1705  prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
1706  break;
1707  default:
1708  prev = 0;
1709  }
1710 
1711  prev = av_clip_intp2((prev + 0x20) >> 6, 21);
1712 
1713  byte = bytestream2_get_byteu(&gb);
1714  if (!channel)
1715  sampledat = sign_extend(byte, 4);
1716  else
1717  sampledat = sign_extend(byte >> 4, 4);
1718 
1719  sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
1720  *samples++ = av_clip_int16(sampledat >> 6);
1722  c->status[channel].sample1 = sampledat;
1723  }
1724  }
1725  if (!channel)
1726  bytestream2_seek(&gb, 0, SEEK_SET);
1727  }
1728  break;
1729  case AV_CODEC_ID_ADPCM_PSX:
1730  for (channel = 0; channel < avctx->channels; channel++) {
1731  samples = samples_p[channel];
1732 
1733  /* Read in every sample for this channel. */
1734  for (i = 0; i < nb_samples / 28; i++) {
1735  int filter, shift, flag, byte;
1736 
1737  filter = bytestream2_get_byteu(&gb);
1738  shift = filter & 0xf;
1739  filter = filter >> 4;
1740  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
1741  return AVERROR_INVALIDDATA;
1742  flag = bytestream2_get_byteu(&gb);
1743 
1744  /* Decode 28 samples. */
1745  for (n = 0; n < 28; n++) {
1746  int sample = 0, scale;
1747 
1748  if (flag < 0x07) {
1749  if (n & 1) {
1750  scale = sign_extend(byte >> 4, 4);
1751  } else {
1752  byte = bytestream2_get_byteu(&gb);
1753  scale = sign_extend(byte, 4);
1754  }
1755 
1756  scale = scale << 12;
1757  sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
1758  }
1759  *samples++ = av_clip_int16(sample);
1761  c->status[channel].sample1 = sample;
1762  }
1763  }
1764  }
1765  break;
1766 
1767  default:
1768  av_assert0(0); // unsupported codec_id should not happen
1769  }
1770 
1771  if (avpkt->size && bytestream2_tell(&gb) == 0) {
1772  av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
1773  return AVERROR_INVALIDDATA;
1774  }
1775 
1776  *got_frame_ptr = 1;
1777 
1778  if (avpkt->size < bytestream2_tell(&gb)) {
1779  av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
1780  return avpkt->size;
1781  }
1782 
1783  return bytestream2_tell(&gb);
1784 }
1785 
1786 static void adpcm_flush(AVCodecContext *avctx)
1787 {
1788  ADPCMDecodeContext *c = avctx->priv_data;
1789  c->has_status = 0;
1790 }
1791 
1792 
1800 
1801 #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
1802 AVCodec ff_ ## name_ ## _decoder = { \
1803  .name = #name_, \
1804  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1805  .type = AVMEDIA_TYPE_AUDIO, \
1806  .id = id_, \
1807  .priv_data_size = sizeof(ADPCMDecodeContext), \
1808  .init = adpcm_decode_init, \
1809  .decode = adpcm_decode_frame, \
1810  .flush = adpcm_flush, \
1811  .capabilities = AV_CODEC_CAP_DR1, \
1812  .sample_fmts = sample_fmts_, \
1813 }
1814 
1815 /* Note: Do not forget to add new entries to the Makefile as well. */
1816 ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
1817 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
1818 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM, sample_fmts_s16, adpcm_agm, "ADPCM AmuseGraphics Movie");
1819 ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
1820 ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
1821 ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
1822 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
1823 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
1824 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
1825 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
1826 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
1827 ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
1828 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
1829 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
1830 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
1831 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
1832 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1833 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1834 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1835 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1836 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
1837 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
1838 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
1839 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1840 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
1841 ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
1842 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_both, adpcm_ms, "ADPCM Microsoft");
1843 ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF, sample_fmts_s16p, adpcm_mtaf, "ADPCM MTAF");
1844 ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
1845 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1846 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1847 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1848 ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
1849 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
1850 ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
1851 ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
1852 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:1577
const char const char void * val
Definition: avisynth_c.h:863
static const int16_t ea_adpcm_table[]
Definition: adpcm.c:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
static int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition: adpcm.c:399
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
#define THP_GET16(g)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#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:1481
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 &&1) 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
static enum AVSampleFormat sample_fmts_s16[]
Definition: adpcm.c:1793
#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:2265
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:359
static int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition: adpcm.c:304
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
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:2236
uint8_t
#define av_cold
Definition: attributes.h:82
static av_cold int adpcm_decode_init(AVCodecContext *avctx)
Definition: adpcm.c:92
float delta
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
static void adpcm_flush(AVCodecContext *avctx)
Definition: adpcm.c:1786
static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
Definition: adpcm.c:488
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1669
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:252
ADPCM tables.
uint8_t * data
Definition: avcodec.h:1480
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:556
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:100
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:2792
#define av_log(a,...)
static const uint16_t table[]
Definition: prosumer.c:206
enum AVCodecID id
Definition: avcodec.h:3498
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
static const int8_t xa_adpcm_table[5][2]
Definition: adpcm.c:60
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
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
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 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:278
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
GLsizei count
Definition: opengl_enc.c:108
static int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition: adpcm.c:323
#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:255
int8_t exp
Definition: eval.c:72
const int8_t ff_adpcm_index_table[16]
Definition: adpcm_data.c:40
static const int8_t swf_index_tables[4][16]
Definition: adpcm.c:77
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:408
#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:1795
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
int n
Definition: avisynth_c.h:760
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 uint8_t ff_adpcm_ima_block_samples[4]
Definition: adpcm_data.h:32
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
const int16_t ff_adpcm_AdaptationTable[]
Definition: adpcm_data.c:84
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:87
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
main external API structure.
Definition: avcodec.h:1568
static int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition: adpcm.c:385
#define DK3_GET_NEXT_NIBBLE()
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1963
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2]...the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so...,+,-,+,-,+,+,-,+,-,+,...hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32-hcoeff[1]-hcoeff[2]-...a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2}an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||.........intra?||||:Block01:yes no||||:Block02:.................||||:Block03::y DC::ref index:||||:Block04::cb DC::motion x:||||.........:cr DC::motion y:||||.................|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------------------------------|||Y subbands||Cb subbands||Cr subbands||||------||------||------|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||------||------||------||||------||------||------|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||------||------||------||||------||------||------|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||------||------||------||||------||------||------|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------------------------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction------------|\Dequantization-------------------\||Reference frames|\IDWT|--------------|Motion\|||Frame 0||Frame 1||Compensation.OBMC v-------|--------------|--------------.\------> Frame n output Frame Frame<----------------------------------/|...|-------------------Range Coder:============Binary Range Coder:-------------------The implemented range coder is an adapted version based upon"Range encoding: an algorithm for removing redundancy from a digitised message."by G.N.N.Martin.The symbols encoded by the Snow range coder are bits(0|1).The associated probabilities are not fix but change depending on the symbol mix seen so far.bit seen|new state---------+-----------------------------------------------0|256-state_transition_table[256-old_state];1|state_transition_table[old_state];state_transition_table={0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:-------------------------FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1.the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:206
static int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
Definition: adpcm.c:229
void * buf
Definition: avisynth_c.h:766
int extradata_size
Definition: avcodec.h:1670
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)\n", 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:659
static int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition: adpcm.c:185
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:365
static unsigned int get_bits_le(GetBitContext *s, int n)
Definition: get_bits.h:420
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
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:758
signed 16 bits
Definition: samplefmt.h:61
#define flag(name)
Definition: cbs_av1.c:553
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
unsigned bps
Definition: movenc.c:1532
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:793
void * priv_data
Definition: avcodec.h:1595
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:664
int channels
number of audio channels
Definition: avcodec.h:2229
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:344
static enum AVSampleFormat sample_fmts_both[]
Definition: adpcm.c:1797
Filter the word “frame” indicates either a video frame or a group of audio samples
int16_t step_index
Definition: adpcm.h:35
signed 16 bits, planar
Definition: samplefmt.h:67
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:342
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1457
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:361
for(j=16;j >0;--j)
#define tb
Definition: regdef.h:68
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
#define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_)
Definition: adpcm.c:1801