]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
Compiler warnings rampage
[vlc] / src / video_output / vout_subpictures.c
1 /*****************************************************************************
2  * vout_subpictures.c : subpicture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                                /* free() */
30 #include <stdio.h>                                              /* sprintf() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <vlc/vlc.h>
34 #include <vlc_vout.h>
35 #include <vlc_block.h>
36 #include <vlc_filter.h>
37 #include <vlc_osd.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static void UpdateSPU   ( spu_t *, vlc_object_t * );
43 static int  CropCallback( vlc_object_t *, char const *,
44                           vlc_value_t, vlc_value_t, void * );
45
46 static int spu_vaControlDefault( spu_t *, int, va_list );
47
48 static subpicture_t *sub_new_buffer( filter_t * );
49 static void sub_del_buffer( filter_t *, subpicture_t * );
50 static subpicture_t *spu_new_buffer( filter_t * );
51 static void spu_del_buffer( filter_t *, subpicture_t * );
52 static picture_t *spu_new_video_buffer( filter_t * );
53 static void spu_del_video_buffer( filter_t *, picture_t * );
54
55 static int spu_ParseChain( spu_t * );
56 static void spu_DeleteChain( spu_t * );
57 static int SubFilterCallback( vlc_object_t *, char const *,
58                               vlc_value_t, vlc_value_t, void * );
59
60 struct filter_owner_sys_t
61 {
62     spu_t *p_spu;
63     int i_channel;
64 };
65
66 /**
67  * Creates the subpicture unit
68  *
69  * \param p_this the parent object which creates the subpicture unit
70  */
71 spu_t *__spu_Create( vlc_object_t *p_this )
72 {
73     int i_index;
74     spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
75
76     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
77     {
78         p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
79     }
80
81     p_spu->p_blend = NULL;
82     p_spu->p_text = NULL;
83     p_spu->p_scale = NULL;
84     p_spu->i_filter = 0;
85     p_spu->pf_control = spu_vaControlDefault;
86
87     /* Register the default subpicture channel */
88     p_spu->i_channel = 2;
89
90     vlc_mutex_init( p_this, &p_spu->subpicture_lock );
91
92     vlc_object_attach( p_spu, p_this );
93
94     return p_spu;
95 }
96
97 /**
98  * Initialise the subpicture unit
99  *
100  * \param p_spu the subpicture unit object
101  */
102 int spu_Init( spu_t *p_spu )
103 {
104     vlc_value_t val;
105
106     /* If the user requested a sub margin, we force the position. */
107     var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
108     var_Get( p_spu, "sub-margin", &val );
109     p_spu->i_margin = val.i_int;
110
111     var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
112     var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
113
114     spu_ParseChain( p_spu );
115
116     return VLC_EGENERIC;
117 }
118
119 int spu_ParseChain( spu_t *p_spu )
120 {
121     char *psz_parser;
122     vlc_value_t val;
123     var_Get( p_spu, "sub-filter", &val );
124     psz_parser = val.psz_string;
125
126     while( psz_parser && *psz_parser )
127     {
128         config_chain_t *p_cfg;
129         char *psz_name;
130
131         psz_parser = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
132
133         msg_Dbg( p_spu, "adding sub-filter: %s", psz_name );
134
135         p_spu->pp_filter[p_spu->i_filter] =
136             vlc_object_create( p_spu, VLC_OBJECT_FILTER );
137         vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
138         p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
139         p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
140         p_spu->pp_filter[p_spu->i_filter]->p_cfg = p_cfg;
141         p_spu->pp_filter[p_spu->i_filter]->p_module =
142             module_Need( p_spu->pp_filter[p_spu->i_filter],
143                          "sub filter", psz_name, 0 );
144         if( p_spu->pp_filter[p_spu->i_filter]->p_module )
145         {
146             filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
147             p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
148             spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
149             p_sys->p_spu = p_spu;
150             p_spu->i_filter++;
151         }
152         else
153         {
154             msg_Dbg( p_spu, "no sub filter found" );
155             config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
156             vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
157             vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
158         }
159
160         if( p_spu->i_filter >= 10 )
161         {
162             msg_Dbg( p_spu, "can't add anymore filters" );
163         }
164
165         free( psz_name );
166     }
167     if( val.psz_string ) free( val.psz_string );
168
169     return VLC_EGENERIC;
170 }
171
172 /**
173  * Destroy the subpicture unit
174  *
175  * \param p_this the parent object which destroys the subpicture unit
176  */
177 void spu_Destroy( spu_t *p_spu )
178 {
179     int i_index;
180
181     vlc_object_detach( p_spu );
182
183     /* Destroy all remaining subpictures */
184     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
185     {
186         if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
187         {
188             spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
189         }
190     }
191
192     if( p_spu->p_blend )
193     {
194         if( p_spu->p_blend->p_module )
195             module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
196
197         vlc_object_detach( p_spu->p_blend );
198         vlc_object_destroy( p_spu->p_blend );
199     }
200
201     if( p_spu->p_text )
202     {
203         if( p_spu->p_text->p_module )
204             module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
205
206         vlc_object_detach( p_spu->p_text );
207         vlc_object_destroy( p_spu->p_text );
208     }
209
210     if( p_spu->p_scale )
211     {
212         if( p_spu->p_scale->p_module )
213             module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
214
215         vlc_object_detach( p_spu->p_scale );
216         vlc_object_destroy( p_spu->p_scale );
217     }
218
219     spu_DeleteChain( p_spu );
220
221     vlc_mutex_destroy( &p_spu->subpicture_lock );
222     vlc_object_destroy( p_spu );
223 }
224
225 static void spu_DeleteChain( spu_t *p_spu )
226 {
227     if( p_spu->i_filter )
228     while( p_spu->i_filter-- )
229     {
230         module_Unneed( p_spu->pp_filter[p_spu->i_filter],
231                        p_spu->pp_filter[p_spu->i_filter]->p_module );
232         free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
233         config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
234         vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
235         vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
236     }
237 }
238
239 /**
240  * Attach/Detach the SPU from any input
241  *
242  * \param p_this the object in which to destroy the subpicture unit
243  * \param b_attach to select attach or detach
244  */
245 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
246 {
247     vlc_object_t *p_input;
248
249     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
250     if( !p_input ) return;
251
252     if( b_attach )
253     {
254         UpdateSPU( p_spu, VLC_OBJECT(p_input) );
255         var_AddCallback( p_input, "highlight", CropCallback, p_spu );
256         vlc_object_release( p_input );
257     }
258     else
259     {
260         /* Delete callback */
261         var_DelCallback( p_input, "highlight", CropCallback, p_spu );
262         vlc_object_release( p_input );
263     }
264 }
265
266 /**
267  * Create a subpicture region
268  *
269  * \param p_this vlc_object_t
270  * \param p_fmt the format that this subpicture region should have
271  */
272 static void RegionPictureRelease( picture_t *p_pic )
273 {
274     if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
275 }
276 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
277                                          video_format_t *p_fmt )
278 {
279     subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
280     memset( p_region, 0, sizeof(subpicture_region_t) );
281     p_region->p_next = 0;
282     p_region->p_cache = 0;
283     p_region->fmt = *p_fmt;
284     p_region->psz_text = 0;
285     p_region->p_style = NULL;
286
287     if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
288         p_fmt->p_palette = p_region->fmt.p_palette =
289             malloc( sizeof(video_palette_t) );
290     else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
291
292     p_region->picture.p_data_orig = NULL;
293
294     if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
295
296     vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
297                           p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
298
299     if( !p_region->picture.i_planes )
300     {
301         free( p_region );
302         free( p_fmt->p_palette );
303         return NULL;
304     }
305
306     p_region->picture.pf_release = RegionPictureRelease;
307
308     return p_region;
309 }
310
311 /**
312  * Make a subpicture region from an existing picture_t
313  *
314  * \param p_this vlc_object_t
315  * \param p_fmt the format that this subpicture region should have
316  * \param p_pic a pointer to the picture creating the region (not freed)
317  */
318 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
319                                        video_format_t *p_fmt,
320                                        picture_t *p_pic )
321 {
322     subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
323     (void)p_this;
324     memset( p_region, 0, sizeof(subpicture_region_t) );
325     p_region->p_next = 0;
326     p_region->p_cache = 0;
327     p_region->fmt = *p_fmt;
328     p_region->psz_text = 0;
329     p_region->p_style = NULL;
330
331     if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
332         p_fmt->p_palette = p_region->fmt.p_palette =
333             malloc( sizeof(video_palette_t) );
334     else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
335
336     memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
337     p_region->picture.pf_release = RegionPictureRelease;
338
339     return p_region;
340 }
341
342 /**
343  * Destroy a subpicture region
344  *
345  * \param p_this vlc_object_t
346  * \param p_region the subpicture region to destroy
347  */
348 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
349 {
350     if( !p_region ) return;
351     if( p_region->picture.pf_release )
352         p_region->picture.pf_release( &p_region->picture );
353     if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
354     if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
355
356     if( p_region->psz_text )
357         free( p_region->psz_text );
358     //free( p_region->p_style ); FIXME --fenrir plugin does not allocate the memory for it. I think it might lead to segfault, video renderer can live longer than the decoder
359     free( p_region );
360 }
361
362 /**
363  * Display a subpicture
364  *
365  * Remove the reservation flag of a subpicture, which will cause it to be
366  * ready for display.
367  * \param p_spu the subpicture unit object
368  * \param p_subpic the subpicture to display
369  */
370 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
371 {
372     /* Check if status is valid */
373     if( p_subpic->i_status != RESERVED_SUBPICTURE )
374     {
375         msg_Err( p_spu, "subpicture %p has invalid status #%d",
376                  p_subpic, p_subpic->i_status );
377     }
378
379     /* Remove reservation flag */
380     p_subpic->i_status = READY_SUBPICTURE;
381
382     if( p_subpic->i_channel == DEFAULT_CHAN )
383     {
384         p_subpic->i_channel = 0xFFFF;
385         spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
386         p_subpic->i_channel = DEFAULT_CHAN;
387     }
388 }
389
390 /**
391  * Allocate a subpicture in the spu heap.
392  *
393  * This function create a reserved subpicture in the spu heap.
394  * A null pointer is returned if the function fails. This method provides an
395  * already allocated zone of memory in the spu data fields. It needs locking
396  * since several pictures can be created by several producers threads.
397  * \param p_spu the subpicture unit in which to create the subpicture
398  * \return NULL on error, a reserved subpicture otherwise
399  */
400 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
401 {
402     int                 i_subpic;                        /* subpicture index */
403     subpicture_t *      p_subpic = NULL;            /* first free subpicture */
404
405     /* Get lock */
406     vlc_mutex_lock( &p_spu->subpicture_lock );
407
408     /*
409      * Look for an empty place
410      */
411     p_subpic = NULL;
412     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
413     {
414         if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
415         {
416             /* Subpicture is empty and ready for allocation */
417             p_subpic = &p_spu->p_subpicture[i_subpic];
418             p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
419             break;
420         }
421     }
422
423     /* If no free subpicture could be found */
424     if( p_subpic == NULL )
425     {
426         msg_Err( p_spu, "subpicture heap is full" );
427         vlc_mutex_unlock( &p_spu->subpicture_lock );
428         return NULL;
429     }
430
431     /* Copy subpicture information, set some default values */
432     memset( p_subpic, 0, sizeof(subpicture_t) );
433     p_subpic->i_status   = RESERVED_SUBPICTURE;
434     p_subpic->b_absolute = VLC_TRUE;
435     p_subpic->b_pausable = VLC_FALSE;
436     p_subpic->b_fade     = VLC_FALSE;
437     p_subpic->i_alpha    = 0xFF;
438     p_subpic->p_region   = 0;
439     p_subpic->pf_render  = 0;
440     p_subpic->pf_destroy = 0;
441     p_subpic->p_sys      = 0;
442     vlc_mutex_unlock( &p_spu->subpicture_lock );
443
444     p_subpic->pf_create_region = __spu_CreateRegion;
445     p_subpic->pf_make_region = __spu_MakeRegion;
446     p_subpic->pf_destroy_region = __spu_DestroyRegion;
447
448     return p_subpic;
449 }
450
451 /**
452  * Remove a subpicture from the heap
453  *
454  * This function frees a previously reserved subpicture.
455  * It is meant to be used when the construction of a picture aborted.
456  * This function does not need locking since reserved subpictures are ignored
457  * by the spu.
458  */
459 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
460 {
461     /* Get lock */
462     vlc_mutex_lock( &p_spu->subpicture_lock );
463
464     /* There can be race conditions so we need to check the status */
465     if( p_subpic->i_status == FREE_SUBPICTURE )
466     {
467         vlc_mutex_unlock( &p_spu->subpicture_lock );
468         return;
469     }
470
471     /* Check if status is valid */
472     if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
473            && ( p_subpic->i_status != READY_SUBPICTURE ) )
474     {
475         msg_Err( p_spu, "subpicture %p has invalid status %d",
476                          p_subpic, p_subpic->i_status );
477     }
478
479     while( p_subpic->p_region )
480     {
481         subpicture_region_t *p_region = p_subpic->p_region;
482         p_subpic->p_region = p_region->p_next;
483         spu_DestroyRegion( p_spu, p_region );
484     }
485
486     if( p_subpic->pf_destroy )
487     {
488         p_subpic->pf_destroy( p_subpic );
489     }
490
491     p_subpic->i_status = FREE_SUBPICTURE;
492
493     vlc_mutex_unlock( &p_spu->subpicture_lock );
494 }
495
496 /*****************************************************************************
497  * spu_RenderSubpictures: render a subpicture list
498  *****************************************************************************
499  * This function renders all sub picture units in the list.
500  *****************************************************************************/
501 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
502                             picture_t *p_pic_dst, picture_t *p_pic_src,
503                             subpicture_t *p_subpic,
504                             int i_scale_width_orig, int i_scale_height_orig )
505 {
506     /* Get lock */
507     vlc_mutex_lock( &p_spu->subpicture_lock );
508
509     /* Check i_status again to make sure spudec hasn't destroyed the subpic */
510     while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
511     {
512         subpicture_region_t *p_region = p_subpic->p_region;
513         int i_scale_width, i_scale_height;
514         int i_subpic_x = p_subpic->i_x;
515
516         /* Load the blending module */
517         if( !p_spu->p_blend && p_region )
518         {
519             p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
520             vlc_object_attach( p_spu->p_blend, p_spu );
521             p_spu->p_blend->fmt_out.video.i_x_offset =
522                 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
523             p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
524             p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
525             p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
526             /* XXX: We'll also be using it for YUVA and RGBA blending ... */
527
528             p_spu->p_blend->p_module =
529                 module_Need( p_spu->p_blend, "video blending", 0, 0 );
530         }
531
532         /* Load the text rendering module */
533         if( !p_spu->p_text && p_region && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
534         {
535             char *psz_modulename = NULL;
536
537             p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
538             vlc_object_attach( p_spu->p_text, p_spu );
539
540             p_spu->p_text->fmt_out.video.i_width =
541                 p_spu->p_text->fmt_out.video.i_visible_width =
542                 p_fmt->i_width;
543             p_spu->p_text->fmt_out.video.i_height =
544                 p_spu->p_text->fmt_out.video.i_visible_height =
545                 p_fmt->i_height;
546
547             p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
548             p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
549
550             psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
551             if( psz_modulename && *psz_modulename )
552             {
553                 p_spu->p_text->p_module =
554                     module_Need( p_spu->p_text, "text renderer", psz_modulename, VLC_TRUE );
555             }
556             if( !p_spu->p_text->p_module )
557             {
558                 p_spu->p_text->p_module =
559                     module_Need( p_spu->p_text, "text renderer", 0, 0 );
560             }
561             if( psz_modulename ) free( psz_modulename );
562         }
563         if( p_spu->p_text )
564         {
565             if( p_subpic->i_original_picture_height > 0 &&
566                 p_subpic->i_original_picture_width  > 0 )
567             {
568                 p_spu->p_text->fmt_out.video.i_width =
569                     p_spu->p_text->fmt_out.video.i_visible_width =
570                     p_subpic->i_original_picture_width;
571                 p_spu->p_text->fmt_out.video.i_height =
572                     p_spu->p_text->fmt_out.video.i_visible_height =
573                     p_subpic->i_original_picture_height;
574             }
575             else
576             {
577                 p_spu->p_text->fmt_out.video.i_width =
578                     p_spu->p_text->fmt_out.video.i_visible_width =
579                     p_fmt->i_width;
580                 p_spu->p_text->fmt_out.video.i_height =
581                     p_spu->p_text->fmt_out.video.i_visible_height =
582                     p_fmt->i_height;
583             }
584         }
585
586         i_scale_width = i_scale_width_orig;
587         i_scale_height = i_scale_height_orig;
588
589         if( p_subpic->i_original_picture_height > 0 &&
590             p_subpic->i_original_picture_width  > 0 )
591         {
592             i_scale_width = i_scale_width * p_fmt->i_width /
593                              p_subpic->i_original_picture_width;
594             i_scale_height = i_scale_height * p_fmt->i_height /
595                              p_subpic->i_original_picture_height;
596         }
597         else if( p_subpic->i_original_picture_height > 0 )
598         {
599             i_scale_height = i_scale_height * p_fmt->i_height /
600                              p_subpic->i_original_picture_height;
601             i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height;
602         }
603
604         /* Set default subpicture aspect ratio */
605         if( p_region && p_region->fmt.i_aspect &&
606             (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
607         {
608             p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
609             p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
610         }
611         if( p_region &&
612             (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
613         {
614             p_region->fmt.i_sar_den = p_fmt->i_sar_den;
615             p_region->fmt.i_sar_num = p_fmt->i_sar_num;
616         }
617
618         /* Take care of the aspect ratio */
619         if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
620             p_region->fmt.i_sar_den * p_fmt->i_sar_num )
621         {
622             i_scale_width = i_scale_width *
623                 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
624                 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
625             i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
626         }
627
628         /* Load the scaling module */
629         if( !p_spu->p_scale && (i_scale_width != 1000 ||
630             i_scale_height != 1000) )
631         {
632             p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
633             vlc_object_attach( p_spu->p_scale, p_spu );
634             p_spu->p_scale->fmt_out.video.i_chroma =
635                 p_spu->p_scale->fmt_in.video.i_chroma =
636                     VLC_FOURCC('Y','U','V','P');
637             /* XXX: We'll also be using it for YUVA and RGBA blending ... */
638             p_spu->p_scale->fmt_in.video.i_width =
639                 p_spu->p_scale->fmt_in.video.i_height = 32;
640             p_spu->p_scale->fmt_out.video.i_width =
641                 p_spu->p_scale->fmt_out.video.i_height = 16;
642
643             p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
644             p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
645             p_spu->p_scale->p_module =
646                 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
647         }
648
649         while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
650         {
651             int i_fade_alpha = 255;
652             int i_x_offset = p_region->i_x + i_subpic_x;
653             int i_y_offset = p_region->i_y + p_subpic->i_y;
654
655             if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
656             {
657                 if( p_spu->p_text && p_spu->p_text->p_module )
658                 {
659                     p_region->i_align = p_subpic->i_flags;
660
661                     if( p_spu->p_text->pf_render_html && p_region->psz_html )
662                     {
663                         p_spu->p_text->pf_render_html( p_spu->p_text,
664                                                        p_region, p_region );
665                     }
666                     else if( p_spu->p_text->pf_render_text )
667                     {
668                         p_spu->p_text->pf_render_text( p_spu->p_text,
669                                                        p_region, p_region );
670                     }
671                 }
672             }
673
674             /* Force palette if requested */
675             if( p_spu->b_force_palette && VLC_FOURCC('Y','U','V','P') ==
676                 p_region->fmt.i_chroma )
677             {
678                 memcpy( p_region->fmt.p_palette->palette,
679                         p_spu->palette, 16 );
680             }
681
682             /* Scale SPU if necessary */
683             if( p_region->p_cache )
684             {
685                 if( i_scale_width * p_region->fmt.i_width / 1000 !=
686                     p_region->p_cache->fmt.i_width ||
687                     i_scale_height * p_region->fmt.i_height / 1000 !=
688                     p_region->p_cache->fmt.i_height )
689                 {
690                     p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
691                                                  p_region->p_cache );
692                     p_region->p_cache = 0;
693                 }
694             }
695
696             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
697                 p_spu->p_scale && !p_region->p_cache )
698             {
699                 picture_t *p_pic;
700
701                 p_spu->p_scale->fmt_in.video = p_region->fmt;
702                 p_spu->p_scale->fmt_out.video = p_region->fmt;
703
704                 p_region->p_cache =
705                     p_subpic->pf_create_region( VLC_OBJECT(p_spu),
706                         &p_spu->p_scale->fmt_out.video );
707                 if( p_spu->p_scale->fmt_out.video.p_palette )
708                     *p_spu->p_scale->fmt_out.video.p_palette =
709                         *p_region->fmt.p_palette;
710                 p_region->p_cache->p_next = p_region->p_next;
711
712                 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
713                                   &p_region->picture );
714
715                 p_spu->p_scale->fmt_out.video.i_width =
716                     p_region->fmt.i_width * i_scale_width / 1000;
717                 p_spu->p_scale->fmt_out.video.i_visible_width =
718                     p_region->fmt.i_visible_width * i_scale_width / 1000;
719                 p_spu->p_scale->fmt_out.video.i_height =
720                     p_region->fmt.i_height * i_scale_height / 1000;
721                 p_spu->p_scale->fmt_out.video.i_visible_height =
722                     p_region->fmt.i_visible_height * i_scale_height / 1000;
723                 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
724                 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
725                 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
726
727                 p_pic = p_spu->p_scale->pf_video_filter(
728                                  p_spu->p_scale, &p_region->p_cache->picture );
729                 if( p_pic )
730                 {
731                     picture_t p_pic_tmp = p_region->p_cache->picture;
732                     p_region->p_cache->picture = *p_pic;
733                     *p_pic = p_pic_tmp;
734                     free( p_pic );
735                 }
736             }
737             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
738                 p_spu->p_scale && p_region->p_cache )
739             {
740                 p_region = p_region->p_cache;
741             }
742
743             if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
744             {
745                 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
746                     p_subpic->i_y;
747             }
748             else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
749             {
750                 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
751             }
752
753             if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
754             {
755                 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
756                     i_subpic_x;
757             }
758             else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
759             {
760                 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
761             }
762
763             if( p_subpic->b_absolute )
764             {
765                 i_x_offset = p_region->i_x +
766                     i_subpic_x * i_scale_width / 1000;
767                 i_y_offset = p_region->i_y +
768                     p_subpic->i_y * i_scale_height / 1000;
769
770             }
771
772             i_x_offset = __MAX( i_x_offset, 0 );
773             i_y_offset = __MAX( i_y_offset, 0 );
774
775             if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
776             {
777                 int i_diff = 0;
778                 int i_low = i_y_offset - p_spu->i_margin;
779                 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
780
781                 /* crop extra margin to keep within bounds */
782                 if( i_low < 0 ) i_diff = i_low;
783                 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
784                 i_y_offset -= ( p_spu->i_margin + i_diff );
785             }
786
787             p_spu->p_blend->fmt_in.video = p_region->fmt;
788
789             /* Force cropping if requested */
790             if( p_spu->b_force_crop )
791             {
792                 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
793                 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
794                 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
795                 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
796                 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
797
798                 /* Find the intersection */
799                 if( i_crop_x + i_crop_width <= i_x_offset ||
800                     i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
801                     i_crop_y + i_crop_height <= i_y_offset ||
802                     i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
803                 {
804                     /* No intersection */
805                     p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
806                 }
807                 else
808                 {
809                     int i_x, i_y, i_x_end, i_y_end;
810                     i_x = __MAX( i_crop_x, i_x_offset );
811                     i_y = __MAX( i_crop_y, i_y_offset );
812                     i_x_end = __MIN( i_crop_x + i_crop_width,
813                                    i_x_offset + (int)p_fmt->i_visible_width );
814                     i_y_end = __MIN( i_crop_y + i_crop_height,
815                                    i_y_offset + (int)p_fmt->i_visible_height );
816
817                     p_fmt->i_x_offset = i_x - i_x_offset;
818                     p_fmt->i_y_offset = i_y - i_y_offset;
819                     p_fmt->i_visible_width = i_x_end - i_x;
820                     p_fmt->i_visible_height = i_y_end - i_y;
821
822                     i_x_offset = i_x;
823                     i_y_offset = i_y;
824                 }
825             }
826
827             /* Update the output picture size */
828             p_spu->p_blend->fmt_out.video.i_width =
829                 p_spu->p_blend->fmt_out.video.i_visible_width =
830                     p_fmt->i_width;
831             p_spu->p_blend->fmt_out.video.i_height =
832                 p_spu->p_blend->fmt_out.video.i_visible_height =
833                     p_fmt->i_height;
834
835             if( p_subpic->b_fade )
836             {
837                 mtime_t i_fade_start = ( p_subpic->i_stop +
838                                          p_subpic->i_start ) / 2;
839                 mtime_t i_now = mdate();
840                 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
841                 {
842                     i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
843                                    ( p_subpic->i_stop - i_fade_start );
844                 }
845             }
846
847             i_x_offset = __MAX( i_x_offset, 0 );
848             i_y_offset = __MAX( i_y_offset, 0 );
849
850             p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
851                 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
852                 i_fade_alpha * p_subpic->i_alpha / 255 );
853
854             p_region = p_region->p_next;
855         }
856
857         p_subpic = p_subpic->p_next;
858     }
859
860     vlc_mutex_unlock( &p_spu->subpicture_lock );
861 }
862
863 /*****************************************************************************
864  * spu_SortSubpictures: find the subpictures to display
865  *****************************************************************************
866  * This function parses all subpictures and decides which ones need to be
867  * displayed. This operation does not need lock, since only READY_SUBPICTURE
868  * are handled. If no picture has been selected, display_date will depend on
869  * the subpicture.
870  * We also check for ephemer DVD subpictures (subpictures that have
871  * to be removed if a newer one is available), which makes it a lot
872  * more difficult to guess if a subpicture has to be rendered or not.
873  *****************************************************************************/
874 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
875                                    vlc_bool_t b_paused )
876 {
877     int i_index, i_channel;
878     subpicture_t *p_subpic = NULL;
879     subpicture_t *p_ephemer;
880     mtime_t      ephemer_date;
881
882     /* Run subpicture filters */
883     for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
884     {
885         subpicture_t *p_subpic_filter;
886         p_subpic_filter = p_spu->pp_filter[i_index]->
887             pf_sub_filter( p_spu->pp_filter[i_index], display_date );
888         if( p_subpic_filter )
889         {
890             spu_DisplaySubpicture( p_spu, p_subpic_filter );
891         }
892     }
893
894     /* We get an easily parsable chained list of subpictures which
895      * ends with NULL since p_subpic was initialized to NULL. */
896     for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
897     {
898         p_ephemer = 0;
899         ephemer_date = 0;
900
901         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
902         {
903             if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
904                 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
905             {
906                 continue;
907             }
908             if( display_date &&
909                 display_date < p_spu->p_subpicture[i_index].i_start )
910             {
911                 /* Too early, come back next monday */
912                 continue;
913             }
914
915             if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
916                 ephemer_date = p_spu->p_subpicture[i_index].i_start;
917
918             if( display_date > p_spu->p_subpicture[i_index].i_stop &&
919                 ( !p_spu->p_subpicture[i_index].b_ephemer ||
920                   p_spu->p_subpicture[i_index].i_stop >
921                   p_spu->p_subpicture[i_index].i_start ) &&
922                 !( p_spu->p_subpicture[i_index].b_pausable &&
923                    b_paused ) )
924             {
925                 /* Too late, destroy the subpic */
926                 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
927                 continue;
928             }
929
930             /* If this is an ephemer subpic, add it to our list */
931             if( p_spu->p_subpicture[i_index].b_ephemer )
932             {
933                 p_spu->p_subpicture[i_index].p_next = p_ephemer;
934                 p_ephemer = &p_spu->p_subpicture[i_index];
935
936                 continue;
937             }
938
939             p_spu->p_subpicture[i_index].p_next = p_subpic;
940             p_subpic = &p_spu->p_subpicture[i_index];
941         }
942
943         /* If we found ephemer subpictures, check if they have to be
944          * displayed or destroyed */
945         while( p_ephemer != NULL )
946         {
947             subpicture_t *p_tmp = p_ephemer;
948             p_ephemer = p_ephemer->p_next;
949
950             if( p_tmp->i_start < ephemer_date )
951             {
952                 /* Ephemer subpicture has lived too long */
953                 spu_DestroySubpicture( p_spu, p_tmp );
954             }
955             else
956             {
957                 /* Ephemer subpicture can still live a bit */
958                 p_tmp->p_next = p_subpic;
959                 p_subpic = p_tmp;
960             }
961         }
962     }
963
964     return p_subpic;
965 }
966
967 /*****************************************************************************
968  * SpuClearChannel: clear an spu channel
969  *****************************************************************************
970  * This function destroys the subpictures which belong to the spu channel
971  * corresponding to i_channel_id.
972  *****************************************************************************/
973 static void SpuClearChannel( spu_t *p_spu, int i_channel )
974 {
975     int          i_subpic;                               /* subpicture index */
976     subpicture_t *p_subpic = NULL;                  /* first free subpicture */
977
978     vlc_mutex_lock( &p_spu->subpicture_lock );
979
980     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
981     {
982         p_subpic = &p_spu->p_subpicture[i_subpic];
983         if( p_subpic->i_status == FREE_SUBPICTURE
984             || ( p_subpic->i_status != RESERVED_SUBPICTURE
985                  && p_subpic->i_status != READY_SUBPICTURE ) )
986         {
987             continue;
988         }
989
990         if( p_subpic->i_channel == i_channel )
991         {
992             while( p_subpic->p_region )
993             {
994                 subpicture_region_t *p_region = p_subpic->p_region;
995                 p_subpic->p_region = p_region->p_next;
996                 spu_DestroyRegion( p_spu, p_region );
997             }
998
999             if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1000             p_subpic->i_status = FREE_SUBPICTURE;
1001         }
1002     }
1003
1004     vlc_mutex_unlock( &p_spu->subpicture_lock );
1005 }
1006
1007 /*****************************************************************************
1008  * spu_ControlDefault: default methods for the subpicture unit control.
1009  *****************************************************************************/
1010 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1011 {
1012     int *pi, i;
1013
1014     switch( i_query )
1015     {
1016     case SPU_CHANNEL_REGISTER:
1017         pi = (int *)va_arg( args, int * );
1018         if( pi ) *pi = p_spu->i_channel++;
1019         break;
1020
1021     case SPU_CHANNEL_CLEAR:
1022         i = (int)va_arg( args, int );
1023         SpuClearChannel( p_spu, i );
1024         break;
1025
1026     default:
1027         msg_Dbg( p_spu, "control query not supported" );
1028         return VLC_EGENERIC;
1029     }
1030
1031     return VLC_SUCCESS;
1032 }
1033
1034 /*****************************************************************************
1035  * Object variables callbacks
1036  *****************************************************************************/
1037
1038 /*****************************************************************************
1039  * UpdateSPU: update subpicture settings
1040  *****************************************************************************
1041  * This function is called from CropCallback and at initialization time, to
1042  * retrieve crop information from the input.
1043  *****************************************************************************/
1044 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1045 {
1046     vlc_value_t val;
1047
1048     p_spu->b_force_palette = VLC_FALSE;
1049     p_spu->b_force_crop = VLC_FALSE;
1050
1051     if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1052
1053     p_spu->b_force_crop = VLC_TRUE;
1054     var_Get( p_object, "x-start", &val );
1055     p_spu->i_crop_x = val.i_int;
1056     var_Get( p_object, "y-start", &val );
1057     p_spu->i_crop_y = val.i_int;
1058     var_Get( p_object, "x-end", &val );
1059     p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1060     var_Get( p_object, "y-end", &val );
1061     p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1062
1063     if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1064     {
1065         memcpy( p_spu->palette, val.p_address, 16 );
1066         p_spu->b_force_palette = VLC_TRUE;
1067     }
1068
1069     msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1070              p_spu->i_crop_x, p_spu->i_crop_y,
1071              p_spu->i_crop_width, p_spu->i_crop_height,
1072              p_spu->b_force_palette );
1073 }
1074
1075 /*****************************************************************************
1076  * CropCallback: called when the highlight properties are changed
1077  *****************************************************************************
1078  * This callback is called from the input thread when we need cropping
1079  *****************************************************************************/
1080 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1081                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1082 {
1083     (void)psz_var; (void)oldval; (void)newval;
1084     UpdateSPU( (spu_t *)p_data, p_object );
1085     return VLC_SUCCESS;
1086 }
1087
1088 /*****************************************************************************
1089  * Buffers allocation callbacks for the filters
1090  *****************************************************************************/
1091 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1092 {
1093     filter_owner_sys_t *p_sys = p_filter->p_owner;
1094     subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1095     if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1096     return p_subpicture;
1097 }
1098
1099 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1100 {
1101     filter_owner_sys_t *p_sys = p_filter->p_owner;
1102     spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1103 }
1104
1105 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1106 {
1107     subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1108     (void)p_filter;
1109     memset( p_subpic, 0, sizeof(subpicture_t) );
1110     p_subpic->b_absolute = VLC_TRUE;
1111
1112     p_subpic->pf_create_region = __spu_CreateRegion;
1113     p_subpic->pf_make_region = __spu_MakeRegion;
1114     p_subpic->pf_destroy_region = __spu_DestroyRegion;
1115
1116     return p_subpic;
1117 }
1118
1119 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1120 {
1121     while( p_subpic->p_region )
1122     {
1123         subpicture_region_t *p_region = p_subpic->p_region;
1124         p_subpic->p_region = p_region->p_next;
1125         p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1126     }
1127
1128     free( p_subpic );
1129 }
1130
1131 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1132 {
1133     picture_t *p_picture = malloc( sizeof(picture_t) );
1134
1135     if( vout_AllocatePicture( p_filter, p_picture,
1136                               p_filter->fmt_out.video.i_chroma,
1137                               p_filter->fmt_out.video.i_width,
1138                               p_filter->fmt_out.video.i_height,
1139                               p_filter->fmt_out.video.i_aspect )
1140         != VLC_SUCCESS )
1141     {
1142         free( p_picture );
1143         return NULL;
1144     }
1145
1146     p_picture->pf_release = RegionPictureRelease;
1147
1148     return p_picture;
1149 }
1150
1151 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1152 {
1153     (void)p_filter;
1154     if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1155     if( p_pic ) free( p_pic );
1156 }
1157
1158 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1159                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1160 {
1161     (void)p_object; (void)oldval; (void)newval;
1162
1163     if( !strcmp( psz_var, "sub-filter" ) )
1164     {
1165         spu_t *p_spu = (spu_t *)p_data;
1166         vlc_mutex_lock( &p_spu->subpicture_lock );
1167         spu_DeleteChain( p_spu );
1168         spu_ParseChain( p_spu );
1169         vlc_mutex_unlock( &p_spu->subpicture_lock );
1170     }
1171     return VLC_SUCCESS;
1172 }