LCOV - code coverage report
Current view: top level - libavfilter - scale.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 52 62 83.9 %
Date: 2017-12-10 21:22:29 Functions: 1 1 100.0 %

          Line data    Source code
       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.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             :     "PI",
      29             :     "PHI",
      30             :     "E",
      31             :     "in_w",   "iw",
      32             :     "in_h",   "ih",
      33             :     "out_w",  "ow",
      34             :     "out_h",  "oh",
      35             :     "a",
      36             :     "sar",
      37             :     "dar",
      38             :     "hsub",
      39             :     "vsub",
      40             :     "ohsub",
      41             :     "ovsub",
      42             :     NULL
      43             : };
      44             : 
      45             : enum var_name {
      46             :     VAR_PI,
      47             :     VAR_PHI,
      48             :     VAR_E,
      49             :     VAR_IN_W,   VAR_IW,
      50             :     VAR_IN_H,   VAR_IH,
      51             :     VAR_OUT_W,  VAR_OW,
      52             :     VAR_OUT_H,  VAR_OH,
      53             :     VAR_A,
      54             :     VAR_SAR,
      55             :     VAR_DAR,
      56             :     VAR_HSUB,
      57             :     VAR_VSUB,
      58             :     VAR_OHSUB,
      59             :     VAR_OVSUB,
      60             :     VARS_NB
      61             : };
      62             : 
      63             : /**
      64             :  * This must be kept in sync with var_names so that it is always a
      65             :  * complete list of var_names with the scale2ref specific names
      66             :  * appended. scale2ref values must appear in the order they appear
      67             :  * in the var_name_scale2ref enum but also be below all of the
      68             :  * non-scale2ref specific values.
      69             :  */
      70             : static const char *const var_names_scale2ref[] = {
      71             :     "PI",
      72             :     "PHI",
      73             :     "E",
      74             :     "in_w",   "iw",
      75             :     "in_h",   "ih",
      76             :     "out_w",  "ow",
      77             :     "out_h",  "oh",
      78             :     "a",
      79             :     "sar",
      80             :     "dar",
      81             :     "hsub",
      82             :     "vsub",
      83             :     "ohsub",
      84             :     "ovsub",
      85             :     "main_w",
      86             :     "main_h",
      87             :     "main_a",
      88             :     "main_sar",
      89             :     "main_dar", "mdar",
      90             :     "main_hsub",
      91             :     "main_vsub",
      92             :     NULL
      93             : };
      94             : 
      95             : enum var_name_scale2ref {
      96             :     VAR_S2R_MAIN_W,
      97             :     VAR_S2R_MAIN_H,
      98             :     VAR_S2R_MAIN_A,
      99             :     VAR_S2R_MAIN_SAR,
     100             :     VAR_S2R_MAIN_DAR, VAR_S2R_MDAR,
     101             :     VAR_S2R_MAIN_HSUB,
     102             :     VAR_S2R_MAIN_VSUB,
     103             :     VARS_S2R_NB
     104             : };
     105             : 
     106        2994 : int ff_scale_eval_dimensions(void *log_ctx,
     107             :     const char *w_expr, const char *h_expr,
     108             :     AVFilterLink *inlink, AVFilterLink *outlink,
     109             :     int *ret_w, int *ret_h)
     110             : {
     111        2994 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     112        2994 :     const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
     113             :     const char *expr;
     114             :     int w, h;
     115             :     int factor_w, factor_h;
     116             :     int eval_w, eval_h;
     117             :     int ret;
     118        2994 :     const char scale2ref = outlink->src->nb_inputs == 2 && outlink->src->inputs[1] == inlink;
     119             :     double var_values[VARS_NB + VARS_S2R_NB], res;
     120             :     const AVPixFmtDescriptor *main_desc;
     121             :     const AVFilterLink *main_link;
     122        2994 :     const char *const *names = scale2ref ? var_names_scale2ref : var_names;
     123             : 
     124        2994 :     if (scale2ref) {
     125           1 :         main_link = outlink->src->inputs[0];
     126           1 :         main_desc = av_pix_fmt_desc_get(main_link->format);
     127             :     }
     128             : 
     129        2994 :     var_values[VAR_PI]    = M_PI;
     130        2994 :     var_values[VAR_PHI]   = M_PHI;
     131        2994 :     var_values[VAR_E]     = M_E;
     132        2994 :     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
     133        2994 :     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
     134        2994 :     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
     135        2994 :     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
     136        2994 :     var_values[VAR_A]     = (double) inlink->w / inlink->h;
     137        5988 :     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
     138        2994 :         (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
     139        2994 :     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
     140        2994 :     var_values[VAR_HSUB]  = 1 << desc->log2_chroma_w;
     141        2994 :     var_values[VAR_VSUB]  = 1 << desc->log2_chroma_h;
     142        2994 :     var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
     143        2994 :     var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
     144             : 
     145        2994 :     if (scale2ref) {
     146           1 :         var_values[VARS_NB + VAR_S2R_MAIN_W] = main_link->w;
     147           1 :         var_values[VARS_NB + VAR_S2R_MAIN_H] = main_link->h;
     148           1 :         var_values[VARS_NB + VAR_S2R_MAIN_A] = (double) main_link->w / main_link->h;
     149           2 :         var_values[VARS_NB + VAR_S2R_MAIN_SAR] = main_link->sample_aspect_ratio.num ?
     150           1 :             (double) main_link->sample_aspect_ratio.num / main_link->sample_aspect_ratio.den : 1;
     151           1 :         var_values[VARS_NB + VAR_S2R_MAIN_DAR] = var_values[VARS_NB + VAR_S2R_MDAR] =
     152           1 :             var_values[VARS_NB + VAR_S2R_MAIN_A] * var_values[VARS_NB + VAR_S2R_MAIN_SAR];
     153           1 :         var_values[VARS_NB + VAR_S2R_MAIN_HSUB] = 1 << main_desc->log2_chroma_w;
     154           1 :         var_values[VARS_NB + VAR_S2R_MAIN_VSUB] = 1 << main_desc->log2_chroma_h;
     155             :     }
     156             : 
     157             :     /* evaluate width and height */
     158        2994 :     av_expr_parse_and_eval(&res, (expr = w_expr),
     159             :                            names, var_values,
     160             :                            NULL, NULL, NULL, NULL, NULL, 0, log_ctx);
     161        2994 :     eval_w = var_values[VAR_OUT_W] = var_values[VAR_OW] = (int) res == 0 ? inlink->w : (int) res;
     162             : 
     163        2994 :     if ((ret = av_expr_parse_and_eval(&res, (expr = h_expr),
     164             :                                       names, var_values,
     165             :                                       NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0)
     166           0 :         goto fail;
     167        2994 :     eval_h = var_values[VAR_OUT_H] = var_values[VAR_OH] = (int) res == 0 ? inlink->h : (int) res;
     168             :     /* evaluate again the width, as it may depend on the output height */
     169        2994 :     if ((ret = av_expr_parse_and_eval(&res, (expr = w_expr),
     170             :                                       names, var_values,
     171             :                                       NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0)
     172           0 :         goto fail;
     173        2994 :     eval_w = (int) res == 0 ? inlink->w : (int) res;
     174             : 
     175        2994 :     w = eval_w;
     176        2994 :     h = eval_h;
     177             : 
     178             :     /* Check if it is requested that the result has to be divisible by a some
     179             :      * factor (w or h = -n with n being the factor). */
     180        2994 :     factor_w = 1;
     181        2994 :     factor_h = 1;
     182        2994 :     if (w < -1) {
     183           0 :         factor_w = -w;
     184             :     }
     185        2994 :     if (h < -1) {
     186           0 :         factor_h = -h;
     187             :     }
     188             : 
     189        2994 :     if (w < 0 && h < 0) {
     190           0 :         w = inlink->w;
     191           0 :         h = inlink->h;
     192             :     }
     193             : 
     194             :     /* Make sure that the result is divisible by the factor we determined
     195             :      * earlier. If no factor was set, it is nothing will happen as the default
     196             :      * factor is 1 */
     197        2994 :     if (w < 0)
     198           0 :         w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
     199        2994 :     if (h < 0)
     200           1 :         h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
     201             : 
     202        2994 :     *ret_w = w;
     203        2994 :     *ret_h = h;
     204             : 
     205        2994 :     return 0;
     206             : 
     207           0 : fail:
     208           0 :     av_log(log_ctx, AV_LOG_ERROR,
     209             :            "Error when evaluating the expression '%s'.\n"
     210             :            "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
     211             :            expr, w_expr, h_expr);
     212           0 :     return ret;
     213             : }

Generated by: LCOV version 1.13