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.

489 lines
17KB

  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 <stdio.h>
  25. #include <string.h>
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/eval.h"
  32. #include "libavutil/internal.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/pixdesc.h"
  37. #include "libavutil/imgutils.h"
  38. #include "libavutil/avassert.h"
  39. #include "libswscale/swscale.h"
  40. static const char *const var_names[] = {
  41. "in_w", "iw",
  42. "in_h", "ih",
  43. "out_w", "ow",
  44. "out_h", "oh",
  45. "a",
  46. "sar",
  47. "dar",
  48. "hsub",
  49. "vsub",
  50. NULL
  51. };
  52. enum var_name {
  53. VAR_IN_W, VAR_IW,
  54. VAR_IN_H, VAR_IH,
  55. VAR_OUT_W, VAR_OW,
  56. VAR_OUT_H, VAR_OH,
  57. VAR_A,
  58. VAR_SAR,
  59. VAR_DAR,
  60. VAR_HSUB,
  61. VAR_VSUB,
  62. VARS_NB
  63. };
  64. typedef struct {
  65. const AVClass *class;
  66. struct SwsContext *sws; ///< software scaler context
  67. struct SwsContext *isws[2]; ///< software scaler context for interlaced material
  68. /**
  69. * New dimensions. Special values are:
  70. * 0 = original width/height
  71. * -1 = keep original aspect
  72. */
  73. int w, h;
  74. char *flags_str; ///sws flags string
  75. char *size_str;
  76. unsigned int flags; ///sws flags
  77. int hsub, vsub; ///< chroma subsampling
  78. int slice_y; ///< top of current output slice
  79. int input_is_pal; ///< set to 1 if the input format is paletted
  80. int output_is_pal; ///< set to 1 if the output format is paletted
  81. int interlaced;
  82. char *w_expr; ///< width expression string
  83. char *h_expr; ///< height expression string
  84. } ScaleContext;
  85. #define OFFSET(x) offsetof(ScaleContext, x)
  86. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  87. static const AVOption scale_options[] = {
  88. { "w", "set width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  89. { "width", "set width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  90. { "h", "set height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  91. { "height", "set height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  92. { "flags", "set libswscale flags", OFFSET(flags_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, INT_MAX, FLAGS },
  93. { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 1, FLAGS },
  94. { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  95. { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  96. { NULL },
  97. };
  98. AVFILTER_DEFINE_CLASS(scale);
  99. static av_cold int init(AVFilterContext *ctx, const char *args)
  100. {
  101. ScaleContext *scale = ctx->priv;
  102. static const char *shorthand[] = { "w", "h", NULL };
  103. int ret;
  104. const char *args0 = args;
  105. scale->class = &scale_class;
  106. av_opt_set_defaults(scale);
  107. if (args && (scale->size_str = av_get_token(&args, ":"))) {
  108. if (av_parse_video_size(&scale->w, &scale->h, scale->size_str) < 0) {
  109. av_freep(&scale->size_str);
  110. args = args0;
  111. } else if (*args)
  112. args++;
  113. }
  114. if ((ret = av_opt_set_from_string(scale, args, shorthand, "=", ":")) < 0)
  115. return ret;
  116. if (scale->size_str && (scale->w_expr || scale->h_expr)) {
  117. av_log(ctx, AV_LOG_ERROR,
  118. "Size and width/height expressions cannot be set at the same time.\n");
  119. return AVERROR(EINVAL);
  120. }
  121. if (scale->size_str) {
  122. char buf[32];
  123. if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
  124. av_log(ctx, AV_LOG_ERROR,
  125. "Invalid size '%s'\n", scale->size_str);
  126. return ret;
  127. }
  128. snprintf(buf, sizeof(buf)-1, "%d", scale->w);
  129. av_opt_set(scale, "w", buf, 0);
  130. snprintf(buf, sizeof(buf)-1, "%d", scale->h);
  131. av_opt_set(scale, "h", buf, 0);
  132. }
  133. if (!scale->w_expr)
  134. av_opt_set(scale, "w", "iw", 0);
  135. if (!scale->h_expr)
  136. av_opt_set(scale, "h", "ih", 0);
  137. av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
  138. scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
  139. scale->flags = SWS_BILINEAR;
  140. if (scale->flags_str) {
  141. const AVClass *class = sws_get_class();
  142. const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
  143. AV_OPT_SEARCH_FAKE_OBJ);
  144. int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
  145. if (ret < 0)
  146. return ret;
  147. }
  148. return 0;
  149. }
  150. static av_cold void uninit(AVFilterContext *ctx)
  151. {
  152. ScaleContext *scale = ctx->priv;
  153. sws_freeContext(scale->sws);
  154. sws_freeContext(scale->isws[0]);
  155. sws_freeContext(scale->isws[1]);
  156. scale->sws = NULL;
  157. av_opt_free(scale);
  158. }
  159. static int query_formats(AVFilterContext *ctx)
  160. {
  161. AVFilterFormats *formats;
  162. enum AVPixelFormat pix_fmt;
  163. int ret;
  164. if (ctx->inputs[0]) {
  165. formats = NULL;
  166. for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
  167. if ( sws_isSupportedInput(pix_fmt)
  168. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  169. ff_formats_unref(&formats);
  170. return ret;
  171. }
  172. ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
  173. }
  174. if (ctx->outputs[0]) {
  175. formats = NULL;
  176. for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
  177. if ( (sws_isSupportedOutput(pix_fmt) || pix_fmt == AV_PIX_FMT_PAL8)
  178. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  179. ff_formats_unref(&formats);
  180. return ret;
  181. }
  182. ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
  183. }
  184. return 0;
  185. }
  186. static int config_props(AVFilterLink *outlink)
  187. {
  188. AVFilterContext *ctx = outlink->src;
  189. AVFilterLink *inlink = outlink->src->inputs[0];
  190. enum AVPixelFormat outfmt = outlink->format;
  191. ScaleContext *scale = ctx->priv;
  192. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  193. int64_t w, h;
  194. double var_values[VARS_NB], res;
  195. char *expr;
  196. int ret;
  197. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  198. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  199. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  200. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  201. var_values[VAR_A] = (double) inlink->w / inlink->h;
  202. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  203. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  204. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  205. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  206. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  207. /* evaluate width and height */
  208. av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  209. var_names, var_values,
  210. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  211. scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  212. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
  213. var_names, var_values,
  214. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  215. goto fail;
  216. scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  217. /* evaluate again the width, as it may depend on the output height */
  218. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  219. var_names, var_values,
  220. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  221. goto fail;
  222. scale->w = res;
  223. w = scale->w;
  224. h = scale->h;
  225. /* sanity check params */
  226. if (w < -1 || h < -1) {
  227. av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
  228. return AVERROR(EINVAL);
  229. }
  230. if (w == -1 && h == -1)
  231. scale->w = scale->h = 0;
  232. if (!(w = scale->w))
  233. w = inlink->w;
  234. if (!(h = scale->h))
  235. h = inlink->h;
  236. if (w == -1)
  237. w = av_rescale(h, inlink->w, inlink->h);
  238. if (h == -1)
  239. h = av_rescale(w, inlink->h, inlink->w);
  240. if (w > INT_MAX || h > INT_MAX ||
  241. (h * inlink->w) > INT_MAX ||
  242. (w * inlink->h) > INT_MAX)
  243. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  244. outlink->w = w;
  245. outlink->h = h;
  246. /* TODO: make algorithm configurable */
  247. scale->input_is_pal = desc->flags & PIX_FMT_PAL ||
  248. desc->flags & PIX_FMT_PSEUDOPAL;
  249. if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
  250. scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & PIX_FMT_PAL ||
  251. av_pix_fmt_desc_get(outfmt)->flags & PIX_FMT_PSEUDOPAL;
  252. if (scale->sws)
  253. sws_freeContext(scale->sws);
  254. if (inlink->w == outlink->w && inlink->h == outlink->h &&
  255. inlink->format == outlink->format)
  256. scale->sws = NULL;
  257. else {
  258. scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
  259. outlink->w, outlink->h, outfmt,
  260. scale->flags, NULL, NULL, NULL);
  261. if (scale->isws[0])
  262. sws_freeContext(scale->isws[0]);
  263. scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
  264. outlink->w, outlink->h/2, outfmt,
  265. scale->flags, NULL, NULL, NULL);
  266. if (scale->isws[1])
  267. sws_freeContext(scale->isws[1]);
  268. scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
  269. outlink->w, outlink->h/2, outfmt,
  270. scale->flags, NULL, NULL, NULL);
  271. if (!scale->sws || !scale->isws[0] || !scale->isws[1])
  272. return AVERROR(EINVAL);
  273. }
  274. if (inlink->sample_aspect_ratio.num){
  275. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
  276. } else
  277. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  278. 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",
  279. inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
  280. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
  281. outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
  282. outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
  283. scale->flags);
  284. return 0;
  285. fail:
  286. av_log(NULL, AV_LOG_ERROR,
  287. "Error when evaluating the expression '%s'.\n"
  288. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  289. expr, scale->w_expr, scale->h_expr);
  290. return ret;
  291. }
  292. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  293. {
  294. ScaleContext *scale = link->dst->priv;
  295. AVFilterLink *outlink = link->dst->outputs[0];
  296. AVFilterBufferRef *outpicref, *for_next_filter;
  297. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  298. char buf[32];
  299. int ret = 0;
  300. if( picref->video->w != link->w
  301. || picref->video->h != link->h
  302. || picref->format != link->format) {
  303. int ret;
  304. snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
  305. av_opt_set(scale, "w", buf, 0);
  306. snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
  307. av_opt_set(scale, "h", buf, 0);
  308. link->dst->inputs[0]->format = picref->format;
  309. link->dst->inputs[0]->w = picref->video->w;
  310. link->dst->inputs[0]->h = picref->video->h;
  311. if ((ret = config_props(outlink)) < 0)
  312. return ret;
  313. }
  314. if (!scale->sws) {
  315. outpicref = avfilter_ref_buffer(picref, ~0);
  316. if (!outpicref)
  317. return AVERROR(ENOMEM);
  318. return ff_start_frame(outlink, outpicref);
  319. }
  320. scale->hsub = desc->log2_chroma_w;
  321. scale->vsub = desc->log2_chroma_h;
  322. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
  323. if (!outpicref)
  324. return AVERROR(ENOMEM);
  325. avfilter_copy_buffer_ref_props(outpicref, picref);
  326. outpicref->video->w = outlink->w;
  327. outpicref->video->h = outlink->h;
  328. if(scale->output_is_pal)
  329. avpriv_set_systematic_pal2((uint32_t*)outpicref->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
  330. av_reduce(&outpicref->video->sample_aspect_ratio.num, &outpicref->video->sample_aspect_ratio.den,
  331. (int64_t)picref->video->sample_aspect_ratio.num * outlink->h * link->w,
  332. (int64_t)picref->video->sample_aspect_ratio.den * outlink->w * link->h,
  333. INT_MAX);
  334. scale->slice_y = 0;
  335. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  336. if (for_next_filter)
  337. ret = ff_start_frame(outlink, for_next_filter);
  338. else
  339. ret = AVERROR(ENOMEM);
  340. if (ret < 0) {
  341. avfilter_unref_bufferp(&outpicref);
  342. return ret;
  343. }
  344. outlink->out_buf = outpicref;
  345. return 0;
  346. }
  347. static int scale_slice(AVFilterLink *link, struct SwsContext *sws, int y, int h, int mul, int field)
  348. {
  349. ScaleContext *scale = link->dst->priv;
  350. AVFilterBufferRef *cur_pic = link->cur_buf;
  351. AVFilterBufferRef *out_buf = link->dst->outputs[0]->out_buf;
  352. const uint8_t *in[4];
  353. uint8_t *out[4];
  354. int in_stride[4],out_stride[4];
  355. int i;
  356. for(i=0; i<4; i++){
  357. int vsub= ((i+1)&2) ? scale->vsub : 0;
  358. in_stride[i] = cur_pic->linesize[i] * mul;
  359. out_stride[i] = out_buf->linesize[i] * mul;
  360. in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
  361. out[i] = out_buf->data[i] + field * out_buf->linesize[i];
  362. }
  363. if(scale->input_is_pal)
  364. in[1] = cur_pic->data[1];
  365. if(scale->output_is_pal)
  366. out[1] = out_buf->data[1];
  367. return sws_scale(sws, in, in_stride, y/mul, h,
  368. out,out_stride);
  369. }
  370. static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  371. {
  372. ScaleContext *scale = link->dst->priv;
  373. int out_h, ret;
  374. if (!scale->sws) {
  375. return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  376. }
  377. if (scale->slice_y == 0 && slice_dir == -1)
  378. scale->slice_y = link->dst->outputs[0]->h;
  379. if(scale->interlaced>0 || (scale->interlaced<0 && link->cur_buf->video->interlaced)){
  380. av_assert0(y%(2<<scale->vsub) == 0);
  381. out_h = scale_slice(link, scale->isws[0], y, (h+1)/2, 2, 0);
  382. out_h+= scale_slice(link, scale->isws[1], y, h /2, 2, 1);
  383. }else{
  384. out_h = scale_slice(link, scale->sws, y, h, 1, 0);
  385. }
  386. if (slice_dir == -1)
  387. scale->slice_y -= out_h;
  388. ret = ff_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
  389. if (slice_dir == 1)
  390. scale->slice_y += out_h;
  391. return ret;
  392. }
  393. static const AVFilterPad avfilter_vf_scale_inputs[] = {
  394. {
  395. .name = "default",
  396. .type = AVMEDIA_TYPE_VIDEO,
  397. .start_frame = start_frame,
  398. .draw_slice = draw_slice,
  399. .min_perms = AV_PERM_READ,
  400. },
  401. { NULL }
  402. };
  403. static const AVFilterPad avfilter_vf_scale_outputs[] = {
  404. {
  405. .name = "default",
  406. .type = AVMEDIA_TYPE_VIDEO,
  407. .config_props = config_props,
  408. },
  409. { NULL }
  410. };
  411. AVFilter avfilter_vf_scale = {
  412. .name = "scale",
  413. .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
  414. .init = init,
  415. .uninit = uninit,
  416. .query_formats = query_formats,
  417. .priv_size = sizeof(ScaleContext),
  418. .inputs = avfilter_vf_scale_inputs,
  419. .outputs = avfilter_vf_scale_outputs,
  420. .priv_class = &scale_class,
  421. };