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.

328 lines
9.9KB

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