FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_crop.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 116 164 70.7%
Functions: 6 7 85.7%
Branches: 37 78 47.4%

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