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.

159 lines
4.2KB

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