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.

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