]> git.sesse.net Git - vlc/blob - modules/video_filter/panoramix.c
Converted panoramix to video splitter.
[vlc] / modules / video_filter / panoramix.c
1 /*****************************************************************************
2  * panoramix.c : Wall panoramic video with edge blending plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Cedric Cocquebert <cedric.cocquebert@supelec.fr>
8  *          based on Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <math.h>
33 #include <assert.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_video_splitter.h>
38
39 /* FIXME it is needed for VOUT_ALIGN_* only */
40 #include <vlc_vout.h>
41
42 #define OVERLAP
43
44 #ifdef OVERLAP
45 /* OS CODE DEPENDENT to get display dimensions */
46 #   ifdef WIN32
47 #       include <windows.h>
48 #   else
49 #       include <X11/Xlib.h>
50 #   endif
51 #endif
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 #define COLS_TEXT N_("Number of columns")
57 #define COLS_LONGTEXT N_("Select the number of horizontal video windows in " \
58     "which to split the video")
59
60 #define ROWS_TEXT N_("Number of rows")
61 #define ROWS_LONGTEXT N_("Select the number of vertical video windows in " \
62     "which to split the video")
63
64 #define ACTIVE_TEXT N_("Active windows")
65 #define ACTIVE_LONGTEXT N_("Comma separated list of active windows, " \
66     "defaults to all")
67
68 #define CFG_PREFIX "panoramix-"
69
70 static int  Open ( vlc_object_t * );
71 static void Close( vlc_object_t * );
72
73 vlc_module_begin()
74     set_description( N_("Panoramix: wall with overlap video filter") )
75     set_shortname( N_("Panoramix" ))
76     set_capability( "video splitter", 0 )
77     set_category( CAT_VIDEO )
78     set_subcategory( SUBCAT_VIDEO_VFILTER )
79
80     add_integer( CFG_PREFIX "cols", -1, NULL, COLS_TEXT, COLS_LONGTEXT, true )
81     add_integer( CFG_PREFIX "rows", -1, NULL, ROWS_TEXT, ROWS_LONGTEXT, true )
82
83 #ifdef OVERLAP
84 #define LENGTH_TEXT N_("length of the overlapping area (in %)")
85 #define LENGTH_LONGTEXT N_("Select in percent the length of the blended zone")
86     add_integer_with_range( CFG_PREFIX "bz-length", 100, 0, 100, NULL, LENGTH_TEXT, LENGTH_LONGTEXT, true )
87
88 #define HEIGHT_TEXT N_("height of the overlapping area (in %)")
89 #define HEIGHT_LONGTEXT N_("Select in percent the height of the blended zone (case of 2x2 wall)")
90     add_integer_with_range( CFG_PREFIX "bz-height", 100, 0, 100, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, true )
91
92 #define ATTENUATION_TEXT N_("Attenuation")
93 #define ATTENUATION_LONGTEXT N_("Check this option if you want attenuate blended zone by this plug-in (if option is unchecked, attenuate is made by opengl)")
94     add_bool( CFG_PREFIX "attenuate", 1, NULL, ATTENUATION_TEXT, ATTENUATION_LONGTEXT, false )
95
96 #define BEGIN_TEXT N_("Attenuation, begin (in %)")
97 #define BEGIN_LONGTEXT N_("Select in percent the Lagrange coeff of the beginning blended zone")
98     add_integer_with_range( CFG_PREFIX "bz-begin", 0, 0, 100, NULL, BEGIN_TEXT, BEGIN_LONGTEXT, true )
99
100 #define MIDDLE_TEXT N_("Attenuation, middle (in %)")
101 #define MIDDLE_LONGTEXT N_("Select in percent the Lagrange coeff of the middle of blended zone")
102     add_integer_with_range( CFG_PREFIX "bz-middle", 50, 0, 100, NULL, MIDDLE_TEXT, MIDDLE_LONGTEXT, false )
103
104 #define END_TEXT N_("Attenuation, end (in %)")
105 #define END_LONGTEXT N_("Select in percent the Lagrange coeff of the end of blended zone")
106     add_integer_with_range( CFG_PREFIX "bz-end", 100, 0, 100, NULL, END_TEXT, END_LONGTEXT, true )
107
108 #define MIDDLE_POS_TEXT N_("middle position (in %)")
109 #define MIDDLE_POS_LONGTEXT N_("Select in percent (50 is center) the position of the middle point (Lagrange) of blended zone")
110     add_integer_with_range( CFG_PREFIX "bz-middle-pos", 50, 1, 99, NULL, MIDDLE_POS_TEXT, MIDDLE_POS_LONGTEXT, false )
111 #define RGAMMA_TEXT N_("Gamma (Red) correction")
112 #define RGAMMA_LONGTEXT N_("Select the gamma for the correction of blended zone (Red or Y component)")
113     add_float_with_range( CFG_PREFIX "bz-gamma-red", 1, 0, 5, NULL, RGAMMA_TEXT, RGAMMA_LONGTEXT, true )
114
115 #define GGAMMA_TEXT N_("Gamma (Green) correction")
116 #define GGAMMA_LONGTEXT N_("Select the gamma for the correction of blended zone (Green or U component)")
117     add_float_with_range( CFG_PREFIX "bz-gamma-green", 1, 0, 5, NULL, GGAMMA_TEXT, GGAMMA_LONGTEXT, true )
118
119 #define BGAMMA_TEXT N_("Gamma (Blue) correction")
120 #define BGAMMA_LONGTEXT N_("Select the gamma for the correction of blended zone (Blue or V component)")
121     add_float_with_range( CFG_PREFIX "bz-gamma-blue", 1, 0, 5, NULL, BGAMMA_TEXT, BGAMMA_LONGTEXT, true )
122
123 #define RGAMMA_BC_TEXT N_("Black Crush for Red")
124 #define RGAMMA_BC_LONGTEXT N_("Select the Black Crush of blended zone (Red or Y component)")
125 #define GGAMMA_BC_TEXT N_("Black Crush for Green")
126 #define GGAMMA_BC_LONGTEXT N_("Select the Black Crush of blended zone (Green or U component)")
127 #define BGAMMA_BC_TEXT N_("Black Crush for Blue")
128 #define BGAMMA_BC_LONGTEXT N_("Select the Black Crush of blended zone (Blue or V component)")
129
130 #define RGAMMA_WC_TEXT N_("White Crush for Red")
131 #define RGAMMA_WC_LONGTEXT N_("Select the White Crush of blended zone (Red or Y component)")
132 #define GGAMMA_WC_TEXT N_("White Crush for Green")
133 #define GGAMMA_WC_LONGTEXT N_("Select the White Crush of blended zone (Green or U component)")
134 #define BGAMMA_WC_TEXT N_("White Crush for Blue")
135 #define BGAMMA_WC_LONGTEXT N_("Select the White Crush of blended zone (Blue or V component)")
136
137 #define RGAMMA_BL_TEXT N_("Black Level for Red")
138 #define RGAMMA_BL_LONGTEXT N_("Select the Black Level of blended zone (Red or Y component)")
139 #define GGAMMA_BL_TEXT N_("Black Level for Green")
140 #define GGAMMA_BL_LONGTEXT N_("Select the Black Level of blended zone (Green or U component)")
141 #define BGAMMA_BL_TEXT N_("Black Level for Blue")
142 #define BGAMMA_BL_LONGTEXT N_("Select the Black Level of blended zone (Blue or V component)")
143
144 #define RGAMMA_WL_TEXT N_("White Level for Red")
145 #define RGAMMA_WL_LONGTEXT N_("Select the White Level of blended zone (Red or Y component)")
146 #define GGAMMA_WL_TEXT N_("White Level for Green")
147 #define GGAMMA_WL_LONGTEXT N_("Select the White Level of blended zone (Green or U component)")
148 #define BGAMMA_WL_TEXT N_("White Level for Blue")
149 #define BGAMMA_WL_LONGTEXT N_("Select the White Level of blended zone (Blue or V component)")
150     add_integer_with_range( CFG_PREFIX "bz-blackcrush-red", 140, 0, 255, NULL, RGAMMA_BC_TEXT, RGAMMA_BC_LONGTEXT, true )
151     add_integer_with_range( CFG_PREFIX "bz-blackcrush-green", 140, 0, 255, NULL, GGAMMA_BC_TEXT, GGAMMA_BC_LONGTEXT, true )
152     add_integer_with_range( CFG_PREFIX "bz-blackcrush-blue", 140, 0, 255, NULL, BGAMMA_BC_TEXT, BGAMMA_BC_LONGTEXT, true )
153     add_integer_with_range( CFG_PREFIX "bz-whitecrush-red", 200, 0, 255, NULL, RGAMMA_WC_TEXT, RGAMMA_WC_LONGTEXT, true )
154     add_integer_with_range( CFG_PREFIX "bz-whitecrush-green", 200, 0, 255, NULL, GGAMMA_WC_TEXT, GGAMMA_WC_LONGTEXT, true )
155     add_integer_with_range( CFG_PREFIX "bz-whitecrush-blue", 200, 0, 255, NULL, BGAMMA_WC_TEXT, BGAMMA_WC_LONGTEXT, true )
156     add_integer_with_range( CFG_PREFIX "bz-blacklevel-red", 150, 0, 255, NULL, RGAMMA_BL_TEXT, RGAMMA_BL_LONGTEXT, true )
157     add_integer_with_range( CFG_PREFIX "bz-blacklevel-green", 150, 0, 255, NULL, GGAMMA_BL_TEXT, GGAMMA_BL_LONGTEXT, true )
158     add_integer_with_range( CFG_PREFIX "bz-blacklevel-blue", 150, 0, 255, NULL, BGAMMA_BL_TEXT, BGAMMA_BL_LONGTEXT, true )
159     add_integer_with_range( CFG_PREFIX "bz-whitelevel-red", 0, 0, 255, NULL, RGAMMA_WL_TEXT, RGAMMA_WL_LONGTEXT, true )
160     add_integer_with_range( CFG_PREFIX "bz-whitelevel-green", 0, 0, 255, NULL, GGAMMA_WL_TEXT, GGAMMA_WL_LONGTEXT, true )
161     add_integer_with_range( CFG_PREFIX "bz-whitelevel-blue", 0, 0, 255, NULL, BGAMMA_WL_TEXT, BGAMMA_WL_LONGTEXT, true )
162 #ifndef WIN32
163     add_deprecated_alias( CFG_PREFIX "xinerama" );
164 #endif
165     add_deprecated_alias( CFG_PREFIX "offset-x" )
166 #endif
167
168     add_string( CFG_PREFIX "active", NULL, NULL, ACTIVE_TEXT, ACTIVE_LONGTEXT, true )
169
170     add_shortcut( "panoramix" )
171     set_callbacks( Open, Close )
172 vlc_module_end()
173
174
175 /*****************************************************************************
176  * Local prototypes
177  *****************************************************************************/
178 static const char *const ppsz_filter_options[] = {
179     "cols", "rows", "bz-length", "bz-height", "attenuate",
180     "bz-begin", "bz-middle", "bz-end", "bz-middle-pos", "bz-gamma-red",
181     "bz-gamma-green", "bz-gamma-blue", "bz-blackcrush-red",
182     "bz-blackcrush-green", "bz-blackcrush-blue", "bz-whitecrush-red",
183     "bz-whitecrush-green", "bz-whitecrush-blue", "bz-blacklevel-red",
184     "bz-blacklevel-green", "bz-blacklevel-blue", "bz-whitelevel-red",
185     "bz-whitelevel-green", "bz-whitelevel-blue", "active",
186     NULL
187 };
188
189 #define ROW_MAX (15)
190 #define COL_MAX (15)
191
192 #define ACCURACY 1000
193
194 /* */
195 static inline int clip_accuracy( int a )
196 {
197     return (a > ACCURACY) ? ACCURACY : (a < 0) ? 0 : a;
198 }
199 static inline float clip_unit( float f )
200 {
201     return f < 0.0 ? 0.0 : ( f > 1.0 ? 1.0 : f );
202 }
203
204 /* */
205 typedef struct
206 {
207     float f_black_crush;
208     float f_black_level;
209
210     float f_white_crush;
211     float f_white_level;
212
213     float f_gamma;
214 } panoramix_gamma_t;
215
216 typedef struct
217 {
218     struct
219     {
220         int i_left;
221         int i_right;
222         int i_top;
223         int i_bottom;
224     } black;
225     struct
226     {
227         int i_left;
228         int i_right;
229         int i_top;
230         int i_bottom;
231     } attenuate;
232 } panoramix_filter_t;
233
234 typedef struct
235 {
236     bool b_active;
237     int  i_output;
238
239     /* Output position and size */
240     int i_x;
241     int i_y;
242     int i_width;
243     int i_height;
244     int i_align;
245
246     /* Source position and size */
247     int  i_src_x;
248     int  i_src_y;
249     int  i_src_width;
250     int  i_src_height;
251
252     /* Filter configuration to use to create the output */
253     panoramix_filter_t filter;
254
255 } panoramix_output_t;
256
257 typedef struct
258 {
259     vlc_fourcc_t i_chroma;
260
261     const int pi_div_w[VOUT_MAX_PLANES];
262     const int pi_div_h[VOUT_MAX_PLANES];
263
264     const int pi_black[VOUT_MAX_PLANES];
265
266     bool b_planar;
267
268 } panoramix_chroma_t;
269
270 struct video_splitter_sys_t
271 {
272     const panoramix_chroma_t *p_chroma;
273
274     /* */
275     bool   b_attenuate;
276     unsigned int bz_length, bz_height;
277     unsigned int bz_begin, bz_middle, bz_end;
278     unsigned int bz_middle_pos;
279     unsigned int a_0, a_1, a_2;
280
281     int lambdav[VOUT_MAX_PLANES][2][ACCURACY/2]; /* [plane][position][?] */
282     int lambdah[VOUT_MAX_PLANES][2][ACCURACY/2];
283
284     unsigned int i_overlap_w2;  /* Half overlap width */
285     unsigned int i_overlap_h2;  /* Half overlap height */
286     uint8_t      p_lut[VOUT_MAX_PLANES][ACCURACY + 1][256];
287
288     /* */
289     int i_col;
290     int i_row;
291     panoramix_output_t pp_output[COL_MAX][ROW_MAX]; /* [x][y] */
292 };
293
294 /* */
295 static int Filter( video_splitter_t *, picture_t *pp_dst[], picture_t * );
296
297 static int Mouse( video_splitter_t *, vlc_mouse_t *,
298                   int i_index,
299                   const vlc_mouse_t *p_old, const vlc_mouse_t *p_new );
300
301
302 /* */
303 static int Configuration( panoramix_output_t pp_output[ROW_MAX][COL_MAX],
304                           int i_col, int i_row,
305                           int i_src_width, int i_src_height,
306                           int i_half_w, int i_half_h,
307                           bool b_attenuate,
308                           const bool *pb_active );
309 static double GammaFactor( const panoramix_gamma_t *, float f_value );
310
311 static void FilterPlanar( uint8_t *p_out, int i_out_pitch,
312                           const uint8_t *p_in, int i_in_pitch,
313                           int i_copy_pitch,
314                           int i_copy_lines,
315                           int i_pixel_black,
316                           const panoramix_filter_t *,
317                           uint8_t p_lut[ACCURACY + 1][256],
318                           int lambdav[2][ACCURACY/2],
319                           int lambdah[2][ACCURACY/2] );
320
321 /* */
322 static const panoramix_chroma_t p_chroma_array[] = {
323     /* Planar chroma */
324     { VLC_CODEC_I410, { 1, 4, 4, }, { 1, 1, 1, }, { 0, 128, 128 }, true },
325     { VLC_CODEC_I411, { 1, 4, 4, }, { 1, 4, 4, }, { 0, 128, 128 }, true },
326     { VLC_CODEC_YV12, { 1, 2, 2, }, { 1, 2, 2, }, { 0, 128, 128 }, true },
327     { VLC_CODEC_I420, { 1, 2, 2, }, { 1, 2, 2, }, { 0, 128, 128 }, true },
328     { VLC_CODEC_J420, { 1, 2, 2, }, { 1, 2, 2, }, { 0, 128, 128 }, true },
329     { VLC_CODEC_I422, { 1, 2, 2, }, { 1, 1, 1, }, { 0, 128, 128 }, true },
330     { VLC_CODEC_J422, { 1, 2, 2, }, { 1, 1, 1, }, { 0, 128, 128 }, true },
331     { VLC_CODEC_I440, { 1, 1, 1, }, { 1, 2, 2, }, { 0, 128, 128 }, true },
332     { VLC_CODEC_J440, { 1, 1, 1, }, { 1, 2, 2, }, { 0, 128, 128 }, true },
333     { VLC_CODEC_I444, { 1, 1, 1, }, { 1, 1, 1, }, { 0, 128, 128 }, true },
334     /* TODO packed chroma (yuv/rgb) ? */
335
336     { 0, {0, }, { 0, }, { 0, 0, 0 }, false }
337 };
338
339 /*****************************************************************************
340  * Open: allocates Wall video thread output method
341  *****************************************************************************
342  * This function allocates and initializes a Wall vout method.
343  *****************************************************************************/
344 static int Open( vlc_object_t *p_this )
345 {
346     video_splitter_t *p_splitter = (video_splitter_t*)p_this;
347     video_splitter_sys_t *p_sys;
348
349     const panoramix_chroma_t *p_chroma;
350     for( int i = 0; ; i++ )
351     {
352         vlc_fourcc_t i_chroma = p_chroma_array[i].i_chroma;
353         if( !i_chroma )
354         {
355             msg_Err( p_splitter, "colorspace not supported by plug-in !" );
356             return VLC_EGENERIC;
357         }
358         if( i_chroma == p_splitter->fmt.i_chroma )
359         {
360             p_chroma = &p_chroma_array[i];
361             break;
362         }
363     }
364
365     /* Allocate structure */
366     p_splitter->p_sys = p_sys = malloc( sizeof( *p_sys ) );
367     if( !p_sys )
368         return VLC_ENOMEM;
369
370     /* */
371     p_sys->p_chroma = p_chroma;
372
373     /* */
374     config_ChainParse( p_splitter, CFG_PREFIX, ppsz_filter_options,
375                        p_splitter->p_cfg );
376
377     /* */
378     p_sys->i_col = var_CreateGetInteger( p_splitter, CFG_PREFIX "cols" );
379     p_sys->i_row = var_CreateGetInteger( p_splitter, CFG_PREFIX "rows" );
380
381     /* Autodetect number of displays */
382     if( p_sys->i_col < 0 || p_sys->i_row < 0 )
383     {
384 #ifdef WIN32
385         const int i_monitor_count = GetSystemMetrics(SM_CMONITORS);
386         if( i_monitor_count > 1 )
387         {
388             p_sys->i_col = GetSystemMetrics( SM_CXVIRTUALSCREEN ) / GetSystemMetrics( SM_CXSCREEN );
389             p_sys->i_row = GetSystemMetrics( SM_CYVIRTUALSCREEN ) / GetSystemMetrics( SM_CYSCREEN );
390             if( p_sys->i_col * p_sys->i_row != i_monitor_count )
391             {
392                 p_sys->i_col = i_monitor_count;
393                 p_sys->i_row = 1;
394             }
395         }
396 #else
397         /* TODO linux */
398 #endif
399         /* By default do 2x1 */
400         if( p_sys->i_col < 0 || p_sys->i_row < 0 )
401         {
402             p_sys->i_col = 2;
403             p_sys->i_row = 1;
404         }
405         var_SetInteger( p_splitter, CFG_PREFIX "cols", p_sys->i_col);
406         var_SetInteger( p_splitter, CFG_PREFIX "rows", p_sys->i_row);
407     }
408
409     /* */
410     p_sys->b_attenuate = var_CreateGetBool( p_splitter, CFG_PREFIX "attenuate");
411     p_sys->bz_length = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-length" );
412     p_sys->bz_height = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-height" );
413     p_sys->bz_begin = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-begin" );
414     p_sys->bz_middle = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-middle" );
415     p_sys->bz_end = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-end" );
416     p_sys->bz_middle_pos = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-middle-pos" );
417     double d_p = 100.0 / p_sys->bz_middle_pos;
418
419     p_sys->a_2 = d_p * p_sys->bz_begin - (double)(d_p * d_p / (d_p - 1)) * p_sys->bz_middle + (double)(d_p / (d_p - 1)) * p_sys->bz_end;
420     p_sys->a_1 = -(d_p + 1) * p_sys->bz_begin + (double)(d_p * d_p / (d_p - 1)) * p_sys->bz_middle - (double)(1 / (d_p - 1)) * p_sys->bz_end;
421     p_sys->a_0 =  p_sys->bz_begin;
422
423     /* */
424     p_sys->i_col = __MAX( 1, __MIN( COL_MAX, p_sys->i_col ) );
425     p_sys->i_row = __MAX( 1, __MIN( ROW_MAX, p_sys->i_row ) );
426     msg_Dbg( p_splitter, "opening a %i x %i wall",
427              p_sys->i_col, p_sys->i_row );
428
429     if( p_sys->bz_length > 0 && ( p_sys->i_row > 1 || p_sys->i_col > 1 ) )
430     {
431         const int i_overlap_w2_max = p_splitter->fmt.i_width  / p_sys->i_col / 2;
432         const int i_overlap_h2_max = p_splitter->fmt.i_height / p_sys->i_row / 2;
433         const int i_overlap2_max = __MIN( i_overlap_w2_max, i_overlap_h2_max );
434
435         if( p_sys->i_col > 1 )
436             p_sys->i_overlap_w2 = i_overlap2_max * p_sys->bz_length / 100;
437         else
438             p_sys->i_overlap_w2 = 0;
439
440         if( p_sys->i_row > 1 )
441             p_sys->i_overlap_h2 = i_overlap2_max * p_sys->bz_height / 100;
442         else
443             p_sys->i_overlap_h2 = 0;
444
445         /* */
446         int i_div_max_w = 1;
447         int i_div_max_h = 1;
448         for( int i = 0; i < VOUT_MAX_PLANES; i++ )
449         {
450             i_div_max_w = __MAX( i_div_max_w, p_chroma->pi_div_w[i] );
451             i_div_max_h = __MAX( i_div_max_h, p_chroma->pi_div_h[i] );
452         }
453         p_sys->i_overlap_w2 = i_div_max_w * (p_sys->i_overlap_w2 / i_div_max_w);
454         p_sys->i_overlap_h2 = i_div_max_h * (p_sys->i_overlap_h2 / i_div_max_h);
455     }
456     else
457     {
458         p_sys->i_overlap_w2 = 0;
459         p_sys->i_overlap_h2 = 0;
460     }
461
462     /* Compute attenuate parameters */
463     if( p_sys->b_attenuate )
464     {
465         panoramix_gamma_t p_gamma[VOUT_MAX_PLANES];
466
467         p_gamma[0].f_gamma = var_CreateGetFloat( p_splitter, CFG_PREFIX "bz-gamma-red" );
468         p_gamma[1].f_gamma = var_CreateGetFloat( p_splitter, CFG_PREFIX "bz-gamma-green" );
469         p_gamma[2].f_gamma = var_CreateGetFloat( p_splitter, CFG_PREFIX "bz-gamma-blue" );
470
471         p_gamma[0].f_black_crush = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-blackcrush-red" ) / 255.0;
472         p_gamma[1].f_black_crush = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-blackcrush-green" ) / 255.0;
473         p_gamma[2].f_black_crush = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-blackcrush-blue" ) / 255.0;
474         p_gamma[0].f_white_crush = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-whitecrush-red" ) / 255.0;
475         p_gamma[1].f_white_crush = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-whitecrush-green" ) / 255.0;
476         p_gamma[2].f_white_crush = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-whitecrush-blue" ) / 255.0;
477
478         p_gamma[0].f_black_level = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-blacklevel-red" ) / 255.0;
479         p_gamma[1].f_black_level = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-blacklevel-green" ) / 255.0;
480         p_gamma[2].f_black_level = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-blacklevel-blue" ) / 255.0;
481         p_gamma[0].f_white_level = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-whitelevel-red" ) / 255.0;
482         p_gamma[1].f_white_level = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-whitelevel-green" ) / 255.0;
483         p_gamma[2].f_white_level = var_CreateGetInteger( p_splitter, CFG_PREFIX "bz-whitelevel-blue" ) / 255.0;
484
485         for( int i = 3; i < VOUT_MAX_PLANES; i++ )
486         {
487             /* Initialize unsupported planes */
488             p_gamma[i].f_gamma = 1.0;
489             p_gamma[i].f_black_crush = 140.0/255.0;
490             p_gamma[i].f_white_crush = 200.0/255.0;
491             p_gamma[i].f_black_level = 150.0/255.0;
492             p_gamma[i].f_white_level = 0.0/255.0;
493         }
494
495         if( p_chroma->i_chroma == VLC_CODEC_YV12 )
496         {
497             /* Exchange U and V */
498             panoramix_gamma_t t = p_gamma[1];
499             p_gamma[1] = p_gamma[2];
500             p_gamma[2] = t;
501         }
502         
503         for( int i_index = 0; i_index < 256; i_index++ )
504         {
505             for( int i_index2 = 0; i_index2 <= ACCURACY; i_index2++ )
506             {
507                 for( int i_plane = 0; i_plane < VOUT_MAX_PLANES; i_plane++ )
508                 {
509                     double f_factor = GammaFactor( &p_gamma[i_plane], (float)i_index / 255.0 );
510
511                     float f_lut = clip_unit( 1.0 - ((ACCURACY - (float)i_index2) * f_factor / (ACCURACY - 1)) );
512
513                     p_sys->p_lut[i_plane][i_index2][i_index] = f_lut * i_index + (int)( (1.0 - f_lut) * (float)p_chroma->pi_black[i_plane] );
514                 }
515             }
516         }
517
518         for( int i_plane = 0; i_plane < VOUT_MAX_PLANES; i_plane++ )
519         {
520             if( !p_chroma->pi_div_w[i_plane] || !p_chroma->pi_div_h[i_plane] )
521                 continue;
522             const int i_length_w = (2 * p_sys->i_overlap_w2) / p_chroma->pi_div_w[i_plane];
523             const int i_length_h = (2 * p_sys->i_overlap_h2) / p_chroma->pi_div_h[i_plane];
524
525             for( int i_dir = 0; i_dir < 2; i_dir++ )
526             {
527                 const int i_length = i_dir == 0 ? i_length_w : i_length_h;
528                 const int i_den = i_length * i_length;
529                 const int a_2 = p_sys->a_2 * (ACCURACY / 100);
530                 const int a_1 = p_sys->a_1 * i_length * (ACCURACY / 100);
531                 const int a_0 = p_sys->a_0 * i_den * (ACCURACY / 100);
532
533                 for( int i_position = 0; i_position < 2; i_position++ )
534                 {
535                     for( int i_index = 0; i_index < i_length; i_index++ )
536                     {
537                         const int v = i_position == 1 ? i_index : (i_length - i_index);
538                         const int i_lambda = clip_accuracy( ACCURACY - (a_2 * v*v + a_1 * v + a_0) / i_den );
539
540                         if( i_dir == 0 )
541                             p_sys->lambdav[i_plane][i_position][i_index] = i_lambda;
542                         else
543                             p_sys->lambdah[i_plane][i_position][i_index] = i_lambda;
544                     }
545                 }
546             }
547         }
548     }
549
550     /* */
551     char *psz_state = var_CreateGetNonEmptyString( p_splitter, CFG_PREFIX "active" );
552
553     /* */
554     bool pb_active[COL_MAX*ROW_MAX];
555
556     for( int i = 0; i < COL_MAX*ROW_MAX; i++ )
557         pb_active[i] = psz_state == NULL;
558
559     /* Parse active list if provided */
560     char *psz_tmp = psz_state;
561     while( psz_tmp && *psz_tmp )
562     {
563         char *psz_next = strchr( psz_tmp, ',' );
564         if( psz_next )
565             *psz_next++ = '\0';
566
567         const int i_index = atoi( psz_tmp );
568         if( i_index >= 0 && i_index < COL_MAX*ROW_MAX )
569             pb_active[i_index] = true;
570
571         psz_tmp = psz_next;
572     }
573     free( psz_state );
574
575     /* */
576     p_splitter->i_output =
577         Configuration( p_sys->pp_output,
578                        p_sys->i_col, p_sys->i_row,
579                        p_splitter->fmt.i_width, p_splitter->fmt.i_height,
580                        p_sys->i_overlap_w2, p_sys->i_overlap_h2,
581                        p_sys->b_attenuate,
582                        pb_active );
583     p_splitter->p_output = calloc( p_splitter->i_output,
584                                    sizeof(*p_splitter->p_output) );
585     if( !p_splitter->p_output )
586     {
587         free( p_sys );
588         return VLC_ENOMEM;
589     }
590
591     for( int y = 0; y < p_sys->i_row; y++ )
592     {
593         for( int x = 0; x < p_sys->i_col; x++ )
594         {
595             panoramix_output_t *p_output = &p_sys->pp_output[x][y];
596             if( !p_output->b_active )
597                 continue;
598
599             video_splitter_output_t *p_cfg = &p_splitter->p_output[p_output->i_output];
600
601             /* */
602             video_format_Copy( &p_cfg->fmt, &p_splitter->fmt );
603             p_cfg->fmt.i_visible_width  =
604             p_cfg->fmt.i_width          = p_output->i_width;
605             p_cfg->fmt.i_visible_height =
606             p_cfg->fmt.i_height         = p_output->i_height;
607
608             p_cfg->window.i_x     = p_output->i_x;
609             p_cfg->window.i_y     = p_output->i_y;
610             p_cfg->window.i_align = p_output->i_align;
611
612             p_cfg->psz_module = NULL;
613         }
614     }
615
616
617     /* */
618     p_splitter->pf_filter = Filter;
619     p_splitter->pf_mouse  = Mouse;
620
621     return VLC_SUCCESS;
622 }
623
624 /**
625  * Terminate a splitter module
626  */
627 static void Close( vlc_object_t *p_this )
628 {
629     video_splitter_t *p_splitter = (video_splitter_t*)p_this;
630     video_splitter_sys_t *p_sys = p_splitter->p_sys;
631
632     free( p_splitter->p_output );
633     free( p_sys );
634 }
635
636 /**
637  * It creates multiples pictures from the source one
638  */
639 static int Filter( video_splitter_t *p_splitter, picture_t *pp_dst[], picture_t *p_src )
640 {
641     video_splitter_sys_t *p_sys = p_splitter->p_sys;
642
643     if( video_splitter_NewPicture( p_splitter, pp_dst ) )
644     {
645         picture_Release( p_src );
646         return VLC_EGENERIC;
647     }
648
649     for( int y = 0; y < p_sys->i_row; y++ )
650     {
651         for( int x = 0; x < p_sys->i_col; x++ )
652         {
653             const panoramix_output_t *p_output = &p_sys->pp_output[x][y];
654             if( !p_output->b_active )
655                 continue;
656
657             /* */
658             picture_t *p_dst = pp_dst[p_output->i_output];
659
660             /* */
661             picture_CopyProperties( p_dst, p_src );
662
663             /* */
664             for( int i_plane = 0; i_plane < p_src->i_planes; i_plane++ )
665             {
666                 const int i_div_w = p_sys->p_chroma->pi_div_w[i_plane];
667                 const int i_div_h = p_sys->p_chroma->pi_div_h[i_plane];
668
669                 if( !i_div_w || !i_div_h )
670                     continue;
671
672                 const plane_t *p_srcp = &p_src->p[i_plane];
673                 const plane_t *p_dstp = &p_dst->p[i_plane];
674
675                 /* */
676                 panoramix_filter_t filter;
677                 filter.black.i_right  = p_output->filter.black.i_right / i_div_w;
678                 filter.black.i_left   = p_output->filter.black.i_left / i_div_w;
679                 filter.black.i_top    = p_output->filter.black.i_top / i_div_h;
680                 filter.black.i_bottom = p_output->filter.black.i_bottom / i_div_h;
681
682                 filter.attenuate.i_right  = p_output->filter.attenuate.i_right / i_div_w;
683                 filter.attenuate.i_left   = p_output->filter.attenuate.i_left / i_div_w;
684                 filter.attenuate.i_top    = p_output->filter.attenuate.i_top / i_div_h;
685                 filter.attenuate.i_bottom = p_output->filter.attenuate.i_bottom / i_div_h;
686
687                 /* */
688                 const int i_x = p_output->i_src_x/i_div_w;
689                 const int i_y = p_output->i_src_y/i_div_h;
690
691                 assert( p_sys->p_chroma->b_planar );
692                 FilterPlanar( p_dstp->p_pixels, p_dstp->i_pitch,
693                               &p_srcp->p_pixels[i_y * p_srcp->i_pitch + i_x * p_srcp->i_pixel_pitch], p_srcp->i_pitch,
694                               p_output->i_src_width/i_div_w, p_output->i_src_height/i_div_h,
695                               p_sys->p_chroma->pi_black[i_plane],
696                               &filter,
697                               p_sys->p_lut[i_plane],
698                               p_sys->lambdav[i_plane],
699                               p_sys->lambdah[i_plane] );
700             }
701         }
702     }
703
704     picture_Release( p_src );
705     return VLC_SUCCESS;
706 }
707
708 /**
709  * It converts mouse events
710  */
711 static int Mouse( video_splitter_t *p_splitter, vlc_mouse_t *p_mouse,
712                   int i_index,
713                   const vlc_mouse_t *p_old, const vlc_mouse_t *p_new )
714 {
715     video_splitter_sys_t *p_sys = p_splitter->p_sys;
716     VLC_UNUSED(p_old);
717
718     for( int y = 0; y < p_sys->i_row; y++ )
719     {
720         for( int x = 0; x < p_sys->i_col; x++ )
721         {
722             const panoramix_output_t *p_output = p_output = &p_sys->pp_output[x][y];
723             if( p_output->b_active && p_output->i_output == i_index )
724             {
725                 const int i_x = p_new->i_x - p_output->filter.black.i_left;
726                 const int i_y = p_new->i_y - p_output->filter.black.i_top;
727                 if( i_x >= 0 && i_x < p_output->i_width  - p_output->filter.black.i_right &&
728                     i_y >= 0 && i_y < p_output->i_height - p_output->filter.black.i_bottom )
729                 {
730                     *p_mouse = *p_new;
731                     p_mouse->i_x = p_output->i_src_x + i_x;
732                     p_mouse->i_y = p_output->i_src_y + i_y;
733                     return VLC_SUCCESS;
734                 }
735             }
736         }
737     }
738     return VLC_EGENERIC;
739 }
740
741 /* It return a coefficient between 0.0 and 1.0 to be applied to the given
742  * value
743  */
744 static double GammaFactor( const panoramix_gamma_t *g, float f_value )
745 {
746     if( f_value <= g->f_black_crush )
747     {
748         float f_input = f_value * g->f_black_level / g->f_black_crush + (1.0 - g->f_black_level);
749
750         return pow( f_input, 1.0 / g->f_gamma );
751     }
752     else if( f_value >= g->f_white_crush )
753     {
754         float f_input = (f_value * (1.0 - (g->f_white_level + 1.0)) + (g->f_white_level + 1.0) * g->f_white_crush - 1.0) / (g->f_white_crush - 1.0);
755         return pow( f_input, 1.0 / g->f_gamma );
756     }
757     return 1.0;
758 }
759
760 /**
761  * It creates the panoramix configuration
762  */
763 static int Configuration( panoramix_output_t pp_output[ROW_MAX][COL_MAX],
764                           int i_col, int i_row,
765                           int i_src_width, int i_src_height,
766                           int i_half_w, int i_half_h,
767                           bool b_attenuate,
768                           const bool *pb_active )
769 {
770 #ifdef OVERLAP
771     const bool b_overlap = true;
772 #else
773     const bool b_overlap = false;
774 #endif
775
776     /* */
777     int i_output = 0;
778     for( int y = 0, i_src_y = 0, i_dst_y = 0; y < i_row; y++ )
779     {
780         const bool b_row_first = y == 0;
781         const bool b_row_last  = y == i_row - 1;
782
783         /* Compute source height */
784         int i_win_height = (i_src_height / i_row ) & ~1;
785         if( b_row_last )
786             i_win_height = i_src_height - y * i_win_height;
787
788         for( int x = 0, i_src_x = 0, i_dst_x = 0; x < i_col; x++ )
789         {
790             const bool b_col_first = x == 0;
791             const bool b_col_last  = x == i_col - 1;
792
793             /* Compute source width */
794             int i_win_width  = (i_src_width  / i_col ) & ~1;
795             if( b_col_last )
796                 i_win_width  = i_src_width  - x * i_win_width;
797
798             /* Compute filter configuration */
799             panoramix_filter_t cfg;
800
801             memset( &cfg, 0, sizeof(cfg) );
802             if( b_overlap && b_attenuate )
803             {
804                 if( i_col > 2 )
805                 {
806                     if( b_col_first )
807                         cfg.black.i_left   = i_half_w;
808                     if( b_col_last )
809                         cfg.black.i_right  = i_half_w;
810                 }
811                 if( i_row > 2 )
812                 {
813                     if( b_row_first )
814                         cfg.black.i_top    = i_half_h;
815                     if( b_row_last )
816                         cfg.black.i_bottom = i_half_h;
817                 }
818                 if( !b_col_first )
819                     cfg.attenuate.i_left   = 2 * i_half_w;
820                 if( !b_col_last )
821                     cfg.attenuate.i_right  = 2 * i_half_w;
822                 if( !b_row_first )
823                     cfg.attenuate.i_top    = 2 * i_half_h;
824                 if( !b_row_last )
825                     cfg.attenuate.i_bottom = 2 * i_half_h;
826             }
827
828             /* Compute alignment */
829             int i_align = 0;
830             if( i_col > 1 )
831             {
832                 if( b_col_first )
833                     i_align |= VOUT_ALIGN_RIGHT;
834                 if( b_col_last )
835                     i_align |= VOUT_ALIGN_LEFT;
836             }
837             if( i_row > 1 )
838             {
839                 if( b_row_first )
840                     i_align |= VOUT_ALIGN_BOTTOM;
841                 if( b_row_last )
842                     i_align |= VOUT_ALIGN_TOP;
843             }
844
845             /* */
846             panoramix_output_t *p_output = &pp_output[x][y];
847
848             /* */
849             p_output->i_src_x = i_src_x - cfg.attenuate.i_left/2;
850             p_output->i_src_y = i_src_y - cfg.attenuate.i_top/2;
851             p_output->i_src_width  = i_win_width  + cfg.attenuate.i_left/2 + cfg.attenuate.i_right/2;
852             p_output->i_src_height = i_win_height + cfg.attenuate.i_top/2  + cfg.attenuate.i_bottom/2;
853
854             /* */
855             p_output->filter = cfg;
856
857             /* */
858             p_output->i_align = i_align;
859             p_output->i_x = i_dst_x;
860             p_output->i_y = i_dst_y;
861
862             p_output->i_width  = cfg.black.i_left + p_output->i_src_width  + cfg.black.i_right;
863             p_output->i_height = cfg.black.i_top  + p_output->i_src_height + cfg.black.i_bottom;
864
865             /* */
866             p_output->b_active = pb_active[y * i_col + x] &&
867                                  p_output->i_width > 0 &&
868                                  p_output->i_height > 0;
869             if( p_output->b_active )
870                 p_output->i_output = i_output++;
871
872             /* */
873             i_src_x += i_win_width;
874             i_dst_x += p_output->i_width;
875         }
876         i_src_y += i_win_height;
877         i_dst_y += pp_output[0][y].i_height;
878     }
879     return i_output;
880 }
881
882 /**
883  * It filters a video plane
884  */
885 static void FilterPlanar( uint8_t *p_out, int i_out_pitch,
886                           const uint8_t *p_in, int i_in_pitch,
887                           int i_copy_pitch,
888                           int i_copy_lines,
889                           int i_pixel_black,
890                           const panoramix_filter_t *p_cfg,
891                           uint8_t p_lut[ACCURACY + 1][256],
892                           int lambdav[2][ACCURACY/2],
893                           int lambdah[2][ACCURACY/2] )
894 {
895     /* */
896     assert( !p_cfg->black.i_left   || !p_cfg->attenuate.i_left );
897     assert( !p_cfg->black.i_right  || !p_cfg->attenuate.i_right );
898     assert( !p_cfg->black.i_top    || !p_cfg->attenuate.i_top );
899     assert( !p_cfg->black.i_bottom || !p_cfg->attenuate.i_bottom );
900
901     const int i_out_width = p_cfg->black.i_left + i_copy_pitch + p_cfg->black.i_right;
902
903     /* Top black border */
904     for( int b = 0; b < p_cfg->black.i_top; b++ )
905     {
906         vlc_memset( p_out, i_pixel_black, i_out_width );
907         p_out += i_out_pitch;
908     }
909
910     for( int y = 0; y < i_copy_lines; y++ )
911     {
912         const uint8_t *p_src = p_in;
913         uint8_t *p_dst = p_out;
914
915         /* Black border on the left */
916         if( p_cfg->black.i_left > 0 )
917         {
918             memset( p_dst, i_pixel_black, p_cfg->black.i_left );
919             p_dst += p_cfg->black.i_left;
920         }
921         /* Attenuated video on the left */
922         for( int i = 0; i < p_cfg->attenuate.i_left; i++ )
923             *p_dst++ = p_lut[lambdav[0][i]][*p_src++];
924
925         /* Unmodified video */
926         const int i_unmodified_width = i_copy_pitch - p_cfg->attenuate.i_left - p_cfg->attenuate.i_right;
927         vlc_memcpy( p_dst, p_src, i_unmodified_width );
928         p_dst += i_unmodified_width;
929         p_src += i_unmodified_width;
930
931         /* Attenuated video on the right */
932         for( int i = 0; i < p_cfg->attenuate.i_right; i++ )
933             *p_dst++ = p_lut[lambdav[1][i]][*p_src++];
934         /* Black border on the right */
935         if( p_cfg->black.i_right > 0 )
936         {
937             memset( p_dst, i_pixel_black, p_cfg->black.i_right );
938             p_dst += p_cfg->black.i_right;
939         }
940
941         /* Attenuate complete line at top/bottom */
942         const bool b_attenuate_top    = y < p_cfg->attenuate.i_top;
943         const bool b_attenuate_bottom = y >= i_copy_lines - p_cfg->attenuate.i_bottom;
944         if( b_attenuate_top || b_attenuate_bottom )
945         {
946             const int i_index = b_attenuate_top ? lambdah[0][y] : lambdah[1][y - (i_copy_lines - p_cfg->attenuate.i_bottom)];
947             uint8_t *p_dst = &p_out[0];
948             for( int i = 0; i < i_out_width; i++)
949                 p_dst[i] = p_lut[i_index][p_dst[i]];
950         }
951
952         /* */
953         p_in  += i_in_pitch;
954         p_out += i_out_pitch;
955     }
956     /* Bottom black border */
957     for( int b = 0; b < p_cfg->black.i_bottom; b++ )
958     {
959         vlc_memset( p_out, i_pixel_black, i_out_width );
960         p_out += i_out_pitch;
961     }
962 }
963
964
965
966 #if 0
967 static void RenderPackedRGB( vout_thread_t *p_vout, picture_t *p_pic )
968 {
969     int length;
970     length = 2 * p_sys->i_overlap_w2 * p_pic->p->i_pixel_pitch;
971
972     if (p_sys->b_has_changed)
973     {
974         int i_plane_;
975         int i_col_mod;
976         Denom = F2(length / p_pic->p->i_pixel_pitch);
977         a_2 = p_sys->a_2 * (ACCURACY / 100);
978         a_1 = p_sys->a_1 * 2 * p_sys->i_overlap_w2 * (ACCURACY / 100);
979         a_0 = p_sys->a_0 * Denom * (ACCURACY / 100);
980         for(i_col_mod = 0; i_col_mod < 2; i_col_mod++)
981             for (i_index = 0; i_index < length / p_pic->p->i_pixel_pitch; i_index++)
982                 for (i_plane_ =  0; i_plane_ < p_pic->p->i_pixel_pitch; i_plane_++)
983                     p_sys->lambda[i_col_mod][i_plane_][i_index] = clip_accuracy(!i_col_mod ? ACCURACY - (F4(a_2, a_1, i_index) + a_0) / Denom : ACCURACY - (F4(a_2, a_1,(length / p_pic->p->i_pixel_pitch) - i_index) + a_0) / Denom);
984     }
985
986     length = 2 * p_sys->i_overlap_h2;
987     if (p_sys->b_has_changed)
988     {
989         int i_plane_;
990         int i_row_mod;
991         Denom = F2(length);
992         a_2 = p_sys->a_2 * (ACCURACY / 100);
993         a_1 = p_sys->a_1 * length * (ACCURACY / 100);
994         a_0 = p_sys->a_0 * Denom * (ACCURACY / 100);
995         for(i_row_mod = 0; i_row_mod < 2; i_row_mod++)
996           for (i_index = 0; i_index < length; i_index++)
997             for (i_plane_ =  0; i_plane_ < p_pic->p->i_pixel_pitch; i_plane_++)
998                 p_sys->lambda2[i_row_mod][i_plane_][i_index] = clip_accuracy(!i_row_mod ? ACCURACY - (F4(a_2, a_1, i_index) + a_0) / Denom : ACCURACY - (F4(a_2, a_1,(length) - i_index) + a_0) / Denom);
999     }
1000 }
1001 #endif
1002
1003