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.

178 lines
5.7KB

  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_eval.h"
  22. #include "libavutil/eval.h"
  23. #include "libavutil/mathematics.h"
  24. #include "libavutil/pixdesc.h"
  25. static const char *const var_names[] = {
  26. "in_w", "iw",
  27. "in_h", "ih",
  28. "out_w", "ow",
  29. "out_h", "oh",
  30. "a",
  31. "sar",
  32. "dar",
  33. "hsub",
  34. "vsub",
  35. "ohsub",
  36. "ovsub",
  37. NULL
  38. };
  39. enum var_name {
  40. VAR_IN_W, VAR_IW,
  41. VAR_IN_H, VAR_IH,
  42. VAR_OUT_W, VAR_OW,
  43. VAR_OUT_H, VAR_OH,
  44. VAR_A,
  45. VAR_SAR,
  46. VAR_DAR,
  47. VAR_HSUB,
  48. VAR_VSUB,
  49. VAR_OHSUB,
  50. VAR_OVSUB,
  51. VARS_NB
  52. };
  53. int ff_scale_eval_dimensions(void *log_ctx,
  54. const char *w_expr, const char *h_expr,
  55. AVFilterLink *inlink, AVFilterLink *outlink,
  56. int *ret_w, int *ret_h)
  57. {
  58. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  59. const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
  60. const char *expr;
  61. int eval_w, eval_h;
  62. int ret;
  63. double var_values[VARS_NB], res;
  64. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  65. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  66. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  67. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  68. var_values[VAR_A] = (double) inlink->w / inlink->h;
  69. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  70. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  71. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  72. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  73. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  74. var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
  75. var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
  76. /* evaluate width and height */
  77. av_expr_parse_and_eval(&res, (expr = w_expr),
  78. var_names, var_values,
  79. NULL, NULL, NULL, NULL, NULL, 0, log_ctx);
  80. eval_w = var_values[VAR_OUT_W] = var_values[VAR_OW] = (int) res == 0 ? inlink->w : (int) res;
  81. if ((ret = av_expr_parse_and_eval(&res, (expr = h_expr),
  82. var_names, var_values,
  83. NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0)
  84. goto fail;
  85. eval_h = var_values[VAR_OUT_H] = var_values[VAR_OH] = (int) res == 0 ? inlink->h : (int) res;
  86. /* evaluate again the width, as it may depend on the output height */
  87. if ((ret = av_expr_parse_and_eval(&res, (expr = w_expr),
  88. var_names, var_values,
  89. NULL, NULL, NULL, NULL, NULL, 0, log_ctx)) < 0)
  90. goto fail;
  91. eval_w = (int) res == 0 ? inlink->w : (int) res;
  92. *ret_w = eval_w;
  93. *ret_h = eval_h;
  94. return 0;
  95. fail:
  96. av_log(log_ctx, AV_LOG_ERROR,
  97. "Error when evaluating the expression '%s'.\n"
  98. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  99. expr, w_expr, h_expr);
  100. return ret;
  101. }
  102. int ff_scale_adjust_dimensions(AVFilterLink *inlink,
  103. int *ret_w, int *ret_h,
  104. int force_original_aspect_ratio, int force_divisible_by)
  105. {
  106. int w, h;
  107. int factor_w, factor_h;
  108. w = *ret_w;
  109. h = *ret_h;
  110. /* Check if it is requested that the result has to be divisible by some
  111. * factor (w or h = -n with n being the factor). */
  112. factor_w = 1;
  113. factor_h = 1;
  114. if (w < -1) {
  115. factor_w = -w;
  116. }
  117. if (h < -1) {
  118. factor_h = -h;
  119. }
  120. if (w < 0 && h < 0) {
  121. w = inlink->w;
  122. h = inlink->h;
  123. }
  124. /* Make sure that the result is divisible by the factor we determined
  125. * earlier. If no factor was set, nothing will happen as the default
  126. * factor is 1 */
  127. if (w < 0)
  128. w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
  129. if (h < 0)
  130. h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
  131. /* Note that force_original_aspect_ratio may overwrite the previous set
  132. * dimensions so that it is not divisible by the set factors anymore
  133. * unless force_divisible_by is defined as well */
  134. if (force_original_aspect_ratio) {
  135. int tmp_w = av_rescale(h, inlink->w, inlink->h);
  136. int tmp_h = av_rescale(w, inlink->h, inlink->w);
  137. if (force_original_aspect_ratio == 1) {
  138. w = FFMIN(tmp_w, w);
  139. h = FFMIN(tmp_h, h);
  140. if (force_divisible_by > 1) {
  141. // round down
  142. w = w / force_divisible_by * force_divisible_by;
  143. h = h / force_divisible_by * force_divisible_by;
  144. }
  145. } else {
  146. w = FFMAX(tmp_w, w);
  147. h = FFMAX(tmp_h, h);
  148. if (force_divisible_by > 1) {
  149. // round up
  150. w = (w + force_divisible_by - 1) / force_divisible_by * force_divisible_by;
  151. h = (h + force_divisible_by - 1) / force_divisible_by * force_divisible_by;
  152. }
  153. }
  154. }
  155. *ret_w = w;
  156. *ret_h = h;
  157. return 0;
  158. }