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.

1289 lines
46KB

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