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  * Argonaut Games ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
16  * Simon & Schuster Interactive ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
17  * Ubisoft ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
18  * High Voltage Software ALP decoder by Zane van Iperen (zane@zanevaniperen.com)
19  * Cunning Developments decoder by Zane van Iperen (zane@zanevaniperen.com)
20  * Sanyo LD-ADPCM decoder by Peter Ross (pross@xvid.org)
21  *
22  * This file is part of FFmpeg.
23  *
24  * FFmpeg is free software; you can redistribute it and/or
25  * modify it under the terms of the GNU Lesser General Public
26  * License as published by the Free Software Foundation; either
27  * version 2.1 of the License, or (at your option) any later version.
28  *
29  * FFmpeg is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32  * Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public
35  * License along with FFmpeg; if not, write to the Free Software
36  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37  */
38 
39 #include "config_components.h"
40 
41 #include "avcodec.h"
42 #include "get_bits.h"
43 #include "bytestream.h"
44 #include "adpcm.h"
45 #include "adpcm_data.h"
46 #include "codec_internal.h"
47 #include "decode.h"
48 
49 #include "libavutil/attributes.h"
50 
51 /**
52  * @file
53  * ADPCM decoders
54  * Features and limitations:
55  *
56  * Reference documents:
57  * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
58  * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
59  * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
60  * http://openquicktime.sourceforge.net/
61  * XAnim sources (xa_codec.c) http://xanim.polter.net/
62  * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
63  * SoX source code http://sox.sourceforge.net/
64  *
65  * CD-ROM XA:
66  * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
67  * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
68  * readstr http://www.geocities.co.jp/Playtown/2004/
69  */
70 
71 #define CASE_0(codec_id, ...)
72 #define CASE_1(codec_id, ...) \
73  case codec_id: \
74  { __VA_ARGS__ } \
75  break;
76 #define CASE_2(enabled, codec_id, ...) \
77  CASE_ ## enabled(codec_id, __VA_ARGS__)
78 #define CASE_3(config, codec_id, ...) \
79  CASE_2(config, codec_id, __VA_ARGS__)
80 #define CASE(codec, ...) \
81  CASE_3(CONFIG_ ## codec ## _DECODER, AV_CODEC_ID_ ## codec, __VA_ARGS__)
82 
83 /* These are for CD-ROM XA ADPCM */
84 static const int8_t xa_adpcm_table[5][2] = {
85  { 0, 0 },
86  { 60, 0 },
87  { 115, -52 },
88  { 98, -55 },
89  { 122, -60 }
90 };
91 
92 static const int16_t afc_coeffs[2][16] = {
93  { 0, 2048, 0, 1024, 4096, 3584, 3072, 4608, 4200, 4800, 5120, 2048, 1024, -1024, -1024, -2048 },
94  { 0, 0, 2048, 1024, -2048, -1536, -1024, -2560, -2248, -2300, -3072, -2048, -1024, 1024, 0, 0 }
95 };
96 
97 static const int16_t ea_adpcm_table[] = {
98  0, 240, 460, 392,
99  0, 0, -208, -220,
100  0, 1, 3, 4,
101  7, 8, 10, 11,
102  0, -1, -3, -4
103 };
104 
105 /*
106  * Dumped from the binaries:
107  * - FantasticJourney.exe - 0x794D2, DGROUP:0x47A4D2
108  * - BigRaceUSA.exe - 0x9B8AA, DGROUP:0x49C4AA
109  * - Timeshock!.exe - 0x8506A, DGROUP:0x485C6A
110  */
111 static const int8_t ima_cunning_index_table[9] = {
112  -1, -1, -1, -1, 1, 2, 3, 4, -1
113 };
114 
115 /*
116  * Dumped from the binaries:
117  * - FantasticJourney.exe - 0x79458, DGROUP:0x47A458
118  * - BigRaceUSA.exe - 0x9B830, DGROUP:0x49C430
119  * - Timeshock!.exe - 0x84FF0, DGROUP:0x485BF0
120  */
121 static const int16_t ima_cunning_step_table[61] = {
122  1, 1, 1, 1, 2, 2, 3, 3, 4, 5,
123  6, 7, 8, 10, 12, 14, 16, 20, 24, 28,
124  32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
125  192, 224, 256, 320, 384, 448, 512, 640, 768, 896,
126  1024, 1280, 1536, 1792, 2048, 2560, 3072, 3584, 4096, 5120,
127  6144, 7168, 8192, 10240, 12288, 14336, 16384, 20480, 24576, 28672, 0
128 };
129 
130 static const int8_t adpcm_index_table2[4] = {
131  -1, 2,
132  -1, 2,
133 };
134 
135 static const int8_t adpcm_index_table3[8] = {
136  -1, -1, 1, 2,
137  -1, -1, 1, 2,
138 };
139 
140 static const int8_t adpcm_index_table5[32] = {
141  -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
142  -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
143 };
144 
145 static const int8_t * const adpcm_index_tables[4] = {
146  &adpcm_index_table2[0],
147  &adpcm_index_table3[0],
149  &adpcm_index_table5[0],
150 };
151 
152 static const int16_t mtaf_stepsize[32][16] = {
153  { 1, 5, 9, 13, 16, 20, 24, 28,
154  -1, -5, -9, -13, -16, -20, -24, -28, },
155  { 2, 6, 11, 15, 20, 24, 29, 33,
156  -2, -6, -11, -15, -20, -24, -29, -33, },
157  { 2, 7, 13, 18, 23, 28, 34, 39,
158  -2, -7, -13, -18, -23, -28, -34, -39, },
159  { 3, 9, 15, 21, 28, 34, 40, 46,
160  -3, -9, -15, -21, -28, -34, -40, -46, },
161  { 3, 11, 18, 26, 33, 41, 48, 56,
162  -3, -11, -18, -26, -33, -41, -48, -56, },
163  { 4, 13, 22, 31, 40, 49, 58, 67,
164  -4, -13, -22, -31, -40, -49, -58, -67, },
165  { 5, 16, 26, 37, 48, 59, 69, 80,
166  -5, -16, -26, -37, -48, -59, -69, -80, },
167  { 6, 19, 31, 44, 57, 70, 82, 95,
168  -6, -19, -31, -44, -57, -70, -82, -95, },
169  { 7, 22, 38, 53, 68, 83, 99, 114,
170  -7, -22, -38, -53, -68, -83, -99, -114, },
171  { 9, 27, 45, 63, 81, 99, 117, 135,
172  -9, -27, -45, -63, -81, -99, -117, -135, },
173  { 10, 32, 53, 75, 96, 118, 139, 161,
174  -10, -32, -53, -75, -96, -118, -139, -161, },
175  { 12, 38, 64, 90, 115, 141, 167, 193,
176  -12, -38, -64, -90, -115, -141, -167, -193, },
177  { 15, 45, 76, 106, 137, 167, 198, 228,
178  -15, -45, -76, -106, -137, -167, -198, -228, },
179  { 18, 54, 91, 127, 164, 200, 237, 273,
180  -18, -54, -91, -127, -164, -200, -237, -273, },
181  { 21, 65, 108, 152, 195, 239, 282, 326,
182  -21, -65, -108, -152, -195, -239, -282, -326, },
183  { 25, 77, 129, 181, 232, 284, 336, 388,
184  -25, -77, -129, -181, -232, -284, -336, -388, },
185  { 30, 92, 153, 215, 276, 338, 399, 461,
186  -30, -92, -153, -215, -276, -338, -399, -461, },
187  { 36, 109, 183, 256, 329, 402, 476, 549,
188  -36, -109, -183, -256, -329, -402, -476, -549, },
189  { 43, 130, 218, 305, 392, 479, 567, 654,
190  -43, -130, -218, -305, -392, -479, -567, -654, },
191  { 52, 156, 260, 364, 468, 572, 676, 780,
192  -52, -156, -260, -364, -468, -572, -676, -780, },
193  { 62, 186, 310, 434, 558, 682, 806, 930,
194  -62, -186, -310, -434, -558, -682, -806, -930, },
195  { 73, 221, 368, 516, 663, 811, 958, 1106,
196  -73, -221, -368, -516, -663, -811, -958, -1106, },
197  { 87, 263, 439, 615, 790, 966, 1142, 1318,
198  -87, -263, -439, -615, -790, -966, -1142, -1318, },
199  { 104, 314, 523, 733, 942, 1152, 1361, 1571,
200  -104, -314, -523, -733, -942, -1152, -1361, -1571, },
201  { 124, 374, 623, 873, 1122, 1372, 1621, 1871,
202  -124, -374, -623, -873, -1122, -1372, -1621, -1871, },
203  { 148, 445, 743, 1040, 1337, 1634, 1932, 2229,
204  -148, -445, -743, -1040, -1337, -1634, -1932, -2229, },
205  { 177, 531, 885, 1239, 1593, 1947, 2301, 2655,
206  -177, -531, -885, -1239, -1593, -1947, -2301, -2655, },
207  { 210, 632, 1053, 1475, 1896, 2318, 2739, 3161,
208  -210, -632, -1053, -1475, -1896, -2318, -2739, -3161, },
209  { 251, 753, 1255, 1757, 2260, 2762, 3264, 3766,
210  -251, -753, -1255, -1757, -2260, -2762, -3264, -3766, },
211  { 299, 897, 1495, 2093, 2692, 3290, 3888, 4486,
212  -299, -897, -1495, -2093, -2692, -3290, -3888, -4486, },
213  { 356, 1068, 1781, 2493, 3206, 3918, 4631, 5343,
214  -356, -1068, -1781, -2493, -3206, -3918, -4631, -5343, },
215  { 424, 1273, 2121, 2970, 3819, 4668, 5516, 6365,
216  -424, -1273, -2121, -2970, -3819, -4668, -5516, -6365, },
217 };
218 
219 static const int16_t oki_step_table[49] = {
220  16, 17, 19, 21, 23, 25, 28, 31, 34, 37,
221  41, 45, 50, 55, 60, 66, 73, 80, 88, 97,
222  107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
223  279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
224  724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552
225 };
226 
227 // padded to zero where table size is less then 16
228 static const int8_t swf_index_tables[4][16] = {
229  /*2*/ { -1, 2 },
230  /*3*/ { -1, -1, 2, 4 },
231  /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
232  /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
233 };
234 
235 static const int8_t zork_index_table[8] = {
236  -1, -1, -1, 1, 4, 7, 10, 12,
237 };
238 
239 static const int8_t mtf_index_table[16] = {
240  8, 6, 4, 2, -1, -1, -1, -1,
241  -1, -1, -1, -1, 2, 4, 6, 8,
242 };
243 
244 /* end of tables */
245 
246 typedef struct ADPCMDecodeContext {
248  int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
249  int has_status; /**< Status flag. Reset to 0 after a flush. */
251 
252 static void adpcm_flush(AVCodecContext *avctx);
253 
255 {
256  ADPCMDecodeContext *c = avctx->priv_data;
257  unsigned int min_channels = 1;
258  unsigned int max_channels = 2;
259 
260  adpcm_flush(avctx);
261 
262  switch(avctx->codec->id) {
264  max_channels = 1;
265  break;
267  max_channels = 2;
268  break;
275  max_channels = 6;
276  break;
278  min_channels = 2;
279  max_channels = 8;
280  if (avctx->ch_layout.nb_channels & 1) {
281  avpriv_request_sample(avctx, "channel count %d", avctx->ch_layout.nb_channels);
282  return AVERROR_PATCHWELCOME;
283  }
284  break;
286  min_channels = 2;
287  break;
289  max_channels = 8;
290  if (avctx->ch_layout.nb_channels <= 0 ||
291  avctx->block_align % (16 * avctx->ch_layout.nb_channels))
292  return AVERROR_INVALIDDATA;
293  break;
297  max_channels = 14;
298  break;
299  }
300  if (avctx->ch_layout.nb_channels < min_channels ||
301  avctx->ch_layout.nb_channels > max_channels) {
302  av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
303  return AVERROR(EINVAL);
304  }
305 
306  switch(avctx->codec->id) {
308  if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
309  return AVERROR_INVALIDDATA;
310  break;
312  if (avctx->bits_per_coded_sample != 4 ||
313  avctx->block_align != 17 * avctx->ch_layout.nb_channels)
314  return AVERROR_INVALIDDATA;
315  break;
317  if (avctx->bits_per_coded_sample < 3 || avctx->bits_per_coded_sample > 5)
318  return AVERROR_INVALIDDATA;
319  break;
321  if (avctx->bits_per_coded_sample != 4)
322  return AVERROR_INVALIDDATA;
323  break;
325  if (avctx->bits_per_coded_sample != 8)
326  return AVERROR_INVALIDDATA;
327  break;
328  default:
329  break;
330  }
331 
332  switch (avctx->codec->id) {
356  break;
358  avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
360  break;
362  avctx->sample_fmt = avctx->ch_layout.nb_channels > 2 ? AV_SAMPLE_FMT_S16P :
364  break;
365  default:
366  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
367  }
368  return 0;
369 }
370 
371 static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
372 {
373  int delta, pred, step, add;
374 
375  pred = c->predictor;
376  delta = nibble & 7;
377  step = c->step;
378  add = (delta * 2 + 1) * step;
379  if (add < 0)
380  add = add + 7;
381 
382  if ((nibble & 8) == 0)
383  pred = av_clip(pred + (add >> 3), -32767, 32767);
384  else
385  pred = av_clip(pred - (add >> 3), -32767, 32767);
386 
387  switch (delta) {
388  case 7:
389  step *= 0x99;
390  break;
391  case 6:
392  c->step = av_clip(c->step * 2, 127, 24576);
393  c->predictor = pred;
394  return pred;
395  case 5:
396  step *= 0x66;
397  break;
398  case 4:
399  step *= 0x4d;
400  break;
401  default:
402  step *= 0x39;
403  break;
404  }
405 
406  if (step < 0)
407  step += 0x3f;
408 
409  c->step = step >> 6;
410  c->step = av_clip(c->step, 127, 24576);
411  c->predictor = pred;
412  return pred;
413 }
414 
415 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
416 {
417  int step_index;
418  int predictor;
419  int sign, delta, diff, step;
420 
421  step = ff_adpcm_step_table[c->step_index];
422  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
423  step_index = av_clip(step_index, 0, 88);
424 
425  sign = nibble & 8;
426  delta = nibble & 7;
427  /* perform direct multiplication instead of series of jumps proposed by
428  * the reference ADPCM implementation since modern CPUs can do the mults
429  * quickly enough */
430  diff = ((2 * delta + 1) * step) >> shift;
431  predictor = c->predictor;
432  if (sign) predictor -= diff;
433  else predictor += diff;
434 
435  c->predictor = av_clip_int16(predictor);
436  c->step_index = step_index;
437 
438  return (int16_t)c->predictor;
439 }
440 
441 static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
442 {
443  int step_index;
444  int predictor;
445  int sign, delta, diff, step;
446 
447  step = ff_adpcm_step_table[c->step_index];
448  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
449  step_index = av_clip(step_index, 0, 88);
450 
451  sign = nibble & 8;
452  delta = nibble & 7;
453  diff = (delta * step) >> shift;
454  predictor = c->predictor;
455  if (sign) predictor -= diff;
456  else predictor += diff;
457 
458  c->predictor = av_clip_int16(predictor);
459  c->step_index = step_index;
460 
461  return (int16_t)c->predictor;
462 }
463 
464 static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
465 {
466  int step_index, step, delta, predictor;
467 
468  step = ff_adpcm_step_table[c->step_index];
469 
470  delta = step * (2 * nibble - 15);
471  predictor = c->predictor + delta;
472 
473  step_index = c->step_index + mtf_index_table[(unsigned)nibble];
474  c->predictor = av_clip_int16(predictor >> 4);
475  c->step_index = av_clip(step_index, 0, 88);
476 
477  return (int16_t)c->predictor;
478 }
479 
480 static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
481 {
482  int step_index;
483  int predictor;
484  int step;
485 
486  nibble = sign_extend(nibble & 0xF, 4);
487 
488  step = ima_cunning_step_table[c->step_index];
489  step_index = c->step_index + ima_cunning_index_table[abs(nibble)];
490  step_index = av_clip(step_index, 0, 60);
491 
492  predictor = c->predictor + step * nibble;
493 
494  c->predictor = av_clip_int16(predictor);
495  c->step_index = step_index;
496 
497  return c->predictor;
498 }
499 
501 {
502  int nibble, step_index, predictor, sign, delta, diff, step, shift;
503 
504  shift = bps - 1;
505  nibble = get_bits_le(gb, bps),
506  step = ff_adpcm_step_table[c->step_index];
507  step_index = c->step_index + adpcm_index_tables[bps - 2][nibble];
508  step_index = av_clip(step_index, 0, 88);
509 
510  sign = nibble & (1 << shift);
511  delta = av_zero_extend(nibble, shift);
512  diff = ((2 * delta + 1) * step) >> shift;
513  predictor = c->predictor;
514  if (sign) predictor -= diff;
515  else predictor += diff;
516 
517  c->predictor = av_clip_int16(predictor);
518  c->step_index = step_index;
519 
520  return (int16_t)c->predictor;
521 }
522 
524 {
525  int step_index;
526  int predictor;
527  int diff, step;
528 
529  step = ff_adpcm_step_table[c->step_index];
530  step_index = c->step_index + ff_adpcm_index_table[nibble];
531  step_index = av_clip(step_index, 0, 88);
532 
533  diff = step >> 3;
534  if (nibble & 4) diff += step;
535  if (nibble & 2) diff += step >> 1;
536  if (nibble & 1) diff += step >> 2;
537 
538  if (nibble & 8)
539  predictor = c->predictor - diff;
540  else
541  predictor = c->predictor + diff;
542 
543  c->predictor = av_clip_int16(predictor);
544  c->step_index = step_index;
545 
546  return c->predictor;
547 }
548 
549 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
550 {
551  int predictor;
552 
553  predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
554  predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
555 
556  c->sample2 = c->sample1;
557  c->sample1 = av_clip_int16(predictor);
558  c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
559  if (c->idelta < 16) c->idelta = 16;
560  if (c->idelta > INT_MAX/768) {
561  av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
562  c->idelta = INT_MAX/768;
563  }
564 
565  return c->sample1;
566 }
567 
568 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
569 {
570  int step_index, predictor, sign, delta, diff, step;
571 
572  step = oki_step_table[c->step_index];
573  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
574  step_index = av_clip(step_index, 0, 48);
575 
576  sign = nibble & 8;
577  delta = nibble & 7;
578  diff = ((2 * delta + 1) * step) >> 3;
579  predictor = c->predictor;
580  if (sign) predictor -= diff;
581  else predictor += diff;
582 
583  c->predictor = av_clip_intp2(predictor, 11);
584  c->step_index = step_index;
585 
586  return c->predictor * 16;
587 }
588 
589 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
590 {
591  int sign, delta, diff;
592  int new_step;
593 
594  sign = nibble & 8;
595  delta = nibble & 7;
596  /* perform direct multiplication instead of series of jumps proposed by
597  * the reference ADPCM implementation since modern CPUs can do the mults
598  * quickly enough */
599  diff = ((2 * delta + 1) * c->step) >> 3;
600  /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
601  c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
602  c->predictor = av_clip_int16(c->predictor);
603  /* calculate new step and clamp it to range 511..32767 */
604  new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
605  c->step = av_clip(new_step, 511, 32767);
606 
607  return (int16_t)c->predictor;
608 }
609 
610 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
611 {
612  int sign, delta, diff;
613 
614  sign = nibble & (1<<(size-1));
615  delta = nibble & ((1<<(size-1))-1);
616  diff = delta << (7 + c->step + shift);
617 
618  /* clamp result */
619  c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
620 
621  /* calculate new step */
622  if (delta >= (2*size - 3) && c->step < 3)
623  c->step++;
624  else if (delta == 0 && c->step > 0)
625  c->step--;
626 
627  return (int16_t) c->predictor;
628 }
629 
630 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
631 {
632  if(!c->step) {
633  c->predictor = 0;
634  c->step = 127;
635  }
636 
637  c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
638  c->predictor = av_clip_int16(c->predictor);
639  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
640  c->step = av_clip(c->step, 127, 24576);
641  return c->predictor;
642 }
643 
644 static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
645 {
646  c->predictor += mtaf_stepsize[c->step][nibble];
647  c->predictor = av_clip_int16(c->predictor);
648  c->step += ff_adpcm_index_table[nibble];
649  c->step = av_clip_uintp2(c->step, 5);
650  return c->predictor;
651 }
652 
653 static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
654 {
655  int16_t index = c->step_index;
656  uint32_t lookup_sample = ff_adpcm_step_table[index];
657  int32_t sample = 0;
658 
659  if (nibble & 0x40)
660  sample += lookup_sample;
661  if (nibble & 0x20)
662  sample += lookup_sample >> 1;
663  if (nibble & 0x10)
664  sample += lookup_sample >> 2;
665  if (nibble & 0x08)
666  sample += lookup_sample >> 3;
667  if (nibble & 0x04)
668  sample += lookup_sample >> 4;
669  if (nibble & 0x02)
670  sample += lookup_sample >> 5;
671  if (nibble & 0x01)
672  sample += lookup_sample >> 6;
673  if (nibble & 0x80)
674  sample = -sample;
675 
676  sample += c->predictor;
678 
679  index += zork_index_table[(nibble >> 4) & 7];
680  index = av_clip(index, 0, 88);
681 
682  c->predictor = sample;
683  c->step_index = index;
684 
685  return sample;
686 }
687 
688 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
689  const uint8_t *in, ADPCMChannelStatus *left,
690  ADPCMChannelStatus *right, int channels, int sample_offset)
691 {
692  int i, j;
693  int shift,filter,f0,f1;
694  int s_1,s_2;
695  int d,s,t;
696 
697  out0 += sample_offset;
698  if (channels == 1)
699  out1 = out0 + 28;
700  else
701  out1 += sample_offset;
702 
703  for(i=0;i<4;i++) {
704  shift = 12 - (in[4+i*2] & 15);
705  filter = in[4+i*2] >> 4;
707  avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
708  filter=0;
709  }
710  if (shift < 0) {
711  avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
712  shift = 0;
713  }
714  f0 = xa_adpcm_table[filter][0];
715  f1 = xa_adpcm_table[filter][1];
716 
717  s_1 = left->sample1;
718  s_2 = left->sample2;
719 
720  for(j=0;j<28;j++) {
721  d = in[16+i+j*4];
722 
723  t = sign_extend(d, 4);
724  s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
725  s_2 = s_1;
726  s_1 = av_clip_int16(s);
727  out0[j] = s_1;
728  }
729 
730  if (channels == 2) {
731  left->sample1 = s_1;
732  left->sample2 = s_2;
733  s_1 = right->sample1;
734  s_2 = right->sample2;
735  }
736 
737  shift = 12 - (in[5+i*2] & 15);
738  filter = in[5+i*2] >> 4;
739  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
740  avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
741  filter=0;
742  }
743  if (shift < 0) {
744  avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
745  shift = 0;
746  }
747 
748  f0 = xa_adpcm_table[filter][0];
749  f1 = xa_adpcm_table[filter][1];
750 
751  for(j=0;j<28;j++) {
752  d = in[16+i+j*4];
753 
754  t = sign_extend(d >> 4, 4);
755  s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
756  s_2 = s_1;
757  s_1 = av_clip_int16(s);
758  out1[j] = s_1;
759  }
760 
761  if (channels == 2) {
762  right->sample1 = s_1;
763  right->sample2 = s_2;
764  } else {
765  left->sample1 = s_1;
766  left->sample2 = s_2;
767  }
768 
769  out0 += 28 * (3 - channels);
770  out1 += 28 * (3 - channels);
771  }
772 
773  return 0;
774 }
775 
776 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
777 {
778  ADPCMDecodeContext *c = avctx->priv_data;
779  GetBitContext gb;
780  const int8_t *table;
781  int channels = avctx->ch_layout.nb_channels;
782  int k0, signmask, nb_bits, count;
783  int size = buf_size*8;
784  int i;
785 
786  init_get_bits(&gb, buf, size);
787 
788  //read bits & initial values
789  nb_bits = get_bits(&gb, 2)+2;
790  table = swf_index_tables[nb_bits-2];
791  k0 = 1 << (nb_bits-2);
792  signmask = 1 << (nb_bits-1);
793 
794  while (get_bits_count(&gb) <= size - 22 * channels) {
795  for (i = 0; i < channels; i++) {
796  *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
797  c->status[i].step_index = get_bits(&gb, 6);
798  }
799 
800  for (count = 0; get_bits_count(&gb) <= size - nb_bits * channels && count < 4095; count++) {
801  int i;
802 
803  for (i = 0; i < channels; i++) {
804  // similar to IMA adpcm
805  int delta = get_bits(&gb, nb_bits);
806  int step = ff_adpcm_step_table[c->status[i].step_index];
807  int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
808  int k = k0;
809 
810  do {
811  if (delta & k)
812  vpdiff += step;
813  step >>= 1;
814  k >>= 1;
815  } while(k);
816  vpdiff += step;
817 
818  if (delta & signmask)
819  c->status[i].predictor -= vpdiff;
820  else
821  c->status[i].predictor += vpdiff;
822 
823  c->status[i].step_index += table[delta & (~signmask)];
824 
825  c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
826  c->status[i].predictor = av_clip_int16(c->status[i].predictor);
827 
828  *samples++ = c->status[i].predictor;
829  }
830  }
831  }
832 }
833 
834 int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
835 {
836  int sample = sign_extend(nibble, 4) * (1 << shift);
837 
838  if (flag)
839  sample += (8 * cs->sample1) - (4 * cs->sample2);
840  else
841  sample += 4 * cs->sample1;
842 
843  sample = av_clip_int16(sample >> 2);
844 
845  cs->sample2 = cs->sample1;
846  cs->sample1 = sample;
847 
848  return sample;
849 }
850 
852 {
853  int sign, delta, add;
854 
855  sign = bits & 4;
856  if (sign)
857  delta = 4 - (bits & 3);
858  else
859  delta = bits;
860 
861  switch (delta) {
862  case 0:
863  add = 0;
864  c->step = (3 * c->step) >> 2;
865  break;
866  case 1:
867  add = c->step;
868  c->step = (4 * c->step - (c->step >> 1)) >> 2;
869  break;
870  case 2:
871  add = 2 * c->step;
872  c->step = ((c->step >> 1) + add) >> 1;
873  break;
874  case 3:
875  add = 4 * c->step - (c->step >> 1);
876  c->step = 2 * c->step;
877  break;
878  case 4:
879  add = (11 * c->step) >> 1;
880  c->step = 3 * c->step;
881  break;
882  default:
883  av_unreachable("There are cases for all control paths when bits is 3-bit");
884  }
885 
886  if (sign)
887  add = -add;
888 
889  c->predictor = av_clip_int16(c->predictor + add);
890  c->step = av_clip(c->step, 1, 7281);
891  return c->predictor;
892 }
893 
895 {
896  int sign, delta, add;
897 
898  sign = bits & 8;
899  if (sign)
900  delta = 8 - (bits & 7);
901  else
902  delta = bits;
903 
904  switch (delta) {
905  case 0:
906  add = 0;
907  c->step = (3 * c->step) >> 2;
908  break;
909  case 1:
910  add = c->step;
911  c->step = (3 * c->step) >> 2;
912  break;
913  case 2:
914  add = 2 * c->step;
915  break;
916  case 3:
917  add = 3 * c->step;
918  break;
919  case 4:
920  add = 4 * c->step;
921  break;
922  case 5:
923  add = (11 * c->step) >> 1;
924  c->step += c->step >> 2;
925  break;
926  case 6:
927  add = (15 * c->step) >> 1;
928  c->step = 2 * c->step;
929  break;
930  case 7:
931  if (sign)
932  add = (19 * c->step) >> 1;
933  else
934  add = (21 * c->step) >> 1;
935  c->step = (c->step >> 1) + 2 * c->step;
936  break;
937  case 8:
938  add = (25 * c->step) >> 1;
939  c->step = 5 * c->step;
940  break;
941  default:
942  av_unreachable("There are cases for all control paths when bits is 4-bit");
943  }
944 
945  if (sign)
946  add = -add;
947 
948  c->predictor = av_clip_int16(c->predictor + add);
949  c->step = av_clip(c->step, 1, 2621);
950  return c->predictor;
951 }
952 
954 {
955  int sign, delta, add;
956 
957  sign = bits & 0x10;
958  if (sign)
959  delta = 16 - (bits & 0xF);
960  else
961  delta = bits;
962 
963  add = delta * c->step;
964  switch (delta) {
965  case 0:
966  c->step += (c->step >> 2) - (c->step >> 1);
967  break;
968  case 1:
969  case 2:
970  case 3:
971  c->step += (c->step >> 3) - (c->step >> 2);
972  break;
973  case 4:
974  case 5:
975  c->step += (c->step >> 4) - (c->step >> 3);
976  break;
977  case 6:
978  break;
979  case 7:
980  c->step += c->step >> 3;
981  break;
982  case 8:
983  c->step += c->step >> 2;
984  break;
985  case 9:
986  c->step += c->step >> 1;
987  break;
988  case 10:
989  c->step = 2 * c->step - (c->step >> 3);
990  break;
991  case 11:
992  c->step = 2 * c->step + (c->step >> 3);
993  break;
994  case 12:
995  c->step = 2 * c->step + (c->step >> 1) - (c->step >> 3);
996  break;
997  case 13:
998  c->step = 3 * c->step - (c->step >> 2);
999  break;
1000  case 14:
1001  c->step *= 3;
1002  break;
1003  case 15:
1004  case 16:
1005  c->step = (7 * c->step) >> 1;
1006  break;
1007  }
1008 
1009  if (sign)
1010  add = -add;
1011 
1012  c->predictor = av_clip_int16(c->predictor + add);
1013  c->step = av_clip(c->step, 1, 1024);
1014  return c->predictor;
1015 }
1016 
1017 /**
1018  * Get the number of samples (per channel) that will be decoded from the packet.
1019  * In one case, this is actually the maximum number of samples possible to
1020  * decode with the given buf_size.
1021  *
1022  * @param[out] coded_samples set to the number of samples as coded in the
1023  * packet, or 0 if the codec does not encode the
1024  * number of samples in each frame.
1025  * @param[out] approx_nb_samples set to non-zero if the number of samples
1026  * returned is an approximation.
1027  */
1029  int buf_size, int *coded_samples, int *approx_nb_samples)
1030 {
1031  ADPCMDecodeContext *s = avctx->priv_data;
1032  int nb_samples = 0;
1033  int ch = avctx->ch_layout.nb_channels;
1034  int has_coded_samples = 0;
1035  int header_size;
1036 
1037  *coded_samples = 0;
1038  *approx_nb_samples = 0;
1039 
1040  if(ch <= 0)
1041  return 0;
1042 
1043  switch (avctx->codec->id) {
1044  /* constant, only check buf_size */
1046  if (buf_size < 76 * ch)
1047  return 0;
1048  nb_samples = 128;
1049  break;
1051  if (buf_size < 34 * ch)
1052  return 0;
1053  nb_samples = 64;
1054  break;
1055  /* simple 4-bit adpcm */
1056  case AV_CODEC_ID_ADPCM_CT:
1068  nb_samples = buf_size * 2 / ch;
1069  break;
1070  }
1071  if (nb_samples)
1072  return nb_samples;
1073 
1074  /* simple 4-bit adpcm, with header */
1075  header_size = 0;
1076  switch (avctx->codec->id) {
1077  case AV_CODEC_ID_ADPCM_4XM:
1078  case AV_CODEC_ID_ADPCM_AGM:
1082  case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
1083  case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
1084  }
1085  if (header_size > 0)
1086  return (buf_size - header_size) * 2 / ch;
1087 
1088  /* more complex formats */
1089  switch (avctx->codec->id) {
1091  bytestream2_skip(gb, 4);
1092  has_coded_samples = 1;
1093  *coded_samples = bytestream2_get_le32u(gb);
1094  nb_samples = FFMIN((buf_size - 8) * 2, *coded_samples);
1095  bytestream2_seek(gb, -8, SEEK_CUR);
1096  break;
1097  case AV_CODEC_ID_ADPCM_EA:
1098  /* Stereo is 30 bytes per block */
1099  /* Mono is 15 bytes per block */
1100  has_coded_samples = 1;
1101  *coded_samples = bytestream2_get_le32(gb);
1102  *coded_samples -= *coded_samples % 28;
1103  nb_samples = (buf_size - 12) / (ch == 2 ? 30 : 15) * 28;
1104  break;
1106  has_coded_samples = 1;
1107  *coded_samples = bytestream2_get_le32(gb);
1108  nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
1109  break;
1111  nb_samples = (buf_size - ch) / ch * 2;
1112  break;
1116  /* maximum number of samples */
1117  /* has internal offsets and a per-frame switch to signal raw 16-bit */
1118  has_coded_samples = 1;
1119  switch (avctx->codec->id) {
1121  header_size = 4 + 9 * ch;
1122  *coded_samples = bytestream2_get_le32(gb);
1123  break;
1125  header_size = 4 + 5 * ch;
1126  *coded_samples = bytestream2_get_le32(gb);
1127  break;
1129  header_size = 4 + 5 * ch;
1130  *coded_samples = bytestream2_get_be32(gb);
1131  break;
1132  }
1133  *coded_samples -= *coded_samples % 28;
1134  nb_samples = (buf_size - header_size) * 2 / ch;
1135  nb_samples -= nb_samples % 28;
1136  *approx_nb_samples = 1;
1137  break;
1139  if (avctx->block_align > 0)
1140  buf_size = FFMIN(buf_size, avctx->block_align);
1141  nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
1142  break;
1144  if (avctx->block_align > 0)
1145  buf_size = FFMIN(buf_size, avctx->block_align);
1146  if (buf_size < 4 * ch)
1147  return AVERROR_INVALIDDATA;
1148  nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
1149  break;
1151  if (avctx->block_align > 0)
1152  buf_size = FFMIN(buf_size, avctx->block_align);
1153  nb_samples = (buf_size - 4 * ch) * 2 / ch;
1154  break;
1155  CASE(ADPCM_IMA_WAV,
1156  int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1157  int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1158  if (avctx->block_align > 0)
1159  buf_size = FFMIN(buf_size, avctx->block_align);
1160  if (buf_size < 4 * ch)
1161  return AVERROR_INVALIDDATA;
1162  nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
1163  ) /* End of CASE */
1164  CASE(ADPCM_IMA_XBOX,
1165  int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1166  int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1167  if (avctx->block_align > 0)
1168  buf_size = FFMIN(buf_size, avctx->block_align);
1169  if (buf_size < 4 * ch)
1170  return AVERROR_INVALIDDATA;
1171  nb_samples = (buf_size - 4 * ch) / (bsize * ch) * bsamples + 1;
1172  ) /* End of CASE */
1173  case AV_CODEC_ID_ADPCM_MS:
1174  if (avctx->block_align > 0)
1175  buf_size = FFMIN(buf_size, avctx->block_align);
1176  nb_samples = (buf_size - 6 * ch) * 2 / ch;
1177  break;
1179  if (avctx->block_align > 0)
1180  buf_size = FFMIN(buf_size, avctx->block_align);
1181  nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
1182  break;
1186  {
1187  int samples_per_byte;
1188  switch (avctx->codec->id) {
1189  case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
1190  case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
1191  case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
1192  }
1193  if (!s->status[0].step_index) {
1194  if (buf_size < ch)
1195  return AVERROR_INVALIDDATA;
1196  nb_samples++;
1197  buf_size -= ch;
1198  }
1199  nb_samples += buf_size * samples_per_byte / ch;
1200  break;
1201  }
1202  case AV_CODEC_ID_ADPCM_SWF:
1203  {
1204  int buf_bits = buf_size * 8 - 2;
1205  int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
1206  int block_hdr_size = 22 * ch;
1207  int block_size = block_hdr_size + nbits * ch * 4095;
1208  int nblocks = buf_bits / block_size;
1209  int bits_left = buf_bits - nblocks * block_size;
1210  nb_samples = nblocks * 4096;
1211  if (bits_left >= block_hdr_size)
1212  nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
1213  break;
1214  }
1215  case AV_CODEC_ID_ADPCM_THP:
1217  if (avctx->extradata) {
1218  nb_samples = buf_size * 14 / (8 * ch);
1219  break;
1220  }
1221  has_coded_samples = 1;
1222  bytestream2_skip(gb, 4); // channel size
1223  *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
1224  bytestream2_get_le32(gb) :
1225  bytestream2_get_be32(gb);
1226  buf_size -= 8 + 36 * ch;
1227  buf_size /= ch;
1228  nb_samples = buf_size / 8 * 14;
1229  if (buf_size % 8 > 1)
1230  nb_samples += (buf_size % 8 - 1) * 2;
1231  *approx_nb_samples = 1;
1232  break;
1233  case AV_CODEC_ID_ADPCM_AFC:
1234  nb_samples = buf_size / (9 * ch) * 16;
1235  break;
1236  case AV_CODEC_ID_ADPCM_XA:
1237  nb_samples = (buf_size / 128) * 224 / ch;
1238  break;
1239  case AV_CODEC_ID_ADPCM_XMD:
1240  nb_samples = buf_size / (21 * ch) * 32;
1241  break;
1242  case AV_CODEC_ID_ADPCM_DTK:
1243  case AV_CODEC_ID_ADPCM_PSX:
1244  nb_samples = buf_size / (16 * ch) * 28;
1245  break;
1247  nb_samples = buf_size / avctx->block_align * 32;
1248  break;
1250  nb_samples = buf_size / ch;
1251  break;
1253  if (!avctx->extradata || avctx->extradata_size != 2)
1254  return AVERROR_INVALIDDATA;
1255  nb_samples = AV_RL16(avctx->extradata);
1256  break;
1257  }
1258 
1259  /* validate coded sample count */
1260  if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
1261  return AVERROR_INVALIDDATA;
1262 
1263  return nb_samples;
1264 }
1265 
1267  int *got_frame_ptr, AVPacket *avpkt)
1268 {
1269  const uint8_t *buf = avpkt->data;
1270  int buf_size = avpkt->size;
1271  ADPCMDecodeContext *c = avctx->priv_data;
1272  int channels = avctx->ch_layout.nb_channels;
1273  int16_t *samples;
1274  int16_t **samples_p;
1275  int st; /* stereo */
1276  int nb_samples, coded_samples, approx_nb_samples, ret;
1277  GetByteContext gb;
1278 
1279  bytestream2_init(&gb, buf, buf_size);
1280  nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
1281  if (nb_samples <= 0) {
1282  av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
1283  return AVERROR_INVALIDDATA;
1284  }
1285 
1286  /* get output buffer */
1287  frame->nb_samples = nb_samples;
1288  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1289  return ret;
1290  samples = (int16_t *)frame->data[0];
1291  samples_p = (int16_t **)frame->extended_data;
1292 
1293  /* use coded_samples when applicable */
1294  /* it is always <= nb_samples, so the output buffer will be large enough */
1295  if (coded_samples) {
1296  if (!approx_nb_samples && coded_samples != nb_samples)
1297  av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
1298  frame->nb_samples = nb_samples = coded_samples;
1299  }
1300 
1301  st = channels == 2 ? 1 : 0;
1302 
1303  switch(avctx->codec->id) {
1304  CASE(ADPCM_IMA_QT,
1305  /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
1306  Channel data is interleaved per-chunk. */
1307  for (int channel = 0; channel < channels; channel++) {
1308  ADPCMChannelStatus *cs = &c->status[channel];
1309  int predictor;
1310  int step_index;
1311  /* (pppppp) (piiiiiii) */
1312 
1313  /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
1314  predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1315  step_index = predictor & 0x7F;
1316  predictor &= ~0x7F;
1317 
1318  if (cs->step_index == step_index) {
1319  int diff = predictor - cs->predictor;
1320  if (diff < 0)
1321  diff = - diff;
1322  if (diff > 0x7f)
1323  goto update;
1324  } else {
1325  update:
1326  cs->step_index = step_index;
1327  cs->predictor = predictor;
1328  }
1329 
1330  if (cs->step_index > 88u){
1331  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1332  channel, cs->step_index);
1333  return AVERROR_INVALIDDATA;
1334  }
1335 
1336  samples = samples_p[channel];
1337 
1338  for (int m = 0; m < 64; m += 2) {
1339  int byte = bytestream2_get_byteu(&gb);
1340  samples[m ] = ff_adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
1341  samples[m + 1] = ff_adpcm_ima_qt_expand_nibble(cs, byte >> 4 );
1342  }
1343  }
1344  ) /* End of CASE */
1345  CASE(ADPCM_IMA_WAV,
1346  for (int i = 0; i < channels; i++) {
1347  ADPCMChannelStatus *cs = &c->status[i];
1348  cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1349 
1350  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1351  if (cs->step_index > 88u){
1352  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1353  i, cs->step_index);
1354  return AVERROR_INVALIDDATA;
1355  }
1356  }
1357 
1358  if (avctx->bits_per_coded_sample != 4) {
1359  int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1360  int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1361  uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1362  GetBitContext g;
1363 
1364  for (int n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1365  for (int i = 0; i < channels; i++) {
1366  ADPCMChannelStatus *cs = &c->status[i];
1367  samples = &samples_p[i][1 + n * samples_per_block];
1368  for (int j = 0; j < block_size; j++) {
1369  temp[j] = buf[4 * channels + block_size * n * channels +
1370  (j % 4) + (j / 4) * (channels * 4) + i * 4];
1371  }
1372  ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1373  if (ret < 0)
1374  return ret;
1375  for (int m = 0; m < samples_per_block; m++) {
1377  avctx->bits_per_coded_sample);
1378  }
1379  }
1380  }
1381  bytestream2_skip(&gb, avctx->block_align - channels * 4);
1382  } else {
1383  for (int n = 0; n < (nb_samples - 1) / 8; n++) {
1384  for (int i = 0; i < channels; i++) {
1385  ADPCMChannelStatus *cs = &c->status[i];
1386  samples = &samples_p[i][1 + n * 8];
1387  for (int m = 0; m < 8; m += 2) {
1388  int v = bytestream2_get_byteu(&gb);
1389  samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1390  samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1391  }
1392  }
1393  }
1394  }
1395  ) /* End of CASE */
1396  CASE(ADPCM_IMA_XBOX,
1397  for (int i = 0; i < channels; i++) {
1398  ADPCMChannelStatus *cs = &c->status[i];
1399  cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1400 
1401  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1402  if (cs->step_index > 88u) {
1403  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1404  i, cs->step_index);
1405  return AVERROR_INVALIDDATA;
1406  }
1407  }
1408 
1409  for (int n = 0; n < (nb_samples-1) / 8; n++) {
1410  for (int i = 0; i < channels; i++) {
1411  ADPCMChannelStatus *cs = &c->status[i];
1412  samples = &samples_p[i][1 + n * 8];
1413  for (int m = 0; m < 8; m += 2) {
1414  int v = bytestream2_get_byteu(&gb);
1415  samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1416  samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1417  }
1418  }
1419  }
1420  frame->nb_samples--;
1421  ) /* End of CASE */
1422  CASE(ADPCM_4XM,
1423  for (int i = 0; i < channels; i++)
1424  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1425 
1426  for (int i = 0; i < channels; i++) {
1427  c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1428  if (c->status[i].step_index > 88u) {
1429  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1430  i, c->status[i].step_index);
1431  return AVERROR_INVALIDDATA;
1432  }
1433  }
1434 
1435  for (int i = 0; i < channels; i++) {
1436  ADPCMChannelStatus *cs = &c->status[i];
1437  samples = (int16_t *)frame->data[i];
1438  for (int n = nb_samples >> 1; n > 0; n--) {
1439  int v = bytestream2_get_byteu(&gb);
1440  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1441  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
1442  }
1443  }
1444  ) /* End of CASE */
1445  CASE(ADPCM_AGM,
1446  for (int i = 0; i < channels; i++)
1447  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1448  for (int i = 0; i < channels; i++)
1449  c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1450 
1451  for (int n = 0; n < nb_samples >> (1 - st); n++) {
1452  int v = bytestream2_get_byteu(&gb);
1453  *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1454  *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1455  }
1456  ) /* End of CASE */
1457  CASE(ADPCM_MS,
1458  int block_predictor;
1459 
1460  if (avctx->ch_layout.nb_channels > 2) {
1461  for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
1462  samples = samples_p[channel];
1463  block_predictor = bytestream2_get_byteu(&gb);
1464  if (block_predictor > 6) {
1465  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1466  channel, block_predictor);
1467  return AVERROR_INVALIDDATA;
1468  }
1469  c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1470  c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1471  c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1472  c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1473  c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1474  *samples++ = c->status[channel].sample2;
1475  *samples++ = c->status[channel].sample1;
1476  for (int n = (nb_samples - 2) >> 1; n > 0; n--) {
1477  int byte = bytestream2_get_byteu(&gb);
1478  *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
1479  *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1480  }
1481  }
1482  } else {
1483  block_predictor = bytestream2_get_byteu(&gb);
1484  if (block_predictor > 6) {
1485  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1486  block_predictor);
1487  return AVERROR_INVALIDDATA;
1488  }
1489  c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1490  c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1491  if (st) {
1492  block_predictor = bytestream2_get_byteu(&gb);
1493  if (block_predictor > 6) {
1494  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1495  block_predictor);
1496  return AVERROR_INVALIDDATA;
1497  }
1498  c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1499  c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1500  }
1501  c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1502  if (st){
1503  c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1504  }
1505 
1506  c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1507  if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1508  c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1509  if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1510 
1511  *samples++ = c->status[0].sample2;
1512  if (st) *samples++ = c->status[1].sample2;
1513  *samples++ = c->status[0].sample1;
1514  if (st) *samples++ = c->status[1].sample1;
1515  for (int n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1516  int byte = bytestream2_get_byteu(&gb);
1517  *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
1518  *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1519  }
1520  }
1521  ) /* End of CASE */
1522  CASE(ADPCM_MTAF,
1523  for (int channel = 0; channel < channels; channel += 2) {
1524  bytestream2_skipu(&gb, 4);
1525  c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
1526  c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1527  c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1528  bytestream2_skipu(&gb, 2);
1529  c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1530  bytestream2_skipu(&gb, 2);
1531  for (int n = 0; n < nb_samples; n += 2) {
1532  int v = bytestream2_get_byteu(&gb);
1533  samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1534  samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
1535  }
1536  for (int n = 0; n < nb_samples; n += 2) {
1537  int v = bytestream2_get_byteu(&gb);
1538  samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1539  samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
1540  }
1541  }
1542  ) /* End of CASE */
1543  CASE(ADPCM_IMA_DK4,
1544  for (int channel = 0; channel < channels; channel++) {
1545  ADPCMChannelStatus *cs = &c->status[channel];
1546  cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1547  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1548  if (cs->step_index > 88u){
1549  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1550  channel, cs->step_index);
1551  return AVERROR_INVALIDDATA;
1552  }
1553  }
1554  for (int n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1555  int v = bytestream2_get_byteu(&gb);
1556  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1557  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1558  }
1559  ) /* End of CASE */
1560 
1561  /* DK3 ADPCM support macro */
1562 #define DK3_GET_NEXT_NIBBLE() \
1563  if (decode_top_nibble_next) { \
1564  nibble = last_byte >> 4; \
1565  decode_top_nibble_next = 0; \
1566  } else { \
1567  last_byte = bytestream2_get_byteu(&gb); \
1568  nibble = last_byte & 0x0F; \
1569  decode_top_nibble_next = 1; \
1570  }
1571  CASE(ADPCM_IMA_DK3,
1572  int last_byte = 0;
1573  int nibble;
1574  int decode_top_nibble_next = 0;
1575  int diff_channel;
1576  const int16_t *samples_end = samples + channels * nb_samples;
1577 
1578  bytestream2_skipu(&gb, 10);
1579  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1580  c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1581  c->status[0].step_index = bytestream2_get_byteu(&gb);
1582  c->status[1].step_index = bytestream2_get_byteu(&gb);
1583  if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1584  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1585  c->status[0].step_index, c->status[1].step_index);
1586  return AVERROR_INVALIDDATA;
1587  }
1588  /* sign extend the predictors */
1589  diff_channel = c->status[1].predictor;
1590 
1591  while (samples < samples_end) {
1592 
1593  /* for this algorithm, c->status[0] is the sum channel and
1594  * c->status[1] is the diff channel */
1595 
1596  /* process the first predictor of the sum channel */
1598  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1599 
1600  /* process the diff channel predictor */
1602  adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1603 
1604  /* process the first pair of stereo PCM samples */
1605  diff_channel = (diff_channel + c->status[1].predictor) / 2;
1606  *samples++ = c->status[0].predictor + c->status[1].predictor;
1607  *samples++ = c->status[0].predictor - c->status[1].predictor;
1608 
1609  /* process the second predictor of the sum channel */
1611  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1612 
1613  /* process the second pair of stereo PCM samples */
1614  diff_channel = (diff_channel + c->status[1].predictor) / 2;
1615  *samples++ = c->status[0].predictor + c->status[1].predictor;
1616  *samples++ = c->status[0].predictor - c->status[1].predictor;
1617  }
1618 
1619  if ((bytestream2_tell(&gb) & 1))
1620  bytestream2_skip(&gb, 1);
1621  ) /* End of CASE */
1622  CASE(ADPCM_IMA_ISS,
1623  for (int channel = 0; channel < channels; channel++) {
1624  ADPCMChannelStatus *cs = &c->status[channel];
1625  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1626  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1627  if (cs->step_index > 88u){
1628  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1629  channel, cs->step_index);
1630  return AVERROR_INVALIDDATA;
1631  }
1632  }
1633 
1634  for (int n = nb_samples >> (1 - st); n > 0; n--) {
1635  int v1, v2;
1636  int v = bytestream2_get_byteu(&gb);
1637  /* nibbles are swapped for mono */
1638  if (st) {
1639  v1 = v >> 4;
1640  v2 = v & 0x0F;
1641  } else {
1642  v2 = v >> 4;
1643  v1 = v & 0x0F;
1644  }
1645  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1646  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1647  }
1648  ) /* End of CASE */
1649  CASE(ADPCM_IMA_MOFLEX,
1650  for (int channel = 0; channel < channels; channel++) {
1651  ADPCMChannelStatus *cs = &c->status[channel];
1652  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1653  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1654  if (cs->step_index > 88u){
1655  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1656  channel, cs->step_index);
1657  return AVERROR_INVALIDDATA;
1658  }
1659  }
1660 
1661  for (int subframe = 0; subframe < nb_samples / 256; subframe++) {
1662  for (int channel = 0; channel < channels; channel++) {
1663  samples = samples_p[channel] + 256 * subframe;
1664  for (int n = 0; n < 256; n += 2) {
1665  int v = bytestream2_get_byteu(&gb);
1666  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1667  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1668  }
1669  }
1670  }
1671  ) /* End of CASE */
1672  CASE(ADPCM_IMA_DAT4,
1673  for (int channel = 0; channel < channels; channel++) {
1674  ADPCMChannelStatus *cs = &c->status[channel];
1675  samples = samples_p[channel];
1676  bytestream2_skip(&gb, 4);
1677  for (int n = 0; n < nb_samples; n += 2) {
1678  int v = bytestream2_get_byteu(&gb);
1679  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1680  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1681  }
1682  }
1683  ) /* End of CASE */
1684  CASE(ADPCM_IMA_APC,
1685  for (int n = nb_samples >> (1 - st); n > 0; n--) {
1686  int v = bytestream2_get_byteu(&gb);
1687  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1688  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1689  }
1690  ) /* End of CASE */
1691  CASE(ADPCM_IMA_SSI,
1692  for (int n = nb_samples >> (1 - st); n > 0; n--) {
1693  int v = bytestream2_get_byteu(&gb);
1694  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4 );
1695  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1696  }
1697  ) /* End of CASE */
1698  CASE(ADPCM_IMA_APM,
1699  for (int n = nb_samples / 2; n > 0; n--) {
1700  for (int channel = 0; channel < channels; channel++) {
1701  int v = bytestream2_get_byteu(&gb);
1702  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4 );
1703  samples[st] = ff_adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1704  }
1705  samples += channels;
1706  }
1707  ) /* End of CASE */
1708  CASE(ADPCM_IMA_ALP,
1709  for (int n = nb_samples / 2; n > 0; n--) {
1710  for (int channel = 0; channel < channels; channel++) {
1711  int v = bytestream2_get_byteu(&gb);
1712  *samples++ = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4 , 2);
1713  samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1714  }
1715  samples += channels;
1716  }
1717  ) /* End of CASE */
1718  CASE(ADPCM_IMA_CUNNING,
1719  for (int channel = 0; channel < channels; channel++) {
1720  int16_t *smp = samples_p[channel];
1721  for (int n = 0; n < nb_samples / 2; n++) {
1722  int v = bytestream2_get_byteu(&gb);
1723  *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v & 0x0F);
1724  *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v >> 4);
1725  }
1726  }
1727  ) /* End of CASE */
1728  CASE(ADPCM_IMA_OKI,
1729  for (int n = nb_samples >> (1 - st); n > 0; n--) {
1730  int v = bytestream2_get_byteu(&gb);
1731  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1732  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1733  }
1734  ) /* End of CASE */
1735  CASE(ADPCM_IMA_RAD,
1736  for (int channel = 0; channel < channels; channel++) {
1737  ADPCMChannelStatus *cs = &c->status[channel];
1738  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1739  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1740  if (cs->step_index > 88u){
1741  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1742  channel, cs->step_index);
1743  return AVERROR_INVALIDDATA;
1744  }
1745  }
1746  for (int n = 0; n < nb_samples / 2; n++) {
1747  int byte[2];
1748 
1749  byte[0] = bytestream2_get_byteu(&gb);
1750  if (st)
1751  byte[1] = bytestream2_get_byteu(&gb);
1752  for (int channel = 0; channel < channels; channel++) {
1753  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1754  }
1755  for (int channel = 0; channel < channels; channel++) {
1756  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1757  }
1758  }
1759  ) /* End of CASE */
1760  CASE(ADPCM_IMA_WS,
1761  if (c->vqa_version == 3) {
1762  for (int channel = 0; channel < channels; channel++) {
1763  int16_t *smp = samples_p[channel];
1764 
1765  for (int n = nb_samples / 2; n > 0; n--) {
1766  int v = bytestream2_get_byteu(&gb);
1767  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1768  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1769  }
1770  }
1771  } else {
1772  for (int n = nb_samples / 2; n > 0; n--) {
1773  for (int channel = 0; channel < channels; channel++) {
1774  int v = bytestream2_get_byteu(&gb);
1775  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1776  samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1777  }
1778  samples += channels;
1779  }
1780  }
1781  bytestream2_seek(&gb, 0, SEEK_END);
1782  ) /* End of CASE */
1783  CASE(ADPCM_XMD,
1784  int bytes_remaining, block = 0;
1785  while (bytestream2_get_bytes_left(&gb) >= 21 * channels) {
1786  for (int channel = 0; channel < channels; channel++) {
1787  int16_t *out = samples_p[channel] + block * 32;
1788  int16_t history[2];
1789  uint16_t scale;
1790 
1791  history[1] = sign_extend(bytestream2_get_le16(&gb), 16);
1792  history[0] = sign_extend(bytestream2_get_le16(&gb), 16);
1793  scale = bytestream2_get_le16(&gb);
1794 
1795  out[0] = history[1];
1796  out[1] = history[0];
1797 
1798  for (int n = 0; n < 15; n++) {
1799  unsigned byte = bytestream2_get_byte(&gb);
1800  int32_t nibble[2];
1801 
1802  nibble[0] = sign_extend(byte & 15, 4);
1803  nibble[1] = sign_extend(byte >> 4, 4);
1804 
1805  out[2+n*2] = nibble[0]*scale + ((history[0]*3667 - history[1]*1642) >> 11);
1806  history[1] = history[0];
1807  history[0] = out[2+n*2];
1808 
1809  out[2+n*2+1] = nibble[1]*scale + ((history[0]*3667 - history[1]*1642) >> 11);
1810  history[1] = history[0];
1811  history[0] = out[2+n*2+1];
1812  }
1813  }
1814 
1815  block++;
1816  }
1817  bytes_remaining = bytestream2_get_bytes_left(&gb);
1818  if (bytes_remaining > 0) {
1819  bytestream2_skip(&gb, bytes_remaining);
1820  }
1821  ) /* End of CASE */
1822  CASE(ADPCM_XA,
1823  int16_t *out0 = samples_p[0];
1824  int16_t *out1 = samples_p[1];
1825  int samples_per_block = 28 * (3 - channels) * 4;
1826  int sample_offset = 0;
1827  int bytes_remaining;
1828  while (bytestream2_get_bytes_left(&gb) >= 128) {
1829  if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
1830  &c->status[0], &c->status[1],
1831  channels, sample_offset)) < 0)
1832  return ret;
1833  bytestream2_skipu(&gb, 128);
1834  sample_offset += samples_per_block;
1835  }
1836  /* Less than a full block of data left, e.g. when reading from
1837  * 2324 byte per sector XA; the remainder is padding */
1838  bytes_remaining = bytestream2_get_bytes_left(&gb);
1839  if (bytes_remaining > 0) {
1840  bytestream2_skip(&gb, bytes_remaining);
1841  }
1842  ) /* End of CASE */
1843  CASE(ADPCM_IMA_EA_EACS,
1844  for (int i = 0; i <= st; i++) {
1845  c->status[i].step_index = bytestream2_get_le32u(&gb);
1846  if (c->status[i].step_index > 88u) {
1847  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1848  i, c->status[i].step_index);
1849  return AVERROR_INVALIDDATA;
1850  }
1851  }
1852  for (int i = 0; i <= st; i++) {
1853  c->status[i].predictor = bytestream2_get_le32u(&gb);
1854  if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
1855  return AVERROR_INVALIDDATA;
1856  }
1857 
1858  for (int n = nb_samples >> (1 - st); n > 0; n--) {
1859  int byte = bytestream2_get_byteu(&gb);
1860  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1861  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1862  }
1863  ) /* End of CASE */
1864  CASE(ADPCM_IMA_EA_SEAD,
1865  for (int n = nb_samples >> (1 - st); n > 0; n--) {
1866  int byte = bytestream2_get_byteu(&gb);
1867  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1868  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1869  }
1870  ) /* End of CASE */
1871  CASE(ADPCM_EA,
1872  int previous_left_sample, previous_right_sample;
1873  int current_left_sample, current_right_sample;
1874  int next_left_sample, next_right_sample;
1875  int coeff1l, coeff2l, coeff1r, coeff2r;
1876  int shift_left, shift_right;
1877 
1878  /* Each EA ADPCM frame has a 12-byte header followed by 30-byte (stereo) or 15-byte (mono) pieces,
1879  each coding 28 stereo/mono samples. */
1880 
1881  if (channels != 2 && channels != 1)
1882  return AVERROR_INVALIDDATA;
1883 
1884  current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1885  previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1886  current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1887  previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
1888 
1889  for (int count1 = 0; count1 < nb_samples / 28; count1++) {
1890  int byte = bytestream2_get_byteu(&gb);
1891  coeff1l = ea_adpcm_table[ byte >> 4 ];
1892  coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
1893  coeff1r = ea_adpcm_table[ byte & 0x0F];
1894  coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1895 
1896  if (channels == 2){
1897  byte = bytestream2_get_byteu(&gb);
1898  shift_left = 20 - (byte >> 4);
1899  shift_right = 20 - (byte & 0x0F);
1900  } else{
1901  /* Mono packs the shift into the coefficient byte's lower nibble instead */
1902  shift_left = 20 - (byte & 0x0F);
1903  }
1904 
1905  for (int count2 = 0; count2 < (channels == 2 ? 28 : 14); count2++) {
1906  byte = bytestream2_get_byteu(&gb);
1907  next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
1908 
1909  next_left_sample = (next_left_sample +
1910  (current_left_sample * coeff1l) +
1911  (previous_left_sample * coeff2l) + 0x80) >> 8;
1912 
1913  previous_left_sample = current_left_sample;
1914  current_left_sample = av_clip_int16(next_left_sample);
1915  *samples++ = current_left_sample;
1916 
1917  if (channels == 2){
1918  next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
1919 
1920  next_right_sample = (next_right_sample +
1921  (current_right_sample * coeff1r) +
1922  (previous_right_sample * coeff2r) + 0x80) >> 8;
1923 
1924  previous_right_sample = current_right_sample;
1925  current_right_sample = av_clip_int16(next_right_sample);
1926  *samples++ = current_right_sample;
1927  } else {
1928  next_left_sample = sign_extend(byte, 4) * (1 << shift_left);
1929 
1930  next_left_sample = (next_left_sample +
1931  (current_left_sample * coeff1l) +
1932  (previous_left_sample * coeff2l) + 0x80) >> 8;
1933 
1934  previous_left_sample = current_left_sample;
1935  current_left_sample = av_clip_int16(next_left_sample);
1936 
1937  *samples++ = current_left_sample;
1938  }
1939  }
1940  }
1941  bytestream2_skip(&gb, channels == 2 ? 2 : 3); // Skip terminating NULs
1942  ) /* End of CASE */
1943  CASE(ADPCM_EA_MAXIS_XA,
1944  int coeff[2][2], shift[2];
1945 
1946  for (int channel = 0; channel < channels; channel++) {
1947  int byte = bytestream2_get_byteu(&gb);
1948  for (int i = 0; i < 2; i++)
1949  coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
1950  shift[channel] = 20 - (byte & 0x0F);
1951  }
1952  for (int count1 = 0; count1 < nb_samples / 2; count1++) {
1953  int byte[2];
1954 
1955  byte[0] = bytestream2_get_byteu(&gb);
1956  if (st) byte[1] = bytestream2_get_byteu(&gb);
1957  for (int i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1958  for (int channel = 0; channel < channels; channel++) {
1959  int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1960  sample = (sample +
1961  c->status[channel].sample1 * coeff[channel][0] +
1962  c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
1963  c->status[channel].sample2 = c->status[channel].sample1;
1964  c->status[channel].sample1 = av_clip_int16(sample);
1965  *samples++ = c->status[channel].sample1;
1966  }
1967  }
1968  }
1969  bytestream2_seek(&gb, 0, SEEK_END);
1970  ) /* End of CASE */
1971 #if CONFIG_ADPCM_EA_R1_DECODER || CONFIG_ADPCM_EA_R2_DECODER || CONFIG_ADPCM_EA_R3_DECODER
1974  case AV_CODEC_ID_ADPCM_EA_R3: {
1975  /* channel numbering
1976  2chan: 0=fl, 1=fr
1977  4chan: 0=fl, 1=rl, 2=fr, 3=rr
1978  6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
1979  const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
1980  int previous_sample, current_sample, next_sample;
1981  int coeff1, coeff2;
1982  int shift;
1983  uint16_t *samplesC;
1984  int count = 0;
1985  int offsets[6];
1986 
1987  for (unsigned channel = 0; channel < channels; channel++)
1988  offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
1989  bytestream2_get_le32(&gb)) +
1990  (channels + 1) * 4;
1991 
1992  for (unsigned channel = 0; channel < channels; channel++) {
1993  int count1;
1994 
1995  bytestream2_seek(&gb, offsets[channel], SEEK_SET);
1996  samplesC = samples_p[channel];
1997 
1998  if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
1999  current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
2000  previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
2001  } else {
2002  current_sample = c->status[channel].predictor;
2003  previous_sample = c->status[channel].prev_sample;
2004  }
2005 
2006  for (count1 = 0; count1 < nb_samples / 28; count1++) {
2007  int byte = bytestream2_get_byte(&gb);
2008  if (byte == 0xEE) { /* only seen in R2 and R3 */
2009  current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
2010  previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
2011 
2012  for (int count2 = 0; count2 < 28; count2++)
2013  *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
2014  } else {
2015  coeff1 = ea_adpcm_table[ byte >> 4 ];
2016  coeff2 = ea_adpcm_table[(byte >> 4) + 4];
2017  shift = 20 - (byte & 0x0F);
2018 
2019  for (int count2 = 0; count2 < 28; count2++) {
2020  if (count2 & 1)
2021  next_sample = (unsigned)sign_extend(byte, 4) << shift;
2022  else {
2023  byte = bytestream2_get_byte(&gb);
2024  next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
2025  }
2026 
2027  next_sample += (current_sample * coeff1) +
2028  (previous_sample * coeff2);
2029  next_sample = av_clip_int16(next_sample >> 8);
2030 
2031  previous_sample = current_sample;
2032  current_sample = next_sample;
2033  *samplesC++ = current_sample;
2034  }
2035  }
2036  }
2037  if (!count) {
2038  count = count1;
2039  } else if (count != count1) {
2040  av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
2041  count = FFMAX(count, count1);
2042  }
2043 
2044  if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
2045  c->status[channel].predictor = current_sample;
2046  c->status[channel].prev_sample = previous_sample;
2047  }
2048  }
2049 
2050  frame->nb_samples = count * 28;
2051  bytestream2_seek(&gb, 0, SEEK_END);
2052  break;
2053  }
2054 #endif /* CONFIG_ADPCM_EA_Rx_DECODER */
2055  CASE(ADPCM_EA_XAS,
2056  for (int channel=0; channel < channels; channel++) {
2057  int coeff[2][4], shift[4];
2058  int16_t *s = samples_p[channel];
2059  for (int n = 0; n < 4; n++, s += 32) {
2060  int val = sign_extend(bytestream2_get_le16u(&gb), 16);
2061  for (int i = 0; i < 2; i++)
2062  coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
2063  s[0] = val & ~0x0F;
2064 
2065  val = sign_extend(bytestream2_get_le16u(&gb), 16);
2066  shift[n] = 20 - (val & 0x0F);
2067  s[1] = val & ~0x0F;
2068  }
2069 
2070  for (int m = 2; m < 32; m += 2) {
2071  s = &samples_p[channel][m];
2072  for (int n = 0; n < 4; n++, s += 32) {
2073  int level, pred;
2074  int byte = bytestream2_get_byteu(&gb);
2075 
2076  level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
2077  pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
2078  s[0] = av_clip_int16((level + pred + 0x80) >> 8);
2079 
2080  level = sign_extend(byte, 4) * (1 << shift[n]);
2081  pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
2082  s[1] = av_clip_int16((level + pred + 0x80) >> 8);
2083  }
2084  }
2085  }
2086  ) /* End of CASE */
2087  CASE(ADPCM_IMA_ACORN,
2088  for (int channel = 0; channel < channels; channel++) {
2089  ADPCMChannelStatus *cs = &c->status[channel];
2090  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2091  cs->step_index = bytestream2_get_le16u(&gb) & 0xFF;
2092  if (cs->step_index > 88u){
2093  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
2094  channel, cs->step_index);
2095  return AVERROR_INVALIDDATA;
2096  }
2097  }
2098  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2099  int byte = bytestream2_get_byteu(&gb);
2100  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte & 0x0F, 3);
2101  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte >> 4, 3);
2102  }
2103  ) /* End of CASE */
2104  CASE(ADPCM_IMA_AMV,
2105  av_assert0(channels == 1);
2106 
2107  /*
2108  * Header format:
2109  * int16_t predictor;
2110  * uint8_t step_index;
2111  * uint8_t reserved;
2112  * uint32_t frame_size;
2113  *
2114  * Some implementations have step_index as 16-bits, but others
2115  * only use the lower 8 and store garbage in the upper 8.
2116  */
2117  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2118  c->status[0].step_index = bytestream2_get_byteu(&gb);
2119  bytestream2_skipu(&gb, 5);
2120  if (c->status[0].step_index > 88u) {
2121  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2122  c->status[0].step_index);
2123  return AVERROR_INVALIDDATA;
2124  }
2125 
2126  for (int n = nb_samples >> 1; n > 0; n--) {
2127  int v = bytestream2_get_byteu(&gb);
2128 
2129  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
2130  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
2131  }
2132 
2133  if (nb_samples & 1) {
2134  int v = bytestream2_get_byteu(&gb);
2135  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
2136 
2137  if (v & 0x0F) {
2138  /* Holds true on all the http://samples.mplayerhq.hu/amv samples. */
2139  av_log(avctx, AV_LOG_WARNING, "Last nibble set on packet with odd sample count.\n");
2140  av_log(avctx, AV_LOG_WARNING, "Sample will be skipped.\n");
2141  }
2142  }
2143  ) /* End of CASE */
2144  CASE(ADPCM_IMA_SMJPEG,
2145  for (int i = 0; i < channels; i++) {
2146  c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
2147  c->status[i].step_index = bytestream2_get_byteu(&gb);
2148  bytestream2_skipu(&gb, 1);
2149  if (c->status[i].step_index > 88u) {
2150  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2151  c->status[i].step_index);
2152  return AVERROR_INVALIDDATA;
2153  }
2154  }
2155 
2156  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2157  int v = bytestream2_get_byteu(&gb);
2158 
2159  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
2160  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
2161  }
2162  ) /* End of CASE */
2163  CASE(ADPCM_CT,
2164  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2165  int v = bytestream2_get_byteu(&gb);
2166  *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
2167  *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
2168  }
2169  ) /* End of CASE */
2170 #if CONFIG_ADPCM_SBPRO_2_DECODER || CONFIG_ADPCM_SBPRO_3_DECODER || \
2171  CONFIG_ADPCM_SBPRO_4_DECODER
2175  if (!c->status[0].step_index) {
2176  /* the first byte is a raw sample */
2177  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2178  if (st)
2179  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2180  c->status[0].step_index = 1;
2181  nb_samples--;
2182  }
2183  if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
2184  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2185  int byte = bytestream2_get_byteu(&gb);
2186  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2187  byte >> 4, 4, 0);
2188  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2189  byte & 0x0F, 4, 0);
2190  }
2191  } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
2192  for (int n = (nb_samples<<st) / 3; n > 0; n--) {
2193  int byte = bytestream2_get_byteu(&gb);
2194  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2195  byte >> 5 , 3, 0);
2196  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2197  (byte >> 2) & 0x07, 3, 0);
2198  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2199  byte & 0x03, 2, 0);
2200  }
2201  } else {
2202  for (int n = nb_samples >> (2 - st); n > 0; n--) {
2203  int byte = bytestream2_get_byteu(&gb);
2204  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2205  byte >> 6 , 2, 2);
2206  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2207  (byte >> 4) & 0x03, 2, 2);
2208  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2209  (byte >> 2) & 0x03, 2, 2);
2210  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2211  byte & 0x03, 2, 2);
2212  }
2213  }
2214  break;
2215 #endif /* CONFIG_ADPCM_SBPRO_x_DECODER */
2216  CASE(ADPCM_SWF,
2217  adpcm_swf_decode(avctx, buf, buf_size, samples);
2218  bytestream2_seek(&gb, 0, SEEK_END);
2219  ) /* End of CASE */
2220  CASE(ADPCM_YAMAHA,
2221  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2222  int v = bytestream2_get_byteu(&gb);
2223  *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
2224  *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
2225  }
2226  ) /* End of CASE */
2227  CASE(ADPCM_AICA,
2228  for (int channel = 0; channel < channels; channel++) {
2229  samples = samples_p[channel];
2230  for (int n = nb_samples >> 1; n > 0; n--) {
2231  int v = bytestream2_get_byteu(&gb);
2232  *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
2233  *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
2234  }
2235  }
2236  ) /* End of CASE */
2237  CASE(ADPCM_AFC,
2238  int samples_per_block;
2239  int blocks;
2240 
2241  if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
2242  samples_per_block = avctx->extradata[0] / 16;
2243  blocks = nb_samples / avctx->extradata[0];
2244  } else {
2245  samples_per_block = nb_samples / 16;
2246  blocks = 1;
2247  }
2248 
2249  for (int m = 0; m < blocks; m++) {
2250  for (int channel = 0; channel < channels; channel++) {
2251  int prev1 = c->status[channel].sample1;
2252  int prev2 = c->status[channel].sample2;
2253 
2254  samples = samples_p[channel] + m * 16;
2255  /* Read in every sample for this channel. */
2256  for (int i = 0; i < samples_per_block; i++) {
2257  int byte = bytestream2_get_byteu(&gb);
2258  int scale = 1 << (byte >> 4);
2259  int index = byte & 0xf;
2260  int factor1 = afc_coeffs[0][index];
2261  int factor2 = afc_coeffs[1][index];
2262 
2263  /* Decode 16 samples. */
2264  for (int n = 0; n < 16; n++) {
2265  int32_t sampledat;
2266 
2267  if (n & 1) {
2268  sampledat = sign_extend(byte, 4);
2269  } else {
2270  byte = bytestream2_get_byteu(&gb);
2271  sampledat = sign_extend(byte >> 4, 4);
2272  }
2273 
2274  sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
2275  sampledat * scale;
2276  *samples = av_clip_int16(sampledat);
2277  prev2 = prev1;
2278  prev1 = *samples++;
2279  }
2280  }
2281 
2282  c->status[channel].sample1 = prev1;
2283  c->status[channel].sample2 = prev2;
2284  }
2285  }
2286  bytestream2_seek(&gb, 0, SEEK_END);
2287  ) /* End of CASE */
2288 #if CONFIG_ADPCM_THP_DECODER || CONFIG_ADPCM_THP_LE_DECODER
2289  case AV_CODEC_ID_ADPCM_THP:
2291  {
2292  int table[14][16];
2293 
2294 #define THP_GET16(g) \
2295  sign_extend( \
2296  avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
2297  bytestream2_get_le16u(&(g)) : \
2298  bytestream2_get_be16u(&(g)), 16)
2299 
2300  if (avctx->extradata) {
2301  GetByteContext tb;
2302  if (avctx->extradata_size < 32 * channels) {
2303  av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
2304  return AVERROR_INVALIDDATA;
2305  }
2306 
2307  bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
2308  for (int i = 0; i < channels; i++)
2309  for (int n = 0; n < 16; n++)
2310  table[i][n] = THP_GET16(tb);
2311  } else {
2312  for (int i = 0; i < channels; i++)
2313  for (int n = 0; n < 16; n++)
2314  table[i][n] = THP_GET16(gb);
2315 
2316  if (!c->has_status) {
2317  /* Initialize the previous sample. */
2318  for (int i = 0; i < channels; i++) {
2319  c->status[i].sample1 = THP_GET16(gb);
2320  c->status[i].sample2 = THP_GET16(gb);
2321  }
2322  c->has_status = 1;
2323  } else {
2324  bytestream2_skip(&gb, channels * 4);
2325  }
2326  }
2327 
2328  for (int ch = 0; ch < channels; ch++) {
2329  samples = samples_p[ch];
2330 
2331  /* Read in every sample for this channel. */
2332  for (int i = 0; i < (nb_samples + 13) / 14; i++) {
2333  int byte = bytestream2_get_byteu(&gb);
2334  int index = (byte >> 4) & 7;
2335  unsigned int exp = byte & 0x0F;
2336  int64_t factor1 = table[ch][index * 2];
2337  int64_t factor2 = table[ch][index * 2 + 1];
2338 
2339  /* Decode 14 samples. */
2340  for (int n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
2341  int32_t sampledat;
2342 
2343  if (n & 1) {
2344  sampledat = sign_extend(byte, 4);
2345  } else {
2346  byte = bytestream2_get_byteu(&gb);
2347  sampledat = sign_extend(byte >> 4, 4);
2348  }
2349 
2350  sampledat = ((c->status[ch].sample1 * factor1
2351  + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
2352  *samples = av_clip_int16(sampledat);
2353  c->status[ch].sample2 = c->status[ch].sample1;
2354  c->status[ch].sample1 = *samples++;
2355  }
2356  }
2357  }
2358  break;
2359  }
2360 #endif /* CONFIG_ADPCM_THP(_LE)_DECODER */
2361  CASE(ADPCM_DTK,
2362  for (int channel = 0; channel < channels; channel++) {
2363  samples = samples_p[channel];
2364 
2365  /* Read in every sample for this channel. */
2366  for (int i = 0; i < nb_samples / 28; i++) {
2367  int byte, header;
2368  if (channel)
2369  bytestream2_skipu(&gb, 1);
2370  header = bytestream2_get_byteu(&gb);
2371  bytestream2_skipu(&gb, 3 - channel);
2372 
2373  /* Decode 28 samples. */
2374  for (int n = 0; n < 28; n++) {
2375  int32_t sampledat, prev;
2376 
2377  switch (header >> 4) {
2378  case 1:
2379  prev = (c->status[channel].sample1 * 0x3c);
2380  break;
2381  case 2:
2382  prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
2383  break;
2384  case 3:
2385  prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
2386  break;
2387  default:
2388  prev = 0;
2389  }
2390 
2391  prev = av_clip_intp2((prev + 0x20) >> 6, 21);
2392 
2393  byte = bytestream2_get_byteu(&gb);
2394  if (!channel)
2395  sampledat = sign_extend(byte, 4);
2396  else
2397  sampledat = sign_extend(byte >> 4, 4);
2398 
2399  sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
2400  *samples++ = av_clip_int16(sampledat >> 6);
2401  c->status[channel].sample2 = c->status[channel].sample1;
2402  c->status[channel].sample1 = sampledat;
2403  }
2404  }
2405  if (!channel)
2406  bytestream2_seek(&gb, 0, SEEK_SET);
2407  }
2408  ) /* End of CASE */
2409  CASE(ADPCM_PSX,
2410  for (int block = 0; block < avpkt->size / FFMAX(avctx->block_align, 16 * channels); block++) {
2411  int nb_samples_per_block = 28 * FFMAX(avctx->block_align, 16 * channels) / (16 * channels);
2412  for (int channel = 0; channel < channels; channel++) {
2413  samples = samples_p[channel] + block * nb_samples_per_block;
2414  av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
2415 
2416  /* Read in every sample for this channel. */
2417  for (int i = 0; i < nb_samples_per_block / 28; i++) {
2418  int filter, shift, flag, byte;
2419 
2420  filter = bytestream2_get_byteu(&gb);
2421  shift = filter & 0xf;
2422  filter = filter >> 4;
2424  return AVERROR_INVALIDDATA;
2425  flag = bytestream2_get_byteu(&gb) & 0x7;
2426 
2427  /* Decode 28 samples. */
2428  for (int n = 0; n < 28; n++) {
2429  int sample = 0, scale;
2430 
2431  if (n & 1) {
2432  scale = sign_extend(byte >> 4, 4);
2433  } else {
2434  byte = bytestream2_get_byteu(&gb);
2435  scale = sign_extend(byte, 4);
2436  }
2437 
2438  if (flag < 0x07) {
2439  scale = scale * (1 << 12);
2440  sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
2441  }
2443  c->status[channel].sample2 = c->status[channel].sample1;
2444  c->status[channel].sample1 = sample;
2445  }
2446  }
2447  }
2448  }
2449  ) /* End of CASE */
2450  CASE(ADPCM_SANYO,
2451  int (*expand)(ADPCMChannelStatus *c, int bits);
2452  GetBitContext g;
2453 
2454  switch(avctx->bits_per_coded_sample) {
2455  case 3: expand = adpcm_sanyo_expand3; break;
2456  case 4: expand = adpcm_sanyo_expand4; break;
2457  case 5: expand = adpcm_sanyo_expand5; break;
2458  }
2459 
2460  for (int ch = 0; ch < channels; ch++) {
2461  c->status[ch].predictor = sign_extend(bytestream2_get_le16(&gb), 16);
2462  c->status[ch].step = sign_extend(bytestream2_get_le16(&gb), 16);
2463  }
2464 
2466  for (int i = 0; i < nb_samples; i++)
2467  for (int ch = 0; ch < channels; ch++)
2468  samples_p[ch][i] = expand(&c->status[ch], get_bits_le(&g, avctx->bits_per_coded_sample));
2469 
2470  align_get_bits(&g);
2471  bytestream2_skip(&gb, get_bits_count(&g) / 8);
2472  ) /* End of CASE */
2473  CASE(ADPCM_ARGO,
2474  /*
2475  * The format of each block:
2476  * uint8_t left_control;
2477  * uint4_t left_samples[nb_samples];
2478  * ---- and if stereo ----
2479  * uint8_t right_control;
2480  * uint4_t right_samples[nb_samples];
2481  *
2482  * Format of the control byte:
2483  * MSB [SSSSRDRR] LSB
2484  * S = (Shift Amount - 2)
2485  * D = Decoder flag.
2486  * R = Reserved
2487  *
2488  * Each block relies on the previous two samples of each channel.
2489  * They should be 0 initially.
2490  */
2491  for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
2492  for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
2493  ADPCMChannelStatus *cs = c->status + channel;
2494  int control, shift;
2495 
2496  samples = samples_p[channel] + block * 32;
2497 
2498  /* Get the control byte and decode the samples, 2 at a time. */
2499  control = bytestream2_get_byteu(&gb);
2500  shift = (control >> 4) + 2;
2501 
2502  for (int n = 0; n < 16; n++) {
2503  int sample = bytestream2_get_byteu(&gb);
2504  *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift, control & 0x04);
2505  *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift, control & 0x04);
2506  }
2507  }
2508  }
2509  ) /* End of CASE */
2510  CASE(ADPCM_ZORK,
2511  for (int n = 0; n < nb_samples * channels; n++) {
2512  int v = bytestream2_get_byteu(&gb);
2513  *samples++ = adpcm_zork_expand_nibble(&c->status[n % channels], v);
2514  }
2515  ) /* End of CASE */
2516  CASE(ADPCM_IMA_MTF,
2517  for (int n = nb_samples / 2; n > 0; n--) {
2518  for (int channel = 0; channel < channels; channel++) {
2519  int v = bytestream2_get_byteu(&gb);
2520  *samples++ = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2521  samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2522  }
2523  samples += channels;
2524  }
2525  ) /* End of CASE */
2526  default:
2527  av_unreachable("There are cases for all codec ids using adpcm_decode_frame");
2528  }
2529 
2530  if (avpkt->size && bytestream2_tell(&gb) == 0) {
2531  av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2532  return AVERROR_INVALIDDATA;
2533  }
2534 
2535  *got_frame_ptr = 1;
2536 
2537  if (avpkt->size < bytestream2_tell(&gb)) {
2538  av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2539  return avpkt->size;
2540  }
2541 
2542  return bytestream2_tell(&gb);
2543 }
2544 
2546 {
2547  ADPCMDecodeContext *c = avctx->priv_data;
2548 
2549  /* Just nuke the entire state and re-init. */
2550  memset(c, 0, sizeof(ADPCMDecodeContext));
2551 
2552  switch(avctx->codec_id) {
2553  case AV_CODEC_ID_ADPCM_CT:
2554  c->status[0].step = c->status[1].step = 511;
2555  break;
2556 
2558  if (avctx->extradata && avctx->extradata_size >= 8) {
2559  c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata ), 18);
2560  c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
2561  }
2562  break;
2563 
2565  if (avctx->extradata && avctx->extradata_size >= 28) {
2566  c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
2567  c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
2568  c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
2569  c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
2570  }
2571  break;
2572 
2574  if (avctx->extradata && avctx->extradata_size >= 2)
2575  c->vqa_version = AV_RL16(avctx->extradata);
2576  break;
2577  default:
2578  /* Other codecs may want to handle this during decoding. */
2579  c->has_status = 0;
2580  return;
2581  }
2582 
2583  c->has_status = 1;
2584 }
2585 
2586 
2594 
2595 #define ADPCM_DECODER_0(id_, sample_fmts_, name_, long_name_)
2596 #define ADPCM_DECODER_1(id_, sample_fmts_, name_, long_name_) \
2597 const FFCodec ff_ ## name_ ## _decoder = { \
2598  .p.name = #name_, \
2599  CODEC_LONG_NAME(long_name_), \
2600  .p.type = AVMEDIA_TYPE_AUDIO, \
2601  .p.id = id_, \
2602  .p.capabilities = AV_CODEC_CAP_DR1, \
2603  CODEC_SAMPLEFMTS_ARRAY(sample_fmts_), \
2604  .priv_data_size = sizeof(ADPCMDecodeContext), \
2605  .init = adpcm_decode_init, \
2606  FF_CODEC_DECODE_CB(adpcm_decode_frame), \
2607  .flush = adpcm_flush, \
2608 };
2609 #define ADPCM_DECODER_2(enabled, codec_id, name, sample_fmts, long_name) \
2610  ADPCM_DECODER_ ## enabled(codec_id, name, sample_fmts, long_name)
2611 #define ADPCM_DECODER_3(config, codec_id, name, sample_fmts, long_name) \
2612  ADPCM_DECODER_2(config, codec_id, name, sample_fmts, long_name)
2613 #define ADPCM_DECODER(codec, name, sample_fmts, long_name) \
2614  ADPCM_DECODER_3(CONFIG_ ## codec ## _DECODER, AV_CODEC_ID_ ## codec, \
2615  name, sample_fmts, long_name)
2616 
2617 /* Note: Do not forget to add new entries to the Makefile as well. */
2618 ADPCM_DECODER(ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie")
2619 ADPCM_DECODER(ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC")
2620 ADPCM_DECODER(ADPCM_AGM, sample_fmts_s16, adpcm_agm, "ADPCM AmuseGraphics Movie")
2621 ADPCM_DECODER(ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA")
2622 ADPCM_DECODER(ADPCM_ARGO, sample_fmts_s16p, adpcm_argo, "ADPCM Argonaut Games")
2623 ADPCM_DECODER(ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology")
2624 ADPCM_DECODER(ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK")
2625 ADPCM_DECODER(ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts")
2626 ADPCM_DECODER(ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA")
2627 ADPCM_DECODER(ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1")
2628 ADPCM_DECODER(ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2")
2629 ADPCM_DECODER(ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3")
2630 ADPCM_DECODER(ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS")
2631 ADPCM_DECODER(ADPCM_IMA_ACORN, sample_fmts_s16, adpcm_ima_acorn, "ADPCM IMA Acorn Replay")
2632 ADPCM_DECODER(ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV")
2633 ADPCM_DECODER(ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC")
2634 ADPCM_DECODER(ADPCM_IMA_APM, sample_fmts_s16, adpcm_ima_apm, "ADPCM IMA Ubisoft APM")
2635 ADPCM_DECODER(ADPCM_IMA_CUNNING, sample_fmts_s16p, adpcm_ima_cunning, "ADPCM IMA Cunning Developments")
2636 ADPCM_DECODER(ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4")
2637 ADPCM_DECODER(ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3")
2638 ADPCM_DECODER(ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4")
2639 ADPCM_DECODER(ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS")
2640 ADPCM_DECODER(ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD")
2641 ADPCM_DECODER(ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS")
2642 ADPCM_DECODER(ADPCM_IMA_MOFLEX, sample_fmts_s16p, adpcm_ima_moflex, "ADPCM IMA MobiClip MOFLEX")
2643 ADPCM_DECODER(ADPCM_IMA_MTF, sample_fmts_s16, adpcm_ima_mtf, "ADPCM IMA Capcom's MT Framework")
2644 ADPCM_DECODER(ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI")
2645 ADPCM_DECODER(ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime")
2646 ADPCM_DECODER(ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical")
2647 ADPCM_DECODER(ADPCM_IMA_SSI, sample_fmts_s16, adpcm_ima_ssi, "ADPCM IMA Simon & Schuster Interactive")
2648 ADPCM_DECODER(ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG")
2649 ADPCM_DECODER(ADPCM_IMA_ALP, sample_fmts_s16, adpcm_ima_alp, "ADPCM IMA High Voltage Software ALP")
2650 ADPCM_DECODER(ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV")
2651 ADPCM_DECODER(ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood")
2652 ADPCM_DECODER(ADPCM_IMA_XBOX, sample_fmts_s16p, adpcm_ima_xbox, "ADPCM IMA Xbox")
2653 ADPCM_DECODER(ADPCM_MS, sample_fmts_both, adpcm_ms, "ADPCM Microsoft")
2654 ADPCM_DECODER(ADPCM_MTAF, sample_fmts_s16p, adpcm_mtaf, "ADPCM MTAF")
2655 ADPCM_DECODER(ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation")
2656 ADPCM_DECODER(ADPCM_SANYO, sample_fmts_s16p, adpcm_sanyo, "ADPCM Sanyo")
2657 ADPCM_DECODER(ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit")
2658 ADPCM_DECODER(ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit")
2659 ADPCM_DECODER(ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit")
2660 ADPCM_DECODER(ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash")
2661 ADPCM_DECODER(ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)")
2662 ADPCM_DECODER(ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP")
2663 ADPCM_DECODER(ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA")
2664 ADPCM_DECODER(ADPCM_XMD, sample_fmts_s16p, adpcm_xmd, "ADPCM Konami XMD")
2665 ADPCM_DECODER(ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha")
2666 ADPCM_DECODER(ADPCM_ZORK, sample_fmts_s16, adpcm_zork, "ADPCM Zork")
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AV_CODEC_ID_ADPCM_MS
@ AV_CODEC_ID_ADPCM_MS
Definition: codec_id.h:382
adpcm_index_table5
static const int8_t adpcm_index_table5[32]
Definition: adpcm.c:140
DK3_GET_NEXT_NIBBLE
#define DK3_GET_NEXT_NIBBLE()
AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition: codec_id.h:376
level
uint8_t level
Definition: svq3.c:208
av_clip
#define av_clip
Definition: common.h:100
AVERROR
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
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition: bytestream.h:158
out
FILE * out
Definition: movenc.c:55
AV_CODEC_ID_ADPCM_DTK
@ AV_CODEC_ID_ADPCM_DTK
Definition: codec_id.h:409
ADPCMChannelStatus::step_index
int16_t step_index
Definition: adpcm.h:33
GetByteContext
Definition: bytestream.h:33
bytestream2_tell
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition: bytestream.h:192
R3
#define R3
Definition: simple_idct.c:168
zork_index_table
static const int8_t zork_index_table[8]
Definition: adpcm.c:235
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:124
ff_adpcm_AdaptationTable
const int16_t ff_adpcm_AdaptationTable[]
Definition: adpcm_data.c:54
bytestream2_skipu
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
int64_t
long long int64_t
Definition: coverity.c:34
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:250
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
bytestream2_seek
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
step
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
Definition: rate_distortion.txt:58
u
#define u(width, name, range_min, range_max)
Definition: cbs_apv.c:68
AV_CODEC_ID_ADPCM_IMA_CUNNING
@ AV_CODEC_ID_ADPCM_IMA_CUNNING
Definition: codec_id.h:424
AVPacket::data
uint8_t * data
Definition: packet.h:558
table
static const uint16_t table[]
Definition: prosumer.c:203
AV_CODEC_ID_ADPCM_EA_R3
@ AV_CODEC_ID_ADPCM_EA_R3
Definition: codec_id.h:397
AV_CODEC_ID_ADPCM_AICA
@ AV_CODEC_ID_ADPCM_AICA
Definition: codec_id.h:414
AV_CODEC_ID_ADPCM_IMA_OKI
@ AV_CODEC_ID_ADPCM_IMA_OKI
Definition: codec_id.h:408
filter
void(* filter)(uint8_t *src, int stride, int qscale)
Definition: h263dsp.c:29
R1
#define R1
Definition: simple_idct.c:166
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
AV_CODEC_ID_ADPCM_XMD
@ AV_CODEC_ID_ADPCM_XMD
Definition: codec_id.h:427
adpcm_sanyo_expand4
static int adpcm_sanyo_expand4(ADPCMChannelStatus *c, int bits)
Definition: adpcm.c:894
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:512
AV_CODEC_ID_ADPCM_THP_LE
@ AV_CODEC_ID_ADPCM_THP_LE
Definition: codec_id.h:412
adpcm_sbpro_expand_nibble
static int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
Definition: adpcm.c:610
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:56
AV_CODEC_ID_ADPCM_CT
@ AV_CODEC_ID_ADPCM_CT
Definition: codec_id.h:388
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:333
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:440
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1039
GetBitContext
Definition: get_bits.h:109
adpcm_ima_mtf_expand_nibble
static int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition: adpcm.c:464
adpcm_ima_expand_nibble
static int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
Definition: adpcm.c:415
val
static double val(void *priv, double ch)
Definition: aeval.c:77
ff_adpcm_ima_block_sizes
static const uint8_t ff_adpcm_ima_block_sizes[4]
Definition: adpcm_data.h:31
ff_adpcm_ima_qt_expand_nibble
int16_t ff_adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition: adpcm.c:523
AV_CODEC_ID_ADPCM_SBPRO_2
@ AV_CODEC_ID_ADPCM_SBPRO_2
Definition: codec_id.h:393
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:539
sample_fmts_s16p
static enum AVSampleFormat sample_fmts_s16p[]
Definition: adpcm.c:2589
adpcm_ima_alp_expand_nibble
static int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
Definition: adpcm.c:441
adpcm_yamaha_expand_nibble
static int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition: adpcm.c:630
ADPCMChannelStatus::sample1
int sample1
Definition: adpcm.h:39
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:515
AV_CODEC_ID_ADPCM_IMA_ACORN
@ AV_CODEC_ID_ADPCM_IMA_ACORN
Definition: codec_id.h:426
adpcm_zork_expand_nibble
static int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition: adpcm.c:653
adpcm_data.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
offsets
static const int offsets[]
Definition: hevc_pel.c:34
AV_CODEC_ID_ADPCM_AFC
@ AV_CODEC_ID_ADPCM_AFC
Definition: codec_id.h:407
AV_CODEC_ID_ADPCM_IMA_EA_SEAD
@ AV_CODEC_ID_ADPCM_IMA_EA_SEAD
Definition: codec_id.h:399
g
const char * g
Definition: vf_curves.c:128
AV_CODEC_ID_ADPCM_IMA_DK3
@ AV_CODEC_ID_ADPCM_IMA_DK3
Definition: codec_id.h:378
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
AV_CODEC_ID_ADPCM_IMA_APC
@ AV_CODEC_ID_ADPCM_IMA_APC
Definition: codec_id.h:405
get_bits_le
static unsigned int get_bits_le(GetBitContext *s, int n)
Definition: get_bits.h:354
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:318
AV_CODEC_ID_ADPCM_IMA_ISS
@ AV_CODEC_ID_ADPCM_IMA_ISS
Definition: codec_id.h:403
channels
channels
Definition: aptx.h:31
decode.h
get_bits.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AV_CODEC_ID_ADPCM_IMA_SMJPEG
@ AV_CODEC_ID_ADPCM_IMA_SMJPEG
Definition: codec_id.h:381
adpcm_ms_expand_nibble
static int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition: adpcm.c:549
AV_CODEC_ID_ADPCM_IMA_XBOX
@ AV_CODEC_ID_ADPCM_IMA_XBOX
Definition: codec_id.h:428
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:441
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
if
if(ret)
Definition: filter_design.txt:179
ff_adpcm_ima_block_samples
static const uint8_t ff_adpcm_ima_block_samples[4]
Definition: adpcm_data.h:32
sample_fmts_s16
static enum AVSampleFormat sample_fmts_s16[]
Definition: adpcm.c:2587
AV_CODEC_ID_ADPCM_EA_XAS
@ AV_CODEC_ID_ADPCM_EA_XAS
Definition: codec_id.h:401
av_clip_int16
#define av_clip_int16
Definition: common.h:115
NULL
#define NULL
Definition: coverity.c:32
ADPCM_DECODER
#define ADPCM_DECODER(codec, name, sample_fmts, long_name)
Definition: adpcm.c:2613
bits_left
#define bits_left
Definition: bitstream.h:116
av_clip_intp2
#define av_clip_intp2
Definition: common.h:121
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AV_CODEC_ID_ADPCM_YAMAHA
@ AV_CODEC_ID_ADPCM_YAMAHA
Definition: codec_id.h:390
oki_step_table
static const int16_t oki_step_table[49]
Definition: adpcm.c:219
AV_CODEC_ID_ADPCM_IMA_WS
@ AV_CODEC_ID_ADPCM_IMA_WS
Definition: codec_id.h:380
av_unreachable
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition: avassert.h:108
AV_CODEC_ID_ADPCM_IMA_EA_EACS
@ AV_CODEC_ID_ADPCM_IMA_EA_EACS
Definition: codec_id.h:400
AV_CODEC_ID_ADPCM_ARGO
@ AV_CODEC_ID_ADPCM_ARGO
Definition: codec_id.h:418
AV_CODEC_ID_ADPCM_IMA_DK4
@ AV_CODEC_ID_ADPCM_IMA_DK4
Definition: codec_id.h:379
AV_CODEC_ID_ADPCM_IMA_AMV
@ AV_CODEC_ID_ADPCM_IMA_AMV
Definition: codec_id.h:395
abs
#define abs(x)
Definition: cuda_runtime.h:35
ea_adpcm_table
static const int16_t ea_adpcm_table[]
Definition: adpcm.c:97
ima_cunning_index_table
static const int8_t ima_cunning_index_table[9]
Definition: adpcm.c:111
exp
int8_t exp
Definition: eval.c:73
ADPCMChannelStatus::sample2
int sample2
Definition: adpcm.h:40
adpcm_sanyo_expand3
static int adpcm_sanyo_expand3(ADPCMChannelStatus *c, int bits)
Definition: adpcm.c:851
index
int index
Definition: gxfenc.c:90
c
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
AV_CODEC_ID_ADPCM_XA
@ AV_CODEC_ID_ADPCM_XA
Definition: codec_id.h:384
adpcm_ct_expand_nibble
static int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition: adpcm.c:589
adpcm.h
adpcm_ima_oki_expand_nibble
static int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition: adpcm.c:568
adpcm_decode_frame
static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: adpcm.c:1266
AV_CODEC_ID_ADPCM_ZORK
@ AV_CODEC_ID_ADPCM_ZORK
Definition: codec_id.h:420
afc_coeffs
static const int16_t afc_coeffs[2][16]
Definition: adpcm.c:92
adpcm_sanyo_expand5
static int adpcm_sanyo_expand5(ADPCMChannelStatus *c, int bits)
Definition: adpcm.c:953
ADPCMDecodeContext
Definition: adpcm.c:246
ff_adpcm_yamaha_difflookup
const int8_t ff_adpcm_yamaha_difflookup[]
Definition: adpcm_data.c:74
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1720
AVPacket::size
int size
Definition: packet.h:559
byte
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:99
codec_internal.h
shift
static int shift(int a, int b)
Definition: bonk.c:261
AV_CODEC_ID_ADPCM_IMA_RAD
@ AV_CODEC_ID_ADPCM_IMA_RAD
Definition: codec_id.h:410
adpcm_ima_cunning_expand_nibble
static int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition: adpcm.c:480
AV_CODEC_ID_ADPCM_IMA_ALP
@ AV_CODEC_ID_ADPCM_IMA_ALP
Definition: codec_id.h:422
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
bps
unsigned bps
Definition: movenc.c:1958
ff_adpcm_step_table
const int16_t ff_adpcm_step_table[89]
This is the step table.
Definition: adpcm_data.c:39
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1031
get_nb_samples
static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb, int buf_size, int *coded_samples, int *approx_nb_samples)
Get the number of samples (per channel) that will be decoded from the packet.
Definition: adpcm.c:1028
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
sample
#define sample
Definition: flacdsp_template.c:44
R2
#define R2
Definition: simple_idct.c:167
AV_CODEC_ID_ADPCM_SWF
@ AV_CODEC_ID_ADPCM_SWF
Definition: codec_id.h:389
size
int size
Definition: twinvq_data.h:10344
header
static const uint8_t header[24]
Definition: sdr2.c:68
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:166
attributes.h
xf
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:622
predictor
static void predictor(uint8_t *src, ptrdiff_t size)
Definition: exrenc.c:170
av_zero_extend
#define av_zero_extend
Definition: common.h:151
xa_decode
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:688
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
adpcm_index_table3
static const int8_t adpcm_index_table3[8]
Definition: adpcm.c:135
AVCodec::id
enum AVCodecID id
Definition: codec.h:186
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1546
sample_fmts_both
static enum AVSampleFormat sample_fmts_both[]
Definition: adpcm.c:2591
AV_CODEC_ID_ADPCM_MTAF
@ AV_CODEC_ID_ADPCM_MTAF
Definition: codec_id.h:416
AV_CODEC_ID_ADPCM_EA_MAXIS_XA
@ AV_CODEC_ID_ADPCM_EA_MAXIS_XA
Definition: codec_id.h:402
ff_adpcm_AdaptCoeff1
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:60
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_adpcm_AdaptCoeff2
const int8_t ff_adpcm_AdaptCoeff2[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:65
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:514
adpcm_index_tables
static const int8_t *const adpcm_index_tables[4]
Definition: adpcm.c:145
MT
#define MT(...)
Definition: codec_desc.c:32
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_CODEC_ID_ADPCM_IMA_APM
@ AV_CODEC_ID_ADPCM_IMA_APM
Definition: codec_id.h:421
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
ADPCMDecodeContext::vqa_version
int vqa_version
VQA version.
Definition: adpcm.c:248
AV_CODEC_ID_ADPCM_IMA_DAT4
@ AV_CODEC_ID_ADPCM_IMA_DAT4
Definition: codec_id.h:415
ff_adpcm_argo_expand_nibble
int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
Definition: adpcm.c:834
xa_adpcm_table
static const int8_t xa_adpcm_table[5][2]
Definition: adpcm.c:84
ff_adpcm_index_table
const int8_t ff_adpcm_index_table[16]
Definition: adpcm_data.c:30
AV_CODEC_ID_ADPCM_SANYO
@ AV_CODEC_ID_ADPCM_SANYO
Definition: codec_id.h:429
avcodec.h
AV_CODEC_ID_ADPCM_EA
@ AV_CODEC_ID_ADPCM_EA
Definition: codec_id.h:386
adpcm_flush
static void adpcm_flush(AVCodecContext *avctx)
Definition: adpcm.c:2545
AV_CODEC_ID_ADPCM_IMA_MTF
@ AV_CODEC_ID_ADPCM_IMA_MTF
Definition: codec_id.h:423
ret
ret
Definition: filter_design.txt:187
pred
static const float pred[4]
Definition: siprdata.h:259
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:1057
frame
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
Definition: filter_design.txt:265
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:555
flag
#define flag(name)
Definition: cbs_av1.c:496
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
left
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:386
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
adpcm_ima_wav_expand_nibble
static int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
Definition: adpcm.c:500
AVCodecContext
main external API structure.
Definition: avcodec.h:431
AV_CODEC_ID_ADPCM_AGM
@ AV_CODEC_ID_ADPCM_AGM
Definition: codec_id.h:417
mtaf_stepsize
static const int16_t mtaf_stepsize[32][16]
Definition: adpcm.c:152
ff_adpcm_yamaha_indexscale
const int16_t ff_adpcm_yamaha_indexscale[]
Definition: adpcm_data.c:69
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:132
AV_CODEC_ID_ADPCM_EA_R1
@ AV_CODEC_ID_ADPCM_EA_R1
Definition: codec_id.h:396
update
static av_always_inline void update(AVFilterContext *ctx, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
Definition: af_silencedetect.c:78
AV_CODEC_ID_ADPCM_EA_R2
@ AV_CODEC_ID_ADPCM_EA_R2
Definition: codec_id.h:398
temp
else temp
Definition: vf_mcdeint.c:271
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
AV_CODEC_ID_ADPCM_THP
@ AV_CODEC_ID_ADPCM_THP
Definition: codec_id.h:394
adpcm_index_table2
static const int8_t adpcm_index_table2[4]
Definition: adpcm.c:130
AV_CODEC_ID_ADPCM_SBPRO_4
@ AV_CODEC_ID_ADPCM_SBPRO_4
Definition: codec_id.h:391
adpcm_swf_decode
static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
Definition: adpcm.c:776
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
expand
static int expand(AVFilterContext *ctx, double *pz, int n, double *coefs)
Definition: af_aiir.c:499
AV_CODEC_ID_ADPCM_IMA_SSI
@ AV_CODEC_ID_ADPCM_IMA_SSI
Definition: codec_id.h:419
adpcm_decode_init
static av_cold int adpcm_decode_init(AVCodecContext *avctx)
Definition: adpcm.c:254
ADPCMDecodeContext::has_status
int has_status
Status flag.
Definition: adpcm.c:249
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:273
AV_CODEC_ID_ADPCM_IMA_MOFLEX
@ AV_CODEC_ID_ADPCM_IMA_MOFLEX
Definition: codec_id.h:425
AVPacket
This structure stores compressed data.
Definition: packet.h:535
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
AV_CODEC_ID_ADPCM_IMA_WAV
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition: codec_id.h:377
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
ADPCMChannelStatus::predictor
int predictor
Definition: adpcm.h:32
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:80
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_ADPCM_4XM
@ AV_CODEC_ID_ADPCM_4XM
Definition: codec_id.h:383
adpcm_agm_expand_nibble
static int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition: adpcm.c:371
AV_CODEC_ID_ADPCM_PSX
@ AV_CODEC_ID_ADPCM_PSX
Definition: codec_id.h:413
adpcm_mtaf_expand_nibble
static int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition: adpcm.c:644
CASE
#define CASE(codec,...)
Definition: adpcm.c:80
ima_cunning_step_table
static const int16_t ima_cunning_step_table[61]
Definition: adpcm.c:121
ADPCMChannelStatus
Definition: adpcm.h:31
mtf_index_table
static const int8_t mtf_index_table[16]
Definition: adpcm.c:239
channel
channel
Definition: ebur128.h:39
AV_CODEC_ID_ADPCM_SBPRO_3
@ AV_CODEC_ID_ADPCM_SBPRO_3
Definition: codec_id.h:392
ADPCMDecodeContext::status
ADPCMChannelStatus status[14]
Definition: adpcm.c:247
swf_index_tables
static const int8_t swf_index_tables[4][16]
Definition: adpcm.c:228