]> git.sesse.net Git - vlc/blob - modules/video_filter/adjust.c
We need to include "config.h" somehow before we can use the HAVE_* defines
[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 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 THRES_TEXT N_("Brightness threshold")
61 #define THRES_LONGTEXT N_("When this mode is enabled, pixels will be " \
62         "shown as black or white. The threshold value will be the brighness " \
63         "defined below." )
64 #define CONT_TEXT N_("Image contrast (0-2)")
65 #define CONT_LONGTEXT N_("Set the image contrast, between 0 and 2. Defaults to 1.")
66 #define HUE_TEXT N_("Image hue (0-360)")
67 #define HUE_LONGTEXT N_("Set the image hue, between 0 and 360. Defaults to 0.")
68 #define SAT_TEXT N_("Image saturation (0-3)")
69 #define SAT_LONGTEXT N_("Set the image saturation, between 0 and 3. Defaults to 1.")
70 #define LUM_TEXT N_("Image brightness (0-2)")
71 #define LUM_LONGTEXT N_("Set the image brightness, between 0 and 2. Defaults to 1.")
72 #define GAMMA_TEXT N_("Image gamma (0-10)")
73 #define GAMMA_LONGTEXT N_("Set the image gamma, between 0.01 and 10. Defaults to 1.")
74
75
76 vlc_module_begin();
77     set_description( _("Image properties filter") );
78     set_shortname( N_("Image adjust" ));
79     set_category( CAT_VIDEO );
80     set_subcategory( SUBCAT_VIDEO_VFILTER );
81     set_capability( "video filter", 0 );
82
83     add_float_with_range( "contrast", 1.0, 0.0, 2.0, NULL,
84                           CONT_TEXT, CONT_LONGTEXT, VLC_FALSE );
85     add_float_with_range( "brightness", 1.0, 0.0, 2.0, NULL,
86                            LUM_TEXT, LUM_LONGTEXT, VLC_FALSE );
87     add_integer_with_range( "hue", 0, 0, 360, NULL,
88                             HUE_TEXT, HUE_LONGTEXT, VLC_FALSE );
89     add_float_with_range( "saturation", 1.0, 0.0, 3.0, NULL,
90                           SAT_TEXT, SAT_LONGTEXT, VLC_FALSE );
91     add_float_with_range( "gamma", 1.0, 0.01, 10.0, NULL,
92                           GAMMA_TEXT, GAMMA_LONGTEXT, VLC_FALSE );
93
94     add_bool( "brightness-threshold", 0, NULL,
95               THRES_TEXT, THRES_LONGTEXT, VLC_FALSE );
96
97     add_shortcut( "adjust" );
98     set_callbacks( Create, Destroy );
99 vlc_module_end();
100
101 /*****************************************************************************
102  * vout_sys_t: adjust video output method descriptor
103  *****************************************************************************
104  * This structure is part of the video output thread descriptor.
105  * It describes the adjust specific properties of an output thread.
106  *****************************************************************************/
107 struct vout_sys_t
108 {
109     vout_thread_t *p_vout;
110 };
111
112 inline static int32_t clip( int32_t a )
113 {
114     return (a > 255) ? 255 : (a < 0) ? 0 : a;
115 }
116
117 /*****************************************************************************
118  * Control: control facility for the vout (forwards to child vout)
119  *****************************************************************************/
120 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
121 {
122     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
123 }
124
125 /*****************************************************************************
126  * Create: allocates adjust video thread output method
127  *****************************************************************************
128  * This function allocates and initializes a adjust vout method.
129  *****************************************************************************/
130 static int Create( vlc_object_t *p_this )
131 {
132     vout_thread_t *p_vout = (vout_thread_t *)p_this;
133
134     /* Allocate structure */
135     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
136     if( p_vout->p_sys == NULL )
137     {
138         msg_Err( p_vout, "out of memory" );
139         return VLC_ENOMEM;
140     }
141
142     p_vout->pf_init = Init;
143     p_vout->pf_end = End;
144     p_vout->pf_manage = NULL;
145     p_vout->pf_render = Render;
146     p_vout->pf_display = NULL;
147     p_vout->pf_control = Control;
148
149     var_Create( p_vout, "contrast", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
150     var_Create( p_vout, "brightness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
151     var_Create( p_vout, "hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
152     var_Create( p_vout, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
153     var_Create( p_vout, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
154     var_Create( p_vout, "brightness-threshold",
155                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
156
157     return VLC_SUCCESS;
158 }
159
160 /*****************************************************************************
161  * Init: initialize adjust video thread output method
162  *****************************************************************************/
163 static int Init( vout_thread_t *p_vout )
164 {
165     int i_index;
166     picture_t *p_pic;
167     video_format_t fmt = {0};
168
169     I_OUTPUTPICTURES = 0;
170
171     /* Initialize the output structure */
172     p_vout->output.i_chroma = p_vout->render.i_chroma;
173     p_vout->output.i_width  = p_vout->render.i_width;
174     p_vout->output.i_height = p_vout->render.i_height;
175     p_vout->output.i_aspect = p_vout->render.i_aspect;
176
177     fmt.i_width = fmt.i_visible_width = p_vout->render.i_width;
178     fmt.i_height = fmt.i_visible_height = p_vout->render.i_height;
179     fmt.i_x_offset = fmt.i_y_offset = 0;
180     fmt.i_chroma = p_vout->render.i_chroma;
181     fmt.i_aspect = p_vout->render.i_aspect;
182     fmt.i_sar_num = p_vout->render.i_aspect * fmt.i_height / fmt.i_width;
183     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
184
185     /* Try to open the real video output */
186     msg_Dbg( p_vout, "spawning the real video output" );
187
188     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
189
190     /* Everything failed */
191     if( p_vout->p_sys->p_vout == NULL )
192     {
193         msg_Err( p_vout, "can't open vout, aborting" );
194         return VLC_EGENERIC;
195     }
196
197     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
198
199     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
200
201     ADD_PARENT_CALLBACKS( SendEventsToChild );
202
203     return VLC_SUCCESS;
204 }
205
206 /*****************************************************************************
207  * End: terminate adjust video thread output method
208  *****************************************************************************/
209 static void End( vout_thread_t *p_vout )
210 {
211     int i_index;
212
213     /* Free the fake output buffers we allocated */
214     for( i_index = I_OUTPUTPICTURES ; i_index ; )
215     {
216         i_index--;
217         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
218     }
219 }
220
221 /*****************************************************************************
222  * Destroy: destroy adjust video thread output method
223  *****************************************************************************
224  * Terminate an output method created by adjustCreateOutputMethod
225  *****************************************************************************/
226 static void Destroy( vlc_object_t *p_this )
227 {
228     vout_thread_t *p_vout = (vout_thread_t *)p_this;
229
230     if( p_vout->p_sys->p_vout )
231     {
232         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
233         vlc_object_detach( p_vout->p_sys->p_vout );
234         vout_Destroy( p_vout->p_sys->p_vout );
235     }
236
237     DEL_PARENT_CALLBACKS( SendEventsToChild );
238
239     free( p_vout->p_sys );
240 }
241
242 /*****************************************************************************
243  * Render: displays previously rendered output
244  *****************************************************************************
245  * This function send the currently rendered image to adjust modified image,
246  * waits until it is displayed and switch the two rendering buffers, preparing
247  * next frame.
248  *****************************************************************************/
249 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
250 {
251     int pi_luma[256];
252     int pi_gamma[256];
253
254     picture_t *p_outpic;
255     uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
256     uint8_t *p_out, *p_out_v;
257
258     vlc_bool_t b_thres;
259     double  f_hue;
260     double  f_gamma;
261     int32_t i_cont, i_lum;
262     int i_sat, i_sin, i_cos, i_x, i_y;
263     int i;
264     vlc_value_t val;
265
266     /* This is a new frame. Get a structure from the video_output. */
267     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
268               == NULL )
269     {
270         if( p_vout->b_die || p_vout->b_error )
271         {
272             return;
273         }
274         msleep( VOUT_OUTMEM_SLEEP );
275     }
276
277     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
278     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
279
280     /* Getvariables */
281     var_Get( p_vout, "contrast", &val );
282     i_cont = (int) ( val.f_float * 255 );
283     var_Get( p_vout, "brightness", &val );
284     i_lum = (int) (( val.f_float - 1.0 ) * 255 );
285     var_Get( p_vout, "hue", &val );
286     f_hue = (float) ( val.i_int * M_PI / 180 );
287     var_Get( p_vout, "saturation", &val );
288     i_sat = (int) (val.f_float * 256 );
289     var_Get( p_vout, "gamma", &val );
290     f_gamma = 1.0 / val.f_float;
291     var_Get( p_vout, "brightness-threshold", &val );
292     b_thres = (vlc_bool_t) ( val.b_bool );
293
294     /*
295      * Threshold mode drops out everything about luma, contrast and gamma.
296      */
297     if( b_thres != VLC_TRUE )
298     {
299
300         /* Contrast is a fast but kludged function, so I put this gap to be
301          * cleaner :) */
302         i_lum += 128 - i_cont / 2;
303
304         /* Fill the gamma lookup table */
305         for( i = 0 ; i < 256 ; i++ )
306         {
307           pi_gamma[ i ] = clip( pow(i / 255.0, f_gamma) * 255.0);
308         }
309
310         /* Fill the luma lookup table */
311         for( i = 0 ; i < 256 ; i++ )
312         {
313             pi_luma[ i ] = pi_gamma[clip( i_lum + i_cont * i / 256)];
314         }
315     }
316     else
317     {
318         /*
319          * We get luma as threshold value: the higher it is, the darker is
320          * the image. Should I reverse this?
321          */
322         for( i = 0 ; i < 256 ; i++ )
323         {
324             pi_luma[ i ] = (i < i_lum) ? 0 : 255;
325         }
326
327         /*
328          * Desaturates image to avoid that strange yellow halo...
329          */
330         i_sat = 0;
331     }
332
333     /*
334      * Do the Y plane
335      */
336
337     p_in = p_pic->p[0].p_pixels;
338     p_in_end = p_in + p_pic->p[0].i_visible_lines * p_pic->p[0].i_pitch - 8;
339
340     p_out = p_outpic->p[0].p_pixels;
341
342     for( ; p_in < p_in_end ; )
343     {
344         p_line_end = p_in + p_pic->p[0].i_visible_pitch - 8;
345
346         for( ; p_in < p_line_end ; )
347         {
348             /* Do 8 pixels at a time */
349             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
350             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
351             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
352             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
353         }
354
355         p_line_end += 8;
356
357         for( ; p_in < p_line_end ; )
358         {
359             *p_out++ = pi_luma[ *p_in++ ];
360         }
361
362         p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
363         p_out += p_outpic->p[0].i_pitch - p_outpic->p[0].i_visible_pitch;
364     }
365
366     /*
367      * Do the U and V planes
368      */
369
370     p_in = p_pic->p[1].p_pixels;
371     p_in_v = p_pic->p[2].p_pixels;
372     p_in_end = p_in + p_pic->p[1].i_visible_lines * p_pic->p[1].i_pitch - 8;
373
374     p_out = p_outpic->p[1].p_pixels;
375     p_out_v = p_outpic->p[2].p_pixels;
376
377     i_sin = sin(f_hue) * 256;
378     i_cos = cos(f_hue) * 256;
379
380     i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
381     i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
382
383     if ( i_sat > 256 )
384     {
385 #define WRITE_UV_CLIP() \
386     i_u = *p_in++ ; i_v = *p_in_v++ ; \
387     *p_out++ = clip( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
388                            * i_sat) >> 8) + 128); \
389     *p_out_v++ = clip( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
390                            * i_sat) >> 8) + 128)
391
392         uint8_t i_u, i_v;
393
394         for( ; p_in < p_in_end ; )
395         {
396             p_line_end = p_in + p_pic->p[1].i_visible_pitch - 8;
397
398             for( ; p_in < p_line_end ; )
399             {
400                 /* Do 8 pixels at a time */
401                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
402                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
403                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
404                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
405             }
406
407             p_line_end += 8;
408
409             for( ; p_in < p_line_end ; )
410             {
411                 WRITE_UV_CLIP();
412             }
413
414             p_in += p_pic->p[1].i_pitch - p_pic->p[1].i_visible_pitch;
415             p_in_v += p_pic->p[2].i_pitch - p_pic->p[2].i_visible_pitch;
416             p_out += p_outpic->p[1].i_pitch - p_outpic->p[1].i_visible_pitch;
417             p_out_v += p_outpic->p[2].i_pitch - p_outpic->p[2].i_visible_pitch;
418         }
419     }
420     else
421     {
422 #define WRITE_UV() \
423     i_u = *p_in++ ; i_v = *p_in_v++ ; \
424     *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
425                        * i_sat) >> 8) + 128; \
426     *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
427                        * i_sat) >> 8) + 128
428
429         uint8_t i_u, i_v;
430
431         for( ; p_in < p_in_end ; )
432         {
433             p_line_end = p_in + p_pic->p[1].i_visible_pitch - 8;
434
435             for( ; p_in < p_line_end ; )
436             {
437                 /* Do 8 pixels at a time */
438                 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
439                 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
440             }
441
442             p_line_end += 8;
443
444             for( ; p_in < p_line_end ; )
445             {
446                 WRITE_UV();
447             }
448
449             p_in += p_pic->p[1].i_pitch - p_pic->p[1].i_visible_pitch;
450             p_in_v += p_pic->p[2].i_pitch - p_pic->p[2].i_visible_pitch;
451             p_out += p_outpic->p[1].i_pitch - p_outpic->p[1].i_visible_pitch;
452             p_out_v += p_outpic->p[2].i_pitch - p_outpic->p[2].i_visible_pitch;
453         }
454     }
455
456     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
457
458     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
459 }
460
461 /*****************************************************************************
462  * SendEvents: forward mouse and keyboard events to the parent p_vout
463  *****************************************************************************/
464 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
465                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
466 {
467     var_Set( (vlc_object_t *)p_data, psz_var, newval );
468
469     return VLC_SUCCESS;
470 }
471
472 /*****************************************************************************
473  * SendEventsToChild: forward events to the child/children vout
474  *****************************************************************************/
475 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
476                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
477 {
478     vout_thread_t *p_vout = (vout_thread_t *)p_this;
479     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
480     return VLC_SUCCESS;
481 }