X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fmisc%2Fsvg.c;h=7c5ad64b8912b575280e0413673cea83d72c4e09;hb=087442fc1e36298dfbef018401b39d72249a17a9;hp=6f017dfbf516857d9f86b59d882812fdee4aac85;hpb=a90a19a6b0468ea9fedadc27cfc1118d70295263;p=vlc diff --git a/modules/misc/svg.c b/modules/misc/svg.c index 6f017dfbf5..7c5ad64b89 100644 --- a/modules/misc/svg.c +++ b/modules/misc/svg.c @@ -1,8 +1,8 @@ /***************************************************************************** * svg.c : Put SVG on the video ***************************************************************************** - * Copyright (C) 2002, 2003 VideoLAN - * $Id: svg.c,v 1.2 2003/07/23 17:26:56 oaubert Exp $ + * Copyright (C) 2002, 2003 the VideoLAN team + * $Id$ * * Authors: Olivier Aubert * @@ -18,82 +18,101 @@ * * 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. *****************************************************************************/ /***************************************************************************** * Preamble *****************************************************************************/ -#include /* malloc( ), free( ) */ -#include + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include #include -#include -#include -#include -#include +#ifdef HAVE_UNISTD_H +# include +#elif defined( WIN32 ) && !defined( UNDER_CE ) +# include +#endif +#include +#include +#include /* g_object_unref( ) */ #include +typedef struct svg_rendition_t svg_rendition_t; + /***************************************************************************** * Local prototypes *****************************************************************************/ static int Create ( vlc_object_t * ); static void Destroy ( vlc_object_t * ); -static void Render ( vout_thread_t *, picture_t *, - const subpicture_t * ); -static subpicture_t *AddText ( vout_thread_t *p_vout, int i_channel, - char *psz_string, text_style_t *p_style, int i_flags, - int i_hmargin, int i_vmargin, mtime_t i_start, - mtime_t i_stop ); -static byte_t *svg_GetTemplate (); -static void svg_SizeCallback (int *width, int *height, gpointer data ); -static void svg_RenderPicture (vout_thread_t *p_vout, - subpicture_sys_t *p_string ); -static void FreeString( subpicture_t * ); +static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out, + subpicture_region_t *p_region_in ); +static char *svg_GetTemplate( vlc_object_t *p_this ); /***************************************************************************** * Module descriptor *****************************************************************************/ #define TEMPLATE_TEXT N_( "SVG template file" ) -#define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template for automatic string conversion" ) - -vlc_module_begin(); - set_category( CAT_VIDEO ); - set_subcategory( SUBCAT_VIDEO_TEXT ); - set_capability( "text renderer", 100 ); - add_shortcut( "svg" ); - add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, VLC_TRUE ); - set_callbacks( Create, Destroy ); -vlc_module_end(); +#define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template "\ + "for automatic string conversion" ) + +vlc_module_begin () + set_category( CAT_INPUT ) + set_category( SUBCAT_INPUT_SCODEC ) + set_capability( "text renderer", 99 ) + add_shortcut( "svg" ) + add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, true ) + set_callbacks( Create, Destroy ) +vlc_module_end () /** Describes a SVG string to be displayed on the video */ -struct subpicture_sys_t +struct svg_rendition_t { int i_width; int i_height; int i_chroma; /** The SVG source associated with this subpicture */ - byte_t *psz_text; + char *psz_text; /* The rendered SVG, as a GdkPixbuf */ GdkPixbuf *p_rendition; }; +static int Render( filter_t *, subpicture_region_t *, svg_rendition_t *, int, int); +static char *svg_GetTemplate (); +static void svg_set_size( filter_t *p_filter, int width, int height ); +static void svg_SizeCallback ( int *width, int *height, gpointer data ); +static void svg_RenderPicture ( filter_t *p_filter, + svg_rendition_t *p_svg ); +static void FreeString( svg_rendition_t * ); + /***************************************************************************** - * vout_sys_t: svg local data + * filter_sys_t: svg local data ***************************************************************************** - * This structure is part of the video output thread descriptor. + * This structure is part of the filter thread descriptor. * It describes the svg specific properties of an output thread. *****************************************************************************/ -struct text_renderer_sys_t +struct filter_sys_t { /* The SVG template used to convert strings */ - byte_t *psz_template; - vlc_mutex_t *lock; + char *psz_template; + /* Default size for rendering. Initialized to the output size. */ + int i_width; + int i_height; }; /***************************************************************************** @@ -103,37 +122,44 @@ struct text_renderer_sys_t *****************************************************************************/ static int Create( vlc_object_t *p_this ) { - vout_thread_t *p_vout = ( vout_thread_t * )p_this; + filter_t *p_filter = ( filter_t * )p_this; + filter_sys_t *p_sys; /* Allocate structure */ - p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) ); - if( p_vout->p_text_renderer_data == NULL ) - { - msg_Err( p_vout, "Out of memory" ); + p_sys = malloc( sizeof( filter_sys_t ) ); + if( !p_sys ) return VLC_ENOMEM; - } - p_vout->pf_add_string = AddText; /* Initialize psz_template */ - p_vout->p_text_renderer_data->psz_template = svg_GetTemplate( p_this ); - if( !p_vout->p_text_renderer_data->psz_template ) + p_sys->psz_template = svg_GetTemplate( p_this ); + if( !p_sys->psz_template ) { - msg_Err( p_vout, "Out of memory" ); + free( p_sys ); return VLC_ENOMEM; } + p_sys->i_width = p_filter->fmt_out.video.i_width; + p_sys->i_height = p_filter->fmt_out.video.i_height; + + p_filter->pf_render_text = RenderText; + p_filter->pf_render_html = NULL; + p_filter->p_sys = p_sys; + + /* MUST call this before any RSVG funcs */ + rsvg_init( ); + return VLC_SUCCESS; } -static byte_t *svg_GetTemplate( vlc_object_t *p_this ) +static char *svg_GetTemplate( vlc_object_t *p_this ) { - vout_thread_t *p_vout = ( vout_thread_t * )p_this; + filter_t *p_filter = ( filter_t * )p_this; char *psz_filename; char *psz_template; FILE *file; - psz_filename = config_GetPsz( p_vout, "svg-template-file" ); - if( !psz_filename || psz_filename[0] == 0 ) + psz_filename = var_InheritString( p_filter, "svg-template-file" ); + if( !psz_filename || (psz_filename[0] == 0) ) { /* No filename. Use a default value. */ psz_template = NULL; @@ -141,39 +167,50 @@ static byte_t *svg_GetTemplate( vlc_object_t *p_this ) else { /* Read the template */ - file = fopen( psz_filename, "rt" ); + file = vlc_fopen( psz_filename, "rt" ); if( !file ) { - msg_Warn( p_this, "SVG template file %s does not exist.", psz_filename ); + msg_Warn( p_this, "SVG template file %s does not exist.", + psz_filename ); psz_template = NULL; } else { struct stat s; - int i_ret; - i_ret = lstat( psz_filename, &s ); - if( i_ret ) + if( fstat( fileno( file ), &s ) ) { /* Problem accessing file information. Should not happen as we could open it. */ psz_template = NULL; } else + if( ((signed)s.st_size) < 0 ) { - fprintf( stderr, "Reading %ld bytes from %s\n", (long)s.st_size, psz_filename ); + msg_Err( p_this, "SVG template too big" ); + psz_template = NULL; + } + else + { + msg_Dbg( p_this, "reading %ld bytes from template %s", + (unsigned long)s.st_size, psz_filename ); - psz_template = malloc( s.st_size + 42 ); + psz_template = calloc( 1, s.st_size + 42 ); if( !psz_template ) { - msg_Err( p_vout, "Out of memory" ); + fclose( file ); + free( psz_filename ); return NULL; } - fread( psz_template, s.st_size, 1, file ); - fclose( file ); + if(! fread( psz_template, s.st_size, 1, file ) ) + { + msg_Dbg( p_this, "No data read from template." ); + } } + fclose( file ); } } + free( psz_filename ); if( !psz_template ) { /* Either there was no file, or there was an error. @@ -194,42 +231,83 @@ static byte_t *svg_GetTemplate( vlc_object_t *p_this ) *****************************************************************************/ static void Destroy( vlc_object_t *p_this ) { - vout_thread_t *p_vout = ( vout_thread_t * )p_this; - free( p_vout->p_text_renderer_data->psz_template ); - free( p_vout->p_text_renderer_data ); + filter_t *p_filter = ( filter_t * )p_this; + filter_sys_t *p_sys = p_filter->p_sys; + + free( p_sys->psz_template ); + free( p_sys ); + rsvg_term( ); } /***************************************************************************** * Render: render SVG in picture - ***************************************************************************** - * This function merges the previously rendered SVG subpicture into a picture *****************************************************************************/ -static void Render( vout_thread_t *p_vout, picture_t *p_pic, - const subpicture_t *p_subpic ) +static int Render( filter_t *p_filter, subpicture_region_t *p_region, + svg_rendition_t *p_svg, int i_width, int i_height ) { - subpicture_sys_t *p_string = p_subpic->p_sys; + video_format_t fmt; + uint8_t *p_y, *p_u, *p_v, *p_a; + int x, y, i_pitch, i_u_pitch; guchar *pixels_in = NULL; - guchar *pixels_out = NULL; - int rowstride_in, rowstride_out; - int channels_in, channels_out; - int x, y; - int i_width, i_height; + int rowstride_in; + int channels_in; int alpha; + picture_t *p_pic; - if( p_string->p_rendition == NULL ) { - /* Something changed ( presumably the dimensions ). Get the new - dimensions and update the pixbuf */ - p_string->i_width = p_vout->output.i_width; - p_string->i_height = p_vout->output.i_height; - svg_RenderPicture( p_vout, p_string ); + if ( p_filter->p_sys->i_width != i_width || + p_filter->p_sys->i_height != i_height ) + { + svg_set_size( p_filter, i_width, i_height ); + p_svg->p_rendition = NULL; } - /* This rendering code is in no way optimized. If someone has some - time to lose to make it work faster, please do. + if( p_svg->p_rendition == NULL ) { + svg_RenderPicture( p_filter, p_svg ); + if( ! p_svg->p_rendition ) + { + msg_Err( p_filter, "Cannot render SVG" ); + return VLC_EGENERIC; + } + } + i_width = gdk_pixbuf_get_width( p_svg->p_rendition ); + i_height = gdk_pixbuf_get_height( p_svg->p_rendition ); + + /* Create a new subpicture region */ + memset( &fmt, 0, sizeof( video_format_t ) ); + fmt.i_chroma = VLC_CODEC_YUVA; + fmt.i_width = fmt.i_visible_width = i_width; + fmt.i_height = fmt.i_visible_height = i_height; + fmt.i_x_offset = fmt.i_y_offset = 0; + fmt.i_sar_num = 1; + fmt.i_sar_den = 1; + + p_region->p_picture = picture_NewFromFormat( &fmt ); + if( !p_region->p_picture ) + return VLC_EGENERIC; + p_region->fmt = fmt; + + p_region->i_x = p_region->i_y = 0; + p_y = p_region->p_picture->Y_PIXELS; + p_u = p_region->p_picture->U_PIXELS; + p_v = p_region->p_picture->V_PIXELS; + p_a = p_region->p_picture->A_PIXELS; + + i_pitch = p_region->p_picture->Y_PITCH; + i_u_pitch = p_region->p_picture->U_PITCH; + + /* Initialize the region pixels (only the alpha will be changed later) */ + memset( p_y, 0x00, i_pitch * p_region->fmt.i_height ); + memset( p_u, 0x80, i_u_pitch * p_region->fmt.i_height ); + memset( p_v, 0x80, i_u_pitch * p_region->fmt.i_height ); + + p_pic = p_region->p_picture; + + /* Copy the data */ + + /* This rendering code is in no way optimized. If someone has some time to + make it work faster or better, please do. */ - /* FIXME: The alpha value is not taken into account. */ - /* p_pixbuf->get_rowstride() is the number of bytes in a line. p_pixbuf->get_height() is the number of lines. @@ -244,207 +322,181 @@ static void Render( vout_thread_t *p_vout, picture_t *p_pic, blue = pixels [ n_channels * ( y*rowstride ) + x ) + 2 ]; */ - pixels_in = gdk_pixbuf_get_pixels( p_string->p_rendition ); - pixels_out = p_pic->p->p_pixels; + pixels_in = gdk_pixbuf_get_pixels( p_svg->p_rendition ); + rowstride_in = gdk_pixbuf_get_rowstride( p_svg->p_rendition ); + channels_in = gdk_pixbuf_get_n_channels( p_svg->p_rendition ); + alpha = gdk_pixbuf_get_has_alpha( p_svg->p_rendition ); - rowstride_in = gdk_pixbuf_get_rowstride( p_string->p_rendition ); - rowstride_out = p_pic->p->i_pitch; - - channels_in = gdk_pixbuf_get_n_channels( p_string->p_rendition ); - channels_out = p_pic->p->i_pixel_pitch; + /* + This crashes the plugin (if !alpha). As there is always an alpha value, + it does not matter for the moment : - alpha = gdk_pixbuf_get_has_alpha( p_string->p_rendition ); + if( !alpha ) + memset( p_a, 0xFF, i_pitch * p_region->fmt.i_height ); + */ #define INDEX_IN( x, y ) ( y * rowstride_in + x * channels_in ) -#define INDEX_OUT( x, y ) ( y * rowstride_out + x * channels_out ) -#define UV_INDEX_OUT( x, y ) ( y * p_pic->p[U_PLANE].i_pitch / 2 + x * p_pic->p[U_PLANE].i_pixel_pitch / 2 ) - - i_width = gdk_pixbuf_get_width( p_string->p_rendition ); - i_height = gdk_pixbuf_get_height( p_string->p_rendition ); +#define INDEX_OUT( x, y ) ( y * i_pitch + x * p_pic->p[Y_PLANE].i_pixel_pitch ) - switch( p_vout->output.i_chroma ) + for( y = 0; y < i_height; y++ ) { - /* I420 target, no scaling */ - case VLC_FOURCC( 'I','4','2','0' ): - case VLC_FOURCC( 'I','Y','U','V' ): - case VLC_FOURCC( 'Y','V','1','2' ): - for( y = 0; y < i_height; y++ ) + for( x = 0; x < i_width; x++ ) { - for( x = 0; x < i_width; x++ ) - { - guchar *p_in; - int i_out; - int i_uv_out; + guchar *p_in; + int i_out; - p_in = &pixels_in[INDEX_IN( x, y )]; + p_in = &pixels_in[INDEX_IN( x, y )]; #define R( pixel ) *pixel #define G( pixel ) *( pixel+1 ) #define B( pixel ) *( pixel+2 ) #define ALPHA( pixel ) *( pixel+3 ) - /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ : - Y = 0.29900 * R + 0.58700 * G + 0.11400 * B - U = -0.1687 * r - 0.3313 * g + 0.5 * b + 128 - V = 0.5 * r - 0.4187 * g - 0.0813 * b + 128 - */ - if( (alpha && ALPHA( p_in ) > 10 ) || ( ! alpha )) { - i_out = INDEX_OUT( x, y ); + /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ : + Y = 0.29900 * R + 0.58700 * G + 0.11400 * B + U = -0.1687 * r - 0.3313 * g + 0.5 * b + 128 + V = 0.5 * r - 0.4187 * g - 0.0813 * b + 128 + */ + if ( alpha ) { + i_out = INDEX_OUT( x, y ); - p_pic->p[Y_PLANE].p_pixels[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in ); + p_pic->Y_PIXELS[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in ); - if( ( x % 2 == 0 ) && ( y % 2 == 0 ) ) { - i_uv_out = UV_INDEX_OUT( x, y ); + p_pic->U_PIXELS[i_out] = -.1687 * R( p_in ) - .3313 * G( p_in ) + .5 * B( p_in ) + 128; + p_pic->V_PIXELS[i_out] = .5 * R( p_in ) - .4187 * G( p_in ) - .0813 * B( p_in ) + 128; - p_pic->p[U_PLANE].p_pixels[i_uv_out] = -.1687 * R( p_in ) - .3313 * G( p_in ) + .5 * B( p_in ) + 128; - p_pic->p[V_PLANE].p_pixels[i_uv_out] = .5 * R( p_in ) - .4187 * G( p_in ) - .0813 * B( p_in ) + 128; - } - } + p_pic->A_PIXELS[i_out] = ALPHA( p_in ); } } - break; - - /* RV32 target, scaling */ - case VLC_FOURCC( 'R','V','2','4' ): - case VLC_FOURCC( 'R','V','3','2' ): - for( y = 0; y < i_height; y++ ) - { - for( x = 0; x < i_width; x++ ) - { - guchar *p_in; - guchar *p_out; - - p_in = &pixels_in[INDEX_IN( x, y )]; - p_out = &pixels_out[INDEX_OUT( x, y )]; + } - *p_out = *p_in; - *( p_out+1 ) = *( p_in+1 ); - *( p_out+2 ) = *( p_in+2 ); - } - } - break; + return VLC_SUCCESS; +} - default: - msg_Err( p_vout, "unknown chroma, can't render SVG" ); - break; - } +static void svg_set_size( filter_t *p_filter, int width, int height ) +{ + p_filter->p_sys->i_width = width; + p_filter->p_sys->i_height = height; } static void svg_SizeCallback( int *width, int *height, gpointer data ) { - subpicture_sys_t *p_string = data; + filter_t *p_filter = data; - *width = p_string->i_width; - *height = p_string->i_height; + *width = p_filter->p_sys->i_width; + *height = p_filter->p_sys->i_height; return; } -static void svg_RenderPicture( vout_thread_t *p_vout, - subpicture_sys_t *p_string ) +static void svg_RenderPicture( filter_t *p_filter, + svg_rendition_t *p_svg ) { /* Render the SVG string p_string->psz_text into a new picture_t p_string->p_rendition with dimensions ( ->i_width, ->i_height ) */ RsvgHandle *p_handle; - GError *error; + GError *error = NULL; + + p_svg->p_rendition = NULL; p_handle = rsvg_handle_new(); - rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_string, NULL ); + if( !p_handle ) + { + msg_Err( p_filter, "Error creating SVG reader" ); + return; + } + + rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_filter, NULL ); - rsvg_handle_write( p_handle, - p_string->psz_text, strlen( p_string->psz_text ) + 1, - &error ); - rsvg_handle_close( p_handle, &error ); + if( ! rsvg_handle_write( p_handle, + ( guchar* )p_svg->psz_text, strlen( p_svg->psz_text ), + &error ) ) + { + msg_Err( p_filter, "error while rendering SVG: %s", error->message ); + g_object_unref( G_OBJECT( p_handle ) ); + return; + } - p_string->p_rendition = rsvg_handle_get_pixbuf( p_handle ); - rsvg_handle_free( p_handle ); + if( ! rsvg_handle_close( p_handle, &error ) ) + { + msg_Err( p_filter, "error while rendering SVG (close): %s", error->message ); + g_object_unref( G_OBJECT( p_handle ) ); + return; + } + + p_svg->p_rendition = rsvg_handle_get_pixbuf( p_handle ); + + g_object_unref( G_OBJECT( p_handle ) ); } -/** - * This function receives a SVG string and creates a subpicture for it. - * It is used as pf_add_string callback in the vout method by this module. - */ -static subpicture_t *AddText ( vout_thread_t *p_vout, int i_channel, - char *psz_string, text_style_t *p_style, int i_flags, - int i_hmargin, int i_vmargin, mtime_t i_start, - mtime_t i_stop ) +static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out, + subpicture_region_t *p_region_in ) { - subpicture_sys_t *p_string; - subpicture_t *p_subpic; + filter_sys_t *p_sys = p_filter->p_sys; + svg_rendition_t *p_svg = NULL; + char *psz_string; - msg_Dbg( p_vout, "adding string \"%s\" start_date "I64Fd - " end_date" I64Fd, psz_string, i_start, i_stop ); + /* Sanity check */ + if( !p_region_in || !p_region_out ) return VLC_EGENERIC; + psz_string = p_region_in->psz_text; + if( !psz_string || !*psz_string ) return VLC_EGENERIC; - /* Create and initialize a subpicture */ - p_subpic = vout_CreateSubPicture( p_vout, i_channel, GRAPH_CONTENT, - MEMORY_SUBPICTURE ); - if( p_subpic == NULL ) - { - return NULL; - } + p_svg = malloc( sizeof( svg_rendition_t ) ); + if( !p_svg ) + return VLC_ENOMEM; - p_subpic->pf_render = Render; - p_subpic->pf_destroy = FreeString; - p_subpic->i_start = i_start; - p_subpic->i_stop = i_stop; - if( i_stop == 0 ) - { - p_subpic->b_ephemer = VLC_TRUE; - } - else - { - p_subpic->b_ephemer = VLC_FALSE; - } - - /* Create and initialize private data for the subpicture */ - p_string = malloc( sizeof( subpicture_sys_t ) ); - if( p_string == NULL ) - { - vout_DestroySubPicture( p_vout, p_subpic ); - return NULL; - } - p_subpic->p_sys = p_string; + p_region_out->i_x = p_region_in->i_x; + p_region_out->i_y = p_region_in->i_y; /* Check if the data is SVG or pure text. In the latter case, convert the text to SVG. FIXME: find a better test */ if( strstr( psz_string, "psz_text = strdup( psz_string ); + p_svg->psz_text = strdup( psz_string ); + if( !p_svg->psz_text ) + { + free( p_svg ); + return VLC_ENOMEM; + } } else { /* Data is text. Convert to SVG */ + /* FIXME: handle p_style attributes */ int length; - byte_t* psz_template = p_vout->p_text_renderer_data->psz_template; + char* psz_template = p_sys->psz_template; length = strlen( psz_string ) + strlen( psz_template ) + 42; - p_string->psz_text = malloc( length + 1 ); - if( p_string->psz_text == NULL ) + p_svg->psz_text = calloc( 1, length + 1 ); + if( !p_svg->psz_text ) { - return NULL; + free( p_svg ); + return VLC_ENOMEM; } - snprintf( p_string->psz_text, length, psz_template, psz_string ); + snprintf( p_svg->psz_text, length, psz_template, psz_string ); } - - p_string->i_width = p_vout->output.i_width; - p_string->i_height = p_vout->output.i_height; - p_string->i_chroma = p_vout->output.i_chroma; + p_svg->i_width = p_sys->i_width; + p_svg->i_height = p_sys->i_height; + p_svg->i_chroma = VLC_CODEC_YUVA; /* Render the SVG. The input data is stored in the p_string structure, and the function updates the p_rendition attribute. */ - svg_RenderPicture( p_vout, p_string ); + svg_RenderPicture( p_filter, p_svg ); + + Render( p_filter, p_region_out, p_svg, p_svg->i_width, p_svg->i_height ); + FreeString( p_svg ); - vout_DisplaySubPicture( p_vout, p_subpic ); - return p_subpic; + return VLC_SUCCESS; } -static void FreeString( subpicture_t *p_subpic ) +static void FreeString( svg_rendition_t *p_svg ) { - subpicture_sys_t *p_string = p_subpic->p_sys; - - free( p_string->psz_text ); - free( p_string->p_rendition ); - free( p_string ); + free( p_svg->psz_text ); + /* p_svg->p_rendition is a GdkPixbuf, and its allocation is + managed through ref. counting */ + if( p_svg->p_rendition ) + g_object_unref( p_svg->p_rendition ); + free( p_svg ); }