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.

1277 lines
45KB

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