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.

346 lines
11KB

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