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.

297 lines
9.6KB

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