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.

1654 lines
60KB

  1. /*
  2. * ADPCM codecs
  3. * Copyright (c) 2001-2003 The ffmpeg Project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "bitstream.h"
  23. #include "bytestream.h"
  24. /**
  25. * @file adpcm.c
  26. * ADPCM codecs.
  27. * First version by Francois Revol (revol@free.fr)
  28. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  29. * by Mike Melanson (melanson@pcisys.net)
  30. * CD-ROM XA ADPCM codec by BERO
  31. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  32. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  33. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  34. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  35. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  36. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  37. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  38. *
  39. * Features and limitations:
  40. *
  41. * Reference documents:
  42. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  43. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  44. * http://openquicktime.sourceforge.net/plugins.htm
  45. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  46. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  47. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  48. *
  49. * CD-ROM XA:
  50. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
  51. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
  52. * readstr http://www.geocities.co.jp/Playtown/2004/
  53. */
  54. #define BLKSIZE 1024
  55. /* step_table[] and index_table[] are from the ADPCM reference source */
  56. /* This is the index table: */
  57. static const int index_table[16] = {
  58. -1, -1, -1, -1, 2, 4, 6, 8,
  59. -1, -1, -1, -1, 2, 4, 6, 8,
  60. };
  61. /**
  62. * This is the step table. Note that many programs use slight deviations from
  63. * this table, but such deviations are negligible:
  64. */
  65. static const int step_table[89] = {
  66. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  67. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  68. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  69. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  70. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  71. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  72. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  73. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  74. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  75. };
  76. /* These are for MS-ADPCM */
  77. /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
  78. static const int AdaptationTable[] = {
  79. 230, 230, 230, 230, 307, 409, 512, 614,
  80. 768, 614, 512, 409, 307, 230, 230, 230
  81. };
  82. static const uint8_t AdaptCoeff1[] = {
  83. 64, 128, 0, 48, 60, 115, 98
  84. };
  85. static const int8_t AdaptCoeff2[] = {
  86. 0, -64, 0, 16, 0, -52, -58
  87. };
  88. /* These are for CD-ROM XA ADPCM */
  89. static const int xa_adpcm_table[5][2] = {
  90. { 0, 0 },
  91. { 60, 0 },
  92. { 115, -52 },
  93. { 98, -55 },
  94. { 122, -60 }
  95. };
  96. static const int ea_adpcm_table[] = {
  97. 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
  98. 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
  99. };
  100. static const int ct_adpcm_table[8] = {
  101. 0x00E6, 0x00E6, 0x00E6, 0x00E6,
  102. 0x0133, 0x0199, 0x0200, 0x0266
  103. };
  104. // padded to zero where table size is less then 16
  105. static const int swf_index_tables[4][16] = {
  106. /*2*/ { -1, 2 },
  107. /*3*/ { -1, -1, 2, 4 },
  108. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  109. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  110. };
  111. static const int yamaha_indexscale[] = {
  112. 230, 230, 230, 230, 307, 409, 512, 614,
  113. 230, 230, 230, 230, 307, 409, 512, 614
  114. };
  115. static const int yamaha_difflookup[] = {
  116. 1, 3, 5, 7, 9, 11, 13, 15,
  117. -1, -3, -5, -7, -9, -11, -13, -15
  118. };
  119. /* end of tables */
  120. typedef struct ADPCMChannelStatus {
  121. int predictor;
  122. short int step_index;
  123. int step;
  124. /* for encoding */
  125. int prev_sample;
  126. /* MS version */
  127. short sample1;
  128. short sample2;
  129. int coeff1;
  130. int coeff2;
  131. int idelta;
  132. } ADPCMChannelStatus;
  133. typedef struct ADPCMContext {
  134. ADPCMChannelStatus status[6];
  135. } ADPCMContext;
  136. /* XXX: implement encoding */
  137. #ifdef CONFIG_ENCODERS
  138. static int adpcm_encode_init(AVCodecContext *avctx)
  139. {
  140. if (avctx->channels > 2)
  141. return -1; /* only stereo or mono =) */
  142. if(avctx->trellis && (unsigned)avctx->trellis > 16U){
  143. av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
  144. return -1;
  145. }
  146. switch(avctx->codec->id) {
  147. case CODEC_ID_ADPCM_IMA_WAV:
  148. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  149. /* and we have 4 bytes per channel overhead */
  150. avctx->block_align = BLKSIZE;
  151. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  152. break;
  153. case CODEC_ID_ADPCM_IMA_QT:
  154. avctx->frame_size = 64;
  155. avctx->block_align = 34 * avctx->channels;
  156. break;
  157. case CODEC_ID_ADPCM_MS:
  158. avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
  159. /* and we have 7 bytes per channel overhead */
  160. avctx->block_align = BLKSIZE;
  161. break;
  162. case CODEC_ID_ADPCM_YAMAHA:
  163. avctx->frame_size = BLKSIZE * avctx->channels;
  164. avctx->block_align = BLKSIZE;
  165. break;
  166. case CODEC_ID_ADPCM_SWF:
  167. if (avctx->sample_rate != 11025 &&
  168. avctx->sample_rate != 22050 &&
  169. avctx->sample_rate != 44100) {
  170. av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
  171. return -1;
  172. }
  173. avctx->frame_size = 512 * (avctx->sample_rate / 11025);
  174. break;
  175. default:
  176. return -1;
  177. break;
  178. }
  179. avctx->coded_frame= avcodec_alloc_frame();
  180. avctx->coded_frame->key_frame= 1;
  181. return 0;
  182. }
  183. static int adpcm_encode_close(AVCodecContext *avctx)
  184. {
  185. av_freep(&avctx->coded_frame);
  186. return 0;
  187. }
  188. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  189. {
  190. int delta = sample - c->prev_sample;
  191. int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
  192. c->prev_sample += ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
  193. c->prev_sample = av_clip_int16(c->prev_sample);
  194. c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
  195. return nibble;
  196. }
  197. static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
  198. {
  199. int predictor, nibble, bias;
  200. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  201. nibble= sample - predictor;
  202. if(nibble>=0) bias= c->idelta/2;
  203. else bias=-c->idelta/2;
  204. nibble= (nibble + bias) / c->idelta;
  205. nibble= av_clip(nibble, -8, 7)&0x0F;
  206. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  207. c->sample2 = c->sample1;
  208. c->sample1 = av_clip_int16(predictor);
  209. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  210. if (c->idelta < 16) c->idelta = 16;
  211. return nibble;
  212. }
  213. static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
  214. {
  215. int nibble, delta;
  216. if(!c->step) {
  217. c->predictor = 0;
  218. c->step = 127;
  219. }
  220. delta = sample - c->predictor;
  221. nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
  222. c->predictor += ((c->step * yamaha_difflookup[nibble]) / 8);
  223. c->predictor = av_clip_int16(c->predictor);
  224. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  225. c->step = av_clip(c->step, 127, 24567);
  226. return nibble;
  227. }
  228. typedef struct TrellisPath {
  229. int nibble;
  230. int prev;
  231. } TrellisPath;
  232. typedef struct TrellisNode {
  233. uint32_t ssd;
  234. int path;
  235. int sample1;
  236. int sample2;
  237. int step;
  238. } TrellisNode;
  239. static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
  240. uint8_t *dst, ADPCMChannelStatus *c, int n)
  241. {
  242. #define FREEZE_INTERVAL 128
  243. //FIXME 6% faster if frontier is a compile-time constant
  244. const int frontier = 1 << avctx->trellis;
  245. const int stride = avctx->channels;
  246. const int version = avctx->codec->id;
  247. const int max_paths = frontier*FREEZE_INTERVAL;
  248. TrellisPath paths[max_paths], *p;
  249. TrellisNode node_buf[2][frontier];
  250. TrellisNode *nodep_buf[2][frontier];
  251. TrellisNode **nodes = nodep_buf[0]; // nodes[] is always sorted by .ssd
  252. TrellisNode **nodes_next = nodep_buf[1];
  253. int pathn = 0, froze = -1, i, j, k;
  254. assert(!(max_paths&(max_paths-1)));
  255. memset(nodep_buf, 0, sizeof(nodep_buf));
  256. nodes[0] = &node_buf[1][0];
  257. nodes[0]->ssd = 0;
  258. nodes[0]->path = 0;
  259. nodes[0]->step = c->step_index;
  260. nodes[0]->sample1 = c->sample1;
  261. nodes[0]->sample2 = c->sample2;
  262. if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_IMA_QT) || (version == CODEC_ID_ADPCM_SWF))
  263. nodes[0]->sample1 = c->prev_sample;
  264. if(version == CODEC_ID_ADPCM_MS)
  265. nodes[0]->step = c->idelta;
  266. if(version == CODEC_ID_ADPCM_YAMAHA) {
  267. if(c->step == 0) {
  268. nodes[0]->step = 127;
  269. nodes[0]->sample1 = 0;
  270. } else {
  271. nodes[0]->step = c->step;
  272. nodes[0]->sample1 = c->predictor;
  273. }
  274. }
  275. for(i=0; i<n; i++) {
  276. TrellisNode *t = node_buf[i&1];
  277. TrellisNode **u;
  278. int sample = samples[i*stride];
  279. memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
  280. for(j=0; j<frontier && nodes[j]; j++) {
  281. // higher j have higher ssd already, so they're unlikely to use a suboptimal next sample too
  282. const int range = (j < frontier/2) ? 1 : 0;
  283. const int step = nodes[j]->step;
  284. int nidx;
  285. if(version == CODEC_ID_ADPCM_MS) {
  286. const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 64;
  287. const int div = (sample - predictor) / step;
  288. const int nmin = av_clip(div-range, -8, 6);
  289. const int nmax = av_clip(div+range, -7, 7);
  290. for(nidx=nmin; nidx<=nmax; nidx++) {
  291. const int nibble = nidx & 0xf;
  292. int dec_sample = predictor + nidx * step;
  293. #define STORE_NODE(NAME, STEP_INDEX)\
  294. int d;\
  295. uint32_t ssd;\
  296. dec_sample = av_clip_int16(dec_sample);\
  297. d = sample - dec_sample;\
  298. ssd = nodes[j]->ssd + d*d;\
  299. if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\
  300. continue;\
  301. /* Collapse any two states with the same previous sample value. \
  302. * One could also distinguish states by step and by 2nd to last
  303. * sample, but the effects of that are negligible. */\
  304. for(k=0; k<frontier && nodes_next[k]; k++) {\
  305. if(dec_sample == nodes_next[k]->sample1) {\
  306. assert(ssd >= nodes_next[k]->ssd);\
  307. goto next_##NAME;\
  308. }\
  309. }\
  310. for(k=0; k<frontier; k++) {\
  311. if(!nodes_next[k] || ssd < nodes_next[k]->ssd) {\
  312. TrellisNode *u = nodes_next[frontier-1];\
  313. if(!u) {\
  314. assert(pathn < max_paths);\
  315. u = t++;\
  316. u->path = pathn++;\
  317. }\
  318. u->ssd = ssd;\
  319. u->step = STEP_INDEX;\
  320. u->sample2 = nodes[j]->sample1;\
  321. u->sample1 = dec_sample;\
  322. paths[u->path].nibble = nibble;\
  323. paths[u->path].prev = nodes[j]->path;\
  324. memmove(&nodes_next[k+1], &nodes_next[k], (frontier-k-1)*sizeof(TrellisNode*));\
  325. nodes_next[k] = u;\
  326. break;\
  327. }\
  328. }\
  329. next_##NAME:;
  330. STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
  331. }
  332. } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_IMA_QT)|| (version == CODEC_ID_ADPCM_SWF)) {
  333. #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
  334. const int predictor = nodes[j]->sample1;\
  335. const int div = (sample - predictor) * 4 / STEP_TABLE;\
  336. int nmin = av_clip(div-range, -7, 6);\
  337. int nmax = av_clip(div+range, -6, 7);\
  338. if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
  339. if(nmax<0) nmax--;\
  340. for(nidx=nmin; nidx<=nmax; nidx++) {\
  341. const int nibble = nidx<0 ? 7-nidx : nidx;\
  342. int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
  343. STORE_NODE(NAME, STEP_INDEX);\
  344. }
  345. LOOP_NODES(ima, step_table[step], av_clip(step + index_table[nibble], 0, 88));
  346. } else { //CODEC_ID_ADPCM_YAMAHA
  347. LOOP_NODES(yamaha, step, av_clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
  348. #undef LOOP_NODES
  349. #undef STORE_NODE
  350. }
  351. }
  352. u = nodes;
  353. nodes = nodes_next;
  354. nodes_next = u;
  355. // prevent overflow
  356. if(nodes[0]->ssd > (1<<28)) {
  357. for(j=1; j<frontier && nodes[j]; j++)
  358. nodes[j]->ssd -= nodes[0]->ssd;
  359. nodes[0]->ssd = 0;
  360. }
  361. // merge old paths to save memory
  362. if(i == froze + FREEZE_INTERVAL) {
  363. p = &paths[nodes[0]->path];
  364. for(k=i; k>froze; k--) {
  365. dst[k] = p->nibble;
  366. p = &paths[p->prev];
  367. }
  368. froze = i;
  369. pathn = 0;
  370. // other nodes might use paths that don't coincide with the frozen one.
  371. // checking which nodes do so is too slow, so just kill them all.
  372. // this also slightly improves quality, but I don't know why.
  373. memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
  374. }
  375. }
  376. p = &paths[nodes[0]->path];
  377. for(i=n-1; i>froze; i--) {
  378. dst[i] = p->nibble;
  379. p = &paths[p->prev];
  380. }
  381. c->predictor = nodes[0]->sample1;
  382. c->sample1 = nodes[0]->sample1;
  383. c->sample2 = nodes[0]->sample2;
  384. c->step_index = nodes[0]->step;
  385. c->step = nodes[0]->step;
  386. c->idelta = nodes[0]->step;
  387. }
  388. static int adpcm_encode_frame(AVCodecContext *avctx,
  389. unsigned char *frame, int buf_size, void *data)
  390. {
  391. int n, i, st;
  392. short *samples;
  393. unsigned char *dst;
  394. ADPCMContext *c = avctx->priv_data;
  395. dst = frame;
  396. samples = (short *)data;
  397. st= avctx->channels == 2;
  398. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  399. switch(avctx->codec->id) {
  400. case CODEC_ID_ADPCM_IMA_WAV:
  401. n = avctx->frame_size / 8;
  402. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  403. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  404. bytestream_put_le16(&dst, c->status[0].prev_sample);
  405. *dst++ = (unsigned char)c->status[0].step_index;
  406. *dst++ = 0; /* unknown */
  407. samples++;
  408. if (avctx->channels == 2) {
  409. c->status[1].prev_sample = (signed short)samples[0];
  410. /* c->status[1].step_index = 0; */
  411. bytestream_put_le16(&dst, c->status[1].prev_sample);
  412. *dst++ = (unsigned char)c->status[1].step_index;
  413. *dst++ = 0;
  414. samples++;
  415. }
  416. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  417. if(avctx->trellis > 0) {
  418. uint8_t buf[2][n*8];
  419. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n*8);
  420. if(avctx->channels == 2)
  421. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n*8);
  422. for(i=0; i<n; i++) {
  423. *dst++ = buf[0][8*i+0] | (buf[0][8*i+1] << 4);
  424. *dst++ = buf[0][8*i+2] | (buf[0][8*i+3] << 4);
  425. *dst++ = buf[0][8*i+4] | (buf[0][8*i+5] << 4);
  426. *dst++ = buf[0][8*i+6] | (buf[0][8*i+7] << 4);
  427. if (avctx->channels == 2) {
  428. *dst++ = buf[1][8*i+0] | (buf[1][8*i+1] << 4);
  429. *dst++ = buf[1][8*i+2] | (buf[1][8*i+3] << 4);
  430. *dst++ = buf[1][8*i+4] | (buf[1][8*i+5] << 4);
  431. *dst++ = buf[1][8*i+6] | (buf[1][8*i+7] << 4);
  432. }
  433. }
  434. } else
  435. for (; n>0; n--) {
  436. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
  437. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
  438. dst++;
  439. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
  440. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
  441. dst++;
  442. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
  443. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
  444. dst++;
  445. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
  446. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
  447. dst++;
  448. /* right channel */
  449. if (avctx->channels == 2) {
  450. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  451. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  452. dst++;
  453. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  454. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  455. dst++;
  456. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  457. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  458. dst++;
  459. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  460. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  461. dst++;
  462. }
  463. samples += 8 * avctx->channels;
  464. }
  465. break;
  466. case CODEC_ID_ADPCM_IMA_QT:
  467. {
  468. int ch, i;
  469. PutBitContext pb;
  470. init_put_bits(&pb, dst, buf_size*8);
  471. for(ch=0; ch<avctx->channels; ch++){
  472. put_bits(&pb, 9, (c->status[ch].prev_sample + 0x10000) >> 7);
  473. put_bits(&pb, 7, c->status[ch].step_index);
  474. if(avctx->trellis > 0) {
  475. uint8_t buf[64];
  476. adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
  477. for(i=0; i<64; i++)
  478. put_bits(&pb, 4, buf[i^1]);
  479. c->status[ch].prev_sample = c->status[ch].predictor & ~0x7F;
  480. } else {
  481. for (i=0; i<64; i+=2){
  482. int t1, t2;
  483. t1 = adpcm_ima_compress_sample(&c->status[ch], samples[avctx->channels*(i+0)+ch]);
  484. t2 = adpcm_ima_compress_sample(&c->status[ch], samples[avctx->channels*(i+1)+ch]);
  485. put_bits(&pb, 4, t2);
  486. put_bits(&pb, 4, t1);
  487. }
  488. c->status[ch].prev_sample &= ~0x7F;
  489. }
  490. }
  491. dst += put_bits_count(&pb)>>3;
  492. break;
  493. }
  494. case CODEC_ID_ADPCM_SWF:
  495. {
  496. int i;
  497. PutBitContext pb;
  498. init_put_bits(&pb, dst, buf_size*8);
  499. n = avctx->frame_size-1;
  500. //Store AdpcmCodeSize
  501. put_bits(&pb, 2, 2); //Set 4bits flash adpcm format
  502. //Init the encoder state
  503. for(i=0; i<avctx->channels; i++){
  504. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
  505. put_sbits(&pb, 16, samples[i]);
  506. put_bits(&pb, 6, c->status[i].step_index);
  507. c->status[i].prev_sample = (signed short)samples[i];
  508. }
  509. if(avctx->trellis > 0) {
  510. uint8_t buf[2][n];
  511. adpcm_compress_trellis(avctx, samples+2, buf[0], &c->status[0], n);
  512. if (avctx->channels == 2)
  513. adpcm_compress_trellis(avctx, samples+3, buf[1], &c->status[1], n);
  514. for(i=0; i<n; i++) {
  515. put_bits(&pb, 4, buf[0][i]);
  516. if (avctx->channels == 2)
  517. put_bits(&pb, 4, buf[1][i]);
  518. }
  519. } else {
  520. for (i=1; i<avctx->frame_size; i++) {
  521. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
  522. if (avctx->channels == 2)
  523. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
  524. }
  525. }
  526. flush_put_bits(&pb);
  527. dst += put_bits_count(&pb)>>3;
  528. break;
  529. }
  530. case CODEC_ID_ADPCM_MS:
  531. for(i=0; i<avctx->channels; i++){
  532. int predictor=0;
  533. *dst++ = predictor;
  534. c->status[i].coeff1 = AdaptCoeff1[predictor];
  535. c->status[i].coeff2 = AdaptCoeff2[predictor];
  536. }
  537. for(i=0; i<avctx->channels; i++){
  538. if (c->status[i].idelta < 16)
  539. c->status[i].idelta = 16;
  540. bytestream_put_le16(&dst, c->status[i].idelta);
  541. }
  542. for(i=0; i<avctx->channels; i++){
  543. c->status[i].sample2= *samples++;
  544. }
  545. for(i=0; i<avctx->channels; i++){
  546. c->status[i].sample1= *samples++;
  547. bytestream_put_le16(&dst, c->status[i].sample1);
  548. }
  549. for(i=0; i<avctx->channels; i++)
  550. bytestream_put_le16(&dst, c->status[i].sample2);
  551. if(avctx->trellis > 0) {
  552. int n = avctx->block_align - 7*avctx->channels;
  553. uint8_t buf[2][n];
  554. if(avctx->channels == 1) {
  555. n *= 2;
  556. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  557. for(i=0; i<n; i+=2)
  558. *dst++ = (buf[0][i] << 4) | buf[0][i+1];
  559. } else {
  560. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  561. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
  562. for(i=0; i<n; i++)
  563. *dst++ = (buf[0][i] << 4) | buf[1][i];
  564. }
  565. } else
  566. for(i=7*avctx->channels; i<avctx->block_align; i++) {
  567. int nibble;
  568. nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
  569. nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
  570. *dst++ = nibble;
  571. }
  572. break;
  573. case CODEC_ID_ADPCM_YAMAHA:
  574. n = avctx->frame_size / 2;
  575. if(avctx->trellis > 0) {
  576. uint8_t buf[2][n*2];
  577. n *= 2;
  578. if(avctx->channels == 1) {
  579. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  580. for(i=0; i<n; i+=2)
  581. *dst++ = buf[0][i] | (buf[0][i+1] << 4);
  582. } else {
  583. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  584. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
  585. for(i=0; i<n; i++)
  586. *dst++ = buf[0][i] | (buf[1][i] << 4);
  587. }
  588. } else
  589. for (; n>0; n--) {
  590. for(i = 0; i < avctx->channels; i++) {
  591. int nibble;
  592. nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
  593. nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
  594. *dst++ = nibble;
  595. }
  596. samples += 2 * avctx->channels;
  597. }
  598. break;
  599. default:
  600. return -1;
  601. }
  602. return dst - frame;
  603. }
  604. #endif //CONFIG_ENCODERS
  605. static av_cold int adpcm_decode_init(AVCodecContext * avctx)
  606. {
  607. ADPCMContext *c = avctx->priv_data;
  608. unsigned int max_channels = 2;
  609. switch(avctx->codec->id) {
  610. case CODEC_ID_ADPCM_EA_R1:
  611. case CODEC_ID_ADPCM_EA_R2:
  612. case CODEC_ID_ADPCM_EA_R3:
  613. max_channels = 6;
  614. break;
  615. }
  616. if(avctx->channels > max_channels){
  617. return -1;
  618. }
  619. switch(avctx->codec->id) {
  620. case CODEC_ID_ADPCM_CT:
  621. c->status[0].step = c->status[1].step = 511;
  622. break;
  623. case CODEC_ID_ADPCM_IMA_WS:
  624. if (avctx->extradata && avctx->extradata_size == 2 * 4) {
  625. c->status[0].predictor = AV_RL32(avctx->extradata);
  626. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  627. }
  628. break;
  629. default:
  630. break;
  631. }
  632. return 0;
  633. }
  634. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  635. {
  636. int step_index;
  637. int predictor;
  638. int sign, delta, diff, step;
  639. step = step_table[c->step_index];
  640. step_index = c->step_index + index_table[(unsigned)nibble];
  641. if (step_index < 0) step_index = 0;
  642. else if (step_index > 88) step_index = 88;
  643. sign = nibble & 8;
  644. delta = nibble & 7;
  645. /* perform direct multiplication instead of series of jumps proposed by
  646. * the reference ADPCM implementation since modern CPUs can do the mults
  647. * quickly enough */
  648. diff = ((2 * delta + 1) * step) >> shift;
  649. predictor = c->predictor;
  650. if (sign) predictor -= diff;
  651. else predictor += diff;
  652. c->predictor = av_clip_int16(predictor);
  653. c->step_index = step_index;
  654. return (short)c->predictor;
  655. }
  656. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  657. {
  658. int predictor;
  659. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  660. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  661. c->sample2 = c->sample1;
  662. c->sample1 = av_clip_int16(predictor);
  663. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  664. if (c->idelta < 16) c->idelta = 16;
  665. return c->sample1;
  666. }
  667. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  668. {
  669. int sign, delta, diff;
  670. int new_step;
  671. sign = nibble & 8;
  672. delta = nibble & 7;
  673. /* perform direct multiplication instead of series of jumps proposed by
  674. * the reference ADPCM implementation since modern CPUs can do the mults
  675. * quickly enough */
  676. diff = ((2 * delta + 1) * c->step) >> 3;
  677. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  678. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  679. c->predictor = av_clip_int16(c->predictor);
  680. /* calculate new step and clamp it to range 511..32767 */
  681. new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
  682. c->step = av_clip(new_step, 511, 32767);
  683. return (short)c->predictor;
  684. }
  685. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  686. {
  687. int sign, delta, diff;
  688. sign = nibble & (1<<(size-1));
  689. delta = nibble & ((1<<(size-1))-1);
  690. diff = delta << (7 + c->step + shift);
  691. /* clamp result */
  692. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  693. /* calculate new step */
  694. if (delta >= (2*size - 3) && c->step < 3)
  695. c->step++;
  696. else if (delta == 0 && c->step > 0)
  697. c->step--;
  698. return (short) c->predictor;
  699. }
  700. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  701. {
  702. if(!c->step) {
  703. c->predictor = 0;
  704. c->step = 127;
  705. }
  706. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  707. c->predictor = av_clip_int16(c->predictor);
  708. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  709. c->step = av_clip(c->step, 127, 24567);
  710. return c->predictor;
  711. }
  712. static void xa_decode(short *out, const unsigned char *in,
  713. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  714. {
  715. int i, j;
  716. int shift,filter,f0,f1;
  717. int s_1,s_2;
  718. int d,s,t;
  719. for(i=0;i<4;i++) {
  720. shift = 12 - (in[4+i*2] & 15);
  721. filter = in[4+i*2] >> 4;
  722. f0 = xa_adpcm_table[filter][0];
  723. f1 = xa_adpcm_table[filter][1];
  724. s_1 = left->sample1;
  725. s_2 = left->sample2;
  726. for(j=0;j<28;j++) {
  727. d = in[16+i+j*4];
  728. t = (signed char)(d<<4)>>4;
  729. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  730. s_2 = s_1;
  731. s_1 = av_clip_int16(s);
  732. *out = s_1;
  733. out += inc;
  734. }
  735. if (inc==2) { /* stereo */
  736. left->sample1 = s_1;
  737. left->sample2 = s_2;
  738. s_1 = right->sample1;
  739. s_2 = right->sample2;
  740. out = out + 1 - 28*2;
  741. }
  742. shift = 12 - (in[5+i*2] & 15);
  743. filter = in[5+i*2] >> 4;
  744. f0 = xa_adpcm_table[filter][0];
  745. f1 = xa_adpcm_table[filter][1];
  746. for(j=0;j<28;j++) {
  747. d = in[16+i+j*4];
  748. t = (signed char)d >> 4;
  749. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  750. s_2 = s_1;
  751. s_1 = av_clip_int16(s);
  752. *out = s_1;
  753. out += inc;
  754. }
  755. if (inc==2) { /* stereo */
  756. right->sample1 = s_1;
  757. right->sample2 = s_2;
  758. out -= 1;
  759. } else {
  760. left->sample1 = s_1;
  761. left->sample2 = s_2;
  762. }
  763. }
  764. }
  765. /* DK3 ADPCM support macro */
  766. #define DK3_GET_NEXT_NIBBLE() \
  767. if (decode_top_nibble_next) \
  768. { \
  769. nibble = last_byte >> 4; \
  770. decode_top_nibble_next = 0; \
  771. } \
  772. else \
  773. { \
  774. last_byte = *src++; \
  775. if (src >= buf + buf_size) break; \
  776. nibble = last_byte & 0x0F; \
  777. decode_top_nibble_next = 1; \
  778. }
  779. static int adpcm_decode_frame(AVCodecContext *avctx,
  780. void *data, int *data_size,
  781. const uint8_t *buf, int buf_size)
  782. {
  783. ADPCMContext *c = avctx->priv_data;
  784. ADPCMChannelStatus *cs;
  785. int n, m, channel, i;
  786. int block_predictor[2];
  787. short *samples;
  788. short *samples_end;
  789. const uint8_t *src;
  790. int st; /* stereo */
  791. /* DK3 ADPCM accounting variables */
  792. unsigned char last_byte = 0;
  793. unsigned char nibble;
  794. int decode_top_nibble_next = 0;
  795. int diff_channel;
  796. /* EA ADPCM state variables */
  797. uint32_t samples_in_chunk;
  798. int32_t previous_left_sample, previous_right_sample;
  799. int32_t current_left_sample, current_right_sample;
  800. int32_t next_left_sample, next_right_sample;
  801. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  802. uint8_t shift_left, shift_right;
  803. int count1, count2;
  804. int coeff[2][2], shift[2];//used in EA MAXIS ADPCM
  805. if (!buf_size)
  806. return 0;
  807. //should protect all 4bit ADPCM variants
  808. //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
  809. //
  810. if(*data_size/4 < buf_size + 8)
  811. return -1;
  812. samples = data;
  813. samples_end= samples + *data_size/2;
  814. *data_size= 0;
  815. src = buf;
  816. st = avctx->channels == 2 ? 1 : 0;
  817. switch(avctx->codec->id) {
  818. case CODEC_ID_ADPCM_IMA_QT:
  819. n = buf_size - 2*avctx->channels;
  820. for (channel = 0; channel < avctx->channels; channel++) {
  821. cs = &(c->status[channel]);
  822. /* (pppppp) (piiiiiii) */
  823. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  824. cs->predictor = (*src++) << 8;
  825. cs->predictor |= (*src & 0x80);
  826. cs->predictor &= 0xFF80;
  827. /* sign extension */
  828. if(cs->predictor & 0x8000)
  829. cs->predictor -= 0x10000;
  830. cs->predictor = av_clip_int16(cs->predictor);
  831. cs->step_index = (*src++) & 0x7F;
  832. if (cs->step_index > 88){
  833. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  834. cs->step_index = 88;
  835. }
  836. cs->step = step_table[cs->step_index];
  837. samples = (short*)data + channel;
  838. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  839. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  840. samples += avctx->channels;
  841. *samples = adpcm_ima_expand_nibble(cs, src[0] >> 4 , 3);
  842. samples += avctx->channels;
  843. src ++;
  844. }
  845. }
  846. if (st)
  847. samples--;
  848. break;
  849. case CODEC_ID_ADPCM_IMA_WAV:
  850. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  851. buf_size = avctx->block_align;
  852. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  853. for(i=0; i<avctx->channels; i++){
  854. cs = &(c->status[i]);
  855. cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
  856. cs->step_index = *src++;
  857. if (cs->step_index > 88){
  858. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  859. cs->step_index = 88;
  860. }
  861. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  862. }
  863. while(src < buf + buf_size){
  864. for(m=0; m<4; m++){
  865. for(i=0; i<=st; i++)
  866. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
  867. for(i=0; i<=st; i++)
  868. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
  869. src++;
  870. }
  871. src += 4*st;
  872. }
  873. break;
  874. case CODEC_ID_ADPCM_4XM:
  875. cs = &(c->status[0]);
  876. c->status[0].predictor= (int16_t)bytestream_get_le16(&src);
  877. if(st){
  878. c->status[1].predictor= (int16_t)bytestream_get_le16(&src);
  879. }
  880. c->status[0].step_index= (int16_t)bytestream_get_le16(&src);
  881. if(st){
  882. c->status[1].step_index= (int16_t)bytestream_get_le16(&src);
  883. }
  884. if (cs->step_index < 0) cs->step_index = 0;
  885. if (cs->step_index > 88) cs->step_index = 88;
  886. m= (buf_size - (src - buf))>>st;
  887. for(i=0; i<m; i++) {
  888. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  889. if (st)
  890. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  891. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  892. if (st)
  893. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  894. }
  895. src += m<<st;
  896. break;
  897. case CODEC_ID_ADPCM_MS:
  898. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  899. buf_size = avctx->block_align;
  900. n = buf_size - 7 * avctx->channels;
  901. if (n < 0)
  902. return -1;
  903. block_predictor[0] = av_clip(*src++, 0, 6);
  904. block_predictor[1] = 0;
  905. if (st)
  906. block_predictor[1] = av_clip(*src++, 0, 6);
  907. c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
  908. if (st){
  909. c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
  910. }
  911. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  912. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  913. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  914. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  915. c->status[0].sample1 = bytestream_get_le16(&src);
  916. if (st) c->status[1].sample1 = bytestream_get_le16(&src);
  917. c->status[0].sample2 = bytestream_get_le16(&src);
  918. if (st) c->status[1].sample2 = bytestream_get_le16(&src);
  919. *samples++ = c->status[0].sample2;
  920. if (st) *samples++ = c->status[1].sample2;
  921. *samples++ = c->status[0].sample1;
  922. if (st) *samples++ = c->status[1].sample1;
  923. for(;n>0;n--) {
  924. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
  925. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  926. src ++;
  927. }
  928. break;
  929. case CODEC_ID_ADPCM_IMA_DK4:
  930. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  931. buf_size = avctx->block_align;
  932. c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
  933. c->status[0].step_index = *src++;
  934. src++;
  935. *samples++ = c->status[0].predictor;
  936. if (st) {
  937. c->status[1].predictor = (int16_t)bytestream_get_le16(&src);
  938. c->status[1].step_index = *src++;
  939. src++;
  940. *samples++ = c->status[1].predictor;
  941. }
  942. while (src < buf + buf_size) {
  943. /* take care of the top nibble (always left or mono channel) */
  944. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  945. src[0] >> 4, 3);
  946. /* take care of the bottom nibble, which is right sample for
  947. * stereo, or another mono sample */
  948. if (st)
  949. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  950. src[0] & 0x0F, 3);
  951. else
  952. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  953. src[0] & 0x0F, 3);
  954. src++;
  955. }
  956. break;
  957. case CODEC_ID_ADPCM_IMA_DK3:
  958. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  959. buf_size = avctx->block_align;
  960. if(buf_size + 16 > (samples_end - samples)*3/8)
  961. return -1;
  962. c->status[0].predictor = (int16_t)AV_RL16(src + 10);
  963. c->status[1].predictor = (int16_t)AV_RL16(src + 12);
  964. c->status[0].step_index = src[14];
  965. c->status[1].step_index = src[15];
  966. /* sign extend the predictors */
  967. src += 16;
  968. diff_channel = c->status[1].predictor;
  969. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  970. * the buffer is consumed */
  971. while (1) {
  972. /* for this algorithm, c->status[0] is the sum channel and
  973. * c->status[1] is the diff channel */
  974. /* process the first predictor of the sum channel */
  975. DK3_GET_NEXT_NIBBLE();
  976. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  977. /* process the diff channel predictor */
  978. DK3_GET_NEXT_NIBBLE();
  979. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  980. /* process the first pair of stereo PCM samples */
  981. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  982. *samples++ = c->status[0].predictor + c->status[1].predictor;
  983. *samples++ = c->status[0].predictor - c->status[1].predictor;
  984. /* process the second predictor of the sum channel */
  985. DK3_GET_NEXT_NIBBLE();
  986. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  987. /* process the second pair of stereo PCM samples */
  988. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  989. *samples++ = c->status[0].predictor + c->status[1].predictor;
  990. *samples++ = c->status[0].predictor - c->status[1].predictor;
  991. }
  992. break;
  993. case CODEC_ID_ADPCM_IMA_WS:
  994. /* no per-block initialization; just start decoding the data */
  995. while (src < buf + buf_size) {
  996. if (st) {
  997. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  998. src[0] >> 4 , 3);
  999. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  1000. src[0] & 0x0F, 3);
  1001. } else {
  1002. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1003. src[0] >> 4 , 3);
  1004. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1005. src[0] & 0x0F, 3);
  1006. }
  1007. src++;
  1008. }
  1009. break;
  1010. case CODEC_ID_ADPCM_XA:
  1011. while (buf_size >= 128) {
  1012. xa_decode(samples, src, &c->status[0], &c->status[1],
  1013. avctx->channels);
  1014. src += 128;
  1015. samples += 28 * 8;
  1016. buf_size -= 128;
  1017. }
  1018. break;
  1019. case CODEC_ID_ADPCM_IMA_EA_EACS:
  1020. samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
  1021. if (samples_in_chunk > buf_size-4-(8<<st)) {
  1022. src += buf_size - 4;
  1023. break;
  1024. }
  1025. for (i=0; i<=st; i++)
  1026. c->status[i].step_index = bytestream_get_le32(&src);
  1027. for (i=0; i<=st; i++)
  1028. c->status[i].predictor = bytestream_get_le32(&src);
  1029. for (; samples_in_chunk; samples_in_chunk--, src++) {
  1030. *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
  1031. *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
  1032. }
  1033. break;
  1034. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1035. for (; src < buf+buf_size; src++) {
  1036. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
  1037. *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
  1038. }
  1039. break;
  1040. case CODEC_ID_ADPCM_EA:
  1041. samples_in_chunk = AV_RL32(src);
  1042. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  1043. src += buf_size;
  1044. break;
  1045. }
  1046. src += 4;
  1047. current_left_sample = (int16_t)bytestream_get_le16(&src);
  1048. previous_left_sample = (int16_t)bytestream_get_le16(&src);
  1049. current_right_sample = (int16_t)bytestream_get_le16(&src);
  1050. previous_right_sample = (int16_t)bytestream_get_le16(&src);
  1051. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  1052. coeff1l = ea_adpcm_table[ *src >> 4 ];
  1053. coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
  1054. coeff1r = ea_adpcm_table[*src & 0x0F];
  1055. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  1056. src++;
  1057. shift_left = (*src >> 4 ) + 8;
  1058. shift_right = (*src & 0x0F) + 8;
  1059. src++;
  1060. for (count2 = 0; count2 < 28; count2++) {
  1061. next_left_sample = (int32_t)((*src & 0xF0) << 24) >> shift_left;
  1062. next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
  1063. src++;
  1064. next_left_sample = (next_left_sample +
  1065. (current_left_sample * coeff1l) +
  1066. (previous_left_sample * coeff2l) + 0x80) >> 8;
  1067. next_right_sample = (next_right_sample +
  1068. (current_right_sample * coeff1r) +
  1069. (previous_right_sample * coeff2r) + 0x80) >> 8;
  1070. previous_left_sample = current_left_sample;
  1071. current_left_sample = av_clip_int16(next_left_sample);
  1072. previous_right_sample = current_right_sample;
  1073. current_right_sample = av_clip_int16(next_right_sample);
  1074. *samples++ = (unsigned short)current_left_sample;
  1075. *samples++ = (unsigned short)current_right_sample;
  1076. }
  1077. }
  1078. break;
  1079. case CODEC_ID_ADPCM_EA_MAXIS_XA:
  1080. for(channel = 0; channel < avctx->channels; channel++) {
  1081. for (i=0; i<2; i++)
  1082. coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
  1083. shift[channel] = (*src & 0x0F) + 8;
  1084. src++;
  1085. }
  1086. for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
  1087. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  1088. for(channel = 0; channel < avctx->channels; channel++) {
  1089. int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
  1090. sample = (sample +
  1091. c->status[channel].sample1 * coeff[channel][0] +
  1092. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  1093. c->status[channel].sample2 = c->status[channel].sample1;
  1094. c->status[channel].sample1 = av_clip_int16(sample);
  1095. *samples++ = c->status[channel].sample1;
  1096. }
  1097. }
  1098. src+=avctx->channels;
  1099. }
  1100. break;
  1101. case CODEC_ID_ADPCM_EA_R1:
  1102. case CODEC_ID_ADPCM_EA_R2:
  1103. case CODEC_ID_ADPCM_EA_R3: {
  1104. /* channel numbering
  1105. 2chan: 0=fl, 1=fr
  1106. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  1107. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  1108. const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
  1109. int32_t previous_sample, current_sample, next_sample;
  1110. int32_t coeff1, coeff2;
  1111. uint8_t shift;
  1112. unsigned int channel;
  1113. uint16_t *samplesC;
  1114. const uint8_t *srcC;
  1115. samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
  1116. : bytestream_get_le32(&src)) / 28;
  1117. if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
  1118. 28*samples_in_chunk*avctx->channels > samples_end-samples) {
  1119. src += buf_size - 4;
  1120. break;
  1121. }
  1122. for (channel=0; channel<avctx->channels; channel++) {
  1123. srcC = src + (big_endian ? bytestream_get_be32(&src)
  1124. : bytestream_get_le32(&src))
  1125. + (avctx->channels-channel-1) * 4;
  1126. samplesC = samples + channel;
  1127. if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
  1128. current_sample = (int16_t)bytestream_get_le16(&srcC);
  1129. previous_sample = (int16_t)bytestream_get_le16(&srcC);
  1130. } else {
  1131. current_sample = c->status[channel].predictor;
  1132. previous_sample = c->status[channel].prev_sample;
  1133. }
  1134. for (count1=0; count1<samples_in_chunk; count1++) {
  1135. if (*srcC == 0xEE) { /* only seen in R2 and R3 */
  1136. srcC++;
  1137. current_sample = (int16_t)bytestream_get_be16(&srcC);
  1138. previous_sample = (int16_t)bytestream_get_be16(&srcC);
  1139. for (count2=0; count2<28; count2++) {
  1140. *samplesC = (int16_t)bytestream_get_be16(&srcC);
  1141. samplesC += avctx->channels;
  1142. }
  1143. } else {
  1144. coeff1 = ea_adpcm_table[ *srcC>>4 ];
  1145. coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
  1146. shift = (*srcC++ & 0x0F) + 8;
  1147. for (count2=0; count2<28; count2++) {
  1148. if (count2 & 1)
  1149. next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
  1150. else
  1151. next_sample = (int32_t)((*srcC & 0xF0) << 24) >> shift;
  1152. next_sample += (current_sample * coeff1) +
  1153. (previous_sample * coeff2);
  1154. next_sample = av_clip_int16(next_sample >> 8);
  1155. previous_sample = current_sample;
  1156. current_sample = next_sample;
  1157. *samplesC = current_sample;
  1158. samplesC += avctx->channels;
  1159. }
  1160. }
  1161. }
  1162. if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
  1163. c->status[channel].predictor = current_sample;
  1164. c->status[channel].prev_sample = previous_sample;
  1165. }
  1166. }
  1167. src = src + buf_size - (4 + 4*avctx->channels);
  1168. samples += 28 * samples_in_chunk * avctx->channels;
  1169. break;
  1170. }
  1171. case CODEC_ID_ADPCM_EA_XAS:
  1172. if (samples_end-samples < 32*4*avctx->channels
  1173. || buf_size < (4+15)*4*avctx->channels) {
  1174. src += buf_size;
  1175. break;
  1176. }
  1177. for (channel=0; channel<avctx->channels; channel++) {
  1178. int coeff[2][4], shift[4];
  1179. short *s2, *s = &samples[channel];
  1180. for (n=0; n<4; n++, s+=32*avctx->channels) {
  1181. for (i=0; i<2; i++)
  1182. coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
  1183. shift[n] = (src[2]&0x0F) + 8;
  1184. for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
  1185. s2[0] = (src[0]&0xF0) + (src[1]<<8);
  1186. }
  1187. for (m=2; m<32; m+=2) {
  1188. s = &samples[m*avctx->channels + channel];
  1189. for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
  1190. for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
  1191. int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
  1192. int pred = s2[-1*avctx->channels] * coeff[0][n]
  1193. + s2[-2*avctx->channels] * coeff[1][n];
  1194. s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
  1195. }
  1196. }
  1197. }
  1198. }
  1199. samples += 32*4*avctx->channels;
  1200. break;
  1201. case CODEC_ID_ADPCM_IMA_AMV:
  1202. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1203. c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
  1204. c->status[0].step_index = bytestream_get_le16(&src);
  1205. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1206. src+=4;
  1207. while (src < buf + buf_size) {
  1208. char hi, lo;
  1209. lo = *src & 0x0F;
  1210. hi = *src >> 4;
  1211. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1212. FFSWAP(char, hi, lo);
  1213. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1214. lo, 3);
  1215. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1216. hi, 3);
  1217. src++;
  1218. }
  1219. break;
  1220. case CODEC_ID_ADPCM_CT:
  1221. while (src < buf + buf_size) {
  1222. if (st) {
  1223. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1224. src[0] >> 4);
  1225. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  1226. src[0] & 0x0F);
  1227. } else {
  1228. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1229. src[0] >> 4);
  1230. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1231. src[0] & 0x0F);
  1232. }
  1233. src++;
  1234. }
  1235. break;
  1236. case CODEC_ID_ADPCM_SBPRO_4:
  1237. case CODEC_ID_ADPCM_SBPRO_3:
  1238. case CODEC_ID_ADPCM_SBPRO_2:
  1239. if (!c->status[0].step_index) {
  1240. /* the first byte is a raw sample */
  1241. *samples++ = 128 * (*src++ - 0x80);
  1242. if (st)
  1243. *samples++ = 128 * (*src++ - 0x80);
  1244. c->status[0].step_index = 1;
  1245. }
  1246. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  1247. while (src < buf + buf_size) {
  1248. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1249. src[0] >> 4, 4, 0);
  1250. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1251. src[0] & 0x0F, 4, 0);
  1252. src++;
  1253. }
  1254. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1255. while (src < buf + buf_size && samples + 2 < samples_end) {
  1256. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1257. src[0] >> 5 , 3, 0);
  1258. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1259. (src[0] >> 2) & 0x07, 3, 0);
  1260. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1261. src[0] & 0x03, 2, 0);
  1262. src++;
  1263. }
  1264. } else {
  1265. while (src < buf + buf_size && samples + 3 < samples_end) {
  1266. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1267. src[0] >> 6 , 2, 2);
  1268. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1269. (src[0] >> 4) & 0x03, 2, 2);
  1270. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1271. (src[0] >> 2) & 0x03, 2, 2);
  1272. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1273. src[0] & 0x03, 2, 2);
  1274. src++;
  1275. }
  1276. }
  1277. break;
  1278. case CODEC_ID_ADPCM_SWF:
  1279. {
  1280. GetBitContext gb;
  1281. const int *table;
  1282. int k0, signmask, nb_bits, count;
  1283. int size = buf_size*8;
  1284. init_get_bits(&gb, buf, size);
  1285. //read bits & initial values
  1286. nb_bits = get_bits(&gb, 2)+2;
  1287. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  1288. table = swf_index_tables[nb_bits-2];
  1289. k0 = 1 << (nb_bits-2);
  1290. signmask = 1 << (nb_bits-1);
  1291. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  1292. for (i = 0; i < avctx->channels; i++) {
  1293. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1294. c->status[i].step_index = get_bits(&gb, 6);
  1295. }
  1296. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  1297. int i;
  1298. for (i = 0; i < avctx->channels; i++) {
  1299. // similar to IMA adpcm
  1300. int delta = get_bits(&gb, nb_bits);
  1301. int step = step_table[c->status[i].step_index];
  1302. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  1303. int k = k0;
  1304. do {
  1305. if (delta & k)
  1306. vpdiff += step;
  1307. step >>= 1;
  1308. k >>= 1;
  1309. } while(k);
  1310. vpdiff += step;
  1311. if (delta & signmask)
  1312. c->status[i].predictor -= vpdiff;
  1313. else
  1314. c->status[i].predictor += vpdiff;
  1315. c->status[i].step_index += table[delta & (~signmask)];
  1316. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  1317. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  1318. *samples++ = c->status[i].predictor;
  1319. if (samples >= samples_end) {
  1320. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1321. return -1;
  1322. }
  1323. }
  1324. }
  1325. }
  1326. src += buf_size;
  1327. break;
  1328. }
  1329. case CODEC_ID_ADPCM_YAMAHA:
  1330. while (src < buf + buf_size) {
  1331. if (st) {
  1332. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1333. src[0] & 0x0F);
  1334. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  1335. src[0] >> 4 );
  1336. } else {
  1337. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1338. src[0] & 0x0F);
  1339. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1340. src[0] >> 4 );
  1341. }
  1342. src++;
  1343. }
  1344. break;
  1345. case CODEC_ID_ADPCM_THP:
  1346. {
  1347. int table[2][16];
  1348. unsigned int samplecnt;
  1349. int prev[2][2];
  1350. int ch;
  1351. if (buf_size < 80) {
  1352. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  1353. return -1;
  1354. }
  1355. src+=4;
  1356. samplecnt = bytestream_get_be32(&src);
  1357. for (i = 0; i < 32; i++)
  1358. table[0][i] = (int16_t)bytestream_get_be16(&src);
  1359. /* Initialize the previous sample. */
  1360. for (i = 0; i < 4; i++)
  1361. prev[0][i] = (int16_t)bytestream_get_be16(&src);
  1362. if (samplecnt >= (samples_end - samples) / (st + 1)) {
  1363. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1364. return -1;
  1365. }
  1366. for (ch = 0; ch <= st; ch++) {
  1367. samples = (unsigned short *) data + ch;
  1368. /* Read in every sample for this channel. */
  1369. for (i = 0; i < samplecnt / 14; i++) {
  1370. int index = (*src >> 4) & 7;
  1371. unsigned int exp = 28 - (*src++ & 15);
  1372. int factor1 = table[ch][index * 2];
  1373. int factor2 = table[ch][index * 2 + 1];
  1374. /* Decode 14 samples. */
  1375. for (n = 0; n < 14; n++) {
  1376. int32_t sampledat;
  1377. if(n&1) sampledat= *src++ <<28;
  1378. else sampledat= (*src&0xF0)<<24;
  1379. sampledat = ((prev[ch][0]*factor1
  1380. + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
  1381. *samples = av_clip_int16(sampledat);
  1382. prev[ch][1] = prev[ch][0];
  1383. prev[ch][0] = *samples++;
  1384. /* In case of stereo, skip one sample, this sample
  1385. is for the other channel. */
  1386. samples += st;
  1387. }
  1388. }
  1389. }
  1390. /* In the previous loop, in case stereo is used, samples is
  1391. increased exactly one time too often. */
  1392. samples -= st;
  1393. break;
  1394. }
  1395. default:
  1396. return -1;
  1397. }
  1398. *data_size = (uint8_t *)samples - (uint8_t *)data;
  1399. return src - buf;
  1400. }
  1401. #ifdef CONFIG_ENCODERS
  1402. #define ADPCM_ENCODER(id,name,long_name_) \
  1403. AVCodec name ## _encoder = { \
  1404. #name, \
  1405. CODEC_TYPE_AUDIO, \
  1406. id, \
  1407. sizeof(ADPCMContext), \
  1408. adpcm_encode_init, \
  1409. adpcm_encode_frame, \
  1410. adpcm_encode_close, \
  1411. NULL, \
  1412. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1413. };
  1414. #else
  1415. #define ADPCM_ENCODER(id,name,long_name_)
  1416. #endif
  1417. #ifdef CONFIG_DECODERS
  1418. #define ADPCM_DECODER(id,name,long_name_) \
  1419. AVCodec name ## _decoder = { \
  1420. #name, \
  1421. CODEC_TYPE_AUDIO, \
  1422. id, \
  1423. sizeof(ADPCMContext), \
  1424. adpcm_decode_init, \
  1425. NULL, \
  1426. NULL, \
  1427. adpcm_decode_frame, \
  1428. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1429. };
  1430. #else
  1431. #define ADPCM_DECODER(id,name,long_name_)
  1432. #endif
  1433. #define ADPCM_CODEC(id,name,long_name_) \
  1434. ADPCM_ENCODER(id,name,long_name_) ADPCM_DECODER(id,name,long_name_)
  1435. /* Note: Do not forget to add new entries to the Makefile as well. */
  1436. ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "4X Movie ADPCM");
  1437. ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "Creative Technology ADPCM");
  1438. ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "Electronic Arts ADPCM");
  1439. ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "Electronic Arts Maxis CDROM XA ADPCM");
  1440. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "Electronic Arts R1 ADPCM");
  1441. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "Electronic Arts R2 ADPCM");
  1442. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "Electronic Arts R3 ADPCM");
  1443. ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "Electronic Arts XAS ADPCM");
  1444. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "IMA AMV ADPCM");
  1445. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "IMA Duck DK3 ADPCM");
  1446. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "IMA Duck DK4 ADPCM");
  1447. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "IMA Electronic Arts EACS ADPCM");
  1448. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "IMA Electronic Arts SEAD ADPCM");
  1449. ADPCM_CODEC (CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "IMA QuickTime ADPCM");
  1450. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "IMA Loki SDL MJPEG ADPCM");
  1451. ADPCM_CODEC (CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "IMA Wav ADPCM");
  1452. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "IMA Westwood ADPCM");
  1453. ADPCM_CODEC (CODEC_ID_ADPCM_MS, adpcm_ms, "Microsoft ADPCM");
  1454. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "Sound Blaster Pro 2-bit ADPCM");
  1455. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "Sound Blaster Pro 2.6-bit ADPCM");
  1456. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "Sound Blaster Pro 4-bit ADPCM");
  1457. ADPCM_CODEC (CODEC_ID_ADPCM_SWF, adpcm_swf, "Shockwave Flash ADPCM");
  1458. ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "Nintendo Gamecube THP ADPCM");
  1459. ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "CDROM XA ADPCM");
  1460. ADPCM_CODEC (CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "Yamaha ADPCM");