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.

1585 lines
61KB

  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. *
  16. * This file is part of FFmpeg.
  17. *
  18. * FFmpeg is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU Lesser General Public
  20. * License as published by the Free Software Foundation; either
  21. * version 2.1 of the License, or (at your option) any later version.
  22. *
  23. * FFmpeg is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * Lesser General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Lesser General Public
  29. * License along with FFmpeg; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  31. */
  32. #include "avcodec.h"
  33. #include "get_bits.h"
  34. #include "bytestream.h"
  35. #include "adpcm.h"
  36. #include "adpcm_data.h"
  37. #include "internal.h"
  38. /**
  39. * @file
  40. * ADPCM decoders
  41. * Features and limitations:
  42. *
  43. * Reference documents:
  44. * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
  45. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
  46. * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
  47. * http://openquicktime.sourceforge.net/
  48. * XAnim sources (xa_codec.c) http://xanim.polter.net/
  49. * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
  50. * SoX source code http://sox.sourceforge.net/
  51. *
  52. * CD-ROM XA:
  53. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
  54. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
  55. * readstr http://www.geocities.co.jp/Playtown/2004/
  56. */
  57. /* These are for CD-ROM XA ADPCM */
  58. static const int xa_adpcm_table[5][2] = {
  59. { 0, 0 },
  60. { 60, 0 },
  61. { 115, -52 },
  62. { 98, -55 },
  63. { 122, -60 }
  64. };
  65. static const int ea_adpcm_table[] = {
  66. 0, 240, 460, 392,
  67. 0, 0, -208, -220,
  68. 0, 1, 3, 4,
  69. 7, 8, 10, 11,
  70. 0, -1, -3, -4
  71. };
  72. // padded to zero where table size is less then 16
  73. static const int swf_index_tables[4][16] = {
  74. /*2*/ { -1, 2 },
  75. /*3*/ { -1, -1, 2, 4 },
  76. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  77. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  78. };
  79. /* end of tables */
  80. typedef struct ADPCMDecodeContext {
  81. ADPCMChannelStatus status[6];
  82. int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
  83. } ADPCMDecodeContext;
  84. static av_cold int adpcm_decode_init(AVCodecContext * avctx)
  85. {
  86. ADPCMDecodeContext *c = avctx->priv_data;
  87. unsigned int min_channels = 1;
  88. unsigned int max_channels = 2;
  89. switch(avctx->codec->id) {
  90. case AV_CODEC_ID_ADPCM_DTK:
  91. case AV_CODEC_ID_ADPCM_EA:
  92. min_channels = 2;
  93. break;
  94. case AV_CODEC_ID_ADPCM_AFC:
  95. case AV_CODEC_ID_ADPCM_EA_R1:
  96. case AV_CODEC_ID_ADPCM_EA_R2:
  97. case AV_CODEC_ID_ADPCM_EA_R3:
  98. case AV_CODEC_ID_ADPCM_EA_XAS:
  99. case AV_CODEC_ID_ADPCM_THP:
  100. max_channels = 6;
  101. break;
  102. }
  103. if (avctx->channels < min_channels || avctx->channels > max_channels) {
  104. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  105. return AVERROR(EINVAL);
  106. }
  107. switch(avctx->codec->id) {
  108. case AV_CODEC_ID_ADPCM_CT:
  109. c->status[0].step = c->status[1].step = 511;
  110. break;
  111. case AV_CODEC_ID_ADPCM_IMA_WAV:
  112. if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
  113. return AVERROR_INVALIDDATA;
  114. break;
  115. case AV_CODEC_ID_ADPCM_IMA_APC:
  116. if (avctx->extradata && avctx->extradata_size >= 8) {
  117. c->status[0].predictor = AV_RL32(avctx->extradata);
  118. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  119. }
  120. break;
  121. case AV_CODEC_ID_ADPCM_IMA_WS:
  122. if (avctx->extradata && avctx->extradata_size >= 2)
  123. c->vqa_version = AV_RL16(avctx->extradata);
  124. break;
  125. default:
  126. break;
  127. }
  128. switch(avctx->codec->id) {
  129. case AV_CODEC_ID_ADPCM_IMA_QT:
  130. case AV_CODEC_ID_ADPCM_IMA_WAV:
  131. case AV_CODEC_ID_ADPCM_4XM:
  132. case AV_CODEC_ID_ADPCM_XA:
  133. case AV_CODEC_ID_ADPCM_EA_R1:
  134. case AV_CODEC_ID_ADPCM_EA_R2:
  135. case AV_CODEC_ID_ADPCM_EA_R3:
  136. case AV_CODEC_ID_ADPCM_EA_XAS:
  137. case AV_CODEC_ID_ADPCM_THP:
  138. case AV_CODEC_ID_ADPCM_AFC:
  139. case AV_CODEC_ID_ADPCM_DTK:
  140. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  141. break;
  142. case AV_CODEC_ID_ADPCM_IMA_WS:
  143. avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
  144. AV_SAMPLE_FMT_S16;
  145. break;
  146. default:
  147. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  148. }
  149. return 0;
  150. }
  151. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  152. {
  153. int step_index;
  154. int predictor;
  155. int sign, delta, diff, step;
  156. step = ff_adpcm_step_table[c->step_index];
  157. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  158. step_index = av_clip(step_index, 0, 88);
  159. sign = nibble & 8;
  160. delta = nibble & 7;
  161. /* perform direct multiplication instead of series of jumps proposed by
  162. * the reference ADPCM implementation since modern CPUs can do the mults
  163. * quickly enough */
  164. diff = ((2 * delta + 1) * step) >> shift;
  165. predictor = c->predictor;
  166. if (sign) predictor -= diff;
  167. else predictor += diff;
  168. c->predictor = av_clip_int16(predictor);
  169. c->step_index = step_index;
  170. return (short)c->predictor;
  171. }
  172. static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
  173. {
  174. int nibble, step_index, predictor, sign, delta, diff, step, shift;
  175. shift = bps - 1;
  176. nibble = get_bits_le(gb, bps),
  177. step = ff_adpcm_step_table[c->step_index];
  178. step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
  179. step_index = av_clip(step_index, 0, 88);
  180. sign = nibble & (1 << shift);
  181. delta = nibble & ((1 << shift) - 1);
  182. diff = ((2 * delta + 1) * step) >> shift;
  183. predictor = c->predictor;
  184. if (sign) predictor -= diff;
  185. else predictor += diff;
  186. c->predictor = av_clip_int16(predictor);
  187. c->step_index = step_index;
  188. return (int16_t)c->predictor;
  189. }
  190. static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
  191. {
  192. int step_index;
  193. int predictor;
  194. int diff, step;
  195. step = ff_adpcm_step_table[c->step_index];
  196. step_index = c->step_index + ff_adpcm_index_table[nibble];
  197. step_index = av_clip(step_index, 0, 88);
  198. diff = step >> 3;
  199. if (nibble & 4) diff += step;
  200. if (nibble & 2) diff += step >> 1;
  201. if (nibble & 1) diff += step >> 2;
  202. if (nibble & 8)
  203. predictor = c->predictor - diff;
  204. else
  205. predictor = c->predictor + diff;
  206. c->predictor = av_clip_int16(predictor);
  207. c->step_index = step_index;
  208. return c->predictor;
  209. }
  210. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
  211. {
  212. int predictor;
  213. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  214. predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  215. c->sample2 = c->sample1;
  216. c->sample1 = av_clip_int16(predictor);
  217. c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
  218. if (c->idelta < 16) c->idelta = 16;
  219. return c->sample1;
  220. }
  221. static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
  222. {
  223. int step_index, predictor, sign, delta, diff, step;
  224. step = ff_adpcm_oki_step_table[c->step_index];
  225. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  226. step_index = av_clip(step_index, 0, 48);
  227. sign = nibble & 8;
  228. delta = nibble & 7;
  229. diff = ((2 * delta + 1) * step) >> 3;
  230. predictor = c->predictor;
  231. if (sign) predictor -= diff;
  232. else predictor += diff;
  233. c->predictor = av_clip(predictor, -2048, 2047);
  234. c->step_index = step_index;
  235. return c->predictor << 4;
  236. }
  237. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  238. {
  239. int sign, delta, diff;
  240. int new_step;
  241. sign = nibble & 8;
  242. delta = nibble & 7;
  243. /* perform direct multiplication instead of series of jumps proposed by
  244. * the reference ADPCM implementation since modern CPUs can do the mults
  245. * quickly enough */
  246. diff = ((2 * delta + 1) * c->step) >> 3;
  247. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  248. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  249. c->predictor = av_clip_int16(c->predictor);
  250. /* calculate new step and clamp it to range 511..32767 */
  251. new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
  252. c->step = av_clip(new_step, 511, 32767);
  253. return (short)c->predictor;
  254. }
  255. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  256. {
  257. int sign, delta, diff;
  258. sign = nibble & (1<<(size-1));
  259. delta = nibble & ((1<<(size-1))-1);
  260. diff = delta << (7 + c->step + shift);
  261. /* clamp result */
  262. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  263. /* calculate new step */
  264. if (delta >= (2*size - 3) && c->step < 3)
  265. c->step++;
  266. else if (delta == 0 && c->step > 0)
  267. c->step--;
  268. return (short) c->predictor;
  269. }
  270. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  271. {
  272. if(!c->step) {
  273. c->predictor = 0;
  274. c->step = 127;
  275. }
  276. c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
  277. c->predictor = av_clip_int16(c->predictor);
  278. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  279. c->step = av_clip(c->step, 127, 24567);
  280. return c->predictor;
  281. }
  282. static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
  283. const uint8_t *in, ADPCMChannelStatus *left,
  284. ADPCMChannelStatus *right, int channels, int sample_offset)
  285. {
  286. int i, j;
  287. int shift,filter,f0,f1;
  288. int s_1,s_2;
  289. int d,s,t;
  290. out0 += sample_offset;
  291. if (channels == 1)
  292. out1 = out0 + 28;
  293. else
  294. out1 += sample_offset;
  295. for(i=0;i<4;i++) {
  296. shift = 12 - (in[4+i*2] & 15);
  297. filter = in[4+i*2] >> 4;
  298. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  299. avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
  300. filter=0;
  301. }
  302. f0 = xa_adpcm_table[filter][0];
  303. f1 = xa_adpcm_table[filter][1];
  304. s_1 = left->sample1;
  305. s_2 = left->sample2;
  306. for(j=0;j<28;j++) {
  307. d = in[16+i+j*4];
  308. t = sign_extend(d, 4);
  309. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  310. s_2 = s_1;
  311. s_1 = av_clip_int16(s);
  312. out0[j] = s_1;
  313. }
  314. if (channels == 2) {
  315. left->sample1 = s_1;
  316. left->sample2 = s_2;
  317. s_1 = right->sample1;
  318. s_2 = right->sample2;
  319. }
  320. shift = 12 - (in[5+i*2] & 15);
  321. filter = in[5+i*2] >> 4;
  322. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  323. avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
  324. filter=0;
  325. }
  326. f0 = xa_adpcm_table[filter][0];
  327. f1 = xa_adpcm_table[filter][1];
  328. for(j=0;j<28;j++) {
  329. d = in[16+i+j*4];
  330. t = sign_extend(d >> 4, 4);
  331. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  332. s_2 = s_1;
  333. s_1 = av_clip_int16(s);
  334. out1[j] = s_1;
  335. }
  336. if (channels == 2) {
  337. right->sample1 = s_1;
  338. right->sample2 = s_2;
  339. } else {
  340. left->sample1 = s_1;
  341. left->sample2 = s_2;
  342. }
  343. out0 += 28 * (3 - channels);
  344. out1 += 28 * (3 - channels);
  345. }
  346. return 0;
  347. }
  348. static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
  349. {
  350. ADPCMDecodeContext *c = avctx->priv_data;
  351. GetBitContext gb;
  352. const int *table;
  353. int k0, signmask, nb_bits, count;
  354. int size = buf_size*8;
  355. int i;
  356. init_get_bits(&gb, buf, size);
  357. //read bits & initial values
  358. nb_bits = get_bits(&gb, 2)+2;
  359. table = swf_index_tables[nb_bits-2];
  360. k0 = 1 << (nb_bits-2);
  361. signmask = 1 << (nb_bits-1);
  362. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  363. for (i = 0; i < avctx->channels; i++) {
  364. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  365. c->status[i].step_index = get_bits(&gb, 6);
  366. }
  367. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  368. int i;
  369. for (i = 0; i < avctx->channels; i++) {
  370. // similar to IMA adpcm
  371. int delta = get_bits(&gb, nb_bits);
  372. int step = ff_adpcm_step_table[c->status[i].step_index];
  373. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  374. int k = k0;
  375. do {
  376. if (delta & k)
  377. vpdiff += step;
  378. step >>= 1;
  379. k >>= 1;
  380. } while(k);
  381. vpdiff += step;
  382. if (delta & signmask)
  383. c->status[i].predictor -= vpdiff;
  384. else
  385. c->status[i].predictor += vpdiff;
  386. c->status[i].step_index += table[delta & (~signmask)];
  387. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  388. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  389. *samples++ = c->status[i].predictor;
  390. }
  391. }
  392. }
  393. }
  394. /**
  395. * Get the number of samples that will be decoded from the packet.
  396. * In one case, this is actually the maximum number of samples possible to
  397. * decode with the given buf_size.
  398. *
  399. * @param[out] coded_samples set to the number of samples as coded in the
  400. * packet, or 0 if the codec does not encode the
  401. * number of samples in each frame.
  402. * @param[out] approx_nb_samples set to non-zero if the number of samples
  403. * returned is an approximation.
  404. */
  405. static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
  406. int buf_size, int *coded_samples, int *approx_nb_samples)
  407. {
  408. ADPCMDecodeContext *s = avctx->priv_data;
  409. int nb_samples = 0;
  410. int ch = avctx->channels;
  411. int has_coded_samples = 0;
  412. int header_size;
  413. *coded_samples = 0;
  414. *approx_nb_samples = 0;
  415. if(ch <= 0)
  416. return 0;
  417. switch (avctx->codec->id) {
  418. /* constant, only check buf_size */
  419. case AV_CODEC_ID_ADPCM_EA_XAS:
  420. if (buf_size < 76 * ch)
  421. return 0;
  422. nb_samples = 128;
  423. break;
  424. case AV_CODEC_ID_ADPCM_IMA_QT:
  425. if (buf_size < 34 * ch)
  426. return 0;
  427. nb_samples = 64;
  428. break;
  429. /* simple 4-bit adpcm */
  430. case AV_CODEC_ID_ADPCM_CT:
  431. case AV_CODEC_ID_ADPCM_IMA_APC:
  432. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  433. case AV_CODEC_ID_ADPCM_IMA_OKI:
  434. case AV_CODEC_ID_ADPCM_IMA_WS:
  435. case AV_CODEC_ID_ADPCM_YAMAHA:
  436. nb_samples = buf_size * 2 / ch;
  437. break;
  438. }
  439. if (nb_samples)
  440. return nb_samples;
  441. /* simple 4-bit adpcm, with header */
  442. header_size = 0;
  443. switch (avctx->codec->id) {
  444. case AV_CODEC_ID_ADPCM_4XM:
  445. case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
  446. case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
  447. case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
  448. }
  449. if (header_size > 0)
  450. return (buf_size - header_size) * 2 / ch;
  451. /* more complex formats */
  452. switch (avctx->codec->id) {
  453. case AV_CODEC_ID_ADPCM_EA:
  454. has_coded_samples = 1;
  455. *coded_samples = bytestream2_get_le32(gb);
  456. *coded_samples -= *coded_samples % 28;
  457. nb_samples = (buf_size - 12) / 30 * 28;
  458. break;
  459. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  460. has_coded_samples = 1;
  461. *coded_samples = bytestream2_get_le32(gb);
  462. nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
  463. break;
  464. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  465. nb_samples = (buf_size - ch) / ch * 2;
  466. break;
  467. case AV_CODEC_ID_ADPCM_EA_R1:
  468. case AV_CODEC_ID_ADPCM_EA_R2:
  469. case AV_CODEC_ID_ADPCM_EA_R3:
  470. /* maximum number of samples */
  471. /* has internal offsets and a per-frame switch to signal raw 16-bit */
  472. has_coded_samples = 1;
  473. switch (avctx->codec->id) {
  474. case AV_CODEC_ID_ADPCM_EA_R1:
  475. header_size = 4 + 9 * ch;
  476. *coded_samples = bytestream2_get_le32(gb);
  477. break;
  478. case AV_CODEC_ID_ADPCM_EA_R2:
  479. header_size = 4 + 5 * ch;
  480. *coded_samples = bytestream2_get_le32(gb);
  481. *approx_nb_samples = 1;
  482. break;
  483. case AV_CODEC_ID_ADPCM_EA_R3:
  484. header_size = 4 + 5 * ch;
  485. *coded_samples = bytestream2_get_be32(gb);
  486. *approx_nb_samples = 1;
  487. break;
  488. }
  489. *coded_samples -= *coded_samples % 28;
  490. nb_samples = (buf_size - header_size) * 2 / ch;
  491. nb_samples -= nb_samples % 28;
  492. break;
  493. case AV_CODEC_ID_ADPCM_IMA_DK3:
  494. if (avctx->block_align > 0)
  495. buf_size = FFMIN(buf_size, avctx->block_align);
  496. nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
  497. break;
  498. case AV_CODEC_ID_ADPCM_IMA_DK4:
  499. if (avctx->block_align > 0)
  500. buf_size = FFMIN(buf_size, avctx->block_align);
  501. nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
  502. break;
  503. case AV_CODEC_ID_ADPCM_IMA_RAD:
  504. if (avctx->block_align > 0)
  505. buf_size = FFMIN(buf_size, avctx->block_align);
  506. nb_samples = (buf_size - 4 * ch) * 2 / ch;
  507. break;
  508. case AV_CODEC_ID_ADPCM_IMA_WAV:
  509. {
  510. int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
  511. int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
  512. if (avctx->block_align > 0)
  513. buf_size = FFMIN(buf_size, avctx->block_align);
  514. nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
  515. break;
  516. }
  517. case AV_CODEC_ID_ADPCM_MS:
  518. if (avctx->block_align > 0)
  519. buf_size = FFMIN(buf_size, avctx->block_align);
  520. nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
  521. break;
  522. case AV_CODEC_ID_ADPCM_SBPRO_2:
  523. case AV_CODEC_ID_ADPCM_SBPRO_3:
  524. case AV_CODEC_ID_ADPCM_SBPRO_4:
  525. {
  526. int samples_per_byte;
  527. switch (avctx->codec->id) {
  528. case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
  529. case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
  530. case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
  531. }
  532. if (!s->status[0].step_index) {
  533. nb_samples++;
  534. buf_size -= ch;
  535. }
  536. nb_samples += buf_size * samples_per_byte / ch;
  537. break;
  538. }
  539. case AV_CODEC_ID_ADPCM_SWF:
  540. {
  541. int buf_bits = buf_size * 8 - 2;
  542. int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
  543. int block_hdr_size = 22 * ch;
  544. int block_size = block_hdr_size + nbits * ch * 4095;
  545. int nblocks = buf_bits / block_size;
  546. int bits_left = buf_bits - nblocks * block_size;
  547. nb_samples = nblocks * 4096;
  548. if (bits_left >= block_hdr_size)
  549. nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
  550. break;
  551. }
  552. case AV_CODEC_ID_ADPCM_THP:
  553. if (avctx->extradata) {
  554. nb_samples = buf_size / (8 * ch) * 14;
  555. break;
  556. }
  557. has_coded_samples = 1;
  558. bytestream2_skip(gb, 4); // channel size
  559. *coded_samples = bytestream2_get_be32(gb);
  560. *coded_samples -= *coded_samples % 14;
  561. nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
  562. break;
  563. case AV_CODEC_ID_ADPCM_AFC:
  564. nb_samples = buf_size / (9 * ch) * 16;
  565. break;
  566. case AV_CODEC_ID_ADPCM_XA:
  567. nb_samples = (buf_size / 128) * 224 / ch;
  568. break;
  569. case AV_CODEC_ID_ADPCM_DTK:
  570. nb_samples = buf_size / (16 * ch) * 28;
  571. break;
  572. }
  573. /* validate coded sample count */
  574. if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
  575. return AVERROR_INVALIDDATA;
  576. return nb_samples;
  577. }
  578. static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
  579. int *got_frame_ptr, AVPacket *avpkt)
  580. {
  581. AVFrame *frame = data;
  582. const uint8_t *buf = avpkt->data;
  583. int buf_size = avpkt->size;
  584. ADPCMDecodeContext *c = avctx->priv_data;
  585. ADPCMChannelStatus *cs;
  586. int n, m, channel, i;
  587. short *samples;
  588. int16_t **samples_p;
  589. int st; /* stereo */
  590. int count1, count2;
  591. int nb_samples, coded_samples, approx_nb_samples, ret;
  592. GetByteContext gb;
  593. bytestream2_init(&gb, buf, buf_size);
  594. nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
  595. if (nb_samples <= 0) {
  596. av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
  597. return AVERROR_INVALIDDATA;
  598. }
  599. /* get output buffer */
  600. frame->nb_samples = nb_samples;
  601. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  602. return ret;
  603. samples = (short *)frame->data[0];
  604. samples_p = (int16_t **)frame->extended_data;
  605. /* use coded_samples when applicable */
  606. /* it is always <= nb_samples, so the output buffer will be large enough */
  607. if (coded_samples) {
  608. if (!approx_nb_samples && coded_samples != nb_samples)
  609. av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
  610. frame->nb_samples = nb_samples = coded_samples;
  611. }
  612. st = avctx->channels == 2 ? 1 : 0;
  613. switch(avctx->codec->id) {
  614. case AV_CODEC_ID_ADPCM_IMA_QT:
  615. /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
  616. Channel data is interleaved per-chunk. */
  617. for (channel = 0; channel < avctx->channels; channel++) {
  618. int predictor;
  619. int step_index;
  620. cs = &(c->status[channel]);
  621. /* (pppppp) (piiiiiii) */
  622. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  623. predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  624. step_index = predictor & 0x7F;
  625. predictor &= ~0x7F;
  626. if (cs->step_index == step_index) {
  627. int diff = predictor - cs->predictor;
  628. if (diff < 0)
  629. diff = - diff;
  630. if (diff > 0x7f)
  631. goto update;
  632. } else {
  633. update:
  634. cs->step_index = step_index;
  635. cs->predictor = predictor;
  636. }
  637. if (cs->step_index > 88u){
  638. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  639. channel, cs->step_index);
  640. return AVERROR_INVALIDDATA;
  641. }
  642. samples = samples_p[channel];
  643. for (m = 0; m < 64; m += 2) {
  644. int byte = bytestream2_get_byteu(&gb);
  645. samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
  646. samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
  647. }
  648. }
  649. break;
  650. case AV_CODEC_ID_ADPCM_IMA_WAV:
  651. for(i=0; i<avctx->channels; i++){
  652. cs = &(c->status[i]);
  653. cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
  654. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  655. if (cs->step_index > 88u){
  656. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  657. i, cs->step_index);
  658. return AVERROR_INVALIDDATA;
  659. }
  660. }
  661. if (avctx->bits_per_coded_sample != 4) {
  662. int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
  663. GetBitContext g;
  664. init_get_bits8(&g, gb.buffer, bytestream2_get_bytes_left(&gb));
  665. for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
  666. for (i = 0; i < avctx->channels; i++) {
  667. cs = &c->status[i];
  668. samples = &samples_p[i][1 + n * samples_per_block];
  669. for (m = 0; m < samples_per_block; m++) {
  670. samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
  671. avctx->bits_per_coded_sample);
  672. }
  673. }
  674. }
  675. bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
  676. } else {
  677. for (n = 0; n < (nb_samples - 1) / 8; n++) {
  678. for (i = 0; i < avctx->channels; i++) {
  679. cs = &c->status[i];
  680. samples = &samples_p[i][1 + n * 8];
  681. for (m = 0; m < 8; m += 2) {
  682. int v = bytestream2_get_byteu(&gb);
  683. samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  684. samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  685. }
  686. }
  687. }
  688. }
  689. break;
  690. case AV_CODEC_ID_ADPCM_4XM:
  691. for (i = 0; i < avctx->channels; i++)
  692. c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  693. for (i = 0; i < avctx->channels; i++) {
  694. c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  695. if (c->status[i].step_index > 88u) {
  696. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  697. i, c->status[i].step_index);
  698. return AVERROR_INVALIDDATA;
  699. }
  700. }
  701. for (i = 0; i < avctx->channels; i++) {
  702. samples = (int16_t *)frame->data[i];
  703. cs = &c->status[i];
  704. for (n = nb_samples >> 1; n > 0; n--) {
  705. int v = bytestream2_get_byteu(&gb);
  706. *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
  707. *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
  708. }
  709. }
  710. break;
  711. case AV_CODEC_ID_ADPCM_MS:
  712. {
  713. int block_predictor;
  714. block_predictor = bytestream2_get_byteu(&gb);
  715. if (block_predictor > 6) {
  716. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
  717. block_predictor);
  718. return AVERROR_INVALIDDATA;
  719. }
  720. c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  721. c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  722. if (st) {
  723. block_predictor = bytestream2_get_byteu(&gb);
  724. if (block_predictor > 6) {
  725. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
  726. block_predictor);
  727. return AVERROR_INVALIDDATA;
  728. }
  729. c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  730. c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  731. }
  732. c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  733. if (st){
  734. c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  735. }
  736. c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  737. if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  738. c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  739. if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  740. *samples++ = c->status[0].sample2;
  741. if (st) *samples++ = c->status[1].sample2;
  742. *samples++ = c->status[0].sample1;
  743. if (st) *samples++ = c->status[1].sample1;
  744. for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
  745. int byte = bytestream2_get_byteu(&gb);
  746. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
  747. *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
  748. }
  749. break;
  750. }
  751. case AV_CODEC_ID_ADPCM_IMA_DK4:
  752. for (channel = 0; channel < avctx->channels; channel++) {
  753. cs = &c->status[channel];
  754. cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
  755. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  756. if (cs->step_index > 88u){
  757. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  758. channel, cs->step_index);
  759. return AVERROR_INVALIDDATA;
  760. }
  761. }
  762. for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
  763. int v = bytestream2_get_byteu(&gb);
  764. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
  765. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  766. }
  767. break;
  768. case AV_CODEC_ID_ADPCM_IMA_DK3:
  769. {
  770. int last_byte = 0;
  771. int nibble;
  772. int decode_top_nibble_next = 0;
  773. int diff_channel;
  774. const int16_t *samples_end = samples + avctx->channels * nb_samples;
  775. bytestream2_skipu(&gb, 10);
  776. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  777. c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  778. c->status[0].step_index = bytestream2_get_byteu(&gb);
  779. c->status[1].step_index = bytestream2_get_byteu(&gb);
  780. if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
  781. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
  782. c->status[0].step_index, c->status[1].step_index);
  783. return AVERROR_INVALIDDATA;
  784. }
  785. /* sign extend the predictors */
  786. diff_channel = c->status[1].predictor;
  787. /* DK3 ADPCM support macro */
  788. #define DK3_GET_NEXT_NIBBLE() \
  789. if (decode_top_nibble_next) { \
  790. nibble = last_byte >> 4; \
  791. decode_top_nibble_next = 0; \
  792. } else { \
  793. last_byte = bytestream2_get_byteu(&gb); \
  794. nibble = last_byte & 0x0F; \
  795. decode_top_nibble_next = 1; \
  796. }
  797. while (samples < samples_end) {
  798. /* for this algorithm, c->status[0] is the sum channel and
  799. * c->status[1] is the diff channel */
  800. /* process the first predictor of the sum channel */
  801. DK3_GET_NEXT_NIBBLE();
  802. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  803. /* process the diff channel predictor */
  804. DK3_GET_NEXT_NIBBLE();
  805. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  806. /* process the first pair of stereo PCM samples */
  807. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  808. *samples++ = c->status[0].predictor + c->status[1].predictor;
  809. *samples++ = c->status[0].predictor - c->status[1].predictor;
  810. /* process the second predictor of the sum channel */
  811. DK3_GET_NEXT_NIBBLE();
  812. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  813. /* process the second pair of stereo PCM samples */
  814. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  815. *samples++ = c->status[0].predictor + c->status[1].predictor;
  816. *samples++ = c->status[0].predictor - c->status[1].predictor;
  817. }
  818. if ((bytestream2_tell(&gb) & 1))
  819. bytestream2_skip(&gb, 1);
  820. break;
  821. }
  822. case AV_CODEC_ID_ADPCM_IMA_ISS:
  823. for (channel = 0; channel < avctx->channels; channel++) {
  824. cs = &c->status[channel];
  825. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  826. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  827. if (cs->step_index > 88u){
  828. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  829. channel, cs->step_index);
  830. return AVERROR_INVALIDDATA;
  831. }
  832. }
  833. for (n = nb_samples >> (1 - st); n > 0; n--) {
  834. int v1, v2;
  835. int v = bytestream2_get_byteu(&gb);
  836. /* nibbles are swapped for mono */
  837. if (st) {
  838. v1 = v >> 4;
  839. v2 = v & 0x0F;
  840. } else {
  841. v2 = v >> 4;
  842. v1 = v & 0x0F;
  843. }
  844. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
  845. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
  846. }
  847. break;
  848. case AV_CODEC_ID_ADPCM_IMA_APC:
  849. while (bytestream2_get_bytes_left(&gb) > 0) {
  850. int v = bytestream2_get_byteu(&gb);
  851. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
  852. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  853. }
  854. break;
  855. case AV_CODEC_ID_ADPCM_IMA_OKI:
  856. while (bytestream2_get_bytes_left(&gb) > 0) {
  857. int v = bytestream2_get_byteu(&gb);
  858. *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
  859. *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
  860. }
  861. break;
  862. case AV_CODEC_ID_ADPCM_IMA_RAD:
  863. for (channel = 0; channel < avctx->channels; channel++) {
  864. cs = &c->status[channel];
  865. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  866. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  867. if (cs->step_index > 88u){
  868. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  869. channel, cs->step_index);
  870. return AVERROR_INVALIDDATA;
  871. }
  872. }
  873. for (n = 0; n < nb_samples / 2; n++) {
  874. int byte[2];
  875. byte[0] = bytestream2_get_byteu(&gb);
  876. if (st)
  877. byte[1] = bytestream2_get_byteu(&gb);
  878. for(channel = 0; channel < avctx->channels; channel++) {
  879. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
  880. }
  881. for(channel = 0; channel < avctx->channels; channel++) {
  882. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
  883. }
  884. }
  885. break;
  886. case AV_CODEC_ID_ADPCM_IMA_WS:
  887. if (c->vqa_version == 3) {
  888. for (channel = 0; channel < avctx->channels; channel++) {
  889. int16_t *smp = samples_p[channel];
  890. for (n = nb_samples / 2; n > 0; n--) {
  891. int v = bytestream2_get_byteu(&gb);
  892. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  893. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  894. }
  895. }
  896. } else {
  897. for (n = nb_samples / 2; n > 0; n--) {
  898. for (channel = 0; channel < avctx->channels; channel++) {
  899. int v = bytestream2_get_byteu(&gb);
  900. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  901. samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  902. }
  903. samples += avctx->channels;
  904. }
  905. }
  906. bytestream2_seek(&gb, 0, SEEK_END);
  907. break;
  908. case AV_CODEC_ID_ADPCM_XA:
  909. {
  910. int16_t *out0 = samples_p[0];
  911. int16_t *out1 = samples_p[1];
  912. int samples_per_block = 28 * (3 - avctx->channels) * 4;
  913. int sample_offset = 0;
  914. while (bytestream2_get_bytes_left(&gb) >= 128) {
  915. if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
  916. &c->status[0], &c->status[1],
  917. avctx->channels, sample_offset)) < 0)
  918. return ret;
  919. bytestream2_skipu(&gb, 128);
  920. sample_offset += samples_per_block;
  921. }
  922. break;
  923. }
  924. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  925. for (i=0; i<=st; i++) {
  926. c->status[i].step_index = bytestream2_get_le32u(&gb);
  927. if (c->status[i].step_index > 88u) {
  928. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  929. i, c->status[i].step_index);
  930. return AVERROR_INVALIDDATA;
  931. }
  932. }
  933. for (i=0; i<=st; i++)
  934. c->status[i].predictor = bytestream2_get_le32u(&gb);
  935. for (n = nb_samples >> (1 - st); n > 0; n--) {
  936. int byte = bytestream2_get_byteu(&gb);
  937. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
  938. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
  939. }
  940. break;
  941. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  942. for (n = nb_samples >> (1 - st); n > 0; n--) {
  943. int byte = bytestream2_get_byteu(&gb);
  944. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
  945. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
  946. }
  947. break;
  948. case AV_CODEC_ID_ADPCM_EA:
  949. {
  950. int previous_left_sample, previous_right_sample;
  951. int current_left_sample, current_right_sample;
  952. int next_left_sample, next_right_sample;
  953. int coeff1l, coeff2l, coeff1r, coeff2r;
  954. int shift_left, shift_right;
  955. /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
  956. each coding 28 stereo samples. */
  957. if(avctx->channels != 2)
  958. return AVERROR_INVALIDDATA;
  959. current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  960. previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  961. current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  962. previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  963. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  964. int byte = bytestream2_get_byteu(&gb);
  965. coeff1l = ea_adpcm_table[ byte >> 4 ];
  966. coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
  967. coeff1r = ea_adpcm_table[ byte & 0x0F];
  968. coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
  969. byte = bytestream2_get_byteu(&gb);
  970. shift_left = 20 - (byte >> 4);
  971. shift_right = 20 - (byte & 0x0F);
  972. for (count2 = 0; count2 < 28; count2++) {
  973. byte = bytestream2_get_byteu(&gb);
  974. next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
  975. next_right_sample = sign_extend(byte, 4) << shift_right;
  976. next_left_sample = (next_left_sample +
  977. (current_left_sample * coeff1l) +
  978. (previous_left_sample * coeff2l) + 0x80) >> 8;
  979. next_right_sample = (next_right_sample +
  980. (current_right_sample * coeff1r) +
  981. (previous_right_sample * coeff2r) + 0x80) >> 8;
  982. previous_left_sample = current_left_sample;
  983. current_left_sample = av_clip_int16(next_left_sample);
  984. previous_right_sample = current_right_sample;
  985. current_right_sample = av_clip_int16(next_right_sample);
  986. *samples++ = current_left_sample;
  987. *samples++ = current_right_sample;
  988. }
  989. }
  990. bytestream2_skip(&gb, 2); // Skip terminating 0x0000
  991. break;
  992. }
  993. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  994. {
  995. int coeff[2][2], shift[2];
  996. for(channel = 0; channel < avctx->channels; channel++) {
  997. int byte = bytestream2_get_byteu(&gb);
  998. for (i=0; i<2; i++)
  999. coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
  1000. shift[channel] = 20 - (byte & 0x0F);
  1001. }
  1002. for (count1 = 0; count1 < nb_samples / 2; count1++) {
  1003. int byte[2];
  1004. byte[0] = bytestream2_get_byteu(&gb);
  1005. if (st) byte[1] = bytestream2_get_byteu(&gb);
  1006. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  1007. for(channel = 0; channel < avctx->channels; channel++) {
  1008. int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
  1009. sample = (sample +
  1010. c->status[channel].sample1 * coeff[channel][0] +
  1011. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  1012. c->status[channel].sample2 = c->status[channel].sample1;
  1013. c->status[channel].sample1 = av_clip_int16(sample);
  1014. *samples++ = c->status[channel].sample1;
  1015. }
  1016. }
  1017. }
  1018. bytestream2_seek(&gb, 0, SEEK_END);
  1019. break;
  1020. }
  1021. case AV_CODEC_ID_ADPCM_EA_R1:
  1022. case AV_CODEC_ID_ADPCM_EA_R2:
  1023. case AV_CODEC_ID_ADPCM_EA_R3: {
  1024. /* channel numbering
  1025. 2chan: 0=fl, 1=fr
  1026. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  1027. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  1028. const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
  1029. int previous_sample, current_sample, next_sample;
  1030. int coeff1, coeff2;
  1031. int shift;
  1032. unsigned int channel;
  1033. uint16_t *samplesC;
  1034. int count = 0;
  1035. int offsets[6];
  1036. for (channel=0; channel<avctx->channels; channel++)
  1037. offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
  1038. bytestream2_get_le32(&gb)) +
  1039. (avctx->channels + 1) * 4;
  1040. for (channel=0; channel<avctx->channels; channel++) {
  1041. bytestream2_seek(&gb, offsets[channel], SEEK_SET);
  1042. samplesC = samples_p[channel];
  1043. if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
  1044. current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  1045. previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  1046. } else {
  1047. current_sample = c->status[channel].predictor;
  1048. previous_sample = c->status[channel].prev_sample;
  1049. }
  1050. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  1051. int byte = bytestream2_get_byte(&gb);
  1052. if (byte == 0xEE) { /* only seen in R2 and R3 */
  1053. current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  1054. previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  1055. for (count2=0; count2<28; count2++)
  1056. *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
  1057. } else {
  1058. coeff1 = ea_adpcm_table[ byte >> 4 ];
  1059. coeff2 = ea_adpcm_table[(byte >> 4) + 4];
  1060. shift = 20 - (byte & 0x0F);
  1061. for (count2=0; count2<28; count2++) {
  1062. if (count2 & 1)
  1063. next_sample = sign_extend(byte, 4) << shift;
  1064. else {
  1065. byte = bytestream2_get_byte(&gb);
  1066. next_sample = sign_extend(byte >> 4, 4) << shift;
  1067. }
  1068. next_sample += (current_sample * coeff1) +
  1069. (previous_sample * coeff2);
  1070. next_sample = av_clip_int16(next_sample >> 8);
  1071. previous_sample = current_sample;
  1072. current_sample = next_sample;
  1073. *samplesC++ = current_sample;
  1074. }
  1075. }
  1076. }
  1077. if (!count) {
  1078. count = count1;
  1079. } else if (count != count1) {
  1080. av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
  1081. count = FFMAX(count, count1);
  1082. }
  1083. if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
  1084. c->status[channel].predictor = current_sample;
  1085. c->status[channel].prev_sample = previous_sample;
  1086. }
  1087. }
  1088. frame->nb_samples = count * 28;
  1089. bytestream2_seek(&gb, 0, SEEK_END);
  1090. break;
  1091. }
  1092. case AV_CODEC_ID_ADPCM_EA_XAS:
  1093. for (channel=0; channel<avctx->channels; channel++) {
  1094. int coeff[2][4], shift[4];
  1095. int16_t *s = samples_p[channel];
  1096. for (n = 0; n < 4; n++, s += 32) {
  1097. int val = sign_extend(bytestream2_get_le16u(&gb), 16);
  1098. for (i=0; i<2; i++)
  1099. coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
  1100. s[0] = val & ~0x0F;
  1101. val = sign_extend(bytestream2_get_le16u(&gb), 16);
  1102. shift[n] = 20 - (val & 0x0F);
  1103. s[1] = val & ~0x0F;
  1104. }
  1105. for (m=2; m<32; m+=2) {
  1106. s = &samples_p[channel][m];
  1107. for (n = 0; n < 4; n++, s += 32) {
  1108. int level, pred;
  1109. int byte = bytestream2_get_byteu(&gb);
  1110. level = sign_extend(byte >> 4, 4) << shift[n];
  1111. pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
  1112. s[0] = av_clip_int16((level + pred + 0x80) >> 8);
  1113. level = sign_extend(byte, 4) << shift[n];
  1114. pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
  1115. s[1] = av_clip_int16((level + pred + 0x80) >> 8);
  1116. }
  1117. }
  1118. }
  1119. break;
  1120. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1121. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  1122. c->status[0].step_index = bytestream2_get_le16u(&gb);
  1123. bytestream2_skipu(&gb, 4);
  1124. if (c->status[0].step_index > 88u) {
  1125. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1126. c->status[0].step_index);
  1127. return AVERROR_INVALIDDATA;
  1128. }
  1129. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1130. int v = bytestream2_get_byteu(&gb);
  1131. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
  1132. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
  1133. }
  1134. break;
  1135. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1136. for (i = 0; i < avctx->channels; i++) {
  1137. c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  1138. c->status[i].step_index = bytestream2_get_byteu(&gb);
  1139. bytestream2_skipu(&gb, 1);
  1140. if (c->status[i].step_index > 88u) {
  1141. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1142. c->status[i].step_index);
  1143. return AVERROR_INVALIDDATA;
  1144. }
  1145. }
  1146. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1147. int v = bytestream2_get_byteu(&gb);
  1148. *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
  1149. *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
  1150. }
  1151. break;
  1152. case AV_CODEC_ID_ADPCM_CT:
  1153. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1154. int v = bytestream2_get_byteu(&gb);
  1155. *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
  1156. *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
  1157. }
  1158. break;
  1159. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1160. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1161. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1162. if (!c->status[0].step_index) {
  1163. /* the first byte is a raw sample */
  1164. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1165. if (st)
  1166. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1167. c->status[0].step_index = 1;
  1168. nb_samples--;
  1169. }
  1170. if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
  1171. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1172. int byte = bytestream2_get_byteu(&gb);
  1173. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1174. byte >> 4, 4, 0);
  1175. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1176. byte & 0x0F, 4, 0);
  1177. }
  1178. } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
  1179. for (n = (nb_samples<<st) / 3; n > 0; n--) {
  1180. int byte = bytestream2_get_byteu(&gb);
  1181. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1182. byte >> 5 , 3, 0);
  1183. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1184. (byte >> 2) & 0x07, 3, 0);
  1185. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1186. byte & 0x03, 2, 0);
  1187. }
  1188. } else {
  1189. for (n = nb_samples >> (2 - st); n > 0; n--) {
  1190. int byte = bytestream2_get_byteu(&gb);
  1191. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1192. byte >> 6 , 2, 2);
  1193. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1194. (byte >> 4) & 0x03, 2, 2);
  1195. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1196. (byte >> 2) & 0x03, 2, 2);
  1197. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1198. byte & 0x03, 2, 2);
  1199. }
  1200. }
  1201. break;
  1202. case AV_CODEC_ID_ADPCM_SWF:
  1203. adpcm_swf_decode(avctx, buf, buf_size, samples);
  1204. bytestream2_seek(&gb, 0, SEEK_END);
  1205. break;
  1206. case AV_CODEC_ID_ADPCM_YAMAHA:
  1207. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1208. int v = bytestream2_get_byteu(&gb);
  1209. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
  1210. *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
  1211. }
  1212. break;
  1213. case AV_CODEC_ID_ADPCM_AFC:
  1214. {
  1215. int samples_per_block;
  1216. int blocks;
  1217. if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
  1218. samples_per_block = avctx->extradata[0] / 16;
  1219. blocks = nb_samples / avctx->extradata[0];
  1220. } else {
  1221. samples_per_block = nb_samples / 16;
  1222. blocks = 1;
  1223. }
  1224. for (m = 0; m < blocks; m++) {
  1225. for (channel = 0; channel < avctx->channels; channel++) {
  1226. int prev1 = c->status[channel].sample1;
  1227. int prev2 = c->status[channel].sample2;
  1228. samples = samples_p[channel] + m * 16;
  1229. /* Read in every sample for this channel. */
  1230. for (i = 0; i < samples_per_block; i++) {
  1231. int byte = bytestream2_get_byteu(&gb);
  1232. int scale = 1 << (byte >> 4);
  1233. int index = byte & 0xf;
  1234. int factor1 = ff_adpcm_afc_coeffs[0][index];
  1235. int factor2 = ff_adpcm_afc_coeffs[1][index];
  1236. /* Decode 16 samples. */
  1237. for (n = 0; n < 16; n++) {
  1238. int32_t sampledat;
  1239. if (n & 1) {
  1240. sampledat = sign_extend(byte, 4);
  1241. } else {
  1242. byte = bytestream2_get_byteu(&gb);
  1243. sampledat = sign_extend(byte >> 4, 4);
  1244. }
  1245. sampledat = ((prev1 * factor1 + prev2 * factor2) +
  1246. ((sampledat * scale) << 11)) >> 11;
  1247. *samples = av_clip_int16(sampledat);
  1248. prev2 = prev1;
  1249. prev1 = *samples++;
  1250. }
  1251. }
  1252. c->status[channel].sample1 = prev1;
  1253. c->status[channel].sample2 = prev2;
  1254. }
  1255. }
  1256. bytestream2_seek(&gb, 0, SEEK_END);
  1257. break;
  1258. }
  1259. case AV_CODEC_ID_ADPCM_THP:
  1260. {
  1261. int table[6][16];
  1262. int ch;
  1263. if (avctx->extradata) {
  1264. GetByteContext tb;
  1265. if (avctx->extradata_size < 32 * avctx->channels) {
  1266. av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
  1267. return AVERROR_INVALIDDATA;
  1268. }
  1269. bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
  1270. for (i = 0; i < avctx->channels; i++)
  1271. for (n = 0; n < 16; n++)
  1272. table[i][n] = sign_extend(bytestream2_get_be16u(&tb), 16);
  1273. } else {
  1274. for (i = 0; i < avctx->channels; i++)
  1275. for (n = 0; n < 16; n++)
  1276. table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
  1277. /* Initialize the previous sample. */
  1278. for (i = 0; i < avctx->channels; i++) {
  1279. c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
  1280. c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
  1281. }
  1282. }
  1283. for (ch = 0; ch < avctx->channels; ch++) {
  1284. samples = samples_p[ch];
  1285. /* Read in every sample for this channel. */
  1286. for (i = 0; i < nb_samples / 14; i++) {
  1287. int byte = bytestream2_get_byteu(&gb);
  1288. int index = (byte >> 4) & 7;
  1289. unsigned int exp = byte & 0x0F;
  1290. int factor1 = table[ch][index * 2];
  1291. int factor2 = table[ch][index * 2 + 1];
  1292. /* Decode 14 samples. */
  1293. for (n = 0; n < 14; n++) {
  1294. int32_t sampledat;
  1295. if (n & 1) {
  1296. sampledat = sign_extend(byte, 4);
  1297. } else {
  1298. byte = bytestream2_get_byteu(&gb);
  1299. sampledat = sign_extend(byte >> 4, 4);
  1300. }
  1301. sampledat = ((c->status[ch].sample1 * factor1
  1302. + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
  1303. *samples = av_clip_int16(sampledat);
  1304. c->status[ch].sample2 = c->status[ch].sample1;
  1305. c->status[ch].sample1 = *samples++;
  1306. }
  1307. }
  1308. }
  1309. break;
  1310. }
  1311. case AV_CODEC_ID_ADPCM_DTK:
  1312. for (channel = 0; channel < avctx->channels; channel++) {
  1313. samples = samples_p[channel];
  1314. /* Read in every sample for this channel. */
  1315. for (i = 0; i < nb_samples / 28; i++) {
  1316. int byte, header;
  1317. if (channel)
  1318. bytestream2_skipu(&gb, 1);
  1319. header = bytestream2_get_byteu(&gb);
  1320. bytestream2_skipu(&gb, 3 - channel);
  1321. /* Decode 28 samples. */
  1322. for (n = 0; n < 28; n++) {
  1323. int32_t sampledat, prev;
  1324. switch (header >> 4) {
  1325. case 1:
  1326. prev = (c->status[channel].sample1 * 0x3c);
  1327. break;
  1328. case 2:
  1329. prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
  1330. break;
  1331. case 3:
  1332. prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
  1333. break;
  1334. default:
  1335. prev = 0;
  1336. }
  1337. prev = av_clip((prev + 0x20) >> 6, -0x200000, 0x1fffff);
  1338. byte = bytestream2_get_byteu(&gb);
  1339. if (!channel)
  1340. sampledat = sign_extend(byte, 4);
  1341. else
  1342. sampledat = sign_extend(byte >> 4, 4);
  1343. sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
  1344. *samples++ = av_clip_int16(sampledat >> 6);
  1345. c->status[channel].sample2 = c->status[channel].sample1;
  1346. c->status[channel].sample1 = sampledat;
  1347. }
  1348. }
  1349. if (!channel)
  1350. bytestream2_seek(&gb, 0, SEEK_SET);
  1351. }
  1352. break;
  1353. default:
  1354. return -1;
  1355. }
  1356. if (avpkt->size && bytestream2_tell(&gb) == 0) {
  1357. av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
  1358. return AVERROR_INVALIDDATA;
  1359. }
  1360. *got_frame_ptr = 1;
  1361. return bytestream2_tell(&gb);
  1362. }
  1363. static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
  1364. AV_SAMPLE_FMT_NONE };
  1365. static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16,
  1366. AV_SAMPLE_FMT_NONE };
  1367. static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
  1368. AV_SAMPLE_FMT_S16P,
  1369. AV_SAMPLE_FMT_NONE };
  1370. #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
  1371. AVCodec ff_ ## name_ ## _decoder = { \
  1372. .name = #name_, \
  1373. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1374. .type = AVMEDIA_TYPE_AUDIO, \
  1375. .id = id_, \
  1376. .priv_data_size = sizeof(ADPCMDecodeContext), \
  1377. .init = adpcm_decode_init, \
  1378. .decode = adpcm_decode_frame, \
  1379. .capabilities = CODEC_CAP_DR1, \
  1380. .sample_fmts = sample_fmts_, \
  1381. }
  1382. /* Note: Do not forget to add new entries to the Makefile as well. */
  1383. ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
  1384. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
  1385. ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
  1386. ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
  1387. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
  1388. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1389. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1390. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1391. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1392. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1393. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
  1394. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
  1395. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1396. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1397. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1398. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1399. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1400. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
  1401. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1402. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
  1403. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1404. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
  1405. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
  1406. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
  1407. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1408. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1409. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1410. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
  1411. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  1412. ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
  1413. ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");