* update the copyright dates
[vlc.git] / modules / video_filter / mosaic.c
1 /*****************************************************************************
2 * mosaic.c : Mosaic video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VideoLAN
5 * $Id: $
6 *
7 * Authors: Antoine Cellerier <dionoea@via.ecp.fr>
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 #include <math.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33
34 #include "vlc_filter.h"
35 #include "vlc_input.h"
36
37 #include "vlc_image.h"
38
39 /*****************************************************************************
40 * Local prototypes
41 *****************************************************************************/
42
43 static int  CreateFilter    ( vlc_object_t * );
44 static void DestroyFilter   ( vlc_object_t * );
45
46 static subpicture_t *Filter( filter_t *, mtime_t );
47
48 /*****************************************************************************
49 * filter_sys_t : filter desriptor
50 *****************************************************************************/
51
52 #include "../video_output/picture.h"
53
54 struct filter_sys_t
55 {
56
57     image_handler_t *p_image;
58     image_handler_t *p_image2;
59     picture_t *p_pic;
60
61     int i_pos; /* mosaic positioning method */
62     int i_width, i_height; /* mosaic height and width */
63     int i_cols, i_rows; /* mosaic rows and cols */
64     int i_xoffset, i_yoffset; /* top left corner offset */
65     int i_vborder, i_hborder; /* border width/height between miniatures */
66     int i_alpha; /* subfilter alpha blending */
67
68 };
69
70 /*****************************************************************************
71 * Module descriptor
72 *****************************************************************************/
73
74 #define ALPHA_TEXT N_("Mosaic alpha blending (0 -> 255)")
75 #define ALPHA_LONGTEXT N_("Mosaic alpha blending (0 -> 255). default is 255")
76
77 #define HEIGHT_TEXT N_("Mosaic height in pixels")
78 #define WIDTH_TEXT N_("Mosaic width in pixels")
79 #define XOFFSET_TEXT N_("Mosaic top left corner x coordinate")
80 #define YOFFSET_TEXT N_("Mosaic top left corner y coordinate")
81 #define VBORDER_TEXT N_("Mosaic vertical border width in pixels")
82 #define HBORDER_TEXT N_("Mosaic horizontal border width in pixels")
83
84 #define POS_TEXT N_("Mosaic positioning method")
85 #define ROWS_TEXT N_("Mosaic number of rows")
86 #define COLS_TEXT N_("Mosaic number of columns")
87
88 static int pi_pos_values[] = { 0, 1 };
89 static char * ppsz_pos_descriptions[] =
90 { N_("auto"), N_("fixed") };
91
92
93 vlc_module_begin();
94     set_description( _("Mosaic video sub filter") );
95     set_shortname( N_("Mosaic") );
96     set_capability( "sub filter", 0 );
97     set_category( CAT_VIDEO );
98     set_subcategory( SUBCAT_VIDEO_SUBPIC );
99     set_callbacks( CreateFilter, DestroyFilter );
100
101     add_integer( "mosaic-alpha", 255, NULL, ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
102     add_integer( "mosaic-height", 100, NULL, HEIGHT_TEXT, HEIGHT_TEXT, VLC_FALSE );
103     add_integer( "mosaic-width", 100, NULL, WIDTH_TEXT, WIDTH_TEXT, VLC_FALSE );
104     add_integer( "mosaic-xoffset", 0, NULL, XOFFSET_TEXT, XOFFSET_TEXT, VLC_FALSE );
105     add_integer( "mosaic-yoffset", 0, NULL, YOFFSET_TEXT, YOFFSET_TEXT, VLC_FALSE );
106     add_integer( "mosaic-vborder", 0, NULL, VBORDER_TEXT, VBORDER_TEXT, VLC_FALSE );
107     add_integer( "mosaic-hborder", 0, NULL, HBORDER_TEXT, HBORDER_TEXT, VLC_FALSE );
108
109     add_integer( "mosaic-position", 0, NULL, POS_TEXT, POS_TEXT, VLC_TRUE );
110         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
111     add_integer( "mosaic-rows", 2, NULL, ROWS_TEXT, ROWS_TEXT, VLC_FALSE );
112     add_integer( "mosaic-cols", 2, NULL, COLS_TEXT, COLS_TEXT, VLC_FALSE );
113 vlc_module_end();
114
115
116 /*****************************************************************************
117 * CreateFiler: allocates mosaic video filter
118 *****************************************************************************/
119
120 static int CreateFilter( vlc_object_t *p_this )
121 {
122     filter_t *p_filter = (filter_t *)p_this;
123     filter_sys_t *p_sys;
124
125     /* Allocate structure */
126     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
127     if( p_sys == NULL )
128     {
129         msg_Err( p_filter, "out of memory" );
130         return VLC_ENOMEM;
131     }
132     p_sys->p_image = image_HandlerCreate( p_filter );
133     p_sys->p_image2 = image_HandlerCreate( p_filter );
134
135     p_filter->pf_sub_filter = Filter;
136     p_sys->p_pic = NULL;
137
138     p_sys->i_width = __MAX( 0, config_GetInt( p_filter, "mosaic-width" ) );
139     p_sys->i_height = __MAX( 0, config_GetInt( p_filter, "mosaic-height" ) );
140
141     p_sys->i_xoffset = __MAX( 0, config_GetInt( p_filter, "mosaic-xoffset" ) );
142     p_sys->i_yoffset = __MAX( 0, config_GetInt( p_filter, "mosaic-yoffset" ) );
143
144     p_sys->i_vborder = __MAX( 0, config_GetInt( p_filter, "mosaic-vborder" ) );
145     p_sys->i_hborder = __MAX( 0, config_GetInt( p_filter, "mosaic-hborder" ) );
146
147     p_sys->i_rows = __MAX( 1, config_GetInt( p_filter, "mosaic-rows") );
148     p_sys->i_cols = __MAX( 1, config_GetInt( p_filter, "mosaic-cols") );
149
150     p_sys->i_alpha = config_GetInt( p_filter, "mosaic-alpha" );
151     p_sys->i_alpha = __MIN( 255, __MAX( 0, p_sys->i_alpha ) );
152
153     p_sys->i_pos = config_GetInt( p_filter, "mosaic-position" );
154     if( p_sys->i_pos > 1 || p_sys->i_pos < 0 ) p_sys->i_pos = 0;
155
156     return VLC_SUCCESS;
157 }
158
159 /*****************************************************************************
160 * DestroyFilter: destroy mosaic video filter
161 *****************************************************************************/
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
168     image_HandlerDelete( p_sys->p_image );
169     image_HandlerDelete( p_sys->p_image2 );
170
171     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
172     free( p_sys );
173 }
174
175 /*****************************************************************************
176 * Filter
177 *****************************************************************************/
178
179 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
180 {
181
182     filter_sys_t *p_sys = p_filter->p_sys;
183     subpicture_t *p_spu;
184
185     libvlc_t *p_libvlc = p_filter->p_libvlc;
186     vlc_value_t val;
187     int i_index;
188
189     subpicture_region_t *p_region;
190     subpicture_region_t *p_region_prev = NULL;
191
192     struct picture_vout_t *p_picture_vout;
193
194     if( var_Get( p_libvlc, "p_picture_vout", &val ) )
195     {
196         return NULL;
197     }
198
199     /* Allocate the subpicture internal data. */
200     p_spu = p_filter->pf_sub_buffer_new( p_filter );
201     if( !p_spu )
202     {
203         return NULL;
204     }
205
206     p_picture_vout = val.p_address;
207
208     vlc_mutex_lock( &p_picture_vout->lock );
209
210     if( p_sys->i_pos == 0 ) /* use automatic positioning */
211     {
212         int i_numpics = 0;
213         for( i_index = 0 ;
214              i_index < p_picture_vout->i_picture_num ;
215              i_index ++ )
216         {
217             if( p_picture_vout->p_pic[i_index].i_status
218                            == PICTURE_VOUT_E_OCCUPIED ) {
219                 i_numpics ++;
220             }
221         }
222         p_sys->i_rows = ((int)ceil(sqrt( (float)i_numpics )));
223         p_sys->i_cols = ( i_numpics%p_sys->i_rows == 0 ?
224                             i_numpics/p_sys->i_rows :
225                             i_numpics/p_sys->i_rows + 1 );
226     }
227
228     for( i_index = 0 ; i_index < p_picture_vout->i_picture_num ; i_index ++ )
229     {
230
231         video_format_t fmt_in = {0}, fmt_middle = {0}, fmt_out = {0};
232
233         picture_t *p_converted, *p_middle;
234
235         if(  p_picture_vout->p_pic[i_index].p_picture == NULL )
236         {
237             break;
238         }
239
240         if(  p_picture_vout->p_pic[i_index].i_status
241                == PICTURE_VOUT_E_AVAILABLE )
242         {
243             msg_Dbg( p_filter, "Picture Vout Element is empty");
244             break;
245         }
246
247
248         /* Convert the images */
249 /*        fprintf (stderr, "Input image %ix%i %4.4s\n",
250                   p_picture_vout->p_pic[i_index].p_picture->format.i_width,
251                   p_picture_vout->p_pic[i_index].p_picture->format.i_height,
252                   (char *)&p_picture_vout->p_pic[i_index].p_picture->format.i_chroma );*/
253
254         fmt_in.i_chroma = p_picture_vout->p_pic[i_index].
255                                                 p_picture->format.i_chroma;
256         fmt_in.i_height = p_picture_vout->p_pic[i_index].
257                                                 p_picture->format.i_height;
258         fmt_in.i_width = p_picture_vout->p_pic[i_index].
259                                                 p_picture->format.i_width;
260
261
262         fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
263         fmt_out.i_width = fmt_in.i_width *( p_sys->i_width / p_sys->i_cols ) / fmt_in.i_width;
264         fmt_out.i_height = fmt_in.i_height*( p_sys->i_height / p_sys->i_rows ) / fmt_in.i_height;
265         fmt_out.i_visible_width = fmt_out.i_width;
266         fmt_out.i_visible_height = fmt_out.i_height;
267
268         fmt_middle.i_chroma = fmt_in.i_chroma;
269         fmt_middle.i_visible_width = fmt_middle.i_width = fmt_out.i_width;
270         fmt_middle.i_visible_height = fmt_middle.i_height = fmt_out.i_height;
271
272         p_middle = image_Convert( p_sys->p_image,
273             p_picture_vout->p_pic[i_index].p_picture, &fmt_in, &fmt_middle );
274         if( !p_middle )
275         {
276             vlc_mutex_unlock( &p_picture_vout->lock );
277             return NULL;
278         }
279
280         p_converted = image_Convert( p_sys->p_image2,
281                  p_middle, &fmt_middle, &fmt_out );
282         if( !p_converted )
283         {
284             vlc_mutex_unlock( &p_picture_vout->lock );
285             return NULL;
286         }
287
288 /*        fprintf( stderr, "Converted %ix%i %4.4s\n", p_converted->format.i_width, p_converted->format.i_height, (char *)&p_converted->format.i_chroma);*/
289
290
291         p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt_out);
292         if( !p_region )
293         {
294             msg_Err( p_filter, "cannot allocate SPU region" );
295             p_filter->pf_sub_buffer_del( p_filter, p_spu );
296             vlc_mutex_unlock( &p_picture_vout->lock );
297             return NULL;
298         }
299
300         p_region->i_x = p_sys->i_xoffset + ( i_index % p_sys->i_cols )
301                             * ( p_sys->i_width / p_sys->i_cols + p_sys->i_vborder );
302         p_region->i_y = p_sys->i_yoffset
303                         + ( ( i_index / p_sys->i_cols ) % p_sys->i_rows )
304                             * ( p_sys->i_height / p_sys->i_rows + p_sys->i_hborder );
305
306
307         if( p_region_prev == NULL ){
308             p_spu->p_region = p_region;
309         } else {
310             p_region_prev->p_next = p_region;
311         }
312
313         p_region_prev = p_region;
314
315         vout_CopyPicture( p_filter, &p_region->picture, p_converted );
316
317         p_middle->pf_release( p_middle );
318         p_converted->pf_release( p_converted );
319     }
320
321     vlc_mutex_unlock( &p_picture_vout->lock );
322
323     /* Initialize subpicture */
324     p_spu->i_channel = 0;
325     p_spu->i_start  = date;
326     p_spu->i_stop = 0;
327     p_spu->b_ephemer = VLC_TRUE;
328     p_spu->i_alpha = p_sys->i_alpha;
329
330     return p_spu;
331 }