vf_aspect.c 5.8 KB

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