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.

1101 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. 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 (i = 0; i < avctx->channels; i++) {
  385. cs = &c->status[i];
  386. for (m = 0; m < 4; m++) {
  387. uint8_t v = *src++;
  388. *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  389. samples += avctx->channels;
  390. *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  391. samples += avctx->channels;
  392. }
  393. samples -= 8 * avctx->channels - 1;
  394. }
  395. samples += 7 * avctx->channels;
  396. }
  397. break;
  398. case CODEC_ID_ADPCM_4XM:
  399. for (i = 0; i < avctx->channels; i++)
  400. c->status[i].predictor= (int16_t)bytestream_get_le16(&src);
  401. for (i = 0; i < avctx->channels; i++) {
  402. c->status[i].step_index= (int16_t)bytestream_get_le16(&src);
  403. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  404. }
  405. m= (buf_size - (src - buf))>>st;
  406. for (i = 0; i < avctx->channels; i++) {
  407. samples = (short*)data + i;
  408. cs = &c->status[i];
  409. for (n = 0; n < m; n++) {
  410. uint8_t v = *src++;
  411. *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
  412. samples += avctx->channels;
  413. *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
  414. samples += avctx->channels;
  415. }
  416. }
  417. samples -= (avctx->channels - 1);
  418. break;
  419. case CODEC_ID_ADPCM_MS:
  420. {
  421. int block_predictor;
  422. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  423. buf_size = avctx->block_align;
  424. n = buf_size - 7 * avctx->channels;
  425. if (n < 0)
  426. return -1;
  427. block_predictor = av_clip(*src++, 0, 6);
  428. c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  429. c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  430. if (st) {
  431. block_predictor = av_clip(*src++, 0, 6);
  432. c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  433. c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  434. }
  435. c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
  436. if (st){
  437. c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
  438. }
  439. c->status[0].sample1 = bytestream_get_le16(&src);
  440. if (st) c->status[1].sample1 = bytestream_get_le16(&src);
  441. c->status[0].sample2 = bytestream_get_le16(&src);
  442. if (st) c->status[1].sample2 = bytestream_get_le16(&src);
  443. *samples++ = c->status[0].sample2;
  444. if (st) *samples++ = c->status[1].sample2;
  445. *samples++ = c->status[0].sample1;
  446. if (st) *samples++ = c->status[1].sample1;
  447. for(;n>0;n--) {
  448. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
  449. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  450. src ++;
  451. }
  452. break;
  453. }
  454. case CODEC_ID_ADPCM_IMA_DK4:
  455. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  456. buf_size = avctx->block_align;
  457. n = buf_size - 4 * avctx->channels;
  458. if (n < 0) {
  459. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  460. return AVERROR(EINVAL);
  461. }
  462. for (channel = 0; channel < avctx->channels; channel++) {
  463. cs = &c->status[channel];
  464. cs->predictor = (int16_t)bytestream_get_le16(&src);
  465. cs->step_index = *src++;
  466. src++;
  467. *samples++ = cs->predictor;
  468. }
  469. while (n-- > 0) {
  470. uint8_t v = *src++;
  471. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
  472. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  473. }
  474. break;
  475. case CODEC_ID_ADPCM_IMA_DK3:
  476. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  477. buf_size = avctx->block_align;
  478. if(buf_size + 16 > (samples_end - samples)*3/8)
  479. return -1;
  480. c->status[0].predictor = (int16_t)AV_RL16(src + 10);
  481. c->status[1].predictor = (int16_t)AV_RL16(src + 12);
  482. c->status[0].step_index = src[14];
  483. c->status[1].step_index = src[15];
  484. /* sign extend the predictors */
  485. src += 16;
  486. diff_channel = c->status[1].predictor;
  487. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  488. * the buffer is consumed */
  489. while (1) {
  490. /* for this algorithm, c->status[0] is the sum channel and
  491. * c->status[1] is the diff channel */
  492. /* process the first predictor of the sum channel */
  493. DK3_GET_NEXT_NIBBLE();
  494. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  495. /* process the diff channel predictor */
  496. DK3_GET_NEXT_NIBBLE();
  497. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  498. /* process the first 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. /* process the second predictor of the sum channel */
  503. DK3_GET_NEXT_NIBBLE();
  504. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  505. /* process the second pair of stereo PCM samples */
  506. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  507. *samples++ = c->status[0].predictor + c->status[1].predictor;
  508. *samples++ = c->status[0].predictor - c->status[1].predictor;
  509. }
  510. break;
  511. case CODEC_ID_ADPCM_IMA_ISS:
  512. c->status[0].predictor = (int16_t)AV_RL16(src + 0);
  513. c->status[0].step_index = src[2];
  514. src += 4;
  515. if(st) {
  516. c->status[1].predictor = (int16_t)AV_RL16(src + 0);
  517. c->status[1].step_index = src[2];
  518. src += 4;
  519. }
  520. while (src < buf + buf_size) {
  521. uint8_t v1, v2;
  522. uint8_t v = *src++;
  523. /* nibbles are swapped for mono */
  524. if (st) {
  525. v1 = v >> 4;
  526. v2 = v & 0x0F;
  527. } else {
  528. v2 = v >> 4;
  529. v1 = v & 0x0F;
  530. }
  531. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
  532. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
  533. }
  534. break;
  535. case CODEC_ID_ADPCM_IMA_WS:
  536. while (src < buf + buf_size) {
  537. uint8_t v = *src++;
  538. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
  539. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  540. }
  541. break;
  542. case CODEC_ID_ADPCM_XA:
  543. while (buf_size >= 128) {
  544. xa_decode(samples, src, &c->status[0], &c->status[1],
  545. avctx->channels);
  546. src += 128;
  547. samples += 28 * 8;
  548. buf_size -= 128;
  549. }
  550. break;
  551. case CODEC_ID_ADPCM_IMA_EA_EACS:
  552. samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
  553. if (samples_in_chunk > buf_size-4-(8<<st)) {
  554. src += buf_size - 4;
  555. break;
  556. }
  557. for (i=0; i<=st; i++)
  558. c->status[i].step_index = bytestream_get_le32(&src);
  559. for (i=0; i<=st; i++)
  560. c->status[i].predictor = bytestream_get_le32(&src);
  561. for (; samples_in_chunk; samples_in_chunk--, src++) {
  562. *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
  563. *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
  564. }
  565. break;
  566. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  567. for (; src < buf+buf_size; src++) {
  568. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
  569. *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
  570. }
  571. break;
  572. case CODEC_ID_ADPCM_EA:
  573. /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
  574. each coding 28 stereo samples. */
  575. if (buf_size < 12) {
  576. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  577. return AVERROR(EINVAL);
  578. }
  579. samples_in_chunk = AV_RL32(src);
  580. if (samples_in_chunk / 28 > (buf_size - 12) / 30) {
  581. av_log(avctx, AV_LOG_ERROR, "invalid frame\n");
  582. return AVERROR(EINVAL);
  583. }
  584. src += 4;
  585. current_left_sample = (int16_t)bytestream_get_le16(&src);
  586. previous_left_sample = (int16_t)bytestream_get_le16(&src);
  587. current_right_sample = (int16_t)bytestream_get_le16(&src);
  588. previous_right_sample = (int16_t)bytestream_get_le16(&src);
  589. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  590. coeff1l = ea_adpcm_table[ *src >> 4 ];
  591. coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
  592. coeff1r = ea_adpcm_table[*src & 0x0F];
  593. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  594. src++;
  595. shift_left = (*src >> 4 ) + 8;
  596. shift_right = (*src & 0x0F) + 8;
  597. src++;
  598. for (count2 = 0; count2 < 28; count2++) {
  599. next_left_sample = (int32_t)((*src & 0xF0) << 24) >> shift_left;
  600. next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
  601. src++;
  602. next_left_sample = (next_left_sample +
  603. (current_left_sample * coeff1l) +
  604. (previous_left_sample * coeff2l) + 0x80) >> 8;
  605. next_right_sample = (next_right_sample +
  606. (current_right_sample * coeff1r) +
  607. (previous_right_sample * coeff2r) + 0x80) >> 8;
  608. previous_left_sample = current_left_sample;
  609. current_left_sample = av_clip_int16(next_left_sample);
  610. previous_right_sample = current_right_sample;
  611. current_right_sample = av_clip_int16(next_right_sample);
  612. *samples++ = (unsigned short)current_left_sample;
  613. *samples++ = (unsigned short)current_right_sample;
  614. }
  615. }
  616. if (src - buf == buf_size - 2)
  617. src += 2; // Skip terminating 0x0000
  618. break;
  619. case CODEC_ID_ADPCM_EA_MAXIS_XA:
  620. for(channel = 0; channel < avctx->channels; channel++) {
  621. for (i=0; i<2; i++)
  622. coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
  623. shift[channel] = (*src & 0x0F) + 8;
  624. src++;
  625. }
  626. for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
  627. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  628. for(channel = 0; channel < avctx->channels; channel++) {
  629. int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
  630. sample = (sample +
  631. c->status[channel].sample1 * coeff[channel][0] +
  632. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  633. c->status[channel].sample2 = c->status[channel].sample1;
  634. c->status[channel].sample1 = av_clip_int16(sample);
  635. *samples++ = c->status[channel].sample1;
  636. }
  637. }
  638. src+=avctx->channels;
  639. }
  640. break;
  641. case CODEC_ID_ADPCM_EA_R1:
  642. case CODEC_ID_ADPCM_EA_R2:
  643. case CODEC_ID_ADPCM_EA_R3: {
  644. /* channel numbering
  645. 2chan: 0=fl, 1=fr
  646. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  647. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  648. const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
  649. int32_t previous_sample, current_sample, next_sample;
  650. int32_t coeff1, coeff2;
  651. uint8_t shift;
  652. unsigned int channel;
  653. uint16_t *samplesC;
  654. const uint8_t *srcC;
  655. const uint8_t *src_end = buf + buf_size;
  656. samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
  657. : bytestream_get_le32(&src)) / 28;
  658. if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
  659. 28*samples_in_chunk*avctx->channels > samples_end-samples) {
  660. src += buf_size - 4;
  661. break;
  662. }
  663. for (channel=0; channel<avctx->channels; channel++) {
  664. int32_t offset = (big_endian ? bytestream_get_be32(&src)
  665. : bytestream_get_le32(&src))
  666. + (avctx->channels-channel-1) * 4;
  667. if ((offset < 0) || (offset >= src_end - src - 4)) break;
  668. srcC = src + offset;
  669. samplesC = samples + channel;
  670. if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
  671. current_sample = (int16_t)bytestream_get_le16(&srcC);
  672. previous_sample = (int16_t)bytestream_get_le16(&srcC);
  673. } else {
  674. current_sample = c->status[channel].predictor;
  675. previous_sample = c->status[channel].prev_sample;
  676. }
  677. for (count1=0; count1<samples_in_chunk; count1++) {
  678. if (*srcC == 0xEE) { /* only seen in R2 and R3 */
  679. srcC++;
  680. if (srcC > src_end - 30*2) break;
  681. current_sample = (int16_t)bytestream_get_be16(&srcC);
  682. previous_sample = (int16_t)bytestream_get_be16(&srcC);
  683. for (count2=0; count2<28; count2++) {
  684. *samplesC = (int16_t)bytestream_get_be16(&srcC);
  685. samplesC += avctx->channels;
  686. }
  687. } else {
  688. coeff1 = ea_adpcm_table[ *srcC>>4 ];
  689. coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
  690. shift = (*srcC++ & 0x0F) + 8;
  691. if (srcC > src_end - 14) break;
  692. for (count2=0; count2<28; count2++) {
  693. if (count2 & 1)
  694. next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
  695. else
  696. next_sample = (int32_t)((*srcC & 0xF0) << 24) >> shift;
  697. next_sample += (current_sample * coeff1) +
  698. (previous_sample * coeff2);
  699. next_sample = av_clip_int16(next_sample >> 8);
  700. previous_sample = current_sample;
  701. current_sample = next_sample;
  702. *samplesC = current_sample;
  703. samplesC += avctx->channels;
  704. }
  705. }
  706. }
  707. if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
  708. c->status[channel].predictor = current_sample;
  709. c->status[channel].prev_sample = previous_sample;
  710. }
  711. }
  712. src = src + buf_size - (4 + 4*avctx->channels);
  713. samples += 28 * samples_in_chunk * avctx->channels;
  714. break;
  715. }
  716. case CODEC_ID_ADPCM_EA_XAS:
  717. if (samples_end-samples < 32*4*avctx->channels
  718. || buf_size < (4+15)*4*avctx->channels) {
  719. src += buf_size;
  720. break;
  721. }
  722. for (channel=0; channel<avctx->channels; channel++) {
  723. int coeff[2][4], shift[4];
  724. short *s2, *s = &samples[channel];
  725. for (n=0; n<4; n++, s+=32*avctx->channels) {
  726. for (i=0; i<2; i++)
  727. coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
  728. shift[n] = (src[2]&0x0F) + 8;
  729. for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
  730. s2[0] = (src[0]&0xF0) + (src[1]<<8);
  731. }
  732. for (m=2; m<32; m+=2) {
  733. s = &samples[m*avctx->channels + channel];
  734. for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
  735. for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
  736. int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
  737. int pred = s2[-1*avctx->channels] * coeff[0][n]
  738. + s2[-2*avctx->channels] * coeff[1][n];
  739. s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
  740. }
  741. }
  742. }
  743. }
  744. samples += 32*4*avctx->channels;
  745. break;
  746. case CODEC_ID_ADPCM_IMA_AMV:
  747. case CODEC_ID_ADPCM_IMA_SMJPEG:
  748. c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
  749. c->status[0].step_index = bytestream_get_le16(&src);
  750. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  751. src+=4;
  752. while (src < buf + buf_size) {
  753. char hi, lo;
  754. lo = *src & 0x0F;
  755. hi = *src >> 4;
  756. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  757. FFSWAP(char, hi, lo);
  758. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  759. lo, 3);
  760. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  761. hi, 3);
  762. src++;
  763. }
  764. break;
  765. case CODEC_ID_ADPCM_CT:
  766. while (src < buf + buf_size) {
  767. uint8_t v = *src++;
  768. *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
  769. *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
  770. }
  771. break;
  772. case CODEC_ID_ADPCM_SBPRO_4:
  773. case CODEC_ID_ADPCM_SBPRO_3:
  774. case CODEC_ID_ADPCM_SBPRO_2:
  775. if (!c->status[0].step_index) {
  776. /* the first byte is a raw sample */
  777. *samples++ = 128 * (*src++ - 0x80);
  778. if (st)
  779. *samples++ = 128 * (*src++ - 0x80);
  780. c->status[0].step_index = 1;
  781. }
  782. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  783. while (src < buf + buf_size) {
  784. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  785. src[0] >> 4, 4, 0);
  786. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  787. src[0] & 0x0F, 4, 0);
  788. src++;
  789. }
  790. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  791. while (src < buf + buf_size && samples + 2 < samples_end) {
  792. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  793. src[0] >> 5 , 3, 0);
  794. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  795. (src[0] >> 2) & 0x07, 3, 0);
  796. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  797. src[0] & 0x03, 2, 0);
  798. src++;
  799. }
  800. } else {
  801. while (src < buf + buf_size && samples + 3 < samples_end) {
  802. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  803. src[0] >> 6 , 2, 2);
  804. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  805. (src[0] >> 4) & 0x03, 2, 2);
  806. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  807. (src[0] >> 2) & 0x03, 2, 2);
  808. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  809. src[0] & 0x03, 2, 2);
  810. src++;
  811. }
  812. }
  813. break;
  814. case CODEC_ID_ADPCM_SWF:
  815. {
  816. GetBitContext gb;
  817. const int *table;
  818. int k0, signmask, nb_bits, count;
  819. int size = buf_size*8;
  820. init_get_bits(&gb, buf, size);
  821. //read bits & initial values
  822. nb_bits = get_bits(&gb, 2)+2;
  823. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  824. table = swf_index_tables[nb_bits-2];
  825. k0 = 1 << (nb_bits-2);
  826. signmask = 1 << (nb_bits-1);
  827. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  828. for (i = 0; i < avctx->channels; i++) {
  829. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  830. c->status[i].step_index = get_bits(&gb, 6);
  831. }
  832. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  833. int i;
  834. for (i = 0; i < avctx->channels; i++) {
  835. // similar to IMA adpcm
  836. int delta = get_bits(&gb, nb_bits);
  837. int step = ff_adpcm_step_table[c->status[i].step_index];
  838. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  839. int k = k0;
  840. do {
  841. if (delta & k)
  842. vpdiff += step;
  843. step >>= 1;
  844. k >>= 1;
  845. } while(k);
  846. vpdiff += step;
  847. if (delta & signmask)
  848. c->status[i].predictor -= vpdiff;
  849. else
  850. c->status[i].predictor += vpdiff;
  851. c->status[i].step_index += table[delta & (~signmask)];
  852. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  853. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  854. *samples++ = c->status[i].predictor;
  855. if (samples >= samples_end) {
  856. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  857. return -1;
  858. }
  859. }
  860. }
  861. }
  862. src += buf_size;
  863. break;
  864. }
  865. case CODEC_ID_ADPCM_YAMAHA:
  866. while (src < buf + buf_size) {
  867. uint8_t v = *src++;
  868. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
  869. *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
  870. }
  871. break;
  872. case CODEC_ID_ADPCM_THP:
  873. {
  874. int table[2][16];
  875. unsigned int samplecnt;
  876. int prev[2][2];
  877. int ch;
  878. if (buf_size < 80) {
  879. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  880. return -1;
  881. }
  882. src+=4;
  883. samplecnt = bytestream_get_be32(&src);
  884. for (i = 0; i < 32; i++)
  885. table[0][i] = (int16_t)bytestream_get_be16(&src);
  886. /* Initialize the previous sample. */
  887. for (i = 0; i < 4; i++)
  888. prev[0][i] = (int16_t)bytestream_get_be16(&src);
  889. if (samplecnt >= (samples_end - samples) / (st + 1)) {
  890. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  891. return -1;
  892. }
  893. for (ch = 0; ch <= st; ch++) {
  894. samples = (unsigned short *) data + ch;
  895. /* Read in every sample for this channel. */
  896. for (i = 0; i < samplecnt / 14; i++) {
  897. int index = (*src >> 4) & 7;
  898. unsigned int exp = 28 - (*src++ & 15);
  899. int factor1 = table[ch][index * 2];
  900. int factor2 = table[ch][index * 2 + 1];
  901. /* Decode 14 samples. */
  902. for (n = 0; n < 14; n++) {
  903. int32_t sampledat;
  904. if(n&1) sampledat= *src++ <<28;
  905. else sampledat= (*src&0xF0)<<24;
  906. sampledat = ((prev[ch][0]*factor1
  907. + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
  908. *samples = av_clip_int16(sampledat);
  909. prev[ch][1] = prev[ch][0];
  910. prev[ch][0] = *samples++;
  911. /* In case of stereo, skip one sample, this sample
  912. is for the other channel. */
  913. samples += st;
  914. }
  915. }
  916. }
  917. /* In the previous loop, in case stereo is used, samples is
  918. increased exactly one time too often. */
  919. samples -= st;
  920. break;
  921. }
  922. default:
  923. return -1;
  924. }
  925. *data_size = (uint8_t *)samples - (uint8_t *)data;
  926. return src - buf;
  927. }
  928. #define ADPCM_DECODER(id_, name_, long_name_) \
  929. AVCodec ff_ ## name_ ## _decoder = { \
  930. .name = #name_, \
  931. .type = AVMEDIA_TYPE_AUDIO, \
  932. .id = id_, \
  933. .priv_data_size = sizeof(ADPCMDecodeContext), \
  934. .init = adpcm_decode_init, \
  935. .decode = adpcm_decode_frame, \
  936. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  937. }
  938. /* Note: Do not forget to add new entries to the Makefile as well. */
  939. ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
  940. ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
  941. ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
  942. ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  943. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  944. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  945. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  946. ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  947. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
  948. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  949. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  950. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  951. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  952. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  953. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
  954. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  955. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
  956. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
  957. ADPCM_DECODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
  958. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  959. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  960. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  961. ADPCM_DECODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
  962. ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  963. ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
  964. ADPCM_DECODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");