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
5.7KB

  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 dar;
  33. AVRational sar;
  34. #if FF_API_OLD_FILTER_OPTS
  35. float aspect_num, aspect_den;
  36. #endif
  37. } AspectContext;
  38. #if FF_API_OLD_FILTER_OPTS
  39. static av_cold int init(AVFilterContext *ctx)
  40. {
  41. AspectContext *s = ctx->priv;
  42. if (s->aspect_num > 0 && s->aspect_den > 0) {
  43. av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
  44. "dar=<number> or dar=num/den.\n");
  45. s->sar = s->dar = av_d2q(s->aspect_num / s->aspect_den, INT_MAX);
  46. }
  47. return 0;
  48. }
  49. #endif
  50. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  51. {
  52. AspectContext *s = link->dst->priv;
  53. frame->sample_aspect_ratio = s->sar;
  54. return ff_filter_frame(link->dst->outputs[0], frame);
  55. }
  56. #define OFFSET(x) offsetof(AspectContext, x)
  57. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  58. #if CONFIG_SETDAR_FILTER
  59. /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
  60. static int setdar_config_props(AVFilterLink *inlink)
  61. {
  62. AspectContext *s = inlink->dst->priv;
  63. AVRational dar;
  64. if (s->dar.num && s->dar.den) {
  65. av_reduce(&s->sar.num, &s->sar.den,
  66. s->dar.num * inlink->h,
  67. s->dar.den * inlink->w, 100);
  68. inlink->sample_aspect_ratio = s->sar;
  69. dar = s->dar;
  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", "display aspect ratio", OFFSET(dar), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
  85. { NULL },
  86. };
  87. static const AVClass setdar_class = {
  88. .class_name = "setdar",
  89. .item_name = av_default_item_name,
  90. .option = setdar_options,
  91. .version = LIBAVUTIL_VERSION_INT,
  92. };
  93. static const AVFilterPad avfilter_vf_setdar_inputs[] = {
  94. {
  95. .name = "default",
  96. .type = AVMEDIA_TYPE_VIDEO,
  97. .config_props = setdar_config_props,
  98. .get_video_buffer = ff_null_get_video_buffer,
  99. .filter_frame = filter_frame,
  100. },
  101. { NULL }
  102. };
  103. static const AVFilterPad avfilter_vf_setdar_outputs[] = {
  104. {
  105. .name = "default",
  106. .type = AVMEDIA_TYPE_VIDEO,
  107. },
  108. { NULL }
  109. };
  110. AVFilter ff_vf_setdar = {
  111. .name = "setdar",
  112. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  113. #if FF_API_OLD_FILTER_OPTS
  114. .init = init,
  115. #endif
  116. .priv_size = sizeof(AspectContext),
  117. .priv_class = &setdar_class,
  118. .inputs = avfilter_vf_setdar_inputs,
  119. .outputs = avfilter_vf_setdar_outputs,
  120. };
  121. #endif /* CONFIG_SETDAR_FILTER */
  122. #if CONFIG_SETSAR_FILTER
  123. /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
  124. static int setsar_config_props(AVFilterLink *inlink)
  125. {
  126. AspectContext *s = inlink->dst->priv;
  127. inlink->sample_aspect_ratio = s->sar;
  128. return 0;
  129. }
  130. static const AVOption setsar_options[] = {
  131. #if FF_API_OLD_FILTER_OPTS
  132. { "sar_num", NULL, OFFSET(aspect_num), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  133. { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
  134. #endif
  135. { "sar", "sample (pixel) aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, INT_MAX, FLAGS },
  136. { NULL },
  137. };
  138. static const AVClass setsar_class = {
  139. .class_name = "setsar",
  140. .item_name = av_default_item_name,
  141. .option = setsar_options,
  142. .version = LIBAVUTIL_VERSION_INT,
  143. };
  144. static const AVFilterPad avfilter_vf_setsar_inputs[] = {
  145. {
  146. .name = "default",
  147. .type = AVMEDIA_TYPE_VIDEO,
  148. .config_props = setsar_config_props,
  149. .get_video_buffer = ff_null_get_video_buffer,
  150. .filter_frame = filter_frame,
  151. },
  152. { NULL }
  153. };
  154. static const AVFilterPad avfilter_vf_setsar_outputs[] = {
  155. {
  156. .name = "default",
  157. .type = AVMEDIA_TYPE_VIDEO,
  158. },
  159. { NULL }
  160. };
  161. AVFilter ff_vf_setsar = {
  162. .name = "setsar",
  163. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  164. #if FF_API_OLD_FILTER_OPTS
  165. .init = init,
  166. #endif
  167. .priv_size = sizeof(AspectContext),
  168. .priv_class = &setsar_class,
  169. .inputs = avfilter_vf_setsar_inputs,
  170. .outputs = avfilter_vf_setsar_outputs,
  171. };
  172. #endif /* CONFIG_SETSAR_FILTER */