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.

734 lines
28KB

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