]> git.sesse.net Git - vlc/blob - modules/video_filter/sepia.c
9382304210c78dc6a05af029afe81566196dcad1
[vlc] / modules / video_filter / sepia.c
1 /*****************************************************************************
2  * sepia.c : Sepia video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Branko Kokanovic <branko.kokanovic@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35
36 #include <assert.h>
37 #include "filter_picture.h"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Create      ( vlc_object_t * );
43 static void Destroy     ( vlc_object_t * );
44
45 static void RVSepia( picture_t *, picture_t *, int );
46 static void PlanarI420Sepia( picture_t *, picture_t *, int);
47 static void PackedYUVSepia( picture_t *, picture_t *, int);
48 static picture_t *Filter( filter_t *, picture_t * );
49
50 static const char *const ppsz_filter_options[] = {
51     "intensity", NULL
52 };
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 #define SEPIA_INTENSITY_TEXT N_("Sepia intensity")
58 #define SEPIA_INTENSITY_LONGTEXT N_("Intensity of sepia effect" )
59
60 #define CFG_PREFIX "sepia-"
61
62 vlc_module_begin ()
63     set_description( N_("Sepia video filter") )
64     set_shortname( N_("Sepia" ) )
65     set_help( N_("Gives video a warmer tone by applying sepia effect") )
66     set_category( CAT_VIDEO )
67     set_subcategory( SUBCAT_VIDEO_VFILTER )
68     set_capability( "video filter2", 0 )
69     add_integer_with_range( CFG_PREFIX "intensity", 100, 0, 255, NULL,
70                            SEPIA_INTENSITY_TEXT, SEPIA_INTENSITY_LONGTEXT,
71                            false )
72     set_callbacks( Create, Destroy )
73 vlc_module_end ()
74
75 /*****************************************************************************
76  * callback prototypes
77  *****************************************************************************/
78 static int FilterCallback( vlc_object_t *, char const *,
79                            vlc_value_t, vlc_value_t, void * );
80
81 typedef void (*SepiaFunction)( picture_t *, picture_t *, int );
82
83 static const struct
84 {
85     vlc_fourcc_t i_chroma;
86     SepiaFunction pf_sepia;
87 } p_sepia_cfg[] = {
88     { VLC_CODEC_I420, PlanarI420Sepia },
89     { VLC_CODEC_RGB24, RVSepia },
90     { VLC_CODEC_RGB32, RVSepia },
91     { VLC_CODEC_UYVY, PackedYUVSepia },
92     { VLC_CODEC_VYUY, PackedYUVSepia },
93     { VLC_CODEC_YUYV, PackedYUVSepia },
94     { VLC_CODEC_YVYU, PackedYUVSepia },
95     { 0, NULL }
96 };
97
98 /*****************************************************************************
99  * filter_sys_t: adjust filter method descriptor
100  *****************************************************************************/
101 struct filter_sys_t
102 {
103     SepiaFunction pf_sepia;
104     int i_intensity;
105     vlc_spinlock_t lock;
106 };
107
108 /*****************************************************************************
109  * Create: allocates Sepia video thread output method
110  *****************************************************************************
111  * This function allocates and initializes a Sepia vout method.
112  *****************************************************************************/
113 static int Create( vlc_object_t *p_this )
114 {
115     filter_t *p_filter = (filter_t *)p_this;
116     filter_sys_t *p_sys;
117
118     /* Allocate structure */
119     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
120     if( p_filter->p_sys == NULL )
121         return VLC_ENOMEM;
122
123     p_sys->pf_sepia = NULL;
124
125     for( int i = 0; p_sepia_cfg[i].i_chroma != 0; i++ )
126     {
127         if( p_sepia_cfg[i].i_chroma != p_filter->fmt_in.video.i_chroma )
128             continue;
129         p_sys->pf_sepia = p_sepia_cfg[i].pf_sepia;
130     }
131
132     if( p_sys->pf_sepia == NULL )
133     {
134         msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
135                 (char*)&(p_filter->fmt_in.video.i_chroma) );
136         free( p_sys );
137         return VLC_EGENERIC;
138     }
139
140     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
141                        p_filter->p_cfg );
142     p_sys->i_intensity= var_CreateGetIntegerCommand( p_filter,
143                        CFG_PREFIX "intensity" );
144
145     vlc_spin_init( &p_sys->lock );
146
147     var_AddCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
148
149     p_filter->pf_video_filter = Filter;
150
151     return VLC_SUCCESS;
152 }
153
154 /*****************************************************************************
155  * Destroy: destroy sepia video thread output method
156  *****************************************************************************
157  * Terminate an output method
158  *****************************************************************************/
159 static void Destroy( vlc_object_t *p_this )
160 {
161     filter_t *p_filter = (filter_t *)p_this;
162
163     var_DelCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
164
165     vlc_spin_destroy( &p_filter->p_sys->lock );
166     free( p_filter->p_sys );
167 }
168
169 /*****************************************************************************
170  * Render: displays previously rendered output
171  *****************************************************************************
172  * This function send the currently rendered image to sepia image, waits
173  * until it is displayed and switch the two rendering buffers, preparing next
174  * frame.
175  *****************************************************************************/
176 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
177 {
178     picture_t *p_outpic;
179     int intensity;
180
181     if( !p_pic ) return NULL;
182
183     filter_sys_t *p_sys = p_filter->p_sys;
184     vlc_spin_lock( &p_sys->lock );
185     intensity = p_sys->i_intensity;
186     vlc_spin_unlock( &p_sys->lock );
187
188     p_outpic = filter_NewPicture( p_filter );
189     if( !p_outpic )
190     {
191         msg_Warn( p_filter, "can't get output picture" );
192         picture_Release( p_pic );
193         return NULL;
194     }
195
196     p_sys->pf_sepia( p_pic, p_outpic, intensity );
197
198     return CopyInfoAndRelease( p_outpic, p_pic );
199 }
200
201 /*****************************************************************************
202  * PlanarI420Sepia: Applies sepia to one frame of the planar I420 video
203  *****************************************************************************
204  * This function applies sepia effect to one frame of the video by iterating
205  * through video lines. We iterate for every two lines and for every two pixels
206  * in line to calculate new sepia values for four y components as well for u
207  * and v components.
208  *****************************************************************************/
209 static void PlanarI420Sepia( picture_t *p_pic, picture_t *p_outpic,
210                                int i_intensity )
211 {
212     // prepared values to copy for U and V channels
213     const uint8_t filling_const_8u = 128 - i_intensity / 6;
214     const uint8_t filling_const_8v = 128 + i_intensity / 14;
215     /* iterate for every two visible line in the frame */
216     for( int y = 0; y < p_pic->p[Y_PLANE].i_visible_lines - 1; y += 2)
217     {
218         const int i_dy_line1_start = y * p_outpic->p[Y_PLANE].i_pitch;
219         const int i_dy_line2_start = ( y + 1 ) * p_outpic->p[Y_PLANE].i_pitch;
220         const int i_du_line_start = (y/2) * p_outpic->p[U_PLANE].i_pitch;
221         const int i_dv_line_start = (y/2) * p_outpic->p[V_PLANE].i_pitch;
222         // to prevent sigsegv if one pic is smaller (theoretically)
223         int i_picture_size_limit = p_pic->p[Y_PLANE].i_visible_pitch 
224                   < p_outpic->p[Y_PLANE].i_visible_pitch 
225                   ? (p_pic->p[Y_PLANE].i_visible_pitch - 1) :
226                   (p_outpic->p[Y_PLANE].i_visible_pitch - 1);
227         /* iterate for every two visible line in the frame */
228         for( int x = 0; x < i_picture_size_limit; x += 2)
229         {
230             // y = y - y/4 {to prevent overflow} + intensity / 4
231             p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] =
232                 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] -
233                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] >> 2) +
234                 (i_intensity >> 2);
235             p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] =
236                 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] -
237                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] >> 2) +
238                 (i_intensity >> 2);
239             p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] =
240                 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] -
241                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] >> 2) +
242                 (i_intensity >> 2);
243             p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] =
244                 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] -
245                 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] >> 2) +
246                 (i_intensity >> 2);
247             // u = 128 {half => B&W} - intensity / 6
248             p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
249                 filling_const_8u;
250             // v = 128 {half => B&W} + intensity / 14
251             p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
252                 filling_const_8v;
253         }
254     }
255 }
256
257 /*****************************************************************************
258  * PackedYUVSepia: Applies sepia to one frame of the packed YUV video
259  *****************************************************************************
260  * This function applies sepia effext to one frame of the video by iterating
261  * through video lines. In every pass, we calculate new values for pixels
262  * (UYVY, VYUY, YUYV and YVYU formats are supported)
263  *****************************************************************************/
264 static void PackedYUVSepia( picture_t *p_pic, picture_t *p_outpic,
265                            int i_intensity )
266 {
267     uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
268     int i_yindex = 1, i_uindex = 2, i_vindex = 0;
269
270     GetPackedYuvOffsets( p_outpic->format.i_chroma,
271                         &i_yindex, &i_uindex, &i_vindex );
272
273     // prepared values to copy for U and V channels
274     const uint8_t filling_const_8u = 128 - i_intensity / 6;
275     const uint8_t filling_const_8v = 128 + i_intensity / 14;
276
277     p_in = p_pic->p[0].p_pixels;
278     p_in_end = p_in + p_pic->p[0].i_visible_lines
279         * p_pic->p[0].i_pitch;
280     p_out = p_outpic->p[0].p_pixels;
281
282     while( p_in < p_in_end )
283     {
284         p_line_end = p_in + p_pic->p[0].i_visible_pitch;
285         while( p_in < p_line_end )
286         {
287             /* calculate new, sepia values */
288             p_out[i_yindex] =
289                 p_in[i_yindex] - (p_in[i_yindex] >> 2) + (i_intensity >> 2);
290             p_out[i_yindex + 2] =
291                 p_in[i_yindex + 2] - (p_in[i_yindex + 2] >> 2) 
292                 + (i_intensity >> 2);
293             p_out[i_uindex] = filling_const_8u;
294             p_out[i_vindex] = filling_const_8v;
295             p_in += 4;
296             p_out += 4;
297         }
298         p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
299         p_out += p_outpic->p[0].i_pitch
300             - p_outpic->p[0].i_visible_pitch;
301     }
302 }
303
304 /*****************************************************************************
305  * RVSepia: Applies sepia to one frame of the RV24/RV32 video
306  *****************************************************************************
307  * This function applies sepia effect to one frame of the video by iterating
308  * through video lines and calculating new values for every byte in chunks of
309  * 3 (RV24) or 4 (RV32) bytes.
310  *****************************************************************************/
311 static void RVSepia( picture_t *p_pic, picture_t *p_outpic, int i_intensity )
312 {
313 #define SCALEBITS 10
314 #define ONE_HALF  (1 << (SCALEBITS - 1))
315 #define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
316     uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
317     int i_r, i_g, i_b;
318     bool b_isRV32 = p_pic->format.i_chroma == VLC_CODEC_RGB32;
319     int i_rindex = 0, i_gindex = 1, i_bindex = 2;
320
321     GetPackedRgbIndexes( &p_outpic->format, &i_rindex, &i_gindex, &i_bindex );
322
323     p_in = p_pic->p[0].p_pixels;
324     p_in_end = p_in + p_pic->p[0].i_visible_lines
325         * p_pic->p[0].i_pitch;
326     p_out = p_outpic->p[0].p_pixels;
327
328     /* Precompute values constant for this certain i_intensity, using the same
329      * formula as YUV functions above */
330     uint8_t r_intensity = (( FIX( 1.40200 * 255.0 / 224.0 ) * (i_intensity * 14)
331                         + ONE_HALF )) >> SCALEBITS;
332     uint8_t g_intensity = (( - FIX(0.34414*255.0/224.0) * ( - i_intensity / 6 )
333                         - FIX( 0.71414 * 255.0 / 224.0) * ( i_intensity * 14 )
334                         + ONE_HALF )) >> SCALEBITS;
335     uint8_t b_intensity = (( FIX( 1.77200 * 255.0 / 224.0) * ( - i_intensity / 6 )
336                         + ONE_HALF )) >> SCALEBITS;
337
338     while (p_in < p_in_end)
339     {
340         p_line_end = p_in + p_pic->p[0].i_visible_pitch;
341         while (p_in < p_line_end)
342         {
343             /* do sepia: this calculation is based on the formula to calculate
344              * YUV->RGB and RGB->YUV (in filter_picture.h) mode and that
345              * y = y - y/4 + intensity/4 . As Y is the only channel that changes
346              * through the whole image. After that, precomputed values are added
347              * for each RGB channel and saved in the output image.
348              * FIXME: needs cleanup */
349             uint8_t i_y = ((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] +  25
350                       * p_in[i_bindex] + 128 ) >> 8 ) * FIX(255.0/219.0))
351                       - (((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] + 25
352                       * p_in[i_bindex] + 128 ) >> 8 )
353                       * FIX( 255.0 / 219.0 )) >> 2 ) + ( i_intensity >> 2 );
354             p_out[i_rindex] = vlc_uint8(i_y + r_intensity);
355             p_out[i_gindex] = vlc_uint8(i_y + g_intensity);
356             p_out[i_bindex] = vlc_uint8(i_y + b_intensity);
357             p_in += 3;
358             p_out += 3;
359             /* for rv32 we take 4 chunks at the time */
360             if (b_isRV32) {
361             /* alpha channel stays the same */
362             *p_out++ = *p_in++;
363             }
364         }
365
366         p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
367         p_out += p_outpic->p[0].i_pitch
368             - p_outpic->p[0].i_visible_pitch;
369     }
370 #undef SCALEBITS
371 #undef ONE_HALF
372 #undef FIX
373 }
374
375 static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
376                             vlc_value_t oldval, vlc_value_t newval,
377                             void *p_data )
378 {
379     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
380     filter_t *p_filter = (filter_t*)p_this;
381     filter_sys_t *p_sys = p_filter->p_sys;
382
383     vlc_spin_lock( &p_sys->lock );
384     p_sys->i_intensity = newval.i_int;
385     vlc_spin_unlock( &p_sys->lock );
386
387     return VLC_SUCCESS;
388 }