]> git.sesse.net Git - vlc/blob - plugins/filter/wall.c
211f8abebcf67065aa7ed2863d14c997c727a2f0
[vlc] / plugins / filter / wall.c
1 /*****************************************************************************
2  * wall.c : Wall video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: wall.c,v 1.16 2002/04/19 13:56:11 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <videolan/vlc.h>
32
33 #include "video.h"
34 #include "video_output.h"
35
36 #include "filter_common.h"
37
38 /*****************************************************************************
39  * Capabilities defined in the other files.
40  *****************************************************************************/
41 static void vout_getfunctions( function_list_t * p_function_list );
42
43 /*****************************************************************************
44  * Build configuration tree.
45  *****************************************************************************/
46 MODULE_CONFIG_START
47 MODULE_CONFIG_STOP
48
49 MODULE_INIT_START
50     SET_DESCRIPTION( _("image wall video module") )
51     /* Capability score set to 0 because we don't want to be spawned
52      * as a video output unless explicitly requested to */
53     ADD_CAPABILITY( VOUT, 0 )
54     ADD_SHORTCUT( "wall" )
55 MODULE_INIT_STOP
56
57 MODULE_ACTIVATE_START
58     vout_getfunctions( &p_module->p_functions->vout );
59 MODULE_ACTIVATE_STOP
60
61 MODULE_DEACTIVATE_START
62 MODULE_DEACTIVATE_STOP
63
64 /*****************************************************************************
65  * vout_sys_t: Wall video output method descriptor
66  *****************************************************************************
67  * This structure is part of the video output thread descriptor.
68  * It describes the Wall specific properties of an output thread.
69  *****************************************************************************/
70 typedef struct vout_sys_s
71 {
72     int    i_col;
73     int    i_row;
74     int    i_vout;
75     struct vout_list_s
76     {
77         boolean_t b_active;
78         int i_width;
79         int i_height;
80         struct vout_thread_s *p_vout;
81     } *pp_vout;
82
83 } vout_sys_t;
84
85 /*****************************************************************************
86  * Local prototypes
87  *****************************************************************************/
88 static int  vout_Create    ( vout_thread_t * );
89 static int  vout_Init      ( vout_thread_t * );
90 static void vout_End       ( vout_thread_t * );
91 static void vout_Destroy   ( vout_thread_t * );
92 static int  vout_Manage    ( vout_thread_t * );
93 static void vout_Render    ( vout_thread_t *, struct picture_s * );
94 static void vout_Display   ( vout_thread_t *, struct picture_s * );
95
96 static void RemoveAllVout  ( vout_thread_t *p_vout );
97
98 /*****************************************************************************
99  * Functions exported as capabilities. They are declared as static so that
100  * we don't pollute the namespace too much.
101  *****************************************************************************/
102 static void vout_getfunctions( function_list_t * p_function_list )
103 {
104     p_function_list->functions.vout.pf_create     = vout_Create;
105     p_function_list->functions.vout.pf_init       = vout_Init;
106     p_function_list->functions.vout.pf_end        = vout_End;
107     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
108     p_function_list->functions.vout.pf_manage     = vout_Manage;
109     p_function_list->functions.vout.pf_render     = vout_Render;
110     p_function_list->functions.vout.pf_display    = vout_Display;
111 }
112
113 /*****************************************************************************
114  * vout_Create: allocates Wall video thread output method
115  *****************************************************************************
116  * This function allocates and initializes a Wall vout method.
117  *****************************************************************************/
118 static int vout_Create( vout_thread_t *p_vout )
119 {
120     char *psz_method, *psz_tmp, *psz_method_tmp;
121     int i_vout;
122
123     /* Allocate structure */
124     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
125     if( p_vout->p_sys == NULL )
126     {
127         intf_ErrMsg("error: %s", strerror(ENOMEM) );
128         return( 1 );
129     }
130
131     /* Look what method was requested */
132     if( !(psz_method = psz_method_tmp
133           = config_GetPszVariable( "filter" )) )
134     {
135         intf_ErrMsg( "vout error: configuration variable %s empty",
136                      "filter" );
137         return( 1 );
138     }
139
140     while( *psz_method && *psz_method != ':' )
141     {
142         psz_method++;
143     }
144
145     if( *psz_method )
146     {
147         psz_method++;
148         psz_tmp = psz_method;
149
150         while( *psz_tmp && *psz_tmp != 'x' && *psz_tmp != ':' )
151         {
152             psz_tmp++;
153         }
154
155         if( *psz_tmp == 'x' )
156         {
157            *psz_tmp = '\0';
158            p_vout->p_sys->i_col = atoi( psz_method );
159
160            psz_tmp++;
161            psz_method = psz_tmp;
162
163            while(  *psz_tmp && *psz_tmp != ':' )
164            {
165               psz_tmp++;
166            }
167
168            if( *psz_tmp )
169            {
170                *psz_tmp = '\0';
171                p_vout->p_sys->i_row = atoi( psz_method );
172                psz_method = psz_tmp + 1;
173            }
174            else
175            {
176                p_vout->p_sys->i_row = atoi( psz_method );
177                psz_method = NULL;
178            }
179         }
180         else if( *psz_tmp == ':' )
181         { 
182             p_vout->p_sys->i_col = p_vout->p_sys->i_row = atoi( psz_method );
183             psz_method = psz_tmp + 1;
184         }
185         else
186         { 
187             p_vout->p_sys->i_col = p_vout->p_sys->i_row = atoi( psz_method );
188             psz_method = NULL;
189         }
190     }
191     else
192     {
193         intf_ErrMsg( "filter error: no valid wall size provided, "
194                      "using wall:3x3" );
195         p_vout->p_sys->i_col = 3;
196         p_vout->p_sys->i_row = 3;
197         psz_method = NULL;
198     }
199
200     p_vout->p_sys->i_col = __MAX( 1, __MIN( 15, p_vout->p_sys->i_col ) );
201     p_vout->p_sys->i_row = __MAX( 1, __MIN( 15, p_vout->p_sys->i_row ) );
202
203     intf_WarnMsg( 3, "filter info: opening a %i x %i wall",
204                   p_vout->p_sys->i_col, p_vout->p_sys->i_row );
205
206     p_vout->p_sys->pp_vout = malloc( p_vout->p_sys->i_row *
207                                      p_vout->p_sys->i_col *
208                                      sizeof(struct vout_list_s) );
209     if( p_vout->p_sys->pp_vout == NULL )
210     {
211         intf_ErrMsg("error: %s", strerror(ENOMEM) );
212         free( psz_method_tmp );
213         free( p_vout->p_sys );
214         return( 1 );
215     }
216
217     /* If no trailing vout are specified, take them all */
218     if( psz_method == NULL )
219     {
220         for( i_vout = p_vout->p_sys->i_row * p_vout->p_sys->i_col;
221              i_vout--; )
222         {
223             p_vout->p_sys->pp_vout[i_vout].b_active = 1;
224         }
225     }
226     /* If trailing vout are specified, activate only the requested ones */
227     else
228     {
229         for( i_vout = p_vout->p_sys->i_row * p_vout->p_sys->i_col;
230              i_vout--; )
231         {
232             p_vout->p_sys->pp_vout[i_vout].b_active = 0;
233         }
234
235         while( *psz_method )
236         {
237             psz_tmp = psz_method;
238             while( *psz_tmp && *psz_tmp != ',' )
239             {
240                 psz_tmp++;
241             }
242
243             if( *psz_tmp )
244             {
245                 *psz_tmp = '\0';
246                 i_vout = atoi( psz_method );
247                 psz_method = psz_tmp + 1;
248             }
249             else
250             {
251                 i_vout = atoi( psz_method );
252                 psz_method = psz_tmp;
253             }
254
255             if( i_vout >= 0 &&
256                 i_vout < p_vout->p_sys->i_row * p_vout->p_sys->i_col )
257             {
258                 p_vout->p_sys->pp_vout[i_vout].b_active = 1;
259             }
260         }
261     }
262
263     free( psz_method_tmp );
264
265     return( 0 );
266 }
267
268 /*****************************************************************************
269  * vout_Init: initialize Wall video thread output method
270  *****************************************************************************/
271 static int vout_Init( vout_thread_t *p_vout )
272 {
273     int i_index, i_row, i_col, i_width, i_height;
274     char *psz_filter;
275     picture_t *p_pic;
276     
277     I_OUTPUTPICTURES = 0;
278
279     /* Initialize the output structure */
280     p_vout->output.i_chroma = p_vout->render.i_chroma;
281     p_vout->output.i_width  = p_vout->render.i_width;
282     p_vout->output.i_height = p_vout->render.i_height;
283     p_vout->output.i_aspect = p_vout->render.i_aspect;
284
285     /* Try to open the real video output */
286     psz_filter = config_GetPszVariable( "filter" );
287     config_PutPszVariable( "filter", NULL );
288
289     intf_WarnMsg( 1, "filter: spawning the real video outputs" );
290
291     p_vout->p_sys->i_vout = 0;
292
293     /* FIXME: use bresenham instead of those ugly divisions */
294     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
295     {
296         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
297         {
298             if( i_col + 1 < p_vout->p_sys->i_col )
299             {
300                 i_width = ( p_vout->render.i_width
301                              / p_vout->p_sys->i_col ) & ~0x1;
302             }
303             else
304             {
305                 i_width = p_vout->render.i_width
306                            - ( ( p_vout->render.i_width
307                                   / p_vout->p_sys->i_col ) & ~0x1 ) * i_col;
308             }
309
310             if( i_row + 1 < p_vout->p_sys->i_row )
311             {
312                 i_height = ( p_vout->render.i_height
313                               / p_vout->p_sys->i_row ) & ~0x3;
314             }
315             else
316             {
317                 i_height = p_vout->render.i_height
318                             - ( ( p_vout->render.i_height
319                                    / p_vout->p_sys->i_row ) & ~0x3 ) * i_row;
320             }
321
322             p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].i_width = i_width;
323             p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].i_height = i_height;
324
325             if( !p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].b_active )
326             {
327                 p_vout->p_sys->i_vout++;
328                 continue;
329             }
330
331             p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout =
332                 vout_CreateThread( NULL, i_width, i_height,
333                                    p_vout->render.i_chroma,
334                                    p_vout->render.i_aspect
335                                     * p_vout->render.i_height / i_height
336                                     * i_width / p_vout->render.i_width );
337             if( p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout == NULL )
338             {
339                 intf_ErrMsg( "vout error: failed to get %ix%i vout threads",
340                              p_vout->p_sys->i_col, p_vout->p_sys->i_row );
341                 RemoveAllVout( p_vout );
342                 config_PutPszVariable( "filter", psz_filter );
343                 if( psz_filter ) free( psz_filter );
344                 return 0;
345             }
346
347             p_vout->p_sys->i_vout++;
348         }
349     }
350
351     config_PutPszVariable( "filter", psz_filter );
352     if( psz_filter ) free( psz_filter );
353
354     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
355
356     return( 0 );
357 }
358
359 /*****************************************************************************
360  * vout_End: terminate Wall video thread output method
361  *****************************************************************************/
362 static void vout_End( vout_thread_t *p_vout )
363 {
364     int i_index;
365
366     /* Free the fake output buffers we allocated */
367     for( i_index = I_OUTPUTPICTURES ; i_index ; )
368     {
369         i_index--;
370         free( PP_OUTPUTPICTURE[ i_index ]->p_data );
371     }
372 }
373
374 /*****************************************************************************
375  * vout_Destroy: destroy Wall video thread output method
376  *****************************************************************************
377  * Terminate an output method created by WallCreateOutputMethod
378  *****************************************************************************/
379 static void vout_Destroy( vout_thread_t *p_vout )
380 {
381     RemoveAllVout( p_vout );
382
383     free( p_vout->p_sys->pp_vout );
384     free( p_vout->p_sys );
385 }
386
387 /*****************************************************************************
388  * vout_Manage: handle Wall events
389  *****************************************************************************
390  * This function should be called regularly by video output thread. It manages
391  * console events. It returns a non null value on error.
392  *****************************************************************************/
393 static int vout_Manage( vout_thread_t *p_vout )
394 {
395     return( 0 );
396 }
397
398 /*****************************************************************************
399  * vout_Render: displays previously rendered output
400  *****************************************************************************
401  * This function send the currently rendered image to Wall image, waits
402  * until it is displayed and switch the two rendering buffers, preparing next
403  * frame.
404  *****************************************************************************/
405 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
406 {
407     picture_t *p_outpic = NULL;
408     int i_col, i_row, i_vout, i_plane;
409     int pi_left_skip[VOUT_MAX_PLANES], pi_top_skip[VOUT_MAX_PLANES];
410
411     i_vout = 0;
412
413     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
414     {
415         pi_top_skip[i_plane] = 0;
416     }
417
418     for( i_row = 0; i_row < p_vout->p_sys->i_row; i_row++ )
419     {
420         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
421         {
422             pi_left_skip[i_plane] = 0;
423         }
424
425         for( i_col = 0; i_col < p_vout->p_sys->i_col; i_col++ )
426         {
427             if( !p_vout->p_sys->pp_vout[ i_vout ].b_active )
428             {
429                 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
430                 {
431                     pi_left_skip[i_plane] +=
432                         p_vout->p_sys->pp_vout[ i_vout ].i_width
433                          * p_pic->p[i_plane].i_pitch / p_vout->output.i_width;
434                 }
435                 i_vout++;
436                 continue;
437             }
438
439             while( ( p_outpic =
440                 vout_CreatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
441                                     0, 0, 0 )
442                    ) == NULL )
443             {
444                 if( p_vout->b_die || p_vout->b_error )
445                 {
446                     vout_DestroyPicture(
447                         p_vout->p_sys->pp_vout[ i_vout ].p_vout, p_outpic );
448                     return;
449                 }
450
451                 msleep( VOUT_OUTMEM_SLEEP );
452             }
453
454             vout_DatePicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
455                               p_outpic, p_pic->date );
456             vout_LinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
457                               p_outpic );
458
459             for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
460             {
461                 u8 *p_in, *p_in_end, *p_out;
462                 int i_in_pitch = p_pic->p[i_plane].i_pitch;
463                 int i_out_pitch = p_outpic->p[i_plane].i_pitch;
464
465                 p_in = p_pic->p[i_plane].p_pixels
466                         + pi_top_skip[i_plane] + pi_left_skip[i_plane];
467
468                 p_in_end = p_in + p_outpic->p[i_plane].i_lines
469                                    * p_pic->p[i_plane].i_pitch;
470
471                 p_out = p_outpic->p[i_plane].p_pixels;
472
473                 while( p_in < p_in_end )
474                 {
475                     FAST_MEMCPY( p_out, p_in, i_out_pitch );
476                     p_in += i_in_pitch;
477                     p_out += i_out_pitch;
478                 }
479
480                 pi_left_skip[i_plane] += i_out_pitch;
481             }
482
483             vout_UnlinkPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
484                                 p_outpic );
485             vout_DisplayPicture( p_vout->p_sys->pp_vout[ i_vout ].p_vout,
486                                  p_outpic );
487
488             i_vout++;
489         }
490
491         for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
492         {
493             pi_top_skip[i_plane] += p_vout->p_sys->pp_vout[ i_vout ].i_height
494                                      * p_pic->p[i_plane].i_lines
495                                      / p_vout->output.i_height
496                                      * p_pic->p[i_plane].i_pitch;
497         }
498     }
499 }
500
501 /*****************************************************************************
502  * vout_Display: displays previously rendered output
503  *****************************************************************************
504  * This function send the currently rendered image to Invert image, waits
505  * until it is displayed and switch the two rendering buffers, preparing next
506  * frame.
507  *****************************************************************************/
508 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
509 {
510     ;
511 }
512
513 /*****************************************************************************
514  * RemoveAllVout: destroy all the child video output threads
515  *****************************************************************************/
516 static void RemoveAllVout( vout_thread_t *p_vout )
517 {
518     while( p_vout->p_sys->i_vout )
519     {
520          --p_vout->p_sys->i_vout;
521          if( p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].b_active )
522          {
523              vout_DestroyThread(
524                p_vout->p_sys->pp_vout[ p_vout->p_sys->i_vout ].p_vout, NULL );
525          }
526     }
527 }
528