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