Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2008 vmrsss | ||
3 | * Copyright (c) 2009 Stefano Sabatini | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | /** | ||
23 | * @file | ||
24 | * video padding filter | ||
25 | */ | ||
26 | |||
27 | #include <float.h> /* DBL_MAX */ | ||
28 | |||
29 | #include "avfilter.h" | ||
30 | #include "formats.h" | ||
31 | #include "internal.h" | ||
32 | #include "video.h" | ||
33 | #include "libavutil/avstring.h" | ||
34 | #include "libavutil/common.h" | ||
35 | #include "libavutil/eval.h" | ||
36 | #include "libavutil/pixdesc.h" | ||
37 | #include "libavutil/colorspace.h" | ||
38 | #include "libavutil/imgutils.h" | ||
39 | #include "libavutil/parseutils.h" | ||
40 | #include "libavutil/mathematics.h" | ||
41 | #include "libavutil/opt.h" | ||
42 | |||
43 | #include "drawutils.h" | ||
44 | |||
45 | static const char *const var_names[] = { | ||
46 | "in_w", "iw", | ||
47 | "in_h", "ih", | ||
48 | "out_w", "ow", | ||
49 | "out_h", "oh", | ||
50 | "x", | ||
51 | "y", | ||
52 | "a", | ||
53 | "sar", | ||
54 | "dar", | ||
55 | "hsub", | ||
56 | "vsub", | ||
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_X, | ||
66 | VAR_Y, | ||
67 | VAR_A, | ||
68 | VAR_SAR, | ||
69 | VAR_DAR, | ||
70 | VAR_HSUB, | ||
71 | VAR_VSUB, | ||
72 | VARS_NB | ||
73 | }; | ||
74 | |||
75 | 102 | static int query_formats(AVFilterContext *ctx) | |
76 | { | ||
77 | 102 | return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0)); | |
78 | } | ||
79 | |||
80 | enum EvalMode { | ||
81 | EVAL_MODE_INIT, | ||
82 | EVAL_MODE_FRAME, | ||
83 | EVAL_MODE_NB | ||
84 | }; | ||
85 | |||
86 | typedef struct PadContext { | ||
87 | const AVClass *class; | ||
88 | int w, h; ///< output dimensions, a value of 0 will result in the input size | ||
89 | int x, y; ///< offsets of the input area with respect to the padded area | ||
90 | int in_w, in_h; ///< width and height for the padded input video, which has to be aligned to the chroma values in order to avoid chroma issues | ||
91 | int inlink_w, inlink_h; | ||
92 | AVRational aspect; | ||
93 | |||
94 | char *w_expr; ///< width expression string | ||
95 | char *h_expr; ///< height expression string | ||
96 | char *x_expr; ///< width expression string | ||
97 | char *y_expr; ///< height expression string | ||
98 | uint8_t rgba_color[4]; ///< color for the padding area | ||
99 | FFDrawContext draw; | ||
100 | FFDrawColor color; | ||
101 | |||
102 | int eval_mode; ///< expression evaluation mode | ||
103 | } PadContext; | ||
104 | |||
105 | 101 | static int config_input(AVFilterLink *inlink) | |
106 | { | ||
107 | 101 | AVFilterContext *ctx = inlink->dst; | |
108 | 101 | PadContext *s = ctx->priv; | |
109 | 101 | AVRational adjusted_aspect = s->aspect; | |
110 | int ret; | ||
111 | double var_values[VARS_NB], res; | ||
112 | char *expr; | ||
113 | |||
114 | 101 | ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0); | |
115 | 101 | ff_draw_color(&s->draw, &s->color, s->rgba_color); | |
116 | |||
117 | 101 | var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w; | |
118 | 101 | var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h; | |
119 | 101 | var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN; | |
120 | 101 | var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN; | |
121 | 101 | var_values[VAR_A] = (double) inlink->w / inlink->h; | |
122 | 202 | var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? | |
123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
|
101 | (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1; |
124 | 101 | var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR]; | |
125 | 101 | var_values[VAR_HSUB] = 1 << s->draw.hsub_max; | |
126 | 101 | var_values[VAR_VSUB] = 1 << s->draw.vsub_max; | |
127 | |||
128 | /* evaluate width and height */ | ||
129 | 101 | av_expr_parse_and_eval(&res, (expr = s->w_expr), | |
130 | var_names, var_values, | ||
131 | NULL, NULL, NULL, NULL, NULL, 0, ctx); | ||
132 | 101 | s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res; | |
133 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
|
101 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr), |
134 | var_names, var_values, | ||
135 | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) | ||
136 | ✗ | goto eval_fail; | |
137 | 101 | s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res; | |
138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
|
101 | if (!s->h) |
139 | ✗ | var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h = inlink->h; | |
140 | |||
141 | /* evaluate the width again, as it may depend on the evaluated output height */ | ||
142 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
|
101 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr), |
143 | var_names, var_values, | ||
144 | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) | ||
145 | ✗ | goto eval_fail; | |
146 | 101 | s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res; | |
147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
|
101 | if (!s->w) |
148 | ✗ | var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w = inlink->w; | |
149 | |||
150 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
101 | if (adjusted_aspect.num && adjusted_aspect.den) { |
151 | ✗ | adjusted_aspect = av_div_q(adjusted_aspect, inlink->sample_aspect_ratio); | |
152 | ✗ | if (s->h < av_rescale(s->w, adjusted_aspect.den, adjusted_aspect.num)) { | |
153 | ✗ | s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = av_rescale(s->w, adjusted_aspect.den, adjusted_aspect.num); | |
154 | } else { | ||
155 | ✗ | s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = av_rescale(s->h, adjusted_aspect.num, adjusted_aspect.den); | |
156 | } | ||
157 | } | ||
158 | |||
159 | /* evaluate x and y */ | ||
160 | 101 | av_expr_parse_and_eval(&res, (expr = s->x_expr), | |
161 | var_names, var_values, | ||
162 | NULL, NULL, NULL, NULL, NULL, 0, ctx); | ||
163 | 101 | s->x = var_values[VAR_X] = res; | |
164 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
|
101 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr), |
165 | var_names, var_values, | ||
166 | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) | ||
167 | ✗ | goto eval_fail; | |
168 | 101 | s->y = var_values[VAR_Y] = res; | |
169 | /* evaluate x again, as it may depend on the evaluated y value */ | ||
170 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
|
101 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr), |
171 | var_names, var_values, | ||
172 | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) | ||
173 | ✗ | goto eval_fail; | |
174 | 101 | s->x = var_values[VAR_X] = res; | |
175 | |||
176 |
2/4✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 101 times.
|
101 | if (s->x < 0 || s->x + inlink->w > s->w) |
177 | ✗ | s->x = var_values[VAR_X] = (s->w - inlink->w) / 2; | |
178 |
2/4✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 101 times.
|
101 | if (s->y < 0 || s->y + inlink->h > s->h) |
179 | ✗ | s->y = var_values[VAR_Y] = (s->h - inlink->h) / 2; | |
180 | |||
181 | 101 | s->w = ff_draw_round_to_sub(&s->draw, 0, -1, s->w); | |
182 | 101 | s->h = ff_draw_round_to_sub(&s->draw, 1, -1, s->h); | |
183 | /* sanity check params */ | ||
184 |
2/4✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 101 times.
|
101 | if (s->w < inlink->w || s->h < inlink->h) { |
185 | ✗ | av_log(ctx, AV_LOG_ERROR, "Padded dimensions cannot be smaller than input dimensions.\n"); | |
186 | ✗ | return AVERROR(EINVAL); | |
187 | } | ||
188 | |||
189 | 101 | s->x = ff_draw_round_to_sub(&s->draw, 0, -1, s->x); | |
190 | 101 | s->y = ff_draw_round_to_sub(&s->draw, 1, -1, s->y); | |
191 | 101 | s->in_w = ff_draw_round_to_sub(&s->draw, 0, -1, inlink->w); | |
192 | 101 | s->in_h = ff_draw_round_to_sub(&s->draw, 1, -1, inlink->h); | |
193 | 101 | s->inlink_w = inlink->w; | |
194 | 101 | s->inlink_h = inlink->h; | |
195 | |||
196 | 101 | av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X\n", | |
197 | inlink->w, inlink->h, s->w, s->h, s->x, s->y, | ||
198 | 101 | s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], s->rgba_color[3]); | |
199 | |||
200 |
2/4✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
|
101 | if (s->x < 0 || s->y < 0 || |
201 |
2/4✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
|
101 | s->w <= 0 || s->h <= 0 || |
202 |
1/2✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
|
101 | (unsigned)s->x + (unsigned)inlink->w > s->w || |
203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
|
101 | (unsigned)s->y + (unsigned)inlink->h > s->h) { |
204 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
205 | "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n", | ||
206 | ✗ | s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h); | |
207 | ✗ | return AVERROR(EINVAL); | |
208 | } | ||
209 | |||
210 | 101 | return 0; | |
211 | |||
212 | ✗ | eval_fail: | |
213 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
214 | "Error when evaluating the expression '%s'\n", expr); | ||
215 | ✗ | return ret; | |
216 | |||
217 | } | ||
218 | |||
219 | 101 | static int config_output(AVFilterLink *outlink) | |
220 | { | ||
221 | 101 | PadContext *s = outlink->src->priv; | |
222 | |||
223 | 101 | outlink->w = s->w; | |
224 | 101 | outlink->h = s->h; | |
225 | 101 | return 0; | |
226 | } | ||
227 | |||
228 | 423 | static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h) | |
229 | { | ||
230 | 423 | PadContext *s = inlink->dst->priv; | |
231 | AVFrame *frame; | ||
232 | int plane; | ||
233 | |||
234 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 423 times.
|
423 | if (s->inlink_w <= 0) |
235 | ✗ | return NULL; | |
236 | |||
237 | 423 | frame = ff_get_video_buffer(inlink->dst->outputs[0], | |
238 | 423 | w + (s->w - s->in_w), | |
239 | 423 | h + (s->h - s->in_h) + (s->x > 0)); | |
240 | |||
241 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 423 times.
|
423 | if (!frame) |
242 | ✗ | return NULL; | |
243 | |||
244 | 423 | frame->width = w; | |
245 | 423 | frame->height = h; | |
246 | |||
247 |
5/6✓ Branch 0 taken 1534 times.
✓ Branch 1 taken 269 times.
✓ Branch 2 taken 1380 times.
✓ Branch 3 taken 154 times.
✓ Branch 4 taken 1380 times.
✗ Branch 5 not taken.
|
1803 | for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) { |
248 | 1380 | int hsub = s->draw.hsub[plane]; | |
249 | 1380 | int vsub = s->draw.vsub[plane]; | |
250 | 1380 | frame->data[plane] += (s->x >> hsub) * s->draw.pixelstep[plane] + | |
251 | 1380 | (s->y >> vsub) * frame->linesize[plane]; | |
252 | } | ||
253 | |||
254 | 423 | return frame; | |
255 | } | ||
256 | |||
257 | /* check whether each plane in this buffer can be padded without copying */ | ||
258 | 1392 | static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf) | |
259 | { | ||
260 | 1392 | int planes[4] = { -1, -1, -1, -1}, *p = planes; | |
261 | int i, j; | ||
262 | |||
263 | /* get all planes in this buffer */ | ||
264 |
4/4✓ Branch 0 taken 5396 times.
✓ Branch 1 taken 1076 times.
✓ Branch 2 taken 5080 times.
✓ Branch 3 taken 316 times.
|
6472 | for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) { |
265 |
2/2✓ Branch 1 taken 1392 times.
✓ Branch 2 taken 3688 times.
|
5080 | if (av_frame_get_plane_buffer(frame, i) == buf) |
266 | 1392 | *p++ = i; | |
267 | } | ||
268 | |||
269 | /* for each plane in this buffer, check that it can be padded without | ||
270 | * going over buffer bounds or other planes */ | ||
271 |
3/4✓ Branch 0 taken 2772 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1392 times.
✓ Branch 3 taken 1380 times.
|
2772 | for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) { |
272 | 1392 | int hsub = s->draw.hsub[planes[i]]; | |
273 | 1392 | int vsub = s->draw.vsub[planes[i]]; | |
274 | |||
275 | 1392 | uint8_t *start = frame->data[planes[i]]; | |
276 | 1392 | uint8_t *end = start + (frame->height >> vsub) * | |
277 | 1392 | frame->linesize[planes[i]]; | |
278 | |||
279 | /* amount of free space needed before the start and after the end | ||
280 | * of the plane */ | ||
281 | 1392 | ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] + | |
282 | 1392 | (s->y >> vsub) * frame->linesize[planes[i]]; | |
283 | 1392 | ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) * | |
284 | 1392 | s->draw.pixelstep[planes[i]] + | |
285 | 1392 | ((s->h - s->y - frame->height) >> vsub) * frame->linesize[planes[i]]; | |
286 | |||
287 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1380 times.
|
1392 | if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]]) |
288 | 12 | return 1; | |
289 |
1/2✓ Branch 0 taken 1380 times.
✗ Branch 1 not taken.
|
1380 | if (start - buf->data < req_start || |
290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1380 times.
|
1380 | (buf->data + buf->size) - end < req_end) |
291 | ✗ | return 1; | |
292 | |||
293 |
3/4✓ Branch 0 taken 2760 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1380 times.
✓ Branch 3 taken 1380 times.
|
2760 | for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) { |
294 | 1380 | int vsub1 = s->draw.vsub[planes[j]]; | |
295 | 1380 | uint8_t *start1 = frame->data[planes[j]]; | |
296 | 1380 | uint8_t *end1 = start1 + (frame->height >> vsub1) * | |
297 | 1380 | frame->linesize[planes[j]]; | |
298 |
1/2✓ Branch 0 taken 1380 times.
✗ Branch 1 not taken.
|
1380 | if (i == j) |
299 | 1380 | continue; | |
300 | |||
301 | ✗ | if (FFSIGN(start - end1) != FFSIGN(start - end1 - req_start) || | |
302 | ✗ | FFSIGN(end - start1) != FFSIGN(end - start1 + req_end)) | |
303 | ✗ | return 1; | |
304 | } | ||
305 | } | ||
306 | |||
307 | 1380 | return 0; | |
308 | } | ||
309 | |||
310 | 435 | static int frame_needs_copy(PadContext *s, AVFrame *frame) | |
311 | { | ||
312 | int i; | ||
313 | |||
314 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 435 times.
|
435 | if (!av_frame_is_writable(frame)) |
315 | ✗ | return 1; | |
316 | |||
317 |
4/4✓ Branch 0 taken 1546 times.
✓ Branch 1 taken 269 times.
✓ Branch 2 taken 1392 times.
✓ Branch 3 taken 154 times.
|
1815 | for (i = 0; i < 4 && frame->buf[i]; i++) |
318 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1380 times.
|
1392 | if (buffer_needs_copy(s, frame, frame->buf[i])) |
319 | 12 | return 1; | |
320 | 423 | return 0; | |
321 | } | ||
322 | |||
323 | 435 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
324 | { | ||
325 | 435 | PadContext *s = inlink->dst->priv; | |
326 | 435 | AVFilterLink *outlink = inlink->dst->outputs[0]; | |
327 | AVFrame *out; | ||
328 | int needs_copy; | ||
329 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 435 times.
|
435 | if(s->eval_mode == EVAL_MODE_FRAME && ( |
330 | ✗ | in->width != s->inlink_w | |
331 | ✗ | || in->height != s->inlink_h | |
332 | ✗ | || in->format != outlink->format | |
333 | ✗ | || in->sample_aspect_ratio.den != outlink->sample_aspect_ratio.den || in->sample_aspect_ratio.num != outlink->sample_aspect_ratio.num)) { | |
334 | int ret; | ||
335 | |||
336 | ✗ | inlink->dst->inputs[0]->format = in->format; | |
337 | ✗ | inlink->dst->inputs[0]->w = in->width; | |
338 | ✗ | inlink->dst->inputs[0]->h = in->height; | |
339 | |||
340 | ✗ | inlink->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den; | |
341 | ✗ | inlink->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num; | |
342 | |||
343 | |||
344 | ✗ | if ((ret = config_input(inlink)) < 0) { | |
345 | ✗ | s->inlink_w = -1; | |
346 | ✗ | return ret; | |
347 | } | ||
348 | ✗ | if ((ret = config_output(outlink)) < 0) { | |
349 | ✗ | s->inlink_w = -1; | |
350 | ✗ | return ret; | |
351 | } | ||
352 | } | ||
353 | |||
354 | 435 | needs_copy = frame_needs_copy(s, in); | |
355 | |||
356 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 423 times.
|
435 | if (needs_copy) { |
357 | 12 | av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n"); | |
358 | 12 | out = ff_get_video_buffer(outlink, | |
359 | 12 | FFMAX(inlink->w, s->w), | |
360 | 12 | FFMAX(inlink->h, s->h)); | |
361 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!out) { |
362 | ✗ | av_frame_free(&in); | |
363 | ✗ | return AVERROR(ENOMEM); | |
364 | } | ||
365 | |||
366 | 12 | av_frame_copy_props(out, in); | |
367 | } else { | ||
368 | int i; | ||
369 | |||
370 | 423 | out = in; | |
371 |
5/6✓ Branch 0 taken 1534 times.
✓ Branch 1 taken 269 times.
✓ Branch 2 taken 1380 times.
✓ Branch 3 taken 154 times.
✓ Branch 4 taken 1380 times.
✗ Branch 5 not taken.
|
1803 | for (i = 0; i < 4 && out->data[i] && out->linesize[i]; i++) { |
372 | 1380 | int hsub = s->draw.hsub[i]; | |
373 | 1380 | int vsub = s->draw.vsub[i]; | |
374 | 1380 | out->data[i] -= (s->x >> hsub) * s->draw.pixelstep[i] + | |
375 | 1380 | (s->y >> vsub) * out->linesize[i]; | |
376 | } | ||
377 | } | ||
378 | |||
379 | /* top bar */ | ||
380 |
1/2✓ Branch 0 taken 435 times.
✗ Branch 1 not taken.
|
435 | if (s->y) { |
381 | 435 | ff_fill_rectangle(&s->draw, &s->color, | |
382 | 435 | out->data, out->linesize, | |
383 | 0, 0, s->w, s->y); | ||
384 | } | ||
385 | |||
386 | /* bottom bar */ | ||
387 |
2/2✓ Branch 0 taken 409 times.
✓ Branch 1 taken 26 times.
|
435 | if (s->h > s->y + s->in_h) { |
388 | 409 | ff_fill_rectangle(&s->draw, &s->color, | |
389 | 409 | out->data, out->linesize, | |
390 | 409 | 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h); | |
391 | } | ||
392 | |||
393 | /* left border */ | ||
394 | 435 | ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize, | |
395 | 435 | 0, s->y, s->x, in->height); | |
396 | |||
397 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 423 times.
|
435 | if (needs_copy) { |
398 | 12 | ff_copy_rectangle2(&s->draw, | |
399 | 12 | out->data, out->linesize, in->data, in->linesize, | |
400 | 12 | s->x, s->y, 0, 0, in->width, in->height); | |
401 | } | ||
402 | |||
403 | /* right border */ | ||
404 | 435 | ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize, | |
405 | 435 | s->x + s->in_w, s->y, s->w - s->x - s->in_w, | |
406 | 435 | in->height); | |
407 | |||
408 | 435 | out->width = s->w; | |
409 | 435 | out->height = s->h; | |
410 | |||
411 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 423 times.
|
435 | if (in != out) |
412 | 12 | av_frame_free(&in); | |
413 | 435 | return ff_filter_frame(outlink, out); | |
414 | } | ||
415 | |||
416 | #define OFFSET(x) offsetof(PadContext, x) | ||
417 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM | ||
418 | |||
419 | static const AVOption pad_options[] = { | ||
420 | { "width", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, 0, 0, FLAGS }, | ||
421 | { "w", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, 0, 0, FLAGS }, | ||
422 | { "height", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, 0, 0, FLAGS }, | ||
423 | { "h", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, 0, 0, FLAGS }, | ||
424 | { "x", "set the x offset expression for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS }, | ||
425 | { "y", "set the y offset expression for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS }, | ||
426 | { "color", "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS }, | ||
427 | { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, .unit = "eval" }, | ||
428 | { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" }, | ||
429 | { "frame", "eval expressions during initialization and per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" }, | ||
430 | { "aspect", "pad to fit an aspect instead of a resolution", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, DBL_MAX, FLAGS }, | ||
431 | { NULL } | ||
432 | }; | ||
433 | |||
434 | AVFILTER_DEFINE_CLASS(pad); | ||
435 | |||
436 | static const AVFilterPad avfilter_vf_pad_inputs[] = { | ||
437 | { | ||
438 | .name = "default", | ||
439 | .type = AVMEDIA_TYPE_VIDEO, | ||
440 | .config_props = config_input, | ||
441 | .get_buffer.video = get_video_buffer, | ||
442 | .filter_frame = filter_frame, | ||
443 | }, | ||
444 | }; | ||
445 | |||
446 | static const AVFilterPad avfilter_vf_pad_outputs[] = { | ||
447 | { | ||
448 | .name = "default", | ||
449 | .type = AVMEDIA_TYPE_VIDEO, | ||
450 | .config_props = config_output, | ||
451 | }, | ||
452 | }; | ||
453 | |||
454 | const AVFilter ff_vf_pad = { | ||
455 | .name = "pad", | ||
456 | .description = NULL_IF_CONFIG_SMALL("Pad the input video."), | ||
457 | .priv_size = sizeof(PadContext), | ||
458 | .priv_class = &pad_class, | ||
459 | FILTER_INPUTS(avfilter_vf_pad_inputs), | ||
460 | FILTER_OUTPUTS(avfilter_vf_pad_outputs), | ||
461 | FILTER_QUERY_FUNC(query_formats), | ||
462 | }; | ||
463 |