33 #define ME_MODE_BIDIR 0
34 #define ME_MODE_BILAT 1
36 #define MC_MODE_OBMC 0
37 #define MC_MODE_AOBMC 1
39 #define SCD_METHOD_NONE 0
40 #define SCD_METHOD_FDIFF 1
43 #define NB_PIXEL_MVS 32
44 #define NB_CLUSTERS 128
46 #define ALPHA_MAX 1024
47 #define CLUSTER_THRESHOLD 4
48 #define PX_WEIGHT_MAX 255
49 #define COST_PRED_SCALE 64
52 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
53 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
54 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
55 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
56 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
57 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
58 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
59 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
60 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
61 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
62 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
63 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
64 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
65 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
66 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
67 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
68 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
69 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
70 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
71 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
72 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
73 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
74 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
75 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
76 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
77 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
78 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
79 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
80 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
81 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
82 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
83 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
87 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
88 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
89 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
90 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
91 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
92 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
93 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
94 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
95 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
96 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
97 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
98 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
99 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
100 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
101 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
102 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
106 4, 12, 20, 28, 28, 20, 12, 4,
107 12, 36, 60, 84, 84, 60, 36, 12,
108 20, 60,100,140,140,100, 60, 20,
109 28, 84,140,196,196,140, 84, 28,
110 28, 84,140,196,196,140, 84, 28,
111 20, 60,100,140,140,100, 60, 20,
112 12, 36, 60, 84, 84, 60, 36, 12,
113 4, 12, 20, 28, 28, 20, 12, 4,
138 typedef struct Block {
159 typedef struct Frame {
199 #define OFFSET(x) offsetof(MIContext, x)
200 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
201 #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, .unit = u }
211 CONST(
"aobmc",
"adaptive overlapped block motion compensation",
MC_MODE_AOBMC,
"mc_mode"),
251 uint8_t *data_cur = me_ctx->
data_cur;
252 uint8_t *data_next = me_ctx->
data_ref;
254 int mv_x1 = x_mv - x;
255 int mv_y1 = y_mv - y;
256 int mv_x, mv_y,
i, j;
264 data_cur += (y + mv_y) * linesize;
265 data_next += (y - mv_y) * linesize;
267 for (j = 0; j < me_ctx->
mb_size; j++)
269 sbad +=
FFABS(data_cur[x + mv_x +
i + j * linesize] - data_next[x - mv_x +
i + j * linesize]);
276 uint8_t *data_cur = me_ctx->
data_cur;
277 uint8_t *data_next = me_ctx->
data_ref;
283 int mv_x1 = x_mv - x;
284 int mv_y1 = y_mv - y;
285 int mv_x, mv_y,
i, j;
290 mv_x =
av_clip(x_mv - x, -
FFMIN(x - x_min, x_max - x),
FFMIN(x - x_min, x_max - x));
291 mv_y =
av_clip(y_mv - y, -
FFMIN(y - y_min, y_max - y),
FFMIN(y - y_min, y_max - y));
293 for (j = -me_ctx->
mb_size / 2; j < me_ctx->mb_size * 3 / 2; j++)
294 for (
i = -me_ctx->
mb_size / 2; i < me_ctx->mb_size * 3 / 2;
i++)
295 sbad +=
FFABS(data_cur[x + mv_x +
i + (y + mv_y + j) * linesize] - data_next[x - mv_x +
i + (y - mv_y + j) * linesize]);
302 uint8_t *data_ref = me_ctx->
data_ref;
303 uint8_t *data_cur = me_ctx->
data_cur;
316 x_mv =
av_clip(x_mv, x_min, x_max);
317 y_mv =
av_clip(y_mv, y_min, y_max);
319 for (j = -me_ctx->
mb_size / 2; j < me_ctx->mb_size * 3 / 2; j++)
320 for (
i = -me_ctx->
mb_size / 2; i < me_ctx->mb_size * 3 / 2;
i++)
321 sad +=
FFABS(data_ref[x_mv +
i + (y_mv + j) * linesize] - data_cur[x +
i + (y + j) * linesize]);
381 for (
i = 0;
i < 3;
i++) {
409 #define ADD_PRED(preds, px, py)\
411 preds.mvs[preds.nb][0] = px;\
412 preds.mvs[preds.nb][1] = py;\
424 const int mb_i = mb_x + mb_y * mi_ctx->
b_width;
425 int mv[2] = {x_mb, y_mb};
465 if (mb_y > 0 && mb_x + 1 < mi_ctx->
b_width)
469 if (preds[0].nb == 4) {
470 me_ctx->
pred_x =
mid_pred(preds[0].mvs[1][0], preds[0].mvs[2][0], preds[0].mvs[3][0]);
471 me_ctx->
pred_y =
mid_pred(preds[0].mvs[1][1], preds[0].mvs[2][1], preds[0].mvs[3][1]);
472 }
else if (preds[0].nb == 3) {
473 me_ctx->
pred_x =
mid_pred(0, preds[0].mvs[1][0], preds[0].mvs[2][0]);
474 me_ctx->
pred_y =
mid_pred(0, preds[0].mvs[1][1], preds[0].mvs[2][1]);
475 }
else if (preds[0].nb == 2) {
499 if (mb_x + 1 < mi_ctx->
b_width)
508 mi_ctx->
mv_table[0][mb_i][dir][0] =
mv[0] - x_mb;
509 mi_ctx->
mv_table[0][mb_i][dir][1] =
mv[1] - y_mb;
520 ADD_PRED(preds[0], blocks[mb_i - 1].mvs[dir][0], blocks[mb_i - 1].mvs[dir][1]);
527 if (mb_x + 1 < mi_ctx->
b_width)
535 if (preds[0].nb == 4) {
536 me_ctx->
pred_x =
mid_pred(preds[0].mvs[1][0], preds[0].mvs[2][0], preds[0].mvs[3][0]);
537 me_ctx->
pred_y =
mid_pred(preds[0].mvs[1][1], preds[0].mvs[2][1], preds[0].mvs[3][1]);
538 }
else if (preds[0].nb == 3) {
539 me_ctx->
pred_x =
mid_pred(0, preds[0].mvs[1][0], preds[0].mvs[2][0]);
540 me_ctx->
pred_y =
mid_pred(0, preds[0].mvs[1][1], preds[0].mvs[2][1]);
541 }
else if (preds[0].nb == 2) {
554 block->mvs[dir][0] =
mv[0] - x_mb;
555 block->mvs[dir][1] =
mv[1] - y_mb;
563 for (mb_y = 0; mb_y < mi_ctx->
b_height; mb_y++)
564 for (mb_x = 0; mb_x < mi_ctx->
b_width; mb_x++) {
570 block->mvs[0][0] = 0;
571 block->mvs[0][1] = 0;
574 for (mb_y = 0; mb_y < mi_ctx->
b_height; mb_y++)
575 for (mb_x = 0; mb_x < mi_ctx->
b_width; mb_x++)
582 uint64_t cost_sb, cost_old;
590 cost_old = me_ctx->
get_cost(me_ctx, x_mb, y_mb, x_mb +
block->mvs[0][0], y_mb +
block->mvs[0][1]);
606 for (y = 0; y < 2; y++)
607 for (x = 0; x < 2; x++) {
609 int mv[2] = {x_mb +
block->mvs[0][0], y_mb +
block->mvs[0][1]};
611 me_ctx->
mb_size = 1 << (n - 1);
623 if (cost_sb < cost_old / 4) {
624 sb->
mvs[0][0] = mv_x;
625 sb->
mvs[0][1] = mv_y;
628 if (
ret =
var_size_bme(mi_ctx, sb, x_mb + (x << (n - 1)), y_mb + (y << (n - 1)), n - 1))
643 int changed,
c, c_max = 0;
644 int mb_x, mb_y, x, y;
645 int mv_x, mv_y, avg_x, avg_y, dx, dy;
648 Cluster *cluster, *cluster_new;
652 for (mb_y = 0; mb_y < mi_ctx->
b_height; mb_y++)
653 for (mb_x = 0; mb_x < mi_ctx->
b_width; mb_x++) {
657 mv_x =
block->mvs[0][0];
658 mv_y =
block->mvs[0][1];
663 avg_x = cluster->
sum[0] / cluster->
nb;
664 avg_y = cluster->
sum[1] / cluster->
nb;
670 for (d = 1; d < 5; d++)
688 cluster_new->
sum[0] += mv_x;
689 cluster_new->
sum[1] += mv_y;
690 cluster->
sum[0] -= mv_x;
691 cluster->
sum[1] -= mv_y;
704 for (mb_y = 0; mb_y < mi_ctx->
b_height; mb_y++)
705 for (mb_x = 0; mb_x < mi_ctx->
b_width; mb_x++) {
712 if ((x - mb_x) && (y - mb_y) || !dx && !dy)
715 if (!mb_x || !mb_y || mb_x == mi_ctx->
b_width - 1 || mb_y == mi_ctx->
b_height - 1)
739 frame_tmp = mi_ctx->
frames[0];
754 for (dir = 0; dir < 2; dir++) {
759 for (mb_y = 0; mb_y < mi_ctx->
b_height; mb_y++)
760 for (mb_x = 0; mb_x < mi_ctx->
b_width; mb_x++)
780 for (mb_y = 0; mb_y < mi_ctx->
b_height; mb_y++)
781 for (mb_x = 0; mb_x < mi_ctx->
b_width; mb_x++) {
798 for (mb_y = 0; mb_y < mi_ctx->
b_height; mb_y++)
799 for (mb_x = 0; mb_x < mi_ctx->
b_width; mb_x++) {
829 mi_ctx->
sad(p1, linesize1, p2, linesize2,
input->w,
input->h, &sad);
841 #define ADD_PIXELS(b_weight, mv_x, mv_y)\
843 if (!b_weight || pixel_refs->nb + 1 >= NB_PIXEL_MVS)\
845 pixel_refs->refs[pixel_refs->nb] = 1;\
846 pixel_weights->weights[pixel_refs->nb] = b_weight * (ALPHA_MAX - alpha);\
847 pixel_mvs->mvs[pixel_refs->nb][0] = av_clip((mv_x * alpha) / ALPHA_MAX, x_min, x_max);\
848 pixel_mvs->mvs[pixel_refs->nb][1] = av_clip((mv_y * alpha) / ALPHA_MAX, y_min, y_max);\
850 pixel_refs->refs[pixel_refs->nb] = 2;\
851 pixel_weights->weights[pixel_refs->nb] = b_weight * alpha;\
852 pixel_mvs->mvs[pixel_refs->nb][0] = av_clip(-mv_x * (ALPHA_MAX - alpha) / ALPHA_MAX, x_min, x_max);\
853 pixel_mvs->mvs[pixel_refs->nb][1] = av_clip(-mv_y * (ALPHA_MAX - alpha) / ALPHA_MAX, y_min, y_max);\
864 for (y = 0; y <
height; y++)
865 for (x = 0; x <
width; x++)
868 for (dir = 0; dir < 2; dir++)
869 for (mb_y = 0; mb_y < mi_ctx->
b_height; mb_y++)
870 for (mb_x = 0; mb_x < mi_ctx->
b_width; mb_x++) {
874 int start_x, start_y;
875 int startc_x, startc_y, endc_x, endc_y;
890 for (y = startc_y; y < endc_y; y++) {
892 int y_max =
height - y - 1;
893 for (x = startc_x; x < endc_x; x++) {
895 int x_max =
width - x - 1;
911 for (plane = 0; plane < mi_ctx->
nb_planes; plane++) {
914 int chroma = plane == 1 || plane == 2;
916 for (y = 0; y <
height; y++)
917 for (x = 0; x <
width; x++) {
925 for (
i = 0;
i < pixel_refs->
nb;
i++)
926 weight_sum += pixel_weights->
weights[
i];
928 if (!weight_sum || !pixel_refs->
nb) {
930 pixel_refs->
refs[0] = 1;
931 pixel_mvs->
mvs[0][0] = 0;
932 pixel_mvs->
mvs[0][1] = 0;
934 pixel_refs->
refs[1] = 2;
935 pixel_mvs->
mvs[1][0] = 0;
936 pixel_mvs->
mvs[1][1] = 0;
942 for (
i = 0;
i < pixel_refs->
nb;
i++) {
948 x_mv = x + pixel_mvs->
mvs[
i][0];
949 y_mv = y + pixel_mvs->
mvs[
i][1];
952 val += pixel_weights->
weights[
i] *
frame->avf->data[plane][x_mv + y_mv *
frame->avf->linesize[plane]];
971 for (sb_y = 0; sb_y < 2; sb_y++)
972 for (sb_x = 0; sb_x < 2; sb_x++) {
976 var_size_bmc(mi_ctx, sb, x_mb + (sb_x << (n - 1)), y_mb + (sb_y << (n - 1)), n - 1,
alpha);
979 int mv_x = sb->
mvs[0][0] * 2;
980 int mv_y = sb->
mvs[0][1] * 2;
982 int start_x = x_mb + (sb_x << (n - 1));
983 int start_y = y_mb + (sb_y << (n - 1));
984 int end_x = start_x + (1 << (n - 1));
985 int end_y = start_y + (1 << (n - 1));
987 for (y = start_y; y < end_y; y++) {
989 int y_max =
height - y - 1;
990 for (x = start_x; x < end_x; x++) {
992 int x_max =
width - x - 1;
1014 int mv_x =
block->mvs[0][0] * 2;
1015 int mv_y =
block->mvs[0][1] * 2;
1016 int start_x, start_y;
1017 int startc_x, startc_y, endc_x, endc_y;
1021 for (nb_x =
FFMAX(0, mb_x - 1); nb_x <
FFMIN(mb_x + 2, mi_ctx->
b_width); nb_x++) {
1025 if (nb_x - mb_x || nb_y - mb_y)
1026 sbads[nb_x - mb_x + 1 + (nb_y - mb_y + 1) * 3] =
get_sbad(&mi_ctx->
me_ctx, x_nb, y_nb, x_nb +
block->mvs[0][0], y_nb +
block->mvs[0][1]);
1037 for (y = startc_y; y < endc_y; y++) {
1039 int y_max =
height - y - 1;
1040 for (x = startc_x; x < endc_x; x++) {
1042 int x_max =
width - x - 1;
1049 nb_x = (((x - start_x) >> (mi_ctx->
log2_mb_size - 1)) * 2 - 3) / 2;
1050 nb_y = (((y - start_y) >> (mi_ctx->
log2_mb_size - 1)) * 2 - 3) / 2;
1053 uint64_t sbad = sbads[nb_x + 1 + (nb_y + 1) * 3];
1056 if (sbad && sbad != UINT64_MAX && nb->sbad != UINT64_MAX) {
1058 obmc_weight = obmc_weight * phi /
ALPHA_MAX;
1106 for (plane = 0; plane < mi_ctx->
nb_planes; plane++) {
1110 if (plane == 1 || plane == 2) {
1115 for (y = 0; y <
height; y++) {
1116 for (x = 0; x <
width; x++) {
1117 avf_out->
data[plane][x + y * avf_out->
linesize[plane]] =
1139 for (mb_x = 0; mb_x < mi_ctx->
b_width; mb_x++) {
1224 for (m = 0; m < mi_ctx->
b_count; m++)
1234 for (
i = 0;
i < 3;
i++)
1256 .
name =
"minterpolate",
1259 .priv_class = &minterpolate_class,