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.

1321 lines
50KB

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