]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/dynamicoverlay/dynamicoverlay.c
Fix encoding (and writing).
[vlc] / modules / video_filter / dynamicoverlay / dynamicoverlay.c
index 289709b41b9f4b2c9f0d5917a2ebeab3e536fe16..dca20ce2cef72f0f3a4dbe2fa3edc1c7e7961063 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 2007 the VideoLAN team
  * $Id$
  *
- * Author: SÃ\83¸ren BÃ\83¸g <avacore@videolan.org>
+ * Author: Søren Bøg <avacore@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,7 +28,8 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_sout.h>
 #include <vlc_vout.h>
 #include <vlc_filter.h>
@@ -60,23 +61,23 @@ static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
 #define OUTPUT_TEXT N_("Output FIFO")
 #define OUTPUT_LONGTEXT N_("FIFO which will be written to for responses")
 
-vlc_module_begin();
-    set_description( _("Dynamic video overlay") );
-    set_shortname( _("Overlay" ));
-    set_category( CAT_VIDEO );
-    set_subcategory( SUBCAT_VIDEO_VFILTER );
-    set_capability( "sub filter", 0 );
+vlc_module_begin ()
+    set_description( N_("Dynamic video overlay") )
+    set_shortname( N_("Overlay" ))
+    set_category( CAT_VIDEO )
+    set_subcategory( SUBCAT_VIDEO_VFILTER )
+    set_capability( "sub filter", 0 )
 
     add_file( "overlay-input", NULL, NULL, INPUT_TEXT, INPUT_LONGTEXT,
-              VLC_FALSE );
+              false )
     add_file( "overlay-output", NULL, NULL, OUTPUT_TEXT, OUTPUT_LONGTEXT,
-              VLC_FALSE );
+              false )
 
-    add_shortcut( "overlay" );
-    set_callbacks( Create, Destroy );
-vlc_module_end();
+    add_shortcut( "overlay" )
+    set_callbacks( Create, Destroy )
+vlc_module_end ()
 
-static const char *ppsz_filter_options[] = {
+static const char *const ppsz_filter_options[] = {
     "input", "output", NULL
 };
 
@@ -93,10 +94,7 @@ static int Create( vlc_object_t *p_this )
     /* Allocate structure */
     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
     if( p_filter->p_sys == NULL )
-    {
-        msg_Err( p_filter, "out of memory" );
         return VLC_ENOMEM;
-    }
     p_sys = p_filter->p_sys;
 
     BufferInit( &p_sys->input );
@@ -108,8 +106,9 @@ static int Create( vlc_object_t *p_this )
 
     p_sys->i_inputfd = -1;
     p_sys->i_outputfd = -1;
-    p_sys->b_updated = VLC_TRUE;
-    p_sys->b_atomic = VLC_FALSE;
+    p_sys->b_updated = true;
+    p_sys->b_atomic = false;
+    vlc_mutex_init( &p_sys->lock );
 
     p_filter->pf_sub_filter = Filter;
 
@@ -136,18 +135,23 @@ static int Create( vlc_object_t *p_this )
 static void Destroy( vlc_object_t *p_this )
 {
     filter_t *p_filter = (filter_t *)p_this;
+    filter_sys_t *p_sys = p_filter->p_sys;
 
-    BufferDestroy( &p_filter->p_sys->input );
-    BufferDestroy( &p_filter->p_sys->output );
-    QueueDestroy( &p_filter->p_sys->atomic );
-    QueueDestroy( &p_filter->p_sys->pending );
-    QueueDestroy( &p_filter->p_sys->processed );
-    ListDestroy( &p_filter->p_sys->overlays );
+    BufferDestroy( &p_sys->input );
+    BufferDestroy( &p_sys->output );
+    QueueDestroy( &p_sys->atomic );
+    QueueDestroy( &p_sys->pending );
+    QueueDestroy( &p_sys->processed );
+    ListDestroy( &p_sys->overlays );
     UnregisterCommand( p_filter );
 
-    free( p_filter->p_sys->psz_inputfile );
-    free( p_filter->p_sys->psz_outputfile );
-    free( p_filter->p_sys );
+    var_DelCallback( p_filter, "overlay-input", AdjustCallback, p_sys );
+    var_DelCallback( p_filter, "overlay-output", AdjustCallback, p_sys );
+
+    vlc_mutex_destroy( &p_sys->lock );
+    free( p_sys->psz_inputfile );
+    free( p_sys->psz_outputfile );
+    free( p_sys );
 }
 
 /*****************************************************************************
@@ -162,6 +166,7 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
     filter_sys_t *p_sys = p_filter->p_sys;
 
     /* We might need to open these at any time. */
+    vlc_mutex_lock( &p_sys->lock );
     if( p_sys->i_inputfd == -1 )
     {
         p_sys->i_inputfd = open( p_sys->psz_inputfile, O_RDONLY | O_NONBLOCK );
@@ -195,6 +200,7 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
                       p_sys->psz_outputfile );
         }
     }
+    vlc_mutex_unlock( &p_sys->lock );
 
     /* Read any waiting commands */
     if( p_sys->i_inputfd != -1 )
@@ -228,7 +234,7 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
                              p_sys->input.i_length ) ) )
     {
         commanddesc_t *p_cur = NULL;
-        vlc_bool_t b_found = VLC_FALSE;
+        bool b_found = false;
         size_t i_index = 0;
 
         *p_end = '\0';
@@ -241,7 +247,7 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
             if( !strncmp( p_cur->psz_command, p_cmd, strlen(p_cur->psz_command) ) )
             {
                 p_cmd[strlen(p_cur->psz_command)] = '\0';
-                b_found = VLC_TRUE;
+                b_found = true;
                 break;
             }
         }
