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.

1439 lines
55KB

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