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.

1534 lines
59KB

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