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.

339 lines
10KB

  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. * scale video filter
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/eval.h"
  32. #include "libavutil/internal.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libswscale/swscale.h"
  37. static const char *const var_names[] = {
  38. "PI",
  39. "PHI",
  40. "E",
  41. "in_w", "iw",
  42. "in_h", "ih",
  43. "out_w", "ow",
  44. "out_h", "oh",
  45. "a", "dar",
  46. "sar",
  47. "hsub",
  48. "vsub",
  49. NULL
  50. };
  51. enum var_name {
  52. VAR_PI,
  53. VAR_PHI,
  54. VAR_E,
  55. VAR_IN_W, VAR_IW,
  56. VAR_IN_H, VAR_IH,
  57. VAR_OUT_W, VAR_OW,
  58. VAR_OUT_H, VAR_OH,
  59. VAR_A, VAR_DAR,
  60. VAR_SAR,
  61. VAR_HSUB,
  62. VAR_VSUB,
  63. VARS_NB
  64. };
  65. typedef struct {
  66. const AVClass *class;
  67. struct SwsContext *sws; ///< software scaler context
  68. /**
  69. * New dimensions. Special values are:
  70. * 0 = original width/height
  71. * -1 = keep original aspect
  72. */
  73. int w, h;
  74. unsigned int flags; ///sws flags
  75. int hsub, vsub; ///< chroma subsampling
  76. int slice_y; ///< top of current output slice
  77. int input_is_pal; ///< set to 1 if the input format is paletted
  78. char *w_expr; ///< width expression string
  79. char *h_expr; ///< height expression string
  80. char *flags_str;
  81. } ScaleContext;
  82. static av_cold int init(AVFilterContext *ctx)
  83. {
  84. ScaleContext *scale = ctx->priv;
  85. if (scale->flags_str) {
  86. const AVClass *class = sws_get_class();
  87. const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
  88. AV_OPT_SEARCH_FAKE_OBJ);
  89. int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
  90. if (ret < 0)
  91. return ret;
  92. }
  93. return 0;
  94. }
  95. static av_cold void uninit(AVFilterContext *ctx)
  96. {
  97. ScaleContext *scale = ctx->priv;
  98. sws_freeContext(scale->sws);
  99. scale->sws = NULL;
  100. }
  101. static int query_formats(AVFilterContext *ctx)
  102. {
  103. AVFilterFormats *formats;
  104. enum AVPixelFormat pix_fmt;
  105. int ret;
  106. if (ctx->inputs[0]) {
  107. formats = NULL;
  108. for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
  109. if ((sws_isSupportedInput(pix_fmt) ||
  110. sws_isSupportedEndiannessConversion(pix_fmt))
  111. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  112. ff_formats_unref(&formats);
  113. return ret;
  114. }
  115. ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
  116. }
  117. if (ctx->outputs[0]) {
  118. formats = NULL;
  119. for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
  120. if ((sws_isSupportedOutput(pix_fmt) ||
  121. sws_isSupportedEndiannessConversion(pix_fmt))
  122. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  123. ff_formats_unref(&formats);
  124. return ret;
  125. }
  126. ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
  127. }
  128. return 0;
  129. }
  130. static int config_props(AVFilterLink *outlink)
  131. {
  132. AVFilterContext *ctx = outlink->src;
  133. AVFilterLink *inlink = outlink->src->inputs[0];
  134. ScaleContext *scale = ctx->priv;
  135. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  136. int64_t w, h;
  137. double var_values[VARS_NB], res;
  138. char *expr;
  139. int ret;
  140. var_values[VAR_PI] = M_PI;
  141. var_values[VAR_PHI] = M_PHI;
  142. var_values[VAR_E] = M_E;
  143. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  144. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  145. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  146. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  147. var_values[VAR_DAR] = var_values[VAR_A] = (double) inlink->w / inlink->h;
  148. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  149. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  150. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  151. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  152. /* evaluate width and height */
  153. av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  154. var_names, var_values,
  155. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  156. scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  157. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
  158. var_names, var_values,
  159. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  160. goto fail;
  161. scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  162. /* evaluate again the width, as it may depend on the output height */
  163. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  164. var_names, var_values,
  165. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  166. goto fail;
  167. scale->w = res;
  168. w = scale->w;
  169. h = scale->h;
  170. /* sanity check params */
  171. if (w < -1 || h < -1) {
  172. av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
  173. return AVERROR(EINVAL);
  174. }
  175. if (w == -1 && h == -1)
  176. scale->w = scale->h = 0;
  177. if (!(w = scale->w))
  178. w = inlink->w;
  179. if (!(h = scale->h))
  180. h = inlink->h;
  181. if (w == -1)
  182. w = av_rescale(h, inlink->w, inlink->h);
  183. if (h == -1)
  184. h = av_rescale(w, inlink->h, inlink->w);
  185. if (w > INT_MAX || h > INT_MAX ||
  186. (h * inlink->w) > INT_MAX ||
  187. (w * inlink->h) > INT_MAX)
  188. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  189. outlink->w = w;
  190. outlink->h = h;
  191. /* TODO: make algorithm configurable */
  192. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
  193. inlink ->w, inlink ->h, av_get_pix_fmt_name(inlink->format),
  194. outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
  195. scale->flags);
  196. scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL ||
  197. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
  198. if (scale->sws)
  199. sws_freeContext(scale->sws);
  200. if (inlink->w == outlink->w && inlink->h == outlink->h &&
  201. inlink->format == outlink->format)
  202. scale->sws = NULL;
  203. else {
  204. scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
  205. outlink->w, outlink->h, outlink->format,
  206. scale->flags, NULL, NULL, NULL);
  207. if (!scale->sws)
  208. return AVERROR(EINVAL);
  209. }
  210. if (inlink->sample_aspect_ratio.num)
  211. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
  212. outlink->w*inlink->h},
  213. inlink->sample_aspect_ratio);
  214. else
  215. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  216. return 0;
  217. fail:
  218. av_log(NULL, AV_LOG_ERROR,
  219. "Error when evaluating the expression '%s'\n", expr);
  220. return ret;
  221. }
  222. static int filter_frame(AVFilterLink *link, AVFrame *in)
  223. {
  224. ScaleContext *scale = link->dst->priv;
  225. AVFilterLink *outlink = link->dst->outputs[0];
  226. AVFrame *out;
  227. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  228. if (!scale->sws)
  229. return ff_filter_frame(outlink, in);
  230. scale->hsub = desc->log2_chroma_w;
  231. scale->vsub = desc->log2_chroma_h;
  232. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  233. if (!out) {
  234. av_frame_free(&in);
  235. return AVERROR(ENOMEM);
  236. }
  237. av_frame_copy_props(out, in);
  238. out->width = outlink->w;
  239. out->height = outlink->h;
  240. av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
  241. (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
  242. (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
  243. INT_MAX);
  244. sws_scale(scale->sws, in->data, in->linesize, 0, in->height,
  245. out->data, out->linesize);
  246. av_frame_free(&in);
  247. return ff_filter_frame(outlink, out);
  248. }
  249. #define OFFSET(x) offsetof(ScaleContext, x)
  250. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  251. static const AVOption options[] = {
  252. { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
  253. { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
  254. { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
  255. { NULL },
  256. };
  257. static const AVClass scale_class = {
  258. .class_name = "scale",
  259. .item_name = av_default_item_name,
  260. .option = options,
  261. .version = LIBAVUTIL_VERSION_INT,
  262. };
  263. static const AVFilterPad avfilter_vf_scale_inputs[] = {
  264. {
  265. .name = "default",
  266. .type = AVMEDIA_TYPE_VIDEO,
  267. .filter_frame = filter_frame,
  268. },
  269. { NULL }
  270. };
  271. static const AVFilterPad avfilter_vf_scale_outputs[] = {
  272. {
  273. .name = "default",
  274. .type = AVMEDIA_TYPE_VIDEO,
  275. .config_props = config_props,
  276. },
  277. { NULL }
  278. };
  279. AVFilter ff_vf_scale = {
  280. .name = "scale",
  281. .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
  282. .init = init,
  283. .uninit = uninit,
  284. .query_formats = query_formats,
  285. .priv_size = sizeof(ScaleContext),
  286. .priv_class = &scale_class,
  287. .inputs = avfilter_vf_scale_inputs,
  288. .outputs = avfilter_vf_scale_outputs,
  289. };