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.

2050 lines
79KB

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