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.

312 lines
10KB

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