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
49KB

  1. /*
  2. * Copyright (c) 2001-2003 The ffmpeg Project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avcodec.h"
  21. #include "get_bits.h"
  22. #include "put_bits.h"
  23. #include "bytestream.h"
  24. #include "adpcm.h"
  25. #include "adpcm_data.h"
  26. /**
  27. * @file
  28. * ADPCM decoders
  29. * First version by Francois Revol (revol@free.fr)
  30. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  31. * by Mike Melanson (melanson@pcisys.net)
  32. * CD-ROM XA ADPCM codec by BERO
  33. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  34. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  35. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  36. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  37. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  38. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  39. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  40. *
  41. * Features and limitations:
  42. *
  43. * Reference documents:
  44. * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs
  45. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead]
  46. * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead]
  47. * http://openquicktime.sourceforge.net/
  48. * XAnim sources (xa_codec.c) http://xanim.polter.net/
  49. * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead]
  50. * SoX source code http://sox.sourceforge.net/
  51. *
  52. * CD-ROM XA:
  53. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead]
  54. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead]
  55. * readstr http://www.geocities.co.jp/Playtown/2004/
  56. */
  57. /* These are for CD-ROM XA ADPCM */
  58. static const int xa_adpcm_table[5][2] = {
  59. { 0, 0 },
  60. { 60, 0 },
  61. { 115, -52 },
  62. { 98, -55 },
  63. { 122, -60 }
  64. };
  65. static const int ea_adpcm_table[] = {
  66. 0, 240, 460, 392,
  67. 0, 0, -208, -220,
  68. 0, 1, 3, 4,
  69. 7, 8, 10, 11,
  70. 0, -1, -3, -4
  71. };
  72. // padded to zero where table size is less then 16
  73. static const int swf_index_tables[4][16] = {
  74. /*2*/ { -1, 2 },
  75. /*3*/ { -1, -1, 2, 4 },
  76. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  77. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  78. };
  79. /* end of tables */
  80. typedef struct ADPCMDecodeContext {
  81. AVFrame frame;
  82. ADPCMChannelStatus status[6];
  83. int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
  84. } ADPCMDecodeContext;
  85. static av_cold int adpcm_decode_init(AVCodecContext * avctx)
  86. {
  87. ADPCMDecodeContext *c = avctx->priv_data;
  88. unsigned int min_channels = 1;
  89. unsigned int max_channels = 2;
  90. switch(avctx->codec->id) {
  91. case AV_CODEC_ID_ADPCM_EA:
  92. min_channels = 2;
  93. break;
  94. case AV_CODEC_ID_ADPCM_EA_R1:
  95. case AV_CODEC_ID_ADPCM_EA_R2:
  96. case AV_CODEC_ID_ADPCM_EA_R3:
  97. case AV_CODEC_ID_ADPCM_EA_XAS:
  98. max_channels = 6;
  99. break;
  100. }
  101. if (avctx->channels < min_channels || avctx->channels > max_channels) {
  102. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  103. return AVERROR(EINVAL);
  104. }
  105. switch(avctx->codec->id) {
  106. case AV_CODEC_ID_ADPCM_CT:
  107. c->status[0].step = c->status[1].step = 511;
  108. break;
  109. case AV_CODEC_ID_ADPCM_IMA_WAV:
  110. if (avctx->bits_per_coded_sample != 4) {
  111. av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n");
  112. return -1;
  113. }
  114. break;
  115. case AV_CODEC_ID_ADPCM_IMA_APC:
  116. if (avctx->extradata && avctx->extradata_size >= 8) {
  117. c->status[0].predictor = AV_RL32(avctx->extradata);
  118. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  119. }
  120. break;
  121. case AV_CODEC_ID_ADPCM_IMA_WS:
  122. if (avctx->extradata && avctx->extradata_size >= 2)
  123. c->vqa_version = AV_RL16(avctx->extradata);
  124. break;
  125. default:
  126. break;
  127. }
  128. 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 >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  242. av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
  243. filter=0;
  244. }
  245. f0 = xa_adpcm_table[filter][0];
  246. f1 = xa_adpcm_table[filter][1];
  247. s_1 = left->sample1;
  248. s_2 = left->sample2;
  249. for(j=0;j<28;j++) {
  250. d = in[16+i+j*4];
  251. t = sign_extend(d, 4);
  252. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  253. s_2 = s_1;
  254. s_1 = av_clip_int16(s);
  255. *out = s_1;
  256. out += inc;
  257. }
  258. if (inc==2) { /* stereo */
  259. left->sample1 = s_1;
  260. left->sample2 = s_2;
  261. s_1 = right->sample1;
  262. s_2 = right->sample2;
  263. out = out + 1 - 28*2;
  264. }
  265. shift = 12 - (in[5+i*2] & 15);
  266. filter = in[5+i*2] >> 4;
  267. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  268. av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
  269. filter=0;
  270. }
  271. f0 = xa_adpcm_table[filter][0];
  272. f1 = xa_adpcm_table[filter][1];
  273. for(j=0;j<28;j++) {
  274. d = in[16+i+j*4];
  275. t = sign_extend(d >> 4, 4);
  276. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  277. s_2 = s_1;
  278. s_1 = av_clip_int16(s);
  279. *out = s_1;
  280. out += inc;
  281. }
  282. if (inc==2) { /* stereo */
  283. right->sample1 = s_1;
  284. right->sample2 = s_2;
  285. out -= 1;
  286. } else {
  287. left->sample1 = s_1;
  288. left->sample2 = s_2;
  289. }
  290. }
  291. return 0;
  292. }
  293. static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
  294. {
  295. ADPCMDecodeContext *c = avctx->priv_data;
  296. GetBitContext gb;
  297. const int *table;
  298. int k0, signmask, nb_bits, count;
  299. int size = buf_size*8;
  300. int i;
  301. init_get_bits(&gb, buf, size);
  302. //read bits & initial values
  303. nb_bits = get_bits(&gb, 2)+2;
  304. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  305. table = swf_index_tables[nb_bits-2];
  306. k0 = 1 << (nb_bits-2);
  307. signmask = 1 << (nb_bits-1);
  308. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  309. for (i = 0; i < avctx->channels; i++) {
  310. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  311. c->status[i].step_index = get_bits(&gb, 6);
  312. }
  313. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  314. int i;
  315. for (i = 0; i < avctx->channels; i++) {
  316. // similar to IMA adpcm
  317. int delta = get_bits(&gb, nb_bits);
  318. int step = ff_adpcm_step_table[c->status[i].step_index];
  319. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  320. int k = k0;
  321. do {
  322. if (delta & k)
  323. vpdiff += step;
  324. step >>= 1;
  325. k >>= 1;
  326. } while(k);
  327. vpdiff += step;
  328. if (delta & signmask)
  329. c->status[i].predictor -= vpdiff;
  330. else
  331. c->status[i].predictor += vpdiff;
  332. c->status[i].step_index += table[delta & (~signmask)];
  333. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  334. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  335. *samples++ = c->status[i].predictor;
  336. }
  337. }
  338. }
  339. }
  340. /**
  341. * Get the number of samples that will be decoded from the packet.
  342. * In one case, this is actually the maximum number of samples possible to
  343. * decode with the given buf_size.
  344. *
  345. * @param[out] coded_samples set to the number of samples as coded in the
  346. * packet, or 0 if the codec does not encode the
  347. * number of samples in each frame.
  348. */
  349. static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
  350. int buf_size, int *coded_samples)
  351. {
  352. ADPCMDecodeContext *s = avctx->priv_data;
  353. int nb_samples = 0;
  354. int ch = avctx->channels;
  355. int has_coded_samples = 0;
  356. int header_size;
  357. *coded_samples = 0;
  358. if(ch <= 0)
  359. return 0;
  360. switch (avctx->codec->id) {
  361. /* constant, only check buf_size */
  362. case AV_CODEC_ID_ADPCM_EA_XAS:
  363. if (buf_size < 76 * ch)
  364. return 0;
  365. nb_samples = 128;
  366. break;
  367. case AV_CODEC_ID_ADPCM_IMA_QT:
  368. if (buf_size < 34 * ch)
  369. return 0;
  370. nb_samples = 64;
  371. break;
  372. /* simple 4-bit adpcm */
  373. case AV_CODEC_ID_ADPCM_CT:
  374. case AV_CODEC_ID_ADPCM_IMA_APC:
  375. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  376. case AV_CODEC_ID_ADPCM_IMA_WS:
  377. case AV_CODEC_ID_ADPCM_YAMAHA:
  378. nb_samples = buf_size * 2 / ch;
  379. break;
  380. }
  381. if (nb_samples)
  382. return nb_samples;
  383. /* simple 4-bit adpcm, with header */
  384. header_size = 0;
  385. switch (avctx->codec->id) {
  386. case AV_CODEC_ID_ADPCM_4XM:
  387. case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
  388. case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
  389. case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4; break;
  390. }
  391. if (header_size > 0)
  392. return (buf_size - header_size) * 2 / ch;
  393. /* more complex formats */
  394. switch (avctx->codec->id) {
  395. case AV_CODEC_ID_ADPCM_EA:
  396. has_coded_samples = 1;
  397. *coded_samples = bytestream2_get_le32(gb);
  398. *coded_samples -= *coded_samples % 28;
  399. nb_samples = (buf_size - 12) / 30 * 28;
  400. break;
  401. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  402. has_coded_samples = 1;
  403. *coded_samples = bytestream2_get_le32(gb);
  404. nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
  405. break;
  406. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  407. nb_samples = (buf_size - ch) / ch * 2;
  408. break;
  409. case AV_CODEC_ID_ADPCM_EA_R1:
  410. case AV_CODEC_ID_ADPCM_EA_R2:
  411. case AV_CODEC_ID_ADPCM_EA_R3:
  412. /* maximum number of samples */
  413. /* has internal offsets and a per-frame switch to signal raw 16-bit */
  414. has_coded_samples = 1;
  415. switch (avctx->codec->id) {
  416. case AV_CODEC_ID_ADPCM_EA_R1:
  417. header_size = 4 + 9 * ch;
  418. *coded_samples = bytestream2_get_le32(gb);
  419. break;
  420. case AV_CODEC_ID_ADPCM_EA_R2:
  421. header_size = 4 + 5 * ch;
  422. *coded_samples = bytestream2_get_le32(gb);
  423. break;
  424. case AV_CODEC_ID_ADPCM_EA_R3:
  425. header_size = 4 + 5 * ch;
  426. *coded_samples = bytestream2_get_be32(gb);
  427. break;
  428. }
  429. *coded_samples -= *coded_samples % 28;
  430. nb_samples = (buf_size - header_size) * 2 / ch;
  431. nb_samples -= nb_samples % 28;
  432. break;
  433. case AV_CODEC_ID_ADPCM_IMA_DK3:
  434. if (avctx->block_align > 0)
  435. buf_size = FFMIN(buf_size, avctx->block_align);
  436. nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
  437. break;
  438. case AV_CODEC_ID_ADPCM_IMA_DK4:
  439. if (avctx->block_align > 0)
  440. buf_size = FFMIN(buf_size, avctx->block_align);
  441. nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
  442. break;
  443. case AV_CODEC_ID_ADPCM_IMA_WAV:
  444. if (avctx->block_align > 0)
  445. buf_size = FFMIN(buf_size, avctx->block_align);
  446. nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
  447. break;
  448. case AV_CODEC_ID_ADPCM_MS:
  449. if (avctx->block_align > 0)
  450. buf_size = FFMIN(buf_size, avctx->block_align);
  451. nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
  452. break;
  453. case AV_CODEC_ID_ADPCM_SBPRO_2:
  454. case AV_CODEC_ID_ADPCM_SBPRO_3:
  455. case AV_CODEC_ID_ADPCM_SBPRO_4:
  456. {
  457. int samples_per_byte;
  458. switch (avctx->codec->id) {
  459. case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
  460. case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
  461. case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
  462. }
  463. if (!s->status[0].step_index) {
  464. nb_samples++;
  465. buf_size -= ch;
  466. }
  467. nb_samples += buf_size * samples_per_byte / ch;
  468. break;
  469. }
  470. case AV_CODEC_ID_ADPCM_SWF:
  471. {
  472. int buf_bits = buf_size * 8 - 2;
  473. int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
  474. int block_hdr_size = 22 * ch;
  475. int block_size = block_hdr_size + nbits * ch * 4095;
  476. int nblocks = buf_bits / block_size;
  477. int bits_left = buf_bits - nblocks * block_size;
  478. nb_samples = nblocks * 4096;
  479. if (bits_left >= block_hdr_size)
  480. nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
  481. break;
  482. }
  483. case AV_CODEC_ID_ADPCM_THP:
  484. has_coded_samples = 1;
  485. bytestream2_skip(gb, 4); // channel size
  486. *coded_samples = bytestream2_get_be32(gb);
  487. *coded_samples -= *coded_samples % 14;
  488. nb_samples = (buf_size - 80) / (8 * ch) * 14;
  489. break;
  490. case AV_CODEC_ID_ADPCM_XA:
  491. nb_samples = (buf_size / 128) * 224 / ch;
  492. break;
  493. }
  494. /* validate coded sample count */
  495. if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
  496. return AVERROR_INVALIDDATA;
  497. return nb_samples;
  498. }
  499. static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
  500. int *got_frame_ptr, AVPacket *avpkt)
  501. {
  502. const uint8_t *buf = avpkt->data;
  503. int buf_size = avpkt->size;
  504. ADPCMDecodeContext *c = avctx->priv_data;
  505. ADPCMChannelStatus *cs;
  506. int n, m, channel, i;
  507. short *samples;
  508. int st; /* stereo */
  509. int count1, count2;
  510. int nb_samples, coded_samples, ret;
  511. GetByteContext gb;
  512. bytestream2_init(&gb, buf, buf_size);
  513. nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
  514. if (nb_samples <= 0) {
  515. av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
  516. return AVERROR_INVALIDDATA;
  517. }
  518. /* get output buffer */
  519. c->frame.nb_samples = nb_samples;
  520. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  521. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  522. return ret;
  523. }
  524. samples = (short *)c->frame.data[0];
  525. /* use coded_samples when applicable */
  526. /* it is always <= nb_samples, so the output buffer will be large enough */
  527. if (coded_samples) {
  528. if (coded_samples != nb_samples)
  529. av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
  530. c->frame.nb_samples = nb_samples = coded_samples;
  531. }
  532. st = avctx->channels == 2 ? 1 : 0;
  533. switch(avctx->codec->id) {
  534. case AV_CODEC_ID_ADPCM_IMA_QT:
  535. /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
  536. Channel data is interleaved per-chunk. */
  537. for (channel = 0; channel < avctx->channels; channel++) {
  538. int predictor;
  539. int step_index;
  540. cs = &(c->status[channel]);
  541. /* (pppppp) (piiiiiii) */
  542. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  543. predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  544. step_index = predictor & 0x7F;
  545. predictor &= ~0x7F;
  546. if (cs->step_index == step_index) {
  547. int diff = predictor - cs->predictor;
  548. if (diff < 0)
  549. diff = - diff;
  550. if (diff > 0x7f)
  551. goto update;
  552. } else {
  553. update:
  554. cs->step_index = step_index;
  555. cs->predictor = predictor;
  556. }
  557. if (cs->step_index > 88u){
  558. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  559. channel, cs->step_index);
  560. return AVERROR_INVALIDDATA;
  561. }
  562. samples = (short *)c->frame.data[0] + channel;
  563. for (m = 0; m < 32; m++) {
  564. int byte = bytestream2_get_byteu(&gb);
  565. *samples = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
  566. samples += avctx->channels;
  567. *samples = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
  568. samples += avctx->channels;
  569. }
  570. }
  571. break;
  572. case AV_CODEC_ID_ADPCM_IMA_WAV:
  573. for(i=0; i<avctx->channels; i++){
  574. cs = &(c->status[i]);
  575. cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
  576. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  577. if (cs->step_index > 88u){
  578. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  579. i, cs->step_index);
  580. return AVERROR_INVALIDDATA;
  581. }
  582. }
  583. for (n = (nb_samples - 1) / 8; n > 0; n--) {
  584. for (i = 0; i < avctx->channels; i++) {
  585. cs = &c->status[i];
  586. for (m = 0; m < 4; m++) {
  587. int v = bytestream2_get_byteu(&gb);
  588. *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  589. samples += avctx->channels;
  590. *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  591. samples += avctx->channels;
  592. }
  593. samples -= 8 * avctx->channels - 1;
  594. }
  595. samples += 7 * avctx->channels;
  596. }
  597. break;
  598. case AV_CODEC_ID_ADPCM_4XM:
  599. for (i = 0; i < avctx->channels; i++)
  600. c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  601. for (i = 0; i < avctx->channels; i++) {
  602. c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  603. if (c->status[i].step_index > 88u) {
  604. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  605. i, c->status[i].step_index);
  606. return AVERROR_INVALIDDATA;
  607. }
  608. }
  609. for (i = 0; i < avctx->channels; i++) {
  610. samples = (short *)c->frame.data[0] + i;
  611. cs = &c->status[i];
  612. for (n = nb_samples >> 1; n > 0; n--) {
  613. int v = bytestream2_get_byteu(&gb);
  614. *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
  615. samples += avctx->channels;
  616. *samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
  617. samples += avctx->channels;
  618. }
  619. }
  620. break;
  621. case AV_CODEC_ID_ADPCM_MS:
  622. {
  623. int block_predictor;
  624. block_predictor = bytestream2_get_byteu(&gb);
  625. if (block_predictor > 6) {
  626. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
  627. block_predictor);
  628. return AVERROR_INVALIDDATA;
  629. }
  630. c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  631. c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  632. if (st) {
  633. block_predictor = bytestream2_get_byteu(&gb);
  634. if (block_predictor > 6) {
  635. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
  636. block_predictor);
  637. return AVERROR_INVALIDDATA;
  638. }
  639. c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  640. c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  641. }
  642. c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  643. if (st){
  644. c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  645. }
  646. c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  647. if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  648. c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  649. if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  650. *samples++ = c->status[0].sample2;
  651. if (st) *samples++ = c->status[1].sample2;
  652. *samples++ = c->status[0].sample1;
  653. if (st) *samples++ = c->status[1].sample1;
  654. for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
  655. int byte = bytestream2_get_byteu(&gb);
  656. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
  657. *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
  658. }
  659. break;
  660. }
  661. case AV_CODEC_ID_ADPCM_IMA_DK4:
  662. for (channel = 0; channel < avctx->channels; channel++) {
  663. cs = &c->status[channel];
  664. cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
  665. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  666. if (cs->step_index > 88u){
  667. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  668. channel, cs->step_index);
  669. return AVERROR_INVALIDDATA;
  670. }
  671. }
  672. for (n = nb_samples >> (1 - st); n > 0; n--) {
  673. int v = bytestream2_get_byteu(&gb);
  674. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
  675. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  676. }
  677. break;
  678. case AV_CODEC_ID_ADPCM_IMA_DK3:
  679. {
  680. int last_byte = 0;
  681. int nibble;
  682. int decode_top_nibble_next = 0;
  683. int diff_channel;
  684. const int16_t *samples_end = samples + avctx->channels * nb_samples;
  685. bytestream2_skipu(&gb, 10);
  686. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  687. c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  688. c->status[0].step_index = bytestream2_get_byteu(&gb);
  689. c->status[1].step_index = bytestream2_get_byteu(&gb);
  690. if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
  691. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
  692. c->status[0].step_index, c->status[1].step_index);
  693. return AVERROR_INVALIDDATA;
  694. }
  695. /* sign extend the predictors */
  696. diff_channel = c->status[1].predictor;
  697. /* DK3 ADPCM support macro */
  698. #define DK3_GET_NEXT_NIBBLE() \
  699. if (decode_top_nibble_next) { \
  700. nibble = last_byte >> 4; \
  701. decode_top_nibble_next = 0; \
  702. } else { \
  703. last_byte = bytestream2_get_byteu(&gb); \
  704. nibble = last_byte & 0x0F; \
  705. decode_top_nibble_next = 1; \
  706. }
  707. while (samples < samples_end) {
  708. /* for this algorithm, c->status[0] is the sum channel and
  709. * c->status[1] is the diff channel */
  710. /* process the first predictor of the sum channel */
  711. DK3_GET_NEXT_NIBBLE();
  712. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  713. /* process the diff channel predictor */
  714. DK3_GET_NEXT_NIBBLE();
  715. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  716. /* process the first pair of stereo PCM samples */
  717. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  718. *samples++ = c->status[0].predictor + c->status[1].predictor;
  719. *samples++ = c->status[0].predictor - c->status[1].predictor;
  720. /* process the second predictor of the sum channel */
  721. DK3_GET_NEXT_NIBBLE();
  722. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  723. /* process the second pair of stereo PCM samples */
  724. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  725. *samples++ = c->status[0].predictor + c->status[1].predictor;
  726. *samples++ = c->status[0].predictor - c->status[1].predictor;
  727. }
  728. break;
  729. }
  730. case AV_CODEC_ID_ADPCM_IMA_ISS:
  731. for (channel = 0; channel < avctx->channels; channel++) {
  732. cs = &c->status[channel];
  733. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  734. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  735. if (cs->step_index > 88u){
  736. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  737. channel, cs->step_index);
  738. return AVERROR_INVALIDDATA;
  739. }
  740. }
  741. for (n = nb_samples >> (1 - st); n > 0; n--) {
  742. int v1, v2;
  743. int v = bytestream2_get_byteu(&gb);
  744. /* nibbles are swapped for mono */
  745. if (st) {
  746. v1 = v >> 4;
  747. v2 = v & 0x0F;
  748. } else {
  749. v2 = v >> 4;
  750. v1 = v & 0x0F;
  751. }
  752. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
  753. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
  754. }
  755. break;
  756. case AV_CODEC_ID_ADPCM_IMA_APC:
  757. while (bytestream2_get_bytes_left(&gb) > 0) {
  758. int v = bytestream2_get_byteu(&gb);
  759. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
  760. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  761. }
  762. break;
  763. case AV_CODEC_ID_ADPCM_IMA_WS:
  764. if (c->vqa_version == 3) {
  765. for (channel = 0; channel < avctx->channels; channel++) {
  766. int16_t *smp = samples + channel;
  767. for (n = nb_samples / 2; n > 0; n--) {
  768. int v = bytestream2_get_byteu(&gb);
  769. *smp = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  770. smp += avctx->channels;
  771. *smp = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  772. smp += avctx->channels;
  773. }
  774. }
  775. } else {
  776. for (n = nb_samples / 2; n > 0; n--) {
  777. for (channel = 0; channel < avctx->channels; channel++) {
  778. int v = bytestream2_get_byteu(&gb);
  779. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  780. samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  781. }
  782. samples += avctx->channels;
  783. }
  784. }
  785. bytestream2_seek(&gb, 0, SEEK_END);
  786. break;
  787. case AV_CODEC_ID_ADPCM_XA:
  788. while (bytestream2_get_bytes_left(&gb) >= 128) {
  789. if ((ret = xa_decode(avctx, samples, buf + bytestream2_tell(&gb), &c->status[0],
  790. &c->status[1], avctx->channels)) < 0)
  791. return ret;
  792. bytestream2_skipu(&gb, 128);
  793. samples += 28 * 8;
  794. }
  795. break;
  796. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  797. for (i=0; i<=st; i++) {
  798. c->status[i].step_index = bytestream2_get_le32u(&gb);
  799. if (c->status[i].step_index > 88u) {
  800. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  801. i, c->status[i].step_index);
  802. return AVERROR_INVALIDDATA;
  803. }
  804. }
  805. for (i=0; i<=st; i++)
  806. c->status[i].predictor = bytestream2_get_le32u(&gb);
  807. for (n = nb_samples >> (1 - st); n > 0; n--) {
  808. int byte = bytestream2_get_byteu(&gb);
  809. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
  810. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
  811. }
  812. break;
  813. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  814. for (n = nb_samples >> (1 - st); n > 0; n--) {
  815. int byte = bytestream2_get_byteu(&gb);
  816. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
  817. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
  818. }
  819. break;
  820. case AV_CODEC_ID_ADPCM_EA:
  821. {
  822. int previous_left_sample, previous_right_sample;
  823. int current_left_sample, current_right_sample;
  824. int next_left_sample, next_right_sample;
  825. int coeff1l, coeff2l, coeff1r, coeff2r;
  826. int shift_left, shift_right;
  827. /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
  828. each coding 28 stereo samples. */
  829. if(avctx->channels != 2)
  830. return AVERROR_INVALIDDATA;
  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 AV_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 AV_CODEC_ID_ADPCM_EA_R1:
  894. case AV_CODEC_ID_ADPCM_EA_R2:
  895. case AV_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 == AV_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 == AV_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 != AV_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 AV_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 AV_CODEC_ID_ADPCM_IMA_AMV:
  995. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  996. if (avctx->codec->id == AV_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 == AV_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 AV_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 AV_CODEC_ID_ADPCM_SBPRO_4:
  1031. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1032. case AV_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 == AV_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 == AV_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 AV_CODEC_ID_ADPCM_SWF:
  1074. adpcm_swf_decode(avctx, buf, buf_size, samples);
  1075. bytestream2_seek(&gb, 0, SEEK_END);
  1076. break;
  1077. case AV_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 AV_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(AV_CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
  1147. ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
  1148. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
  1149. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1150. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1151. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1152. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1153. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1154. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
  1155. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, adpcm_ima_apc, "ADPCM IMA CRYO APC");
  1156. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1157. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1158. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1159. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1160. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1161. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1162. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1163. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
  1164. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
  1165. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
  1166. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1167. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1168. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1169. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
  1170. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  1171. ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
  1172. ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");