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.

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