]> git.sesse.net Git - vlc/blob - modules/video_filter/adjust.c
Fix a bug with preferences
[vlc] / modules / video_filter / adjust.c
1 /*****************************************************************************
2  * adjust.c : Contrast/Hue/Saturation/Brightness video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Simon Latapie <garf@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30 #include <math.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/vout.h>
34
35 #include "filter_common.h"
36
37 #ifndef M_PI
38 #   define M_PI 3.14159265358979323846
39 #endif
40
41 #define eight_times( x )    x x x x x x x x
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  Create    ( vlc_object_t * );
47 static void Destroy   ( vlc_object_t * );
48
49 static int  Init      ( vout_thread_t * );
50 static void End       ( vout_thread_t * );
51 static void Render    ( vout_thread_t *, picture_t * );
52
53 static int  SendEvents( vlc_object_t *, char const *,
54                         vlc_value_t, vlc_value_t, void * );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59
60 #define CONT_TEXT N_("Image contrast (0-2)")
61 #define CONT_LONGTEXT N_("Set the image contrast, between 0 and 2. Defaults to 1")
62 #define HUE_TEXT N_("Image hue (0-360)")
63 #define HUE_LONGTEXT N_("Set the image hue, between 0 and 360. Defaults to 0")
64 #define SAT_TEXT N_("Image saturation (0-3)")
65 #define SAT_LONGTEXT N_("Set the image saturation, between 0 and 3. Defaults to 1")
66 #define LUM_TEXT N_("Image brightness (0-2)")
67 #define LUM_LONGTEXT N_("Set the image brightness, between 0 and 2. Defaults to 1")
68 #define GAMMA_TEXT N_("Image gamma (0-10)")
69 #define GAMMA_LONGTEXT N_("Set the image gamma, between 0.01 and 10. Defaults to 1")
70
71
72 vlc_module_begin();
73     set_description( _("Image properties filter") );
74     set_shortname( N_("Image adjust" ));
75     set_category( CAT_VIDEO );
76     set_subcategory( SUBCAT_VIDEO_VFILTER );
77     set_capability( "video filter", 0 );
78
79     add_float_with_range( "contrast", 1.0, 0.0, 2.0, NULL, CONT_TEXT, CONT_LONGTEXT, VLC_FALSE );
80     add_float_with_range( "brightness", 1.0, 0.0, 2.0, NULL, LUM_TEXT, LUM_LONGTEXT, VLC_FALSE );
81     add_integer_with_range( "hue", 0, 0, 360, NULL, HUE_TEXT, HUE_LONGTEXT, VLC_FALSE );
82     add_float_with_range( "saturation", 1.0, 0.0, 3.0, NULL, SAT_TEXT, SAT_LONGTEXT, VLC_FALSE );
83     add_float_with_range( "gamma", 1.0, 0.01, 10.0, NULL, GAMMA_TEXT, GAMMA_LONGTEXT, VLC_FALSE );
84
85     add_shortcut( "adjust" );
86     set_callbacks( Create, Destroy );
87 vlc_module_end();
88
89 /*****************************************************************************
90  * vout_sys_t: adjust video output method descriptor
91  *****************************************************************************
92  * This structure is part of the video output thread descriptor.
93  * It describes the adjust specific properties of an output thread.
94  *****************************************************************************/
95 struct vout_sys_t
96 {
97     vout_thread_t *p_vout;
98 };
99
100 inline static int32_t clip( int32_t a )
101 {
102     return (a > 255) ? 255 : (a < 0) ? 0 : a;
103 }
104
105 /*****************************************************************************
106  * Control: control facility for the vout (forwards to child vout)
107  *****************************************************************************/
108 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
109 {
110     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
111 }
112
113 /*****************************************************************************
114  * Create: allocates adjust video thread output method
115  *****************************************************************************
116  * This function allocates and initializes a adjust vout method.
117  *****************************************************************************/
118 static int Create( vlc_object_t *p_this )
119 {
120     vout_thread_t *p_vout = (vout_thread_t *)p_this;
121
122     /* Allocate structure */
123     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
124     if( p_vout->p_sys == NULL )
125     {
126         msg_Err( p_vout, "out of memory" );
127         return VLC_ENOMEM;
128     }
129
130     p_vout->pf_init = Init;
131     p_vout->pf_end = End;
132     p_vout->pf_manage = NULL;
133     p_vout->pf_render = Render;
134     p_vout->pf_display = NULL;
135     p_vout->pf_control = Control;
136     
137     var_Create( p_vout, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
138     var_Create( p_vout, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
139     var_Create( p_vout, "hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
140     var_Create( p_vout, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
141     var_Create( p_vout, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
142
143     return VLC_SUCCESS;
144 }
145
146 /*****************************************************************************
147  * Init: initialize adjust video thread output method
148  *****************************************************************************/
149 static int Init( vout_thread_t *p_vout )
150 {
151     int i_index;
152     picture_t *p_pic;
153
154     I_OUTPUTPICTURES = 0;
155
156     /* Initialize the output structure */
157     p_vout->output.i_chroma = p_vout->render.i_chroma;
158     p_vout->output.i_width  = p_vout->render.i_width;
159     p_vout->output.i_height = p_vout->render.i_height;
160     p_vout->output.i_aspect = p_vout->render.i_aspect;
161
162     /* Try to open the real video output */
163     msg_Dbg( p_vout, "spawning the real video output" );
164
165     p_vout->p_sys->p_vout = vout_Create( p_vout,
166                      p_vout->render.i_width, p_vout->render.i_height,
167                      p_vout->render.i_chroma, p_vout->render.i_aspect );
168
169     /* Everything failed */
170     if( p_vout->p_sys->p_vout == NULL )
171     {
172         msg_Err( p_vout, "can't open vout, aborting" );
173
174         return VLC_EGENERIC;
175     }
176
177     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
178
179     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
180
181     ADD_PARENT_CALLBACKS( SendEventsToChild );
182
183     return VLC_SUCCESS;
184 }
185
186 /*****************************************************************************
187  * End: terminate adjust video thread output method
188  *****************************************************************************/
189 static void End( vout_thread_t *p_vout )
190 {
191     int i_index;
192
193     /* Free the fake output buffers we allocated */
194     for( i_index = I_OUTPUTPICTURES ; i_index ; )
195     {
196         i_index--;
197         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
198     }
199 }
200
201 /*****************************************************************************
202  * Destroy: destroy adjust video thread output method
203  *****************************************************************************
204  * Terminate an output method created by adjustCreateOutputMethod
205  *****************************************************************************/
206 static void Destroy( vlc_object_t *p_this )
207 {
208     vout_thread_t *p_vout = (vout_thread_t *)p_this;
209
210     if( p_vout->p_sys->p_vout )
211     {
212         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
213         vlc_object_detach( p_vout->p_sys->p_vout );
214         vout_Destroy( p_vout->p_sys->p_vout );
215     }
216
217     DEL_PARENT_CALLBACKS( SendEventsToChild );
218
219     free( p_vout->p_sys );
220 }
221
222 /*****************************************************************************
223  * Render: displays previously rendered output
224  *****************************************************************************
225  * This function send the currently rendered image to adjust modified image,
226  * waits until it is displayed and switch the two rendering buffers, preparing
227  * next frame.
228  *****************************************************************************/
229 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
230 {
231     int pi_luma[256];
232     int pi_gamma[256];
233
234     picture_t *p_outpic;
235     uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
236     uint8_t *p_out, *p_out_v;
237
238     double  f_hue;
239     double  f_gamma;
240     int32_t i_cont, i_lum;
241     int i_sat, i_sin, i_cos, i_x, i_y;
242     int i;
243     vlc_value_t val;
244
245     /* This is a new frame. Get a structure from the video_output. */
246     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
247               == NULL )
248     {
249         if( p_vout->b_die || p_vout->b_error )
250         {
251             return;
252         }
253         msleep( VOUT_OUTMEM_SLEEP );
254     }
255
256     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
257     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
258
259     /* Getvariables */
260     var_Get( p_vout, "contrast", &val );
261     i_cont = (int) ( val.f_float * 255 );
262     var_Get( p_vout, "brightness", &val );
263     i_lum = (int) (( val.f_float - 1.0 ) * 255 );
264     var_Get( p_vout, "hue", &val );
265     f_hue = (float) ( val.i_int * M_PI / 180 );
266     var_Get( p_vout, "saturation", &val );
267     i_sat = (int) (val.f_float * 256 );
268     var_Get( p_vout, "gamma", &val );
269     f_gamma = 1.0 / val.f_float;
270
271     /* Contrast is a fast but kludged function, so I put this gap to be
272      * cleaner :) */
273     i_lum += 128 - i_cont / 2;
274
275     /* Fill the gamma lookup table */
276     for( i = 0 ; i < 256 ; i++ )
277     {
278       pi_gamma[ i ] = clip( pow(i / 255.0, f_gamma) * 255.0);
279     }
280
281     /* Fill the luma lookup table */
282     for( i = 0 ; i < 256 ; i++ )
283     {
284         pi_luma[ i ] = pi_gamma[clip( i_lum + i_cont * i / 256)];
285     }
286
287     /*
288      * Do the Y plane
289      */
290
291     p_in = p_pic->p[0].p_pixels;
292     p_in_end = p_in + p_pic->p[0].i_visible_lines * p_pic->p[0].i_pitch - 8;
293
294     p_out = p_outpic->p[0].p_pixels;
295
296     for( ; p_in < p_in_end ; )
297     {
298         p_line_end = p_in + p_pic->p[0].i_visible_pitch - 8;
299
300         for( ; p_in < p_line_end ; )
301         {
302             /* Do 8 pixels at a time */
303             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
304             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
305             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
306             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
307         }
308
309         p_line_end += 8;
310
311         for( ; p_in < p_line_end ; )
312         {
313             *p_out++ = pi_luma[ *p_in++ ];
314         }
315
316         p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
317         p_out += p_outpic->p[0].i_pitch - p_outpic->p[0].i_visible_pitch;
318     }
319
320     /*
321      * Do the U and V planes
322      */
323
324     p_in = p_pic->p[1].p_pixels;
325     p_in_v = p_pic->p[2].p_pixels;
326     p_in_end = p_in + p_pic->p[1].i_visible_lines * p_pic->p[1].i_pitch - 8;
327
328     p_out = p_outpic->p[1].p_pixels;
329     p_out_v = p_outpic->p[2].p_pixels;
330
331     i_sin = sin(f_hue) * 256;
332     i_cos = cos(f_hue) * 256;
333
334     i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
335     i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
336
337     if ( i_sat > 256 )
338     {
339 #define WRITE_UV_CLIP() \
340     i_u = *p_in++ ; i_v = *p_in_v++ ; \
341     *p_out++ = clip( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
342                            * i_sat) >> 8) + 128); \
343     *p_out_v++ = clip( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
344                            * i_sat) >> 8) + 128)
345
346         uint8_t i_u, i_v;
347
348         for( ; p_in < p_in_end ; )
349         {
350             p_line_end = p_in + p_pic->p[1].i_visible_pitch - 8;
351
352             for( ; p_in < p_line_end ; )
353             {
354                 /* Do 8 pixels at a time */
355                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
356                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
357                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
358                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
359             }
360
361             p_line_end += 8;
362
363             for( ; p_in < p_line_end ; )
364             {
365                 WRITE_UV_CLIP();
366             }
367
368             p_in += p_pic->p[1].i_pitch - p_pic->p[1].i_visible_pitch;
369             p_in_v += p_pic->p[2].i_pitch - p_pic->p[2].i_visible_pitch;
370             p_out += p_outpic->p[1].i_pitch - p_outpic->p[1].i_visible_pitch;
371             p_out_v += p_outpic->p[2].i_pitch - p_outpic->p[2].i_visible_pitch;
372         }
373     }
374     else
375     {
376 #define WRITE_UV() \
377     i_u = *p_in++ ; i_v = *p_in_v++ ; \
378     *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
379                        * i_sat) >> 8) + 128; \
380     *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
381                        * i_sat) >> 8) + 128
382
383         uint8_t i_u, i_v;
384
385         for( ; p_in < p_in_end ; )
386         {
387             p_line_end = p_in + p_pic->p[1].i_visible_pitch - 8;
388
389             for( ; p_in < p_line_end ; )
390             {
391                 /* Do 8 pixels at a time */
392                 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
393                 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
394             }
395
396             p_line_end += 8;
397
398             for( ; p_in < p_line_end ; )
399             {
400                 WRITE_UV();
401             }
402
403             p_in += p_pic->p[1].i_pitch - p_pic->p[1].i_visible_pitch;
404             p_in_v += p_pic->p[2].i_pitch - p_pic->p[2].i_visible_pitch;
405             p_out += p_outpic->p[1].i_pitch - p_outpic->p[1].i_visible_pitch;
406             p_out_v += p_outpic->p[2].i_pitch - p_outpic->p[2].i_visible_pitch;
407         }
408     }
409
410     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
411
412     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
413 }
414
415 /*****************************************************************************
416  * SendEvents: forward mouse and keyboard events to the parent p_vout
417  *****************************************************************************/
418 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
419                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
420 {
421     var_Set( (vlc_object_t *)p_data, psz_var, newval );
422
423     return VLC_SUCCESS;
424 }
425
426 /*****************************************************************************
427  * SendEventsToChild: forward events to the child/children vout
428  *****************************************************************************/
429 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
430                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
431 {
432     vout_thread_t *p_vout = (vout_thread_t *)p_this;
433     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
434     return VLC_SUCCESS;
435 }