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.

371 lines
13KB

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