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