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.

1644 lines
63KB

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