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.

1339 lines
51KB

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