vf_aspect.c 9.2 KB

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