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.

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