motionblur.c 7.86 KB
Newer Older
1 2 3
/*****************************************************************************
 * motion_blur.c : motion blur filter for vlc
 *****************************************************************************
4
 * Copyright (C) 2000, 2001, 2002, 2003 the VideoLAN team
5
 * $Id$
6
 *
7
 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8
 *          Antoine Cellerier <dionoea &t videolan d.t org>
9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 *
15 16 17 18 19 20 21
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * 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 37
#include <vlc_sout.h>
#include <vlc_vout.h>
#include <vlc_filter.h>
38
#include "filter_picture.h"
39 40 41 42

/*****************************************************************************
 * Local protypes
 *****************************************************************************/
43 44 45 46
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 * );
Rafaël Carré's avatar
Rafaël Carré committed
47
static void Copy         ( filter_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

vlc_module_begin();
60 61
    set_shortname( N_("Motion blur") );
    set_description( N_("Motion blur filter") );
62
    set_capability( "video filter2", 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, NULL,
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
{
83 84 85 86 87
    int        i_factor;

    uint8_t  **pp_planes;
    int        i_planes;
};
88

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

    /* Allocate structure */
97 98
    p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
    if( p_filter->p_sys == NULL )
99
    {
100
        msg_Err( p_filter, "out of memory" );
101
        return VLC_ENOMEM;
102 103
    }

104
    p_filter->pf_video_filter = Filter;
105

106 107
    config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
                       p_filter->p_cfg );
108

109
    p_filter->p_sys->i_factor =
110 111 112
        var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "factor" );
    var_AddCallback( p_filter, FILTER_PREFIX "factor",
                     MotionBlurCallback, p_filter->p_sys );
113

114 115
    p_filter->p_sys->pp_planes = NULL;
    p_filter->p_sys->i_planes = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
116

117
    return VLC_SUCCESS;
118 119 120
}

/*****************************************************************************
121
 * Destroy
122 123 124
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
125
    filter_t *p_filter = (filter_t *)p_this;
126

127 128 129 130 131
    while( p_filter->p_sys->i_planes-- )
        free( p_filter->p_sys->pp_planes[p_filter->p_sys->i_planes] );
    free( p_filter->p_sys->pp_planes );
    free( p_filter->p_sys );
}
132 133

/*****************************************************************************
134
 * Filter
135
 *****************************************************************************/
136
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
137 138
{
    picture_t * p_outpic;
139
    filter_sys_t *p_sys = p_filter->p_sys;
140

141 142 143 144
    if( !p_pic ) return NULL;

    p_outpic = p_filter->pf_vout_buffer_new( p_filter );
    if( !p_outpic )
145
    {
146
        msg_Warn( p_filter, "can't get output picture" );
147 148
        if( p_pic->pf_release )
            p_pic->pf_release( p_pic );
149
        return NULL;
150 151
    }

152
    if( !p_sys->pp_planes )
153
    {
154 155 156 157 158 159
        /* initialise our picture buffer */
        int i_plane;
        p_sys->i_planes = p_pic->i_planes;
        p_sys->pp_planes =
            (uint8_t**)malloc( p_sys->i_planes * sizeof( uint8_t * ) );
        for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
160
        {
161 162
            p_sys->pp_planes[i_plane] = (uint8_t*)malloc(
                p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
163
        }
Rafaël Carré's avatar
Rafaël Carré committed
164
        Copy( p_filter, p_pic );
165 166
    }

167 168
    /* Get a new picture */
    RenderBlur( p_sys, p_pic, p_outpic );
Rafaël Carré's avatar
Rafaël Carré committed
169
    Copy( p_filter, p_outpic );
170

171
    return CopyInfoAndRelease( p_outpic, p_pic );
172
}
173

174
/*****************************************************************************
175
 * RenderBlur: renders a blurred picture
176
 *****************************************************************************/
177 178
static void RenderBlur( filter_sys_t *p_sys, picture_t *p_newpic,
                        picture_t *p_outpic )
179 180
{
    int i_plane;
181 182
    int i_oldfactor = p_sys->i_factor;
    int i_newfactor = 128 - i_oldfactor;
183 184
    for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
    {
185
        uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
186 187 188 189
        const int i_pitch = p_outpic->p[i_plane].i_pitch;
        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;

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

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

204 205 206
            p_old += i_pitch - i_visible_pitch;
            p_new += i_pitch - i_visible_pitch;
            p_out += i_pitch - i_visible_pitch;
207 208 209
        }
    }
}
210

Rafaël Carré's avatar
Rafaël Carré committed
211
static void Copy( filter_t *p_filter, picture_t *p_pic )
Gildas Bazin's avatar
 
Gildas Bazin committed
212
{
213 214 215
    int i_plane;
    for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
    {
216
        vlc_memcpy(
217 218 219
            p_filter->p_sys->pp_planes[i_plane], p_pic->p[i_plane].p_pixels,
            p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_visible_lines );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
220
}
221 222 223 224 225

static int MotionBlurCallback( vlc_object_t *p_this, char const *psz_var,
                               vlc_value_t oldval, vlc_value_t newval,
                               void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
226
    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
227 228 229 230 231
    filter_sys_t *p_sys = (filter_sys_t *)p_data;
    if( !strcmp( psz_var, FILTER_PREFIX "factor" ) )
        p_sys->i_factor = __MIN( 127, __MAX( 1, newval.i_int ) );
    return VLC_SUCCESS;
}