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.

727 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 FFmpeg.
  9. *
  10. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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_close(AVCodecContext *avctx);
  55. static av_cold int adpcm_encode_init(AVCodecContext *avctx)
  56. {
  57. ADPCMEncodeContext *s = avctx->priv_data;
  58. uint8_t *extradata;
  59. int i;
  60. int ret = AVERROR(ENOMEM);
  61. if (avctx->channels > 2) {
  62. av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
  63. return AVERROR(EINVAL);
  64. }
  65. if (avctx->trellis && (unsigned)avctx->trellis > 16U) {
  66. av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
  67. return AVERROR(EINVAL);
  68. }
  69. if (avctx->trellis) {
  70. int frontier = 1 << avctx->trellis;
  71. int max_paths = frontier * FREEZE_INTERVAL;
  72. FF_ALLOC_OR_GOTO(avctx, s->paths,
  73. max_paths * sizeof(*s->paths), error);
  74. FF_ALLOC_OR_GOTO(avctx, s->node_buf,
  75. 2 * frontier * sizeof(*s->node_buf), error);
  76. FF_ALLOC_OR_GOTO(avctx, s->nodep_buf,
  77. 2 * frontier * sizeof(*s->nodep_buf), error);
  78. FF_ALLOC_OR_GOTO(avctx, s->trellis_hash,
  79. 65536 * sizeof(*s->trellis_hash), error);
  80. }
  81. avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
  82. switch (avctx->codec->id) {
  83. case AV_CODEC_ID_ADPCM_IMA_WAV:
  84. /* each 16 bits sample gives one nibble
  85. and we have 4 bytes per channel overhead */
  86. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 /
  87. (4 * avctx->channels) + 1;
  88. /* seems frame_size isn't taken into account...
  89. have to buffer the samples :-( */
  90. avctx->block_align = BLKSIZE;
  91. avctx->bits_per_coded_sample = 4;
  92. break;
  93. case AV_CODEC_ID_ADPCM_IMA_QT:
  94. avctx->frame_size = 64;
  95. avctx->block_align = 34 * avctx->channels;
  96. break;
  97. case AV_CODEC_ID_ADPCM_MS:
  98. /* each 16 bits sample gives one nibble
  99. and we have 7 bytes per channel overhead */
  100. avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2;
  101. avctx->bits_per_coded_sample = 4;
  102. avctx->block_align = BLKSIZE;
  103. if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
  104. goto error;
  105. avctx->extradata_size = 32;
  106. extradata = avctx->extradata;
  107. bytestream_put_le16(&extradata, avctx->frame_size);
  108. bytestream_put_le16(&extradata, 7); /* wNumCoef */
  109. for (i = 0; i < 7; i++) {
  110. bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
  111. bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
  112. }
  113. break;
  114. case AV_CODEC_ID_ADPCM_YAMAHA:
  115. avctx->frame_size = BLKSIZE * 2 / avctx->channels;
  116. avctx->block_align = BLKSIZE;
  117. break;
  118. case AV_CODEC_ID_ADPCM_SWF:
  119. if (avctx->sample_rate != 11025 &&
  120. avctx->sample_rate != 22050 &&
  121. avctx->sample_rate != 44100) {
  122. av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
  123. "22050 or 44100\n");
  124. ret = AVERROR(EINVAL);
  125. goto error;
  126. }
  127. avctx->frame_size = 512 * (avctx->sample_rate / 11025);
  128. break;
  129. default:
  130. ret = AVERROR(EINVAL);
  131. goto error;
  132. }
  133. return 0;
  134. error:
  135. return ret;
  136. }
  137. static av_cold int adpcm_encode_close(AVCodecContext *avctx)
  138. {
  139. ADPCMEncodeContext *s = avctx->priv_data;
  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 uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
  147. int16_t 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 uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
  159. int16_t 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 uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
  190. int16_t 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_intp2(nibble, 3) & 0x0F;
  202. predictor += ((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[nibble] * c->idelta) >> 8;
  206. if (c->idelta < 16)
  207. c->idelta = 16;
  208. return nibble;
  209. }
  210. static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
  211. int16_t 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, 24576);
  224. return nibble;
  225. }
  226. static void adpcm_compress_trellis(AVCodecContext *avctx,
  227. const int16_t *samples, uint8_t *dst,
  228. ADPCMChannelStatus *c, int n, int stride)
  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 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 == AV_CODEC_ID_ADPCM_IMA_WAV ||
  250. version == AV_CODEC_ID_ADPCM_IMA_QT ||
  251. version == AV_CODEC_ID_ADPCM_SWF)
  252. nodes[0]->sample1 = c->prev_sample;
  253. if (version == AV_CODEC_ID_ADPCM_MS)
  254. nodes[0]->step = c->idelta;
  255. if (version == AV_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 == AV_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*(unsigned)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. av_assert1(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 == AV_CODEC_ID_ADPCM_IMA_WAV ||
  353. version == AV_CODEC_ID_ADPCM_IMA_QT ||
  354. version == AV_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 { //AV_CODEC_ID_ADPCM_YAMAHA
  374. LOOP_NODES(yamaha, step,
  375. av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
  376. 127, 24576));
  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, AVPacket *avpkt,
  423. const AVFrame *frame, int *got_packet_ptr)
  424. {
  425. int n, i, ch, st, pkt_size, ret;
  426. const int16_t *samples;
  427. int16_t **samples_p;
  428. uint8_t *dst;
  429. ADPCMEncodeContext *c = avctx->priv_data;
  430. uint8_t *buf;
  431. samples = (const int16_t *)frame->data[0];
  432. samples_p = (int16_t **)frame->extended_data;
  433. st = avctx->channels == 2;
  434. if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
  435. pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
  436. else
  437. pkt_size = avctx->block_align;
  438. if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
  439. return ret;
  440. dst = avpkt->data;
  441. switch(avctx->codec->id) {
  442. case AV_CODEC_ID_ADPCM_IMA_WAV:
  443. {
  444. int blocks, j;
  445. blocks = (frame->nb_samples - 1) / 8;
  446. for (ch = 0; ch < avctx->channels; ch++) {
  447. ADPCMChannelStatus *status = &c->status[ch];
  448. status->prev_sample = samples_p[ch][0];
  449. /* status->step_index = 0;
  450. XXX: not sure how to init the state machine */
  451. bytestream_put_le16(&dst, status->prev_sample);
  452. *dst++ = status->step_index;
  453. *dst++ = 0; /* unknown */
  454. }
  455. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
  456. if (avctx->trellis > 0) {
  457. FF_ALLOC_ARRAY_OR_GOTO(avctx, buf, avctx->channels, blocks * 8, error);
  458. for (ch = 0; ch < avctx->channels; ch++) {
  459. adpcm_compress_trellis(avctx, &samples_p[ch][1],
  460. buf + ch * blocks * 8, &c->status[ch],
  461. blocks * 8, 1);
  462. }
  463. for (i = 0; i < blocks; i++) {
  464. for (ch = 0; ch < avctx->channels; ch++) {
  465. uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
  466. for (j = 0; j < 8; j += 2)
  467. *dst++ = buf1[j] | (buf1[j + 1] << 4);
  468. }
  469. }
  470. av_free(buf);
  471. } else {
  472. for (i = 0; i < blocks; i++) {
  473. for (ch = 0; ch < avctx->channels; ch++) {
  474. ADPCMChannelStatus *status = &c->status[ch];
  475. const int16_t *smp = &samples_p[ch][1 + i * 8];
  476. for (j = 0; j < 8; j += 2) {
  477. uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
  478. v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
  479. *dst++ = v;
  480. }
  481. }
  482. }
  483. }
  484. break;
  485. }
  486. case AV_CODEC_ID_ADPCM_IMA_QT:
  487. {
  488. PutBitContext pb;
  489. init_put_bits(&pb, dst, pkt_size);
  490. for (ch = 0; ch < avctx->channels; ch++) {
  491. ADPCMChannelStatus *status = &c->status[ch];
  492. put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
  493. put_bits(&pb, 7, status->step_index);
  494. if (avctx->trellis > 0) {
  495. uint8_t buf[64];
  496. adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
  497. 64, 1);
  498. for (i = 0; i < 64; i++)
  499. put_bits(&pb, 4, buf[i ^ 1]);
  500. status->prev_sample = status->predictor;
  501. } else {
  502. for (i = 0; i < 64; i += 2) {
  503. int t1, t2;
  504. t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
  505. t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
  506. put_bits(&pb, 4, t2);
  507. put_bits(&pb, 4, t1);
  508. }
  509. }
  510. }
  511. flush_put_bits(&pb);
  512. break;
  513. }
  514. case AV_CODEC_ID_ADPCM_SWF:
  515. {
  516. PutBitContext pb;
  517. init_put_bits(&pb, dst, pkt_size);
  518. n = frame->nb_samples - 1;
  519. // store AdpcmCodeSize
  520. put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
  521. // init the encoder state
  522. for (i = 0; i < avctx->channels; i++) {
  523. // clip step so it fits 6 bits
  524. c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
  525. put_sbits(&pb, 16, samples[i]);
  526. put_bits(&pb, 6, c->status[i].step_index);
  527. c->status[i].prev_sample = samples[i];
  528. }
  529. if (avctx->trellis > 0) {
  530. FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
  531. adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
  532. &c->status[0], n, avctx->channels);
  533. if (avctx->channels == 2)
  534. adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
  535. buf + n, &c->status[1], n,
  536. avctx->channels);
  537. for (i = 0; i < n; i++) {
  538. put_bits(&pb, 4, buf[i]);
  539. if (avctx->channels == 2)
  540. put_bits(&pb, 4, buf[n + i]);
  541. }
  542. av_free(buf);
  543. } else {
  544. for (i = 1; i < frame->nb_samples; i++) {
  545. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
  546. samples[avctx->channels * i]));
  547. if (avctx->channels == 2)
  548. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
  549. samples[2 * i + 1]));
  550. }
  551. }
  552. flush_put_bits(&pb);
  553. break;
  554. }
  555. case AV_CODEC_ID_ADPCM_MS:
  556. for (i = 0; i < avctx->channels; i++) {
  557. int predictor = 0;
  558. *dst++ = predictor;
  559. c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
  560. c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
  561. }
  562. for (i = 0; i < avctx->channels; i++) {
  563. if (c->status[i].idelta < 16)
  564. c->status[i].idelta = 16;
  565. bytestream_put_le16(&dst, c->status[i].idelta);
  566. }
  567. for (i = 0; i < avctx->channels; i++)
  568. c->status[i].sample2= *samples++;
  569. for (i = 0; i < avctx->channels; i++) {
  570. c->status[i].sample1 = *samples++;
  571. bytestream_put_le16(&dst, c->status[i].sample1);
  572. }
  573. for (i = 0; i < avctx->channels; i++)
  574. bytestream_put_le16(&dst, c->status[i].sample2);
  575. if (avctx->trellis > 0) {
  576. n = avctx->block_align - 7 * avctx->channels;
  577. FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
  578. if (avctx->channels == 1) {
  579. adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
  580. avctx->channels);
  581. for (i = 0; i < n; i += 2)
  582. *dst++ = (buf[i] << 4) | buf[i + 1];
  583. } else {
  584. adpcm_compress_trellis(avctx, samples, buf,
  585. &c->status[0], n, avctx->channels);
  586. adpcm_compress_trellis(avctx, samples + 1, buf + n,
  587. &c->status[1], n, avctx->channels);
  588. for (i = 0; i < n; i++)
  589. *dst++ = (buf[i] << 4) | buf[n + i];
  590. }
  591. av_free(buf);
  592. } else {
  593. for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
  594. int nibble;
  595. nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
  596. nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
  597. *dst++ = nibble;
  598. }
  599. }
  600. break;
  601. case AV_CODEC_ID_ADPCM_YAMAHA:
  602. n = frame->nb_samples / 2;
  603. if (avctx->trellis > 0) {
  604. FF_ALLOC_OR_GOTO(avctx, buf, 2 * n * 2, error);
  605. n *= 2;
  606. if (avctx->channels == 1) {
  607. adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
  608. avctx->channels);
  609. for (i = 0; i < n; i += 2)
  610. *dst++ = buf[i] | (buf[i + 1] << 4);
  611. } else {
  612. adpcm_compress_trellis(avctx, samples, buf,
  613. &c->status[0], n, avctx->channels);
  614. adpcm_compress_trellis(avctx, samples + 1, buf + n,
  615. &c->status[1], n, avctx->channels);
  616. for (i = 0; i < n; i++)
  617. *dst++ = buf[i] | (buf[n + i] << 4);
  618. }
  619. av_free(buf);
  620. } else
  621. for (n *= avctx->channels; n > 0; n--) {
  622. int nibble;
  623. nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
  624. nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
  625. *dst++ = nibble;
  626. }
  627. break;
  628. default:
  629. return AVERROR(EINVAL);
  630. }
  631. avpkt->size = pkt_size;
  632. *got_packet_ptr = 1;
  633. return 0;
  634. error:
  635. return AVERROR(ENOMEM);
  636. }
  637. static const enum AVSampleFormat sample_fmts[] = {
  638. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
  639. };
  640. static const enum AVSampleFormat sample_fmts_p[] = {
  641. AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
  642. };
  643. #define ADPCM_ENCODER(id_, name_, sample_fmts_, long_name_) \
  644. AVCodec ff_ ## name_ ## _encoder = { \
  645. .name = #name_, \
  646. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  647. .type = AVMEDIA_TYPE_AUDIO, \
  648. .id = id_, \
  649. .priv_data_size = sizeof(ADPCMEncodeContext), \
  650. .init = adpcm_encode_init, \
  651. .encode2 = adpcm_encode_frame, \
  652. .close = adpcm_encode_close, \
  653. .sample_fmts = sample_fmts_, \
  654. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
  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");