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.

1094 lines
39KB

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