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.

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