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.

692 lines
26KB

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