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.

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