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