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.

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