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.

1336 lines
51KB

  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. AVFrame frame;
  82. ADPCMChannelStatus status[6];
  83. int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
  84. } ADPCMDecodeContext;
  85. static av_cold int adpcm_decode_init(AVCodecContext * avctx)
  86. {
  87. ADPCMDecodeContext *c = avctx->priv_data;
  88. unsigned int min_channels = 1;
  89. unsigned int max_channels = 2;
  90. switch(avctx->codec->id) {
  91. case AV_CODEC_ID_ADPCM_EA:
  92. min_channels = 2;
  93. break;
  94. case AV_CODEC_ID_ADPCM_EA_R1:
  95. case AV_CODEC_ID_ADPCM_EA_R2:
  96. case AV_CODEC_ID_ADPCM_EA_R3:
  97. case AV_CODEC_ID_ADPCM_EA_XAS:
  98. max_channels = 6;
  99. break;
  100. }
  101. if (avctx->channels < min_channels || avctx->channels > max_channels) {
  102. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  103. return AVERROR(EINVAL);
  104. }
  105. switch(avctx->codec->id) {
  106. case AV_CODEC_ID_ADPCM_CT:
  107. c->status[0].step = c->status[1].step = 511;
  108. break;
  109. case AV_CODEC_ID_ADPCM_IMA_WAV:
  110. if (avctx->bits_per_coded_sample != 4) {
  111. av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
  112. return -1;
  113. }
  114. break;
  115. case AV_CODEC_ID_ADPCM_IMA_APC:
  116. if (avctx->extradata && avctx->extradata_size >= 8) {
  117. c->status[0].predictor = AV_RL32(avctx->extradata);
  118. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  119. }
  120. break;
  121. case AV_CODEC_ID_ADPCM_IMA_WS:
  122. if (avctx->extradata && avctx->extradata_size >= 2)
  123. c->vqa_version = AV_RL16(avctx->extradata);
  124. break;
  125. default:
  126. break;
  127. }
  128. switch(avctx->codec->id) {
  129. case AV_CODEC_ID_ADPCM_IMA_QT:
  130. case AV_CODEC_ID_ADPCM_IMA_WAV:
  131. case AV_CODEC_ID_ADPCM_4XM:
  132. case AV_CODEC_ID_ADPCM_XA:
  133. case AV_CODEC_ID_ADPCM_EA_R1:
  134. case AV_CODEC_ID_ADPCM_EA_R2:
  135. case AV_CODEC_ID_ADPCM_EA_R3:
  136. case AV_CODEC_ID_ADPCM_EA_XAS:
  137. case AV_CODEC_ID_ADPCM_THP:
  138. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  139. break;
  140. case AV_CODEC_ID_ADPCM_IMA_WS:
  141. avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
  142. AV_SAMPLE_FMT_S16;
  143. break;
  144. default:
  145. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  146. }
  147. avcodec_get_frame_defaults(&c->frame);
  148. avctx->coded_frame = &c->frame;
  149. return 0;
  150. }
  151. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  152. {
  153. int step_index;
  154. int predictor;
  155. int sign, delta, diff, step;
  156. step = ff_adpcm_step_table[c->step_index];
  157. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  158. step_index = av_clip(step_index, 0, 88);
  159. sign = nibble & 8;
  160. delta = nibble & 7;
  161. /* perform direct multiplication instead of series of jumps proposed by
  162. * the reference ADPCM implementation since modern CPUs can do the mults
  163. * quickly enough */
  164. diff = ((2 * delta + 1) * step) >> shift;
  165. predictor = c->predictor;
  166. if (sign) predictor -= diff;
  167. else predictor += diff;
  168. c->predictor = av_clip_int16(predictor);
  169. c->step_index = step_index;
  170. return (short)c->predictor;
  171. }
  172. static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
  173. {
  174. int step_index;
  175. int predictor;
  176. int diff, step;
  177. step = ff_adpcm_step_table[c->step_index];
  178. step_index = c->step_index + ff_adpcm_index_table[nibble];
  179. step_index = av_clip(step_index, 0, 88);
  180. diff = step >> 3;
  181. if (nibble & 4) diff += step;
  182. if (nibble & 2) diff += step >> 1;
  183. if (nibble & 1) diff += step >> 2;
  184. if (nibble & 8)
  185. predictor = c->predictor - diff;
  186. else
  187. predictor = c->predictor + diff;
  188. c->predictor = av_clip_int16(predictor);
  189. c->step_index = step_index;
  190. return c->predictor;
  191. }
  192. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
  193. {
  194. int predictor;
  195. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  196. predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  197. c->sample2 = c->sample1;
  198. c->sample1 = av_clip_int16(predictor);
  199. c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
  200. if (c->idelta < 16) c->idelta = 16;
  201. return c->sample1;
  202. }
  203. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  204. {
  205. int sign, delta, diff;
  206. int new_step;
  207. sign = nibble & 8;
  208. delta = nibble & 7;
  209. /* perform direct multiplication instead of series of jumps proposed by
  210. * the reference ADPCM implementation since modern CPUs can do the mults
  211. * quickly enough */
  212. diff = ((2 * delta + 1) * c->step) >> 3;
  213. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  214. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  215. c->predictor = av_clip_int16(c->predictor);
  216. /* calculate new step and clamp it to range 511..32767 */
  217. new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
  218. c->step = av_clip(new_step, 511, 32767);
  219. return (short)c->predictor;
  220. }
  221. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  222. {
  223. int sign, delta, diff;
  224. sign = nibble & (1<<(size-1));
  225. delta = nibble & ((1<<(size-1))-1);
  226. diff = delta << (7 + c->step + shift);
  227. /* clamp result */
  228. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  229. /* calculate new step */
  230. if (delta >= (2*size - 3) && c->step < 3)
  231. c->step++;
  232. else if (delta == 0 && c->step > 0)
  233. c->step--;
  234. return (short) c->predictor;
  235. }
  236. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  237. {
  238. if(!c->step) {
  239. c->predictor = 0;
  240. c->step = 127;
  241. }
  242. c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
  243. c->predictor = av_clip_int16(c->predictor);
  244. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  245. c->step = av_clip(c->step, 127, 24567);
  246. return c->predictor;
  247. }
  248. static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
  249. const uint8_t *in, ADPCMChannelStatus *left,
  250. ADPCMChannelStatus *right, int channels, int sample_offset)
  251. {
  252. int i, j;
  253. int shift,filter,f0,f1;
  254. int s_1,s_2;
  255. int d,s,t;
  256. out0 += sample_offset;
  257. if (channels == 1)
  258. out1 = out0 + 28;
  259. else
  260. out1 += sample_offset;
  261. for(i=0;i<4;i++) {
  262. shift = 12 - (in[4+i*2] & 15);
  263. filter = in[4+i*2] >> 4;
  264. if (filter > 4) {
  265. av_log(avctx, AV_LOG_ERROR,
  266. "Invalid XA-ADPCM filter %d (max. allowed is 4)\n",
  267. filter);
  268. return AVERROR_INVALIDDATA;
  269. }
  270. f0 = xa_adpcm_table[filter][0];
  271. f1 = xa_adpcm_table[filter][1];
  272. s_1 = left->sample1;
  273. s_2 = left->sample2;
  274. for(j=0;j<28;j++) {
  275. d = in[16+i+j*4];
  276. t = sign_extend(d, 4);
  277. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  278. s_2 = s_1;
  279. s_1 = av_clip_int16(s);
  280. out0[j] = s_1;
  281. }
  282. if (channels == 2) {
  283. left->sample1 = s_1;
  284. left->sample2 = s_2;
  285. s_1 = right->sample1;
  286. s_2 = right->sample2;
  287. }
  288. shift = 12 - (in[5+i*2] & 15);
  289. filter = in[5+i*2] >> 4;
  290. if (filter > 4) {
  291. av_log(avctx, AV_LOG_ERROR,
  292. "Invalid XA-ADPCM filter %d (max. allowed is 4)\n",
  293. filter);
  294. return AVERROR_INVALIDDATA;
  295. }
  296. f0 = xa_adpcm_table[filter][0];
  297. f1 = xa_adpcm_table[filter][1];
  298. for(j=0;j<28;j++) {
  299. d = in[16+i+j*4];
  300. t = sign_extend(d >> 4, 4);
  301. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  302. s_2 = s_1;
  303. s_1 = av_clip_int16(s);
  304. out1[j] = s_1;
  305. }
  306. if (channels == 2) {
  307. right->sample1 = s_1;
  308. right->sample2 = s_2;
  309. } else {
  310. left->sample1 = s_1;
  311. left->sample2 = s_2;
  312. }
  313. out0 += 28 * (3 - channels);
  314. out1 += 28 * (3 - channels);
  315. }
  316. return 0;
  317. }
  318. static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
  319. {
  320. ADPCMDecodeContext *c = avctx->priv_data;
  321. GetBitContext gb;
  322. const int *table;
  323. int k0, signmask, nb_bits, count;
  324. int size = buf_size*8;
  325. int i;
  326. init_get_bits(&gb, buf, size);
  327. //read bits & initial values
  328. nb_bits = get_bits(&gb, 2)+2;
  329. table = swf_index_tables[nb_bits-2];
  330. k0 = 1 << (nb_bits-2);
  331. signmask = 1 << (nb_bits-1);
  332. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  333. for (i = 0; i < avctx->channels; i++) {
  334. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  335. c->status[i].step_index = get_bits(&gb, 6);
  336. }
  337. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  338. int i;
  339. for (i = 0; i < avctx->channels; i++) {
  340. // similar to IMA adpcm
  341. int delta = get_bits(&gb, nb_bits);
  342. int step = ff_adpcm_step_table[c->status[i].step_index];
  343. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  344. int k = k0;
  345. do {
  346. if (delta & k)
  347. vpdiff += step;
  348. step >>= 1;
  349. k >>= 1;
  350. } while(k);
  351. vpdiff += step;
  352. if (delta & signmask)
  353. c->status[i].predictor -= vpdiff;
  354. else
  355. c->status[i].predictor += vpdiff;
  356. c->status[i].step_index += table[delta & (~signmask)];
  357. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  358. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  359. *samples++ = c->status[i].predictor;
  360. }
  361. }
  362. }
  363. }
  364. /**
  365. * Get the number of samples that will be decoded from the packet.
  366. * In one case, this is actually the maximum number of samples possible to
  367. * decode with the given buf_size.
  368. *
  369. * @param[out] coded_samples set to the number of samples as coded in the
  370. * packet, or 0 if the codec does not encode the
  371. * number of samples in each frame.
  372. */
  373. static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
  374. int buf_size, int *coded_samples)
  375. {
  376. ADPCMDecodeContext *s = avctx->priv_data;
  377. int nb_samples = 0;
  378. int ch = avctx->channels;
  379. int has_coded_samples = 0;
  380. int header_size;
  381. *coded_samples = 0;
  382. switch (avctx->codec->id) {
  383. /* constant, only check buf_size */
  384. case AV_CODEC_ID_ADPCM_EA_XAS:
  385. if (buf_size < 76 * ch)
  386. return 0;
  387. nb_samples = 128;
  388. break;
  389. case AV_CODEC_ID_ADPCM_IMA_QT:
  390. if (buf_size < 34 * ch)
  391. return 0;
  392. nb_samples = 64;
  393. break;
  394. /* simple 4-bit adpcm */
  395. case AV_CODEC_ID_ADPCM_CT:
  396. case AV_CODEC_ID_ADPCM_IMA_APC:
  397. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  398. case AV_CODEC_ID_ADPCM_IMA_WS:
  399. case AV_CODEC_ID_ADPCM_YAMAHA:
  400. nb_samples = buf_size * 2 / ch;
  401. break;
  402. }
  403. if (nb_samples)
  404. return nb_samples;
  405. /* simple 4-bit adpcm, with header */
  406. header_size = 0;
  407. switch (avctx->codec->id) {
  408. case AV_CODEC_ID_ADPCM_4XM:
  409. case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
  410. case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
  411. case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4; break;
  412. }
  413. if (header_size > 0)
  414. return (buf_size - header_size) * 2 / ch;
  415. /* more complex formats */
  416. switch (avctx->codec->id) {
  417. case AV_CODEC_ID_ADPCM_EA:
  418. has_coded_samples = 1;
  419. *coded_samples = bytestream2_get_le32(gb);
  420. *coded_samples -= *coded_samples % 28;
  421. nb_samples = (buf_size - 12) / 30 * 28;
  422. break;
  423. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  424. has_coded_samples = 1;
  425. *coded_samples = bytestream2_get_le32(gb);
  426. nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
  427. break;
  428. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  429. nb_samples = (buf_size - ch) / ch * 2;
  430. break;
  431. case AV_CODEC_ID_ADPCM_EA_R1:
  432. case AV_CODEC_ID_ADPCM_EA_R2:
  433. case AV_CODEC_ID_ADPCM_EA_R3:
  434. /* maximum number of samples */
  435. /* has internal offsets and a per-frame switch to signal raw 16-bit */
  436. has_coded_samples = 1;
  437. switch (avctx->codec->id) {
  438. case AV_CODEC_ID_ADPCM_EA_R1:
  439. header_size = 4 + 9 * ch;
  440. *coded_samples = bytestream2_get_le32(gb);
  441. break;
  442. case AV_CODEC_ID_ADPCM_EA_R2:
  443. header_size = 4 + 5 * ch;
  444. *coded_samples = bytestream2_get_le32(gb);
  445. break;
  446. case AV_CODEC_ID_ADPCM_EA_R3:
  447. header_size = 4 + 5 * ch;
  448. *coded_samples = bytestream2_get_be32(gb);
  449. break;
  450. }
  451. *coded_samples -= *coded_samples % 28;
  452. nb_samples = (buf_size - header_size) * 2 / ch;
  453. nb_samples -= nb_samples % 28;
  454. break;
  455. case AV_CODEC_ID_ADPCM_IMA_DK3:
  456. if (avctx->block_align > 0)
  457. buf_size = FFMIN(buf_size, avctx->block_align);
  458. nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
  459. break;
  460. case AV_CODEC_ID_ADPCM_IMA_DK4:
  461. if (avctx->block_align > 0)
  462. buf_size = FFMIN(buf_size, avctx->block_align);
  463. nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
  464. break;
  465. case AV_CODEC_ID_ADPCM_IMA_WAV:
  466. if (avctx->block_align > 0)
  467. buf_size = FFMIN(buf_size, avctx->block_align);
  468. nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
  469. break;
  470. case AV_CODEC_ID_ADPCM_MS:
  471. if (avctx->block_align > 0)
  472. buf_size = FFMIN(buf_size, avctx->block_align);
  473. nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
  474. break;
  475. case AV_CODEC_ID_ADPCM_SBPRO_2:
  476. case AV_CODEC_ID_ADPCM_SBPRO_3:
  477. case AV_CODEC_ID_ADPCM_SBPRO_4:
  478. {
  479. int samples_per_byte;
  480. switch (avctx->codec->id) {
  481. case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
  482. case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
  483. case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
  484. }
  485. if (!s->status[0].step_index) {
  486. nb_samples++;
  487. buf_size -= ch;
  488. }
  489. nb_samples += buf_size * samples_per_byte / ch;
  490. break;
  491. }
  492. case AV_CODEC_ID_ADPCM_SWF:
  493. {
  494. int buf_bits = buf_size * 8 - 2;
  495. int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
  496. int block_hdr_size = 22 * ch;
  497. int block_size = block_hdr_size + nbits * ch * 4095;
  498. int nblocks = buf_bits / block_size;
  499. int bits_left = buf_bits - nblocks * block_size;
  500. nb_samples = nblocks * 4096;
  501. if (bits_left >= block_hdr_size)
  502. nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
  503. break;
  504. }
  505. case AV_CODEC_ID_ADPCM_THP:
  506. has_coded_samples = 1;
  507. bytestream2_skip(gb, 4); // channel size
  508. *coded_samples = bytestream2_get_be32(gb);
  509. *coded_samples -= *coded_samples % 14;
  510. nb_samples = (buf_size - 80) / (8 * ch) * 14;
  511. break;
  512. case AV_CODEC_ID_ADPCM_XA:
  513. nb_samples = (buf_size / 128) * 224 / ch;
  514. break;
  515. }
  516. /* validate coded sample count */
  517. if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
  518. return AVERROR_INVALIDDATA;
  519. return nb_samples;
  520. }
  521. static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
  522. int *got_frame_ptr, AVPacket *avpkt)
  523. {
  524. const uint8_t *buf = avpkt->data;
  525. int buf_size = avpkt->size;
  526. ADPCMDecodeContext *c = avctx->priv_data;
  527. ADPCMChannelStatus *cs;
  528. int n, m, channel, i;
  529. short *samples;
  530. int16_t **samples_p;
  531. int st; /* stereo */
  532. int count1, count2;
  533. int nb_samples, coded_samples, ret;
  534. GetByteContext gb;
  535. bytestream2_init(&gb, buf, buf_size);
  536. nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
  537. if (nb_samples <= 0) {
  538. av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
  539. return AVERROR_INVALIDDATA;
  540. }
  541. /* get output buffer */
  542. c->frame.nb_samples = nb_samples;
  543. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  544. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  545. return ret;
  546. }
  547. samples = (short *)c->frame.data[0];
  548. samples_p = (int16_t **)c->frame.extended_data;
  549. /* use coded_samples when applicable */
  550. /* it is always <= nb_samples, so the output buffer will be large enough */
  551. if (coded_samples) {
  552. if (coded_samples != nb_samples)
  553. av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
  554. c->frame.nb_samples = nb_samples = coded_samples;
  555. }
  556. st = avctx->channels == 2 ? 1 : 0;
  557. switch(avctx->codec->id) {
  558. case AV_CODEC_ID_ADPCM_IMA_QT:
  559. /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
  560. Channel data is interleaved per-chunk. */
  561. for (channel = 0; channel < avctx->channels; channel++) {
  562. int predictor;
  563. int step_index;
  564. cs = &(c->status[channel]);
  565. /* (pppppp) (piiiiiii) */
  566. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  567. predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  568. step_index = predictor & 0x7F;
  569. predictor &= ~0x7F;
  570. if (cs->step_index == step_index) {
  571. int diff = predictor - cs->predictor;
  572. if (diff < 0)
  573. diff = - diff;
  574. if (diff > 0x7f)
  575. goto update;
  576. } else {
  577. update:
  578. cs->step_index = step_index;
  579. cs->predictor = predictor;
  580. }
  581. if (cs->step_index > 88u){
  582. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  583. channel, cs->step_index);
  584. return AVERROR_INVALIDDATA;
  585. }
  586. samples = samples_p[channel];
  587. for (m = 0; m < 64; m += 2) {
  588. int byte = bytestream2_get_byteu(&gb);
  589. samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
  590. samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
  591. }
  592. }
  593. break;
  594. case AV_CODEC_ID_ADPCM_IMA_WAV:
  595. for(i=0; i<avctx->channels; i++){
  596. cs = &(c->status[i]);
  597. cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
  598. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  599. if (cs->step_index > 88u){
  600. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  601. i, cs->step_index);
  602. return AVERROR_INVALIDDATA;
  603. }
  604. }
  605. for (n = 0; n < (nb_samples - 1) / 8; n++) {
  606. for (i = 0; i < avctx->channels; i++) {
  607. cs = &c->status[i];
  608. samples = &samples_p[i][1 + n * 8];
  609. for (m = 0; m < 8; m += 2) {
  610. int v = bytestream2_get_byteu(&gb);
  611. samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  612. samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  613. }
  614. }
  615. }
  616. break;
  617. case AV_CODEC_ID_ADPCM_4XM:
  618. for (i = 0; i < avctx->channels; i++)
  619. c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  620. for (i = 0; i < avctx->channels; i++) {
  621. c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  622. if (c->status[i].step_index > 88u) {
  623. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  624. i, c->status[i].step_index);
  625. return AVERROR_INVALIDDATA;
  626. }
  627. }
  628. for (i = 0; i < avctx->channels; i++) {
  629. samples = (int16_t *)c->frame.data[i];
  630. cs = &c->status[i];
  631. for (n = nb_samples >> 1; n > 0; n--) {
  632. int v = bytestream2_get_byteu(&gb);
  633. *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
  634. *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
  635. }
  636. }
  637. break;
  638. case AV_CODEC_ID_ADPCM_MS:
  639. {
  640. int block_predictor;
  641. block_predictor = bytestream2_get_byteu(&gb);
  642. if (block_predictor > 6) {
  643. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
  644. block_predictor);
  645. return AVERROR_INVALIDDATA;
  646. }
  647. c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  648. c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  649. if (st) {
  650. block_predictor = bytestream2_get_byteu(&gb);
  651. if (block_predictor > 6) {
  652. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
  653. block_predictor);
  654. return AVERROR_INVALIDDATA;
  655. }
  656. c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  657. c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  658. }
  659. c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  660. if (st){
  661. c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  662. }
  663. c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  664. if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  665. c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  666. if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  667. *samples++ = c->status[0].sample2;
  668. if (st) *samples++ = c->status[1].sample2;
  669. *samples++ = c->status[0].sample1;
  670. if (st) *samples++ = c->status[1].sample1;
  671. for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
  672. int byte = bytestream2_get_byteu(&gb);
  673. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
  674. *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
  675. }
  676. break;
  677. }
  678. case AV_CODEC_ID_ADPCM_IMA_DK4:
  679. for (channel = 0; channel < avctx->channels; channel++) {
  680. cs = &c->status[channel];
  681. cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
  682. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  683. if (cs->step_index > 88u){
  684. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  685. channel, cs->step_index);
  686. return AVERROR_INVALIDDATA;
  687. }
  688. }
  689. for (n = nb_samples >> (1 - st); n > 0; n--) {
  690. int v = bytestream2_get_byteu(&gb);
  691. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
  692. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  693. }
  694. break;
  695. case AV_CODEC_ID_ADPCM_IMA_DK3:
  696. {
  697. int last_byte = 0;
  698. int nibble;
  699. int decode_top_nibble_next = 0;
  700. int diff_channel;
  701. const int16_t *samples_end = samples + avctx->channels * nb_samples;
  702. bytestream2_skipu(&gb, 10);
  703. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  704. c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  705. c->status[0].step_index = bytestream2_get_byteu(&gb);
  706. c->status[1].step_index = bytestream2_get_byteu(&gb);
  707. if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
  708. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
  709. c->status[0].step_index, c->status[1].step_index);
  710. return AVERROR_INVALIDDATA;
  711. }
  712. /* sign extend the predictors */
  713. diff_channel = c->status[1].predictor;
  714. /* DK3 ADPCM support macro */
  715. #define DK3_GET_NEXT_NIBBLE() \
  716. if (decode_top_nibble_next) { \
  717. nibble = last_byte >> 4; \
  718. decode_top_nibble_next = 0; \
  719. } else { \
  720. last_byte = bytestream2_get_byteu(&gb); \
  721. nibble = last_byte & 0x0F; \
  722. decode_top_nibble_next = 1; \
  723. }
  724. while (samples < samples_end) {
  725. /* for this algorithm, c->status[0] is the sum channel and
  726. * c->status[1] is the diff channel */
  727. /* process the first predictor of the sum channel */
  728. DK3_GET_NEXT_NIBBLE();
  729. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  730. /* process the diff channel predictor */
  731. DK3_GET_NEXT_NIBBLE();
  732. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  733. /* process the first pair of stereo PCM samples */
  734. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  735. *samples++ = c->status[0].predictor + c->status[1].predictor;
  736. *samples++ = c->status[0].predictor - c->status[1].predictor;
  737. /* process the second predictor of the sum channel */
  738. DK3_GET_NEXT_NIBBLE();
  739. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  740. /* process the second pair of stereo PCM samples */
  741. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  742. *samples++ = c->status[0].predictor + c->status[1].predictor;
  743. *samples++ = c->status[0].predictor - c->status[1].predictor;
  744. }
  745. break;
  746. }
  747. case AV_CODEC_ID_ADPCM_IMA_ISS:
  748. for (channel = 0; channel < avctx->channels; channel++) {
  749. cs = &c->status[channel];
  750. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  751. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  752. if (cs->step_index > 88u){
  753. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  754. channel, cs->step_index);
  755. return AVERROR_INVALIDDATA;
  756. }
  757. }
  758. for (n = nb_samples >> (1 - st); n > 0; n--) {
  759. int v1, v2;
  760. int v = bytestream2_get_byteu(&gb);
  761. /* nibbles are swapped for mono */
  762. if (st) {
  763. v1 = v >> 4;
  764. v2 = v & 0x0F;
  765. } else {
  766. v2 = v >> 4;
  767. v1 = v & 0x0F;
  768. }
  769. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
  770. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
  771. }
  772. break;
  773. case AV_CODEC_ID_ADPCM_IMA_APC:
  774. while (bytestream2_get_bytes_left(&gb) > 0) {
  775. int v = bytestream2_get_byteu(&gb);
  776. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
  777. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  778. }
  779. break;
  780. case AV_CODEC_ID_ADPCM_IMA_WS:
  781. if (c->vqa_version == 3) {
  782. for (channel = 0; channel < avctx->channels; channel++) {
  783. int16_t *smp = samples_p[channel];
  784. for (n = nb_samples / 2; n > 0; n--) {
  785. int v = bytestream2_get_byteu(&gb);
  786. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  787. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  788. }
  789. }
  790. } else {
  791. for (n = nb_samples / 2; n > 0; n--) {
  792. for (channel = 0; channel < avctx->channels; channel++) {
  793. int v = bytestream2_get_byteu(&gb);
  794. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  795. samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  796. }
  797. samples += avctx->channels;
  798. }
  799. }
  800. bytestream2_seek(&gb, 0, SEEK_END);
  801. break;
  802. case AV_CODEC_ID_ADPCM_XA:
  803. {
  804. int16_t *out0 = samples_p[0];
  805. int16_t *out1 = samples_p[1];
  806. int samples_per_block = 28 * (3 - avctx->channels) * 4;
  807. int sample_offset = 0;
  808. while (bytestream2_get_bytes_left(&gb) >= 128) {
  809. if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
  810. &c->status[0], &c->status[1],
  811. avctx->channels, sample_offset)) < 0)
  812. return ret;
  813. bytestream2_skipu(&gb, 128);
  814. sample_offset += samples_per_block;
  815. }
  816. break;
  817. }
  818. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  819. for (i=0; i<=st; i++) {
  820. c->status[i].step_index = bytestream2_get_le32u(&gb);
  821. if (c->status[i].step_index > 88u) {
  822. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  823. i, c->status[i].step_index);
  824. return AVERROR_INVALIDDATA;
  825. }
  826. }
  827. for (i=0; i<=st; i++)
  828. c->status[i].predictor = bytestream2_get_le32u(&gb);
  829. for (n = nb_samples >> (1 - st); n > 0; n--) {
  830. int byte = bytestream2_get_byteu(&gb);
  831. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
  832. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
  833. }
  834. break;
  835. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  836. for (n = nb_samples >> (1 - st); n > 0; n--) {
  837. int byte = bytestream2_get_byteu(&gb);
  838. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
  839. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
  840. }
  841. break;
  842. case AV_CODEC_ID_ADPCM_EA:
  843. {
  844. int previous_left_sample, previous_right_sample;
  845. int current_left_sample, current_right_sample;
  846. int next_left_sample, next_right_sample;
  847. int coeff1l, coeff2l, coeff1r, coeff2r;
  848. int shift_left, shift_right;
  849. /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
  850. each coding 28 stereo samples. */
  851. current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  852. previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  853. current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  854. previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  855. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  856. int byte = bytestream2_get_byteu(&gb);
  857. coeff1l = ea_adpcm_table[ byte >> 4 ];
  858. coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
  859. coeff1r = ea_adpcm_table[ byte & 0x0F];
  860. coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
  861. byte = bytestream2_get_byteu(&gb);
  862. shift_left = 20 - (byte >> 4);
  863. shift_right = 20 - (byte & 0x0F);
  864. for (count2 = 0; count2 < 28; count2++) {
  865. byte = bytestream2_get_byteu(&gb);
  866. next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
  867. next_right_sample = sign_extend(byte, 4) << shift_right;
  868. next_left_sample = (next_left_sample +
  869. (current_left_sample * coeff1l) +
  870. (previous_left_sample * coeff2l) + 0x80) >> 8;
  871. next_right_sample = (next_right_sample +
  872. (current_right_sample * coeff1r) +
  873. (previous_right_sample * coeff2r) + 0x80) >> 8;
  874. previous_left_sample = current_left_sample;
  875. current_left_sample = av_clip_int16(next_left_sample);
  876. previous_right_sample = current_right_sample;
  877. current_right_sample = av_clip_int16(next_right_sample);
  878. *samples++ = current_left_sample;
  879. *samples++ = current_right_sample;
  880. }
  881. }
  882. bytestream2_skip(&gb, 2); // Skip terminating 0x0000
  883. break;
  884. }
  885. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  886. {
  887. int coeff[2][2], shift[2];
  888. for(channel = 0; channel < avctx->channels; channel++) {
  889. int byte = bytestream2_get_byteu(&gb);
  890. for (i=0; i<2; i++)
  891. coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
  892. shift[channel] = 20 - (byte & 0x0F);
  893. }
  894. for (count1 = 0; count1 < nb_samples / 2; count1++) {
  895. int byte[2];
  896. byte[0] = bytestream2_get_byteu(&gb);
  897. if (st) byte[1] = bytestream2_get_byteu(&gb);
  898. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  899. for(channel = 0; channel < avctx->channels; channel++) {
  900. int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
  901. sample = (sample +
  902. c->status[channel].sample1 * coeff[channel][0] +
  903. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  904. c->status[channel].sample2 = c->status[channel].sample1;
  905. c->status[channel].sample1 = av_clip_int16(sample);
  906. *samples++ = c->status[channel].sample1;
  907. }
  908. }
  909. }
  910. bytestream2_seek(&gb, 0, SEEK_END);
  911. break;
  912. }
  913. case AV_CODEC_ID_ADPCM_EA_R1:
  914. case AV_CODEC_ID_ADPCM_EA_R2:
  915. case AV_CODEC_ID_ADPCM_EA_R3: {
  916. /* channel numbering
  917. 2chan: 0=fl, 1=fr
  918. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  919. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  920. const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
  921. int previous_sample, current_sample, next_sample;
  922. int coeff1, coeff2;
  923. int shift;
  924. unsigned int channel;
  925. uint16_t *samplesC;
  926. int count = 0;
  927. int offsets[6];
  928. for (channel=0; channel<avctx->channels; channel++)
  929. offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
  930. bytestream2_get_le32(&gb)) +
  931. (avctx->channels + 1) * 4;
  932. for (channel=0; channel<avctx->channels; channel++) {
  933. bytestream2_seek(&gb, offsets[channel], SEEK_SET);
  934. samplesC = samples_p[channel];
  935. if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
  936. current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  937. previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  938. } else {
  939. current_sample = c->status[channel].predictor;
  940. previous_sample = c->status[channel].prev_sample;
  941. }
  942. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  943. int byte = bytestream2_get_byte(&gb);
  944. if (byte == 0xEE) { /* only seen in R2 and R3 */
  945. current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  946. previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  947. for (count2=0; count2<28; count2++)
  948. *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
  949. } else {
  950. coeff1 = ea_adpcm_table[ byte >> 4 ];
  951. coeff2 = ea_adpcm_table[(byte >> 4) + 4];
  952. shift = 20 - (byte & 0x0F);
  953. for (count2=0; count2<28; count2++) {
  954. if (count2 & 1)
  955. next_sample = sign_extend(byte, 4) << shift;
  956. else {
  957. byte = bytestream2_get_byte(&gb);
  958. next_sample = sign_extend(byte >> 4, 4) << shift;
  959. }
  960. next_sample += (current_sample * coeff1) +
  961. (previous_sample * coeff2);
  962. next_sample = av_clip_int16(next_sample >> 8);
  963. previous_sample = current_sample;
  964. current_sample = next_sample;
  965. *samplesC++ = current_sample;
  966. }
  967. }
  968. }
  969. if (!count) {
  970. count = count1;
  971. } else if (count != count1) {
  972. av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
  973. count = FFMAX(count, count1);
  974. }
  975. if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
  976. c->status[channel].predictor = current_sample;
  977. c->status[channel].prev_sample = previous_sample;
  978. }
  979. }
  980. c->frame.nb_samples = count * 28;
  981. bytestream2_seek(&gb, 0, SEEK_END);
  982. break;
  983. }
  984. case AV_CODEC_ID_ADPCM_EA_XAS:
  985. for (channel=0; channel<avctx->channels; channel++) {
  986. int coeff[2][4], shift[4];
  987. int16_t *s = samples_p[channel];
  988. for (n = 0; n < 4; n++, s += 32) {
  989. int val = sign_extend(bytestream2_get_le16u(&gb), 16);
  990. for (i=0; i<2; i++)
  991. coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
  992. s[0] = val & ~0x0F;
  993. val = sign_extend(bytestream2_get_le16u(&gb), 16);
  994. shift[n] = 20 - (val & 0x0F);
  995. s[1] = val & ~0x0F;
  996. }
  997. for (m=2; m<32; m+=2) {
  998. s = &samples_p[channel][m];
  999. for (n = 0; n < 4; n++, s += 32) {
  1000. int level, pred;
  1001. int byte = bytestream2_get_byteu(&gb);
  1002. level = sign_extend(byte >> 4, 4) << shift[n];
  1003. pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
  1004. s[0] = av_clip_int16((level + pred + 0x80) >> 8);
  1005. level = sign_extend(byte, 4) << shift[n];
  1006. pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
  1007. s[1] = av_clip_int16((level + pred + 0x80) >> 8);
  1008. }
  1009. }
  1010. }
  1011. break;
  1012. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1013. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1014. if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_AMV) {
  1015. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  1016. c->status[0].step_index = bytestream2_get_le16u(&gb);
  1017. bytestream2_skipu(&gb, 4);
  1018. } else {
  1019. c->status[0].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  1020. c->status[0].step_index = bytestream2_get_byteu(&gb);
  1021. bytestream2_skipu(&gb, 1);
  1022. }
  1023. if (c->status[0].step_index > 88u) {
  1024. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1025. c->status[0].step_index);
  1026. return AVERROR_INVALIDDATA;
  1027. }
  1028. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1029. int hi, lo, v = bytestream2_get_byteu(&gb);
  1030. if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_AMV) {
  1031. hi = v & 0x0F;
  1032. lo = v >> 4;
  1033. } else {
  1034. lo = v & 0x0F;
  1035. hi = v >> 4;
  1036. }
  1037. *samples++ = adpcm_ima_expand_nibble(&c->status[0], lo, 3);
  1038. *samples++ = adpcm_ima_expand_nibble(&c->status[0], hi, 3);
  1039. }
  1040. break;
  1041. case AV_CODEC_ID_ADPCM_CT:
  1042. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1043. int v = bytestream2_get_byteu(&gb);
  1044. *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
  1045. *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
  1046. }
  1047. break;
  1048. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1049. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1050. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1051. if (!c->status[0].step_index) {
  1052. /* the first byte is a raw sample */
  1053. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1054. if (st)
  1055. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1056. c->status[0].step_index = 1;
  1057. nb_samples--;
  1058. }
  1059. if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
  1060. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1061. int byte = bytestream2_get_byteu(&gb);
  1062. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1063. byte >> 4, 4, 0);
  1064. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1065. byte & 0x0F, 4, 0);
  1066. }
  1067. } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
  1068. for (n = nb_samples / 3; n > 0; n--) {
  1069. int byte = bytestream2_get_byteu(&gb);
  1070. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1071. byte >> 5 , 3, 0);
  1072. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1073. (byte >> 2) & 0x07, 3, 0);
  1074. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1075. byte & 0x03, 2, 0);
  1076. }
  1077. } else {
  1078. for (n = nb_samples >> (2 - st); n > 0; n--) {
  1079. int byte = bytestream2_get_byteu(&gb);
  1080. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1081. byte >> 6 , 2, 2);
  1082. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1083. (byte >> 4) & 0x03, 2, 2);
  1084. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1085. (byte >> 2) & 0x03, 2, 2);
  1086. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1087. byte & 0x03, 2, 2);
  1088. }
  1089. }
  1090. break;
  1091. case AV_CODEC_ID_ADPCM_SWF:
  1092. adpcm_swf_decode(avctx, buf, buf_size, samples);
  1093. bytestream2_seek(&gb, 0, SEEK_END);
  1094. break;
  1095. case AV_CODEC_ID_ADPCM_YAMAHA:
  1096. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1097. int v = bytestream2_get_byteu(&gb);
  1098. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
  1099. *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
  1100. }
  1101. break;
  1102. case AV_CODEC_ID_ADPCM_THP:
  1103. {
  1104. int table[2][16];
  1105. int prev[2][2];
  1106. int ch;
  1107. for (i = 0; i < 2; i++)
  1108. for (n = 0; n < 16; n++)
  1109. table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
  1110. /* Initialize the previous sample. */
  1111. for (i = 0; i < 2; i++)
  1112. for (n = 0; n < 2; n++)
  1113. prev[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
  1114. for (ch = 0; ch <= st; ch++) {
  1115. samples = samples_p[ch];
  1116. /* Read in every sample for this channel. */
  1117. for (i = 0; i < nb_samples / 14; i++) {
  1118. int byte = bytestream2_get_byteu(&gb);
  1119. int index = (byte >> 4) & 7;
  1120. unsigned int exp = byte & 0x0F;
  1121. int factor1 = table[ch][index * 2];
  1122. int factor2 = table[ch][index * 2 + 1];
  1123. /* Decode 14 samples. */
  1124. for (n = 0; n < 14; n++) {
  1125. int32_t sampledat;
  1126. if (n & 1) {
  1127. sampledat = sign_extend(byte, 4);
  1128. } else {
  1129. byte = bytestream2_get_byteu(&gb);
  1130. sampledat = sign_extend(byte >> 4, 4);
  1131. }
  1132. sampledat = ((prev[ch][0]*factor1
  1133. + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
  1134. *samples = av_clip_int16(sampledat);
  1135. prev[ch][1] = prev[ch][0];
  1136. prev[ch][0] = *samples++;
  1137. }
  1138. }
  1139. }
  1140. break;
  1141. }
  1142. default:
  1143. return -1;
  1144. }
  1145. *got_frame_ptr = 1;
  1146. *(AVFrame *)data = c->frame;
  1147. return bytestream2_tell(&gb);
  1148. }
  1149. static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
  1150. AV_SAMPLE_FMT_NONE };
  1151. static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16,
  1152. AV_SAMPLE_FMT_NONE };
  1153. static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
  1154. AV_SAMPLE_FMT_S16P,
  1155. AV_SAMPLE_FMT_NONE };
  1156. #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
  1157. AVCodec ff_ ## name_ ## _decoder = { \
  1158. .name = #name_, \
  1159. .type = AVMEDIA_TYPE_AUDIO, \
  1160. .id = id_, \
  1161. .priv_data_size = sizeof(ADPCMDecodeContext), \
  1162. .init = adpcm_decode_init, \
  1163. .decode = adpcm_decode_frame, \
  1164. .capabilities = CODEC_CAP_DR1, \
  1165. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1166. .sample_fmts = sample_fmts_, \
  1167. }
  1168. /* Note: Do not forget to add new entries to the Makefile as well. */
  1169. ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
  1170. ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
  1171. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
  1172. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1173. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1174. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1175. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1176. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1177. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
  1178. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
  1179. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1180. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1181. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1182. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1183. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1184. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1185. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1186. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
  1187. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
  1188. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
  1189. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1190. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1191. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1192. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
  1193. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  1194. ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
  1195. ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");