vf_scale.c 12 KB

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