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.

344 lines
11KB

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