FFmpeg  4.3
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 <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 {
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 {
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 
114  int *ret_w, int *ret_h,
115  int force_original_aspect_ratio, int force_divisible_by)
116 {
117  int w, h;
118  int factor_w, factor_h;
119 
120  w = *ret_w;
121  h = *ret_h;
122 
123  /* Check if it is requested that the result has to be divisible by some
124  * factor (w or h = -n with n being the factor). */
125  factor_w = 1;
126  factor_h = 1;
127  if (w < -1) {
128  factor_w = -w;
129  }
130  if (h < -1) {
131  factor_h = -h;
132  }
133 
134  if (w < 0 && h < 0) {
135  w = inlink->w;
136  h = inlink->h;
137  }
138 
139  /* Make sure that the result is divisible by the factor we determined
140  * earlier. If no factor was set, nothing will happen as the default
141  * factor is 1 */
142  if (w < 0)
143  w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
144  if (h < 0)
145  h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
146 
147  /* Note that force_original_aspect_ratio may overwrite the previous set
148  * dimensions so that it is not divisible by the set factors anymore
149  * unless force_divisible_by is defined as well */
150  if (force_original_aspect_ratio) {
151  int tmp_w = av_rescale(h, inlink->w, inlink->h);
152  int tmp_h = av_rescale(w, inlink->h, inlink->w);
153 
154  if (force_original_aspect_ratio == 1) {
155  w = FFMIN(tmp_w, w);
156  h = FFMIN(tmp_h, h);
157  if (force_divisible_by > 1) {
158  // round down
159  w = w / force_divisible_by * force_divisible_by;
160  h = h / force_divisible_by * force_divisible_by;
161  }
162  } else {
163  w = FFMAX(tmp_w, w);
164  h = FFMAX(tmp_h, h);
165  if (force_divisible_by > 1) {
166  // round up
167  w = (w + force_divisible_by - 1) / force_divisible_by * force_divisible_by;
168  h = (h + force_divisible_by - 1) / force_divisible_by * force_divisible_by;
169  }
170  }
171  }
172 
173  *ret_w = w;
174  *ret_h = h;
175 
176  return 0;
177 }
VAR_IH
@ VAR_IH
Definition: scale_eval.c:44
VAR_OUT_W
@ VAR_OUT_W
Definition: scale_eval.c:45
VAR_OH
@ VAR_OH
Definition: scale_eval.c:46
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2549
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
pixdesc.h
ff_scale_eval_dimensions
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:57
mathematics.h
VAR_OVSUB
@ VAR_OVSUB
Definition: scale_eval.c:53
VARS_NB
@ VARS_NB
Definition: scale_eval.c:54
VAR_A
@ VAR_A
Definition: scale_eval.c:47
VAR_SAR
@ VAR_SAR
Definition: scale_eval.c:48
fail
#define fail()
Definition: checkasm.h:123
VAR_OUT_H
@ VAR_OUT_H
Definition: scale_eval.c:46
VAR_OHSUB
@ VAR_OHSUB
Definition: scale_eval.c:52
VAR_VSUB
@ VAR_VSUB
Definition: scale_eval.c:51
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
NAN
#define NAN
Definition: mathematics.h:64
VAR_HSUB
@ VAR_HSUB
Definition: scale_eval.c:50
NULL
#define NULL
Definition: coverity.c:32
eval.h
desc
const char * desc
Definition: nvenc.c:79
av_expr_parse_and_eval
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:776
var_name
var_name
Definition: aeval.c:46
VAR_DAR
@ VAR_DAR
Definition: scale_eval.c:49
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
var_names
static const char *const var_names[]
Definition: scale_eval.c:27
scale_eval.h
VAR_IW
@ VAR_IW
Definition: scale_eval.c:43
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
av_rescale
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
VAR_OW
@ VAR_OW
Definition: scale_eval.c:45
ret
ret
Definition: filter_design.txt:187
w
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new please make sure they are as small as space on each network bandwidth and so on benefit from smaller test cases Also keep in mind older checkouts use existing sample that means in practice generally do not remove or overwrite files as it likely would break older checkouts or releases Also all needed samples for a commit should be ideally before the push If you need an account for frequently uploading samples or you wish to help others by doing that send a mail to ffmpeg devel rsync vauL Duo ug o o w
Definition: fate.txt:150
ff_scale_adjust_dimensions
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:113
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
VAR_IN_H
@ VAR_IN_H
Definition: scale_eval.c:44
VAR_IN_W
@ VAR_IN_W
Definition: scale_eval.c:43
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
h
h
Definition: vp9dsp_template.c:2038
int
int
Definition: ffmpeg_filter.c:192
AVPixFmtDescriptor::log2_chroma_h
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101