]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/mosaic.c
Fix unsigned int vs int compilation warning.
[vlc] / modules / video_filter / mosaic.c
index bbe258da1f0104dd5bf0f546673edeed14018514..a4dbfc909e3c8202d317fa55ff42076b1e42cc88 100644 (file)
@@ -19,7 +19,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
 
 /*****************************************************************************
@@ -75,6 +75,9 @@ struct filter_sys_t
     int i_vborder, i_hborder; /* border width/height between miniatures */
     int i_alpha; /* subfilter alpha blending */
 
+    vlc_bool_t b_bs; /* Bluescreen vars */
+    int i_bsu, i_bsv, i_bsut, i_bsvt;
+
     char **ppsz_order; /* list of picture-id */
     int i_order_length;
 
@@ -111,6 +114,12 @@ struct filter_sys_t
         "will be delayed accordingly (in milliseconds). For high " \
         "values you will need to raise file-caching and others.")
 
+#define BLUESCREEN_TEXT N_("Enable bluescreen (aka greenscreen or chroma key) video background replacing")
+#define BLUESCREENU_TEXT N_("Bluescreen chroma key U (0-255)")
+#define BLUESCREENV_TEXT N_("Bluescreen chroma key V (0-255)")
+#define BLUESCREENUTOL_TEXT N_("Bluescreen chroma key U tolerance")
+#define BLUESCREENVTOL_TEXT N_("Bluescreen chroma key V tolerance")
+
 static int pi_pos_values[] = { 0, 1 };
 static char * ppsz_pos_descriptions[] =
 { N_("auto"), N_("fixed") };
@@ -150,6 +159,17 @@ vlc_module_begin();
     add_integer( "mosaic-delay", 0, NULL, DELAY_TEXT, DELAY_LONGTEXT,
                  VLC_FALSE );
 
