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.

1423 lines
54KB

  1. /*
  2. * Copyright (c) 2001-2003 The ffmpeg Project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avcodec.h"
  21. #include "get_bits.h"
  22. #include "put_bits.h"
  23. #include "bytestream.h"
  24. #include "adpcm.h"
  25. #include "adpcm_data.h"
  26. /**
  27. * @file
  28. * ADPCM decoders
  29. * First version by Francois Revol (revol@free.fr)
  30. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  31. * by Mike Melanson (melanson@pcisys.net)
  32. * CD-ROM XA ADPCM codec by BERO
  33. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  34. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  35. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  36. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  37. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  38. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  39. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  40. *
  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. AVFrame frame;
  82. ADPCMChannelStatus status[6];
  83. int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
  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_EA:
  92. min_channels = 2;
  93. break;
  94. case AV_CODEC_ID_ADPCM_AFC:
  95. case AV_CODEC_ID_ADPCM_EA_R1:
  96. case AV_CODEC_ID_ADPCM_EA_R2:
  97. case AV_CODEC_ID_ADPCM_EA_R3:
  98. case AV_CODEC_ID_ADPCM_EA_XAS:
  99. case AV_CODEC_ID_ADPCM_THP:
  100. max_channels = 6;
  101. break;
  102. }
  103. if (avctx->channels < min_channels || avctx->channels > max_channels) {
  104. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  105. return AVERROR(EINVAL);
  106. }
  107. switch(avctx->codec->id) {
  108. case AV_CODEC_ID_ADPCM_CT:
  109. c->status[0].step = c->status[1].step = 511;
  110. break;
  111. case AV_CODEC_ID_ADPCM_IMA_WAV:
  112. if (avctx->bits_per_coded_sample != 4) {
  113. av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
  114. return -1;
  115. }
  116. break;
  117. case AV_CODEC_ID_ADPCM_IMA_APC:
  118. if (avctx->extradata && avctx->extradata_size >= 8) {
  119. c->status[0].predictor = AV_RL32(avctx->extradata);
  120. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  121. }
  122. break;
  123. case AV_CODEC_ID_ADPCM_IMA_WS:
  124. if (avctx->extradata && avctx->extradata_size >= 2)
  125. c->vqa_version = AV_RL16(avctx->extradata);
  126. break;
  127. default:
  128. break;
  129. }
  130. switch(avctx->codec->id) {
  131. case AV_CODEC_ID_ADPCM_IMA_QT:
  132. case AV_CODEC_ID_ADPCM_IMA_WAV:
  133. case AV_CODEC_ID_ADPCM_4XM:
  134. case AV_CODEC_ID_ADPCM_XA:
  135. case AV_CODEC_ID_ADPCM_EA_R1:
  136. case AV_CODEC_ID_ADPCM_EA_R2:
  137. case AV_CODEC_ID_ADPCM_EA_R3:
  138. case AV_CODEC_ID_ADPCM_EA_XAS:
  139. case AV_CODEC_ID_ADPCM_THP:
  140. case AV_CODEC_ID_ADPCM_AFC:
  141. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  142. break;
  143. case AV_CODEC_ID_ADPCM_IMA_WS:
  144. avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
  145. AV_SAMPLE_FMT_S16;
  146. break;
  147. default:
  148. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  149. }
  150. avcodec_get_frame_defaults(&c->frame);
  151. avctx->coded_frame = &c->frame;
  152. return 0;
  153. }
  154. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  155. {
  156. int step_index;
  157. int predictor;
  158. int sign, delta, diff, step;
  159. step = ff_adpcm_step_table[c->step_index];
  160. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  161. step_index = av_clip(step_index, 0, 88);
  162. sign = nibble & 8;
  163. delta = nibble & 7;
  164. /* perform direct multiplication instead of series of jumps proposed by
  165. * the reference ADPCM implementation since modern CPUs can do the mults
  166. * quickly enough */
  167. diff = ((2 * delta + 1) * step) >> shift;
  168. predictor = c->predictor;
  169. if (sign) predictor -= diff;
  170. else predictor += diff;
  171. c->predictor = av_clip_int16(predictor);
  172. c->step_index = step_index;
  173. return (short)c->predictor;
  174. }
  175. static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
  176. {
  177. int step_index;
  178. int predictor;
  179. int diff, step;
  180. step = ff_adpcm_step_table[c->step_index];
  181. step_index = c->step_index + ff_adpcm_index_table[nibble];
  182. step_index = av_clip(step_index, 0, 88);
  183. diff = step >> 3;
  184. if (nibble & 4) diff += step;
  185. if (nibble & 2) diff += step >> 1;
  186. if (nibble & 1) diff += step >> 2;
  187. if (nibble & 8)
  188. predictor = c->predictor - diff;
  189. else
  190. predictor = c->predictor + diff;
  191. c->predictor = av_clip_int16(predictor);
  192. c->step_index = step_index;
  193. return c->predictor;
  194. }
  195. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
  196. {
  197. int predictor;
  198. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  199. predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  200. c->sample2 = c->sample1;
  201. c->sample1 = av_clip_int16(predictor);
  202. c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
  203. if (c->idelta < 16) c->idelta = 16;
  204. return c->sample1;
  205. }
  206. static inline short adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
  207. {
  208. int step_index, predictor, sign, delta, diff, step;
  209. step = ff_adpcm_oki_step_table[c->step_index];
  210. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  211. step_index = av_clip(step_index, 0, 48);
  212. sign = nibble & 8;
  213. delta = nibble & 7;
  214. diff = ((2 * delta + 1) * step) >> 3;
  215. predictor = c->predictor;
  216. if (sign) predictor -= diff;
  217. else predictor += diff;
  218. c->predictor = av_clip(predictor, -2048, 2047);
  219. c->step_index = step_index;
  220. return c->predictor << 4;
  221. }
  222. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  223. {
  224. int sign, delta, diff;
  225. int new_step;
  226. sign = nibble & 8;
  227. delta = nibble & 7;
  228. /* perform direct multiplication instead of series of jumps proposed by
  229. * the reference ADPCM implementation since modern CPUs can do the mults
  230. * quickly enough */
  231. diff = ((2 * delta + 1) * c->step) >> 3;
  232. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  233. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  234. c->predictor = av_clip_int16(c->predictor);
  235. /* calculate new step and clamp it to range 511..32767 */
  236. new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
  237. c->step = av_clip(new_step, 511, 32767);
  238. return (short)c->predictor;
  239. }
  240. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  241. {
  242. int sign, delta, diff;
  243. sign = nibble & (1<<(size-1));
  244. delta = nibble & ((1<<(size-1))-1);
  245. diff = delta << (7 + c->step + shift);
  246. /* clamp result */
  247. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  248. /* calculate new step */
  249. if (delta >= (2*size - 3) && c->step < 3)
  250. c->step++;
  251. else if (delta == 0 && c->step > 0)
  252. c->step--;
  253. return (short) c->predictor;
  254. }
  255. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  256. {
  257. if(!c->step) {
  258. c->predictor = 0;
  259. c->step = 127;
  260. }
  261. c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
  262. c->predictor = av_clip_int16(c->predictor);
  263. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  264. c->step = av_clip(c->step, 127, 24567);
  265. return c->predictor;
  266. }
  267. static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
  268. const uint8_t *in, ADPCMChannelStatus *left,
  269. ADPCMChannelStatus *right, int channels, int sample_offset)
  270. {
  271. int i, j;
  272. int shift,filter,f0,f1;
  273. int s_1,s_2;
  274. int d,s,t;
  275. out0 += sample_offset;
  276. if (channels == 1)
  277. out1 = out0 + 28;
  278. else
  279. out1 += sample_offset;
  280. for(i=0;i<4;i++) {
  281. shift = 12 - (in[4+i*2] & 15);
  282. filter = in[4+i*2] >> 4;
  283. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  284. av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
  285. filter=0;
  286. }
  287. f0 = xa_adpcm_table[filter][0];
  288. f1 = xa_adpcm_table[filter][1];
  289. s_1 = left->sample1;
  290. s_2 = left->sample2;
  291. for(j=0;j<28;j++) {
  292. d = in[16+i+j*4];
  293. t = sign_extend(d, 4);
  294. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  295. s_2 = s_1;
  296. s_1 = av_clip_int16(s);
  297. out0[j] = s_1;
  298. }
  299. if (channels == 2) {
  300. left->sample1 = s_1;
  301. left->sample2 = s_2;
  302. s_1 = right->sample1;
  303. s_2 = right->sample2;
  304. }
  305. shift = 12 - (in[5+i*2] & 15);
  306. filter = in[5+i*2] >> 4;
  307. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  308. av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
  309. filter=0;
  310. }
  311. f0 = xa_adpcm_table[filter][0];
  312. f1 = xa_adpcm_table[filter][1];
  313. for(j=0;j<28;j++) {
  314. d = in[16+i+j*4];
  315. t = sign_extend(d >> 4, 4);
  316. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  317. s_2 = s_1;
  318. s_1 = av_clip_int16(s);
  319. out1[j] = s_1;
  320. }
  321. if (channels == 2) {
  322. right->sample1 = s_1;
  323. right->sample2 = s_2;
  324. } else {
  325. left->sample1 = s_1;
  326. left->sample2 = s_2;
  327. }
  328. out0 += 28 * (3 - channels);
  329. out1 += 28 * (3 - channels);
  330. }
  331. return 0;
  332. }
  333. static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
  334. {
  335. ADPCMDecodeContext *c = avctx->priv_data;
  336. GetBitContext gb;
  337. const int *table;
  338. int k0, signmask, nb_bits, count;
  339. int size = buf_size*8;
  340. int i;
  341. init_get_bits(&gb, buf, size);
  342. //read bits & initial values
  343. nb_bits = get_bits(&gb, 2)+2;
  344. table = swf_index_tables[nb_bits-2];
  345. k0 = 1 << (nb_bits-2);
  346. signmask = 1 << (nb_bits-1);
  347. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  348. for (i = 0; i < avctx->channels; i++) {
  349. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  350. c->status[i].step_index = get_bits(&gb, 6);
  351. }
  352. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  353. int i;
  354. for (i = 0; i < avctx->channels; i++) {
  355. // similar to IMA adpcm
  356. int delta = get_bits(&gb, nb_bits);
  357. int step = ff_adpcm_step_table[c->status[i].step_index];
  358. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  359. int k = k0;
  360. do {
  361. if (delta & k)
  362. vpdiff += step;
  363. step >>= 1;
  364. k >>= 1;
  365. } while(k);
  366. vpdiff += step;
  367. if (delta & signmask)
  368. c->status[i].predictor -= vpdiff;
  369. else
  370. c->status[i].predictor += vpdiff;
  371. c->status[i].step_index += table[delta & (~signmask)];
  372. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  373. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  374. *samples++ = c->status[i].predictor;
  375. }
  376. }
  377. }
  378. }
  379. /**
  380. * Get the number of samples that will be decoded from the packet.
  381. * In one case, this is actually the maximum number of samples possible to
  382. * decode with the given buf_size.
  383. *
  384. * @param[out] coded_samples set to the number of samples as coded in the
  385. * packet, or 0 if the codec does not encode the
  386. * number of samples in each frame.
  387. */
  388. static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
  389. int buf_size, int *coded_samples)
  390. {
  391. ADPCMDecodeContext *s = avctx->priv_data;
  392. int nb_samples = 0;
  393. int ch = avctx->channels;
  394. int has_coded_samples = 0;
  395. int header_size;
  396. *coded_samples = 0;
  397. if(ch <= 0)
  398. return 0;
  399. switch (avctx->codec->id) {
  400. /* constant, only check buf_size */
  401. case AV_CODEC_ID_ADPCM_EA_XAS:
  402. if (buf_size < 76 * ch)
  403. return 0;
  404. nb_samples = 128;
  405. break;
  406. case AV_CODEC_ID_ADPCM_IMA_QT:
  407. if (buf_size < 34 * ch)
  408. return 0;
  409. nb_samples = 64;
  410. break;
  411. /* simple 4-bit adpcm */
  412. case AV_CODEC_ID_ADPCM_CT:
  413. case AV_CODEC_ID_ADPCM_IMA_APC:
  414. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  415. case AV_CODEC_ID_ADPCM_IMA_OKI:
  416. case AV_CODEC_ID_ADPCM_IMA_WS:
  417. case AV_CODEC_ID_ADPCM_YAMAHA:
  418. nb_samples = buf_size * 2 / ch;
  419. break;
  420. }
  421. if (nb_samples)
  422. return nb_samples;
  423. /* simple 4-bit adpcm, with header */
  424. header_size = 0;
  425. switch (avctx->codec->id) {
  426. case AV_CODEC_ID_ADPCM_4XM:
  427. case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
  428. case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
  429. case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
  430. }
  431. if (header_size > 0)
  432. return (buf_size - header_size) * 2 / ch;
  433. /* more complex formats */
  434. switch (avctx->codec->id) {
  435. case AV_CODEC_ID_ADPCM_EA:
  436. has_coded_samples = 1;
  437. *coded_samples = bytestream2_get_le32(gb);
  438. *coded_samples -= *coded_samples % 28;
  439. nb_samples = (buf_size - 12) / 30 * 28;
  440. break;
  441. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  442. has_coded_samples = 1;
  443. *coded_samples = bytestream2_get_le32(gb);
  444. nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
  445. break;
  446. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  447. nb_samples = (buf_size - ch) / ch * 2;
  448. break;
  449. case AV_CODEC_ID_ADPCM_EA_R1:
  450. case AV_CODEC_ID_ADPCM_EA_R2:
  451. case AV_CODEC_ID_ADPCM_EA_R3:
  452. /* maximum number of samples */
  453. /* has internal offsets and a per-frame switch to signal raw 16-bit */
  454. has_coded_samples = 1;
  455. switch (avctx->codec->id) {
  456. case AV_CODEC_ID_ADPCM_EA_R1:
  457. header_size = 4 + 9 * ch;
  458. *coded_samples = bytestream2_get_le32(gb);
  459. break;
  460. case AV_CODEC_ID_ADPCM_EA_R2:
  461. header_size = 4 + 5 * ch;
  462. *coded_samples = bytestream2_get_le32(gb);
  463. break;
  464. case AV_CODEC_ID_ADPCM_EA_R3:
  465. header_size = 4 + 5 * ch;
  466. *coded_samples = bytestream2_get_be32(gb);
  467. break;
  468. }
  469. *coded_samples -= *coded_samples % 28;
  470. nb_samples = (buf_size - header_size) * 2 / ch;
  471. nb_samples -= nb_samples % 28;
  472. break;
  473. case AV_CODEC_ID_ADPCM_IMA_DK3:
  474. if (avctx->block_align > 0)
  475. buf_size = FFMIN(buf_size, avctx->block_align);
  476. nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
  477. break;
  478. case AV_CODEC_ID_ADPCM_IMA_DK4:
  479. if (avctx->block_align > 0)
  480. buf_size = FFMIN(buf_size, avctx->block_align);
  481. nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
  482. break;
  483. case AV_CODEC_ID_ADPCM_IMA_WAV:
  484. if (avctx->block_align > 0)
  485. buf_size = FFMIN(buf_size, avctx->block_align);
  486. nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
  487. break;
  488. case AV_CODEC_ID_ADPCM_MS:
  489. if (avctx->block_align > 0)
  490. buf_size = FFMIN(buf_size, avctx->block_align);
  491. nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
  492. break;
  493. case AV_CODEC_ID_ADPCM_SBPRO_2:
  494. case AV_CODEC_ID_ADPCM_SBPRO_3:
  495. case AV_CODEC_ID_ADPCM_SBPRO_4:
  496. {
  497. int samples_per_byte;
  498. switch (avctx->codec->id) {
  499. case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
  500. case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
  501. case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
  502. }
  503. if (!s->status[0].step_index) {
  504. nb_samples++;
  505. buf_size -= ch;
  506. }
  507. nb_samples += buf_size * samples_per_byte / ch;
  508. break;
  509. }
  510. case AV_CODEC_ID_ADPCM_SWF:
  511. {
  512. int buf_bits = buf_size * 8 - 2;
  513. int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
  514. int block_hdr_size = 22 * ch;
  515. int block_size = block_hdr_size + nbits * ch * 4095;
  516. int nblocks = buf_bits / block_size;
  517. int bits_left = buf_bits - nblocks * block_size;
  518. nb_samples = nblocks * 4096;
  519. if (bits_left >= block_hdr_size)
  520. nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
  521. break;
  522. }
  523. case AV_CODEC_ID_ADPCM_THP:
  524. has_coded_samples = 1;
  525. bytestream2_skip(gb, 4); // channel size
  526. *coded_samples = bytestream2_get_be32(gb);
  527. *coded_samples -= *coded_samples % 14;
  528. nb_samples = (buf_size - (8 + 36 * ch)) / (8 * ch) * 14;
  529. break;
  530. case AV_CODEC_ID_ADPCM_AFC:
  531. nb_samples = buf_size / (9 * ch) * 16;
  532. break;
  533. case AV_CODEC_ID_ADPCM_XA:
  534. nb_samples = (buf_size / 128) * 224 / ch;
  535. break;
  536. }
  537. /* validate coded sample count */
  538. if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
  539. return AVERROR_INVALIDDATA;
  540. return nb_samples;
  541. }
  542. static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
  543. int *got_frame_ptr, AVPacket *avpkt)
  544. {
  545. const uint8_t *buf = avpkt->data;
  546. int buf_size = avpkt->size;
  547. ADPCMDecodeContext *c = avctx->priv_data;
  548. ADPCMChannelStatus *cs;
  549. int n, m, channel, i;
  550. short *samples;
  551. int16_t **samples_p;
  552. int st; /* stereo */
  553. int count1, count2;
  554. int nb_samples, coded_samples, ret;
  555. GetByteContext gb;
  556. bytestream2_init(&gb, buf, buf_size);
  557. nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
  558. if (nb_samples <= 0) {
  559. av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
  560. return AVERROR_INVALIDDATA;
  561. }
  562. /* get output buffer */
  563. c->frame.nb_samples = nb_samples;
  564. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  565. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  566. return ret;
  567. }
  568. samples = (short *)c->frame.data[0];
  569. samples_p = (int16_t **)c->frame.extended_data;
  570. /* use coded_samples when applicable */
  571. /* it is always <= nb_samples, so the output buffer will be large enough */
  572. if (coded_samples) {
  573. if (coded_samples != nb_samples)
  574. av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
  575. c->frame.nb_samples = nb_samples = coded_samples;
  576. }
  577. st = avctx->channels == 2 ? 1 : 0;
  578. switch(avctx->codec->id) {
  579. case AV_CODEC_ID_ADPCM_IMA_QT:
  580. /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
  581. Channel data is interleaved per-chunk. */
  582. for (channel = 0; channel < avctx->channels; channel++) {
  583. int predictor;
  584. int step_index;
  585. cs = &(c->status[channel]);
  586. /* (pppppp) (piiiiiii) */
  587. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  588. predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  589. step_index = predictor & 0x7F;
  590. predictor &= ~0x7F;
  591. if (cs->step_index == step_index) {
  592. int diff = predictor - cs->predictor;
  593. if (diff < 0)
  594. diff = - diff;
  595. if (diff > 0x7f)
  596. goto update;
  597. } else {
  598. update:
  599. cs->step_index = step_index;
  600. cs->predictor = predictor;
  601. }
  602. if (cs->step_index > 88u){
  603. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  604. channel, cs->step_index);
  605. return AVERROR_INVALIDDATA;
  606. }
  607. samples = samples_p[channel];
  608. for (m = 0; m < 64; m += 2) {
  609. int byte = bytestream2_get_byteu(&gb);
  610. samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
  611. samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
  612. }
  613. }
  614. break;
  615. case AV_CODEC_ID_ADPCM_IMA_WAV:
  616. for(i=0; i<avctx->channels; i++){
  617. cs = &(c->status[i]);
  618. cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
  619. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  620. if (cs->step_index > 88u){
  621. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  622. i, cs->step_index);
  623. return AVERROR_INVALIDDATA;
  624. }
  625. }
  626. for (n = 0; n < (nb_samples - 1) / 8; n++) {
  627. for (i = 0; i < avctx->channels; i++) {
  628. cs = &c->status[i];
  629. samples = &samples_p[i][1 + n * 8];
  630. for (m = 0; m < 8; m += 2) {
  631. int v = bytestream2_get_byteu(&gb);
  632. samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  633. samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  634. }
  635. }
  636. }
  637. break;
  638. case AV_CODEC_ID_ADPCM_4XM:
  639. for (i = 0; i < avctx->channels; i++)
  640. c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  641. for (i = 0; i < avctx->channels; i++) {
  642. c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  643. if (c->status[i].step_index > 88u) {
  644. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  645. i, c->status[i].step_index);
  646. return AVERROR_INVALIDDATA;
  647. }
  648. }
  649. for (i = 0; i < avctx->channels; i++) {
  650. samples = (int16_t *)c->frame.data[i];
  651. cs = &c->status[i];
  652. for (n = nb_samples >> 1; n > 0; n--) {
  653. int v = bytestream2_get_byteu(&gb);
  654. *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
  655. *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
  656. }
  657. }
  658. break;
  659. case AV_CODEC_ID_ADPCM_MS:
  660. {
  661. int block_predictor;
  662. block_predictor = bytestream2_get_byteu(&gb);
  663. if (block_predictor > 6) {
  664. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
  665. block_predictor);
  666. return AVERROR_INVALIDDATA;
  667. }
  668. c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  669. c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  670. if (st) {
  671. block_predictor = bytestream2_get_byteu(&gb);
  672. if (block_predictor > 6) {
  673. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
  674. block_predictor);
  675. return AVERROR_INVALIDDATA;
  676. }
  677. c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  678. c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  679. }
  680. c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  681. if (st){
  682. c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  683. }
  684. c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  685. if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  686. c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  687. if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  688. *samples++ = c->status[0].sample2;
  689. if (st) *samples++ = c->status[1].sample2;
  690. *samples++ = c->status[0].sample1;
  691. if (st) *samples++ = c->status[1].sample1;
  692. for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
  693. int byte = bytestream2_get_byteu(&gb);
  694. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
  695. *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
  696. }
  697. break;
  698. }
  699. case AV_CODEC_ID_ADPCM_IMA_DK4:
  700. for (channel = 0; channel < avctx->channels; channel++) {
  701. cs = &c->status[channel];
  702. cs->predictor = *samples++ = 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. channel, cs->step_index);
  707. return AVERROR_INVALIDDATA;
  708. }
  709. }
  710. for (n = nb_samples >> (1 - st); n > 0; n--) {
  711. int v = bytestream2_get_byteu(&gb);
  712. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
  713. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  714. }
  715. break;
  716. case AV_CODEC_ID_ADPCM_IMA_DK3:
  717. {
  718. int last_byte = 0;
  719. int nibble;
  720. int decode_top_nibble_next = 0;
  721. int diff_channel;
  722. const int16_t *samples_end = samples + avctx->channels * nb_samples;
  723. bytestream2_skipu(&gb, 10);
  724. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  725. c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  726. c->status[0].step_index = bytestream2_get_byteu(&gb);
  727. c->status[1].step_index = bytestream2_get_byteu(&gb);
  728. if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
  729. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
  730. c->status[0].step_index, c->status[1].step_index);
  731. return AVERROR_INVALIDDATA;
  732. }
  733. /* sign extend the predictors */
  734. diff_channel = c->status[1].predictor;
  735. /* DK3 ADPCM support macro */
  736. #define DK3_GET_NEXT_NIBBLE() \
  737. if (decode_top_nibble_next) { \
  738. nibble = last_byte >> 4; \
  739. decode_top_nibble_next = 0; \
  740. } else { \
  741. last_byte = bytestream2_get_byteu(&gb); \
  742. nibble = last_byte & 0x0F; \
  743. decode_top_nibble_next = 1; \
  744. }
  745. while (samples < samples_end) {
  746. /* for this algorithm, c->status[0] is the sum channel and
  747. * c->status[1] is the diff channel */
  748. /* process the first predictor of the sum channel */
  749. DK3_GET_NEXT_NIBBLE();
  750. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  751. /* process the diff channel predictor */
  752. DK3_GET_NEXT_NIBBLE();
  753. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  754. /* process the first pair of stereo PCM samples */
  755. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  756. *samples++ = c->status[0].predictor + c->status[1].predictor;
  757. *samples++ = c->status[0].predictor - c->status[1].predictor;
  758. /* process the second predictor of the sum channel */
  759. DK3_GET_NEXT_NIBBLE();
  760. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  761. /* process the second pair of stereo PCM samples */
  762. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  763. *samples++ = c->status[0].predictor + c->status[1].predictor;
  764. *samples++ = c->status[0].predictor - c->status[1].predictor;
  765. }
  766. break;
  767. }
  768. case AV_CODEC_ID_ADPCM_IMA_ISS:
  769. for (channel = 0; channel < avctx->channels; channel++) {
  770. cs = &c->status[channel];
  771. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  772. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  773. if (cs->step_index > 88u){
  774. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  775. channel, cs->step_index);
  776. return AVERROR_INVALIDDATA;
  777. }
  778. }
  779. for (n = nb_samples >> (1 - st); n > 0; n--) {
  780. int v1, v2;
  781. int v = bytestream2_get_byteu(&gb);
  782. /* nibbles are swapped for mono */
  783. if (st) {
  784. v1 = v >> 4;
  785. v2 = v & 0x0F;
  786. } else {
  787. v2 = v >> 4;
  788. v1 = v & 0x0F;
  789. }
  790. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
  791. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
  792. }
  793. break;
  794. case AV_CODEC_ID_ADPCM_IMA_APC:
  795. while (bytestream2_get_bytes_left(&gb) > 0) {
  796. int v = bytestream2_get_byteu(&gb);
  797. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
  798. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  799. }
  800. break;
  801. case AV_CODEC_ID_ADPCM_IMA_OKI:
  802. while (bytestream2_get_bytes_left(&gb) > 0) {
  803. int v = bytestream2_get_byteu(&gb);
  804. *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
  805. *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
  806. }
  807. break;
  808. case AV_CODEC_ID_ADPCM_IMA_WS:
  809. if (c->vqa_version == 3) {
  810. for (channel = 0; channel < avctx->channels; channel++) {
  811. int16_t *smp = samples_p[channel];
  812. for (n = nb_samples / 2; n > 0; n--) {
  813. int v = bytestream2_get_byteu(&gb);
  814. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  815. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  816. }
  817. }
  818. } else {
  819. for (n = nb_samples / 2; n > 0; n--) {
  820. for (channel = 0; channel < avctx->channels; channel++) {
  821. int v = bytestream2_get_byteu(&gb);
  822. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  823. samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  824. }
  825. samples += avctx->channels;
  826. }
  827. }
  828. bytestream2_seek(&gb, 0, SEEK_END);
  829. break;
  830. case AV_CODEC_ID_ADPCM_XA:
  831. {
  832. int16_t *out0 = samples_p[0];
  833. int16_t *out1 = samples_p[1];
  834. int samples_per_block = 28 * (3 - avctx->channels) * 4;
  835. int sample_offset = 0;
  836. while (bytestream2_get_bytes_left(&gb) >= 128) {
  837. if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
  838. &c->status[0], &c->status[1],
  839. avctx->channels, sample_offset)) < 0)
  840. return ret;
  841. bytestream2_skipu(&gb, 128);
  842. sample_offset += samples_per_block;
  843. }
  844. break;
  845. }
  846. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  847. for (i=0; i<=st; i++) {
  848. c->status[i].step_index = bytestream2_get_le32u(&gb);
  849. if (c->status[i].step_index > 88u) {
  850. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  851. i, c->status[i].step_index);
  852. return AVERROR_INVALIDDATA;
  853. }
  854. }
  855. for (i=0; i<=st; i++)
  856. c->status[i].predictor = bytestream2_get_le32u(&gb);
  857. for (n = nb_samples >> (1 - st); n > 0; n--) {
  858. int byte = bytestream2_get_byteu(&gb);
  859. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
  860. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
  861. }
  862. break;
  863. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  864. for (n = nb_samples >> (1 - st); n > 0; n--) {
  865. int byte = bytestream2_get_byteu(&gb);
  866. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
  867. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
  868. }
  869. break;
  870. case AV_CODEC_ID_ADPCM_EA:
  871. {
  872. int previous_left_sample, previous_right_sample;
  873. int current_left_sample, current_right_sample;
  874. int next_left_sample, next_right_sample;
  875. int coeff1l, coeff2l, coeff1r, coeff2r;
  876. int shift_left, shift_right;
  877. /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
  878. each coding 28 stereo samples. */
  879. if(avctx->channels != 2)
  880. return AVERROR_INVALIDDATA;
  881. current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  882. previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  883. current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  884. previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  885. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  886. int byte = bytestream2_get_byteu(&gb);
  887. coeff1l = ea_adpcm_table[ byte >> 4 ];
  888. coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
  889. coeff1r = ea_adpcm_table[ byte & 0x0F];
  890. coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
  891. byte = bytestream2_get_byteu(&gb);
  892. shift_left = 20 - (byte >> 4);
  893. shift_right = 20 - (byte & 0x0F);
  894. for (count2 = 0; count2 < 28; count2++) {
  895. byte = bytestream2_get_byteu(&gb);
  896. next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
  897. next_right_sample = sign_extend(byte, 4) << shift_right;
  898. next_left_sample = (next_left_sample +
  899. (current_left_sample * coeff1l) +
  900. (previous_left_sample * coeff2l) + 0x80) >> 8;
  901. next_right_sample = (next_right_sample +
  902. (current_right_sample * coeff1r) +
  903. (previous_right_sample * coeff2r) + 0x80) >> 8;
  904. previous_left_sample = current_left_sample;
  905. current_left_sample = av_clip_int16(next_left_sample);
  906. previous_right_sample = current_right_sample;
  907. current_right_sample = av_clip_int16(next_right_sample);
  908. *samples++ = current_left_sample;
  909. *samples++ = current_right_sample;
  910. }
  911. }
  912. bytestream2_skip(&gb, 2); // Skip terminating 0x0000
  913. break;
  914. }
  915. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  916. {
  917. int coeff[2][2], shift[2];
  918. for(channel = 0; channel < avctx->channels; channel++) {
  919. int byte = bytestream2_get_byteu(&gb);
  920. for (i=0; i<2; i++)
  921. coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
  922. shift[channel] = 20 - (byte & 0x0F);
  923. }
  924. for (count1 = 0; count1 < nb_samples / 2; count1++) {
  925. int byte[2];
  926. byte[0] = bytestream2_get_byteu(&gb);
  927. if (st) byte[1] = bytestream2_get_byteu(&gb);
  928. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  929. for(channel = 0; channel < avctx->channels; channel++) {
  930. int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
  931. sample = (sample +
  932. c->status[channel].sample1 * coeff[channel][0] +
  933. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  934. c->status[channel].sample2 = c->status[channel].sample1;
  935. c->status[channel].sample1 = av_clip_int16(sample);
  936. *samples++ = c->status[channel].sample1;
  937. }
  938. }
  939. }
  940. bytestream2_seek(&gb, 0, SEEK_END);
  941. break;
  942. }
  943. case AV_CODEC_ID_ADPCM_EA_R1:
  944. case AV_CODEC_ID_ADPCM_EA_R2:
  945. case AV_CODEC_ID_ADPCM_EA_R3: {
  946. /* channel numbering
  947. 2chan: 0=fl, 1=fr
  948. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  949. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  950. const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
  951. int previous_sample, current_sample, next_sample;
  952. int coeff1, coeff2;
  953. int shift;
  954. unsigned int channel;
  955. uint16_t *samplesC;
  956. int count = 0;
  957. int offsets[6];
  958. for (channel=0; channel<avctx->channels; channel++)
  959. offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
  960. bytestream2_get_le32(&gb)) +
  961. (avctx->channels + 1) * 4;
  962. for (channel=0; channel<avctx->channels; channel++) {
  963. bytestream2_seek(&gb, offsets[channel], SEEK_SET);
  964. samplesC = samples_p[channel];
  965. if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
  966. current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  967. previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  968. } else {
  969. current_sample = c->status[channel].predictor;
  970. previous_sample = c->status[channel].prev_sample;
  971. }
  972. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  973. int byte = bytestream2_get_byte(&gb);
  974. if (byte == 0xEE) { /* only seen in R2 and R3 */
  975. current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  976. previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  977. for (count2=0; count2<28; count2++)
  978. *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
  979. } else {
  980. coeff1 = ea_adpcm_table[ byte >> 4 ];
  981. coeff2 = ea_adpcm_table[(byte >> 4) + 4];
  982. shift = 20 - (byte & 0x0F);
  983. for (count2=0; count2<28; count2++) {
  984. if (count2 & 1)
  985. next_sample = sign_extend(byte, 4) << shift;
  986. else {
  987. byte = bytestream2_get_byte(&gb);
  988. next_sample = sign_extend(byte >> 4, 4) << shift;
  989. }
  990. next_sample += (current_sample * coeff1) +
  991. (previous_sample * coeff2);
  992. next_sample = av_clip_int16(next_sample >> 8);
  993. previous_sample = current_sample;
  994. current_sample = next_sample;
  995. *samplesC++ = current_sample;
  996. }
  997. }
  998. }
  999. if (!count) {
  1000. count = count1;
  1001. } else if (count != count1) {
  1002. av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
  1003. count = FFMAX(count, count1);
  1004. }
  1005. if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
  1006. c->status[channel].predictor = current_sample;
  1007. c->status[channel].prev_sample = previous_sample;
  1008. }
  1009. }
  1010. c->frame.nb_samples = count * 28;
  1011. bytestream2_seek(&gb, 0, SEEK_END);
  1012. break;
  1013. }
  1014. case AV_CODEC_ID_ADPCM_EA_XAS:
  1015. for (channel=0; channel<avctx->channels; channel++) {
  1016. int coeff[2][4], shift[4];
  1017. int16_t *s = samples_p[channel];
  1018. for (n = 0; n < 4; n++, s += 32) {
  1019. int val = sign_extend(bytestream2_get_le16u(&gb), 16);
  1020. for (i=0; i<2; i++)
  1021. coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
  1022. s[0] = val & ~0x0F;
  1023. val = sign_extend(bytestream2_get_le16u(&gb), 16);
  1024. shift[n] = 20 - (val & 0x0F);
  1025. s[1] = val & ~0x0F;
  1026. }
  1027. for (m=2; m<32; m+=2) {
  1028. s = &samples_p[channel][m];
  1029. for (n = 0; n < 4; n++, s += 32) {
  1030. int level, pred;
  1031. int byte = bytestream2_get_byteu(&gb);
  1032. level = sign_extend(byte >> 4, 4) << shift[n];
  1033. pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
  1034. s[0] = av_clip_int16((level + pred + 0x80) >> 8);
  1035. level = sign_extend(byte, 4) << shift[n];
  1036. pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
  1037. s[1] = av_clip_int16((level + pred + 0x80) >> 8);
  1038. }
  1039. }
  1040. }
  1041. break;
  1042. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1043. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  1044. c->status[0].step_index = bytestream2_get_le16u(&gb);
  1045. bytestream2_skipu(&gb, 4);
  1046. if (c->status[0].step_index > 88u) {
  1047. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1048. c->status[0].step_index);
  1049. return AVERROR_INVALIDDATA;
  1050. }
  1051. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1052. int v = bytestream2_get_byteu(&gb);
  1053. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
  1054. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
  1055. }
  1056. break;
  1057. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1058. for (i = 0; i < avctx->channels; i++) {
  1059. c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  1060. c->status[i].step_index = bytestream2_get_byteu(&gb);
  1061. bytestream2_skipu(&gb, 1);
  1062. if (c->status[i].step_index > 88u) {
  1063. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1064. c->status[i].step_index);
  1065. return AVERROR_INVALIDDATA;
  1066. }
  1067. }
  1068. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1069. int v = bytestream2_get_byteu(&gb);
  1070. *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
  1071. *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
  1072. }
  1073. break;
  1074. case AV_CODEC_ID_ADPCM_CT:
  1075. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1076. int v = bytestream2_get_byteu(&gb);
  1077. *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
  1078. *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
  1079. }
  1080. break;
  1081. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1082. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1083. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1084. if (!c->status[0].step_index) {
  1085. /* the first byte is a raw sample */
  1086. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1087. if (st)
  1088. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1089. c->status[0].step_index = 1;
  1090. nb_samples--;
  1091. }
  1092. if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
  1093. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1094. int byte = bytestream2_get_byteu(&gb);
  1095. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1096. byte >> 4, 4, 0);
  1097. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1098. byte & 0x0F, 4, 0);
  1099. }
  1100. } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
  1101. for (n = nb_samples / 3; n > 0; n--) {
  1102. int byte = bytestream2_get_byteu(&gb);
  1103. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1104. byte >> 5 , 3, 0);
  1105. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1106. (byte >> 2) & 0x07, 3, 0);
  1107. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1108. byte & 0x03, 2, 0);
  1109. }
  1110. } else {
  1111. for (n = nb_samples >> (2 - st); n > 0; n--) {
  1112. int byte = bytestream2_get_byteu(&gb);
  1113. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1114. byte >> 6 , 2, 2);
  1115. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1116. (byte >> 4) & 0x03, 2, 2);
  1117. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1118. (byte >> 2) & 0x03, 2, 2);
  1119. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1120. byte & 0x03, 2, 2);
  1121. }
  1122. }
  1123. break;
  1124. case AV_CODEC_ID_ADPCM_SWF:
  1125. adpcm_swf_decode(avctx, buf, buf_size, samples);
  1126. bytestream2_seek(&gb, 0, SEEK_END);
  1127. break;
  1128. case AV_CODEC_ID_ADPCM_YAMAHA:
  1129. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1130. int v = bytestream2_get_byteu(&gb);
  1131. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
  1132. *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
  1133. }
  1134. break;
  1135. case AV_CODEC_ID_ADPCM_AFC:
  1136. for (channel = 0; channel < avctx->channels; channel++) {
  1137. int prev1 = c->status[channel].sample1;
  1138. int prev2 = c->status[channel].sample2;
  1139. samples = samples_p[channel];
  1140. /* Read in every sample for this channel. */
  1141. for (i = 0; i < nb_samples / 16; i++) {
  1142. int byte = bytestream2_get_byteu(&gb);
  1143. int scale = 1 << (byte >> 4);
  1144. int index = byte & 0xf;
  1145. int factor1 = ff_adpcm_afc_coeffs[0][index];
  1146. int factor2 = ff_adpcm_afc_coeffs[1][index];
  1147. /* Decode 16 samples. */
  1148. for (n = 0; n < 16; n++) {
  1149. int32_t sampledat;
  1150. if (n & 1) {
  1151. sampledat = sign_extend(byte, 4);
  1152. } else {
  1153. byte = bytestream2_get_byteu(&gb);
  1154. sampledat = sign_extend(byte >> 4, 4);
  1155. }
  1156. sampledat = ((prev1 * factor1 + prev2 * factor2) +
  1157. ((sampledat * scale) << 11)) >> 11;
  1158. *samples = av_clip_int16(sampledat);
  1159. prev2 = prev1;
  1160. prev1 = *samples++;
  1161. }
  1162. }
  1163. c->status[channel].sample1 = prev1;
  1164. c->status[channel].sample2 = prev2;
  1165. }
  1166. bytestream2_seek(&gb, 0, SEEK_END);
  1167. break;
  1168. case AV_CODEC_ID_ADPCM_THP:
  1169. {
  1170. int table[6][16];
  1171. int ch;
  1172. for (i = 0; i < avctx->channels; i++)
  1173. for (n = 0; n < 16; n++)
  1174. table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
  1175. /* Initialize the previous sample. */
  1176. for (i = 0; i < avctx->channels; i++) {
  1177. c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
  1178. c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
  1179. }
  1180. for (ch = 0; ch < avctx->channels; ch++) {
  1181. samples = samples_p[ch];
  1182. /* Read in every sample for this channel. */
  1183. for (i = 0; i < nb_samples / 14; i++) {
  1184. int byte = bytestream2_get_byteu(&gb);
  1185. int index = (byte >> 4) & 7;
  1186. unsigned int exp = byte & 0x0F;
  1187. int factor1 = table[ch][index * 2];
  1188. int factor2 = table[ch][index * 2 + 1];
  1189. /* Decode 14 samples. */
  1190. for (n = 0; n < 14; n++) {
  1191. int32_t sampledat;
  1192. if (n & 1) {
  1193. sampledat = sign_extend(byte, 4);
  1194. } else {
  1195. byte = bytestream2_get_byteu(&gb);
  1196. sampledat = sign_extend(byte >> 4, 4);
  1197. }
  1198. sampledat = ((c->status[ch].sample1 * factor1
  1199. + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
  1200. *samples = av_clip_int16(sampledat);
  1201. c->status[ch].sample2 = c->status[ch].sample1;
  1202. c->status[ch].sample1 = *samples++;
  1203. }
  1204. }
  1205. }
  1206. break;
  1207. }
  1208. default:
  1209. return -1;
  1210. }
  1211. if (avpkt->size && bytestream2_tell(&gb) == 0) {
  1212. av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
  1213. return AVERROR_INVALIDDATA;
  1214. }
  1215. *got_frame_ptr = 1;
  1216. *(AVFrame *)data = c->frame;
  1217. return bytestream2_tell(&gb);
  1218. }
  1219. static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
  1220. AV_SAMPLE_FMT_NONE };
  1221. static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16,
  1222. AV_SAMPLE_FMT_NONE };
  1223. static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
  1224. AV_SAMPLE_FMT_S16P,
  1225. AV_SAMPLE_FMT_NONE };
  1226. #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
  1227. AVCodec ff_ ## name_ ## _decoder = { \
  1228. .name = #name_, \
  1229. .type = AVMEDIA_TYPE_AUDIO, \
  1230. .id = id_, \
  1231. .priv_data_size = sizeof(ADPCMDecodeContext), \
  1232. .init = adpcm_decode_init, \
  1233. .decode = adpcm_decode_frame, \
  1234. .capabilities = CODEC_CAP_DR1, \
  1235. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1236. .sample_fmts = sample_fmts_, \
  1237. }
  1238. /* Note: Do not forget to add new entries to the Makefile as well. */
  1239. ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
  1240. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
  1241. ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
  1242. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
  1243. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1244. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1245. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1246. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1247. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1248. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
  1249. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
  1250. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1251. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1252. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1253. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1254. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1255. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
  1256. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1257. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1258. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
  1259. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
  1260. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
  1261. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1262. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1263. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1264. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
  1265. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  1266. ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
  1267. ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");