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.

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