motionblur.c 7.42 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * motionblur.c : motion blur filter for vlc
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2000, 2001, 2002, 2003 VLC authors and VideoLAN
5
 * $Id$
6
 *
7
 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8
 *          Antoine Cellerier <dionoea &t videolan d.t org>
9
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13
 * (at your option) any later version.
14
 *
15 16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35 36
#include <vlc_sout.h>
#include <vlc_filter.h>
37
#include <vlc_atomic.h>
38
#include <vlc_picture.h>
39
#include "filter_picture.h"
40 41 42 43

/*****************************************************************************
 * Local protypes
 *****************************************************************************/
44 45 46 47
static int  Create       ( vlc_object_t * );
static void Destroy      ( vlc_object_t * );
static picture_t *Filter ( filter_t *, picture_t * );
static void RenderBlur   ( filter_sys_t *, picture_t *, picture_t * );
48 49
static int MotionBlurCallback( vlc_object_t *, char const *,
                               vlc_value_t, vlc_value_t, void * );
50

51 52 53
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
54 55 56 57
#define FACTOR_TEXT N_("Blur factor (1-127)")
#define FACTOR_LONGTEXT N_("The degree of blurring from 1 to 127.")

#define FILTER_PREFIX "blur-"
58

59 60 61
vlc_module_begin ()
    set_shortname( N_("Motion blur") )
    set_description( N_("Motion blur filter") )
62
    set_capability( "video filter", 0 )
63 64
    set_category( CAT_VIDEO )
    set_subcategory( SUBCAT_VIDEO_VFILTER )
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
65

66
    add_integer_with_range( FILTER_PREFIX "factor", 80, 1, 127,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
67
                            FACTOR_TEXT, FACTOR_LONGTEXT, false )
68

69
    add_shortcut( "blur" )
70

71 72
    set_callbacks( Create, Destroy )
vlc_module_end ()
73

74
static const char *const ppsz_filter_options[] = {
75
    "factor", NULL
76 77
};

78
/*****************************************************************************
79
 * filter_sys_t
80
 *****************************************************************************/
81
struct filter_sys_t
82
{
Laurent Aimar's avatar
Laurent Aimar committed
83 84
    picture_t *p_tmp;
    bool      b_first;
85
    atomic_int i_factor;
86
};
87

88
/*****************************************************************************
89
 * Create
90 91
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
92
{
93
    filter_t *p_filter = (filter_t *)p_this;
94

95 96 97 98 99
    const vlc_chroma_description_t *p_chroma =
        vlc_fourcc_GetChromaDescription( p_filter->fmt_in.video.i_chroma );
    if( p_chroma == NULL || p_chroma->plane_count == 0 )
        return VLC_EGENERIC;

100
    /* Allocate structure */
101 102
    p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
    if( p_filter->p_sys == NULL )
103
        return VLC_ENOMEM;
104

Laurent Aimar's avatar
Laurent Aimar committed
105 106 107 108 109 110 111 112
    p_filter->p_sys->p_tmp = picture_NewFromFormat( &p_filter->fmt_in.video );
    if( !p_filter->p_sys->p_tmp )
    {
        free( p_filter->p_sys );
        return VLC_ENOMEM;
    }
    p_filter->p_sys->b_first = true;

113
    p_filter->pf_video_filter = Filter;
114

115 116
    config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
                       p_filter->p_cfg );
117

118 119
    atomic_init( &p_filter->p_sys->i_factor,
             var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "factor" ) );
120 121
    var_AddCallback( p_filter, FILTER_PREFIX "factor",
                     MotionBlurCallback, p_filter->p_sys );
122

123

124
    return VLC_SUCCESS;
125 126 127
}

/*****************************************************************************
128
 * Destroy
129 130 131
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
132
    filter_t *p_filter = (filter_t *)p_this;
133

134 135 136
    var_DelCallback( p_filter, FILTER_PREFIX "factor",
                     MotionBlurCallback, p_filter->p_sys );

Laurent Aimar's avatar
Laurent Aimar committed
137
    picture_Release( p_filter->p_sys->p_tmp );
138 139
    free( p_filter->p_sys );
}
140 141

/*****************************************************************************
142
 * Filter
143
 *****************************************************************************/
144
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
145 146
{
    picture_t * p_outpic;
147
    filter_sys_t *p_sys = p_filter->p_sys;
148

149 150
    if( !p_pic ) return NULL;

Laurent Aimar's avatar
Laurent Aimar committed
151
    p_outpic = filter_NewPicture( p_filter );
152
    if( !p_outpic )
153
    {
154
        picture_Release( p_pic );
155
        return NULL;
156 157
    }

Laurent Aimar's avatar
Laurent Aimar committed
158
    if( p_sys->b_first )
159
    {
Laurent Aimar's avatar
Laurent Aimar committed
160 161
        picture_CopyPixels( p_sys->p_tmp, p_pic );
        p_sys->b_first = false;
162 163
    }

164 165
    /* Get a new picture */
    RenderBlur( p_sys, p_pic, p_outpic );
Laurent Aimar's avatar
Laurent Aimar committed
166 167

    picture_CopyPixels( p_sys->p_tmp, p_outpic );
168

169
    return CopyInfoAndRelease( p_outpic, p_pic );
170
}
171

172
/*****************************************************************************
173
 * RenderBlur: renders a blurred picture
174
 *****************************************************************************/
175 176
static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
                        picture_t *p_outpic )
177
{
178
    const int i_oldfactor = atomic_load( &p_sys->i_factor );
179
    int i_newfactor = 128 - i_oldfactor;
180

181
    for( int i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
182
    {
183
        uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
184 185 186
        const int i_visible_pitch = p_outpic->p[i_plane].i_visible_pitch;
        const int i_visible_lines = p_outpic->p[i_plane].i_visible_lines;

187 188
        p_out = p_outpic->p[i_plane].p_pixels;
        p_new = p_newpic->p[i_plane].p_pixels;
Laurent Aimar's avatar
Laurent Aimar committed
189 190
        p_old = p_sys->p_tmp->p[i_plane].p_pixels;
        p_out_end = p_out + p_outpic->p[i_plane].i_pitch * i_visible_lines;
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
191
        while ( p_out < p_out_end )
192
        {
193
            p_out_line_end = p_out + i_visible_pitch;
194 195 196 197 198 199 200

            while ( p_out < p_out_line_end )
            {
                *p_out++ = (((*p_old++) * i_oldfactor) +
                            ((*p_new++) * i_newfactor)) >> 7;
            }

Laurent Aimar's avatar
Laurent Aimar committed
201 202 203
            p_old += p_sys->p_tmp->p[i_plane].i_pitch - i_visible_pitch;
            p_new += p_newpic->p[i_plane].i_pitch     - i_visible_pitch;
            p_out += p_outpic->p[i_plane].i_pitch     - i_visible_pitch;
204 205 206
        }
    }
}
207

208 209 210 211
static int MotionBlurCallback( vlc_object_t *p_this, char const *psz_var,
                               vlc_value_t oldval, vlc_value_t newval,
                               void *p_data )
{
212
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
213
    filter_sys_t *p_sys = (filter_sys_t *)p_data;
214

215
    atomic_store( &p_sys->i_factor, VLC_CLIP( newval.i_int, 1, 127 ) );
216 217
    return VLC_SUCCESS;
}