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.

734 lines
27KB

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