]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
* Don't allow negative offsets when blending
[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 #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 struct filter_owner_sys_t
56 {
57     spu_t *p_spu;
58     int i_channel;
59 };
60
61 /**
62  * Creates the subpicture unit
63  *
64  * \param p_this the parent object which creates the subpicture unit
65  */
66 spu_t *__spu_Create( vlc_object_t *p_this )
67 {
68     int i_index;
69     spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
70
71     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
72     {
73         p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
74     }
75
76     p_spu->p_blend = NULL;
77     p_spu->p_text = NULL;
78     p_spu->p_scale = NULL;
79     p_spu->i_filter = 0;
80     p_spu->pf_control = spu_vaControlDefault;
81
82     /* Register the default subpicture channel */
83     p_spu->i_channel = 2;
84
85     vlc_mutex_init( p_this, &p_spu->subpicture_lock );
86
87     vlc_object_attach( p_spu, p_this );
88
89     return p_spu;
90 }
91
92 /**
93  * Initialise the subpicture unit
94  *
95  * \param p_spu the subpicture unit object
96  */
97 int spu_Init( spu_t *p_spu )
98 {
99     char *psz_parser;
100     vlc_value_t val;
101
102     /* If the user requested a sub margin, we force the position. */
103     var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
104     var_Get( p_spu, "sub-margin", &val );
105     p_spu->i_margin = val.i_int;
106
107     var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
108     var_Get( p_spu, "sub-filter", &val );
109
110     psz_parser = val.psz_string;
111
112     while( psz_parser && *psz_parser )
113     {
114         config_chain_t *p_cfg; /* Do we ever need to free this ? */
115         char *psz_name;
116
117         psz_parser = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
118
119         msg_Dbg( p_spu, "adding sub-filter: %s", psz_name );
120
121         p_spu->pp_filter[p_spu->i_filter] =
122             vlc_object_create( p_spu, VLC_OBJECT_FILTER );
123         vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
124         p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
125         p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
126         p_spu->pp_filter[p_spu->i_filter]->p_cfg = p_cfg;;
127         p_spu->pp_filter[p_spu->i_filter]->p_module =
128             module_Need( p_spu->pp_filter[p_spu->i_filter],
129                          "sub filter", psz_name, 0 );
130         if( p_spu->pp_filter[p_spu->i_filter]->p_module )
131         {
132             filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
133             p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
134             spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
135             p_sys->p_spu = p_spu;
136             p_spu->i_filter++;
137         }
138         else
139         {
140             msg_Dbg( p_spu, "no sub filter found" );
141             vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
142             vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
143         }
144
145         if( p_spu->i_filter >= 10 )
146         {
147             msg_Dbg( p_spu, "can't add anymore filters" );
148         }
149
150         free( psz_name );
151     }
152     if( val.psz_string ) free( val.psz_string );
153
154     return VLC_EGENERIC;
155 }
156
157 /**
158  * Destroy the subpicture unit
159  *
160  * \param p_this the parent object which destroys the subpicture unit
161  */
162 void spu_Destroy( spu_t *p_spu )
163 {
164     int i_index;
165
166     vlc_object_detach( p_spu );
167
168     /* Destroy all remaining subpictures */
169     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
170     {
171         if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
172         {
173             spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
174         }
175     }
176
177     if( p_spu->p_blend )
178     {
179         if( p_spu->p_blend->p_module )
180             module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
181
182         vlc_object_detach( p_spu->p_blend );
183         vlc_object_destroy( p_spu->p_blend );
184     }
185
186     if( p_spu->p_text )
187     {
188         if( p_spu->p_text->p_module )
189             module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
190
191         vlc_object_detach( p_spu->p_text );
192         vlc_object_destroy( p_spu->p_text );
193     }
194
195     if( p_spu->p_scale )
196     {
197         if( p_spu->p_scale->p_module )
198             module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
199
200         vlc_object_detach( p_spu->p_scale );
201         vlc_object_destroy( p_spu->p_scale );
202     }
203
204     while( p_spu->i_filter-- )
205     {
206         module_Unneed( p_spu->pp_filter[p_spu->i_filter],
207                        p_spu->pp_filter[p_spu->i_filter]->p_module );
208         free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
209         vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
210         vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
211     }
212
213     vlc_mutex_destroy( &p_spu->subpicture_lock );
214     vlc_object_destroy( p_spu );
215 }
216
217 /**
218  * Attach/Detach the SPU from any input
219  *
220  * \param p_this the object in which to destroy the subpicture unit
221  * \param b_attach to select attach or detach
222  */
223 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
224 {
225     vlc_object_t *p_input;
226
227     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
228     if( !p_input ) return;
229
230     if( b_attach )
231     {
232         UpdateSPU( p_spu, VLC_OBJECT(p_input) );
233         var_AddCallback( p_input, "highlight", CropCallback, p_spu );
234         vlc_object_release( p_input );
235     }
236     else
237     {
238         /* Delete callback */
239         var_DelCallback( p_input, "highlight", CropCallback, p_spu );
240         vlc_object_release( p_input );
241     }
242 }
243
244 /**
245  * Create a subpicture region
246  *
247  * \param p_this vlc_object_t
248  * \param p_fmt the format that this subpicture region should have
249  */
250 static void RegionPictureRelease( picture_t *p_pic )
251 {
252     if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
253 }
254 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
255                                          video_format_t *p_fmt )
256 {
257     subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
258     memset( p_region, 0, sizeof(subpicture_region_t) );
259     p_region->p_next = 0;
260     p_region->p_cache = 0;
261     p_region->fmt = *p_fmt;
262     p_region->psz_text = 0;
263     p_region->p_style = NULL;
264
265     if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
266         p_fmt->p_palette = p_region->fmt.p_palette =
267             malloc( sizeof(video_palette_t) );
268     else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
269
270     p_region->picture.p_data_orig = NULL;
271
272     if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
273
274     vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
275                           p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
276
277     if( !p_region->picture.i_planes )
278     {
279         free( p_region );
280         free( p_fmt->p_palette );
281         return NULL;
282     }
283
284     p_region->picture.pf_release = RegionPictureRelease;
285
286     return p_region;
287 }
288
289 /**
290  * Make a subpicture region from an existing picture_t
291  *
292  * \param p_this vlc_object_t
293  * \param p_fmt the format that this subpicture region should have
294  * \param p_pic a pointer to the picture creating the region (not freed)
295  */
296 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
297                                        video_format_t *p_fmt,
298                                        picture_t *p_pic )
299 {
300     subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
301     memset( p_region, 0, sizeof(subpicture_region_t) );
302     p_region->p_next = 0;
303     p_region->p_cache = 0;
304     p_region->fmt = *p_fmt;
305     p_region->psz_text = 0;
306     p_region->p_style = NULL;
307
308     if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
309         p_fmt->p_palette = p_region->fmt.p_palette =
310             malloc( sizeof(video_palette_t) );
311     else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
312
313     memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
314     p_region->picture.pf_release = RegionPictureRelease;
315
316     return p_region;
317 }
318
319 /**
320  * Destroy a subpicture region
321  *
322  * \param p_this vlc_object_t
323  * \param p_region the subpicture region to destroy
324  */
325 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
326 {
327     if( !p_region ) return;
328     if( p_region->picture.pf_release )
329         p_region->picture.pf_release( &p_region->picture );
330     if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
331     if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
332     free( p_region );
333 }
334
335 /**
336  * Display a subpicture
337  *
338  * Remove the reservation flag of a subpicture, which will cause it to be
339  * ready for display.
340  * \param p_spu the subpicture unit object
341  * \param p_subpic the subpicture to display
342  */
343 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
344 {
345     /* Check if status is valid */
346     if( p_subpic->i_status != RESERVED_SUBPICTURE )
347     {
348         msg_Err( p_spu, "subpicture %p has invalid status #%d",
349                  p_subpic, p_subpic->i_status );
350     }
351
352     /* Remove reservation flag */
353     p_subpic->i_status = READY_SUBPICTURE;
354
355     if( p_subpic->i_channel == DEFAULT_CHAN )
356     {
357         p_subpic->i_channel = 0xFFFF;
358         spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
359         p_subpic->i_channel = DEFAULT_CHAN;
360     }
361 }
362
363 /**
364  * Allocate a subpicture in the spu heap.
365  *
366  * This function create a reserved subpicture in the spu heap.
367  * A null pointer is returned if the function fails. This method provides an
368  * already allocated zone of memory in the spu data fields. It needs locking
369  * since several pictures can be created by several producers threads.
370  * \param p_spu the subpicture unit in which to create the subpicture
371  * \return NULL on error, a reserved subpicture otherwise
372  */
373 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
374 {
375     int                 i_subpic;                        /* subpicture index */
376     subpicture_t *      p_subpic = NULL;            /* first free subpicture */
377
378     /* Get lock */
379     vlc_mutex_lock( &p_spu->subpicture_lock );
380
381     /*
382      * Look for an empty place
383      */
384     p_subpic = NULL;
385     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
386     {
387         if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
388         {
389             /* Subpicture is empty and ready for allocation */
390             p_subpic = &p_spu->p_subpicture[i_subpic];
391             p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
392             break;
393         }
394     }
395
396     /* If no free subpicture could be found */
397     if( p_subpic == NULL )
398     {
399         msg_Err( p_spu, "subpicture heap is full" );
400         vlc_mutex_unlock( &p_spu->subpicture_lock );
401         return NULL;
402     }
403
404     /* Copy subpicture information, set some default values */
405     memset( p_subpic, 0, sizeof(subpicture_t) );
406     p_subpic->i_status   = RESERVED_SUBPICTURE;
407     p_subpic->b_absolute = VLC_TRUE;
408     p_subpic->b_pausable = VLC_FALSE;
409     p_subpic->b_fade     = VLC_FALSE;
410     p_subpic->i_alpha    = 0xFF;
411     p_subpic->p_region   = 0;
412     p_subpic->pf_render  = 0;
413     p_subpic->pf_destroy = 0;
414     p_subpic->p_sys      = 0;
415     vlc_mutex_unlock( &p_spu->subpicture_lock );
416
417     p_subpic->pf_create_region = __spu_CreateRegion;
418     p_subpic->pf_make_region = __spu_MakeRegion;
419     p_subpic->pf_destroy_region = __spu_DestroyRegion;
420
421     return p_subpic;
422 }
423
424 /**
425  * Remove a subpicture from the heap
426  *
427  * This function frees a previously reserved subpicture.
428  * It is meant to be used when the construction of a picture aborted.
429  * This function does not need locking since reserved subpictures are ignored
430  * by the spu.
431  */
432 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
433 {
434     /* Get lock */
435     vlc_mutex_lock( &p_spu->subpicture_lock );
436
437     /* There can be race conditions so we need to check the status */
438     if( p_subpic->i_status == FREE_SUBPICTURE )
439     {
440         vlc_mutex_unlock( &p_spu->subpicture_lock );
441         return;
442     }
443
444     /* Check if status is valid */
445     if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
446            && ( p_subpic->i_status != READY_SUBPICTURE ) )
447     {
448         msg_Err( p_spu, "subpicture %p has invalid status %d",
449                          p_subpic, p_subpic->i_status );
450     }
451
452     while( p_subpic->p_region )
453     {
454         subpicture_region_t *p_region = p_subpic->p_region;
455         p_subpic->p_region = p_region->p_next;
456         spu_DestroyRegion( p_spu, p_region );
457     }
458
459     if( p_subpic->pf_destroy )
460     {
461         p_subpic->pf_destroy( p_subpic );
462     }
463
464     p_subpic->i_status = FREE_SUBPICTURE;
465
466     vlc_mutex_unlock( &p_spu->subpicture_lock );
467 }
468
469 /*****************************************************************************
470  * spu_RenderSubpictures: render a subpicture list
471  *****************************************************************************
472  * This function renders all sub picture units in the list.
473  *****************************************************************************/
474 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
475                             picture_t *p_pic_dst, picture_t *p_pic_src,
476                             subpicture_t *p_subpic,
477                             int i_scale_width_orig, int i_scale_height_orig )
478 {
479     /* Get lock */
480     vlc_mutex_lock( &p_spu->subpicture_lock );
481
482     /* Check i_status again to make sure spudec hasn't destroyed the subpic */
483     while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
484     {
485         subpicture_region_t *p_region = p_subpic->p_region;
486         int i_scale_width, i_scale_height;
487         int i_subpic_x = p_subpic->i_x;
488
489         /* Load the blending module */
490         if( !p_spu->p_blend && p_region )
491         {
492             p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
493             vlc_object_attach( p_spu->p_blend, p_spu );
494             p_spu->p_blend->fmt_out.video.i_x_offset =
495                 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
496             p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
497             p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
498             p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
499
500             p_spu->p_blend->p_module =
501                 module_Need( p_spu->p_blend, "video blending", 0, 0 );
502         }
503
504         /* Load the text rendering module */
505         if( !p_spu->p_text && p_region && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
506         {
507             char *psz_modulename = NULL;
508
509             p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
510             vlc_object_attach( p_spu->p_text, p_spu );
511
512             p_spu->p_text->fmt_out.video.i_width =
513                 p_spu->p_text->fmt_out.video.i_visible_width =
514                 p_fmt->i_width;
515             p_spu->p_text->fmt_out.video.i_height =
516                 p_spu->p_text->fmt_out.video.i_visible_height =
517                 p_fmt->i_height;
518
519             p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
520             p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
521
522             psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
523             if( psz_modulename && *psz_modulename )
524             {
525                 p_spu->p_text->p_module =
526                     module_Need( p_spu->p_text, "text renderer", psz_modulename, VLC_TRUE );
527             }
528             if( !p_spu->p_text->p_module )
529             {
530                 p_spu->p_text->p_module =
531                     module_Need( p_spu->p_text, "text renderer", 0, 0 );
532             }
533             if( psz_modulename ) free( psz_modulename );
534         }
535         if( p_spu->p_text )
536         {
537             if( p_subpic->i_original_picture_height > 0 &&
538                 p_subpic->i_original_picture_width  > 0 )
539             {
540                 p_spu->p_text->fmt_out.video.i_width =
541                     p_spu->p_text->fmt_out.video.i_visible_width =
542                     p_subpic->i_original_picture_width;
543                 p_spu->p_text->fmt_out.video.i_height =
544                     p_spu->p_text->fmt_out.video.i_visible_height =
545                     p_subpic->i_original_picture_height;
546             }
547             else
548             {
549                 p_spu->p_text->fmt_out.video.i_width =
550                     p_spu->p_text->fmt_out.video.i_visible_width =
551                     p_fmt->i_width;
552                 p_spu->p_text->fmt_out.video.i_height =
553                     p_spu->p_text->fmt_out.video.i_visible_height =
554                     p_fmt->i_height;
555             }
556         }
557
558         i_scale_width = i_scale_width_orig;
559         i_scale_height = i_scale_height_orig;
560
561         if( p_subpic->i_original_picture_height > 0 &&
562             p_subpic->i_original_picture_width  > 0 )
563         {
564             i_scale_width = i_scale_width * p_fmt->i_width /
565                              p_subpic->i_original_picture_width;
566             i_scale_height = i_scale_height * p_fmt->i_height /
567                              p_subpic->i_original_picture_height;
568         }
569         else if( p_subpic->i_original_picture_height > 0 )
570         {
571             i_scale_height = i_scale_height * p_fmt->i_height /
572                              p_subpic->i_original_picture_height;
573             i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height; 
574         }
575
576         /* Set default subpicture aspect ratio */
577         if( p_region && p_region->fmt.i_aspect &&
578             (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
579         {
580             p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
581             p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
582         }
583         if( p_region &&
584             (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
585         {
586             p_region->fmt.i_sar_den = p_fmt->i_sar_den;
587             p_region->fmt.i_sar_num = p_fmt->i_sar_num;
588         }
589
590         /* Take care of the aspect ratio */
591         if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
592             p_region->fmt.i_sar_den * p_fmt->i_sar_num )
593         {
594             i_scale_width = i_scale_width *
595                 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
596                 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
597             i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
598         }
599
600         /* Load the scaling module */
601         if( !p_spu->p_scale && (i_scale_width != 1000 ||
602             i_scale_height != 1000) )
603         {
604             p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
605             vlc_object_attach( p_spu->p_scale, p_spu );
606             p_spu->p_scale->fmt_out.video.i_chroma =
607                 p_spu->p_scale->fmt_in.video.i_chroma =
608                     VLC_FOURCC('Y','U','V','P');
609             p_spu->p_scale->fmt_in.video.i_width =
610                 p_spu->p_scale->fmt_in.video.i_height = 32;
611             p_spu->p_scale->fmt_out.video.i_width =
612                 p_spu->p_scale->fmt_out.video.i_height = 16;
613
614             p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
615             p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
616             p_spu->p_scale->p_module =
617                 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
618         }
619
620         while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
621         {
622             int i_fade_alpha = 255;
623             int i_x_offset = p_region->i_x + i_subpic_x;
624             int i_y_offset = p_region->i_y + p_subpic->i_y;
625
626             if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
627             {
628                 if( p_spu->p_text && p_spu->p_text->p_module &&
629                     p_spu->p_text->pf_render_text )
630                 {
631                     p_region->i_align = p_subpic->i_flags;
632                     p_spu->p_text->pf_render_text( p_spu->p_text,
633                                                    p_region, p_region ); 
634                 }
635             }
636
637             /* Force palette if requested */
638             if( p_spu->b_force_palette && VLC_FOURCC('Y','U','V','P') ==
639                 p_region->fmt.i_chroma )
640             {
641                 memcpy( p_region->fmt.p_palette->palette,
642                         p_spu->palette, 16 );
643             }
644
645             /* Scale SPU if necessary */
646             if( p_region->p_cache )
647             {
648                 if( i_scale_width * p_region->fmt.i_width / 1000 !=
649                     p_region->p_cache->fmt.i_width ||
650                     i_scale_height * p_region->fmt.i_height / 1000 !=
651                     p_region->p_cache->fmt.i_height )
652                 {
653                     p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
654                                                  p_region->p_cache );
655                     p_region->p_cache = 0;
656                 }
657             }
658
659             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
660                 p_spu->p_scale && !p_region->p_cache )
661             {
662                 picture_t *p_pic;
663
664                 p_spu->p_scale->fmt_in.video = p_region->fmt;
665                 p_spu->p_scale->fmt_out.video = p_region->fmt;
666
667                 p_region->p_cache =
668                     p_subpic->pf_create_region( VLC_OBJECT(p_spu),
669                         &p_spu->p_scale->fmt_out.video );
670                 if( p_spu->p_scale->fmt_out.video.p_palette )
671                     *p_spu->p_scale->fmt_out.video.p_palette =
672                         *p_region->fmt.p_palette;
673                 p_region->p_cache->p_next = p_region->p_next;
674
675                 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
676                                   &p_region->picture );
677
678                 p_spu->p_scale->fmt_out.video.i_width =
679                     p_region->fmt.i_width * i_scale_width / 1000;
680                 p_spu->p_scale->fmt_out.video.i_visible_width =
681                     p_region->fmt.i_visible_width * i_scale_width / 1000;
682                 p_spu->p_scale->fmt_out.video.i_height =
683                     p_region->fmt.i_height * i_scale_height / 1000;
684                 p_spu->p_scale->fmt_out.video.i_visible_height =
685                     p_region->fmt.i_visible_height * i_scale_height / 1000;
686                 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
687                 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
688                 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
689
690                 p_pic = p_spu->p_scale->pf_video_filter(
691                                  p_spu->p_scale, &p_region->p_cache->picture );
692                 if( p_pic )
693                 {
694                     picture_t p_pic_tmp = p_region->p_cache->picture;
695                     p_region->p_cache->picture = *p_pic;
696                     *p_pic = p_pic_tmp;
697                     free( p_pic );
698                 }
699             }
700             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
701                 p_spu->p_scale && p_region->p_cache )
702             {
703                 p_region = p_region->p_cache;
704             }
705
706             if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
707             {
708                 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
709                     p_subpic->i_y;
710             }
711             else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
712             {
713                 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
714             }
715
716             if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
717             {
718                 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
719                     i_subpic_x;
720             }
721             else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
722             {
723                 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
724             }
725
726             if( p_subpic->b_absolute )
727             {
728                 i_x_offset = p_region->i_x +
729                     i_subpic_x * i_scale_width / 1000;
730                 i_y_offset = p_region->i_y +
731                     p_subpic->i_y * i_scale_height / 1000;
732
733             }
734
735             if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
736             {
737                 int i_diff = 0;
738                 int i_low = i_y_offset - p_spu->i_margin;
739                 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
740
741                 /* crop extra margin to keep within bounds */
742                 if( i_low < 0 ) i_diff = i_low;
743                 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
744                 i_y_offset -= ( p_spu->i_margin + i_diff );
745             }
746
747             p_spu->p_blend->fmt_in.video = p_region->fmt;
748
749             /* Force cropping if requested */
750             if( p_spu->b_force_crop )
751             {
752                 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
753                 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
754                 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
755                 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
756                 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
757
758                 /* Find the intersection */
759                 if( i_crop_x + i_crop_width <= i_x_offset ||
760                     i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
761                     i_crop_y + i_crop_height <= i_y_offset ||
762                     i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
763                 {
764                     /* No intersection */
765                     p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
766                 }
767                 else
768                 {
769                     int i_x, i_y, i_x_end, i_y_end;
770                     i_x = __MAX( i_crop_x, i_x_offset );
771                     i_y = __MAX( i_crop_y, i_y_offset );
772                     i_x_end = __MIN( i_crop_x + i_crop_width,
773                                    i_x_offset + (int)p_fmt->i_visible_width );
774                     i_y_end = __MIN( i_crop_y + i_crop_height,
775                                    i_y_offset + (int)p_fmt->i_visible_height );
776
777                     p_fmt->i_x_offset = i_x - i_x_offset;
778                     p_fmt->i_y_offset = i_y - i_y_offset;
779                     p_fmt->i_visible_width = i_x_end - i_x;
780                     p_fmt->i_visible_height = i_y_end - i_y;
781
782                     i_x_offset = i_x;
783                     i_y_offset = i_y;
784                 }
785             }
786
787             /* Update the output picture size */
788             p_spu->p_blend->fmt_out.video.i_width =
789                 p_spu->p_blend->fmt_out.video.i_visible_width =
790                     p_fmt->i_width;
791             p_spu->p_blend->fmt_out.video.i_height =
792                 p_spu->p_blend->fmt_out.video.i_visible_height =
793                     p_fmt->i_height;
794
795             if( p_subpic->b_fade )
796             {
797                 mtime_t i_fade_start = ( p_subpic->i_stop +
798                                          p_subpic->i_start ) / 2;
799                 mtime_t i_now = mdate();
800                 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
801                 {
802                     i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
803                                    ( p_subpic->i_stop - i_fade_start );
804                 }
805             }
806
807             i_x_offset = __MIN( i_x_offset, 0 );
808             i_y_offset = __MIN( i_y_offset, 0 );
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 }