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.

1718 lines
66KB

  1. /*
  2. * Copyright (c) 2001-2003 The FFmpeg Project
  3. *
  4. * first version by Francois Revol (revol@free.fr)
  5. * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  6. * by Mike Melanson (melanson@pcisys.net)
  7. * CD-ROM XA ADPCM codec by BERO
  8. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  9. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  10. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  11. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  12. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  13. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  14. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  15. *
  16. * This file is part of FFmpeg.
  17. *
  18. * FFmpeg is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU Lesser General Public
  20. * License as published by the Free Software Foundation; either
  21. * version 2.1 of the License, or (at your option) any later version.
  22. *
  23. * FFmpeg is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * Lesser General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Lesser General Public
  29. * License along with FFmpeg; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  31. */
  32. #include "avcodec.h"
  33. #include "get_bits.h"
  34. #include "bytestream.h"
  35. #include "adpcm.h"
  36. #include "adpcm_data.h"
  37. #include "internal.h"
  38. /**
  39. * @file
  40. * ADPCM decoders
  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. ADPCMChannelStatus status[14];
  82. int vqa_version; /**< VQA version. Used for ADPCM_IMA_WS */
  83. int has_status;
  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_DTK:
  92. case AV_CODEC_ID_ADPCM_EA:
  93. min_channels = 2;
  94. break;
  95. case AV_CODEC_ID_ADPCM_AFC:
  96. case AV_CODEC_ID_ADPCM_EA_R1:
  97. case AV_CODEC_ID_ADPCM_EA_R2:
  98. case AV_CODEC_ID_ADPCM_EA_R3:
  99. case AV_CODEC_ID_ADPCM_EA_XAS:
  100. max_channels = 6;
  101. break;
  102. case AV_CODEC_ID_ADPCM_PSX:
  103. max_channels = 8;
  104. break;
  105. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  106. case AV_CODEC_ID_ADPCM_THP:
  107. case AV_CODEC_ID_ADPCM_THP_LE:
  108. max_channels = 14;
  109. break;
  110. }
  111. if (avctx->channels < min_channels || avctx->channels > max_channels) {
  112. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  113. return AVERROR(EINVAL);
  114. }
  115. switch(avctx->codec->id) {
  116. case AV_CODEC_ID_ADPCM_CT:
  117. c->status[0].step = c->status[1].step = 511;
  118. break;
  119. case AV_CODEC_ID_ADPCM_IMA_WAV:
  120. if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)
  121. return AVERROR_INVALIDDATA;
  122. break;
  123. case AV_CODEC_ID_ADPCM_IMA_APC:
  124. if (avctx->extradata && avctx->extradata_size >= 8) {
  125. c->status[0].predictor = AV_RL32(avctx->extradata);
  126. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  127. }
  128. break;
  129. case AV_CODEC_ID_ADPCM_IMA_WS:
  130. if (avctx->extradata && avctx->extradata_size >= 2)
  131. c->vqa_version = AV_RL16(avctx->extradata);
  132. break;
  133. default:
  134. break;
  135. }
  136. switch(avctx->codec->id) {
  137. case AV_CODEC_ID_ADPCM_AICA:
  138. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  139. case AV_CODEC_ID_ADPCM_IMA_QT:
  140. case AV_CODEC_ID_ADPCM_IMA_WAV:
  141. case AV_CODEC_ID_ADPCM_4XM:
  142. case AV_CODEC_ID_ADPCM_XA:
  143. case AV_CODEC_ID_ADPCM_EA_R1:
  144. case AV_CODEC_ID_ADPCM_EA_R2:
  145. case AV_CODEC_ID_ADPCM_EA_R3:
  146. case AV_CODEC_ID_ADPCM_EA_XAS:
  147. case AV_CODEC_ID_ADPCM_THP:
  148. case AV_CODEC_ID_ADPCM_THP_LE:
  149. case AV_CODEC_ID_ADPCM_AFC:
  150. case AV_CODEC_ID_ADPCM_DTK:
  151. case AV_CODEC_ID_ADPCM_PSX:
  152. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  153. break;
  154. case AV_CODEC_ID_ADPCM_IMA_WS:
  155. avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :
  156. AV_SAMPLE_FMT_S16;
  157. break;
  158. default:
  159. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  160. }
  161. return 0;
  162. }
  163. static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift)
  164. {
  165. int step_index;
  166. int predictor;
  167. int sign, delta, diff, step;
  168. step = ff_adpcm_step_table[c->step_index];
  169. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  170. step_index = av_clip(step_index, 0, 88);
  171. sign = nibble & 8;
  172. delta = nibble & 7;
  173. /* perform direct multiplication instead of series of jumps proposed by
  174. * the reference ADPCM implementation since modern CPUs can do the mults
  175. * quickly enough */
  176. diff = ((2 * delta + 1) * step) >> shift;
  177. predictor = c->predictor;
  178. if (sign) predictor -= diff;
  179. else predictor += diff;
  180. c->predictor = av_clip_int16(predictor);
  181. c->step_index = step_index;
  182. return (int16_t)c->predictor;
  183. }
  184. static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
  185. {
  186. int nibble, step_index, predictor, sign, delta, diff, step, shift;
  187. shift = bps - 1;
  188. nibble = get_bits_le(gb, bps),
  189. step = ff_adpcm_step_table[c->step_index];
  190. step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];
  191. step_index = av_clip(step_index, 0, 88);
  192. sign = nibble & (1 << shift);
  193. delta = av_mod_uintp2(nibble, shift);
  194. diff = ((2 * delta + 1) * step) >> shift;
  195. predictor = c->predictor;
  196. if (sign) predictor -= diff;
  197. else predictor += diff;
  198. c->predictor = av_clip_int16(predictor);
  199. c->step_index = step_index;
  200. return (int16_t)c->predictor;
  201. }
  202. static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift)
  203. {
  204. int step_index;
  205. int predictor;
  206. int diff, step;
  207. step = ff_adpcm_step_table[c->step_index];
  208. step_index = c->step_index + ff_adpcm_index_table[nibble];
  209. step_index = av_clip(step_index, 0, 88);
  210. diff = step >> 3;
  211. if (nibble & 4) diff += step;
  212. if (nibble & 2) diff += step >> 1;
  213. if (nibble & 1) diff += step >> 2;
  214. if (nibble & 8)
  215. predictor = c->predictor - diff;
  216. else
  217. predictor = c->predictor + diff;
  218. c->predictor = av_clip_int16(predictor);
  219. c->step_index = step_index;
  220. return c->predictor;
  221. }
  222. static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
  223. {
  224. int predictor;
  225. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  226. predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  227. c->sample2 = c->sample1;
  228. c->sample1 = av_clip_int16(predictor);
  229. c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
  230. if (c->idelta < 16) c->idelta = 16;
  231. if (c->idelta > INT_MAX/768) {
  232. av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
  233. c->idelta = INT_MAX/768;
  234. }
  235. return c->sample1;
  236. }
  237. static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble)
  238. {
  239. int step_index, predictor, sign, delta, diff, step;
  240. step = ff_adpcm_oki_step_table[c->step_index];
  241. step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
  242. step_index = av_clip(step_index, 0, 48);
  243. sign = nibble & 8;
  244. delta = nibble & 7;
  245. diff = ((2 * delta + 1) * step) >> 3;
  246. predictor = c->predictor;
  247. if (sign) predictor -= diff;
  248. else predictor += diff;
  249. c->predictor = av_clip_intp2(predictor, 11);
  250. c->step_index = step_index;
  251. return c->predictor << 4;
  252. }
  253. static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
  254. {
  255. int sign, delta, diff;
  256. int new_step;
  257. sign = nibble & 8;
  258. delta = nibble & 7;
  259. /* perform direct multiplication instead of series of jumps proposed by
  260. * the reference ADPCM implementation since modern CPUs can do the mults
  261. * quickly enough */
  262. diff = ((2 * delta + 1) * c->step) >> 3;
  263. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  264. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  265. c->predictor = av_clip_int16(c->predictor);
  266. /* calculate new step and clamp it to range 511..32767 */
  267. new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
  268. c->step = av_clip(new_step, 511, 32767);
  269. return (int16_t)c->predictor;
  270. }
  271. static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift)
  272. {
  273. int sign, delta, diff;
  274. sign = nibble & (1<<(size-1));
  275. delta = nibble & ((1<<(size-1))-1);
  276. diff = delta << (7 + c->step + shift);
  277. /* clamp result */
  278. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  279. /* calculate new step */
  280. if (delta >= (2*size - 3) && c->step < 3)
  281. c->step++;
  282. else if (delta == 0 && c->step > 0)
  283. c->step--;
  284. return (int16_t) c->predictor;
  285. }
  286. static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble)
  287. {
  288. if(!c->step) {
  289. c->predictor = 0;
  290. c->step = 127;
  291. }
  292. c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
  293. c->predictor = av_clip_int16(c->predictor);
  294. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  295. c->step = av_clip(c->step, 127, 24567);
  296. return c->predictor;
  297. }
  298. static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,
  299. const uint8_t *in, ADPCMChannelStatus *left,
  300. ADPCMChannelStatus *right, int channels, int sample_offset)
  301. {
  302. int i, j;
  303. int shift,filter,f0,f1;
  304. int s_1,s_2;
  305. int d,s,t;
  306. out0 += sample_offset;
  307. if (channels == 1)
  308. out1 = out0 + 28;
  309. else
  310. out1 += sample_offset;
  311. for(i=0;i<4;i++) {
  312. shift = 12 - (in[4+i*2] & 15);
  313. filter = in[4+i*2] >> 4;
  314. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  315. avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
  316. filter=0;
  317. }
  318. f0 = xa_adpcm_table[filter][0];
  319. f1 = xa_adpcm_table[filter][1];
  320. s_1 = left->sample1;
  321. s_2 = left->sample2;
  322. for(j=0;j<28;j++) {
  323. d = in[16+i+j*4];
  324. t = sign_extend(d, 4);
  325. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  326. s_2 = s_1;
  327. s_1 = av_clip_int16(s);
  328. out0[j] = s_1;
  329. }
  330. if (channels == 2) {
  331. left->sample1 = s_1;
  332. left->sample2 = s_2;
  333. s_1 = right->sample1;
  334. s_2 = right->sample2;
  335. }
  336. shift = 12 - (in[5+i*2] & 15);
  337. filter = in[5+i*2] >> 4;
  338. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
  339. avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
  340. filter=0;
  341. }
  342. f0 = xa_adpcm_table[filter][0];
  343. f1 = xa_adpcm_table[filter][1];
  344. for(j=0;j<28;j++) {
  345. d = in[16+i+j*4];
  346. t = sign_extend(d >> 4, 4);
  347. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  348. s_2 = s_1;
  349. s_1 = av_clip_int16(s);
  350. out1[j] = s_1;
  351. }
  352. if (channels == 2) {
  353. right->sample1 = s_1;
  354. right->sample2 = s_2;
  355. } else {
  356. left->sample1 = s_1;
  357. left->sample2 = s_2;
  358. }
  359. out0 += 28 * (3 - channels);
  360. out1 += 28 * (3 - channels);
  361. }
  362. return 0;
  363. }
  364. static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
  365. {
  366. ADPCMDecodeContext *c = avctx->priv_data;
  367. GetBitContext gb;
  368. const int *table;
  369. int k0, signmask, nb_bits, count;
  370. int size = buf_size*8;
  371. int i;
  372. init_get_bits(&gb, buf, size);
  373. //read bits & initial values
  374. nb_bits = get_bits(&gb, 2)+2;
  375. table = swf_index_tables[nb_bits-2];
  376. k0 = 1 << (nb_bits-2);
  377. signmask = 1 << (nb_bits-1);
  378. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  379. for (i = 0; i < avctx->channels; i++) {
  380. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  381. c->status[i].step_index = get_bits(&gb, 6);
  382. }
  383. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  384. int i;
  385. for (i = 0; i < avctx->channels; i++) {
  386. // similar to IMA adpcm
  387. int delta = get_bits(&gb, nb_bits);
  388. int step = ff_adpcm_step_table[c->status[i].step_index];
  389. int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  390. int k = k0;
  391. do {
  392. if (delta & k)
  393. vpdiff += step;
  394. step >>= 1;
  395. k >>= 1;
  396. } while(k);
  397. vpdiff += step;
  398. if (delta & signmask)
  399. c->status[i].predictor -= vpdiff;
  400. else
  401. c->status[i].predictor += vpdiff;
  402. c->status[i].step_index += table[delta & (~signmask)];
  403. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  404. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  405. *samples++ = c->status[i].predictor;
  406. }
  407. }
  408. }
  409. }
  410. /**
  411. * Get the number of samples that will be decoded from the packet.
  412. * In one case, this is actually the maximum number of samples possible to
  413. * decode with the given buf_size.
  414. *
  415. * @param[out] coded_samples set to the number of samples as coded in the
  416. * packet, or 0 if the codec does not encode the
  417. * number of samples in each frame.
  418. * @param[out] approx_nb_samples set to non-zero if the number of samples
  419. * returned is an approximation.
  420. */
  421. static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
  422. int buf_size, int *coded_samples, int *approx_nb_samples)
  423. {
  424. ADPCMDecodeContext *s = avctx->priv_data;
  425. int nb_samples = 0;
  426. int ch = avctx->channels;
  427. int has_coded_samples = 0;
  428. int header_size;
  429. *coded_samples = 0;
  430. *approx_nb_samples = 0;
  431. if(ch <= 0)
  432. return 0;
  433. switch (avctx->codec->id) {
  434. /* constant, only check buf_size */
  435. case AV_CODEC_ID_ADPCM_EA_XAS:
  436. if (buf_size < 76 * ch)
  437. return 0;
  438. nb_samples = 128;
  439. break;
  440. case AV_CODEC_ID_ADPCM_IMA_QT:
  441. if (buf_size < 34 * ch)
  442. return 0;
  443. nb_samples = 64;
  444. break;
  445. /* simple 4-bit adpcm */
  446. case AV_CODEC_ID_ADPCM_CT:
  447. case AV_CODEC_ID_ADPCM_IMA_APC:
  448. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  449. case AV_CODEC_ID_ADPCM_IMA_OKI:
  450. case AV_CODEC_ID_ADPCM_IMA_WS:
  451. case AV_CODEC_ID_ADPCM_YAMAHA:
  452. case AV_CODEC_ID_ADPCM_AICA:
  453. nb_samples = buf_size * 2 / ch;
  454. break;
  455. }
  456. if (nb_samples)
  457. return nb_samples;
  458. /* simple 4-bit adpcm, with header */
  459. header_size = 0;
  460. switch (avctx->codec->id) {
  461. case AV_CODEC_ID_ADPCM_4XM:
  462. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  463. case AV_CODEC_ID_ADPCM_IMA_ISS: header_size = 4 * ch; break;
  464. case AV_CODEC_ID_ADPCM_IMA_AMV: header_size = 8; break;
  465. case AV_CODEC_ID_ADPCM_IMA_SMJPEG: header_size = 4 * ch; break;
  466. }
  467. if (header_size > 0)
  468. return (buf_size - header_size) * 2 / ch;
  469. /* more complex formats */
  470. switch (avctx->codec->id) {
  471. case AV_CODEC_ID_ADPCM_EA:
  472. has_coded_samples = 1;
  473. *coded_samples = bytestream2_get_le32(gb);
  474. *coded_samples -= *coded_samples % 28;
  475. nb_samples = (buf_size - 12) / 30 * 28;
  476. break;
  477. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  478. has_coded_samples = 1;
  479. *coded_samples = bytestream2_get_le32(gb);
  480. nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
  481. break;
  482. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  483. nb_samples = (buf_size - ch) / ch * 2;
  484. break;
  485. case AV_CODEC_ID_ADPCM_EA_R1:
  486. case AV_CODEC_ID_ADPCM_EA_R2:
  487. case AV_CODEC_ID_ADPCM_EA_R3:
  488. /* maximum number of samples */
  489. /* has internal offsets and a per-frame switch to signal raw 16-bit */
  490. has_coded_samples = 1;
  491. switch (avctx->codec->id) {
  492. case AV_CODEC_ID_ADPCM_EA_R1:
  493. header_size = 4 + 9 * ch;
  494. *coded_samples = bytestream2_get_le32(gb);
  495. break;
  496. case AV_CODEC_ID_ADPCM_EA_R2:
  497. header_size = 4 + 5 * ch;
  498. *coded_samples = bytestream2_get_le32(gb);
  499. break;
  500. case AV_CODEC_ID_ADPCM_EA_R3:
  501. header_size = 4 + 5 * ch;
  502. *coded_samples = bytestream2_get_be32(gb);
  503. break;
  504. }
  505. *coded_samples -= *coded_samples % 28;
  506. nb_samples = (buf_size - header_size) * 2 / ch;
  507. nb_samples -= nb_samples % 28;
  508. *approx_nb_samples = 1;
  509. break;
  510. case AV_CODEC_ID_ADPCM_IMA_DK3:
  511. if (avctx->block_align > 0)
  512. buf_size = FFMIN(buf_size, avctx->block_align);
  513. nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
  514. break;
  515. case AV_CODEC_ID_ADPCM_IMA_DK4:
  516. if (avctx->block_align > 0)
  517. buf_size = FFMIN(buf_size, avctx->block_align);
  518. if (buf_size < 4 * ch)
  519. return AVERROR_INVALIDDATA;
  520. nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
  521. break;
  522. case AV_CODEC_ID_ADPCM_IMA_RAD:
  523. if (avctx->block_align > 0)
  524. buf_size = FFMIN(buf_size, avctx->block_align);
  525. nb_samples = (buf_size - 4 * ch) * 2 / ch;
  526. break;
  527. case AV_CODEC_ID_ADPCM_IMA_WAV:
  528. {
  529. int bsize = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
  530. int bsamples = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
  531. if (avctx->block_align > 0)
  532. buf_size = FFMIN(buf_size, avctx->block_align);
  533. if (buf_size < 4 * ch)
  534. return AVERROR_INVALIDDATA;
  535. nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
  536. break;
  537. }
  538. case AV_CODEC_ID_ADPCM_MS:
  539. if (avctx->block_align > 0)
  540. buf_size = FFMIN(buf_size, avctx->block_align);
  541. nb_samples = (buf_size - 6 * ch) * 2 / ch;
  542. break;
  543. case AV_CODEC_ID_ADPCM_SBPRO_2:
  544. case AV_CODEC_ID_ADPCM_SBPRO_3:
  545. case AV_CODEC_ID_ADPCM_SBPRO_4:
  546. {
  547. int samples_per_byte;
  548. switch (avctx->codec->id) {
  549. case AV_CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
  550. case AV_CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
  551. case AV_CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
  552. }
  553. if (!s->status[0].step_index) {
  554. if (buf_size < ch)
  555. return AVERROR_INVALIDDATA;
  556. nb_samples++;
  557. buf_size -= ch;
  558. }
  559. nb_samples += buf_size * samples_per_byte / ch;
  560. break;
  561. }
  562. case AV_CODEC_ID_ADPCM_SWF:
  563. {
  564. int buf_bits = buf_size * 8 - 2;
  565. int nbits = (bytestream2_get_byte(gb) >> 6) + 2;
  566. int block_hdr_size = 22 * ch;
  567. int block_size = block_hdr_size + nbits * ch * 4095;
  568. int nblocks = buf_bits / block_size;
  569. int bits_left = buf_bits - nblocks * block_size;
  570. nb_samples = nblocks * 4096;
  571. if (bits_left >= block_hdr_size)
  572. nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
  573. break;
  574. }
  575. case AV_CODEC_ID_ADPCM_THP:
  576. case AV_CODEC_ID_ADPCM_THP_LE:
  577. if (avctx->extradata) {
  578. nb_samples = buf_size * 14 / (8 * ch);
  579. break;
  580. }
  581. has_coded_samples = 1;
  582. bytestream2_skip(gb, 4); // channel size
  583. *coded_samples = (avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE) ?
  584. bytestream2_get_le32(gb) :
  585. bytestream2_get_be32(gb);
  586. buf_size -= 8 + 36 * ch;
  587. buf_size /= ch;
  588. nb_samples = buf_size / 8 * 14;
  589. if (buf_size % 8 > 1)
  590. nb_samples += (buf_size % 8 - 1) * 2;
  591. *approx_nb_samples = 1;
  592. break;
  593. case AV_CODEC_ID_ADPCM_AFC:
  594. nb_samples = buf_size / (9 * ch) * 16;
  595. break;
  596. case AV_CODEC_ID_ADPCM_XA:
  597. nb_samples = (buf_size / 128) * 224 / ch;
  598. break;
  599. case AV_CODEC_ID_ADPCM_DTK:
  600. case AV_CODEC_ID_ADPCM_PSX:
  601. nb_samples = buf_size / (16 * ch) * 28;
  602. break;
  603. }
  604. /* validate coded sample count */
  605. if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
  606. return AVERROR_INVALIDDATA;
  607. return nb_samples;
  608. }
  609. static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
  610. int *got_frame_ptr, AVPacket *avpkt)
  611. {
  612. AVFrame *frame = data;
  613. const uint8_t *buf = avpkt->data;
  614. int buf_size = avpkt->size;
  615. ADPCMDecodeContext *c = avctx->priv_data;
  616. ADPCMChannelStatus *cs;
  617. int n, m, channel, i;
  618. int16_t *samples;
  619. int16_t **samples_p;
  620. int st; /* stereo */
  621. int count1, count2;
  622. int nb_samples, coded_samples, approx_nb_samples, ret;
  623. GetByteContext gb;
  624. bytestream2_init(&gb, buf, buf_size);
  625. nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples);
  626. if (nb_samples <= 0) {
  627. av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
  628. return AVERROR_INVALIDDATA;
  629. }
  630. /* get output buffer */
  631. frame->nb_samples = nb_samples;
  632. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  633. return ret;
  634. samples = (int16_t *)frame->data[0];
  635. samples_p = (int16_t **)frame->extended_data;
  636. /* use coded_samples when applicable */
  637. /* it is always <= nb_samples, so the output buffer will be large enough */
  638. if (coded_samples) {
  639. if (!approx_nb_samples && coded_samples != nb_samples)
  640. av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
  641. frame->nb_samples = nb_samples = coded_samples;
  642. }
  643. st = avctx->channels == 2 ? 1 : 0;
  644. switch(avctx->codec->id) {
  645. case AV_CODEC_ID_ADPCM_IMA_QT:
  646. /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
  647. Channel data is interleaved per-chunk. */
  648. for (channel = 0; channel < avctx->channels; channel++) {
  649. int predictor;
  650. int step_index;
  651. cs = &(c->status[channel]);
  652. /* (pppppp) (piiiiiii) */
  653. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  654. predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  655. step_index = predictor & 0x7F;
  656. predictor &= ~0x7F;
  657. if (cs->step_index == step_index) {
  658. int diff = predictor - cs->predictor;
  659. if (diff < 0)
  660. diff = - diff;
  661. if (diff > 0x7f)
  662. goto update;
  663. } else {
  664. update:
  665. cs->step_index = step_index;
  666. cs->predictor = predictor;
  667. }
  668. if (cs->step_index > 88u){
  669. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  670. channel, cs->step_index);
  671. return AVERROR_INVALIDDATA;
  672. }
  673. samples = samples_p[channel];
  674. for (m = 0; m < 64; m += 2) {
  675. int byte = bytestream2_get_byteu(&gb);
  676. samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);
  677. samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 , 3);
  678. }
  679. }
  680. break;
  681. case AV_CODEC_ID_ADPCM_IMA_WAV:
  682. for(i=0; i<avctx->channels; i++){
  683. cs = &(c->status[i]);
  684. cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
  685. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  686. if (cs->step_index > 88u){
  687. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  688. i, cs->step_index);
  689. return AVERROR_INVALIDDATA;
  690. }
  691. }
  692. if (avctx->bits_per_coded_sample != 4) {
  693. int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2];
  694. int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2];
  695. uint8_t temp[20] = { 0 };
  696. GetBitContext g;
  697. for (n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
  698. for (i = 0; i < avctx->channels; i++) {
  699. int j;
  700. cs = &c->status[i];
  701. samples = &samples_p[i][1 + n * samples_per_block];
  702. for (j = 0; j < block_size; j++) {
  703. temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
  704. (j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
  705. }
  706. ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
  707. if (ret < 0)
  708. return ret;
  709. for (m = 0; m < samples_per_block; m++) {
  710. samples[m] = adpcm_ima_wav_expand_nibble(cs, &g,
  711. avctx->bits_per_coded_sample);
  712. }
  713. }
  714. }
  715. bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
  716. } else {
  717. for (n = 0; n < (nb_samples - 1) / 8; n++) {
  718. for (i = 0; i < avctx->channels; i++) {
  719. cs = &c->status[i];
  720. samples = &samples_p[i][1 + n * 8];
  721. for (m = 0; m < 8; m += 2) {
  722. int v = bytestream2_get_byteu(&gb);
  723. samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  724. samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  725. }
  726. }
  727. }
  728. }
  729. break;
  730. case AV_CODEC_ID_ADPCM_4XM:
  731. for (i = 0; i < avctx->channels; i++)
  732. c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  733. for (i = 0; i < avctx->channels; i++) {
  734. c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  735. if (c->status[i].step_index > 88u) {
  736. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  737. i, c->status[i].step_index);
  738. return AVERROR_INVALIDDATA;
  739. }
  740. }
  741. for (i = 0; i < avctx->channels; i++) {
  742. samples = (int16_t *)frame->data[i];
  743. cs = &c->status[i];
  744. for (n = nb_samples >> 1; n > 0; n--) {
  745. int v = bytestream2_get_byteu(&gb);
  746. *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
  747. *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
  748. }
  749. }
  750. break;
  751. case AV_CODEC_ID_ADPCM_MS:
  752. {
  753. int block_predictor;
  754. block_predictor = bytestream2_get_byteu(&gb);
  755. if (block_predictor > 6) {
  756. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
  757. block_predictor);
  758. return AVERROR_INVALIDDATA;
  759. }
  760. c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  761. c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  762. if (st) {
  763. block_predictor = bytestream2_get_byteu(&gb);
  764. if (block_predictor > 6) {
  765. av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
  766. block_predictor);
  767. return AVERROR_INVALIDDATA;
  768. }
  769. c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
  770. c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
  771. }
  772. c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  773. if (st){
  774. c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);
  775. }
  776. c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  777. if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);
  778. c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  779. if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);
  780. *samples++ = c->status[0].sample2;
  781. if (st) *samples++ = c->status[1].sample2;
  782. *samples++ = c->status[0].sample1;
  783. if (st) *samples++ = c->status[1].sample1;
  784. for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
  785. int byte = bytestream2_get_byteu(&gb);
  786. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4 );
  787. *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
  788. }
  789. break;
  790. }
  791. case AV_CODEC_ID_ADPCM_IMA_DK4:
  792. for (channel = 0; channel < avctx->channels; channel++) {
  793. cs = &c->status[channel];
  794. cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
  795. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  796. if (cs->step_index > 88u){
  797. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  798. channel, cs->step_index);
  799. return AVERROR_INVALIDDATA;
  800. }
  801. }
  802. for (n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
  803. int v = bytestream2_get_byteu(&gb);
  804. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
  805. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  806. }
  807. break;
  808. case AV_CODEC_ID_ADPCM_IMA_DK3:
  809. {
  810. int last_byte = 0;
  811. int nibble;
  812. int decode_top_nibble_next = 0;
  813. int diff_channel;
  814. const int16_t *samples_end = samples + avctx->channels * nb_samples;
  815. bytestream2_skipu(&gb, 10);
  816. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  817. c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  818. c->status[0].step_index = bytestream2_get_byteu(&gb);
  819. c->status[1].step_index = bytestream2_get_byteu(&gb);
  820. if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){
  821. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
  822. c->status[0].step_index, c->status[1].step_index);
  823. return AVERROR_INVALIDDATA;
  824. }
  825. /* sign extend the predictors */
  826. diff_channel = c->status[1].predictor;
  827. /* DK3 ADPCM support macro */
  828. #define DK3_GET_NEXT_NIBBLE() \
  829. if (decode_top_nibble_next) { \
  830. nibble = last_byte >> 4; \
  831. decode_top_nibble_next = 0; \
  832. } else { \
  833. last_byte = bytestream2_get_byteu(&gb); \
  834. nibble = last_byte & 0x0F; \
  835. decode_top_nibble_next = 1; \
  836. }
  837. while (samples < samples_end) {
  838. /* for this algorithm, c->status[0] is the sum channel and
  839. * c->status[1] is the diff channel */
  840. /* process the first predictor of the sum channel */
  841. DK3_GET_NEXT_NIBBLE();
  842. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  843. /* process the diff channel predictor */
  844. DK3_GET_NEXT_NIBBLE();
  845. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  846. /* process the first pair of stereo PCM samples */
  847. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  848. *samples++ = c->status[0].predictor + c->status[1].predictor;
  849. *samples++ = c->status[0].predictor - c->status[1].predictor;
  850. /* process the second predictor of the sum channel */
  851. DK3_GET_NEXT_NIBBLE();
  852. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  853. /* process the second pair of stereo PCM samples */
  854. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  855. *samples++ = c->status[0].predictor + c->status[1].predictor;
  856. *samples++ = c->status[0].predictor - c->status[1].predictor;
  857. }
  858. if ((bytestream2_tell(&gb) & 1))
  859. bytestream2_skip(&gb, 1);
  860. break;
  861. }
  862. case AV_CODEC_ID_ADPCM_IMA_ISS:
  863. for (channel = 0; channel < avctx->channels; channel++) {
  864. cs = &c->status[channel];
  865. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  866. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  867. if (cs->step_index > 88u){
  868. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  869. channel, cs->step_index);
  870. return AVERROR_INVALIDDATA;
  871. }
  872. }
  873. for (n = nb_samples >> (1 - st); n > 0; n--) {
  874. int v1, v2;
  875. int v = bytestream2_get_byteu(&gb);
  876. /* nibbles are swapped for mono */
  877. if (st) {
  878. v1 = v >> 4;
  879. v2 = v & 0x0F;
  880. } else {
  881. v2 = v >> 4;
  882. v1 = v & 0x0F;
  883. }
  884. *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);
  885. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
  886. }
  887. break;
  888. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  889. for (channel = 0; channel < avctx->channels; channel++) {
  890. cs = &c->status[channel];
  891. samples = samples_p[channel];
  892. bytestream2_skip(&gb, 4);
  893. for (n = 0; n < nb_samples; n += 2) {
  894. int v = bytestream2_get_byteu(&gb);
  895. *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
  896. *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
  897. }
  898. }
  899. break;
  900. case AV_CODEC_ID_ADPCM_IMA_APC:
  901. while (bytestream2_get_bytes_left(&gb) > 0) {
  902. int v = bytestream2_get_byteu(&gb);
  903. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
  904. *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
  905. }
  906. break;
  907. case AV_CODEC_ID_ADPCM_IMA_OKI:
  908. while (bytestream2_get_bytes_left(&gb) > 0) {
  909. int v = bytestream2_get_byteu(&gb);
  910. *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 );
  911. *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
  912. }
  913. break;
  914. case AV_CODEC_ID_ADPCM_IMA_RAD:
  915. for (channel = 0; channel < avctx->channels; channel++) {
  916. cs = &c->status[channel];
  917. cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
  918. cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  919. if (cs->step_index > 88u){
  920. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  921. channel, cs->step_index);
  922. return AVERROR_INVALIDDATA;
  923. }
  924. }
  925. for (n = 0; n < nb_samples / 2; n++) {
  926. int byte[2];
  927. byte[0] = bytestream2_get_byteu(&gb);
  928. if (st)
  929. byte[1] = bytestream2_get_byteu(&gb);
  930. for(channel = 0; channel < avctx->channels; channel++) {
  931. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
  932. }
  933. for(channel = 0; channel < avctx->channels; channel++) {
  934. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
  935. }
  936. }
  937. break;
  938. case AV_CODEC_ID_ADPCM_IMA_WS:
  939. if (c->vqa_version == 3) {
  940. for (channel = 0; channel < avctx->channels; channel++) {
  941. int16_t *smp = samples_p[channel];
  942. for (n = nb_samples / 2; n > 0; n--) {
  943. int v = bytestream2_get_byteu(&gb);
  944. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  945. *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  946. }
  947. }
  948. } else {
  949. for (n = nb_samples / 2; n > 0; n--) {
  950. for (channel = 0; channel < avctx->channels; channel++) {
  951. int v = bytestream2_get_byteu(&gb);
  952. *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
  953. samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
  954. }
  955. samples += avctx->channels;
  956. }
  957. }
  958. bytestream2_seek(&gb, 0, SEEK_END);
  959. break;
  960. case AV_CODEC_ID_ADPCM_XA:
  961. {
  962. int16_t *out0 = samples_p[0];
  963. int16_t *out1 = samples_p[1];
  964. int samples_per_block = 28 * (3 - avctx->channels) * 4;
  965. int sample_offset = 0;
  966. while (bytestream2_get_bytes_left(&gb) >= 128) {
  967. if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
  968. &c->status[0], &c->status[1],
  969. avctx->channels, sample_offset)) < 0)
  970. return ret;
  971. bytestream2_skipu(&gb, 128);
  972. sample_offset += samples_per_block;
  973. }
  974. break;
  975. }
  976. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  977. for (i=0; i<=st; i++) {
  978. c->status[i].step_index = bytestream2_get_le32u(&gb);
  979. if (c->status[i].step_index > 88u) {
  980. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
  981. i, c->status[i].step_index);
  982. return AVERROR_INVALIDDATA;
  983. }
  984. }
  985. for (i=0; i<=st; i++)
  986. c->status[i].predictor = bytestream2_get_le32u(&gb);
  987. for (n = nb_samples >> (1 - st); n > 0; n--) {
  988. int byte = bytestream2_get_byteu(&gb);
  989. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
  990. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
  991. }
  992. break;
  993. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  994. for (n = nb_samples >> (1 - st); n > 0; n--) {
  995. int byte = bytestream2_get_byteu(&gb);
  996. *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
  997. *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
  998. }
  999. break;
  1000. case AV_CODEC_ID_ADPCM_EA:
  1001. {
  1002. int previous_left_sample, previous_right_sample;
  1003. int current_left_sample, current_right_sample;
  1004. int next_left_sample, next_right_sample;
  1005. int coeff1l, coeff2l, coeff1r, coeff2r;
  1006. int shift_left, shift_right;
  1007. /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
  1008. each coding 28 stereo samples. */
  1009. if(avctx->channels != 2)
  1010. return AVERROR_INVALIDDATA;
  1011. current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  1012. previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  1013. current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  1014. previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
  1015. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  1016. int byte = bytestream2_get_byteu(&gb);
  1017. coeff1l = ea_adpcm_table[ byte >> 4 ];
  1018. coeff2l = ea_adpcm_table[(byte >> 4 ) + 4];
  1019. coeff1r = ea_adpcm_table[ byte & 0x0F];
  1020. coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
  1021. byte = bytestream2_get_byteu(&gb);
  1022. shift_left = 20 - (byte >> 4);
  1023. shift_right = 20 - (byte & 0x0F);
  1024. for (count2 = 0; count2 < 28; count2++) {
  1025. byte = bytestream2_get_byteu(&gb);
  1026. next_left_sample = sign_extend(byte >> 4, 4) << shift_left;
  1027. next_right_sample = sign_extend(byte, 4) << shift_right;
  1028. next_left_sample = (next_left_sample +
  1029. (current_left_sample * coeff1l) +
  1030. (previous_left_sample * coeff2l) + 0x80) >> 8;
  1031. next_right_sample = (next_right_sample +
  1032. (current_right_sample * coeff1r) +
  1033. (previous_right_sample * coeff2r) + 0x80) >> 8;
  1034. previous_left_sample = current_left_sample;
  1035. current_left_sample = av_clip_int16(next_left_sample);
  1036. previous_right_sample = current_right_sample;
  1037. current_right_sample = av_clip_int16(next_right_sample);
  1038. *samples++ = current_left_sample;
  1039. *samples++ = current_right_sample;
  1040. }
  1041. }
  1042. bytestream2_skip(&gb, 2); // Skip terminating 0x0000
  1043. break;
  1044. }
  1045. case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
  1046. {
  1047. int coeff[2][2], shift[2];
  1048. for(channel = 0; channel < avctx->channels; channel++) {
  1049. int byte = bytestream2_get_byteu(&gb);
  1050. for (i=0; i<2; i++)
  1051. coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
  1052. shift[channel] = 20 - (byte & 0x0F);
  1053. }
  1054. for (count1 = 0; count1 < nb_samples / 2; count1++) {
  1055. int byte[2];
  1056. byte[0] = bytestream2_get_byteu(&gb);
  1057. if (st) byte[1] = bytestream2_get_byteu(&gb);
  1058. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  1059. for(channel = 0; channel < avctx->channels; channel++) {
  1060. int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
  1061. sample = (sample +
  1062. c->status[channel].sample1 * coeff[channel][0] +
  1063. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  1064. c->status[channel].sample2 = c->status[channel].sample1;
  1065. c->status[channel].sample1 = av_clip_int16(sample);
  1066. *samples++ = c->status[channel].sample1;
  1067. }
  1068. }
  1069. }
  1070. bytestream2_seek(&gb, 0, SEEK_END);
  1071. break;
  1072. }
  1073. case AV_CODEC_ID_ADPCM_EA_R1:
  1074. case AV_CODEC_ID_ADPCM_EA_R2:
  1075. case AV_CODEC_ID_ADPCM_EA_R3: {
  1076. /* channel numbering
  1077. 2chan: 0=fl, 1=fr
  1078. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  1079. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  1080. const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;
  1081. int previous_sample, current_sample, next_sample;
  1082. int coeff1, coeff2;
  1083. int shift;
  1084. unsigned int channel;
  1085. uint16_t *samplesC;
  1086. int count = 0;
  1087. int offsets[6];
  1088. for (channel=0; channel<avctx->channels; channel++)
  1089. offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
  1090. bytestream2_get_le32(&gb)) +
  1091. (avctx->channels + 1) * 4;
  1092. for (channel=0; channel<avctx->channels; channel++) {
  1093. bytestream2_seek(&gb, offsets[channel], SEEK_SET);
  1094. samplesC = samples_p[channel];
  1095. if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {
  1096. current_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  1097. previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
  1098. } else {
  1099. current_sample = c->status[channel].predictor;
  1100. previous_sample = c->status[channel].prev_sample;
  1101. }
  1102. for (count1 = 0; count1 < nb_samples / 28; count1++) {
  1103. int byte = bytestream2_get_byte(&gb);
  1104. if (byte == 0xEE) { /* only seen in R2 and R3 */
  1105. current_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  1106. previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
  1107. for (count2=0; count2<28; count2++)
  1108. *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16);
  1109. } else {
  1110. coeff1 = ea_adpcm_table[ byte >> 4 ];
  1111. coeff2 = ea_adpcm_table[(byte >> 4) + 4];
  1112. shift = 20 - (byte & 0x0F);
  1113. for (count2=0; count2<28; count2++) {
  1114. if (count2 & 1)
  1115. next_sample = sign_extend(byte, 4) << shift;
  1116. else {
  1117. byte = bytestream2_get_byte(&gb);
  1118. next_sample = sign_extend(byte >> 4, 4) << shift;
  1119. }
  1120. next_sample += (current_sample * coeff1) +
  1121. (previous_sample * coeff2);
  1122. next_sample = av_clip_int16(next_sample >> 8);
  1123. previous_sample = current_sample;
  1124. current_sample = next_sample;
  1125. *samplesC++ = current_sample;
  1126. }
  1127. }
  1128. }
  1129. if (!count) {
  1130. count = count1;
  1131. } else if (count != count1) {
  1132. av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
  1133. count = FFMAX(count, count1);
  1134. }
  1135. if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {
  1136. c->status[channel].predictor = current_sample;
  1137. c->status[channel].prev_sample = previous_sample;
  1138. }
  1139. }
  1140. frame->nb_samples = count * 28;
  1141. bytestream2_seek(&gb, 0, SEEK_END);
  1142. break;
  1143. }
  1144. case AV_CODEC_ID_ADPCM_EA_XAS:
  1145. for (channel=0; channel<avctx->channels; channel++) {
  1146. int coeff[2][4], shift[4];
  1147. int16_t *s = samples_p[channel];
  1148. for (n = 0; n < 4; n++, s += 32) {
  1149. int val = sign_extend(bytestream2_get_le16u(&gb), 16);
  1150. for (i=0; i<2; i++)
  1151. coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
  1152. s[0] = val & ~0x0F;
  1153. val = sign_extend(bytestream2_get_le16u(&gb), 16);
  1154. shift[n] = 20 - (val & 0x0F);
  1155. s[1] = val & ~0x0F;
  1156. }
  1157. for (m=2; m<32; m+=2) {
  1158. s = &samples_p[channel][m];
  1159. for (n = 0; n < 4; n++, s += 32) {
  1160. int level, pred;
  1161. int byte = bytestream2_get_byteu(&gb);
  1162. level = sign_extend(byte >> 4, 4) << shift[n];
  1163. pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
  1164. s[0] = av_clip_int16((level + pred + 0x80) >> 8);
  1165. level = sign_extend(byte, 4) << shift[n];
  1166. pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
  1167. s[1] = av_clip_int16((level + pred + 0x80) >> 8);
  1168. }
  1169. }
  1170. }
  1171. break;
  1172. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1173. c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
  1174. c->status[0].step_index = bytestream2_get_byteu(&gb);
  1175. bytestream2_skipu(&gb, 5);
  1176. if (c->status[0].step_index > 88u) {
  1177. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1178. c->status[0].step_index);
  1179. return AVERROR_INVALIDDATA;
  1180. }
  1181. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1182. int v = bytestream2_get_byteu(&gb);
  1183. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
  1184. *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
  1185. }
  1186. break;
  1187. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1188. for (i = 0; i < avctx->channels; i++) {
  1189. c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
  1190. c->status[i].step_index = bytestream2_get_byteu(&gb);
  1191. bytestream2_skipu(&gb, 1);
  1192. if (c->status[i].step_index > 88u) {
  1193. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
  1194. c->status[i].step_index);
  1195. return AVERROR_INVALIDDATA;
  1196. }
  1197. }
  1198. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1199. int v = bytestream2_get_byteu(&gb);
  1200. *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);
  1201. *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);
  1202. }
  1203. break;
  1204. case AV_CODEC_ID_ADPCM_CT:
  1205. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1206. int v = bytestream2_get_byteu(&gb);
  1207. *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
  1208. *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
  1209. }
  1210. break;
  1211. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1212. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1213. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1214. if (!c->status[0].step_index) {
  1215. /* the first byte is a raw sample */
  1216. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1217. if (st)
  1218. *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
  1219. c->status[0].step_index = 1;
  1220. nb_samples--;
  1221. }
  1222. if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {
  1223. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1224. int byte = bytestream2_get_byteu(&gb);
  1225. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1226. byte >> 4, 4, 0);
  1227. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1228. byte & 0x0F, 4, 0);
  1229. }
  1230. } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {
  1231. for (n = (nb_samples<<st) / 3; n > 0; n--) {
  1232. int byte = bytestream2_get_byteu(&gb);
  1233. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1234. byte >> 5 , 3, 0);
  1235. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1236. (byte >> 2) & 0x07, 3, 0);
  1237. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1238. byte & 0x03, 2, 0);
  1239. }
  1240. } else {
  1241. for (n = nb_samples >> (2 - st); n > 0; n--) {
  1242. int byte = bytestream2_get_byteu(&gb);
  1243. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1244. byte >> 6 , 2, 2);
  1245. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1246. (byte >> 4) & 0x03, 2, 2);
  1247. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1248. (byte >> 2) & 0x03, 2, 2);
  1249. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1250. byte & 0x03, 2, 2);
  1251. }
  1252. }
  1253. break;
  1254. case AV_CODEC_ID_ADPCM_SWF:
  1255. adpcm_swf_decode(avctx, buf, buf_size, samples);
  1256. bytestream2_seek(&gb, 0, SEEK_END);
  1257. break;
  1258. case AV_CODEC_ID_ADPCM_YAMAHA:
  1259. for (n = nb_samples >> (1 - st); n > 0; n--) {
  1260. int v = bytestream2_get_byteu(&gb);
  1261. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
  1262. *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
  1263. }
  1264. break;
  1265. case AV_CODEC_ID_ADPCM_AICA:
  1266. if (!c->has_status) {
  1267. for (channel = 0; channel < avctx->channels; channel++)
  1268. c->status[channel].step = 0;
  1269. c->has_status = 1;
  1270. }
  1271. for (channel = 0; channel < avctx->channels; channel++) {
  1272. samples = samples_p[channel];
  1273. for (n = nb_samples >> 1; n > 0; n--) {
  1274. int v = bytestream2_get_byteu(&gb);
  1275. *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
  1276. *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 );
  1277. }
  1278. }
  1279. break;
  1280. case AV_CODEC_ID_ADPCM_AFC:
  1281. {
  1282. int samples_per_block;
  1283. int blocks;
  1284. if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
  1285. samples_per_block = avctx->extradata[0] / 16;
  1286. blocks = nb_samples / avctx->extradata[0];
  1287. } else {
  1288. samples_per_block = nb_samples / 16;
  1289. blocks = 1;
  1290. }
  1291. for (m = 0; m < blocks; m++) {
  1292. for (channel = 0; channel < avctx->channels; channel++) {
  1293. int prev1 = c->status[channel].sample1;
  1294. int prev2 = c->status[channel].sample2;
  1295. samples = samples_p[channel] + m * 16;
  1296. /* Read in every sample for this channel. */
  1297. for (i = 0; i < samples_per_block; i++) {
  1298. int byte = bytestream2_get_byteu(&gb);
  1299. int scale = 1 << (byte >> 4);
  1300. int index = byte & 0xf;
  1301. int factor1 = ff_adpcm_afc_coeffs[0][index];
  1302. int factor2 = ff_adpcm_afc_coeffs[1][index];
  1303. /* Decode 16 samples. */
  1304. for (n = 0; n < 16; n++) {
  1305. int32_t sampledat;
  1306. if (n & 1) {
  1307. sampledat = sign_extend(byte, 4);
  1308. } else {
  1309. byte = bytestream2_get_byteu(&gb);
  1310. sampledat = sign_extend(byte >> 4, 4);
  1311. }
  1312. sampledat = ((prev1 * factor1 + prev2 * factor2) +
  1313. ((sampledat * scale) << 11)) >> 11;
  1314. *samples = av_clip_int16(sampledat);
  1315. prev2 = prev1;
  1316. prev1 = *samples++;
  1317. }
  1318. }
  1319. c->status[channel].sample1 = prev1;
  1320. c->status[channel].sample2 = prev2;
  1321. }
  1322. }
  1323. bytestream2_seek(&gb, 0, SEEK_END);
  1324. break;
  1325. }
  1326. case AV_CODEC_ID_ADPCM_THP:
  1327. case AV_CODEC_ID_ADPCM_THP_LE:
  1328. {
  1329. int table[14][16];
  1330. int ch;
  1331. #define THP_GET16(g) \
  1332. sign_extend( \
  1333. avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \
  1334. bytestream2_get_le16u(&(g)) : \
  1335. bytestream2_get_be16u(&(g)), 16)
  1336. if (avctx->extradata) {
  1337. GetByteContext tb;
  1338. if (avctx->extradata_size < 32 * avctx->channels) {
  1339. av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
  1340. return AVERROR_INVALIDDATA;
  1341. }
  1342. bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
  1343. for (i = 0; i < avctx->channels; i++)
  1344. for (n = 0; n < 16; n++)
  1345. table[i][n] = THP_GET16(tb);
  1346. } else {
  1347. for (i = 0; i < avctx->channels; i++)
  1348. for (n = 0; n < 16; n++)
  1349. table[i][n] = THP_GET16(gb);
  1350. if (!c->has_status) {
  1351. /* Initialize the previous sample. */
  1352. for (i = 0; i < avctx->channels; i++) {
  1353. c->status[i].sample1 = THP_GET16(gb);
  1354. c->status[i].sample2 = THP_GET16(gb);
  1355. }
  1356. c->has_status = 1;
  1357. } else {
  1358. bytestream2_skip(&gb, avctx->channels * 4);
  1359. }
  1360. }
  1361. for (ch = 0; ch < avctx->channels; ch++) {
  1362. samples = samples_p[ch];
  1363. /* Read in every sample for this channel. */
  1364. for (i = 0; i < (nb_samples + 13) / 14; i++) {
  1365. int byte = bytestream2_get_byteu(&gb);
  1366. int index = (byte >> 4) & 7;
  1367. unsigned int exp = byte & 0x0F;
  1368. int factor1 = table[ch][index * 2];
  1369. int factor2 = table[ch][index * 2 + 1];
  1370. /* Decode 14 samples. */
  1371. for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
  1372. int32_t sampledat;
  1373. if (n & 1) {
  1374. sampledat = sign_extend(byte, 4);
  1375. } else {
  1376. byte = bytestream2_get_byteu(&gb);
  1377. sampledat = sign_extend(byte >> 4, 4);
  1378. }
  1379. sampledat = ((c->status[ch].sample1 * factor1
  1380. + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);
  1381. *samples = av_clip_int16(sampledat);
  1382. c->status[ch].sample2 = c->status[ch].sample1;
  1383. c->status[ch].sample1 = *samples++;
  1384. }
  1385. }
  1386. }
  1387. break;
  1388. }
  1389. case AV_CODEC_ID_ADPCM_DTK:
  1390. for (channel = 0; channel < avctx->channels; channel++) {
  1391. samples = samples_p[channel];
  1392. /* Read in every sample for this channel. */
  1393. for (i = 0; i < nb_samples / 28; i++) {
  1394. int byte, header;
  1395. if (channel)
  1396. bytestream2_skipu(&gb, 1);
  1397. header = bytestream2_get_byteu(&gb);
  1398. bytestream2_skipu(&gb, 3 - channel);
  1399. /* Decode 28 samples. */
  1400. for (n = 0; n < 28; n++) {
  1401. int32_t sampledat, prev;
  1402. switch (header >> 4) {
  1403. case 1:
  1404. prev = (c->status[channel].sample1 * 0x3c);
  1405. break;
  1406. case 2:
  1407. prev = (c->status[channel].sample1 * 0x73) - (c->status[channel].sample2 * 0x34);
  1408. break;
  1409. case 3:
  1410. prev = (c->status[channel].sample1 * 0x62) - (c->status[channel].sample2 * 0x37);
  1411. break;
  1412. default:
  1413. prev = 0;
  1414. }
  1415. prev = av_clip_intp2((prev + 0x20) >> 6, 21);
  1416. byte = bytestream2_get_byteu(&gb);
  1417. if (!channel)
  1418. sampledat = sign_extend(byte, 4);
  1419. else
  1420. sampledat = sign_extend(byte >> 4, 4);
  1421. sampledat = (((sampledat << 12) >> (header & 0xf)) << 6) + prev;
  1422. *samples++ = av_clip_int16(sampledat >> 6);
  1423. c->status[channel].sample2 = c->status[channel].sample1;
  1424. c->status[channel].sample1 = sampledat;
  1425. }
  1426. }
  1427. if (!channel)
  1428. bytestream2_seek(&gb, 0, SEEK_SET);
  1429. }
  1430. break;
  1431. case AV_CODEC_ID_ADPCM_PSX:
  1432. for (channel = 0; channel < avctx->channels; channel++) {
  1433. samples = samples_p[channel];
  1434. /* Read in every sample for this channel. */
  1435. for (i = 0; i < nb_samples / 28; i++) {
  1436. int filter, shift, flag, byte;
  1437. filter = bytestream2_get_byteu(&gb);
  1438. shift = filter & 0xf;
  1439. filter = filter >> 4;
  1440. if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
  1441. return AVERROR_INVALIDDATA;
  1442. flag = bytestream2_get_byteu(&gb);
  1443. /* Decode 28 samples. */
  1444. for (n = 0; n < 28; n++) {
  1445. int sample = 0, scale;
  1446. if (flag < 0x07) {
  1447. if (n & 1) {
  1448. scale = sign_extend(byte >> 4, 4);
  1449. } else {
  1450. byte = bytestream2_get_byteu(&gb);
  1451. scale = sign_extend(byte, 4);
  1452. }
  1453. scale = scale << 12;
  1454. sample = (int)((scale >> shift) + (c->status[channel].sample1 * xa_adpcm_table[filter][0] + c->status[channel].sample2 * xa_adpcm_table[filter][1]) / 64);
  1455. }
  1456. *samples++ = av_clip_int16(sample);
  1457. c->status[channel].sample2 = c->status[channel].sample1;
  1458. c->status[channel].sample1 = sample;
  1459. }
  1460. }
  1461. }
  1462. break;
  1463. default:
  1464. return -1;
  1465. }
  1466. if (avpkt->size && bytestream2_tell(&gb) == 0) {
  1467. av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
  1468. return AVERROR_INVALIDDATA;
  1469. }
  1470. *got_frame_ptr = 1;
  1471. if (avpkt->size < bytestream2_tell(&gb)) {
  1472. av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb));
  1473. return avpkt->size;
  1474. }
  1475. return bytestream2_tell(&gb);
  1476. }
  1477. static void adpcm_flush(AVCodecContext *avctx)
  1478. {
  1479. ADPCMDecodeContext *c = avctx->priv_data;
  1480. c->has_status = 0;
  1481. }
  1482. static const enum AVSampleFormat sample_fmts_s16[] = { AV_SAMPLE_FMT_S16,
  1483. AV_SAMPLE_FMT_NONE };
  1484. static const enum AVSampleFormat sample_fmts_s16p[] = { AV_SAMPLE_FMT_S16P,
  1485. AV_SAMPLE_FMT_NONE };
  1486. static const enum AVSampleFormat sample_fmts_both[] = { AV_SAMPLE_FMT_S16,
  1487. AV_SAMPLE_FMT_S16P,
  1488. AV_SAMPLE_FMT_NONE };
  1489. #define ADPCM_DECODER(id_, sample_fmts_, name_, long_name_) \
  1490. AVCodec ff_ ## name_ ## _decoder = { \
  1491. .name = #name_, \
  1492. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1493. .type = AVMEDIA_TYPE_AUDIO, \
  1494. .id = id_, \
  1495. .priv_data_size = sizeof(ADPCMDecodeContext), \
  1496. .init = adpcm_decode_init, \
  1497. .decode = adpcm_decode_frame, \
  1498. .flush = adpcm_flush, \
  1499. .capabilities = AV_CODEC_CAP_DR1, \
  1500. .sample_fmts = sample_fmts_, \
  1501. }
  1502. /* Note: Do not forget to add new entries to the Makefile as well. */
  1503. ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, "ADPCM 4X Movie");
  1504. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC");
  1505. ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA");
  1506. ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology");
  1507. ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK");
  1508. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts");
  1509. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_MAXIS_XA, sample_fmts_s16, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1510. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R1, sample_fmts_s16p, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1511. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R2, sample_fmts_s16p, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1512. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_R3, sample_fmts_s16p, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1513. ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA_XAS, sample_fmts_s16p, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1514. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
  1515. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
  1516. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
  1517. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1518. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1519. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_EACS, sample_fmts_s16, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1520. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_EA_SEAD, sample_fmts_s16, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1521. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_ISS, sample_fmts_s16, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1522. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_OKI, sample_fmts_s16, adpcm_ima_oki, "ADPCM IMA Dialogic OKI");
  1523. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_QT, sample_fmts_s16p, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1524. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_RAD, sample_fmts_s16, adpcm_ima_rad, "ADPCM IMA Radical");
  1525. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_SMJPEG, sample_fmts_s16, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1526. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WAV, sample_fmts_s16p, adpcm_ima_wav, "ADPCM IMA WAV");
  1527. ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_WS, sample_fmts_both, adpcm_ima_ws, "ADPCM IMA Westwood");
  1528. ADPCM_DECODER(AV_CODEC_ID_ADPCM_MS, sample_fmts_s16, adpcm_ms, "ADPCM Microsoft");
  1529. ADPCM_DECODER(AV_CODEC_ID_ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADPCM Playstation");
  1530. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1531. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1532. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1533. ADPCM_DECODER(AV_CODEC_ID_ADPCM_SWF, sample_fmts_s16, adpcm_swf, "ADPCM Shockwave Flash");
  1534. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP_LE, sample_fmts_s16p, adpcm_thp_le, "ADPCM Nintendo THP (little-endian)");
  1535. ADPCM_DECODER(AV_CODEC_ID_ADPCM_THP, sample_fmts_s16p, adpcm_thp, "ADPCM Nintendo THP");
  1536. ADPCM_DECODER(AV_CODEC_ID_ADPCM_XA, sample_fmts_s16p, adpcm_xa, "ADPCM CDROM XA");
  1537. ADPCM_DECODER(AV_CODEC_ID_ADPCM_YAMAHA, sample_fmts_s16, adpcm_yamaha, "ADPCM Yamaha");