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.

1774 lines
68KB

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