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.

1421 lines
54KB

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