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.

1337 lines
51KB

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