History

avfilter/scale: add animation support

Width and height expressions in scale and scale2ref filters can now reference frame index, timestamp and packet position.

Gyan Doshi committed 6 years ago

Commit 0dc08371 parent ce2cfa62 files · +86 −4Browse files

Unified · Split

2 files
  1. doc/filters.texi +26 −0
  2. libavfilter/vf_scale.c +60 −4
doc/filters.texi+26−0
@@ -16139,6 +16139,19 @@ pixel format "yuv422p" @var{hsub} is 2 and @var{vsub} is 1. · 16138 unmodified lines
1613916139 @item ovsub
1614016140 horizontal and vertical output chroma subsample values. For example for the
1614116141 pixel format "yuv422p" @var{hsub} is 2 and @var{vsub} is 1.
16142+
16143+@item n
16144+The (sequential) number of the input frame, starting from 0.
16145+Only available with @code{eval=frame}.
16146+
16147+@item t
16148+The presentation timestamp of the input frame, expressed as a number of
16149+seconds. Only available with @code{eval=frame}.
16150+
16151+@item pos
16152+The position (byte offset) of the frame in the input stream, or NaN if
16153+this information is unavailable and/or meaningless (for example in case of synthetic video).
16154+Only available with @code{eval=frame}.
1614216155 @end table
1614316156
1614416157 @subsection Examples
@@ -16362,6 +16375,19 @@ The main input video's display aspect ratio. Calculated from · 217 unmodified lines
1636216375 The main input video's horizontal and vertical chroma subsample values.
1636316376 For example for the pixel format "yuv422p" @var{hsub} is 2 and @var{vsub}
1636416377 is 1.
16378+
16379+@item main_n
16380+The (sequential) number of the main input frame, starting from 0.
16381+Only available with @code{eval=frame}.
16382+
16383+@item main_t
16384+The presentation timestamp of the main input frame, expressed as a number of
16385+seconds. Only available with @code{eval=frame}.
16386+
16387+@item main_pos
16388+The position (byte offset) of the frame in the main input stream, or NaN if
16389+this information is unavailable and/or meaningless (for example in case of synthetic video).
16390+Only available with @code{eval=frame}.
1636516391 @end table
1636616392
1636716393 @subsection Examples
libavfilter/vf_scale.c+60−4
@@ -54,6 +54,9 @@ static const char *const var_names[] = { · 53 unmodified lines
5454 "vsub",
5555 "ohsub",
5656 "ovsub",
57+ "n",
58+ "t",
59+ "pos",
5760 "main_w",
5861 "main_h",
5962 "main_a",
@@ -61,6 +64,9 @@ static const char *const var_names[] = { · 1 unmodified line
6164 "main_dar", "mdar",
6265 "main_hsub",
6366 "main_vsub",
67+ "main_n",
68+ "main_t",
69+ "main_pos",
6470 NULL
6571 };
6672
@@ -76,6 +82,9 @@ enum var_name { · 9 unmodified lines
7682 VAR_VSUB,
7783 VAR_OHSUB,
7884 VAR_OVSUB,
85+ VAR_N,
86+ VAR_T,
87+ VAR_POS,
7988 VAR_S2R_MAIN_W,
8089 VAR_S2R_MAIN_H,
8190 VAR_S2R_MAIN_A,
@@ -83,6 +92,9 @@ enum var_name { · 1 unmodified line
8392 VAR_S2R_MAIN_DAR, VAR_S2R_MDAR,
8493 VAR_S2R_MAIN_HSUB,
8594 VAR_S2R_MAIN_VSUB,
95+ VAR_S2R_MAIN_N,
96+ VAR_S2R_MAIN_T,
97+ VAR_S2R_MAIN_POS,
8698 VARS_NB
8799 };
88100
@@ -184,11 +196,25 @@ static int check_exprs(AVFilterContext *ctx) · 95 unmodified lines
184196 vars_w[VAR_S2R_MAIN_DAR] || vars_h[VAR_S2R_MAIN_DAR] ||
185197 vars_w[VAR_S2R_MDAR] || vars_h[VAR_S2R_MDAR] ||
186198 vars_w[VAR_S2R_MAIN_HSUB] || vars_h[VAR_S2R_MAIN_HSUB] ||
187− vars_w[VAR_S2R_MAIN_VSUB] || vars_h[VAR_S2R_MAIN_VSUB]) ) {
199+ vars_w[VAR_S2R_MAIN_VSUB] || vars_h[VAR_S2R_MAIN_VSUB] ||
200+ vars_w[VAR_S2R_MAIN_N] || vars_h[VAR_S2R_MAIN_N] ||
201+ vars_w[VAR_S2R_MAIN_T] || vars_h[VAR_S2R_MAIN_T] ||
202+ vars_w[VAR_S2R_MAIN_POS] || vars_h[VAR_S2R_MAIN_POS]) ) {
188203 av_log(ctx, AV_LOG_ERROR, "Expressions with scale2ref variables are not valid in scale filter.\n");
189204 return AVERROR(EINVAL);
190205 }
191206
207+ if (scale->eval_mode == EVAL_MODE_INIT &&
208+ (vars_w[VAR_N] || vars_h[VAR_N] ||
209+ vars_w[VAR_T] || vars_h[VAR_T] ||
210+ vars_w[VAR_POS] || vars_h[VAR_POS] ||
211+ vars_w[VAR_S2R_MAIN_N] || vars_h[VAR_S2R_MAIN_N] ||
212+ vars_w[VAR_S2R_MAIN_T] || vars_h[VAR_S2R_MAIN_T] ||
213+ vars_w[VAR_S2R_MAIN_POS] || vars_h[VAR_S2R_MAIN_POS]) ) {
214+ av_log(ctx, AV_LOG_ERROR, "Expressions with frame variables 'n', 't', 'pos' are not valid in init eval_mode.\n");
215+ return AVERROR(EINVAL);
216+ }
217+
192218 return 0;
193219 }
194220
@@ -622,6 +648,8 @@ static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, s · 427 unmodified lines
622648 out,out_stride);
623649 }
624650
651+#define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
652+
625653 static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out)
626654 {
627655 AVFilterContext *ctx = link->dst;
@@ -643,10 +671,20 @@ static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) · 15 unmodified lines
643671 in->sample_aspect_ratio.den != link->sample_aspect_ratio.den ||
644672 in->sample_aspect_ratio.num != link->sample_aspect_ratio.num;
645673
646− if (frame_changed ||
647− (scale->eval_mode == EVAL_MODE_FRAME &&
648− ctx->filter == &ff_vf_scale2ref) ) {
674+ if (scale->eval_mode == EVAL_MODE_FRAME || frame_changed) {
649675 int ret;
676+ unsigned vars_w[VARS_NB] = { 0 }, vars_h[VARS_NB] = { 0 };
677+
678+ av_expr_count_vars(scale->w_pexpr, vars_w, VARS_NB);
679+ av_expr_count_vars(scale->h_pexpr, vars_h, VARS_NB);
680+
681+ if (scale->eval_mode == EVAL_MODE_FRAME &&
682+ !frame_changed &&
683+ ctx->filter != &ff_vf_scale2ref &&
684+ !(vars_w[VAR_N] || vars_w[VAR_T] || vars_w[VAR_POS]) &&
685+ !(vars_h[VAR_N] || vars_h[VAR_T] || vars_h[VAR_POS]) &&
686+ scale->w && scale->h)
687+ goto scale;
650688
651689 if (scale->eval_mode == EVAL_MODE_INIT) {
652690 snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
@@ -663,6 +701,16 @@ static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) · 10 unmodified lines
663701 return ret;
664702 }
665703
704+ if (ctx->filter == &ff_vf_scale2ref) {
705+ scale->var_values[VAR_S2R_MAIN_N] = link->frame_count_out;
706+ scale->var_values[VAR_S2R_MAIN_T] = TS2T(in->pts, link->time_base);
707+ scale->var_values[VAR_S2R_MAIN_POS] = in->pkt_pos == -1 ? NAN : in->pkt_pos;
708+ } else {
709+ scale->var_values[VAR_N] = link->frame_count_out;
710+ scale->var_values[VAR_T] = TS2T(in->pts, link->time_base);
711+ scale->var_values[VAR_POS] = in->pkt_pos == -1 ? NAN : in->pkt_pos;
712+ }
713+
666714 link->dst->inputs[0]->format = in->format;
667715 link->dst->inputs[0]->w = in->width;
668716 link->dst->inputs[0]->h = in->height;
@@ -674,6 +722,7 @@ static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) · 5 unmodified lines
674722 return ret;
675723 }
676724
725+scale:
677726 if (!scale->sws) {
678727 *frame_out = in;
679728 return 0;
@@ -780,6 +829,7 @@ static int filter_frame(AVFilterLink *link, AVFrame *in) · 100 unmodified lines
780829
781830 static int filter_frame_ref(AVFilterLink *link, AVFrame *in)
782831 {
832+ ScaleContext *scale = link->dst->priv;
783833 AVFilterLink *outlink = link->dst->outputs[1];
784834 int frame_changed;
785835
@@ -799,6 +849,12 @@ static int filter_frame_ref(AVFilterLink *link, AVFrame *in) · 13 unmodified lines
799849 config_props_ref(outlink);
800850 }
801851
852+ if (scale->eval_mode == EVAL_MODE_FRAME) {
853+ scale->var_values[VAR_N] = link->frame_count_out;
854+ scale->var_values[VAR_T] = TS2T(in->pts, link->time_base);
855+ scale->var_values[VAR_POS] = in->pkt_pos == -1 ? NAN : in->pkt_pos;
856+ }
857+
802858 return ff_filter_frame(outlink, in);
803859 }
804860

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t