]> git.sesse.net Git - vlc/blob - modules/video_filter/dynamicoverlay/dynamicoverlay.c
Fix encoding (and writing).
[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         return VLC_ENOMEM;
98     p_sys = p_filter->p_sys;
99
100     BufferInit( &p_sys->input );
101     BufferInit( &p_sys->output );
102     QueueInit( &p_sys->atomic );
103     QueueInit( &p_sys->pending );
104     QueueInit( &p_sys->processed );
105     ListInit( &p_sys->overlays );
106
107     p_sys->i_inputfd = -1;
108     p_sys->i_outputfd = -1;
109     p_sys->b_updated = true;
110     p_sys->b_atomic = false;
111     vlc_mutex_init( &p_sys->lock );
112
113     p_filter->pf_sub_filter = Filter;
114
115     config_ChainParse( p_filter, "overlay-", ppsz_filter_options,
116                        p_filter->p_cfg );
117
118     p_sys->psz_inputfile = var_CreateGetStringCommand( p_filter,
119                                                        "overlay-input" );
120     p_sys->psz_outputfile = var_CreateGetStringCommand( p_filter,
121                                                         "overlay-output" );
122
123     var_AddCallback( p_filter, "overlay-input", AdjustCallback, p_sys );
124     var_AddCallback( p_filter, "overlay-output", AdjustCallback, p_sys );
125
126     RegisterCommand( p_filter );
127     return VLC_SUCCESS;
128 }
129
130 /*****************************************************************************
131  * Destroy: destroy adjust video thread output method
132  *****************************************************************************
133  * Terminate an output method created by adjustCreateOutputMethod
134  *****************************************************************************/
135 static void Destroy( vlc_object_t *p_this )
136 {
137     filter_t *p_filter = (filter_t *)p_this;
138     filter_sys_t *p_sys = p_filter->p_sys;
139
140     BufferDestroy( &p_sys->input );
141     BufferDestroy( &p_sys->output );
142     QueueDestroy( &p_sys->atomic );
143     QueueDestroy( &p_sys->pending );
144     QueueDestroy( &p_sys->processed );
145     ListDestroy( &p_sys->overlays );
146     UnregisterCommand( p_filter );
147
148     var_DelCallback( p_filter, "overlay-input", AdjustCallback, p_sys );
149     var_DelCallback( p_filter, "overlay-output", AdjustCallback, p_sys );
150
151     vlc_mutex_destroy( &p_sys->lock );
152     free( p_sys->psz_inputfile );
153     free( p_sys->psz_outputfile );
154     free( p_sys );
155 }
156
157 /*****************************************************************************
158  * Render: displays previously rendered output
159  *****************************************************************************
160  * This function send the currently rendered image to adjust modified image,
161  * waits until it is displayed and switch the two rendering buffers, preparing
162  * next frame.
163  *****************************************************************************/
164 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
165 {
166     filter_sys_t *p_sys = p_filter->p_sys;
167
168     /* We might need to open these at any time. */
169     vlc_mutex_lock( &p_sys->lock );
170     if( p_sys->i_inputfd == -1 )
171     {
172         p_sys->i_inputfd = open( p_sys->psz_inputfile, O_RDONLY | O_NONBLOCK );
173         if( p_sys->i_inputfd == -1 )
174         {
175             msg_Warn( p_filter, "Failed to grab input file: %s (%s)",
176                       p_sys->psz_inputfile, strerror( errno ) );
177         }
178         else
179         {
180             msg_Info( p_filter, "Grabbed input file: %s",
181                       p_sys->psz_inputfile );
182         }
183     }
184
185     if( p_sys->i_outputfd == -1 )
186     {
187         p_sys->i_outputfd = open( p_sys->psz_outputfile,
188                                   O_WRONLY | O_NONBLOCK );
189         if( p_sys->i_outputfd == -1 )
190         {
191             if( errno != ENXIO )
192             {
193                 msg_Warn( p_filter, "Failed to grab output file: %s (%s)",
194                           p_sys->psz_outputfile, strerror( errno ) );
195             }
196         }
197         else
198         {
199             msg_Info( p_filter, "Grabbed output file: %s",
200                       p_sys->psz_outputfile );
201         }
202     }
203     vlc_mutex_unlock( &p_sys->lock );
204
205     /* Read any waiting commands */
206     if( p_sys->i_inputfd != -1 )
207     {
208         char p_buffer[1024];
209         ssize_t i_len = read( p_sys->i_inputfd, p_buffer, 1024 );
210         if( i_len == -1 )
211         {
212             /* We hit an error */
213             if( errno != EAGAIN )
214             {
215                 msg_Warn( p_filter, "Error on input file: %s",
216                           strerror( errno ) );
217                 close( p_sys->i_inputfd );
218                 p_sys->i_inputfd = -1;
219             }
220         }
221         else if( i_len == 0 )
222         {
223             /* We hit the end-of-file */
224         }
225         else
226         {
227             BufferAdd( &p_sys->input, p_buffer, i_len );
228         }
229     }
230
231     /* Parse any complete commands */
232     char *p_end, *p_cmd;
233     while( ( p_end = memchr( p_sys->input.p_begin, '\n',
234                              p_sys->input.i_length ) ) )
235     {
236         commanddesc_t *p_cur = NULL;
237         bool b_found = false;
238         size_t i_index = 0;
239
240         *p_end = '\0';
241         p_cmd = BufferGetToken( &p_sys->input );
242
243         msg_Info( p_filter, "Search command: %s", p_cmd );
244         for( i_index = 0; i_index < p_sys->i_commands; i_index++ )
245         {
246             p_cur = p_sys->pp_commands[i_index];
247             if( !strncmp( p_cur->psz_command, p_cmd, strlen(p_cur->psz_command) ) )
248             {
249                 p_cmd[strlen(p_cur->psz_command)] = '\0';
250                 b_found = true;
251                 break;
252             }
253         }
254
255         if( !b_found )
256         {
257             /* No matching command */
258             msg_Err( p_filter, "Got invalid command: %s", p_cmd );
259             BufferPrintf( &p_sys->output, "FAILURE: %d Invalid Command\n", VLC_EGENERIC );
260         }
261         else
262         {
263             msg_Info( p_filter, "Got valid command: %s", p_cmd );
264
265             command_t *p_cmddesc = malloc( sizeof( command_t ) );
266             if( !p_cmddesc )
267                 return NULL;
268
269             p_cmd = p_cmd + strlen(p_cur->psz_command) +1;
270             p_cmddesc->p_command = p_cur;
271             p_cmddesc->p_command->pf_parser( p_cmd, p_end,
272                                              &p_cmddesc->params );
273
274             if( ( p_cmddesc->p_command->b_atomic == true ) &&
275                 ( p_sys->b_atomic == true ) )
276                 QueueEnqueue( &p_sys->atomic, p_cmddesc );
277             else
278                 QueueEnqueue( &p_sys->pending, p_cmddesc );
279         }
280
281         BufferDel( &p_sys->input, p_end - p_sys->input.p_begin + 1 );
282     }
283
284     /* Process any pending commands */
285     command_t *p_command = NULL;
286     while( (p_command = QueueDequeue( &p_sys->pending )) )
287     {
288         p_command->i_status =
289             p_command->p_command->pf_execute( p_filter, &p_command->params,
290                                               &p_command->results );
291         QueueEnqueue( &p_sys->processed, p_command );
292     }
293
294     /* Output any processed commands */
295     while( (p_command = QueueDequeue( &p_sys->processed )) )
296     {
297         if( p_command->i_status == VLC_SUCCESS )
298         {
299             const char *psz_success = "SUCCESS:";
300             const char *psz_nl = "\n";
301             BufferAdd( &p_sys->output, psz_success, 8 );
302             p_command->p_command->pf_unparse( &p_command->results,
303                                               &p_sys->output );
304             BufferAdd( &p_sys->output, psz_nl, 1 );
305         }
306         else
307         {
308             BufferPrintf( &p_sys->output, "FAILURE: %d\n",
309                           p_command->i_status );
310         }
311     }
312
313     /* Try emptying the output buffer */
314     if( p_sys->i_outputfd != -1 )
315     {
316         ssize_t i_len = write( p_sys->i_outputfd, p_sys->output.p_begin,
317                               p_sys->output.i_length );
318         if( i_len == -1 )
319         {
320             /* We hit an error */
321             if( errno != EAGAIN )
322             {
323                 msg_Warn( p_filter, "Error on output file: %s",
324                           strerror( errno ) );
325                 close( p_sys->i_outputfd );
326                 p_sys->i_outputfd = -1;
327             }
328         }
329         else
330         {
331             BufferDel( &p_sys->output, i_len );
332         }
333     }
334
335     if( p_sys->b_updated == false )
336         return NULL;
337
338     subpicture_t *p_spu = NULL;
339     overlay_t *p_overlay = NULL;
340
341     p_spu = p_filter->pf_sub_buffer_new( p_filter );
342     if( !p_spu )
343     {
344         msg_Err( p_filter, "cannot allocate subpicture" );
345         return NULL;
346     }
347
348     p_spu->b_absolute = true;
349     p_spu->i_start = date;
350     p_spu->i_stop = 0;
351     p_spu->b_ephemer = true;
352
353     subpicture_region_t **pp_region = &p_spu->p_region;
354     while( (p_overlay = ListWalk( &p_sys->overlays )) )
355     {
356         subpicture_region_t *p_region;
357
358         *pp_region = p_region = subpicture_region_New( &p_overlay->format );
359         if( !p_region )
360             break;
361
362         msg_Dbg( p_filter, "Displaying overlay: %4.4s, %d, %d, %d",
363                  (char*)&p_overlay->format.i_chroma, p_overlay->i_x, p_overlay->i_y,
364                  p_overlay->i_alpha );
365
366         if( p_overlay->format.i_chroma == VLC_CODEC_TEXT )
367         {
368             p_region->psz_text = strdup( p_overlay->data.p_text );
369             p_region->p_style = text_style_Duplicate( p_overlay->p_fontstyle );
370         }
371         else
372         {
373             /* FIXME the copy is probably not needed anymore */
374             picture_Copy( p_region->p_picture, p_overlay->data.p_pic );
375         }
376         p_region->i_x = p_overlay->i_x;
377         p_region->i_y = p_overlay->i_y;
378         p_region->i_align = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
379         p_region->i_alpha = p_overlay->i_alpha;
380         pp_region = &p_region->p_next;
381     }
382
383     p_sys->b_updated = false;
384     return p_spu;
385 }
386
387 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
388                            vlc_value_t oldval, vlc_value_t newval,
389                            void *p_data )
390 {
391     filter_sys_t *p_sys = (filter_sys_t *)p_data;
392     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
393
394     vlc_mutex_lock( &p_sys->lock );
395     if( !strncmp( psz_var, "overlay-input", 13 ) )
396     {
397         free( p_sys->psz_inputfile );
398         p_sys->psz_inputfile = strdup( newval.psz_string );
399     }
400     else if( !strncmp( psz_var, "overlay-output", 14 ) )
401     {
402         free( p_sys->psz_outputfile );
403         p_sys->psz_outputfile = strdup( newval.psz_string );
404     }
405     vlc_mutex_unlock( &p_sys->lock );
406
407     return VLC_EGENERIC;
408 }