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