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.

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