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.

126 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 "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. if (args) {
  33. if (sscanf(args, "%d:%d", &aspect->aspect.num, &aspect->aspect.den) < 2) {
  34. if (sscanf(args, "%lf", &ratio) < 1) {
  35. av_log(ctx, AV_LOG_ERROR,
  36. "Invalid string '%s' for aspect ratio.\n", args);
  37. return AVERROR(EINVAL);
  38. }
  39. aspect->aspect = av_d2q(ratio, 100);
  40. } else {
  41. gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
  42. if (gcd) {
  43. aspect->aspect.num /= gcd;
  44. aspect->aspect.den /= gcd;
  45. }
  46. }
  47. }
  48. if (aspect->aspect.den == 0)
  49. aspect->aspect = (AVRational) {0, 1};
  50. return 0;
  51. }
  52. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  53. {
  54. AspectContext *aspect = link->dst->priv;
  55. picref->video->pixel_aspect = aspect->aspect;
  56. avfilter_start_frame(link->dst->outputs[0], picref);
  57. }
  58. #if CONFIG_ASPECT_FILTER
  59. /* for aspect filter, convert from frame aspect ratio to pixel aspect ratio */
  60. static int frameaspect_config_props(AVFilterLink *inlink)
  61. {
  62. AspectContext *aspect = inlink->dst->priv;
  63. av_reduce(&aspect->aspect.num, &aspect->aspect.den,
  64. aspect->aspect.num * inlink->h,
  65. aspect->aspect.den * inlink->w, 100);
  66. return 0;
  67. }
  68. AVFilter avfilter_vf_aspect = {
  69. .name = "aspect",
  70. .description = NULL_IF_CONFIG_SMALL("Set the frame aspect ratio."),
  71. .init = init,
  72. .priv_size = sizeof(AspectContext),
  73. .inputs = (AVFilterPad[]) {{ .name = "default",
  74. .type = AVMEDIA_TYPE_VIDEO,
  75. .config_props = frameaspect_config_props,
  76. .get_video_buffer = avfilter_null_get_video_buffer,
  77. .start_frame = start_frame,
  78. .end_frame = avfilter_null_end_frame },
  79. { .name = NULL}},
  80. .outputs = (AVFilterPad[]) {{ .name = "default",
  81. .type = AVMEDIA_TYPE_VIDEO, },
  82. { .name = NULL}},
  83. };
  84. #endif /* CONFIG_ASPECT_FILTER */
  85. #if CONFIG_PIXELASPECT_FILTER
  86. AVFilter avfilter_vf_pixelaspect = {
  87. .name = "pixelaspect",
  88. .description = NULL_IF_CONFIG_SMALL("Set the pixel aspect ratio."),
  89. .init = init,
  90. .priv_size = sizeof(AspectContext),
  91. .inputs = (AVFilterPad[]) {{ .name = "default",
  92. .type = AVMEDIA_TYPE_VIDEO,
  93. .get_video_buffer = avfilter_null_get_video_buffer,
  94. .start_frame = start_frame,
  95. .end_frame = avfilter_null_end_frame },
  96. { .name = NULL}},
  97. .outputs = (AVFilterPad[]) {{ .name = "default",
  98. .type = AVMEDIA_TYPE_VIDEO, },
  99. { .name = NULL}},
  100. };
  101. #endif /* CONFIG_PIXELASPECT_FILTER */