You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.6KB

  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. #include <stdint.h>
  21. #include "scale.h"
  22. #include "libavutil/eval.h"
  23. #include "libavutil/mathematics.h"
  24. #include "libavutil/pixdesc.h"
  25. static const char *const var_names[] = {
  26. "PI",
  27. "PHI",
  28. "E",
  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. enum var_name {
  43. VAR_PI,
  44. VAR_PHI,
  45. VAR_E,
  46. VAR_IN_W, VAR_IW,
  47. VAR_IN_H, VAR_IH,
  48. VAR_OUT_W, VAR_OW,
  49. VAR_OUT_H, VAR_OH,
  50. VAR_A,
  51. VAR_SAR,
  52. VAR_DAR,
  53. VAR_HSUB,
  54. VAR_VSUB,
  55. VAR_OHSUB,
  56. VAR_OVSUB,
  57. VARS_NB
  58. };
  59. int ff_scale_eval_dimensions(void *log_ctx,
  60. const char *w_expr, const char *h_expr,
  61. AVFilterLink *inlink, AVFilterLink *outlink,
  62. int *ret_w, int *ret_h)
  63. {
  64. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  65. const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
  66. const char *expr;
  67. int w, h;
  68. int factor_w, factor_h;
  69. int eval_w, eval_h;
  70. int ret;
  71. double var_values[VARS_NB], res;
  72. var_values[VAR_PI] = M_PI;
  73. var_values[VAR_PHI] = M_PHI;
  74. var_values[VAR_E] = M_E;
  75. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  76. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  77. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  78. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  79. var_values[VAR_A] = (double) inlink->w / inlink->h;
  80. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  81. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  82. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  83. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  84. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  85. var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
  86. var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
  87. /* evaluate width and height */
  88. av_expr_parse_and_eval(&res, (expr = w_expr),
  89. var_names, var_values,
  90. NULL, NULL, NULL, NULL, NULL, 0, log_ctx);
  91. eval_w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  92. if ((ret = av_expr_parse_and_eval(&res, (expr = h_expr),
  93. var_names, var_values,
  94. NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0)
  95. goto fail;
  96. eval_h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  97. /* evaluate again the width, as it may depend on the output height */
  98. if ((ret = av_expr_parse_and_eval(&res, (expr = w_expr),
  99. var_names, var_values,
  100. NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0)
  101. goto fail;
  102. eval_w = res;
  103. w = eval_w;
  104. h = eval_h;
  105. /* Check if it is requested that the result has to be divisible by a some
  106. * factor (w or h = -n with n being the factor). */
  107. factor_w = 1;
  108. factor_h = 1;
  109. if (w < -1) {
  110. factor_w = -w;
  111. }
  112. if (h < -1) {
  113. factor_h = -h;
  114. }
  115. if (w < 0 && h < 0)
  116. eval_w = eval_h = 0;
  117. if (!(w = eval_w))
  118. w = inlink->w;
  119. if (!(h = eval_h))
  120. h = inlink->h;
  121. /* Make sure that the result is divisible by the factor we determined
  122. * earlier. If no factor was set, it is nothing will happen as the default
  123. * factor is 1 */
  124. if (w < 0)
  125. w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
  126. if (h < 0)
  127. h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
  128. *ret_w = w;
  129. *ret_h = h;
  130. return 0;
  131. fail:
  132. av_log(log_ctx, AV_LOG_ERROR,
  133. "Error when evaluating the expression '%s'.\n"
  134. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  135. expr, w_expr, h_expr);
  136. return ret;
  137. }