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.

133 lines
4.4KB

  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 "libavutil/mathematics.h"
  24. #include "libavutil/parseutils.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct {
  29. AVRational ratio;
  30. } AspectContext;
  31. static av_cold int init(AVFilterContext *ctx, const char *args)
  32. {
  33. AspectContext *aspect = ctx->priv;
  34. aspect->ratio = (AVRational) {0, 1};
  35. if (args) {
  36. if (av_parse_ratio(&aspect->ratio, args, 100, 0, ctx) < 0 ||
  37. aspect->ratio.num < 0 || aspect->ratio.den <= 0) {
  38. av_log(ctx, AV_LOG_ERROR,
  39. "Invalid string '%s' for aspect ratio.\n", args);
  40. return AVERROR(EINVAL);
  41. }
  42. }
  43. av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->ratio.num, aspect->ratio.den);
  44. return 0;
  45. }
  46. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  47. {
  48. AspectContext *aspect = link->dst->priv;
  49. picref->video->sample_aspect_ratio = aspect->ratio;
  50. ff_start_frame(link->dst->outputs[0], picref);
  51. }
  52. #if CONFIG_SETDAR_FILTER
  53. static int setdar_config_props(AVFilterLink *inlink)
  54. {
  55. AspectContext *aspect = inlink->dst->priv;
  56. AVRational dar = aspect->ratio;
  57. av_reduce(&aspect->ratio.num, &aspect->ratio.den,
  58. aspect->ratio.num * inlink->h,
  59. aspect->ratio.den * inlink->w, 100);
  60. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
  61. inlink->w, inlink->h, dar.num, dar.den, aspect->ratio.num, aspect->ratio.den);
  62. inlink->sample_aspect_ratio = aspect->ratio;
  63. return 0;
  64. }
  65. AVFilter avfilter_vf_setdar = {
  66. .name = "setdar",
  67. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  68. .init = init,
  69. .priv_size = sizeof(AspectContext),
  70. .inputs = (const AVFilterPad[]) {{ .name = "default",
  71. .type = AVMEDIA_TYPE_VIDEO,
  72. .config_props = setdar_config_props,
  73. .get_video_buffer = ff_null_get_video_buffer,
  74. .start_frame = start_frame,
  75. .end_frame = ff_null_end_frame },
  76. { .name = NULL}},
  77. .outputs = (const AVFilterPad[]) {{ .name = "default",
  78. .type = AVMEDIA_TYPE_VIDEO, },
  79. { .name = NULL}},
  80. };
  81. #endif /* CONFIG_SETDAR_FILTER */
  82. #if CONFIG_SETSAR_FILTER
  83. static int setsar_config_props(AVFilterLink *inlink)
  84. {
  85. AspectContext *aspect = inlink->dst->priv;
  86. inlink->sample_aspect_ratio = aspect->ratio;
  87. return 0;
  88. }
  89. AVFilter avfilter_vf_setsar = {
  90. .name = "setsar",
  91. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  92. .init = init,
  93. .priv_size = sizeof(AspectContext),
  94. .inputs = (const AVFilterPad[]) {{ .name = "default",
  95. .type = AVMEDIA_TYPE_VIDEO,
  96. .config_props = setsar_config_props,
  97. .get_video_buffer = ff_null_get_video_buffer,
  98. .start_frame = start_frame,
  99. .end_frame = ff_null_end_frame },
  100. { .name = NULL}},
  101. .outputs = (const AVFilterPad[]) {{ .name = "default",
  102. .type = AVMEDIA_TYPE_VIDEO, },
  103. { .name = NULL}},
  104. };
  105. #endif /* CONFIG_SETSAR_FILTER */