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.

361 lines
12KB

  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. int64_t w, h;
  140. double var_values[VARS_NB], res;
  141. char *expr;
  142. int ret;
  143. var_values[VAR_PI] = M_PI;
  144. var_values[VAR_PHI] = M_PHI;
  145. var_values[VAR_E] = M_E;
  146. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  147. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  148. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  149. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  150. var_values[VAR_DAR] = var_values[VAR_A] = (double) inlink->w / inlink->h;
  151. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  152. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  153. var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  154. var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  155. /* evaluate width and height */
  156. av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  157. var_names, var_values,
  158. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  159. scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  160. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
  161. var_names, var_values,
  162. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  163. goto fail;
  164. scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  165. /* evaluate again the width, as it may depend on the output height */
  166. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  167. var_names, var_values,
  168. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  169. goto fail;
  170. scale->w = res;
  171. w = scale->w;
  172. h = scale->h;
  173. /* sanity check params */
  174. if (w < -1 || h < -1) {
  175. av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
  176. return AVERROR(EINVAL);
  177. }
  178. if (w == -1 && h == -1)
  179. scale->w = scale->h = 0;
  180. if (!(w = scale->w))
  181. w = inlink->w;
  182. if (!(h = scale->h))
  183. h = inlink->h;
  184. if (w == -1)
  185. w = av_rescale(h, inlink->w, inlink->h);
  186. if (h == -1)
  187. h = av_rescale(w, inlink->h, inlink->w);
  188. if (w > INT_MAX || h > INT_MAX ||
  189. (h * inlink->w) > INT_MAX ||
  190. (w * inlink->h) > INT_MAX)
  191. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  192. outlink->w = w;
  193. outlink->h = h;
  194. /* TODO: make algorithm configurable */
  195. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
  196. inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
  197. outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
  198. scale->flags);
  199. scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
  200. av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
  201. if (scale->sws)
  202. sws_freeContext(scale->sws);
  203. if (inlink->w == outlink->w && inlink->h == outlink->h &&
  204. inlink->format == outlink->format)
  205. scale->sws = NULL;
  206. else {
  207. scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
  208. outlink->w, outlink->h, outlink->format,
  209. scale->flags, NULL, NULL, NULL);
  210. if (!scale->sws)
  211. return AVERROR(EINVAL);
  212. }
  213. if (inlink->sample_aspect_ratio.num)
  214. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
  215. outlink->w*inlink->h},
  216. inlink->sample_aspect_ratio);
  217. else
  218. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  219. return 0;
  220. fail:
  221. av_log(NULL, AV_LOG_ERROR,
  222. "Error when evaluating the expression '%s'\n", expr);
  223. return ret;
  224. }
  225. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  226. {
  227. ScaleContext *scale = link->dst->priv;
  228. AVFilterLink *outlink = link->dst->outputs[0];
  229. AVFilterBufferRef *outpicref, *for_next_filter;
  230. int ret = 0;
  231. if (!scale->sws) {
  232. outpicref = avfilter_ref_buffer(picref, ~0);
  233. if (!outpicref)
  234. return AVERROR(ENOMEM);
  235. return ff_start_frame(outlink, outpicref);
  236. }
  237. scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
  238. scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  239. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  240. if (!outpicref)
  241. return AVERROR(ENOMEM);
  242. avfilter_copy_buffer_ref_props(outpicref, picref);
  243. outpicref->video->w = outlink->w;
  244. outpicref->video->h = outlink->h;
  245. av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
  246. (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
  247. (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
  248. INT_MAX);
  249. scale->slice_y = 0;
  250. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  251. if (for_next_filter)
  252. ret = ff_start_frame(outlink, for_next_filter);
  253. else
  254. ret = AVERROR(ENOMEM);
  255. if (ret < 0) {
  256. avfilter_unref_bufferp(&outpicref);
  257. return ret;
  258. }
  259. outlink->out_buf = outpicref;
  260. return 0;
  261. }
  262. static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  263. {
  264. ScaleContext *scale = link->dst->priv;
  265. int out_h, ret;
  266. AVFilterBufferRef *cur_pic = link->cur_buf;
  267. const uint8_t *data[4];
  268. if (!scale->sws) {
  269. return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  270. }
  271. if (scale->slice_y == 0 && slice_dir == -1)
  272. scale->slice_y = link->dst->outputs[0]->h;
  273. data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
  274. data[1] = scale->input_is_pal ?
  275. cur_pic->data[1] :
  276. cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
  277. data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
  278. data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
  279. out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
  280. link->dst->outputs[0]->out_buf->data,
  281. link->dst->outputs[0]->out_buf->linesize);
  282. if (slice_dir == -1)
  283. scale->slice_y -= out_h;
  284. ret = ff_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
  285. if (slice_dir == 1)
  286. scale->slice_y += out_h;
  287. return ret;
  288. }
  289. AVFilter avfilter_vf_scale = {
  290. .name = "scale",
  291. .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
  292. .init = init,
  293. .uninit = uninit,
  294. .query_formats = query_formats,
  295. .priv_size = sizeof(ScaleContext),
  296. .inputs = (const AVFilterPad[]) {{ .name = "default",
  297. .type = AVMEDIA_TYPE_VIDEO,
  298. .start_frame = start_frame,
  299. .draw_slice = draw_slice,
  300. .min_perms = AV_PERM_READ, },
  301. { .name = NULL}},
  302. .outputs = (const AVFilterPad[]) {{ .name = "default",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .config_props = config_props, },
  305. { .name = NULL}},
  306. };