LCOV - code coverage report
Current view: top level - libavfilter - vf_alphamerge.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 71 83 85.5 %
Date: 2017-12-17 11:58:42 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012 Steven Robertson
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : /**
      22             :  * @file
      23             :  * copy an alpha component from another video's luma
      24             :  */
      25             : 
      26             : #include <string.h>
      27             : 
      28             : #include "libavutil/imgutils.h"
      29             : #include "libavutil/pixfmt.h"
      30             : #include "avfilter.h"
      31             : #include "bufferqueue.h"
      32             : #include "drawutils.h"
      33             : #include "formats.h"
      34             : #include "internal.h"
      35             : #include "video.h"
      36             : 
      37             : enum { Y, U, V, A };
      38             : 
      39             : typedef struct AlphaMergeContext {
      40             :     int is_packed_rgb;
      41             :     uint8_t rgba_map[4];
      42             :     struct FFBufQueue queue_main;
      43             :     struct FFBufQueue queue_alpha;
      44             : } AlphaMergeContext;
      45             : 
      46           8 : static av_cold void uninit(AVFilterContext *ctx)
      47             : {
      48           8 :     AlphaMergeContext *merge = ctx->priv;
      49           8 :     ff_bufqueue_discard_all(&merge->queue_main);
      50           8 :     ff_bufqueue_discard_all(&merge->queue_alpha);
      51           8 : }
      52             : 
      53           4 : static int query_formats(AVFilterContext *ctx)
      54             : {
      55             :     static const enum AVPixelFormat main_fmts[] = {
      56             :         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
      57             :         AV_PIX_FMT_GBRAP,
      58             :         AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
      59             :         AV_PIX_FMT_NONE
      60             :     };
      61             :     static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
      62           4 :     AVFilterFormats *main_formats = NULL, *alpha_formats = NULL;
      63             :     int ret;
      64             : 
      65           8 :     if (!(main_formats = ff_make_format_list(main_fmts)) ||
      66           4 :         !(alpha_formats = ff_make_format_list(alpha_fmts))) {
      67           0 :             ret = AVERROR(ENOMEM);
      68           0 :             goto fail;
      69             :         }
      70           8 :     if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
      71           8 :         (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
      72           4 :         (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
      73             :             goto fail;
      74           4 :     return 0;
      75           0 : fail:
      76           0 :     if (main_formats)
      77           0 :         av_freep(&main_formats->formats);
      78           0 :     av_freep(&main_formats);
      79           0 :     if (alpha_formats)
      80           0 :         av_freep(&alpha_formats->formats);
      81           0 :     av_freep(&alpha_formats);
      82           0 :     return ret;
      83             : }
      84             : 
      85           4 : static int config_input_main(AVFilterLink *inlink)
      86             : {
      87           4 :     AlphaMergeContext *merge = inlink->dst->priv;
      88           4 :     merge->is_packed_rgb =
      89           6 :         ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0 &&
      90           2 :         inlink->format != AV_PIX_FMT_GBRAP;
      91           4 :     return 0;
      92             : }
      93             : 
      94           4 : static int config_output(AVFilterLink *outlink)
      95             : {
      96           4 :     AVFilterContext *ctx = outlink->src;
      97           4 :     AVFilterLink *mainlink = ctx->inputs[0];
      98           4 :     AVFilterLink *alphalink = ctx->inputs[1];
      99           4 :     if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
     100           0 :         av_log(ctx, AV_LOG_ERROR,
     101             :                "Input frame sizes do not match (%dx%d vs %dx%d).\n",
     102             :                mainlink->w, mainlink->h,
     103             :                alphalink->w, alphalink->h);
     104           0 :         return AVERROR(EINVAL);
     105             :     }
     106             : 
     107           4 :     outlink->w = mainlink->w;
     108           4 :     outlink->h = mainlink->h;
     109           4 :     outlink->time_base = mainlink->time_base;
     110           4 :     outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
     111           4 :     outlink->frame_rate = mainlink->frame_rate;
     112           4 :     return 0;
     113             : }
     114             : 
     115         200 : static void draw_frame(AVFilterContext *ctx,
     116             :                        AVFrame *main_buf,
     117             :                        AVFrame *alpha_buf)
     118             : {
     119         200 :     AlphaMergeContext *merge = ctx->priv;
     120         200 :     int h = main_buf->height;
     121             : 
     122         200 :     if (merge->is_packed_rgb) {
     123             :         int x, y;
     124             :         uint8_t *pin, *pout;
     125       28900 :         for (y = 0; y < h; y++) {
     126       28800 :             pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
     127       28800 :             pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
     128    10166400 :             for (x = 0; x < main_buf->width; x++) {
     129    10137600 :                 *pout = *pin;
     130    10137600 :                 pin += 1;
     131    10137600 :                 pout += 4;
     132             :             }
     133             :         }
     134             :     } else {
     135         100 :         const int main_linesize = main_buf->linesize[A];
     136         100 :         const int alpha_linesize = alpha_buf->linesize[Y];
     137         200 :         av_image_copy_plane(main_buf->data[A], main_linesize,
     138         100 :                             alpha_buf->data[Y], alpha_linesize,
     139             :                             FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
     140             :     }
     141         200 : }
     142             : 
     143         400 : static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
     144             : {
     145         400 :     AVFilterContext *ctx = inlink->dst;
     146         400 :     AlphaMergeContext *merge = ctx->priv;
     147             : 
     148         400 :     int ret = 0;
     149         400 :     int is_alpha = (inlink == ctx->inputs[1]);
     150         400 :     struct FFBufQueue *queue =
     151         400 :         (is_alpha ? &merge->queue_alpha : &merge->queue_main);
     152         400 :     ff_bufqueue_add(ctx, queue, buf);
     153             : 
     154             :     do {
     155             :         AVFrame *main_buf, *alpha_buf;
     156             : 
     157        1000 :         if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
     158         400 :             !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
     159             : 
     160         200 :         main_buf = ff_bufqueue_get(&merge->queue_main);
     161         200 :         alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
     162             : 
     163         200 :         draw_frame(ctx, main_buf, alpha_buf);
     164         200 :         ret = ff_filter_frame(ctx->outputs[0], main_buf);
     165         200 :         av_frame_free(&alpha_buf);
     166         200 :     } while (ret >= 0);
     167         400 :     return ret;
     168             : }
     169             : 
     170         302 : static int request_frame(AVFilterLink *outlink)
     171             : {
     172         302 :     AVFilterContext *ctx = outlink->src;
     173         302 :     AlphaMergeContext *merge = ctx->priv;
     174             :     int in, ret;
     175             : 
     176         302 :     in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
     177         302 :     ret = ff_request_frame(ctx->inputs[in]);
     178         302 :     if (ret < 0)
     179           4 :         return ret;
     180         298 :     return 0;
     181             : }
     182             : 
     183             : static const AVFilterPad alphamerge_inputs[] = {
     184             :     {
     185             :         .name             = "main",
     186             :         .type             = AVMEDIA_TYPE_VIDEO,
     187             :         .config_props     = config_input_main,
     188             :         .filter_frame     = filter_frame,
     189             :         .needs_writable   = 1,
     190             :     },{
     191             :         .name             = "alpha",
     192             :         .type             = AVMEDIA_TYPE_VIDEO,
     193             :         .filter_frame     = filter_frame,
     194             :     },
     195             :     { NULL }
     196             : };
     197             : 
     198             : static const AVFilterPad alphamerge_outputs[] = {
     199             :     {
     200             :         .name          = "default",
     201             :         .type          = AVMEDIA_TYPE_VIDEO,
     202             :         .config_props  = config_output,
     203             :         .request_frame = request_frame,
     204             :     },
     205             :     { NULL }
     206             : };
     207             : 
     208             : AVFilter ff_vf_alphamerge = {
     209             :     .name           = "alphamerge",
     210             :     .description    = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
     211             :                       "input into the alpha channel of the first input."),
     212             :     .uninit         = uninit,
     213             :     .priv_size      = sizeof(AlphaMergeContext),
     214             :     .query_formats  = query_formats,
     215             :     .inputs         = alphamerge_inputs,
     216             :     .outputs        = alphamerge_outputs,
     217             : };

Generated by: LCOV version 1.13