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.

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