vf_aspect.c 4.4 KB

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