scale.c 6.6 KB

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