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.

725 lines
28KB

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