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.

899 lines
33KB

  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 "libavutil/opt.h"
  25. #include "avcodec.h"
  26. #include "put_bits.h"
  27. #include "bytestream.h"
  28. #include "adpcm.h"
  29. #include "adpcm_data.h"
  30. #include "internal.h"
  31. /**
  32. * @file
  33. * ADPCM encoders
  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. AVClass *class;
  49. int block_size;
  50. ADPCMChannelStatus status[6];
  51. TrellisPath *paths;
  52. TrellisNode *node_buf;
  53. TrellisNode **nodep_buf;
  54. uint8_t *trellis_hash;
  55. } ADPCMEncodeContext;
  56. #define FREEZE_INTERVAL 128
  57. static av_cold int adpcm_encode_init(AVCodecContext *avctx)
  58. {
  59. ADPCMEncodeContext *s = avctx->priv_data;
  60. uint8_t *extradata;
  61. int i;
  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 (s->block_size & (s->block_size - 1)) {
  67. av_log(avctx, AV_LOG_ERROR, "block size must be power of 2\n");
  68. return AVERROR(EINVAL);
  69. }
  70. if (avctx->trellis) {
  71. int frontier, max_paths;
  72. if ((unsigned)avctx->trellis > 16U) {
  73. av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
  74. return AVERROR(EINVAL);
  75. }
  76. if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
  77. avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
  78. avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO) {
  79. /*
  80. * The current trellis implementation doesn't work for extended
  81. * runs of samples without periodic resets. Disallow it.
  82. */
  83. av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
  84. return AVERROR_PATCHWELCOME;
  85. }
  86. frontier = 1 << avctx->trellis;
  87. max_paths = frontier * FREEZE_INTERVAL;
  88. if (!FF_ALLOC_TYPED_ARRAY(s->paths, max_paths) ||
  89. !FF_ALLOC_TYPED_ARRAY(s->node_buf, 2 * frontier) ||
  90. !FF_ALLOC_TYPED_ARRAY(s->nodep_buf, 2 * frontier) ||
  91. !FF_ALLOC_TYPED_ARRAY(s->trellis_hash, 65536))
  92. return AVERROR(ENOMEM);
  93. }
  94. avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
  95. switch (avctx->codec->id) {
  96. case AV_CODEC_ID_ADPCM_IMA_WAV:
  97. /* each 16 bits sample gives one nibble
  98. and we have 4 bytes per channel overhead */
  99. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 /
  100. (4 * avctx->channels) + 1;
  101. /* seems frame_size isn't taken into account...
  102. have to buffer the samples :-( */
  103. avctx->block_align = BLKSIZE;
  104. avctx->bits_per_coded_sample = 4;
  105. break;
  106. case AV_CODEC_ID_ADPCM_IMA_QT:
  107. avctx->frame_size = 64;
  108. avctx->block_align = 34 * avctx->channels;
  109. break;
  110. case AV_CODEC_ID_ADPCM_MS:
  111. /* each 16 bits sample gives one nibble
  112. and we have 7 bytes per channel overhead */
  113. avctx->frame_size = (s->block_size - 7 * avctx->channels) * 2 / avctx->channels + 2;
  114. avctx->bits_per_coded_sample = 4;
  115. avctx->block_align = s->block_size;
  116. if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
  117. return AVERROR(ENOMEM);
  118. avctx->extradata_size = 32;
  119. extradata = avctx->extradata;
  120. bytestream_put_le16(&extradata, avctx->frame_size);
  121. bytestream_put_le16(&extradata, 7); /* wNumCoef */
  122. for (i = 0; i < 7; i++) {
  123. bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
  124. bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
  125. }
  126. break;
  127. case AV_CODEC_ID_ADPCM_YAMAHA:
  128. avctx->frame_size = BLKSIZE * 2 / avctx->channels;
  129. avctx->block_align = BLKSIZE;
  130. break;
  131. case AV_CODEC_ID_ADPCM_SWF:
  132. if (avctx->sample_rate != 11025 &&
  133. avctx->sample_rate != 22050 &&
  134. avctx->sample_rate != 44100) {
  135. av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
  136. "22050 or 44100\n");
  137. return AVERROR(EINVAL);
  138. }
  139. avctx->frame_size = 512 * (avctx->sample_rate / 11025);
  140. break;
  141. case AV_CODEC_ID_ADPCM_IMA_SSI:
  142. avctx->frame_size = BLKSIZE * 2 / avctx->channels;
  143. avctx->block_align = BLKSIZE;
  144. break;
  145. case AV_CODEC_ID_ADPCM_IMA_APM:
  146. avctx->frame_size = BLKSIZE * 2 / avctx->channels;
  147. avctx->block_align = BLKSIZE;
  148. if (!(avctx->extradata = av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
  149. return AVERROR(ENOMEM);
  150. avctx->extradata_size = 28;
  151. break;
  152. case AV_CODEC_ID_ADPCM_ARGO:
  153. avctx->frame_size = 32;
  154. avctx->block_align = 17 * avctx->channels;
  155. break;
  156. default:
  157. return AVERROR(EINVAL);
  158. }
  159. return 0;
  160. }
  161. static av_cold int adpcm_encode_close(AVCodecContext *avctx)
  162. {
  163. ADPCMEncodeContext *s = avctx->priv_data;
  164. av_freep(&s->paths);
  165. av_freep(&s->node_buf);
  166. av_freep(&s->nodep_buf);
  167. av_freep(&s->trellis_hash);
  168. return 0;
  169. }
  170. static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
  171. int16_t sample)
  172. {
  173. int delta = sample - c->prev_sample;
  174. int nibble = FFMIN(7, abs(delta) * 4 /
  175. ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
  176. c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
  177. ff_adpcm_yamaha_difflookup[nibble]) / 8);
  178. c->prev_sample = av_clip_int16(c->prev_sample);
  179. c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
  180. return nibble;
  181. }
  182. static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
  183. int16_t sample)
  184. {
  185. int delta = sample - c->prev_sample;
  186. int diff, step = ff_adpcm_step_table[c->step_index];
  187. int nibble = 8*(delta < 0);
  188. delta= abs(delta);
  189. diff = delta + (step >> 3);
  190. if (delta >= step) {
  191. nibble |= 4;
  192. delta -= step;
  193. }
  194. step >>= 1;
  195. if (delta >= step) {
  196. nibble |= 2;
  197. delta -= step;
  198. }
  199. step >>= 1;
  200. if (delta >= step) {
  201. nibble |= 1;
  202. delta -= step;
  203. }
  204. diff -= delta;
  205. if (nibble & 8)
  206. c->prev_sample -= diff;
  207. else
  208. c->prev_sample += diff;
  209. c->prev_sample = av_clip_int16(c->prev_sample);
  210. c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
  211. return nibble;
  212. }
  213. static inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
  214. int16_t sample)
  215. {
  216. int predictor, nibble, bias;
  217. predictor = (((c->sample1) * (c->coeff1)) +
  218. (( c->sample2) * (c->coeff2))) / 64;
  219. nibble = sample - predictor;
  220. if (nibble >= 0)
  221. bias = c->idelta / 2;
  222. else
  223. bias = -c->idelta / 2;
  224. nibble = (nibble + bias) / c->idelta;
  225. nibble = av_clip_intp2(nibble, 3) & 0x0F;
  226. predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
  227. c->sample2 = c->sample1;
  228. c->sample1 = av_clip_int16(predictor);
  229. c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
  230. if (c->idelta < 16)
  231. c->idelta = 16;
  232. return nibble;
  233. }
  234. static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
  235. int16_t sample)
  236. {
  237. int nibble, delta;
  238. if (!c->step) {
  239. c->predictor = 0;
  240. c->step = 127;
  241. }
  242. delta = sample - c->predictor;
  243. nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
  244. c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
  245. c->predictor = av_clip_int16(c->predictor);
  246. c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
  247. c->step = av_clip(c->step, 127, 24576);
  248. return nibble;
  249. }
  250. static void adpcm_compress_trellis(AVCodecContext *avctx,
  251. const int16_t *samples, uint8_t *dst,
  252. ADPCMChannelStatus *c, int n, int stride)
  253. {
  254. //FIXME 6% faster if frontier is a compile-time constant
  255. ADPCMEncodeContext *s = avctx->priv_data;
  256. const int frontier = 1 << avctx->trellis;
  257. const int version = avctx->codec->id;
  258. TrellisPath *paths = s->paths, *p;
  259. TrellisNode *node_buf = s->node_buf;
  260. TrellisNode **nodep_buf = s->nodep_buf;
  261. TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
  262. TrellisNode **nodes_next = nodep_buf + frontier;
  263. int pathn = 0, froze = -1, i, j, k, generation = 0;
  264. uint8_t *hash = s->trellis_hash;
  265. memset(hash, 0xff, 65536 * sizeof(*hash));
  266. memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
  267. nodes[0] = node_buf + frontier;
  268. nodes[0]->ssd = 0;
  269. nodes[0]->path = 0;
  270. nodes[0]->step = c->step_index;
  271. nodes[0]->sample1 = c->sample1;
  272. nodes[0]->sample2 = c->sample2;
  273. if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
  274. version == AV_CODEC_ID_ADPCM_IMA_QT ||
  275. version == AV_CODEC_ID_ADPCM_SWF)
  276. nodes[0]->sample1 = c->prev_sample;
  277. if (version == AV_CODEC_ID_ADPCM_MS)
  278. nodes[0]->step = c->idelta;
  279. if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
  280. if (c->step == 0) {
  281. nodes[0]->step = 127;
  282. nodes[0]->sample1 = 0;
  283. } else {
  284. nodes[0]->step = c->step;
  285. nodes[0]->sample1 = c->predictor;
  286. }
  287. }
  288. for (i = 0; i < n; i++) {
  289. TrellisNode *t = node_buf + frontier*(i&1);
  290. TrellisNode **u;
  291. int sample = samples[i * stride];
  292. int heap_pos = 0;
  293. memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
  294. for (j = 0; j < frontier && nodes[j]; j++) {
  295. // higher j have higher ssd already, so they're likely
  296. // to yield a suboptimal next sample too
  297. const int range = (j < frontier / 2) ? 1 : 0;
  298. const int step = nodes[j]->step;
  299. int nidx;
  300. if (version == AV_CODEC_ID_ADPCM_MS) {
  301. const int predictor = ((nodes[j]->sample1 * c->coeff1) +
  302. (nodes[j]->sample2 * c->coeff2)) / 64;
  303. const int div = (sample - predictor) / step;
  304. const int nmin = av_clip(div-range, -8, 6);
  305. const int nmax = av_clip(div+range, -7, 7);
  306. for (nidx = nmin; nidx <= nmax; nidx++) {
  307. const int nibble = nidx & 0xf;
  308. int dec_sample = predictor + nidx * step;
  309. #define STORE_NODE(NAME, STEP_INDEX)\
  310. int d;\
  311. uint32_t ssd;\
  312. int pos;\
  313. TrellisNode *u;\
  314. uint8_t *h;\
  315. dec_sample = av_clip_int16(dec_sample);\
  316. d = sample - dec_sample;\
  317. ssd = nodes[j]->ssd + d*(unsigned)d;\
  318. /* Check for wraparound, skip such samples completely. \
  319. * Note, changing ssd to a 64 bit variable would be \
  320. * simpler, avoiding this check, but it's slower on \
  321. * x86 32 bit at the moment. */\
  322. if (ssd < nodes[j]->ssd)\
  323. goto next_##NAME;\
  324. /* Collapse any two states with the same previous sample value. \
  325. * One could also distinguish states by step and by 2nd to last
  326. * sample, but the effects of that are negligible.
  327. * Since nodes in the previous generation are iterated
  328. * through a heap, they're roughly ordered from better to
  329. * worse, but not strictly ordered. Therefore, an earlier
  330. * node with the same sample value is better in most cases
  331. * (and thus the current is skipped), but not strictly
  332. * in all cases. Only skipping samples where ssd >=
  333. * ssd of the earlier node with the same sample gives
  334. * slightly worse quality, though, for some reason. */ \
  335. h = &hash[(uint16_t) dec_sample];\
  336. if (*h == generation)\
  337. goto next_##NAME;\
  338. if (heap_pos < frontier) {\
  339. pos = heap_pos++;\
  340. } else {\
  341. /* Try to replace one of the leaf nodes with the new \
  342. * one, but try a different slot each time. */\
  343. pos = (frontier >> 1) +\
  344. (heap_pos & ((frontier >> 1) - 1));\
  345. if (ssd > nodes_next[pos]->ssd)\
  346. goto next_##NAME;\
  347. heap_pos++;\
  348. }\
  349. *h = generation;\
  350. u = nodes_next[pos];\
  351. if (!u) {\
  352. av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
  353. u = t++;\
  354. nodes_next[pos] = u;\
  355. u->path = pathn++;\
  356. }\
  357. u->ssd = ssd;\
  358. u->step = STEP_INDEX;\
  359. u->sample2 = nodes[j]->sample1;\
  360. u->sample1 = dec_sample;\
  361. paths[u->path].nibble = nibble;\
  362. paths[u->path].prev = nodes[j]->path;\
  363. /* Sift the newly inserted node up in the heap to \
  364. * restore the heap property. */\
  365. while (pos > 0) {\
  366. int parent = (pos - 1) >> 1;\
  367. if (nodes_next[parent]->ssd <= ssd)\
  368. break;\
  369. FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
  370. pos = parent;\
  371. }\
  372. next_##NAME:;
  373. STORE_NODE(ms, FFMAX(16,
  374. (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
  375. }
  376. } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
  377. version == AV_CODEC_ID_ADPCM_IMA_QT ||
  378. version == AV_CODEC_ID_ADPCM_SWF) {
  379. #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
  380. const int predictor = nodes[j]->sample1;\
  381. const int div = (sample - predictor) * 4 / STEP_TABLE;\
  382. int nmin = av_clip(div - range, -7, 6);\
  383. int nmax = av_clip(div + range, -6, 7);\
  384. if (nmin <= 0)\
  385. nmin--; /* distinguish -0 from +0 */\
  386. if (nmax < 0)\
  387. nmax--;\
  388. for (nidx = nmin; nidx <= nmax; nidx++) {\
  389. const int nibble = nidx < 0 ? 7 - nidx : nidx;\
  390. int dec_sample = predictor +\
  391. (STEP_TABLE *\
  392. ff_adpcm_yamaha_difflookup[nibble]) / 8;\
  393. STORE_NODE(NAME, STEP_INDEX);\
  394. }
  395. LOOP_NODES(ima, ff_adpcm_step_table[step],
  396. av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
  397. } else { //AV_CODEC_ID_ADPCM_YAMAHA
  398. LOOP_NODES(yamaha, step,
  399. av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
  400. 127, 24576));
  401. #undef LOOP_NODES
  402. #undef STORE_NODE
  403. }
  404. }
  405. u = nodes;
  406. nodes = nodes_next;
  407. nodes_next = u;
  408. generation++;
  409. if (generation == 255) {
  410. memset(hash, 0xff, 65536 * sizeof(*hash));
  411. generation = 0;
  412. }
  413. // prevent overflow
  414. if (nodes[0]->ssd > (1 << 28)) {
  415. for (j = 1; j < frontier && nodes[j]; j++)
  416. nodes[j]->ssd -= nodes[0]->ssd;
  417. nodes[0]->ssd = 0;
  418. }
  419. // merge old paths to save memory
  420. if (i == froze + FREEZE_INTERVAL) {
  421. p = &paths[nodes[0]->path];
  422. for (k = i; k > froze; k--) {
  423. dst[k] = p->nibble;
  424. p = &paths[p->prev];
  425. }
  426. froze = i;
  427. pathn = 0;
  428. // other nodes might use paths that don't coincide with the frozen one.
  429. // checking which nodes do so is too slow, so just kill them all.
  430. // this also slightly improves quality, but I don't know why.
  431. memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
  432. }
  433. }
  434. p = &paths[nodes[0]->path];
  435. for (i = n - 1; i > froze; i--) {
  436. dst[i] = p->nibble;
  437. p = &paths[p->prev];
  438. }
  439. c->predictor = nodes[0]->sample1;
  440. c->sample1 = nodes[0]->sample1;
  441. c->sample2 = nodes[0]->sample2;
  442. c->step_index = nodes[0]->step;
  443. c->step = nodes[0]->step;
  444. c->idelta = nodes[0]->step;
  445. }
  446. static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
  447. int shift, int flag)
  448. {
  449. int nibble;
  450. if (flag)
  451. nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
  452. else
  453. nibble = 4 * s - 4 * cs->sample1;
  454. return (nibble >> shift) & 0x0F;
  455. }
  456. static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
  457. const int16_t *samples, int nsamples,
  458. int shift, int flag)
  459. {
  460. int64_t error = 0;
  461. if (pb) {
  462. put_bits(pb, 4, shift - 2);
  463. put_bits(pb, 1, 0);
  464. put_bits(pb, 1, !!flag);
  465. put_bits(pb, 2, 0);
  466. }
  467. for (int n = 0; n < nsamples; n++) {
  468. /* Compress the nibble, then expand it to see how much precision we've lost. */
  469. int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
  470. int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
  471. error += abs(samples[n] - sample);
  472. if (pb)
  473. put_bits(pb, 4, nibble);
  474. }
  475. return error;
  476. }
  477. static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  478. const AVFrame *frame, int *got_packet_ptr)
  479. {
  480. int n, i, ch, st, pkt_size, ret;
  481. const int16_t *samples;
  482. int16_t **samples_p;
  483. uint8_t *dst;
  484. ADPCMEncodeContext *c = avctx->priv_data;
  485. uint8_t *buf;
  486. samples = (const int16_t *)frame->data[0];
  487. samples_p = (int16_t **)frame->extended_data;
  488. st = avctx->channels == 2;
  489. if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
  490. pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
  491. else if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
  492. avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM)
  493. pkt_size = (frame->nb_samples * avctx->channels) / 2;
  494. else
  495. pkt_size = avctx->block_align;
  496. if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
  497. return ret;
  498. dst = avpkt->data;
  499. switch(avctx->codec->id) {
  500. case AV_CODEC_ID_ADPCM_IMA_WAV:
  501. {
  502. int blocks, j;
  503. blocks = (frame->nb_samples - 1) / 8;
  504. for (ch = 0; ch < avctx->channels; ch++) {
  505. ADPCMChannelStatus *status = &c->status[ch];
  506. status->prev_sample = samples_p[ch][0];
  507. /* status->step_index = 0;
  508. XXX: not sure how to init the state machine */
  509. bytestream_put_le16(&dst, status->prev_sample);
  510. *dst++ = status->step_index;
  511. *dst++ = 0; /* unknown */
  512. }
  513. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
  514. if (avctx->trellis > 0) {
  515. if (!FF_ALLOC_TYPED_ARRAY(buf, avctx->channels * blocks * 8))
  516. return AVERROR(ENOMEM);
  517. for (ch = 0; ch < avctx->channels; ch++) {
  518. adpcm_compress_trellis(avctx, &samples_p[ch][1],
  519. buf + ch * blocks * 8, &c->status[ch],
  520. blocks * 8, 1);
  521. }
  522. for (i = 0; i < blocks; i++) {
  523. for (ch = 0; ch < avctx->channels; ch++) {
  524. uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
  525. for (j = 0; j < 8; j += 2)
  526. *dst++ = buf1[j] | (buf1[j + 1] << 4);
  527. }
  528. }
  529. av_free(buf);
  530. } else {
  531. for (i = 0; i < blocks; i++) {
  532. for (ch = 0; ch < avctx->channels; ch++) {
  533. ADPCMChannelStatus *status = &c->status[ch];
  534. const int16_t *smp = &samples_p[ch][1 + i * 8];
  535. for (j = 0; j < 8; j += 2) {
  536. uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
  537. v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
  538. *dst++ = v;
  539. }
  540. }
  541. }
  542. }
  543. break;
  544. }
  545. case AV_CODEC_ID_ADPCM_IMA_QT:
  546. {
  547. PutBitContext pb;
  548. init_put_bits(&pb, dst, pkt_size);
  549. for (ch = 0; ch < avctx->channels; ch++) {
  550. ADPCMChannelStatus *status = &c->status[ch];
  551. put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
  552. put_bits(&pb, 7, status->step_index);
  553. if (avctx->trellis > 0) {
  554. uint8_t buf[64];
  555. adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
  556. 64, 1);
  557. for (i = 0; i < 64; i++)
  558. put_bits(&pb, 4, buf[i ^ 1]);
  559. status->prev_sample = status->predictor;
  560. } else {
  561. for (i = 0; i < 64; i += 2) {
  562. int t1, t2;
  563. t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
  564. t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
  565. put_bits(&pb, 4, t2);
  566. put_bits(&pb, 4, t1);
  567. }
  568. }
  569. }
  570. flush_put_bits(&pb);
  571. break;
  572. }
  573. case AV_CODEC_ID_ADPCM_IMA_SSI:
  574. {
  575. PutBitContext pb;
  576. init_put_bits(&pb, dst, pkt_size);
  577. av_assert0(avctx->trellis == 0);
  578. for (i = 0; i < frame->nb_samples; i++) {
  579. for (ch = 0; ch < avctx->channels; ch++) {
  580. put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
  581. }
  582. }
  583. flush_put_bits(&pb);
  584. break;
  585. }
  586. case AV_CODEC_ID_ADPCM_SWF:
  587. {
  588. PutBitContext pb;
  589. init_put_bits(&pb, dst, pkt_size);
  590. n = frame->nb_samples - 1;
  591. // store AdpcmCodeSize
  592. put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
  593. // init the encoder state
  594. for (i = 0; i < avctx->channels; i++) {
  595. // clip step so it fits 6 bits
  596. c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
  597. put_sbits(&pb, 16, samples[i]);
  598. put_bits(&pb, 6, c->status[i].step_index);
  599. c->status[i].prev_sample = samples[i];
  600. }
  601. if (avctx->trellis > 0) {
  602. if (!(buf = av_malloc(2 * n)))
  603. return AVERROR(ENOMEM);
  604. adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
  605. &c->status[0], n, avctx->channels);
  606. if (avctx->channels == 2)
  607. adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
  608. buf + n, &c->status[1], n,
  609. avctx->channels);
  610. for (i = 0; i < n; i++) {
  611. put_bits(&pb, 4, buf[i]);
  612. if (avctx->channels == 2)
  613. put_bits(&pb, 4, buf[n + i]);
  614. }
  615. av_free(buf);
  616. } else {
  617. for (i = 1; i < frame->nb_samples; i++) {
  618. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
  619. samples[avctx->channels * i]));
  620. if (avctx->channels == 2)
  621. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
  622. samples[2 * i + 1]));
  623. }
  624. }
  625. flush_put_bits(&pb);
  626. break;
  627. }
  628. case AV_CODEC_ID_ADPCM_MS:
  629. for (i = 0; i < avctx->channels; i++) {
  630. int predictor = 0;
  631. *dst++ = predictor;
  632. c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
  633. c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
  634. }
  635. for (i = 0; i < avctx->channels; i++) {
  636. if (c->status[i].idelta < 16)
  637. c->status[i].idelta = 16;
  638. bytestream_put_le16(&dst, c->status[i].idelta);
  639. }
  640. for (i = 0; i < avctx->channels; i++)
  641. c->status[i].sample2= *samples++;
  642. for (i = 0; i < avctx->channels; i++) {
  643. c->status[i].sample1 = *samples++;
  644. bytestream_put_le16(&dst, c->status[i].sample1);
  645. }
  646. for (i = 0; i < avctx->channels; i++)
  647. bytestream_put_le16(&dst, c->status[i].sample2);
  648. if (avctx->trellis > 0) {
  649. n = avctx->block_align - 7 * avctx->channels;
  650. if (!(buf = av_malloc(2 * n)))
  651. return AVERROR(ENOMEM);
  652. if (avctx->channels == 1) {
  653. adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
  654. avctx->channels);
  655. for (i = 0; i < n; i += 2)
  656. *dst++ = (buf[i] << 4) | buf[i + 1];
  657. } else {
  658. adpcm_compress_trellis(avctx, samples, buf,
  659. &c->status[0], n, avctx->channels);
  660. adpcm_compress_trellis(avctx, samples + 1, buf + n,
  661. &c->status[1], n, avctx->channels);
  662. for (i = 0; i < n; i++)
  663. *dst++ = (buf[i] << 4) | buf[n + i];
  664. }
  665. av_free(buf);
  666. } else {
  667. for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
  668. int nibble;
  669. nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
  670. nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
  671. *dst++ = nibble;
  672. }
  673. }
  674. break;
  675. case AV_CODEC_ID_ADPCM_YAMAHA:
  676. n = frame->nb_samples / 2;
  677. if (avctx->trellis > 0) {
  678. if (!(buf = av_malloc(2 * n * 2)))
  679. return AVERROR(ENOMEM);
  680. n *= 2;
  681. if (avctx->channels == 1) {
  682. adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
  683. avctx->channels);
  684. for (i = 0; i < n; i += 2)
  685. *dst++ = buf[i] | (buf[i + 1] << 4);
  686. } else {
  687. adpcm_compress_trellis(avctx, samples, buf,
  688. &c->status[0], n, avctx->channels);
  689. adpcm_compress_trellis(avctx, samples + 1, buf + n,
  690. &c->status[1], n, avctx->channels);
  691. for (i = 0; i < n; i++)
  692. *dst++ = buf[i] | (buf[n + i] << 4);
  693. }
  694. av_free(buf);
  695. } else
  696. for (n *= avctx->channels; n > 0; n--) {
  697. int nibble;
  698. nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
  699. nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
  700. *dst++ = nibble;
  701. }
  702. break;
  703. case AV_CODEC_ID_ADPCM_IMA_APM:
  704. {
  705. PutBitContext pb;
  706. init_put_bits(&pb, dst, pkt_size);
  707. av_assert0(avctx->trellis == 0);
  708. for (n = frame->nb_samples / 2; n > 0; n--) {
  709. for (ch = 0; ch < avctx->channels; ch++) {
  710. put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
  711. put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
  712. }
  713. samples += avctx->channels;
  714. }
  715. flush_put_bits(&pb);
  716. break;
  717. }
  718. case AV_CODEC_ID_ADPCM_ARGO:
  719. {
  720. PutBitContext pb;
  721. init_put_bits(&pb, dst, pkt_size);
  722. av_assert0(frame->nb_samples == 32);
  723. for (ch = 0; ch < avctx->channels; ch++) {
  724. int64_t error = INT64_MAX, tmperr = INT64_MAX;
  725. int shift = 2, flag = 0;
  726. int saved1 = c->status[ch].sample1;
  727. int saved2 = c->status[ch].sample2;
  728. /* Find the optimal coefficients, bail early if we find a perfect result. */
  729. for (int s = 2; s < 18 && tmperr != 0; s++) {
  730. for (int f = 0; f < 2 && tmperr != 0; f++) {
  731. c->status[ch].sample1 = saved1;
  732. c->status[ch].sample2 = saved2;
  733. tmperr = adpcm_argo_compress_block(c->status + ch, NULL, samples_p[ch],
  734. frame->nb_samples, s, f);
  735. if (tmperr < error) {
  736. shift = s;
  737. flag = f;
  738. error = tmperr;
  739. }
  740. }
  741. }
  742. /* Now actually do the encode. */
  743. c->status[ch].sample1 = saved1;
  744. c->status[ch].sample2 = saved2;
  745. adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
  746. frame->nb_samples, shift, flag);
  747. }
  748. flush_put_bits(&pb);
  749. break;
  750. }
  751. default:
  752. return AVERROR(EINVAL);
  753. }
  754. avpkt->size = pkt_size;
  755. *got_packet_ptr = 1;
  756. return 0;
  757. }
  758. static const enum AVSampleFormat sample_fmts[] = {
  759. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
  760. };
  761. static const enum AVSampleFormat sample_fmts_p[] = {
  762. AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
  763. };
  764. static const AVOption options[] = {
  765. {
  766. .name = "block_size",
  767. .help = "set the block size",
  768. .offset = offsetof(ADPCMEncodeContext, block_size),
  769. .type = AV_OPT_TYPE_INT,
  770. .default_val = {.i64 = BLKSIZE},
  771. .min = 32,
  772. .max = 8192, /* Is this a reasonable upper limit? */
  773. .flags = AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  774. },
  775. { NULL }
  776. };
  777. static const AVClass adpcm_encoder_class = {
  778. .class_name = "ADPCM Encoder",
  779. .item_name = av_default_item_name,
  780. .option = options,
  781. .version = LIBAVUTIL_VERSION_INT,
  782. };
  783. #define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_) \
  784. AVCodec ff_ ## name_ ## _encoder = { \
  785. .name = #name_, \
  786. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  787. .type = AVMEDIA_TYPE_AUDIO, \
  788. .id = id_, \
  789. .priv_data_size = sizeof(ADPCMEncodeContext), \
  790. .init = adpcm_encode_init, \
  791. .encode2 = adpcm_encode_frame, \
  792. .close = adpcm_encode_close, \
  793. .sample_fmts = sample_fmts_, \
  794. .capabilities = capabilities_, \
  795. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
  796. .priv_class = &adpcm_encoder_class, \
  797. }
  798. ADPCM_ENCODER(AV_CODEC_ID_ADPCM_ARGO, adpcm_argo, sample_fmts_p, 0, "ADPCM Argonaut Games");
  799. ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM");
  800. ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, 0, "ADPCM IMA QuickTime");
  801. ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive");
  802. ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0, "ADPCM IMA WAV");
  803. ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, sample_fmts, 0, "ADPCM Microsoft");
  804. ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, sample_fmts, 0, "ADPCM Shockwave Flash");
  805. ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, 0, "ADPCM Yamaha");