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.

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