vf_aspect.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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->pixel_aspect = 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 par:%d/%d\n",
  69. inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
  70. return 0;
  71. }
  72. AVFilter avfilter_vf_setdar = {
  73. .name = "setdar",
  74. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  75. .init = init,
  76. .priv_size = sizeof(AspectContext),
  77. .inputs = (AVFilterPad[]) {{ .name = "default",
  78. .type = AVMEDIA_TYPE_VIDEO,
  79. .config_props = setdar_config_props,
  80. .get_video_buffer = avfilter_null_get_video_buffer,
  81. .start_frame = start_frame,
  82. .end_frame = avfilter_null_end_frame },
  83. { .name = NULL}},
  84. .outputs = (AVFilterPad[]) {{ .name = "default",
  85. .type = AVMEDIA_TYPE_VIDEO, },
  86. { .name = NULL}},
  87. };
  88. #endif /* CONFIG_SETDAR_FILTER */
  89. #if CONFIG_SETSAR_FILTER
  90. AVFilter avfilter_vf_setsar = {
  91. .name = "setsar",
  92. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  93. .init = init,
  94. .priv_size = sizeof(AspectContext),
  95. .inputs = (AVFilterPad[]) {{ .name = "default",
  96. .type = AVMEDIA_TYPE_VIDEO,
  97. .get_video_buffer = avfilter_null_get_video_buffer,
  98. .start_frame = start_frame,
  99. .end_frame = avfilter_null_end_frame },
  100. { .name = NULL}},
  101. .outputs = (AVFilterPad[]) {{ .name = "default",
  102. .type = AVMEDIA_TYPE_VIDEO, },
  103. { .name = NULL}},
  104. };
  105. #endif /* CONFIG_SETSAR_FILTER */