| 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 | #include <stdint.h> | ||
| 22 | #include "scale_eval.h" | ||
| 23 | #include "libavutil/eval.h" | ||
| 24 | #include "libavutil/mathematics.h" | ||
| 25 | #include "libavutil/pixdesc.h" | ||
| 26 | |||
| 27 | static const char *const var_names[] = { | ||
| 28 | "in_w", "iw", | ||
| 29 | "in_h", "ih", | ||
| 30 | "out_w", "ow", | ||
| 31 | "out_h", "oh", | ||
| 32 | "a", | ||
| 33 | "sar", | ||
| 34 | "dar", | ||
| 35 | "hsub", | ||
| 36 | "vsub", | ||
| 37 | "ohsub", | ||
| 38 | "ovsub", | ||
| 39 | NULL | ||
| 40 | }; | ||
| 41 | |||
| 42 | enum var_name { | ||
| 43 | VAR_IN_W, VAR_IW, | ||
| 44 | VAR_IN_H, VAR_IH, | ||
| 45 | VAR_OUT_W, VAR_OW, | ||
| 46 | VAR_OUT_H, VAR_OH, | ||
| 47 | VAR_A, | ||
| 48 | VAR_SAR, | ||
| 49 | VAR_DAR, | ||
| 50 | VAR_HSUB, | ||
| 51 | VAR_VSUB, | ||
| 52 | VAR_OHSUB, | ||
| 53 | VAR_OVSUB, | ||
| 54 | VARS_NB | ||
| 55 | }; | ||
| 56 | |||
| 57 | ✗ | int ff_scale_eval_dimensions(void *log_ctx, | |
| 58 | const char *w_expr, const char *h_expr, | ||
| 59 | AVFilterLink *inlink, AVFilterLink *outlink, | ||
| 60 | int *ret_w, int *ret_h) | ||
| 61 | { | ||
| 62 | ✗ | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 63 | ✗ | const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format); | |
| 64 | const char *expr; | ||
| 65 | int eval_w, eval_h; | ||
| 66 | int ret; | ||
| 67 | double var_values[VARS_NB], res; | ||
| 68 | |||
| 69 | ✗ | var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w; | |
| 70 | ✗ | var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h; | |
| 71 | ✗ | var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN; | |
| 72 | ✗ | var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN; | |
| 73 | ✗ | var_values[VAR_A] = (double) inlink->w / inlink->h; | |
| 74 | ✗ | var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? | |
| 75 | ✗ | (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1; | |
| 76 | ✗ | var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR]; | |
| 77 | ✗ | var_values[VAR_HSUB] = 1 << desc->log2_chroma_w; | |
| 78 | ✗ | var_values[VAR_VSUB] = 1 << desc->log2_chroma_h; | |
| 79 | ✗ | var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w; | |
| 80 | ✗ | var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h; | |
| 81 | |||
| 82 | /* evaluate width and height */ | ||
| 83 | ✗ | av_expr_parse_and_eval(&res, (expr = w_expr), | |
| 84 | var_names, var_values, | ||
| 85 | NULL, NULL, NULL, NULL, NULL, 0, log_ctx); | ||
| 86 | ✗ | eval_w = var_values[VAR_OUT_W] = var_values[VAR_OW] = (int) res == 0 ? inlink->w : (int) res; | |
| 87 | |||
| 88 | ✗ | if ((ret = av_expr_parse_and_eval(&res, (expr = h_expr), | |
| 89 | var_names, var_values, | ||
| 90 | NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0) | ||
| 91 | ✗ | goto fail; | |
| 92 | ✗ | eval_h = var_values[VAR_OUT_H] = var_values[VAR_OH] = (int) res == 0 ? inlink->h : (int) res; | |
| 93 | /* evaluate again the width, as it may depend on the output height */ | ||
| 94 | ✗ | if ((ret = av_expr_parse_and_eval(&res, (expr = w_expr), | |
| 95 | var_names, var_values, | ||
| 96 | NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0) | ||
| 97 | ✗ | goto fail; | |
| 98 | ✗ | eval_w = (int) res == 0 ? inlink->w : (int) res; | |
| 99 | |||
| 100 | ✗ | *ret_w = eval_w; | |
| 101 | ✗ | *ret_h = eval_h; | |
| 102 | |||
| 103 | ✗ | return 0; | |
| 104 | |||
| 105 | ✗ | fail: | |
| 106 | ✗ | av_log(log_ctx, AV_LOG_ERROR, | |
| 107 | "Error when evaluating the expression '%s'.\n" | ||
| 108 | "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n", | ||
| 109 | expr, w_expr, h_expr); | ||
| 110 | ✗ | return ret; | |
| 111 | } | ||
| 112 | |||
| 113 | 6665 | int ff_scale_adjust_dimensions(AVFilterLink *inlink, | |
| 114 | int *ret_w, int *ret_h, | ||
| 115 | int force_original_aspect_ratio, int force_divisible_by, | ||
| 116 | double w_adj) | ||
| 117 | { | ||
| 118 | int64_t w, h; | ||
| 119 | int factor_w, factor_h; | ||
| 120 | |||
| 121 | 6665 | w = *ret_w; | |
| 122 | 6665 | h = *ret_h; | |
| 123 | |||
| 124 | /* Check if it is requested that the result has to be divisible by some | ||
| 125 | * factor (w or h = -n with n being the factor). */ | ||
| 126 | 6665 | factor_w = 1; | |
| 127 | 6665 | factor_h = 1; | |
| 128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6665 times.
|
6665 | if (w < -1) { |
| 129 | ✗ | factor_w = -w; | |
| 130 | } | ||
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6665 times.
|
6665 | if (h < -1) { |
| 132 | ✗ | factor_h = -h; | |
| 133 | } | ||
| 134 | |||
| 135 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 6665 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6665 | if (w < 0 && h < 0) { |
| 136 | ✗ | w = inlink->w * w_adj; | |
| 137 | ✗ | h = inlink->h; | |
| 138 | } | ||
| 139 | |||
| 140 | /* Make sure that the result is divisible by the factor we determined | ||
| 141 | * earlier. If no factor was set, nothing will happen as the default | ||
| 142 | * factor is 1 */ | ||
| 143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6665 times.
|
6665 | if (w < 0) |
| 144 | ✗ | w = av_rescale(h, inlink->w * w_adj, inlink->h * factor_w) * factor_w; | |
| 145 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6664 times.
|
6665 | if (h < 0) |
| 146 | 1 | h = av_rescale(w, inlink->h, inlink->w * w_adj * factor_h) * factor_h; | |
| 147 | |||
| 148 | /* Note that force_original_aspect_ratio may overwrite the previous set | ||
| 149 | * dimensions so that it is not divisible by the set factors anymore | ||
| 150 | * unless force_divisible_by is defined as well */ | ||
| 151 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6665 times.
|
6665 | if (force_original_aspect_ratio != SCALE_FORCE_OAR_DISABLE) { |
| 152 | // Including force_divisible_by here rounds to the nearest multiple of it. | ||
| 153 | ✗ | int64_t tmp_w = av_rescale(h, inlink->w * w_adj, inlink->h * (int64_t)force_divisible_by) | |
| 154 | ✗ | * force_divisible_by; | |
| 155 | ✗ | int64_t tmp_h = av_rescale(w, inlink->h, inlink->w * w_adj * (int64_t)force_divisible_by) | |
| 156 | ✗ | * force_divisible_by; | |
| 157 | |||
| 158 | ✗ | if (force_original_aspect_ratio == SCALE_FORCE_OAR_DECREASE) { | |
| 159 | ✗ | w = FFMIN(tmp_w, w); | |
| 160 | ✗ | h = FFMIN(tmp_h, h); | |
| 161 | ✗ | if (force_divisible_by > 1) { | |
| 162 | // round down in case provided w or h is not divisible. | ||
| 163 | ✗ | w = w / force_divisible_by * force_divisible_by; | |
| 164 | ✗ | h = h / force_divisible_by * force_divisible_by; | |
| 165 | } | ||
| 166 | } else { // SCALE_FORCE_OAR_INCREASE | ||
| 167 | ✗ | w = FFMAX(tmp_w, w); | |
| 168 | ✗ | h = FFMAX(tmp_h, h); | |
| 169 | ✗ | if (force_divisible_by > 1) { | |
| 170 | // round up in case provided w or h is not divisible. | ||
| 171 | ✗ | w = (w + force_divisible_by - 1) / force_divisible_by * force_divisible_by; | |
| 172 | ✗ | h = (h + force_divisible_by - 1) / force_divisible_by * force_divisible_by; | |
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 |
2/4✓ Branch 0 taken 6665 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6665 times.
|
6665 | if ((int32_t)w != w || (int32_t)h != h) |
| 178 | ✗ | return AVERROR(EINVAL); | |
| 179 | |||
| 180 | 6665 | *ret_w = w; | |
| 181 | 6665 | *ret_h = h; | |
| 182 | |||
| 183 | 6665 | return 0; | |
| 184 | } | ||
| 185 |