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.

1673 lines
64KB

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