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.

1301 lines
48KB

  1. /*
  2. * Copyright (c) 2001-2003 The ffmpeg Project
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avcodec.h"
  21. #include "get_bits.h"
  22. #include "put_bits.h"
  23. #include "bytestream.h"
  24. #include "adpcm.h"
  25. #include "adpcm_data.h"
  26. /**
  27. * @file
  28. * ADPCM decoders
  29. * First version by Francois Revol (revol@free.fr)
  30. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  31. * by Mike Melanson (melanson@pcisys.net)
  32. * CD-ROM XA ADPCM codec by BERO
  33. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  34. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  35. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  36. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  37. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  38. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  39. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  40. *
  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. AVFrame frame;
  82. ADPCMChannelStatus status[6];
  83. int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
  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 CODEC_ID_ADPCM_EA:
  92. min_channels = 2;
  93. break;
  94. case CODEC_ID_ADPCM_EA_R1:
  95. case CODEC_ID_ADPCM_EA_R2:
  96. case CODEC_ID_ADPCM_EA_R3:
  97. case CODEC_ID_ADPCM_EA_XAS:
  98. max_channels = 6;
  99. break;
  100. }
  101. if (avctx->channels < min_channels || avctx->channels > max_channels) {
  102. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  103. return AVERROR(EINVAL);
  104. }
  105. switch(avctx->codec->id) {
  106. case CODEC_ID_ADPCM_CT:
  107. c->status[0].step = c->status[1].step = 511;
  108. break;
  109. case CODEC_ID_ADPCM_IMA_WAV:
  110. if (avctx->bits_per_coded_sample != 4) {
  111. av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
  112. return -1;
  113. }
  114. break;
  115. case CODEC_ID_ADPCM_IMA_APC:
  116. if (avctx->extradata && avctx->extradata_size >= 8) {
  117. c->status[0].predictor = AV_RL32(avctx->extradata);
  118. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  119. }
  120. break;
  121. case CODEC_ID_ADPCM_IMA_WS:
  122. if (avctx->extradata && avctx->extradata_size >= 42)
  123. c->vqa_version = AV_RL16(avctx->extradata);
  124. break;
  125. default:
  126. break;
  127. }
  128. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  129. avcodec_get_frame_defaults(&c->frame);
  130. avctx->coded_frame = &c->frame;
  131. return 0;
  132. }
  133. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  134. {
  135. int step_index;
  136. int predictor;
  137. int sign, delta, diff, step;
  138. step = ff_adpcm_step_table[c->step_index];
  139. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  140. step_index = av_clip(step_index, 0, 88);
  141. sign = nibble & 8;
  142. delta = nibble & 7;
  143. /* perform direct multiplication instead of series of jumps proposed by
  144. * the reference ADPCM implementation since modern CPUs can do the mults
  145. * quickly enough */
  146. diff = ((2 * delta + 1) * step) >> shift;
  147. predictor = c->predictor;
  148. if (sign) predictor -= diff;
  149. else predictor += diff;
  150. c->predictor = av_clip_int16(predictor);
  151. c->step_index = step_index;
  152. return (short)c->predictor;
  153. }
  154. static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
  155. {
  156. int step_index;
  157. int predictor;
  158. int diff, step;
  159. step = ff_adpcm_step_table[c->step_index];
  160. step_index = c->step_index + ff_adpcm_index_table[nibble];
  161. step_index = av_clip(step_index, 0, 88);
  162. diff = step >> 3;
  163. if (nibble & 4) diff += step;
  164. if (nibble & 2) diff += step >> 1;
  165. if (nibble & 1) diff += step >> 2;
  166. if (nibble & 8)
  167. predictor = c->predictor - diff;
  168. else
  169. predictor = c->predictor + diff;
  170. c->predictor = av_clip_int16(predictor);
  171. c->step_index = step_index;
  172. return c->predictor;
  173. }
  174. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
  175. {
  176. int predictor;
  177. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  178. predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  179. c->sample2 = c->sample1;
  180. c->sample1 = av_clip_int16(predictor);
  181. c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
  182. if (c->idelta < 16) c->idelta = 16;
  183. return c->sample1;
  184. }
  185. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  186. {
  187. int sign, delta, diff;
  188. int new_step;
  189. sign = nibble & 8;
  190. delta = nibble & 7;
  191. /* perform direct multiplication instead of series of jumps proposed by
  192. * the reference ADPCM implementation since modern CPUs can do the mults
  193. * quickly enough */
  194. diff = ((2 * delta + 1) * c->step) >> 3;
  195. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  196. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  197. c->predictor = av_clip_int16(c->predictor);
  198. /* calculate new step and clamp it to range 511..32767 */
  199. new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
  200. c->step = av_clip(new_step, 511, 32767);
  201. return (short)c->predictor;
  202. }
  203. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  204. {
  205. int sign, delta, diff;
  206. sign = nibble & (1<<(size-1));
  207. delta = nibble & ((1<<(size-1))-1);
  208. diff = delta << (7 + c->step + shift);
  209. /* clamp result */
  210. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  211. /* calculate new step */
  212. if (delta >= (2*size - 3) && c->step < 3)
  213. c->step++;
  214. else if (delta == 0 && c->step > 0)
  215. c->step--;
  216. return (short) c->predictor;
  217. }
  218. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  219. {
  220. if(!c->step) {
  221. c->predictor = 0;
  222. c->step = 127;
  223. }
  224. c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
  225. c->predictor = av_clip_int16(c->predictor);
  226. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  227. c->step = av_clip(c->step, 127, 24567);
  228. return c->predictor;
  229. }
  230. static int xa_decode(AVCodecContext *avctx,
  231. short *out, const unsigned char *in,
  232. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  233. {
  234. int i, j;
  235. int shift,filter,f0,f1;
  236. int s_1,s_2;
  237. int d,s,t;
  238. for(i=0;i<4;i++) {
  239. shift = 12 - (in[4+i*2] & 15);
  240. filter = in[4+i*2] >> 4;
  241. if (filter > 4) {
  242. av_log(avctx, AV_LOG_ERROR,
  243. "Invalid XA-ADPCM filter %d (max. allowed is 4)\n",
  244. filter);
  245. return AVERROR_INVALIDDATA;
  246. }
  247. f0 = xa_adpcm_table[filter][0];
  248. f1 = xa_adpcm_table[filter][1];
  249. s_1 = left->sample1;
  250. s_2 = left->sample2;
  251. for(j=0;j<28;j++) {
  252. d = in[16+i+j*4];
  253. t = sign_extend(d, 4);
  254. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  255. s_2 = s_1;
  256. s_1 = av_clip_int16(s);
  257. *out = s_1;
  258. out += inc;
  259. }
  260. if (inc==2) { /* stereo */
  261. left->sample1 = s_1;
  262. left->sample2 = s_2;
  263. s_1 = right->sample1;
  264. s_2 = right->sample2;
  265. out = out + 1 - 28*2;
  266. }
  267. shift = 12 - (in[5+i*2] & 15);
  268. filter = in[5+i*2] >> 4;
  269. if (filter > 4) {
  270. av_log(avctx, AV_LOG_ERROR,
  271. "Invalid XA-ADPCM filter %d (max. allowed is 4)\n",
  272. filter);
  273. return AVERROR_INVALIDDATA;
  274. }
  275. f0 = xa_adpcm_table[filter][0];
  276. f1 = xa_adpcm_table[filter][1];
  277. for(j=0;j<28;j++) {
  278. d = in[16+i+j*4];
  279. t = sign_extend(d >> 4, 4);
  280. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  281. s_2 = s_1;
  282. s_1 = av_clip_int16(s);
  283. *out = s_1;
  284. out += inc;
  285. }
  286. if (inc==2) { /* stereo */
  287. right->sample1 = s_1;
  288. right->sample2 = s_2;
  289. out -= 1;
  290. } else {
  291. left->sample1 = s_1;
  292. left->sample2 = s_2;
  293. }
  294. }
  295. return 0;
  296. }
  297. /**
  298. * Get the number of samples that will be decoded from the packet.
  299. * In one case, this is actually the maximum number of samples possible to
  300. * decode with the given buf_size.
  301. *
  302. * @param[out] coded_samples set to the number of samples as coded in the
  303. * packet, or 0 if the codec does not encode the
  304. * number of samples in each frame.
  305. */
  306. static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
  307. GetByteContext *gb,
  308. int buf_size, int *coded_samples)
  309. {
  310. ADPCMDecodeContext *s = avctx->priv_data;
  311. int nb_samples = 0;
  312. int ch = avctx->channels;
  313. int has_coded_samples = 0;
  314. int header_size;
  315. *coded_samples = 0;
  316. switch (avctx->codec->id) {
  317. /* constant, only check buf_size */
  318. case CODEC_ID_ADPCM_EA_XAS:
  319. if (buf_size < 76 * ch)
  320. return 0;
  321. nb_samples = 128;
  322. break;
  323. case CODEC_ID_ADPCM_IMA_QT:
  324. if (buf_size < 34 * ch)
  325. return 0;
  326. nb_samples = 64;
  327. break;
  328. /* simple 4-bit adpcm */
  329. case CODEC_ID_ADPCM_CT:
  330. case CODEC_ID_ADPCM_IMA_APC:
  331. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  332. case CODEC_ID_ADPCM_IMA_WS:
  333. case CODEC_ID_ADPCM_YAMAHA:
  334. nb_samples = buf_size * 2 / ch;
  335. break;
  336. }
  337. if (nb_samples)
  338. return nb_samples;
  339. /* simple 4-bit adpcm, with header */
  340. header_size = 0;
  341. switch (avctx->codec->id) {
  342. case CODEC_ID_ADPCM_4XM:
  343. case CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
  344. case CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
  345. case CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4; break;
  346. }
  347. if (header_size > 0)
  348. return (buf_size - header_size) * 2 / ch;
  349. /* more complex formats */
  350. switch (avctx->codec->id) {
  351. case CODEC_ID_ADPCM_EA:
  352. has_coded_samples = 1;
  353. *coded_samples = bytestream2_get_le32(gb);
  354. *coded_samples -= *coded_samples % 28;
  355. nb_samples = (buf_size - 12) / 30 * 28;
  356. break;
  357. case CODEC_ID_ADPCM_IMA_EA_EACS:
  358. has_coded_samples = 1;
  359. *coded_samples = bytestream2_get_le32(gb);
  360. nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
  361. break;
  362. case CODEC_ID_ADPCM_EA_MAXIS_XA:
  363. nb_samples = (buf_size - ch) / ch * 2;
  364. break;
  365. case CODEC_ID_ADPCM_EA_R1:
  366. case CODEC_ID_ADPCM_EA_R2:
  367. case CODEC_ID_ADPCM_EA_R3:
  368. /* maximum number of samples */
  369. /* has internal offsets and a per-frame switch to signal raw 16-bit */
  370. has_coded_samples = 1;
  371. switch (avctx->codec->id) {
  372. case CODEC_ID_ADPCM_EA_R1:
  373. header_size = 4 + 9 * ch;
  374. *coded_samples = bytestream2_get_le32(gb);
  375. break;
  376. case CODEC_ID_ADPCM_EA_R2:
  377. header_size = 4 + 5 * ch;
  378. *coded_samples = bytestream2_get_le32(gb);
  379. break;
  380. case CODEC_ID_ADPCM_EA_R3:
  381. header_size = 4 + 5 * ch;
  382. *coded_samples = bytestream2_get_be32(gb);
  383. break;
  384. }
  385. *coded_samples -= *coded_samples % 28;
  386. nb_samples = (buf_size - header_size) * 2 / ch;
  387. nb_samples -= nb_samples % 28;
  388. break;
  389. case CODEC_ID_ADPCM_IMA_DK3:
  390. if (avctx->block_align > 0)
  391. buf_size = FFMIN(buf_size, avctx->block_align);
  392. nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
  393. break;
  394. case CODEC_ID_ADPCM_IMA_DK4:
  395. if (avctx->block_align > 0)
  396. buf_size = FFMIN(buf_size, avctx->block_align);
  397. nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
  398. break;
  399. case CODEC_ID_ADPCM_IMA_WAV:
  400. if (avctx->block_align > 0)
  401. buf_size = FFMIN(buf_size, avctx->block_align);
  402. nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
  403. break;
  404. case CODEC_ID_ADPCM_MS:
  405. if (avctx->block_align > 0)
  406. buf_size = FFMIN(buf_size, avctx->block_align);
  407. nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
  408. break;
  409. case CODEC_ID_ADPCM_SBPRO_2:
  410. case CODEC_ID_ADPCM_SBPRO_3:
  411. case CODEC_ID_ADPCM_SBPRO_4:
  412. {
  413. int samples_per_byte;
  414. switch (avctx->codec->id) {
  415. case CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
  416. case CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
  417. case CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
  418. }
  419. if (!s->status[0].step_index) {
  420. nb_samples++;
  421. buf_size -= ch;
  422. }
  423. nb_samples += buf_size * samples_per_byte / ch;
  424. break;
  425. }
  426. case CODEC_ID_ADPCM_SWF:
  427. {
  428. int buf_bits = buf_size * 8 - 2;
  429. int nbits = (buf[0] >> 6) + 2;
  430. int block_hdr_size = 22 * ch;
  431. int block_size = block_hdr_size + nbits * ch * 4095;
  432. int nblocks = buf_bits / block_size;
  433. int bits_left = buf_bits - nblocks * block_size;
  434. nb_samples = nblocks * 4096;
  435. if (bits_left >= block_hdr_size)
  436. nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
  437. break;
  438. }
  439. case CODEC_ID_ADPCM_THP:
  440. has_coded_samples = 1;
  441. if (buf_size < 8)
  442. return 0;
  443. *coded_samples = AV_RB32(&buf[4]);
  444. *coded_samples -= *coded_samples % 14;
  445. nb_samples = (buf_size - 80) / (8 * ch) * 14;
  446. break;
  447. case CODEC_ID_ADPCM_XA:
  448. nb_samples = (buf_size / 128) * 224 / ch;
  449. break;
  450. }
  451. /* validate coded sample count */
  452. if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
  453. return AVERROR_INVALIDDATA;
  454. return nb_samples;
  455. }
  456. static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
  457. int *got_frame_ptr, AVPacket *avpkt)
  458. {
  459. const uint8_t *buf = avpkt->data;
  460. int buf_size = avpkt->size;
  461. ADPCMDecodeContext *c = avctx->priv_data;
  462. ADPCMChannelStatus *cs;
  463. int n, m, channel, i;
  464. short *samples;
  465. const uint8_t *src;
  466. int st; /* stereo */
  467. int count1, count2;
  468. int nb_samples, coded_samples, ret;
  469. GetByteContext gb;
  470. bytestream2_init(&gb, buf, buf_size);
  471. nb_samples = get_nb_samples(avctx, buf, &gb, buf_size, &coded_samples);
  472. if (nb_samples <= 0) {
  473. av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
  474. return AVERROR_INVALIDDATA;
  475. }
  476. /* get output buffer */
  477. c->frame.nb_samples = nb_samples;
  478. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  479. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  480. return ret;
  481. }
  482. samples = (short *)c->frame.data[0];
  483. /* use coded_samples when applicable */
  484. /* it is always <= nb_samples, so the output buffer will be large enough */
  485. if (coded_samples) {
  486. if (coded_samples != nb_samples)
  487. av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
  488. c->frame.nb_samples = nb_samples = coded_samples;
  489. }
  490. src = buf;
  491. st = avctx->channels == 2 ? 1 : 0;
  492. switch(avctx->codec->id) {
  493. case CODEC_ID_ADPCM_IMA_QT:
  494. /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
  495. Channel data is interleaved per-chunk. */
  496. for (channel = 0; channel < avctx->channels; channel++) {
  497. int predictor;
  498. int step_index;
  499. cs = &(c->status[channel]);
  500. /* (pppppp) (piiiiiii) */
  501. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  502. predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  503. step_index = predictor & 0x7F;
  504. predictor &= ~0x7F;
  505. if (cs->step_index == step_index) {
  506. int diff = predictor - cs->predictor;
  507. if (diff < 0)
  508. diff = - diff;
  509. if (diff > 0x7f)
  510. goto update;
  511. } else {
  512. update:
  513. cs->step_index = step_index;
  514. cs->predictor = predictor;
  515. }
  516. if (cs->step_index > 88u){
  517. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  518. channel, cs->step_index);
  519. return AVERROR_INVALIDDATA;
  520. }
  521. samples = (short *)c->frame.data[0] + channel;
  522. for (m = 0; m < 32; m++) {
  523. int byte = bytestream2_get_byteu(&gb);
  524. *samples = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
  525. samples += avctx->channels;
  526. *samples = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
  527. samples += avctx->channels;
  528. }
  529. }
  530. break;
  531. case CODEC_ID_ADPCM_IMA_WAV:
  532. for(i=0; i<avctx->channels; i++){
  533. cs = &(c->status[i]);
  534. cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
  535. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  536. if (cs->step_index > 88u){
  537. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  538. i, cs->step_index);
  539. return AVERROR_INVALIDDATA;
  540. }
  541. }
  542. for (n = (nb_samples - 1) / 8; n > 0; n--) {
  543. for (i = 0; i < avctx->channels; i++) {
  544. cs = &c->status[i];
  545. for (m = 0; m < 4; m++) {
  546. int v = bytestream2_get_byteu(&gb);
  547. *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  548. samples += avctx->channels;
  549. *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  550. samples += avctx->channels;
  551. }
  552. samples -= 8 * avctx->channels - 1;
  553. }
  554. samples += 7 * avctx->channels;
  555. }
  556. break;
  557. case CODEC_ID_ADPCM_4XM:
  558. for (i = 0; i < avctx->channels; i++)
  559. c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  560. for (i = 0; i < avctx->channels; i++) {
  561. c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  562. if (c->status[i].step_index > 88u) {
  563. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  564. i, c->status[i].step_index);
  565. return AVERROR_INVALIDDATA;
  566. }
  567. }
  568. for (i = 0; i < avctx->channels; i++) {
  569. samples = (short *)c->frame.data[0] + i;
  570. cs = &c->status[i];
  571. for (n = nb_samples >> 1; n > 0; n--) {
  572. int v = bytestream2_get_byteu(&gb);
  573. *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
  574. samples += avctx->channels;
  575. *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
  576. samples += avctx->channels;
  577. }
  578. }
  579. break;
  580. case CODEC_ID_ADPCM_MS:
  581. {
  582. int block_predictor;
  583. block_predictor = bytestream2_get_byteu(&gb);
  584. if (block_predictor > 6) {
  585. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
  586. block_predictor);
  587. return AVERROR_INVALIDDATA;
  588. }
  589. c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  590. c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  591. if (st) {
  592. block_predictor = bytestream2_get_byteu(&gb);
  593. if (block_predictor > 6) {
  594. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
  595. block_predictor);
  596. return AVERROR_INVALIDDATA;
  597. }
  598. c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  599. c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  600. }
  601. c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  602. if (st){
  603. c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  604. }
  605. c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  606. if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  607. c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  608. if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  609. *samples++ = c->status[0].sample2;
  610. if (st) *samples++ = c->status[1].sample2;
  611. *samples++ = c->status[0].sample1;
  612. if (st) *samples++ = c->status[1].sample1;
  613. for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
  614. int byte = bytestream2_get_byteu(&gb);
  615. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
  616. *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
  617. }
  618. break;
  619. }
  620. case CODEC_ID_ADPCM_IMA_DK4:
  621. for (channel = 0; channel < avctx->channels; channel++) {
  622. cs = &c->status[channel];
  623. cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
  624. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  625. if (cs->step_index > 88u){
  626. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  627. channel, cs->step_index);
  628. return AVERROR_INVALIDDATA;
  629. }
  630. }
  631. for (n = nb_samples >> (1 - st); n > 0; n--) {
  632. int v = bytestream2_get_byteu(&gb);
  633. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
  634. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  635. }
  636. break;
  637. case CODEC_ID_ADPCM_IMA_DK3:
  638. {
  639. int last_byte = 0;
  640. int nibble;
  641. int decode_top_nibble_next = 0;
  642. int diff_channel;
  643. const int16_t *samples_end = samples + avctx->channels * nb_samples;
  644. bytestream2_skipu(&gb, 10);
  645. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  646. c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  647. c->status[0].step_index = bytestream2_get_byteu(&gb);
  648. c->status[1].step_index = bytestream2_get_byteu(&gb);
  649. if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
  650. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
  651. c->status[0].step_index, c->status[1].step_index);
  652. return AVERROR_INVALIDDATA;
  653. }
  654. /* sign extend the predictors */
  655. diff_channel = c->status[1].predictor;
  656. /* DK3 ADPCM support macro */
  657. #define DK3_GET_NEXT_NIBBLE() \
  658. if (decode_top_nibble_next) { \
  659. nibble = last_byte >> 4; \
  660. decode_top_nibble_next = 0; \
  661. } else { \
  662. last_byte = bytestream2_get_byteu(&gb); \
  663. nibble = last_byte & 0x0F; \
  664. decode_top_nibble_next = 1; \
  665. }
  666. while (samples < samples_end) {
  667. /* for this algorithm, c->status[0] is the sum channel and
  668. * c->status[1] is the diff channel */
  669. /* process the first predictor of the sum channel */
  670. DK3_GET_NEXT_NIBBLE();
  671. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  672. /* process the diff channel predictor */
  673. DK3_GET_NEXT_NIBBLE();
  674. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  675. /* process the first pair of stereo PCM samples */
  676. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  677. *samples++ = c->status[0].predictor + c->status[1].predictor;
  678. *samples++ = c->status[0].predictor - c->status[1].predictor;
  679. /* process the second predictor of the sum channel */
  680. DK3_GET_NEXT_NIBBLE();
  681. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  682. /* process the second pair of stereo PCM samples */
  683. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  684. *samples++ = c->status[0].predictor + c->status[1].predictor;
  685. *samples++ = c->status[0].predictor - c->status[1].predictor;
  686. }
  687. break;
  688. }
  689. case CODEC_ID_ADPCM_IMA_ISS:
  690. for (channel = 0; channel < avctx->channels; channel++) {
  691. cs = &c->status[channel];
  692. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  693. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  694. if (cs->step_index > 88u){
  695. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  696. channel, cs->step_index);
  697. return AVERROR_INVALIDDATA;
  698. }
  699. }
  700. for (n = nb_samples >> (1 - st); n > 0; n--) {
  701. int v1, v2;
  702. int v = bytestream2_get_byteu(&gb);
  703. /* nibbles are swapped for mono */
  704. if (st) {
  705. v1 = v >> 4;
  706. v2 = v & 0x0F;
  707. } else {
  708. v2 = v >> 4;
  709. v1 = v & 0x0F;
  710. }
  711. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
  712. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
  713. }
  714. break;
  715. case CODEC_ID_ADPCM_IMA_APC:
  716. while (bytestream2_get_bytes_left(&gb) > 0) {
  717. int v = bytestream2_get_byteu(&gb);
  718. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
  719. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  720. }
  721. break;
  722. case CODEC_ID_ADPCM_IMA_WS:
  723. if (c->vqa_version == 3) {
  724. for (channel = 0; channel < avctx->channels; channel++) {
  725. int16_t *smp = samples + channel;
  726. for (n = nb_samples / 2; n > 0; n--) {
  727. int v = bytestream2_get_byteu(&gb);
  728. *smp = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  729. smp += avctx->channels;
  730. *smp = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  731. smp += avctx->channels;
  732. }
  733. }
  734. } else {
  735. for (n = nb_samples / 2; n > 0; n--) {
  736. for (channel = 0; channel < avctx->channels; channel++) {
  737. int v = bytestream2_get_byteu(&gb);
  738. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  739. samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  740. }
  741. samples += avctx->channels;
  742. }
  743. }
  744. bytestream2_seek(&gb, 0, SEEK_END);
  745. break;
  746. case CODEC_ID_ADPCM_XA:
  747. while (bytestream2_get_bytes_left(&gb) >= 128) {
  748. if ((ret = xa_decode(avctx, samples, buf + bytestream2_tell(&gb), &c->status[0],
  749. &c->status[1], avctx->channels)) < 0)
  750. return ret;
  751. bytestream2_skipu(&gb, 128);
  752. samples += 28 * 8;
  753. }
  754. break;
  755. case CODEC_ID_ADPCM_IMA_EA_EACS:
  756. for (i=0; i<=st; i++) {
  757. c->status[i].step_index = bytestream2_get_le32u(&gb);
  758. if (c->status[i].step_index > 88u) {
  759. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  760. i, c->status[i].step_index);
  761. return AVERROR_INVALIDDATA;
  762. }
  763. }
  764. for (i=0; i<=st; i++)
  765. c->status[i].predictor = bytestream2_get_le32u(&gb);
  766. for (n = nb_samples >> (1 - st); n > 0; n--) {
  767. int byte = bytestream2_get_byteu(&gb);
  768. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
  769. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
  770. }
  771. break;
  772. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  773. for (n = nb_samples >> (1 - st); n > 0; n--) {
  774. int byte = bytestream2_get_byteu(&gb);
  775. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
  776. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
  777. }
  778. break;
  779. case CODEC_ID_ADPCM_EA:
  780. {
  781. int previous_left_sample, previous_right_sample;
  782. int current_left_sample, current_right_sample;
  783. int next_left_sample, next_right_sample;
  784. int coeff1l, coeff2l, coeff1r, coeff2r;
  785. int shift_left, shift_right;
  786. /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
  787. each coding 28 stereo samples. */
  788. current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  789. previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  790. current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  791. previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  792. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  793. int byte = bytestream2_get_byteu(&gb);
  794. coeff1l = ea_adpcm_table[ byte >> 4 ];
  795. coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
  796. coeff1r = ea_adpcm_table[ byte & 0x0F];
  797. coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
  798. byte = bytestream2_get_byteu(&gb);
  799. shift_left = 20 - (byte >> 4);
  800. shift_right = 20 - (byte & 0x0F);
  801. for (count2 = 0; count2 < 28; count2++) {
  802. byte = bytestream2_get_byteu(&gb);
  803. next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
  804. next_right_sample = sign_extend(byte, 4) << shift_right;
  805. next_left_sample = (next_left_sample +
  806. (current_left_sample * coeff1l) +
  807. (previous_left_sample * coeff2l) + 0x80) >> 8;
  808. next_right_sample = (next_right_sample +
  809. (current_right_sample * coeff1r) +
  810. (previous_right_sample * coeff2r) + 0x80) >> 8;
  811. previous_left_sample = current_left_sample;
  812. current_left_sample = av_clip_int16(next_left_sample);
  813. previous_right_sample = current_right_sample;
  814. current_right_sample = av_clip_int16(next_right_sample);
  815. *samples++ = current_left_sample;
  816. *samples++ = current_right_sample;
  817. }
  818. }
  819. bytestream2_skip(&gb, 2); // Skip terminating 0x0000
  820. break;
  821. }
  822. case CODEC_ID_ADPCM_EA_MAXIS_XA:
  823. {
  824. int coeff[2][2], shift[2];
  825. for(channel = 0; channel < avctx->channels; channel++) {
  826. int byte = bytestream2_get_byteu(&gb);
  827. for (i=0; i<2; i++)
  828. coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
  829. shift[channel] = 20 - (byte & 0x0F);
  830. }
  831. for (count1 = 0; count1 < nb_samples / 2; count1++) {
  832. int byte[2];
  833. byte[0] = bytestream2_get_byteu(&gb);
  834. if (st) byte[1] = bytestream2_get_byteu(&gb);
  835. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  836. for(channel = 0; channel < avctx->channels; channel++) {
  837. int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
  838. sample = (sample +
  839. c->status[channel].sample1 * coeff[channel][0] +
  840. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  841. c->status[channel].sample2 = c->status[channel].sample1;
  842. c->status[channel].sample1 = av_clip_int16(sample);
  843. *samples++ = c->status[channel].sample1;
  844. }
  845. }
  846. }
  847. bytestream2_seek(&gb, 0, SEEK_END);
  848. break;
  849. }
  850. case CODEC_ID_ADPCM_EA_R1:
  851. case CODEC_ID_ADPCM_EA_R2:
  852. case CODEC_ID_ADPCM_EA_R3: {
  853. /* channel numbering
  854. 2chan: 0=fl, 1=fr
  855. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  856. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  857. const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
  858. int previous_sample, current_sample, next_sample;
  859. int coeff1, coeff2;
  860. int shift;
  861. unsigned int channel;
  862. uint16_t *samplesC;
  863. int count = 0;
  864. int offsets[6];
  865. for (channel=0; channel<avctx->channels; channel++)
  866. offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
  867. bytestream2_get_le32(&gb)) +
  868. (avctx->channels + 1) * 4;
  869. for (channel=0; channel<avctx->channels; channel++) {
  870. bytestream2_seek(&gb, offsets[channel], SEEK_SET);
  871. samplesC = samples + channel;
  872. if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
  873. current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  874. previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  875. } else {
  876. current_sample = c->status[channel].predictor;
  877. previous_sample = c->status[channel].prev_sample;
  878. }
  879. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  880. int byte = bytestream2_get_byte(&gb);
  881. if (byte == 0xEE) { /* only seen in R2 and R3 */
  882. current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  883. previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  884. for (count2=0; count2<28; count2++) {
  885. *samplesC = sign_extend(bytestream2_get_be16(&gb), 16);
  886. samplesC += avctx->channels;
  887. }
  888. } else {
  889. coeff1 = ea_adpcm_table[ byte >> 4 ];
  890. coeff2 = ea_adpcm_table[(byte >> 4) + 4];
  891. shift = 20 - (byte & 0x0F);
  892. for (count2=0; count2<28; count2++) {
  893. if (count2 & 1)
  894. next_sample = sign_extend(byte, 4) << shift;
  895. else {
  896. byte = bytestream2_get_byte(&gb);
  897. next_sample = sign_extend(byte >> 4, 4) << shift;
  898. }
  899. next_sample += (current_sample * coeff1) +
  900. (previous_sample * coeff2);
  901. next_sample = av_clip_int16(next_sample >> 8);
  902. previous_sample = current_sample;
  903. current_sample = next_sample;
  904. *samplesC = current_sample;
  905. samplesC += avctx->channels;
  906. }
  907. }
  908. }
  909. if (!count) {
  910. count = count1;
  911. } else if (count != count1) {
  912. av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
  913. count = FFMAX(count, count1);
  914. }
  915. if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
  916. c->status[channel].predictor = current_sample;
  917. c->status[channel].prev_sample = previous_sample;
  918. }
  919. }
  920. c->frame.nb_samples = count * 28;
  921. bytestream2_seek(&gb, 0, SEEK_END);
  922. break;
  923. }
  924. case CODEC_ID_ADPCM_EA_XAS:
  925. for (channel=0; channel<avctx->channels; channel++) {
  926. int coeff[2][4], shift[4];
  927. short *s2, *s = &samples[channel];
  928. for (n=0; n<4; n++, s+=32*avctx->channels) {
  929. int val = sign_extend(bytestream2_get_le16u(&gb), 16);
  930. for (i=0; i<2; i++)
  931. coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
  932. s[0] = val & ~0x0F;
  933. val = sign_extend(bytestream2_get_le16u(&gb), 16);
  934. shift[n] = 20 - (val & 0x0F);
  935. s[avctx->channels] = val & ~0x0F;
  936. }
  937. for (m=2; m<32; m+=2) {
  938. s = &samples[m*avctx->channels + channel];
  939. for (n=0; n<4; n++, s+=32*avctx->channels) {
  940. int byte = bytestream2_get_byteu(&gb);
  941. for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
  942. int level = sign_extend(byte >> (4 - i), 4) << shift[n];
  943. int pred = s2[-1*avctx->channels] * coeff[0][n]
  944. + s2[-2*avctx->channels] * coeff[1][n];
  945. s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
  946. }
  947. }
  948. }
  949. }
  950. break;
  951. case CODEC_ID_ADPCM_IMA_AMV:
  952. case CODEC_ID_ADPCM_IMA_SMJPEG:
  953. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) {
  954. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  955. c->status[0].step_index = bytestream2_get_le16u(&gb);
  956. bytestream2_skipu(&gb, 4);
  957. } else {
  958. c->status[0].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  959. c->status[0].step_index = bytestream2_get_byteu(&gb);
  960. bytestream2_skipu(&gb, 1);
  961. }
  962. if (c->status[0].step_index > 88u) {
  963. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  964. c->status[0].step_index);
  965. return AVERROR_INVALIDDATA;
  966. }
  967. for (n = nb_samples >> (1 - st); n > 0; n--) {
  968. int hi, lo, v = bytestream2_get_byteu(&gb);
  969. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) {
  970. hi = v & 0x0F;
  971. lo = v >> 4;
  972. } else {
  973. lo = v & 0x0F;
  974. hi = v >> 4;
  975. }
  976. *samples++ = adpcm_ima_expand_nibble(&c->status[0], lo, 3);
  977. *samples++ = adpcm_ima_expand_nibble(&c->status[0], hi, 3);
  978. }
  979. break;
  980. case CODEC_ID_ADPCM_CT:
  981. for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
  982. uint8_t v = *src;
  983. *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
  984. *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
  985. }
  986. break;
  987. case CODEC_ID_ADPCM_SBPRO_4:
  988. case CODEC_ID_ADPCM_SBPRO_3:
  989. case CODEC_ID_ADPCM_SBPRO_2:
  990. if (!c->status[0].step_index) {
  991. /* the first byte is a raw sample */
  992. *samples++ = 128 * (*src++ - 0x80);
  993. if (st)
  994. *samples++ = 128 * (*src++ - 0x80);
  995. c->status[0].step_index = 1;
  996. nb_samples--;
  997. }
  998. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  999. for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
  1000. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1001. src[0] >> 4, 4, 0);
  1002. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1003. src[0] & 0x0F, 4, 0);
  1004. }
  1005. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1006. for (n = nb_samples / 3; n > 0; n--, src++) {
  1007. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1008. src[0] >> 5 , 3, 0);
  1009. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1010. (src[0] >> 2) & 0x07, 3, 0);
  1011. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1012. src[0] & 0x03, 2, 0);
  1013. }
  1014. } else {
  1015. for (n = nb_samples >> (2 - st); n > 0; n--, src++) {
  1016. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1017. src[0] >> 6 , 2, 2);
  1018. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1019. (src[0] >> 4) & 0x03, 2, 2);
  1020. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1021. (src[0] >> 2) & 0x03, 2, 2);
  1022. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1023. src[0] & 0x03, 2, 2);
  1024. }
  1025. }
  1026. break;
  1027. case CODEC_ID_ADPCM_SWF:
  1028. {
  1029. GetBitContext gb;
  1030. const int *table;
  1031. int k0, signmask, nb_bits, count;
  1032. int size = buf_size*8;
  1033. init_get_bits(&gb, buf, size);
  1034. //read bits & initial values
  1035. nb_bits = get_bits(&gb, 2)+2;
  1036. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  1037. table = swf_index_tables[nb_bits-2];
  1038. k0 = 1 << (nb_bits-2);
  1039. signmask = 1 << (nb_bits-1);
  1040. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  1041. for (i = 0; i < avctx->channels; i++) {
  1042. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1043. c->status[i].step_index = get_bits(&gb, 6);
  1044. }
  1045. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  1046. int i;
  1047. for (i = 0; i < avctx->channels; i++) {
  1048. // similar to IMA adpcm
  1049. int delta = get_bits(&gb, nb_bits);
  1050. int step = ff_adpcm_step_table[c->status[i].step_index];
  1051. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  1052. int k = k0;
  1053. do {
  1054. if (delta & k)
  1055. vpdiff += step;
  1056. step >>= 1;
  1057. k >>= 1;
  1058. } while(k);
  1059. vpdiff += step;
  1060. if (delta & signmask)
  1061. c->status[i].predictor -= vpdiff;
  1062. else
  1063. c->status[i].predictor += vpdiff;
  1064. c->status[i].step_index += table[delta & (~signmask)];
  1065. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  1066. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  1067. *samples++ = c->status[i].predictor;
  1068. }
  1069. }
  1070. }
  1071. src += buf_size;
  1072. break;
  1073. }
  1074. case CODEC_ID_ADPCM_YAMAHA:
  1075. for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
  1076. uint8_t v = *src;
  1077. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
  1078. *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
  1079. }
  1080. break;
  1081. case CODEC_ID_ADPCM_THP:
  1082. {
  1083. int table[2][16];
  1084. int prev[2][2];
  1085. int ch;
  1086. src += 4; // skip channel size
  1087. src += 4; // skip number of samples (already read)
  1088. for (i = 0; i < 32; i++)
  1089. table[0][i] = (int16_t)bytestream_get_be16(&src);
  1090. /* Initialize the previous sample. */
  1091. for (i = 0; i < 4; i++)
  1092. prev[0][i] = (int16_t)bytestream_get_be16(&src);
  1093. for (ch = 0; ch <= st; ch++) {
  1094. samples = (short *)c->frame.data[0] + ch;
  1095. /* Read in every sample for this channel. */
  1096. for (i = 0; i < nb_samples / 14; i++) {
  1097. int index = (*src >> 4) & 7;
  1098. unsigned int exp = *src++ & 15;
  1099. int factor1 = table[ch][index * 2];
  1100. int factor2 = table[ch][index * 2 + 1];
  1101. /* Decode 14 samples. */
  1102. for (n = 0; n < 14; n++) {
  1103. int32_t sampledat;
  1104. if(n&1) sampledat = sign_extend(*src++, 4);
  1105. else sampledat = sign_extend(*src >> 4, 4);
  1106. sampledat = ((prev[ch][0]*factor1
  1107. + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
  1108. *samples = av_clip_int16(sampledat);
  1109. prev[ch][1] = prev[ch][0];
  1110. prev[ch][0] = *samples++;
  1111. /* In case of stereo, skip one sample, this sample
  1112. is for the other channel. */
  1113. samples += st;
  1114. }
  1115. }
  1116. }
  1117. break;
  1118. }
  1119. default:
  1120. return -1;
  1121. }
  1122. *got_frame_ptr = 1;
  1123. *(AVFrame *)data = c->frame;
  1124. return src == buf ? bytestream2_tell(&gb) : src - buf;
  1125. }
  1126. #define ADPCM_DECODER(id_, name_, long_name_) \
  1127. AVCodec ff_ ## name_ ## _decoder = { \
  1128. .name = #name_, \
  1129. .type = AVMEDIA_TYPE_AUDIO, \
  1130. .id = id_, \
  1131. .priv_data_size = sizeof(ADPCMDecodeContext), \
  1132. .init = adpcm_decode_init, \
  1133. .decode = adpcm_decode_frame, \
  1134. .capabilities = CODEC_CAP_DR1, \
  1135. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1136. }
  1137. /* Note: Do not forget to add new entries to the Makefile as well. */
  1138. ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
  1139. ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
  1140. ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
  1141. ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1142. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1143. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1144. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1145. ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1146. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
  1147. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_APC, adpcm_ima_apc, "ADPCM IMA CRYO APC");
  1148. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1149. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1150. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1151. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1152. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1153. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1154. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1155. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
  1156. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
  1157. ADPCM_DECODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
  1158. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1159. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1160. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1161. ADPCM_DECODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
  1162. ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  1163. ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
  1164. ADPCM_DECODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");