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.

2213 lines
85KB

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