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.

1096 lines
40KB

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