vf_scale.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. "ohsub",
  51. "ovsub",
  52. NULL
  53. };
  54. enum var_name {
  55. VAR_IN_W, VAR_IW,
  56. VAR_IN_H, VAR_IH,
  57. VAR_OUT_W, VAR_OW,
  58. VAR_OUT_H, VAR_OH,
  59. VAR_A,
  60. VAR_SAR,
  61. VAR_DAR,
  62. VAR_HSUB,
  63. VAR_VSUB,
  64. VAR_OHSUB,
  65. VAR_OVSUB,
  66. VARS_NB
  67. };
  68. enum EvalMode {
  69. EVAL_MODE_INIT,
  70. EVAL_MODE_FRAME,
  71. EVAL_MODE_NB
  72. };
  73. typedef struct ScaleContext {
  74. const AVClass *class;
  75. struct SwsContext *sws; ///< software scaler context
  76. struct SwsContext *isws[2]; ///< software scaler context for interlaced material
  77. AVDictionary *opts;
  78. /**
  79. * New dimensions. Special values are:
  80. * 0 = original width/height
  81. * -1 = keep original aspect
  82. * -N = try to keep aspect but make sure it is divisible by N
  83. */
  84. int w, h;
  85. char *size_str;
  86. unsigned int flags; ///sws flags
  87. double param[2]; // sws params
  88. int hsub, vsub; ///< chroma subsampling
  89. int slice_y; ///< top of current output slice
  90. int input_is_pal; ///< set to 1 if the input format is paletted
  91. int output_is_pal; ///< set to 1 if the output format is paletted
  92. int interlaced;
  93. char *w_expr; ///< width expression string
  94. char *h_expr; ///< height expression string
  95. char *flags_str;
  96. char *in_color_matrix;
  97. char *out_color_matrix;
  98. int in_range;
  99. int out_range;
  100. int out_h_chr_pos;
  101. int out_v_chr_pos;
  102. int in_h_chr_pos;
  103. int in_v_chr_pos;
  104. int force_original_aspect_ratio;
  105. int nb_slices;
  106. int eval_mode; ///< expression evaluation mode
  107. } ScaleContext;
  108. AVFilter ff_vf_scale2ref;
  109. static av_cold int init_dict(AVFilterContext *ctx, AVDictionary **opts)
  110. {
  111. ScaleContext *scale = ctx->priv;
  112. int ret;
  113. if (scale->size_str && (scale->w_expr || scale->h_expr)) {
  114. av_log(ctx, AV_LOG_ERROR,
  115. "Size and width/height expressions cannot be set at the same time.\n");
  116. return AVERROR(EINVAL);
  117. }
  118. if (scale->w_expr && !scale->h_expr)
  119. FFSWAP(char *, scale->w_expr, scale->size_str);
  120. if (scale->size_str) {
  121. char buf[32];
  122. if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
  123. av_log(ctx, AV_LOG_ERROR,
  124. "Invalid size '%s'\n", scale->size_str);
  125. return ret;
  126. }
  127. snprintf(buf, sizeof(buf)-1, "%d", scale->w);
  128. av_opt_set(scale, "w", buf, 0);
  129. snprintf(buf, sizeof(buf)-1, "%d", scale->h);
  130. av_opt_set(scale, "h", buf, 0);
  131. }
  132. if (!scale->w_expr)
  133. av_opt_set(scale, "w", "iw", 0);
  134. if (!scale->h_expr)
  135. av_opt_set(scale, "h", "ih", 0);
  136. av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
  137. scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
  138. scale->flags = 0;
  139. if (scale->flags_str) {
  140. const AVClass *class = sws_get_class();
  141. const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
  142. AV_OPT_SEARCH_FAKE_OBJ);
  143. int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
  144. if (ret < 0)
  145. return ret;
  146. }
  147. scale->opts = *opts;
  148. *opts = NULL;
  149. return 0;
  150. }
  151. static av_cold void uninit(AVFilterContext *ctx)
  152. {
  153. ScaleContext *scale = ctx->priv;
  154. sws_freeContext(scale->sws);
  155. sws_freeContext(scale->isws[0]);
  156. sws_freeContext(scale->isws[1]);
  157. scale->sws = NULL;
  158. av_dict_free(&scale->opts);
  159. }
  160. static int query_formats(AVFilterContext *ctx)
  161. {
  162. AVFilterFormats *formats;
  163. enum AVPixelFormat pix_fmt;
  164. int ret;
  165. if (ctx->inputs[0]) {
  166. const AVPixFmtDescriptor *desc = NULL;
  167. formats = NULL;
  168. while ((desc = av_pix_fmt_desc_next(desc))) {
  169. pix_fmt = av_pix_fmt_desc_get_id(desc);
  170. if ((sws_isSupportedInput(pix_fmt) ||
  171. sws_isSupportedEndiannessConversion(pix_fmt))
  172. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  173. return ret;
  174. }
  175. }
  176. if ((ret = ff_formats_ref(formats, &ctx->inputs[0]->out_formats)) < 0)
  177. return ret;
  178. }
  179. if (ctx->outputs[0]) {
  180. const AVPixFmtDescriptor *desc = NULL;
  181. formats = NULL;
  182. while ((desc = av_pix_fmt_desc_next(desc))) {
  183. pix_fmt = av_pix_fmt_desc_get_id(desc);
  184. if ((sws_isSupportedOutput(pix_fmt) || pix_fmt == AV_PIX_FMT_PAL8 ||
  185. sws_isSupportedEndiannessConversion(pix_fmt))
  186. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  187. return ret;
  188. }
  189. }
  190. if ((ret = ff_formats_ref(formats, &ctx->outputs[0]->in_formats)) < 0)
  191. return ret;
  192. }
  193. return 0;
  194. }
  195. static const int *parse_yuv_type(const char *s, enum AVColorSpace colorspace)
  196. {
  197. if (!s)
  198. s = "bt601";
  199. if (s && strstr(s, "bt709")) {
  200. colorspace = AVCOL_SPC_BT709;
  201. } else if (s && strstr(s, "fcc")) {
  202. colorspace = AVCOL_SPC_FCC;
  203. } else if (s && strstr(s, "smpte240m")) {
  204. colorspace = AVCOL_SPC_SMPTE240M;
  205. } else if (s && (strstr(s, "bt601") || strstr(s, "bt470") || strstr(s, "smpte170m"))) {
  206. colorspace = AVCOL_SPC_BT470BG;
  207. } else if (s && strstr(s, "bt2020")) {
  208. colorspace = AVCOL_SPC_BT2020_NCL;
  209. }
  210. if (colorspace < 1 || colorspace > 10 || colorspace == 8) {
  211. colorspace = AVCOL_SPC_BT470BG;
  212. }
  213. return sws_getCoefficients(colorspace);
  214. }
  215. static int config_props(AVFilterLink *outlink)
  216. {
  217. AVFilterContext *ctx = outlink->src;
  218. AVFilterLink *inlink0 = outlink->src->inputs[0];
  219. AVFilterLink *inlink = ctx->filter == &ff_vf_scale2ref ?
  220. outlink->src->inputs[1] :
  221. outlink->src->inputs[0];
  222. enum AVPixelFormat outfmt = outlink->format;
  223. ScaleContext *scale = ctx->priv;
  224. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  225. const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
  226. int64_t w, h;
  227. double var_values[VARS_NB], res;
  228. char *expr;
  229. int ret;
  230. int factor_w, factor_h;
  231. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  232. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  233. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  234. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  235. var_values[VAR_A] = (double) inlink->w / inlink->h;
  236. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  237. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  238. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  239. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  240. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  241. var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
  242. var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
  243. /* evaluate width and height */
  244. av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  245. var_names, var_values,
  246. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  247. scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  248. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
  249. var_names, var_values,
  250. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  251. goto fail;
  252. scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  253. /* evaluate again the width, as it may depend on the output height */
  254. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  255. var_names, var_values,
  256. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  257. goto fail;
  258. scale->w = res;
  259. w = scale->w;
  260. h = scale->h;
  261. /* Check if it is requested that the result has to be divisible by a some
  262. * factor (w or h = -n with n being the factor). */
  263. factor_w = 1;
  264. factor_h = 1;
  265. if (w < -1) {
  266. factor_w = -w;
  267. }
  268. if (h < -1) {
  269. factor_h = -h;
  270. }
  271. if (w < 0 && h < 0)
  272. scale->w = scale->h = 0;
  273. if (!(w = scale->w))
  274. w = inlink->w;
  275. if (!(h = scale->h))
  276. h = inlink->h;
  277. /* Make sure that the result is divisible by the factor we determined
  278. * earlier. If no factor was set, it is nothing will happen as the default
  279. * factor is 1 */
  280. if (w < 0)
  281. w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
  282. if (h < 0)
  283. h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
  284. /* Note that force_original_aspect_ratio may overwrite the previous set
  285. * dimensions so that it is not divisible by the set factors anymore. */
  286. if (scale->force_original_aspect_ratio) {
  287. int tmp_w = av_rescale(h, inlink->w, inlink->h);
  288. int tmp_h = av_rescale(w, inlink->h, inlink->w);
  289. if (scale->force_original_aspect_ratio == 1) {
  290. w = FFMIN(tmp_w, w);
  291. h = FFMIN(tmp_h, h);
  292. } else {
  293. w = FFMAX(tmp_w, w);
  294. h = FFMAX(tmp_h, h);
  295. }
  296. }
  297. if (w > INT_MAX || h > INT_MAX ||
  298. (h * inlink->w) > INT_MAX ||
  299. (w * inlink->h) > INT_MAX)
  300. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  301. outlink->w = w;
  302. outlink->h = h;
  303. /* TODO: make algorithm configurable */
  304. scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL ||
  305. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
  306. if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
  307. scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PAL ||
  308. av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
  309. if (scale->sws)
  310. sws_freeContext(scale->sws);
  311. if (scale->isws[0])
  312. sws_freeContext(scale->isws[0]);
  313. if (scale->isws[1])
  314. sws_freeContext(scale->isws[1]);
  315. scale->isws[0] = scale->isws[1] = scale->sws = NULL;
  316. if (inlink0->w == outlink->w &&
  317. inlink0->h == outlink->h &&
  318. !scale->out_color_matrix &&
  319. scale->in_range == scale->out_range &&
  320. inlink0->format == outlink->format)
  321. ;
  322. else {
  323. struct SwsContext **swscs[3] = {&scale->sws, &scale->isws[0], &scale->isws[1]};
  324. int i;
  325. for (i = 0; i < 3; i++) {
  326. struct SwsContext **s = swscs[i];
  327. *s = sws_alloc_context();
  328. if (!*s)
  329. return AVERROR(ENOMEM);
  330. av_opt_set_int(*s, "srcw", inlink0 ->w, 0);
  331. av_opt_set_int(*s, "srch", inlink0 ->h >> !!i, 0);
  332. av_opt_set_int(*s, "src_format", inlink0->format, 0);
  333. av_opt_set_int(*s, "dstw", outlink->w, 0);
  334. av_opt_set_int(*s, "dsth", outlink->h >> !!i, 0);
  335. av_opt_set_int(*s, "dst_format", outfmt, 0);
  336. av_opt_set_int(*s, "sws_flags", scale->flags, 0);
  337. av_opt_set_int(*s, "param0", scale->param[0], 0);
  338. av_opt_set_int(*s, "param1", scale->param[1], 0);
  339. if (scale->in_range != AVCOL_RANGE_UNSPECIFIED)
  340. av_opt_set_int(*s, "src_range",
  341. scale->in_range == AVCOL_RANGE_JPEG, 0);
  342. if (scale->out_range != AVCOL_RANGE_UNSPECIFIED)
  343. av_opt_set_int(*s, "dst_range",
  344. scale->out_range == AVCOL_RANGE_JPEG, 0);
  345. if (scale->opts) {
  346. AVDictionaryEntry *e = NULL;
  347. while ((e = av_dict_get(scale->opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
  348. if ((ret = av_opt_set(*s, e->key, e->value, 0)) < 0)
  349. return ret;
  350. }
  351. }
  352. /* Override YUV420P default settings to have the correct (MPEG-2) chroma positions
  353. * MPEG-2 chroma positions are used by convention
  354. * XXX: support other 4:2:0 pixel formats */
  355. if (inlink0->format == AV_PIX_FMT_YUV420P && scale->in_v_chr_pos == -513) {
  356. scale->in_v_chr_pos = (i == 0) ? 128 : (i == 1) ? 64 : 192;
  357. }
  358. if (outlink->format == AV_PIX_FMT_YUV420P && scale->out_v_chr_pos == -513) {
  359. scale->out_v_chr_pos = (i == 0) ? 128 : (i == 1) ? 64 : 192;
  360. }
  361. av_opt_set_int(*s, "src_h_chr_pos", scale->in_h_chr_pos, 0);
  362. av_opt_set_int(*s, "src_v_chr_pos", scale->in_v_chr_pos, 0);
  363. av_opt_set_int(*s, "dst_h_chr_pos", scale->out_h_chr_pos, 0);
  364. av_opt_set_int(*s, "dst_v_chr_pos", scale->out_v_chr_pos, 0);
  365. if ((ret = sws_init_context(*s, NULL, NULL)) < 0)
  366. return ret;
  367. if (!scale->interlaced)
  368. break;
  369. }
  370. }
  371. if (inlink->sample_aspect_ratio.num){
  372. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
  373. } else
  374. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  375. 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",
  376. inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
  377. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
  378. outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
  379. outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
  380. scale->flags);
  381. return 0;
  382. fail:
  383. av_log(NULL, AV_LOG_ERROR,
  384. "Error when evaluating the expression '%s'.\n"
  385. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  386. expr, scale->w_expr, scale->h_expr);
  387. return ret;
  388. }
  389. static int config_props_ref(AVFilterLink *outlink)
  390. {
  391. AVFilterLink *inlink = outlink->src->inputs[1];
  392. outlink->w = inlink->w;
  393. outlink->h = inlink->h;
  394. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  395. outlink->time_base = inlink->time_base;
  396. return 0;
  397. }
  398. static int request_frame(AVFilterLink *outlink)
  399. {
  400. return ff_request_frame(outlink->src->inputs[0]);
  401. }
  402. static int request_frame_ref(AVFilterLink *outlink)
  403. {
  404. return ff_request_frame(outlink->src->inputs[1]);
  405. }
  406. static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, struct SwsContext *sws, int y, int h, int mul, int field)
  407. {
  408. ScaleContext *scale = link->dst->priv;
  409. const uint8_t *in[4];
  410. uint8_t *out[4];
  411. int in_stride[4],out_stride[4];
  412. int i;
  413. for(i=0; i<4; i++){
  414. int vsub= ((i+1)&2) ? scale->vsub : 0;
  415. in_stride[i] = cur_pic->linesize[i] * mul;
  416. out_stride[i] = out_buf->linesize[i] * mul;
  417. in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
  418. out[i] = out_buf->data[i] + field * out_buf->linesize[i];
  419. }
  420. if(scale->input_is_pal)
  421. in[1] = cur_pic->data[1];
  422. if(scale->output_is_pal)
  423. out[1] = out_buf->data[1];
  424. return sws_scale(sws, in, in_stride, y/mul, h,
  425. out,out_stride);
  426. }
  427. static int filter_frame(AVFilterLink *link, AVFrame *in)
  428. {
  429. ScaleContext *scale = link->dst->priv;
  430. AVFilterLink *outlink = link->dst->outputs[0];
  431. AVFrame *out;
  432. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  433. char buf[32];
  434. int in_range;
  435. if (av_frame_get_colorspace(in) == AVCOL_SPC_YCGCO)
  436. av_log(link->dst, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
  437. if( in->width != link->w
  438. || in->height != link->h
  439. || in->format != link->format
  440. || in->sample_aspect_ratio.den != link->sample_aspect_ratio.den || in->sample_aspect_ratio.num != link->sample_aspect_ratio.num) {
  441. int ret;
  442. if (scale->eval_mode == EVAL_MODE_INIT) {
  443. snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
  444. av_opt_set(scale, "w", buf, 0);
  445. snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
  446. av_opt_set(scale, "h", buf, 0);
  447. }
  448. link->dst->inputs[0]->format = in->format;
  449. link->dst->inputs[0]->w = in->width;
  450. link->dst->inputs[0]->h = in->height;
  451. link->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
  452. link->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
  453. if ((ret = config_props(outlink)) < 0)
  454. return ret;
  455. }
  456. if (!scale->sws)
  457. return ff_filter_frame(outlink, in);
  458. scale->hsub = desc->log2_chroma_w;
  459. scale->vsub = desc->log2_chroma_h;
  460. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  461. if (!out) {
  462. av_frame_free(&in);
  463. return AVERROR(ENOMEM);
  464. }
  465. av_frame_copy_props(out, in);
  466. out->width = outlink->w;
  467. out->height = outlink->h;
  468. if(scale->output_is_pal)
  469. avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
  470. in_range = av_frame_get_color_range(in);
  471. if ( scale->in_color_matrix
  472. || scale->out_color_matrix
  473. || scale-> in_range != AVCOL_RANGE_UNSPECIFIED
  474. || in_range != AVCOL_RANGE_UNSPECIFIED
  475. || scale->out_range != AVCOL_RANGE_UNSPECIFIED) {
  476. int in_full, out_full, brightness, contrast, saturation;
  477. const int *inv_table, *table;
  478. sws_getColorspaceDetails(scale->sws, (int **)&inv_table, &in_full,
  479. (int **)&table, &out_full,
  480. &brightness, &contrast, &saturation);
  481. if (scale->in_color_matrix)
  482. inv_table = parse_yuv_type(scale->in_color_matrix, av_frame_get_colorspace(in));
  483. if (scale->out_color_matrix)
  484. table = parse_yuv_type(scale->out_color_matrix, AVCOL_SPC_UNSPECIFIED);
  485. else if (scale->in_color_matrix)
  486. table = inv_table;
  487. if (scale-> in_range != AVCOL_RANGE_UNSPECIFIED)
  488. in_full = (scale-> in_range == AVCOL_RANGE_JPEG);
  489. else if (in_range != AVCOL_RANGE_UNSPECIFIED)
  490. in_full = (in_range == AVCOL_RANGE_JPEG);
  491. if (scale->out_range != AVCOL_RANGE_UNSPECIFIED)
  492. out_full = (scale->out_range == AVCOL_RANGE_JPEG);
  493. sws_setColorspaceDetails(scale->sws, inv_table, in_full,
  494. table, out_full,
  495. brightness, contrast, saturation);
  496. if (scale->isws[0])
  497. sws_setColorspaceDetails(scale->isws[0], inv_table, in_full,
  498. table, out_full,
  499. brightness, contrast, saturation);
  500. if (scale->isws[1])
  501. sws_setColorspaceDetails(scale->isws[1], inv_table, in_full,
  502. table, out_full,
  503. brightness, contrast, saturation);
  504. av_frame_set_color_range(out, out_full ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG);
  505. }
  506. av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
  507. (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
  508. (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
  509. INT_MAX);
  510. if(scale->interlaced>0 || (scale->interlaced<0 && in->interlaced_frame)){
  511. scale_slice(link, out, in, scale->isws[0], 0, (link->h+1)/2, 2, 0);
  512. scale_slice(link, out, in, scale->isws[1], 0, link->h /2, 2, 1);
  513. }else if (scale->nb_slices) {
  514. int i, slice_h, slice_start, slice_end = 0;
  515. const int nb_slices = FFMIN(scale->nb_slices, link->h);
  516. for (i = 0; i < nb_slices; i++) {
  517. slice_start = slice_end;
  518. slice_end = (link->h * (i+1)) / nb_slices;
  519. slice_h = slice_end - slice_start;
  520. scale_slice(link, out, in, scale->sws, slice_start, slice_h, 1, 0);
  521. }
  522. }else{
  523. scale_slice(link, out, in, scale->sws, 0, link->h, 1, 0);
  524. }
  525. av_frame_free(&in);
  526. return ff_filter_frame(outlink, out);
  527. }
  528. static int filter_frame_ref(AVFilterLink *link, AVFrame *in)
  529. {
  530. AVFilterLink *outlink = link->dst->outputs[1];
  531. return ff_filter_frame(outlink, in);
  532. }
  533. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  534. char *res, int res_len, int flags)
  535. {
  536. ScaleContext *scale = ctx->priv;
  537. int ret;
  538. if ( !strcmp(cmd, "width") || !strcmp(cmd, "w")
  539. || !strcmp(cmd, "height") || !strcmp(cmd, "h")) {
  540. int old_w = scale->w;
  541. int old_h = scale->h;
  542. AVFilterLink *outlink = ctx->outputs[0];
  543. av_opt_set(scale, cmd, args, 0);
  544. if ((ret = config_props(outlink)) < 0) {
  545. scale->w = old_w;
  546. scale->h = old_h;
  547. }
  548. } else
  549. ret = AVERROR(ENOSYS);
  550. return ret;
  551. }
  552. static const AVClass *child_class_next(const AVClass *prev)
  553. {
  554. return prev ? NULL : sws_get_class();
  555. }
  556. #define OFFSET(x) offsetof(ScaleContext, x)
  557. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  558. static const AVOption scale_options[] = {
  559. { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  560. { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  561. { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  562. { "height","Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  563. { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
  564. { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_BOOL, {.i64 = 0 }, -1, 1, FLAGS },
  565. { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  566. { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  567. { "in_color_matrix", "set input YCbCr type", OFFSET(in_color_matrix), AV_OPT_TYPE_STRING, { .str = "auto" }, .flags = FLAGS },
  568. { "out_color_matrix", "set output YCbCr type", OFFSET(out_color_matrix), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
  569. { "in_range", "set input color range", OFFSET( in_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" },
  570. { "out_range", "set output color range", OFFSET(out_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" },
  571. { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 0, FLAGS, "range" },
  572. { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  573. { "jpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  574. { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, "range" },
  575. { "tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, "range" },
  576. { "pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  577. { "in_v_chr_pos", "input vertical chroma position in luma grid/256" , OFFSET(in_v_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS },
  578. { "in_h_chr_pos", "input horizontal chroma position in luma grid/256", OFFSET(in_h_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS },
  579. { "out_v_chr_pos", "output vertical chroma position in luma grid/256" , OFFSET(out_v_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS },
  580. { "out_h_chr_pos", "output horizontal chroma position in luma grid/256", OFFSET(out_h_chr_pos), AV_OPT_TYPE_INT, { .i64 = -513}, -513, 512, FLAGS },
  581. { "force_original_aspect_ratio", "decrease or increase w/h if necessary to keep the original AR", OFFSET(force_original_aspect_ratio), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 2, FLAGS, "force_oar" },
  582. { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
  583. { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
  584. { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
  585. { "param0", "Scaler param 0", OFFSET(param[0]), AV_OPT_TYPE_DOUBLE, { .dbl = SWS_PARAM_DEFAULT }, INT_MIN, INT_MAX, FLAGS },
  586. { "param1", "Scaler param 1", OFFSET(param[1]), AV_OPT_TYPE_DOUBLE, { .dbl = SWS_PARAM_DEFAULT }, INT_MIN, INT_MAX, FLAGS },
  587. { "nb_slices", "set the number of slices (debug purpose only)", OFFSET(nb_slices), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  588. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  589. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  590. { "frame", "eval expressions during initialization and per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  591. { NULL }
  592. };
  593. static const AVClass scale_class = {
  594. .class_name = "scale",
  595. .item_name = av_default_item_name,
  596. .option = scale_options,
  597. .version = LIBAVUTIL_VERSION_INT,
  598. .category = AV_CLASS_CATEGORY_FILTER,
  599. .child_class_next = child_class_next,
  600. };
  601. static const AVFilterPad avfilter_vf_scale_inputs[] = {
  602. {
  603. .name = "default",
  604. .type = AVMEDIA_TYPE_VIDEO,
  605. .filter_frame = filter_frame,
  606. },
  607. { NULL }
  608. };
  609. static const AVFilterPad avfilter_vf_scale_outputs[] = {
  610. {
  611. .name = "default",
  612. .type = AVMEDIA_TYPE_VIDEO,
  613. .config_props = config_props,
  614. },
  615. { NULL }
  616. };
  617. AVFilter ff_vf_scale = {
  618. .name = "scale",
  619. .description = NULL_IF_CONFIG_SMALL("Scale the input video size and/or convert the image format."),
  620. .init_dict = init_dict,
  621. .uninit = uninit,
  622. .query_formats = query_formats,
  623. .priv_size = sizeof(ScaleContext),
  624. .priv_class = &scale_class,
  625. .inputs = avfilter_vf_scale_inputs,
  626. .outputs = avfilter_vf_scale_outputs,
  627. .process_command = process_command,
  628. };
  629. static const AVClass scale2ref_class = {
  630. .class_name = "scale2ref",
  631. .item_name = av_default_item_name,
  632. .option = scale_options,
  633. .version = LIBAVUTIL_VERSION_INT,
  634. .category = AV_CLASS_CATEGORY_FILTER,
  635. .child_class_next = child_class_next,
  636. };
  637. static const AVFilterPad avfilter_vf_scale2ref_inputs[] = {
  638. {
  639. .name = "default",
  640. .type = AVMEDIA_TYPE_VIDEO,
  641. .filter_frame = filter_frame,
  642. },
  643. {
  644. .name = "ref",
  645. .type = AVMEDIA_TYPE_VIDEO,
  646. .filter_frame = filter_frame_ref,
  647. },
  648. { NULL }
  649. };
  650. static const AVFilterPad avfilter_vf_scale2ref_outputs[] = {
  651. {
  652. .name = "default",
  653. .type = AVMEDIA_TYPE_VIDEO,
  654. .config_props = config_props,
  655. .request_frame= request_frame,
  656. },
  657. {
  658. .name = "ref",
  659. .type = AVMEDIA_TYPE_VIDEO,
  660. .config_props = config_props_ref,
  661. .request_frame= request_frame_ref,
  662. },
  663. { NULL }
  664. };
  665. AVFilter ff_vf_scale2ref = {
  666. .name = "scale2ref",
  667. .description = NULL_IF_CONFIG_SMALL("Scale the input video size and/or convert the image format to the given reference."),
  668. .init_dict = init_dict,
  669. .uninit = uninit,
  670. .query_formats = query_formats,
  671. .priv_size = sizeof(ScaleContext),
  672. .priv_class = &scale2ref_class,
  673. .inputs = avfilter_vf_scale2ref_inputs,
  674. .outputs = avfilter_vf_scale2ref_outputs,
  675. .process_command = process_command,
  676. };