]> git.sesse.net Git - vlc/blobdiff - src/video_output/vout_subpictures.c
* src/video_output/video_output.c: even uglier fix for the on-the-fly deinterlacing...
[vlc] / src / video_output / vout_subpictures.c
index 3f2785ac2cabb3be63f31fbdb5508c3690e57142..a539d2bcd1a9ecb3a0f119c052231a12c726abd5 100644 (file)
@@ -37,7 +37,6 @@
 #include "video_output.h"
 #include "vlc_spu.h"
 #include "vlc_filter.h"
-#include "osd.h"
 
 /*****************************************************************************
  * Local prototypes
@@ -46,6 +45,8 @@ static void UpdateSPU   ( spu_t *, vlc_object_t * );
 static int  CropCallback( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
 
+static int spu_vaControlDefault( spu_t *, int, va_list );
+
 static subpicture_t *sub_new_buffer( filter_t * );
 static void sub_del_buffer( filter_t *, subpicture_t * );
 static subpicture_t *spu_new_buffer( filter_t * );
@@ -53,6 +54,12 @@ static void spu_del_buffer( filter_t *, subpicture_t * );
 static picture_t *spu_new_video_buffer( filter_t * );
 static void spu_del_video_buffer( filter_t *, picture_t * );
 
+struct filter_owner_sys_t
+{
+    spu_t *p_spu;
+    int i_channel;
+};
+
 /**
  * Creates the subpicture unit
  *
@@ -66,16 +73,16 @@ spu_t *__spu_Create( vlc_object_t *p_this )
     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
     {
         p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
-        p_spu->p_subpicture[i_index].i_type   = EMPTY_SUBPICTURE;
     }
 
     p_spu->p_blend = NULL;
     p_spu->p_text = NULL;
     p_spu->p_scale = NULL;
     p_spu->i_filter = 0;
+    p_spu->pf_control = spu_vaControlDefault;
 
     /* Register the default subpicture channel */
-    p_spu->i_channel = 1;
+    p_spu->i_channel = 2;
 
     vlc_mutex_init( p_this, &p_spu->subpicture_lock );
 
@@ -102,25 +109,42 @@ int spu_Init( spu_t *p_spu )
     var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
     var_Get( p_spu, "sub-filter", &val );
     psz_filter = psz_filter_orig = val.psz_string;
-    if( psz_filter && *psz_filter )
+    while( psz_filter && *psz_filter )
     {
+        char *psz_parser = strchr( psz_filter, ',' );
+        if( !psz_parser ) psz_parser = strchr( psz_filter, ':' );
+
+        if( psz_parser ) *psz_parser++ = 0;
+
         p_spu->pp_filter[p_spu->i_filter] =
             vlc_object_create( p_spu, VLC_OBJECT_FILTER );
         vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
         p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
         p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
-        p_spu->pp_filter[p_spu->i_filter]->p_owner =
-            (filter_owner_sys_t *)p_spu;
         p_spu->pp_filter[p_spu->i_filter]->p_module =
             module_Need( p_spu->pp_filter[p_spu->i_filter],
                          "sub filter", psz_filter, 0 );
-        if( p_spu->pp_filter[p_spu->i_filter]->p_module ) p_spu->i_filter++;
+        if( p_spu->pp_filter[p_spu->i_filter]->p_module )
+        {
+            filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
+            p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
+            spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
+            p_sys->p_spu = p_spu;
+            p_spu->i_filter++;
+        }
         else
         {
             msg_Dbg( p_spu, "no sub filter found" );
             vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
             vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
         }
+
+        if( p_spu->i_filter >= 10 )
+        {
+            msg_Dbg( p_spu, "can't add anymore filters" );
+        }
+
+        psz_filter = psz_parser;
     }
     if( psz_filter_orig ) free( psz_filter_orig );
 
@@ -178,6 +202,7 @@ void spu_Destroy( spu_t *p_spu )
     {
         module_Unneed( p_spu->pp_filter[p_spu->i_filter],
                        p_spu->pp_filter[p_spu->i_filter]->p_module );
+        free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
         vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
         vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
     }
