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.

210 lines
6.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 <float.h>
  24. #include "libavutil/common.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/parseutils.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. typedef struct {
  33. const AVClass *class;
  34. AVRational aspect;
  35. int max;
  36. #if FF_API_OLD_FILTER_OPTS
  37. float aspect_num, aspect_den;
  38. #endif
  39. } AspectContext;
  40. #if FF_API_OLD_FILTER_OPTS
  41. static av_cold int init(AVFilterContext *ctx)
  42. {
  43. AspectContext *s = ctx->priv;
  44. if (s->aspect_num > 0 && s->aspect_den > 0) {
  45. av_log(ctx, AV_LOG_WARNING,
  46. "num:den syntax is deprecated, please use num/den or named options instead\n");
  47. s->aspect = av_d2q(s->aspect_num / s->aspect_den, s->max);
  48. }
  49. return 0;
  50. }
  51. #endif
  52. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  53. {
  54. AspectContext *aspect = link->dst->priv;
  55. frame->sample_aspect_ratio = aspect->aspect;
  56. return ff_filter_frame(link->dst->outputs[0], frame);
  57. }
  58. #define OFFSET(x) offsetof(AspectContext, x)
  59. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  60. #if CONFIG_SETDAR_FILTER
  61. static int setdar_config_props(AVFilterLink *inlink)
  62. {
  63. AspectContext *aspect = inlink->dst->priv;
  64. AVRational dar = aspect->aspect;
  65. if (aspect->aspect.num && aspect->aspect.den) {
  66. av_reduce(&aspect->aspect.num, &aspect->aspect.den,
  67. aspect->aspect.num * inlink->h,
  68. aspect->aspect.den * inlink->w, 100);
  69. inlink->sample_aspect_ratio = aspect->aspect;
  70. } else {
  71. inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
  72. dar = (AVRational){ inlink->w, inlink->h };
  73. }
  74. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
  75. inlink->w, inlink->h, dar.num, dar.den,
  76. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
  77. return 0;
  78. }
  79. static const AVOption setdar_options[] = {
  80. #if FF_API_OLD_FILTER_OPTS
  81. { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  82. { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  83. #endif
  84. { "dar", "set display aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
  85. { "ratio", "set display aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
  86. { "r", "set display aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
  87. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  88. { NULL }
  89. };
  90. AVFILTER_DEFINE_CLASS(setdar);
  91. static const AVFilterPad avfilter_vf_setdar_inputs[] = {
  92. {
  93. .name = "default",
  94. .type = AVMEDIA_TYPE_VIDEO,
  95. .config_props = setdar_config_props,
  96. .get_video_buffer = ff_null_get_video_buffer,
  97. .filter_frame = filter_frame,
  98. },
  99. { NULL }
  100. };
  101. static const AVFilterPad avfilter_vf_setdar_outputs[] = {
  102. {
  103. .name = "default",
  104. .type = AVMEDIA_TYPE_VIDEO,
  105. },
  106. { NULL }
  107. };
  108. AVFilter avfilter_vf_setdar = {
  109. .name = "setdar",
  110. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  111. #if FF_API_OLD_FILTER_OPTS
  112. .init = init,
  113. #endif
  114. .priv_size = sizeof(AspectContext),
  115. .priv_class = &setdar_class,
  116. .inputs = avfilter_vf_setdar_inputs,
  117. .outputs = avfilter_vf_setdar_outputs,
  118. };
  119. #endif /* CONFIG_SETDAR_FILTER */
  120. #if CONFIG_SETSAR_FILTER
  121. static int setsar_config_props(AVFilterLink *inlink)
  122. {
  123. AspectContext *aspect = inlink->dst->priv;
  124. inlink->sample_aspect_ratio = aspect->aspect;
  125. return 0;
  126. }
  127. static const AVOption setsar_options[] = {
  128. #if FF_API_OLD_FILTER_OPTS
  129. { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  130. { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  131. #endif
  132. { "sar", "set sample (pixel) aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
  133. { "ratio", "set sample (pixel) aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
  134. { "r", "set sample (pixel) aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS },
  135. { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
  136. { NULL }
  137. };
  138. AVFILTER_DEFINE_CLASS(setsar);
  139. static const AVFilterPad avfilter_vf_setsar_inputs[] = {
  140. {
  141. .name = "default",
  142. .type = AVMEDIA_TYPE_VIDEO,
  143. .config_props = setsar_config_props,
  144. .get_video_buffer = ff_null_get_video_buffer,
  145. .filter_frame = filter_frame,
  146. },
  147. { NULL }
  148. };
  149. static const AVFilterPad avfilter_vf_setsar_outputs[] = {
  150. {
  151. .name = "default",
  152. .type = AVMEDIA_TYPE_VIDEO,
  153. },
  154. { NULL }
  155. };
  156. AVFilter avfilter_vf_setsar = {
  157. .name = "setsar",
  158. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  159. #if FF_API_OLD_FILTER_OPTS
  160. .init = init,
  161. #endif
  162. .priv_size = sizeof(AspectContext),
  163. .priv_class = &setsar_class,
  164. .inputs = avfilter_vf_setsar_inputs,
  165. .outputs = avfilter_vf_setsar_outputs,
  166. };
  167. #endif /* CONFIG_SETSAR_FILTER */