vf_aspect.c 4.9 KB

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