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.

149 lines
5.0KB

  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 int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  57. {
  58. AspectContext *aspect = link->dst->priv;
  59. picref->video->pixel_aspect = aspect->aspect;
  60. link->cur_buf = NULL;
  61. return ff_start_frame(link->dst->outputs[0], picref);
  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. AVFilter avfilter_vf_setdar = {
  78. .name = "setdar",
  79. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  80. .init = init,
  81. .priv_size = sizeof(AspectContext),
  82. .inputs = (const AVFilterPad[]) {{ .name = "default",
  83. .type = AVMEDIA_TYPE_VIDEO,
  84. .config_props = setdar_config_props,
  85. .get_video_buffer = ff_null_get_video_buffer,
  86. .start_frame = start_frame,
  87. .end_frame = ff_null_end_frame },
  88. { .name = NULL}},
  89. .outputs = (const AVFilterPad[]) {{ .name = "default",
  90. .type = AVMEDIA_TYPE_VIDEO, },
  91. { .name = NULL}},
  92. };
  93. #endif /* CONFIG_SETDAR_FILTER */
  94. #if CONFIG_SETSAR_FILTER
  95. /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
  96. static int setsar_config_props(AVFilterLink *inlink)
  97. {
  98. AspectContext *aspect = inlink->dst->priv;
  99. inlink->sample_aspect_ratio = aspect->aspect;
  100. return 0;
  101. }
  102. AVFilter avfilter_vf_setsar = {
  103. .name = "setsar",
  104. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  105. .init = init,
  106. .priv_size = sizeof(AspectContext),
  107. .inputs = (const AVFilterPad[]) {{ .name = "default",
  108. .type = AVMEDIA_TYPE_VIDEO,
  109. .config_props = setsar_config_props,
  110. .get_video_buffer = ff_null_get_video_buffer,
  111. .start_frame = start_frame,
  112. .end_frame = ff_null_end_frame },
  113. { .name = NULL}},
  114. .outputs = (const AVFilterPad[]) {{ .name = "default",
  115. .type = AVMEDIA_TYPE_VIDEO, },
  116. { .name = NULL}},
  117. };
  118. #endif /* CONFIG_SETSAR_FILTER */