vf_scale.c 9.7 KB

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