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.

134 lines
4.5KB

  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 int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  47. {
  48. AspectContext *aspect = link->dst->priv;
  49. picref->video->sample_aspect_ratio = aspect->ratio;
  50. link->cur_buf = NULL;
  51. return ff_start_frame(link->dst->outputs[0], picref);
  52. }
  53. #if CONFIG_SETDAR_FILTER
  54. static int setdar_config_props(AVFilterLink *inlink)
  55. {
  56. AspectContext *aspect = inlink->dst->priv;
  57. AVRational dar = aspect->ratio;
  58. av_reduce(&aspect->ratio.num, &aspect->ratio.den,
  59. aspect->ratio.num * inlink->h,
  60. aspect->ratio.den * inlink->w, 100);
  61. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
  62. inlink->w, inlink->h, dar.num, dar.den, aspect->ratio.num, aspect->ratio.den);
  63. inlink->sample_aspect_ratio = aspect->ratio;
  64. return 0;
  65. }
  66. AVFilter avfilter_vf_setdar = {
  67. .name = "setdar",
  68. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  69. .init = init,
  70. .priv_size = sizeof(AspectContext),
  71. .inputs = (const AVFilterPad[]) {{ .name = "default",
  72. .type = AVMEDIA_TYPE_VIDEO,
  73. .config_props = setdar_config_props,
  74. .get_video_buffer = ff_null_get_video_buffer,
  75. .start_frame = start_frame,
  76. .end_frame = ff_null_end_frame },
  77. { .name = NULL}},
  78. .outputs = (const AVFilterPad[]) {{ .name = "default",
  79. .type = AVMEDIA_TYPE_VIDEO, },
  80. { .name = NULL}},
  81. };
  82. #endif /* CONFIG_SETDAR_FILTER */
  83. #if CONFIG_SETSAR_FILTER
  84. static int setsar_config_props(AVFilterLink *inlink)
  85. {
  86. AspectContext *aspect = inlink->dst->priv;
  87. inlink->sample_aspect_ratio = aspect->ratio;
  88. return 0;
  89. }
  90. AVFilter avfilter_vf_setsar = {
  91. .name = "setsar",
  92. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  93. .init = init,
  94. .priv_size = sizeof(AspectContext),
  95. .inputs = (const AVFilterPad[]) {{ .name = "default",
  96. .type = AVMEDIA_TYPE_VIDEO,
  97. .config_props = setsar_config_props,
  98. .get_video_buffer = ff_null_get_video_buffer,
  99. .start_frame = start_frame,
  100. .end_frame = ff_null_end_frame },
  101. { .name = NULL}},
  102. .outputs = (const AVFilterPad[]) {{ .name = "default",
  103. .type = AVMEDIA_TYPE_VIDEO, },
  104. { .name = NULL}},
  105. };
  106. #endif /* CONFIG_SETSAR_FILTER */