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.

123 lines
4.0KB

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