59 #define FREEZE_INTERVAL 128
75 int frontier = 1 << avctx->
trellis;
78 max_paths *
sizeof(*s->
paths), error);
80 2 * frontier *
sizeof(*s->
node_buf), error);
82 2 * frontier *
sizeof(*s->
nodep_buf), error);
113 bytestream_put_le16(&extradata, avctx->
frame_size);
114 bytestream_put_le16(&extradata, 7);
115 for (i = 0; i < 7; i++) {
167 int nibble =
FFMIN(7, abs(delta) * 4 /
181 int diff = step >> 3;
189 for (mask = 4;
mask;) {
213 int predictor, nibble, bias;
218 nibble = sample - predictor;
224 nibble = (nibble + bias) / c->
idelta;
225 nibble = av_clip(nibble, -8, 7) & 0x0F;
227 predictor += (signed)((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->
idelta;
230 c->
sample1 = av_clip_int16(predictor);
251 nibble =
FFMIN(7, abs(delta) * 4 / c->
step) + (delta < 0) * 8;
256 c->
step = av_clip(c->
step, 127, 24567);
266 const int frontier = 1 << avctx->
trellis;
274 int pathn = 0, froze = -1, i, j, k, generation = 0;
276 memset(hash, 0xff, 65536 *
sizeof(*hash));
278 memset(nodep_buf, 0, 2 * frontier *
sizeof(*nodep_buf));
279 nodes[0] = node_buf + frontier;
293 nodes[0]->
step = 127;
301 for (i = 0; i < n; i++) {
304 int sample = samples[i *
stride];
306 memset(nodes_next, 0, frontier *
sizeof(
TrellisNode*));
307 for (j = 0; j < frontier && nodes[j]; j++) {
310 const int range = (j < frontier / 2) ? 1 : 0;
311 const int step = nodes[j]->step;
314 const int predictor = ((nodes[j]->sample1 * c->
coeff1) +
315 (nodes[j]->sample2 * c->
coeff2)) / 64;
316 const int div = (sample - predictor) / step;
317 const int nmin = av_clip(div-range, -8, 6);
318 const int nmax = av_clip(div+range, -7, 7);
319 for (nidx = nmin; nidx <= nmax; nidx++) {
320 const int nibble = nidx & 0xf;
321 int dec_sample = predictor + nidx *
step;
322 #define STORE_NODE(NAME, STEP_INDEX)\
328 dec_sample = av_clip_int16(dec_sample);\
329 d = sample - dec_sample;\
330 ssd = nodes[j]->ssd + d*d;\
335 if (ssd < nodes[j]->ssd)\
348 h = &hash[(uint16_t) dec_sample];\
349 if (*h == generation)\
351 if (heap_pos < frontier) {\
356 pos = (frontier >> 1) +\
357 (heap_pos & ((frontier >> 1) - 1));\
358 if (ssd > nodes_next[pos]->ssd)\
363 u = nodes_next[pos];\
365 assert(pathn < FREEZE_INTERVAL << avctx->trellis);\
367 nodes_next[pos] = u;\
371 u->step = STEP_INDEX;\
372 u->sample2 = nodes[j]->sample1;\
373 u->sample1 = dec_sample;\
374 paths[u->path].nibble = nibble;\
375 paths[u->path].prev = nodes[j]->path;\
379 int parent = (pos - 1) >> 1;\
380 if (nodes_next[parent]->ssd <= ssd)\
382 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
392 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
393 const int predictor = nodes[j]->sample1;\
394 const int div = (sample - predictor) * 4 / STEP_TABLE;\
395 int nmin = av_clip(div - range, -7, 6);\
396 int nmax = av_clip(div + range, -6, 7);\
401 for (nidx = nmin; nidx <= nmax; nidx++) {\
402 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
403 int dec_sample = predictor +\
405 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
406 STORE_NODE(NAME, STEP_INDEX);\
424 if (generation == 255) {
425 memset(hash, 0xff, 65536 *
sizeof(*hash));
430 if (nodes[0]->ssd > (1 << 28)) {
431 for (j = 1; j < frontier && nodes[j]; j++)
432 nodes[j]->ssd -= nodes[0]->ssd;
438 p = &paths[nodes[0]->path];
439 for (k = i; k > froze; k--) {
448 memset(nodes + 1, 0, (frontier - 1) *
sizeof(
TrellisNode*));
452 p = &paths[nodes[0]->
path];
453 for (i = n - 1; i > froze; i--) {
459 c->
sample1 = nodes[0]->sample1;
460 c->
sample2 = nodes[0]->sample2;
462 c->
step = nodes[0]->step;
463 c->
idelta = nodes[0]->step;
467 unsigned char *frame,
int buf_size,
void *
data)
476 samples = (
short *)data;
507 for (i = 0; i < n; i++) {
508 *dst++ = buf[8 * i + 0] | (buf[8 * i + 1] << 4);
509 *dst++ = buf[8 * i + 2] | (buf[8 * i + 3] << 4);
510 *dst++ = buf[8 * i + 4] | (buf[8 * i + 5] << 4);
511 *dst++ = buf[8 * i + 6] | (buf[8 * i + 7] << 4);
513 uint8_t *buf1 = buf + n * 8;
514 *dst++ = buf1[8 * i + 0] | (buf1[8 * i + 1] << 4);
515 *dst++ = buf1[8 * i + 2] | (buf1[8 * i + 3] << 4);
516 *dst++ = buf1[8 * i + 4] | (buf1[8 * i + 5] << 4);
517 *dst++ = buf1[8 * i + 6] | (buf1[8 * i + 7] << 4);
552 for (ch = 0; ch < avctx->
channels; ch++) {
558 for (i = 0; i < 64; i++)
561 for (i = 0; i < 64; i += 2) {
564 samples[avctx->
channels * (i + 0) + ch]);
566 samples[avctx->
channels * (i + 1) + ch]);
589 for (i = 0; i < avctx->
channels; i++) {
603 for (i = 0; i < n; i++) {
615 samples[2 * i + 1]));
623 for (i = 0; i < avctx->
channels; i++) {
629 for (i = 0; i < avctx->
channels; i++) {
634 for (i = 0; i < avctx->
channels; i++)
640 for (i = 0; i < avctx->
channels; i++)
648 for (i = 0; i < n; i += 2)
649 *dst++ = (buf[i] << 4) | buf[i + 1];
653 for (i = 0; i < n; i++)
654 *dst++ = (buf[i] << 4) | buf[n + i];
658 for (i = 7 * avctx->
channels; i < avctx->block_align; i++) {
673 for (i = 0; i < n; i += 2)
674 *dst++ = buf[i] | (buf[i + 1] << 4);
678 for (i = 0; i < n; i++)
679 *dst++ = buf[i] | (buf[n + i] << 4);
698 #define ADPCM_ENCODER(id_, name_, long_name_) \
699 AVCodec ff_ ## name_ ## _encoder = { \
701 .type = AVMEDIA_TYPE_AUDIO, \
703 .priv_data_size = sizeof(ADPCMEncodeContext), \
704 .init = adpcm_encode_init, \
705 .encode = adpcm_encode_frame, \
706 .close = adpcm_encode_close, \
707 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, \
708 AV_SAMPLE_FMT_NONE}, \
709 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \