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.

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