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