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.

340 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 "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/eval.h"
  30. #include "libavutil/mathematics.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libswscale/swscale.h"
  34. static const char *const var_names[] = {
  35. "PI",
  36. "PHI",
  37. "E",
  38. "in_w", "iw",
  39. "in_h", "ih",
  40. "out_w", "ow",
  41. "out_h", "oh",
  42. "a", "dar",
  43. "sar",
  44. "hsub",
  45. "vsub",
  46. NULL
  47. };
  48. enum var_name {
  49. VAR_PI,
  50. VAR_PHI,
  51. VAR_E,
  52. VAR_IN_W, VAR_IW,
  53. VAR_IN_H, VAR_IH,
  54. VAR_OUT_W, VAR_OW,
  55. VAR_OUT_H, VAR_OH,
  56. VAR_A, VAR_DAR,
  57. VAR_SAR,
  58. VAR_HSUB,
  59. VAR_VSUB,
  60. VARS_NB
  61. };
  62. typedef struct {
  63. struct SwsContext *sws; ///< software scaler context
  64. /**
  65. * New dimensions. Special values are:
  66. * 0 = original width/height
  67. * -1 = keep original aspect
  68. */
  69. int w, h;
  70. unsigned int flags; ///sws flags
  71. int hsub, vsub; ///< chroma subsampling
  72. int slice_y; ///< top of current output slice
  73. int input_is_pal; ///< set to 1 if the input format is paletted
  74. char w_expr[256]; ///< width expression string
  75. char h_expr[256]; ///< height expression string
  76. } ScaleContext;
  77. static av_cold int init(AVFilterContext *ctx, const char *args)
  78. {
  79. ScaleContext *scale = ctx->priv;
  80. const char *p;
  81. av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
  82. av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
  83. scale->flags = SWS_BILINEAR;
  84. if (args) {
  85. sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
  86. p = strstr(args,"flags=");
  87. if (p) {
  88. const AVClass *class = sws_get_class();
  89. const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
  90. AV_OPT_SEARCH_FAKE_OBJ);
  91. int ret = av_opt_eval_flags(&class, o, p + 6, &scale->flags);
  92. if (ret < 0)
  93. return ret;
  94. }
  95. }
  96. return 0;
  97. }
  98. static av_cold void uninit(AVFilterContext *ctx)
  99. {
  100. ScaleContext *scale = ctx->priv;
  101. sws_freeContext(scale->sws);
  102. scale->sws = NULL;
  103. }
  104. static int query_formats(AVFilterContext *ctx)
  105. {
  106. AVFilterFormats *formats;
  107. enum PixelFormat pix_fmt;
  108. int ret;
  109. if (ctx->inputs[0]) {
  110. formats = NULL;
  111. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
  112. if ( sws_isSupportedInput(pix_fmt)
  113. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  114. ff_formats_unref(&formats);
  115. return ret;
  116. }
  117. ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
  118. }
  119. if (ctx->outputs[0]) {
  120. formats = NULL;
  121. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
  122. if ( sws_isSupportedOutput(pix_fmt)
  123. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  124. ff_formats_unref(&formats);
  125. return ret;
  126. }
  127. ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
  128. }
  129. return 0;
  130. }
  131. static int config_props(AVFilterLink *outlink)
  132. {
  133. AVFilterContext *ctx = outlink->src;
  134. AVFilterLink *inlink = outlink->src->inputs[0];
  135. ScaleContext *scale = ctx->priv;
  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] = (float) inlink->w / inlink->h;
  148. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  149. (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  150. var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  151. var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].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_pix_fmt_descriptors[ inlink->format].name,
  194. outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
  195. scale->flags);
  196. scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
  197. av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_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 void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  223. {
  224. ScaleContext *scale = link->dst->priv;
  225. AVFilterLink *outlink = link->dst->outputs[0];
  226. AVFilterBufferRef *outpicref;
  227. if (!scale->sws) {
  228. ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  229. return;
  230. }
  231. scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
  232. scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  233. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  234. avfilter_copy_buffer_ref_props(outpicref, picref);
  235. outpicref->video->w = outlink->w;
  236. outpicref->video->h = outlink->h;
  237. outlink->out_buf = outpicref;
  238. av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
  239. (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
  240. (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
  241. INT_MAX);
  242. scale->slice_y = 0;
  243. ff_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
  244. }
  245. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  246. {
  247. ScaleContext *scale = link->dst->priv;
  248. int out_h;
  249. AVFilterBufferRef *cur_pic = link->cur_buf;
  250. const uint8_t *data[4];
  251. if (!scale->sws) {
  252. ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  253. return;
  254. }
  255. if (scale->slice_y == 0 && slice_dir == -1)
  256. scale->slice_y = link->dst->outputs[0]->h;
  257. data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
  258. data[1] = scale->input_is_pal ?
  259. cur_pic->data[1] :
  260. cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
  261. data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
  262. data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
  263. out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
  264. link->dst->outputs[0]->out_buf->data,
  265. link->dst->outputs[0]->out_buf->linesize);
  266. if (slice_dir == -1)
  267. scale->slice_y -= out_h;
  268. ff_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
  269. if (slice_dir == 1)
  270. scale->slice_y += out_h;
  271. }
  272. AVFilter avfilter_vf_scale = {
  273. .name = "scale",
  274. .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
  275. .init = init,
  276. .uninit = uninit,
  277. .query_formats = query_formats,
  278. .priv_size = sizeof(ScaleContext),
  279. .inputs = (AVFilterPad[]) {{ .name = "default",
  280. .type = AVMEDIA_TYPE_VIDEO,
  281. .start_frame = start_frame,
  282. .draw_slice = draw_slice,
  283. .min_perms = AV_PERM_READ, },
  284. { .name = NULL}},
  285. .outputs = (AVFilterPad[]) {{ .name = "default",
  286. .type = AVMEDIA_TYPE_VIDEO,
  287. .config_props = config_props, },
  288. { .name = NULL}},
  289. };