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