@@ -345,7 +370,6 @@ subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
 
     /* Copy subpicture information, set some default values */
     memset( p_subpic, 0, sizeof(subpicture_t) );
-    p_subpic->i_type     = MEMORY_SUBPICTURE;
     p_subpic->i_status   = RESERVED_SUBPICTURE;
     p_subpic->b_absolute = VLC_TRUE;
     p_subpic->pf_render  = 0;
@@ -413,7 +437,7 @@ void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
                             picture_t *p_pic_dst, picture_t *p_pic_src,
                             subpicture_t *p_subpic,
-                            int i_scale_width, int i_scale_height )
+                            int i_scale_width_orig, int i_scale_height_orig )
 {
     /* Get lock */
     vlc_mutex_lock( &p_spu->subpicture_lock );
@@ -422,6 +446,7 @@ void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
     while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
     {
         subpicture_region_t *p_region = p_subpic->p_region;
+        int i_scale_width, i_scale_height;
 
         /* Load the blending module */
         if( !p_spu->p_blend && p_region )
@@ -459,6 +484,18 @@ void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
                 module_Need( p_spu->p_text, "text renderer", 0, 0 );
         }
 
+        i_scale_width = i_scale_width_orig;
+        i_scale_height = i_scale_height_orig;
+
+        if( p_subpic->i_original_picture_width &&
+            p_subpic->i_original_picture_height )
+        {
+            i_scale_width = i_scale_width * p_fmt->i_width /
+                p_subpic->i_original_picture_width;
+            i_scale_height = i_scale_height * p_fmt->i_height /
+                p_subpic->i_original_picture_height;
+        }
+
         /* Load the scaling module */
         if( !p_spu->p_scale && (i_scale_width != 1000 ||
             i_scale_height != 1000) )
@@ -531,90 +568,14 @@ void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
                 }
             }
 
-            if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
-            {
-                i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
-                    p_subpic->i_y;
-            }
-            else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
-            {
-                i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
-            }
-
-            if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
-            {
-                i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
-                    p_subpic->i_x;
-            }
-            else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
-            {
-                i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
-            }
-
-            if( p_subpic->b_absolute )
-            {
-                i_x_offset = p_region->i_x + p_subpic->i_x;
-                i_y_offset = p_region->i_y + p_subpic->i_y;
-
-                if( p_spu->i_margin >= 0 )
-                {
-                    if( p_subpic->i_height + (unsigned int)p_spu->i_margin <=
-                        p_fmt->i_height )
-                    {
-                        i_y_offset = p_fmt->i_height -
-                            p_spu->i_margin - p_subpic->i_height;
-                    }
-                }
-            }
-
-            /* Force cropping if requested */
-            if( p_spu->b_force_crop )
-            {
-                video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
-
-                /* Find the intersection */
-                if( p_spu->i_crop_x + p_spu->i_crop_width <= i_x_offset ||
-                    i_x_offset + (int)p_fmt->i_visible_width <
-                        p_spu->i_crop_x ||
-                    p_spu->i_crop_y + p_spu->i_crop_height <= i_y_offset ||
-                    i_y_offset + (int)p_fmt->i_visible_height <
-                        p_spu->i_crop_y )
-                {
-                    /* No intersection */
-                    p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
-                }
-                else
-                {
-                    int i_x, i_y, i_x_end, i_y_end;
-                    i_x = __MAX( p_spu->i_crop_x, i_x_offset );
-                    i_y = __MAX( p_spu->i_crop_y, i_y_offset );
-                    i_x_end = __MIN( p_spu->i_crop_x + p_spu->i_crop_width,
-                                   i_x_offset + (int)p_fmt->i_visible_width );
-                    i_y_end = __MIN( p_spu->i_crop_y + p_spu->i_crop_height,
-                                   i_y_offset + (int)p_fmt->i_visible_height );
-
-                    p_fmt->i_x_offset = i_x - i_x_offset;
-                    p_fmt->i_y_offset = i_y - i_y_offset;
-                    p_fmt->i_visible_width = i_x_end - i_x;
-                    p_fmt->i_visible_height = i_y_end - i_y;
-
-                    i_x_offset = i_x;
-                    i_y_offset = i_y;
-                }
-            }
-
             /* Force palette if requested */
             if( p_spu->b_force_alpha && VLC_FOURCC('Y','U','V','P') ==
-                p_spu->p_blend->fmt_in.video.i_chroma )
+                p_region->fmt.i_chroma )
             {
-                p_spu->p_blend->fmt_in.video.p_palette->palette[0][3] =
-                    p_spu->pi_alpha[0];
-                p_spu->p_blend->fmt_in.video.p_palette->palette[1][3] =
-                    p_spu->pi_alpha[1];
-                p_spu->p_blend->fmt_in.video.p_palette->palette[2][3] =
-                    p_spu->pi_alpha[2];
-                p_spu->p_blend->fmt_in.video.p_palette->palette[3][3] =
-                    p_spu->pi_alpha[3];
+                p_region->fmt.p_palette->palette[0][3] = p_spu->pi_alpha[0];
+                p_region->fmt.p_palette->palette[1][3] = p_spu->pi_alpha[1];
+                p_region->fmt.p_palette->palette[2][3] = p_spu->pi_alpha[2];
+                p_region->fmt.p_palette->palette[3][3] = p_spu->pi_alpha[3];
             }
 
             /* Scale SPU if necessary */
