Trailing spaces, again.
[vlc.git] / 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
333     if( p_region->psz_text )
334         free( p_region->psz_text );
335     //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
336     free( p_region );
337 }
338
339 /**
340  * Display a subpicture
341  *
342  * Remove the reservation flag of a subpicture, which will cause it to be
343  * ready for display.
344  * \param p_spu the subpicture unit object
345  * \param p_subpic the subpicture to display
346  */
347 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
348 {
349     /* Check if status is valid */
350     if( p_subpic->i_status != RESERVED_SUBPICTURE )
351     {
352         msg_Err( p_spu, "subpicture %p has invalid status #%d",
353                  p_subpic, p_subpic->i_status );
354     }
355
356     /* Remove reservation flag */
357     p_subpic->i_status = READY_SUBPICTURE;
358
359     if( p_subpic->i_channel == DEFAULT_CHAN )
360     {
361         p_subpic->i_channel = 0xFFFF;
362         spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
363         p_subpic->i_channel = DEFAULT_CHAN;
364     }
365 }
366
367 /**
368  * Allocate a subpicture in the spu heap.
369  *
370  * This function create a reserved subpicture in the spu heap.
371  * A null pointer is returned if the function fails. This method provides an
372  * already allocated zone of memory in the spu data fields. It needs locking
373  * since several pictures can be created by several producers threads.
374  * \param p_spu the subpicture unit in which to create the subpicture
375  * \return NULL on error, a reserved subpicture otherwise
376  */
377 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
378 {
379     int                 i_subpic;                        /* subpicture index */
380     subpicture_t *      p_subpic = NULL;            /* first free subpicture */
381
382     /* Get lock */
383     vlc_mutex_lock( &p_spu->subpicture_lock );
384
385     /*
386      * Look for an empty place
387      */
388     p_subpic = NULL;
389     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
390     {
391         if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
392         {
393             /* Subpicture is empty and ready for allocation */
394             p_subpic = &p_spu->p_subpicture[i_subpic];
395             p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
396             break;
397         }
398     }
399
400     /* If no free subpicture could be found */
401     if( p_subpic == NULL )
402     {
403         msg_Err( p_spu, "subpicture heap is full" );
404         vlc_mutex_unlock( &p_spu->subpicture_lock );
405         return NULL;
406     }
407
408     /* Copy subpicture information, set some default values */
409     memset( p_subpic, 0, sizeof(subpicture_t) );
410     p_subpic->i_status   = RESERVED_SUBPICTURE;
411     p_subpic->b_absolute = VLC_TRUE;
412     p_subpic->b_pausable = VLC_FALSE;
413     p_subpic->b_fade     = VLC_FALSE;
414     p_subpic->i_alpha    = 0xFF;
415     p_subpic->p_region   = 0;
416     p_subpic->pf_render  = 0;
417     p_subpic->pf_destroy = 0;
418     p_subpic->p_sys      = 0;
419     vlc_mutex_unlock( &p_spu->subpicture_lock );
420
421     p_subpic->pf_create_region = __spu_CreateRegion;
422     p_subpic->pf_make_region = __spu_MakeRegion;
423     p_subpic->pf_destroy_region = __spu_DestroyRegion;
424
425     return p_subpic;
426 }
427
428 /**
429  * Remove a subpicture from the heap
430  *
431  * This function frees a previously reserved subpicture.
432  * It is meant to be used when the construction of a picture aborted.
433  * This function does not need locking since reserved subpictures are ignored
434  * by the spu.
435  */
436 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
437 {
438     /* Get lock */
439     vlc_mutex_lock( &p_spu->subpicture_lock );
440
441     /* There can be race conditions so we need to check the status */
442     if( p_subpic->i_status == FREE_SUBPICTURE )
443     {
444         vlc_mutex_unlock( &p_spu->subpicture_lock );
445         return;
446     }
447
448     /* Check if status is valid */
449     if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
450            && ( p_subpic->i_status != READY_SUBPICTURE ) )
451     {
452         msg_Err( p_spu, "subpicture %p has invalid status %d",
453                          p_subpic, p_subpic->i_status );
454     }
455
456     while( p_subpic->p_region )
457     {
458         subpicture_region_t *p_region = p_subpic->p_region;
459         p_subpic->p_region = p_region->p_next;
460         spu_DestroyRegion( p_spu, p_region );
461     }
462
463     if( p_subpic->pf_destroy )
464     {
465         p_subpic->pf_destroy( p_subpic );
466     }
467
468     p_subpic->i_status = FREE_SUBPICTURE;
469
470     vlc_mutex_unlock( &p_spu->subpicture_lock );
471 }
472
473 /*****************************************************************************
474  * spu_RenderSubpictures: render a subpicture list
475  *****************************************************************************
476  * This function renders all sub picture units in the list.
477  *****************************************************************************/
478 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
479                             picture_t *p_pic_dst, picture_t *p_pic_src,
480                             subpicture_t *p_subpic,
481                             int i_scale_width_orig, int i_scale_height_orig )
482 {
483     /* Get lock */
484     vlc_mutex_lock( &p_spu->subpicture_lock );
485
486     /* Check i_status again to make sure spudec hasn't destroyed the subpic */
487     while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
488     {
489         subpicture_region_t *p_region = p_subpic->p_region;
490         int i_scale_width, i_scale_height;
491         int i_subpic_x = p_subpic->i_x;
492
493         /* Load the blending module */
494         if( !p_spu->p_blend && p_region )
495         {
496             p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
497             vlc_object_attach( p_spu->p_blend, p_spu );
498             p_spu->p_blend->fmt_out.video.i_x_offset =
499                 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
500             p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
501             p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
502             p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
503
504             p_spu->p_blend->p_module =
505                 module_Need( p_spu->p_blend, "video blending", 0, 0 );
506         }
507
508         /* Load the text rendering module */
509         if( !p_spu->p_text && p_region && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
510         {
511             char *psz_modulename = NULL;
512
513             p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
514             vlc_object_attach( p_spu->p_text, p_spu );
515
516             p_spu->p_text->fmt_out.video.i_width =
517                 p_spu->p_text->fmt_out.video.i_visible_width =
518                 p_fmt->i_width;
519             p_spu->p_text->fmt_out.video.i_height =
520                 p_spu->p_text->fmt_out.video.i_visible_height =
521                 p_fmt->i_height;
522
523             p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
524             p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
525
526             psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
527             if( psz_modulename && *psz_modulename )
528             {
529                 p_spu->p_text->p_module =
530                     module_Need( p_spu->p_text, "text renderer", psz_modulename, VLC_TRUE );
531             }
532             if( !p_spu->p_text->p_module )
533             {
534                 p_spu->p_text->p_module =
535                     module_Need( p_spu->p_text, "text renderer", 0, 0 );
536             }
537             if( psz_modulename ) free( psz_modulename );
538         }
539         if( p_spu->p_text )
540         {
541             if( p_subpic->i_original_picture_height > 0 &&
542                 p_subpic->i_original_picture_width  > 0 )
543             {
544                 p_spu->p_text->fmt_out.video.i_width =
545                     p_spu->p_text->fmt_out.video.i_visible_width =
546                     p_subpic->i_original_picture_width;
547                 p_spu->p_text->fmt_out.video.i_height =
548                     p_spu->p_text->fmt_out.video.i_visible_height =
549                     p_subpic->i_original_picture_height;
550             }
551             else
552             {
553                 p_spu->p_text->fmt_out.video.i_width =
554                     p_spu->p_text->fmt_out.video.i_visible_width =
555                     p_fmt->i_width;
556                 p_spu->p_text->fmt_out.video.i_height =
557                     p_spu->p_text->fmt_out.video.i_visible_height =
558                     p_fmt->i_height;
559             }
560         }
561
562         i_scale_width = i_scale_width_orig;
563         i_scale_height = i_scale_height_orig;
564
565         if( p_subpic->i_original_picture_height > 0 &&
566             p_subpic->i_original_picture_width  > 0 )
567         {
568             i_scale_width = i_scale_width * p_fmt->i_width /
569                              p_subpic->i_original_picture_width;
570             i_scale_height = i_scale_height * p_fmt->i_height /
571                              p_subpic->i_original_picture_height;
572         }
573         else if( p_subpic->i_original_picture_height > 0 )
574         {
575             i_scale_height = i_scale_height * p_fmt->i_height /
576                              p_subpic->i_original_picture_height;
577             i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height;
578         }
579
580         /* Set default subpicture aspect ratio */
581         if( p_region && p_region->fmt.i_aspect &&
582             (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
583         {
584             p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
585             p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
586         }
587         if( p_region &&
588             (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
589         {
590             p_region->fmt.i_sar_den = p_fmt->i_sar_den;
591             p_region->fmt.i_sar_num = p_fmt->i_sar_num;
592         }
593
594         /* Take care of the aspect ratio */
595         if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
596             p_region->fmt.i_sar_den * p_fmt->i_sar_num )
597         {
598             i_scale_width = i_scale_width *
599                 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
600                 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
601             i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
602         }
603
604         /* Load the scaling module */
605         if( !p_spu->p_scale && (i_scale_width != 1000 ||
606             i_scale_height != 1000) )
607         {
608             p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
609             vlc_object_attach( p_spu->p_scale, p_spu );
610             p_spu->p_scale->fmt_out.video.i_chroma =
611                 p_spu->p_scale->fmt_in.video.i_chroma =
612                     VLC_FOURCC('Y','U','V','P');
613             p_spu->p_scale->fmt_in.video.i_width =
614                 p_spu->p_scale->fmt_in.video.i_height = 32;
615             p_spu->p_scale->fmt_out.video.i_width =
616                 p_spu->p_scale->fmt_out.video.i_height = 16;
617
618             p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
619             p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
620             p_spu->p_scale->p_module =
621                 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
622         }
623
624         while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
625         {
626             int i_fade_alpha = 255;
627             int i_x_offset = p_region->i_x + i_subpic_x;
628             int i_y_offset = p_region->i_y + p_subpic->i_y;
629
630             if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
631             {
632                 if( p_spu->p_text && p_spu->p_text->p_module )
633                 {
634                     p_region->i_align = p_subpic->i_flags;
635
636                     if( p_spu->p_text->pf_render_html && p_region->psz_html )
637                     {
638                         p_spu->p_text->pf_render_html( p_spu->p_text,
639                                                        p_region, p_region );
640                     }
641                     else if( p_spu->p_text->pf_render_text )
642                     {
643                         p_spu->p_text->pf_render_text( p_spu->p_text,
644                                                        p_region, p_region );
645                     }
646                 }
647             }
648
649             /* Force palette if requested */
650             if( p_spu->b_force_palette && VLC_FOURCC('Y','U','V','P') ==
651                 p_region->fmt.i_chroma )
652             {
653                 memcpy( p_region->fmt.p_palette->palette,
654                         p_spu->palette, 16 );
655             }
656
657             /* Scale SPU if necessary */
658             if( p_region->p_cache )
659             {
660                 if( i_scale_width * p_region->fmt.i_width / 1000 !=
661                     p_region->p_cache->fmt.i_width ||
662                     i_scale_height * p_region->fmt.i_height / 1000 !=
663                     p_region->p_cache->fmt.i_height )
664                 {
665                     p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
666                                                  p_region->p_cache );
667                     p_region->p_cache = 0;
668                 }
669             }
670
671             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
672                 p_spu->p_scale && !p_region->p_cache )
673             {
674                 picture_t *p_pic;
675
676                 p_spu->p_scale->fmt_in.video = p_region->fmt;
677                 p_spu->p_scale->fmt_out.video = p_region->fmt;
678
679                 p_region->p_cache =
680                     p_subpic->pf_create_region( VLC_OBJECT(p_spu),
681                         &p_spu->p_scale->fmt_out.video );
682                 if( p_spu->p_scale->fmt_out.video.p_palette )
683                     *p_spu->p_scale->fmt_out.video.p_palette =
684                         *p_region->fmt.p_palette;
685                 p_region->p_cache->p_next = p_region->p_next;
686
687                 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
688                                   &p_region->picture );
689
690                 p_spu->p_scale->fmt_out.video.i_width =
691                     p_region->fmt.i_width * i_scale_width / 1000;
692                 p_spu->p_scale->fmt_out.video.i_visible_width =
693                     p_region->fmt.i_visible_width * i_scale_width / 1000;
694                 p_spu->p_scale->fmt_out.video.i_height =
695                     p_region->fmt.i_height * i_scale_height / 1000;
696                 p_spu->p_scale->fmt_out.video.i_visible_height =
697                     p_region->fmt.i_visible_height * i_scale_height / 1000;
698                 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
699                 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
700                 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
701
702                 p_pic = p_spu->p_scale->pf_video_filter(
703                                  p_spu->p_scale, &p_region->p_cache->picture );
704                 if( p_pic )
705                 {
706                     picture_t p_pic_tmp = p_region->p_cache->picture;
707                     p_region->p_cache->picture = *p_pic;
708                     *p_pic = p_pic_tmp;
709                     free( p_pic );
710                 }
711             }
712             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
713                 p_spu->p_scale && p_region->p_cache )
714             {
715                 p_region = p_region->p_cache;
716             }
717
718             if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
719             {
720                 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
721                     p_subpic->i_y;
722             }
723             else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
724             {
725                 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
726             }
727
728             if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
729             {
730                 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
731                     i_subpic_x;
732             }
733             else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
734             {
735                 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
736             }
737
738             if( p_subpic->b_absolute )
739             {
740                 i_x_offset = p_region->i_x +
741                     i_subpic_x * i_scale_width / 1000;
742                 i_y_offset = p_region->i_y +
743                     p_subpic->i_y * i_scale_height / 1000;
744
745             }
746
747             i_x_offset = __MAX( i_x_offset, 0 );
748             i_y_offset = __MAX( i_y_offset, 0 );
749
750             if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
751             {
752                 int i_diff = 0;
753                 int i_low = i_y_offset - p_spu->i_margin;
754                 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
755
756                 /* crop extra margin to keep within bounds */
757                 if( i_low < 0 ) i_diff = i_low;
758                 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
759                 i_y_offset -= ( p_spu->i_margin + i_diff );
760             }
761
762             p_spu->p_blend->fmt_in.video = p_region->fmt;
763
764             /* Force cropping if requested */
765             if( p_spu->b_force_crop )
766             {
767                 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
768                 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
769                 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
770                 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
771                 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
772
773                 /* Find the intersection */
774                 if( i_crop_x + i_crop_width <= i_x_offset ||
775                     i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
776                     i_crop_y + i_crop_height <= i_y_offset ||
777                     i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
778                 {
779                     /* No intersection */
780                     p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
781                 }
782                 else
783                 {
784                     int i_x, i_y, i_x_end, i_y_end;
785                     i_x = __MAX( i_crop_x, i_x_offset );
786                     i_y = __MAX( i_crop_y, i_y_offset );
787                     i_x_end = __MIN( i_crop_x + i_crop_width,
788                                    i_x_offset + (int)p_fmt->i_visible_width );
789                     i_y_end = __MIN( i_crop_y + i_crop_height,
790                                    i_y_offset + (int)p_fmt->i_visible_height );
791
792                     p_fmt->i_x_offset = i_x - i_x_offset;
793                     p_fmt->i_y_offset = i_y - i_y_offset;
794                     p_fmt->i_visible_width = i_x_end - i_x;
795                     p_fmt->i_visible_height = i_y_end - i_y;
796
797                     i_x_offset = i_x;
798                     i_y_offset = i_y;
799                 }
800             }
801
802             /* Update the output picture size */
803             p_spu->p_blend->fmt_out.video.i_width =
804                 p_spu->p_blend->fmt_out.video.i_visible_width =
805                     p_fmt->i_width;
806             p_spu->p_blend->fmt_out.video.i_height =
807                 p_spu->p_blend->fmt_out.video.i_visible_height =
808                     p_fmt->i_height;
809
810             if( p_subpic->b_fade )
811             {
812                 mtime_t i_fade_start = ( p_subpic->i_stop +
813                                          p_subpic->i_start ) / 2;
814                 mtime_t i_now = mdate();
815                 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
816                 {
817                     i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
818                                    ( p_subpic->i_stop - i_fade_start );
819                 }
820             }
821
822             i_x_offset = __MAX( i_x_offset, 0 );
823             i_y_offset = __MAX( i_y_offset, 0 );
824
825             p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
826                 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
827                 i_fade_alpha * p_subpic->i_alpha / 255 );
828
829             p_region = p_region->p_next;
830         }
831
832         p_subpic = p_subpic->p_next;
833     }
834
835     vlc_mutex_unlock( &p_spu->subpicture_lock );
836 }
837
838 /*****************************************************************************
839  * spu_SortSubpictures: find the subpictures to display
840  *****************************************************************************
841  * This function parses all subpictures and decides which ones need to be
842  * displayed. This operation does not need lock, since only READY_SUBPICTURE
843  * are handled. If no picture has been selected, display_date will depend on
844  * the subpicture.
845  * We also check for ephemer DVD subpictures (subpictures that have
846  * to be removed if a newer one is available), which makes it a lot
847  * more difficult to guess if a subpicture has to be rendered or not.
848  *****************************************************************************/
849 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
850                                    vlc_bool_t b_paused )
851 {
852     int i_index, i_channel;
853     subpicture_t *p_subpic = NULL;
854     subpicture_t *p_ephemer;
855     mtime_t      ephemer_date;
856
857     /* Run subpicture filters */
858     for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
859     {
860         subpicture_t *p_subpic_filter;
861         p_subpic_filter = p_spu->pp_filter[i_index]->
862             pf_sub_filter( p_spu->pp_filter[i_index], display_date );
863         if( p_subpic_filter )
864         {
865             spu_DisplaySubpicture( p_spu, p_subpic_filter );
866         }
867     }
868
869     /* We get an easily parsable chained list of subpictures which
870      * ends with NULL since p_subpic was initialized to NULL. */
871     for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
872     {
873         p_ephemer = 0;
874         ephemer_date = 0;
875
876         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
877         {
878             if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
879                 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
880             {
881                 continue;
882             }
883             if( display_date &&
884                 display_date < p_spu->p_subpicture[i_index].i_start )
885             {
886                 /* Too early, come back next monday */
887                 continue;
888             }
889
890             if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
891                 ephemer_date = p_spu->p_subpicture[i_index].i_start;
892
893             if( display_date > p_spu->p_subpicture[i_index].i_stop &&
894                 ( !p_spu->p_subpicture[i_index].b_ephemer ||
895                   p_spu->p_subpicture[i_index].i_stop >
896                   p_spu->p_subpicture[i_index].i_start ) &&
897                 !( p_spu->p_subpicture[i_index].b_pausable &&
898                    b_paused ) )
899             {
900                 /* Too late, destroy the subpic */
901                 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
902                 continue;
903             }
904
905             /* If this is an ephemer subpic, add it to our list */
906             if( p_spu->p_subpicture[i_index].b_ephemer )
907             {
908                 p_spu->p_subpicture[i_index].p_next = p_ephemer;
909                 p_ephemer = &p_spu->p_subpicture[i_index];
910
911                 continue;
912             }
913
914             p_spu->p_subpicture[i_index].p_next = p_subpic;
915             p_subpic = &p_spu->p_subpicture[i_index];
916         }
917
918         /* If we found ephemer subpictures, check if they have to be
919          * displayed or destroyed */
920         while( p_ephemer != NULL )
921         {
922             subpicture_t *p_tmp = p_ephemer;
923             p_ephemer = p_ephemer->p_next;
924
925             if( p_tmp->i_start < ephemer_date )
926             {
927                 /* Ephemer subpicture has lived too long */
928                 spu_DestroySubpicture( p_spu, p_tmp );
929             }
930             else
931             {
932                 /* Ephemer subpicture can still live a bit */
933                 p_tmp->p_next = p_subpic;
934                 p_subpic = p_tmp;
935             }
936         }
937     }
938
939     return p_subpic;
940 }
941
942 /*****************************************************************************
943  * SpuClearChannel: clear an spu channel
944  *****************************************************************************
945  * This function destroys the subpictures which belong to the spu channel
946  * corresponding to i_channel_id.
947  *****************************************************************************/
948 static void SpuClearChannel( spu_t *p_spu, int i_channel )
949 {
950     int          i_subpic;                               /* subpicture index */
951     subpicture_t *p_subpic = NULL;                  /* first free subpicture */
952
953     vlc_mutex_lock( &p_spu->subpicture_lock );
954
955     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
956     {
957         p_subpic = &p_spu->p_subpicture[i_subpic];
958         if( p_subpic->i_status == FREE_SUBPICTURE
959             || ( p_subpic->i_status != RESERVED_SUBPICTURE
960                  && p_subpic->i_status != READY_SUBPICTURE ) )
961         {
962             continue;
963         }
964
965         if( p_subpic->i_channel == i_channel )
966         {
967             while( p_subpic->p_region )
968             {
969                 subpicture_region_t *p_region = p_subpic->p_region;
970                 p_subpic->p_region = p_region->p_next;
971                 spu_DestroyRegion( p_spu, p_region );
972             }
973
974             if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
975             p_subpic->i_status = FREE_SUBPICTURE;
976         }
977     }
978
979     vlc_mutex_unlock( &p_spu->subpicture_lock );
980 }
981
982 /*****************************************************************************
983  * spu_ControlDefault: default methods for the subpicture unit control.
984  *****************************************************************************/
985 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
986 {
987     int *pi, i;
988
989     switch( i_query )
990     {
991     case SPU_CHANNEL_REGISTER:
992         pi = (int *)va_arg( args, int * );
993         if( pi ) *pi = p_spu->i_channel++;
994         break;
995
996     case SPU_CHANNEL_CLEAR:
997         i = (int)va_arg( args, int );
998         SpuClearChannel( p_spu, i );
999         break;
1000
1001     default:
1002         msg_Dbg( p_spu, "control query not supported" );
1003         return VLC_EGENERIC;
1004     }
1005
1006     return VLC_SUCCESS;
1007 }
1008
1009 /*****************************************************************************
1010  * Object variables callbacks
1011  *****************************************************************************/
1012
1013 /*****************************************************************************
1014  * UpdateSPU: update subpicture settings
1015  *****************************************************************************
1016  * This function is called from CropCallback and at initialization time, to
1017  * retrieve crop information from the input.
1018  *****************************************************************************/
1019 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1020 {
1021     vlc_value_t val;
1022
1023     p_spu->b_force_palette = VLC_FALSE;
1024     p_spu->b_force_crop = VLC_FALSE;
1025
1026     if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1027
1028     p_spu->b_force_crop = VLC_TRUE;
1029     var_Get( p_object, "x-start", &val );
1030     p_spu->i_crop_x = val.i_int;
1031     var_Get( p_object, "y-start", &val );
1032     p_spu->i_crop_y = val.i_int;
1033     var_Get( p_object, "x-end", &val );
1034     p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1035     var_Get( p_object, "y-end", &val );
1036     p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1037
1038     if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1039     {
1040         memcpy( p_spu->palette, val.p_address, 16 );
1041         p_spu->b_force_palette = VLC_TRUE;
1042     }
1043
1044     msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1045              p_spu->i_crop_x, p_spu->i_crop_y,
1046              p_spu->i_crop_width, p_spu->i_crop_height,
1047              p_spu->b_force_palette );
1048 }
1049
1050 /*****************************************************************************
1051  * CropCallback: called when the highlight properties are changed
1052  *****************************************************************************
1053  * This callback is called from the input thread when we need cropping
1054  *****************************************************************************/
1055 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1056                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1057 {
1058     UpdateSPU( (spu_t *)p_data, p_object );
1059     return VLC_SUCCESS;
1060 }
1061
1062 /*****************************************************************************
1063  * Buffers allocation callbacks for the filters
1064  *****************************************************************************/
1065 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1066 {
1067     filter_owner_sys_t *p_sys = p_filter->p_owner;
1068     subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1069     if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1070     return p_subpicture;
1071 }
1072
1073 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1074 {
1075     filter_owner_sys_t *p_sys = p_filter->p_owner;
1076     spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1077 }
1078
1079 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1080 {
1081     subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1082     memset( p_subpic, 0, sizeof(subpicture_t) );
1083     p_subpic->b_absolute = VLC_TRUE;
1084
1085     p_subpic->pf_create_region = __spu_CreateRegion;
1086     p_subpic->pf_make_region = __spu_MakeRegion;
1087     p_subpic->pf_destroy_region = __spu_DestroyRegion;
1088
1089     return p_subpic;
1090 }
1091
1092 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1093 {
1094     while( p_subpic->p_region )
1095     {
1096         subpicture_region_t *p_region = p_subpic->p_region;
1097         p_subpic->p_region = p_region->p_next;
1098         p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1099     }
1100
1101     free( p_subpic );
1102 }
1103
1104 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1105 {
1106     picture_t *p_picture = malloc( sizeof(picture_t) );
1107
1108     if( vout_AllocatePicture( p_filter, p_picture,
1109                               p_filter->fmt_out.video.i_chroma,
1110                               p_filter->fmt_out.video.i_width,
1111                               p_filter->fmt_out.video.i_height,
1112                               p_filter->fmt_out.video.i_aspect )
1113         != VLC_SUCCESS )
1114     {
1115         free( p_picture );
1116         return NULL;
1117     }
1118
1119     p_picture->pf_release = RegionPictureRelease;
1120
1121     return p_picture;
1122 }
1123
1124 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1125 {
1126     if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1127     if( p_pic ) free( p_pic );
1128 }