]> git.sesse.net Git - vlc/blob - modules/video_filter/marq.c
video_filter/marq.c: converted to unix file format
[vlc] / modules / video_filter / marq.c
1 /*****************************************************************************
2  * marq.c : marquee display video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id: time.c 8751 2004-09-20 21:51:41Z gbazin $
6  *
7  * Authors: Mark Moriarty
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include "vlc_filter.h"
34 #include "vlc_block.h"
35 #include "osd.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  CreateFilter ( vlc_object_t * );
41 static void DestroyFilter( vlc_object_t * );
42 static subpicture_t *Filter( filter_t *, mtime_t );
43
44
45 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
46                             vlc_value_t oldval, vlc_value_t newval,
47                             void *p_data );
48
49 /*****************************************************************************
50  * filter_sys_t: marquee filter descriptor
51  *****************************************************************************/
52 struct filter_sys_t
53 {
54     int i_xoff, i_yoff;  /* offsets for the display string in the video window */
55     int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
56     int i_timeout;
57
58     char *psz_marquee;    /* marquee string */
59
60     time_t last_time;
61     vlc_bool_t b_absolute; /* position control, relative vs. absolute */
62
63     vlc_bool_t b_need_update;
64 };
65
66 #define MSG_TEXT N_("Marquee text")
67 #define MSG_LONGTEXT N_("Marquee text to display")
68 #define POSX_TEXT N_("X offset, from left")
69 #define POSX_LONGTEXT N_("X offset, from the left screen edge" )
70 #define POSY_TEXT N_("Y offset, from the top")
71 #define POSY_LONGTEXT N_("Y offset, down from the top" )
72 #define TIMEOUT_TEXT N_("Marquee timeout")
73 #define TIMEOUT_LONGTEXT N_("Defines the time the marquee must remain " \
74                             "displayed, in milliseconds. Default value is " \
75                             "0 (remain forever).")
76 #define POS_TEXT N_("Marquee position")
77 #define POS_LONGTEXT N_( \
78   "You can enforce the marquee position on the video " \
79   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
80   "also use combinations of these values by adding them).")
81
82 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
83 static char *ppsz_pos_descriptions[] =
84 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
85   N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
86
87 /*****************************************************************************
88  * Module descriptor
89  *****************************************************************************/
90 vlc_module_begin();
91     set_capability( "sub filter", 0 );
92     set_shortname( N_("Marquee" ));
93     set_callbacks( CreateFilter, DestroyFilter );
94     set_category( CAT_VIDEO );
95     set_subcategory( SUBCAT_VIDEO_SUBPIC );
96     add_string( "marq-marquee", "Marquee", NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );
97     add_integer( "marq-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
98     add_integer( "marq-y", -1, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
99     add_integer( "marq-timeout", 0, NULL, TIMEOUT_TEXT, TIMEOUT_LONGTEXT,
100                  VLC_FALSE );
101     add_integer( "marq-position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
102     /* 5 sets the default to top [1] left [4] */
103     change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
104     set_description( _("Marquee display sub filter") );
105     add_shortcut( "marq" );
106 vlc_module_end();
107
108 /*****************************************************************************
109  * CreateFilter: allocates marquee video filter
110  *****************************************************************************/
111 static int CreateFilter( vlc_object_t *p_this )
112 {
113     filter_t *p_filter = (filter_t *)p_this;
114     filter_sys_t *p_sys;
115     vlc_object_t *p_input;
116
117     /* Allocate structure */
118     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
119     if( p_sys == NULL )
120     {
121         msg_Err( p_filter, "out of memory" );
122         return VLC_ENOMEM;
123     }
124
125     /* Hook used for callback variables */
126     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
127     if( !p_input )
128     {
129         return VLC_ENOOBJ;
130     }
131
132     p_sys->i_xoff = var_CreateGetInteger( p_input->p_libvlc , "marq-x" );
133     p_sys->i_yoff = var_CreateGetInteger( p_input->p_libvlc , "marq-y" );
134     p_sys->i_timeout = var_CreateGetInteger( p_input->p_libvlc , "marq-timeout" );
135     p_sys->i_pos = var_CreateGetInteger( p_input->p_libvlc , "marq-position" );
136     p_sys->psz_marquee =  var_CreateGetString( p_input->p_libvlc, "marq-marquee" );
137
138     var_AddCallback( p_input->p_libvlc, "marq-x", MarqueeCallback, p_sys );
139     var_AddCallback( p_input->p_libvlc, "marq-y", MarqueeCallback, p_sys );
140     var_AddCallback( p_input->p_libvlc, "marq-marquee", MarqueeCallback, p_sys );
141     var_AddCallback( p_input->p_libvlc, "marq-timeout", MarqueeCallback, p_sys );
142     var_AddCallback( p_input->p_libvlc, "marq-position", MarqueeCallback, p_sys );
143
144     vlc_object_release( p_input );
145
146     p_sys->b_absolute = VLC_TRUE;
147     if( p_sys->i_xoff < 0 || p_sys->i_yoff < 0 )
148     {
149         p_sys->b_absolute = VLC_FALSE;
150         p_sys->i_xoff = 0; p_sys->i_yoff = 0;
151     }
152
153     /* Misc init */
154     p_filter->pf_sub_filter = Filter;
155     p_sys->last_time = ((time_t)-1);
156     p_sys->b_need_update = VLC_TRUE;
157
158     return VLC_SUCCESS;
159 }
160 /*****************************************************************************
161  * DestroyFilter: destroy marquee video filter
162  *****************************************************************************/
163 static void DestroyFilter( vlc_object_t *p_this )
164 {
165     filter_t *p_filter = (filter_t *)p_this;
166     filter_sys_t *p_sys = p_filter->p_sys;
167     vlc_object_t *p_input;
168
169     if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
170     free( p_sys );
171
172     /* Delete the marquee variables from playlist */
173     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
174     if( !p_input )
175     {
176         return;
177     }
178     var_Destroy( p_input->p_libvlc , "marq-marquee" );
179     var_Destroy( p_input->p_libvlc , "marq-x" );
180     var_Destroy( p_input->p_libvlc , "marq-y" );
181     var_Destroy( p_input->p_libvlc , "marq-timeout" );
182     var_Destroy( p_input->p_libvlc , "marq-position" );
183     vlc_object_release( p_input );
184 }
185
186 /****************************************************************************
187  * Filter: the whole thing
188  ****************************************************************************
189  * This function outputs subpictures at regular time intervals.
190  ****************************************************************************/
191 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
192 {
193     filter_sys_t *p_sys = p_filter->p_sys;
194     subpicture_t *p_spu;
195     video_format_t fmt;
196     time_t t;
197
198     if( p_sys->last_time == time( NULL ) )
199     {
200         return NULL;
201     }
202
203     if( p_sys->b_need_update == VLC_FALSE )
204     {
205         return NULL;
206     }
207
208     p_spu = p_filter->pf_sub_buffer_new( p_filter );
209     if( !p_spu ) return NULL;
210
211     p_spu->b_absolute = p_sys->b_absolute;
212     memset( &fmt, 0, sizeof(video_format_t) );
213     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
214     fmt.i_aspect = 0;
215     fmt.i_width = fmt.i_height = 0;
216     fmt.i_x_offset = 0;
217     fmt.i_y_offset = 0;
218
219     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
220     if( !p_spu->p_region )
221     {
222         p_filter->pf_sub_buffer_del( p_filter, p_spu );
223         return NULL;
224     }
225
226     t = p_sys->last_time = time( NULL );
227
228     p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);
229     p_spu->i_start = date;
230     p_spu->i_stop  = p_sys->i_timeout == 0 ? 0 : date + p_sys->i_timeout * 1000;
231     p_spu->b_ephemer = VLC_TRUE;
232     p_spu->i_x = p_sys->i_xoff;
233     p_spu->i_y = p_sys->i_yoff;
234
235     p_spu->i_flags = p_sys->i_pos;
236
237     p_sys->b_need_update = VLC_FALSE;
238     return p_spu;
239 }
240
241 /**********************************************************************
242  * Callback to update params on the fly
243  **********************************************************************/
244 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
245                             vlc_value_t oldval, vlc_value_t newval,
246                             void *p_data )
247 {
248     filter_sys_t *p_sys = (filter_sys_t *) p_data;
249
250     if( !strncmp( psz_var, "marq-marquee", 7 ) )
251     {
252         if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
253         p_sys->psz_marquee = strdup( newval.psz_string );
254     }
255     else if ( !strncmp( psz_var, "marq-x", 6 ) )
256     {
257         p_sys->i_xoff = newval.i_int;
258     }
259     else if ( !strncmp( psz_var, "marq-y", 6 ) )
260     {
261         p_sys->i_yoff = newval.i_int;
262     }
263     else if ( !strncmp( psz_var, "marq-timeout", 12 ) )
264     {
265         p_sys->i_timeout = newval.i_int;
266     }
267     else if ( !strncmp( psz_var, "marq-position", 8 ) )
268     /* willing to accept a match against marq-pos */
269     {
270         p_sys->i_pos = newval.i_int;
271     }
272     p_sys->b_need_update = VLC_TRUE;
273     return VLC_SUCCESS;
274 }