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.

246 lines
6.6KB

  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/eval.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/parseutils.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avfilter.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. static const char *const var_names[] = {
  34. "PI",
  35. "PHI",
  36. "E",
  37. "w",
  38. "h",
  39. "a", "dar",
  40. "sar",
  41. "hsub",
  42. "vsub",
  43. NULL
  44. };
  45. enum var_name {
  46. VAR_PI,
  47. VAR_PHI,
  48. VAR_E,
  49. VAR_W,
  50. VAR_H,
  51. VAR_A, VAR_DAR,
  52. VAR_SAR,
  53. VAR_HSUB,
  54. VAR_VSUB,
  55. VARS_NB
  56. };
  57. typedef struct AspectContext {
  58. const AVClass *class;
  59. AVRational dar;
  60. AVRational sar;
  61. char *ratio_expr;
  62. } AspectContext;
  63. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  64. {
  65. AspectContext *s = link->dst->priv;
  66. frame->sample_aspect_ratio = s->sar;
  67. return ff_filter_frame(link->dst->outputs[0], frame);
  68. }
  69. #define OFFSET(x) offsetof(AspectContext, x)
  70. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  71. static int get_aspect_ratio(AVFilterLink *inlink, AVRational *aspect_ratio)
  72. {
  73. AVFilterContext *ctx = inlink->dst;
  74. AspectContext *s = inlink->dst->priv;
  75. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  76. double var_values[VARS_NB], res;
  77. int ret;
  78. var_values[VAR_PI] = M_PI;
  79. var_values[VAR_PHI] = M_PHI;
  80. var_values[VAR_E] = M_E;
  81. var_values[VAR_W] = inlink->w;
  82. var_values[VAR_H] = inlink->h;
  83. var_values[VAR_A] = (double) inlink->w / inlink->h;
  84. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  85. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  86. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  87. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  88. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  89. /* evaluate new aspect ratio*/
  90. if ((ret = av_expr_parse_and_eval(&res, s->ratio_expr,
  91. var_names, var_values,
  92. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  93. av_log(NULL, AV_LOG_ERROR,
  94. "Error when evaluating the expression '%s'\n", s->ratio_expr);
  95. return ret;
  96. }
  97. *aspect_ratio = av_d2q(res, INT_MAX);
  98. return 0;
  99. }
  100. #if CONFIG_SETDAR_FILTER
  101. /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
  102. static int setdar_config_props(AVFilterLink *inlink)
  103. {
  104. AspectContext *s = inlink->dst->priv;
  105. AVRational dar;
  106. int ret;
  107. if ((ret = get_aspect_ratio(inlink, &s->dar)))
  108. return ret;
  109. if (s->dar.num && s->dar.den) {
  110. av_reduce(&s->sar.num, &s->sar.den,
  111. s->dar.num * inlink->h,
  112. s->dar.den * inlink->w, 100);
  113. inlink->sample_aspect_ratio = s->sar;
  114. dar = s->dar;
  115. } else {
  116. inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
  117. dar = (AVRational){ inlink->w, inlink->h };
  118. }
  119. av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
  120. inlink->w, inlink->h, dar.num, dar.den,
  121. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
  122. return 0;
  123. }
  124. static const AVOption setdar_options[] = {
  125. { "dar", "display aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
  126. { NULL },
  127. };
  128. static const AVClass setdar_class = {
  129. .class_name = "setdar",
  130. .item_name = av_default_item_name,
  131. .option = setdar_options,
  132. .version = LIBAVUTIL_VERSION_INT,
  133. };
  134. static const AVFilterPad avfilter_vf_setdar_inputs[] = {
  135. {
  136. .name = "default",
  137. .type = AVMEDIA_TYPE_VIDEO,
  138. .config_props = setdar_config_props,
  139. .get_video_buffer = ff_null_get_video_buffer,
  140. .filter_frame = filter_frame,
  141. },
  142. { NULL }
  143. };
  144. static const AVFilterPad avfilter_vf_setdar_outputs[] = {
  145. {
  146. .name = "default",
  147. .type = AVMEDIA_TYPE_VIDEO,
  148. },
  149. { NULL }
  150. };
  151. AVFilter ff_vf_setdar = {
  152. .name = "setdar",
  153. .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
  154. .priv_size = sizeof(AspectContext),
  155. .priv_class = &setdar_class,
  156. .inputs = avfilter_vf_setdar_inputs,
  157. .outputs = avfilter_vf_setdar_outputs,
  158. };
  159. #endif /* CONFIG_SETDAR_FILTER */
  160. #if CONFIG_SETSAR_FILTER
  161. /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
  162. static int setsar_config_props(AVFilterLink *inlink)
  163. {
  164. AspectContext *s = inlink->dst->priv;
  165. int ret;
  166. if ((ret = get_aspect_ratio(inlink, &s->sar)))
  167. return ret;
  168. inlink->sample_aspect_ratio = s->sar;
  169. return 0;
  170. }
  171. static const AVOption setsar_options[] = {
  172. { "sar", "sample (pixel) aspect ratio", OFFSET(ratio_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
  173. { NULL },
  174. };
  175. static const AVClass setsar_class = {
  176. .class_name = "setsar",
  177. .item_name = av_default_item_name,
  178. .option = setsar_options,
  179. .version = LIBAVUTIL_VERSION_INT,
  180. };
  181. static const AVFilterPad avfilter_vf_setsar_inputs[] = {
  182. {
  183. .name = "default",
  184. .type = AVMEDIA_TYPE_VIDEO,
  185. .config_props = setsar_config_props,
  186. .get_video_buffer = ff_null_get_video_buffer,
  187. .filter_frame = filter_frame,
  188. },
  189. { NULL }
  190. };
  191. static const AVFilterPad avfilter_vf_setsar_outputs[] = {
  192. {
  193. .name = "default",
  194. .type = AVMEDIA_TYPE_VIDEO,
  195. },
  196. { NULL }
  197. };
  198. AVFilter ff_vf_setsar = {
  199. .name = "setsar",
  200. .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
  201. .priv_size = sizeof(AspectContext),
  202. .priv_class = &setsar_class,
  203. .inputs = avfilter_vf_setsar_inputs,
  204. .outputs = avfilter_vf_setsar_outputs,
  205. };
  206. #endif /* CONFIG_SETSAR_FILTER */