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.

2163 lines
84KB

  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", avctx->channels);
  123. return AVERROR_PATCHWELCOME;
  124. }
  125. break;
  126. case AV_CODEC_ID_ADPCM_PSX:
  127. max_channels = 8;
  128. if (avctx->block_align % (16 * avctx->channels))
  129. return AVERROR_INVALIDDATA;
  130. break;
  131. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  132. case AV_CODEC_ID_ADPCM_THP:
  133. case AV_CODEC_ID_ADPCM_THP_LE:
  134. max_channels = 14;
  135. break;
  136. }
  137. if (avctx->channels < min_channels || avctx->channels > max_channels) {
  138. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  139. return AVERROR(EINVAL);
  140. }
  141. switch(avctx->codec->id) {
  142. case AV_CODEC_ID_ADPCM_CT:
  143. c->status[0].step = c->status[1].step = 511;
  144. break;
  145. case AV_CODEC_ID_ADPCM_IMA_WAV:
  146. if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
  147. return AVERROR_INVALIDDATA;
  148. break;
  149. case AV_CODEC_ID_ADPCM_IMA_APC:
  150. if (avctx->extradata && avctx->extradata_size >= 8) {
  151. c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata ), 18);
  152. c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
  153. }
  154. break;
  155. case AV_CODEC_ID_ADPCM_IMA_APM:
  156. if (avctx->extradata) {
  157. if (avctx->extradata_size >= 28) {
  158. c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
  159. c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
  160. c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
  161. c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
  162. } else if (avctx->extradata_size >= 16) {
  163. c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 0), 18);
  164. c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 4), 0, 88);
  165. c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 8), 18);
  166. c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 12), 0, 88);
  167. }
  168. }
  169. break;
  170. case AV_CODEC_ID_ADPCM_IMA_WS:
  171. if (avctx->extradata && avctx->extradata_size >= 2)
  172. c->vqa_version = AV_RL16(avctx->extradata);
  173. break;
  174. case AV_CODEC_ID_ADPCM_ARGO:
  175. if (avctx->bits_per_coded_sample != 4 || avctx->block_align != 17 * avctx->channels)
  176. return AVERROR_INVALIDDATA;
  177. break;
  178. case AV_CODEC_ID_ADPCM_ZORK:
  179. if (avctx->bits_per_coded_sample != 8)
  180. return AVERROR_INVALIDDATA;
  181. break;
  182. default:
  183. break;
  184. }
  185. switch (avctx->codec->id) {
  186. case AV_CODEC_ID_ADPCM_AICA:
  187. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  188. case AV_CODEC_ID_ADPCM_IMA_QT:
  189. case AV_CODEC_ID_ADPCM_IMA_WAV:
  190. case AV_CODEC_ID_ADPCM_4XM:
  191. case AV_CODEC_ID_ADPCM_XA:
  192. case AV_CODEC_ID_ADPCM_EA_R1:
  193. case AV_CODEC_ID_ADPCM_EA_R2:
  194. case AV_CODEC_ID_ADPCM_EA_R3:
  195. case AV_CODEC_ID_ADPCM_EA_XAS:
  196. case AV_CODEC_ID_ADPCM_THP:
  197. case AV_CODEC_ID_ADPCM_THP_LE:
  198. case AV_CODEC_ID_ADPCM_AFC:
  199. case AV_CODEC_ID_ADPCM_DTK:
  200. case AV_CODEC_ID_ADPCM_PSX:
  201. case AV_CODEC_ID_ADPCM_MTAF:
  202. case AV_CODEC_ID_ADPCM_ARGO:
  203. case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
  204. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  205. break;
  206. case AV_CODEC_ID_ADPCM_IMA_WS:
  207. avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
  208. AV_SAMPLE_FMT_S16;
  209. break;
  210. case AV_CODEC_ID_ADPCM_MS:
  211. avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
  212. AV_SAMPLE_FMT_S16;
  213. break;
  214. default:
  215. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  216. }
  217. return 0;
  218. }
  219. static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
  220. {
  221. int delta, pred, step, add;
  222. pred = c->predictor;
  223. delta = nibble & 7;
  224. step = c->step;
  225. add = (delta * 2 + 1) * step;
  226. if (add < 0)
  227. add = add + 7;
  228. if ((nibble & 8) == 0)
  229. pred = av_clip(pred + (add >> 3), -32767, 32767);
  230. else
  231. pred = av_clip(pred - (add >> 3), -32767, 32767);
  232. switch (delta) {
  233. case 7:
  234. step *= 0x99;
  235. break;
  236. case 6:
  237. c->step = av_clip(c->step * 2, 127, 24576);
  238. c->predictor = pred;
  239. return pred;
  240. case 5:
  241. step *= 0x66;
  242. break;
  243. case 4:
  244. step *= 0x4d;
  245. break;
  246. default:
  247. step *= 0x39;
  248. break;
  249. }
  250. if (step < 0)
  251. step += 0x3f;
  252. c->step = step >> 6;
  253. c->step = av_clip(c->step, 127, 24576);
  254. c->predictor = pred;
  255. return pred;
  256. }
  257. static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
  258. {
  259. int step_index;
  260. int predictor;
  261. int sign, delta, diff, step;
  262. step = ff_adpcm_step_table[c->step_index];
  263. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  264. step_index = av_clip(step_index, 0, 88);
  265. sign = nibble & 8;
  266. delta = nibble & 7;
  267. /* perform direct multiplication instead of series of jumps proposed by
  268. * the reference ADPCM implementation since modern CPUs can do the mults
  269. * quickly enough */
  270. diff = ((2 * delta + 1) * step) >> shift;
  271. predictor = c->predictor;
  272. if (sign) predictor -= diff;
  273. else predictor += diff;
  274. c->predictor = av_clip_int16(predictor);
  275. c->step_index = step_index;
  276. return (int16_t)c->predictor;
  277. }
  278. static inline int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
  279. {
  280. int step_index;
  281. int predictor;
  282. int sign, delta, diff, step;
  283. step = ff_adpcm_step_table[c->step_index];
  284. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  285. step_index = av_clip(step_index, 0, 88);
  286. sign = nibble & 8;
  287. delta = nibble & 7;
  288. diff = (delta * step) >> shift;
  289. predictor = c->predictor;
  290. if (sign) predictor -= diff;
  291. else predictor += diff;
  292. c->predictor = av_clip_int16(predictor);
  293. c->step_index = step_index;
  294. return (int16_t)c->predictor;
  295. }
  296. static inline int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble)
  297. {
  298. int step_index, step, delta, predictor;
  299. step = ff_adpcm_step_table[c->step_index];
  300. delta = step * (2 * nibble - 15);
  301. predictor = c->predictor + delta;
  302. step_index = c->step_index + mtf_index_table[(unsigned)nibble];
  303. c->predictor = av_clip_int16(predictor >> 4);
  304. c->step_index = av_clip(step_index, 0, 88);
  305. return (int16_t)c->predictor;
  306. }
  307. static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
  308. {
  309. int step_index;
  310. int predictor;
  311. int step;
  312. nibble = sign_extend(nibble & 0xF, 4);
  313. step = ff_adpcm_ima_cunning_step_table[c->step_index];
  314. step_index = c->step_index + ff_adpcm_ima_cunning_index_table[abs(nibble)];
  315. step_index = av_clip(step_index, 0, 60);
  316. predictor = c->predictor + step * nibble;
  317. c->predictor = av_clip_int16(predictor);
  318. c->step_index = step_index;
  319. return c->predictor;
  320. }
  321. static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
  322. {
  323. int nibble, step_index, predictor, sign, delta, diff, step, shift;
  324. shift = bps - 1;
  325. nibble = get_bits_le(gb, bps),
  326. step = ff_adpcm_step_table[c->step_index];
  327. step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
  328. step_index = av_clip(step_index, 0, 88);
  329. sign = nibble & (1 << shift);
  330. delta = av_mod_uintp2(nibble, shift);
  331. diff = ((2 * delta + 1) * step) >> shift;
  332. predictor = c->predictor;
  333. if (sign) predictor -= diff;
  334. else predictor += diff;
  335. c->predictor = av_clip_int16(predictor);
  336. c->step_index = step_index;
  337. return (int16_t)c->predictor;
  338. }
  339. static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
  340. {
  341. int step_index;
  342. int predictor;
  343. int diff, step;
  344. step = ff_adpcm_step_table[c->step_index];
  345. step_index = c->step_index + ff_adpcm_index_table[nibble];
  346. step_index = av_clip(step_index, 0, 88);
  347. diff = step >> 3;
  348. if (nibble & 4) diff += step;
  349. if (nibble & 2) diff += step >> 1;
  350. if (nibble & 1) diff += step >> 2;
  351. if (nibble & 8)
  352. predictor = c->predictor - diff;
  353. else
  354. predictor = c->predictor + diff;
  355. c->predictor = av_clip_int16(predictor);
  356. c->step_index = step_index;
  357. return c->predictor;
  358. }
  359. static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
  360. {
  361. int predictor;
  362. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  363. predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  364. c->sample2 = c->sample1;
  365. c->sample1 = av_clip_int16(predictor);
  366. c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
  367. if (c->idelta < 16) c->idelta = 16;
  368. if (c->idelta > INT_MAX/768) {
  369. av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
  370. c->idelta = INT_MAX/768;
  371. }
  372. return c->sample1;
  373. }
  374. static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
  375. {
  376. int step_index, predictor, sign, delta, diff, step;
  377. step = ff_adpcm_oki_step_table[c->step_index];
  378. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  379. step_index = av_clip(step_index, 0, 48);
  380. sign = nibble & 8;
  381. delta = nibble & 7;
  382. diff = ((2 * delta + 1) * step) >> 3;
  383. predictor = c->predictor;
  384. if (sign) predictor -= diff;
  385. else predictor += diff;
  386. c->predictor = av_clip_intp2(predictor, 11);
  387. c->step_index = step_index;
  388. return c->predictor * 16;
  389. }
  390. static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
  391. {
  392. int sign, delta, diff;
  393. int new_step;
  394. sign = nibble & 8;
  395. delta = nibble & 7;
  396. /* perform direct multiplication instead of series of jumps proposed by
  397. * the reference ADPCM implementation since modern CPUs can do the mults
  398. * quickly enough */
  399. diff = ((2 * delta + 1) * c->step) >> 3;
  400. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  401. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  402. c->predictor = av_clip_int16(c->predictor);
  403. /* calculate new step and clamp it to range 511..32767 */
  404. new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
  405. c->step = av_clip(new_step, 511, 32767);
  406. return (int16_t)c->predictor;
  407. }
  408. static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
  409. {
  410. int sign, delta, diff;
  411. sign = nibble & (1<<(size-1));
  412. delta = nibble & ((1<<(size-1))-1);
  413. diff = delta << (7 + c->step + shift);
  414. /* clamp result */
  415. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  416. /* calculate new step */
  417. if (delta >= (2*size - 3) && c->step < 3)
  418. c->step++;
  419. else if (delta == 0 && c->step > 0)
  420. c->step--;
  421. return (int16_t) c->predictor;
  422. }
  423. static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
  424. {
  425. if(!c->step) {
  426. c->predictor = 0;
  427. c->step = 127;
  428. }
  429. c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
  430. c->predictor = av_clip_int16(c->predictor);
  431. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  432. c->step = av_clip(c->step, 127, 24576);
  433. return c->predictor;
  434. }
  435. static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
  436. {
  437. c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];
  438. c->predictor = av_clip_int16(c->predictor);
  439. c->step += ff_adpcm_index_table[nibble];
  440. c->step = av_clip_uintp2(c->step, 5);
  441. return c->predictor;
  442. }
  443. static inline int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
  444. {
  445. int16_t index = c->step_index;
  446. uint32_t lookup_sample = ff_adpcm_step_table[index];
  447. int32_t sample = 0;
  448. if (nibble & 0x40)
  449. sample += lookup_sample;
  450. if (nibble & 0x20)
  451. sample += lookup_sample >> 1;
  452. if (nibble & 0x10)
  453. sample += lookup_sample >> 2;
  454. if (nibble & 0x08)
  455. sample += lookup_sample >> 3;
  456. if (nibble & 0x04)
  457. sample += lookup_sample >> 4;
  458. if (nibble & 0x02)
  459. sample += lookup_sample >> 5;
  460. if (nibble & 0x01)
  461. sample += lookup_sample >> 6;
  462. if (nibble & 0x80)
  463. sample = -sample;
  464. sample += c->predictor;
  465. sample = av_clip_int16(sample);
  466. index += zork_index_table[(nibble >> 4) & 7];
  467. index = av_clip(index, 0, 88);
  468. c->predictor = sample;
  469. c->step_index = index;
  470. return sample;
  471. }
  472. static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
  473. const uint8_t *in, ADPCMChannelStatus *left,
  474. ADPCMChannelStatus *right, int channels, int sample_offset)
  475. {
  476. int i, j;
  477. int shift,filter,f0,f1;
  478. int s_1,s_2;
  479. int d,s,t;
  480. out0 += sample_offset;
  481. if (channels == 1)
  482. out1 = out0 + 28;
  483. else
  484. out1 += sample_offset;
  485. for(i=0;i<4;i++) {
  486. shift = 12 - (in[4+i*2] & 15);
  487. filter = in[4+i*2] >> 4;
  488. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  489. avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
  490. filter=0;
  491. }
  492. if (shift < 0) {
  493. avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
  494. shift = 0;
  495. }
  496. f0 = xa_adpcm_table[filter][0];
  497. f1 = xa_adpcm_table[filter][1];
  498. s_1 = left->sample1;
  499. s_2 = left->sample2;
  500. for(j=0;j<28;j++) {
  501. d = in[16+i+j*4];
  502. t = sign_extend(d, 4);
  503. s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
  504. s_2 = s_1;
  505. s_1 = av_clip_int16(s);
  506. out0[j] = s_1;
  507. }
  508. if (channels == 2) {
  509. left->sample1 = s_1;
  510. left->sample2 = s_2;
  511. s_1 = right->sample1;
  512. s_2 = right->sample2;
  513. }
  514. shift = 12 - (in[5+i*2] & 15);
  515. filter = in[5+i*2] >> 4;
  516. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
  517. avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
  518. filter=0;
  519. }
  520. if (shift < 0) {
  521. avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
  522. shift = 0;
  523. }
  524. f0 = xa_adpcm_table[filter][0];
  525. f1 = xa_adpcm_table[filter][1];
  526. for(j=0;j<28;j++) {
  527. d = in[16+i+j*4];
  528. t = sign_extend(d >> 4, 4);
  529. s = t*(1<<shift) + ((s_1*f0 + s_2*f1+32)>>6);
  530. s_2 = s_1;
  531. s_1 = av_clip_int16(s);
  532. out1[j] = s_1;
  533. }
  534. if (channels == 2) {
  535. right->sample1 = s_1;
  536. right->sample2 = s_2;
  537. } else {
  538. left->sample1 = s_1;
  539. left->sample2 = s_2;
  540. }
  541. out0 += 28 * (3 - channels);
  542. out1 += 28 * (3 - channels);
  543. }
  544. return 0;
  545. }
  546. static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
  547. {
  548. ADPCMDecodeContext *c = avctx->priv_data;
  549. GetBitContext gb;
  550. const int8_t *table;
  551. int k0, signmask, nb_bits, count;
  552. int size = buf_size*8;
  553. int i;
  554. init_get_bits(&gb, buf, size);
  555. //read bits & initial values
  556. nb_bits = get_bits(&gb, 2)+2;
  557. table = swf_index_tables[nb_bits-2];
  558. k0 = 1 << (nb_bits-2);
  559. signmask = 1 << (nb_bits-1);
  560. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  561. for (i = 0; i < avctx->channels; i++) {
  562. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  563. c->status[i].step_index = get_bits(&gb, 6);
  564. }
  565. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  566. int i;
  567. for (i = 0; i < avctx->channels; i++) {
  568. // similar to IMA adpcm
  569. int delta = get_bits(&gb, nb_bits);
  570. int step = ff_adpcm_step_table[c->status[i].step_index];
  571. int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  572. int k = k0;
  573. do {
  574. if (delta & k)
  575. vpdiff += step;
  576. step >>= 1;
  577. k >>= 1;
  578. } while(k);
  579. vpdiff += step;
  580. if (delta & signmask)
  581. c->status[i].predictor -= vpdiff;
  582. else
  583. c->status[i].predictor += vpdiff;
  584. c->status[i].step_index += table[delta & (~signmask)];
  585. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  586. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  587. *samples++ = c->status[i].predictor;
  588. }
  589. }
  590. }
  591. }
  592. int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
  593. {
  594. int sample = sign_extend(nibble, 4) * (1 << shift);
  595. if (flag)
  596. sample += (8 * cs->sample1) - (4 * cs->sample2);
  597. else
  598. sample += 4 * cs->sample1;
  599. sample = av_clip_int16(sample >> 2);
  600. cs->sample2 = cs->sample1;
  601. cs->sample1 = sample;
  602. return sample;
  603. }
  604. /**
  605. * Get the number of samples (per channel) that will be decoded from the packet.
  606. * In one case, this is actually the maximum number of samples possible to
  607. * decode with the given buf_size.
  608. *
  609. * @param[out] coded_samples set to the number of samples as coded in the
  610. * packet, or 0 if the codec does not encode the
  611. * number of samples in each frame.
  612. * @param[out] approx_nb_samples set to non-zero if the number of samples
  613. * returned is an approximation.
  614. */
  615. static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
  616. int buf_size, int *coded_samples, int *approx_nb_samples)
  617. {
  618. ADPCMDecodeContext *s = avctx->priv_data;
  619. int nb_samples = 0;
  620. int ch = avctx->channels;
  621. int has_coded_samples = 0;
  622. int header_size;
  623. *coded_samples = 0;
  624. *approx_nb_samples = 0;
  625. if(ch <= 0)
  626. return 0;
  627. switch (avctx->codec->id) {
  628. /* constant, only check buf_size */
  629. case AV_CODEC_ID_ADPCM_EA_XAS:
  630. if (buf_size < 76 * ch)
  631. return 0;
  632. nb_samples = 128;
  633. break;
  634. case AV_CODEC_ID_ADPCM_IMA_QT:
  635. if (buf_size < 34 * ch)
  636. return 0;
  637. nb_samples = 64;
  638. break;
  639. /* simple 4-bit adpcm */
  640. case AV_CODEC_ID_ADPCM_CT:
  641. case AV_CODEC_ID_ADPCM_IMA_APC:
  642. case AV_CODEC_ID_ADPCM_IMA_CUNNING:
  643. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  644. case AV_CODEC_ID_ADPCM_IMA_OKI:
  645. case AV_CODEC_ID_ADPCM_IMA_WS:
  646. case AV_CODEC_ID_ADPCM_YAMAHA:
  647. case AV_CODEC_ID_ADPCM_AICA:
  648. case AV_CODEC_ID_ADPCM_IMA_SSI:
  649. case AV_CODEC_ID_ADPCM_IMA_APM:
  650. case AV_CODEC_ID_ADPCM_IMA_ALP:
  651. case AV_CODEC_ID_ADPCM_IMA_MTF:
  652. nb_samples = buf_size * 2 / ch;
  653. break;
  654. }
  655. if (nb_samples)
  656. return nb_samples;
  657. /* simple 4-bit adpcm, with header */
  658. header_size = 0;
  659. switch (avctx->codec->id) {
  660. case AV_CODEC_ID_ADPCM_4XM:
  661. case AV_CODEC_ID_ADPCM_AGM:
  662. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  663. case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
  664. case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
  665. case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
  666. case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
  667. }
  668. if (header_size > 0)
  669. return (buf_size - header_size) * 2 / ch;
  670. /* more complex formats */
  671. switch (avctx->codec->id) {
  672. case AV_CODEC_ID_ADPCM_EA:
  673. has_coded_samples = 1;
  674. *coded_samples = bytestream2_get_le32(gb);
  675. *coded_samples -= *coded_samples % 28;
  676. nb_samples = (buf_size - 12) / 30 * 28;
  677. break;
  678. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  679. has_coded_samples = 1;
  680. *coded_samples = bytestream2_get_le32(gb);
  681. nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
  682. break;
  683. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  684. nb_samples = (buf_size - ch) / ch * 2;
  685. break;
  686. case AV_CODEC_ID_ADPCM_EA_R1:
  687. case AV_CODEC_ID_ADPCM_EA_R2:
  688. case AV_CODEC_ID_ADPCM_EA_R3:
  689. /* maximum number of samples */
  690. /* has internal offsets and a per-frame switch to signal raw 16-bit */
  691. has_coded_samples = 1;
  692. switch (avctx->codec->id) {
  693. case AV_CODEC_ID_ADPCM_EA_R1:
  694. header_size = 4 + 9 * ch;
  695. *coded_samples = bytestream2_get_le32(gb);
  696. break;
  697. case AV_CODEC_ID_ADPCM_EA_R2:
  698. header_size = 4 + 5 * ch;
  699. *coded_samples = bytestream2_get_le32(gb);
  700. break;
  701. case AV_CODEC_ID_ADPCM_EA_R3:
  702. header_size = 4 + 5 * ch;
  703. *coded_samples = bytestream2_get_be32(gb);
  704. break;
  705. }
  706. *coded_samples -= *coded_samples % 28;
  707. nb_samples = (buf_size - header_size) * 2 / ch;
  708. nb_samples -= nb_samples % 28;
  709. *approx_nb_samples = 1;
  710. break;
  711. case AV_CODEC_ID_ADPCM_IMA_DK3:
  712. if (avctx->block_align > 0)
  713. buf_size = FFMIN(buf_size, avctx->block_align);
  714. nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
  715. break;
  716. case AV_CODEC_ID_ADPCM_IMA_DK4:
  717. if (avctx->block_align > 0)
  718. buf_size = FFMIN(buf_size, avctx->block_align);
  719. if (buf_size < 4 * ch)
  720. return AVERROR_INVALIDDATA;
  721. nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
  722. break;
  723. case AV_CODEC_ID_ADPCM_IMA_RAD:
  724. if (avctx->block_align > 0)
  725. buf_size = FFMIN(buf_size, avctx->block_align);
  726. nb_samples = (buf_size - 4 * ch) * 2 / ch;
  727. break;
  728. case AV_CODEC_ID_ADPCM_IMA_WAV:
  729. {
  730. int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
  731. int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
  732. if (avctx->block_align > 0)
  733. buf_size = FFMIN(buf_size, avctx->block_align);
  734. if (buf_size < 4 * ch)
  735. return AVERROR_INVALIDDATA;
  736. nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
  737. break;
  738. }
  739. case AV_CODEC_ID_ADPCM_MS:
  740. if (avctx->block_align > 0)
  741. buf_size = FFMIN(buf_size, avctx->block_align);
  742. nb_samples = (buf_size - 6 * ch) * 2 / ch;
  743. break;
  744. case AV_CODEC_ID_ADPCM_MTAF:
  745. if (avctx->block_align > 0)
  746. buf_size = FFMIN(buf_size, avctx->block_align);
  747. nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
  748. break;
  749. case AV_CODEC_ID_ADPCM_SBPRO_2:
  750. case AV_CODEC_ID_ADPCM_SBPRO_3:
  751. case AV_CODEC_ID_ADPCM_SBPRO_4:
  752. {
  753. int samples_per_byte;
  754. switch (avctx->codec->id) {
  755. case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
  756. case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
  757. case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
  758. }
  759. if (!s->status[0].step_index) {
  760. if (buf_size < ch)
  761. return AVERROR_INVALIDDATA;
  762. nb_samples++;
  763. buf_size -= ch;
  764. }
  765. nb_samples += buf_size * samples_per_byte / ch;
  766. break;
  767. }
  768. case AV_CODEC_ID_ADPCM_SWF:
  769. {
  770. int buf_bits = buf_size * 8 - 2;
  771. int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
  772. int block_hdr_size = 22 * ch;
  773. int block_size = block_hdr_size + nbits * ch * 4095;
  774. int nblocks = buf_bits / block_size;
  775. int bits_left = buf_bits - nblocks * block_size;
  776. nb_samples = nblocks * 4096;
  777. if (bits_left >= block_hdr_size)
  778. nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
  779. break;
  780. }
  781. case AV_CODEC_ID_ADPCM_THP:
  782. case AV_CODEC_ID_ADPCM_THP_LE:
  783. if (avctx->extradata) {
  784. nb_samples = buf_size * 14 / (8 * ch);
  785. break;
  786. }
  787. has_coded_samples = 1;
  788. bytestream2_skip(gb, 4); // channel size
  789. *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
  790. bytestream2_get_le32(gb) :
  791. bytestream2_get_be32(gb);
  792. buf_size -= 8 + 36 * ch;
  793. buf_size /= ch;
  794. nb_samples = buf_size / 8 * 14;
  795. if (buf_size % 8 > 1)
  796. nb_samples += (buf_size % 8 - 1) * 2;
  797. *approx_nb_samples = 1;
  798. break;
  799. case AV_CODEC_ID_ADPCM_AFC:
  800. nb_samples = buf_size / (9 * ch) * 16;
  801. break;
  802. case AV_CODEC_ID_ADPCM_XA:
  803. nb_samples = (buf_size / 128) * 224 / ch;
  804. break;
  805. case AV_CODEC_ID_ADPCM_DTK:
  806. case AV_CODEC_ID_ADPCM_PSX:
  807. nb_samples = buf_size / (16 * ch) * 28;
  808. break;
  809. case AV_CODEC_ID_ADPCM_ARGO:
  810. nb_samples = buf_size / avctx->block_align * 32;
  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. av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
  1771. /* Read in every sample for this channel. */
  1772. for (i = 0; i < nb_samples_per_block / 28; i++) {
  1773. int filter, shift, flag, byte;
  1774. filter = bytestream2_get_byteu(&gb);
  1775. shift = filter & 0xf;
  1776. filter = filter >> 4;
  1777. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
  1778. return AVERROR_INVALIDDATA;
  1779. flag = bytestream2_get_byteu(&gb);
  1780. /* Decode 28 samples. */
  1781. for (n = 0; n < 28; n++) {
  1782. int sample = 0, scale;
  1783. if (flag < 0x07) {
  1784. if (n & 1) {
  1785. scale = sign_extend(byte >> 4, 4);
  1786. } else {
  1787. byte = bytestream2_get_byteu(&gb);
  1788. scale = sign_extend(byte, 4);
  1789. }
  1790. scale = scale * (1 << 12);
  1791. sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
  1792. }
  1793. *samples++ = av_clip_int16(sample);
  1794. c->status[channel].sample2 = c->status[channel].sample1;
  1795. c->status[channel].sample1 = sample;
  1796. }
  1797. }
  1798. }
  1799. }
  1800. break;
  1801. case AV_CODEC_ID_ADPCM_ARGO:
  1802. /*
  1803. * The format of each block:
  1804. * uint8_t left_control;
  1805. * uint4_t left_samples[nb_samples];
  1806. * ---- and if stereo ----
  1807. * uint8_t right_control;
  1808. * uint4_t right_samples[nb_samples];
  1809. *
  1810. * Format of the control byte:
  1811. * MSB [SSSSRDRR] LSB
  1812. * S = (Shift Amount - 2)
  1813. * D = Decoder flag.
  1814. * R = Reserved
  1815. *
  1816. * Each block relies on the previous two samples of each channel.
  1817. * They should be 0 initially.
  1818. */
  1819. for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
  1820. for (channel = 0; channel < avctx->channels; channel++) {
  1821. int control, shift;
  1822. samples = samples_p[channel] + block * 32;
  1823. cs = c->status + channel;
  1824. /* Get the control byte and decode the samples, 2 at a time. */
  1825. control = bytestream2_get_byteu(&gb);
  1826. shift = (control >> 4) + 2;
  1827. for (n = 0; n < 16; n++) {
  1828. int sample = bytestream2_get_byteu(&gb);
  1829. *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift, control & 0x04);
  1830. *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift, control & 0x04);
  1831. }
  1832. }
  1833. }
  1834. break;
  1835. case AV_CODEC_ID_ADPCM_ZORK:
  1836. if (!c->has_status) {
  1837. for (channel = 0; channel < avctx->channels; channel++) {
  1838. c->status[channel].predictor = 0;
  1839. c->status[channel].step_index = 0;
  1840. }
  1841. c->has_status = 1;
  1842. }
  1843. for (n = 0; n < nb_samples * avctx->channels; n++) {
  1844. int v = bytestream2_get_byteu(&gb);
  1845. *samples++ = adpcm_zork_expand_nibble(&c->status[n % avctx->channels], v);
  1846. }
  1847. break;
  1848. case AV_CODEC_ID_ADPCM_IMA_MTF:
  1849. for (n = nb_samples / 2; n > 0; n--) {
  1850. for (channel = 0; channel < avctx->channels; channel++) {
  1851. int v = bytestream2_get_byteu(&gb);
  1852. *samples++ = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
  1853. samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
  1854. }
  1855. samples += avctx->channels;
  1856. }
  1857. break;
  1858. default:
  1859. av_assert0(0); // unsupported codec_id should not happen
  1860. }
  1861. if (avpkt->size && bytestream2_tell(&gb) == 0) {
  1862. av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
  1863. return AVERROR_INVALIDDATA;
  1864. }
  1865. *got_frame_ptr = 1;
  1866. if (avpkt->size < bytestream2_tell(&gb)) {
  1867. av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
  1868. return avpkt->size;
  1869. }
  1870. return bytestream2_tell(&gb);
  1871. }
  1872. static void adpcm_flush(AVCodecContext *avctx)
  1873. {
  1874. ADPCMDecodeContext *c = avctx->priv_data;
  1875. c->has_status = 0;
  1876. }
  1877. static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
  1878. AV_SAMPLE_FMT_NONE };
  1879. static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
  1880. AV_SAMPLE_FMT_NONE };
  1881. static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
  1882. AV_SAMPLE_FMT_S16P,
  1883. AV_SAMPLE_FMT_NONE };
  1884. #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
  1885. AVCodec ff_ ## name_ ## _decoder = { \
  1886. .name = #name_, \
  1887. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1888. .type = AVMEDIA_TYPE_AUDIO, \
  1889. .id = id_, \
  1890. .priv_data_size = sizeof(ADPCMDecodeContext), \
  1891. .init = adpcm_decode_init, \
  1892. .decode = adpcm_decode_frame, \
  1893. .flush = adpcm_flush, \
  1894. .capabilities = AV_CODEC_CAP_DR1, \
  1895. .sample_fmts = sample_fmts_, \
  1896. }
  1897. /* Note: Do not forget to add new entries to the Makefile as well. */
  1898. ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
  1899. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
  1900. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM, sample_fmts_s16, adpcm_agm, "ADPCM AmuseGraphics Movie");
  1901. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
  1902. ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO, sample_fmts_s16p, adpcm_argo, "ADPCM Argonaut Games");
  1903. ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
  1904. ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
  1905. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
  1906. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1907. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1908. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1909. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1910. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1911. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
  1912. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
  1913. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM, sample_fmts_s16, adpcm_ima_apm, "ADPCM IMA Ubisoft APM");
  1914. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_CUNNING, sample_fmts_s16, adpcm_ima_cunning, "ADPCM IMA Cunning Developments");
  1915. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
  1916. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1917. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1918. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1919. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1920. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1921. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MOFLEX, sample_fmts_s16p, adpcm_ima_moflex, "ADPCM IMA MobiClip MOFLEX");
  1922. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_MTF, sample_fmts_s16, adpcm_ima_mtf, "ADPCM IMA Capcom's MT Framework");
  1923. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
  1924. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1925. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
  1926. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SSI, sample_fmts_s16, adpcm_ima_ssi, "ADPCM IMA Simon & Schuster Interactive");
  1927. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1928. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ALP, sample_fmts_s16, adpcm_ima_alp, "ADPCM IMA High Voltage Software ALP");
  1929. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
  1930. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
  1931. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_both, adpcm_ms, "ADPCM Microsoft");
  1932. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF, sample_fmts_s16p, adpcm_mtaf, "ADPCM MTAF");
  1933. ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
  1934. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1935. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1936. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1937. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
  1938. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
  1939. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
  1940. ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
  1941. ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");
  1942. ADPCM_DECODER(AV_CODEC_ID_ADPCM_ZORK, sample_fmts_s16, adpcm_zork, "ADPCM Zork");