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.

1309 lines
49KB

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