]> git.sesse.net Git - ffmpeg/blob - libavfilter/scale_eval.c
avcodec/simple_idct_template: fix integer overflow
[ffmpeg] / libavfilter / scale_eval.c
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 /**
58  * This must be kept in sync with var_names so that it is always a
59  * complete list of var_names with the scale2ref specific names
60  * appended. scale2ref values must appear in the order they appear
61  * in the var_name_scale2ref enum but also be below all of the
62  * non-scale2ref specific values.
63  */
64 static const char *const var_names_scale2ref[] = {
65     "in_w",   "iw",
66     "in_h",   "ih",
67     "out_w",  "ow",
68     "out_h",  "oh",
69     "a",
70     "sar",
71     "dar",
72     "hsub",
73     "vsub",
74     "ohsub",
75     "ovsub",
76     "main_w",
77     "main_h",
78     "main_a",
79     "main_sar",
80     "main_dar", "mdar",
81     "main_hsub",
82     "main_vsub",
83     NULL
84 };
85
86 enum var_name_scale2ref {
87     VAR_S2R_MAIN_W,
88     VAR_S2R_MAIN_H,
89     VAR_S2R_MAIN_A,
90     VAR_S2R_MAIN_SAR,
91     VAR_S2R_MAIN_DAR, VAR_S2R_MDAR,
92     VAR_S2R_MAIN_HSUB,
93     VAR_S2R_MAIN_VSUB,
94     VARS_S2R_NB
95 };
96
97 int ff_scale_eval_dimensions(void *log_ctx,
98     const char *w_expr, const char *h_expr,
99     AVFilterLink *inlink, AVFilterLink *outlink,
100     int *ret_w, int *ret_h)
101 {
102     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
103     const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
104     const char *expr;
105     int eval_w, eval_h;
106     int ret;
107     const char scale2ref = outlink->src->nb_inputs == 2 && outlink->src->inputs[1] == inlink;
108     double var_values[VARS_NB + VARS_S2R_NB], res;
109     const AVPixFmtDescriptor *main_desc;
110     const AVFilterLink *main_link;
111     const char *const *names = scale2ref ? var_names_scale2ref : var_names;
112
113     if (scale2ref) {
114         main_link = outlink->src->inputs[0];
115         main_desc = av_pix_fmt_desc_get(main_link->format);
116     }
117
118     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
119     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
120     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
121     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
122     var_values[VAR_A]     = (double) inlink->w / inlink->h;
123     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
124         (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
125     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
126     var_values[VAR_HSUB]  = 1 << desc->log2_chroma_w;
127     var_values[VAR_VSUB]  = 1 << desc->log2_chroma_h;
128     var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
129     var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
130
131     if (scale2ref) {
132         var_values[VARS_NB + VAR_S2R_MAIN_W] = main_link->w;
133         var_values[VARS_NB + VAR_S2R_MAIN_H] = main_link->h;
134         var_values[VARS_NB + VAR_S2R_MAIN_A] = (double) main_link->w / main_link->h;
135         var_values[VARS_NB + VAR_S2R_MAIN_SAR] = main_link->sample_aspect_ratio.num ?
136             (double) main_link->sample_aspect_ratio.num / main_link->sample_aspect_ratio.den : 1;
137         var_values[VARS_NB + VAR_S2R_MAIN_DAR] = var_values[VARS_NB + VAR_S2R_MDAR] =
138             var_values[VARS_NB + VAR_S2R_MAIN_A] * var_values[VARS_NB + VAR_S2R_MAIN_SAR];
139         var_values[VARS_NB + VAR_S2R_MAIN_HSUB] = 1 << main_desc->log2_chroma_w;
140         var_values[VARS_NB + VAR_S2R_MAIN_VSUB] = 1 << main_desc->log2_chroma_h;
141     }
142
143     /* evaluate width and height */
144     av_expr_parse_and_eval(&res, (expr = w_expr),
145                            names, var_values,
146                            NULL, NULL, NULL, NULL, NULL, 0, log_ctx);
147     eval_w = var_values[VAR_OUT_W] = var_values[VAR_OW] = (int) res == 0 ? inlink->w : (int) res;
148
149     if ((ret = av_expr_parse_and_eval(&res, (expr = h_expr),
150                                       names, var_values,
151                                       NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0)
152         goto fail;
153     eval_h = var_values[VAR_OUT_H] = var_values[VAR_OH] = (int) res == 0 ? inlink->h : (int) res;
154     /* evaluate again the width, as it may depend on the output height */
155     if ((ret = av_expr_parse_and_eval(&res, (expr = w_expr),
156                                       names, var_values,
157                                       NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0)
158         goto fail;
159     eval_w = (int) res == 0 ? inlink->w : (int) res;
160
161     *ret_w = eval_w;
162     *ret_h = eval_h;
163
164     return 0;
165
166 fail:
167     av_log(log_ctx, AV_LOG_ERROR,
168            "Error when evaluating the expression '%s'.\n"
169            "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
170            expr, w_expr, h_expr);
171     return ret;
172 }
173
174 int ff_scale_adjust_dimensions(AVFilterLink *inlink,
175     int *ret_w, int *ret_h,
176     int force_original_aspect_ratio, int force_divisible_by)
177 {
178     int w, h;
179     int factor_w, factor_h;
180
181     w = *ret_w;
182     h = *ret_h;
183
184     /* Check if it is requested that the result has to be divisible by some
185      * factor (w or h = -n with n being the factor). */
186     factor_w = 1;
187     factor_h = 1;
188     if (w < -1) {
189         factor_w = -w;
190     }
191     if (h < -1) {
192         factor_h = -h;
193     }
194
195     if (w < 0 && h < 0) {
196         w = inlink->w;
197         h = inlink->h;
198     }
199
200     /* Make sure that the result is divisible by the factor we determined
201      * earlier. If no factor was set, nothing will happen as the default
202      * factor is 1 */
203     if (w < 0)
204         w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
205     if (h < 0)
206         h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
207
208     /* Note that force_original_aspect_ratio may overwrite the previous set
209      * dimensions so that it is not divisible by the set factors anymore
210      * unless force_divisible_by is defined as well */
211     if (force_original_aspect_ratio) {
212         int tmp_w = av_rescale(h, inlink->w, inlink->h);
213         int tmp_h = av_rescale(w, inlink->h, inlink->w);
214
215         if (force_original_aspect_ratio == 1) {
216              w = FFMIN(tmp_w, w);
217              h = FFMIN(tmp_h, h);
218              if (force_divisible_by > 1) {
219                  // round down
220                  w = w / force_divisible_by * force_divisible_by;
221                  h = h / force_divisible_by * force_divisible_by;
222              }
223         } else {
224              w = FFMAX(tmp_w, w);
225              h = FFMAX(tmp_h, h);
226              if (force_divisible_by > 1) {
227                  // round up
228                  w = (w + force_divisible_by - 1) / force_divisible_by * force_divisible_by;
229                  h = (h + force_divisible_by - 1) / force_divisible_by * force_divisible_by;
230              }
231         }
232     }
233
234     *ret_w = w;
235     *ret_h = h;
236
237     return 0;
238 }