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.

867 lines
32KB

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