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.

136 lines
4.3KB

  1. /*
  2. * Aspect ratio modification video filter
  3. * Copyright (c) 2010 Bobby Bingham
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file libavfilter/vf_aspect.c
  22. * aspect ratio modification video filter
  23. */
  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. if(args) {
  34. if(sscanf(args, "%d:%d", &aspect->aspect.num, &aspect->aspect.den) < 2) {
  35. if(sscanf(args, "%lf", &ratio) < 1)
  36. return -1;
  37. aspect->aspect = av_d2q(ratio, 100);
  38. } else {
  39. gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
  40. if(gcd) {
  41. aspect->aspect.num /= gcd;
  42. aspect->aspect.den /= gcd;
  43. }
  44. }
  45. }
  46. if(aspect->aspect.den == 0)
  47. aspect->aspect = (AVRational) {0, 1};
  48. return 0;
  49. }
  50. static AVFilterPicRef *get_video_buffer(AVFilterLink *link, int perms,
  51. int w, int h)
  52. {
  53. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  54. }
  55. static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  56. {
  57. AspectContext *aspect = link->dst->priv;
  58. picref->pixel_aspect = aspect->aspect;
  59. avfilter_start_frame(link->dst->outputs[0], picref);
  60. }
  61. static void end_frame(AVFilterLink *link)
  62. {
  63. avfilter_end_frame(link->dst->outputs[0]);
  64. }
  65. #if CONFIG_ASPECT_FILTER
  66. /* for aspect filter, convert from frame aspect ratio to pixel aspect ratio */
  67. static int frameaspect_config_props(AVFilterLink *inlink)
  68. {
  69. AspectContext *aspect = inlink->dst->priv;
  70. av_reduce(&aspect->aspect.num, &aspect->aspect.den,
  71. aspect->aspect.num * inlink->h,
  72. aspect->aspect.den * inlink->w, 100);
  73. return 0;
  74. }
  75. AVFilter avfilter_vf_aspect = {
  76. .name = "aspect",
  77. .description = NULL_IF_CONFIG_SMALL("Set the frame aspect ratio."),
  78. .init = init,
  79. .priv_size = sizeof(AspectContext),
  80. .inputs = (AVFilterPad[]) {{ .name = "default",
  81. .type = CODEC_TYPE_VIDEO,
  82. .config_props = frameaspect_config_props,
  83. .get_video_buffer = get_video_buffer,
  84. .start_frame = start_frame,
  85. .end_frame = end_frame },
  86. { .name = NULL}},
  87. .outputs = (AVFilterPad[]) {{ .name = "default",
  88. .type = CODEC_TYPE_VIDEO, },
  89. { .name = NULL}},
  90. };
  91. #endif /* CONFIG_ASPECT_FILTER */
  92. #if CONFIG_PIXELASPECT_FILTER
  93. AVFilter avfilter_vf_pixelaspect = {
  94. .name = "pixelaspect",
  95. .description = NULL_IF_CONFIG_SMALL("Set the pixel aspect ratio."),
  96. .init = init,
  97. .priv_size = sizeof(AspectContext),
  98. .inputs = (AVFilterPad[]) {{ .name = "default",
  99. .type = CODEC_TYPE_VIDEO,
  100. .get_video_buffer = get_video_buffer,
  101. .start_frame = start_frame,
  102. .end_frame = end_frame },
  103. { .name = NULL}},
  104. .outputs = (AVFilterPad[]) {{ .name = "default",
  105. .type = CODEC_TYPE_VIDEO, },
  106. { .name = NULL}},
  107. };
  108. #endif /* CONFIG_PIXELASPECT_FILTER */