1 /*****************************************************************************
2 * adjust.c : Contrast/Hue/Saturation/Brightness video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: adjust.c,v 1.4 2002/12/12 10:56:24 garf Exp $
7 * Authors: Simon Latapie <garf@via.ecp.fr>, Samuel Hocevar <sam@zoy.org>
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 eight_times( x ) x x x x x x x x
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Create ( vlc_object_t * );
44 static void Destroy ( vlc_object_t * );
46 static int Init ( vout_thread_t * );
47 static void End ( vout_thread_t * );
48 static void Render ( vout_thread_t *, picture_t * );
50 /*****************************************************************************
52 *****************************************************************************/
54 #define CONT_TEXT N_("Contrast")
55 #define CONT_LONGTEXT N_("Default to 1")
56 #define HUE_TEXT N_("Hue")
57 #define HUE_LONGTEXT N_("Between 0 and 360. Default to 0")
58 #define SAT_TEXT N_("Saturation")
59 #define SAT_LONGTEXT N_("Default to 1")
60 #define LUM_TEXT N_("Brightness")
61 #define LUM_LONGTEXT N_("Default to 1")
65 add_category_hint( N_("Miscellaneous"), NULL );
66 add_float( "Contrast", 1.0, NULL, CONT_TEXT, CONT_LONGTEXT );
67 add_float( "Brightness", 1.0, NULL, LUM_TEXT, LUM_LONGTEXT );
68 add_integer( "Hue", 0, NULL, HUE_TEXT, HUE_LONGTEXT );
69 add_float( "Saturation", 1.0, NULL, SAT_TEXT, SAT_LONGTEXT );
70 set_description( _("Contrast/Hue/Saturation/Brightness filter") );
71 set_capability( "video filter", 0 );
72 add_shortcut( "adjust" );
73 set_callbacks( Create, Destroy );
76 /*****************************************************************************
77 * vout_sys_t: adjust video output method descriptor
78 *****************************************************************************
79 * This structure is part of the video output thread descriptor.
80 * It describes the adjust specific properties of an output thread.
81 *****************************************************************************/
84 vout_thread_t *p_vout;
87 inline static int32_t maxmin( int32_t a )
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 )
105 vout_thread_t *p_vout = (vout_thread_t *)p_this;
107 /* Allocate structure */
108 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
109 if( p_vout->p_sys == NULL )
111 msg_Err( p_vout, "out of memory" );
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;
124 /*****************************************************************************
125 * Init: initialize adjust video thread output method
126 *****************************************************************************/
127 static int Init( vout_thread_t *p_vout )
132 I_OUTPUTPICTURES = 0;
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;
140 /* Try to open the real video output */
141 msg_Dbg( p_vout, "spawning the real video output" );
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 );
147 /* Everything failed */
148 if( p_vout->p_sys->p_vout == NULL )
150 msg_Err( p_vout, "can't open vout, aborting" );
155 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
160 /*****************************************************************************
161 * End: terminate adjust video thread output method
162 *****************************************************************************/
163 static void End( vout_thread_t *p_vout )
167 /* Free the fake output buffers we allocated */
168 for( i_index = I_OUTPUTPICTURES ; i_index ; )
171 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
175 /*****************************************************************************
176 * Destroy: destroy adjust video thread output method
177 *****************************************************************************
178 * Terminate an output method created by adjustCreateOutputMethod
179 *****************************************************************************/
180 static void Destroy( vlc_object_t *p_this )
182 vout_thread_t *p_vout = (vout_thread_t *)p_this;
184 vout_Destroy( p_vout->p_sys->p_vout );
186 free( p_vout->p_sys );
189 /*****************************************************************************
190 * Render: displays previously rendered output
191 *****************************************************************************
192 * This function send the currently rendered image to adjust modified image,
193 * waits until it is displayed and switch the two rendering buffers, preparing
195 *****************************************************************************/
196 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
203 /* Contrast is a fast but cludged function, so I put this gap to be
214 /* This is a new frame. Get a structure from the video_output. */
216 cont = (s32) ( config_GetFloat( p_vout, "Contrast" ) * 255 );
217 lum = (s32) ( ( config_GetFloat( p_vout, "Brightness" ) * 255 ) - 255 );
218 hue = config_GetInt( p_vout, "Hue" ) * 3.14159 / 180 ; /* convert in radian */
219 i_sat = (int) ( config_GetFloat( p_vout, "Saturation" ) * 256 );
221 dec = 128 - ( cont / 2 );
222 i_Sin = (int) ( sin(hue) * 256);
223 i_Cos = (int) ( cos(hue) * 256);
226 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
229 if( p_vout->b_die || p_vout->b_error )
233 msleep( VOUT_OUTMEM_SLEEP );
236 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
237 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
239 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
245 u8 *p_in, *p_in_end, *p_out;
247 p_in = p_pic->p[i_index].p_pixels;
248 p_in_end = p_in + p_pic->p[i_index].i_lines
249 * p_pic->p[i_index].i_pitch -8;
251 p_out = p_outpic->p[i_index].p_pixels;
253 for( i = 0 ; i < 256 ; i++ )
255 p_lum_func[ i ] = maxmin( (( i * cont ) >> 8 ) + lum + dec );
259 for( ; p_in < p_in_end ; )
261 /* Do 8 pixels at a time */
263 eight_times( *p_out = p_lum_func[ *p_in ]; p_out++; p_in++; )
269 for( ; p_in < p_in_end ; )
271 /* Do 1 pixel at a time */
272 *p_out = p_lum_func[ *p_in ]; p_out++; p_in++;
280 u8 *p_in_u, *p_in_v, *p_in_end, *p_out_u, *p_out_v, i_u, i_v;
281 s32 cospsin, cosmsin;
283 p_in_u = p_pic->p[i_index].p_pixels;
284 p_in_v = p_pic->p[i_index + 1].p_pixels;
285 p_in_end = p_in_u + p_pic->p[i_index].i_lines
286 * p_pic->p[i_index].i_pitch -8;
288 p_out_u = p_outpic->p[i_index].p_pixels;
289 p_out_v = p_outpic->p[i_index + 1].p_pixels;
291 cospsin = 32768 * ( cos(hue) + sin(hue) );
292 cosmsin = 32768 * ( cos(hue) - sin(hue) );
296 for( ; p_in_u < p_in_end ; )
298 /* Do 8 pixels at a time */
300 eight_times( i_u = *p_in_u ;
302 *p_out_u = maxmin( (( ((i_u * i_Cos + i_v * i_Sin - cospsin) >> 8) * i_sat) >> 8) + 128);
303 *p_out_v = maxmin( (( ((i_v * i_Cos - i_u * i_Sin - cosmsin) >> 8) * i_sat) >> 8) + 128);
304 p_out_u++; p_in_u++; p_out_v++; p_in_v++; )
310 for( ; p_in_u < p_in_end ; )
312 /* Do 1 pixel at a time */
315 *p_out_u = maxmin( (( ((i_u * i_Cos + i_v * i_Sin - cospsin) >> 8) * i_sat) >> 8) + 128);
316 *p_out_v = maxmin( (( ((i_v * i_Cos - i_u * i_Sin - cosmsin) >> 8) * i_sat) >> 8) + 128);
317 p_out_u++; p_in_u++; p_out_v++; p_in_v++;
324 for( ; p_in_u < p_in_end ; )
326 /* Do 8 pixels at a time */
328 eight_times( i_u = *p_in_u ;
330 *p_out_u = (( ((i_u * i_Cos + i_v * i_Sin - cospsin) >> 8) * i_sat) >> 8) + 128;
331 *p_out_v = (( ((i_v * i_Cos - i_u * i_Sin - cosmsin) >> 8) * i_sat) >> 8) + 128;
332 p_out_u++; p_in_u++; p_out_v++; p_in_v++; )
338 for( ; p_in_u < p_in_end ; )
340 /* Do 1 pixel at a time */
343 *p_out_u = (( ((i_u * i_Cos + i_v * i_Sin - cospsin) >> 8) * i_sat) >> 8) + 128;
344 *p_out_v = (( ((i_v * i_Cos - i_u * i_Sin - cosmsin) >> 8) * i_sat) >> 8) + 128;
345 p_out_u++; p_in_u++; p_out_v++; p_in_v++;
356 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
358 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );