]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/mosaic.c
shame on me ... *cough* *cough*
[vlc] / modules / video_filter / mosaic.c
index 40fd66c8628d9e1f0b0e197fca2d80db61ad0c13..19d6111acd2f7abc8aaa094444a7b825483c1e45 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * mosaic.c : Mosaic video plugin for vlc
  *****************************************************************************
- * Copyright (C) 2004-2005 VideoLAN
+ * Copyright (C) 2004-2005 the VideoLAN team
  * $Id$
  *
  * Authors: Antoine Cellerier <dionoea@via.ecp.fr>
@@ -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.
 *****************************************************************************/
 
 /*****************************************************************************
@@ -39,7 +39,9 @@
 #include "vlc_filter.h"
 #include "vlc_image.h"
 
-#include "../video_output/picture.h"
+#include "mosaic.h"
+
+#define BLANK_DELAY I64C(1000000)
 
 /*****************************************************************************
  * Local prototypes
@@ -58,11 +60,9 @@ static int MosaicCallback( vlc_object_t *, char const *, vlc_value_t,
 struct filter_sys_t
 {
     vlc_mutex_t lock;
+    vlc_mutex_t *p_lock;
 
     image_handler_t *p_image;
-#ifdef IMAGE_2PASSES
-    image_handler_t *p_image2;
-#endif
     picture_t *p_pic;
 
     int i_position; /* mosaic positioning method */
@@ -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;
 
@@ -96,8 +99,8 @@ struct filter_sys_t
 #define ALIGN_TEXT N_("Mosaic alignment")
 
 #define POS_TEXT N_("Positioning method")
-#define POS_LONGTEXT N_("Positioning method. auto : automatically choose " \
-        "the best number of rows and columns. fixed : use the user-defined " \
+#define POS_LONGTEXT N_("Positioning method. auto: automatically choose " \
+        "the best number of rows and columns. fixed: use the user-defined " \
         "number of rows and columns.")
 #define ROWS_TEXT N_("Number of rows")
 #define COLS_TEXT N_("Number of columns")
@@ -108,9 +111,15 @@ struct filter_sys_t
 
 #define DELAY_TEXT N_("Delay")
 #define DELAY_LONGTEXT N_("Pictures coming from the picture video outputs " \
-        "will be delayed accordingly (in milliseconds, >= 100 ms). For high " \
+        "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") };
@@ -124,6 +133,8 @@ static char *ppsz_align_descriptions[] =
 vlc_module_begin();
     set_description( N_("Mosaic video sub filter") );
     set_shortname( N_("Mosaic") );
+    set_category( CAT_VIDEO );
+    set_subcategory( SUBCAT_VIDEO_SUBPIC);
     set_capability( "sub filter", 0 );
     set_callbacks( CreateFilter, DestroyFilter );
 
@@ -145,8 +156,21 @@ vlc_module_begin();
     add_bool( "mosaic-keep-picture", 0, NULL, KEEP_TEXT, KEEP_TEXT, VLC_FALSE );
     add_string( "mosaic-order", "", NULL, ORDER_TEXT, ORDER_TEXT, VLC_FALSE );
 
-    add_integer( "mosaic-delay", 100, NULL, DELAY_TEXT, DELAY_LONGTEXT,
+    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();
 
 
@@ -160,6 +184,10 @@ static int CreateFilter( vlc_object_t *p_this )
     libvlc_t *p_libvlc = p_filter->p_libvlc;
     char *psz_order;
     int i_index;
+    vlc_value_t val;
+
+    /* The mosaic thread is more important than the decoder threads */
+    vlc_thread_set_priority( p_this, VLC_THREAD_PRIORITY_OUTPUT );
 
     /* Allocate structure */
     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
@@ -175,6 +203,9 @@ static int CreateFilter( vlc_object_t *p_this )
     vlc_mutex_init( p_filter, &p_sys->lock );
     vlc_mutex_lock( &p_sys->lock );
 
+    var_Get( p_libvlc, "mosaic-lock", &val );
+    p_sys->p_lock = val.p_address;
+
 #define GET_VAR( name, min, max )                                           \
     p_sys->i_##name = __MIN( max, __MAX( min,                               \
                 var_CreateGetInteger( p_filter, "mosaic-" #name ) ) );      \
@@ -216,9 +247,6 @@ static int CreateFilter( vlc_object_t *p_this )
     if ( !p_sys->b_keep )
     {
         p_sys->p_image = image_HandlerCreate( p_filter );
-#ifdef IMAGE_2PASSES
-        p_sys->p_image2 = image_HandlerCreate( p_filter );
-#endif
     }
 
     p_sys->i_order_length = 0;
@@ -230,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,
@@ -242,9 +270,23 @@ static int CreateFilter( vlc_object_t *p_this )
         p_sys->i_order_length = i_index;
     }
 
-    vlc_mutex_unlock( &p_sys->lock );
+    /* 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_thread_set_priority( p_filter, VLC_THREAD_PRIORITY_OUTPUT );
+    vlc_mutex_unlock( &p_sys->lock );
 
     return VLC_SUCCESS;
 }
@@ -264,9 +306,6 @@ static void DestroyFilter( vlc_object_t *p_this )
     if( !p_sys->b_keep )
     {
         image_HandlerDelete( p_sys->p_image );
-#ifdef IMAGE_2PASSES
-        image_HandlerDelete( p_sys->p_image2 );
-#endif
     }
 
     if( p_sys->i_order_length )
@@ -291,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 );
@@ -313,7 +358,7 @@ static void MosaicReleasePicture( picture_t *p_picture )
 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
 {
     filter_sys_t *p_sys = p_filter->p_sys;
-    libvlc_t *p_libvlc = p_filter->p_libvlc;
+    bridge_t *p_bridge;
 
     subpicture_t *p_spu;
 
@@ -323,26 +368,6 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
     subpicture_region_t *p_region;
     subpicture_region_t *p_region_prev = NULL;
 
-    picture_vout_t *p_picture_vout;
-    vlc_value_t val, lockval;
-
-    /* Wait for the specified date. This is to avoid running too fast and
-     * take twice the same pictures. */
-    mwait( date - p_sys->i_delay );
-
-    if ( var_Get( p_libvlc, "picture-lock", &lockval ) )
-        return NULL;
-
-    vlc_mutex_lock( lockval.p_address );
-
-    if( var_Get( p_libvlc, "p_picture_vout", &val ) )
-    {
-        vlc_mutex_unlock( lockval.p_address );
-        return NULL;
-    }
-
-    p_picture_vout = val.p_address;
-
     /* Allocate the subpicture internal data. */
     p_spu = p_filter->pf_sub_buffer_new( p_filter );
     if( !p_spu )
@@ -360,28 +385,33 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
     p_spu->b_absolute = VLC_FALSE;
 
     vlc_mutex_lock( &p_sys->lock );
+    vlc_mutex_lock( p_sys->p_lock );
 
-    if( p_sys->i_position == 0 ) /* use automatic positioning */
+    p_bridge = GetBridge( p_filter );
+    if ( p_bridge == NULL )
+    {
+        vlc_mutex_unlock( p_sys->p_lock );
+        vlc_mutex_unlock( &p_sys->lock );
+        return p_spu;
+    }
+
+    if ( p_sys->i_position == 0 ) /* use automatic positioning */
     {
         int i_numpics = p_sys->i_order_length; /* keep slots and all */
-        for( i_index = 0;
-             i_index < p_picture_vout->i_picture_num;
-             i_index++ )
+        for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
         {
-            if( p_picture_vout->p_pic[i_index].i_status
-                           == PICTURE_VOUT_E_OCCUPIED )
+            bridged_es_t *p_es = p_bridge->pp_es[i_index];
+            if ( !p_es->b_empty )
             {
                 i_numpics ++;
-                if( p_sys->i_order_length
-                    && p_picture_vout->p_pic[i_index].psz_id != 0 )
+                if( p_sys->i_order_length && p_es->psz_id != 0 )
                 {
                     /* We also want to leave slots for images given in
                      * mosaic-order that are not available in p_vout_picture */
                     int i;
                     for( i = 0; i < p_sys->i_order_length ; i++ )
                     {
-                        if( !strcmp( p_sys->ppsz_order[i],
-                                     p_picture_vout->p_pic[i_index].psz_id ) )
+                        if( !strcmp( p_sys->ppsz_order[i], p_es->psz_id ) )
                         {
                             i_numpics--;
                             break;
@@ -399,50 +429,73 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
 
     i_real_index = 0;
 
-    for( i_index = 0; i_index < p_picture_vout->i_picture_num; i_index++ )
+    for ( i_index = 0; i_index < p_bridge->i_es_num; i_index++ )
     {
-        picture_vout_e_t *p_pic = &p_picture_vout->p_pic[i_index];
+        bridged_es_t *p_es = p_bridge->pp_es[i_index];
         video_format_t fmt_in = {0}, fmt_out = {0};
         picture_t *p_converted;
-#ifdef IMAGE_2PASSES
-        video_format_t fmt_middle = {0};
-        picture_t *p_middle;
-#endif
 
-        if( p_pic->i_status == PICTURE_VOUT_E_AVAILABLE
-             || p_pic->p_picture == NULL )
-        {
+        if ( p_es->b_empty )
             continue;
+
+        while ( p_es->p_picture != NULL
+                 && p_es->p_picture->date + p_sys->i_delay < date )
+        {
+            if ( p_es->p_picture->p_next != NULL )
+            {
+                picture_t *p_next = p_es->p_picture->p_next;
+                p_es->p_picture->pf_release( p_es->p_picture );
+                p_es->p_picture = p_next;
+            }
+            else if ( p_es->p_picture->date + p_sys->i_delay + BLANK_DELAY <
+                        date )
+            {
+                /* Display blank */
+                p_es->p_picture->pf_release( p_es->p_picture );
+                p_es->p_picture = NULL;
+                p_es->pp_last = &p_es->p_picture;
+                break;
+            }
+            else
+            {
+                msg_Dbg( p_filter, "too late picture for %s (" I64Fd ")",
+                         p_es->psz_id,
+                         date - p_es->p_picture->date - p_sys->i_delay );
+                break;
+            }
         }
 
-        if( p_sys->i_order_length == 0 )
+        if ( p_es->p_picture == NULL )
+            continue;
+
+        if ( p_sys->i_order_length == 0 )
         {
             i_real_index++;
         }
         else
         {
             int i;
-            for( i = 0; i <= p_sys->i_order_length; i++ )
+            for ( i = 0; i <= p_sys->i_order_length; i++ )
             {
-                if( i == p_sys->i_order_length ) break;
-                if( strcmp( p_pic->psz_id, p_sys->ppsz_order[i] ) == 0 )
+                if ( i == p_sys->i_order_length ) break;
+                if ( strcmp( p_es->psz_id, p_sys->ppsz_order[i] ) == 0 )
                 {
                     i_real_index = i;
                     break;
                 }
             }
-            if( i == p_sys->i_order_length )
+            if ( i == p_sys->i_order_length )
                 i_real_index = ++i_greatest_real_index_used;
         }
         i_row = ( i_real_index / p_sys->i_cols ) % p_sys->i_rows;
         i_col = i_real_index % p_sys->i_cols ;
 
-        if( !p_sys->b_keep )
+        if ( !p_sys->b_keep )
         {
             /* Convert the images */
-            fmt_in.i_chroma = p_pic->p_picture->format.i_chroma;
-            fmt_in.i_height = p_pic->p_picture->format.i_height;
-            fmt_in.i_width = p_pic->p_picture->format.i_width;
+            fmt_in.i_chroma = p_es->p_picture->format.i_chroma;
+            fmt_in.i_height = p_es->p_picture->format.i_height;
+            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 *
@@ -457,42 +510,19 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
                       > (float)fmt_in.i_width / (float)fmt_in.i_height )
                 {
                     fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width )
-                                         / fmt_in.i_height ;
+                                         / fmt_in.i_height;
                 }
                 else
                 {
                     fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height )
-                                        / fmt_in.i_width ;
+                                        / fmt_in.i_width;
                 }
              }
 
             fmt_out.i_visible_width = fmt_out.i_width;
             fmt_out.i_visible_height = fmt_out.i_height;
 
-#ifdef IMAGE_2PASSES
-            fmt_middle.i_chroma = fmt_in.i_chroma;
-            fmt_middle.i_visible_width = fmt_middle.i_width = fmt_out.i_width;
-            fmt_middle.i_visible_height = fmt_middle.i_height = fmt_out.i_height;
-
-            p_middle = image_Convert( p_sys->p_image2, p_pic->p_picture,
-                                      &fmt_in, &fmt_middle );
-            if( !p_middle )
-            {
-                msg_Warn( p_filter, "image resizing failed" );
-                continue;
-            }
-
-            p_converted = image_Convert( p_sys->p_image, p_middle,
-                                         &fmt_middle, &fmt_out );
-            p_middle->pf_release( p_middle );
-
-            if( !p_converted )
-            {
-                msg_Warn( p_filter, "image chroma conversion failed" );
-                continue;
-            }
-#else
-            p_converted = image_Convert( p_sys->p_image, p_pic->p_picture,
+            p_converted = image_Convert( p_sys->p_image, p_es->p_picture,
                                          &fmt_in, &fmt_out );
             if( !p_converted )
             {
@@ -500,11 +530,90 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
                            "image resizing and chroma conversion failed" );
                 continue;
             }
-#endif
+
+            /* 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
         {
-            p_converted = p_pic->p_picture;
+            p_converted = p_es->p_picture;
             p_converted->i_refcount++;
             fmt_in.i_width = fmt_out.i_width = p_converted->format.i_width;
             fmt_in.i_height = fmt_out.i_height = p_converted->format.i_height;
@@ -520,8 +629,8 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
             msg_Err( p_filter, "cannot allocate SPU region" );
             p_filter->pf_sub_buffer_del( p_filter, p_spu );
             vlc_mutex_unlock( &p_sys->lock );
-            vlc_mutex_unlock( lockval.p_address );
-            return NULL;
+            vlc_mutex_unlock( p_sys->p_lock );
+            return p_spu;
         }
 
         /* HACK ALERT : let's fix the pointers to avoid picture duplication.
@@ -578,8 +687,8 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
         p_region_prev = p_region;
     }
 
+    vlc_mutex_unlock( p_sys->p_lock );
     vlc_mutex_unlock( &p_sys->lock );
-    vlc_mutex_unlock( lockval.p_address );
 
     return p_spu;
 }