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.

375 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 {
  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 start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  227. {
  228. ScaleContext *scale = link->dst->priv;
  229. AVFilterLink *outlink = link->dst->outputs[0];
  230. AVFilterBufferRef *outpicref, *for_next_filter;
  231. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  232. int ret = 0;
  233. if (!scale->sws) {
  234. outpicref = avfilter_ref_buffer(picref, ~0);
  235. if (!outpicref)
  236. return AVERROR(ENOMEM);
  237. return ff_start_frame(outlink, outpicref);
  238. }
  239. scale->hsub = desc->log2_chroma_w;
  240. scale->vsub = desc->log2_chroma_h;
  241. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  242. if (!outpicref)
  243. return AVERROR(ENOMEM);
  244. avfilter_copy_buffer_ref_props(outpicref, picref);
  245. outpicref->video->w = outlink->w;
  246. outpicref->video->h = outlink->h;
  247. av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
  248. (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
  249. (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
  250. INT_MAX);
  251. scale->slice_y = 0;
  252. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  253. if (for_next_filter)
  254. ret = ff_start_frame(outlink, for_next_filter);
  255. else
  256. ret = AVERROR(ENOMEM);
  257. if (ret < 0) {
  258. avfilter_unref_bufferp(&outpicref);
  259. return ret;
  260. }
  261. outlink->out_buf = outpicref;
  262. return 0;
  263. }
  264. static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  265. {
  266. ScaleContext *scale = link->dst->priv;
  267. int out_h, ret;
  268. AVFilterBufferRef *cur_pic = link->cur_buf;
  269. const uint8_t *data[4];
  270. if (!scale->sws) {
  271. return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  272. }
  273. if (scale->slice_y == 0 && slice_dir == -1)
  274. scale->slice_y = link->dst->outputs[0]->h;
  275. data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
  276. data[1] = scale->input_is_pal ?
  277. cur_pic->data[1] :
  278. cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
  279. data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
  280. data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
  281. out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
  282. link->dst->outputs[0]->out_buf->data,
  283. link->dst->outputs[0]->out_buf->linesize);
  284. if (slice_dir == -1)
  285. scale->slice_y -= out_h;
  286. ret = ff_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
  287. if (slice_dir == 1)
  288. scale->slice_y += out_h;
  289. return ret;
  290. }
  291. static const AVFilterPad avfilter_vf_scale_inputs[] = {
  292. {
  293. .name = "default",
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .start_frame = start_frame,
  296. .draw_slice = draw_slice,
  297. .min_perms = AV_PERM_READ,
  298. },
  299. { NULL }
  300. };
  301. static const AVFilterPad avfilter_vf_scale_outputs[] = {
  302. {
  303. .name = "default",
  304. .type = AVMEDIA_TYPE_VIDEO,
  305. .config_props = config_props,
  306. },
  307. { NULL }
  308. };
  309. AVFilter avfilter_vf_scale = {
  310. .name = "scale",
  311. .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
  312. .init = init,
  313. .uninit = uninit,
  314. .query_formats = query_formats,
  315. .priv_size = sizeof(ScaleContext),
  316. .inputs = avfilter_vf_scale_inputs,
  317. .outputs = avfilter_vf_scale_outputs,
  318. };