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) {
265  max_channels = 1;
266  break;
268  max_channels = 2;
269  break;
276  max_channels = 6;
277  break;
279  min_channels = 2;
280  max_channels = 8;
281  if (avctx->ch_layout.nb_channels & 1) {
282  avpriv_request_sample(avctx, "channel count %d", avctx->ch_layout.nb_channels);
283  return AVERROR_PATCHWELCOME;
284  }
285  break;
287  min_channels = 2;
288  break;
290  max_channels = 8;
291  if (avctx->ch_layout.nb_channels <= 0 ||
292  avctx->block_align % (16 * avctx->ch_layout.nb_channels))
293  return AVERROR_INVALIDDATA;
294  break;
296  max_channels = 8;
297  if (avctx->ch_layout.nb_channels <= 0 || avctx->block_align <= 0 ||
298  avctx->block_align % avctx->ch_layout.nb_channels)
299  return AVERROR_INVALIDDATA;
300  break;
304  max_channels = 14;
305  break;
306  }
307  if (avctx->ch_layout.nb_channels < min_channels ||
308  avctx->ch_layout.nb_channels > max_channels) {
309  av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
310  return AVERROR(EINVAL);
311  }
312 
313  switch(avctx->codec->id) {
315  if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
316  return AVERROR_INVALIDDATA;
317  break;
319  if (avctx->bits_per_coded_sample != 4 ||
320  avctx->block_align != 17 * avctx->ch_layout.nb_channels)
321  return AVERROR_INVALIDDATA;
322  break;
324  if (avctx->bits_per_coded_sample < 3 || avctx->bits_per_coded_sample > 5)
325  return AVERROR_INVALIDDATA;
326  break;
328  if (avctx->bits_per_coded_sample != 4)
329  return AVERROR_INVALIDDATA;
330  break;
332  if (avctx->bits_per_coded_sample != 8)
333  return AVERROR_INVALIDDATA;
334  break;
335  default:
336  break;
337  }
338 
339  switch (avctx->codec->id) {
365  break;
367  avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
369  break;
371  avctx->sample_fmt = avctx->ch_layout.nb_channels > 2 ? AV_SAMPLE_FMT_S16P :
373  break;
374  default:
375  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
376  }
377  return 0;
378 }
379 
380 static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
381 {
382  int delta, pred, step, add;
383 
384  pred = c->predictor;
385  delta = nibble & 7;
386  step = c->step;
387  add = (delta * 2 + 1) * step;
388  if (add < 0)
389  add = add + 7;
390 
391  if ((nibble & 8) == 0)
392  pred = av_clip(pred + (add >> 3), -32767, 32767);
393  else
394  pred = av_clip(pred - (add >> 3), -32767, 32767);
395 
396  switch (delta) {
397  case 7:
398  step *= 0x99;
399  break;
400  case 6:
401  c->step = av_clip(c->step * 2, 127, 24576);
402  c->predictor = pred;
403  return pred;
404  case 5:
405  step *= 0x66;
406  break;
407  case 4:
408  step *= 0x4d;
409  break;
410  default:
411  step *= 0x39;
412  break;
413  }
414 
415  if (step < 0)
416  step += 0x3f;
417 
418  c->step = step >> 6;
419  c->step = av_clip(c->step, 127, 24576);
420  c->predictor = pred;
421  return pred;
422 }
423 
424 static inline int16_t adpcm_ima_escape_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
425 {
426  int step_index;
427  int predictor;
428  int sign, delta, diff, step;
429 
430  step = ff_adpcm_step_table[c->step_index];
431  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
432  step_index = av_clip(step_index, 0, 88);
433 
434  sign = nibble & 8;
435  delta = nibble & 7;
436  diff = (delta * step) >> 2;
437  predictor = c->predictor;
438  if (sign) predictor -= diff;
439  else predictor += diff;
440 
441  c->predictor = av_clip_int16(predictor);
442  c->step_index = step_index;
443 
444  return (int16_t)c->predictor;
445 }
446 
447 static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
448 {
449  int step_index;
450  int predictor;
451  int sign, delta, diff, step;
452 
453  step = ff_adpcm_step_table[c->step_index];
454  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
455  step_index = av_clip(step_index, 0, 88);
456 
457  sign = nibble & 8;
458  delta = nibble & 7;
459  /* perform direct multiplication instead of series of jumps proposed by
460  * the reference ADPCM implementation since modern CPUs can do the mults
461  * quickly enough */
462  diff = ((2 * delta + 1) * step) >> shift;
463  predictor = c->predictor;
464  if (sign) predictor -= diff;
465  else predictor += diff;
466 
467  c->predictor = av_clip_int16(predictor);
468  c->step_index = step_index;
469 
470  return (int16_t)c->predictor;
471 }
472 
473 static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
474 {
475  int step_index;
476  int predictor;
477  int sign, delta, diff, step;
478 
479  step = ff_adpcm_step_table[c->step_index];
480  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
481  step_index = av_clip(step_index, 0, 88);
482 
483  sign = nibble & 8;
484  delta = nibble & 7;
485  diff = (delta * step) >> shift;
486  predictor = c->predictor;
487  if (sign) predictor -= diff;
488  else predictor += diff;
489 
490  c->predictor = av_clip_int16(predictor);
491  c->step_index = step_index;
492 
493  return (int16_t)c->predictor;
494 }
495 
496 static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
497 {
498  int step_index, step, delta, predictor;
499 
500  step = ff_adpcm_step_table[c->step_index];
501 
502  delta = step * (2 * nibble - 15);
503  predictor = c->predictor + delta;
504 
505  step_index = c->step_index + mtf_index_table[(unsigned)nibble];
506  c->predictor = av_clip_int16(predictor >> 4);
507  c->step_index = av_clip(step_index, 0, 88);
508 
509  return (int16_t)c->predictor;
510 }
511 
512 static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
513 {
514  int step_index;
515  int predictor;
516  int step;
517 
518  nibble = sign_extend(nibble & 0xF, 4);
519 
520  step = ima_cunning_step_table[c->step_index];
521  step_index = c->step_index + ima_cunning_index_table[abs(nibble)];
522  step_index = av_clip(step_index, 0, 60);
523 
524  predictor = c->predictor + step * nibble;
525 
526  c->predictor = av_clip_int16(predictor);
527  c->step_index = step_index;
528 
529  return c->predictor;
530 }
531 
533 {
534  int nibble, step_index, predictor, sign, delta, diff, step, shift;
535 
536  shift = bps - 1;
537  nibble = get_bits_le(gb, bps),
538  step = ff_adpcm_step_table[c->step_index];
539  step_index = c->step_index + adpcm_index_tables[bps - 2][nibble];
540  step_index = av_clip(step_index, 0, 88);
541 
542  sign = nibble & (1 << shift);
543  delta = av_zero_extend(nibble, shift);
544  diff = step >> shift;
545  for (int i = 0; i < shift; i++)
546  diff += (step >> (shift-1-i)) * !!(delta & (1 << i));
547  predictor = c->predictor;
548  if (sign) predictor -= diff;
549  else predictor += diff;
550 
551  c->predictor = av_clip_int16(predictor);
552  c->step_index = step_index;
553 
554  return (int16_t)c->predictor;
555 }
556 
558 {
559  int step_index;
560  int predictor;
561  int diff, step;
562 
563  step = ff_adpcm_step_table[c->step_index];
564  step_index = c->step_index + ff_adpcm_index_table[nibble];
565  step_index = av_clip(step_index, 0, 88);
566 
567  diff = step >> 3;
568  if (nibble & 4) diff += step;
569  if (nibble & 2) diff += step >> 1;
570  if (nibble & 1) diff += step >> 2;
571 
572  if (nibble & 8)
573  predictor = c->predictor - diff;
574  else
575  predictor = c->predictor + diff;
576 
577  c->predictor = av_clip_int16(predictor);
578  c->step_index = step_index;
579 
580  return c->predictor;
581 }
582 
583 static void decode_adpcm_ima_hvqm2(AVCodecContext *avctx, int16_t *outbuf, int samples_to_do,
584  int frame_format, GetByteContext *gb)
585 {
586  ADPCMDecodeContext *c = avctx->priv_data;
587  int st = avctx->ch_layout.nb_channels == 2;
588  uint8_t nibble;
589 
590  for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
591  unsigned tmp;
592 
593  switch (frame_format) {
594  case 0: /* combined hist+index */
595  tmp = bytestream2_get_be16(gb);
596  c->status[ch].predictor = sign_extend(tmp & 0xFF80, 16);
597  c->status[ch].step_index = tmp & 0x7f;
598  *outbuf++ = c->status[ch].predictor;
599  samples_to_do--;
600  break;
601  default:
602  break;
603  }
604 
605  c->status[ch].step_index = av_clip(c->status[ch].step_index, 0, 88);
606  }
607 
608  for (int i = 0; i < samples_to_do; i++) {
609  if (!(i&1)) {
610  nibble = bytestream2_get_byte(gb);
611  *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], nibble >> 4);
612  } else {
613  *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[ 0], nibble & 0xF);
614  }
615  }
616 
617  bytestream2_seek(gb, 0, SEEK_END);
618 }
619 
620 static void decode_adpcm_ima_hvqm4(AVCodecContext *avctx, int16_t *outbuf, int samples_to_do,
621  int frame_format, GetByteContext *gb)
622 {
623  ADPCMDecodeContext *c = avctx->priv_data;
624  int st = avctx->ch_layout.nb_channels == 2;
625  unsigned tmp;
626 
627  for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
628  switch (frame_format) {
629  case 1: /* combined hist+index */
630  tmp = bytestream2_get_be16(gb);
631  c->status[ch].predictor = sign_extend(tmp & 0xFF80, 16);
632  c->status[ch].step_index = tmp & 0x7f;
633  break;
634  case 2: /* no hist/index (continues from previous frame) */
635  default:
636  break;
637  case 3: /* separate hist+index */
638  tmp = bytestream2_get_be16(gb);
639  c->status[ch].predictor = sign_extend(tmp, 16);
640  c->status[ch].step_index = bytestream2_get_byte(gb);
641  break;
642  }
643 
644  c->status[ch].step_index = av_clip(c->status[ch].step_index, 0, 88);
645  }
646 
647  if (frame_format == 1 || frame_format == 3) {
648  for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
649  *outbuf++ = (int16_t)c->status[st - ch].predictor;
650  samples_to_do--;
651  }
652 
653  for (int i = 0; i < samples_to_do; i += 1+(!st)) {
654  uint8_t nibble = bytestream2_get_byte(gb);
655 
656  *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], nibble & 0xF);
657  *outbuf++ = ff_adpcm_ima_qt_expand_nibble(&c->status[ 0], nibble >> 4);
658  }
659 
660  bytestream2_seek(gb, 0, SEEK_END);
661 }
662 
663 static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
664 {
665  int predictor;
666 
667  predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
668  predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
669 
670  c->sample2 = c->sample1;
671  c->sample1 = av_clip_int16(predictor);
672  c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
673  if (c->idelta < 16) c->idelta = 16;
674  if (c->idelta > INT_MAX/768) {
675  av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
676  c->idelta = INT_MAX/768;
677  }
678 
679  return c->sample1;
680 }
681 
682 static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
683 {
684  int step_index, predictor, sign, delta, diff, step;
685 
686  step = oki_step_table[c->step_index];
687  step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
688  step_index = av_clip(step_index, 0, 48);
689 
690  sign = nibble & 8;
691  delta = nibble & 7;
692  diff = ((2 * delta + 1) * step) >> 3;
693  predictor = c->predictor;
694  if (sign) predictor -= diff;
695  else predictor += diff;
696 
697  c->predictor = av_clip_intp2(predictor, 11);
698  c->step_index = step_index;
699 
700  return c->predictor * 16;
701 }
702 
703 static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
704 {
705  int sign, delta, diff;
706  int new_step;
707 
708  sign = nibble & 8;
709  delta = nibble & 7;
710  /* perform direct multiplication instead of series of jumps proposed by
711  * the reference ADPCM implementation since modern CPUs can do the mults
712  * quickly enough */
713  diff = ((2 * delta + 1) * c->step) >> 3;
714  /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
715  c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
716  c->predictor = av_clip_int16(c->predictor);
717  /* calculate new step and clamp it to range 511..32767 */
718  new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
719  c->step = av_clip(new_step, 511, 32767);
720 
721  return (int16_t)c->predictor;
722 }
723 
724 static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
725 {
726  int sign, delta, diff;
727 
728  sign = nibble & (1<<(size-1));
729  delta = nibble & ((1<<(size-1))-1);
730  diff = delta << (7 + c->step + shift);
731 
732  /* clamp result */
733  c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
734 
735  /* calculate new step */
736  if (delta >= (2*size - 3) && c->step < 3)
737  c->step++;
738  else if (delta == 0 && c->step > 0)
739  c->step--;
740 
741  return (int16_t) c->predictor;
742 }
743 
744 static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
745 {
746  if(!c->step) {
747  c->predictor = 0;
748  c->step = 127;
749  }
750 
751  c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
752  c->predictor = av_clip_int16(c->predictor);
753  c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
754  c->step = av_clip(c->step, 127, 24576);
755  return c->predictor;
756 }
757 
758 static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
759 {
760  c->predictor += mtaf_stepsize[c->step][nibble];
761  c->predictor = av_clip_int16(c->predictor);
762  c->step += ff_adpcm_index_table[nibble];
763  c->step = av_clip_uintp2(c->step, 5);
764  return c->predictor;
765 }
766 
767 static inline int16_t adpcm_circus_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
768 {
769  int32_t sample = c->predictor;
770  int32_t scale = c->step;
771  int32_t code = sign_extend(nibble, 8);
772 
773  sample += code * (1 << scale);
774  if (code == 0) {
775  scale--;
776  } else if (code == 127 || code == -128) {
777  scale++;
778  }
779  scale = av_clip(scale, 0, 8);
781 
782  c->predictor = sample;
783  c->step = scale;
784 
785  return sample;
786 }
787 
788 static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
789 {
790  int16_t index = c->step_index;
791  uint32_t lookup_sample = ff_adpcm_step_table[index];
792  int32_t sample = 0;
793 
794  if (nibble & 0x40)
795  sample += lookup_sample;
796  if (nibble & 0x20)
797  sample += lookup_sample >> 1;
798  if (nibble & 0x10)
799  sample += lookup_sample >> 2;
800  if (nibble & 0x08)
801  sample += lookup_sample >> 3;
802  if (nibble & 0x04)
803  sample += lookup_sample >> 4;
804  if (nibble & 0x02)
805  sample += lookup_sample >> 5;
806  if (nibble & 0x01)
807  sample += lookup_sample >> 6;
808  if (nibble & 0x80)
809  sample = -sample;
810 
811  sample += c->predictor;
813 
814  index += zork_index_table[(nibble >> 4) & 7];
815  index = av_clip(index, 0, 88);
816 
817  c->predictor = sample;
818  c->step_index = index;
819 
820  return sample;
821 }
822 
823 static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
824  const uint8_t *in, ADPCMChannelStatus *left,
825  ADPCMChannelStatus *right, int channels, int sample_offset)
826 {
827  int i, j;
828  int shift,filter,f0,f1;
829  int s_1,s_2;
830  int d,s,t;
831 
832  out0 += sample_offset;
833  if (channels == 1)
834  out1 = out0 + 28;
835  else
836  out1 += sample_offset;
837 
838  for(i=0;i<4;i++) {
839  shift = 12 - (in[4+i*2] & 15);
840  filter = in[4+i*2] >> 4;
842  avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
843  filter=0;
844  }
845  if (shift < 0) {
846  avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
847  shift = 0;
848  }
849  f0 = xa_adpcm_table[filter][0];
850  f1 = xa_adpcm_table[filter][1];
851 
852  s_1 = left->sample1;
853  s_2 = left->sample2;
854 
855  for(j=0;j<28;j++) {
856  d = in[16+i+j*4];
857 
858  t = sign_extend(d, 4);
859  s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
860  s_2 = s_1;
861  s_1 = av_clip_int16(s);
862  out0[j] = s_1;
863  }
864 
865  if (channels == 2) {
866  left->sample1 = s_1;
867  left->sample2 = s_2;
868  s_1 = right->sample1;
869  s_2 = right->sample2;
870  }
871 
872  shift = 12 - (in[5+i*2] & 15);
873  filter = in[5+i*2] >> 4;
874  if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
875  avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
876  filter=0;
877  }
878  if (shift < 0) {
879  avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
880  shift = 0;
881  }
882 
883  f0 = xa_adpcm_table[filter][0];
884  f1 = xa_adpcm_table[filter][1];
885 
886  for(j=0;j<28;j++) {
887  d = in[16+i+j*4];
888 
889  t = sign_extend(d >> 4, 4);
890  s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
891  s_2 = s_1;
892  s_1 = av_clip_int16(s);
893  out1[j] = s_1;
894  }
895 
896  if (channels == 2) {
897  right->sample1 = s_1;
898  right->sample2 = s_2;
899  } else {
900  left->sample1 = s_1;
901  left->sample2 = s_2;
902  }
903 
904  out0 += 28 * (3 - channels);
905  out1 += 28 * (3 - channels);
906  }
907 
908  return 0;
909 }
910 
911 static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
912 {
913  ADPCMDecodeContext *c = avctx->priv_data;
914  GetBitContext gb;
915  const int8_t *table;
916  int channels = avctx->ch_layout.nb_channels;
917  int k0, signmask, nb_bits, count;
918  int size = buf_size*8;
919  int i;
920 
921  init_get_bits(&gb, buf, size);
922 
923  //read bits & initial values
924  nb_bits = get_bits(&gb, 2)+2;
925  table = swf_index_tables[nb_bits-2];
926  k0 = 1 << (nb_bits-2);
927  signmask = 1 << (nb_bits-1);
928 
929  while (get_bits_count(&gb) <= size - 22 * channels) {
930  for (i = 0; i < channels; i++) {
931  *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
932  c->status[i].step_index = get_bits(&gb, 6);
933  }
934 
935  for (count = 0; get_bits_count(&gb) <= size - nb_bits * channels && count < 4095; count++) {
936  int i;
937 
938  for (i = 0; i < channels; i++) {
939  // similar to IMA adpcm
940  int delta = get_bits(&gb, nb_bits);
941  int step = ff_adpcm_step_table[c->status[i].step_index];
942  int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
943  int k = k0;
944 
945  do {
946  if (delta & k)
947  vpdiff += step;
948  step >>= 1;
949  k >>= 1;
950  } while(k);
951  vpdiff += step;
952 
953  if (delta & signmask)
954  c->status[i].predictor -= vpdiff;
955  else
956  c->status[i].predictor += vpdiff;
957 
958  c->status[i].step_index += table[delta & (~signmask)];
959 
960  c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
961  c->status[i].predictor = av_clip_int16(c->status[i].predictor);
962 
963  *samples++ = c->status[i].predictor;
964  }
965  }
966  }
967 }
968 
969 int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
970 {
971  int sample = sign_extend(nibble, 4) * (1 << shift);
972 
973  if (flag)
974  sample += (8 * cs->sample1) - (4 * cs->sample2);
975  else
976  sample += 4 * cs->sample1;
977 
978  sample = av_clip_int16(sample >> 2);
979 
980  cs->sample2 = cs->sample1;
981  cs->sample1 = sample;
982 
983  return sample;
984 }
985 
987 {
988  int sign, delta, add;
989 
990  sign = bits & 4;
991  if (sign)
992  delta = 4 - (bits & 3);
993  else
994  delta = bits;
995 
996  switch (delta) {
997  case 0:
998  add = 0;
999  c->step = (3 * c->step) >> 2;
1000  break;
1001  case 1:
1002  add = c->step;
1003  c->step = (4 * c->step - (c->step >> 1)) >> 2;
1004  break;
1005  case 2:
1006  add = 2 * c->step;
1007  c->step = ((c->step >> 1) + add) >> 1;
1008  break;
1009  case 3:
1010  add = 4 * c->step - (c->step >> 1);
1011  c->step = 2 * c->step;
1012  break;
1013  case 4:
1014  add = (11 * c->step) >> 1;
1015  c->step = 3 * c->step;
1016  break;
1017  default:
1018  av_unreachable("There are cases for all control paths when bits is 3-bit");
1019  }
1020 
1021  if (sign)
1022  add = -add;
1023 
1024  c->predictor = av_clip_int16(c->predictor + add);
1025  c->step = av_clip(c->step, 1, 7281);
1026  return c->predictor;
1027 }
1028 
1030 {
1031  int sign, delta, add;
1032 
1033  sign = bits & 8;
1034  if (sign)
1035  delta = 8 - (bits & 7);
1036  else
1037  delta = bits;
1038 
1039  switch (delta) {
1040  case 0:
1041  add = 0;
1042  c->step = (3 * c->step) >> 2;
1043  break;
1044  case 1:
1045  add = c->step;
1046  c->step = (3 * c->step) >> 2;
1047  break;
1048  case 2:
1049  add = 2 * c->step;
1050  break;
1051  case 3:
1052  add = 3 * c->step;
1053  break;
1054  case 4:
1055  add = 4 * c->step;
1056  break;
1057  case 5:
1058  add = (11 * c->step) >> 1;
1059  c->step += c->step >> 2;
1060  break;
1061  case 6:
1062  add = (15 * c->step) >> 1;
1063  c->step = 2 * c->step;
1064  break;
1065  case 7:
1066  if (sign)
1067  add = (19 * c->step) >> 1;
1068  else
1069  add = (21 * c->step) >> 1;
1070  c->step = (c->step >> 1) + 2 * c->step;
1071  break;
1072  case 8:
1073  add = (25 * c->step) >> 1;
1074  c->step = 5 * c->step;
1075  break;
1076  default:
1077  av_unreachable("There are cases for all control paths when bits is 4-bit");
1078  }
1079 
1080  if (sign)
1081  add = -add;
1082 
1083  c->predictor = av_clip_int16(c->predictor + add);
1084  c->step = av_clip(c->step, 1, 2621);
1085  return c->predictor;
1086 }
1087 
1089 {
1090  int sign, delta, add;
1091 
1092  sign = bits & 0x10;
1093  if (sign)
1094  delta = 16 - (bits & 0xF);
1095  else
1096  delta = bits;
1097 
1098  add = delta * c->step;
1099  switch (delta) {
1100  case 0:
1101  c->step += (c->step >> 2) - (c->step >> 1);
1102  break;
1103  case 1:
1104  case 2:
1105  case 3:
1106  c->step += (c->step >> 3) - (c->step >> 2);
1107  break;
1108  case 4:
1109  case 5:
1110  c->step += (c->step >> 4) - (c->step >> 3);
1111  break;
1112  case 6:
1113  break;
1114  case 7:
1115  c->step += c->step >> 3;
1116  break;
1117  case 8:
1118  c->step += c->step >> 2;
1119  break;
1120  case 9:
1121  c->step += c->step >> 1;
1122  break;
1123  case 10:
1124  c->step = 2 * c->step - (c->step >> 3);
1125  break;
1126  case 11:
1127  c->step = 2 * c->step + (c->step >> 3);
1128  break;
1129  case 12:
1130  c->step = 2 * c->step + (c->step >> 1) - (c->step >> 3);
1131  break;
1132  case 13:
1133  c->step = 3 * c->step - (c->step >> 2);
1134  break;
1135  case 14:
1136  c->step *= 3;
1137  break;
1138  case 15:
1139  case 16:
1140  c->step = (7 * c->step) >> 1;
1141  break;
1142  }
1143 
1144  if (sign)
1145  add = -add;
1146 
1147  c->predictor = av_clip_int16(c->predictor + add);
1148  c->step = av_clip(c->step, 1, 1024);
1149  return c->predictor;
1150 }
1151 
1152 /**
1153  * Get the number of samples (per channel) that will be decoded from the packet.
1154  * In one case, this is actually the maximum number of samples possible to
1155  * decode with the given buf_size.
1156  *
1157  * @param[out] coded_samples set to the number of samples as coded in the
1158  * packet, or 0 if the codec does not encode the
1159  * number of samples in each frame.
1160  * @param[out] approx_nb_samples set to non-zero if the number of samples
1161  * returned is an approximation.
1162  */
1164  int buf_size, int *coded_samples, int *approx_nb_samples)
1165 {
1166  ADPCMDecodeContext *s = avctx->priv_data;
1167  int nb_samples = 0;
1168  int ch = avctx->ch_layout.nb_channels;
1169  int has_coded_samples = 0;
1170  int header_size;
1171 
1172  *coded_samples = 0;
1173  *approx_nb_samples = 0;
1174 
1175  if(ch <= 0)
1176  return 0;
1177  if (buf_size > INT_MAX / 14)
1178  return 0;
1179 
1180  switch (avctx->codec->id) {
1181  /* constant, only check buf_size */
1183  if (buf_size < 76 * ch)
1184  return 0;
1185  nb_samples = 128;
1186  break;
1188  if (buf_size < 34 * ch)
1189  return 0;
1190  nb_samples = 64;
1191  break;
1192  case AV_CODEC_ID_ADPCM_N64:
1193  nb_samples = (buf_size / 9) * 16;
1194  break;
1195  /* simple 4-bit adpcm */
1196  case AV_CODEC_ID_ADPCM_CT:
1209  nb_samples = buf_size * 2 / ch;
1210  break;
1211  }
1212  if (nb_samples)
1213  return nb_samples;
1214 
1215  /* simple 4-bit adpcm, with header */
1216  header_size = 0;
1217  switch (avctx->codec->id) {
1218  case AV_CODEC_ID_ADPCM_4XM:
1219  case AV_CODEC_ID_ADPCM_AGM:
1223  case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
1224  case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
1225  }
1226  if (header_size > 0)
1227  return (buf_size - header_size) * 2 / ch;
1228 
1229  /* more complex formats */
1230  switch (avctx->codec->id) {
1232  bytestream2_skip(gb, 4);
1233  has_coded_samples = 1;
1234  *coded_samples = bytestream2_get_le32u(gb);
1235  nb_samples = FFMIN((buf_size - 8) * 2, *coded_samples);
1236  bytestream2_seek(gb, -8, SEEK_CUR);
1237  break;
1238  case AV_CODEC_ID_ADPCM_EA:
1239  /* Stereo is 30 bytes per block */
1240  /* Mono is 15 bytes per block */
1241  has_coded_samples = 1;
1242  *coded_samples = bytestream2_get_le32(gb);
1243  *coded_samples -= *coded_samples % 28;
1244  nb_samples = (buf_size - 12) / (ch == 2 ? 30 : 15) * 28;
1245  break;
1247  nb_samples = ((bytestream2_peek_be64(gb) >> 16) & 0xFFFF);
1248  break;
1250  {
1251  int frame_format = bytestream2_get_be16(gb);
1252  int skip = 6;
1253 
1254  if (frame_format == 1)
1255  skip += 2 * ch;
1256  if (frame_format == 3)
1257  skip += 3 * ch;
1258 
1259  nb_samples = (buf_size - skip) * 2 / ch;
1260  bytestream2_seek(gb, 0, SEEK_SET);
1261  }
1262  break;
1264  has_coded_samples = 1;
1265  *coded_samples = bytestream2_get_le32(gb);
1266  nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
1267  break;
1269  nb_samples = (buf_size - ch) / ch * 2;
1270  break;
1274  /* maximum number of samples */
1275  /* has internal offsets and a per-frame switch to signal raw 16-bit */
1276  has_coded_samples = 1;
1277  switch (avctx->codec->id) {
1279  header_size = 4 + 9 * ch;
1280  *coded_samples = bytestream2_get_le32(gb);
1281  break;
1283  header_size = 4 + 5 * ch;
1284  *coded_samples = bytestream2_get_le32(gb);
1285  break;
1287  header_size = 4 + 5 * ch;
1288  *coded_samples = bytestream2_get_be32(gb);
1289  break;
1290  }
1291  *coded_samples -= *coded_samples % 28;
1292  nb_samples = (buf_size - header_size) * 2 / ch;
1293  nb_samples -= nb_samples % 28;
1294  *approx_nb_samples = 1;
1295  break;
1297  if (avctx->block_align > 0)
1298  buf_size = FFMIN(buf_size, avctx->block_align);
1299  nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
1300  break;
1302  if (avctx->block_align > 0)
1303  buf_size = FFMIN(buf_size, avctx->block_align);
1304  if (buf_size < 4 * ch)
1305  return AVERROR_INVALIDDATA;
1306  nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
1307  break;
1309  if (avctx->block_align > 0)
1310  buf_size = FFMIN(buf_size, avctx->block_align);
1311  nb_samples = (buf_size - 4 * ch) * 2 / ch;
1312  break;
1314  if (avctx->block_align > 0)
1315  buf_size = FFMIN(buf_size, avctx->block_align);
1316  nb_samples = (buf_size - 4 * ch) * 2 / ch;
1317  break;
1319  if (avctx->block_align > 0)
1320  buf_size = FFMIN(buf_size, avctx->block_align);
1321  nb_samples = (buf_size - 4 * ch) * 2 / ch;
1322  if (ch == 1) {
1323  avpriv_request_sample(avctx, "mono ADPCM Magix");
1324  return AVERROR_PATCHWELCOME;
1325  }
1326  break;
1327  CASE(ADPCM_IMA_WAV,
1328  int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1329  int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1330  if (avctx->block_align > 0)
1331  buf_size = FFMIN(buf_size, avctx->block_align);
1332  if (buf_size < 4 * ch)
1333  return AVERROR_INVALIDDATA;
1334  nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
1335  ) /* End of CASE */
1336  CASE(ADPCM_IMA_XBOX,
1337  int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1338  int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1339  if (avctx->block_align > 0)
1340  buf_size = FFMIN(buf_size, avctx->block_align);
1341  if (buf_size < 4 * ch)
1342  return AVERROR_INVALIDDATA;
1343  nb_samples = (buf_size - 4 * ch) / (bsize * ch) * bsamples + 1;
1344  ) /* End of CASE */
1345  case AV_CODEC_ID_ADPCM_MS:
1346  if (avctx->block_align > 0)
1347  buf_size = FFMIN(buf_size, avctx->block_align);
1348  nb_samples = (buf_size - 6 * ch) * 2 / ch;
1349  break;
1351  if (avctx->block_align > 0)
1352  buf_size = FFMIN(buf_size, avctx->block_align);
1353  nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
1354  break;
1358  {
1359  int samples_per_byte;
1360  switch (avctx->codec->id) {
1361  case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
1362  case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
1363  case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
1364  }
1365  if (!s->status[0].step_index) {
1366  if (buf_size < ch)
1367  return AVERROR_INVALIDDATA;
1368  nb_samples++;
1369  buf_size -= ch;
1370  }
1371  nb_samples += buf_size * samples_per_byte / ch;
1372  break;
1373  }
1374  case AV_CODEC_ID_ADPCM_SWF:
1375  {
1376  int buf_bits = buf_size * 8 - 2;
1377  int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
1378  int block_hdr_size = 22 * ch;
1379  int block_size = block_hdr_size + nbits * ch * 4095;
1380  int nblocks = buf_bits / block_size;
1381  int bits_left = buf_bits - nblocks * block_size;
1382  nb_samples = nblocks * 4096;
1383  if (bits_left >= block_hdr_size)
1384  nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
1385  break;
1386  }
1387  case AV_CODEC_ID_ADPCM_THP:
1389  if (avctx->extradata) {
1390  nb_samples = buf_size * 14 / (8 * ch);
1391  break;
1392  }
1393  has_coded_samples = 1;
1394  bytestream2_skip(gb, 4); // channel size
1395  *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
1396  bytestream2_get_le32(gb) :
1397  bytestream2_get_be32(gb);
1398  buf_size -= 8 + 36 * ch;
1399  buf_size /= ch;
1400  nb_samples = buf_size / 8 * 14;
1401  if (buf_size % 8 > 1)
1402  nb_samples += (buf_size % 8 - 1) * 2;
1403  *approx_nb_samples = 1;
1404  break;
1405  case AV_CODEC_ID_ADPCM_AFC:
1406  nb_samples = buf_size / (9 * ch) * 16;
1407  break;
1408  case AV_CODEC_ID_ADPCM_XA:
1409  nb_samples = (buf_size / 128) * 224 / ch;
1410  break;
1411  case AV_CODEC_ID_ADPCM_XMD:
1412  nb_samples = buf_size / (21 * ch) * 32;
1413  break;
1414  case AV_CODEC_ID_ADPCM_DTK:
1415  case AV_CODEC_ID_ADPCM_PSX:
1416  nb_samples = buf_size / (16 * ch) * 28;
1417  break;
1419  nb_samples = ((buf_size - 1) / ch) * 2;
1420  break;
1422  nb_samples = buf_size / avctx->block_align * 32;
1423  break;
1426  nb_samples = buf_size / ch;
1427  break;
1429  if (!avctx->extradata || avctx->extradata_size != 2)
1430  return AVERROR_INVALIDDATA;
1431  nb_samples = AV_RL16(avctx->extradata);
1432  break;
1433  }
1434 
1435  /* validate coded sample count */
1436  if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
1437  return AVERROR_INVALIDDATA;
1438 
1439  return nb_samples;
1440 }
1441 
1443  int *got_frame_ptr, AVPacket *avpkt)
1444 {
1445  const uint8_t *buf = avpkt->data;
1446  int buf_size = avpkt->size;
1447  ADPCMDecodeContext *c = avctx->priv_data;
1448  int channels = avctx->ch_layout.nb_channels;
1449  int16_t *samples;
1450  int16_t **samples_p;
1451  int st; /* stereo */
1452  int nb_samples, coded_samples, approx_nb_samples, ret;
1453  GetByteContext gb;
1454 
1455  bytestream2_init(&gb, buf, buf_size);
1456  nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
1457  if (nb_samples <= 0) {
1458  av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
1459  return AVERROR_INVALIDDATA;
1460  }
1461 
1462  /* get output buffer */
1463  frame->nb_samples = nb_samples;
1464  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1465  return ret;
1466  samples = (int16_t *)frame->data[0];
1467  samples_p = (int16_t **)frame->extended_data;
1468 
1469  /* use coded_samples when applicable */
1470  /* it is always <= nb_samples, so the output buffer will be large enough */
1471  if (coded_samples) {
1472  if (!approx_nb_samples && coded_samples != nb_samples)
1473  av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
1474  frame->nb_samples = nb_samples = coded_samples;
1475  }
1476 
1477  st = channels == 2 ? 1 : 0;
1478 
1479  switch(avctx->codec->id) {
1480  CASE(ADPCM_IMA_QT,
1481  /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
1482  Channel data is interleaved per-chunk. */
1483  for (int channel = 0; channel < channels; channel++) {
1484  ADPCMChannelStatus *cs = &c->status[channel];
1485  int predictor;
1486  int step_index;
1487  /* (pppppp) (piiiiiii) */
1488 
1489  /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
1490  predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
1491  step_index = predictor & 0x7F;
1492  predictor &= ~0x7F;
1493 
1494  if (cs->step_index == step_index) {
1495  int diff = predictor - cs->predictor;
1496  if (diff < 0)
1497  diff = - diff;
1498  if (diff > 0x7f)
1499  goto update;
1500  } else {
1501  update:
1502  cs->step_index = step_index;
1503  cs->predictor = predictor;
1504  }
1505 
1506  if (cs->step_index > 88u){
1507  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1508  channel, cs->step_index);
1509  return AVERROR_INVALIDDATA;
1510  }
1511 
1512  samples = samples_p[channel];
1513 
1514  for (int m = 0; m < 64; m += 2) {
1515  int byte = bytestream2_get_byteu(&gb);
1516  samples[m ] = ff_adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
1517  samples[m + 1] = ff_adpcm_ima_qt_expand_nibble(cs, byte >> 4 );
1518  }
1519  }
1520  ) /* End of CASE */
1521  CASE(ADPCM_IMA_WAV,
1522  for (int i = 0; i < channels; i++) {
1523  ADPCMChannelStatus *cs = &c->status[i];
1524  cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1525 
1526  cs->step_index = bytestream2_get_byteu(&gb);
1527  bytestream2_skipu(&gb, 1);
1528  if (cs->step_index > 88u){
1529  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1530  i, cs->step_index);
1531  return AVERROR_INVALIDDATA;
1532  }
1533  }
1534 
1535  if (avctx->bits_per_coded_sample != 4) {
1536  int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
1537  int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
1538  uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
1539  GetBitContext g;
1540 
1541  for (int n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
1542  for (int i = 0; i < channels; i++) {
1543  ADPCMChannelStatus *cs = &c->status[i];
1544  samples = &samples_p[i][1 + n * samples_per_block];
1545  for (int j = 0; j < block_size; j++) {
1546  temp[j] = buf[4 * channels + block_size * n * channels +
1547  (j % 4) + (j / 4) * (channels * 4) + i * 4];
1548  }
1549  ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
1550  if (ret < 0)
1551  return ret;
1552  for (int m = 0; m < samples_per_block; m++) {
1554  avctx->bits_per_coded_sample);
1555  }
1556  }
1557  }
1558  bytestream2_skip(&gb, avctx->block_align - channels * 4);
1559  } else {
1560  for (int n = 0; n < (nb_samples - 1) / 8; n++) {
1561  for (int i = 0; i < channels; i++) {
1562  ADPCMChannelStatus *cs = &c->status[i];
1563  samples = &samples_p[i][1 + n * 8];
1564  for (int m = 0; m < 8; m += 2) {
1565  int v = bytestream2_get_byteu(&gb);
1566  samples[m ] = ff_adpcm_ima_qt_expand_nibble(cs, v & 0x0F);
1567  samples[m + 1] = ff_adpcm_ima_qt_expand_nibble(cs, v >> 4);
1568  }
1569  }
1570  }
1571  }
1572  ) /* End of CASE */
1573  CASE(ADPCM_IMA_XBOX,
1574  for (int i = 0; i < channels; i++) {
1575  ADPCMChannelStatus *cs = &c->status[i];
1576  cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
1577 
1578  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1579  if (cs->step_index > 88u) {
1580  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1581  i, cs->step_index);
1582  return AVERROR_INVALIDDATA;
1583  }
1584  }
1585 
1586  for (int n = 0; n < (nb_samples-1) / 8; n++) {
1587  for (int i = 0; i < channels; i++) {
1588  ADPCMChannelStatus *cs = &c->status[i];
1589  samples = &samples_p[i][1 + n * 8];
1590  for (int m = 0; m < 8; m += 2) {
1591  int v = bytestream2_get_byteu(&gb);
1592  samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1593  samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1594  }
1595  }
1596  }
1597  frame->nb_samples--;
1598  ) /* End of CASE */
1599  CASE(ADPCM_4XM,
1600  for (int i = 0; i < channels; i++)
1601  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1602 
1603  for (int i = 0; i < channels; i++) {
1604  c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1605  if (c->status[i].step_index > 88u) {
1606  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1607  i, c->status[i].step_index);
1608  return AVERROR_INVALIDDATA;
1609  }
1610  }
1611 
1612  for (int i = 0; i < channels; i++) {
1613  ADPCMChannelStatus *cs = &c->status[i];
1614  samples = (int16_t *)frame->data[i];
1615  for (int n = nb_samples >> 1; n > 0; n--) {
1616  int v = bytestream2_get_byteu(&gb);
1617  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
1618  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
1619  }
1620  }
1621  ) /* End of CASE */
1622  CASE(ADPCM_AGM,
1623  for (int i = 0; i < channels; i++)
1624  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1625  for (int i = 0; i < channels; i++)
1626  c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
1627 
1628  for (int n = 0; n < nb_samples >> (1 - st); n++) {
1629  int v = bytestream2_get_byteu(&gb);
1630  *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
1631  *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
1632  }
1633  ) /* End of CASE */
1634  CASE(ADPCM_MS,
1635  int block_predictor;
1636 
1637  if (avctx->ch_layout.nb_channels > 2) {
1638  for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
1639  samples = samples_p[channel];
1640  block_predictor = bytestream2_get_byteu(&gb);
1641  if (block_predictor > 6) {
1642  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1643  channel, block_predictor);
1644  return AVERROR_INVALIDDATA;
1645  }
1646  c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1647  c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1648  c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1649  c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1650  c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1651  *samples++ = c->status[channel].sample2;
1652  *samples++ = c->status[channel].sample1;
1653  for (int n = (nb_samples - 2) >> 1; n > 0; n--) {
1654  int byte = bytestream2_get_byteu(&gb);
1655  *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
1656  *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1657  }
1658  }
1659  } else {
1660  block_predictor = bytestream2_get_byteu(&gb);
1661  if (block_predictor > 6) {
1662  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1663  block_predictor);
1664  return AVERROR_INVALIDDATA;
1665  }
1666  c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1667  c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1668  if (st) {
1669  block_predictor = bytestream2_get_byteu(&gb);
1670  if (block_predictor > 6) {
1671  av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1672  block_predictor);
1673  return AVERROR_INVALIDDATA;
1674  }
1675  c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1676  c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1677  }
1678  c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1679  if (st){
1680  c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
1681  }
1682 
1683  c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1684  if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
1685  c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1686  if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
1687 
1688  *samples++ = c->status[0].sample2;
1689  if (st) *samples++ = c->status[1].sample2;
1690  *samples++ = c->status[0].sample1;
1691  if (st) *samples++ = c->status[1].sample1;
1692  for (int n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1693  int byte = bytestream2_get_byteu(&gb);
1694  *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
1695  *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1696  }
1697  }
1698  ) /* End of CASE */
1699  CASE(ADPCM_MTAF,
1700  for (int channel = 0; channel < channels; channel += 2) {
1701  bytestream2_skipu(&gb, 4);
1702  c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
1703  c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
1704  c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1705  bytestream2_skipu(&gb, 2);
1706  c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1707  bytestream2_skipu(&gb, 2);
1708  for (int n = 0; n < nb_samples; n += 2) {
1709  int v = bytestream2_get_byteu(&gb);
1710  samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1711  samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
1712  }
1713  for (int n = 0; n < nb_samples; n += 2) {
1714  int v = bytestream2_get_byteu(&gb);
1715  samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1716  samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
1717  }
1718  }
1719  ) /* End of CASE */
1720  CASE(ADPCM_IMA_DK4,
1721  for (int channel = 0; channel < channels; channel++) {
1722  ADPCMChannelStatus *cs = &c->status[channel];
1723  cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
1724  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1725  if (cs->step_index > 88u){
1726  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1727  channel, cs->step_index);
1728  return AVERROR_INVALIDDATA;
1729  }
1730  }
1731  for (int n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1732  int v = bytestream2_get_byteu(&gb);
1733  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
1734  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1735  }
1736  ) /* End of CASE */
1737 
1738  /* DK3 ADPCM support macro */
1739 #define DK3_GET_NEXT_NIBBLE() \
1740  if (decode_top_nibble_next) { \
1741  nibble = last_byte >> 4; \
1742  decode_top_nibble_next = 0; \
1743  } else { \
1744  last_byte = bytestream2_get_byteu(&gb); \
1745  nibble = last_byte & 0x0F; \
1746  decode_top_nibble_next = 1; \
1747  }
1748  CASE(ADPCM_IMA_DK3,
1749  int last_byte = 0;
1750  int nibble;
1751  int decode_top_nibble_next = 0;
1752  int diff_channel;
1753  const int16_t *samples_end = samples + channels * nb_samples;
1754 
1755  bytestream2_skipu(&gb, 10);
1756  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1757  c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1758  c->status[0].step_index = bytestream2_get_byteu(&gb);
1759  c->status[1].step_index = bytestream2_get_byteu(&gb);
1760  if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
1761  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1762  c->status[0].step_index, c->status[1].step_index);
1763  return AVERROR_INVALIDDATA;
1764  }
1765  /* sign extend the predictors */
1766  diff_channel = c->status[1].predictor;
1767 
1768  while (samples < samples_end) {
1769 
1770  /* for this algorithm, c->status[0] is the sum channel and
1771  * c->status[1] is the diff channel */
1772 
1773  /* process the first predictor of the sum channel */
1775  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1776 
1777  /* process the diff channel predictor */
1779  adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1780 
1781  /* process the first pair of stereo PCM samples */
1782  diff_channel = (diff_channel + c->status[1].predictor) / 2;
1783  *samples++ = c->status[0].predictor + c->status[1].predictor;
1784  *samples++ = c->status[0].predictor - c->status[1].predictor;
1785 
1786  /* process the second predictor of the sum channel */
1788  adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1789 
1790  /* process the second pair of stereo PCM samples */
1791  diff_channel = (diff_channel + c->status[1].predictor) / 2;
1792  *samples++ = c->status[0].predictor + c->status[1].predictor;
1793  *samples++ = c->status[0].predictor - c->status[1].predictor;
1794  }
1795 
1796  if ((bytestream2_tell(&gb) & 1))
1797  bytestream2_skip(&gb, 1);
1798  ) /* End of CASE */
1799  CASE(ADPCM_IMA_MAGIX,
1800  for (int channel = 0; channel < channels; channel++) {
1801  ADPCMChannelStatus *cs = &c->status[channel];
1802  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1803  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1804  if (cs->step_index > 88u){
1805  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1806  channel, cs->step_index);
1807  return AVERROR_INVALIDDATA;
1808  }
1809  }
1810 
1811  for (int m = 0; m < channels*nb_samples/16; m ++) {
1812  uint32_t v0 = bytestream2_get_le32u(&gb);
1813  uint32_t v1 = bytestream2_get_le32u(&gb);
1814 
1815  for (int n = 8; n > 0; n--, v0 >>= 4, v1 >>= 4, samples += 2) {
1816  samples[0] = adpcm_ima_expand_nibble(&c->status[0], v0 & 15, 3);
1817  samples[1] = adpcm_ima_expand_nibble(&c->status[1], v1 & 15, 3);
1818  }
1819  }
1820  ) /* End of CASE */
1821  CASE(ADPCM_IMA_ISS,
1822  for (int channel = 0; channel < channels; channel++) {
1823  ADPCMChannelStatus *cs = &c->status[channel];
1824  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1825  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1826  if (cs->step_index > 88u){
1827  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1828  channel, cs->step_index);
1829  return AVERROR_INVALIDDATA;
1830  }
1831  }
1832 
1833  for (int n = nb_samples >> (1 - st); n > 0; n--) {
1834  int v1, v2;
1835  int v = bytestream2_get_byteu(&gb);
1836  /* nibbles are swapped for mono */
1837  if (st) {
1838  v1 = v >> 4;
1839  v2 = v & 0x0F;
1840  } else {
1841  v2 = v >> 4;
1842  v1 = v & 0x0F;
1843  }
1844  *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
1845  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1846  }
1847  ) /* End of CASE */
1848  CASE(ADPCM_IMA_MOFLEX,
1849  for (int channel = 0; channel < channels; channel++) {
1850  ADPCMChannelStatus *cs = &c->status[channel];
1851  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1852  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1853  if (cs->step_index > 88u){
1854  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1855  channel, cs->step_index);
1856  return AVERROR_INVALIDDATA;
1857  }
1858  }
1859 
1860  for (int subframe = 0; subframe < nb_samples / 256; subframe++) {
1861  for (int channel = 0; channel < channels; channel++) {
1862  samples = samples_p[channel] + 256 * subframe;
1863  for (int n = 0; n < 256; n += 2) {
1864  int v = bytestream2_get_byteu(&gb);
1865  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1866  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1867  }
1868  }
1869  }
1870  ) /* End of CASE */
1871  CASE(ADPCM_IMA_DAT4,
1872  for (int channel = 0; channel < channels; channel++) {
1873  ADPCMChannelStatus *cs = &c->status[channel];
1874  samples = samples_p[channel];
1875  bytestream2_skip(&gb, 4);
1876  for (int n = 0; n < nb_samples; n += 2) {
1877  int v = bytestream2_get_byteu(&gb);
1878  *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
1879  *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1880  }
1881  }
1882  ) /* End of CASE */
1883  CASE(ADPCM_IMA_APC,
1884  for (int n = nb_samples >> (1 - st); n > 0; n--) {
1885  int v = bytestream2_get_byteu(&gb);
1886  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
1887  *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1888  }
1889  ) /* End of CASE */
1890  CASE(ADPCM_IMA_HVQM2,
1891  int format = bytestream2_get_be16(&gb);
1892 
1893  bytestream2_skip(&gb, 4);
1894  decode_adpcm_ima_hvqm2(avctx, samples, nb_samples, format, &gb);
1895  ) /* End of CASE */
1896  CASE(ADPCM_IMA_HVQM4,
1897  int format = bytestream2_get_be16(&gb);
1898 
1899  bytestream2_skip(&gb, 4);
1900  decode_adpcm_ima_hvqm4(avctx, samples, nb_samples, format, &gb);
1901  ) /* End of CASE */
1902  CASE(ADPCM_IMA_SSI,
1903  for (int n = nb_samples >> (1 - st); n > 0; n--) {
1904  int v = bytestream2_get_byteu(&gb);
1905  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4 );
1906  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1907  }
1908  ) /* End of CASE */
1909  CASE(ADPCM_IMA_APM,
1910  for (int n = nb_samples / 2; n > 0; n--) {
1911  for (int channel = 0; channel < channels; channel++) {
1912  int v = bytestream2_get_byteu(&gb);
1913  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4 );
1914  samples[st] = ff_adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1915  }
1916  samples += channels;
1917  }
1918  ) /* End of CASE */
1919  CASE(ADPCM_IMA_ALP,
1920  for (int n = nb_samples / 2; n > 0; n--) {
1921  for (int channel = 0; channel < channels; channel++) {
1922  int v = bytestream2_get_byteu(&gb);
1923  *samples++ = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4 , 2);
1924  samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1925  }
1926  samples += channels;
1927  }
1928  ) /* End of CASE */
1929  CASE(ADPCM_IMA_CUNNING,
1930  for (int channel = 0; channel < channels; channel++) {
1931  int16_t *smp = samples_p[channel];
1932  for (int n = 0; n < nb_samples / 2; n++) {
1933  int v = bytestream2_get_byteu(&gb);
1934  *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v & 0x0F);
1935  *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v >> 4);
1936  }
1937  }
1938  ) /* End of CASE */
1939  CASE(ADPCM_IMA_OKI,
1940  for (int n = nb_samples >> (1 - st); n > 0; n--) {
1941  int v = bytestream2_get_byteu(&gb);
1942  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
1943  *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1944  }
1945  ) /* End of CASE */
1946  CASE(ADPCM_IMA_RAD,
1947  for (int channel = 0; channel < channels; channel++) {
1948  ADPCMChannelStatus *cs = &c->status[channel];
1949  cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
1950  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
1951  if (cs->step_index > 88u){
1952  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
1953  channel, cs->step_index);
1954  return AVERROR_INVALIDDATA;
1955  }
1956  }
1957  for (int n = 0; n < nb_samples / 2; n++) {
1958  int byte[2];
1959 
1960  byte[0] = bytestream2_get_byteu(&gb);
1961  if (st)
1962  byte[1] = bytestream2_get_byteu(&gb);
1963  for (int channel = 0; channel < channels; channel++) {
1964  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
1965  }
1966  for (int channel = 0; channel < channels; channel++) {
1967  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
1968  }
1969  }
1970  ) /* End of CASE */
1971  CASE(ADPCM_IMA_WS,
1972  if (c->vqa_version == 3) {
1973  for (int channel = 0; channel < channels; channel++) {
1974  int16_t *smp = samples_p[channel];
1975 
1976  for (int n = nb_samples / 2; n > 0; n--) {
1977  int v = bytestream2_get_byteu(&gb);
1978  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1979  *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1980  }
1981  }
1982  } else {
1983  for (int n = nb_samples / 2; n > 0; n--) {
1984  for (int channel = 0; channel < channels; channel++) {
1985  int v = bytestream2_get_byteu(&gb);
1986  *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1987  samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
1988  }
1989  samples += channels;
1990  }
1991  }
1992  bytestream2_seek(&gb, 0, SEEK_END);
1993  ) /* End of CASE */
1994  CASE(ADPCM_XMD,
1995  int bytes_remaining, block = 0;
1996  while (bytestream2_get_bytes_left(&gb) >= 21 * channels) {
1997  for (int channel = 0; channel < channels; channel++) {
1998  int16_t *out = samples_p[channel] + block * 32;
1999  int16_t history[2];
2000  uint16_t scale;
2001 
2002  history[1] = sign_extend(bytestream2_get_le16(&gb), 16);
2003  history[0] = sign_extend(bytestream2_get_le16(&gb), 16);
2004  scale = bytestream2_get_le16(&gb);
2005 
2006  out[0] = history[1];
2007  out[1] = history[0];
2008 
2009  for (int n = 0; n < 15; n++) {
2010  unsigned byte = bytestream2_get_byte(&gb);
2011  int32_t nibble[2];
2012 
2013  nibble[0] = sign_extend(byte & 15, 4);
2014  nibble[1] = sign_extend(byte >> 4, 4);
2015 
2016  out[2+n*2] = nibble[0]*scale + ((history[0]*3667 - history[1]*1642) >> 11);
2017  history[1] = history[0];
2018  history[0] = out[2+n*2];
2019 
2020  out[2+n*2+1] = nibble[1]*scale + ((history[0]*3667 - history[1]*1642) >> 11);
2021  history[1] = history[0];
2022  history[0] = out[2+n*2+1];
2023  }
2024  }
2025 
2026  block++;
2027  }
2028  bytes_remaining = bytestream2_get_bytes_left(&gb);
2029  if (bytes_remaining > 0) {
2030  bytestream2_skip(&gb, bytes_remaining);
2031  }
2032  ) /* End of CASE */
2033  CASE(ADPCM_XA,
2034  int16_t *out0 = samples_p[0];
2035  int16_t *out1 = samples_p[1];
2036  int samples_per_block = 28 * (3 - channels) * 4;
2037  int sample_offset = 0;
2038  int bytes_remaining;
2039  while (bytestream2_get_bytes_left(&gb) >= 128) {
2040  if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
2041  &c->status[0], &c->status[1],
2042  channels, sample_offset)) < 0)
2043  return ret;
2044  bytestream2_skipu(&gb, 128);
2045  sample_offset += samples_per_block;
2046  }
2047  /* Less than a full block of data left, e.g. when reading from
2048  * 2324 byte per sector XA; the remainder is padding */
2049  bytes_remaining = bytestream2_get_bytes_left(&gb);
2050  if (bytes_remaining > 0) {
2051  bytestream2_skip(&gb, bytes_remaining);
2052  }
2053  ) /* End of CASE */
2054  CASE(ADPCM_IMA_ESCAPE,
2055  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2056  int byte = bytestream2_get_byteu(&gb);
2057  *samples++ = adpcm_ima_escape_expand_nibble(&c->status[0], byte >> 4);
2058  *samples++ = adpcm_ima_escape_expand_nibble(&c->status[st], byte & 0xF);
2059  }
2060  ) /* End of CASE */
2061  CASE(ADPCM_IMA_EA_EACS,
2062  for (int i = 0; i <= st; i++) {
2063  c->status[i].step_index = bytestream2_get_le32u(&gb);
2064  if (c->status[i].step_index > 88u) {
2065  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
2066  i, c->status[i].step_index);
2067  return AVERROR_INVALIDDATA;
2068  }
2069  }
2070  for (int i = 0; i <= st; i++) {
2071  c->status[i].predictor = bytestream2_get_le32u(&gb);
2072  if (FFABS((int64_t)c->status[i].predictor) > (1<<16))
2073  return AVERROR_INVALIDDATA;
2074  }
2075 
2076  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2077  int byte = bytestream2_get_byteu(&gb);
2078  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
2079  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
2080  }
2081  ) /* End of CASE */
2082  CASE(ADPCM_IMA_EA_SEAD,
2083  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2084  int byte = bytestream2_get_byteu(&gb);
2085  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
2086  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
2087  }
2088  ) /* End of CASE */
2089  CASE(ADPCM_EA,
2090  int previous_left_sample, previous_right_sample;
2091  int current_left_sample, current_right_sample;
2092  int next_left_sample, next_right_sample;
2093  int coeff1l, coeff2l, coeff1r, coeff2r;
2094  int shift_left, shift_right;
2095 
2096  /* Each EA ADPCM frame has a 12-byte header followed by 30-byte (stereo) or 15-byte (mono) pieces,
2097  each coding 28 stereo/mono samples. */
2098 
2099  if (channels != 2 && channels != 1)
2100  return AVERROR_INVALIDDATA;
2101 
2102  current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2103  previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2104  current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2105  previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
2106 
2107  for (int count1 = 0; count1 < nb_samples / 28; count1++) {
2108  int byte = bytestream2_get_byteu(&gb);
2109  coeff1l = ea_adpcm_table[ byte >> 4 ];
2110  coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
2111  coeff1r = ea_adpcm_table[ byte & 0x0F];
2112  coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
2113 
2114  if (channels == 2){
2115  byte = bytestream2_get_byteu(&gb);
2116  shift_left = 20 - (byte >> 4);
2117  shift_right = 20 - (byte & 0x0F);
2118  } else{
2119  /* Mono packs the shift into the coefficient byte's lower nibble instead */
2120  shift_left = 20 - (byte & 0x0F);
2121  }
2122 
2123  for (int count2 = 0; count2 < (channels == 2 ? 28 : 14); count2++) {
2124  byte = bytestream2_get_byteu(&gb);
2125  next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
2126 
2127  next_left_sample = (next_left_sample +
2128  (current_left_sample * coeff1l) +
2129  (previous_left_sample * coeff2l) + 0x80) >> 8;
2130 
2131  previous_left_sample = current_left_sample;
2132  current_left_sample = av_clip_int16(next_left_sample);
2133  *samples++ = current_left_sample;
2134 
2135  if (channels == 2){
2136  next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
2137 
2138  next_right_sample = (next_right_sample +
2139  (current_right_sample * coeff1r) +
2140  (previous_right_sample * coeff2r) + 0x80) >> 8;
2141 
2142  previous_right_sample = current_right_sample;
2143  current_right_sample = av_clip_int16(next_right_sample);
2144  *samples++ = current_right_sample;
2145  } else {
2146  next_left_sample = sign_extend(byte, 4) * (1 << shift_left);
2147 
2148  next_left_sample = (next_left_sample +
2149  (current_left_sample * coeff1l) +
2150  (previous_left_sample * coeff2l) + 0x80) >> 8;
2151 
2152  previous_left_sample = current_left_sample;
2153  current_left_sample = av_clip_int16(next_left_sample);
2154 
2155  *samples++ = current_left_sample;
2156  }
2157  }
2158  }
2159  bytestream2_skip(&gb, channels == 2 ? 2 : 3); // Skip terminating NULs
2160  ) /* End of CASE */
2161  CASE(ADPCM_EA_MAXIS_XA,
2162  int coeff[2][2], shift[2];
2163 
2164  for (int channel = 0; channel < channels; channel++) {
2165  int byte = bytestream2_get_byteu(&gb);
2166  for (int i = 0; i < 2; i++)
2167  coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
2168  shift[channel] = 20 - (byte & 0x0F);
2169  }
2170  for (int count1 = 0; count1 < nb_samples / 2; count1++) {
2171  int byte[2];
2172 
2173  byte[0] = bytestream2_get_byteu(&gb);
2174  if (st) byte[1] = bytestream2_get_byteu(&gb);
2175  for (int i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
2176  for (int channel = 0; channel < channels; channel++) {
2177  int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
2178  sample = (sample +
2179  c->status[channel].sample1 * coeff[channel][0] +
2180  c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
2181  c->status[channel].sample2 = c->status[channel].sample1;
2182  c->status[channel].sample1 = av_clip_int16(sample);
2183  *samples++ = c->status[channel].sample1;
2184  }
2185  }
2186  }
2187  bytestream2_seek(&gb, 0, SEEK_END);
2188  ) /* End of CASE */
2189 #if CONFIG_ADPCM_EA_R1_DECODER || CONFIG_ADPCM_EA_R2_DECODER || CONFIG_ADPCM_EA_R3_DECODER
2192  case AV_CODEC_ID_ADPCM_EA_R3: {
2193  /* channel numbering
2194  2chan: 0=fl, 1=fr
2195  4chan: 0=fl, 1=rl, 2=fr, 3=rr
2196  6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
2197  const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
2198  int previous_sample, current_sample, next_sample;
2199  int coeff1, coeff2;
2200  int shift;
2201  uint16_t *samplesC;
2202  int count = 0;
2203  int offsets[6];
2204 
2205  for (unsigned channel = 0; channel < channels; channel++)
2206  offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
2207  bytestream2_get_le32(&gb)) +
2208  (channels + 1) * 4;
2209 
2210  for (unsigned channel = 0; channel < channels; channel++) {
2211  int count1;
2212 
2213  bytestream2_seek(&gb, offsets[channel], SEEK_SET);
2214  samplesC = samples_p[channel];
2215 
2216  if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
2217  current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
2218  previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
2219  } else {
2220  current_sample = c->status[channel].predictor;
2221  previous_sample = c->status[channel].prev_sample;
2222  }
2223 
2224  for (count1 = 0; count1 < nb_samples / 28; count1++) {
2225  int byte = bytestream2_get_byte(&gb);
2226  if (byte == 0xEE) { /* only seen in R2 and R3 */
2227  current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
2228  previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
2229 
2230  for (int count2 = 0; count2 < 28; count2++)
2231  *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
2232  } else {
2233  coeff1 = ea_adpcm_table[ byte >> 4 ];
2234  coeff2 = ea_adpcm_table[(byte >> 4) + 4];
2235  shift = 20 - (byte & 0x0F);
2236 
2237  for (int count2 = 0; count2 < 28; count2++) {
2238  if (count2 & 1)
2239  next_sample = (unsigned)sign_extend(byte, 4) << shift;
2240  else {
2241  byte = bytestream2_get_byte(&gb);
2242  next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
2243  }
2244 
2245  next_sample += (current_sample * coeff1) +
2246  (previous_sample * coeff2);
2247  next_sample = av_clip_int16(next_sample >> 8);
2248 
2249  previous_sample = current_sample;
2250  current_sample = next_sample;
2251  *samplesC++ = current_sample;
2252  }
2253  }
2254  }
2255  if (!count) {
2256  count = count1;
2257  } else if (count != count1) {
2258  av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
2259  count = FFMAX(count, count1);
2260  }
2261 
2262  if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
2263  c->status[channel].predictor = current_sample;
2264  c->status[channel].prev_sample = previous_sample;
2265  }
2266  }
2267 
2268  frame->nb_samples = count * 28;
2269  bytestream2_seek(&gb, 0, SEEK_END);
2270  break;
2271  }
2272 #endif /* CONFIG_ADPCM_EA_Rx_DECODER */
2273  CASE(ADPCM_EA_XAS,
2274  for (int channel=0; channel < channels; channel++) {
2275  int coeff[2][4], shift[4];
2276  int16_t *s = samples_p[channel];
2277  for (int n = 0; n < 4; n++, s += 32) {
2278  int val = sign_extend(bytestream2_get_le16u(&gb), 16);
2279  for (int i = 0; i < 2; i++)
2280  coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
2281  s[0] = val & ~0x0F;
2282 
2283  val = sign_extend(bytestream2_get_le16u(&gb), 16);
2284  shift[n] = 20 - (val & 0x0F);
2285  s[1] = val & ~0x0F;
2286  }
2287 
2288  for (int m = 2; m < 32; m += 2) {
2289  s = &samples_p[channel][m];
2290  for (int n = 0; n < 4; n++, s += 32) {
2291  int level, pred;
2292  int byte = bytestream2_get_byteu(&gb);
2293 
2294  level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
2295  pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
2296  s[0] = av_clip_int16((level + pred + 0x80) >> 8);
2297 
2298  level = sign_extend(byte, 4) * (1 << shift[n]);
2299  pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
2300  s[1] = av_clip_int16((level + pred + 0x80) >> 8);
2301  }
2302  }
2303  }
2304  ) /* End of CASE */
2305  CASE(ADPCM_IMA_ACORN,
2306  for (int channel = 0; channel < channels; channel++) {
2307  ADPCMChannelStatus *cs = &c->status[channel];
2308  cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2309  cs->step_index = bytestream2_get_le16u(&gb) & 0xFF;
2310  if (cs->step_index > 88u){
2311  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
2312  channel, cs->step_index);
2313  return AVERROR_INVALIDDATA;
2314  }
2315  }
2316  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2317  int byte = bytestream2_get_byteu(&gb);
2318  *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte & 0x0F, 3);
2319  *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte >> 4, 3);
2320  }
2321  ) /* End of CASE */
2322  CASE(ADPCM_IMA_AMV,
2323  av_assert0(channels == 1);
2324 
2325  /*
2326  * Header format:
2327  * int16_t predictor;
2328  * uint8_t step_index;
2329  * uint8_t reserved;
2330  * uint32_t frame_size;
2331  *
2332  * Some implementations have step_index as 16-bits, but others
2333  * only use the lower 8 and store garbage in the upper 8.
2334  */
2335  c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2336  c->status[0].step_index = bytestream2_get_byteu(&gb);
2337  bytestream2_skipu(&gb, 5);
2338  if (c->status[0].step_index > 88u) {
2339  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2340  c->status[0].step_index);
2341  return AVERROR_INVALIDDATA;
2342  }
2343 
2344  for (int n = nb_samples >> 1; n > 0; n--) {
2345  int v = bytestream2_get_byteu(&gb);
2346 
2347  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
2348  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
2349  }
2350 
2351  if (nb_samples & 1) {
2352  int v = bytestream2_get_byteu(&gb);
2353  *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
2354 
2355  if (v & 0x0F) {
2356  /* Holds true on all the http://samples.mplayerhq.hu/amv samples. */
2357  av_log(avctx, AV_LOG_WARNING, "Last nibble set on packet with odd sample count.\n");
2358  av_log(avctx, AV_LOG_WARNING, "Sample will be skipped.\n");
2359  }
2360  }
2361  ) /* End of CASE */
2362  CASE(ADPCM_IMA_PDA,
2363  for (int i = 0; i < channels; i++) {
2364  c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
2365  c->status[i].step_index = bytestream2_get_byteu(&gb);
2366  bytestream2_skipu(&gb, 1);
2367  if (c->status[i].step_index > 88u) {
2368  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2369  c->status[i].step_index);
2370  return AVERROR_INVALIDDATA;
2371  }
2372  }
2373 
2374  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2375  int v = bytestream2_get_byteu(&gb);
2376 
2377  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
2378  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
2379  }
2380  ) /* End of CASE */
2381  CASE(ADPCM_IMA_SMJPEG,
2382  for (int i = 0; i < channels; i++) {
2383  c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
2384  c->status[i].step_index = bytestream2_get_byteu(&gb);
2385  bytestream2_skipu(&gb, 1);
2386  if (c->status[i].step_index > 88u) {
2387  av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2388  c->status[i].step_index);
2389  return AVERROR_INVALIDDATA;
2390  }
2391  }
2392 
2393  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2394  int v = bytestream2_get_byteu(&gb);
2395 
2396  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
2397  *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
2398  }
2399  ) /* End of CASE */
2400  CASE(ADPCM_CT,
2401  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2402  int v = bytestream2_get_byteu(&gb);
2403  *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
2404  *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
2405  }
2406  ) /* End of CASE */
2407 #if CONFIG_ADPCM_SBPRO_2_DECODER || CONFIG_ADPCM_SBPRO_3_DECODER || \
2408  CONFIG_ADPCM_SBPRO_4_DECODER
2412  if (!c->status[0].step_index) {
2413  /* the first byte is a raw sample */
2414  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2415  if (st)
2416  *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
2417  c->status[0].step_index = 1;
2418  nb_samples--;
2419  }
2420  if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
2421  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2422  int byte = bytestream2_get_byteu(&gb);
2423  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2424  byte >> 4, 4, 0);
2425  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2426  byte & 0x0F, 4, 0);
2427  }
2428  } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
2429  for (int n = (nb_samples<<st) / 3; n > 0; n--) {
2430  int byte = bytestream2_get_byteu(&gb);
2431  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2432  byte >> 5 , 3, 0);
2433  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2434  (byte >> 2) & 0x07, 3, 0);
2435  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2436  byte & 0x03, 2, 0);
2437  }
2438  } else {
2439  for (int n = nb_samples >> (2 - st); n > 0; n--) {
2440  int byte = bytestream2_get_byteu(&gb);
2441  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2442  byte >> 6 , 2, 2);
2443  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2444  (byte >> 4) & 0x03, 2, 2);
2445  *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
2446  (byte >> 2) & 0x03, 2, 2);
2447  *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
2448  byte & 0x03, 2, 2);
2449  }
2450  }
2451  break;
2452 #endif /* CONFIG_ADPCM_SBPRO_x_DECODER */
2453  CASE(ADPCM_SWF,
2454  adpcm_swf_decode(avctx, buf, buf_size, samples);
2455  bytestream2_seek(&gb, 0, SEEK_END);
2456  ) /* End of CASE */
2457  CASE(ADPCM_YAMAHA,
2458  for (int n = nb_samples >> (1 - st); n > 0; n--) {
2459  int v = bytestream2_get_byteu(&gb);
2460  *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
2461  *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
2462  }
2463  ) /* End of CASE */
2464  CASE(ADPCM_AICA,
2465  for (int channel = 0; channel < channels; channel++) {
2466  samples = samples_p[channel];
2467  for (int n = nb_samples >> 1; n > 0; n--) {
2468  int v = bytestream2_get_byteu(&gb);
2469  *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
2470  *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
2471  }
2472  }
2473  ) /* End of CASE */
2474  CASE(ADPCM_AFC,
2475  int samples_per_block;
2476  int blocks;
2477 
2478  if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
2479  samples_per_block = avctx->extradata[0] / 16;
2480  blocks = nb_samples / avctx->extradata[0];
2481  } else {
2482  samples_per_block = nb_samples / 16;
2483  blocks = 1;
2484  }
2485 
2486  for (int m = 0; m < blocks; m++) {
2487  for (int channel = 0; channel < channels; channel++) {
2488  int prev1 = c->status[channel].sample1;
2489  int prev2 = c->status[channel].sample2;
2490 
2491  samples = samples_p[channel] + m * 16;
2492  /* Read in every sample for this channel. */
2493  for (int i = 0; i < samples_per_block; i++) {
2494  int byte = bytestream2_get_byteu(&gb);
2495  int scale = 1 << (byte >> 4);
2496  int index = byte & 0xf;
2497  int factor1 = afc_coeffs[0][index];
2498  int factor2 = afc_coeffs[1][index];
2499 
2500  /* Decode 16 samples. */
2501  for (int n = 0; n < 16; n++) {
2502  int32_t sampledat;
2503 
2504  if (n & 1) {
2505  sampledat = sign_extend(byte, 4);
2506  } else {
2507  byte = bytestream2_get_byteu(&gb);
2508  sampledat = sign_extend(byte >> 4, 4);
2509  }
2510 
2511  sampledat = ((prev1 * factor1 + prev2 * factor2) >> 11) +
2512  sampledat * scale;
2513  *samples = av_clip_int16(sampledat);
2514  prev2 = prev1;
2515  prev1 = *samples++;
2516  }
2517  }
2518 
2519  c->status[channel].sample1 = prev1;
2520  c->status[channel].sample2 = prev2;
2521  }
2522  }
2523  bytestream2_seek(&gb, 0, SEEK_END);
2524  ) /* End of CASE */
2525 #if CONFIG_ADPCM_THP_DECODER || CONFIG_ADPCM_THP_LE_DECODER
2526  case AV_CODEC_ID_ADPCM_THP:
2528  {
2529  int table[14][16];
2530 
2531 #define THP_GET16(g) \
2532  sign_extend( \
2533  avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
2534  bytestream2_get_le16u(&(g)) : \
2535  bytestream2_get_be16u(&(g)), 16)
2536 
2537  if (avctx->extradata) {
2538  GetByteContext tb;
2539  if (avctx->extradata_size < 32 * channels) {
2540  av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
2541  return AVERROR_INVALIDDATA;
2542  }
2543 
2544  bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
2545  for (int i = 0; i < channels; i++)
2546  for (int n = 0; n < 16; n++)
2547  table[i][n] = THP_GET16(tb);
2548  } else {
2549  for (int i = 0; i < channels; i++)
2550  for (int n = 0; n < 16; n++)
2551  table[i][n] = THP_GET16(gb);
2552 
2553  if (!c->has_status) {
2554  /* Initialize the previous sample. */
2555  for (int i = 0; i < channels; i++) {
2556  c->status[i].sample1 = THP_GET16(gb);
2557  c->status[i].sample2 = THP_GET16(gb);
2558  }
2559  c->has_status = 1;
2560  } else {
2561  bytestream2_skip(&gb, channels * 4);
2562  }
2563  }
2564 
2565  for (int ch = 0; ch < channels; ch++) {
2566  samples = samples_p[ch];
2567 
2568  /* Read in every sample for this channel. */
2569  for (int i = 0; i < (nb_samples + 13) / 14; i++) {
2570  int byte = bytestream2_get_byteu(&gb);
2571  int index = (byte >> 4) & 7;
2572  unsigned int exp = byte & 0x0F;
2573  int64_t factor1 = table[ch][index * 2];
2574  int64_t factor2 = table[ch][index * 2 + 1];
2575 
2576  /* Decode 14 samples. */
2577  for (int n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
2578  int32_t sampledat;
2579 
2580  if (n & 1) {
2581  sampledat = sign_extend(byte, 4);
2582  } else {
2583  byte = bytestream2_get_byteu(&gb);
2584  sampledat = sign_extend(byte >> 4, 4);
2585  }
2586 
2587  sampledat = ((c->status[ch].sample1 * factor1
2588  + c->status[ch].sample2 * factor2) >> 11) + sampledat * (1 << exp);
2589  *samples = av_clip_int16(sampledat);
2590  c->status[ch].sample2 = c->status[ch].sample1;
2591  c->status[ch].sample1 = *samples++;
2592  }
2593  }
2594  }
2595  break;
2596  }
2597 #endif /* CONFIG_ADPCM_THP(_LE)_DECODER */
2598  CASE(ADPCM_DTK,
2599  for (int channel = 0; channel < channels; channel++) {
2600  samples = samples_p[channel];
2601 
2602  /* Read in every sample for this channel. */
2603  for (int i = 0; i < nb_samples / 28; i++) {
2604  int byte, header;
2605  if (channel)
2606  bytestream2_skipu(&gb, 1);
2607  header = bytestream2_get_byteu(&gb);
2608  bytestream2_skipu(&gb, 3 - channel);
2609 
2610  /* Decode 28 samples. */
2611  for (int n = 0; n < 28; n++) {
2612  int32_t sampledat, prev;
2613 
2614  switch (header >> 4) {
2615  case 1:
2616  prev = (c->status[channel].sample1 * 0x3c);
2617  break;
2618  case 2:
2619  prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
2620  break;
2621  case 3:
2622  prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
2623  break;
2624  default:
2625  prev = 0;
2626  }
2627 
2628  prev = av_clip_intp2((prev + 0x20) >> 6, 21);
2629 
2630  byte = bytestream2_get_byteu(&gb);
2631  if (!channel)
2632  sampledat = sign_extend(byte, 4);
2633  else
2634  sampledat = sign_extend(byte >> 4, 4);
2635 
2636  sampledat = ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
2637  *samples++ = av_clip_int16(sampledat >> 6);
2638  c->status[channel].sample2 = c->status[channel].sample1;
2639  c->status[channel].sample1 = sampledat;
2640  }
2641  }
2642  if (!channel)
2643  bytestream2_seek(&gb, 0, SEEK_SET);
2644  }
2645  ) /* End of CASE */
2646  CASE(ADPCM_N64,
2647  ADPCMChannelStatus *cs = &c->status[0];
2648  int coefs[8*2*8] = { 0 };
2649 
2650  if (avctx->extradata) {
2651  int version, order, entries;
2653 
2654  bytestream2_init(&cb, avctx->extradata, avctx->extradata_size);
2655 
2656  version = bytestream2_get_be16(&cb);
2657  order = bytestream2_get_be16(&cb);
2658  entries = bytestream2_get_be16(&cb);
2659  if (version != 1 || order != 2 || entries > 8)
2660  return AVERROR_INVALIDDATA;
2661 
2662  for (int n = 0; n < order * entries * 8; n++)
2663  coefs[n] = sign_extend(bytestream2_get_be16(&cb), 16);
2664  }
2665 
2666  for (int block = 0; block < avpkt->size / 9; block++) {
2667  int scale, index, codes[16];
2668  int16_t hist[8] = { 0 };
2669  const int order = 2;
2670  int16_t out[16];
2671 
2672  hist[6] = cs->sample2;
2673  hist[7] = cs->sample1;
2674 
2675  samples = samples_p[0] + block * 16;
2676 
2677  scale = (buf[0] >> 4) & 0xF;
2678  index = (buf[0] >> 0) & 0xF;
2679  scale = 1 << scale;
2680  index = FFMIN(index, 8);
2681 
2682  for (int i = 0, j = 0; i < 16; i += 2, j++) {
2683  int n0 = (buf[j+1] >> 4) & 0xF;
2684  int n1 = (buf[j+1] >> 0) & 0xF;
2685 
2686  if (n0 & 8)
2687  n0 = n0 - 16;
2688  if (n1 & 8)
2689  n1 = n1 - 16;
2690 
2691  codes[i+0] = n0 * scale;
2692  codes[i+1] = n1 * scale;
2693  }
2694 
2695  for (int j = 0; j < 2; j++) {
2696  int *sf_codes = &codes[j*8];
2697  int16_t *sf_out = &out[j*8];
2698 
2699  for (int i = 0; i < 8; i++) {
2700  int sample;
2701  unsigned delta = 0;
2702 
2703  for (int o = 0; o < order; o++)
2704  delta += coefs[o*8 + i] * hist[(8 - order) + o];
2705 
2706  for (int k = i-1; k > -1; k--) {
2707  for (int o = 1; o < order; o++)
2708  delta += sf_codes[(i-1) - k] * (unsigned)coefs[(o*8) + k];
2709  }
2710 
2711  sample = sf_codes[i] * 2048;
2712  sample = (int)(sample + delta) / 2048;
2714  sf_out[i] = sample;
2715  }
2716 
2717  for (int i = 8 - order; i < 8; i++)
2718  hist[i] = sf_out[i];
2719  }
2720 
2721  memcpy(samples, out, sizeof(out));
2722 
2723  cs->sample2 = hist[6];
2724  cs->sample1 = hist[7];
2725 
2726  buf += 9;
2727  }
2728  bytestream2_seek(&gb, 0, SEEK_END);
2729  ) /* End of CASE */
2730  CASE(ADPCM_PSX,
2731  for (int block = 0; block < avpkt->size / FFMAX(avctx->block_align, 16 * channels); block++) {
2732  int nb_samples_per_block = 28 * FFMAX(avctx->block_align, 16 * channels) / (16 * channels);
2733  for (int channel = 0; channel < channels; channel++) {
2734  samples = samples_p[channel] + block * nb_samples_per_block;
2735  av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
2736 
2737  /* Read in every sample for this channel. */
2738  for (int i = 0; i < nb_samples_per_block / 28; i++) {
2739  int filter, shift, flag, byte;
2740 
2741  filter = bytestream2_get_byteu(&gb);
2742  shift = filter & 0xf;
2743  filter = filter >> 4;
2745  return AVERROR_INVALIDDATA;
2746  flag = bytestream2_get_byteu(&gb) & 0x7;
2747 
2748  /* Decode 28 samples. */
2749  for (int n = 0; n < 28; n++) {
2750  int sample = 0, scale;
2751 
2752  if (n & 1) {
2753  scale = sign_extend(byte >> 4, 4);
2754  } else {
2755  byte = bytestream2_get_byteu(&gb);
2756  scale = sign_extend(byte, 4);
2757  }
2758 
2759  if (flag < 0x07) {
2760  scale = scale * (1 << 12);
2761  sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
2762  }
2764  c->status[channel].sample2 = c->status[channel].sample1;
2765  c->status[channel].sample1 = sample;
2766  }
2767  }
2768  }
2769  }
2770  ) /* End of CASE */
2771  CASE(ADPCM_PSXC,
2772  for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
2773  int nb_samples_per_block = ((avctx->block_align - 1) / channels) * 2;
2774  for (int channel = 0; channel < channels; channel++) {
2775  int filter, shift, byte;
2776 
2777  samples = samples_p[channel] + block * nb_samples_per_block;
2778  av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
2779 
2780  filter = bytestream2_get_byteu(&gb);
2781  shift = filter & 0xf;
2782  filter = filter >> 4;
2784  return AVERROR_INVALIDDATA;
2785 
2786  for (int n = 0; n < nb_samples_per_block; n++) {
2787  int sample = 0, scale;
2788 
2789  if (n & 1) {
2790  scale = sign_extend(byte >> 4, 4);
2791  } else {
2792  byte = bytestream2_get_byteu(&gb);
2793  scale = sign_extend(byte & 0xF, 4);
2794  }
2795 
2796  scale = scale * (1 << 12);
2797  sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
2799  c->status[channel].sample2 = c->status[channel].sample1;
2800  c->status[channel].sample1 = sample;
2801  }
2802  }
2803  }
2804  ) /* End of CASE */
2805  CASE(ADPCM_SANYO,
2806  int (*expand)(ADPCMChannelStatus *c, int bits);
2807  GetBitContext g;
2808 
2809  switch(avctx->bits_per_coded_sample) {
2810  case 3: expand = adpcm_sanyo_expand3; break;
2811  case 4: expand = adpcm_sanyo_expand4; break;
2812  case 5: expand = adpcm_sanyo_expand5; break;
2813  }
2814 
2815  for (int ch = 0; ch < channels; ch++) {
2816  c->status[ch].predictor = sign_extend(bytestream2_get_le16(&gb), 16);
2817  c->status[ch].step = sign_extend(bytestream2_get_le16(&gb), 16);
2818  }
2819 
2821  for (int i = 0; i < nb_samples; i++)
2822  for (int ch = 0; ch < channels; ch++)
2823  samples_p[ch][i] = expand(&c->status[ch], get_bits_le(&g, avctx->bits_per_coded_sample));
2824 
2825  align_get_bits(&g);
2826  bytestream2_skip(&gb, get_bits_count(&g) / 8);
2827  ) /* End of CASE */
2828  CASE(ADPCM_ARGO,
2829  /*
2830  * The format of each block:
2831  * uint8_t left_control;
2832  * uint4_t left_samples[nb_samples];
2833  * ---- and if stereo ----
2834  * uint8_t right_control;
2835  * uint4_t right_samples[nb_samples];
2836  *
2837  * Format of the control byte:
2838  * MSB [SSSSRDRR] LSB
2839  * S = (Shift Amount - 2)
2840  * D = Decoder flag.
2841  * R = Reserved
2842  *
2843  * Each block relies on the previous two samples of each channel.
2844  * They should be 0 initially.
2845  */
2846  for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
2847  for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
2848  ADPCMChannelStatus *cs = c->status + channel;
2849  int control, shift;
2850 
2851  samples = samples_p[channel] + block * 32;
2852 
2853  /* Get the control byte and decode the samples, 2 at a time. */
2854  control = bytestream2_get_byteu(&gb);
2855  shift = (control >> 4) + 2;
2856 
2857  for (int n = 0; n < 16; n++) {
2858  int sample = bytestream2_get_byteu(&gb);
2859  *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift, control & 0x04);
2860  *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift, control & 0x04);
2861  }
2862  }
2863  }
2864  ) /* End of CASE */
2865  CASE(ADPCM_CIRCUS,
2866  for (int n = 0; n < nb_samples; n++) {
2867  for (int ch = 0; ch < channels; ch++) {
2868  int v = bytestream2_get_byteu(&gb);
2869  *samples++ = adpcm_circus_expand_nibble(&c->status[ch], v);
2870  }
2871  }
2872  ) /* End of CASE */
2873  CASE(ADPCM_ZORK,
2874  for (int n = 0; n < nb_samples * channels; n++) {
2875  int v = bytestream2_get_byteu(&gb);
2876  *samples++ = adpcm_zork_expand_nibble(&c->status[n % channels], v);
2877  }
2878  ) /* End of CASE */
2879  CASE(ADPCM_IMA_MTF,
2880  for (int n = nb_samples / 2; n > 0; n--) {
2881  for (int channel = 0; channel < channels; channel++) {
2882  int v = bytestream2_get_byteu(&gb);
2883  *samples++ = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2884  samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2885  }
2886  samples += channels;
2887  }
2888  ) /* End of CASE */
2889  default:
2890  av_unreachable("There are cases for all codec ids using adpcm_decode_frame");
2891  }
2892 
2893  if (avpkt->size && bytestream2_tell(&gb) == 0) {
2894  av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
2895  return AVERROR_INVALIDDATA;
2896  }
2897 
2898  *got_frame_ptr = 1;
2899 
2900  if (avpkt->size < bytestream2_tell(&gb)) {
2901  av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
2902  return avpkt->size;
2903  }
2904 
2905  return bytestream2_tell(&gb);
2906 }
2907 
2909 {
2910  ADPCMDecodeContext *c = avctx->priv_data;
2911 
2912  /* Just nuke the entire state and re-init. */
2913  memset(c, 0, sizeof(ADPCMDecodeContext));
2914 
2915  switch(avctx->codec_id) {
2916  case AV_CODEC_ID_ADPCM_CT:
2917  c->status[0].step = c->status[1].step = 511;
2918  break;
2919 
2921  if (avctx->extradata && avctx->extradata_size >= 8) {
2922  c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata ), 18);
2923  c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
2924  }
2925  break;
2926 
2928  if (avctx->extradata && avctx->extradata_size >= 28) {
2929  c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
2930  c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
2931  c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
2932  c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
2933  }
2934  break;
2935 
2937  if (avctx->extradata && avctx->extradata_size >= 2)
2938  c->vqa_version = AV_RL16(avctx->extradata);
2939  break;
2940  default:
2941  /* Other codecs may want to handle this during decoding. */
2942  c->has_status = 0;
2943  return;
2944  }
2945 
2946  c->has_status = 1;
2947 }
2948 
2949 
2950 #define ADPCM_DECODER_0(id_, name_, long_name_)
2951 #define ADPCM_DECODER_1(id_, name_, long_name_) \
2952 const FFCodec ff_ ## name_ ## _decoder = { \
2953  .p.name = #name_, \
2954  CODEC_LONG_NAME(long_name_), \
2955  .p.type = AVMEDIA_TYPE_AUDIO, \
2956  .p.id = id_, \
2957  .p.capabilities = AV_CODEC_CAP_DR1, \
2958  .priv_data_size = sizeof(ADPCMDecodeContext), \
2959  .init = adpcm_decode_init, \
2960  FF_CODEC_DECODE_CB(adpcm_decode_frame), \
2961  .flush = adpcm_flush, \
2962 };
2963 #define ADPCM_DECODER_2(enabled, codec_id, name, long_name) \
2964  ADPCM_DECODER_ ## enabled(codec_id, name, long_name)
2965 #define ADPCM_DECODER_3(config, codec_id, name, long_name) \
2966  ADPCM_DECODER_2(config, codec_id, name, long_name)
2967 #define ADPCM_DECODER(codec, name, long_name) \
2968  ADPCM_DECODER_3(CONFIG_ ## codec ## _DECODER, AV_CODEC_ID_ ## codec, \
2969  name, long_name)
2970 
2971 /* Note: Do not forget to add new entries to the Makefile as well. */
2972 ADPCM_DECODER(ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie")
2973 ADPCM_DECODER(ADPCM_AFC, adpcm_afc, "ADPCM Nintendo Gamecube AFC")
2974 ADPCM_DECODER(ADPCM_AGM, adpcm_agm, "ADPCM AmuseGraphics Movie")
2975 ADPCM_DECODER(ADPCM_AICA, adpcm_aica, "ADPCM Yamaha AICA")
2976 ADPCM_DECODER(ADPCM_ARGO, adpcm_argo, "ADPCM Argonaut Games")
2977 ADPCM_DECODER(ADPCM_CIRCUS, adpcm_circus, "ADPCM Circus")
2978 ADPCM_DECODER(ADPCM_CT, adpcm_ct, "ADPCM Creative Technology")
2979 ADPCM_DECODER(ADPCM_DTK, adpcm_dtk, "ADPCM Nintendo Gamecube DTK")
2980 ADPCM_DECODER(ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts")
2981 ADPCM_DECODER(ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA")
2982 ADPCM_DECODER(ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1")
2983 ADPCM_DECODER(ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2")
2984 ADPCM_DECODER(ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3")
2985 ADPCM_DECODER(ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS")
2986 ADPCM_DECODER(ADPCM_IMA_ACORN, adpcm_ima_acorn, "ADPCM IMA Acorn Replay")
2987 ADPCM_DECODER(ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV")
2988 ADPCM_DECODER(ADPCM_IMA_APC, adpcm_ima_apc, "ADPCM IMA CRYO APC")
2989 ADPCM_DECODER(ADPCM_IMA_APM, adpcm_ima_apm, "ADPCM IMA Ubisoft APM")
2990 ADPCM_DECODER(ADPCM_IMA_CUNNING, adpcm_ima_cunning, "ADPCM IMA Cunning Developments")
2991 ADPCM_DECODER(ADPCM_IMA_DAT4, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4")
2992 ADPCM_DECODER(ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3")
2993 ADPCM_DECODER(ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4")
2994 ADPCM_DECODER(ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS")
2995 ADPCM_DECODER(ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD")
2996 ADPCM_DECODER(ADPCM_IMA_ESCAPE, adpcm_ima_escape, "ADPCM IMA Acorn Escape")
2997 ADPCM_DECODER(ADPCM_IMA_HVQM2, adpcm_ima_hvqm2, "ADPCM IMA HVQM2")
2998 ADPCM_DECODER(ADPCM_IMA_HVQM4, adpcm_ima_hvqm4, "ADPCM IMA HVQM4")
2999 ADPCM_DECODER(ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS")
3000 ADPCM_DECODER(ADPCM_IMA_MAGIX, adpcm_ima_magix, "ADPCM IMA Magix")
3001 ADPCM_DECODER(ADPCM_IMA_MOFLEX, adpcm_ima_moflex, "ADPCM IMA MobiClip MOFLEX")
3002 ADPCM_DECODER(ADPCM_IMA_MTF, adpcm_ima_mtf, "ADPCM IMA Capcom's MT Framework")
3003 ADPCM_DECODER(ADPCM_IMA_OKI, adpcm_ima_oki, "ADPCM IMA Dialogic OKI")
3004 ADPCM_DECODER(ADPCM_IMA_PDA, adpcm_ima_pda, "ADPCM IMA PlayDate")
3005 ADPCM_DECODER(ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime")
3006 ADPCM_DECODER(ADPCM_IMA_RAD, adpcm_ima_rad, "ADPCM IMA Radical")
3007 ADPCM_DECODER(ADPCM_IMA_SSI, adpcm_ima_ssi, "ADPCM IMA Simon & Schuster Interactive")
3008 ADPCM_DECODER(ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG")
3009 ADPCM_DECODER(ADPCM_IMA_ALP, adpcm_ima_alp, "ADPCM IMA High Voltage Software ALP")
3010 ADPCM_DECODER(ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV")
3011 ADPCM_DECODER(ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood")
3012 ADPCM_DECODER(ADPCM_IMA_XBOX, adpcm_ima_xbox, "ADPCM IMA Xbox")
3013 ADPCM_DECODER(ADPCM_MS, adpcm_ms, "ADPCM Microsoft")
3014 ADPCM_DECODER(ADPCM_MTAF, adpcm_mtaf, "ADPCM MTAF")
3015 ADPCM_DECODER(ADPCM_N64, adpcm_n64, "ADPCM Silicon Graphics N64")
3016 ADPCM_DECODER(ADPCM_PSX, adpcm_psx, "ADPCM Playstation")
3017 ADPCM_DECODER(ADPCM_PSXC, adpcm_psxc, "ADPCM Playstation C")
3018 ADPCM_DECODER(ADPCM_SANYO, adpcm_sanyo, "ADPCM Sanyo")
3019 ADPCM_DECODER(ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit")
3020 ADPCM_DECODER(ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit")
3021 ADPCM_DECODER(ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit")
3022 ADPCM_DECODER(ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash")
3023 ADPCM_DECODER(ADPCM_THP_LE, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)")
3024 ADPCM_DECODER(ADPCM_THP, adpcm_thp, "ADPCM Nintendo THP")
3025 ADPCM_DECODER(ADPCM_XA, adpcm_xa, "ADPCM CDROM XA")
3026 ADPCM_DECODER(ADPCM_XMD, adpcm_xmd, "ADPCM Konami XMD")
3027 ADPCM_DECODER(ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha")
3028 ADPCM_DECODER(ADPCM_ZORK, 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:384
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:378
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
flag
int flag
Definition: cpu.c:40
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition: bytestream.h:158
out
static FILE * out
Definition: movenc.c:55
AV_CODEC_ID_ADPCM_DTK
@ AV_CODEC_ID_ADPCM_DTK
Definition: codec_id.h:411
ADPCMChannelStatus::step_index
int16_t step_index
Definition: adpcm.h:33
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:247
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
AV_CODEC_ID_ADPCM_N64
@ AV_CODEC_ID_ADPCM_N64
Definition: codec_id.h:434
bytestream2_skipu
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
av_cold
#define av_cold
Definition: attributes.h:119
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:254
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:466
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:426
AVPacket::data
uint8_t * data
Definition: packet.h:603
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:399
AV_CODEC_ID_ADPCM_AICA
@ AV_CODEC_ID_ADPCM_AICA
Definition: codec_id.h:416
AV_CODEC_ID_ADPCM_IMA_OKI
@ AV_CODEC_ID_ADPCM_IMA_OKI
Definition: codec_id.h:410
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:429
AV_CODEC_ID_ADPCM_IMA_ESCAPE
@ AV_CODEC_ID_ADPCM_IMA_ESCAPE
Definition: codec_id.h:439
adpcm_sanyo_expand4
static int adpcm_sanyo_expand4(ADPCMChannelStatus *c, int bits)
Definition: adpcm.c:1029
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:517
AV_CODEC_ID_ADPCM_THP_LE
@ AV_CODEC_ID_ADPCM_THP_LE
Definition: codec_id.h:414
adpcm_sbpro_expand_nibble
static int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
Definition: adpcm.c:724
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:56
AV_CODEC_ID_ADPCM_CT
@ AV_CODEC_ID_ADPCM_CT
Definition: codec_id.h:390
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:337
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:452
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1055
decode_adpcm_ima_hvqm4
static void decode_adpcm_ima_hvqm4(AVCodecContext *avctx, int16_t *outbuf, int samples_to_do, int frame_format, GetByteContext *gb)
Definition: adpcm.c:620
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:496
adpcm_ima_expand_nibble
static int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
Definition: adpcm.c:447
AV_CODEC_ID_ADPCM_PSXC
@ AV_CODEC_ID_ADPCM_PSXC
Definition: codec_id.h:437
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:557
AV_CODEC_ID_ADPCM_SBPRO_2
@ AV_CODEC_ID_ADPCM_SBPRO_2
Definition: codec_id.h:395
C
s EdgeDetect Foobar g libavfilter vf_edgedetect c libavfilter vf_foobar c edit libavfilter and add an entry for foobar following the pattern of the other filters edit libavfilter allfilters and add an entry for foobar following the pattern of the other filters configure make j< whatever > ffmpeg ffmpeg i you should get a foobar png with Lena edge detected That s your new playground is ready Some little details about what s going which in turn will define variables for the build system and the C
Definition: writing_filters.txt:58
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
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:544
adpcm_ima_alp_expand_nibble
static int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
Definition: adpcm.c:473
adpcm_yamaha_expand_nibble
static int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition: adpcm.c:744
ADPCMChannelStatus::sample1
int sample1
Definition: adpcm.h:39
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:527
AV_CODEC_ID_ADPCM_IMA_ACORN
@ AV_CODEC_ID_ADPCM_IMA_ACORN
Definition: codec_id.h:428
decode_adpcm_ima_hvqm2
static void decode_adpcm_ima_hvqm2(AVCodecContext *avctx, int16_t *outbuf, int samples_to_do, int frame_format, GetByteContext *gb)
Definition: adpcm.c:583
adpcm_zork_expand_nibble
static int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition: adpcm.c:788
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:409
AV_CODEC_ID_ADPCM_IMA_EA_SEAD
@ AV_CODEC_ID_ADPCM_IMA_EA_SEAD
Definition: codec_id.h:401
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:380
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:42
AV_CODEC_ID_ADPCM_IMA_APC
@ AV_CODEC_ID_ADPCM_IMA_APC
Definition: codec_id.h:407
get_bits_le
static unsigned int get_bits_le(GetBitContext *s, int n)
Definition: get_bits.h:358
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:322
AV_CODEC_ID_ADPCM_IMA_ISS
@ AV_CODEC_ID_ADPCM_IMA_ISS
Definition: codec_id.h:405
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:383
adpcm_ms_expand_nibble
static int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition: adpcm.c:663
AV_CODEC_ID_ADPCM_IMA_XBOX
@ AV_CODEC_ID_ADPCM_IMA_XBOX
Definition: codec_id.h:430
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:453
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
AV_CODEC_ID_ADPCM_EA_XAS
@ AV_CODEC_ID_ADPCM_EA_XAS
Definition: codec_id.h:403
av_clip_int16
#define av_clip_int16
Definition: common.h:115
NULL
#define NULL
Definition: coverity.c:32
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
format
New swscale design to change SwsGraph is what coordinates multiple passes These can include cascaded scaling error diffusion and so on Or we could have separate passes for the vertical and horizontal scaling In between each SwsPass lies a fully allocated image buffer Graph passes may have different levels of e g we can have a single threaded error diffusion pass following a multi threaded scaling pass SwsGraph is internally recreated whenever the image format
Definition: swscale-v2.txt:14
AV_CODEC_ID_ADPCM_IMA_MAGIX
@ AV_CODEC_ID_ADPCM_IMA_MAGIX
Definition: codec_id.h:436
AV_CODEC_ID_ADPCM_YAMAHA
@ AV_CODEC_ID_ADPCM_YAMAHA
Definition: codec_id.h:392
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:382
av_unreachable
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition: avassert.h:116
AV_CODEC_ID_ADPCM_IMA_EA_EACS
@ AV_CODEC_ID_ADPCM_IMA_EA_EACS
Definition: codec_id.h:402
AV_CODEC_ID_ADPCM_ARGO
@ AV_CODEC_ID_ADPCM_ARGO
Definition: codec_id.h:420
AV_CODEC_ID_ADPCM_IMA_DK4
@ AV_CODEC_ID_ADPCM_IMA_DK4
Definition: codec_id.h:381
AV_CODEC_ID_ADPCM_IMA_AMV
@ AV_CODEC_ID_ADPCM_IMA_AMV
Definition: codec_id.h:397
abs
#define abs(x)
Definition: cuda_runtime.h:35
attributes.h
ea_adpcm_table
static const int16_t ea_adpcm_table[]
Definition: adpcm.c:97
adpcm_ima_escape_expand_nibble
static int16_t adpcm_ima_escape_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition: adpcm.c:424
ima_cunning_index_table
static const int8_t ima_cunning_index_table[9]
Definition: adpcm.c:111
exp
int8_t exp
Definition: eval.c:76
ADPCMChannelStatus::sample2
int sample2
Definition: adpcm.h:40
adpcm_sanyo_expand3
static int adpcm_sanyo_expand3(ADPCMChannelStatus *c, int bits)
Definition: adpcm.c:986
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:386
ADPCM_DECODER
#define ADPCM_DECODER(codec, name, long_name)
Definition: adpcm.c:2967
adpcm_ct_expand_nibble
static int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition: adpcm.c:703
adpcm.h
adpcm_ima_oki_expand_nibble
static int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition: adpcm.c:682
adpcm_decode_frame
static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: adpcm.c:1442
AV_CODEC_ID_ADPCM_ZORK
@ AV_CODEC_ID_ADPCM_ZORK
Definition: codec_id.h:422
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:1088
AV_CODEC_ID_ADPCM_CIRCUS
@ AV_CODEC_ID_ADPCM_CIRCUS
Definition: codec_id.h:438
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:1769
AVPacket::size
int size
Definition: packet.h:604
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:412
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
adpcm_ima_cunning_expand_nibble
static int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition: adpcm.c:512
AV_CODEC_ID_ADPCM_IMA_ALP
@ AV_CODEC_ID_ADPCM_IMA_ALP
Definition: codec_id.h:424
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
bps
unsigned bps
Definition: movenc.c:2055
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:1047
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:1163
AV_CODEC_ID_ADPCM_IMA_HVQM4
@ AV_CODEC_ID_ADPCM_IMA_HVQM4
Definition: codec_id.h:432
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:391
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
xf
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:622
version
version
Definition: libkvazaar.c:313
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:823
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:1564
AV_CODEC_ID_ADPCM_MTAF
@ AV_CODEC_ID_ADPCM_MTAF
Definition: codec_id.h:418
AV_CODEC_ID_ADPCM_EA_MAXIS_XA
@ AV_CODEC_ID_ADPCM_EA_MAXIS_XA
Definition: codec_id.h:404
ff_adpcm_AdaptCoeff1
const uint8_t ff_adpcm_AdaptCoeff1[]
Divided by 4 to fit in 8-bit integers.
Definition: adpcm_data.c:60
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
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:526
adpcm_index_tables
static const int8_t *const adpcm_index_tables[4]
Definition: adpcm.c:145
MT
#define MT(...)
Definition: codec_desc.c:32
AV_CODEC_ID_ADPCM_IMA_HVQM2
@ AV_CODEC_ID_ADPCM_IMA_HVQM2
Definition: codec_id.h:435
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:423
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:417
ff_adpcm_argo_expand_nibble
int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
Definition: adpcm.c:969
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:431
adpcm_circus_expand_nibble
static int16_t adpcm_circus_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition: adpcm.c:767
avcodec.h
AV_CODEC_ID_ADPCM_EA
@ AV_CODEC_ID_ADPCM_EA
Definition: codec_id.h:388
adpcm_flush
static void adpcm_flush(AVCodecContext *avctx)
Definition: adpcm.c:2908
AV_CODEC_ID_ADPCM_IMA_MTF
@ AV_CODEC_ID_ADPCM_IMA_MTF
Definition: codec_id.h:425
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:1075
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:560
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_CODEC_ID_ADPCM_IMA_PDA
@ AV_CODEC_ID_ADPCM_IMA_PDA
Definition: codec_id.h:433
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:532
AVCodecContext
main external API structure.
Definition: avcodec.h:443
AV_CODEC_ID_ADPCM_AGM
@ AV_CODEC_ID_ADPCM_AGM
Definition: codec_id.h:419
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:135
AV_CODEC_ID_ADPCM_EA_R1
@ AV_CODEC_ID_ADPCM_EA_R1
Definition: codec_id.h:398
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:400
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:396
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:393
adpcm_swf_decode
static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
Definition: adpcm.c:911
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:421
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:278
AV_CODEC_ID_ADPCM_IMA_MOFLEX
@ AV_CODEC_ID_ADPCM_IMA_MOFLEX
Definition: codec_id.h:427
AVPacket
This structure stores compressed data.
Definition: packet.h:580
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:470
AV_CODEC_ID_ADPCM_IMA_WAV
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition: codec_id.h:379
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:385
adpcm_agm_expand_nibble
static int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
Definition: adpcm.c:380
AV_CODEC_ID_ADPCM_PSX
@ AV_CODEC_ID_ADPCM_PSX
Definition: codec_id.h:415
adpcm_mtaf_expand_nibble
static int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
Definition: adpcm.c:758
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
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:383
channel
channel
Definition: ebur128.h:39
AV_CODEC_ID_ADPCM_SBPRO_3
@ AV_CODEC_ID_ADPCM_SBPRO_3
Definition: codec_id.h:394
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