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.

1112 lines
40KB

  1. /*
  2. * Copyright (c) 2001-2003 The ffmpeg Project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avcodec.h"
  21. #include "get_bits.h"
  22. #include "put_bits.h"
  23. #include "bytestream.h"
  24. #include "adpcm.h"
  25. #include "adpcm_data.h"
  26. /**
  27. * @file
  28. * ADPCM decoders
  29. * First version by Francois Revol (revol@free.fr)
  30. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  31. * by Mike Melanson (melanson@pcisys.net)
  32. * CD-ROM XA ADPCM codec by BERO
  33. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  34. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  35. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  36. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  37. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  38. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  39. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  40. *
  41. * Features and limitations:
  42. *
  43. * Reference documents:
  44. * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
  45. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
  46. * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
  47. * http://openquicktime.sourceforge.net/
  48. * XAnim sources (xa_codec.c) http://xanim.polter.net/
  49. * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
  50. * SoX source code http://sox.sourceforge.net/
  51. *
  52. * CD-ROM XA:
  53. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
  54. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
  55. * readstr http://www.geocities.co.jp/Playtown/2004/
  56. */
  57. /* These are for CD-ROM XA ADPCM */
  58. static const int xa_adpcm_table[5][2] = {
  59. { 0, 0 },
  60. { 60, 0 },
  61. { 115, -52 },
  62. { 98, -55 },
  63. { 122, -60 }
  64. };
  65. static const int ea_adpcm_table[] = {
  66. 0, 240, 460, 392,
  67. 0, 0, -208, -220,
  68. 0, 1, 3, 4,
  69. 7, 8, 10, 11,
  70. 0, -1, -3, -4
  71. };
  72. // padded to zero where table size is less then 16
  73. static const int swf_index_tables[4][16] = {
  74. /*2*/ { -1, 2 },
  75. /*3*/ { -1, -1, 2, 4 },
  76. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  77. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  78. };
  79. /* end of tables */
  80. typedef struct ADPCMDecodeContext {
  81. 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. uint32_t samples_in_chunk;
  299. int count1, count2;
  300. if (!buf_size)
  301. return 0;
  302. //should protect all 4bit ADPCM variants
  303. //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
  304. //
  305. if(*data_size/4 < buf_size + 8)
  306. return -1;
  307. samples = data;
  308. samples_end= samples + *data_size/2;
  309. *data_size= 0;
  310. src = buf;
  311. st = avctx->channels == 2 ? 1 : 0;
  312. switch(avctx->codec->id) {
  313. case CODEC_ID_ADPCM_IMA_QT:
  314. /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
  315. Channel data is interleaved per-chunk. */
  316. if (buf_size / 34 < avctx->channels) {
  317. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  318. return AVERROR(EINVAL);
  319. }
  320. for (channel = 0; channel < avctx->channels; channel++) {
  321. int16_t predictor;
  322. int step_index;
  323. cs = &(c->status[channel]);
  324. /* (pppppp) (piiiiiii) */
  325. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  326. predictor = AV_RB16(src);
  327. step_index = predictor & 0x7F;
  328. predictor &= 0xFF80;
  329. src += 2;
  330. if (cs->step_index == step_index) {
  331. int diff = (int)predictor - cs->predictor;
  332. if (diff < 0)
  333. diff = - diff;
  334. if (diff > 0x7f)
  335. goto update;
  336. } else {
  337. update:
  338. cs->step_index = step_index;
  339. cs->predictor = predictor;
  340. }
  341. if (cs->step_index > 88){
  342. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  343. cs->step_index = 88;
  344. }
  345. samples = (short*)data + channel;
  346. for (m = 0; m < 32; m++) {
  347. *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3);
  348. samples += avctx->channels;
  349. *samples = adpcm_ima_qt_expand_nibble(cs, src[0] >> 4 , 3);
  350. samples += avctx->channels;
  351. src ++;
  352. }
  353. }
  354. if (st)
  355. samples--;
  356. break;
  357. case CODEC_ID_ADPCM_IMA_WAV:
  358. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  359. buf_size = avctx->block_align;
  360. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  361. for(i=0; i<avctx->channels; i++){
  362. cs = &(c->status[i]);
  363. cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
  364. cs->step_index = *src++;
  365. if (cs->step_index > 88){
  366. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  367. cs->step_index = 88;
  368. }
  369. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  370. }
  371. while(src < buf + buf_size){
  372. for (i = 0; i < avctx->channels; i++) {
  373. cs = &c->status[i];
  374. for (m = 0; m < 4; m++) {
  375. uint8_t v = *src++;
  376. *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  377. samples += avctx->channels;
  378. *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  379. samples += avctx->channels;
  380. }
  381. samples -= 8 * avctx->channels - 1;
  382. }
  383. samples += 7 * avctx->channels;
  384. }
  385. break;
  386. case CODEC_ID_ADPCM_4XM:
  387. for (i = 0; i < avctx->channels; i++)
  388. c->status[i].predictor= (int16_t)bytestream_get_le16(&src);
  389. for (i = 0; i < avctx->channels; i++) {
  390. c->status[i].step_index= (int16_t)bytestream_get_le16(&src);
  391. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  392. }
  393. m= (buf_size - (src - buf))>>st;
  394. for (i = 0; i < avctx->channels; i++) {
  395. samples = (short*)data + i;
  396. cs = &c->status[i];
  397. for (n = 0; n < m; n++) {
  398. uint8_t v = *src++;
  399. *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
  400. samples += avctx->channels;
  401. *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
  402. samples += avctx->channels;
  403. }
  404. }
  405. samples -= (avctx->channels - 1);
  406. break;
  407. case CODEC_ID_ADPCM_MS:
  408. {
  409. int block_predictor;
  410. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  411. buf_size = avctx->block_align;
  412. n = buf_size - 7 * avctx->channels;
  413. if (n < 0)
  414. return -1;
  415. block_predictor = av_clip(*src++, 0, 6);
  416. c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  417. c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  418. if (st) {
  419. block_predictor = av_clip(*src++, 0, 6);
  420. c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  421. c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  422. }
  423. c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
  424. if (st){
  425. c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
  426. }
  427. c->status[0].sample1 = bytestream_get_le16(&src);
  428. if (st) c->status[1].sample1 = bytestream_get_le16(&src);
  429. c->status[0].sample2 = bytestream_get_le16(&src);
  430. if (st) c->status[1].sample2 = bytestream_get_le16(&src);
  431. *samples++ = c->status[0].sample2;
  432. if (st) *samples++ = c->status[1].sample2;
  433. *samples++ = c->status[0].sample1;
  434. if (st) *samples++ = c->status[1].sample1;
  435. for(;n>0;n--) {
  436. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
  437. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  438. src ++;
  439. }
  440. break;
  441. }
  442. case CODEC_ID_ADPCM_IMA_DK4:
  443. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  444. buf_size = avctx->block_align;
  445. n = buf_size - 4 * avctx->channels;
  446. if (n < 0) {
  447. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  448. return AVERROR(EINVAL);
  449. }
  450. for (channel = 0; channel < avctx->channels; channel++) {
  451. cs = &c->status[channel];
  452. cs->predictor = (int16_t)bytestream_get_le16(&src);
  453. cs->step_index = *src++;
  454. src++;
  455. *samples++ = cs->predictor;
  456. }
  457. while (n-- > 0) {
  458. uint8_t v = *src++;
  459. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
  460. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  461. }
  462. break;
  463. case CODEC_ID_ADPCM_IMA_DK3:
  464. {
  465. unsigned char last_byte = 0;
  466. unsigned char nibble;
  467. int decode_top_nibble_next = 0;
  468. int diff_channel;
  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. }
  505. case CODEC_ID_ADPCM_IMA_ISS:
  506. n = buf_size - 4 * avctx->channels;
  507. if (n < 0) {
  508. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  509. return AVERROR(EINVAL);
  510. }
  511. for (channel = 0; channel < avctx->channels; channel++) {
  512. cs = &c->status[channel];
  513. cs->predictor = (int16_t)bytestream_get_le16(&src);
  514. cs->step_index = *src++;
  515. src++;
  516. }
  517. while (n-- > 0) {
  518. uint8_t v1, v2;
  519. uint8_t v = *src++;
  520. /* nibbles are swapped for mono */
  521. if (st) {
  522. v1 = v >> 4;
  523. v2 = v & 0x0F;
  524. } else {
  525. v2 = v >> 4;
  526. v1 = v & 0x0F;
  527. }
  528. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
  529. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
  530. }
  531. break;
  532. case CODEC_ID_ADPCM_IMA_WS:
  533. while (src < buf + buf_size) {
  534. uint8_t v = *src++;
  535. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
  536. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  537. }
  538. break;
  539. case CODEC_ID_ADPCM_XA:
  540. while (buf_size >= 128) {
  541. xa_decode(samples, src, &c->status[0], &c->status[1],
  542. avctx->channels);
  543. src += 128;
  544. samples += 28 * 8;
  545. buf_size -= 128;
  546. }
  547. break;
  548. case CODEC_ID_ADPCM_IMA_EA_EACS: {
  549. unsigned header_size = 4 + (8<<st);
  550. samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
  551. if (buf_size < header_size || samples_in_chunk > buf_size - header_size) {
  552. src += buf_size - 4;
  553. break;
  554. }
  555. for (i=0; i<=st; i++)
  556. c->status[i].step_index = bytestream_get_le32(&src);
  557. for (i=0; i<=st; i++)
  558. c->status[i].predictor = bytestream_get_le32(&src);
  559. for (; samples_in_chunk; samples_in_chunk--, src++) {
  560. *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
  561. *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
  562. }
  563. break;
  564. }
  565. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  566. for (; src < buf+buf_size; src++) {
  567. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
  568. *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
  569. }
  570. break;
  571. case CODEC_ID_ADPCM_EA:
  572. {
  573. int32_t previous_left_sample, previous_right_sample;
  574. int32_t current_left_sample, current_right_sample;
  575. int32_t next_left_sample, next_right_sample;
  576. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  577. uint8_t shift_left, shift_right;
  578. /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
  579. each coding 28 stereo samples. */
  580. if (buf_size < 12) {
  581. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  582. return AVERROR(EINVAL);
  583. }
  584. samples_in_chunk = AV_RL32(src);
  585. if (samples_in_chunk / 28 > (buf_size - 12) / 30) {
  586. av_log(avctx, AV_LOG_ERROR, "invalid frame\n");
  587. return AVERROR(EINVAL);
  588. }
  589. src += 4;
  590. current_left_sample = (int16_t)bytestream_get_le16(&src);
  591. previous_left_sample = (int16_t)bytestream_get_le16(&src);
  592. current_right_sample = (int16_t)bytestream_get_le16(&src);
  593. previous_right_sample = (int16_t)bytestream_get_le16(&src);
  594. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  595. coeff1l = ea_adpcm_table[ *src >> 4 ];
  596. coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
  597. coeff1r = ea_adpcm_table[*src & 0x0F];
  598. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  599. src++;
  600. shift_left = (*src >> 4 ) + 8;
  601. shift_right = (*src & 0x0F) + 8;
  602. src++;
  603. for (count2 = 0; count2 < 28; count2++) {
  604. next_left_sample = (int32_t)((*src & 0xF0) << 24) >> shift_left;
  605. next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
  606. src++;
  607. next_left_sample = (next_left_sample +
  608. (current_left_sample * coeff1l) +
  609. (previous_left_sample * coeff2l) + 0x80) >> 8;
  610. next_right_sample = (next_right_sample +
  611. (current_right_sample * coeff1r) +
  612. (previous_right_sample * coeff2r) + 0x80) >> 8;
  613. previous_left_sample = current_left_sample;
  614. current_left_sample = av_clip_int16(next_left_sample);
  615. previous_right_sample = current_right_sample;
  616. current_right_sample = av_clip_int16(next_right_sample);
  617. *samples++ = (unsigned short)current_left_sample;
  618. *samples++ = (unsigned short)current_right_sample;
  619. }
  620. }
  621. if (src - buf == buf_size - 2)
  622. src += 2; // Skip terminating 0x0000
  623. break;
  624. }
  625. case CODEC_ID_ADPCM_EA_MAXIS_XA:
  626. {
  627. int coeff[2][2], shift[2];
  628. for(channel = 0; channel < avctx->channels; channel++) {
  629. for (i=0; i<2; i++)
  630. coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
  631. shift[channel] = (*src & 0x0F) + 8;
  632. src++;
  633. }
  634. for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
  635. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  636. for(channel = 0; channel < avctx->channels; channel++) {
  637. int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
  638. sample = (sample +
  639. c->status[channel].sample1 * coeff[channel][0] +
  640. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  641. c->status[channel].sample2 = c->status[channel].sample1;
  642. c->status[channel].sample1 = av_clip_int16(sample);
  643. *samples++ = c->status[channel].sample1;
  644. }
  645. }
  646. src+=avctx->channels;
  647. }
  648. break;
  649. }
  650. case CODEC_ID_ADPCM_EA_R1:
  651. case CODEC_ID_ADPCM_EA_R2:
  652. case CODEC_ID_ADPCM_EA_R3: {
  653. /* channel numbering
  654. 2chan: 0=fl, 1=fr
  655. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  656. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  657. const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
  658. int32_t previous_sample, current_sample, next_sample;
  659. int32_t coeff1, coeff2;
  660. uint8_t shift;
  661. unsigned int channel;
  662. uint16_t *samplesC;
  663. const uint8_t *srcC;
  664. const uint8_t *src_end = buf + buf_size;
  665. samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
  666. : bytestream_get_le32(&src)) / 28;
  667. if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
  668. 28*samples_in_chunk*avctx->channels > samples_end-samples) {
  669. src += buf_size - 4;
  670. break;
  671. }
  672. for (channel=0; channel<avctx->channels; channel++) {
  673. int32_t offset = (big_endian ? bytestream_get_be32(&src)
  674. : bytestream_get_le32(&src))
  675. + (avctx->channels-channel-1) * 4;
  676. if ((offset < 0) || (offset >= src_end - src - 4)) break;
  677. srcC = src + offset;
  678. samplesC = samples + channel;
  679. if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
  680. current_sample = (int16_t)bytestream_get_le16(&srcC);
  681. previous_sample = (int16_t)bytestream_get_le16(&srcC);
  682. } else {
  683. current_sample = c->status[channel].predictor;
  684. previous_sample = c->status[channel].prev_sample;
  685. }
  686. for (count1=0; count1<samples_in_chunk; count1++) {
  687. if (*srcC == 0xEE) { /* only seen in R2 and R3 */
  688. srcC++;
  689. if (srcC > src_end - 30*2) break;
  690. current_sample = (int16_t)bytestream_get_be16(&srcC);
  691. previous_sample = (int16_t)bytestream_get_be16(&srcC);
  692. for (count2=0; count2<28; count2++) {
  693. *samplesC = (int16_t)bytestream_get_be16(&srcC);
  694. samplesC += avctx->channels;
  695. }
  696. } else {
  697. coeff1 = ea_adpcm_table[ *srcC>>4 ];
  698. coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
  699. shift = (*srcC++ & 0x0F) + 8;
  700. if (srcC > src_end - 14) break;
  701. for (count2=0; count2<28; count2++) {
  702. if (count2 & 1)
  703. next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
  704. else
  705. next_sample = (int32_t)((*srcC & 0xF0) << 24) >> shift;
  706. next_sample += (current_sample * coeff1) +
  707. (previous_sample * coeff2);
  708. next_sample = av_clip_int16(next_sample >> 8);
  709. previous_sample = current_sample;
  710. current_sample = next_sample;
  711. *samplesC = current_sample;
  712. samplesC += avctx->channels;
  713. }
  714. }
  715. }
  716. if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
  717. c->status[channel].predictor = current_sample;
  718. c->status[channel].prev_sample = previous_sample;
  719. }
  720. }
  721. src = src + buf_size - (4 + 4*avctx->channels);
  722. samples += 28 * samples_in_chunk * avctx->channels;
  723. break;
  724. }
  725. case CODEC_ID_ADPCM_EA_XAS:
  726. if (samples_end-samples < 32*4*avctx->channels
  727. || buf_size < (4+15)*4*avctx->channels) {
  728. src += buf_size;
  729. break;
  730. }
  731. for (channel=0; channel<avctx->channels; channel++) {
  732. int coeff[2][4], shift[4];
  733. short *s2, *s = &samples[channel];
  734. for (n=0; n<4; n++, s+=32*avctx->channels) {
  735. for (i=0; i<2; i++)
  736. coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
  737. shift[n] = (src[2]&0x0F) + 8;
  738. for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
  739. s2[0] = (src[0]&0xF0) + (src[1]<<8);
  740. }
  741. for (m=2; m<32; m+=2) {
  742. s = &samples[m*avctx->channels + channel];
  743. for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
  744. for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
  745. int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
  746. int pred = s2[-1*avctx->channels] * coeff[0][n]
  747. + s2[-2*avctx->channels] * coeff[1][n];
  748. s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
  749. }
  750. }
  751. }
  752. }
  753. samples += 32*4*avctx->channels;
  754. break;
  755. case CODEC_ID_ADPCM_IMA_AMV:
  756. case CODEC_ID_ADPCM_IMA_SMJPEG:
  757. c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
  758. c->status[0].step_index = bytestream_get_le16(&src);
  759. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  760. src+=4;
  761. while (src < buf + buf_size) {
  762. char hi, lo;
  763. lo = *src & 0x0F;
  764. hi = *src >> 4;
  765. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  766. FFSWAP(char, hi, lo);
  767. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  768. lo, 3);
  769. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  770. hi, 3);
  771. src++;
  772. }
  773. break;
  774. case CODEC_ID_ADPCM_CT:
  775. while (src < buf + buf_size) {
  776. uint8_t v = *src++;
  777. *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
  778. *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
  779. }
  780. break;
  781. case CODEC_ID_ADPCM_SBPRO_4:
  782. case CODEC_ID_ADPCM_SBPRO_3:
  783. case CODEC_ID_ADPCM_SBPRO_2:
  784. if (!c->status[0].step_index) {
  785. /* the first byte is a raw sample */
  786. *samples++ = 128 * (*src++ - 0x80);
  787. if (st)
  788. *samples++ = 128 * (*src++ - 0x80);
  789. c->status[0].step_index = 1;
  790. }
  791. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  792. while (src < buf + buf_size) {
  793. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  794. src[0] >> 4, 4, 0);
  795. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  796. src[0] & 0x0F, 4, 0);
  797. src++;
  798. }
  799. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  800. while (src < buf + buf_size && samples + 2 < samples_end) {
  801. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  802. src[0] >> 5 , 3, 0);
  803. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  804. (src[0] >> 2) & 0x07, 3, 0);
  805. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  806. src[0] & 0x03, 2, 0);
  807. src++;
  808. }
  809. } else {
  810. while (src < buf + buf_size && samples + 3 < samples_end) {
  811. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  812. src[0] >> 6 , 2, 2);
  813. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  814. (src[0] >> 4) & 0x03, 2, 2);
  815. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  816. (src[0] >> 2) & 0x03, 2, 2);
  817. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  818. src[0] & 0x03, 2, 2);
  819. src++;
  820. }
  821. }
  822. break;
  823. case CODEC_ID_ADPCM_SWF:
  824. {
  825. GetBitContext gb;
  826. const int *table;
  827. int k0, signmask, nb_bits, count;
  828. int size = buf_size*8;
  829. init_get_bits(&gb, buf, size);
  830. //read bits & initial values
  831. nb_bits = get_bits(&gb, 2)+2;
  832. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  833. table = swf_index_tables[nb_bits-2];
  834. k0 = 1 << (nb_bits-2);
  835. signmask = 1 << (nb_bits-1);
  836. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  837. for (i = 0; i < avctx->channels; i++) {
  838. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  839. c->status[i].step_index = get_bits(&gb, 6);
  840. }
  841. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  842. int i;
  843. for (i = 0; i < avctx->channels; i++) {
  844. // similar to IMA adpcm
  845. int delta = get_bits(&gb, nb_bits);
  846. int step = ff_adpcm_step_table[c->status[i].step_index];
  847. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  848. int k = k0;
  849. do {
  850. if (delta & k)
  851. vpdiff += step;
  852. step >>= 1;
  853. k >>= 1;
  854. } while(k);
  855. vpdiff += step;
  856. if (delta & signmask)
  857. c->status[i].predictor -= vpdiff;
  858. else
  859. c->status[i].predictor += vpdiff;
  860. c->status[i].step_index += table[delta & (~signmask)];
  861. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  862. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  863. *samples++ = c->status[i].predictor;
  864. if (samples >= samples_end) {
  865. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  866. return -1;
  867. }
  868. }
  869. }
  870. }
  871. src += buf_size;
  872. break;
  873. }
  874. case CODEC_ID_ADPCM_YAMAHA:
  875. while (src < buf + buf_size) {
  876. uint8_t v = *src++;
  877. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
  878. *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
  879. }
  880. break;
  881. case CODEC_ID_ADPCM_THP:
  882. {
  883. int table[2][16];
  884. unsigned int samplecnt;
  885. int prev[2][2];
  886. int ch;
  887. if (buf_size < 80) {
  888. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  889. return -1;
  890. }
  891. src+=4;
  892. samplecnt = bytestream_get_be32(&src);
  893. for (i = 0; i < 32; i++)
  894. table[0][i] = (int16_t)bytestream_get_be16(&src);
  895. /* Initialize the previous sample. */
  896. for (i = 0; i < 4; i++)
  897. prev[0][i] = (int16_t)bytestream_get_be16(&src);
  898. if (samplecnt >= (samples_end - samples) / (st + 1)) {
  899. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  900. return -1;
  901. }
  902. for (ch = 0; ch <= st; ch++) {
  903. samples = (unsigned short *) data + ch;
  904. /* Read in every sample for this channel. */
  905. for (i = 0; i < samplecnt / 14; i++) {
  906. int index = (*src >> 4) & 7;
  907. unsigned int exp = 28 - (*src++ & 15);
  908. int factor1 = table[ch][index * 2];
  909. int factor2 = table[ch][index * 2 + 1];
  910. /* Decode 14 samples. */
  911. for (n = 0; n < 14; n++) {
  912. int32_t sampledat;
  913. if(n&1) sampledat= *src++ <<28;
  914. else sampledat= (*src&0xF0)<<24;
  915. sampledat = ((prev[ch][0]*factor1
  916. + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
  917. *samples = av_clip_int16(sampledat);
  918. prev[ch][1] = prev[ch][0];
  919. prev[ch][0] = *samples++;
  920. /* In case of stereo, skip one sample, this sample
  921. is for the other channel. */
  922. samples += st;
  923. }
  924. }
  925. }
  926. /* In the previous loop, in case stereo is used, samples is
  927. increased exactly one time too often. */
  928. samples -= st;
  929. break;
  930. }
  931. default:
  932. return -1;
  933. }
  934. *data_size = (uint8_t *)samples - (uint8_t *)data;
  935. return src - buf;
  936. }
  937. #define ADPCM_DECODER(id_, name_, long_name_) \
  938. AVCodec ff_ ## name_ ## _decoder = { \
  939. .name = #name_, \
  940. .type = AVMEDIA_TYPE_AUDIO, \
  941. .id = id_, \
  942. .priv_data_size = sizeof(ADPCMDecodeContext), \
  943. .init = adpcm_decode_init, \
  944. .decode = adpcm_decode_frame, \
  945. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  946. }
  947. /* Note: Do not forget to add new entries to the Makefile as well. */
  948. ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
  949. ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
  950. ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
  951. ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  952. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  953. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  954. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  955. ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  956. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
  957. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  958. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  959. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  960. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  961. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  962. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
  963. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  964. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
  965. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
  966. ADPCM_DECODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
  967. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  968. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  969. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  970. ADPCM_DECODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
  971. ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  972. ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
  973. ADPCM_DECODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");