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.

1923 lines
74KB

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