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.

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