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