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.

337 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 "libavutil/avstring.h"
  26. #include "libavutil/eval.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libswscale/swscale.h"
  31. static const char *const var_names[] = {
  32. "PI",
  33. "PHI",
  34. "E",
  35. "in_w", "iw",
  36. "in_h", "ih",
  37. "out_w", "ow",
  38. "out_h", "oh",
  39. "a", "dar",
  40. "sar",
  41. "hsub",
  42. "vsub",
  43. NULL
  44. };
  45. enum var_name {
  46. VAR_PI,
  47. VAR_PHI,
  48. VAR_E,
  49. VAR_IN_W, VAR_IW,
  50. VAR_IN_H, VAR_IH,
  51. VAR_OUT_W, VAR_OW,
  52. VAR_OUT_H, VAR_OH,
  53. VAR_A, VAR_DAR,
  54. VAR_SAR,
  55. VAR_HSUB,
  56. VAR_VSUB,
  57. VARS_NB
  58. };
  59. typedef struct {
  60. struct SwsContext *sws; ///< software scaler context
  61. /**
  62. * New dimensions. Special values are:
  63. * 0 = original width/height
  64. * -1 = keep original aspect
  65. */
  66. int w, h;
  67. unsigned int flags; ///sws flags
  68. int hsub, vsub; ///< chroma subsampling
  69. int slice_y; ///< top of current output slice
  70. int input_is_pal; ///< set to 1 if the input format is paletted
  71. char w_expr[256]; ///< width expression string
  72. char h_expr[256]; ///< height expression string
  73. } ScaleContext;
  74. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  75. {
  76. ScaleContext *scale = ctx->priv;
  77. const char *p;
  78. av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
  79. av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
  80. scale->flags = SWS_BILINEAR;
  81. if (args) {
  82. sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
  83. p = strstr(args,"flags=");
  84. if (p) {
  85. const AVClass *class = sws_get_class();
  86. const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
  87. AV_OPT_SEARCH_FAKE_OBJ);
  88. int ret = av_opt_eval_flags(&class, o, p + 6, &scale->flags);
  89. if (ret < 0)
  90. return ret;
  91. }
  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 PixelFormat pix_fmt;
  105. int ret;
  106. if (ctx->inputs[0]) {
  107. formats = NULL;
  108. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
  109. if ( sws_isSupportedInput(pix_fmt)
  110. && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
  111. avfilter_formats_unref(&formats);
  112. return ret;
  113. }
  114. avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
  115. }
  116. if (ctx->outputs[0]) {
  117. formats = NULL;
  118. for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
  119. if ( sws_isSupportedOutput(pix_fmt)
  120. && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
  121. avfilter_formats_unref(&formats);
  122. return ret;
  123. }
  124. avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
  125. }
  126. return 0;
  127. }
  128. static int config_props(AVFilterLink *outlink)
  129. {
  130. AVFilterContext *ctx = outlink->src;
  131. AVFilterLink *inlink = outlink->src->inputs[0];
  132. ScaleContext *scale = ctx->priv;
  133. int64_t w, h;
  134. double var_values[VARS_NB], res;
  135. char *expr;
  136. int ret;
  137. var_values[VAR_PI] = M_PI;
  138. var_values[VAR_PHI] = M_PHI;
  139. var_values[VAR_E] = M_E;
  140. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  141. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  142. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  143. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  144. var_values[VAR_DAR] = var_values[VAR_A] = (float) inlink->w / inlink->h;
  145. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  146. (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  147. var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  148. var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  149. /* evaluate width and height */
  150. av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  151. var_names, var_values,
  152. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  153. scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  154. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
  155. var_names, var_values,
  156. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  157. goto fail;
  158. scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  159. /* evaluate again the width, as it may depend on the output height */
  160. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  161. var_names, var_values,
  162. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  163. goto fail;
  164. scale->w = res;
  165. w = scale->w;
  166. h = scale->h;
  167. /* sanity check params */
  168. if (w < -1 || h < -1) {
  169. av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
  170. return AVERROR(EINVAL);
  171. }
  172. if (w == -1 && h == -1)
  173. scale->w = scale->h = 0;
  174. if (!(w = scale->w))
  175. w = inlink->w;
  176. if (!(h = scale->h))
  177. h = inlink->h;
  178. if (w == -1)
  179. w = av_rescale(h, inlink->w, inlink->h);
  180. if (h == -1)
  181. h = av_rescale(w, inlink->h, inlink->w);
  182. if (w > INT_MAX || h > INT_MAX ||
  183. (h * inlink->w) > INT_MAX ||
  184. (w * inlink->h) > INT_MAX)
  185. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  186. outlink->w = w;
  187. outlink->h = h;
  188. /* TODO: make algorithm configurable */
  189. av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
  190. inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
  191. outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
  192. scale->flags);
  193. scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
  194. av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
  195. if (scale->sws)
  196. sws_freeContext(scale->sws);
  197. if (inlink->w == outlink->w && inlink->h == outlink->h &&
  198. inlink->format == outlink->format)
  199. scale->sws = NULL;
  200. else {
  201. scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
  202. outlink->w, outlink->h, outlink->format,
  203. scale->flags, NULL, NULL, NULL);
  204. if (!scale->sws)
  205. return AVERROR(EINVAL);
  206. }
  207. if (inlink->sample_aspect_ratio.num)
  208. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
  209. outlink->w*inlink->h},
  210. inlink->sample_aspect_ratio);
  211. else
  212. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  213. return 0;
  214. fail:
  215. av_log(NULL, AV_LOG_ERROR,
  216. "Error when evaluating the expression '%s'\n", expr);
  217. return ret;
  218. }
  219. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  220. {
  221. ScaleContext *scale = link->dst->priv;
  222. AVFilterLink *outlink = link->dst->outputs[0];
  223. AVFilterBufferRef *outpicref;
  224. if (!scale->sws) {
  225. avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  226. return;
  227. }
  228. scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
  229. scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  230. outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  231. avfilter_copy_buffer_ref_props(outpicref, picref);
  232. outpicref->video->w = outlink->w;
  233. outpicref->video->h = outlink->h;
  234. outlink->out_buf = outpicref;
  235. av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
  236. (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
  237. (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
  238. INT_MAX);
  239. scale->slice_y = 0;
  240. avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
  241. }
  242. static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  243. {
  244. ScaleContext *scale = link->dst->priv;
  245. int out_h;
  246. AVFilterBufferRef *cur_pic = link->cur_buf;
  247. const uint8_t *data[4];
  248. if (!scale->sws) {
  249. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  250. return;
  251. }
  252. if (scale->slice_y == 0 && slice_dir == -1)
  253. scale->slice_y = link->dst->outputs[0]->h;
  254. data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
  255. data[1] = scale->input_is_pal ?
  256. cur_pic->data[1] :
  257. cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
  258. data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
  259. data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
  260. out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
  261. link->dst->outputs[0]->out_buf->data,
  262. link->dst->outputs[0]->out_buf->linesize);
  263. if (slice_dir == -1)
  264. scale->slice_y -= out_h;
  265. avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
  266. if (slice_dir == 1)
  267. scale->slice_y += out_h;
  268. }
  269. AVFilter avfilter_vf_scale = {
  270. .name = "scale",
  271. .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
  272. .init = init,
  273. .uninit = uninit,
  274. .query_formats = query_formats,
  275. .priv_size = sizeof(ScaleContext),
  276. .inputs = (AVFilterPad[]) {{ .name = "default",
  277. .type = AVMEDIA_TYPE_VIDEO,
  278. .start_frame = start_frame,
  279. .draw_slice = draw_slice,
  280. .min_perms = AV_PERM_READ, },
  281. { .name = NULL}},
  282. .outputs = (AVFilterPad[]) {{ .name = "default",
  283. .type = AVMEDIA_TYPE_VIDEO,
  284. .config_props = config_props, },
  285. { .name = NULL}},
  286. };