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