vf_aspect.c 4.9 KB

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