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. adpcm_encode_close(avctx);
  136. return ret;
  137. }
  138. static av_cold int adpcm_encode_close(AVCodecContext *avctx)
  139. {
  140. ADPCMEncodeContext *s = avctx->priv_data;
  141. av_freep(&s->paths);
  142. av_freep(&s->node_buf);
  143. av_freep(&s->nodep_buf);
  144. av_freep(&s->trellis_hash);
  145. return 0;
  146. }
  147. static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
  148. int16_t sample)
  149. {
  150. int delta = sample - c->prev_sample;
  151. int nibble = FFMIN(7, abs(delta) * 4 /
  152. ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
  153. c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
  154. ff_adpcm_yamaha_difflookup[nibble]) / 8);
  155. c->prev_sample = av_clip_int16(c->prev_sample);
  156. c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
  157. return nibble;
  158. }
  159. static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
  160. int16_t sample)
  161. {
  162. int delta = sample - c->prev_sample;
  163. int diff, step = ff_adpcm_step_table[c->step_index];
  164. int nibble = 8*(delta < 0);
  165. delta= abs(delta);
  166. diff = delta + (step >> 3);
  167. if (delta >= step) {
  168. nibble |= 4;
  169. delta -= step;
  170. }
  171. step >>= 1;
  172. if (delta >= step) {
  173. nibble |= 2;
  174. delta -= step;
  175. }
  176. step >>= 1;
  177. if (delta >= step) {
  178. nibble |= 1;
  179. delta -= step;
  180. }
  181. diff -= delta;
  182. if (nibble & 8)
  183. c->prev_sample -= diff;
  184. else
  185. c->prev_sample += diff;
  186. c->prev_sample = av_clip_int16(c->prev_sample);
  187. c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
  188. return nibble;
  189. }
  190. static inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
  191. int16_t sample)
  192. {
  193. int predictor, nibble, bias;
  194. predictor = (((c->sample1) * (c->coeff1)) +
  195. (( c->sample2) * (c->coeff2))) / 64;
  196. nibble = sample - predictor;
  197. if (nibble >= 0)
  198. bias = c->idelta / 2;
  199. else
  200. bias = -c->idelta / 2;
  201. nibble = (nibble + bias) / c->idelta;
  202. nibble = av_clip_intp2(nibble, 3) & 0x0F;
  203. predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
  204. c->sample2 = c->sample1;
  205. c->sample1 = av_clip_int16(predictor);
  206. c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
  207. if (c->idelta < 16)
  208. c->idelta = 16;
  209. return nibble;
  210. }
  211. static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
  212. int16_t sample)
  213. {
  214. int nibble, delta;
  215. if (!c->step) {
  216. c->predictor = 0;
  217. c->step = 127;
  218. }
  219. delta = sample - c->predictor;
  220. nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
  221. c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
  222. c->predictor = av_clip_int16(c->predictor);
  223. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  224. c->step = av_clip(c->step, 127, 24567);
  225. return nibble;
  226. }
  227. static void adpcm_compress_trellis(AVCodecContext *avctx,
  228. const int16_t *samples, uint8_t *dst,
  229. ADPCMChannelStatus *c, int n, int stride)
  230. {
  231. //FIXME 6% faster if frontier is a compile-time constant
  232. ADPCMEncodeContext *s = avctx->priv_data;
  233. const int frontier = 1 << avctx->trellis;
  234. const int version = avctx->codec->id;
  235. TrellisPath *paths = s->paths, *p;
  236. TrellisNode *node_buf = s->node_buf;
  237. TrellisNode **nodep_buf = s->nodep_buf;
  238. TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
  239. TrellisNode **nodes_next = nodep_buf + frontier;
  240. int pathn = 0, froze = -1, i, j, k, generation = 0;
  241. uint8_t *hash = s->trellis_hash;
  242. memset(hash, 0xff, 65536 * sizeof(*hash));
  243. memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
  244. nodes[0] = node_buf + frontier;
  245. nodes[0]->ssd = 0;
  246. nodes[0]->path = 0;
  247. nodes[0]->step = c->step_index;
  248. nodes[0]->sample1 = c->sample1;
  249. nodes[0]->sample2 = c->sample2;
  250. if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
  251. version == AV_CODEC_ID_ADPCM_IMA_QT ||
  252. version == AV_CODEC_ID_ADPCM_SWF)
  253. nodes[0]->sample1 = c->prev_sample;
  254. if (version == AV_CODEC_ID_ADPCM_MS)
  255. nodes[0]->step = c->idelta;
  256. if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
  257. if (c->step == 0) {
  258. nodes[0]->step = 127;
  259. nodes[0]->sample1 = 0;
  260. } else {
  261. nodes[0]->step = c->step;
  262. nodes[0]->sample1 = c->predictor;
  263. }
  264. }
  265. for (i = 0; i < n; i++) {
  266. TrellisNode *t = node_buf + frontier*(i&1);
  267. TrellisNode **u;
  268. int sample = samples[i * stride];
  269. int heap_pos = 0;
  270. memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
  271. for (j = 0; j < frontier && nodes[j]; j++) {
  272. // higher j have higher ssd already, so they're likely
  273. // to yield a suboptimal next sample too
  274. const int range = (j < frontier / 2) ? 1 : 0;
  275. const int step = nodes[j]->step;
  276. int nidx;
  277. if (version == AV_CODEC_ID_ADPCM_MS) {
  278. const int predictor = ((nodes[j]->sample1 * c->coeff1) +
  279. (nodes[j]->sample2 * c->coeff2)) / 64;
  280. const int div = (sample - predictor) / step;
  281. const int nmin = av_clip(div-range, -8, 6);
  282. const int nmax = av_clip(div+range, -7, 7);
  283. for (nidx = nmin; nidx <= nmax; nidx++) {
  284. const int nibble = nidx & 0xf;
  285. int dec_sample = predictor + nidx * step;
  286. #define STORE_NODE(NAME, STEP_INDEX)\
  287. int d;\
  288. uint32_t ssd;\
  289. int pos;\
  290. TrellisNode *u;\
  291. uint8_t *h;\
  292. dec_sample = av_clip_int16(dec_sample);\
  293. d = sample - dec_sample;\
  294. ssd = nodes[j]->ssd + d*(unsigned)d;\
  295. /* Check for wraparound, skip such samples completely. \
  296. * Note, changing ssd to a 64 bit variable would be \
  297. * simpler, avoiding this check, but it's slower on \
  298. * x86 32 bit at the moment. */\
  299. if (ssd < nodes[j]->ssd)\
  300. goto next_##NAME;\
  301. /* Collapse any two states with the same previous sample value. \
  302. * One could also distinguish states by step and by 2nd to last
  303. * sample, but the effects of that are negligible.
  304. * Since nodes in the previous generation are iterated
  305. * through a heap, they're roughly ordered from better to
  306. * worse, but not strictly ordered. Therefore, an earlier
  307. * node with the same sample value is better in most cases
  308. * (and thus the current is skipped), but not strictly
  309. * in all cases. Only skipping samples where ssd >=
  310. * ssd of the earlier node with the same sample gives
  311. * slightly worse quality, though, for some reason. */ \
  312. h = &hash[(uint16_t) dec_sample];\
  313. if (*h == generation)\
  314. goto next_##NAME;\
  315. if (heap_pos < frontier) {\
  316. pos = heap_pos++;\
  317. } else {\
  318. /* Try to replace one of the leaf nodes with the new \
  319. * one, but try a different slot each time. */\
  320. pos = (frontier >> 1) +\
  321. (heap_pos & ((frontier >> 1) - 1));\
  322. if (ssd > nodes_next[pos]->ssd)\
  323. goto next_##NAME;\
  324. heap_pos++;\
  325. }\
  326. *h = generation;\
  327. u = nodes_next[pos];\
  328. if (!u) {\
  329. av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
  330. u = t++;\
  331. nodes_next[pos] = u;\
  332. u->path = pathn++;\
  333. }\
  334. u->ssd = ssd;\
  335. u->step = STEP_INDEX;\
  336. u->sample2 = nodes[j]->sample1;\
  337. u->sample1 = dec_sample;\
  338. paths[u->path].nibble = nibble;\
  339. paths[u->path].prev = nodes[j]->path;\
  340. /* Sift the newly inserted node up in the heap to \
  341. * restore the heap property. */\
  342. while (pos > 0) {\
  343. int parent = (pos - 1) >> 1;\
  344. if (nodes_next[parent]->ssd <= ssd)\
  345. break;\
  346. FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
  347. pos = parent;\
  348. }\
  349. next_##NAME:;
  350. STORE_NODE(ms, FFMAX(16,
  351. (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
  352. }
  353. } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
  354. version == AV_CODEC_ID_ADPCM_IMA_QT ||
  355. version == AV_CODEC_ID_ADPCM_SWF) {
  356. #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
  357. const int predictor = nodes[j]->sample1;\
  358. const int div = (sample - predictor) * 4 / STEP_TABLE;\
  359. int nmin = av_clip(div - range, -7, 6);\
  360. int nmax = av_clip(div + range, -6, 7);\
  361. if (nmin <= 0)\
  362. nmin--; /* distinguish -0 from +0 */\
  363. if (nmax < 0)\
  364. nmax--;\
  365. for (nidx = nmin; nidx <= nmax; nidx++) {\
  366. const int nibble = nidx < 0 ? 7 - nidx : nidx;\
  367. int dec_sample = predictor +\
  368. (STEP_TABLE *\
  369. ff_adpcm_yamaha_difflookup[nibble]) / 8;\
  370. STORE_NODE(NAME, STEP_INDEX);\
  371. }
  372. LOOP_NODES(ima, ff_adpcm_step_table[step],
  373. av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
  374. } else { //AV_CODEC_ID_ADPCM_YAMAHA
  375. LOOP_NODES(yamaha, step,
  376. av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
  377. 127, 24567));
  378. #undef LOOP_NODES
  379. #undef STORE_NODE
  380. }
  381. }
  382. u = nodes;
  383. nodes = nodes_next;
  384. nodes_next = u;
  385. generation++;
  386. if (generation == 255) {
  387. memset(hash, 0xff, 65536 * sizeof(*hash));
  388. generation = 0;
  389. }
  390. // prevent overflow
  391. if (nodes[0]->ssd > (1 << 28)) {
  392. for (j = 1; j < frontier && nodes[j]; j++)
  393. nodes[j]->ssd -= nodes[0]->ssd;
  394. nodes[0]->ssd = 0;
  395. }
  396. // merge old paths to save memory
  397. if (i == froze + FREEZE_INTERVAL) {
  398. p = &paths[nodes[0]->path];
  399. for (k = i; k > froze; k--) {
  400. dst[k] = p->nibble;
  401. p = &paths[p->prev];
  402. }
  403. froze = i;
  404. pathn = 0;
  405. // other nodes might use paths that don't coincide with the frozen one.
  406. // checking which nodes do so is too slow, so just kill them all.
  407. // this also slightly improves quality, but I don't know why.
  408. memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
  409. }
  410. }
  411. p = &paths[nodes[0]->path];
  412. for (i = n - 1; i > froze; i--) {
  413. dst[i] = p->nibble;
  414. p = &paths[p->prev];
  415. }
  416. c->predictor = nodes[0]->sample1;
  417. c->sample1 = nodes[0]->sample1;
  418. c->sample2 = nodes[0]->sample2;
  419. c->step_index = nodes[0]->step;
  420. c->step = nodes[0]->step;
  421. c->idelta = nodes[0]->step;
  422. }
  423. static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  424. const AVFrame *frame, int *got_packet_ptr)
  425. {
  426. int n, i, ch, st, pkt_size, ret;
  427. const int16_t *samples;
  428. int16_t **samples_p;
  429. uint8_t *dst;
  430. ADPCMEncodeContext *c = avctx->priv_data;
  431. uint8_t *buf;
  432. samples = (const int16_t *)frame->data[0];
  433. samples_p = (int16_t **)frame->extended_data;
  434. st = avctx->channels == 2;
  435. if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
  436. pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
  437. else
  438. pkt_size = avctx->block_align;
  439. if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
  440. return ret;
  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_ARRAY_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);
  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);
  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_uintp2(c->status[i].step_index, 6);
  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");