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.

726 lines
27KB

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