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
16139@item ovsub16139@item ovsub
16140horizontal and vertical output chroma subsample values. For example for the16140horizontal and vertical output chroma subsample values. For example for the
16141pixel format "yuv422p" @var{hsub} is 2 and @var{vsub} is 1.16141pixel format "yuv422p" @var{hsub} is 2 and @var{vsub} is 1.
16142
16143@item n
16144The (sequential) number of the input frame, starting from 0.
16145Only available with @code{eval=frame}.
16146
16147@item t
16148The presentation timestamp of the input frame, expressed as a number of
16149seconds. Only available with @code{eval=frame}.
16150
16151@item pos
16152The position (byte offset) of the frame in the input stream, or NaN if
16153this information is unavailable and/or meaningless (for example in case of synthetic video).
16154Only available with @code{eval=frame}.
16142@end table16155@end table
16143 16156
16144@subsection Examples16157@subsection Examples
@@ -16362,6 +16375,19 @@ The main input video's display aspect ratio. Calculated from · 217 unmodified lines
16362The main input video's horizontal and vertical chroma subsample values.16375The main input video's horizontal and vertical chroma subsample values.
16363For example for the pixel format "yuv422p" @var{hsub} is 2 and @var{vsub}16376For example for the pixel format "yuv422p" @var{hsub} is 2 and @var{vsub}
16364is 1.16377is 1.
16378
16379@item main_n
16380The (sequential) number of the main input frame, starting from 0.
16381Only available with @code{eval=frame}.
16382
16383@item main_t
16384The presentation timestamp of the main input frame, expressed as a number of
16385seconds. Only available with @code{eval=frame}.
16386
16387@item main_pos
16388The position (byte offset) of the frame in the main input stream, or NaN if
16389this information is unavailable and/or meaningless (for example in case of synthetic video).
16390Only available with @code{eval=frame}.
16365@end table16391@end table
16366 16392
16367@subsection Examples16393@subsection Examples
libavfilter/vf_scale.c+60−4
@@ -54,6 +54,9 @@ static const char *const var_names[] = { · 53 unmodified lines
54 "vsub",54 "vsub",
55 "ohsub",55 "ohsub",
56 "ovsub",56 "ovsub",
57 "n",
58 "t",
59 "pos",
57 "main_w",60 "main_w",
58 "main_h",61 "main_h",
59 "main_a",62 "main_a",
@@ -61,6 +64,9 @@ static const char *const var_names[] = { · 1 unmodified line
61 "main_dar", "mdar",64 "main_dar", "mdar",
62 "main_hsub",65 "main_hsub",
63 "main_vsub",66 "main_vsub",
67 "main_n",
68 "main_t",
69 "main_pos",
64 NULL70 NULL
65};71};
66 72
@@ -76,6 +82,9 @@ enum var_name { · 9 unmodified lines
76 VAR_VSUB,82 VAR_VSUB,
77 VAR_OHSUB,83 VAR_OHSUB,
78 VAR_OVSUB,84 VAR_OVSUB,
85 VAR_N,
86 VAR_T,
87 VAR_POS,
79 VAR_S2R_MAIN_W,88 VAR_S2R_MAIN_W,
80 VAR_S2R_MAIN_H,89 VAR_S2R_MAIN_H,
81 VAR_S2R_MAIN_A,90 VAR_S2R_MAIN_A,
@@ -83,6 +92,9 @@ enum var_name { · 1 unmodified line
83 VAR_S2R_MAIN_DAR, VAR_S2R_MDAR,92 VAR_S2R_MAIN_DAR, VAR_S2R_MDAR,
84 VAR_S2R_MAIN_HSUB,93 VAR_S2R_MAIN_HSUB,
85 VAR_S2R_MAIN_VSUB,94 VAR_S2R_MAIN_VSUB,
95 VAR_S2R_MAIN_N,
96 VAR_S2R_MAIN_T,
97 VAR_S2R_MAIN_POS,
86 VARS_NB98 VARS_NB
87};99};
88 100
@@ -184,11 +196,25 @@ static int check_exprs(AVFilterContext *ctx) · 95 unmodified lines
184 vars_w[VAR_S2R_MAIN_DAR] || vars_h[VAR_S2R_MAIN_DAR] ||196 vars_w[VAR_S2R_MAIN_DAR] || vars_h[VAR_S2R_MAIN_DAR] ||
185 vars_w[VAR_S2R_MDAR] || vars_h[VAR_S2R_MDAR] ||197 vars_w[VAR_S2R_MDAR] || vars_h[VAR_S2R_MDAR] ||
186 vars_w[VAR_S2R_MAIN_HSUB] || vars_h[VAR_S2R_MAIN_HSUB] ||198 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]) ) {
188 av_log(ctx, AV_LOG_ERROR, "Expressions with scale2ref variables are not valid in scale filter.\n");203 av_log(ctx, AV_LOG_ERROR, "Expressions with scale2ref variables are not valid in scale filter.\n");
189 return AVERROR(EINVAL);204 return AVERROR(EINVAL);
190 }205 }
191 206
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
192 return 0;218 return 0;
193}219}
194 220
@@ -622,6 +648,8 @@ static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, s · 427 unmodified lines
622 out,out_stride);648 out,out_stride);
623}649}
624 650
651#define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
652
625static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out)653static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out)
626{654{
627 AVFilterContext *ctx = link->dst;655 AVFilterContext *ctx = link->dst;
@@ -643,10 +671,20 @@ static int scale_frame(AVFilterLink *link, AVFrame *in, AVFrame **frame_out) · 15 unmodified lines
643 in->sample_aspect_ratio.den != link->sample_aspect_ratio.den ||671 in->sample_aspect_ratio.den != link->sample_aspect_ratio.den ||
644 in->sample_aspect_ratio.num != link->sample_aspect_ratio.num;672 in->sample_aspect_ratio.num != link->sample_aspect_ratio.num;
645 673
646 if (frame_changed ||674 if (scale->eval_mode == EVAL_MODE_FRAME || frame_changed) {
647 (scale->eval_mode == EVAL_MODE_FRAME &&
648 ctx->filter == &ff_vf_scale2ref) ) {
649 int ret;675 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;
650 688
651 if (scale->eval_mode == EVAL_MODE_INIT) {689 if (scale->eval_mode == EVAL_MODE_INIT) {
652 snprintf(buf, sizeof(buf)-1, "%d", outlink->w);690 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
663 return ret;701 return ret;
664 }702 }
665 703
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
666 link->dst->inputs[0]->format = in->format;714 link->dst->inputs[0]->format = in->format;
667 link->dst->inputs[0]->w = in->width;715 link->dst->inputs[0]->w = in->width;
668 link->dst->inputs[0]->h = in->height;716 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
674 return ret;722 return ret;
675 }723 }
676 724
725scale:
677 if (!scale->sws) {726 if (!scale->sws) {
678 *frame_out = in;727 *frame_out = in;
679 return 0;728 return 0;
@@ -780,6 +829,7 @@ static int filter_frame(AVFilterLink *link, AVFrame *in) · 100 unmodified lines
780 829
781static int filter_frame_ref(AVFilterLink *link, AVFrame *in)830static int filter_frame_ref(AVFilterLink *link, AVFrame *in)
782{831{
832 ScaleContext *scale = link->dst->priv;
783 AVFilterLink *outlink = link->dst->outputs[1];833 AVFilterLink *outlink = link->dst->outputs[1];
784 int frame_changed;834 int frame_changed;
785 835
@@ -799,6 +849,12 @@ static int filter_frame_ref(AVFilterLink *link, AVFrame *in) · 13 unmodified lines
799 config_props_ref(outlink);849 config_props_ref(outlink);
800 }850 }
801 851
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
802 return ff_filter_frame(outlink, in);858 return ff_filter_frame(outlink, in);
803}859}
804 860

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