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.

1759 lines
68KB

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