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