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.

693 lines
27KB

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