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.

171 lines
4.7KB

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