FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_crop.c
Date: 2026-10-30 17:37:53
Exec Total Coverage
Lines: 122 178 68.5%
Functions: 7 8 87.5%
Branches: 37 84 44.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2007 Bobby Bingham
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 /**
22 * @file
23 * video crop filter
24 */
25
26 #include <math.h>
27 #include <stdio.h>
28
29 #include "avfilter.h"
30 #include "filters.h"
31 #include "formats.h"
32 #include "video.h"
33 #include "libavutil/eval.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/opt.h"
39
40 static const char *const var_names[] = {
41 "in_w", "iw", ///< width of the input video
42 "in_h", "ih", ///< height of the input video
43 "out_w", "ow", ///< width of the cropped video
44 "out_h", "oh", ///< height of the cropped video
45 "a",
46 "sar",
47 "dar",
48 "hsub",
49 "vsub",
50 "x",
51 "y",
52 "n", ///< number of frame
53 "t", ///< timestamp expressed in seconds
54 NULL
55 };
56
57 enum var_name {
58 VAR_IN_W, VAR_IW,
59 VAR_IN_H, VAR_IH,
60 VAR_OUT_W, VAR_OW,
61 VAR_OUT_H, VAR_OH,
62 VAR_A,
63 VAR_SAR,
64 VAR_DAR,
65 VAR_HSUB,
66 VAR_VSUB,
67 VAR_X,
68 VAR_Y,
69 VAR_N,
70 VAR_T,
71 VAR_VARS_NB
72 };
73
74 typedef struct CropContext {
75 const AVClass *class;
76 int x; ///< x offset of the non-cropped area with respect to the input area
77 int y; ///< y offset of the non-cropped area with respect to the input area
78 int w; ///< width of the cropped area
79 int h; ///< height of the cropped area
80 int in_w, in_h; ///< input size the cropped area was computed for
81
82 AVRational out_sar; ///< output sample aspect ratio
83 int keep_aspect; ///< keep display aspect ratio when cropping
84 int exact; ///< exact cropping, for subsampled formats
85
86 int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
87 int hsub, vsub; ///< chroma subsampling
88 char *x_expr, *y_expr, *w_expr, *h_expr;
89 AVExpr *x_pexpr, *y_pexpr; /* parsed expressions for x and y */
90 double var_values[VAR_VARS_NB];
91 } CropContext;
92
93 220 static int query_formats(const AVFilterContext *ctx,
94 AVFilterFormatsConfig **cfg_in,
95 AVFilterFormatsConfig **cfg_out)
96 {
97 220 int reject_flags = AV_PIX_FMT_FLAG_BITSTREAM | FF_PIX_FMT_FLAG_SW_FLAT_SUB;
98
99 220 return ff_set_common_formats2(ctx, cfg_in, cfg_out,
100 ff_formats_pixdesc_filter(0, reject_flags));
101 }
102
103 430 static av_cold void uninit(AVFilterContext *ctx)
104 {
105 430 CropContext *s = ctx->priv;
106
107 430 av_expr_free(s->x_pexpr);
108 430 s->x_pexpr = NULL;
109 430 av_expr_free(s->y_pexpr);
110 430 s->y_pexpr = NULL;
111 430 }
112
113 4610 static inline int normalize_double(int *n, double d)
114 {
115 4610 int ret = 0;
116
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4610 times.
4610 if (isnan(d)) {
118 ✗ ret = AVERROR(EINVAL);
119
2/4
✓ Branch 0 taken 4610 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4610 times.
4610 } else if (d > INT_MAX || d < INT_MIN) {
120 ✗ *n = d > INT_MAX ? INT_MAX : INT_MIN;
121 ✗ ret = AVERROR(EINVAL);
122 } else
123 4610 *n = lrint(d);
124
125 4610 return ret;
126 }
127
128 219 static int config_crop_area(AVFilterLink *link)
129 {
130 219 AVFilterContext *ctx = link->dst;
131 219 CropContext *s = ctx->priv;
132 219 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(link->format);
133 int ret;
134 const char *expr;
135 double res;
136
137 219 s->var_values[VAR_IN_W] = s->var_values[VAR_IW] = ctx->inputs[0]->w;
138 219 s->var_values[VAR_IN_H] = s->var_values[VAR_IH] = ctx->inputs[0]->h;
139 219 s->var_values[VAR_A] = (float) link->w / link->h;
140
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 213 times.
219 s->var_values[VAR_SAR] = link->sample_aspect_ratio.num ? av_q2d(link->sample_aspect_ratio) : 1;
141 219 s->var_values[VAR_DAR] = s->var_values[VAR_A] * s->var_values[VAR_SAR];
142 219 s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
143 219 s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
144 219 s->var_values[VAR_OUT_W] = s->var_values[VAR_OW] = NAN;
145 219 s->var_values[VAR_OUT_H] = s->var_values[VAR_OH] = NAN;
146 219 s->var_values[VAR_N] = 0;
147 219 s->var_values[VAR_T] = NAN;
148
149 219 av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
150
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
219 if (pix_desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
152 ✗ s->hsub = 1;
153 ✗ s->vsub = 1;
154 } else {
155 219 s->hsub = pix_desc->log2_chroma_w;
156 219 s->vsub = pix_desc->log2_chroma_h;
157 }
158
159 219 av_expr_parse_and_eval(&res, (expr = s->w_expr),
160 219 var_names, s->var_values,
161 NULL, NULL, NULL, NULL, NULL, 0, ctx);
162 219 s->var_values[VAR_OUT_W] = s->var_values[VAR_OW] = res;
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
219 if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
164 219 var_names, s->var_values,
165 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
166 ✗ goto fail_expr;
167 219 s->var_values[VAR_OUT_H] = s->var_values[VAR_OH] = res;
168 /* evaluate again ow as it may depend on oh */
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
219 if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
170 219 var_names, s->var_values,
171 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
172 ✗ goto fail_expr;
173
174 219 s->var_values[VAR_OUT_W] = s->var_values[VAR_OW] = res;
175
2/4
✓ Branch 1 taken 219 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 219 times.
438 if (normalize_double(&s->w, s->var_values[VAR_OUT_W]) < 0 ||
176 219 normalize_double(&s->h, s->var_values[VAR_OUT_H]) < 0) {
177 ✗ av_log(ctx, AV_LOG_ERROR,
178 "Too big value or invalid expression for out_w/ow or out_h/oh. "
179 "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
180 s->w_expr, s->h_expr);
181 ✗ return AVERROR(EINVAL);
182 }
183
184
1/2
✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
219 if (!s->exact) {
185 219 s->w &= ~((1 << s->hsub) - 1);
186 219 s->h &= ~((1 << s->vsub) - 1);
187 }
188
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
219 if (s->keep_aspect) {
190 ✗ AVRational dar = av_mul_q(link->sample_aspect_ratio,
191 ✗ (AVRational){ link->w, link->h });
192 ✗ av_reduce(&s->out_sar.num, &s->out_sar.den,
193 ✗ (int64_t)dar.num * s->h, (int64_t)dar.den * s->w, INT_MAX);
194 } else
195 219 s->out_sar = link->sample_aspect_ratio;
196
197 219 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d -> w:%d h:%d sar:%d/%d\n",
198 link->w, link->h, link->sample_aspect_ratio.num, link->sample_aspect_ratio.den,
199 s->w, s->h, s->out_sar.num, s->out_sar.den);
200
201
2/4
✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 219 times.
✗ Branch 3 not taken.
219 if (s->w <= 0 || s->h <= 0 ||
202
2/4
✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 219 times.
219 s->w > link->w || s->h > link->h) {
203 ✗ av_log(ctx, AV_LOG_ERROR,
204 "Invalid too big or non positive size for width '%d' or height '%d'\n",
205 s->w, s->h);
206 ✗ return AVERROR(EINVAL);
207 }
208
209 /* set default, required in the case the first computed value for x/y is NAN */
210 219 s->x = (link->w - s->w) / 2;
211 219 s->y = (link->h - s->h) / 2;
212
1/2
✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
219 if (!s->exact) {
213 219 s->x &= ~((1 << s->hsub) - 1);
214 219 s->y &= ~((1 << s->vsub) - 1);
215 }
216 219 s->in_w = link->w;
217 219 s->in_h = link->h;
218 219 return 0;
219
220 ✗ fail_expr:
221 ✗ av_log(ctx, AV_LOG_ERROR, "Error when evaluating the expression '%s'\n", expr);
222 ✗ return ret;
223 }
224
225 219 static int config_input(AVFilterLink *link)
226 {
227 219 CropContext *s = link->dst->priv;
228 219 AVExpr *x_pexpr = NULL, *y_pexpr = NULL;
229
230 219 s->var_values[VAR_X] = NAN;
231 219 s->var_values[VAR_Y] = NAN;
232
1/2
✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
219 if (av_expr_parse(&x_pexpr, s->x_expr, var_names,
233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 219 times.
438 NULL, NULL, NULL, NULL, 0, link->dst) < 0 ||
234 219 av_expr_parse(&y_pexpr, s->y_expr, var_names,
235 219 NULL, NULL, NULL, NULL, 0, link->dst) < 0) {
236 ✗ av_expr_free(x_pexpr);
237 ✗ return AVERROR(EINVAL);
238 }
239 219 av_expr_free(s->x_pexpr);
240 219 av_expr_free(s->y_pexpr);
241 219 s->x_pexpr = x_pexpr;
242 219 s->y_pexpr = y_pexpr;
243
244 219 return config_crop_area(link);
245 }
246
247 219 static int config_output(AVFilterLink *link)
248 {
249 219 CropContext *s = link->src->priv;
250 219 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
251
252
1/2
✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
219 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
253 // Hardware frames adjust the cropping regions rather than
254 // changing the frame size.
255 } else {
256 219 link->w = s->w;
257 219 link->h = s->h;
258 }
259 219 link->sample_aspect_ratio = s->out_sar;
260
261 219 return 0;
262 }
263
264 2086 static int filter_frame(AVFilterLink *link, AVFrame *frame)
265 {
266 2086 FilterLink *l = ff_filter_link(link);
267 2086 AVFilterContext *ctx = link->dst;
268 2086 CropContext *s = ctx->priv;
269 2086 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
270 int i, ret;
271
272
2/4
✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2086 times.
2086 if (frame->width != s->in_w || frame->height != s->in_h) {
273 ✗ link->w = frame->width;
274 ✗ link->h = frame->height;
275 ✗ if ((ret = config_crop_area(link)) < 0 ||
276 ✗ (ret = config_output(ctx->outputs[0])) < 0) {
277 ✗ s->in_w = -1;
278 ✗ av_frame_free(&frame);
279 ✗ return ret;
280 }
281 }
282
283 2086 s->var_values[VAR_N] = l->frame_count_out;
284 4172 s->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
285
1/2
✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
2086 NAN : frame->pts * av_q2d(link->time_base);
286 2086 s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
287 2086 s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
288 /* It is necessary if x is expressed from y */
289 2086 s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
290
291 2086 normalize_double(&s->x, s->var_values[VAR_X]);
292 2086 normalize_double(&s->y, s->var_values[VAR_Y]);
293
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2086 times.
2086 if (s->x < 0)
295 ✗ s->x = 0;
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2086 times.
2086 if (s->y < 0)
297 ✗ s->y = 0;
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2086 times.
2086 if ((unsigned)s->x + (unsigned)s->w > link->w)
299 ✗ s->x = link->w - s->w;
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2086 times.
2086 if ((unsigned)s->y + (unsigned)s->h > link->h)
301 ✗ s->y = link->h - s->h;
302
1/2
✓ Branch 0 taken 2086 times.
✗ Branch 1 not taken.
2086 if (!s->exact) {
303 2086 s->x &= ~((1 << s->hsub) - 1);
304 2086 s->y &= ~((1 << s->vsub) - 1);
305 }
306
307 2086 av_log(ctx, AV_LOG_TRACE, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
308 2086 (int)s->var_values[VAR_N], s->var_values[VAR_T],
309 2086 s->x, s->y, s->x+s->w, s->y+s->h);
310
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2086 times.
2086 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
312 ✗ frame->crop_top += s->y;
313 ✗ frame->crop_left += s->x;
314 ✗ frame->crop_bottom = frame->height - frame->crop_top - frame->crop_bottom - s->h;
315 ✗ frame->crop_right = frame->width - frame->crop_left - frame->crop_right - s->w;
316 } else {
317 2086 frame->width = s->w;
318 2086 frame->height = s->h;
319
320 2086 frame->data[0] += s->y * frame->linesize[0];
321 2086 frame->data[0] += s->x * s->max_step[0];
322
323
2/2
✓ Branch 0 taken 2084 times.
✓ Branch 1 taken 2 times.
2086 if (!(desc->flags & AV_PIX_FMT_FLAG_PAL)) {
324
2/2
✓ Branch 0 taken 4168 times.
✓ Branch 1 taken 2084 times.
6252 for (i = 1; i < 3; i ++) {
325
2/2
✓ Branch 0 taken 3846 times.
✓ Branch 1 taken 322 times.
4168 if (frame->data[i]) {
326 3846 frame->data[i] += (s->y >> s->vsub) * frame->linesize[i];
327 3846 frame->data[i] += (s->x * s->max_step[i]) >> s->hsub;
328 }
329 }
330 }
331
332 /* alpha plane */
333
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 2013 times.
2086 if (frame->data[3]) {
334 73 frame->data[3] += s->y * frame->linesize[3];
335 73 frame->data[3] += s->x * s->max_step[3];
336 }
337 }
338
339 2086 return ff_filter_frame(link->dst->outputs[0], frame);
340 }
341
342 ✗ static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
343 char *res, int res_len, int flags)
344 {
345 ✗ CropContext *s = ctx->priv;
346 int ret;
347
348 ✗ if ( !strcmp(cmd, "out_w") || !strcmp(cmd, "w")
349 ✗ || !strcmp(cmd, "out_h") || !strcmp(cmd, "h")
350 ✗ || !strcmp(cmd, "x") || !strcmp(cmd, "y")) {
351
352 ✗ int old_x = s->x;
353 ✗ int old_y = s->y;
354 ✗ int old_w = s->w;
355 ✗ int old_h = s->h;
356
357 ✗ AVFilterLink *outlink = ctx->outputs[0];
358 ✗ AVFilterLink *inlink = ctx->inputs[0];
359
360 ✗ av_opt_set(s, cmd, args, 0);
361
362 ✗ if ((ret = config_input(inlink)) < 0) {
363 ✗ s->x = old_x;
364 ✗ s->y = old_y;
365 ✗ s->w = old_w;
366 ✗ s->h = old_h;
367 ✗ return ret;
368 }
369
370 ✗ ret = config_output(outlink);
371
372 } else
373 ✗ ret = AVERROR(ENOSYS);
374
375 ✗ return ret;
376 }
377
378 #define OFFSET(x) offsetof(CropContext, x)
379 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
380 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
381
382 static const AVOption crop_options[] = {
383 { "out_w", "set the width crop area expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, 0, 0, TFLAGS },
384 { "w", "set the width crop area expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, 0, 0, TFLAGS },
385 { "out_h", "set the height crop area expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, 0, 0, TFLAGS },
386 { "h", "set the height crop area expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, 0, 0, TFLAGS },
387 { "x", "set the x crop area expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "(in_w-out_w)/2"}, 0, 0, TFLAGS },
388 { "y", "set the y crop area expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "(in_h-out_h)/2"}, 0, 0, TFLAGS },
389 { "keep_aspect", "keep aspect ratio", OFFSET(keep_aspect), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
390 { "exact", "do exact cropping", OFFSET(exact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
391 { NULL }
392 };
393
394 AVFILTER_DEFINE_CLASS(crop);
395
396 static const AVFilterPad avfilter_vf_crop_inputs[] = {
397 {
398 .name = "default",
399 .type = AVMEDIA_TYPE_VIDEO,
400 .filter_frame = filter_frame,
401 .config_props = config_input,
402 },
403 };
404
405 static const AVFilterPad avfilter_vf_crop_outputs[] = {
406 {
407 .name = "default",
408 .type = AVMEDIA_TYPE_VIDEO,
409 .config_props = config_output,
410 },
411 };
412
413 const FFFilter ff_vf_crop = {
414 .p.name = "crop",
415 .p.description = NULL_IF_CONFIG_SMALL("Crop the input video."),
416 .p.priv_class = &crop_class,
417 .priv_size = sizeof(CropContext),
418 .uninit = uninit,
419 FILTER_INPUTS(avfilter_vf_crop_inputs),
420 FILTER_OUTPUTS(avfilter_vf_crop_outputs),
421 FILTER_QUERY_FUNC2(query_formats),
422 .process_command = process_command,
423 .flags_internal = FF_FILTER_FLAG_DYNAMIC_FRAME_SIZE,
424 };
425