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.

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