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.

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