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