+    add_bool( "mosaic-bs", 0, NULL, BLUESCREEN_TEXT,
+              BLUESCREEN_TEXT, VLC_FALSE );
+    add_integer( "mosaic-bsu", 120, NULL, BLUESCREENU_TEXT,
+                 BLUESCREENU_TEXT, VLC_FALSE );
+    add_integer( "mosaic-bsv", 90, NULL, BLUESCREENV_TEXT,
+                 BLUESCREENV_TEXT, VLC_FALSE );
+    add_integer( "mosaic-bsut", 17, NULL, BLUESCREENUTOL_TEXT,
+                 BLUESCREENUTOL_TEXT, VLC_FALSE );
+    add_integer( "mosaic-bsvt", 17, NULL, BLUESCREENVTOL_TEXT,
+                 BLUESCREENVTOL_TEXT, VLC_FALSE );
+
     var_Create( p_module->p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
 vlc_module_end();
 
@@ -238,7 +258,7 @@ static int CreateFilter( vlc_object_t *p_this )
         char *psz_end = NULL;
         i_index = 0;
         do
-        { 
+        {
             psz_end = strchr( psz_order, ',' );
             i_index++;
             p_sys->ppsz_order = realloc( p_sys->ppsz_order,
@@ -250,6 +270,22 @@ static int CreateFilter( vlc_object_t *p_this )
         p_sys->i_order_length = i_index;
     }
 
+    /* Bluescreen specific stuff */
+    GET_VAR( bsu, 0x00, 0xff );
+    GET_VAR( bsv, 0x00, 0xff );
+    GET_VAR( bsut, 0x00, 0xff );
+    GET_VAR( bsvt, 0x00, 0xff );
+    p_sys->b_bs = var_CreateGetBool( p_filter, "mosaic-bs" );
+    var_Destroy( p_filter, "mosaic-bs" );
+    var_Create( p_libvlc, "mosaic-bs", VLC_VAR_INTEGER );
+    var_SetBool( p_libvlc, "mosaic-bs", p_sys->b_bs );
+    var_AddCallback( p_libvlc, "mosaic-bs", MosaicCallback, p_sys );
+    if( p_sys->b_bs && p_sys->b_keep )
+    {
+        msg_Warn( p_filter, "mosaic-keep-picture needs to be disabled for"
+                            " bluescreen to work" );
+    }
+
     vlc_mutex_unlock( &p_sys->lock );
 
     return VLC_SUCCESS;
@@ -294,6 +330,12 @@ static void DestroyFilter( vlc_object_t *p_this )
     var_Destroy( p_libvlc, "mosaic-cols" );
     var_Destroy( p_libvlc, "mosaic-keep-aspect-ratio" );
 
+    var_Destroy( p_libvlc, "mosaic-bsu" );
+    var_Destroy( p_libvlc, "mosaic-bsv" );
+    var_Destroy( p_libvlc, "mosaic-bsut" );
+    var_Destroy( p_libvlc, "mosaic-bsvt" );
+    var_Destroy( p_libvlc, "mosaic-bs" );
+
     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
     vlc_mutex_unlock( &p_sys->lock );
     vlc_mutex_destroy( &p_sys->lock );
@@ -323,6 +365,8 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
     int i_index, i_real_index, i_row, i_col;
     int i_greatest_real_index_used = p_sys->i_order_length - 1;
 
+    unsigned int col_inner_width, row_inner_height;
+
     subpicture_region_t *p_region;
     subpicture_region_t *p_region_prev = NULL;
 
@@ -385,6 +429,11 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
                             i_numpics / p_sys->i_rows + 1 );
     }
 
+    col_inner_width  = ( ( p_sys->i_width - ( p_sys->i_cols - 1 )
+                       * p_sys->i_vborder ) / p_sys->i_cols );
+    row_inner_height = ( ( p_sys->i_height - ( p_sys->i_rows - 1 )
+                       * p_sys->i_hborder ) / p_sys->i_rows );
+
     i_real_index = 0;
 
     for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
@@ -456,12 +505,9 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
             fmt_in.i_width = p_es->p_picture->format.i_width;
 
             fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
-            fmt_out.i_width = fmt_in.i_width *
-                ( ( p_sys->i_width - ( p_sys->i_cols - 1 ) * p_sys->i_vborder )
-                  / p_sys->i_cols ) / fmt_in.i_width;
-            fmt_out.i_height = fmt_in.i_height *
-                ( ( p_sys->i_height - ( p_sys->i_rows - 1 ) * p_sys->i_hborder )
-                  / p_sys->i_rows ) / fmt_in.i_height;
+            fmt_out.i_width = col_inner_width;
+            fmt_out.i_height = row_inner_height;
+
             if( p_sys->b_ar ) /* keep aspect ratio */
             {
                 if( (float)fmt_out.i_width / (float)fmt_out.i_height
@@ -488,6 +534,86 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
                            "image resizing and chroma conversion failed" );
                 continue;
             }
+
+            /* Bluescreen stuff */
+            if( p_sys->b_bs )
+            {
+                int i,j;
+                int i_lines = p_converted->p[ A_PLANE ].i_lines;
+                int i_pitch = p_converted->p[ A_PLANE ].i_pitch;
+                uint8_t *p_a = p_converted->p[ A_PLANE ].p_pixels;
+                uint8_t *p_at = malloc( i_lines * i_pitch * sizeof( uint8_t ) );
+                uint8_t *p_u = p_converted->p[ U_PLANE ].p_pixels;
+                uint8_t *p_v = p_converted->p[ V_PLANE ].p_pixels;
+                uint8_t umin, umax, vmin, vmax;
+                umin = p_sys->i_bsu - p_sys->i_bsut >= 0x00 ?
+                       p_sys->i_bsu - p_sys->i_bsut : 0x00;
+                umax = p_sys->i_bsu + p_sys->i_bsut <= 0xff ?
+                       p_sys->i_bsu + p_sys->i_bsut : 0xff;
+                vmin = p_sys->i_bsv - p_sys->i_bsvt >= 0x00 ?
+                       p_sys->i_bsv - p_sys->i_bsvt : 0x00;
+                vmax = p_sys->i_bsv + p_sys->i_bsvt <= 0xff ?
+                       p_sys->i_bsv + p_sys->i_bsvt : 0xff;
+
+                for( i = 0; i < i_lines*i_pitch; i++ )
+                {
+                    if(    p_u[i] < umax
+                        && p_u[i] > umin
+                        && p_v[i] < vmax
+                        && p_v[i] > vmin )
+                    {
+                        p_at[i] = 0x00;
+                    }
+                    else
+                    {
+                        p_at[i] = 0xff;
+                    }
+                }
+                /* Gaussian convolution to make it look cleaner */
+                memset( p_a, 0, 2 * i_pitch );
+                for( i = 2; i < i_lines - 2; i++ )
+                {
+                    p_a[i*i_pitch] = 0x00;
+                    p_a[i*i_pitch+1] = 0x00;
+                    for( j = 2; j < i_pitch - 2; j ++ )
+                    {
+                        p_a[i*i_pitch+j] = (uint8_t)((
+                          /* 2 rows up */
+                            ( p_at[(i-2)*i_pitch+j-2]<<1 )
+                          + ( p_at[(i-2)*i_pitch+j-1]<<2 )
+                          + ( p_at[(i-2)*i_pitch+j]<<2 )
+                          + ( p_at[(i-2)*i_pitch+j+1]<<2 )
+                          + ( p_at[(i-2)*i_pitch+j+2]<<1 )
+                          /* 1 row up */
+                          + ( p_at[(i-1)*i_pitch+j-1]<<3 )
+                          + ( p_at[(i-1)*i_pitch+j-2]<<2 )
+                          + ( p_at[(i-1)*i_pitch+j]*12 )
+                          + ( p_at[(i-1)*i_pitch+j+1]<<3 )
+                          + ( p_at[(i-1)*i_pitch+j+2]<<2 )
+                          /* */
+                          + ( p_at[i*i_pitch+j-2]<<2 )
+                          + ( p_at[i*i_pitch+j-1]*12 )
+                          + ( p_at[i*i_pitch+j]<<4 )
+                          + ( p_at[i*i_pitch+j+1]*12 )
+                          + ( p_at[i*i_pitch+j+2]<<2 )
+                          /* 1 row down */
+                          + ( p_at[(i+1)*i_pitch+j-2]<<2 )
+                          + ( p_at[(i+1)*i_pitch+j-1]<<3 )
+                          + ( p_at[(i+1)*i_pitch+j]*12 )
+                          + ( p_at[(i+1)*i_pitch+j+1]<<3 )
+                          + ( p_at[(i+1)*i_pitch+j+2]<<2 )
+                          /* 2 rows down */
+                          + ( p_at[(i+2)*i_pitch+j-2]<<1 )
+                          + ( p_at[(i+2)*i_pitch+j-1]<<2 )
+                          + ( p_at[(i+2)*i_pitch+j]<<2 )
+                          + ( p_at[(i+2)*i_pitch+j+1]<<2 )
+                          + ( p_at[(i+2)*i_pitch+j+2]<<1 )
+                          )/152);
+                          if( p_a[i*i_pitch+j] < 0xbf ) p_a[i*i_pitch+j] = 0x00;
+                    }
+                }
+                free( p_at );
+            }
         }
         else
         {
@@ -525,32 +651,40 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
             p_region->picture.pf_release = MosaicReleasePicture;
         }
 
