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 "filters.h" | ||
31 | #include "formats.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 | 105 | static int query_formats(const AVFilterContext *ctx, | |
76 | AVFilterFormatsConfig **cfg_in, | ||
77 | AVFilterFormatsConfig **cfg_out) | ||
78 | { | ||
79 | 105 | return ff_set_common_formats2(ctx, cfg_in, cfg_out, | |
80 | ff_draw_supported_pixel_formats(0)); | ||
81 | } | ||
82 | |||
83 | enum EvalMode { | ||
84 | EVAL_MODE_INIT, | ||
85 | EVAL_MODE_FRAME, | ||
86 | EVAL_MODE_NB | ||
87 | }; | ||
88 | |||
89 | typedef struct PadContext { | ||
90 | const AVClass *class; | ||
91 | int w, h; ///< output dimensions, a value of 0 will result in the input size | ||
92 | int x, y; ///< offsets of the input area with respect to the padded area | ||
93 | 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 | ||
94 | int inlink_w, inlink_h; | ||
95 | AVRational aspect; | ||
96 | |||
97 | char *w_expr; ///< width expression string | ||
98 | char *h_expr; ///< height expression string | ||
99 | char *x_expr; ///< width expression string | ||
100 | char *y_expr; ///< height expression string | ||
101 | uint8_t rgba_color[4]; ///< color for the padding area | ||
102 | FFDrawContext draw; | ||
103 | FFDrawColor color; | ||
104 | |||
105 | int eval_mode; ///< expression evaluation mode | ||
106 | } PadContext; | ||
107 | |||
108 | 104 | static int config_input(AVFilterLink *inlink) | |
109 | { | ||
110 | 104 | AVFilterContext *ctx = inlink->dst; | |
111 | 104 | PadContext *s = ctx->priv; | |
112 | 104 | AVRational adjusted_aspect = s->aspect; | |
113 | int ret; | ||
114 | double var_values[VARS_NB], res; | ||
115 | char *expr; | ||
116 | |||
117 | 104 | ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0); | |
118 | 104 | ff_draw_color(&s->draw, &s->color, s->rgba_color); | |
119 | |||
120 | 104 | var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w; | |
121 | 104 | var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h; | |
122 | 104 | var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN; | |
123 | 104 | var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN; | |
124 | 104 | var_values[VAR_A] = (double) inlink->w / inlink->h; | |
125 | 208 | var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? | |
126 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1; |
127 | 104 | var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR]; | |
128 | 104 | var_values[VAR_HSUB] = 1 << s->draw.hsub_max; | |
129 | 104 | var_values[VAR_VSUB] = 1 << s->draw.vsub_max; | |
130 | |||
131 | /* evaluate width and height */ | ||
132 | 104 | av_expr_parse_and_eval(&res, (expr = s->w_expr), | |
133 | var_names, var_values, | ||
134 | NULL, NULL, NULL, NULL, NULL, 0, ctx); | ||
135 | 104 | s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res; | |
136 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
|
104 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr), |
137 | var_names, var_values, | ||
138 | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) | ||
139 | ✗ | goto eval_fail; | |
140 | 104 | s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res; | |
141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | if (!s->h) |
142 | ✗ | var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h = inlink->h; | |
143 | |||
144 | /* evaluate the width again, as it may depend on the evaluated output height */ | ||
145 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
|
104 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr), |
146 | var_names, var_values, | ||
147 | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) | ||
148 | ✗ | goto eval_fail; | |
149 | 104 | s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res; | |
150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | if (!s->w) |
151 | ✗ | var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w = inlink->w; | |
152 | |||
153 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
104 | if (adjusted_aspect.num && adjusted_aspect.den) { |
154 | ✗ | adjusted_aspect = av_div_q(adjusted_aspect, inlink->sample_aspect_ratio); | |
155 | ✗ | if (s->h < av_rescale(s->w, adjusted_aspect.den, adjusted_aspect.num)) { | |
156 | ✗ | s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = av_rescale(s->w, adjusted_aspect.den, adjusted_aspect.num); | |
157 | } else { | ||
158 | ✗ | s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = av_rescale(s->h, adjusted_aspect.num, adjusted_aspect.den); | |
159 | } | ||
160 | } | ||
161 | |||
162 | /* evaluate x and y */ | ||
163 | 104 | av_expr_parse_and_eval(&res, (expr = s->x_expr), | |
164 | var_names, var_values, | ||
165 | NULL, NULL, NULL, NULL, NULL, 0, ctx); | ||
166 | 104 | s->x = var_values[VAR_X] = res; | |
167 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
|
104 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr), |
168 | var_names, var_values, | ||
169 | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) | ||
170 | ✗ | goto eval_fail; | |
171 | 104 | s->y = var_values[VAR_Y] = res; | |
172 | /* evaluate x again, as it may depend on the evaluated y value */ | ||
173 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
|
104 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr), |
174 | var_names, var_values, | ||
175 | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) | ||
176 | ✗ | goto eval_fail; | |
177 | 104 | s->x = var_values[VAR_X] = res; | |
178 | |||
179 |
2/4✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
|
104 | if (s->x < 0 || s->x + inlink->w > s->w) |
180 | ✗ | s->x = var_values[VAR_X] = (s->w - inlink->w) / 2; | |
181 |
2/4✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
|
104 | if (s->y < 0 || s->y + inlink->h > s->h) |
182 | ✗ | s->y = var_values[VAR_Y] = (s->h - inlink->h) / 2; | |
183 | |||
184 | 104 | s->w = ff_draw_round_to_sub(&s->draw, 0, -1, s->w); | |
185 | 104 | s->h = ff_draw_round_to_sub(&s->draw, 1, -1, s->h); | |
186 | /* sanity check params */ | ||
187 |
2/4✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
|
104 | if (s->w < inlink->w || s->h < inlink->h) { |
188 | ✗ | av_log(ctx, AV_LOG_ERROR, "Padded dimensions cannot be smaller than input dimensions.\n"); | |
189 | ✗ | return AVERROR(EINVAL); | |
190 | } | ||
191 | |||
192 | 104 | s->x = ff_draw_round_to_sub(&s->draw, 0, -1, s->x); | |
193 | 104 | s->y = ff_draw_round_to_sub(&s->draw, 1, -1, s->y); | |
194 | 104 | s->in_w = ff_draw_round_to_sub(&s->draw, 0, -1, inlink->w); | |
195 | 104 | s->in_h = ff_draw_round_to_sub(&s->draw, 1, -1, inlink->h); | |
196 | 104 | s->inlink_w = inlink->w; | |
197 | 104 | s->inlink_h = inlink->h; | |
198 | |||
199 | 104 | 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", | |
200 | inlink->w, inlink->h, s->w, s->h, s->x, s->y, | ||
201 | 104 | s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], s->rgba_color[3]); | |
202 | |||
203 |
2/4✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
✗ Branch 3 not taken.
|
104 | if (s->x < 0 || s->y < 0 || |
204 |
2/4✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
✗ Branch 3 not taken.
|
104 | s->w <= 0 || s->h <= 0 || |
205 |
1/2✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
|
104 | (unsigned)s->x + (unsigned)inlink->w > s->w || |
206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | (unsigned)s->y + (unsigned)inlink->h > s->h) { |
207 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
208 | "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n", | ||
209 | ✗ | s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h); | |
210 | ✗ | return AVERROR(EINVAL); | |
211 | } | ||
212 | |||
213 | 104 | return 0; | |
214 | |||
215 | ✗ | eval_fail: | |
216 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
217 | "Error when evaluating the expression '%s'\n", expr); | ||
218 | ✗ | return ret; | |
219 | |||
220 | } | ||
221 | |||
222 | 104 | static int config_output(AVFilterLink *outlink) | |
223 | { | ||
224 | 104 | PadContext *s = outlink->src->priv; | |
225 | |||
226 | 104 | outlink->w = s->w; | |
227 | 104 | outlink->h = s->h; | |
228 | 104 | return 0; | |
229 | } | ||
230 | |||
231 | 426 | static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h) | |
232 | { | ||
233 | 426 | PadContext *s = inlink->dst->priv; | |
234 | AVFrame *frame; | ||
235 | int plane; | ||
236 | |||
237 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 426 times.
|
426 | if (s->inlink_w <= 0) |
238 | ✗ | return NULL; | |
239 | |||
240 | 426 | frame = ff_get_video_buffer(inlink->dst->outputs[0], | |
241 | 426 | w + (s->w - s->in_w), | |
242 | 426 | h + (s->h - s->in_h) + (s->x > 0)); | |
243 | |||
244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 426 times.
|
426 | if (!frame) |
245 | ✗ | return NULL; | |
246 | |||
247 | 426 | frame->width = w; | |
248 | 426 | frame->height = h; | |
249 | |||
250 |
5/6✓ Branch 0 taken 1540 times.
✓ Branch 1 taken 269 times.
✓ Branch 2 taken 1383 times.
✓ Branch 3 taken 157 times.
✓ Branch 4 taken 1383 times.
✗ Branch 5 not taken.
|
1809 | for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) { |
251 | 1383 | int hsub = s->draw.hsub[plane]; | |
252 | 1383 | int vsub = s->draw.vsub[plane]; | |
253 | 1383 | frame->data[plane] += (s->x >> hsub) * s->draw.pixelstep[plane] + | |
254 | 1383 | (s->y >> vsub) * frame->linesize[plane]; | |
255 | } | ||
256 | |||
257 | 426 | return frame; | |
258 | } | ||
259 | |||
260 | /* check whether each plane in this buffer can be padded without copying */ | ||
261 | 1395 | static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf) | |
262 | { | ||
263 | 1395 | int planes[4] = { -1, -1, -1, -1}, *p = planes; | |
264 | int i, j; | ||
265 | |||
266 | /* get all planes in this buffer */ | ||
267 |
4/4✓ Branch 0 taken 5402 times.
✓ Branch 1 taken 1076 times.
✓ Branch 2 taken 5083 times.
✓ Branch 3 taken 319 times.
|
6478 | for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) { |
268 |
2/2✓ Branch 1 taken 1395 times.
✓ Branch 2 taken 3688 times.
|
5083 | if (av_frame_get_plane_buffer(frame, i) == buf) |
269 | 1395 | *p++ = i; | |
270 | } | ||
271 | |||
272 | /* for each plane in this buffer, check that it can be padded without | ||
273 | * going over buffer bounds or other planes */ | ||
274 |
3/4✓ Branch 0 taken 2778 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1395 times.
✓ Branch 3 taken 1383 times.
|
2778 | for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) { |
275 | 1395 | int hsub = s->draw.hsub[planes[i]]; | |
276 | 1395 | int vsub = s->draw.vsub[planes[i]]; | |
277 | |||
278 | 1395 | uint8_t *start = frame->data[planes[i]]; | |
279 | 1395 | uint8_t *end = start + (frame->height >> vsub) * | |
280 | 1395 | frame->linesize[planes[i]]; | |
281 | |||
282 | /* amount of free space needed before the start and after the end | ||
283 | * of the plane */ | ||
284 | 1395 | ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] + | |
285 | 1395 | (s->y >> vsub) * frame->linesize[planes[i]]; | |
286 | 1395 | ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) * | |
287 | 1395 | s->draw.pixelstep[planes[i]] + | |
288 | 1395 | ((s->h - s->y - frame->height) >> vsub) * frame->linesize[planes[i]]; | |
289 | |||
290 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1383 times.
|
1395 | if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]]) |
291 | 12 | return 1; | |
292 |
1/2✓ Branch 0 taken 1383 times.
✗ Branch 1 not taken.
|
1383 | if (start - buf->data < req_start || |
293 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1383 times.
|
1383 | (buf->data + buf->size) - end < req_end) |
294 | ✗ | return 1; | |
295 | |||
296 |
3/4✓ Branch 0 taken 2766 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1383 times.
✓ Branch 3 taken 1383 times.
|
2766 | for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) { |
297 | 1383 | int vsub1 = s->draw.vsub[planes[j]]; | |
298 | 1383 | uint8_t *start1 = frame->data[planes[j]]; | |
299 | 1383 | uint8_t *end1 = start1 + (frame->height >> vsub1) * | |
300 | 1383 | frame->linesize[planes[j]]; | |
301 |
1/2✓ Branch 0 taken 1383 times.
✗ Branch 1 not taken.
|
1383 | if (i == j) |
302 | 1383 | continue; | |
303 | |||
304 | ✗ | if (FFSIGN(start - end1) != FFSIGN(start - end1 - req_start) || | |
305 | ✗ | FFSIGN(end - start1) != FFSIGN(end - start1 + req_end)) | |
306 | ✗ | return 1; | |
307 | } | ||
308 | } | ||
309 | |||
310 | 1383 | return 0; | |
311 | } | ||
312 | |||
313 | 438 | static int frame_needs_copy(PadContext *s, AVFrame *frame) | |
314 | { | ||
315 | int i; | ||
316 | |||
317 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 438 times.
|
438 | if (!av_frame_is_writable(frame)) |
318 | ✗ | return 1; | |
319 | |||
320 |
4/4✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 269 times.
✓ Branch 2 taken 1395 times.
✓ Branch 3 taken 157 times.
|
1821 | for (i = 0; i < 4 && frame->buf[i]; i++) |
321 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1383 times.
|
1395 | if (buffer_needs_copy(s, frame, frame->buf[i])) |
322 | 12 | return 1; | |
323 | 426 | return 0; | |
324 | } | ||
325 | |||
326 | 438 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
327 | { | ||
328 | 438 | PadContext *s = inlink->dst->priv; | |
329 | 438 | AVFilterLink *outlink = inlink->dst->outputs[0]; | |
330 | AVFrame *out; | ||
331 | int needs_copy; | ||
332 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 438 times.
|
438 | if(s->eval_mode == EVAL_MODE_FRAME && ( |
333 | ✗ | in->width != s->inlink_w | |
334 | ✗ | || in->height != s->inlink_h | |
335 | ✗ | || in->format != outlink->format | |
336 | ✗ | || in->sample_aspect_ratio.den != outlink->sample_aspect_ratio.den || in->sample_aspect_ratio.num != outlink->sample_aspect_ratio.num)) { | |
337 | int ret; | ||
338 | |||
339 | ✗ | inlink->dst->inputs[0]->format = in->format; | |
340 | ✗ | inlink->dst->inputs[0]->w = in->width; | |
341 | ✗ | inlink->dst->inputs[0]->h = in->height; | |
342 | |||
343 | ✗ | inlink->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den; | |
344 | ✗ | inlink->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num; | |
345 | |||
346 | |||
347 | ✗ | if ((ret = config_input(inlink)) < 0) { | |
348 | ✗ | s->inlink_w = -1; | |
349 | ✗ | return ret; | |
350 | } | ||
351 | ✗ | if ((ret = config_output(outlink)) < 0) { | |
352 | ✗ | s->inlink_w = -1; | |
353 | ✗ | return ret; | |
354 | } | ||
355 | } | ||
356 | |||
357 | 438 | needs_copy = frame_needs_copy(s, in); | |
358 | |||
359 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 426 times.
|
438 | if (needs_copy) { |
360 | 12 | av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n"); | |
361 | 12 | out = ff_get_video_buffer(outlink, | |
362 | 12 | FFMAX(inlink->w, s->w), | |
363 | 12 | FFMAX(inlink->h, s->h)); | |
364 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!out) { |
365 | ✗ | av_frame_free(&in); | |
366 | ✗ | return AVERROR(ENOMEM); | |
367 | } | ||
368 | |||
369 | 12 | av_frame_copy_props(out, in); | |
370 | } else { | ||
371 | int i; | ||
372 | |||
373 | 426 | out = in; | |
374 |
5/6✓ Branch 0 taken 1540 times.
✓ Branch 1 taken 269 times.
✓ Branch 2 taken 1383 times.
✓ Branch 3 taken 157 times.
✓ Branch 4 taken 1383 times.
✗ Branch 5 not taken.
|
1809 | for (i = 0; i < 4 && out->data[i] && out->linesize[i]; i++) { |
375 | 1383 | int hsub = s->draw.hsub[i]; | |
376 | 1383 | int vsub = s->draw.vsub[i]; | |
377 | 1383 | out->data[i] -= (s->x >> hsub) * s->draw.pixelstep[i] + | |
378 | 1383 | (s->y >> vsub) * out->linesize[i]; | |
379 | } | ||
380 | } | ||
381 | |||
382 | /* top bar */ | ||
383 |
1/2✓ Branch 0 taken 438 times.
✗ Branch 1 not taken.
|
438 | if (s->y) { |
384 | 438 | ff_fill_rectangle(&s->draw, &s->color, | |
385 | 438 | out->data, out->linesize, | |
386 | 0, 0, s->w, s->y); | ||
387 | } | ||
388 | |||
389 | /* bottom bar */ | ||
390 |
2/2✓ Branch 0 taken 412 times.
✓ Branch 1 taken 26 times.
|
438 | if (s->h > s->y + s->in_h) { |
391 | 412 | ff_fill_rectangle(&s->draw, &s->color, | |
392 | 412 | out->data, out->linesize, | |
393 | 412 | 0, s->y + s->in_h, s->w, s->h - s->y - s->in_h); | |
394 | } | ||
395 | |||
396 | /* left border */ | ||
397 | 438 | ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize, | |
398 | 438 | 0, s->y, s->x, in->height); | |
399 | |||
400 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 426 times.
|
438 | if (needs_copy) { |
401 | 12 | ff_copy_rectangle2(&s->draw, | |
402 | 12 | out->data, out->linesize, in->data, in->linesize, | |
403 | 12 | s->x, s->y, 0, 0, in->width, in->height); | |
404 | } | ||
405 | |||
406 | /* right border */ | ||
407 | 438 | ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize, | |
408 | 438 | s->x + s->in_w, s->y, s->w - s->x - s->in_w, | |
409 | 438 | in->height); | |
410 | |||
411 | 438 | out->width = s->w; | |
412 | 438 | out->height = s->h; | |
413 | |||
414 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 426 times.
|
438 | if (in != out) |
415 | 12 | av_frame_free(&in); | |
416 | 438 | return ff_filter_frame(outlink, out); | |
417 | } | ||
418 | |||
419 | #define OFFSET(x) offsetof(PadContext, x) | ||
420 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM | ||
421 | |||
422 | static const AVOption pad_options[] = { | ||
423 | { "width", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, 0, 0, FLAGS }, | ||
424 | { "w", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, 0, 0, FLAGS }, | ||
425 | { "height", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, 0, 0, FLAGS }, | ||
426 | { "h", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, 0, 0, FLAGS }, | ||
427 | { "x", "set the x offset expression for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS }, | ||
428 | { "y", "set the y offset expression for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS }, | ||
429 | { "color", "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS }, | ||
430 | { "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" }, | ||
431 | { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" }, | ||
432 | { "frame", "eval expressions during initialization and per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" }, | ||
433 | { "aspect", "pad to fit an aspect instead of a resolution", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, DBL_MAX, FLAGS }, | ||
434 | { NULL } | ||
435 | }; | ||
436 | |||
437 | AVFILTER_DEFINE_CLASS(pad); | ||
438 | |||
439 | static const AVFilterPad avfilter_vf_pad_inputs[] = { | ||
440 | { | ||
441 | .name = "default", | ||
442 | .type = AVMEDIA_TYPE_VIDEO, | ||
443 | .config_props = config_input, | ||
444 | .get_buffer.video = get_video_buffer, | ||
445 | .filter_frame = filter_frame, | ||
446 | }, | ||
447 | }; | ||
448 | |||
449 | static const AVFilterPad avfilter_vf_pad_outputs[] = { | ||
450 | { | ||
451 | .name = "default", | ||
452 | .type = AVMEDIA_TYPE_VIDEO, | ||
453 | .config_props = config_output, | ||
454 | }, | ||
455 | }; | ||
456 | |||
457 | const AVFilter ff_vf_pad = { | ||
458 | .name = "pad", | ||
459 | .description = NULL_IF_CONFIG_SMALL("Pad the input video."), | ||
460 | .priv_size = sizeof(PadContext), | ||
461 | .priv_class = &pad_class, | ||
462 | FILTER_INPUTS(avfilter_vf_pad_inputs), | ||
463 | FILTER_OUTPUTS(avfilter_vf_pad_outputs), | ||
464 | FILTER_QUERY_FUNC2(query_formats), | ||
465 | }; | ||
466 |