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.

208 lines
5.8KB

  1. /*
  2. * Copyright (c) 2010 Bobby Bingham
  3. * This file is part of Libav.
  4. *
  5. * Libav 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. * Libav 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 Libav; 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/mathematics.h"
  26. #include "libavutil/opt.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. typedef struct {
  31. const AVClass *class;
  32. AVRational aspect;
  33. #if FF_API_OLD_FILTER_OPTS
  34. float aspect_num, aspect_den;
  35. #endif
  36. } AspectContext;
  37. #if FF_API_OLD_FILTER_OPTS
  38. static av_cold int init(AVFilterContext *ctx)
  39. {
  40. AspectContext *s = ctx->priv;
  41. if (s->aspect_num > 0 && s->aspect_den > 0) {
  42. av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
  43. "dar=<number> or dar=num/den.\n");
  44. s->aspect = av_d2q(s->aspect_num / s->aspect_den, INT_MAX);
  45. }
  46. return 0;
  47. }
  48. #endif
  49. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  50. {
  51. AspectContext *aspect = link->dst->priv;
  52. frame->sample_aspect_ratio = aspect->aspect;
  53. return ff_filter_frame(link->dst->outputs[0], frame);
  54. }
  55. #define OFFSET(x) offsetof(AspectContext, x)
  56. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  57. #if CONFIG_SETDAR_FILTER
  58. /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
  59. static int setdar_config_props(AVFilterLink *inlink)
  60. {
  61. AspectContext *aspect = inlink->dst->priv;
  62. AVRational dar = aspect->aspect;
  63. if (aspect->aspect.num && aspect->aspect.den) {
  64. av_reduce(&aspect->aspect.num, &aspect->aspect.den,
  65. aspect->aspect.num * inlink->h,
  66. aspect->aspect.den * inlink->w, 100);
  67. inlink->sample_aspect_ratio = aspect->aspect;
  68. } else {
  69. inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
  70. dar = (AVRational){ inlink->w, inlink->h };
  71. }
  72. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
  73. inlink->w, inlink->h, dar.num, dar.den,
  74. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
  75. return 0;
  76. }
  77. static const AVOption setdar_options[] = {
  78. #if FF_API_OLD_FILTER_OPTS
  79. { "dar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  80. { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  81. #endif
  82. { "dar", "display aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  83. { NULL },
  84. };
  85. static const AVClass setdar_class = {
  86. .class_name = "setdar",
  87. .item_name = av_default_item_name,
  88. .option = setdar_options,
  89. .version = LIBAVUTIL_VERSION_INT,
  90. };
  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. /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
  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", "sample (pixel) aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, INT_MAX, FLAGS },
  134. { NULL },
  135. };
  136. static const AVClass setsar_class = {
  137. .class_name = "setsar",
  138. .item_name = av_default_item_name,
  139. .option = setsar_options,
  140. .version = LIBAVUTIL_VERSION_INT,
  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. #if FF_API_OLD_FILTER_OPTS
  163. .init = init,
  164. #endif
  165. .priv_size = sizeof(AspectContext),
  166. .priv_class = &setsar_class,
  167. .inputs = avfilter_vf_setsar_inputs,
  168. .outputs = avfilter_vf_setsar_outputs,
  169. };
  170. #endif /* CONFIG_SETSAR_FILTER */