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.

2160 lines
83KB

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