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.

752 lines
28KB

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