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