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.

656 lines
26KB

  1. /*
  2. * Copyright (c) 2001-2003 The ffmpeg Project
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avcodec.h"
  21. #include "get_bits.h"
  22. #include "put_bits.h"
  23. #include "bytestream.h"
  24. #include "adpcm.h"
  25. #include "adpcm_data.h"
  26. /**
  27. * @file
  28. * ADPCM encoders
  29. * First version by Francois Revol (revol@free.fr)
  30. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  31. * by Mike Melanson (melanson@pcisys.net)
  32. *
  33. * Reference documents:
  34. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  35. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  36. * http://openquicktime.sourceforge.net/plugins.htm
  37. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  38. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  39. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  40. */
  41. typedef struct TrellisPath {
  42. int nibble;
  43. int prev;
  44. } TrellisPath;
  45. typedef struct TrellisNode {
  46. uint32_t ssd;
  47. int path;
  48. int sample1;
  49. int sample2;
  50. int step;
  51. } TrellisNode;
  52. typedef struct ADPCMEncodeContext {
  53. ADPCMChannelStatus status[6];
  54. TrellisPath *paths;
  55. TrellisNode *node_buf;
  56. TrellisNode **nodep_buf;
  57. uint8_t *trellis_hash;
  58. } ADPCMEncodeContext;
  59. #define FREEZE_INTERVAL 128
  60. static av_cold int adpcm_encode_init(AVCodecContext *avctx)
  61. {
  62. ADPCMEncodeContext *s = avctx->priv_data;
  63. uint8_t *extradata;
  64. int i;
  65. if (avctx->channels > 2)
  66. return -1; /* only stereo or mono =) */
  67. if(avctx->trellis && (unsigned)avctx->trellis > 16U){
  68. av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
  69. return -1;
  70. }
  71. if (avctx->trellis) {
  72. int frontier = 1 << avctx->trellis;
  73. int max_paths = frontier * FREEZE_INTERVAL;
  74. FF_ALLOC_OR_GOTO(avctx, s->paths, max_paths * sizeof(*s->paths), error);
  75. FF_ALLOC_OR_GOTO(avctx, s->node_buf, 2 * frontier * sizeof(*s->node_buf), error);
  76. FF_ALLOC_OR_GOTO(avctx, s->nodep_buf, 2 * frontier * sizeof(*s->nodep_buf), error);
  77. FF_ALLOC_OR_GOTO(avctx, s->trellis_hash, 65536 * sizeof(*s->trellis_hash), error);
  78. }
  79. switch(avctx->codec->id) {
  80. case CODEC_ID_ADPCM_IMA_WAV:
  81. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  82. /* and we have 4 bytes per channel overhead */
  83. avctx->block_align = BLKSIZE;
  84. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  85. break;
  86. case CODEC_ID_ADPCM_IMA_QT:
  87. avctx->frame_size = 64;
  88. avctx->block_align = 34 * avctx->channels;
  89. break;
  90. case CODEC_ID_ADPCM_MS:
  91. avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
  92. /* and we have 7 bytes per channel overhead */
  93. avctx->block_align = BLKSIZE;
  94. avctx->extradata_size = 32;
  95. extradata = avctx->extradata = av_malloc(avctx->extradata_size);
  96. if (!extradata)
  97. return AVERROR(ENOMEM);
  98. bytestream_put_le16(&extradata, avctx->frame_size);
  99. bytestream_put_le16(&extradata, 7); /* wNumCoef */
  100. for (i = 0; i < 7; i++) {
  101. bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
  102. bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
  103. }
  104. break;
  105. case CODEC_ID_ADPCM_YAMAHA:
  106. avctx->frame_size = BLKSIZE * avctx->channels;
  107. avctx->block_align = BLKSIZE;
  108. break;
  109. case CODEC_ID_ADPCM_SWF:
  110. if (avctx->sample_rate != 11025 &&
  111. avctx->sample_rate != 22050 &&
  112. avctx->sample_rate != 44100) {
  113. av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
  114. goto error;
  115. }
  116. avctx->frame_size = 512 * (avctx->sample_rate / 11025);
  117. break;
  118. default:
  119. goto error;
  120. }
  121. avctx->coded_frame= avcodec_alloc_frame();
  122. avctx->coded_frame->key_frame= 1;
  123. return 0;
  124. error:
  125. av_freep(&s->paths);
  126. av_freep(&s->node_buf);
  127. av_freep(&s->nodep_buf);
  128. av_freep(&s->trellis_hash);
  129. return -1;
  130. }
  131. static av_cold int adpcm_encode_close(AVCodecContext *avctx)
  132. {
  133. ADPCMEncodeContext *s = avctx->priv_data;
  134. av_freep(&avctx->coded_frame);
  135. av_freep(&s->paths);
  136. av_freep(&s->node_buf);
  137. av_freep(&s->nodep_buf);
  138. av_freep(&s->trellis_hash);
  139. return 0;
  140. }
  141. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  142. {
  143. int delta = sample - c->prev_sample;
  144. int nibble = FFMIN(7, abs(delta)*4/ff_adpcm_step_table[c->step_index]) + (delta<0)*8;
  145. c->prev_sample += ((ff_adpcm_step_table[c->step_index] * ff_adpcm_yamaha_difflookup[nibble]) / 8);
  146. c->prev_sample = av_clip_int16(c->prev_sample);
  147. c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
  148. return nibble;
  149. }
  150. static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
  151. {
  152. int predictor, nibble, bias;
  153. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  154. nibble= sample - predictor;
  155. if(nibble>=0) bias= c->idelta/2;
  156. else bias=-c->idelta/2;
  157. nibble= (nibble + bias) / c->idelta;
  158. nibble= av_clip(nibble, -8, 7)&0x0F;
  159. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  160. c->sample2 = c->sample1;
  161. c->sample1 = av_clip_int16(predictor);
  162. c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
  163. if (c->idelta < 16) c->idelta = 16;
  164. return nibble;
  165. }
  166. static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
  167. {
  168. int nibble, delta;
  169. if(!c->step) {
  170. c->predictor = 0;
  171. c->step = 127;
  172. }
  173. delta = sample - c->predictor;
  174. nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
  175. c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
  176. c->predictor = av_clip_int16(c->predictor);
  177. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  178. c->step = av_clip(c->step, 127, 24567);
  179. return nibble;
  180. }
  181. static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
  182. uint8_t *dst, ADPCMChannelStatus *c, int n)
  183. {
  184. //FIXME 6% faster if frontier is a compile-time constant
  185. ADPCMEncodeContext *s = avctx->priv_data;
  186. const int frontier = 1 << avctx->trellis;
  187. const int stride = avctx->channels;
  188. const int version = avctx->codec->id;
  189. TrellisPath *paths = s->paths, *p;
  190. TrellisNode *node_buf = s->node_buf;
  191. TrellisNode **nodep_buf = s->nodep_buf;
  192. TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
  193. TrellisNode **nodes_next = nodep_buf + frontier;
  194. int pathn = 0, froze = -1, i, j, k, generation = 0;
  195. uint8_t *hash = s->trellis_hash;
  196. memset(hash, 0xff, 65536 * sizeof(*hash));
  197. memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
  198. nodes[0] = node_buf + frontier;
  199. nodes[0]->ssd = 0;
  200. nodes[0]->path = 0;
  201. nodes[0]->step = c->step_index;
  202. nodes[0]->sample1 = c->sample1;
  203. nodes[0]->sample2 = c->sample2;
  204. if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_IMA_QT) || (version == CODEC_ID_ADPCM_SWF))
  205. nodes[0]->sample1 = c->prev_sample;
  206. if(version == CODEC_ID_ADPCM_MS)
  207. nodes[0]->step = c->idelta;
  208. if(version == CODEC_ID_ADPCM_YAMAHA) {
  209. if(c->step == 0) {
  210. nodes[0]->step = 127;
  211. nodes[0]->sample1 = 0;
  212. } else {
  213. nodes[0]->step = c->step;
  214. nodes[0]->sample1 = c->predictor;
  215. }
  216. }
  217. for(i=0; i<n; i++) {
  218. TrellisNode *t = node_buf + frontier*(i&1);
  219. TrellisNode **u;
  220. int sample = samples[i*stride];
  221. int heap_pos = 0;
  222. memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
  223. for(j=0; j<frontier && nodes[j]; j++) {
  224. // higher j have higher ssd already, so they're likely to yield a suboptimal next sample too
  225. const int range = (j < frontier/2) ? 1 : 0;
  226. const int step = nodes[j]->step;
  227. int nidx;
  228. if(version == CODEC_ID_ADPCM_MS) {
  229. const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 64;
  230. const int div = (sample - predictor) / step;
  231. const int nmin = av_clip(div-range, -8, 6);
  232. const int nmax = av_clip(div+range, -7, 7);
  233. for(nidx=nmin; nidx<=nmax; nidx++) {
  234. const int nibble = nidx & 0xf;
  235. int dec_sample = predictor + nidx * step;
  236. #define STORE_NODE(NAME, STEP_INDEX)\
  237. int d;\
  238. uint32_t ssd;\
  239. int pos;\
  240. TrellisNode *u;\
  241. uint8_t *h;\
  242. dec_sample = av_clip_int16(dec_sample);\
  243. d = sample - dec_sample;\
  244. ssd = nodes[j]->ssd + d*d;\
  245. /* Check for wraparound, skip such samples completely. \
  246. * Note, changing ssd to a 64 bit variable would be \
  247. * simpler, avoiding this check, but it's slower on \
  248. * x86 32 bit at the moment. */\
  249. if (ssd < nodes[j]->ssd)\
  250. goto next_##NAME;\
  251. /* Collapse any two states with the same previous sample value. \
  252. * One could also distinguish states by step and by 2nd to last
  253. * sample, but the effects of that are negligible.
  254. * Since nodes in the previous generation are iterated
  255. * through a heap, they're roughly ordered from better to
  256. * worse, but not strictly ordered. Therefore, an earlier
  257. * node with the same sample value is better in most cases
  258. * (and thus the current is skipped), but not strictly
  259. * in all cases. Only skipping samples where ssd >=
  260. * ssd of the earlier node with the same sample gives
  261. * slightly worse quality, though, for some reason. */ \
  262. h = &hash[(uint16_t) dec_sample];\
  263. if (*h == generation)\
  264. goto next_##NAME;\
  265. if (heap_pos < frontier) {\
  266. pos = heap_pos++;\
  267. } else {\
  268. /* Try to replace one of the leaf nodes with the new \
  269. * one, but try a different slot each time. */\
  270. pos = (frontier >> 1) + (heap_pos & ((frontier >> 1) - 1));\
  271. if (ssd > nodes_next[pos]->ssd)\
  272. goto next_##NAME;\
  273. heap_pos++;\
  274. }\
  275. *h = generation;\
  276. u = nodes_next[pos];\
  277. if(!u) {\
  278. assert(pathn < FREEZE_INTERVAL<<avctx->trellis);\
  279. u = t++;\
  280. nodes_next[pos] = u;\
  281. u->path = pathn++;\
  282. }\
  283. u->ssd = ssd;\
  284. u->step = STEP_INDEX;\
  285. u->sample2 = nodes[j]->sample1;\
  286. u->sample1 = dec_sample;\
  287. paths[u->path].nibble = nibble;\
  288. paths[u->path].prev = nodes[j]->path;\
  289. /* Sift the newly inserted node up in the heap to \
  290. * restore the heap property. */\
  291. while (pos > 0) {\
  292. int parent = (pos - 1) >> 1;\
  293. if (nodes_next[parent]->ssd <= ssd)\
  294. break;\
  295. FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
  296. pos = parent;\
  297. }\
  298. next_##NAME:;
  299. STORE_NODE(ms, FFMAX(16, (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
  300. }
  301. } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_IMA_QT)|| (version == CODEC_ID_ADPCM_SWF)) {
  302. #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
  303. const int predictor = nodes[j]->sample1;\
  304. const int div = (sample - predictor) * 4 / STEP_TABLE;\
  305. int nmin = av_clip(div-range, -7, 6);\
  306. int nmax = av_clip(div+range, -6, 7);\
  307. if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
  308. if(nmax<0) nmax--;\
  309. for(nidx=nmin; nidx<=nmax; nidx++) {\
  310. const int nibble = nidx<0 ? 7-nidx : nidx;\
  311. int dec_sample = predictor + (STEP_TABLE * ff_adpcm_yamaha_difflookup[nibble]) / 8;\
  312. STORE_NODE(NAME, STEP_INDEX);\
  313. }
  314. LOOP_NODES(ima, ff_adpcm_step_table[step], av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
  315. } else { //CODEC_ID_ADPCM_YAMAHA
  316. LOOP_NODES(yamaha, step, av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8, 127, 24567));
  317. #undef LOOP_NODES
  318. #undef STORE_NODE
  319. }
  320. }
  321. u = nodes;
  322. nodes = nodes_next;
  323. nodes_next = u;
  324. generation++;
  325. if (generation == 255) {
  326. memset(hash, 0xff, 65536 * sizeof(*hash));
  327. generation = 0;
  328. }
  329. // prevent overflow
  330. if(nodes[0]->ssd > (1<<28)) {
  331. for(j=1; j<frontier && nodes[j]; j++)
  332. nodes[j]->ssd -= nodes[0]->ssd;
  333. nodes[0]->ssd = 0;
  334. }
  335. // merge old paths to save memory
  336. if(i == froze + FREEZE_INTERVAL) {
  337. p = &paths[nodes[0]->path];
  338. for(k=i; k>froze; k--) {
  339. dst[k] = p->nibble;
  340. p = &paths[p->prev];
  341. }
  342. froze = i;
  343. pathn = 0;
  344. // other nodes might use paths that don't coincide with the frozen one.
  345. // checking which nodes do so is too slow, so just kill them all.
  346. // this also slightly improves quality, but I don't know why.
  347. memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
  348. }
  349. }
  350. p = &paths[nodes[0]->path];
  351. for(i=n-1; i>froze; i--) {
  352. dst[i] = p->nibble;
  353. p = &paths[p->prev];
  354. }
  355. c->predictor = nodes[0]->sample1;
  356. c->sample1 = nodes[0]->sample1;
  357. c->sample2 = nodes[0]->sample2;
  358. c->step_index = nodes[0]->step;
  359. c->step = nodes[0]->step;
  360. c->idelta = nodes[0]->step;
  361. }
  362. static int adpcm_encode_frame(AVCodecContext *avctx,
  363. unsigned char *frame, int buf_size, void *data)
  364. {
  365. int n, i, st;
  366. short *samples;
  367. unsigned char *dst;
  368. ADPCMEncodeContext *c = avctx->priv_data;
  369. uint8_t *buf;
  370. dst = frame;
  371. samples = (short *)data;
  372. st= avctx->channels == 2;
  373. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  374. switch(avctx->codec->id) {
  375. case CODEC_ID_ADPCM_IMA_WAV:
  376. n = avctx->frame_size / 8;
  377. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  378. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  379. bytestream_put_le16(&dst, c->status[0].prev_sample);
  380. *dst++ = (unsigned char)c->status[0].step_index;
  381. *dst++ = 0; /* unknown */
  382. samples++;
  383. if (avctx->channels == 2) {
  384. c->status[1].prev_sample = (signed short)samples[0];
  385. /* c->status[1].step_index = 0; */
  386. bytestream_put_le16(&dst, c->status[1].prev_sample);
  387. *dst++ = (unsigned char)c->status[1].step_index;
  388. *dst++ = 0;
  389. samples++;
  390. }
  391. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  392. if(avctx->trellis > 0) {
  393. FF_ALLOC_OR_GOTO(avctx, buf, 2*n*8, error);
  394. adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n*8);
  395. if(avctx->channels == 2)
  396. adpcm_compress_trellis(avctx, samples+1, buf + n*8, &c->status[1], n*8);
  397. for(i=0; i<n; i++) {
  398. *dst++ = buf[8*i+0] | (buf[8*i+1] << 4);
  399. *dst++ = buf[8*i+2] | (buf[8*i+3] << 4);
  400. *dst++ = buf[8*i+4] | (buf[8*i+5] << 4);
  401. *dst++ = buf[8*i+6] | (buf[8*i+7] << 4);
  402. if (avctx->channels == 2) {
  403. uint8_t *buf1 = buf + n*8;
  404. *dst++ = buf1[8*i+0] | (buf1[8*i+1] << 4);
  405. *dst++ = buf1[8*i+2] | (buf1[8*i+3] << 4);
  406. *dst++ = buf1[8*i+4] | (buf1[8*i+5] << 4);
  407. *dst++ = buf1[8*i+6] | (buf1[8*i+7] << 4);
  408. }
  409. }
  410. av_free(buf);
  411. } else
  412. for (; n>0; n--) {
  413. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
  414. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
  415. dst++;
  416. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
  417. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
  418. dst++;
  419. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
  420. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
  421. dst++;
  422. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
  423. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
  424. dst++;
  425. /* right channel */
  426. if (avctx->channels == 2) {
  427. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  428. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  429. dst++;
  430. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  431. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  432. dst++;
  433. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  434. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  435. dst++;
  436. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  437. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  438. dst++;
  439. }
  440. samples += 8 * avctx->channels;
  441. }
  442. break;
  443. case CODEC_ID_ADPCM_IMA_QT:
  444. {
  445. int ch, i;
  446. PutBitContext pb;
  447. init_put_bits(&pb, dst, buf_size*8);
  448. for(ch=0; ch<avctx->channels; ch++){
  449. put_bits(&pb, 9, (c->status[ch].prev_sample + 0x10000) >> 7);
  450. put_bits(&pb, 7, c->status[ch].step_index);
  451. if(avctx->trellis > 0) {
  452. uint8_t buf[64];
  453. adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
  454. for(i=0; i<64; i++)
  455. put_bits(&pb, 4, buf[i^1]);
  456. c->status[ch].prev_sample = c->status[ch].predictor & ~0x7F;
  457. } else {
  458. for (i=0; i<64; i+=2){
  459. int t1, t2;
  460. t1 = adpcm_ima_compress_sample(&c->status[ch], samples[avctx->channels*(i+0)+ch]);
  461. t2 = adpcm_ima_compress_sample(&c->status[ch], samples[avctx->channels*(i+1)+ch]);
  462. put_bits(&pb, 4, t2);
  463. put_bits(&pb, 4, t1);
  464. }
  465. c->status[ch].prev_sample &= ~0x7F;
  466. }
  467. }
  468. flush_put_bits(&pb);
  469. dst += put_bits_count(&pb)>>3;
  470. break;
  471. }
  472. case CODEC_ID_ADPCM_SWF:
  473. {
  474. int i;
  475. PutBitContext pb;
  476. init_put_bits(&pb, dst, buf_size*8);
  477. n = avctx->frame_size-1;
  478. //Store AdpcmCodeSize
  479. put_bits(&pb, 2, 2); //Set 4bits flash adpcm format
  480. //Init the encoder state
  481. for(i=0; i<avctx->channels; i++){
  482. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
  483. put_sbits(&pb, 16, samples[i]);
  484. put_bits(&pb, 6, c->status[i].step_index);
  485. c->status[i].prev_sample = (signed short)samples[i];
  486. }
  487. if(avctx->trellis > 0) {
  488. FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
  489. adpcm_compress_trellis(avctx, samples+2, buf, &c->status[0], n);
  490. if (avctx->channels == 2)
  491. adpcm_compress_trellis(avctx, samples+3, buf+n, &c->status[1], n);
  492. for(i=0; i<n; i++) {
  493. put_bits(&pb, 4, buf[i]);
  494. if (avctx->channels == 2)
  495. put_bits(&pb, 4, buf[n+i]);
  496. }
  497. av_free(buf);
  498. } else {
  499. for (i=1; i<avctx->frame_size; i++) {
  500. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
  501. if (avctx->channels == 2)
  502. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
  503. }
  504. }
  505. flush_put_bits(&pb);
  506. dst += put_bits_count(&pb)>>3;
  507. break;
  508. }
  509. case CODEC_ID_ADPCM_MS:
  510. for(i=0; i<avctx->channels; i++){
  511. int predictor=0;
  512. *dst++ = predictor;
  513. c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
  514. c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
  515. }
  516. for(i=0; i<avctx->channels; i++){
  517. if (c->status[i].idelta < 16)
  518. c->status[i].idelta = 16;
  519. bytestream_put_le16(&dst, c->status[i].idelta);
  520. }
  521. for(i=0; i<avctx->channels; i++){
  522. c->status[i].sample2= *samples++;
  523. }
  524. for(i=0; i<avctx->channels; i++){
  525. c->status[i].sample1= *samples++;
  526. bytestream_put_le16(&dst, c->status[i].sample1);
  527. }
  528. for(i=0; i<avctx->channels; i++)
  529. bytestream_put_le16(&dst, c->status[i].sample2);
  530. if(avctx->trellis > 0) {
  531. int n = avctx->block_align - 7*avctx->channels;
  532. FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
  533. if(avctx->channels == 1) {
  534. adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
  535. for(i=0; i<n; i+=2)
  536. *dst++ = (buf[i] << 4) | buf[i+1];
  537. } else {
  538. adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
  539. adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
  540. for(i=0; i<n; i++)
  541. *dst++ = (buf[i] << 4) | buf[n+i];
  542. }
  543. av_free(buf);
  544. } else
  545. for(i=7*avctx->channels; i<avctx->block_align; i++) {
  546. int nibble;
  547. nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
  548. nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
  549. *dst++ = nibble;
  550. }
  551. break;
  552. case CODEC_ID_ADPCM_YAMAHA:
  553. n = avctx->frame_size / 2;
  554. if(avctx->trellis > 0) {
  555. FF_ALLOC_OR_GOTO(avctx, buf, 2*n*2, error);
  556. n *= 2;
  557. if(avctx->channels == 1) {
  558. adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
  559. for(i=0; i<n; i+=2)
  560. *dst++ = buf[i] | (buf[i+1] << 4);
  561. } else {
  562. adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
  563. adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
  564. for(i=0; i<n; i++)
  565. *dst++ = buf[i] | (buf[n+i] << 4);
  566. }
  567. av_free(buf);
  568. } else
  569. for (n *= avctx->channels; n>0; n--) {
  570. int nibble;
  571. nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
  572. nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
  573. *dst++ = nibble;
  574. }
  575. break;
  576. default:
  577. error:
  578. return -1;
  579. }
  580. return dst - frame;
  581. }
  582. #define ADPCM_ENCODER(id,name,long_name_) \
  583. AVCodec ff_ ## name ## _encoder = { \
  584. #name, \
  585. AVMEDIA_TYPE_AUDIO, \
  586. id, \
  587. sizeof(ADPCMEncodeContext), \
  588. adpcm_encode_init, \
  589. adpcm_encode_frame, \
  590. adpcm_encode_close, \
  591. NULL, \
  592. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE}, \
  593. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  594. }
  595. ADPCM_ENCODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
  596. ADPCM_ENCODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
  597. ADPCM_ENCODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
  598. ADPCM_ENCODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
  599. ADPCM_ENCODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");