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.

450 lines
15KB

  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 *size_str;
  75. unsigned int flags; ///sws flags
  76. int hsub, vsub; ///< chroma subsampling
  77. int slice_y; ///< top of current output slice
  78. int input_is_pal; ///< set to 1 if the input format is paletted
  79. int output_is_pal; ///< set to 1 if the output format is paletted
  80. int interlaced;
  81. char *w_expr; ///< width expression string
  82. char *h_expr; ///< height expression string
  83. char *flags_str;
  84. } ScaleContext;
  85. static av_cold int init(AVFilterContext *ctx)
  86. {
  87. ScaleContext *scale = ctx->priv;
  88. int ret;
  89. if (scale->size_str && (scale->w_expr || scale->h_expr)) {
  90. av_log(ctx, AV_LOG_ERROR,
  91. "Size and width/height expressions cannot be set at the same time.\n");
  92. return AVERROR(EINVAL);
  93. }
  94. if (scale->w_expr && !scale->h_expr)
  95. FFSWAP(char *, scale->w_expr, scale->size_str);
  96. if (scale->size_str) {
  97. char buf[32];
  98. if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
  99. av_log(ctx, AV_LOG_ERROR,
  100. "Invalid size '%s'\n", scale->size_str);
  101. return ret;
  102. }
  103. snprintf(buf, sizeof(buf)-1, "%d", scale->w);
  104. av_opt_set(scale, "w", buf, 0);
  105. snprintf(buf, sizeof(buf)-1, "%d", scale->h);
  106. av_opt_set(scale, "h", buf, 0);
  107. }
  108. if (!scale->w_expr)
  109. av_opt_set(scale, "w", "iw", 0);
  110. if (!scale->h_expr)
  111. av_opt_set(scale, "h", "ih", 0);
  112. av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
  113. scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
  114. scale->flags = SWS_BILINEAR;
  115. if (scale->flags_str) {
  116. const AVClass *class = sws_get_class();
  117. const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
  118. AV_OPT_SEARCH_FAKE_OBJ);
  119. int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
  120. if (ret < 0)
  121. return ret;
  122. }
  123. return 0;
  124. }
  125. static av_cold void uninit(AVFilterContext *ctx)
  126. {
  127. ScaleContext *scale = ctx->priv;
  128. sws_freeContext(scale->sws);
  129. sws_freeContext(scale->isws[0]);
  130. sws_freeContext(scale->isws[1]);
  131. scale->sws = NULL;
  132. }
  133. static int query_formats(AVFilterContext *ctx)
  134. {
  135. AVFilterFormats *formats;
  136. enum AVPixelFormat pix_fmt;
  137. int ret;
  138. if (ctx->inputs[0]) {
  139. formats = NULL;
  140. for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
  141. if ((sws_isSupportedInput(pix_fmt) ||
  142. sws_isSupportedEndiannessConversion(pix_fmt))
  143. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  144. ff_formats_unref(&formats);
  145. return ret;
  146. }
  147. ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
  148. }
  149. if (ctx->outputs[0]) {
  150. formats = NULL;
  151. for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
  152. if ((sws_isSupportedOutput(pix_fmt) || pix_fmt == AV_PIX_FMT_PAL8 ||
  153. sws_isSupportedEndiannessConversion(pix_fmt))
  154. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  155. ff_formats_unref(&formats);
  156. return ret;
  157. }
  158. ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
  159. }
  160. return 0;
  161. }
  162. static int config_props(AVFilterLink *outlink)
  163. {
  164. AVFilterContext *ctx = outlink->src;
  165. AVFilterLink *inlink = outlink->src->inputs[0];
  166. enum AVPixelFormat outfmt = outlink->format;
  167. ScaleContext *scale = ctx->priv;
  168. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  169. int64_t w, h;
  170. double var_values[VARS_NB], res;
  171. char *expr;
  172. int ret;
  173. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  174. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  175. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  176. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  177. var_values[VAR_A] = (double) inlink->w / inlink->h;
  178. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  179. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  180. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  181. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  182. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  183. /* evaluate width and height */
  184. av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  185. var_names, var_values,
  186. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  187. scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  188. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
  189. var_names, var_values,
  190. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  191. goto fail;
  192. scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  193. /* evaluate again the width, as it may depend on the output height */
  194. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  195. var_names, var_values,
  196. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  197. goto fail;
  198. scale->w = res;
  199. w = scale->w;
  200. h = scale->h;
  201. /* sanity check params */
  202. if (w < -1 || h < -1) {
  203. av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
  204. return AVERROR(EINVAL);
  205. }
  206. if (w == -1 && h == -1)
  207. scale->w = scale->h = 0;
  208. if (!(w = scale->w))
  209. w = inlink->w;
  210. if (!(h = scale->h))
  211. h = inlink->h;
  212. if (w == -1)
  213. w = av_rescale(h, inlink->w, inlink->h);
  214. if (h == -1)
  215. h = av_rescale(w, inlink->h, inlink->w);
  216. if (w > INT_MAX || h > INT_MAX ||
  217. (h * inlink->w) > INT_MAX ||
  218. (w * inlink->h) > INT_MAX)
  219. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  220. outlink->w = w;
  221. outlink->h = h;
  222. /* TODO: make algorithm configurable */
  223. scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL ||
  224. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
  225. if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
  226. scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PAL ||
  227. av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
  228. if (scale->sws)
  229. sws_freeContext(scale->sws);
  230. if (scale->isws[0])
  231. sws_freeContext(scale->isws[0]);
  232. if (scale->isws[1])
  233. sws_freeContext(scale->isws[1]);
  234. scale->isws[0] = scale->isws[1] = scale->sws = NULL;
  235. if (inlink->w == outlink->w && inlink->h == outlink->h &&
  236. inlink->format == outlink->format)
  237. ;
  238. else {
  239. struct SwsContext **swscs[3] = {&scale->sws, &scale->isws[0], &scale->isws[1]};
  240. int i;
  241. for (i = 0; i < 3; i++) {
  242. struct SwsContext **s = swscs[i];
  243. *s = sws_alloc_context();
  244. if (!*s)
  245. return AVERROR(ENOMEM);
  246. av_opt_set_int(*s, "srcw", inlink ->w, 0);
  247. av_opt_set_int(*s, "srch", inlink ->h >> !!i, 0);
  248. av_opt_set_int(*s, "src_format", inlink->format, 0);
  249. av_opt_set_int(*s, "dstw", outlink->w, 0);
  250. av_opt_set_int(*s, "dsth", outlink->h >> !!i, 0);
  251. av_opt_set_int(*s, "dst_format", outfmt, 0);
  252. av_opt_set_int(*s, "sws_flags", scale->flags, 0);
  253. if ((ret = sws_init_context(*s, NULL, NULL)) < 0)
  254. return ret;
  255. if (!scale->interlaced)
  256. break;
  257. }
  258. }
  259. if (inlink->sample_aspect_ratio.num){
  260. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
  261. } else
  262. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  263. 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",
  264. inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
  265. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
  266. outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
  267. outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
  268. scale->flags);
  269. return 0;
  270. fail:
  271. av_log(NULL, AV_LOG_ERROR,
  272. "Error when evaluating the expression '%s'.\n"
  273. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  274. expr, scale->w_expr, scale->h_expr);
  275. return ret;
  276. }
  277. static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, struct SwsContext *sws, int y, int h, int mul, int field)
  278. {
  279. ScaleContext *scale = link->dst->priv;
  280. const uint8_t *in[4];
  281. uint8_t *out[4];
  282. int in_stride[4],out_stride[4];
  283. int i;
  284. for(i=0; i<4; i++){
  285. int vsub= ((i+1)&2) ? scale->vsub : 0;
  286. in_stride[i] = cur_pic->linesize[i] * mul;
  287. out_stride[i] = out_buf->linesize[i] * mul;
  288. in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
  289. out[i] = out_buf->data[i] + field * out_buf->linesize[i];
  290. }
  291. if(scale->input_is_pal)
  292. in[1] = cur_pic->data[1];
  293. if(scale->output_is_pal)
  294. out[1] = out_buf->data[1];
  295. return sws_scale(sws, in, in_stride, y/mul, h,
  296. out,out_stride);
  297. }
  298. static int filter_frame(AVFilterLink *link, AVFrame *in)
  299. {
  300. ScaleContext *scale = link->dst->priv;
  301. AVFilterLink *outlink = link->dst->outputs[0];
  302. AVFrame *out;
  303. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  304. char buf[32];
  305. if( in->width != link->w
  306. || in->height != link->h
  307. || in->format != link->format) {
  308. int ret;
  309. snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
  310. av_opt_set(scale, "w", buf, 0);
  311. snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
  312. av_opt_set(scale, "h", buf, 0);
  313. link->dst->inputs[0]->format = in->format;
  314. link->dst->inputs[0]->w = in->width;
  315. link->dst->inputs[0]->h = in->height;
  316. if ((ret = config_props(outlink)) < 0)
  317. return ret;
  318. }
  319. if (!scale->sws)
  320. return ff_filter_frame(outlink, in);
  321. scale->hsub = desc->log2_chroma_w;
  322. scale->vsub = desc->log2_chroma_h;
  323. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  324. if (!out) {
  325. av_frame_free(&in);
  326. return AVERROR(ENOMEM);
  327. }
  328. av_frame_copy_props(out, in);
  329. out->width = outlink->w;
  330. out->height = outlink->h;
  331. if(scale->output_is_pal)
  332. avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
  333. av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
  334. (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
  335. (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
  336. INT_MAX);
  337. if(scale->interlaced>0 || (scale->interlaced<0 && in->interlaced_frame)){
  338. scale_slice(link, out, in, scale->isws[0], 0, (link->h+1)/2, 2, 0);
  339. scale_slice(link, out, in, scale->isws[1], 0, link->h /2, 2, 1);
  340. }else{
  341. scale_slice(link, out, in, scale->sws, 0, link->h, 1, 0);
  342. }
  343. av_frame_free(&in);
  344. return ff_filter_frame(outlink, out);
  345. }
  346. #define OFFSET(x) offsetof(ScaleContext, x)
  347. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  348. static const AVOption scale_options[] = {
  349. { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  350. { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  351. { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  352. { "height","Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  353. { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
  354. { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 1, FLAGS },
  355. { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  356. { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  357. { NULL },
  358. };
  359. AVFILTER_DEFINE_CLASS(scale);
  360. static const AVFilterPad avfilter_vf_scale_inputs[] = {
  361. {
  362. .name = "default",
  363. .type = AVMEDIA_TYPE_VIDEO,
  364. .filter_frame = filter_frame,
  365. },
  366. { NULL }
  367. };
  368. static const AVFilterPad avfilter_vf_scale_outputs[] = {
  369. {
  370. .name = "default",
  371. .type = AVMEDIA_TYPE_VIDEO,
  372. .config_props = config_props,
  373. },
  374. { NULL }
  375. };
  376. AVFilter avfilter_vf_scale = {
  377. .name = "scale",
  378. .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
  379. .init = init,
  380. .uninit = uninit,
  381. .query_formats = query_formats,
  382. .priv_size = sizeof(ScaleContext),
  383. .priv_class = &scale_class,
  384. .inputs = avfilter_vf_scale_inputs,
  385. .outputs = avfilter_vf_scale_outputs,
  386. };