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.

1435 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. #include "internal.h"
  27. /**
  28. * @file
  29. * ADPCM decoders
  30. * First version by Francois Revol (revol@free.fr)
  31. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  32. * by Mike Melanson (melanson@pcisys.net)
  33. * CD-ROM XA ADPCM codec by BERO
  34. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  35. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  36. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  37. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  38. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  39. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  40. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  41. *
  42. * Features and limitations:
  43. *
  44. * Reference documents:
  45. * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
  46. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
  47. * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
  48. * http://openquicktime.sourceforge.net/
  49. * XAnim sources (xa_codec.c) http://xanim.polter.net/
  50. * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
  51. * SoX source code http://sox.sourceforge.net/
  52. *
  53. * CD-ROM XA:
  54. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
  55. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
  56. * readstr http://www.geocities.co.jp/Playtown/2004/
  57. */
  58. /* These are for CD-ROM XA ADPCM */
  59. static const int xa_adpcm_table[5][2] = {
  60. { 0, 0 },
  61. { 60, 0 },
  62. { 115, -52 },
  63. { 98, -55 },
  64. { 122, -60 }
  65. };
  66. static const int ea_adpcm_table[] = {
  67. 0, 240, 460, 392,
  68. 0, 0, -208, -220,
  69. 0, 1, 3, 4,
  70. 7, 8, 10, 11,
  71. 0, -1, -3, -4
  72. };
  73. // padded to zero where table size is less then 16
  74. static const int swf_index_tables[4][16] = {
  75. /*2*/ { -1, 2 },
  76. /*3*/ { -1, -1, 2, 4 },
  77. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  78. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  79. };
  80. /* end of tables */
  81. typedef struct ADPCMDecodeContext {
  82. ADPCMChannelStatus status[6];
  83. int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
  84. } ADPCMDecodeContext;
  85. static av_cold int adpcm_decode_init(AVCodecContext * avctx)
  86. {
  87. ADPCMDecodeContext *c = avctx->priv_data;
  88. unsigned int min_channels = 1;
  89. unsigned int max_channels = 2;
  90. switch(avctx->codec->id) {
  91. case AV_CODEC_ID_ADPCM_EA:
  92. min_channels = 2;
  93. break;
  94. case AV_CODEC_ID_ADPCM_AFC:
  95. case AV_CODEC_ID_ADPCM_EA_R1:
  96. case AV_CODEC_ID_ADPCM_EA_R2:
  97. case AV_CODEC_ID_ADPCM_EA_R3:
  98. case AV_CODEC_ID_ADPCM_EA_XAS:
  99. case AV_CODEC_ID_ADPCM_THP:
  100. max_channels = 6;
  101. break;
  102. }
  103. if (avctx->channels < min_channels || avctx->channels > max_channels) {
  104. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  105. return AVERROR(EINVAL);
  106. }
  107. switch(avctx->codec->id) {
  108. case AV_CODEC_ID_ADPCM_CT:
  109. c->status[0].step = c->status[1].step = 511;
  110. break;
  111. case AV_CODEC_ID_ADPCM_IMA_WAV:
  112. if (avctx->bits_per_coded_sample != 4) {
  113. av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
  114. return -1;
  115. }
  116. break;
  117. case AV_CODEC_ID_ADPCM_IMA_APC:
  118. if (avctx->extradata && avctx->extradata_size >= 8) {
  119. c->status[0].predictor = AV_RL32(avctx->extradata);
  120. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  121. }
  122. break;
  123. case AV_CODEC_ID_ADPCM_IMA_WS:
  124. if (avctx->extradata && avctx->extradata_size >= 2)
  125. c->vqa_version = AV_RL16(avctx->extradata);
  126. break;
  127. default:
  128. break;
  129. }
  130. switch(avctx->codec->id) {
  131. case AV_CODEC_ID_ADPCM_IMA_QT:
  132. case AV_CODEC_ID_ADPCM_IMA_WAV:
  133. case AV_CODEC_ID_ADPCM_4XM:
  134. case AV_CODEC_ID_ADPCM_XA:
  135. case AV_CODEC_ID_ADPCM_EA_R1:
  136. case AV_CODEC_ID_ADPCM_EA_R2:
  137. case AV_CODEC_ID_ADPCM_EA_R3:
  138. case AV_CODEC_ID_ADPCM_EA_XAS:
  139. case AV_CODEC_ID_ADPCM_THP:
  140. case AV_CODEC_ID_ADPCM_AFC:
  141. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  142. break;
  143. case AV_CODEC_ID_ADPCM_IMA_WS:
  144. avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
  145. AV_SAMPLE_FMT_S16;
  146. break;
  147. default:
  148. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  149. }
  150. 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 - (8 + 36 * ch)) / (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. AVFrame *frame = data;
  544. const uint8_t *buf = avpkt->data;
  545. int buf_size = avpkt->size;
  546. ADPCMDecodeContext *c = avctx->priv_data;
  547. ADPCMChannelStatus *cs;
  548. int n, m, channel, i;
  549. short *samples;
  550. int16_t **samples_p;
  551. int st; /* stereo */
  552. int count1, count2;
  553. int nb_samples, coded_samples, ret;
  554. GetByteContext gb;
  555. bytestream2_init(&gb, buf, buf_size);
  556. nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
  557. if (nb_samples <= 0) {
  558. av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
  559. return AVERROR_INVALIDDATA;
  560. }
  561. /* get output buffer */
  562. frame->nb_samples = nb_samples;
  563. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  564. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  565. return ret;
  566. }
  567. samples = (short *)frame->data[0];
  568. samples_p = (int16_t **)frame->extended_data;
  569. /* use coded_samples when applicable */
  570. /* it is always <= nb_samples, so the output buffer will be large enough */
  571. if (coded_samples) {
  572. if (coded_samples != nb_samples)
  573. av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
  574. frame->nb_samples = nb_samples = coded_samples;
  575. }
  576. st = avctx->channels == 2 ? 1 : 0;
  577. switch(avctx->codec->id) {
  578. case AV_CODEC_ID_ADPCM_IMA_QT:
  579. /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
  580. Channel data is interleaved per-chunk. */
  581. for (channel = 0; channel < avctx->channels; channel++) {
  582. int predictor;
  583. int step_index;
  584. cs = &(c->status[channel]);
  585. /* (pppppp) (piiiiiii) */
  586. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  587. predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  588. step_index = predictor & 0x7F;
  589. predictor &= ~0x7F;
  590. if (cs->step_index == step_index) {
  591. int diff = predictor - cs->predictor;
  592. if (diff < 0)
  593. diff = - diff;
  594. if (diff > 0x7f)
  595. goto update;
  596. } else {
  597. update:
  598. cs->step_index = step_index;
  599. cs->predictor = predictor;
  600. }
  601. if (cs->step_index > 88u){
  602. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  603. channel, cs->step_index);
  604. return AVERROR_INVALIDDATA;
  605. }
  606. samples = samples_p[channel];
  607. for (m = 0; m < 64; m += 2) {
  608. int byte = bytestream2_get_byteu(&gb);
  609. samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
  610. samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
  611. }
  612. }
  613. break;
  614. case AV_CODEC_ID_ADPCM_IMA_WAV:
  615. for(i=0; i<avctx->channels; i++){
  616. cs = &(c->status[i]);
  617. cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
  618. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  619. if (cs->step_index > 88u){
  620. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  621. i, cs->step_index);
  622. return AVERROR_INVALIDDATA;
  623. }
  624. }
  625. for (n = 0; n < (nb_samples - 1) / 8; n++) {
  626. for (i = 0; i < avctx->channels; i++) {
  627. cs = &c->status[i];
  628. samples = &samples_p[i][1 + n * 8];
  629. for (m = 0; m < 8; m += 2) {
  630. int v = bytestream2_get_byteu(&gb);
  631. samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  632. samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  633. }
  634. }
  635. }
  636. break;
  637. case AV_CODEC_ID_ADPCM_4XM:
  638. for (i = 0; i < avctx->channels; i++)
  639. c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  640. for (i = 0; i < avctx->channels; i++) {
  641. c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  642. if (c->status[i].step_index > 88u) {
  643. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  644. i, c->status[i].step_index);
  645. return AVERROR_INVALIDDATA;
  646. }
  647. }
  648. for (i = 0; i < avctx->channels; i++) {
  649. samples = (int16_t *)frame->data[i];
  650. cs = &c->status[i];
  651. for (n = nb_samples >> 1; n > 0; n--) {
  652. int v = bytestream2_get_byteu(&gb);
  653. *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
  654. *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
  655. }
  656. }
  657. break;
  658. case AV_CODEC_ID_ADPCM_MS:
  659. {
  660. int block_predictor;
  661. block_predictor = bytestream2_get_byteu(&gb);
  662. if (block_predictor > 6) {
  663. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
  664. block_predictor);
  665. return AVERROR_INVALIDDATA;
  666. }
  667. c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  668. c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  669. if (st) {
  670. block_predictor = bytestream2_get_byteu(&gb);
  671. if (block_predictor > 6) {
  672. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
  673. block_predictor);
  674. return AVERROR_INVALIDDATA;
  675. }
  676. c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  677. c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  678. }
  679. c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  680. if (st){
  681. c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  682. }
  683. c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  684. if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  685. c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  686. if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  687. *samples++ = c->status[0].sample2;
  688. if (st) *samples++ = c->status[1].sample2;
  689. *samples++ = c->status[0].sample1;
  690. if (st) *samples++ = c->status[1].sample1;
  691. for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
  692. int byte = bytestream2_get_byteu(&gb);
  693. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
  694. *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
  695. }
  696. break;
  697. }
  698. case AV_CODEC_ID_ADPCM_IMA_DK4:
  699. for (channel = 0; channel < avctx->channels; channel++) {
  700. cs = &c->status[channel];
  701. cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
  702. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  703. if (cs->step_index > 88u){
  704. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  705. channel, cs->step_index);
  706. return AVERROR_INVALIDDATA;
  707. }
  708. }
  709. for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
  710. int v = bytestream2_get_byteu(&gb);
  711. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
  712. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  713. }
  714. break;
  715. case AV_CODEC_ID_ADPCM_IMA_DK3:
  716. {
  717. int last_byte = 0;
  718. int nibble;
  719. int decode_top_nibble_next = 0;
  720. int diff_channel;
  721. const int16_t *samples_end = samples + avctx->channels * nb_samples;
  722. bytestream2_skipu(&gb, 10);
  723. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  724. c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  725. c->status[0].step_index = bytestream2_get_byteu(&gb);
  726. c->status[1].step_index = bytestream2_get_byteu(&gb);
  727. if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
  728. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
  729. c->status[0].step_index, c->status[1].step_index);
  730. return AVERROR_INVALIDDATA;
  731. }
  732. /* sign extend the predictors */
  733. diff_channel = c->status[1].predictor;
  734. /* DK3 ADPCM support macro */
  735. #define DK3_GET_NEXT_NIBBLE() \
  736. if (decode_top_nibble_next) { \
  737. nibble = last_byte >> 4; \
  738. decode_top_nibble_next = 0; \
  739. } else { \
  740. last_byte = bytestream2_get_byteu(&gb); \
  741. nibble = last_byte & 0x0F; \
  742. decode_top_nibble_next = 1; \
  743. }
  744. while (samples < samples_end) {
  745. /* for this algorithm, c->status[0] is the sum channel and
  746. * c->status[1] is the diff channel */
  747. /* process the first predictor of the sum channel */
  748. DK3_GET_NEXT_NIBBLE();
  749. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  750. /* process the diff channel predictor */
  751. DK3_GET_NEXT_NIBBLE();
  752. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  753. /* process the first pair of stereo PCM samples */
  754. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  755. *samples++ = c->status[0].predictor + c->status[1].predictor;
  756. *samples++ = c->status[0].predictor - c->status[1].predictor;
  757. /* process the second predictor of the sum channel */
  758. DK3_GET_NEXT_NIBBLE();
  759. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  760. /* process the second pair of stereo PCM samples */
  761. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  762. *samples++ = c->status[0].predictor + c->status[1].predictor;
  763. *samples++ = c->status[0].predictor - c->status[1].predictor;
  764. }
  765. break;
  766. }
  767. case AV_CODEC_ID_ADPCM_IMA_ISS:
  768. for (channel = 0; channel < avctx->channels; channel++) {
  769. cs = &c->status[channel];
  770. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  771. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  772. if (cs->step_index > 88u){
  773. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  774. channel, cs->step_index);
  775. return AVERROR_INVALIDDATA;
  776. }
  777. }
  778. for (n = nb_samples >> (1 - st); n > 0; n--) {
  779. int v1, v2;
  780. int v = bytestream2_get_byteu(&gb);
  781. /* nibbles are swapped for mono */
  782. if (st) {
  783. v1 = v >> 4;
  784. v2 = v & 0x0F;
  785. } else {
  786. v2 = v >> 4;
  787. v1 = v & 0x0F;
  788. }
  789. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
  790. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
  791. }
  792. break;
  793. case AV_CODEC_ID_ADPCM_IMA_APC:
  794. while (bytestream2_get_bytes_left(&gb) > 0) {
  795. int v = bytestream2_get_byteu(&gb);
  796. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
  797. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  798. }
  799. break;
  800. case AV_CODEC_ID_ADPCM_IMA_OKI:
  801. while (bytestream2_get_bytes_left(&gb) > 0) {
  802. int v = bytestream2_get_byteu(&gb);
  803. *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
  804. *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
  805. }
  806. break;
  807. case AV_CODEC_ID_ADPCM_IMA_WS:
  808. if (c->vqa_version == 3) {
  809. for (channel = 0; channel < avctx->channels; channel++) {
  810. int16_t *smp = samples_p[channel];
  811. for (n = nb_samples / 2; n > 0; n--) {
  812. int v = bytestream2_get_byteu(&gb);
  813. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  814. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  815. }
  816. }
  817. } else {
  818. for (n = nb_samples / 2; n > 0; n--) {
  819. for (channel = 0; channel < avctx->channels; channel++) {
  820. int v = bytestream2_get_byteu(&gb);
  821. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  822. samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  823. }
  824. samples += avctx->channels;
  825. }
  826. }
  827. bytestream2_seek(&gb, 0, SEEK_END);
  828. break;
  829. case AV_CODEC_ID_ADPCM_XA:
  830. {
  831. int16_t *out0 = samples_p[0];
  832. int16_t *out1 = samples_p[1];
  833. int samples_per_block = 28 * (3 - avctx->channels) * 4;
  834. int sample_offset = 0;
  835. while (bytestream2_get_bytes_left(&gb) >= 128) {
  836. if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
  837. &c->status[0], &c->status[1],
  838. avctx->channels, sample_offset)) < 0)
  839. return ret;
  840. bytestream2_skipu(&gb, 128);
  841. sample_offset += samples_per_block;
  842. }
  843. break;
  844. }
  845. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  846. for (i=0; i<=st; i++) {
  847. c->status[i].step_index = bytestream2_get_le32u(&gb);
  848. if (c->status[i].step_index > 88u) {
  849. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  850. i, c->status[i].step_index);
  851. return AVERROR_INVALIDDATA;
  852. }
  853. }
  854. for (i=0; i<=st; i++)
  855. c->status[i].predictor = bytestream2_get_le32u(&gb);
  856. for (n = nb_samples >> (1 - st); n > 0; n--) {
  857. int byte = bytestream2_get_byteu(&gb);
  858. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
  859. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
  860. }
  861. break;
  862. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  863. for (n = nb_samples >> (1 - st); n > 0; n--) {
  864. int byte = bytestream2_get_byteu(&gb);
  865. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
  866. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
  867. }
  868. break;
  869. case AV_CODEC_ID_ADPCM_EA:
  870. {
  871. int previous_left_sample, previous_right_sample;
  872. int current_left_sample, current_right_sample;
  873. int next_left_sample, next_right_sample;
  874. int coeff1l, coeff2l, coeff1r, coeff2r;
  875. int shift_left, shift_right;
  876. /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
  877. each coding 28 stereo samples. */
  878. if(avctx->channels != 2)
  879. return AVERROR_INVALIDDATA;
  880. current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  881. previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  882. current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  883. previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  884. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  885. int byte = bytestream2_get_byteu(&gb);
  886. coeff1l = ea_adpcm_table[ byte >> 4 ];
  887. coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
  888. coeff1r = ea_adpcm_table[ byte & 0x0F];
  889. coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
  890. byte = bytestream2_get_byteu(&gb);
  891. shift_left = 20 - (byte >> 4);
  892. shift_right = 20 - (byte & 0x0F);
  893. for (count2 = 0; count2 < 28; count2++) {
  894. byte = bytestream2_get_byteu(&gb);
  895. next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
  896. next_right_sample = sign_extend(byte, 4) << shift_right;
  897. next_left_sample = (next_left_sample +
  898. (current_left_sample * coeff1l) +
  899. (previous_left_sample * coeff2l) + 0x80) >> 8;
  900. next_right_sample = (next_right_sample +
  901. (current_right_sample * coeff1r) +
  902. (previous_right_sample * coeff2r) + 0x80) >> 8;
  903. previous_left_sample = current_left_sample;
  904. current_left_sample = av_clip_int16(next_left_sample);
  905. previous_right_sample = current_right_sample;
  906. current_right_sample = av_clip_int16(next_right_sample);
  907. *samples++ = current_left_sample;
  908. *samples++ = current_right_sample;
  909. }
  910. }
  911. bytestream2_skip(&gb, 2); // Skip terminating 0x0000
  912. break;
  913. }
  914. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  915. {
  916. int coeff[2][2], shift[2];
  917. for(channel = 0; channel < avctx->channels; channel++) {
  918. int byte = bytestream2_get_byteu(&gb);
  919. for (i=0; i<2; i++)
  920. coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
  921. shift[channel] = 20 - (byte & 0x0F);
  922. }
  923. for (count1 = 0; count1 < nb_samples / 2; count1++) {
  924. int byte[2];
  925. byte[0] = bytestream2_get_byteu(&gb);
  926. if (st) byte[1] = bytestream2_get_byteu(&gb);
  927. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  928. for(channel = 0; channel < avctx->channels; channel++) {
  929. int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
  930. sample = (sample +
  931. c->status[channel].sample1 * coeff[channel][0] +
  932. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  933. c->status[channel].sample2 = c->status[channel].sample1;
  934. c->status[channel].sample1 = av_clip_int16(sample);
  935. *samples++ = c->status[channel].sample1;
  936. }
  937. }
  938. }
  939. bytestream2_seek(&gb, 0, SEEK_END);
  940. break;
  941. }
  942. case AV_CODEC_ID_ADPCM_EA_R1:
  943. case AV_CODEC_ID_ADPCM_EA_R2:
  944. case AV_CODEC_ID_ADPCM_EA_R3: {
  945. /* channel numbering
  946. 2chan: 0=fl, 1=fr
  947. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  948. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  949. const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
  950. int previous_sample, current_sample, next_sample;
  951. int coeff1, coeff2;
  952. int shift;
  953. unsigned int channel;
  954. uint16_t *samplesC;
  955. int count = 0;
  956. int offsets[6];
  957. for (channel=0; channel<avctx->channels; channel++)
  958. offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
  959. bytestream2_get_le32(&gb)) +
  960. (avctx->channels + 1) * 4;
  961. for (channel=0; channel<avctx->channels; channel++) {
  962. bytestream2_seek(&gb, offsets[channel], SEEK_SET);
  963. samplesC = samples_p[channel];
  964. if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
  965. current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  966. previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  967. } else {
  968. current_sample = c->status[channel].predictor;
  969. previous_sample = c->status[channel].prev_sample;
  970. }
  971. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  972. int byte = bytestream2_get_byte(&gb);
  973. if (byte == 0xEE) { /* only seen in R2 and R3 */
  974. current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  975. previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  976. for (count2=0; count2<28; count2++)
  977. *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
  978. } else {
  979. coeff1 = ea_adpcm_table[ byte >> 4 ];
  980. coeff2 = ea_adpcm_table[(byte >> 4) + 4];
  981. shift = 20 - (byte & 0x0F);
  982. for (count2=0; count2<28; count2++) {
  983. if (count2 & 1)
  984. next_sample = sign_extend(byte, 4) << shift;
  985. else {
  986. byte = bytestream2_get_byte(&gb);
  987. next_sample = sign_extend(byte >> 4, 4) << shift;
  988. }
  989. next_sample += (current_sample * coeff1) +
  990. (previous_sample * coeff2);
  991. next_sample = av_clip_int16(next_sample >> 8);
  992. previous_sample = current_sample;
  993. current_sample = next_sample;
  994. *samplesC++ = current_sample;
  995. }
  996. }
  997. }
  998. if (!count) {
  999. count = count1;
  1000. } else if (count != count1) {
  1001. av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
  1002. count = FFMAX(count, count1);
  1003. }
  1004. if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
  1005. c->status[channel].predictor = current_sample;
  1006. c->status[channel].prev_sample = previous_sample;
  1007. }
  1008. }
  1009. frame->nb_samples = count * 28;
  1010. bytestream2_seek(&gb, 0, SEEK_END);
  1011. break;
  1012. }
  1013. case AV_CODEC_ID_ADPCM_EA_XAS:
  1014. for (channel=0; channel<avctx->channels; channel++) {
  1015. int coeff[2][4], shift[4];
  1016. int16_t *s = samples_p[channel];
  1017. for (n = 0; n < 4; n++, s += 32) {
  1018. int val = sign_extend(bytestream2_get_le16u(&gb), 16);
  1019. for (i=0; i<2; i++)
  1020. coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
  1021. s[0] = val & ~0x0F;
  1022. val = sign_extend(bytestream2_get_le16u(&gb), 16);
  1023. shift[n] = 20 - (val & 0x0F);
  1024. s[1] = val & ~0x0F;
  1025. }
  1026. for (m=2; m<32; m+=2) {
  1027. s = &samples_p[channel][m];
  1028. for (n = 0; n < 4; n++, s += 32) {
  1029. int level, pred;
  1030. int byte = bytestream2_get_byteu(&gb);
  1031. level = sign_extend(byte >> 4, 4) << shift[n];
  1032. pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
  1033. s[0] = av_clip_int16((level + pred + 0x80) >> 8);
  1034. level = sign_extend(byte, 4) << shift[n];
  1035. pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
  1036. s[1] = av_clip_int16((level + pred + 0x80) >> 8);
  1037. }
  1038. }
  1039. }
  1040. break;
  1041. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1042. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  1043. c->status[0].step_index = bytestream2_get_le16u(&gb);
  1044. bytestream2_skipu(&gb, 4);
  1045. if (c->status[0].step_index > 88u) {
  1046. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1047. c->status[0].step_index);
  1048. return AVERROR_INVALIDDATA;
  1049. }
  1050. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1051. int v = bytestream2_get_byteu(&gb);
  1052. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
  1053. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
  1054. }
  1055. break;
  1056. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1057. for (i = 0; i < avctx->channels; i++) {
  1058. c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  1059. c->status[i].step_index = bytestream2_get_byteu(&gb);
  1060. bytestream2_skipu(&gb, 1);
  1061. if (c->status[i].step_index > 88u) {
  1062. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1063. c->status[i].step_index);
  1064. return AVERROR_INVALIDDATA;
  1065. }
  1066. }
  1067. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1068. int v = bytestream2_get_byteu(&gb);
  1069. *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
  1070. *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
  1071. }
  1072. break;
  1073. case AV_CODEC_ID_ADPCM_CT:
  1074. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1075. int v = bytestream2_get_byteu(&gb);
  1076. *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
  1077. *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
  1078. }
  1079. break;
  1080. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1081. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1082. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1083. if (!c->status[0].step_index) {
  1084. /* the first byte is a raw sample */
  1085. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1086. if (st)
  1087. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1088. c->status[0].step_index = 1;
  1089. nb_samples--;
  1090. }
  1091. if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
  1092. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1093. int byte = bytestream2_get_byteu(&gb);
  1094. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1095. byte >> 4, 4, 0);
  1096. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1097. byte & 0x0F, 4, 0);
  1098. }
  1099. } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
  1100. for (n = nb_samples / 3; n > 0; n--) {
  1101. int byte = bytestream2_get_byteu(&gb);
  1102. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1103. byte >> 5 , 3, 0);
  1104. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1105. (byte >> 2) & 0x07, 3, 0);
  1106. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1107. byte & 0x03, 2, 0);
  1108. }
  1109. } else {
  1110. for (n = nb_samples >> (2 - st); n > 0; n--) {
  1111. int byte = bytestream2_get_byteu(&gb);
  1112. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1113. byte >> 6 , 2, 2);
  1114. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1115. (byte >> 4) & 0x03, 2, 2);
  1116. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1117. (byte >> 2) & 0x03, 2, 2);
  1118. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1119. byte & 0x03, 2, 2);
  1120. }
  1121. }
  1122. break;
  1123. case AV_CODEC_ID_ADPCM_SWF:
  1124. adpcm_swf_decode(avctx, buf, buf_size, samples);
  1125. bytestream2_seek(&gb, 0, SEEK_END);
  1126. break;
  1127. case AV_CODEC_ID_ADPCM_YAMAHA:
  1128. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1129. int v = bytestream2_get_byteu(&gb);
  1130. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
  1131. *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
  1132. }
  1133. break;
  1134. case AV_CODEC_ID_ADPCM_AFC:
  1135. {
  1136. int samples_per_block;
  1137. int blocks;
  1138. if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
  1139. samples_per_block = avctx->extradata[0] / 16;
  1140. blocks = nb_samples / avctx->extradata[0];
  1141. } else {
  1142. samples_per_block = nb_samples / 16;
  1143. blocks = 1;
  1144. }
  1145. for (m = 0; m < blocks; m++) {
  1146. for (channel = 0; channel < avctx->channels; channel++) {
  1147. int prev1 = c->status[channel].sample1;
  1148. int prev2 = c->status[channel].sample2;
  1149. samples = samples_p[channel] + m * 16;
  1150. /* Read in every sample for this channel. */
  1151. for (i = 0; i < samples_per_block; i++) {
  1152. int byte = bytestream2_get_byteu(&gb);
  1153. int scale = 1 << (byte >> 4);
  1154. int index = byte & 0xf;
  1155. int factor1 = ff_adpcm_afc_coeffs[0][index];
  1156. int factor2 = ff_adpcm_afc_coeffs[1][index];
  1157. /* Decode 16 samples. */
  1158. for (n = 0; n < 16; n++) {
  1159. int32_t sampledat;
  1160. if (n & 1) {
  1161. sampledat = sign_extend(byte, 4);
  1162. } else {
  1163. byte = bytestream2_get_byteu(&gb);
  1164. sampledat = sign_extend(byte >> 4, 4);
  1165. }
  1166. sampledat = ((prev1 * factor1 + prev2 * factor2) +
  1167. ((sampledat * scale) << 11)) >> 11;
  1168. *samples = av_clip_int16(sampledat);
  1169. prev2 = prev1;
  1170. prev1 = *samples++;
  1171. }
  1172. }
  1173. c->status[channel].sample1 = prev1;
  1174. c->status[channel].sample2 = prev2;
  1175. }
  1176. }
  1177. bytestream2_seek(&gb, 0, SEEK_END);
  1178. break;
  1179. }
  1180. case AV_CODEC_ID_ADPCM_THP:
  1181. {
  1182. int table[6][16];
  1183. int ch;
  1184. for (i = 0; i < avctx->channels; i++)
  1185. for (n = 0; n < 16; n++)
  1186. table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
  1187. /* Initialize the previous sample. */
  1188. for (i = 0; i < avctx->channels; i++) {
  1189. c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);
  1190. c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);
  1191. }
  1192. for (ch = 0; ch < avctx->channels; ch++) {
  1193. samples = samples_p[ch];
  1194. /* Read in every sample for this channel. */
  1195. for (i = 0; i < nb_samples / 14; i++) {
  1196. int byte = bytestream2_get_byteu(&gb);
  1197. int index = (byte >> 4) & 7;
  1198. unsigned int exp = byte & 0x0F;
  1199. int factor1 = table[ch][index * 2];
  1200. int factor2 = table[ch][index * 2 + 1];
  1201. /* Decode 14 samples. */
  1202. for (n = 0; n < 14; n++) {
  1203. int32_t sampledat;
  1204. if (n & 1) {
  1205. sampledat = sign_extend(byte, 4);
  1206. } else {
  1207. byte = bytestream2_get_byteu(&gb);
  1208. sampledat = sign_extend(byte >> 4, 4);
  1209. }
  1210. sampledat = ((c->status[ch].sample1 * factor1
  1211. + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
  1212. *samples = av_clip_int16(sampledat);
  1213. c->status[ch].sample2 = c->status[ch].sample1;
  1214. c->status[ch].sample1 = *samples++;
  1215. }
  1216. }
  1217. }
  1218. break;
  1219. }
  1220. default:
  1221. return -1;
  1222. }
  1223. if (avpkt->size && bytestream2_tell(&gb) == 0) {
  1224. av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
  1225. return AVERROR_INVALIDDATA;
  1226. }
  1227. *got_frame_ptr = 1;
  1228. return bytestream2_tell(&gb);
  1229. }
  1230. static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
  1231. AV_SAMPLE_FMT_NONE };
  1232. static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16,
  1233. AV_SAMPLE_FMT_NONE };
  1234. static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
  1235. AV_SAMPLE_FMT_S16P,
  1236. AV_SAMPLE_FMT_NONE };
  1237. #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
  1238. AVCodec ff_ ## name_ ## _decoder = { \
  1239. .name = #name_, \
  1240. .type = AVMEDIA_TYPE_AUDIO, \
  1241. .id = id_, \
  1242. .priv_data_size = sizeof(ADPCMDecodeContext), \
  1243. .init = adpcm_decode_init, \
  1244. .decode = adpcm_decode_frame, \
  1245. .capabilities = CODEC_CAP_DR1, \
  1246. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1247. .sample_fmts = sample_fmts_, \
  1248. }
  1249. /* Note: Do not forget to add new entries to the Makefile as well. */
  1250. ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
  1251. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
  1252. ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
  1253. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
  1254. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1255. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1256. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1257. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1258. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1259. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
  1260. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
  1261. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1262. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1263. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1264. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1265. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1266. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
  1267. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1268. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1269. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
  1270. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
  1271. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
  1272. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1273. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1274. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1275. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
  1276. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  1277. ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
  1278. ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");