History

avfilter/vf_alphamerge: use framesync

Paul B Mahol committed 6 years ago

Commit 88db1741 parent abc884b3 files · +64 −80Browse files

Showing the changes to libavfilter (2 of 3 files). Show all

Unified · Split

2 files
  1. libavfilter/Makefile +1 −1
  2. libavfilter/vf_alphamerge.c +63 −73
libavfilter/Makefile+1−1
@@ -158,7 +158,7 @@ OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o · 157 unmodified lines
158158 # video filters
159159 OBJS-$(CONFIG_ADDROI_FILTER) += vf_addroi.o
160160 OBJS-$(CONFIG_ALPHAEXTRACT_FILTER) += vf_extractplanes.o
161−OBJS-$(CONFIG_ALPHAMERGE_FILTER) += vf_alphamerge.o
161+OBJS-$(CONFIG_ALPHAMERGE_FILTER) += vf_alphamerge.o framesync.o
162162 OBJS-$(CONFIG_AMPLIFY_FILTER) += vf_amplify.o
163163 OBJS-$(CONFIG_ASS_FILTER) += vf_subtitles.o
164164 OBJS-$(CONFIG_ATADENOISE_FILTER) += vf_atadenoise.o
libavfilter/vf_alphamerge.c+63−73
@@ -32,6 +32,7 @@ · 31 unmodified lines
3232 #include "drawutils.h"
3333 #include "formats.h"
3434 #include "filters.h"
35+#include "framesync.h"
3536 #include "internal.h"
3637 #include "video.h"
3738
@@ -42,10 +43,54 @@ typedef struct AlphaMergeContext { · 4 unmodified lines
4243
4344 int is_packed_rgb;
4445 uint8_t rgba_map[4];
45− AVFrame *main_frame;
46− AVFrame *alpha_frame;
46+
47+ FFFrameSync fs;
4748 } AlphaMergeContext;
4849
50+static int do_alphamerge(FFFrameSync *fs)
51+{
52+ AVFilterContext *ctx = fs->parent;
53+ AlphaMergeContext *s = ctx->priv;
54+ AVFrame *main_buf, *alpha_buf;
55+ int ret;
56+
57+ ret = ff_framesync_dualinput_get_writable(fs, &main_buf, &alpha_buf);
58+ if (ret < 0)
59+ return ret;
60+ if (!alpha_buf)
61+ return ff_filter_frame(ctx->outputs[0], main_buf);
62+
63+ if (s->is_packed_rgb) {
64+ int x, y;
65+ uint8_t *pin, *pout;
66+ for (y = 0; y < main_buf->height; y++) {
67+ pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
68+ pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A];
69+ for (x = 0; x < main_buf->width; x++) {
70+ *pout = *pin;
71+ pin += 1;
72+ pout += 4;
73+ }
74+ }
75+ } else {
76+ const int main_linesize = main_buf->linesize[A];
77+ const int alpha_linesize = alpha_buf->linesize[Y];
78+ av_image_copy_plane(main_buf->data[A], main_linesize,
79+ alpha_buf->data[Y], alpha_linesize,
80+ FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
81+ }
82+
83+ return ff_filter_frame(ctx->outputs[0], main_buf);
84+}
85+
86+static av_cold int init(AVFilterContext *ctx)
87+{
88+ AlphaMergeContext *s = ctx->priv;
89+
90+ s->fs.on_event = do_alphamerge;
91+ return 0;
92+}
93+
4994 static int query_formats(AVFilterContext *ctx)
5095 {
5196 static const enum AVPixelFormat main_fmts[] = {
@@ -78,8 +123,11 @@ static int config_input_main(AVFilterLink *inlink) · 26 unmodified lines
78123 static int config_output(AVFilterLink *outlink)
79124 {
80125 AVFilterContext *ctx = outlink->src;
126+ AlphaMergeContext *s = ctx->priv;
81127 AVFilterLink *mainlink = ctx->inputs[0];
82128 AVFilterLink *alphalink = ctx->inputs[1];
129+ int ret;
130+
83131 if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
84132 av_log(ctx, AV_LOG_ERROR,
85133 "Input frame sizes do not match (%dx%d vs %dx%d).\n",
@@ -88,89 +136,29 @@ static int config_output(AVFilterLink *outlink) · 2 unmodified lines
88136 return AVERROR(EINVAL);
89137 }
90138
139+ if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
140+ return ret;
141+
91142 outlink->w = mainlink->w;
92143 outlink->h = mainlink->h;
93144 outlink->time_base = mainlink->time_base;
94145 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
95146 outlink->frame_rate = mainlink->frame_rate;
96− return 0;
147+
148+ return ff_framesync_configure(&s->fs);
97149 }
98150
99−static void draw_frame(AVFilterContext *ctx,
100− AVFrame *main_buf,
101− AVFrame *alpha_buf)
151+static int activate(AVFilterContext *ctx)
102152 {
103153 AlphaMergeContext *s = ctx->priv;
104− int h = main_buf->height;
105−
106− if (s->is_packed_rgb) {
107− int x, y;
108− uint8_t *pin, *pout;
109− for (y = 0; y < h; y++) {
110− pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
111− pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A];
112− for (x = 0; x < main_buf->width; x++) {
113− *pout = *pin;
114− pin += 1;
115− pout += 4;
116− }
117− }
118− } else {
119− const int main_linesize = main_buf->linesize[A];
120− const int alpha_linesize = alpha_buf->linesize[Y];
121− av_image_copy_plane(main_buf->data[A], main_linesize,
122− alpha_buf->data[Y], alpha_linesize,
123− FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
124− }
154+ return ff_framesync_activate(&s->fs);
125155 }
126156
127−static int activate(AVFilterContext *ctx)
157+static av_cold void uninit(AVFilterContext *ctx)
128158 {
129− AVFilterLink *outlink = ctx->outputs[0];
130159 AlphaMergeContext *s = ctx->priv;
131− int ret;
132−
133− FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
134−
135− if (!s->main_frame) {
136− ret = ff_inlink_consume_frame(ctx->inputs[0], &s->main_frame);
137− if (ret < 0)
138− return ret;
139− }
140−
141− if (!s->alpha_frame) {
142− ret = ff_inlink_consume_frame(ctx->inputs[1], &s->alpha_frame);
143− if (ret < 0)
144− return ret;
145− }
146−
147− if (s->main_frame && s->alpha_frame) {
148− if (!ctx->is_disabled)
149− draw_frame(ctx, s->main_frame, s->alpha_frame);
150− ret = ff_filter_frame(outlink, s->main_frame);
151− av_frame_free(&s->alpha_frame);
152− s->main_frame = NULL;
153− return ret;
154− }
155−
156− FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
157− FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
158−
159− if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
160− !ff_outlink_get_status(ctx->inputs[0]) &&
161− !s->main_frame) {
162− ff_inlink_request_frame(ctx->inputs[0]);
163− return 0;
164− }
165−
166− if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
167− !ff_outlink_get_status(ctx->inputs[1]) &&
168− !s->alpha_frame) {
169− ff_inlink_request_frame(ctx->inputs[1]);
170− return 0;
171− }
172160
173− return FFERROR_NOT_READY;
161+ ff_framesync_uninit(&s->fs);
174162 }
175163
176164 static const AVFilterPad alphamerge_inputs[] = {
@@ -178,7 +166,6 @@ static const AVFilterPad alphamerge_inputs[] = { · 1 unmodified line
178166 .name = "main",
179167 .type = AVMEDIA_TYPE_VIDEO,
180168 .config_props = config_input_main,
181− .needs_writable = 1,
182169 },{
183170 .name = "alpha",
184171 .type = AVMEDIA_TYPE_VIDEO,
@@ -199,17 +186,20 @@ static const AVOption alphamerge_options[] = { · 14 unmodified lines
199186 { NULL }
200187 };
201188
202−AVFILTER_DEFINE_CLASS(alphamerge);
189+FRAMESYNC_DEFINE_CLASS(alphamerge, AlphaMergeContext, fs);
203190
204191 AVFilter ff_vf_alphamerge = {
205192 .name = "alphamerge",
206193 .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
207194 "input into the alpha channel of the first input."),
195+ .preinit = alphamerge_framesync_preinit,
208196 .priv_size = sizeof(AlphaMergeContext),
209197 .priv_class = &alphamerge_class,
198+ .init = init,
210199 .query_formats = query_formats,
211200 .inputs = alphamerge_inputs,
212201 .outputs = alphamerge_outputs,
202+ .uninit = uninit,
213203 .activate = activate,
214204 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
215205 };

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