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.

2014 lines
77KB

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