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.

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