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