]> git.sesse.net Git - vlc/blob - modules/video_filter/dynamicoverlay/dynamicoverlay.c
da4d799cecf4c55b13044fb4e05d593b071ea1dc
[vlc] / modules / video_filter / dynamicoverlay / dynamicoverlay.c
1 /*****************************************************************************
2  * dynamicoverlay.c : dynamic overlay plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Author: Søren Bøg <avacore@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_sout.h>
34 #include <vlc_vout.h>
35 #include <vlc_filter.h>
36 #include <vlc_osd.h>
37
38 #include <ctype.h>
39 #include <fcntl.h>
40
41 #include "dynamicoverlay.h"
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int Create( vlc_object_t * );
47 static void Destroy( vlc_object_t * );
48 static subpicture_t *Filter( filter_t *, mtime_t );
49
50 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
51                            vlc_value_t oldval, vlc_value_t newval,
52                            void *p_data );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57
58 #define INPUT_TEXT N_("Input FIFO")
59 #define INPUT_LONGTEXT N_("FIFO which will be read for commands")
60
61 #define OUTPUT_TEXT N_("Output FIFO")
62 #define OUTPUT_LONGTEXT N_("FIFO which will be written to for responses")
63
64 vlc_module_begin();
65     set_description( N_("Dynamic video overlay") );
66     set_shortname( N_("Overlay" ));
67     set_category( CAT_VIDEO );
68     set_subcategory( SUBCAT_VIDEO_VFILTER );
69     set_capability( "sub filter", 0 );
70
71     add_file( "overlay-input", NULL, NULL, INPUT_TEXT, INPUT_LONGTEXT,
72               false );
73     add_file( "overlay-output", NULL, NULL, OUTPUT_TEXT, OUTPUT_LONGTEXT,
74               false );
75
76     add_shortcut( "overlay" );
77     set_callbacks( Create, Destroy );
78 vlc_module_end();
79
80 static const char *const ppsz_filter_options[] = {
81     "input", "output", NULL
82 };
83
84 /*****************************************************************************
85  * Create: allocates adjust video thread output method
86  *****************************************************************************
87  * This function allocates and initializes a adjust vout method.
88  *****************************************************************************/
89 static int Create( vlc_object_t *p_this )
90 {
91     filter_t *p_filter = (filter_t *)p_this;
92     filter_sys_t *p_sys;
93
94     /* Allocate structure */
95     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
96     if( p_filter->p_sys == NULL )
97     {
98         msg_Err( p_filter, "out of memory" );
99         return VLC_ENOMEM;
100     }
101     p_sys = p_filter->p_sys;
102
103     BufferInit( &p_sys->input );
104     BufferInit( &p_sys->output );
105     QueueInit( &p_sys->atomic );
106     QueueInit( &p_sys->pending );
107     QueueInit( &p_sys->processed );
108     ListInit( &p_sys->overlays );
109
110     p_sys->i_inputfd = -1;
111     p_sys->i_outputfd = -1;
112     p_sys->b_updated = true;
113     p_sys->b_atomic = false;
114
115     p_filter->pf_sub_filter = Filter;
116
117     config_ChainParse( p_filter, "overlay-", ppsz_filter_options,
118                        p_filter->p_cfg );
119
120     p_sys->psz_inputfile = var_CreateGetStringCommand( p_filter,
121                                                        "overlay-input" );
122     p_sys->psz_outputfile = var_CreateGetStringCommand( p_filter,
123                                                         "overlay-output" );
124
125     var_AddCallback( p_filter, "overlay-input", AdjustCallback, p_sys );
126     var_AddCallback( p_filter, "overlay-output", AdjustCallback, p_sys );
127
128     RegisterCommand( p_filter );
129     return VLC_SUCCESS;
130 }
131
132 /*****************************************************************************
133  * Destroy: destroy adjust video thread output method
134  *****************************************************************************
135  * Terminate an output method created by adjustCreateOutputMethod
136  *****************************************************************************/
137 static void Destroy( vlc_object_t *p_this )
138 {
139     filter_t *p_filter = (filter_t *)p_this;
140
141     BufferDestroy( &p_filter->p_sys->input );
142     BufferDestroy( &p_filter->p_sys->output );
143     QueueDestroy( &p_filter->p_sys->atomic );
144     QueueDestroy( &p_filter->p_sys->pending );
145     QueueDestroy( &p_filter->p_sys->processed );
146     ListDestroy( &p_filter->p_sys->overlays );
147     UnregisterCommand( p_filter );
148
149     free( p_filter->p_sys->psz_inputfile );
150     free( p_filter->p_sys->psz_outputfile );
151     free( p_filter->p_sys );
152 }
153
154 /*****************************************************************************
155  * Render: displays previously rendered output
156  *****************************************************************************
157  * This function send the currently rendered image to adjust modified image,
158  * waits until it is displayed and switch the two rendering buffers, preparing
159  * next frame.
160  *****************************************************************************/
161 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
162 {
163     filter_sys_t *p_sys = p_filter->p_sys;
164
165     /* We might need to open these at any time. */
166     if( p_sys->i_inputfd == -1 )
167     {
168         p_sys->i_inputfd = open( p_sys->psz_inputfile, O_RDONLY | O_NONBLOCK );
169         if( p_sys->i_inputfd == -1 )
170         {
171             msg_Warn( p_filter, "Failed to grab input file: %s (%s)",
172                       p_sys->psz_inputfile, strerror( errno ) );
173         }
174         else
175         {
176             msg_Info( p_filter, "Grabbed input file: %s",
177                       p_sys->psz_inputfile );
178         }
179     }
180
181     if( p_sys->i_outputfd == -1 )
182     {
183         p_sys->i_outputfd = open( p_sys->psz_outputfile,
184                                   O_WRONLY | O_NONBLOCK );
185         if( p_sys->i_outputfd == -1 )
186         {
187             if( errno != ENXIO )
188             {
189                 msg_Warn( p_filter, "Failed to grab output file: %s (%s)",
190                           p_sys->psz_outputfile, strerror( errno ) );
191             }
192         }
193         else
194         {
195             msg_Info( p_filter, "Grabbed output file: %s",
196                       p_sys->psz_outputfile );
197         }
198     }
199
200     /* Read any waiting commands */
201     if( p_sys->i_inputfd != -1 )
202     {
203         char p_buffer[1024];
204         ssize_t i_len = read( p_sys->i_inputfd, p_buffer, 1024 );
205         if( i_len == -1 )
206         {
207             /* We hit an error */
208             if( errno != EAGAIN )
209             {
210                 msg_Warn( p_filter, "Error on input file: %s",
211                           strerror( errno ) );
212                 close( p_sys->i_inputfd );
213                 p_sys->i_inputfd = -1;
214             }
215         }
216         else if( i_len == 0 )
217         {
218             /* We hit the end-of-file */
219         }
220         else
221         {
222             BufferAdd( &p_sys->input, p_buffer, i_len );
223         }
224     }
225
226     /* Parse any complete commands */
227     char *p_end, *p_cmd;
228     while( ( p_end = memchr( p_sys->input.p_begin, '\n',
229                              p_sys->input.i_length ) ) )
230     {
231         commanddesc_t *p_cur = NULL;
232         bool b_found = false;
233         size_t i_index = 0;
234
235         *p_end = '\0';
236         p_cmd = BufferGetToken( &p_sys->input );
237
238         msg_Info( p_filter, "Search command: %s", p_cmd );
239         for( i_index = 0; i_index < p_sys->i_commands; i_index++ )
240         {
241             p_cur = p_sys->pp_commands[i_index];
242             if( !strncmp( p_cur->psz_command, p_cmd, strlen(p_cur->psz_command) ) )
243             {
244                 p_cmd[strlen(p_cur->psz_command)] = '\0';
245                 b_found = true;
246                 break;
247             }
248         }
249
250         if( !b_found )
251         {
252             /* No matching command */
253             msg_Err( p_filter, "Got invalid command: %s", p_cmd );
254             BufferPrintf( &p_sys->output, "FAILURE: %d Invalid Command\n", VLC_EGENERIC );
255         }
256         else
257         {
258             msg_Info( p_filter, "Got valid command: %s", p_cmd );
259
260             command_t *p_cmddesc = malloc( sizeof( command_t ) );
261             if( !p_cmddesc )
262                 return NULL;
263
264             p_cmd = p_cmd + strlen(p_cur->psz_command) +1;
265             p_cmddesc->p_command = p_cur;
266             p_cmddesc->p_command->pf_parser( p_cmd, p_end,
267                                              &p_cmddesc->params );
268
269             if( ( p_cmddesc->p_command->b_atomic == true ) &&
270                 ( p_sys->b_atomic == true ) )
271                 QueueEnqueue( &p_sys->atomic, p_cmddesc );
272             else
273                 QueueEnqueue( &p_sys->pending, p_cmddesc );
274         }
275
276         BufferDel( &p_sys->input, p_end - p_sys->input.p_begin + 1 );
277     }
278
279     /* Process any pending commands */
280     command_t *p_command = NULL;
281     while( (p_command = QueueDequeue( &p_sys->pending )) )
282     {
283         p_command->i_status =
284             p_command->p_command->pf_execute( p_filter, &p_command->params,
285                                               &p_command->results );
286         QueueEnqueue( &p_sys->processed, p_command );
287     }
288
289     /* Output any processed commands */
290     while( (p_command = QueueDequeue( &p_sys->processed )) )
291     {
292         if( p_command->i_status == VLC_SUCCESS )
293         {
294             const char *psz_success = "SUCCESS:";
295             const char *psz_nl = "\n";
296             BufferAdd( &p_sys->output, psz_success, 8 );
297             p_command->p_command->pf_unparse( &p_command->results,
298                                               &p_sys->output );
299             BufferAdd( &p_sys->output, psz_nl, 1 );
300         }
301         else
302         {
303             BufferPrintf( &p_sys->output, "FAILURE: %d\n",
304                           p_command->i_status );
305         }
306     }
307
308     /* Try emptying the output buffer */
309     if( p_sys->i_outputfd != -1 )
310     {
311         ssize_t i_len = write( p_sys->i_outputfd, p_sys->output.p_begin,
312                               p_sys->output.i_length );
313         if( i_len == -1 )
314         {
315             /* We hit an error */
316             if( errno != EAGAIN )
317             {
318                 msg_Warn( p_filter, "Error on output file: %s",
319                           strerror( errno ) );
320                 close( p_sys->i_outputfd );
321                 p_sys->i_outputfd = -1;
322             }
323         }
324         else
325         {
326             BufferDel( &p_sys->output, i_len );
327         }
328     }
329
330     if( p_sys->b_updated == false )
331         return NULL;
332
333     subpicture_t *p_spu = NULL;
334     overlay_t *p_overlay = NULL;
335
336     p_spu = p_filter->pf_sub_buffer_new( p_filter );
337     if( !p_spu )
338     {
339         msg_Err( p_filter, "cannot allocate subpicture" );
340         return NULL;
341     }
342
343     p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
344     p_spu->i_x = 0;
345     p_spu->i_y = 0;
346     p_spu->b_absolute = true;
347     p_spu->i_start = date;
348     p_spu->i_stop = 0;
349     p_spu->b_ephemer = true;
350
351     subpicture_region_t **pp_region = &p_spu->p_region;
352     while( (p_overlay = ListWalk( &p_sys->overlays )) )
353     {
354         msg_Dbg( p_filter, "Displaying overlay: %4.4s, %d, %d, %d",
355                  (char*)&p_overlay->format.i_chroma, p_overlay->i_x, p_overlay->i_y,
356                  p_overlay->i_alpha );
357
358         if( p_overlay->format.i_chroma == VLC_FOURCC('T','E','X','T') )
359         {
360             *pp_region = p_spu->pf_create_region( VLC_OBJECT(p_filter),
361                                                   &p_overlay->format );
362             if( !*pp_region )
363                 break;
364             (*pp_region)->psz_text = strdup( p_overlay->data.p_text );
365             (*pp_region)->p_style = malloc( sizeof(struct text_style_t) );
366             if( !(*pp_region)->p_style )
367             {
368                 p_spu->pf_destroy_region( VLC_OBJECT(p_filter), (*pp_region) );
369                 *pp_region = NULL;
370                 break;
371             }
372             vlc_memcpy( (*pp_region)->p_style, &p_overlay->fontstyle,
373                         sizeof(struct text_style_t) );
374         }
375         else
376         {
377             picture_t clone;
378             if( vout_AllocatePicture( p_filter, &clone,
379                                       p_overlay->format.i_chroma,
380                                       p_overlay->format.i_width,
381                                       p_overlay->format.i_height,
382                                       p_overlay->format.i_aspect ) )
383             {
384                 msg_Err( p_filter, "cannot allocate picture" );
385                 continue;
386             }
387             vout_CopyPicture( p_filter, &clone, p_overlay->data.p_pic );
388             *pp_region = p_spu->pf_make_region( VLC_OBJECT(p_filter),
389                                                 &p_overlay->format,
390                                                 &clone );
391             if( !*pp_region )
392             {
393                 msg_Err( p_filter, "cannot allocate subpicture region" );
394                 continue;
395             }
396         }
397         (*pp_region)->i_x = p_overlay->i_x;
398         (*pp_region)->i_y = p_overlay->i_y;
399         (*pp_region)->i_align = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
400         (*pp_region)->i_alpha = p_overlay->i_alpha;
401         pp_region = &(*pp_region)->p_next;
402     }
403
404     p_sys->b_updated = false;
405     return p_spu;
406 }
407
408 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
409                            vlc_value_t oldval, vlc_value_t newval,
410                            void *p_data )
411 {
412     filter_sys_t *p_sys = (filter_sys_t *)p_data;
413     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
414
415     if( !strncmp( psz_var, "overlay-input", 13 ) )
416         p_sys->psz_inputfile = newval.psz_string;
417     else if( !strncmp( psz_var, "overlay-output", 14 ) )
418         p_sys->psz_outputfile = newval.psz_string;
419
420     return VLC_EGENERIC;
421 }