@@ -265,8 +271,8 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
             p_cmddesc->p_command->pf_parser( p_cmd, p_end,
                                              &p_cmddesc->params );
 
-            if( ( p_cmddesc->p_command->b_atomic == VLC_TRUE ) &&
-                ( p_sys->b_atomic == VLC_TRUE ) )
+            if( ( p_cmddesc->p_command->b_atomic == true ) &&
+                ( p_sys->b_atomic == true ) )
                 QueueEnqueue( &p_sys->atomic, p_cmddesc );
             else
                 QueueEnqueue( &p_sys->pending, p_cmddesc );
@@ -293,8 +299,8 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
             const char *psz_success = "SUCCESS:";
             const char *psz_nl = "\n";
             BufferAdd( &p_sys->output, psz_success, 8 );
-            p_command->p_command->pf_unparser( &p_command->results,
-                                               &p_sys->output );
+            p_command->p_command->pf_unparse( &p_command->results,
+                                              &p_sys->output );
             BufferAdd( &p_sys->output, psz_nl, 1 );
         }
         else
@@ -326,7 +332,7 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
         }
     }
 
-    if( p_sys->b_updated == VLC_FALSE )
+    if( p_sys->b_updated == false )
         return NULL;
 
     subpicture_t *p_spu = NULL;
@@ -339,68 +345,42 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
         return NULL;
     }
 
-    p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
-    p_spu->i_x = 0;
-    p_spu->i_y = 0;
-    p_spu->b_absolute = VLC_TRUE;
+    p_spu->b_absolute = true;
     p_spu->i_start = date;
     p_spu->i_stop = 0;
-    p_spu->b_ephemer = VLC_TRUE;
+    p_spu->b_ephemer = true;
 
     subpicture_region_t **pp_region = &p_spu->p_region;
     while( (p_overlay = ListWalk( &p_sys->overlays )) )
     {
+        subpicture_region_t *p_region;
+
+        *pp_region = p_region = subpicture_region_New( &p_overlay->format );
+        if( !p_region )
+            break;
+
         msg_Dbg( p_filter, "Displaying overlay: %4.4s, %d, %d, %d",
                  (char*)&p_overlay->format.i_chroma, p_overlay->i_x, p_overlay->i_y,
                  p_overlay->i_alpha );
 
-        if( p_overlay->format.i_chroma == VLC_FOURCC('T','E','X','T') )
+        if( p_overlay->format.i_chroma == VLC_CODEC_TEXT )
         {
-            *pp_region = p_spu->pf_create_region( VLC_OBJECT(p_filter),
-                                                  &p_overlay->format );
-            if( !*pp_region )
-                break;
-            (*pp_region)->psz_text = strdup( p_overlay->data.p_text );
-            (*pp_region)->p_style = malloc( sizeof(struct text_style_t) );
-            if( !(*pp_region)->p_style )
-            {
-                p_spu->pf_destroy_region( VLC_OBJECT(p_filter), (*pp_region) );
-                *pp_region = NULL;
-                break;
-            }
-            (*pp_region)->psz_text = strdup( p_overlay->data.p_text );
-            memcpy( (*pp_region)->p_style, &p_overlay->fontstyle, sizeof(text_style_t) );
+            p_region->psz_text = strdup( p_overlay->data.p_text );
+            p_region->p_style = text_style_Duplicate( p_overlay->p_fontstyle );
         }
         else
         {
-            picture_t clone;
-            if( vout_AllocatePicture( p_filter, &clone,
-                                      p_overlay->format.i_chroma,
-                                      p_overlay->format.i_width,
-                                      p_overlay->format.i_height,
-                                      p_overlay->format.i_aspect ) )
-            {
-                msg_Err( p_filter, "cannot allocate picture" );
-                continue;
-            }
-            vout_CopyPicture( p_filter, &clone, p_overlay->data.p_pic );
-            *pp_region = p_spu->pf_make_region( VLC_OBJECT(p_filter),
-                                                &p_overlay->format,
-                                                &clone );
-            if( !*pp_region )
-            {
-                msg_Err( p_filter, "cannot allocate subpicture region" );
-                continue;
-            }
+            /* FIXME the copy is probably not needed anymore */
+            picture_Copy( p_region->p_picture, p_overlay->data.p_pic );
         }
-        (*pp_region)->i_x = p_overlay->i_x;
-        (*pp_region)->i_y = p_overlay->i_y;
-        (*pp_region)->i_align = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
-        (*pp_region)->i_alpha = p_overlay->i_alpha;
-        pp_region = &(*pp_region)->p_next;
+        p_region->i_x = p_overlay->i_x;
+        p_region->i_y = p_overlay->i_y;
+        p_region->i_align = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
+        p_region->i_alpha = p_overlay->i_alpha;
+        pp_region = &p_region->p_next;
     }
 
-    p_sys->b_updated = VLC_FALSE;
+    p_sys->b_updated = false;
     return p_spu;
 }
 
@@ -411,10 +391,18 @@ static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
     filter_sys_t *p_sys = (filter_sys_t *)p_data;
     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
 
+    vlc_mutex_lock( &p_sys->lock );
     if( !strncmp( psz_var, "overlay-input", 13 ) )
-        p_sys->psz_inputfile = newval.psz_string;
+    {
+        free( p_sys->psz_inputfile );
+        p_sys->psz_inputfile = strdup( newval.psz_string );
+    }
     else if( !strncmp( psz_var, "overlay-output", 14 ) )
-        p_sys->psz_outputfile = newval.psz_string;
+    {
+        free( p_sys->psz_outputfile );
+        p_sys->psz_outputfile = strdup( newval.psz_string );
+    }
+    vlc_mutex_unlock( &p_sys->lock );
 
     return VLC_EGENERIC;
 }