FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_mestimate.c
Date: 2026-09-10 12:58:17
Exec Total Coverage
Lines: 70 165 42.4%
Functions: 5 5 100.0%
Branches: 35 126 27.8%

Line Branch Exec Source
1 /**
2 * Copyright (c) 2016 Davinder Singh (DSM_) <ds.mudhar<@gmail.com>
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
21 #include "motion_estimation.h"
22 #include "libavcodec/mathops.h"
23 #include "libavutil/common.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/motion_vector.h"
27 #include "avfilter.h"
28 #include "filters.h"
29
30 typedef struct MEContext {
31 const AVClass *class;
32 AVMotionEstContext me_ctx;
33 int method; ///< motion estimation method
34
35 int mb_size; ///< macroblock size
36 int search_param; ///< search parameter
37 int b_width, b_height, b_count;
38 int log2_mb_size;
39
40 AVFrame *prev, *cur, *next;
41
42 int (*mv_table[3])[2][2]; ///< motion vectors of current & prev 2 frames
43 } MEContext;
44
45 #define OFFSET(x) offsetof(MEContext, x)
46 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
47 #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, .unit = u }
48
49 static const AVOption mestimate_options[] = {
50 { "method", "motion estimation method", OFFSET(method), AV_OPT_TYPE_INT, {.i64 = AV_ME_METHOD_ESA}, AV_ME_METHOD_ESA, AV_ME_METHOD_UMH, FLAGS, .unit = "method" },
51 CONST("esa", "exhaustive search", AV_ME_METHOD_ESA, "method"),
52 CONST("tss", "three step search", AV_ME_METHOD_TSS, "method"),
53 CONST("tdls", "two dimensional logarithmic search", AV_ME_METHOD_TDLS, "method"),
54 CONST("ntss", "new three step search", AV_ME_METHOD_NTSS, "method"),
55 CONST("fss", "four step search", AV_ME_METHOD_FSS, "method"),
56 CONST("ds", "diamond search", AV_ME_METHOD_DS, "method"),
57 CONST("hexbs", "hexagon-based search", AV_ME_METHOD_HEXBS, "method"),
58 CONST("epzs", "enhanced predictive zonal search", AV_ME_METHOD_EPZS, "method"),
59 CONST("umh", "uneven multi-hexagon search", AV_ME_METHOD_UMH, "method"),
60 { "mb_size", "macroblock size", OFFSET(mb_size), AV_OPT_TYPE_INT, {.i64 = 16}, 8, INT_MAX, FLAGS },
61 { "search_param", "search parameter", OFFSET(search_param), AV_OPT_TYPE_INT, {.i64 = 7}, 4, INT_MAX, FLAGS },
62 { NULL }
63 };
64
65 AVFILTER_DEFINE_CLASS(mestimate);
66
67 static const enum AVPixelFormat pix_fmts[] = {
68 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
69 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
70 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
71 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
72 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
73 AV_PIX_FMT_YUVJ411P,
74 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
75 AV_PIX_FMT_GRAY8,
76 AV_PIX_FMT_NONE
77 };
78
79 2 static int config_input(AVFilterLink *inlink)
80 {
81 2 MEContext *s = inlink->dst->priv;
82 int i;
83
84 2 s->log2_mb_size = av_ceil_log2_c(s->mb_size);
85 2 s->mb_size = 1 << s->log2_mb_size;
86
87 2 s->b_width = inlink->w >> s->log2_mb_size;
88 2 s->b_height = inlink->h >> s->log2_mb_size;
89 2 s->b_count = s->b_width * s->b_height;
90
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->b_count == 0)
92 return AVERROR(EINVAL);
93
94
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (i = 0; i < 3; i++) {
95 6 s->mv_table[i] = av_calloc(s->b_count, sizeof(*s->mv_table[0]));
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!s->mv_table[i])
97 return AVERROR(ENOMEM);
98 }
99
100 2 ff_me_init_context(&s->me_ctx, s->mb_size, s->search_param, inlink->w, inlink->h, 0, (s->b_width - 1) << s->log2_mb_size, 0, (s->b_height - 1) << s->log2_mb_size);
101
102 2 return 0;
103 }
104
105 242440 static void add_mv_data(AVMotionVector *mv, int mb_size,
106 int x, int y, int x_mv, int y_mv, int dir)
107 {
108 242440 mv->w = mb_size;
109 242440 mv->h = mb_size;
110 242440 mv->dst_x = x + (mb_size >> 1);
111 242440 mv->dst_y = y + (mb_size >> 1);
112 242440 mv->src_x = x_mv + (mb_size >> 1);
113 242440 mv->src_y = y_mv + (mb_size >> 1);
114
2/2
✓ Branch 0 taken 114840 times.
✓ Branch 1 taken 127600 times.
242440 mv->source = dir ? 1 : -1;
115 242440 mv->flags = 0;
116 242440 }
117
118 #define SEARCH_MV(method)\
119 do {\
120 for (mb_y = 0; mb_y < s->b_height; mb_y++)\
121 for (mb_x = 0; mb_x < s->b_width; mb_x++) {\
122 const int x_mb = mb_x << s->log2_mb_size;\
123 const int y_mb = mb_y << s->log2_mb_size;\
124 int mv[2] = {x_mb, y_mb};\
125 ff_me_search_##method(me_ctx, x_mb, y_mb, mv);\
126 add_mv_data(((AVMotionVector *) sd->data) + mv_count++, me_ctx->mb_size, x_mb, y_mb, mv[0], mv[1], dir);\
127 }\
128 } while (0)
129
130 #define ADD_PRED(preds, px, py)\
131 do {\
132 preds.mvs[preds.nb][0] = px;\
133 preds.mvs[preds.nb][1] = py;\
134 preds.nb++;\
135 } while(0)
136
137 22 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
138 {
139 22 AVFilterContext *ctx = inlink->dst;
140 22 MEContext *s = ctx->priv;
141 22 AVMotionEstContext *me_ctx = &s->me_ctx;
142 AVFrameSideData *sd;
143 AVFrame *out;
144 int mb_x, mb_y, dir;
145 22 int32_t mv_count = 0;
146 int ret;
147
148
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
22 if (frame && frame->pts == AV_NOPTS_VALUE) {
149 ret = ff_filter_frame(ctx->outputs[0], frame);
150 return ret;
151 }
152
153 22 av_frame_free(&s->prev);
154 22 s->prev = s->cur;
155 22 s->cur = s->next;
156 22 s->next = frame;
157
158 22 s->mv_table[2] = memcpy(s->mv_table[2], s->mv_table[1], sizeof(*s->mv_table[1]) * s->b_count);
159 22 s->mv_table[1] = memcpy(s->mv_table[1], s->mv_table[0], sizeof(*s->mv_table[0]) * s->b_count);
160
161
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
22 if (!s->cur) {
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!frame)
163 return 0;
164 2 s->cur = av_frame_clone(frame);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->cur)
166 return AVERROR(ENOMEM);
167 }
168
169
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
22 if (!s->prev)
170 2 return 0;
171
172 20 out = av_frame_clone(s->cur);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!out)
174 return AVERROR(ENOMEM);
175
176 /* The last frame has no forward reference. */
177
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 const int nb_dirs = s->next ? 2 : 1;
178
179 20 sd = av_frame_new_side_data(out, AV_FRAME_DATA_MOTION_VECTORS,
180 20 nb_dirs * s->b_count * sizeof(AVMotionVector));
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!sd) {
182 av_frame_free(&out);
183 return AVERROR(ENOMEM);
184 }
185
186 20 me_ctx->data_cur = s->cur->data[0];
187 20 me_ctx->linesize = s->cur->linesize[0];
188
189
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 20 times.
58 for (dir = 0; dir < nb_dirs; dir++) {
190
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 20 times.
38 me_ctx->data_ref = (dir ? s->next : s->prev)->data[0];
191
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (s->method == AV_ME_METHOD_DS)
193 SEARCH_MV(ds);
194
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 else if (s->method == AV_ME_METHOD_ESA)
195
4/4
✓ Branch 2 taken 242440 times.
✓ Branch 3 taken 3743 times.
✓ Branch 4 taken 3743 times.
✓ Branch 5 taken 38 times.
246221 SEARCH_MV(esa);
196 else if (s->method == AV_ME_METHOD_FSS)
197 SEARCH_MV(fss);
198 else if (s->method == AV_ME_METHOD_NTSS)
199 SEARCH_MV(ntss);
200 else if (s->method == AV_ME_METHOD_TDLS)
201 SEARCH_MV(tdls);
202 else if (s->method == AV_ME_METHOD_TSS)
203 SEARCH_MV(tss);
204 else if (s->method == AV_ME_METHOD_HEXBS)
205 SEARCH_MV(hexbs);
206 else if (s->method == AV_ME_METHOD_UMH) {
207 for (mb_y = 0; mb_y < s->b_height; mb_y++)
208 for (mb_x = 0; mb_x < s->b_width; mb_x++) {
209 const int mb_i = mb_x + mb_y * s->b_width;
210 const int x_mb = mb_x << s->log2_mb_size;
211 const int y_mb = mb_y << s->log2_mb_size;
212 int mv[2] = {x_mb, y_mb};
213
214 AVMotionEstPredictor *preds = me_ctx->preds;
215 preds[0].nb = 0;
216
217 ADD_PRED(preds[0], 0, 0);
218
219 //left mb in current frame
220 if (mb_x > 0)
221 ADD_PRED(preds[0], s->mv_table[0][mb_i - 1][dir][0], s->mv_table[0][mb_i - 1][dir][1]);
222
223 if (mb_y > 0) {
224 //top mb in current frame
225 ADD_PRED(preds[0], s->mv_table[0][mb_i - s->b_width][dir][0], s->mv_table[0][mb_i - s->b_width][dir][1]);
226
227 //top-right mb in current frame
228 if (mb_x + 1 < s->b_width)
229 ADD_PRED(preds[0], s->mv_table[0][mb_i - s->b_width + 1][dir][0], s->mv_table[0][mb_i - s->b_width + 1][dir][1]);
230 //top-left mb in current frame
231 else if (mb_x > 0)
232 ADD_PRED(preds[0], s->mv_table[0][mb_i - s->b_width - 1][dir][0], s->mv_table[0][mb_i - s->b_width - 1][dir][1]);
233 }
234
235 //median predictor
236 if (preds[0].nb == 4) {
237 me_ctx->pred_x = mid_pred(preds[0].mvs[1][0], preds[0].mvs[2][0], preds[0].mvs[3][0]);
238 me_ctx->pred_y = mid_pred(preds[0].mvs[1][1], preds[0].mvs[2][1], preds[0].mvs[3][1]);
239 } else if (preds[0].nb == 3) {
240 me_ctx->pred_x = mid_pred(0, preds[0].mvs[1][0], preds[0].mvs[2][0]);
241 me_ctx->pred_y = mid_pred(0, preds[0].mvs[1][1], preds[0].mvs[2][1]);
242 } else if (preds[0].nb == 2) {
243 me_ctx->pred_x = preds[0].mvs[1][0];
244 me_ctx->pred_y = preds[0].mvs[1][1];
245 } else {
246 me_ctx->pred_x = 0;
247 me_ctx->pred_y = 0;
248 }
249
250 ff_me_search_umh(me_ctx, x_mb, y_mb, mv);
251
252 s->mv_table[0][mb_i][dir][0] = mv[0] - x_mb;
253 s->mv_table[0][mb_i][dir][1] = mv[1] - y_mb;
254 add_mv_data(((AVMotionVector *) sd->data) + mv_count++, me_ctx->mb_size, x_mb, y_mb, mv[0], mv[1], dir);
255 }
256
257 } else if (s->method == AV_ME_METHOD_EPZS) {
258
259 for (mb_y = 0; mb_y < s->b_height; mb_y++)
260 for (mb_x = 0; mb_x < s->b_width; mb_x++) {
261 const int mb_i = mb_x + mb_y * s->b_width;
262 const int x_mb = mb_x << s->log2_mb_size;
263 const int y_mb = mb_y << s->log2_mb_size;
264 int mv[2] = {x_mb, y_mb};
265
266 AVMotionEstPredictor *preds = me_ctx->preds;
267 preds[0].nb = 0;
268 preds[1].nb = 0;
269
270 ADD_PRED(preds[0], 0, 0);
271
272 //left mb in current frame
273 if (mb_x > 0)
274 ADD_PRED(preds[0], s->mv_table[0][mb_i - 1][dir][0], s->mv_table[0][mb_i - 1][dir][1]);
275
276 //top mb in current frame
277 if (mb_y > 0)
278 ADD_PRED(preds[0], s->mv_table[0][mb_i - s->b_width][dir][0], s->mv_table[0][mb_i - s->b_width][dir][1]);
279
280 //top-right mb in current frame
281 if (mb_y > 0 && mb_x + 1 < s->b_width)
282 ADD_PRED(preds[0], s->mv_table[0][mb_i - s->b_width + 1][dir][0], s->mv_table[0][mb_i - s->b_width + 1][dir][1]);
283
284 //median predictor
285 if (preds[0].nb == 4) {
286 me_ctx->pred_x = mid_pred(preds[0].mvs[1][0], preds[0].mvs[2][0], preds[0].mvs[3][0]);
287 me_ctx->pred_y = mid_pred(preds[0].mvs[1][1], preds[0].mvs[2][1], preds[0].mvs[3][1]);
288 } else if (preds[0].nb == 3) {
289 me_ctx->pred_x = mid_pred(0, preds[0].mvs[1][0], preds[0].mvs[2][0]);
290 me_ctx->pred_y = mid_pred(0, preds[0].mvs[1][1], preds[0].mvs[2][1]);
291 } else if (preds[0].nb == 2) {
292 me_ctx->pred_x = preds[0].mvs[1][0];
293 me_ctx->pred_y = preds[0].mvs[1][1];
294 } else {
295 me_ctx->pred_x = 0;
296 me_ctx->pred_y = 0;
297 }
298
299 //collocated mb in prev frame
300 ADD_PRED(preds[0], s->mv_table[1][mb_i][dir][0], s->mv_table[1][mb_i][dir][1]);
301
302 //accelerator motion vector of collocated block in prev frame
303 ADD_PRED(preds[1], s->mv_table[1][mb_i][dir][0] + (s->mv_table[1][mb_i][dir][0] - s->mv_table[2][mb_i][dir][0]),
304 s->mv_table[1][mb_i][dir][1] + (s->mv_table[1][mb_i][dir][1] - s->mv_table[2][mb_i][dir][1]));
305
306 //left mb in prev frame
307 if (mb_x > 0)
308 ADD_PRED(preds[1], s->mv_table[1][mb_i - 1][dir][0], s->mv_table[1][mb_i - 1][dir][1]);
309
310 //top mb in prev frame
311 if (mb_y > 0)
312 ADD_PRED(preds[1], s->mv_table[1][mb_i - s->b_width][dir][0], s->mv_table[1][mb_i - s->b_width][dir][1]);
313
314 //right mb in prev frame
315 if (mb_x + 1 < s->b_width)
316 ADD_PRED(preds[1], s->mv_table[1][mb_i + 1][dir][0], s->mv_table[1][mb_i + 1][dir][1]);
317
318 //bottom mb in prev frame
319 if (mb_y + 1 < s->b_height)
320 ADD_PRED(preds[1], s->mv_table[1][mb_i + s->b_width][dir][0], s->mv_table[1][mb_i + s->b_width][dir][1]);
321
322 ff_me_search_epzs(me_ctx, x_mb, y_mb, mv);
323
324 s->mv_table[0][mb_i][dir][0] = mv[0] - x_mb;
325 s->mv_table[0][mb_i][dir][1] = mv[1] - y_mb;
326 add_mv_data(((AVMotionVector *) sd->data) + mv_count++, s->mb_size, x_mb, y_mb, mv[0], mv[1], dir);
327 }
328 }
329 }
330
331 20 return ff_filter_frame(ctx->outputs[0], out);
332 }
333
334 26 static int request_frame(AVFilterLink *outlink)
335 {
336 26 AVFilterContext *ctx = outlink->src;
337 26 MEContext *s = ctx->priv;
338 int ret;
339
340 26 ret = ff_request_frame(ctx->inputs[0]);
341
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
26 if (ret == AVERROR_EOF && s->next)
342 2 ret = filter_frame(ctx->inputs[0], NULL);
343
344 26 return ret;
345 }
346
347 2 static av_cold void uninit(AVFilterContext *ctx)
348 {
349 2 MEContext *s = ctx->priv;
350 int i;
351
352 2 av_frame_free(&s->prev);
353 2 av_frame_free(&s->cur);
354 2 av_frame_free(&s->next);
355
356
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (i = 0; i < 3; i++)
357 6 av_freep(&s->mv_table[i]);
358 2 }
359
360 static const AVFilterPad mestimate_inputs[] = {
361 {
362 .name = "default",
363 .type = AVMEDIA_TYPE_VIDEO,
364 .filter_frame = filter_frame,
365 .config_props = config_input,
366 },
367 };
368
369 static const AVFilterPad mestimate_outputs[] = {
370 {
371 .name = "default",
372 .type = AVMEDIA_TYPE_VIDEO,
373 .request_frame = request_frame,
374 },
375 };
376
377 const FFFilter ff_vf_mestimate = {
378 .p.name = "mestimate",
379 .p.description = NULL_IF_CONFIG_SMALL("Generate motion vectors."),
380 .p.priv_class = &mestimate_class,
381 .p.flags = AVFILTER_FLAG_METADATA_ONLY,
382 .priv_size = sizeof(MEContext),
383 .uninit = uninit,
384 FILTER_INPUTS(mestimate_inputs),
385 FILTER_OUTPUTS(mestimate_outputs),
386 FILTER_PIXFMTS_ARRAY(pix_fmts),
387 };
388