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.

313 lines
9.3KB

  1. /*
  2. * Copyright (c) 2010 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. * @file
  22. * aspect ratio modification video filters
  23. */
  24. #include <float.h>
  25. #include "libavutil/common.h"
  26. #include "libavutil/eval.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/parseutils.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. static const char *const var_names[] = {
  35. "PI",
  36. "PHI",
  37. "E",
  38. "w",
  39. "h",
  40. "a", "dar",
  41. "sar",
  42. "hsub",
  43. "vsub",
  44. NULL
  45. };
  46. enum var_name {
  47. VAR_PI,
  48. VAR_PHI,
  49. VAR_E,
  50. VAR_W,
  51. VAR_H,
  52. VAR_A, VAR_DAR,
  53. VAR_SAR,
  54. VAR_HSUB,
  55. VAR_VSUB,
  56. VARS_NB
  57. };
  58. typedef struct {
  59. const AVClass *class;
  60. AVRational dar;
  61. AVRational sar;
  62. int max;
  63. #if FF_API_OLD_FILTER_OPTS
  64. float aspect_den;
  65. #endif
  66. char *ratio_expr;
  67. } AspectContext;
  68. static av_cold int init(AVFilterContext *ctx)
  69. {
  70. AspectContext *s = ctx->priv;
  71. int ret;
  72. #if FF_API_OLD_FILTER_OPTS
  73. if (s->ratio_expr && s->aspect_den > 0) {
  74. double num;
  75. av_log(ctx, AV_LOG_WARNING,
  76. "num:den syntax is deprecated, please use num/den or named options instead\n");
  77. ret = av_expr_parse_and_eval(&num, s->ratio_expr, NULL, NULL,
  78. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  79. if (ret < 0) {
  80. av_log(ctx, AV_LOG_ERROR, "Unable to parse ratio numerator \"%s\"\n", s->ratio_expr);
  81. return AVERROR(EINVAL);
  82. }
  83. s->sar = s->dar = av_d2q(num / s->aspect_den, s->max);
  84. }
  85. #endif
  86. return 0;
  87. }
  88. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  89. {
  90. AspectContext *s = link->dst->priv;
  91. frame->sample_aspect_ratio = s->sar;
  92. return ff_filter_frame(link->dst->outputs[0], frame);
  93. }
  94. #define OFFSET(x) offsetof(AspectContext, x)
  95. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  96. static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
  97. {
  98. if (sar.num && sar.den) {
  99. av_reduce(&dar->num, &dar->den, sar.num * w, sar.den * h, INT_MAX);
  100. } else {
  101. av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
  102. }
  103. }
  104. static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
  105. {
  106. AVFilterContext *ctx = inlink->dst;
  107. AspectContext *s = inlink->dst->priv;
  108. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  109. double var_values[VARS_NB], res;
  110. int ret;
  111. var_values[VAR_PI] = M_PI;
  112. var_values[VAR_PHI] = M_PHI;
  113. var_values[VAR_E] = M_E;
  114. var_values[VAR_W] = inlink->w;
  115. var_values[VAR_H] = inlink->h;
  116. var_values[VAR_A] = (double) inlink->w / inlink->h;
  117. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  118. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  119. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  120. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  121. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  122. /* evaluate new aspect ratio*/
  123. ret = av_expr_parse_and_eval(&res, s->ratio_expr,
  124. var_names, var_values,
  125. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  126. if (ret < 0) {
  127. ret = av_parse_ratio(aspect_ratio, s->ratio_expr, s->max, 0, ctx);
  128. } else
  129. *aspect_ratio = av_d2q(res, s->max);
  130. if (ret < 0) {
  131. av_log(ctx, AV_LOG_ERROR,
  132. "Error when evaluating the expression '%s'\n", s->ratio_expr);
  133. return ret;
  134. }
  135. if (aspect_ratio->num < 0 || aspect_ratio->den <= 0) {
  136. av_log(ctx, AV_LOG_ERROR,
  137. "Invalid string '%s' for aspect ratio\n", s->ratio_expr);
  138. return AVERROR(EINVAL);
  139. }
  140. return 0;
  141. }
  142. #if CONFIG_SETDAR_FILTER
  143. static int setdar_config_props(AVFilterLink *inlink)
  144. {
  145. AspectContext *s = inlink->dst->priv;
  146. AVRational dar;
  147. AVRational old_dar;
  148. AVRational old_sar = inlink->sample_aspect_ratio;
  149. int ret;
  150. #if FF_API_OLD_FILTER_OPTS
  151. if (!(s->ratio_expr && s->aspect_den > 0)) {
  152. #endif
  153. if ((ret = get_aspect_ratio(inlink, &s->dar)))
  154. return ret;
  155. #if FF_API_OLD_FILTER_OPTS
  156. }
  157. #endif
  158. if (s->dar.num && s->dar.den) {
  159. av_reduce(&s->sar.num, &s->sar.den,
  160. s->dar.num * inlink->h,
  161. s->dar.den * inlink->w, INT_MAX);
  162. inlink->sample_aspect_ratio = s->sar;
  163. dar = s->dar;
  164. } else {
  165. inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
  166. dar = (AVRational){ inlink->w, inlink->h };
  167. }
  168. compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
  169. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
  170. inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
  171. dar.num, dar.den, inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
  172. return 0;
  173. }
  174. static const AVOption setdar_options[] = {
  175. { "dar", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  176. { "ratio", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  177. { "r", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  178. #if FF_API_OLD_FILTER_OPTS
  179. { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  180. #endif
  181. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  182. { NULL }
  183. };
  184. AVFILTER_DEFINE_CLASS(setdar);
  185. static const AVFilterPad avfilter_vf_setdar_inputs[] = {
  186. {
  187. .name = "default",
  188. .type = AVMEDIA_TYPE_VIDEO,
  189. .config_props = setdar_config_props,
  190. .filter_frame = filter_frame,
  191. },
  192. { NULL }
  193. };
  194. static const AVFilterPad avfilter_vf_setdar_outputs[] = {
  195. {
  196. .name = "default",
  197. .type = AVMEDIA_TYPE_VIDEO,
  198. },
  199. { NULL }
  200. };
  201. AVFilter ff_vf_setdar = {
  202. .name = "setdar",
  203. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  204. .init = init,
  205. .priv_size = sizeof(AspectContext),
  206. .priv_class = &setdar_class,
  207. .inputs = avfilter_vf_setdar_inputs,
  208. .outputs = avfilter_vf_setdar_outputs,
  209. };
  210. #endif /* CONFIG_SETDAR_FILTER */
  211. #if CONFIG_SETSAR_FILTER
  212. static int setsar_config_props(AVFilterLink *inlink)
  213. {
  214. AspectContext *s = inlink->dst->priv;
  215. AVRational old_sar = inlink->sample_aspect_ratio;
  216. AVRational old_dar, dar;
  217. int ret;
  218. #if FF_API_OLD_FILTER_OPTS
  219. if (!(s->ratio_expr && s->aspect_den > 0)) {
  220. #endif
  221. if ((ret = get_aspect_ratio(inlink, &s->sar)))
  222. return ret;
  223. #if FF_API_OLD_FILTER_OPTS
  224. }
  225. #endif
  226. inlink->sample_aspect_ratio = s->sar;
  227. compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
  228. compute_dar(&dar, s->sar, inlink->w, inlink->h);
  229. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
  230. inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
  231. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den, dar.num, dar.den);
  232. return 0;
  233. }
  234. static const AVOption setsar_options[] = {
  235. { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  236. { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  237. { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  238. #if FF_API_OLD_FILTER_OPTS
  239. { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  240. #endif
  241. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  242. { NULL }
  243. };
  244. AVFILTER_DEFINE_CLASS(setsar);
  245. static const AVFilterPad avfilter_vf_setsar_inputs[] = {
  246. {
  247. .name = "default",
  248. .type = AVMEDIA_TYPE_VIDEO,
  249. .config_props = setsar_config_props,
  250. .filter_frame = filter_frame,
  251. },
  252. { NULL }
  253. };
  254. static const AVFilterPad avfilter_vf_setsar_outputs[] = {
  255. {
  256. .name = "default",
  257. .type = AVMEDIA_TYPE_VIDEO,
  258. },
  259. { NULL }
  260. };
  261. AVFilter ff_vf_setsar = {
  262. .name = "setsar",
  263. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  264. .init = init,
  265. .priv_size = sizeof(AspectContext),
  266. .priv_class = &setsar_class,
  267. .inputs = avfilter_vf_setsar_inputs,
  268. .outputs = avfilter_vf_setsar_outputs,
  269. };
  270. #endif /* CONFIG_SETSAR_FILTER */