60 #define FREEZE_INTERVAL 128
80 int frontier = 1 << avctx->
trellis;
83 max_paths *
sizeof(*s->
paths), error);
85 2 * frontier *
sizeof(*s->
node_buf), error);
87 2 * frontier *
sizeof(*s->
nodep_buf), error);
118 bytestream_put_le16(&extradata, avctx->
frame_size);
119 bytestream_put_le16(&extradata, 7);
120 for (i = 0; i < 7; i++) {
170 int nibble =
FFMIN(7, abs(delta) * 4 /
184 int diff = step >> 3;
192 for (mask = 4;
mask;) {
216 int predictor, nibble, bias;
221 nibble = sample - predictor;
227 nibble = (nibble + bias) / c->
idelta;
228 nibble = av_clip(nibble, -8, 7) & 0x0F;
230 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->
idelta;
233 c->
sample1 = av_clip_int16(predictor);
254 nibble =
FFMIN(7, abs(delta) * 4 / c->
step) + (delta < 0) * 8;
259 c->
step = av_clip(c->
step, 127, 24567);
270 const int frontier = 1 << avctx->
trellis;
277 int pathn = 0, froze = -1, i, j, k, generation = 0;
279 memset(hash, 0xff, 65536 *
sizeof(*hash));
281 memset(nodep_buf, 0, 2 * frontier *
sizeof(*nodep_buf));
282 nodes[0] = node_buf + frontier;
296 nodes[0]->
step = 127;
304 for (i = 0; i < n; i++) {
309 memset(nodes_next, 0, frontier *
sizeof(
TrellisNode*));
310 for (j = 0; j < frontier && nodes[j]; j++) {
313 const int range = (j < frontier / 2) ? 1 : 0;
314 const int step = nodes[j]->step;
317 const int predictor = ((nodes[j]->sample1 * c->
coeff1) +
318 (nodes[j]->sample2 * c->
coeff2)) / 64;
319 const int div = (sample - predictor) / step;
320 const int nmin = av_clip(div-range, -8, 6);
321 const int nmax = av_clip(div+range, -7, 7);
322 for (nidx = nmin; nidx <= nmax; nidx++) {
323 const int nibble = nidx & 0xf;
324 int dec_sample = predictor + nidx *
step;
325 #define STORE_NODE(NAME, STEP_INDEX)\
331 dec_sample = av_clip_int16(dec_sample);\
332 d = sample - dec_sample;\
333 ssd = nodes[j]->ssd + d*d;\
338 if (ssd < nodes[j]->ssd)\
351 h = &hash[(uint16_t) dec_sample];\
352 if (*h == generation)\
354 if (heap_pos < frontier) {\
359 pos = (frontier >> 1) +\
360 (heap_pos & ((frontier >> 1) - 1));\
361 if (ssd > nodes_next[pos]->ssd)\
366 u = nodes_next[pos];\
368 assert(pathn < FREEZE_INTERVAL << avctx->trellis);\
370 nodes_next[pos] = u;\
374 u->step = STEP_INDEX;\
375 u->sample2 = nodes[j]->sample1;\
376 u->sample1 = dec_sample;\
377 paths[u->path].nibble = nibble;\
378 paths[u->path].prev = nodes[j]->path;\
382 int parent = (pos - 1) >> 1;\
383 if (nodes_next[parent]->ssd <= ssd)\
385 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
395 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
396 const int predictor = nodes[j]->sample1;\
397 const int div = (sample - predictor) * 4 / STEP_TABLE;\
398 int nmin = av_clip(div - range, -7, 6);\
399 int nmax = av_clip(div + range, -6, 7);\
404 for (nidx = nmin; nidx <= nmax; nidx++) {\
405 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
406 int dec_sample = predictor +\
408 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
409 STORE_NODE(NAME, STEP_INDEX);\
427 if (generation == 255) {
428 memset(hash, 0xff, 65536 *
sizeof(*hash));
433 if (nodes[0]->ssd > (1 << 28)) {
434 for (j = 1; j < frontier && nodes[j]; j++)
435 nodes[j]->ssd -= nodes[0]->ssd;
441 p = &paths[nodes[0]->path];
442 for (k = i; k > froze; k--) {
451 memset(nodes + 1, 0, (frontier - 1) *
sizeof(
TrellisNode*));
455 p = &paths[nodes[0]->
path];
456 for (i = n - 1; i > froze; i--) {
462 c->
sample1 = nodes[0]->sample1;
463 c->
sample2 = nodes[0]->sample2;
465 c->
step = nodes[0]->step;
466 c->
idelta = nodes[0]->step;
470 const AVFrame *frame,
int *got_packet_ptr)
472 int n, i, ch, st, pkt_size, ret;
479 samples = (
const int16_t *)frame->
data[0];
500 for (ch = 0; ch < avctx->
channels; ch++) {
513 for (ch = 0; ch < avctx->
channels; ch++) {
515 buf + ch * blocks * 8, &c->
status[ch],
518 for (i = 0; i < blocks; i++) {
519 for (ch = 0; ch < avctx->
channels; ch++) {
520 uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
521 for (j = 0; j < 8; j += 2)
522 *dst++ = buf1[j] | (buf1[j + 1] << 4);
527 for (i = 0; i < blocks; i++) {
528 for (ch = 0; ch < avctx->
channels; ch++) {
530 const int16_t *smp = &samples_p[ch][1 + i * 8];
531 for (j = 0; j < 8; j += 2) {
546 for (ch = 0; ch < avctx->
channels; ch++) {
554 for (i = 0; i < 64; i++)
557 for (i = 0; i < 64; i += 2) {
581 for (i = 0; i < avctx->
channels; i++) {
595 buf + n, &c->
status[1], n,
597 for (i = 0; i < n; i++) {
609 samples[2 * i + 1]));
616 for (i = 0; i < avctx->
channels; i++) {
622 for (i = 0; i < avctx->
channels; i++) {
627 for (i = 0; i < avctx->
channels; i++)
633 for (i = 0; i < avctx->
channels; i++)
642 for (i = 0; i < n; i += 2)
643 *dst++ = (buf[i] << 4) | buf[i + 1];
649 for (i = 0; i < n; i++)
650 *dst++ = (buf[i] << 4) | buf[n + i];
654 for (i = 7 * avctx->
channels; i < avctx->block_align; i++) {
670 for (i = 0; i < n; i += 2)
671 *dst++ = buf[i] | (buf[i + 1] << 4);
677 for (i = 0; i < n; i++)
678 *dst++ = buf[i] | (buf[n + i] << 4);
682 for (n *= avctx->
channels; n > 0; n--) {
708 #define ADPCM_ENCODER(id_, name_, sample_fmts_, long_name_) \
709 AVCodec ff_ ## name_ ## _encoder = { \
711 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
712 .type = AVMEDIA_TYPE_AUDIO, \
714 .priv_data_size = sizeof(ADPCMEncodeContext), \
715 .init = adpcm_encode_init, \
716 .encode2 = adpcm_encode_frame, \
717 .close = adpcm_encode_close, \
718 .sample_fmts = sample_fmts_, \