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.

1255 lines
45KB

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