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.

938 lines
34KB

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