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.

218 lines
6.2KB

  1. /*
  2. * Copyright (c) 2010 Bobby Bingham
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file
  21. * aspect ratio modification video filters
  22. */
  23. #include <float.h>
  24. #include "libavutil/common.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/parseutils.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. typedef struct {
  33. const AVClass *class;
  34. AVRational aspect;
  35. char *ratio_str;
  36. int max;
  37. #if FF_API_OLD_FILTER_OPTS
  38. float aspect_num, aspect_den;
  39. #endif
  40. } AspectContext;
  41. #if FF_API_OLD_FILTER_OPTS
  42. static av_cold int init(AVFilterContext *ctx, const char *args)
  43. {
  44. AspectContext *s = ctx->priv;
  45. static const char *shorthand[] = { "ratio", "max", NULL };
  46. char c;
  47. int ret;
  48. AVRational q;
  49. if (args && sscanf(args, "%d:%d%c", &q.num, &q.den, &c) == 2) {
  50. s->ratio_str = av_strdup(args);
  51. av_log(ctx, AV_LOG_WARNING,
  52. "num:den syntax is deprecated, please use num/den or named options instead\n");
  53. } else if ((ret = av_opt_set_from_string(s, args, shorthand, "=", ":")) < 0) {
  54. return ret;
  55. }
  56. if (s->ratio_str) {
  57. ret = av_parse_ratio(&s->aspect, s->ratio_str, s->max, 0, ctx);
  58. if (ret < 0 || s->aspect.num < 0 || s->aspect.den <= 0) {
  59. av_log(ctx, AV_LOG_ERROR,
  60. "Invalid string '%s' for aspect ratio\n", args);
  61. return AVERROR(EINVAL);
  62. }
  63. }
  64. return 0;
  65. }
  66. #endif
  67. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  68. {
  69. AspectContext *aspect = link->dst->priv;
  70. frame->sample_aspect_ratio = aspect->aspect;
  71. return ff_filter_frame(link->dst->outputs[0], frame);
  72. }
  73. #define OFFSET(x) offsetof(AspectContext, x)
  74. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  75. #if CONFIG_SETDAR_FILTER
  76. static int setdar_config_props(AVFilterLink *inlink)
  77. {
  78. AspectContext *aspect = inlink->dst->priv;
  79. AVRational dar = aspect->aspect;
  80. if (aspect->aspect.num && aspect->aspect.den) {
  81. av_reduce(&aspect->aspect.num, &aspect->aspect.den,
  82. aspect->aspect.num * inlink->h,
  83. aspect->aspect.den * inlink->w, 100);
  84. inlink->sample_aspect_ratio = aspect->aspect;
  85. } else {
  86. inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
  87. dar = (AVRational){ inlink->w, inlink->h };
  88. }
  89. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
  90. inlink->w, inlink->h, dar.num, dar.den,
  91. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
  92. return 0;
  93. }
  94. static const AVOption setdar_options[] = {
  95. {"max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  96. {"ratio", "display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  97. {"r", "display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  98. {"dar", "display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  99. {NULL}
  100. };
  101. AVFILTER_DEFINE_CLASS(setdar);
  102. static const AVFilterPad avfilter_vf_setdar_inputs[] = {
  103. {
  104. .name = "default",
  105. .type = AVMEDIA_TYPE_VIDEO,
  106. .config_props = setdar_config_props,
  107. .get_video_buffer = ff_null_get_video_buffer,
  108. .filter_frame = filter_frame,
  109. },
  110. { NULL }
  111. };
  112. static const AVFilterPad avfilter_vf_setdar_outputs[] = {
  113. {
  114. .name = "default",
  115. .type = AVMEDIA_TYPE_VIDEO,
  116. },
  117. { NULL }
  118. };
  119. AVFilter avfilter_vf_setdar = {
  120. .name = "setdar",
  121. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  122. #if FF_API_OLD_FILTER_OPTS
  123. .init = init,
  124. #endif
  125. .priv_size = sizeof(AspectContext),
  126. .priv_class = &setdar_class,
  127. .inputs = avfilter_vf_setdar_inputs,
  128. .outputs = avfilter_vf_setdar_outputs,
  129. };
  130. #endif /* CONFIG_SETDAR_FILTER */
  131. #if CONFIG_SETSAR_FILTER
  132. static int setsar_config_props(AVFilterLink *inlink)
  133. {
  134. AspectContext *aspect = inlink->dst->priv;
  135. inlink->sample_aspect_ratio = aspect->aspect;
  136. return 0;
  137. }
  138. static const AVOption setsar_options[] = {
  139. {"max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  140. {"ratio", "sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  141. {"r", "sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  142. {"sar", "sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  143. {NULL}
  144. };
  145. AVFILTER_DEFINE_CLASS(setsar);
  146. static const AVFilterPad avfilter_vf_setsar_inputs[] = {
  147. {
  148. .name = "default",
  149. .type = AVMEDIA_TYPE_VIDEO,
  150. .config_props = setsar_config_props,
  151. .get_video_buffer = ff_null_get_video_buffer,
  152. .filter_frame = filter_frame,
  153. },
  154. { NULL }
  155. };
  156. static const AVFilterPad avfilter_vf_setsar_outputs[] = {
  157. {
  158. .name = "default",
  159. .type = AVMEDIA_TYPE_VIDEO,
  160. },
  161. { NULL }
  162. };
  163. AVFilter avfilter_vf_setsar = {
  164. .name = "setsar",
  165. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  166. #if FF_API_OLD_FILTER_OPTS
  167. .init = init,
  168. #endif
  169. .priv_size = sizeof(AspectContext),
  170. .priv_class = &setsar_class,
  171. .inputs = avfilter_vf_setsar_inputs,
  172. .outputs = avfilter_vf_setsar_outputs,
  173. };
  174. #endif /* CONFIG_SETSAR_FILTER */