@@ -658,6 +619,8 @@ void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
                 p_spu->p_scale->fmt_out.video.i_visible_height =
                     p_region->fmt.i_visible_height * i_scale_height / 1000;
                 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
+                p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
+                p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
 
                 p_pic = p_spu->p_scale->pf_video_filter(
                                  p_spu->p_scale, &p_region->p_cache->picture );
@@ -675,14 +638,84 @@ void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
                 p_region = p_region->p_cache;
             }
 
+            if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
+            {
+                i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
+                    p_subpic->i_y;
+            }
+            else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
+            {
+                i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
+            }
+
+            if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
+            {
+                i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
+                    p_subpic->i_x;
+            }
+            else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
+            {
+                i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
+            }
+
             if( p_subpic->b_absolute )
             {
-                i_x_offset = i_x_offset * i_scale_width / 1000;
-                i_y_offset = i_y_offset * i_scale_height / 1000;
+                i_x_offset = p_region->i_x +
+                    p_subpic->i_x * i_scale_width / 1000;
+                i_y_offset = p_region->i_y +
+                    p_subpic->i_y * i_scale_height / 1000;
+
+                if( p_spu->i_margin >= 0 )
+                {
+                    if( p_subpic->i_height + (unsigned int)p_spu->i_margin <=
+                        p_fmt->i_height )
+                    {
+                        i_y_offset = p_fmt->i_height -
+                            p_spu->i_margin - p_subpic->i_height;
+                    }
+                }
             }
 
             p_spu->p_blend->fmt_in.video = p_region->fmt;
 
