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.

717 lines
27KB

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