vf_aspect.c 4.5 KB

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