vf_scale.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 PixelFormat pix_fmt;
  118. int ret;
  119. if (ctx->inputs[0]) {
  120. formats = NULL;
  121. for (pix_fmt = 0; pix_fmt < 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 < PIX_FMT_NB; pix_fmt++)
  132. if ( (sws_isSupportedOutput(pix_fmt) || pix_fmt == 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 PixelFormat outfmt = outlink->format;
  146. ScaleContext *scale = ctx->priv;
  147. int64_t w, h;
  148. double var_values[VARS_NB], res;
  149. char *expr;
  150. int ret;
  151. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  152. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  153. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  154. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  155. var_values[VAR_A] = (float) inlink->w / inlink->h;
  156. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  157. (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  158. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  159. var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  160. var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  161. /* evaluate width and height */
  162. av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  163. var_names, var_values,
  164. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  165. scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  166. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
  167. var_names, var_values,
  168. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  169. goto fail;
  170. scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  171. /* evaluate again the width, as it may depend on the output height */
  172. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  173. var_names, var_values,
  174. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  175. goto fail;
  176. scale->w = res;
  177. w = scale->w;
  178. h = scale->h;
  179. /* sanity check params */
  180. if (w < -1 || h < -1) {
  181. av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
  182. return AVERROR(EINVAL);
  183. }
  184. if (w == -1 && h == -1)
  185. scale->w = scale->h = 0;
  186. if (!(w = scale->w))
  187. w = inlink->w;
  188. if (!(h = scale->h))
  189. h = inlink->h;
  190. if (w == -1)
  191. w = av_rescale(h, inlink->w, inlink->h);
  192. if (h == -1)
  193. h = av_rescale(w, inlink->h, inlink->w);
  194. if (w > INT_MAX || h > INT_MAX ||
  195. (h * inlink->w) > INT_MAX ||
  196. (w * inlink->h) > INT_MAX)
  197. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  198. outlink->w = w;
  199. outlink->h = h;
  200. /* TODO: make algorithm configurable */
  201. scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
  202. av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
  203. if (outfmt == PIX_FMT_PAL8) outfmt = PIX_FMT_BGR8;
  204. scale->output_is_pal = av_pix_fmt_descriptors[outfmt].flags & PIX_FMT_PAL ||
  205. av_pix_fmt_descriptors[outfmt].flags & PIX_FMT_PSEUDOPAL;
  206. if (scale->sws)
  207. sws_freeContext(scale->sws);
  208. if (inlink->w == outlink->w && inlink->h == outlink->h &&
  209. inlink->format == outlink->format)
  210. scale->sws = NULL;
  211. else {
  212. scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
  213. outlink->w, outlink->h, outfmt,
  214. scale->flags, NULL, NULL, NULL);
  215. if (scale->isws[0])
  216. sws_freeContext(scale->isws[0]);
  217. scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
  218. outlink->w, outlink->h/2, outfmt,
  219. scale->flags, NULL, NULL, NULL);
  220. if (scale->isws[1])
  221. sws_freeContext(scale->isws[1]);
  222. scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
  223. outlink->w, outlink->h/2, outfmt,
  224. scale->flags, NULL, NULL, NULL);
  225. if (!scale->sws || !scale->isws[0] || !scale->isws[1])
  226. return AVERROR(EINVAL);
  227. }
  228. if (inlink->sample_aspect_ratio.num){
  229. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
  230. } else
  231. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  232. 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",
  233. inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
  234. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
  235. outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
  236. outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
  237. scale->flags);
  238. return 0;
  239. fail:
  240. av_log(NULL, AV_LOG_ERROR,
  241. "Error when evaluating the expression '%s'.\n"
  242. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  243. expr, scale->w_expr, scale->h_expr);
  244. return ret;
  245. }
  246. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  247. {
  248. ScaleContext *scale = link->dst->priv;
  249. AVFilterLink *outlink = link->dst->outputs[0];
  250. AVFilterBufferRef *outpicref, *for_next_filter;
  251. int ret = 0;
  252. if( picref->video->w != link->w
  253. || picref->video->h != link->h
  254. || picref->format != link->format) {
  255. int ret;
  256. snprintf(scale->w_expr, sizeof(scale->w_expr)-1, "%d", outlink->w);
  257. snprintf(scale->h_expr, sizeof(scale->h_expr)-1, "%d", outlink->h);
  258. link->dst->inputs[0]->format = picref->format;
  259. link->dst->inputs[0]->w = picref->video->w;
  260. link->dst->inputs[0]->h = picref->video->h;
  261. if ((ret = config_props(outlink)) < 0)
  262. av_assert0(0); //what to do here ?
  263. }
  264. if (!scale->sws) {
  265. outpicref = avfilter_ref_buffer(picref, ~0);
  266. if (!outpicref)
  267. return AVERROR(ENOMEM);
  268. return ff_start_frame(outlink, outpicref);
  269. }
  270. scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
  271. scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  272. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
  273. if (!outpicref)
  274. return AVERROR(ENOMEM);
  275. avfilter_copy_buffer_ref_props(outpicref, picref);
  276. outpicref->video->w = outlink->w;
  277. outpicref->video->h = outlink->h;
  278. if(scale->output_is_pal)
  279. ff_set_systematic_pal2((uint32_t*)outpicref->data[1], outlink->format == PIX_FMT_PAL8 ? PIX_FMT_BGR8 : outlink->format);
  280. av_reduce(&outpicref->video->sample_aspect_ratio.num, &outpicref->video->sample_aspect_ratio.den,
  281. (int64_t)picref->video->sample_aspect_ratio.num * outlink->h * link->w,
  282. (int64_t)picref->video->sample_aspect_ratio.den * outlink->w * link->h,
  283. INT_MAX);
  284. scale->slice_y = 0;
  285. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  286. if (for_next_filter)
  287. ret = ff_start_frame(outlink, for_next_filter);
  288. else
  289. ret = AVERROR(ENOMEM);
  290. if (ret < 0) {
  291. avfilter_unref_bufferp(&outpicref);
  292. return ret;
  293. }
  294. outlink->out_buf = outpicref;
  295. return 0;
  296. }
  297. static int scale_slice(AVFilterLink *link, struct SwsContext *sws, int y, int h, int mul, int field)
  298. {
  299. ScaleContext *scale = link->dst->priv;
  300. AVFilterBufferRef *cur_pic = link->cur_buf;
  301. AVFilterBufferRef *out_buf = link->dst->outputs[0]->out_buf;
  302. const uint8_t *in[4];
  303. uint8_t *out[4];
  304. int in_stride[4],out_stride[4];
  305. int i;
  306. for(i=0; i<4; i++){
  307. int vsub= ((i+1)&2) ? scale->vsub : 0;
  308. in_stride[i] = cur_pic->linesize[i] * mul;
  309. out_stride[i] = out_buf->linesize[i] * mul;
  310. in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
  311. out[i] = out_buf->data[i] + field * out_buf->linesize[i];
  312. }
  313. if(scale->input_is_pal)
  314. in[1] = cur_pic->data[1];
  315. if(scale->output_is_pal)
  316. out[1] = out_buf->data[1];
  317. return sws_scale(sws, in, in_stride, y/mul, h,
  318. out,out_stride);
  319. }
  320. static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  321. {
  322. ScaleContext *scale = link->dst->priv;
  323. int out_h, ret;
  324. if (!scale->sws) {
  325. return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  326. }
  327. if (scale->slice_y == 0 && slice_dir == -1)
  328. scale->slice_y = link->dst->outputs[0]->h;
  329. if(scale->interlaced>0 || (scale->interlaced<0 && link->cur_buf->video->interlaced)){
  330. av_assert0(y%(2<<scale->vsub) == 0);
  331. out_h = scale_slice(link, scale->isws[0], y, (h+1)/2, 2, 0);
  332. out_h+= scale_slice(link, scale->isws[1], y, h /2, 2, 1);
  333. }else{
  334. out_h = scale_slice(link, scale->sws, y, h, 1, 0);
  335. }
  336. if (slice_dir == -1)
  337. scale->slice_y -= out_h;
  338. ret = ff_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
  339. if (slice_dir == 1)
  340. scale->slice_y += out_h;
  341. return ret;
  342. }
  343. AVFilter avfilter_vf_scale = {
  344. .name = "scale",
  345. .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
  346. .init = init,
  347. .uninit = uninit,
  348. .query_formats = query_formats,
  349. .priv_size = sizeof(ScaleContext),
  350. .inputs = (const AVFilterPad[]) {{ .name = "default",
  351. .type = AVMEDIA_TYPE_VIDEO,
  352. .start_frame = start_frame,
  353. .draw_slice = draw_slice,
  354. .min_perms = AV_PERM_READ, },
  355. { .name = NULL}},
  356. .outputs = (const AVFilterPad[]) {{ .name = "default",
  357. .type = AVMEDIA_TYPE_VIDEO,
  358. .config_props = config_props, },
  359. { .name = NULL}},
  360. };