]> git.sesse.net Git - vlc/blob - modules/video_filter/mosaic.c
* add svn Id support and some copyright info in picture.h
[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 static int MosaicCallback( vlc_object_t *, char const *, vlc_value_t,
49                             vlc_value_t, void * );
50
51 /*****************************************************************************
52 * filter_sys_t : filter desriptor
53 *****************************************************************************/
54
55 #include "../video_output/picture.h"
56
57 struct filter_sys_t
58 {
59
60     image_handler_t *p_image;
61     image_handler_t *p_image2;
62     picture_t *p_pic;
63
64     int i_pos; /* mosaic positioning method */
65     int i_ar; /* do we keep aspect ratio ? */
66     int i_width, i_height; /* mosaic height and width */
67     int i_cols, i_rows; /* mosaic rows and cols */
68     int i_xoffset, i_yoffset; /* top left corner offset */
69     int i_vborder, i_hborder; /* border width/height between miniatures */
70     int i_alpha; /* subfilter alpha blending */
71
72 };
73
74 /*****************************************************************************
75 * Module descriptor
76 *****************************************************************************/
77
78 #define ALPHA_TEXT N_("Mosaic alpha blending (0 -> 255)")
79 #define ALPHA_LONGTEXT N_("Mosaic alpha blending (0 -> 255). default is 255")
80
81 #define HEIGHT_TEXT N_("Mosaic height in pixels")
82 #define WIDTH_TEXT N_("Mosaic width in pixels")
83 #define XOFFSET_TEXT N_("Mosaic top left corner x coordinate")
84 #define YOFFSET_TEXT N_("Mosaic top left corner y coordinate")
85 #define VBORDER_TEXT N_("Mosaic vertical border width in pixels")
86 #define HBORDER_TEXT N_("Mosaic horizontal border width in pixels")
87
88 #define POS_TEXT N_("Mosaic positioning method")
89 #define POS_LONGTEXT N_("Mosaic positioning method. auto : automatically chose the best number of rows and columns. fixed : used the user defined number of rows and columns.")
90 #define ROWS_TEXT N_("Mosaic number of rows")
91 #define COLS_TEXT N_("Mosaic number of columns")
92 #define AR_TEXT N_("Keep aspect ratio when resizing")
93
94 static int pi_pos_values[] = { 0, 1 };
95 static char * ppsz_pos_descriptions[] =
96 { N_("auto"), N_("fixed") };
97
98
99 vlc_module_begin();
100     set_description( _("Mosaic video sub filter") );
101     set_shortname( N_("Mosaic") );
102     set_capability( "sub filter", 0 );
103     set_category( CAT_VIDEO );
104     set_subcategory( SUBCAT_VIDEO_SUBPIC );
105     set_callbacks( CreateFilter, DestroyFilter );
106
107     add_integer( "mosaic-alpha", 255, NULL, ALPHA_TEXT, ALPHA_LONGTEXT, VLC_FALSE );
108     add_integer( "mosaic-height", 100, NULL, HEIGHT_TEXT, HEIGHT_TEXT, VLC_FALSE );
109     add_integer( "mosaic-width", 100, NULL, WIDTH_TEXT, WIDTH_TEXT, VLC_FALSE );
110     add_integer( "mosaic-xoffset", 0, NULL, XOFFSET_TEXT, XOFFSET_TEXT, VLC_TRUE );
111     add_integer( "mosaic-yoffset", 0, NULL, YOFFSET_TEXT, YOFFSET_TEXT, VLC_TRUE );
112     add_integer( "mosaic-vborder", 0, NULL, VBORDER_TEXT, VBORDER_TEXT, VLC_TRUE );
113     add_integer( "mosaic-hborder", 0, NULL, HBORDER_TEXT, HBORDER_TEXT, VLC_TRUE );
114
115     add_integer( "mosaic-position", 0, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
116         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
117     add_integer( "mosaic-rows", 2, NULL, ROWS_TEXT, ROWS_TEXT, VLC_FALSE );
118     add_integer( "mosaic-cols", 2, NULL, COLS_TEXT, COLS_TEXT, VLC_FALSE );
119     add_bool( "mosaic-keep-aspect-ratio", 0, NULL, AR_TEXT, AR_TEXT, VLC_FALSE );
120 vlc_module_end();
121
122
123 /*****************************************************************************
124 * CreateFiler: allocates mosaic video filter
125 *****************************************************************************/
126
127 static int CreateFilter( vlc_object_t *p_this )
128 {
129     filter_t *p_filter = (filter_t *)p_this;
130     filter_sys_t *p_sys;
131
132     /* Allocate structure */
133     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
134     if( p_sys == NULL )
135     {
136         msg_Err( p_filter, "out of memory" );
137         return VLC_ENOMEM;
138     }
139     p_sys->p_image = image_HandlerCreate( p_filter );
140     p_sys->p_image2 = image_HandlerCreate( p_filter );
141
142     p_filter->pf_sub_filter = Filter;
143     p_sys->p_pic = NULL;
144
145     p_sys->i_width = __MAX( 0, var_CreateGetInteger( p_filter->p_libvlc, "mosaic-width" ) );
146     p_sys->i_height = __MAX( 0, var_CreateGetInteger( p_filter->p_libvlc, "mosaic-height" ) );
147
148     p_sys->i_xoffset = __MAX( 0, var_CreateGetInteger( p_filter->p_libvlc, "mosaic-xoffset" ) );
149     p_sys->i_yoffset = __MAX( 0, var_CreateGetInteger( p_filter->p_libvlc, "mosaic-yoffset" ) );
150
151     p_sys->i_vborder = __MAX( 0, var_CreateGetInteger( p_filter->p_libvlc, "mosaic-vborder" ) );
152     p_sys->i_hborder = __MAX( 0, var_CreateGetInteger( p_filter->p_libvlc, "mosaic-hborder" ) );
153
154     p_sys->i_rows = __MAX( 1, var_CreateGetInteger( p_filter->p_libvlc, "mosaic-rows") );
155     p_sys->i_cols = __MAX( 1, var_CreateGetInteger( p_filter->p_libvlc, "mosaic-cols") );
156
157     p_sys->i_alpha = var_CreateGetInteger( p_filter->p_libvlc, "mosaic-alpha" );
158     p_sys->i_alpha = __MIN( 255, __MAX( 0, p_sys->i_alpha ) );
159
160     p_sys->i_pos = var_CreateGetInteger( p_filter->p_libvlc, "mosaic-position" );
161     if( p_sys->i_pos > 1 || p_sys->i_pos < 0 ) p_sys->i_pos = 0;
162
163     p_sys->i_ar = var_CreateGetInteger( p_filter->p_libvlc, "mosaic-keep-aspect-ratio" );
164
165     var_AddCallback( p_filter->p_libvlc, "mosaic-alpha",
166                      MosaicCallback, p_sys );
167     var_AddCallback( p_filter->p_libvlc, "mosaic-height",
168                      MosaicCallback, p_sys );
169     var_AddCallback( p_filter->p_libvlc, "mosaic-width",
170                      MosaicCallback, p_sys );
171     var_AddCallback( p_filter->p_libvlc, "mosaic-xoffset",
172                      MosaicCallback, p_sys );
173     var_AddCallback( p_filter->p_libvlc, "mosaic-yoffset",
174                      MosaicCallback, p_sys );
175     var_AddCallback( p_filter->p_libvlc, "mosaic-vborder",
176                      MosaicCallback, p_sys );
177     var_AddCallback( p_filter->p_libvlc, "mosaic-hborder",
178                      MosaicCallback, p_sys );
179     var_AddCallback( p_filter->p_libvlc, "mosaic-position",
180                      MosaicCallback, p_sys );
181     var_AddCallback( p_filter->p_libvlc, "mosaic-rows",
182                      MosaicCallback, p_sys );
183     var_AddCallback( p_filter->p_libvlc, "mosaic-cols",
184                      MosaicCallback, p_sys );
185     var_AddCallback( p_filter->p_libvlc, "mosaic-keep-aspect-ratio",
186                      MosaicCallback, p_sys );
187
188     return VLC_SUCCESS;
189 }
190
191 /*****************************************************************************
192 * DestroyFilter: destroy mosaic video filter
193 *****************************************************************************/
194
195 static void DestroyFilter( vlc_object_t *p_this )
196 {
197     filter_t *p_filter = (filter_t*)p_this;
198     filter_sys_t *p_sys = p_filter->p_sys;
199
200     image_HandlerDelete( p_sys->p_image );
201     image_HandlerDelete( p_sys->p_image2 );
202
203     var_Destroy( p_filter->p_libvlc, "mosaic-alpha" );
204     var_Destroy( p_filter->p_libvlc, "mosaic-height" );
205     var_Destroy( p_filter->p_libvlc, "mosaic-width" );
206     var_Destroy( p_filter->p_libvlc, "mosaic-xoffset" );
207     var_Destroy( p_filter->p_libvlc, "mosaic-yoffset" );
208     var_Destroy( p_filter->p_libvlc, "mosaic-vborder" );
209     var_Destroy( p_filter->p_libvlc, "mosaic-hborder" );
210     var_Destroy( p_filter->p_libvlc, "mosaic-position" );
211     var_Destroy( p_filter->p_libvlc, "mosaic-rows" );
212     var_Destroy( p_filter->p_libvlc, "mosaic-cols" );
213     var_Destroy( p_filter->p_libvlc, "mosaic-keep-aspect-ratio" );
214     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
215     free( p_sys );
216 }
217
218 /*****************************************************************************
219 * Filter
220 *****************************************************************************/
221
222 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
223 {
224
225     filter_sys_t *p_sys = p_filter->p_sys;
226     subpicture_t *p_spu;
227
228     libvlc_t *p_libvlc = p_filter->p_libvlc;
229     vlc_value_t val;
230     int i_index;
231
232     subpicture_region_t *p_region;
233     subpicture_region_t *p_region_prev = NULL;
234
235     struct picture_vout_t *p_picture_vout;
236
237     if( var_Get( p_libvlc, "p_picture_vout", &val ) )
238     {
239         return NULL;
240     }
241
242     p_picture_vout = val.p_address;
243
244     /* Allocate the subpicture internal data. */
245     p_spu = p_filter->pf_sub_buffer_new( p_filter );
246     if( !p_spu )
247     {
248         return NULL;
249     }
250
251     /* Initialize subpicture */
252     p_spu->i_channel = 0;
253     p_spu->i_start  = date;
254     p_spu->i_stop = 0;
255     p_spu->b_ephemer = VLC_TRUE;
256     p_spu->i_alpha = p_sys->i_alpha;
257
258     vlc_mutex_lock( &p_picture_vout->lock );
259
260     if( p_sys->i_pos == 0 ) /* use automatic positioning */
261     {
262         int i_numpics = 0;
263         for( i_index = 0 ;
264              i_index < p_picture_vout->i_picture_num ;
265              i_index ++ )
266         {
267             if( p_picture_vout->p_pic[i_index].i_status
268                            == PICTURE_VOUT_E_OCCUPIED ) {
269                 i_numpics ++;
270             }
271         }
272         p_sys->i_rows = ((int)ceil(sqrt( (float)i_numpics )));
273         p_sys->i_cols = ( i_numpics%p_sys->i_rows == 0 ?
274                             i_numpics/p_sys->i_rows :
275                             i_numpics/p_sys->i_rows + 1 );
276     }
277
278     for( i_index = 0 ; i_index < p_picture_vout->i_picture_num ; i_index ++ )
279     {
280
281         video_format_t fmt_in = {0}, fmt_middle = {0}, fmt_out = {0};
282
283         picture_t *p_converted, *p_middle;
284
285         if(  p_picture_vout->p_pic[i_index].p_picture == NULL )
286         {
287             break;
288         }
289
290         if(  p_picture_vout->p_pic[i_index].i_status
291                == PICTURE_VOUT_E_AVAILABLE )
292         {
293             msg_Dbg( p_filter, "Picture Vout Element is empty");
294             break;
295         }
296
297
298         /* Convert the images */
299 /*        fprintf (stderr, "Input image %ix%i %4.4s\n",
300                   p_picture_vout->p_pic[i_index].p_picture->format.i_width,
301                   p_picture_vout->p_pic[i_index].p_picture->format.i_height,
302                   (char *)&p_picture_vout->p_pic[i_index].p_picture->format.i_chroma );*/
303
304         fmt_in.i_chroma = p_picture_vout->p_pic[i_index].
305                                                 p_picture->format.i_chroma;
306         fmt_in.i_height = p_picture_vout->p_pic[i_index].
307                                                 p_picture->format.i_height;
308         fmt_in.i_width = p_picture_vout->p_pic[i_index].
309                                                 p_picture->format.i_width;
310
311
312         fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
313         fmt_out.i_width = fmt_in.i_width *( p_sys->i_width / p_sys->i_cols ) / fmt_in.i_width;
314         fmt_out.i_height = fmt_in.i_height*( p_sys->i_height / p_sys->i_rows ) / fmt_in.i_height;
315         if( p_sys->i_ar ) /* keep aspect ratio */
316         {
317             if( (float)fmt_out.i_width/(float)fmt_out.i_height
318                 > (float)fmt_in.i_width/(float)fmt_in.i_height )
319             {
320                 fmt_out.i_width = ( fmt_out.i_height * fmt_in.i_width ) / fmt_in.i_height ;
321             } else {
322                 fmt_out.i_height = ( fmt_out.i_width * fmt_in.i_height ) / fmt_in.i_width ;
323             }
324          }
325
326         fmt_out.i_visible_width = fmt_out.i_width;
327         fmt_out.i_visible_height = fmt_out.i_height;
328
329         fmt_middle.i_chroma = fmt_in.i_chroma;
330         fmt_middle.i_visible_width = fmt_middle.i_width = fmt_out.i_width;
331         fmt_middle.i_visible_height = fmt_middle.i_height = fmt_out.i_height;
332
333         p_middle = image_Convert( p_sys->p_image,
334             p_picture_vout->p_pic[i_index].p_picture, &fmt_in, &fmt_middle );
335         if( !p_middle )
336         {
337             msg_Err( p_filter, "image resizing failed" );
338             p_filter->pf_sub_buffer_del( p_filter, p_spu );
339             vlc_mutex_unlock( &p_picture_vout->lock );
340             return NULL;
341         }
342
343         p_converted = image_Convert( p_sys->p_image2,
344                  p_middle, &fmt_middle, &fmt_out );
345         if( !p_converted )
346         {
347             msg_Err( p_filter, "image chroma convertion failed" );
348             p_filter->pf_sub_buffer_del( p_filter, p_spu );
349             vlc_mutex_unlock( &p_picture_vout->lock );
350             return NULL;
351         }
352
353 /*        fprintf( stderr, "Converted %ix%i %4.4s\n", p_converted->format.i_width, p_converted->format.i_height, (char *)&p_converted->format.i_chroma);*/
354
355
356         p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt_out);
357         if( !p_region )
358         {
359             msg_Err( p_filter, "cannot allocate SPU region" );
360             p_filter->pf_sub_buffer_del( p_filter, p_spu );
361             vlc_mutex_unlock( &p_picture_vout->lock );
362             return NULL;
363         }
364
365         if( p_sys->i_ar ) /* keep aspect ratio */
366         {
367             /* center the video in the dedicated rectangle */
368             p_region->i_x = p_sys->i_xoffset + ( i_index % p_sys->i_cols )
369                             * ( p_sys->i_width / p_sys->i_cols
370                                 + p_sys->i_vborder )
371                         + ( fmt_in.i_width *( p_sys->i_width / p_sys->i_cols )
372                             / fmt_in.i_width - fmt_out.i_width ) / 2;
373             p_region->i_y = p_sys->i_yoffset
374                         + ( ( i_index / p_sys->i_cols ) % p_sys->i_rows )
375                             * ( p_sys->i_height / p_sys->i_rows
376                                 + p_sys->i_hborder )
377                         + ( fmt_in.i_height*( p_sys->i_height / p_sys->i_rows ) 
378                         / fmt_in.i_height - fmt_out.i_height ) / 2;
379         } else {
380             /* we don't have to center the video since it takes the
381             whole rectangle area */
382             p_region->i_x = p_sys->i_xoffset + ( i_index % p_sys->i_cols )
383                             * ( p_sys->i_width / p_sys->i_cols
384                                 + p_sys->i_vborder );
385             p_region->i_y = p_sys->i_yoffset
386                         + ( ( i_index / p_sys->i_cols ) % p_sys->i_rows )
387                             * ( p_sys->i_height / p_sys->i_rows
388                                 + p_sys->i_hborder );
389         }
390
391         if( 1 )
392         {
393             uint8_t *p_a = p_region->picture.A_PIXELS;
394             int i_pitch = p_region->picture.Y_PITCH;
395             int x,y;
396
397             for( x = 0, y=0; x+y < 20; x++, y++){
398                 p_a[ x + i_pitch * y ] = 0xff;
399             }
400
401         }
402
403         if( p_region_prev == NULL ){
404             p_spu->p_region = p_region;
405         } else {
406             p_region_prev->p_next = p_region;
407         }
408
409         p_region_prev = p_region;
410
411         vout_CopyPicture( p_filter, &p_region->picture, p_converted );
412
413         p_middle->pf_release( p_middle );
414         p_converted->pf_release( p_converted );
415     }
416
417     vlc_mutex_unlock( &p_picture_vout->lock );
418
419     return p_spu;
420 }
421
422 /*****************************************************************************
423 * Callback to update params on the fly
424 *****************************************************************************/
425
426 static int MosaicCallback( vlc_object_t *p_this, char const *psz_var,
427                             vlc_value_t oldval, vlc_value_t newval,
428                             void *p_data )
429 {
430     filter_sys_t *p_sys = (filter_sys_t *) p_data;
431     fprintf( stderr, "Callback" );
432     if( !strcmp( psz_var, "mosaic-alpha" ) )
433     {
434         msg_Dbg( p_this, "Changing alpha from %d/255 to %d/255",
435                          p_sys->i_alpha, newval.i_int);
436         p_sys->i_alpha = __MIN( __MAX( newval.i_int, 0 ), 255 );
437     }
438     else if( !strcmp( psz_var, "mosaic-height" ) )
439     {
440         msg_Dbg( p_this, "Changing height from %dpx to %dpx",
441                           p_sys->i_height, newval.i_int );
442         p_sys->i_height = __MAX( newval.i_int, 0 );
443     }
444     else if( !strcmp( psz_var, "mosaic-width" ) )
445     {
446         msg_Dbg( p_this, "Changing width from %dpx to %dpx",
447                          p_sys->i_width, newval.i_int );
448         p_sys->i_width = __MAX( newval.i_int, 0 );
449     }
450     else if( !strcmp( psz_var, "mosaic-xoffset" ) )
451     {
452         msg_Dbg( p_this, "Changing x offset from %dpx to %dpx",
453                          p_sys->i_xoffset, newval.i_int );
454         p_sys->i_xoffset = __MAX( newval.i_int, 0 );
455     }
456     else if( !strcmp( psz_var, "mosaic-yoffset" ) )
457     {
458         msg_Dbg( p_this, "Changing y offset from %dpx to %dpx",
459                          p_sys->i_yoffset, newval.i_int );
460         p_sys->i_yoffset = __MAX( newval.i_int, 0 );
461     }
462     else if( !strcmp( psz_var, "mosaic-vborder" ) )
463     {
464         msg_Dbg( p_this, "Changing vertical border from %dpx to %dpx",
465                          p_sys->i_vborder, newval.i_int );
466         p_sys->i_vborder = __MAX( newval.i_int, 0 );
467     }
468     else if( !strcmp( psz_var, "mosaic-hborder" ) )
469     {
470         msg_Dbg( p_this, "Changing horizontal border from %dpx to %dpx",
471                          p_sys->i_vborder, newval.i_int );
472         p_sys->i_hborder = __MAX( newval.i_int, 0 );
473     }
474     else if( !strcmp( psz_var, "mosaic-position" ) )
475     {
476         if( newval.i_int > 1 || newval.i_int < 0 )
477         {
478             msg_Err( p_this, "Position is either 0 (auto) or 1 (fixed)" );
479         }
480         else
481         {
482             msg_Dbg( p_this, "Changing position method from %d (%s) to %d (%s)",
483                              p_sys->i_pos, ppsz_pos_descriptions[p_sys->i_pos],
484                              newval.i_int, ppsz_pos_descriptions[newval.i_int]);
485             p_sys->i_pos = newval.i_int;
486         }
487     }
488     else if( !strcmp( psz_var, "mosaic-rows" ) )
489     {
490         msg_Dbg( p_this, "Changing number of rows from %d to %d",
491                          p_sys->i_rows, newval.i_int );
492         p_sys->i_rows = __MAX( newval.i_int, 1 );
493     }
494     else if( !strcmp( psz_var, "mosaic-cols" ) )
495     {
496         msg_Dbg( p_this, "Changing number of columns from %d to %d",
497                          p_sys->i_cols, newval.i_int );
498         p_sys->i_cols = __MAX( newval.i_int, 1 );
499     }
500     else if( !strcmp( psz_var, "mosaic-keep-aspect-ratio" ) )
501     {
502         if( newval.i_int )
503         {
504             msg_Dbg( p_this, "Keep aspect ratio" );
505             p_sys->i_ar = 1;
506         }
507         else
508         {
509             msg_Dbg( p_this, "Don't keep aspect ratio" );
510             p_sys->i_ar = 0;
511         }
512     }
513     return VLC_SUCCESS;
514 }