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.

1853 lines
71KB

  1. /*
  2. * Copyright (c) 2001-2003 The FFmpeg project
  3. *
  4. * first version by Francois Revol (revol@free.fr)
  5. * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  6. * by Mike Melanson (melanson@pcisys.net)
  7. * CD-ROM XA ADPCM codec by BERO
  8. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  9. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  10. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  11. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  12. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  13. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  14. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  15. *
  16. * This file is part of FFmpeg.
  17. *
  18. * FFmpeg is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU Lesser General Public
  20. * License as published by the Free Software Foundation; either
  21. * version 2.1 of the License, or (at your option) any later version.
  22. *
  23. * FFmpeg is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * Lesser General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Lesser General Public
  29. * License along with FFmpeg; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  31. */
  32. #include "avcodec.h"
  33. #include "get_bits.h"
  34. #include "bytestream.h"
  35. #include "adpcm.h"
  36. #include "adpcm_data.h"
  37. #include "internal.h"
  38. /**
  39. * @file
  40. * ADPCM decoders
  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 int8_t xa_adpcm_table[5][2] = {
  59. { 0, 0 },
  60. { 60, 0 },
  61. { 115, -52 },
  62. { 98, -55 },
  63. { 122, -60 }
  64. };
  65. static const int16_t 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 int8_t 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. ADPCMChannelStatus status[14];
  82. int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
  83. int has_status;
  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_DTK:
  92. case AV_CODEC_ID_ADPCM_EA:
  93. min_channels = 2;
  94. break;
  95. case AV_CODEC_ID_ADPCM_AFC:
  96. case AV_CODEC_ID_ADPCM_EA_R1:
  97. case AV_CODEC_ID_ADPCM_EA_R2:
  98. case AV_CODEC_ID_ADPCM_EA_R3:
  99. case AV_CODEC_ID_ADPCM_EA_XAS:
  100. case AV_CODEC_ID_ADPCM_MS:
  101. max_channels = 6;
  102. break;
  103. case AV_CODEC_ID_ADPCM_MTAF:
  104. min_channels = 2;
  105. max_channels = 8;
  106. break;
  107. case AV_CODEC_ID_ADPCM_PSX:
  108. max_channels = 8;
  109. break;
  110. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  111. case AV_CODEC_ID_ADPCM_THP:
  112. case AV_CODEC_ID_ADPCM_THP_LE:
  113. max_channels = 14;
  114. break;
  115. }
  116. if (avctx->channels < min_channels || avctx->channels > max_channels) {
  117. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  118. return AVERROR(EINVAL);
  119. }
  120. switch(avctx->codec->id) {
  121. case AV_CODEC_ID_ADPCM_CT:
  122. c->status[0].step = c->status[1].step = 511;
  123. break;
  124. case AV_CODEC_ID_ADPCM_IMA_WAV:
  125. if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
  126. return AVERROR_INVALIDDATA;
  127. break;
  128. case AV_CODEC_ID_ADPCM_IMA_APC:
  129. if (avctx->extradata && avctx->extradata_size >= 8) {
  130. c->status[0].predictor = AV_RL32(avctx->extradata);
  131. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  132. }
  133. break;
  134. case AV_CODEC_ID_ADPCM_IMA_WS:
  135. if (avctx->extradata && avctx->extradata_size >= 2)
  136. c->vqa_version = AV_RL16(avctx->extradata);
  137. break;
  138. default:
  139. break;
  140. }
  141. switch(avctx->codec->id) {
  142. case AV_CODEC_ID_ADPCM_AICA:
  143. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  144. case AV_CODEC_ID_ADPCM_IMA_QT:
  145. case AV_CODEC_ID_ADPCM_IMA_WAV:
  146. case AV_CODEC_ID_ADPCM_4XM:
  147. case AV_CODEC_ID_ADPCM_XA:
  148. case AV_CODEC_ID_ADPCM_EA_R1:
  149. case AV_CODEC_ID_ADPCM_EA_R2:
  150. case AV_CODEC_ID_ADPCM_EA_R3:
  151. case AV_CODEC_ID_ADPCM_EA_XAS:
  152. case AV_CODEC_ID_ADPCM_THP:
  153. case AV_CODEC_ID_ADPCM_THP_LE:
  154. case AV_CODEC_ID_ADPCM_AFC:
  155. case AV_CODEC_ID_ADPCM_DTK:
  156. case AV_CODEC_ID_ADPCM_PSX:
  157. case AV_CODEC_ID_ADPCM_MTAF:
  158. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  159. break;
  160. case AV_CODEC_ID_ADPCM_IMA_WS:
  161. avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
  162. AV_SAMPLE_FMT_S16;
  163. break;
  164. case AV_CODEC_ID_ADPCM_MS:
  165. avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
  166. AV_SAMPLE_FMT_S16;
  167. break;
  168. default:
  169. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  170. }
  171. return 0;
  172. }
  173. static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
  174. {
  175. int delta, pred, step, add;
  176. pred = c->predictor;
  177. delta = nibble & 7;
  178. step = c->step;
  179. add = (delta * 2 + 1) * step;
  180. if (add < 0)
  181. add = add + 7;
  182. if ((nibble & 8) == 0)
  183. pred = av_clip(pred + (add >> 3), -32767, 32767);
  184. else
  185. pred = av_clip(pred - (add >> 3), -32767, 32767);
  186. switch (delta) {
  187. case 7:
  188. step *= 0x99;
  189. break;
  190. case 6:
  191. c->step = av_clip(c->step * 2, 127, 24576);
  192. c->predictor = pred;
  193. return pred;
  194. case 5:
  195. step *= 0x66;
  196. break;
  197. case 4:
  198. step *= 0x4d;
  199. break;
  200. default:
  201. step *= 0x39;
  202. break;
  203. }
  204. if (step < 0)
  205. step += 0x3f;
  206. c->step = step >> 6;
  207. c->step = av_clip(c->step, 127, 24576);
  208. c->predictor = pred;
  209. return pred;
  210. }
  211. static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
  212. {
  213. int step_index;
  214. int predictor;
  215. int sign, delta, diff, step;
  216. step = ff_adpcm_step_table[c->step_index];
  217. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  218. step_index = av_clip(step_index, 0, 88);
  219. sign = nibble & 8;
  220. delta = nibble & 7;
  221. /* perform direct multiplication instead of series of jumps proposed by
  222. * the reference ADPCM implementation since modern CPUs can do the mults
  223. * quickly enough */
  224. diff = ((2 * delta + 1) * step) >> shift;
  225. predictor = c->predictor;
  226. if (sign) predictor -= diff;
  227. else predictor += diff;
  228. c->predictor = av_clip_int16(predictor);
  229. c->step_index = step_index;
  230. return (int16_t)c->predictor;
  231. }
  232. static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
  233. {
  234. int nibble, step_index, predictor, sign, delta, diff, step, shift;
  235. shift = bps - 1;
  236. nibble = get_bits_le(gb, bps),
  237. step = ff_adpcm_step_table[c->step_index];
  238. step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
  239. step_index = av_clip(step_index, 0, 88);
  240. sign = nibble & (1 << shift);
  241. delta = av_mod_uintp2(nibble, shift);
  242. diff = ((2 * delta + 1) * step) >> shift;
  243. predictor = c->predictor;
  244. if (sign) predictor -= diff;
  245. else predictor += diff;
  246. c->predictor = av_clip_int16(predictor);
  247. c->step_index = step_index;
  248. return (int16_t)c->predictor;
  249. }
  250. static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
  251. {
  252. int step_index;
  253. int predictor;
  254. int diff, step;
  255. step = ff_adpcm_step_table[c->step_index];
  256. step_index = c->step_index + ff_adpcm_index_table[nibble];
  257. step_index = av_clip(step_index, 0, 88);
  258. diff = step >> 3;
  259. if (nibble & 4) diff += step;
  260. if (nibble & 2) diff += step >> 1;
  261. if (nibble & 1) diff += step >> 2;
  262. if (nibble & 8)
  263. predictor = c->predictor - diff;
  264. else
  265. predictor = c->predictor + diff;
  266. c->predictor = av_clip_int16(predictor);
  267. c->step_index = step_index;
  268. return c->predictor;
  269. }
  270. static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
  271. {
  272. int predictor;
  273. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  274. predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  275. c->sample2 = c->sample1;
  276. c->sample1 = av_clip_int16(predictor);
  277. c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
  278. if (c->idelta < 16) c->idelta = 16;
  279. if (c->idelta > INT_MAX/768) {
  280. av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
  281. c->idelta = INT_MAX/768;
  282. }
  283. return c->sample1;
  284. }
  285. static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
  286. {
  287. int step_index, predictor, sign, delta, diff, step;
  288. step = ff_adpcm_oki_step_table[c->step_index];
  289. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  290. step_index = av_clip(step_index, 0, 48);
  291. sign = nibble & 8;
  292. delta = nibble & 7;
  293. diff = ((2 * delta + 1) * step) >> 3;
  294. predictor = c->predictor;
  295. if (sign) predictor -= diff;
  296. else predictor += diff;
  297. c->predictor = av_clip_intp2(predictor, 11);
  298. c->step_index = step_index;
  299. return c->predictor << 4;
  300. }
  301. static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
  302. {
  303. int sign, delta, diff;
  304. int new_step;
  305. sign = nibble & 8;
  306. delta = nibble & 7;
  307. /* perform direct multiplication instead of series of jumps proposed by
  308. * the reference ADPCM implementation since modern CPUs can do the mults
  309. * quickly enough */
  310. diff = ((2 * delta + 1) * c->step) >> 3;
  311. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  312. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  313. c->predictor = av_clip_int16(c->predictor);
  314. /* calculate new step and clamp it to range 511..32767 */
  315. new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
  316. c->step = av_clip(new_step, 511, 32767);
  317. return (int16_t)c->predictor;
  318. }
  319. static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
  320. {
  321. int sign, delta, diff;
  322. sign = nibble & (1<<(size-1));
  323. delta = nibble & ((1<<(size-1))-1);
  324. diff = delta << (7 + c->step + shift);
  325. /* clamp result */
  326. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  327. /* calculate new step */
  328. if (delta >= (2*size - 3) && c->step < 3)
  329. c->step++;
  330. else if (delta == 0 && c->step > 0)
  331. c->step--;
  332. return (int16_t) c->predictor;
  333. }
  334. static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
  335. {
  336. if(!c->step) {
  337. c->predictor = 0;
  338. c->step = 127;
  339. }
  340. c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
  341. c->predictor = av_clip_int16(c->predictor);
  342. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  343. c->step = av_clip(c->step, 127, 24576);
  344. return c->predictor;
  345. }
  346. static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
  347. {
  348. c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];
  349. c->predictor = av_clip_int16(c->predictor);
  350. c->step += ff_adpcm_index_table[nibble];
  351. c->step = av_clip_uintp2(c->step, 5);
  352. return c->predictor;
  353. }
  354. static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
  355. const uint8_t *in, ADPCMChannelStatus *left,
  356. ADPCMChannelStatus *right, int channels, int sample_offset)
  357. {
  358. int i, j;
  359. int shift,filter,f0,f1;
  360. int s_1,s_2;
  361. int d,s,t;
  362. out0 += sample_offset;
  363. if (channels == 1)
  364. out1 = out0 + 28;
  365. else
  366. out1 += sample_offset;
  367. for(i=0;i<4;i++) {
  368. shift = 12 - (in[4+i*2] & 15);
  369. filter = in[4+i*2] >> 4;
  370. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  371. avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
  372. filter=0;
  373. }
  374. f0 = xa_adpcm_table[filter][0];
  375. f1 = xa_adpcm_table[filter][1];
  376. s_1 = left->sample1;
  377. s_2 = left->sample2;
  378. for(j=0;j<28;j++) {
  379. d = in[16+i+j*4];
  380. t = sign_extend(d, 4);
  381. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  382. s_2 = s_1;
  383. s_1 = av_clip_int16(s);
  384. out0[j] = s_1;
  385. }
  386. if (channels == 2) {
  387. left->sample1 = s_1;
  388. left->sample2 = s_2;
  389. s_1 = right->sample1;
  390. s_2 = right->sample2;
  391. }
  392. shift = 12 - (in[5+i*2] & 15);
  393. filter = in[5+i*2] >> 4;
  394. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  395. avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
  396. filter=0;
  397. }
  398. f0 = xa_adpcm_table[filter][0];
  399. f1 = xa_adpcm_table[filter][1];
  400. for(j=0;j<28;j++) {
  401. d = in[16+i+j*4];
  402. t = sign_extend(d >> 4, 4);
  403. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  404. s_2 = s_1;
  405. s_1 = av_clip_int16(s);
  406. out1[j] = s_1;
  407. }
  408. if (channels == 2) {
  409. right->sample1 = s_1;
  410. right->sample2 = s_2;
  411. } else {
  412. left->sample1 = s_1;
  413. left->sample2 = s_2;
  414. }
  415. out0 += 28 * (3 - channels);
  416. out1 += 28 * (3 - channels);
  417. }
  418. return 0;
  419. }
  420. static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
  421. {
  422. ADPCMDecodeContext *c = avctx->priv_data;
  423. GetBitContext gb;
  424. const int8_t *table;
  425. int k0, signmask, nb_bits, count;
  426. int size = buf_size*8;
  427. int i;
  428. init_get_bits(&gb, buf, size);
  429. //read bits & initial values
  430. nb_bits = get_bits(&gb, 2)+2;
  431. table = swf_index_tables[nb_bits-2];
  432. k0 = 1 << (nb_bits-2);
  433. signmask = 1 << (nb_bits-1);
  434. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  435. for (i = 0; i < avctx->channels; i++) {
  436. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  437. c->status[i].step_index = get_bits(&gb, 6);
  438. }
  439. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  440. int i;
  441. for (i = 0; i < avctx->channels; i++) {
  442. // similar to IMA adpcm
  443. int delta = get_bits(&gb, nb_bits);
  444. int step = ff_adpcm_step_table[c->status[i].step_index];
  445. int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  446. int k = k0;
  447. do {
  448. if (delta & k)
  449. vpdiff += step;
  450. step >>= 1;
  451. k >>= 1;
  452. } while(k);
  453. vpdiff += step;
  454. if (delta & signmask)
  455. c->status[i].predictor -= vpdiff;
  456. else
  457. c->status[i].predictor += vpdiff;
  458. c->status[i].step_index += table[delta & (~signmask)];
  459. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  460. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  461. *samples++ = c->status[i].predictor;
  462. }
  463. }
  464. }
  465. }
  466. /**
  467. * Get the number of samples that will be decoded from the packet.
  468. * In one case, this is actually the maximum number of samples possible to
  469. * decode with the given buf_size.
  470. *
  471. * @param[out] coded_samples set to the number of samples as coded in the
  472. * packet, or 0 if the codec does not encode the
  473. * number of samples in each frame.
  474. * @param[out] approx_nb_samples set to non-zero if the number of samples
  475. * returned is an approximation.
  476. */
  477. static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
  478. int buf_size, int *coded_samples, int *approx_nb_samples)
  479. {
  480. ADPCMDecodeContext *s = avctx->priv_data;
  481. int nb_samples = 0;
  482. int ch = avctx->channels;
  483. int has_coded_samples = 0;
  484. int header_size;
  485. *coded_samples = 0;
  486. *approx_nb_samples = 0;
  487. if(ch <= 0)
  488. return 0;
  489. switch (avctx->codec->id) {
  490. /* constant, only check buf_size */
  491. case AV_CODEC_ID_ADPCM_EA_XAS:
  492. if (buf_size < 76 * ch)
  493. return 0;
  494. nb_samples = 128;
  495. break;
  496. case AV_CODEC_ID_ADPCM_IMA_QT:
  497. if (buf_size < 34 * ch)
  498. return 0;
  499. nb_samples = 64;
  500. break;
  501. /* simple 4-bit adpcm */
  502. case AV_CODEC_ID_ADPCM_CT:
  503. case AV_CODEC_ID_ADPCM_IMA_APC:
  504. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  505. case AV_CODEC_ID_ADPCM_IMA_OKI:
  506. case AV_CODEC_ID_ADPCM_IMA_WS:
  507. case AV_CODEC_ID_ADPCM_YAMAHA:
  508. case AV_CODEC_ID_ADPCM_AICA:
  509. nb_samples = buf_size * 2 / ch;
  510. break;
  511. }
  512. if (nb_samples)
  513. return nb_samples;
  514. /* simple 4-bit adpcm, with header */
  515. header_size = 0;
  516. switch (avctx->codec->id) {
  517. case AV_CODEC_ID_ADPCM_4XM:
  518. case AV_CODEC_ID_ADPCM_AGM:
  519. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  520. case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
  521. case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
  522. case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
  523. }
  524. if (header_size > 0)
  525. return (buf_size - header_size) * 2 / ch;
  526. /* more complex formats */
  527. switch (avctx->codec->id) {
  528. case AV_CODEC_ID_ADPCM_EA:
  529. has_coded_samples = 1;
  530. *coded_samples = bytestream2_get_le32(gb);
  531. *coded_samples -= *coded_samples % 28;
  532. nb_samples = (buf_size - 12) / 30 * 28;
  533. break;
  534. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  535. has_coded_samples = 1;
  536. *coded_samples = bytestream2_get_le32(gb);
  537. nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
  538. break;
  539. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  540. nb_samples = (buf_size - ch) / ch * 2;
  541. break;
  542. case AV_CODEC_ID_ADPCM_EA_R1:
  543. case AV_CODEC_ID_ADPCM_EA_R2:
  544. case AV_CODEC_ID_ADPCM_EA_R3:
  545. /* maximum number of samples */
  546. /* has internal offsets and a per-frame switch to signal raw 16-bit */
  547. has_coded_samples = 1;
  548. switch (avctx->codec->id) {
  549. case AV_CODEC_ID_ADPCM_EA_R1:
  550. header_size = 4 + 9 * ch;
  551. *coded_samples = bytestream2_get_le32(gb);
  552. break;
  553. case AV_CODEC_ID_ADPCM_EA_R2:
  554. header_size = 4 + 5 * ch;
  555. *coded_samples = bytestream2_get_le32(gb);
  556. break;
  557. case AV_CODEC_ID_ADPCM_EA_R3:
  558. header_size = 4 + 5 * ch;
  559. *coded_samples = bytestream2_get_be32(gb);
  560. break;
  561. }
  562. *coded_samples -= *coded_samples % 28;
  563. nb_samples = (buf_size - header_size) * 2 / ch;
  564. nb_samples -= nb_samples % 28;
  565. *approx_nb_samples = 1;
  566. break;
  567. case AV_CODEC_ID_ADPCM_IMA_DK3:
  568. if (avctx->block_align > 0)
  569. buf_size = FFMIN(buf_size, avctx->block_align);
  570. nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
  571. break;
  572. case AV_CODEC_ID_ADPCM_IMA_DK4:
  573. if (avctx->block_align > 0)
  574. buf_size = FFMIN(buf_size, avctx->block_align);
  575. if (buf_size < 4 * ch)
  576. return AVERROR_INVALIDDATA;
  577. nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
  578. break;
  579. case AV_CODEC_ID_ADPCM_IMA_RAD:
  580. if (avctx->block_align > 0)
  581. buf_size = FFMIN(buf_size, avctx->block_align);
  582. nb_samples = (buf_size - 4 * ch) * 2 / ch;
  583. break;
  584. case AV_CODEC_ID_ADPCM_IMA_WAV:
  585. {
  586. int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
  587. int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
  588. if (avctx->block_align > 0)
  589. buf_size = FFMIN(buf_size, avctx->block_align);
  590. if (buf_size < 4 * ch)
  591. return AVERROR_INVALIDDATA;
  592. nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
  593. break;
  594. }
  595. case AV_CODEC_ID_ADPCM_MS:
  596. if (avctx->block_align > 0)
  597. buf_size = FFMIN(buf_size, avctx->block_align);
  598. nb_samples = (buf_size - 6 * ch) * 2 / ch;
  599. break;
  600. case AV_CODEC_ID_ADPCM_MTAF:
  601. if (avctx->block_align > 0)
  602. buf_size = FFMIN(buf_size, avctx->block_align);
  603. nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
  604. break;
  605. case AV_CODEC_ID_ADPCM_SBPRO_2:
  606. case AV_CODEC_ID_ADPCM_SBPRO_3:
  607. case AV_CODEC_ID_ADPCM_SBPRO_4:
  608. {
  609. int samples_per_byte;
  610. switch (avctx->codec->id) {
  611. case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
  612. case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
  613. case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
  614. }
  615. if (!s->status[0].step_index) {
  616. if (buf_size < ch)
  617. return AVERROR_INVALIDDATA;
  618. nb_samples++;
  619. buf_size -= ch;
  620. }
  621. nb_samples += buf_size * samples_per_byte / ch;
  622. break;
  623. }
  624. case AV_CODEC_ID_ADPCM_SWF:
  625. {
  626. int buf_bits = buf_size * 8 - 2;
  627. int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
  628. int block_hdr_size = 22 * ch;
  629. int block_size = block_hdr_size + nbits * ch * 4095;
  630. int nblocks = buf_bits / block_size;
  631. int bits_left = buf_bits - nblocks * block_size;
  632. nb_samples = nblocks * 4096;
  633. if (bits_left >= block_hdr_size)
  634. nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
  635. break;
  636. }
  637. case AV_CODEC_ID_ADPCM_THP:
  638. case AV_CODEC_ID_ADPCM_THP_LE:
  639. if (avctx->extradata) {
  640. nb_samples = buf_size * 14 / (8 * ch);
  641. break;
  642. }
  643. has_coded_samples = 1;
  644. bytestream2_skip(gb, 4); // channel size
  645. *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
  646. bytestream2_get_le32(gb) :
  647. bytestream2_get_be32(gb);
  648. buf_size -= 8 + 36 * ch;
  649. buf_size /= ch;
  650. nb_samples = buf_size / 8 * 14;
  651. if (buf_size % 8 > 1)
  652. nb_samples += (buf_size % 8 - 1) * 2;
  653. *approx_nb_samples = 1;
  654. break;
  655. case AV_CODEC_ID_ADPCM_AFC:
  656. nb_samples = buf_size / (9 * ch) * 16;
  657. break;
  658. case AV_CODEC_ID_ADPCM_XA:
  659. nb_samples = (buf_size / 128) * 224 / ch;
  660. break;
  661. case AV_CODEC_ID_ADPCM_DTK:
  662. case AV_CODEC_ID_ADPCM_PSX:
  663. nb_samples = buf_size / (16 * ch) * 28;
  664. break;
  665. }
  666. /* validate coded sample count */
  667. if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
  668. return AVERROR_INVALIDDATA;
  669. return nb_samples;
  670. }
  671. static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
  672. int *got_frame_ptr, AVPacket *avpkt)
  673. {
  674. AVFrame *frame = data;
  675. const uint8_t *buf = avpkt->data;
  676. int buf_size = avpkt->size;
  677. ADPCMDecodeContext *c = avctx->priv_data;
  678. ADPCMChannelStatus *cs;
  679. int n, m, channel, i;
  680. int16_t *samples;
  681. int16_t **samples_p;
  682. int st; /* stereo */
  683. int count1, count2;
  684. int nb_samples, coded_samples, approx_nb_samples, ret;
  685. GetByteContext gb;
  686. bytestream2_init(&gb, buf, buf_size);
  687. nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
  688. if (nb_samples <= 0) {
  689. av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
  690. return AVERROR_INVALIDDATA;
  691. }
  692. /* get output buffer */
  693. frame->nb_samples = nb_samples;
  694. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  695. return ret;
  696. samples = (int16_t *)frame->data[0];
  697. samples_p = (int16_t **)frame->extended_data;
  698. /* use coded_samples when applicable */
  699. /* it is always <= nb_samples, so the output buffer will be large enough */
  700. if (coded_samples) {
  701. if (!approx_nb_samples && coded_samples != nb_samples)
  702. av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
  703. frame->nb_samples = nb_samples = coded_samples;
  704. }
  705. st = avctx->channels == 2 ? 1 : 0;
  706. switch(avctx->codec->id) {
  707. case AV_CODEC_ID_ADPCM_IMA_QT:
  708. /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
  709. Channel data is interleaved per-chunk. */
  710. for (channel = 0; channel < avctx->channels; channel++) {
  711. int predictor;
  712. int step_index;
  713. cs = &(c->status[channel]);
  714. /* (pppppp) (piiiiiii) */
  715. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  716. predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  717. step_index = predictor & 0x7F;
  718. predictor &= ~0x7F;
  719. if (cs->step_index == step_index) {
  720. int diff = predictor - cs->predictor;
  721. if (diff < 0)
  722. diff = - diff;
  723. if (diff > 0x7f)
  724. goto update;
  725. } else {
  726. update:
  727. cs->step_index = step_index;
  728. cs->predictor = predictor;
  729. }
  730. if (cs->step_index > 88u){
  731. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  732. channel, cs->step_index);
  733. return AVERROR_INVALIDDATA;
  734. }
  735. samples = samples_p[channel];
  736. for (m = 0; m < 64; m += 2) {
  737. int byte = bytestream2_get_byteu(&gb);
  738. samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
  739. samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
  740. }
  741. }
  742. break;
  743. case AV_CODEC_ID_ADPCM_IMA_WAV:
  744. for(i=0; i<avctx->channels; i++){
  745. cs = &(c->status[i]);
  746. cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
  747. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  748. if (cs->step_index > 88u){
  749. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  750. i, cs->step_index);
  751. return AVERROR_INVALIDDATA;
  752. }
  753. }
  754. if (avctx->bits_per_coded_sample != 4) {
  755. int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
  756. int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
  757. uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
  758. GetBitContext g;
  759. for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
  760. for (i = 0; i < avctx->channels; i++) {
  761. int j;
  762. cs = &c->status[i];
  763. samples = &samples_p[i][1 + n * samples_per_block];
  764. for (j = 0; j < block_size; j++) {
  765. temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
  766. (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
  767. }
  768. ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
  769. if (ret < 0)
  770. return ret;
  771. for (m = 0; m < samples_per_block; m++) {
  772. samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
  773. avctx->bits_per_coded_sample);
  774. }
  775. }
  776. }
  777. bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
  778. } else {
  779. for (n = 0; n < (nb_samples - 1) / 8; n++) {
  780. for (i = 0; i < avctx->channels; i++) {
  781. cs = &c->status[i];
  782. samples = &samples_p[i][1 + n * 8];
  783. for (m = 0; m < 8; m += 2) {
  784. int v = bytestream2_get_byteu(&gb);
  785. samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  786. samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  787. }
  788. }
  789. }
  790. }
  791. break;
  792. case AV_CODEC_ID_ADPCM_4XM:
  793. for (i = 0; i < avctx->channels; i++)
  794. c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  795. for (i = 0; i < avctx->channels; i++) {
  796. c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  797. if (c->status[i].step_index > 88u) {
  798. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  799. i, c->status[i].step_index);
  800. return AVERROR_INVALIDDATA;
  801. }
  802. }
  803. for (i = 0; i < avctx->channels; i++) {
  804. samples = (int16_t *)frame->data[i];
  805. cs = &c->status[i];
  806. for (n = nb_samples >> 1; n > 0; n--) {
  807. int v = bytestream2_get_byteu(&gb);
  808. *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
  809. *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
  810. }
  811. }
  812. break;
  813. case AV_CODEC_ID_ADPCM_AGM:
  814. for (i = 0; i < avctx->channels; i++)
  815. c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  816. for (i = 0; i < avctx->channels; i++)
  817. c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
  818. for (n = 0; n < nb_samples >> (1 - st); n++) {
  819. int v = bytestream2_get_byteu(&gb);
  820. *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
  821. *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 );
  822. }
  823. break;
  824. case AV_CODEC_ID_ADPCM_MS:
  825. {
  826. int block_predictor;
  827. if (avctx->channels > 2) {
  828. for (channel = 0; channel < avctx->channels; channel++) {
  829. samples = samples_p[channel];
  830. block_predictor = bytestream2_get_byteu(&gb);
  831. if (block_predictor > 6) {
  832. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
  833. channel, block_predictor);
  834. return AVERROR_INVALIDDATA;
  835. }
  836. c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  837. c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  838. c->status[channel].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  839. c->status[channel].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  840. c->status[channel].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  841. *samples++ = c->status[channel].sample2;
  842. *samples++ = c->status[channel].sample1;
  843. for(n = (nb_samples - 2) >> 1; n > 0; n--) {
  844. int byte = bytestream2_get_byteu(&gb);
  845. *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4 );
  846. *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
  847. }
  848. }
  849. } else {
  850. block_predictor = bytestream2_get_byteu(&gb);
  851. if (block_predictor > 6) {
  852. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
  853. block_predictor);
  854. return AVERROR_INVALIDDATA;
  855. }
  856. c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  857. c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  858. if (st) {
  859. block_predictor = bytestream2_get_byteu(&gb);
  860. if (block_predictor > 6) {
  861. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
  862. block_predictor);
  863. return AVERROR_INVALIDDATA;
  864. }
  865. c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  866. c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  867. }
  868. c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  869. if (st){
  870. c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  871. }
  872. c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  873. if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  874. c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  875. if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  876. *samples++ = c->status[0].sample2;
  877. if (st) *samples++ = c->status[1].sample2;
  878. *samples++ = c->status[0].sample1;
  879. if (st) *samples++ = c->status[1].sample1;
  880. for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
  881. int byte = bytestream2_get_byteu(&gb);
  882. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
  883. *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
  884. }
  885. }
  886. break;
  887. }
  888. case AV_CODEC_ID_ADPCM_MTAF:
  889. for (channel = 0; channel < avctx->channels; channel+=2) {
  890. bytestream2_skipu(&gb, 4);
  891. c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
  892. c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
  893. c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  894. bytestream2_skipu(&gb, 2);
  895. c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  896. bytestream2_skipu(&gb, 2);
  897. for (n = 0; n < nb_samples; n+=2) {
  898. int v = bytestream2_get_byteu(&gb);
  899. samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
  900. samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 );
  901. }
  902. for (n = 0; n < nb_samples; n+=2) {
  903. int v = bytestream2_get_byteu(&gb);
  904. samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
  905. samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 );
  906. }
  907. }
  908. break;
  909. case AV_CODEC_ID_ADPCM_IMA_DK4:
  910. for (channel = 0; channel < avctx->channels; channel++) {
  911. cs = &c->status[channel];
  912. cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
  913. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  914. if (cs->step_index > 88u){
  915. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  916. channel, cs->step_index);
  917. return AVERROR_INVALIDDATA;
  918. }
  919. }
  920. for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
  921. int v = bytestream2_get_byteu(&gb);
  922. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
  923. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  924. }
  925. break;
  926. case AV_CODEC_ID_ADPCM_IMA_DK3:
  927. {
  928. int last_byte = 0;
  929. int nibble;
  930. int decode_top_nibble_next = 0;
  931. int diff_channel;
  932. const int16_t *samples_end = samples + avctx->channels * nb_samples;
  933. bytestream2_skipu(&gb, 10);
  934. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  935. c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  936. c->status[0].step_index = bytestream2_get_byteu(&gb);
  937. c->status[1].step_index = bytestream2_get_byteu(&gb);
  938. if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
  939. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
  940. c->status[0].step_index, c->status[1].step_index);
  941. return AVERROR_INVALIDDATA;
  942. }
  943. /* sign extend the predictors */
  944. diff_channel = c->status[1].predictor;
  945. /* DK3 ADPCM support macro */
  946. #define DK3_GET_NEXT_NIBBLE() \
  947. if (decode_top_nibble_next) { \
  948. nibble = last_byte >> 4; \
  949. decode_top_nibble_next = 0; \
  950. } else { \
  951. last_byte = bytestream2_get_byteu(&gb); \
  952. nibble = last_byte & 0x0F; \
  953. decode_top_nibble_next = 1; \
  954. }
  955. while (samples < samples_end) {
  956. /* for this algorithm, c->status[0] is the sum channel and
  957. * c->status[1] is the diff channel */
  958. /* process the first predictor of the sum channel */
  959. DK3_GET_NEXT_NIBBLE();
  960. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  961. /* process the diff channel predictor */
  962. DK3_GET_NEXT_NIBBLE();
  963. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  964. /* process the first pair of stereo PCM samples */
  965. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  966. *samples++ = c->status[0].predictor + c->status[1].predictor;
  967. *samples++ = c->status[0].predictor - c->status[1].predictor;
  968. /* process the second predictor of the sum channel */
  969. DK3_GET_NEXT_NIBBLE();
  970. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  971. /* process the second pair of stereo PCM samples */
  972. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  973. *samples++ = c->status[0].predictor + c->status[1].predictor;
  974. *samples++ = c->status[0].predictor - c->status[1].predictor;
  975. }
  976. if ((bytestream2_tell(&gb) & 1))
  977. bytestream2_skip(&gb, 1);
  978. break;
  979. }
  980. case AV_CODEC_ID_ADPCM_IMA_ISS:
  981. for (channel = 0; channel < avctx->channels; channel++) {
  982. cs = &c->status[channel];
  983. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  984. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  985. if (cs->step_index > 88u){
  986. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  987. channel, cs->step_index);
  988. return AVERROR_INVALIDDATA;
  989. }
  990. }
  991. for (n = nb_samples >> (1 - st); n > 0; n--) {
  992. int v1, v2;
  993. int v = bytestream2_get_byteu(&gb);
  994. /* nibbles are swapped for mono */
  995. if (st) {
  996. v1 = v >> 4;
  997. v2 = v & 0x0F;
  998. } else {
  999. v2 = v >> 4;
  1000. v1 = v & 0x0F;
  1001. }
  1002. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
  1003. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
  1004. }
  1005. break;
  1006. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  1007. for (channel = 0; channel < avctx->channels; channel++) {
  1008. cs = &c->status[channel];
  1009. samples = samples_p[channel];
  1010. bytestream2_skip(&gb, 4);
  1011. for (n = 0; n < nb_samples; n += 2) {
  1012. int v = bytestream2_get_byteu(&gb);
  1013. *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  1014. *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  1015. }
  1016. }
  1017. break;
  1018. case AV_CODEC_ID_ADPCM_IMA_APC:
  1019. while (bytestream2_get_bytes_left(&gb) > 0) {
  1020. int v = bytestream2_get_byteu(&gb);
  1021. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
  1022. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  1023. }
  1024. break;
  1025. case AV_CODEC_ID_ADPCM_IMA_OKI:
  1026. while (bytestream2_get_bytes_left(&gb) > 0) {
  1027. int v = bytestream2_get_byteu(&gb);
  1028. *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
  1029. *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
  1030. }
  1031. break;
  1032. case AV_CODEC_ID_ADPCM_IMA_RAD:
  1033. for (channel = 0; channel < avctx->channels; channel++) {
  1034. cs = &c->status[channel];
  1035. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  1036. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  1037. if (cs->step_index > 88u){
  1038. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  1039. channel, cs->step_index);
  1040. return AVERROR_INVALIDDATA;
  1041. }
  1042. }
  1043. for (n = 0; n < nb_samples / 2; n++) {
  1044. int byte[2];
  1045. byte[0] = bytestream2_get_byteu(&gb);
  1046. if (st)
  1047. byte[1] = bytestream2_get_byteu(&gb);
  1048. for(channel = 0; channel < avctx->channels; channel++) {
  1049. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
  1050. }
  1051. for(channel = 0; channel < avctx->channels; channel++) {
  1052. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
  1053. }
  1054. }
  1055. break;
  1056. case AV_CODEC_ID_ADPCM_IMA_WS:
  1057. if (c->vqa_version == 3) {
  1058. for (channel = 0; channel < avctx->channels; channel++) {
  1059. int16_t *smp = samples_p[channel];
  1060. for (n = nb_samples / 2; n > 0; n--) {
  1061. int v = bytestream2_get_byteu(&gb);
  1062. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  1063. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  1064. }
  1065. }
  1066. } else {
  1067. for (n = nb_samples / 2; n > 0; n--) {
  1068. for (channel = 0; channel < avctx->channels; channel++) {
  1069. int v = bytestream2_get_byteu(&gb);
  1070. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  1071. samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  1072. }
  1073. samples += avctx->channels;
  1074. }
  1075. }
  1076. bytestream2_seek(&gb, 0, SEEK_END);
  1077. break;
  1078. case AV_CODEC_ID_ADPCM_XA:
  1079. {
  1080. int16_t *out0 = samples_p[0];
  1081. int16_t *out1 = samples_p[1];
  1082. int samples_per_block = 28 * (3 - avctx->channels) * 4;
  1083. int sample_offset = 0;
  1084. int bytes_remaining;
  1085. while (bytestream2_get_bytes_left(&gb) >= 128) {
  1086. if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
  1087. &c->status[0], &c->status[1],
  1088. avctx->channels, sample_offset)) < 0)
  1089. return ret;
  1090. bytestream2_skipu(&gb, 128);
  1091. sample_offset += samples_per_block;
  1092. }
  1093. /* Less than a full block of data left, e.g. when reading from
  1094. * 2324 byte per sector XA; the remainder is padding */
  1095. bytes_remaining = bytestream2_get_bytes_left(&gb);
  1096. if (bytes_remaining > 0) {
  1097. bytestream2_skip(&gb, bytes_remaining);
  1098. }
  1099. break;
  1100. }
  1101. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  1102. for (i=0; i<=st; i++) {
  1103. c->status[i].step_index = bytestream2_get_le32u(&gb);
  1104. if (c->status[i].step_index > 88u) {
  1105. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  1106. i, c->status[i].step_index);
  1107. return AVERROR_INVALIDDATA;
  1108. }
  1109. }
  1110. for (i=0; i<=st; i++)
  1111. c->status[i].predictor = bytestream2_get_le32u(&gb);
  1112. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1113. int byte = bytestream2_get_byteu(&gb);
  1114. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
  1115. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
  1116. }
  1117. break;
  1118. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1119. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1120. int byte = bytestream2_get_byteu(&gb);
  1121. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
  1122. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
  1123. }
  1124. break;
  1125. case AV_CODEC_ID_ADPCM_EA:
  1126. {
  1127. int previous_left_sample, previous_right_sample;
  1128. int current_left_sample, current_right_sample;
  1129. int next_left_sample, next_right_sample;
  1130. int coeff1l, coeff2l, coeff1r, coeff2r;
  1131. int shift_left, shift_right;
  1132. /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
  1133. each coding 28 stereo samples. */
  1134. if(avctx->channels != 2)
  1135. return AVERROR_INVALIDDATA;
  1136. current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  1137. previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  1138. current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  1139. previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  1140. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  1141. int byte = bytestream2_get_byteu(&gb);
  1142. coeff1l = ea_adpcm_table[ byte >> 4 ];
  1143. coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
  1144. coeff1r = ea_adpcm_table[ byte & 0x0F];
  1145. coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
  1146. byte = bytestream2_get_byteu(&gb);
  1147. shift_left = 20 - (byte >> 4);
  1148. shift_right = 20 - (byte & 0x0F);
  1149. for (count2 = 0; count2 < 28; count2++) {
  1150. byte = bytestream2_get_byteu(&gb);
  1151. next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
  1152. next_right_sample = sign_extend(byte, 4) << shift_right;
  1153. next_left_sample = (next_left_sample +
  1154. (current_left_sample * coeff1l) +
  1155. (previous_left_sample * coeff2l) + 0x80) >> 8;
  1156. next_right_sample = (next_right_sample +
  1157. (current_right_sample * coeff1r) +
  1158. (previous_right_sample * coeff2r) + 0x80) >> 8;
  1159. previous_left_sample = current_left_sample;
  1160. current_left_sample = av_clip_int16(next_left_sample);
  1161. previous_right_sample = current_right_sample;
  1162. current_right_sample = av_clip_int16(next_right_sample);
  1163. *samples++ = current_left_sample;
  1164. *samples++ = current_right_sample;
  1165. }
  1166. }
  1167. bytestream2_skip(&gb, 2); // Skip terminating 0x0000
  1168. break;
  1169. }
  1170. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  1171. {
  1172. int coeff[2][2], shift[2];
  1173. for(channel = 0; channel < avctx->channels; channel++) {
  1174. int byte = bytestream2_get_byteu(&gb);
  1175. for (i=0; i<2; i++)
  1176. coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
  1177. shift[channel] = 20 - (byte & 0x0F);
  1178. }
  1179. for (count1 = 0; count1 < nb_samples / 2; count1++) {
  1180. int byte[2];
  1181. byte[0] = bytestream2_get_byteu(&gb);
  1182. if (st) byte[1] = bytestream2_get_byteu(&gb);
  1183. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  1184. for(channel = 0; channel < avctx->channels; channel++) {
  1185. int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
  1186. sample = (sample +
  1187. c->status[channel].sample1 * coeff[channel][0] +
  1188. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  1189. c->status[channel].sample2 = c->status[channel].sample1;
  1190. c->status[channel].sample1 = av_clip_int16(sample);
  1191. *samples++ = c->status[channel].sample1;
  1192. }
  1193. }
  1194. }
  1195. bytestream2_seek(&gb, 0, SEEK_END);
  1196. break;
  1197. }
  1198. case AV_CODEC_ID_ADPCM_EA_R1:
  1199. case AV_CODEC_ID_ADPCM_EA_R2:
  1200. case AV_CODEC_ID_ADPCM_EA_R3: {
  1201. /* channel numbering
  1202. 2chan: 0=fl, 1=fr
  1203. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  1204. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  1205. const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
  1206. int previous_sample, current_sample, next_sample;
  1207. int coeff1, coeff2;
  1208. int shift;
  1209. unsigned int channel;
  1210. uint16_t *samplesC;
  1211. int count = 0;
  1212. int offsets[6];
  1213. for (channel=0; channel<avctx->channels; channel++)
  1214. offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
  1215. bytestream2_get_le32(&gb)) +
  1216. (avctx->channels + 1) * 4;
  1217. for (channel=0; channel<avctx->channels; channel++) {
  1218. bytestream2_seek(&gb, offsets[channel], SEEK_SET);
  1219. samplesC = samples_p[channel];
  1220. if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
  1221. current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  1222. previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  1223. } else {
  1224. current_sample = c->status[channel].predictor;
  1225. previous_sample = c->status[channel].prev_sample;
  1226. }
  1227. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  1228. int byte = bytestream2_get_byte(&gb);
  1229. if (byte == 0xEE) { /* only seen in R2 and R3 */
  1230. current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  1231. previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  1232. for (count2=0; count2<28; count2++)
  1233. *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
  1234. } else {
  1235. coeff1 = ea_adpcm_table[ byte >> 4 ];
  1236. coeff2 = ea_adpcm_table[(byte >> 4) + 4];
  1237. shift = 20 - (byte & 0x0F);
  1238. for (count2=0; count2<28; count2++) {
  1239. if (count2 & 1)
  1240. next_sample = sign_extend(byte, 4) << shift;
  1241. else {
  1242. byte = bytestream2_get_byte(&gb);
  1243. next_sample = sign_extend(byte >> 4, 4) << shift;
  1244. }
  1245. next_sample += (current_sample * coeff1) +
  1246. (previous_sample * coeff2);
  1247. next_sample = av_clip_int16(next_sample >> 8);
  1248. previous_sample = current_sample;
  1249. current_sample = next_sample;
  1250. *samplesC++ = current_sample;
  1251. }
  1252. }
  1253. }
  1254. if (!count) {
  1255. count = count1;
  1256. } else if (count != count1) {
  1257. av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
  1258. count = FFMAX(count, count1);
  1259. }
  1260. if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
  1261. c->status[channel].predictor = current_sample;
  1262. c->status[channel].prev_sample = previous_sample;
  1263. }
  1264. }
  1265. frame->nb_samples = count * 28;
  1266. bytestream2_seek(&gb, 0, SEEK_END);
  1267. break;
  1268. }
  1269. case AV_CODEC_ID_ADPCM_EA_XAS:
  1270. for (channel=0; channel<avctx->channels; channel++) {
  1271. int coeff[2][4], shift[4];
  1272. int16_t *s = samples_p[channel];
  1273. for (n = 0; n < 4; n++, s += 32) {
  1274. int val = sign_extend(bytestream2_get_le16u(&gb), 16);
  1275. for (i=0; i<2; i++)
  1276. coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
  1277. s[0] = val & ~0x0F;
  1278. val = sign_extend(bytestream2_get_le16u(&gb), 16);
  1279. shift[n] = 20 - (val & 0x0F);
  1280. s[1] = val & ~0x0F;
  1281. }
  1282. for (m=2; m<32; m+=2) {
  1283. s = &samples_p[channel][m];
  1284. for (n = 0; n < 4; n++, s += 32) {
  1285. int level, pred;
  1286. int byte = bytestream2_get_byteu(&gb);
  1287. level = sign_extend(byte >> 4, 4) << shift[n];
  1288. pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
  1289. s[0] = av_clip_int16((level + pred + 0x80) >> 8);
  1290. level = sign_extend(byte, 4) << shift[n];
  1291. pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
  1292. s[1] = av_clip_int16((level + pred + 0x80) >> 8);
  1293. }
  1294. }
  1295. }
  1296. break;
  1297. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1298. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  1299. c->status[0].step_index = bytestream2_get_byteu(&gb);
  1300. bytestream2_skipu(&gb, 5);
  1301. if (c->status[0].step_index > 88u) {
  1302. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1303. c->status[0].step_index);
  1304. return AVERROR_INVALIDDATA;
  1305. }
  1306. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1307. int v = bytestream2_get_byteu(&gb);
  1308. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
  1309. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
  1310. }
  1311. break;
  1312. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1313. for (i = 0; i < avctx->channels; i++) {
  1314. c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  1315. c->status[i].step_index = bytestream2_get_byteu(&gb);
  1316. bytestream2_skipu(&gb, 1);
  1317. if (c->status[i].step_index > 88u) {
  1318. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1319. c->status[i].step_index);
  1320. return AVERROR_INVALIDDATA;
  1321. }
  1322. }
  1323. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1324. int v = bytestream2_get_byteu(&gb);
  1325. *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
  1326. *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
  1327. }
  1328. break;
  1329. case AV_CODEC_ID_ADPCM_CT:
  1330. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1331. int v = bytestream2_get_byteu(&gb);
  1332. *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
  1333. *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
  1334. }
  1335. break;
  1336. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1337. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1338. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1339. if (!c->status[0].step_index) {
  1340. /* the first byte is a raw sample */
  1341. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1342. if (st)
  1343. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1344. c->status[0].step_index = 1;
  1345. nb_samples--;
  1346. }
  1347. if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
  1348. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1349. int byte = bytestream2_get_byteu(&gb);
  1350. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1351. byte >> 4, 4, 0);
  1352. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1353. byte & 0x0F, 4, 0);
  1354. }
  1355. } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
  1356. for (n = (nb_samples<<st) / 3; n > 0; n--) {
  1357. int byte = bytestream2_get_byteu(&gb);
  1358. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1359. byte >> 5 , 3, 0);
  1360. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1361. (byte >> 2) & 0x07, 3, 0);
  1362. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1363. byte & 0x03, 2, 0);
  1364. }
  1365. } else {
  1366. for (n = nb_samples >> (2 - st); n > 0; n--) {
  1367. int byte = bytestream2_get_byteu(&gb);
  1368. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1369. byte >> 6 , 2, 2);
  1370. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1371. (byte >> 4) & 0x03, 2, 2);
  1372. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1373. (byte >> 2) & 0x03, 2, 2);
  1374. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1375. byte & 0x03, 2, 2);
  1376. }
  1377. }
  1378. break;
  1379. case AV_CODEC_ID_ADPCM_SWF:
  1380. adpcm_swf_decode(avctx, buf, buf_size, samples);
  1381. bytestream2_seek(&gb, 0, SEEK_END);
  1382. break;
  1383. case AV_CODEC_ID_ADPCM_YAMAHA:
  1384. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1385. int v = bytestream2_get_byteu(&gb);
  1386. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
  1387. *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
  1388. }
  1389. break;
  1390. case AV_CODEC_ID_ADPCM_AICA:
  1391. if (!c->has_status) {
  1392. for (channel = 0; channel < avctx->channels; channel++)
  1393. c->status[channel].step = 0;
  1394. c->has_status = 1;
  1395. }
  1396. for (channel = 0; channel < avctx->channels; channel++) {
  1397. samples = samples_p[channel];
  1398. for (n = nb_samples >> 1; n > 0; n--) {
  1399. int v = bytestream2_get_byteu(&gb);
  1400. *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
  1401. *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
  1402. }
  1403. }
  1404. break;
  1405. case AV_CODEC_ID_ADPCM_AFC:
  1406. {
  1407. int samples_per_block;
  1408. int blocks;
  1409. if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
  1410. samples_per_block = avctx->extradata[0] / 16;
  1411. blocks = nb_samples / avctx->extradata[0];
  1412. } else {
  1413. samples_per_block = nb_samples / 16;
  1414. blocks = 1;
  1415. }
  1416. for (m = 0; m < blocks; m++) {
  1417. for (channel = 0; channel < avctx->channels; channel++) {
  1418. int prev1 = c->status[channel].sample1;
  1419. int prev2 = c->status[channel].sample2;
  1420. samples = samples_p[channel] + m * 16;
  1421. /* Read in every sample for this channel. */
  1422. for (i = 0; i < samples_per_block; i++) {
  1423. int byte = bytestream2_get_byteu(&gb);
  1424. int scale = 1 << (byte >> 4);
  1425. int index = byte & 0xf;
  1426. int factor1 = ff_adpcm_afc_coeffs[0][index];
  1427. int factor2 = ff_adpcm_afc_coeffs[1][index];
  1428. /* Decode 16 samples. */
  1429. for (n = 0; n < 16; n++) {
  1430. int32_t sampledat;
  1431. if (n & 1) {
  1432. sampledat = sign_extend(byte, 4);
  1433. } else {
  1434. byte = bytestream2_get_byteu(&gb);
  1435. sampledat = sign_extend(byte >> 4, 4);
  1436. }
  1437. sampledat = ((prev1 * factor1 + prev2 * factor2) +
  1438. ((sampledat * scale) << 11)) >> 11;
  1439. *samples = av_clip_int16(sampledat);
  1440. prev2 = prev1;
  1441. prev1 = *samples++;
  1442. }
  1443. }
  1444. c->status[channel].sample1 = prev1;
  1445. c->status[channel].sample2 = prev2;
  1446. }
  1447. }
  1448. bytestream2_seek(&gb, 0, SEEK_END);
  1449. break;
  1450. }
  1451. case AV_CODEC_ID_ADPCM_THP:
  1452. case AV_CODEC_ID_ADPCM_THP_LE:
  1453. {
  1454. int table[14][16];
  1455. int ch;
  1456. #define THP_GET16(g) \
  1457. sign_extend( \
  1458. avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
  1459. bytestream2_get_le16u(&(g)) : \
  1460. bytestream2_get_be16u(&(g)), 16)
  1461. if (avctx->extradata) {
  1462. GetByteContext tb;
  1463. if (avctx->extradata_size < 32 * avctx->channels) {
  1464. av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
  1465. return AVERROR_INVALIDDATA;
  1466. }
  1467. bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
  1468. for (i = 0; i < avctx->channels; i++)
  1469. for (n = 0; n < 16; n++)
  1470. table[i][n] = THP_GET16(tb);
  1471. } else {
  1472. for (i = 0; i < avctx->channels; i++)
  1473. for (n = 0; n < 16; n++)
  1474. table[i][n] = THP_GET16(gb);
  1475. if (!c->has_status) {
  1476. /* Initialize the previous sample. */
  1477. for (i = 0; i < avctx->channels; i++) {
  1478. c->status[i].sample1 = THP_GET16(gb);
  1479. c->status[i].sample2 = THP_GET16(gb);
  1480. }
  1481. c->has_status = 1;
  1482. } else {
  1483. bytestream2_skip(&gb, avctx->channels * 4);
  1484. }
  1485. }
  1486. for (ch = 0; ch < avctx->channels; ch++) {
  1487. samples = samples_p[ch];
  1488. /* Read in every sample for this channel. */
  1489. for (i = 0; i < (nb_samples + 13) / 14; i++) {
  1490. int byte = bytestream2_get_byteu(&gb);
  1491. int index = (byte >> 4) & 7;
  1492. unsigned int exp = byte & 0x0F;
  1493. int factor1 = table[ch][index * 2];
  1494. int factor2 = table[ch][index * 2 + 1];
  1495. /* Decode 14 samples. */
  1496. for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
  1497. int32_t sampledat;
  1498. if (n & 1) {
  1499. sampledat = sign_extend(byte, 4);
  1500. } else {
  1501. byte = bytestream2_get_byteu(&gb);
  1502. sampledat = sign_extend(byte >> 4, 4);
  1503. }
  1504. sampledat = ((c->status[ch].sample1 * factor1
  1505. + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
  1506. *samples = av_clip_int16(sampledat);
  1507. c->status[ch].sample2 = c->status[ch].sample1;
  1508. c->status[ch].sample1 = *samples++;
  1509. }
  1510. }
  1511. }
  1512. break;
  1513. }
  1514. case AV_CODEC_ID_ADPCM_DTK:
  1515. for (channel = 0; channel < avctx->channels; channel++) {
  1516. samples = samples_p[channel];
  1517. /* Read in every sample for this channel. */
  1518. for (i = 0; i < nb_samples / 28; i++) {
  1519. int byte, header;
  1520. if (channel)
  1521. bytestream2_skipu(&gb, 1);
  1522. header = bytestream2_get_byteu(&gb);
  1523. bytestream2_skipu(&gb, 3 - channel);
  1524. /* Decode 28 samples. */
  1525. for (n = 0; n < 28; n++) {
  1526. int32_t sampledat, prev;
  1527. switch (header >> 4) {
  1528. case 1:
  1529. prev = (c->status[channel].sample1 * 0x3c);
  1530. break;
  1531. case 2:
  1532. prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
  1533. break;
  1534. case 3:
  1535. prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
  1536. break;
  1537. default:
  1538. prev = 0;
  1539. }
  1540. prev = av_clip_intp2((prev + 0x20) >> 6, 21);
  1541. byte = bytestream2_get_byteu(&gb);
  1542. if (!channel)
  1543. sampledat = sign_extend(byte, 4);
  1544. else
  1545. sampledat = sign_extend(byte >> 4, 4);
  1546. sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
  1547. *samples++ = av_clip_int16(sampledat >> 6);
  1548. c->status[channel].sample2 = c->status[channel].sample1;
  1549. c->status[channel].sample1 = sampledat;
  1550. }
  1551. }
  1552. if (!channel)
  1553. bytestream2_seek(&gb, 0, SEEK_SET);
  1554. }
  1555. break;
  1556. case AV_CODEC_ID_ADPCM_PSX:
  1557. for (channel = 0; channel < avctx->channels; channel++) {
  1558. samples = samples_p[channel];
  1559. /* Read in every sample for this channel. */
  1560. for (i = 0; i < nb_samples / 28; i++) {
  1561. int filter, shift, flag, byte;
  1562. filter = bytestream2_get_byteu(&gb);
  1563. shift = filter & 0xf;
  1564. filter = filter >> 4;
  1565. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
  1566. return AVERROR_INVALIDDATA;
  1567. flag = bytestream2_get_byteu(&gb);
  1568. /* Decode 28 samples. */
  1569. for (n = 0; n < 28; n++) {
  1570. int sample = 0, scale;
  1571. if (flag < 0x07) {
  1572. if (n & 1) {
  1573. scale = sign_extend(byte >> 4, 4);
  1574. } else {
  1575. byte = bytestream2_get_byteu(&gb);
  1576. scale = sign_extend(byte, 4);
  1577. }
  1578. scale = scale << 12;
  1579. sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
  1580. }
  1581. *samples++ = av_clip_int16(sample);
  1582. c->status[channel].sample2 = c->status[channel].sample1;
  1583. c->status[channel].sample1 = sample;
  1584. }
  1585. }
  1586. }
  1587. break;
  1588. default:
  1589. av_assert0(0); // unsupported codec_id should not happen
  1590. }
  1591. if (avpkt->size && bytestream2_tell(&gb) == 0) {
  1592. av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
  1593. return AVERROR_INVALIDDATA;
  1594. }
  1595. *got_frame_ptr = 1;
  1596. if (avpkt->size < bytestream2_tell(&gb)) {
  1597. av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
  1598. return avpkt->size;
  1599. }
  1600. return bytestream2_tell(&gb);
  1601. }
  1602. static void adpcm_flush(AVCodecContext *avctx)
  1603. {
  1604. ADPCMDecodeContext *c = avctx->priv_data;
  1605. c->has_status = 0;
  1606. }
  1607. static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
  1608. AV_SAMPLE_FMT_NONE };
  1609. static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
  1610. AV_SAMPLE_FMT_NONE };
  1611. static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
  1612. AV_SAMPLE_FMT_S16P,
  1613. AV_SAMPLE_FMT_NONE };
  1614. #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
  1615. AVCodec ff_ ## name_ ## _decoder = { \
  1616. .name = #name_, \
  1617. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1618. .type = AVMEDIA_TYPE_AUDIO, \
  1619. .id = id_, \
  1620. .priv_data_size = sizeof(ADPCMDecodeContext), \
  1621. .init = adpcm_decode_init, \
  1622. .decode = adpcm_decode_frame, \
  1623. .flush = adpcm_flush, \
  1624. .capabilities = AV_CODEC_CAP_DR1, \
  1625. .sample_fmts = sample_fmts_, \
  1626. }
  1627. /* Note: Do not forget to add new entries to the Makefile as well. */
  1628. ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
  1629. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
  1630. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM, sample_fmts_s16, adpcm_agm, "ADPCM AmuseGraphics Movie");
  1631. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
  1632. ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
  1633. ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
  1634. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
  1635. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1636. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1637. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1638. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1639. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1640. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
  1641. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
  1642. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
  1643. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1644. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1645. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1646. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1647. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1648. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
  1649. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1650. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
  1651. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1652. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
  1653. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
  1654. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_both, adpcm_ms, "ADPCM Microsoft");
  1655. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MTAF, sample_fmts_s16p, adpcm_mtaf, "ADPCM MTAF");
  1656. ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
  1657. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1658. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1659. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1660. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
  1661. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
  1662. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
  1663. ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
  1664. ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");