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.

347 lines
12KB

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