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.

238 lines
7.4KB

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