+            /* Force cropping if requested */
+            if( p_spu->b_force_crop )
+            {
+                video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
+               int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
+               int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
+               int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
+               int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
+
+                /* Find the intersection */
+                if( i_crop_x + i_crop_width <= i_x_offset ||
+                    i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
+                    i_crop_y + i_crop_height <= i_y_offset ||
+                    i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
+                {
+                    /* No intersection */
+                    p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
+                }
+                else
+                {
+                    int i_x, i_y, i_x_end, i_y_end;
+                    i_x = __MAX( i_crop_x, i_x_offset );
+                    i_y = __MAX( i_crop_y, i_y_offset );
+                    i_x_end = __MIN( i_crop_x + i_crop_width,
+                                   i_x_offset + (int)p_fmt->i_visible_width );
+                    i_y_end = __MIN( i_crop_y + i_crop_height,
+                                   i_y_offset + (int)p_fmt->i_visible_height );
+
+                    p_fmt->i_x_offset = i_x - i_x_offset;
+                    p_fmt->i_y_offset = i_y - i_y_offset;
+                    p_fmt->i_visible_width = i_x_end - i_x;
+                    p_fmt->i_visible_height = i_y_end - i_y;
+
+                    i_x_offset = i_x;
+                    i_y_offset = i_y;
+                }
+            }
+
             /* Update the output picture size */
             p_spu->p_blend->fmt_out.video.i_width =
                 p_spu->p_blend->fmt_out.video.i_visible_width =
@@ -716,10 +749,10 @@ void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
  *****************************************************************************/
 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
 {
-    int i_index;
-    subpicture_t *p_subpic     = NULL;
-    subpicture_t *p_ephemer    = NULL;
-    mtime_t       ephemer_date = 0;
+    int i_index, i_channel;
+    subpicture_t *p_subpic = NULL;
+    subpicture_t *p_ephemer;
+    mtime_t      ephemer_date;
 
     /* Run subpicture filters */
     for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
@@ -735,94 +768,73 @@ subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
 
     /* We get an easily parsable chained list of subpictures which
      * ends with NULL since p_subpic was initialized to NULL. */
-    for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
+    for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
     {
-        if( p_spu->p_subpicture[i_index].i_status == READY_SUBPICTURE )
+        p_ephemer = 0;
+        ephemer_date = 0;
+
+        for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
         {
-            /* If it is a DVD subpicture, check its date */
-            if( p_spu->p_subpicture[i_index].i_type == MEMORY_SUBPICTURE )
+            if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
+                p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
             {
-                if( !p_spu->p_subpicture[i_index].b_ephemer
-                     && display_date > p_spu->p_subpicture[i_index].i_stop )
-                {
-                    /* Too late, destroy the subpic */
-                    spu_DestroySubpicture(p_spu,&p_spu->p_subpicture[i_index]);
-                    continue;
-                }
+                continue;
+            }
 
-                if( display_date
-                     && display_date < p_spu->p_subpicture[i_index].i_start )
-                {
-                    /* Too early, come back next monday */
-                    continue;
-                }
+            if( display_date &&
+                display_date < p_spu->p_subpicture[i_index].i_start )
+            {
+                /* Too early, come back next monday */
+                continue;
+            }
 
-                /* If this is an ephemer subpic, see if it's the
-                 * youngest we have */
-                if( p_spu->p_subpicture[i_index].b_ephemer )
-                {
-                    if( p_ephemer == NULL )
-                    {
-                        p_ephemer = &p_spu->p_subpicture[i_index];
-                        continue;
-                    }
+            if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
+                ephemer_date = p_spu->p_subpicture[i_index].i_start;
 
-                    if( p_spu->p_subpicture[i_index].i_start
-                                                     < p_ephemer->i_start )
-                    {
-                        /* Link the previous ephemer subpicture and
-                         * replace it with the current one */
-                        p_ephemer->p_next = p_subpic;
-                        p_subpic = p_ephemer;
-                        p_ephemer = &p_spu->p_subpicture[i_index];
-
-                        /* If it's the 2nd youngest subpicture,
-                         * register its date */
-                        if( !ephemer_date
-                              || ephemer_date > p_subpic->i_start )
-                        {
-                            ephemer_date = p_subpic->i_start;
-                        }
+            if( display_date > p_spu->p_subpicture[i_index].i_stop &&
+                ( !p_spu->p_subpicture[i_index].b_ephemer ||
+                  p_spu->p_subpicture[i_index].i_stop >
+                  p_spu->p_subpicture[i_index].i_start ) )
+            {
+                /* Too late, destroy the subpic */
+                spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
+                continue;
+            }
 
-                        continue;
-                    }
-                }
+            /* If this is an ephemer subpic, add it to our list */
+            if( p_spu->p_subpicture[i_index].b_ephemer )
+            {
+                p_spu->p_subpicture[i_index].p_next = p_ephemer;
+                p_ephemer = &p_spu->p_subpicture[i_index];
 
-                p_spu->p_subpicture[i_index].p_next = p_subpic;
-                p_subpic = &p_spu->p_subpicture[i_index];
+                continue;
+            }
 
-                /* If it's the 2nd youngest subpicture, register its date */
-                if( !ephemer_date || ephemer_date > p_subpic->i_start )
-                {
-                    ephemer_date = p_subpic->i_start;
-                }
+            p_spu->p_subpicture[i_index].p_next = p_subpic;
+            p_subpic = &p_spu->p_subpicture[i_index];
+        }
+
+        /* If we found ephemer subpictures, check if they have to be
+         * displayed or destroyed */
+        while( p_ephemer != NULL )
+        {
+            subpicture_t *p_tmp = p_ephemer;
+            p_ephemer = p_ephemer->p_next;
+
+            if( p_tmp->i_start < ephemer_date )
+            {
+                /* Ephemer subpicture has lived too long */
+                spu_DestroySubpicture( p_spu, p_tmp );
             }
-            /* If it's not a DVD subpicture, just register it */
             else
             {
-                p_spu->p_subpicture[i_index].p_next = p_subpic;
-                p_subpic = &p_spu->p_subpicture[i_index];
+                /* Ephemer subpicture can still live a bit */
+                p_tmp->p_next = p_subpic;
+                p_subpic = p_tmp;
             }
         }
     }
 
-    /* If we found an ephemer subpicture, check if it has to be
-     * displayed */
-    if( p_ephemer != NULL )
-    {
-        if( p_ephemer->i_start <= ephemer_date )
-        {
-            /* Ephemer subpicture has lived too long */
-            spu_DestroySubpicture( p_spu, p_ephemer );
-        }
-        else
-        {
-            /* Ephemer subpicture can still live a bit */
-            p_ephemer->p_next = p_subpic;
-            return p_ephemer;
-        }
-    }
-
     return p_subpic;
 }
 
@@ -834,8 +846,8 @@ subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
  *****************************************************************************/
 static void SpuClearChannel( spu_t *p_spu, int i_channel )
 {
-    int                 i_subpic;                        /* subpicture index */
-    subpicture_t *      p_subpic = NULL;            /* first free subpicture */
+    int          i_subpic;                               /* subpicture index */
+    subpicture_t *p_subpic = NULL;                  /* first free subpicture */
 
     vlc_mutex_lock( &p_spu->subpicture_lock );
 
@@ -869,7 +881,7 @@ static void SpuClearChannel( spu_t *p_spu, int i_channel )
 /*****************************************************************************
  * spu_ControlDefault: default methods for the subpicture unit control.
  *****************************************************************************/
-int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
+static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
 {
     int *pi, i;
 
@@ -877,10 +889,9 @@ int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
     {
     case SPU_CHANNEL_REGISTER:
         pi = (int *)va_arg( args, int * );
-        p_spu->i_channel++;
-        if( pi ) *pi = p_spu->i_channel;
+        if( pi ) *pi = p_spu->i_channel++;
         msg_Dbg( p_spu, "Registering subpicture channel, ID: %i",
-                 p_spu->i_channel );
+                 p_spu->i_channel - 1 );
         break;
 
     case SPU_CHANNEL_CLEAR:
@@ -970,14 +981,16 @@ static int CropCallback( vlc_object_t *p_object, char const *psz_var,
  *****************************************************************************/
 static subpicture_t *sub_new_buffer( filter_t *p_filter )
 {
-    spu_t *p_spu = (spu_t *)p_filter->p_owner;
-    return spu_CreateSubpicture( p_spu );
+    filter_owner_sys_t *p_sys = p_filter->p_owner;
+    subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
+    if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
+    return p_subpicture;
 }
 
 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
 {
-    spu_t *p_spu = (spu_t *)p_filter->p_owner;
-    spu_DestroySubpicture( p_spu, p_subpic );
+    filter_owner_sys_t *p_sys = p_filter->p_owner;
+    spu_DestroySubpicture( p_sys->p_spu, p_subpic );
 }
 
 static subpicture_t *spu_new_buffer( filter_t *p_filter )