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 PixelFormat main_fmts[] = {
00056         PIX_FMT_YUVA444P, PIX_FMT_YUVA422P, PIX_FMT_YUVA420P,
00057         PIX_FMT_RGBA, PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR,
00058         PIX_FMT_NONE
00059     };
00060     enum PixelFormat alpha_fmts[] = { PIX_FMT_GRAY8, 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 int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref) {return 0;}
00099 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) {return 0;}
00100 
00101 static void draw_frame(AVFilterContext *ctx,
00102                        AVFilterBufferRef *main_buf,
00103                        AVFilterBufferRef *alpha_buf)
00104 {
00105     AlphaMergeContext *merge = ctx->priv;
00106     int h = main_buf->video->h;
00107 
00108     if (merge->is_packed_rgb) {
00109         int x, y;
00110         uint8_t *pin, *pout;
00111         for (y = 0; y < h; y++) {
00112             pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
00113             pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
00114             for (x = 0; x < main_buf->video->w; x++) {
00115                 *pout = *pin;
00116                 pin += 1;
00117                 pout += 4;
00118             }
00119         }
00120     } else {
00121         int y;
00122         const int main_linesize = main_buf->linesize[A];
00123         const int alpha_linesize = alpha_buf->linesize[Y];
00124         for (y = 0; y < h && y < alpha_buf->video->h; y++) {
00125             memcpy(main_buf->data[A] + y * main_linesize,
00126                    alpha_buf->data[Y] + y * alpha_linesize,
00127                    FFMIN(main_linesize, alpha_linesize));
00128         }
00129     }
00130     ff_draw_slice(ctx->outputs[0], 0, h, 1);
00131 }
00132 
00133 static int end_frame(AVFilterLink *inlink)
00134 {
00135     AVFilterContext *ctx = inlink->dst;
00136     AlphaMergeContext *merge = ctx->priv;
00137 
00138     int is_alpha = (inlink == ctx->inputs[1]);
00139     struct FFBufQueue *queue =
00140         (is_alpha ? &merge->queue_alpha : &merge->queue_main);
00141     ff_bufqueue_add(ctx, queue, inlink->cur_buf);
00142     inlink->cur_buf = NULL;
00143 
00144     while (1) {
00145         AVFilterBufferRef *main_buf, *alpha_buf;
00146 
00147         if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
00148             !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
00149 
00150         main_buf = ff_bufqueue_get(&merge->queue_main);
00151         alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
00152 
00153         ctx->outputs[0]->out_buf = main_buf;
00154         ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(main_buf, ~0));
00155         merge->frame_requested = 0;
00156         draw_frame(ctx, main_buf, alpha_buf);
00157         ff_end_frame(ctx->outputs[0]);
00158         avfilter_unref_buffer(alpha_buf);
00159     }
00160     return 0;
00161 }
00162 
00163 static int request_frame(AVFilterLink *outlink)
00164 {
00165     AVFilterContext *ctx = outlink->src;
00166     AlphaMergeContext *merge = ctx->priv;
00167     int in, ret;
00168 
00169     merge->frame_requested = 1;
00170     while (merge->frame_requested) {
00171         in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
00172         ret = ff_request_frame(ctx->inputs[in]);
00173         if (ret < 0)
00174             return ret;
00175     }
00176     return 0;
00177 }
00178 
00179 AVFilter avfilter_vf_alphamerge = {
00180     .name           = "alphamerge",
00181     .description    = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
00182                       "input into the alpha channel of the first input."),
00183     .uninit         = uninit,
00184     .priv_size      = sizeof(AlphaMergeContext),
00185     .query_formats  = query_formats,
00186 
00187     .inputs    = (const AVFilterPad[]) {
00188         { .name             = "main",
00189           .type             = AVMEDIA_TYPE_VIDEO,
00190           .config_props     = config_input_main,
00191           .get_video_buffer = ff_null_get_video_buffer,
00192           .start_frame      = start_frame,
00193           .draw_slice       = draw_slice,
00194           .end_frame        = end_frame,
00195           .min_perms        = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE },
00196         { .name             = "alpha",
00197           .type             = AVMEDIA_TYPE_VIDEO,
00198           .start_frame      = start_frame,
00199           .draw_slice       = draw_slice,
00200           .end_frame        = end_frame,
00201           .min_perms        = AV_PERM_READ | AV_PERM_PRESERVE },
00202         { .name = NULL }
00203     },
00204     .outputs   = (const AVFilterPad[]) {
00205       { .name               = "default",
00206         .type               = AVMEDIA_TYPE_VIDEO,
00207         .config_props       = config_output,
00208         .request_frame      = request_frame },
00209       { .name = NULL }
00210     },
00211 };