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