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.

1620 lines
62KB

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