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.

1335 lines
51KB

  1. /*
  2. * Copyright (c) 2001-2003 The FFmpeg project
  3. *
  4. * first version by Francois Revol (revol@free.fr)
  5. * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  6. * by Mike Melanson (melanson@pcisys.net)
  7. * CD-ROM XA ADPCM codec by BERO
  8. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  9. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  10. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  11. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  12. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  13. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  14. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  15. *
  16. * This file is part of Libav.
  17. *
  18. * Libav is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU Lesser General Public
  20. * License as published by the Free Software Foundation; either
  21. * version 2.1 of the License, or (at your option) any later version.
  22. *
  23. * Libav is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * Lesser General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Lesser General Public
  29. * License along with Libav; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  31. */
  32. #include "avcodec.h"
  33. #include "bitstream.h"
  34. #include "put_bits.h"
  35. #include "bytestream.h"
  36. #include "adpcm.h"
  37. #include "adpcm_data.h"
  38. #include "internal.h"
  39. /**
  40. * @file
  41. * ADPCM decoders
  42. * Features and limitations:
  43. *
  44. * Reference documents:
  45. * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
  46. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
  47. * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
  48. * http://openquicktime.sourceforge.net/
  49. * XAnim sources (xa_codec.c) http://xanim.polter.net/
  50. * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
  51. * SoX source code http://sox.sourceforge.net/
  52. *
  53. * CD-ROM XA:
  54. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
  55. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
  56. * readstr http://www.geocities.co.jp/Playtown/2004/
  57. */
  58. /* These are for CD-ROM XA ADPCM */
  59. static const int xa_adpcm_table[5][2] = {
  60. { 0, 0 },
  61. { 60, 0 },
  62. { 115, -52 },
  63. { 98, -55 },
  64. { 122, -60 }
  65. };
  66. static const int ea_adpcm_table[] = {
  67. 0, 240, 460, 392,
  68. 0, 0, -208, -220,
  69. 0, 1, 3, 4,
  70. 7, 8, 10, 11,
  71. 0, -1, -3, -4
  72. };
  73. // padded to zero where table size is less then 16
  74. static const int swf_index_tables[4][16] = {
  75. /*2*/ { -1, 2 },
  76. /*3*/ { -1, -1, 2, 4 },
  77. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  78. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  79. };
  80. /* end of tables */
  81. typedef struct ADPCMDecodeContext {
  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. return 0;
  148. }
  149. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  150. {
  151. int step_index;
  152. int predictor;
  153. int sign, delta, diff, step;
  154. step = ff_adpcm_step_table[c->step_index];
  155. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  156. step_index = av_clip(step_index, 0, 88);
  157. sign = nibble & 8;
  158. delta = nibble & 7;
  159. /* perform direct multiplication instead of series of jumps proposed by
  160. * the reference ADPCM implementation since modern CPUs can do the mults
  161. * quickly enough */
  162. diff = ((2 * delta + 1) * step) >> shift;
  163. predictor = c->predictor;
  164. if (sign) predictor -= diff;
  165. else predictor += diff;
  166. c->predictor = av_clip_int16(predictor);
  167. c->step_index = step_index;
  168. return (short)c->predictor;
  169. }
  170. static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
  171. {
  172. int step_index;
  173. int predictor;
  174. int diff, step;
  175. step = ff_adpcm_step_table[c->step_index];
  176. step_index = c->step_index + ff_adpcm_index_table[nibble];
  177. step_index = av_clip(step_index, 0, 88);
  178. diff = step >> 3;
  179. if (nibble & 4) diff += step;
  180. if (nibble & 2) diff += step >> 1;
  181. if (nibble & 1) diff += step >> 2;
  182. if (nibble & 8)
  183. predictor = c->predictor - diff;
  184. else
  185. predictor = c->predictor + diff;
  186. c->predictor = av_clip_int16(predictor);
  187. c->step_index = step_index;
  188. return c->predictor;
  189. }
  190. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
  191. {
  192. int predictor;
  193. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  194. predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  195. c->sample2 = c->sample1;
  196. c->sample1 = av_clip_int16(predictor);
  197. c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
  198. if (c->idelta < 16) c->idelta = 16;
  199. return c->sample1;
  200. }
  201. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  202. {
  203. int sign, delta, diff;
  204. int new_step;
  205. sign = nibble & 8;
  206. delta = nibble & 7;
  207. /* perform direct multiplication instead of series of jumps proposed by
  208. * the reference ADPCM implementation since modern CPUs can do the mults
  209. * quickly enough */
  210. diff = ((2 * delta + 1) * c->step) >> 3;
  211. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  212. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  213. c->predictor = av_clip_int16(c->predictor);
  214. /* calculate new step and clamp it to range 511..32767 */
  215. new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
  216. c->step = av_clip(new_step, 511, 32767);
  217. return (short)c->predictor;
  218. }
  219. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  220. {
  221. int sign, delta, diff;
  222. sign = nibble & (1<<(size-1));
  223. delta = nibble & ((1<<(size-1))-1);
  224. diff = delta << (7 + c->step + shift);
  225. /* clamp result */
  226. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  227. /* calculate new step */
  228. if (delta >= (2*size - 3) && c->step < 3)
  229. c->step++;
  230. else if (delta == 0 && c->step > 0)
  231. c->step--;
  232. return (short) c->predictor;
  233. }
  234. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  235. {
  236. if(!c->step) {
  237. c->predictor = 0;
  238. c->step = 127;
  239. }
  240. c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
  241. c->predictor = av_clip_int16(c->predictor);
  242. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  243. c->step = av_clip(c->step, 127, 24567);
  244. return c->predictor;
  245. }
  246. static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
  247. const uint8_t *in, ADPCMChannelStatus *left,
  248. ADPCMChannelStatus *right, int channels, int sample_offset)
  249. {
  250. int i, j;
  251. int shift,filter,f0,f1;
  252. int s_1,s_2;
  253. int d,s,t;
  254. out0 += sample_offset;
  255. if (channels == 1)
  256. out1 = out0 + 28;
  257. else
  258. out1 += sample_offset;
  259. for(i=0;i<4;i++) {
  260. shift = 12 - (in[4+i*2] & 15);
  261. filter = in[4+i*2] >> 4;
  262. if (filter > 4) {
  263. av_log(avctx, AV_LOG_ERROR,
  264. "Invalid XA-ADPCM filter %d (max. allowed is 4)\n",
  265. filter);
  266. return AVERROR_INVALIDDATA;
  267. }
  268. f0 = xa_adpcm_table[filter][0];
  269. f1 = xa_adpcm_table[filter][1];
  270. s_1 = left->sample1;
  271. s_2 = left->sample2;
  272. for(j=0;j<28;j++) {
  273. d = in[16+i+j*4];
  274. t = sign_extend(d, 4);
  275. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  276. s_2 = s_1;
  277. s_1 = av_clip_int16(s);
  278. out0[j] = s_1;
  279. }
  280. if (channels == 2) {
  281. left->sample1 = s_1;
  282. left->sample2 = s_2;
  283. s_1 = right->sample1;
  284. s_2 = right->sample2;
  285. }
  286. shift = 12 - (in[5+i*2] & 15);
  287. filter = in[5+i*2] >> 4;
  288. if (filter > 4) {
  289. av_log(avctx, AV_LOG_ERROR,
  290. "Invalid XA-ADPCM filter %d (max. allowed is 4)\n",
  291. filter);
  292. return AVERROR_INVALIDDATA;
  293. }
  294. f0 = xa_adpcm_table[filter][0];
  295. f1 = xa_adpcm_table[filter][1];
  296. for(j=0;j<28;j++) {
  297. d = in[16+i+j*4];
  298. t = sign_extend(d >> 4, 4);
  299. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  300. s_2 = s_1;
  301. s_1 = av_clip_int16(s);
  302. out1[j] = s_1;
  303. }
  304. if (channels == 2) {
  305. right->sample1 = s_1;
  306. right->sample2 = s_2;
  307. } else {
  308. left->sample1 = s_1;
  309. left->sample2 = s_2;
  310. }
  311. out0 += 28 * (3 - channels);
  312. out1 += 28 * (3 - channels);
  313. }
  314. return 0;
  315. }
  316. static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
  317. {
  318. ADPCMDecodeContext *c = avctx->priv_data;
  319. BitstreamContext bc;
  320. const int *table;
  321. int k0, signmask, nb_bits, count;
  322. int size = buf_size*8;
  323. int i;
  324. bitstream_init(&bc, buf, size);
  325. //read bits & initial values
  326. nb_bits = bitstream_read(&bc, 2)+2;
  327. table = swf_index_tables[nb_bits-2];
  328. k0 = 1 << (nb_bits-2);
  329. signmask = 1 << (nb_bits-1);
  330. while (bitstream_tell(&bc) <= size - 22 * avctx->channels) {
  331. for (i = 0; i < avctx->channels; i++) {
  332. *samples++ =
  333. c->status[i].predictor = bitstream_read_signed(&bc, 16);
  334. c->status[i].step_index = bitstream_read(&bc, 6);
  335. }
  336. for (count = 0; bitstream_tell(&bc) <= size - nb_bits * avctx->channels && count < 4095; count++) {
  337. int i;
  338. for (i = 0; i < avctx->channels; i++) {
  339. // similar to IMA adpcm
  340. int delta = bitstream_read(&bc, nb_bits);
  341. int step = ff_adpcm_step_table[c->status[i].step_index];
  342. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  343. int k = k0;
  344. do {
  345. if (delta & k)
  346. vpdiff += step;
  347. step >>= 1;
  348. k >>= 1;
  349. } while(k);
  350. vpdiff += step;
  351. if (delta & signmask)
  352. c->status[i].predictor -= vpdiff;
  353. else
  354. c->status[i].predictor += vpdiff;
  355. c->status[i].step_index += table[delta & (~signmask)];
  356. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  357. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  358. *samples++ = c->status[i].predictor;
  359. }
  360. }
  361. }
  362. }
  363. /**
  364. * Get the number of samples that will be decoded from the packet.
  365. * In one case, this is actually the maximum number of samples possible to
  366. * decode with the given buf_size.
  367. *
  368. * @param[out] coded_samples set to the number of samples as coded in the
  369. * packet, or 0 if the codec does not encode the
  370. * number of samples in each frame.
  371. */
  372. static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
  373. int buf_size, int *coded_samples)
  374. {
  375. ADPCMDecodeContext *s = avctx->priv_data;
  376. int nb_samples = 0;
  377. int ch = avctx->channels;
  378. int has_coded_samples = 0;
  379. int header_size;
  380. *coded_samples = 0;
  381. switch (avctx->codec->id) {
  382. /* constant, only check buf_size */
  383. case AV_CODEC_ID_ADPCM_EA_XAS:
  384. if (buf_size < 76 * ch)
  385. return 0;
  386. nb_samples = 128;
  387. break;
  388. case AV_CODEC_ID_ADPCM_IMA_QT:
  389. if (buf_size < 34 * ch)
  390. return 0;
  391. nb_samples = 64;
  392. break;
  393. /* simple 4-bit adpcm */
  394. case AV_CODEC_ID_ADPCM_CT:
  395. case AV_CODEC_ID_ADPCM_IMA_APC:
  396. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  397. case AV_CODEC_ID_ADPCM_IMA_WS:
  398. case AV_CODEC_ID_ADPCM_YAMAHA:
  399. nb_samples = buf_size * 2 / ch;
  400. break;
  401. }
  402. if (nb_samples)
  403. return nb_samples;
  404. /* simple 4-bit adpcm, with header */
  405. header_size = 0;
  406. switch (avctx->codec->id) {
  407. case AV_CODEC_ID_ADPCM_4XM:
  408. case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
  409. case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
  410. case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4; break;
  411. }
  412. if (header_size > 0)
  413. return (buf_size - header_size) * 2 / ch;
  414. /* more complex formats */
  415. switch (avctx->codec->id) {
  416. case AV_CODEC_ID_ADPCM_EA:
  417. has_coded_samples = 1;
  418. *coded_samples = bytestream2_get_le32(gb);
  419. *coded_samples -= *coded_samples % 28;
  420. nb_samples = (buf_size - 12) / 30 * 28;
  421. break;
  422. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  423. has_coded_samples = 1;
  424. *coded_samples = bytestream2_get_le32(gb);
  425. nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
  426. break;
  427. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  428. nb_samples = (buf_size - ch) / ch * 2;
  429. break;
  430. case AV_CODEC_ID_ADPCM_EA_R1:
  431. case AV_CODEC_ID_ADPCM_EA_R2:
  432. case AV_CODEC_ID_ADPCM_EA_R3:
  433. /* maximum number of samples */
  434. /* has internal offsets and a per-frame switch to signal raw 16-bit */
  435. has_coded_samples = 1;
  436. switch (avctx->codec->id) {
  437. case AV_CODEC_ID_ADPCM_EA_R1:
  438. header_size = 4 + 9 * ch;
  439. *coded_samples = bytestream2_get_le32(gb);
  440. break;
  441. case AV_CODEC_ID_ADPCM_EA_R2:
  442. header_size = 4 + 5 * ch;
  443. *coded_samples = bytestream2_get_le32(gb);
  444. break;
  445. case AV_CODEC_ID_ADPCM_EA_R3:
  446. header_size = 4 + 5 * ch;
  447. *coded_samples = bytestream2_get_be32(gb);
  448. break;
  449. }
  450. *coded_samples -= *coded_samples % 28;
  451. nb_samples = (buf_size - header_size) * 2 / ch;
  452. nb_samples -= nb_samples % 28;
  453. break;
  454. case AV_CODEC_ID_ADPCM_IMA_DK3:
  455. if (avctx->block_align > 0)
  456. buf_size = FFMIN(buf_size, avctx->block_align);
  457. nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
  458. break;
  459. case AV_CODEC_ID_ADPCM_IMA_DK4:
  460. if (avctx->block_align > 0)
  461. buf_size = FFMIN(buf_size, avctx->block_align);
  462. nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
  463. break;
  464. case AV_CODEC_ID_ADPCM_IMA_WAV:
  465. if (avctx->block_align > 0)
  466. buf_size = FFMIN(buf_size, avctx->block_align);
  467. nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
  468. break;
  469. case AV_CODEC_ID_ADPCM_MS:
  470. if (avctx->block_align > 0)
  471. buf_size = FFMIN(buf_size, avctx->block_align);
  472. nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
  473. break;
  474. case AV_CODEC_ID_ADPCM_SBPRO_2:
  475. case AV_CODEC_ID_ADPCM_SBPRO_3:
  476. case AV_CODEC_ID_ADPCM_SBPRO_4:
  477. {
  478. int samples_per_byte;
  479. switch (avctx->codec->id) {
  480. case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
  481. case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
  482. case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
  483. }
  484. if (!s->status[0].step_index) {
  485. nb_samples++;
  486. buf_size -= ch;
  487. }
  488. nb_samples += buf_size * samples_per_byte / ch;
  489. break;
  490. }
  491. case AV_CODEC_ID_ADPCM_SWF:
  492. {
  493. int buf_bits = buf_size * 8 - 2;
  494. int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
  495. int block_hdr_size = 22 * ch;
  496. int block_size = block_hdr_size + nbits * ch * 4095;
  497. int nblocks = buf_bits / block_size;
  498. int bits_left = buf_bits - nblocks * block_size;
  499. nb_samples = nblocks * 4096;
  500. if (bits_left >= block_hdr_size)
  501. nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
  502. break;
  503. }
  504. case AV_CODEC_ID_ADPCM_THP:
  505. has_coded_samples = 1;
  506. bytestream2_skip(gb, 4); // channel size
  507. *coded_samples = bytestream2_get_be32(gb);
  508. *coded_samples -= *coded_samples % 14;
  509. nb_samples = (buf_size - 80) / (8 * ch) * 14;
  510. break;
  511. case AV_CODEC_ID_ADPCM_XA:
  512. nb_samples = (buf_size / 128) * 224 / ch;
  513. break;
  514. }
  515. /* validate coded sample count */
  516. if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
  517. return AVERROR_INVALIDDATA;
  518. return nb_samples;
  519. }
  520. static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
  521. int *got_frame_ptr, AVPacket *avpkt)
  522. {
  523. AVFrame *frame = data;
  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. frame->nb_samples = nb_samples;
  543. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  544. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  545. return ret;
  546. }
  547. samples = (short *)frame->data[0];
  548. samples_p = (int16_t **)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. 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 *)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)) - 1; 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. 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. return bytestream2_tell(&gb);
  1147. }
  1148. static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
  1149. AV_SAMPLE_FMT_NONE };
  1150. static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16,
  1151. AV_SAMPLE_FMT_NONE };
  1152. static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
  1153. AV_SAMPLE_FMT_S16P,
  1154. AV_SAMPLE_FMT_NONE };
  1155. #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
  1156. AVCodec ff_ ## name_ ## _decoder = { \
  1157. .name = #name_, \
  1158. .long_name = NULL_IF_CONFIG_SMALL(long_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 = AV_CODEC_CAP_DR1, \
  1165. .sample_fmts = sample_fmts_, \
  1166. }
  1167. /* Note: Do not forget to add new entries to the Makefile as well. */
  1168. ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
  1169. ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
  1170. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
  1171. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1172. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1173. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1174. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1175. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1176. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
  1177. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
  1178. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1179. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1180. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1181. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1182. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1183. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1184. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1185. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
  1186. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
  1187. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
  1188. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1189. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1190. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1191. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
  1192. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  1193. ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
  1194. ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");