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.

2131 lines
82KB

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