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.

215 lines
6.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/opt.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_num, 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->aspect_num > 0 && s->aspect_den > 0) {
  48. av_log(ctx, AV_LOG_WARNING,
  49. "num:den syntax is deprecated, please use num/den or named options instead\n");
  50. s->aspect = av_d2q(s->aspect_num / s->aspect_den, s->max);
  51. }
  52. #endif
  53. if (s->ratio_str) {
  54. ret = av_parse_ratio(&s->aspect, s->ratio_str, s->max, 0, ctx);
  55. if (ret < 0 || s->aspect.num < 0 || s->aspect.den <= 0) {
  56. av_log(ctx, AV_LOG_ERROR,
  57. "Invalid string '%s' for aspect ratio\n", s->ratio_str);
  58. return AVERROR(EINVAL);
  59. }
  60. }
  61. return 0;
  62. }
  63. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  64. {
  65. AspectContext *aspect = link->dst->priv;
  66. frame->sample_aspect_ratio = aspect->aspect;
  67. return ff_filter_frame(link->dst->outputs[0], frame);
  68. }
  69. #define OFFSET(x) offsetof(AspectContext, x)
  70. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  71. #if CONFIG_SETDAR_FILTER
  72. static int setdar_config_props(AVFilterLink *inlink)
  73. {
  74. AspectContext *aspect = inlink->dst->priv;
  75. AVRational dar = aspect->aspect;
  76. if (aspect->aspect.num && aspect->aspect.den) {
  77. av_reduce(&aspect->aspect.num, &aspect->aspect.den,
  78. aspect->aspect.num * inlink->h,
  79. aspect->aspect.den * inlink->w, INT_MAX);
  80. inlink->sample_aspect_ratio = aspect->aspect;
  81. } else {
  82. inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
  83. dar = (AVRational){ inlink->w, inlink->h };
  84. }
  85. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
  86. inlink->w, inlink->h, dar.num, dar.den,
  87. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
  88. return 0;
  89. }
  90. static const AVOption setdar_options[] = {
  91. #if FF_API_OLD_FILTER_OPTS
  92. { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  93. { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  94. #endif
  95. { "dar", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  96. { "ratio", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  97. { "r", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  98. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, 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. .init = init,
  123. .priv_size = sizeof(AspectContext),
  124. .priv_class = &setdar_class,
  125. .inputs = avfilter_vf_setdar_inputs,
  126. .outputs = avfilter_vf_setdar_outputs,
  127. };
  128. #endif /* CONFIG_SETDAR_FILTER */
  129. #if CONFIG_SETSAR_FILTER
  130. static int setsar_config_props(AVFilterLink *inlink)
  131. {
  132. AspectContext *aspect = inlink->dst->priv;
  133. inlink->sample_aspect_ratio = aspect->aspect;
  134. return 0;
  135. }
  136. static const AVOption setsar_options[] = {
  137. #if FF_API_OLD_FILTER_OPTS
  138. { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  139. { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  140. #endif
  141. { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  142. { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  143. { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
  144. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  145. { NULL }
  146. };
  147. AVFILTER_DEFINE_CLASS(setsar);
  148. static const AVFilterPad avfilter_vf_setsar_inputs[] = {
  149. {
  150. .name = "default",
  151. .type = AVMEDIA_TYPE_VIDEO,
  152. .config_props = setsar_config_props,
  153. .get_video_buffer = ff_null_get_video_buffer,
  154. .filter_frame = filter_frame,
  155. },
  156. { NULL }
  157. };
  158. static const AVFilterPad avfilter_vf_setsar_outputs[] = {
  159. {
  160. .name = "default",
  161. .type = AVMEDIA_TYPE_VIDEO,
  162. },
  163. { NULL }
  164. };
  165. AVFilter avfilter_vf_setsar = {
  166. .name = "setsar",
  167. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  168. #if FF_API_OLD_FILTER_OPTS
  169. .init = init,
  170. #endif
  171. .priv_size = sizeof(AspectContext),
  172. .priv_class = &setsar_class,
  173. .inputs = avfilter_vf_setsar_inputs,
  174. .outputs = avfilter_vf_setsar_outputs,
  175. };
  176. #endif /* CONFIG_SETSAR_FILTER */