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.

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