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.15 2004/01/22 15:00:10 sigmunau Exp $
7 * Authors: Simon Latapie <garf@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
35 #include "filter_common.h"
38 # define M_PI 3.14159265358979323846
41 #define eight_times( x ) x x x x x x x x
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Create ( vlc_object_t * );
47 static void Destroy ( vlc_object_t * );
49 static int Init ( vout_thread_t * );
50 static void End ( vout_thread_t * );
51 static void Render ( vout_thread_t *, picture_t * );
53 static int SendEvents( vlc_object_t *, char const *,
54 vlc_value_t, vlc_value_t, void * );
56 /*****************************************************************************
58 *****************************************************************************/
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 #define GAMMA_TEXT N_("Set image gamma")
69 #define GAMMA_LONGTEXT N_("Set the image gamma, between 0.01 and 10. Defaults to 1")
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( _("contrast/hue/saturation/brightness/gamma filter") );
80 set_capability( "video filter", 0 );
81 add_shortcut( "adjust" );
82 set_callbacks( Create, Destroy );
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 *****************************************************************************/
93 vout_thread_t *p_vout;
96 inline static int32_t clip( int32_t a )
98 return (a > 255) ? 255 : (a < 0) ? 0 : a;
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 )
108 vout_thread_t *p_vout = (vout_thread_t *)p_this;
110 /* Allocate structure */
111 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
112 if( p_vout->p_sys == NULL )
114 msg_Err( p_vout, "out of memory" );
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;
127 /*****************************************************************************
128 * Init: initialize adjust video thread output method
129 *****************************************************************************/
130 static int Init( vout_thread_t *p_vout )
135 I_OUTPUTPICTURES = 0;
137 /* Initialize the output structure */
138 p_vout->output.i_chroma = p_vout->render.i_chroma;
139 p_vout->output.i_width = p_vout->render.i_width;
140 p_vout->output.i_height = p_vout->render.i_height;
141 p_vout->output.i_aspect = p_vout->render.i_aspect;
143 /* Try to open the real video output */
144 msg_Dbg( p_vout, "spawning the real video output" );
146 p_vout->p_sys->p_vout = vout_Create( p_vout,
147 p_vout->render.i_width, p_vout->render.i_height,
148 p_vout->render.i_chroma, p_vout->render.i_aspect );
150 /* Everything failed */
151 if( p_vout->p_sys->p_vout == NULL )
153 msg_Err( p_vout, "can't open vout, aborting" );
158 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
160 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
162 ADD_PARENT_CALLBACKS( SendEventsToChild );
167 /*****************************************************************************
168 * End: terminate adjust video thread output method
169 *****************************************************************************/
170 static void End( vout_thread_t *p_vout )
174 /* Free the fake output buffers we allocated */
175 for( i_index = I_OUTPUTPICTURES ; i_index ; )
178 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
182 /*****************************************************************************
183 * Destroy: destroy adjust video thread output method
184 *****************************************************************************
185 * Terminate an output method created by adjustCreateOutputMethod
186 *****************************************************************************/
187 static void Destroy( vlc_object_t *p_this )
189 vout_thread_t *p_vout = (vout_thread_t *)p_this;
191 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
192 vlc_object_detach( p_vout->p_sys->p_vout );
193 vout_Destroy( p_vout->p_sys->p_vout );
195 DEL_PARENT_CALLBACKS( SendEventsToChild );
197 free( p_vout->p_sys );
200 /*****************************************************************************
201 * Render: displays previously rendered output
202 *****************************************************************************
203 * This function send the currently rendered image to adjust modified image,
204 * waits until it is displayed and switch the two rendering buffers, preparing
206 *****************************************************************************/
207 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
213 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
214 uint8_t *p_out, *p_out_v;
218 int32_t i_cont, i_lum;
219 int i_sat, i_sin, i_cos, i_x, i_y;
222 /* This is a new frame. Get a structure from the video_output. */
223 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
226 if( p_vout->b_die || p_vout->b_error )
230 msleep( VOUT_OUTMEM_SLEEP );
233 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
234 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
236 /* Get configuration variables */
237 i_cont = config_GetFloat( p_vout, "contrast" ) * 255;
238 i_lum = (config_GetFloat( p_vout, "brightness" ) - 1.0) * 255;
239 f_hue = config_GetInt( p_vout, "hue" ) * M_PI / 180;
240 i_sat = config_GetFloat( p_vout, "saturation" ) * 256;
241 f_gamma = 1.0 / config_GetFloat( p_vout, "gamma" );
243 /* Contrast is a fast but kludged function, so I put this gap to be
245 i_lum += 128 - i_cont / 2;
247 /* Fill the gamma lookup table */
248 for( i = 0 ; i < 256 ; i++ )
250 pi_gamma[ i ] = clip( pow(i / 255.0, f_gamma) * 255.0);
253 /* Fill the luma lookup table */
254 for( i = 0 ; i < 256 ; i++ )
256 pi_luma[ i ] = pi_gamma[clip( i_lum + i_cont * i / 256)];
263 p_in = p_pic->p[0].p_pixels;
264 p_in_end = p_in + p_pic->p[0].i_lines * p_pic->p[0].i_pitch - 8;
266 p_out = p_outpic->p[0].p_pixels;
268 for( ; p_in < p_in_end ; )
270 p_line_end = p_in + p_pic->p[0].i_visible_pitch - 8;
272 for( ; p_in < p_line_end ; )
274 /* Do 8 pixels at a time */
275 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
276 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
277 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
278 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
283 for( ; p_in < p_line_end ; )
285 *p_out++ = pi_luma[ *p_in++ ];
288 p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
289 p_out += p_outpic->p[0].i_pitch - p_outpic->p[0].i_visible_pitch;
293 * Do the U and V planes
296 p_in = p_pic->p[1].p_pixels;
297 p_in_v = p_pic->p[2].p_pixels;
298 p_in_end = p_in + p_pic->p[1].i_lines * p_pic->p[1].i_pitch - 8;
300 p_out = p_outpic->p[1].p_pixels;
301 p_out_v = p_outpic->p[2].p_pixels;
303 i_sin = sin(f_hue) * 256;
304 i_cos = cos(f_hue) * 256;
306 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
307 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
311 #define WRITE_UV_CLIP() \
312 i_u = *p_in++ ; i_v = *p_in_v++ ; \
313 *p_out++ = clip( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
314 * i_sat) >> 8) + 128); \
315 *p_out_v++ = clip( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
316 * i_sat) >> 8) + 128)
320 for( ; p_in < p_in_end ; )
322 p_line_end = p_in + p_pic->p[1].i_visible_pitch - 8;
324 for( ; p_in < p_line_end ; )
326 /* Do 8 pixels at a time */
327 WRITE_UV_CLIP(); WRITE_UV_CLIP();
328 WRITE_UV_CLIP(); WRITE_UV_CLIP();
329 WRITE_UV_CLIP(); WRITE_UV_CLIP();
330 WRITE_UV_CLIP(); WRITE_UV_CLIP();
335 for( ; p_in < p_line_end ; )
340 p_in += p_pic->p[1].i_pitch - p_pic->p[1].i_visible_pitch;
341 p_in_v += p_pic->p[2].i_pitch - p_pic->p[2].i_visible_pitch;
342 p_out += p_outpic->p[1].i_pitch - p_outpic->p[1].i_visible_pitch;
343 p_out_v += p_outpic->p[2].i_pitch - p_outpic->p[2].i_visible_pitch;
349 i_u = *p_in++ ; i_v = *p_in_v++ ; \
350 *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
351 * i_sat) >> 8) + 128; \
352 *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
357 for( ; p_in < p_in_end ; )
359 p_line_end = p_in + p_pic->p[1].i_visible_pitch - 8;
361 for( ; p_in < p_line_end ; )
363 /* Do 8 pixels at a time */
364 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
365 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
370 for( ; p_in < p_line_end ; )
375 p_in += p_pic->p[1].i_pitch - p_pic->p[1].i_visible_pitch;
376 p_in_v += p_pic->p[2].i_pitch - p_pic->p[2].i_visible_pitch;
377 p_out += p_outpic->p[1].i_pitch - p_outpic->p[1].i_visible_pitch;
378 p_out_v += p_outpic->p[2].i_pitch - p_outpic->p[2].i_visible_pitch;
382 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
384 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
387 /*****************************************************************************
388 * SendEvents: forward mouse and keyboard events to the parent p_vout
389 *****************************************************************************/
390 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
391 vlc_value_t oldval, vlc_value_t newval, void *p_data )
393 var_Set( (vlc_object_t *)p_data, psz_var, newval );
398 /*****************************************************************************
399 * SendEventsToChild: forward events to the child/children vout
400 *****************************************************************************/
401 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
402 vlc_value_t oldval, vlc_value_t newval, void *p_data )
404 vout_thread_t *p_vout = (vout_thread_t *)p_this;
405 var_Set( p_vout->p_sys->p_vout, psz_var, newval );