]> git.sesse.net Git - vlc/blobdiff - src/video_output/video_text.c
libvlc: use vlc_common.h (libvlccore) instead of vlc/vlc.h
[vlc] / src / video_output / video_text.c
index a2789b7fe29276791763987a6fb1384778a2bb59..d98532284ebcc843da9035136d1cffa3890e47c5 100644 (file)
-/*******************************************************************************
+/*****************************************************************************
  * video_text.c : text manipulation functions
- * (c)1999 VideoLAN
- *******************************************************************************/
-
-/*******************************************************************************
- * Preamble
- *******************************************************************************/
-#include <errno.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "common.h"
-#include "config.h"
-#include "video_text.h"
-#include "intf_msg.h"
+ *****************************************************************************
+ * Copyright (C) 1999-2007 the VideoLAN team
+ * $Id$
+ *
+ * Author: Sigmund Augdal Helberg <dnumgis@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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
-/*******************************************************************************
- * vout_font_t: bitmap font
- *******************************************************************************
- * This structure is used when the system doesn't provide a convenient function 
- * to print simple characters in a buffer.
- * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
- * first array of characters followed by a second array of borders masks. 
- * Therefore the border masks can't be complete if the font has pixels on the
- * border.
- *******************************************************************************/
-typedef struct vout_font_s
+#include <vlc_common.h>
+#include <vlc_vout.h>
+#include <vlc_block.h>
+#include <vlc_filter.h>
+#include <vlc_osd.h>
+
+/**
+ * \brief Show text on the video for some time
+ * \param p_vout pointer to the vout the text is to be showed on
+ * \param i_channel Subpicture channel
+ * \param psz_string The text to be shown
+ * \param p_style Pointer to a struct with text style info
+ * \param i_flags flags for alignment and such
+ * \param i_hmargin horizontal margin in pixels
+ * \param i_vmargin vertical margin in pixels
+ * \param i_duration Amount of time the text is to be shown.
+ */
+int vout_ShowTextRelative( 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_duration )
 {
-    int                 i_type;                                   /* font type */
-    int                 i_width;                  /* character width in pixels */
-    int                 i_height;                /* character height in pixels */
-    int                 i_interspacing;   /* characters interspacing in pixels */
-    int                 i_bytes_per_line;          /* bytes per character line */    
-    int                 i_bytes_per_char;               /* bytes per character */    
-    u16                 i_first;                            /* first character */    
-    u16                 i_last;                              /* last character */
-    byte_t *            p_data;                         /* font character data */
-} vout_font_t;
-
-/* Font types */
-#define VOUT_FIXED_FONT       0                           /* simple fixed font */
-
-/*******************************************************************************
- * vout_put_byte_t: PutByte function
- *******************************************************************************
- * These functions will transform masks in a set of pixels. For each pixel, 
- * character, then border and background masks are tested, and the first
- * encountered color is set.
- *******************************************************************************/
-typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border, 
-                                int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
-
+    mtime_t i_now = mdate();
 
