vf_aspect.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. char *ratio_expr;
  58. } AspectContext;
  59. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  60. {
  61. AspectContext *s = link->dst->priv;
  62. frame->sample_aspect_ratio = s->sar;
  63. return ff_filter_frame(link->dst->outputs[0], frame);
  64. }
  65. #define OFFSET(x) offsetof(AspectContext, x)
  66. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  67. static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
  68. {
  69. if (sar.num && sar.den) {
  70. av_reduce(&dar->num, &dar->den, sar.num * (int64_t)w, sar.den * (int64_t)h, INT_MAX);
  71. } else {
  72. av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
  73. }
  74. }
  75. static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
  76. {
  77. AVFilterContext *ctx = inlink->dst;
  78. AspectContext *s = inlink->dst->priv;
  79. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  80. double var_values[VARS_NB], res;
  81. int ret;
  82. var_values[VAR_W] = inlink->w;
  83. var_values[VAR_H] = inlink->h;
  84. var_values[VAR_A] = (double) inlink->w / inlink->h;
  85. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  86. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  87. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  88. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  89. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  90. /* evaluate new aspect ratio*/
  91. ret = av_expr_parse_and_eval(&res, s->ratio_expr,
  92. var_names, var_values,
  93. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  94. if (ret < 0) {
  95. ret = av_parse_ratio(aspect_ratio, s->ratio_expr, s->max, 0, ctx);
  96. } else
  97. *aspect_ratio = av_d2q(res, s->max);
  98. if (ret < 0) {
  99. av_log(ctx, AV_LOG_ERROR,
  100. "Error when evaluating the expression '%s'\n", s->ratio_expr);
  101. return ret;
  102. }
  103. if (aspect_ratio->num < 0 || aspect_ratio->den <= 0) {
  104. av_log(ctx, AV_LOG_ERROR,
  105. "Invalid string '%s' for aspect ratio\n", s->ratio_expr);
  106. return AVERROR(EINVAL);
  107. }
  108. return 0;
  109. }
  110. #if CONFIG_SETDAR_FILTER
  111. static int setdar_config_props(AVFilterLink *outlink)
  112. {
  113. AVFilterContext *ctx = outlink->src;
  114. AVFilterLink *inlink = ctx->inputs[0];
  115. AspectContext *s = ctx->priv;
  116. AVRational dar;
  117. AVRational old_dar;
  118. AVRational old_sar = inlink->sample_aspect_ratio;
  119. int ret;
  120. if ((ret = get_aspect_ratio(inlink, &s->dar)))
  121. return ret;
  122. if (s->dar.num && s->dar.den) {
  123. av_reduce(&s->sar.num, &s->sar.den,
  124. s->dar.num * inlink->h,
  125. s->dar.den * inlink->w, INT_MAX);
  126. outlink->sample_aspect_ratio = s->sar;
  127. dar = s->dar;
  128. } else {
  129. outlink->sample_aspect_ratio = (AVRational){ 1, 1 };
  130. dar = (AVRational){ inlink->w, inlink->h };
  131. }
  132. compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
  133. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
  134. inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
  135. dar.num, dar.den, outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den);
  136. return 0;
  137. }
  138. static const AVOption setdar_options[] = {
  139. { "dar", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  140. { "ratio", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  141. { "r", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  142. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  143. { NULL }
  144. };
  145. AVFILTER_DEFINE_CLASS(setdar);
  146. static const AVFilterPad avfilter_vf_setdar_inputs[] = {
  147. {
  148. .name = "default",
  149. .type = AVMEDIA_TYPE_VIDEO,
  150. .filter_frame = filter_frame,
  151. },
  152. { NULL }
  153. };
  154. static const AVFilterPad avfilter_vf_setdar_outputs[] = {
  155. {
  156. .name = "default",
  157. .type = AVMEDIA_TYPE_VIDEO,
  158. .config_props = setdar_config_props,
  159. },
  160. { NULL }
  161. };
  162. AVFilter ff_vf_setdar = {
  163. .name = "setdar",
  164. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  165. .priv_size = sizeof(AspectContext),
  166. .priv_class = &setdar_class,
  167. .inputs = avfilter_vf_setdar_inputs,
  168. .outputs = avfilter_vf_setdar_outputs,
  169. };
  170. #endif /* CONFIG_SETDAR_FILTER */
  171. #if CONFIG_SETSAR_FILTER
  172. static int setsar_config_props(AVFilterLink *outlink)
  173. {
  174. AVFilterContext *ctx = outlink->src;
  175. AVFilterLink *inlink = ctx->inputs[0];
  176. AspectContext *s = ctx->priv;
  177. AVRational old_sar = inlink->sample_aspect_ratio;
  178. AVRational old_dar, dar;
  179. int ret;
  180. if ((ret = get_aspect_ratio(inlink, &s->sar)))
  181. return ret;
  182. outlink->sample_aspect_ratio = s->sar;
  183. compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
  184. compute_dar(&dar, s->sar, inlink->w, inlink->h);
  185. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
  186. inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
  187. outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den, dar.num, dar.den);
  188. return 0;
  189. }
  190. static const AVOption setsar_options[] = {
  191. { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  192. { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  193. { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  194. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  195. { NULL }
  196. };
  197. AVFILTER_DEFINE_CLASS(setsar);
  198. static const AVFilterPad avfilter_vf_setsar_inputs[] = {
  199. {
  200. .name = "default",
  201. .type = AVMEDIA_TYPE_VIDEO,
  202. .filter_frame = filter_frame,
  203. },
  204. { NULL }
  205. };
  206. static const AVFilterPad avfilter_vf_setsar_outputs[] = {
  207. {
  208. .name = "default",
  209. .type = AVMEDIA_TYPE_VIDEO,
  210. .config_props = setsar_config_props,
  211. },
  212. { NULL }
  213. };
  214. AVFilter ff_vf_setsar = {
  215. .name = "setsar",
  216. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  217. .priv_size = sizeof(AspectContext),
  218. .priv_class = &setsar_class,
  219. .inputs = avfilter_vf_setsar_inputs,
  220. .outputs = avfilter_vf_setsar_outputs,
  221. };
  222. #endif /* CONFIG_SETSAR_FILTER */