FFmpeg  4.4.9
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"
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 {
55  VARS_NB
56 };
57 
58 static 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 
73 int 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 ?
91  (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
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 
117 fail:
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 {
129  int64_t w, h;
130  int64_t factor_w, factor_h;
131 
132  w = *ret_w;
133  h = *ret_h;
134 
135  /* Check if it is requested that the result has to be divisible by some
136  * factor (w or h = -n with n being the factor). */
137  factor_w = 1;
138  factor_h = 1;
139  if (w < -1) {
140  factor_w = -w;
141  }
142  if (h < -1) {
143  factor_h = -h;
144  }
145 
146  if (w < 0 && h < 0) {
147  w = inlink->w;
148  h = inlink->h;
149  }
150 
151  /* Make sure that the result is divisible by the factor we determined
152  * earlier. If no factor was set, nothing will happen as the default
153  * factor is 1 */
154  if (w < 0)
155  w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
156  if (h < 0)
157  h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
158 
159  /* Note that force_original_aspect_ratio may overwrite the previous set
160  * dimensions so that it is not divisible by the set factors anymore
161  * unless force_divisible_by is defined as well */
162  if (force_original_aspect_ratio) {
163  int64_t tmp_w = av_rescale(h, inlink->w, inlink->h);
164  int64_t tmp_h = av_rescale(w, inlink->h, inlink->w);
165 
166  if (force_original_aspect_ratio == 1) {
167  w = FFMIN(tmp_w, w);
168  h = FFMIN(tmp_h, h);
169  if (force_divisible_by > 1) {
170  // round down
171  w = w / force_divisible_by * force_divisible_by;
172  h = h / force_divisible_by * force_divisible_by;
173  }
174  } else {
175  w = FFMAX(tmp_w, w);
176  h = FFMAX(tmp_h, h);
177  if (force_divisible_by > 1) {
178  // round up
179  w = (w + force_divisible_by - 1) / force_divisible_by * force_divisible_by;
180  h = (h + force_divisible_by - 1) / force_divisible_by * force_divisible_by;
181  }
182  }
183  }
184 
185  if ((int32_t)w != w || (int32_t)h != h)
186  return AVERROR(EINVAL);
187 
188  if (w <= 0 || h <= 0) {
189  av_log(inlink->dst, AV_LOG_ERROR,
190  "Rescaled dimensions %"PRId64"x%"PRId64" are invalid, "
191  "output dimensions must be positive.\n", w, h);
192  return AVERROR(EINVAL);
193  }
194 
195  *ret_w = w;
196  *ret_h = h;
197 
198  return 0;
199 }
int32_t
#define fail()
Definition: checkasm.h:133
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static __device__ float trunc(float a)
Definition: cuda_runtime.h:179
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:791
simple arithmetic expression evaluator
int
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
const char * desc
Definition: libsvtav1.c:83
uint8_t w
Definition: llviddspenc.c:39
#define NAN
Definition: mathematics.h:64
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
@ VAR_VSUB
Definition: scale_eval.c:52
@ VAR_OUT_H
Definition: scale_eval.c:47
@ VARS_NB
Definition: scale_eval.c:55
@ VAR_SAR
Definition: scale_eval.c:49
@ VAR_OH
Definition: scale_eval.c:47
@ VAR_IN_W
Definition: scale_eval.c:44
@ VAR_IW
Definition: scale_eval.c:44
@ VAR_OW
Definition: scale_eval.c:46
@ VAR_OHSUB
Definition: scale_eval.c:53
@ VAR_IH
Definition: scale_eval.c:45
@ VAR_A
Definition: scale_eval.c:48
@ VAR_OUT_W
Definition: scale_eval.c:46
@ VAR_OVSUB
Definition: scale_eval.c:54
@ VAR_HSUB
Definition: scale_eval.c:51
@ VAR_IN_H
Definition: scale_eval.c:45
@ VAR_DAR
Definition: scale_eval.c:50
int ff_scale_adjust_dimensions(AVFilterLink *inlink, int *ret_w, int *ret_h, int force_original_aspect_ratio, int force_divisible_by)
Transform evaluated width and height obtained from ff_scale_eval_dimensions into actual target width ...
Definition: scale_eval.c:125
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
static const char *const var_names[]
Definition: scale_eval.c:28
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
var_name
Definition: setts_bsf.c:50
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
#define av_log(a,...)