2 * Copyright (c) 2016 Davinder Singh (DSM_) <ds.mudhar<@gmail.com>
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "motion_estimation.h"
23 static const int8_t sqr1[8][2] = {{ 0,-1}, { 0, 1}, {-1, 0}, { 1, 0}, {-1,-1}, {-1, 1}, { 1,-1}, { 1, 1}};
24 static const int8_t dia1[4][2] = {{-1, 0}, { 0,-1}, { 1, 0}, { 0, 1}};
25 static const int8_t dia2[8][2] = {{-2, 0}, {-1,-1}, { 0,-2}, { 1,-1}, { 2, 0}, { 1, 1}, { 0, 2}, {-1, 1}};
26 static const int8_t hex2[6][2] = {{-2, 0}, {-1,-2}, {-1, 2}, { 1,-2}, { 1, 2}, { 2, 0}};
27 static const int8_t hex4[16][2] = {{-4,-2}, {-4,-1}, {-4, 0}, {-4, 1}, {-4, 2},
28 { 4,-2}, { 4,-1}, { 4, 0}, { 4, 1}, { 4, 2},
29 {-2, 3}, { 0, 4}, { 2, 3}, {-2,-3}, { 0,-4}, { 2,-3}};
31 #define COST_MV(x, y)\
33 cost = me_ctx->get_cost(me_ctx, x_mb, y_mb, x, y);\
34 if (cost < cost_min) {\
41 #define COST_P_MV(x, y)\
42 if (x >= x_min && x <= x_max && y >= y_min && y <= y_max)\
45 void ff_me_init_context(AVMotionEstContext *me_ctx, int mb_size, int search_param,
46 int width, int height, int x_min, int x_max, int y_min, int y_max)
48 me_ctx->width = width;
49 me_ctx->height = height;
50 me_ctx->mb_size = mb_size;
51 me_ctx->search_param = search_param;
52 me_ctx->get_cost = &ff_me_cmp_sad;
53 me_ctx->x_min = x_min;
54 me_ctx->x_max = x_max;
55 me_ctx->y_min = y_min;
56 me_ctx->y_max = y_max;
59 uint64_t ff_me_cmp_sad(AVMotionEstContext *me_ctx, int x_mb, int y_mb, int x_mv, int y_mv)
61 const int linesize = me_ctx->linesize;
62 uint8_t *data_ref = me_ctx->data_ref;
63 uint8_t *data_cur = me_ctx->data_cur;
67 data_ref += y_mv * linesize;
68 data_cur += y_mb * linesize;
70 for (j = 0; j < me_ctx->mb_size; j++)
71 for (i = 0; i < me_ctx->mb_size; i++)
72 sad += FFABS(data_ref[x_mv + i + j * linesize] - data_cur[x_mb + i + j * linesize]);
77 uint64_t ff_me_search_esa(AVMotionEstContext *me_ctx, int x_mb, int y_mb, int *mv)
80 int x_min = FFMAX(me_ctx->x_min, x_mb - me_ctx->search_param);
81 int y_min = FFMAX(me_ctx->y_min, y_mb - me_ctx->search_param);
82 int x_max = FFMIN(x_mb + me_ctx->search_param, me_ctx->x_max);
83 int y_max = FFMIN(y_mb + me_ctx->search_param, me_ctx->y_max);
84 uint64_t cost, cost_min;
86 if (!(cost_min = me_ctx->get_cost(me_ctx, x_mb, y_mb, x_mb, y_mb)))
89 for (y = y_min; y <= y_max; y++)
90 for (x = x_min; x <= x_max; x++)
96 uint64_t ff_me_search_tss(AVMotionEstContext *me_ctx, int x_mb, int y_mb, int *mv)
99 int x_min = FFMAX(me_ctx->x_min, x_mb - me_ctx->search_param);
100 int y_min = FFMAX(me_ctx->y_min, y_mb - me_ctx->search_param);
101 int x_max = FFMIN(x_mb + me_ctx->search_param, me_ctx->x_max);
102 int y_max = FFMIN(y_mb + me_ctx->search_param, me_ctx->y_max);
103 uint64_t cost, cost_min;
104 int step = ROUNDED_DIV(me_ctx->search_param, 2);
110 if (!(cost_min = me_ctx->get_cost(me_ctx, x_mb, y_mb, x_mb, y_mb)))
117 for (i = 0; i < 8; i++)
118 COST_P_MV(x + sqr1[i][0] * step, y + sqr1[i][1] * step);
127 uint64_t ff_me_search_tdls(AVMotionEstContext *me_ctx, int x_mb, int y_mb, int *mv)
130 int x_min = FFMAX(me_ctx->x_min, x_mb - me_ctx->search_param);
131 int y_min = FFMAX(me_ctx->y_min, y_mb - me_ctx->search_param);
132 int x_max = FFMIN(x_mb + me_ctx->search_param, me_ctx->x_max);
133 int y_max = FFMIN(y_mb + me_ctx->search_param, me_ctx->y_max);
134 uint64_t cost, cost_min;
135 int step = ROUNDED_DIV(me_ctx->search_param, 2);
141 if (!(cost_min = me_ctx->get_cost(me_ctx, x_mb, y_mb, x_mb, y_mb)))
148 for (i = 0; i < 4; i++)
149 COST_P_MV(x + dia1[i][0] * step, y + dia1[i][1] * step);
151 if (x == mv[0] && y == mv[1])
159 uint64_t ff_me_search_ntss(AVMotionEstContext *me_ctx, int x_mb, int y_mb, int *mv)
162 int x_min = FFMAX(me_ctx->x_min, x_mb - me_ctx->search_param);
163 int y_min = FFMAX(me_ctx->y_min, y_mb - me_ctx->search_param);
164 int x_max = FFMIN(x_mb + me_ctx->search_param, me_ctx->x_max);
165 int y_max = FFMIN(y_mb + me_ctx->search_param, me_ctx->y_max);
166 uint64_t cost, cost_min;
167 int step = ROUNDED_DIV(me_ctx->search_param, 2);
174 if (!(cost_min = me_ctx->get_cost(me_ctx, x_mb, y_mb, x_mb, y_mb)))
181 for (i = 0; i < 8; i++)
182 COST_P_MV(x + sqr1[i][0] * step, y + sqr1[i][1] * step);
184 /* addition to TSS in NTSS */
187 for (i = 0; i < 8; i++)
188 COST_P_MV(x + sqr1[i][0], y + sqr1[i][1]);
190 if (x == mv[0] && y == mv[1])
193 if (FFABS(x - mv[0]) <= 1 && FFABS(y - mv[1]) <= 1) {
197 for (i = 0; i < 8; i++)
198 COST_P_MV(x + sqr1[i][0], y + sqr1[i][1]);
212 uint64_t ff_me_search_fss(AVMotionEstContext *me_ctx, int x_mb, int y_mb, int *mv)
215 int x_min = FFMAX(me_ctx->x_min, x_mb - me_ctx->search_param);
216 int y_min = FFMAX(me_ctx->y_min, y_mb - me_ctx->search_param);
217 int x_max = FFMIN(x_mb + me_ctx->search_param, me_ctx->x_max);
218 int y_max = FFMIN(y_mb + me_ctx->search_param, me_ctx->y_max);
219 uint64_t cost, cost_min;
226 if (!(cost_min = me_ctx->get_cost(me_ctx, x_mb, y_mb, x_mb, y_mb)))
233 for (i = 0; i < 8; i++)
234 COST_P_MV(x + sqr1[i][0] * step, y + sqr1[i][1] * step);
236 if (x == mv[0] && y == mv[1])
244 uint64_t ff_me_search_ds(AVMotionEstContext *me_ctx, int x_mb, int y_mb, int *mv)
247 int x_min = FFMAX(me_ctx->x_min, x_mb - me_ctx->search_param);
248 int y_min = FFMAX(me_ctx->y_min, y_mb - me_ctx->search_param);
249 int x_max = FFMIN(x_mb + me_ctx->search_param, me_ctx->x_max);
250 int y_max = FFMIN(y_mb + me_ctx->search_param, me_ctx->y_max);
251 uint64_t cost, cost_min;
253 av_unused int dir_x, dir_y;
255 if (!(cost_min = me_ctx->get_cost(me_ctx, x_mb, y_mb, x_mb, y_mb)))
266 for (i = 0; i < 8; i++)
267 COST_P_MV(x + dia2[i][0], y + dia2[i][1]);
269 /* this version skips previously examined 3 or 5 locations based on prev origin */
272 if (dir_x <= 0 && dir_y <= 0)
273 COST_P_MV(x - 1, y - 1);
276 if (dir_x >= 0 && dir_y <= 0)
277 COST_P_MV(x + 1, y - 1);
280 if (dir_x >= 0 && dir_y >= 0)
281 COST_P_MV(x + 1, y + 1);
284 if (dir_x <= 0 && dir_y >= 0)
285 COST_P_MV(x - 1, y + 1);
291 } while (x != mv[0] || y != mv[1]);
293 for (i = 0; i < 4; i++)
294 COST_P_MV(x + dia1[i][0], y + dia1[i][1]);
299 uint64_t ff_me_search_hexbs(AVMotionEstContext *me_ctx, int x_mb, int y_mb, int *mv)
302 int x_min = FFMAX(me_ctx->x_min, x_mb - me_ctx->search_param);
303 int y_min = FFMAX(me_ctx->y_min, y_mb - me_ctx->search_param);
304 int x_max = FFMIN(x_mb + me_ctx->search_param, me_ctx->x_max);
305 int y_max = FFMIN(y_mb + me_ctx->search_param, me_ctx->y_max);
306 uint64_t cost, cost_min;
309 if (!(cost_min = me_ctx->get_cost(me_ctx, x_mb, y_mb, x_mb, y_mb)))
316 for (i = 0; i < 6; i++)
317 COST_P_MV(x + hex2[i][0], y + hex2[i][1]);
319 } while (x != mv[0] || y != mv[1]);
321 for (i = 0; i < 4; i++)
322 COST_P_MV(x + dia1[i][0], y + dia1[i][1]);
327 /* two subsets of predictors are used
328 me->pred_x|y is set to median of current frame's left, top, top-right
329 set 1: me->preds[0] has: (0, 0), left, top, top-right, collocated block in prev frame
330 set 2: me->preds[1] has: accelerator mv, top, left, right, bottom adj mb of prev frame
332 uint64_t ff_me_search_epzs(AVMotionEstContext *me_ctx, int x_mb, int y_mb, int *mv)
335 int x_min = FFMAX(me_ctx->x_min, x_mb - me_ctx->search_param);
336 int y_min = FFMAX(me_ctx->y_min, y_mb - me_ctx->search_param);
337 int x_max = FFMIN(x_mb + me_ctx->search_param, me_ctx->x_max);
338 int y_max = FFMIN(y_mb + me_ctx->search_param, me_ctx->y_max);
339 uint64_t cost, cost_min;
342 AVMotionEstPredictor *preds = me_ctx->preds;
344 cost_min = UINT64_MAX;
346 COST_P_MV(x_mb + me_ctx->pred_x, y_mb + me_ctx->pred_y);
348 for (i = 0; i < preds[0].nb; i++)
349 COST_P_MV(x_mb + preds[0].mvs[i][0], y_mb + preds[0].mvs[i][1]);
351 for (i = 0; i < preds[1].nb; i++)
352 COST_P_MV(x_mb + preds[1].mvs[i][0], y_mb + preds[1].mvs[i][1]);
358 for (i = 0; i < 4; i++)
359 COST_P_MV(x + dia1[i][0], y + dia1[i][1]);
361 } while (x != mv[0] || y != mv[1]);
366 /* required predictor order: median, (0,0), left, top, top-right
367 rules when mb not available:
368 replace left with (0, 0)
369 replace top-right with top-left
370 replace top two with left
371 repeated can be skipped, if no predictors are used, set me_ctx->pred to (0,0)
373 uint64_t ff_me_search_umh(AVMotionEstContext *me_ctx, int x_mb, int y_mb, int *mv)
376 int x_min = FFMAX(me_ctx->x_min, x_mb - me_ctx->search_param);
377 int y_min = FFMAX(me_ctx->y_min, y_mb - me_ctx->search_param);
378 int x_max = FFMIN(x_mb + me_ctx->search_param, me_ctx->x_max);
379 int y_max = FFMIN(y_mb + me_ctx->search_param, me_ctx->y_max);
380 uint64_t cost, cost_min;
384 AVMotionEstPredictor *pred = &me_ctx->preds[0];
386 cost_min = UINT64_MAX;
388 COST_P_MV(x_mb + me_ctx->pred_x, y_mb + me_ctx->pred_y);
390 for (i = 0; i < pred->nb; i++)
391 COST_P_MV(x_mb + pred->mvs[i][0], y_mb + pred->mvs[i][1]);
393 // Unsymmetrical-cross Search
396 for (d = 1; d <= me_ctx->search_param; d += 2) {
399 if (d <= me_ctx->search_param / 2) {
405 // Uneven Multi-Hexagon-Grid Search
406 end_x = FFMIN(mv[0] + 2, x_max);
407 end_y = FFMIN(mv[1] + 2, y_max);
408 for (y = FFMAX(y_min, mv[1] - 2); y <= end_y; y++)
409 for (x = FFMAX(x_min, mv[0] - 2); x <= end_x; x++)
414 for (d = 1; d <= me_ctx->search_param / 4; d++)
415 for (i = 1; i < 16; i++)
416 COST_P_MV(x + hex4[i][0] * d, y + hex4[i][1] * d);
418 // Extended Hexagon-based Search
423 for (i = 0; i < 6; i++)
424 COST_P_MV(x + hex2[i][0], y + hex2[i][1]);
426 } while (x != mv[0] || y != mv[1]);
428 for (i = 0; i < 4; i++)
429 COST_P_MV(x + dia1[i][0], y + dia1[i][1]);