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.

174 lines
4.8KB

  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 start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  58. {
  59. AspectContext *aspect = link->dst->priv;
  60. picref->video->pixel_aspect = aspect->aspect;
  61. link->cur_buf = NULL;
  62. return ff_start_frame(link->dst->outputs[0], picref);
  63. }
  64. #if CONFIG_SETDAR_FILTER
  65. /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
  66. static int setdar_config_props(AVFilterLink *inlink)
  67. {
  68. AspectContext *aspect = inlink->dst->priv;
  69. AVRational dar = aspect->aspect;
  70. av_reduce(&aspect->aspect.num, &aspect->aspect.den,
  71. aspect->aspect.num * inlink->h,
  72. aspect->aspect.den * inlink->w, 100);
  73. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
  74. inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
  75. inlink->sample_aspect_ratio = aspect->aspect;
  76. return 0;
  77. }
  78. static const AVFilterPad avfilter_vf_setdar_inputs[] = {
  79. {
  80. .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. },
  87. { NULL }
  88. };
  89. static const AVFilterPad avfilter_vf_setdar_outputs[] = {
  90. {
  91. .name = "default",
  92. .type = AVMEDIA_TYPE_VIDEO,
  93. },
  94. { NULL }
  95. };
  96. AVFilter avfilter_vf_setdar = {
  97. .name = "setdar",
  98. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  99. .init = init,
  100. .priv_size = sizeof(AspectContext),
  101. .inputs = avfilter_vf_setdar_inputs,
  102. .outputs = avfilter_vf_setdar_outputs,
  103. };
  104. #endif /* CONFIG_SETDAR_FILTER */
  105. #if CONFIG_SETSAR_FILTER
  106. /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
  107. static int setsar_config_props(AVFilterLink *inlink)
  108. {
  109. AspectContext *aspect = inlink->dst->priv;
  110. inlink->sample_aspect_ratio = aspect->aspect;
  111. return 0;
  112. }
  113. static const AVFilterPad avfilter_vf_setsar_inputs[] = {
  114. {
  115. .name = "default",
  116. .type = AVMEDIA_TYPE_VIDEO,
  117. .config_props = setsar_config_props,
  118. .get_video_buffer = ff_null_get_video_buffer,
  119. .start_frame = start_frame,
  120. .end_frame = ff_null_end_frame
  121. },
  122. { NULL }
  123. };
  124. static const AVFilterPad avfilter_vf_setsar_outputs[] = {
  125. {
  126. .name = "default",
  127. .type = AVMEDIA_TYPE_VIDEO,
  128. },
  129. { NULL }
  130. };
  131. AVFilter avfilter_vf_setsar = {
  132. .name = "setsar",
  133. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  134. .init = init,
  135. .priv_size = sizeof(AspectContext),
  136. .inputs = avfilter_vf_setsar_inputs,
  137. .outputs = avfilter_vf_setsar_outputs,
  138. };
  139. #endif /* CONFIG_SETSAR_FILTER */