FFmpeg
Loading...
Searching...
No Matches
scale_eval.c
Go to the documentation of this file.
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"
26#include "libavutil/pixdesc.h"
27
28static 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
57
58static int eval_dimension(void *log_ctx, const char *expr, const double *var_values,
59 int in_dimension, int *out_dimension)
60{
61 double res;
62 int ret = av_expr_parse_and_eval(&res, expr, var_names, var_values,
63 NULL, NULL, NULL, NULL, NULL, 0, log_ctx);
64 if (ret < 0)
65 return ret;
66 if (!(res >= INT32_MIN && res <= INT32_MAX))
67 return AVERROR(EINVAL);
68
69 *out_dimension = (int) res == 0 ? in_dimension : (int) res;
70 return 0;
71}
72
73int ff_scale_eval_dimensions(void *log_ctx,
74 const char *w_expr, const char *h_expr,
75 AVFilterLink *inlink, AVFilterLink *outlink,
76 int *ret_w, int *ret_h)
77{
79 const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
80 const char *expr;
81 int eval_w, eval_h;
82 int ret;
83 double var_values[VARS_NB], res;
84
85 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
86 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
87 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
88 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
89 var_values[VAR_A] = (double) inlink->w / inlink->h;
90 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
92 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
93 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
94 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
95 var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
96 var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
97
98 /* evaluate width and height */
99 av_expr_parse_and_eval(&res, (expr = w_expr),
100 var_names, var_values,
101 NULL, NULL, NULL, NULL, NULL, 0, log_ctx);
102 var_values[VAR_OUT_W] = var_values[VAR_OW] = trunc(res) == 0 ? inlink->w : trunc(res);
103
104 if ((ret = eval_dimension(log_ctx, (expr = h_expr), var_values, inlink->h, &eval_h)) < 0)
105 goto fail;
106
107 var_values[VAR_OUT_H] = var_values[VAR_OH] = eval_h;
108 /* evaluate again the width, as it may depend on the output height */
109 if ((ret = eval_dimension(log_ctx, (expr = w_expr), var_values, inlink->w, &eval_w)) < 0)
110 goto fail;
111
112 *ret_w = eval_w;
113 *ret_h = eval_h;
114
115 return 0;
116
117fail:
118 av_log(log_ctx, AV_LOG_ERROR,
119 "Error when evaluating the expression '%s'.\n"
120 "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
121 expr, w_expr, h_expr);
122 return ret;
123}
124
126 int *ret_w, int *ret_h,
127 int force_original_aspect_ratio, int force_divisible_by,
128 double w_adj)
129{
130 int64_t w, h;
131 int64_t factor_w, factor_h;
132
133 w = *ret_w;
134 h = *ret_h;
135
136 /* Check if it is requested that the result has to be divisible by some
137 * factor (w or h = -n with n being the factor). */
138 factor_w = 1;
139 factor_h = 1;
140 if (w < -1) {
141 factor_w = -w;
142 }
143 if (h < -1) {
144 factor_h = -h;
145 }
146
147 if (w < 0 && h < 0) {
148 w = inlink->w * w_adj;
149 h = inlink->h;
150 }
151
152 /* Make sure that the result is divisible by the factor we determined
153 * earlier. If no factor was set, nothing will happen as the default
154 * factor is 1 */
155 if (w < 0)
156 w = av_rescale(h, inlink->w * w_adj, inlink->h * factor_w) * factor_w;
157 if (h < 0)
158 h = av_rescale(w, inlink->h, inlink->w * w_adj * factor_h) * factor_h;
159
160 /* Note that force_original_aspect_ratio may overwrite the previous set
161 * dimensions so that it is not divisible by the set factors anymore
162 * unless force_divisible_by is defined as well */
163 if (force_original_aspect_ratio != SCALE_FORCE_OAR_DISABLE) {
164 // Including force_divisible_by here rounds to the nearest multiple of it.
165 int64_t tmp_w = av_rescale(h, inlink->w * w_adj, inlink->h * (int64_t)force_divisible_by)
166 * force_divisible_by;
167 int64_t tmp_h = av_rescale(w, inlink->h, inlink->w * w_adj * (int64_t)force_divisible_by)
168 * force_divisible_by;
169
170 if (force_original_aspect_ratio == SCALE_FORCE_OAR_DECREASE) {
171 w = FFMIN(tmp_w, w);
172 h = FFMIN(tmp_h, h);
173 if (force_divisible_by > 1) {
174 // round down in case provided w or h is not divisible.
175 w = w / force_divisible_by * force_divisible_by;
176 h = h / force_divisible_by * force_divisible_by;
177 }
178 } else { // SCALE_FORCE_OAR_INCREASE
179 w = FFMAX(tmp_w, w);
180 h = FFMAX(tmp_h, h);
181 if (force_divisible_by > 1) {
182 // round up in case provided w or h is not divisible.
183 w = (w + force_divisible_by - 1) / force_divisible_by * force_divisible_by;
184 h = (h + force_divisible_by - 1) / force_divisible_by * force_divisible_by;
185 }
186 }
187 }
188
189 if ((int32_t)w != w || (int32_t)h != h)
190 return AVERROR(EINVAL);
191
192 if (w <= 0 || h <= 0) {
193 av_log(inlink->dst, AV_LOG_ERROR,
194 "Rescaled dimensions %"PRId64"x%"PRId64" are invalid, "
195 "output dimensions must be positive.\n", w, h);
196 return AVERROR(EINVAL);
197 }
198
199 *ret_w = w;
200 *ret_h = h;
201
202 return 0;
203}
int32_t
@ VAR_VSUB
Definition boxblur.c:42
@ VARS_NB
Definition boxblur.c:43
@ VAR_HSUB
Definition boxblur.c:41
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float trunc(float a)
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition eval.c:839
simple arithmetic expression evaluator
@ VAR_IW
Definition f_select.c:147
@ VAR_IH
Definition f_select.c:146
int ret
Definition ffplay.c:391
#define fail
Definition test.h:479
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
const char * desc
Definition libsvtav1.c:83
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define NAN
var_name
Definition noise.c:46
static const char *const var_names[]
Definition noise.c:30
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
@ VAR_OUT_H
Definition scale_eval.c:47
@ VAR_SAR
Definition scale_eval.c:49
@ VAR_OH
Definition scale_eval.c:47
@ VAR_IN_W
Definition scale_eval.c:44
@ VAR_OW
Definition scale_eval.c:46
@ VAR_OHSUB
Definition scale_eval.c:53
@ VAR_A
Definition scale_eval.c:48
@ VAR_OUT_W
Definition scale_eval.c:46
@ VAR_OVSUB
Definition scale_eval.c:54
@ VAR_IN_H
Definition scale_eval.c:45
@ VAR_DAR
Definition scale_eval.c:50
static int eval_dimension(void *log_ctx, const char *expr, const double *var_values, int in_dimension, int *out_dimension)
Definition scale_eval.c:58
int ff_scale_adjust_dimensions(AVFilterLink *inlink, int *ret_w, int *ret_h, int force_original_aspect_ratio, int force_divisible_by, double w_adj)
Transform evaluated width and height obtained from ff_scale_eval_dimensions into actual target width ...
Definition scale_eval.c:125
int ff_scale_eval_dimensions(void *log_ctx, const char *w_expr, const char *h_expr, AVFilterLink *inlink, AVFilterLink *outlink, int *ret_w, int *ret_h)
Parse and evaluate string expressions for width and height.
Definition scale_eval.c:73
@ SCALE_FORCE_OAR_DISABLE
Definition scale_eval.h:25
@ SCALE_FORCE_OAR_DECREASE
Definition scale_eval.h:26
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
#define av_log(a,...)