-/*******************************************************************************
- * Macros
- *******************************************************************************/
-
-/* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
- * to optimize the number of tests. It is used in the PutByte functions. */
-#define TREE( i_mask, i_mask_color )                                            \
-if( i_mask & 0xf0 )                                       /* one from 1111 */   \
-{                                                                               \
-    if( i_mask & 0xc0 )                                   /* one from 1100 */   \
-    {                                                                           \
-        if( i_mask & 0x80 )                                        /* 1000 */   \
-        {                                                                       \
-            p_pic[0] = i_mask_color;                                            \
-            if( i_mask & 0x40 )                                    /* 0100 */   \
-            {                                                                   \
-                p_pic[1] = i_mask_color;                                        \
-            }                                                                   \
-        }                                                                       \
-        else                                        /* not 1000 means 0100 */   \
-        {                                                                       \
-            p_pic[1] = i_mask_color;                                            \
-        }                                                                       \
-        if( i_mask & 0x30 )                               /* one from 0011 */   \
-        {                                                                       \
-            if( i_mask & 0x20 )                                    /* 0010 */   \
-            {                                                                   \
-                p_pic[2] = i_mask_color;                                        \
-                if( i_mask & 0x10 )                                /* 0001 */   \
-                {                                                               \
-                    p_pic[3] = i_mask_color;                                    \
-                }                                                               \
-            }                                                                   \
-            else                                    /* not 0010 means 0001 */   \
-            {                                                                   \
-                 p_pic[3] = i_mask_color;                                       \
-            }                                                                   \
-        }                                                                       \
-    }                                                                           \
-    else                                            /* not 1100 means 0011 */   \
-    {                                                                           \
-        if( i_mask & 0x20 )                                        /* 0010 */   \
-        {                                                                       \
-            p_pic[2] = i_mask_color;                                            \
-            if( i_mask & 0x10 )                                    /* 0001 */   \
-            {                                                                   \
-                p_pic[3] = i_mask_color;                                        \
-            }                                                                   \
-        }                                                                       \
-        else                                        /* not 0010 means 0001 */   \
-        {                                                                       \
-            p_pic[3] = i_mask_color;                                            \
-        }                                                                       \
-    }                                                                           \
-}                                                                               \
-if( i_mask & 0x0f )                                                             \
-{                                                                               \
-    if( i_mask & 0x0c )                       /* one from 1100 */               \
-    {                                                                           \
-        if( i_mask & 0x08 )                                        /* 1000 */   \
-        {                                                                       \
-            p_pic[4] = i_mask_color;                                            \
-            if( i_mask & 0x04 )                                    /* 0100 */   \
-            {                                                                   \
-                p_pic[5] = i_mask_color;                                        \
-            }                                                                   \
-        }                                                                       \
-        else                                        /* not 1000 means 0100 */   \
-        {                                                                       \
-            p_pic[5] = i_mask_color;                                            \
-        }                                                                       \
-        if( i_mask & 0x03 )                               /* one from 0011 */   \
-        {                                                                       \
-            if( i_mask & 0x02 )                                    /* 0010 */   \
-            {                                                                   \
-                p_pic[6] = i_mask_color;                                        \
-                if( i_mask & 0x01 )                                /* 0001 */   \
-                {                                                               \
-                    p_pic[7] = i_mask_color;                                    \
-                }                                                               \
-            }                                                                   \
-            else                                    /* not 0010 means 0001 */   \
-            {                                                                   \
-                 p_pic[7] = i_mask_color;                                       \
-            }                                                                   \
-        }                                                                       \
-    }                                                                           \
-    else                                            /* not 1100 means 0011 */   \
-    {                                                                           \
-        if( i_mask & 0x02 )                                        /* 0010 */   \
-        {                                                                       \
-            p_pic[6] = i_mask_color;                                            \
-            if( i_mask & 0x01 )                                    /* 0001 */   \
-            {                                                                   \
-                p_pic[7] = i_mask_color;                                        \
-            }                                                                   \
-        }                                                                       \
-        else                                        /* not 0010 means 0001 */   \
-        {                                                                       \
-            p_pic[7] = i_mask_color;                                            \
-        }                                                                       \
-    }                                                                           \
+    return vout_ShowTextAbsolute( p_vout, i_channel, psz_string,
+                                  p_style, i_flags, i_hmargin, i_vmargin,
+                                  i_now, i_now + i_duration );
 }
 
