vout_subpictures.c: Do not call the scaling module if the subpicture is using RGBA...
[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             /* XXX: We'll also be using it for YUVA and RGBA blending ... */
506
507             p_spu->p_blend->p_module =
508                 module_Need( p_spu->p_blend, "video blending", 0, 0 );
509         }
510
511         /* Load the text rendering module */
512         if( !p_spu->p_text && p_region && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
513         {
514             char *psz_modulename = NULL;
515
516             p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
517             vlc_object_attach( p_spu->p_text, p_spu );
518
519             p_spu->p_text->fmt_out.video.i_width =
520                 p_spu->p_text->fmt_out.video.i_visible_width =
521                 p_fmt->i_width;
522             p_spu->p_text->fmt_out.video.i_height =
523                 p_spu->p_text->fmt_out.video.i_visible_height =
524                 p_fmt->i_height;
525
526             p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
527             p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
528
529             psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
530             if( psz_modulename && *psz_modulename )
531             {
532                 p_spu->p_text->p_module =
533                     module_Need( p_spu->p_text, "text renderer", psz_modulename, VLC_TRUE );
534             }
535             if( !p_spu->p_text->p_module )
536             {
537                 p_spu->p_text->p_module =
538                     module_Need( p_spu->p_text, "text renderer", 0, 0 );
539             }
540             if( psz_modulename ) free( psz_modulename );
541         }
542         if( p_spu->p_text )
543         {
544             if( p_subpic->i_original_picture_height > 0 &&
545                 p_subpic->i_original_picture_width  > 0 )
546             {
547                 p_spu->p_text->fmt_out.video.i_width =
548                     p_spu->p_text->fmt_out.video.i_visible_width =
549                     p_subpic->i_original_picture_width;
550                 p_spu->p_text->fmt_out.video.i_height =
551                     p_spu->p_text->fmt_out.video.i_visible_height =
552                     p_subpic->i_original_picture_height;
553             }
554             else
555             {
556                 p_spu->p_text->fmt_out.video.i_width =
557                     p_spu->p_text->fmt_out.video.i_visible_width =
558                     p_fmt->i_width;
559                 p_spu->p_text->fmt_out.video.i_height =
560                     p_spu->p_text->fmt_out.video.i_visible_height =
561                     p_fmt->i_height;
562             }
563         }
564
565         i_scale_width = i_scale_width_orig;
566         i_scale_height = i_scale_height_orig;
567
568         if( p_subpic->i_original_picture_height > 0 &&
569             p_subpic->i_original_picture_width  > 0 )
570         {
571             i_scale_width = i_scale_width * p_fmt->i_width /
572                              p_subpic->i_original_picture_width;
573             i_scale_height = i_scale_height * p_fmt->i_height /
574                              p_subpic->i_original_picture_height;
575         }
576         else if( p_subpic->i_original_picture_height > 0 )
577         {
578             i_scale_height = i_scale_height * p_fmt->i_height /
579                              p_subpic->i_original_picture_height;
580             i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height;
581         }
582
583         /* Set default subpicture aspect ratio */
584         if( p_region && p_region->fmt.i_aspect &&
585             (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
586         {
587             p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
588             p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
589         }
590         if( p_region &&
591             (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
592         {
593             p_region->fmt.i_sar_den = p_fmt->i_sar_den;
594             p_region->fmt.i_sar_num = p_fmt->i_sar_num;
595         }
596
597         /* Take care of the aspect ratio */
598         if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
599             p_region->fmt.i_sar_den * p_fmt->i_sar_num )
600         {
601             i_scale_width = i_scale_width *
602                 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
603                 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
604             i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
605         }
606
607         /* Load the scaling module */
608         if( !p_spu->p_scale && (i_scale_width != 1000 ||
609             i_scale_height != 1000) )
610         {
611             p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
612             vlc_object_attach( p_spu->p_scale, p_spu );
613             p_spu->p_scale->fmt_out.video.i_chroma =
614                 p_spu->p_scale->fmt_in.video.i_chroma =
615                     VLC_FOURCC('Y','U','V','P');
616             /* XXX: We'll also be using it for YUVA and RGBA blending ... */
617             p_spu->p_scale->fmt_in.video.i_width =
618                 p_spu->p_scale->fmt_in.video.i_height = 32;
619             p_spu->p_scale->fmt_out.video.i_width =
620                 p_spu->p_scale->fmt_out.video.i_height = 16;
621
622             p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
623             p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
624             p_spu->p_scale->p_module =
625                 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
626         }
627
628         while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
629         {
630             int i_fade_alpha = 255;
631             int i_x_offset = p_region->i_x + i_subpic_x;
632             int i_y_offset = p_region->i_y + p_subpic->i_y;
633
634             if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
635             {
636                 if( p_spu->p_text && p_spu->p_text->p_module )
637                 {
638                     p_region->i_align = p_subpic->i_flags;
639
640                     if( p_spu->p_text->pf_render_html && p_region->psz_html )
641                     {
642                         p_spu->p_text->pf_render_html( p_spu->p_text,
643                                                        p_region, p_region );
644                     }
645                     else if( p_spu->p_text->pf_render_text )
646                     {
647                         p_spu->p_text->pf_render_text( p_spu->p_text,
648                                                        p_region, p_region );
649                     }
650                 }
651             }
652
653             /* Force palette if requested */
654             if( p_spu->b_force_palette && VLC_FOURCC('Y','U','V','P') ==
655                 p_region->fmt.i_chroma )
656             {
657                 memcpy( p_region->fmt.p_palette->palette,
658                         p_spu->palette, 16 );
659             }
660
661             /* Scale SPU if necessary */
662             if( p_region->p_cache )
663             {
664                 if( i_scale_width * p_region->fmt.i_width / 1000 !=
665                     p_region->p_cache->fmt.i_width ||
666                     i_scale_height * p_region->fmt.i_height / 1000 !=
667                     p_region->p_cache->fmt.i_height )
668                 {
669                     p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
670                                                  p_region->p_cache );
671                     p_region->p_cache = 0;
672                 }
673             }
674
675             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
676                 p_spu->p_scale && !p_region->p_cache &&
677                 VLC_FOURCC('R','G','B','A') != p_region->fmt.i_chroma /* FIXME */ )
678             {
679                 picture_t *p_pic;
680
681                 p_spu->p_scale->fmt_in.video = p_region->fmt;
682                 p_spu->p_scale->fmt_out.video = p_region->fmt;
683
684                 p_region->p_cache =
685                     p_subpic->pf_create_region( VLC_OBJECT(p_spu),
686                         &p_spu->p_scale->fmt_out.video );
687                 if( p_spu->p_scale->fmt_out.video.p_palette )
688                     *p_spu->p_scale->fmt_out.video.p_palette =
689                         *p_region->fmt.p_palette;
690                 p_region->p_cache->p_next = p_region->p_next;
691
692                 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
693                                   &p_region->picture );
694
695                 p_spu->p_scale->fmt_out.video.i_width =
696                     p_region->fmt.i_width * i_scale_width / 1000;
697                 p_spu->p_scale->fmt_out.video.i_visible_width =
698                     p_region->fmt.i_visible_width * i_scale_width / 1000;
699                 p_spu->p_scale->fmt_out.video.i_height =
700                     p_region->fmt.i_height * i_scale_height / 1000;
701                 p_spu->p_scale->fmt_out.video.i_visible_height =
702                     p_region->fmt.i_visible_height * i_scale_height / 1000;
703                 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
704                 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
705                 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
706
707                 p_pic = p_spu->p_scale->pf_video_filter(
708                                  p_spu->p_scale, &p_region->p_cache->picture );
709                 if( p_pic )
710                 {
711                     picture_t p_pic_tmp = p_region->p_cache->picture;
712                     p_region->p_cache->picture = *p_pic;
713                     *p_pic = p_pic_tmp;
714                     free( p_pic );
715                 }
716             }
717             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
718                 p_spu->p_scale && p_region->p_cache )
719             {
720                 p_region = p_region->p_cache;
721             }
722
723             if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
724             {
725                 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
726                     p_subpic->i_y;
727             }
728             else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
729             {
730                 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
731             }
732
733             if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
734             {
735                 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
736                     i_subpic_x;
737             }
738             else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
739             {
740                 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
741             }
742
743             if( p_subpic->b_absolute )
744             {
745                 i_x_offset = p_region->i_x +
746                     i_subpic_x * i_scale_width / 1000;
747                 i_y_offset = p_region->i_y +
748                     p_subpic->i_y * i_scale_height / 1000;
749
750             }
751
752             i_x_offset = __MAX( i_x_offset, 0 );
753             i_y_offset = __MAX( i_y_offset, 0 );
754
755             if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
756             {
757                 int i_diff = 0;
758                 int i_low = i_y_offset - p_spu->i_margin;
759                 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
760
761                 /* crop extra margin to keep within bounds */
762                 if( i_low < 0 ) i_diff = i_low;
763                 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
764                 i_y_offset -= ( p_spu->i_margin + i_diff );
765             }
766
767             p_spu->p_blend->fmt_in.video = p_region->fmt;
768
769             /* Force cropping if requested */
770             if( p_spu->b_force_crop )
771             {
772                 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
773                 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
774                 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
775                 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
776                 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
777
778                 /* Find the intersection */
779                 if( i_crop_x + i_crop_width <= i_x_offset ||
780                     i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
781                     i_crop_y + i_crop_height <= i_y_offset ||
782                     i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
783                 {
784                     /* No intersection */
785                     p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
786                 }
787                 else
788                 {
789                     int i_x, i_y, i_x_end, i_y_end;
790                     i_x = __MAX( i_crop_x, i_x_offset );
791                     i_y = __MAX( i_crop_y, i_y_offset );
792                     i_x_end = __MIN( i_crop_x + i_crop_width,
793                                    i_x_offset + (int)p_fmt->i_visible_width );
794                     i_y_end = __MIN( i_crop_y + i_crop_height,
795                                    i_y_offset + (int)p_fmt->i_visible_height );
796
797                     p_fmt->i_x_offset = i_x - i_x_offset;
798                     p_fmt->i_y_offset = i_y - i_y_offset;
799                     p_fmt->i_visible_width = i_x_end - i_x;
800                     p_fmt->i_visible_height = i_y_end - i_y;
801
802                     i_x_offset = i_x;
803                     i_y_offset = i_y;
804                 }
805             }
806
807             /* Update the output picture size */
808             p_spu->p_blend->fmt_out.video.i_width =
809                 p_spu->p_blend->fmt_out.video.i_visible_width =
810                     p_fmt->i_width;
811             p_spu->p_blend->fmt_out.video.i_height =
812                 p_spu->p_blend->fmt_out.video.i_visible_height =
813                     p_fmt->i_height;
814
815             if( p_subpic->b_fade )
816             {
817                 mtime_t i_fade_start = ( p_subpic->i_stop +
818                                          p_subpic->i_start ) / 2;
819                 mtime_t i_now = mdate();
820                 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
821                 {
822                     i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
823                                    ( p_subpic->i_stop - i_fade_start );
824                 }
825             }
826
827             i_x_offset = __MAX( i_x_offset, 0 );
828             i_y_offset = __MAX( i_y_offset, 0 );
829
830             p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
831                 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
832                 i_fade_alpha * p_subpic->i_alpha / 255 );
833
834             p_region = p_region->p_next;
835         }
836
837         p_subpic = p_subpic->p_next;
838     }
839
840     vlc_mutex_unlock( &p_spu->subpicture_lock );
841 }
842
843 /*****************************************************************************
844  * spu_SortSubpictures: find the subpictures to display
845  *****************************************************************************
846  * This function parses all subpictures and decides which ones need to be
847  * displayed. This operation does not need lock, since only READY_SUBPICTURE
848  * are handled. If no picture has been selected, display_date will depend on
849  * the subpicture.
850  * We also check for ephemer DVD subpictures (subpictures that have
851  * to be removed if a newer one is available), which makes it a lot
852  * more difficult to guess if a subpicture has to be rendered or not.
853  *****************************************************************************/
854 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
855                                    vlc_bool_t b_paused )
856 {
857     int i_index, i_channel;
858     subpicture_t *p_subpic = NULL;
859     subpicture_t *p_ephemer;
860     mtime_t      ephemer_date;
861
862     /* Run subpicture filters */
863     for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
864     {
865         subpicture_t *p_subpic_filter;
866         p_subpic_filter = p_spu->pp_filter[i_index]->
867             pf_sub_filter( p_spu->pp_filter[i_index], display_date );
868         if( p_subpic_filter )
869         {
870             spu_DisplaySubpicture( p_spu, p_subpic_filter );
871         }
872     }
873
874     /* We get an easily parsable chained list of subpictures which
875      * ends with NULL since p_subpic was initialized to NULL. */
876     for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
877     {
878         p_ephemer = 0;
879         ephemer_date = 0;
880
881         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
882         {
883             if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
884                 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
885             {
886                 continue;
887             }
888             if( display_date &&
889                 display_date < p_spu->p_subpicture[i_index].i_start )
890             {
891                 /* Too early, come back next monday */
892                 continue;
893             }
894
895             if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
896                 ephemer_date = p_spu->p_subpicture[i_index].i_start;
897
898             if( display_date > p_spu->p_subpicture[i_index].i_stop &&
899                 ( !p_spu->p_subpicture[i_index].b_ephemer ||
900                   p_spu->p_subpicture[i_index].i_stop >
901                   p_spu->p_subpicture[i_index].i_start ) &&
902                 !( p_spu->p_subpicture[i_index].b_pausable &&
903                    b_paused ) )
904             {
905                 /* Too late, destroy the subpic */
906                 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
907                 continue;
908             }
909
910             /* If this is an ephemer subpic, add it to our list */
911             if( p_spu->p_subpicture[i_index].b_ephemer )
912             {
913                 p_spu->p_subpicture[i_index].p_next = p_ephemer;
914                 p_ephemer = &p_spu->p_subpicture[i_index];
915
916                 continue;
917             }
918
919             p_spu->p_subpicture[i_index].p_next = p_subpic;
920             p_subpic = &p_spu->p_subpicture[i_index];
921         }
922
923         /* If we found ephemer subpictures, check if they have to be
924          * displayed or destroyed */
925         while( p_ephemer != NULL )
926         {
927             subpicture_t *p_tmp = p_ephemer;
928             p_ephemer = p_ephemer->p_next;
929
930             if( p_tmp->i_start < ephemer_date )
931             {
932                 /* Ephemer subpicture has lived too long */
933                 spu_DestroySubpicture( p_spu, p_tmp );
934             }
935             else
936             {
937                 /* Ephemer subpicture can still live a bit */
938                 p_tmp->p_next = p_subpic;
939                 p_subpic = p_tmp;
940             }
941         }
942     }
943
944     return p_subpic;
945 }
946
947 /*****************************************************************************
948  * SpuClearChannel: clear an spu channel
949  *****************************************************************************
950  * This function destroys the subpictures which belong to the spu channel
951  * corresponding to i_channel_id.
952  *****************************************************************************/
953 static void SpuClearChannel( spu_t *p_spu, int i_channel )
954 {
955     int          i_subpic;                               /* subpicture index */
956     subpicture_t *p_subpic = NULL;                  /* first free subpicture */
957
958     vlc_mutex_lock( &p_spu->subpicture_lock );
959
960     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
961     {
962         p_subpic = &p_spu->p_subpicture[i_subpic];
963         if( p_subpic->i_status == FREE_SUBPICTURE
964             || ( p_subpic->i_status != RESERVED_SUBPICTURE
965                  && p_subpic->i_status != READY_SUBPICTURE ) )
966         {
967             continue;
968         }
969
970         if( p_subpic->i_channel == i_channel )
971         {
972             while( p_subpic->p_region )
973             {
974                 subpicture_region_t *p_region = p_subpic->p_region;
975                 p_subpic->p_region = p_region->p_next;
976                 spu_DestroyRegion( p_spu, p_region );
977             }
978
979             if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
980             p_subpic->i_status = FREE_SUBPICTURE;
981         }
982     }
983
984     vlc_mutex_unlock( &p_spu->subpicture_lock );
985 }
986
987 /*****************************************************************************
988  * spu_ControlDefault: default methods for the subpicture unit control.
989  *****************************************************************************/
990 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
991 {
992     int *pi, i;
993
994     switch( i_query )
995     {
996     case SPU_CHANNEL_REGISTER:
997         pi = (int *)va_arg( args, int * );
998         if( pi ) *pi = p_spu->i_channel++;
999         break;
1000
1001     case SPU_CHANNEL_CLEAR:
1002         i = (int)va_arg( args, int );
1003         SpuClearChannel( p_spu, i );
1004         break;
1005
1006     default:
1007         msg_Dbg( p_spu, "control query not supported" );
1008         return VLC_EGENERIC;
1009     }
1010
1011     return VLC_SUCCESS;
1012 }
1013
1014 /*****************************************************************************
1015  * Object variables callbacks
1016  *****************************************************************************/
1017
1018 /*****************************************************************************
1019  * UpdateSPU: update subpicture settings
1020  *****************************************************************************
1021  * This function is called from CropCallback and at initialization time, to
1022  * retrieve crop information from the input.
1023  *****************************************************************************/
1024 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1025 {
1026     vlc_value_t val;
1027
1028     p_spu->b_force_palette = VLC_FALSE;
1029     p_spu->b_force_crop = VLC_FALSE;
1030
1031     if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1032
1033     p_spu->b_force_crop = VLC_TRUE;
1034     var_Get( p_object, "x-start", &val );
1035     p_spu->i_crop_x = val.i_int;
1036     var_Get( p_object, "y-start", &val );
1037     p_spu->i_crop_y = val.i_int;
1038     var_Get( p_object, "x-end", &val );
1039     p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1040     var_Get( p_object, "y-end", &val );
1041     p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1042
1043     if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1044     {
1045         memcpy( p_spu->palette, val.p_address, 16 );
1046         p_spu->b_force_palette = VLC_TRUE;
1047     }
1048
1049     msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1050              p_spu->i_crop_x, p_spu->i_crop_y,
1051              p_spu->i_crop_width, p_spu->i_crop_height,
1052              p_spu->b_force_palette );
1053 }
1054
1055 /*****************************************************************************
1056  * CropCallback: called when the highlight properties are changed
1057  *****************************************************************************
1058  * This callback is called from the input thread when we need cropping
1059  *****************************************************************************/
1060 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1061                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1062 {
1063     UpdateSPU( (spu_t *)p_data, p_object );
1064     return VLC_SUCCESS;
1065 }
1066
1067 /*****************************************************************************
1068  * Buffers allocation callbacks for the filters
1069  *****************************************************************************/
1070 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1071 {
1072     filter_owner_sys_t *p_sys = p_filter->p_owner;
1073     subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1074     if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1075     return p_subpicture;
1076 }
1077
1078 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1079 {
1080     filter_owner_sys_t *p_sys = p_filter->p_owner;
1081     spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1082 }
1083
1084 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1085 {
1086     subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1087     memset( p_subpic, 0, sizeof(subpicture_t) );
1088     p_subpic->b_absolute = VLC_TRUE;
1089
1090     p_subpic->pf_create_region = __spu_CreateRegion;
1091     p_subpic->pf_make_region = __spu_MakeRegion;
1092     p_subpic->pf_destroy_region = __spu_DestroyRegion;
1093
1094     return p_subpic;
1095 }
1096
1097 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1098 {
1099     while( p_subpic->p_region )
1100     {
1101         subpicture_region_t *p_region = p_subpic->p_region;
1102         p_subpic->p_region = p_region->p_next;
1103         p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1104     }
1105
1106     free( p_subpic );
1107 }
1108
1109 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1110 {
1111     picture_t *p_picture = malloc( sizeof(picture_t) );
1112
1113     if( vout_AllocatePicture( p_filter, p_picture,
1114                               p_filter->fmt_out.video.i_chroma,
1115                               p_filter->fmt_out.video.i_width,
1116                               p_filter->fmt_out.video.i_height,
1117                               p_filter->fmt_out.video.i_aspect )
1118         != VLC_SUCCESS )
1119     {
1120         free( p_picture );
1121         return NULL;
1122     }
1123
1124     p_picture->pf_release = RegionPictureRelease;
1125
1126     return p_picture;
1127 }
1128
1129 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1130 {
1131     if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1132     if( p_pic ) free( p_pic );
1133 }