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.

133 lines
4.5KB

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