-/*******************************************************************************
- * Local prototypes 
- *******************************************************************************/
-static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border, int i_bg, 
-                       u32 i_char_color, u32 i_border_color, u32 i_bg_color );
-static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg, 
-                       u32 i_char_color, u32 i_border_color, u32 i_bg_color );
-static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg, 
-                       u32 i_char_color, u32 i_border_color, u32 i_bg_color );
-
-/*******************************************************************************
- * vout_LoadFont: load a bitmap font from a file
- *******************************************************************************
- * This function will try to open a .psf font and load it. It will return
- * NULL on error.
- *******************************************************************************/
-vout_font_t *vout_LoadFont( const char *psz_name )
+/**
+ * \brief Show text on the video from a given start date to a given end date
+ * \param p_vout pointer to the vout the text is to be showed on
+ * \param i_channel Subpicture channel
+ * \param psz_string The text to be shown
+ * \param p_style Pointer to a struct with text style info
+ * \param i_flags flags for alignment and such
+ * \param i_hmargin horizontal margin in pixels
+ * \param i_vmargin vertical margin in pixels
+ * \param i_start the time when this string is to appear on the video
+ * \param i_stop the time when this string should stop to be displayed
+ *               if this is 0 the string will be shown untill the next string
+ *               is about to be shown
+ */
+int vout_ShowTextAbsolute( vout_thread_t *p_vout, int i_channel,
+                           const 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 )
 {
-    int                 i_char, i_line;          /* character and line indexes */    
-    int                 i_file;                                 /* source file */
-    byte_t              pi_buffer[2];                           /* file buffer */
-    vout_font_t *       p_font;                             /* the font itself */                
-    
-    /* Open file */
-    i_file = open( psz_name, O_RDONLY );
-    if( i_file == -1 )
-    {
-        intf_ErrMsg("error: can't open file '%s' (%s)\n", psz_name, strerror(errno));        
-        return( NULL );        
-    }
-
-    /* Read magick number */
-    if( read( i_file, pi_buffer, 2 ) != 2 )
-    {
-        intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
-        close( i_file );        
-        return( NULL );                
-    }
-
-    /* Allocate font descriptor */
-    p_font = malloc( sizeof( vout_font_t ) );
-    if( p_font == NULL )
+    subpicture_t *p_spu;
+    video_format_t fmt;
+    /* (void)p_style; FIXME: <-- why ask for this if it's unused?!? */
+
+    if( !psz_string ) return VLC_EGENERIC;
+
+    p_spu = spu_CreateSubpicture( p_vout->p_spu );
+    if( !p_spu ) return VLC_EGENERIC;
+
+    /* Create a new subpicture region */
+    memset( &fmt, 0, sizeof(video_format_t) );
+    fmt.i_chroma = VLC_FOURCC('T','E','X','T');
+    fmt.i_aspect = 0;
+    fmt.i_width = fmt.i_height = 0;
+    fmt.i_x_offset = fmt.i_y_offset = 0;
+    p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_vout), &fmt );
+    if( !p_spu->p_region )
     {
-        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
-        close( i_file );
-        return( NULL );        
+        msg_Err( p_vout, "cannot allocate SPU region" );
+        spu_DestroySubpicture( p_vout->p_spu, p_spu );
+        return VLC_EGENERIC;
     }
-    
-    /* Read file */
-    switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
-    {
-    case 0x3604:                                              /* .psf file */
-        /* 
-         * PSF font: simple fixed font. Only the first 256 characters are read.
-         * Those fonts are always 1 byte width, and 256 or 512 characters long.
-         */
 
-        /* Read font header - two bytes indicate the font properties */
-        if( read( i_file, pi_buffer, 2 ) != 2)
-        {
-            intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
-            free( p_font );            
-            close( i_file );        
-            return( NULL );                
-        }
+    p_spu->p_region->psz_text = strdup( psz_string );
+    p_spu->p_region->i_align = i_flags & SUBPICTURE_ALIGN_MASK;
+    p_spu->i_start = i_start;
+    p_spu->i_stop = i_stop;
+    p_spu->b_ephemer = true;
+    p_spu->b_absolute = false;
+    p_spu->b_fade = true;
 
-        /* Copy font properties */
-        p_font->i_type =                VOUT_FIXED_FONT;
-        p_font->i_width =               8;
-        p_font->i_height =              pi_buffer[1];        
-        p_font->i_interspacing =        8;
-        p_font->i_bytes_per_line =      1;
-        p_font->i_bytes_per_char =      pi_buffer[1];
-        p_font->i_first =               0;
-        p_font->i_last =                255;
-        
-        /* Allocate font space */
-        p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
-        if( p_font->p_data == NULL )
-        {
-            intf_ErrMsg("error: %s\n", strerror(ENOMEM));
-            free( p_font );            
-            close( i_file );
-            return( NULL );        
-        }
-        /* Copy raw data */
-        if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
-        {
-            intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
-            free( p_font->p_data );            
-            free( p_font );            
-            close( i_file );        
-            return( NULL );                
-        } 
+    p_spu->i_x = i_hmargin;
+    p_spu->i_y = i_vmargin;
+    p_spu->i_flags = i_flags & ~SUBPICTURE_ALIGN_MASK;
+    p_spu->i_channel = i_channel;
 
-        /* Computes border masks - remember that masks have the same matrix as 
-         * characters, so an empty character border is required to have a complete
-         * border mask. */
-        for( i_char = 0; i_char <= 255; i_char++ )
-        {
-            for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
-            {
-                
-                p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
-                    ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
-                     (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
-                     (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
-                     (i_line < pi_buffer[1] ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0)) 
-                    & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];          
-            }            
-        }
-        
-        break;
-    default:
-        intf_ErrMsg("error: file '%s' has an unknown format\n", psz_name );
-        free( p_font );        
-        close( i_file );
-        return( NULL );        
-        break;                
-    }        
-    
-    
-    intf_DbgMsg( "loaded %s: type %d, %d-%dx%d\n", psz_name, p_font->i_type, 
-                 p_font->i_width, p_font->i_interspacing, p_font->i_height );
-    return( p_font );
-}
+    spu_DisplaySubpicture( p_vout->p_spu, p_spu );
 
