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.

728 lines
28KB

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