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.

1231 lines
44KB

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