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