00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include <string.h>
00027
00028 #include "libavutil/pixfmt.h"
00029 #include "avfilter.h"
00030 #include "bufferqueue.h"
00031 #include "drawutils.h"
00032 #include "formats.h"
00033 #include "internal.h"
00034 #include "video.h"
00035
00036 enum { Y, U, V, A };
00037
00038 typedef struct {
00039 int frame_requested;
00040 int is_packed_rgb;
00041 uint8_t rgba_map[4];
00042 struct FFBufQueue queue_main;
00043 struct FFBufQueue queue_alpha;
00044 } AlphaMergeContext;
00045
00046 static av_cold void uninit(AVFilterContext *ctx)
00047 {
00048 AlphaMergeContext *merge = ctx->priv;
00049 ff_bufqueue_discard_all(&merge->queue_main);
00050 ff_bufqueue_discard_all(&merge->queue_alpha);
00051 }
00052
00053 static int query_formats(AVFilterContext *ctx)
00054 {
00055 enum AVPixelFormat main_fmts[] = {
00056 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
00057 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
00058 AV_PIX_FMT_NONE
00059 };
00060 enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
00061 AVFilterFormats *main_formats = ff_make_format_list(main_fmts);
00062 AVFilterFormats *alpha_formats = ff_make_format_list(alpha_fmts);
00063 ff_formats_ref(main_formats, &ctx->inputs[0]->out_formats);
00064 ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats);
00065 ff_formats_ref(main_formats, &ctx->outputs[0]->in_formats);
00066 return 0;
00067 }
00068
00069 static int config_input_main(AVFilterLink *inlink)
00070 {
00071 AlphaMergeContext *merge = inlink->dst->priv;
00072 merge->is_packed_rgb =
00073 ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0;
00074 return 0;
00075 }
00076
00077 static int config_output(AVFilterLink *outlink)
00078 {
00079 AVFilterContext *ctx = outlink->src;
00080 AVFilterLink *mainlink = ctx->inputs[0];
00081 AVFilterLink *alphalink = ctx->inputs[1];
00082 if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
00083 av_log(ctx, AV_LOG_ERROR,
00084 "Input frame sizes do not match (%dx%d vs %dx%d).\n",
00085 mainlink->w, mainlink->h,
00086 alphalink->w, alphalink->h);
00087 return AVERROR(EINVAL);
00088 }
00089
00090 outlink->w = mainlink->w;
00091 outlink->h = mainlink->h;
00092 outlink->time_base = mainlink->time_base;
00093 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
00094 outlink->frame_rate = mainlink->frame_rate;
00095 return 0;
00096 }
00097
00098 static void draw_frame(AVFilterContext *ctx,
00099 AVFilterBufferRef *main_buf,
00100 AVFilterBufferRef *alpha_buf)
00101 {
00102 AlphaMergeContext *merge = ctx->priv;
00103 int h = main_buf->video->h;
00104
00105 if (merge->is_packed_rgb) {
00106 int x, y;
00107 uint8_t *pin, *pout;
00108 for (y = 0; y < h; y++) {
00109 pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
00110 pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
00111 for (x = 0; x < main_buf->video->w; x++) {
00112 *pout = *pin;
00113 pin += 1;
00114 pout += 4;
00115 }
00116 }
00117 } else {
00118 int y;
00119 const int main_linesize = main_buf->linesize[A];
00120 const int alpha_linesize = alpha_buf->linesize[Y];
00121 for (y = 0; y < h && y < alpha_buf->video->h; y++) {
00122 memcpy(main_buf->data[A] + y * main_linesize,
00123 alpha_buf->data[Y] + y * alpha_linesize,
00124 FFMIN(main_linesize, alpha_linesize));
00125 }
00126 }
00127 }
00128
00129 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
00130 {
00131 AVFilterContext *ctx = inlink->dst;
00132 AlphaMergeContext *merge = ctx->priv;
00133
00134 int is_alpha = (inlink == ctx->inputs[1]);
00135 struct FFBufQueue *queue =
00136 (is_alpha ? &merge->queue_alpha : &merge->queue_main);
00137 ff_bufqueue_add(ctx, queue, buf);
00138
00139 while (1) {
00140 AVFilterBufferRef *main_buf, *alpha_buf;
00141
00142 if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
00143 !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
00144
00145 main_buf = ff_bufqueue_get(&merge->queue_main);
00146 alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
00147
00148 merge->frame_requested = 0;
00149 draw_frame(ctx, main_buf, alpha_buf);
00150 ff_filter_frame(ctx->outputs[0], main_buf);
00151 avfilter_unref_buffer(alpha_buf);
00152 }
00153 return 0;
00154 }
00155
00156 static int request_frame(AVFilterLink *outlink)
00157 {
00158 AVFilterContext *ctx = outlink->src;
00159 AlphaMergeContext *merge = ctx->priv;
00160 int in, ret;
00161
00162 merge->frame_requested = 1;
00163 while (merge->frame_requested) {
00164 in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
00165 ret = ff_request_frame(ctx->inputs[in]);
00166 if (ret < 0)
00167 return ret;
00168 }
00169 return 0;
00170 }
00171
00172 static const AVFilterPad alphamerge_inputs[] = {
00173 {
00174 .name = "main",
00175 .type = AVMEDIA_TYPE_VIDEO,
00176 .config_props = config_input_main,
00177 .get_video_buffer = ff_null_get_video_buffer,
00178 .filter_frame = filter_frame,
00179 .min_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE,
00180 },{
00181 .name = "alpha",
00182 .type = AVMEDIA_TYPE_VIDEO,
00183 .filter_frame = filter_frame,
00184 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
00185 },
00186 { NULL }
00187 };
00188
00189 static const AVFilterPad alphamerge_outputs[] = {
00190 {
00191 .name = "default",
00192 .type = AVMEDIA_TYPE_VIDEO,
00193 .config_props = config_output,
00194 .request_frame = request_frame,
00195 },
00196 { NULL }
00197 };
00198
00199 AVFilter avfilter_vf_alphamerge = {
00200 .name = "alphamerge",
00201 .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
00202 "input into the alpha channel of the first input."),
00203 .uninit = uninit,
00204 .priv_size = sizeof(AlphaMergeContext),
00205 .query_formats = query_formats,
00206 .inputs = alphamerge_inputs,
00207 .outputs = alphamerge_outputs,
00208 };