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.

687 lines
26KB

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