-/*******************************************************************************
- * vout_UnloadFont: unload a font
- *******************************************************************************
- * This function free the resources allocated by vout_LoadFont
- *******************************************************************************/
-void vout_UnloadFont( vout_font_t *p_font )
-{
-    intf_DbgMsg( "font %p\n", p_font );    
-    free( p_font->p_data );    
-    free( p_font );    
+    return VLC_SUCCESS;
 }
 
-/*******************************************************************************
- * vout_TextSize: return the dimensions of a text
- *******************************************************************************
- * This function is used to align text. It returns the width and height of a
- * given text. 
- *******************************************************************************/
-void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
-{
-    switch( p_font->i_type )
-    {
-    case VOUT_FIXED_FONT:        
-        *pi_width  = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) * 
-            (strlen( psz_text ) - 1) + p_font->i_width;
-        *pi_height = p_font->i_height;
-        if( i_style & ITALIC_TEXT )
-        {
-            *pi_width = *pi_height / 3;            
-        }        
-        break;
-#ifdef DEBUG
-    default:
-        intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );        
-        break;        
-#endif
-    }
-}
 
-/*******************************************************************************
- * vout_Print: low level printing function
-  *******************************************************************************
- * This function prints a text, without clipping, in a buffer using a previously
- * loaded bitmap font.
- *******************************************************************************/
-void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line, 
-                 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text )
+/**
+ * \brief Write an informative message at the default location,
+ *        for the default duration and only if the OSD option is enabled.
+ * \param p_caller The object that called the function.
+ * \param i_channel Subpicture channel
+ * \param psz_format printf style formatting
+ **/
+void __vout_OSDMessage( vlc_object_t *p_caller, int i_channel,
+                        const char *psz_format, ... )
 {
-    byte_t      *p_char, *p_border;          /* character and border mask data */    
-    int         i_char_mask, i_border_mask, i_bg_mask;                /* masks */    
-    int         i_line;                           /* current line in character */    
-    int         i_byte;                           /* current byte in character */
-    int         i_interspacing;                    /* offset between two chars */    
-    int         i_font_bytes_per_line, i_font_height;       /* font properties */    
-    vout_put_byte_t *p_PutByte;                            /* PutByte function */    
+    vout_thread_t *p_vout;
+    char *psz_string;
+    va_list args;
 
-    //?? background: can be something else that whole byte 
+    if( !config_GetInt( p_caller, "osd" ) ) return;
 
-    /* Select output function */
-    switch( i_bytes_per_pixel )
+    p_vout = vlc_object_find( p_caller, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+    if( p_vout )
     {
-    case 2:
-        p_PutByte = (vout_put_byte_t *) PutByte16;        
-        break;        
-    case 3:
-        p_PutByte = (vout_put_byte_t *) PutByte24;        
-        break;        
-    case 4:
-#ifndef DEBUG
-    default:        
-#endif
-        p_PutByte = (vout_put_byte_t *) PutByte32;        
-        break;        
-#ifdef DEBUG
-    default:
-        intf_DbgMsg("error: invalid bytes per pixel %d\n", i_bytes_per_pixel );        
-        p_PutByte = NULL;        
-        break;
-#endif
-    }    
-
-    /* Choose masks and copy font data to local variables */
-    i_char_mask =               (i_style & VOID_TEXT) ?         0 : 0xff;
-    i_border_mask =             (i_style & OUTLINED_TEXT) ?     0xff : 0;
-    i_bg_mask =                 (i_style & OPAQUE_TEXT) ?       0xff : 0;
-
-    i_font_bytes_per_line =     p_font->i_bytes_per_line;
-    i_font_height =             p_font->i_height;
-    i_interspacing =            i_bytes_per_pixel * ((i_style & WIDE_TEXT) ? 
-                                                     p_font->i_interspacing * 2 : 
-                                                     p_font->i_interspacing);
-
-    /* Print text */
-    for( ; *psz_text != '\0'; psz_text++ )
-    {
-        /* Check that the character is valid */
-        if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
-        {       
-            /* Select character - bytes per char is always valid, event for
-             * non fixed fonts */
-            p_char =    p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
-            p_border =  p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;            
-
-            /* Select base address for output */            
-            switch( p_font->i_type )
-            {
-            case VOUT_FIXED_FONT:
-                /* 
-                 * Simple fixed width font 
-                 */
-
-                /* Italic text: shift picture start right */
-                if( i_style & ITALIC_TEXT )
-                {
-                    p_pic += i_bytes_per_pixel * (p_font->i_height / 3);                    
-                }
-
-                /* Print character */
-                for( i_line = 0; i_line < i_font_height; i_line ++ )
-                {                                        
-                    for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
-                    {
-                        /* Put pixels */
-                        p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte, 
-                                   *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask, 
-                                   i_char_color, i_border_color, i_bg_color );
-                    }
-                        
-                    /* Italic text: shift picture start left */
-                    if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
-                    {                            
-                        p_pic -= i_bytes_per_pixel;
-                    }
-                }
-
-                /* Jump to next character */
-                p_pic += i_interspacing;
-                break;                
-#ifdef DEBUG
-            default:
-                intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );        
-                break;        
-#endif
-            }
-        }       
-    }    
-}
-
-/* following functions are local */
-
-/*******************************************************************************
- * PutByte16: print a fixed width font character byte in 15 or 16 bpp
- *******************************************************************************/
-static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border, int i_bg, 
-                       u32 i_char_color, u32 i_border_color, u32 i_bg_color )
-{
-    /* Computes position offset and background mask */
-    p_pic += 8 * i_byte;
-    i_bg &= ~(i_char | i_border);
-
-    /* Put character bits */
-    TREE(i_char, i_char_color);
-    TREE(i_border, i_border_color);
-    TREE(i_bg, i_bg_color);
-}
-
-/*******************************************************************************
- * PutByte24: print a fixed width font character byte in 24 bpp
- *******************************************************************************/
-static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg, 
-                       u32 i_char_color, u32 i_border_color, u32 i_bg_color )
-{
-    //??
-}
-
-/*******************************************************************************
- * PutByte32: print a fixed width font character byte in 32 bpp
- *******************************************************************************/
-static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg, 
-                       u32 i_char_color, u32 i_border_color, u32 i_bg_color )
-{
-    /* Computes position offset and background mask */
-    p_pic += 8 * i_byte;
-    i_bg &= ~(i_char | i_border);
-
-    /* Put character bits */
-    TREE(i_char, i_char_color);
-    TREE(i_border, i_border_color);
-    TREE(i_bg, i_bg_color);
+        va_start( args, psz_format );
+        vasprintf( &psz_string, psz_format, args );
+
+        vout_ShowTextRelative( p_vout, i_channel, psz_string, NULL,
+                               OSD_ALIGN_TOP|OSD_ALIGN_RIGHT,
+                               30 + p_vout->fmt_in.i_width
+                                  - p_vout->fmt_in.i_visible_width
+                                  - p_vout->fmt_in.i_x_offset,
+                               20 + p_vout->fmt_in.i_y_offset, 1000000 );
+
+        vlc_object_release( p_vout );
+        free( psz_string );
+        va_end( args );
+    }
 }
-