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.

994 lines
37KB

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