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.

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