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.

416 lines
14KB

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