vf_scale.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. typedef struct ScaleContext {
  69. const AVClass *class;
  70. struct SwsContext *sws; ///< software scaler context
  71. struct SwsContext *isws[2]; ///< software scaler context for interlaced material
  72. AVDictionary *opts;
  73. /**
  74. * New dimensions. Special values are:
  75. * 0 = original width/height
  76. * -1 = keep original aspect
  77. * -N = try to keep aspect but make sure it is divisible by N
  78. */
  79. int w, h;
  80. char *size_str;
  81. unsigned int flags; ///sws flags
  82. int hsub, vsub; ///< chroma subsampling
  83. int slice_y; ///< top of current output slice
  84. int input_is_pal; ///< set to 1 if the input format is paletted
  85. int output_is_pal; ///< set to 1 if the output format is paletted
  86. int interlaced;
  87. char *w_expr; ///< width expression string
  88. char *h_expr; ///< height expression string
  89. char *flags_str;
  90. char *in_color_matrix;
  91. char *out_color_matrix;
  92. int in_range;
  93. int out_range;
  94. int out_h_chr_pos;
  95. int out_v_chr_pos;
  96. int in_h_chr_pos;
  97. int in_v_chr_pos;
  98. int force_original_aspect_ratio;
  99. } ScaleContext;
  100. static av_cold int init_dict(AVFilterContext *ctx, AVDictionary **opts)
  101. {
  102. ScaleContext *scale = ctx->priv;
  103. int ret;
  104. if (scale->size_str && (scale->w_expr || scale->h_expr)) {
  105. av_log(ctx, AV_LOG_ERROR,
  106. "Size and width/height expressions cannot be set at the same time.\n");
  107. return AVERROR(EINVAL);
  108. }
  109. if (scale->w_expr && !scale->h_expr)
  110. FFSWAP(char *, scale->w_expr, scale->size_str);
  111. if (scale->size_str) {
  112. char buf[32];
  113. if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
  114. av_log(ctx, AV_LOG_ERROR,
  115. "Invalid size '%s'\n", scale->size_str);
  116. return ret;
  117. }
  118. snprintf(buf, sizeof(buf)-1, "%d", scale->w);
  119. av_opt_set(scale, "w", buf, 0);
  120. snprintf(buf, sizeof(buf)-1, "%d", scale->h);
  121. av_opt_set(scale, "h", buf, 0);
  122. }
  123. if (!scale->w_expr)
  124. av_opt_set(scale, "w", "iw", 0);
  125. if (!scale->h_expr)
  126. av_opt_set(scale, "h", "ih", 0);
  127. av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
  128. scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
  129. scale->flags = 0;
  130. if (scale->flags_str) {
  131. const AVClass *class = sws_get_class();
  132. const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
  133. AV_OPT_SEARCH_FAKE_OBJ);
  134. int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
  135. if (ret < 0)
  136. return ret;
  137. }
  138. scale->opts = *opts;
  139. *opts = NULL;
  140. return 0;
  141. }
  142. static av_cold void uninit(AVFilterContext *ctx)
  143. {
  144. ScaleContext *scale = ctx->priv;
  145. sws_freeContext(scale->sws);
  146. sws_freeContext(scale->isws[0]);
  147. sws_freeContext(scale->isws[1]);
  148. scale->sws = NULL;
  149. av_dict_free(&scale->opts);
  150. }
  151. static int query_formats(AVFilterContext *ctx)
  152. {
  153. AVFilterFormats *formats;
  154. enum AVPixelFormat pix_fmt;
  155. int ret;
  156. if (ctx->inputs[0]) {
  157. const AVPixFmtDescriptor *desc = NULL;
  158. formats = NULL;
  159. while ((desc = av_pix_fmt_desc_next(desc))) {
  160. pix_fmt = av_pix_fmt_desc_get_id(desc);
  161. if ((sws_isSupportedInput(pix_fmt) ||
  162. sws_isSupportedEndiannessConversion(pix_fmt))
  163. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  164. ff_formats_unref(&formats);
  165. return ret;
  166. }
  167. }
  168. ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
  169. }
  170. if (ctx->outputs[0]) {
  171. const AVPixFmtDescriptor *desc = NULL;
  172. formats = NULL;
  173. while ((desc = av_pix_fmt_desc_next(desc))) {
  174. pix_fmt = av_pix_fmt_desc_get_id(desc);
  175. if ((sws_isSupportedOutput(pix_fmt) || pix_fmt == AV_PIX_FMT_PAL8 ||
  176. sws_isSupportedEndiannessConversion(pix_fmt))
  177. && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
  178. ff_formats_unref(&formats);
  179. return ret;
  180. }
  181. }
  182. ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
  183. }
  184. return 0;
  185. }
  186. static const int *parse_yuv_type(const char *s, enum AVColorSpace colorspace)
  187. {
  188. if (!s)
  189. s = "bt601";
  190. if (s && strstr(s, "bt709")) {
  191. colorspace = AVCOL_SPC_BT709;
  192. } else if (s && strstr(s, "fcc")) {
  193. colorspace = AVCOL_SPC_FCC;
  194. } else if (s && strstr(s, "smpte240m")) {
  195. colorspace = AVCOL_SPC_SMPTE240M;
  196. } else if (s && (strstr(s, "bt601") || strstr(s, "bt470") || strstr(s, "smpte170m"))) {
  197. colorspace = AVCOL_SPC_BT470BG;
  198. }
  199. if (colorspace < 1 || colorspace > 7) {
  200. colorspace = AVCOL_SPC_BT470BG;
  201. }
  202. return sws_getCoefficients(colorspace);
  203. }
  204. static int config_props(AVFilterLink *outlink)
  205. {
  206. AVFilterContext *ctx = outlink->src;
  207. AVFilterLink *inlink = outlink->src->inputs[0];
  208. enum AVPixelFormat outfmt = outlink->format;
  209. ScaleContext *scale = ctx->priv;
  210. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  211. const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(outlink->format);
  212. int64_t w, h;
  213. double var_values[VARS_NB], res;
  214. char *expr;
  215. int ret;
  216. int factor_w, factor_h;
  217. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  218. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  219. var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  220. var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  221. var_values[VAR_A] = (double) inlink->w / inlink->h;
  222. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  223. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  224. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  225. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  226. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  227. var_values[VAR_OHSUB] = 1 << out_desc->log2_chroma_w;
  228. var_values[VAR_OVSUB] = 1 << out_desc->log2_chroma_h;
  229. /* evaluate width and height */
  230. av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  231. var_names, var_values,
  232. NULL, NULL, NULL, NULL, NULL, 0, ctx);
  233. scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  234. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
  235. var_names, var_values,
  236. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  237. goto fail;
  238. scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  239. /* evaluate again the width, as it may depend on the output height */
  240. if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
  241. var_names, var_values,
  242. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  243. goto fail;
  244. scale->w = res;
  245. w = scale->w;
  246. h = scale->h;
  247. /* Check if it is requested that the result has to be divisible by a some
  248. * factor (w or h = -n with n being the factor). */
  249. factor_w = 1;
  250. factor_h = 1;
  251. if (w < -1) {
  252. factor_w = -w;
  253. }
  254. if (h < -1) {
  255. factor_h = -h;
  256. }
  257. if (w < 0 && h < 0)
  258. scale->w = scale->h = 0;
  259. if (!(w = scale->w))
  260. w = inlink->w;
  261. if (!(h = scale->h))
  262. h = inlink->h;
  263. /* Make sure that the result is divisible by the factor we determined
  264. * earlier. If no factor was set, it is nothing will happen as the default
  265. * factor is 1 */
  266. if (w < 0)
  267. w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w;
  268. if (h < 0)
  269. h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h;
  270. /* Note that force_original_aspect_ratio may overwrite the previous set
  271. * dimensions so that it is not divisible by the set factors anymore. */
  272. if (scale->force_original_aspect_ratio) {
  273. int tmp_w = av_rescale(h, inlink->w, inlink->h);
  274. int tmp_h = av_rescale(w, inlink->h, inlink->w);
  275. if (scale->force_original_aspect_ratio == 1) {
  276. w = FFMIN(tmp_w, w);
  277. h = FFMIN(tmp_h, h);
  278. } else {
  279. w = FFMAX(tmp_w, w);
  280. h = FFMAX(tmp_h, h);
  281. }
  282. }
  283. if (w > INT_MAX || h > INT_MAX ||
  284. (h * inlink->w) > INT_MAX ||
  285. (w * inlink->h) > INT_MAX)
  286. av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
  287. outlink->w = w;
  288. outlink->h = h;
  289. /* TODO: make algorithm configurable */
  290. scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL ||
  291. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
  292. if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
  293. scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PAL ||
  294. av_pix_fmt_desc_get(outfmt)->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
  295. if (scale->sws)
  296. sws_freeContext(scale->sws);
  297. if (scale->isws[0])
  298. sws_freeContext(scale->isws[0]);
  299. if (scale->isws[1])
  300. sws_freeContext(scale->isws[1]);
  301. scale->isws[0] = scale->isws[1] = scale->sws = NULL;
  302. if (inlink->w == outlink->w && inlink->h == outlink->h &&
  303. inlink->format == outlink->format)
  304. ;
  305. else {
  306. struct SwsContext **swscs[3] = {&scale->sws, &scale->isws[0], &scale->isws[1]};
  307. int i;
  308. for (i = 0; i < 3; i++) {
  309. struct SwsContext **s = swscs[i];
  310. *s = sws_alloc_context();
  311. if (!*s)
  312. return AVERROR(ENOMEM);
  313. if (scale->opts) {
  314. AVDictionaryEntry *e = NULL;
  315. while ((e = av_dict_get(scale->opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
  316. if ((ret = av_opt_set(*s, e->key, e->value, 0)) < 0)
  317. return ret;
  318. }
  319. }
  320. av_opt_set_int(*s, "srcw", inlink ->w, 0);
  321. av_opt_set_int(*s, "srch", inlink ->h >> !!i, 0);
  322. av_opt_set_int(*s, "src_format", inlink->format, 0);
  323. av_opt_set_int(*s, "dstw", outlink->w, 0);
  324. av_opt_set_int(*s, "dsth", outlink->h >> !!i, 0);
  325. av_opt_set_int(*s, "dst_format", outfmt, 0);
  326. av_opt_set_int(*s, "sws_flags", scale->flags, 0);
  327. av_opt_set_int(*s, "src_h_chr_pos", scale->in_h_chr_pos, 0);
  328. av_opt_set_int(*s, "src_v_chr_pos", scale->in_v_chr_pos, 0);
  329. av_opt_set_int(*s, "dst_h_chr_pos", scale->out_h_chr_pos, 0);
  330. av_opt_set_int(*s, "dst_v_chr_pos", scale->out_v_chr_pos, 0);
  331. if ((ret = sws_init_context(*s, NULL, NULL)) < 0)
  332. return ret;
  333. if (!scale->interlaced)
  334. break;
  335. }
  336. }
  337. if (inlink->sample_aspect_ratio.num){
  338. outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
  339. } else
  340. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  341. 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",
  342. inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
  343. inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
  344. outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
  345. outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
  346. scale->flags);
  347. return 0;
  348. fail:
  349. av_log(NULL, AV_LOG_ERROR,
  350. "Error when evaluating the expression '%s'.\n"
  351. "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
  352. expr, scale->w_expr, scale->h_expr);
  353. return ret;
  354. }
  355. static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, struct SwsContext *sws, int y, int h, int mul, int field)
  356. {
  357. ScaleContext *scale = link->dst->priv;
  358. const uint8_t *in[4];
  359. uint8_t *out[4];
  360. int in_stride[4],out_stride[4];
  361. int i;
  362. for(i=0; i<4; i++){
  363. int vsub= ((i+1)&2) ? scale->vsub : 0;
  364. in_stride[i] = cur_pic->linesize[i] * mul;
  365. out_stride[i] = out_buf->linesize[i] * mul;
  366. in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
  367. out[i] = out_buf->data[i] + field * out_buf->linesize[i];
  368. }
  369. if(scale->input_is_pal)
  370. in[1] = cur_pic->data[1];
  371. if(scale->output_is_pal)
  372. out[1] = out_buf->data[1];
  373. return sws_scale(sws, in, in_stride, y/mul, h,
  374. out,out_stride);
  375. }
  376. static int filter_frame(AVFilterLink *link, AVFrame *in)
  377. {
  378. ScaleContext *scale = link->dst->priv;
  379. AVFilterLink *outlink = link->dst->outputs[0];
  380. AVFrame *out;
  381. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  382. char buf[32];
  383. int in_range;
  384. if( in->width != link->w
  385. || in->height != link->h
  386. || in->format != link->format) {
  387. int ret;
  388. snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
  389. av_opt_set(scale, "w", buf, 0);
  390. snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
  391. av_opt_set(scale, "h", buf, 0);
  392. link->dst->inputs[0]->format = in->format;
  393. link->dst->inputs[0]->w = in->width;
  394. link->dst->inputs[0]->h = in->height;
  395. if ((ret = config_props(outlink)) < 0)
  396. return ret;
  397. }
  398. if (!scale->sws)
  399. return ff_filter_frame(outlink, in);
  400. scale->hsub = desc->log2_chroma_w;
  401. scale->vsub = desc->log2_chroma_h;
  402. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  403. if (!out) {
  404. av_frame_free(&in);
  405. return AVERROR(ENOMEM);
  406. }
  407. av_frame_copy_props(out, in);
  408. out->width = outlink->w;
  409. out->height = outlink->h;
  410. if(scale->output_is_pal)
  411. avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
  412. in_range = av_frame_get_color_range(in);
  413. if ( scale->in_color_matrix
  414. || scale->out_color_matrix
  415. || scale-> in_range != AVCOL_RANGE_UNSPECIFIED
  416. || in_range != AVCOL_RANGE_UNSPECIFIED
  417. || scale->out_range != AVCOL_RANGE_UNSPECIFIED) {
  418. int in_full, out_full, brightness, contrast, saturation;
  419. const int *inv_table, *table;
  420. sws_getColorspaceDetails(scale->sws, (int **)&inv_table, &in_full,
  421. (int **)&table, &out_full,
  422. &brightness, &contrast, &saturation);
  423. if (scale->in_color_matrix)
  424. inv_table = parse_yuv_type(scale->in_color_matrix, av_frame_get_colorspace(in));
  425. if (scale->out_color_matrix)
  426. table = parse_yuv_type(scale->out_color_matrix, AVCOL_SPC_UNSPECIFIED);
  427. if (scale-> in_range != AVCOL_RANGE_UNSPECIFIED)
  428. in_full = (scale-> in_range == AVCOL_RANGE_JPEG);
  429. else if (in_range != AVCOL_RANGE_UNSPECIFIED)
  430. in_full = (in_range == AVCOL_RANGE_JPEG);
  431. if (scale->out_range != AVCOL_RANGE_UNSPECIFIED)
  432. out_full = (scale->out_range == AVCOL_RANGE_JPEG);
  433. sws_setColorspaceDetails(scale->sws, inv_table, in_full,
  434. table, out_full,
  435. brightness, contrast, saturation);
  436. if (scale->isws[0])
  437. sws_setColorspaceDetails(scale->isws[0], inv_table, in_full,
  438. table, out_full,
  439. brightness, contrast, saturation);
  440. if (scale->isws[1])
  441. sws_setColorspaceDetails(scale->isws[1], inv_table, in_full,
  442. table, out_full,
  443. brightness, contrast, saturation);
  444. }
  445. av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
  446. (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
  447. (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
  448. INT_MAX);
  449. if(scale->interlaced>0 || (scale->interlaced<0 && in->interlaced_frame)){
  450. scale_slice(link, out, in, scale->isws[0], 0, (link->h+1)/2, 2, 0);
  451. scale_slice(link, out, in, scale->isws[1], 0, link->h /2, 2, 1);
  452. }else{
  453. scale_slice(link, out, in, scale->sws, 0, link->h, 1, 0);
  454. }
  455. av_frame_free(&in);
  456. return ff_filter_frame(outlink, out);
  457. }
  458. static const AVClass *child_class_next(const AVClass *prev)
  459. {
  460. return prev ? NULL : sws_get_class();
  461. }
  462. #define OFFSET(x) offsetof(ScaleContext, x)
  463. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  464. static const AVOption scale_options[] = {
  465. { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  466. { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  467. { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  468. { "height","Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
  469. { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
  470. { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 1, FLAGS },
  471. { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  472. { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
  473. { "in_color_matrix", "set input YCbCr type", OFFSET(in_color_matrix), AV_OPT_TYPE_STRING, { .str = "auto" }, .flags = FLAGS },
  474. { "out_color_matrix", "set output YCbCr type", OFFSET(out_color_matrix), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
  475. { "in_range", "set input color range", OFFSET( in_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" },
  476. { "out_range", "set output color range", OFFSET(out_range), AV_OPT_TYPE_INT, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 2, FLAGS, "range" },
  477. { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_UNSPECIFIED }, 0, 0, FLAGS, "range" },
  478. { "full", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  479. { "jpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  480. { "mpeg", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, "range" },
  481. { "tv", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, "range" },
  482. { "pc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, "range" },
  483. { "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 },
  484. { "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 },
  485. { "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 },
  486. { "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 },
  487. { "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" },
  488. { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
  489. { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
  490. { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
  491. { NULL }
  492. };
  493. static const AVClass scale_class = {
  494. .class_name = "scale",
  495. .item_name = av_default_item_name,
  496. .option = scale_options,
  497. .version = LIBAVUTIL_VERSION_INT,
  498. .category = AV_CLASS_CATEGORY_FILTER,
  499. .child_class_next = child_class_next,
  500. };
  501. static const AVFilterPad avfilter_vf_scale_inputs[] = {
  502. {
  503. .name = "default",
  504. .type = AVMEDIA_TYPE_VIDEO,
  505. .filter_frame = filter_frame,
  506. },
  507. { NULL }
  508. };
  509. static const AVFilterPad avfilter_vf_scale_outputs[] = {
  510. {
  511. .name = "default",
  512. .type = AVMEDIA_TYPE_VIDEO,
  513. .config_props = config_props,
  514. },
  515. { NULL }
  516. };
  517. AVFilter ff_vf_scale = {
  518. .name = "scale",
  519. .description = NULL_IF_CONFIG_SMALL("Scale the input video size and/or convert the image format."),
  520. .init_dict = init_dict,
  521. .uninit = uninit,
  522. .query_formats = query_formats,
  523. .priv_size = sizeof(ScaleContext),
  524. .priv_class = &scale_class,
  525. .inputs = avfilter_vf_scale_inputs,
  526. .outputs = avfilter_vf_scale_outputs,
  527. };