History

avfilter/vf_alphamerge: use framesync

Paul B Mahol committed 6 years ago

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

Unified · Split

3 files
  1. doc/filters.texi +0 −6
  2. libavfilter/Makefile +1 −1
  3. libavfilter/vf_alphamerge.c +63 −73
doc/filters.texi+0−6
@@ -6470,12 +6470,6 @@ and a separate video created with @var{alphaextract}, you might use: · 6469 unmodified lines
6470movie=in_alpha.mkv [alpha]; [in][alpha] alphamerge [out]6470movie=in_alpha.mkv [alpha]; [in][alpha] alphamerge [out]
6471@end example6471@end example
6472 6472
6473Since this filter is designed for reconstruction, it operates on frame
6474sequences without considering timestamps, and terminates when either
6475input reaches end of stream. This will cause problems if your encoding
6476pipeline drops frames. If you're trying to apply an image as an
6477overlay to a video stream, consider the @var{overlay} filter instead.
6478
6479@section amplify6473@section amplify
6480 6474
6481Amplify differences between current pixel and pixels of adjacent frames in6475Amplify differences between current pixel and pixels of adjacent frames in
libavfilter/Makefile+1−1
@@ -158,7 +158,7 @@ OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o · 157 unmodified lines
158# video filters158# video filters
159OBJS-$(CONFIG_ADDROI_FILTER) += vf_addroi.o159OBJS-$(CONFIG_ADDROI_FILTER) += vf_addroi.o
160OBJS-$(CONFIG_ALPHAEXTRACT_FILTER) += vf_extractplanes.o160OBJS-$(CONFIG_ALPHAEXTRACT_FILTER) += vf_extractplanes.o
161OBJS-$(CONFIG_ALPHAMERGE_FILTER) += vf_alphamerge.o161OBJS-$(CONFIG_ALPHAMERGE_FILTER) += vf_alphamerge.o framesync.o
162OBJS-$(CONFIG_AMPLIFY_FILTER) += vf_amplify.o162OBJS-$(CONFIG_AMPLIFY_FILTER) += vf_amplify.o
163OBJS-$(CONFIG_ASS_FILTER) += vf_subtitles.o163OBJS-$(CONFIG_ASS_FILTER) += vf_subtitles.o
164OBJS-$(CONFIG_ATADENOISE_FILTER) += vf_atadenoise.o164OBJS-$(CONFIG_ATADENOISE_FILTER) += vf_atadenoise.o
libavfilter/vf_alphamerge.c+63−73
@@ -32,6 +32,7 @@ · 31 unmodified lines
32#include "drawutils.h"32#include "drawutils.h"
33#include "formats.h"33#include "formats.h"
34#include "filters.h"34#include "filters.h"
35#include "framesync.h"
35#include "internal.h"36#include "internal.h"
36#include "video.h"37#include "video.h"
37 38
@@ -42,10 +43,54 @@ typedef struct AlphaMergeContext { · 4 unmodified lines
42 43
43 int is_packed_rgb;44 int is_packed_rgb;
44 uint8_t rgba_map[4];45 uint8_t rgba_map[4];
45 AVFrame *main_frame;46
46 AVFrame *alpha_frame;47 FFFrameSync fs;
47} AlphaMergeContext;48} AlphaMergeContext;
48 49
50static 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
86static 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
49static int query_formats(AVFilterContext *ctx)94static int query_formats(AVFilterContext *ctx)
50{95{
51 static const enum AVPixelFormat main_fmts[] = {96 static const enum AVPixelFormat main_fmts[] = {
@@ -78,8 +123,11 @@ static int config_input_main(AVFilterLink *inlink) · 26 unmodified lines
78static int config_output(AVFilterLink *outlink)123static int config_output(AVFilterLink *outlink)
79{124{
80 AVFilterContext *ctx = outlink->src;125 AVFilterContext *ctx = outlink->src;
126 AlphaMergeContext *s = ctx->priv;
81 AVFilterLink *mainlink = ctx->inputs[0];127 AVFilterLink *mainlink = ctx->inputs[0];
82 AVFilterLink *alphalink = ctx->inputs[1];128 AVFilterLink *alphalink = ctx->inputs[1];
129 int ret;
130
83 if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {131 if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
84 av_log(ctx, AV_LOG_ERROR,132 av_log(ctx, AV_LOG_ERROR,
85 "Input frame sizes do not match (%dx%d vs %dx%d).\n",133 "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
88 return AVERROR(EINVAL);136 return AVERROR(EINVAL);
89 }137 }
90 138
139 if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
140 return ret;
141
91 outlink->w = mainlink->w;142 outlink->w = mainlink->w;
92 outlink->h = mainlink->h;143 outlink->h = mainlink->h;
93 outlink->time_base = mainlink->time_base;144 outlink->time_base = mainlink->time_base;
94 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;145 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
95 outlink->frame_rate = mainlink->frame_rate;146 outlink->frame_rate = mainlink->frame_rate;
96 return 0;147
148 return ff_framesync_configure(&s->fs);
97}149}
98 150
99static void draw_frame(AVFilterContext *ctx,151static int activate(AVFilterContext *ctx)
100 AVFrame *main_buf,
101 AVFrame *alpha_buf)
102{152{
103 AlphaMergeContext *s = ctx->priv;153 AlphaMergeContext *s = ctx->priv;
104 int h = main_buf->height;154 return ff_framesync_activate(&s->fs);
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 }
125}155}
126 156
127static int activate(AVFilterContext *ctx)157static av_cold void uninit(AVFilterContext *ctx)
128{158{
129 AVFilterLink *outlink = ctx->outputs[0];
130 AlphaMergeContext *s = ctx->priv;159 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 }
172 160
173 return FFERROR_NOT_READY;161 ff_framesync_uninit(&s->fs);
174}162}
175 163
176static const AVFilterPad alphamerge_inputs[] = {164static const AVFilterPad alphamerge_inputs[] = {
@@ -178,7 +166,6 @@ static const AVFilterPad alphamerge_inputs[] = { · 1 unmodified line
178 .name = "main",166 .name = "main",
179 .type = AVMEDIA_TYPE_VIDEO,167 .type = AVMEDIA_TYPE_VIDEO,
180 .config_props = config_input_main,168 .config_props = config_input_main,
181 .needs_writable = 1,
182 },{169 },{
183 .name = "alpha",170 .name = "alpha",
184 .type = AVMEDIA_TYPE_VIDEO,171 .type = AVMEDIA_TYPE_VIDEO,
@@ -199,17 +186,20 @@ static const AVOption alphamerge_options[] = { · 14 unmodified lines
199 { NULL }186 { NULL }
200};187};
201 188
202AVFILTER_DEFINE_CLASS(alphamerge);189FRAMESYNC_DEFINE_CLASS(alphamerge, AlphaMergeContext, fs);
203 190
204AVFilter ff_vf_alphamerge = {191AVFilter ff_vf_alphamerge = {
205 .name = "alphamerge",192 .name = "alphamerge",
206 .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "193 .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
207 "input into the alpha channel of the first input."),194 "input into the alpha channel of the first input."),
195 .preinit = alphamerge_framesync_preinit,
208 .priv_size = sizeof(AlphaMergeContext),196 .priv_size = sizeof(AlphaMergeContext),
209 .priv_class = &alphamerge_class,197 .priv_class = &alphamerge_class,
198 .init = init,
210 .query_formats = query_formats,199 .query_formats = query_formats,
211 .inputs = alphamerge_inputs,200 .inputs = alphamerge_inputs,
212 .outputs = alphamerge_outputs,201 .outputs = alphamerge_outputs,
202 .uninit = uninit,
213 .activate = activate,203 .activate = activate,
214 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,204 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
215};205};

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