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.

742 lines
28KB

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