vf_scale.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 ScaleContext {
  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. double param[2]; // sws params
  76. int hsub, vsub; ///< chroma subsampling
  77. int slice_y; ///< top of current output slice
  78. int input_is_pal; ///< set to 1 if the input format is paletted
  79. char *w_expr; ///< width expression string
  80. char *h_expr; ///< height expression string
  81. char *flags_str;
  82. } ScaleContext;
  83. static av_cold int init(AVFilterContext *ctx)
  84. {
  85. ScaleContext *scale = ctx->priv;
  86. if (scale->flags_str) {
  87. const AVClass *class = sws_get_class();
  88. const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
  89. AV_OPT_SEARCH_FAKE_OBJ);
  90. int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
  91. if (ret < 0)
  92. return ret;
  93. }
  94. return 0;
  95. }
  96. static av_cold void uninit(AVFilterContext *ctx)
  97. {
  98. ScaleContext *scale = ctx->priv;
  99. sws_freeContext(scale->sws);
  100. scale->sws = NULL;
  101. }
  102. static int query_formats(AVFilterContext *ctx)
  103. {
  104. AVFilterFormats *formats;
  105. enum AVPixelFormat pix_fmt;
  106. int ret;
  107. if (ctx->inputs[0]) {
  108. const AVPixFmtDescriptor *desc = NULL;
  109. formats = NULL;
  110. while ((desc = av_pix_fmt_desc_next(desc))) {
  111. pix_fmt = av_pix_fmt_desc_get_id(desc);
  112. if ((sws_isSupportedInput(pix_fmt) ||
  113. sws_isSupportedEndiannessConversion(pix_fmt))
  114. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  115. ff_formats_unref(&formats);
  116. return ret;
  117. }
  118. }
  119. ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
  120. }
  121. if (ctx->outputs[0]) {
  122. const AVPixFmtDescriptor *desc = NULL;
  123. formats = NULL;
  124. while ((desc = av_pix_fmt_desc_next(desc))) {
  125. pix_fmt = av_pix_fmt_desc_get_id(desc);
  126. if ((sws_isSupportedOutput(pix_fmt) ||
  127. sws_isSupportedEndiannessConversion(pix_fmt))
  128. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  129. ff_formats_unref(&formats);
  130. return ret;
  131. }
  132. }
  133. ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
  134. }
  135. return 0;
  136. }
  137. static int config_props(AVFilterLink *outlink)
  138. {
  139. AVFilterContext *ctx = outlink->src;
  140. AVFilterLink *inlink = outlink->src->inputs[0];
  141. ScaleContext *scale = ctx->priv;
  142. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  143. int64_t w, h;
  144. double var_values[VARS_NB], res;
  145. char *expr;
  146. int ret;
  147. var_values[VAR_PI] = M_PI;
  148. var_values[VAR_PHI] = M_PHI;
  149. var_values[VAR_E] = M_E;
  150. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  151. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  152. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  153. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  154. var_values[VAR_A] = (double) inlink->w / inlink->h;
  155. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  156. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  157. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  158. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  159. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  160. /* evaluate width and height */
  161. av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  162. var_names, var_values,
  163. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  164. scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  165. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
  166. var_names, var_values,
  167. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  168. goto fail;
  169. scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  170. /* evaluate again the width, as it may depend on the output height */
  171. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  172. var_names, var_values,
  173. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  174. goto fail;
  175. scale->w = res;
  176. w = scale->w;
  177. h = scale->h;
  178. /* sanity check params */
  179. if (w < -1 || h < -1) {
  180. av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
  181. return AVERROR(EINVAL);
  182. }
  183. if (w == -1 && h == -1)
  184. scale->w = scale->h = 0;
  185. if (!(w = scale->w))
  186. w = inlink->w;
  187. if (!(h = scale->h))
  188. h = inlink->h;
  189. if (w == -1)
  190. w = av_rescale(h, inlink->w, inlink->h);
  191. if (h == -1)
  192. h = av_rescale(w, inlink->h, inlink->w);
  193. if (w > INT_MAX || h > INT_MAX ||
  194. (h * inlink->w) > INT_MAX ||
  195. (w * inlink->h) > INT_MAX)
  196. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  197. outlink->w = w;
  198. outlink->h = h;
  199. /* TODO: make algorithm configurable */
  200. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
  201. inlink ->w, inlink ->h, av_get_pix_fmt_name(inlink->format),
  202. outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
  203. scale->flags);
  204. scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL ||
  205. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
  206. if (scale->sws)
  207. sws_freeContext(scale->sws);
  208. if (inlink->w == outlink->w && inlink->h == outlink->h &&
  209. inlink->format == outlink->format)
  210. scale->sws = NULL;
  211. else {
  212. scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
  213. outlink->w, outlink->h, outlink->format,
  214. scale->flags, NULL, NULL, scale->param);
  215. if (!scale->sws)
  216. return AVERROR(EINVAL);
  217. }
  218. if (inlink->sample_aspect_ratio.num)
  219. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
  220. outlink->w*inlink->h},
  221. inlink->sample_aspect_ratio);
  222. else
  223. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  224. return 0;
  225. fail:
  226. av_log(NULL, AV_LOG_ERROR,
  227. "Error when evaluating the expression '%s'\n", expr);
  228. return ret;
  229. }
  230. static int filter_frame(AVFilterLink *link, AVFrame *in)
  231. {
  232. ScaleContext *scale = link->dst->priv;
  233. AVFilterLink *outlink = link->dst->outputs[0];
  234. AVFrame *out;
  235. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  236. if (!scale->sws)
  237. return ff_filter_frame(outlink, in);
  238. scale->hsub = desc->log2_chroma_w;
  239. scale->vsub = desc->log2_chroma_h;
  240. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  241. if (!out) {
  242. av_frame_free(&in);
  243. return AVERROR(ENOMEM);
  244. }
  245. av_frame_copy_props(out, in);
  246. out->width = outlink->w;
  247. out->height = outlink->h;
  248. av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
  249. (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
  250. (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
  251. INT_MAX);
  252. sws_scale(scale->sws, in->data, in->linesize, 0, in->height,
  253. out->data, out->linesize);
  254. av_frame_free(&in);
  255. return ff_filter_frame(outlink, out);
  256. }
  257. #define OFFSET(x) offsetof(ScaleContext, x)
  258. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  259. static const AVOption options[] = {
  260. { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
  261. { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
  262. { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
  263. { "param0", "Scaler param 0", OFFSET(param[0]), AV_OPT_TYPE_DOUBLE, { .dbl = SWS_PARAM_DEFAULT }, INT_MIN, INT_MAX, FLAGS },
  264. { "param1", "Scaler param 1", OFFSET(param[1]), AV_OPT_TYPE_DOUBLE, { .dbl = SWS_PARAM_DEFAULT }, INT_MIN, INT_MAX, FLAGS },
  265. { NULL },
  266. };
  267. static const AVClass scale_class = {
  268. .class_name = "scale",
  269. .item_name = av_default_item_name,
  270. .option = options,
  271. .version = LIBAVUTIL_VERSION_INT,
  272. };
  273. static const AVFilterPad avfilter_vf_scale_inputs[] = {
  274. {
  275. .name = "default",
  276. .type = AVMEDIA_TYPE_VIDEO,
  277. .filter_frame = filter_frame,
  278. },
  279. { NULL }
  280. };
  281. static const AVFilterPad avfilter_vf_scale_outputs[] = {
  282. {
  283. .name = "default",
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. .config_props = config_props,
  286. },
  287. { NULL }
  288. };
  289. AVFilter ff_vf_scale = {
  290. .name = "scale",
  291. .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
  292. .init = init,
  293. .uninit = uninit,
  294. .query_formats = query_formats,
  295. .priv_size = sizeof(ScaleContext),
  296. .priv_class = &scale_class,
  297. .inputs = avfilter_vf_scale_inputs,
  298. .outputs = avfilter_vf_scale_outputs,
  299. };