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.

1831 lines
70KB

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