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.

735 lines
27KB

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