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.

234 lines
7.5KB

  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 "avfilter.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. typedef struct {
  34. const AVClass *class;
  35. AVRational aspect;
  36. int max;
  37. #if FF_API_OLD_FILTER_OPTS
  38. float aspect_den;
  39. #endif
  40. char *ratio_str;
  41. } AspectContext;
  42. static av_cold int init(AVFilterContext *ctx)
  43. {
  44. AspectContext *s = ctx->priv;
  45. int ret;
  46. #if FF_API_OLD_FILTER_OPTS
  47. if (s->ratio_str && s->aspect_den > 0) {
  48. double num;
  49. av_log(ctx, AV_LOG_WARNING,
  50. "num:den syntax is deprecated, please use num/den or named options instead\n");
  51. ret = av_expr_parse_and_eval(&num, s->ratio_str, NULL, NULL,
  52. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  53. if (ret < 0) {
  54. av_log(ctx, AV_LOG_ERROR, "Unable to parse ratio numerator \"%s\"\n", s->ratio_str);
  55. return AVERROR(EINVAL);
  56. }
  57. s->aspect = av_d2q(num / s->aspect_den, s->max);
  58. } else
  59. #endif
  60. if (s->ratio_str) {
  61. ret = av_parse_ratio(&s->aspect, s->ratio_str, s->max, 0, ctx);
  62. if (ret < 0 || s->aspect.num < 0 || s->aspect.den <= 0) {
  63. av_log(ctx, AV_LOG_ERROR,
  64. "Invalid string '%s' for aspect ratio\n", s->ratio_str);
  65. return AVERROR(EINVAL);
  66. }
  67. }
  68. return 0;
  69. }
  70. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  71. {
  72. AspectContext *aspect = link->dst->priv;
  73. frame->sample_aspect_ratio = aspect->aspect;
  74. return ff_filter_frame(link->dst->outputs[0], frame);
  75. }
  76. #define OFFSET(x) offsetof(AspectContext, x)
  77. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  78. static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
  79. {
  80. if (sar.num && sar.den) {
  81. av_reduce(&dar->num, &dar->den, sar.num * w, sar.den * h, INT_MAX);
  82. } else {
  83. av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
  84. }
  85. }
  86. #if CONFIG_SETDAR_FILTER
  87. static int setdar_config_props(AVFilterLink *inlink)
  88. {
  89. AspectContext *aspect = inlink->dst->priv;
  90. AVRational dar = aspect->aspect, old_dar;
  91. AVRational old_sar = inlink->sample_aspect_ratio;
  92. if (aspect->aspect.num && aspect->aspect.den) {
  93. av_reduce(&aspect->aspect.num, &aspect->aspect.den,
  94. aspect->aspect.num * inlink->h,
  95. aspect->aspect.den * inlink->w, INT_MAX);
  96. inlink->sample_aspect_ratio = aspect->aspect;
  97. } else {
  98. inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
  99. dar = (AVRational){ inlink->w, inlink->h };
  100. }
  101. compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
  102. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
  103. inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
  104. dar.num, dar.den, inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
  105. return 0;
  106. }
  107. static const AVOption setdar_options[] = {
  108. { "dar", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  109. { "ratio", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  110. { "r", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  111. #if FF_API_OLD_FILTER_OPTS
  112. { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  113. #endif
  114. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  115. { NULL }
  116. };
  117. AVFILTER_DEFINE_CLASS(setdar);
  118. static const AVFilterPad avfilter_vf_setdar_inputs[] = {
  119. {
  120. .name = "default",
  121. .type = AVMEDIA_TYPE_VIDEO,
  122. .config_props = setdar_config_props,
  123. .get_video_buffer = ff_null_get_video_buffer,
  124. .filter_frame = filter_frame,
  125. },
  126. { NULL }
  127. };
  128. static const AVFilterPad avfilter_vf_setdar_outputs[] = {
  129. {
  130. .name = "default",
  131. .type = AVMEDIA_TYPE_VIDEO,
  132. },
  133. { NULL }
  134. };
  135. AVFilter avfilter_vf_setdar = {
  136. .name = "setdar",
  137. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  138. .init = init,
  139. .priv_size = sizeof(AspectContext),
  140. .priv_class = &setdar_class,
  141. .inputs = avfilter_vf_setdar_inputs,
  142. .outputs = avfilter_vf_setdar_outputs,
  143. };
  144. #endif /* CONFIG_SETDAR_FILTER */
  145. #if CONFIG_SETSAR_FILTER
  146. static int setsar_config_props(AVFilterLink *inlink)
  147. {
  148. AspectContext *aspect = inlink->dst->priv;
  149. AVRational old_sar = inlink->sample_aspect_ratio;
  150. AVRational old_dar, dar;
  151. inlink->sample_aspect_ratio = aspect->aspect;
  152. compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
  153. compute_dar(&dar, aspect->aspect, inlink->w, inlink->h);
  154. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
  155. inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
  156. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den, dar.num, dar.den);
  157. return 0;
  158. }
  159. static const AVOption setsar_options[] = {
  160. { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  161. { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  162. { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  163. #if FF_API_OLD_FILTER_OPTS
  164. { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  165. #endif
  166. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  167. { NULL }
  168. };
  169. AVFILTER_DEFINE_CLASS(setsar);
  170. static const AVFilterPad avfilter_vf_setsar_inputs[] = {
  171. {
  172. .name = "default",
  173. .type = AVMEDIA_TYPE_VIDEO,
  174. .config_props = setsar_config_props,
  175. .get_video_buffer = ff_null_get_video_buffer,
  176. .filter_frame = filter_frame,
  177. },
  178. { NULL }
  179. };
  180. static const AVFilterPad avfilter_vf_setsar_outputs[] = {
  181. {
  182. .name = "default",
  183. .type = AVMEDIA_TYPE_VIDEO,
  184. },
  185. { NULL }
  186. };
  187. AVFilter avfilter_vf_setsar = {
  188. .name = "setsar",
  189. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  190. .init = init,
  191. .priv_size = sizeof(AspectContext),
  192. .priv_class = &setsar_class,
  193. .inputs = avfilter_vf_setsar_inputs,
  194. .outputs = avfilter_vf_setsar_outputs,
  195. };
  196. #endif /* CONFIG_SETSAR_FILTER */