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.

148 lines
4.9KB

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