-        if( p_sys->b_ar || p_sys->b_keep ) /* keep aspect ratio */
+        if( fmt_out.i_width > col_inner_width ||
+            p_sys->b_ar || p_sys->b_keep )
         {
-            /* center the video in the dedicated rectangle */
+            /* we don't have to center the video since it takes the
+            whole rectangle area or it's larger than the rectangle */
             p_region->i_x = p_sys->i_xoffset
                         + i_col * ( p_sys->i_width / p_sys->i_cols )
-                        + ( i_col * p_sys->i_vborder ) / p_sys->i_cols
-                        + ( ( ( p_sys->i_width
-                                 - ( p_sys->i_cols - 1 ) * p_sys->i_vborder )
-                            / p_sys->i_cols ) - fmt_out.i_width ) / 2;
-            p_region->i_y = p_sys->i_yoffset
-                        + i_row * ( p_sys->i_height / p_sys->i_rows )
-                        + ( i_row * p_sys->i_hborder ) / p_sys->i_rows
-                        + ( ( ( p_sys->i_height
-                                 - ( p_sys->i_rows - 1 ) * p_sys->i_hborder )
-                            / p_sys->i_rows ) - fmt_out.i_height ) / 2;
+                        + ( i_col * p_sys->i_vborder ) / p_sys->i_cols;
         }
         else
         {
-            /* we don't have to center the video since it takes the
-            whole rectangle area */
+            /* center the video in the dedicated rectangle */
             p_region->i_x = p_sys->i_xoffset
-                            + i_col * ( p_sys->i_width / p_sys->i_cols )
-                            + ( i_col * p_sys->i_vborder ) / p_sys->i_cols;
+                    + i_col * ( p_sys->i_width / p_sys->i_cols )
+                    + ( i_col * p_sys->i_vborder ) / p_sys->i_cols
+                    + ( col_inner_width - fmt_out.i_width ) / 2;
+        }
+
+        if( fmt_out.i_height < row_inner_height
+            || p_sys->b_ar || p_sys->b_keep )
+        {
+            /* we don't have to center the video since it takes the
+            whole rectangle area or it's taller than the rectangle */
+            p_region->i_y = p_sys->i_yoffset
+                    + i_row * ( p_sys->i_height / p_sys->i_rows )
+                    + ( i_row * p_sys->i_hborder ) / p_sys->i_rows;
+        }
+        else
+        {
+            /* center the video in the dedicated rectangle */
             p_region->i_y = p_sys->i_yoffset
-                        + i_row * ( p_sys->i_height / p_sys->i_rows )
-                        + ( i_row * p_sys->i_hborder ) / p_sys->i_rows;
+                    + i_row * ( p_sys->i_height / p_sys->i_rows )
+                    + ( i_row * p_sys->i_hborder ) / p_sys->i_rows
+                    + ( row_inner_height - fmt_out.i_height ) / 2;
         }
 
         if( p_region_prev == NULL )