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