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.

258 lines
7.9KB

  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 * w, sar.den * 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 *inlink)
  112. {
  113. AspectContext *s = inlink->dst->priv;
  114. AVRational dar;
  115. AVRational old_dar;
  116. AVRational old_sar = inlink->sample_aspect_ratio;
  117. int ret;
  118. if ((ret = get_aspect_ratio(inlink, &s->dar)))
  119. return ret;
  120. if (s->dar.num && s->dar.den) {
  121. av_reduce(&s->sar.num, &s->sar.den,
  122. s->dar.num * inlink->h,
  123. s->dar.den * inlink->w, INT_MAX);
  124. inlink->sample_aspect_ratio = s->sar;
  125. dar = s->dar;
  126. } else {
  127. inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
  128. dar = (AVRational){ inlink->w, inlink->h };
  129. }
  130. compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
  131. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
  132. inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
  133. dar.num, dar.den, inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
  134. return 0;
  135. }
  136. static const AVOption setdar_options[] = {
  137. { "dar", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  138. { "ratio", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  139. { "r", "set display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  140. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  141. { NULL }
  142. };
  143. AVFILTER_DEFINE_CLASS(setdar);
  144. static const AVFilterPad avfilter_vf_setdar_inputs[] = {
  145. {
  146. .name = "default",
  147. .type = AVMEDIA_TYPE_VIDEO,
  148. .config_props = setdar_config_props,
  149. .filter_frame = filter_frame,
  150. },
  151. { NULL }
  152. };
  153. static const AVFilterPad avfilter_vf_setdar_outputs[] = {
  154. {
  155. .name = "default",
  156. .type = AVMEDIA_TYPE_VIDEO,
  157. },
  158. { NULL }
  159. };
  160. AVFilter ff_vf_setdar = {
  161. .name = "setdar",
  162. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  163. .priv_size = sizeof(AspectContext),
  164. .priv_class = &setdar_class,
  165. .inputs = avfilter_vf_setdar_inputs,
  166. .outputs = avfilter_vf_setdar_outputs,
  167. };
  168. #endif /* CONFIG_SETDAR_FILTER */
  169. #if CONFIG_SETSAR_FILTER
  170. static int setsar_config_props(AVFilterLink *inlink)
  171. {
  172. AspectContext *s = inlink->dst->priv;
  173. AVRational old_sar = inlink->sample_aspect_ratio;
  174. AVRational old_dar, dar;
  175. int ret;
  176. if ((ret = get_aspect_ratio(inlink, &s->sar)))
  177. return ret;
  178. inlink->sample_aspect_ratio = s->sar;
  179. compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
  180. compute_dar(&dar, s->sar, inlink->w, inlink->h);
  181. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
  182. inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
  183. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den, dar.num, dar.den);
  184. return 0;
  185. }
  186. static const AVOption setsar_options[] = {
  187. { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  188. { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  189. { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  190. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  191. { NULL }
  192. };
  193. AVFILTER_DEFINE_CLASS(setsar);
  194. static const AVFilterPad avfilter_vf_setsar_inputs[] = {
  195. {
  196. .name = "default",
  197. .type = AVMEDIA_TYPE_VIDEO,
  198. .config_props = setsar_config_props,
  199. .filter_frame = filter_frame,
  200. },
  201. { NULL }
  202. };
  203. static const AVFilterPad avfilter_vf_setsar_outputs[] = {
  204. {
  205. .name = "default",
  206. .type = AVMEDIA_TYPE_VIDEO,
  207. },
  208. { NULL }
  209. };
  210. AVFilter ff_vf_setsar = {
  211. .name = "setsar",
  212. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  213. .priv_size = sizeof(AspectContext),
  214. .priv_class = &setsar_class,
  215. .inputs = avfilter_vf_setsar_inputs,
  216. .outputs = avfilter_vf_setsar_outputs,
  217. };
  218. #endif /* CONFIG_SETSAR_FILTER */