You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2083 lines
80KB

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