]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
scale.c: implement RGBA scaling.
[vlc] / src / video_output / vout_subpictures.c
1 /*****************************************************************************
2  * vout_subpictures.c : subpicture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                                /* free() */
30 #include <stdio.h>                                              /* sprintf() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <vlc/vlc.h>
34 #include <vlc_vout.h>
35 #include <vlc_block.h>
36 #include <vlc_filter.h>
37 #include <vlc_osd.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static void UpdateSPU   ( spu_t *, vlc_object_t * );
43 static int  CropCallback( vlc_object_t *, char const *,
44                           vlc_value_t, vlc_value_t, void * );
45
46 static int spu_vaControlDefault( spu_t *, int, va_list );
47
48 static subpicture_t *sub_new_buffer( filter_t * );
49 static void sub_del_buffer( filter_t *, subpicture_t * );
50 static subpicture_t *spu_new_buffer( filter_t * );
51 static void spu_del_buffer( filter_t *, subpicture_t * );
52 static picture_t *spu_new_video_buffer( filter_t * );
53 static void spu_del_video_buffer( filter_t *, picture_t * );
54
55 struct filter_owner_sys_t
56 {
57     spu_t *p_spu;
58     int i_channel;
59 };
60
61 /**
62  * Creates the subpicture unit
63  *
64  * \param p_this the parent object which creates the subpicture unit
65  */
66 spu_t *__spu_Create( vlc_object_t *p_this )
67 {
68     int i_index;
69     spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
70
71     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
72     {
73         p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
74     }
75
76     p_spu->p_blend = NULL;
77     p_spu->p_text = NULL;
78     p_spu->p_scale = NULL;
79     p_spu->i_filter = 0;
80     p_spu->pf_control = spu_vaControlDefault;
81
82     /* Register the default subpicture channel */
83     p_spu->i_channel = 2;
84
85     vlc_mutex_init( p_this, &p_spu->subpicture_lock );
86
87     vlc_object_attach( p_spu, p_this );
88
89     return p_spu;
90 }
91
92 /**
93  * Initialise the subpicture unit
94  *
95  * \param p_spu the subpicture unit object
96  */
97 int spu_Init( spu_t *p_spu )
98 {
99     char *psz_parser;
100     vlc_value_t val;
101
102     /* If the user requested a sub margin, we force the position. */
103     var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
104     var_Get( p_spu, "sub-margin", &val );
105     p_spu->i_margin = val.i_int;
106
107     var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
108     var_Get( p_spu, "sub-filter", &val );
109
110     psz_parser = val.psz_string;
111
112     while( psz_parser && *psz_parser )
113     {
114         config_chain_t *p_cfg;
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             {
678                 picture_t *p_pic;
679
680                 p_spu->p_scale->fmt_in.video = p_region->fmt;
681                 p_spu->p_scale->fmt_out.video = p_region->fmt;
682
683                 p_region->p_cache =
684                     p_subpic->pf_create_region( VLC_OBJECT(p_spu),
685                         &p_spu->p_scale->fmt_out.video );
686                 if( p_spu->p_scale->fmt_out.video.p_palette )
687                     *p_spu->p_scale->fmt_out.video.p_palette =
688                         *p_region->fmt.p_palette;
689                 p_region->p_cache->p_next = p_region->p_next;
690
691                 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
692                                   &p_region->picture );
693
694                 p_spu->p_scale->fmt_out.video.i_width =
695                     p_region->fmt.i_width * i_scale_width / 1000;
696                 p_spu->p_scale->fmt_out.video.i_visible_width =
697                     p_region->fmt.i_visible_width * i_scale_width / 1000;
698                 p_spu->p_scale->fmt_out.video.i_height =
699                     p_region->fmt.i_height * i_scale_height / 1000;
700                 p_spu->p_scale->fmt_out.video.i_visible_height =
701                     p_region->fmt.i_visible_height * i_scale_height / 1000;
702                 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
703                 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
704                 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
705
706                 p_pic = p_spu->p_scale->pf_video_filter(
707                                  p_spu->p_scale, &p_region->p_cache->picture );
708                 if( p_pic )
709                 {
710                     picture_t p_pic_tmp = p_region->p_cache->picture;
711                     p_region->p_cache->picture = *p_pic;
712                     *p_pic = p_pic_tmp;
713                     free( p_pic );
714                 }
715             }
716             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
717                 p_spu->p_scale && p_region->p_cache )
718             {
719                 p_region = p_region->p_cache;
720             }
721
722             if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
723             {
724                 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
725                     p_subpic->i_y;
726             }
727             else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
728             {
729                 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
730             }
731
732             if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
733             {
734                 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
735                     i_subpic_x;
736             }
737             else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
738             {
739                 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
740             }
741
742             if( p_subpic->b_absolute )
743             {
744                 i_x_offset = p_region->i_x +
745                     i_subpic_x * i_scale_width / 1000;
746                 i_y_offset = p_region->i_y +
747                     p_subpic->i_y * i_scale_height / 1000;
748
749             }
750
751             i_x_offset = __MAX( i_x_offset, 0 );
752             i_y_offset = __MAX( i_y_offset, 0 );
753
754             if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
755             {
756                 int i_diff = 0;
757                 int i_low = i_y_offset - p_spu->i_margin;
758                 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
759
760                 /* crop extra margin to keep within bounds */
761                 if( i_low < 0 ) i_diff = i_low;
762                 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
763                 i_y_offset -= ( p_spu->i_margin + i_diff );
764             }
765
766             p_spu->p_blend->fmt_in.video = p_region->fmt;
767
768             /* Force cropping if requested */
769             if( p_spu->b_force_crop )
770             {
771                 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
772                 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
773                 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
774                 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
775                 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
776
777                 /* Find the intersection */
778                 if( i_crop_x + i_crop_width <= i_x_offset ||
779                     i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
780                     i_crop_y + i_crop_height <= i_y_offset ||
781                     i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
782                 {
783                     /* No intersection */
784                     p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
785                 }
786                 else
787                 {
788                     int i_x, i_y, i_x_end, i_y_end;
789                     i_x = __MAX( i_crop_x, i_x_offset );
790                     i_y = __MAX( i_crop_y, i_y_offset );
791                     i_x_end = __MIN( i_crop_x + i_crop_width,
792                                    i_x_offset + (int)p_fmt->i_visible_width );
793                     i_y_end = __MIN( i_crop_y + i_crop_height,
794                                    i_y_offset + (int)p_fmt->i_visible_height );
795
796                     p_fmt->i_x_offset = i_x - i_x_offset;
797                     p_fmt->i_y_offset = i_y - i_y_offset;
798                     p_fmt->i_visible_width = i_x_end - i_x;
799                     p_fmt->i_visible_height = i_y_end - i_y;
800
801                     i_x_offset = i_x;
802                     i_y_offset = i_y;
803                 }
804             }
805
806             /* Update the output picture size */
807             p_spu->p_blend->fmt_out.video.i_width =
808                 p_spu->p_blend->fmt_out.video.i_visible_width =
809                     p_fmt->i_width;
810             p_spu->p_blend->fmt_out.video.i_height =
811                 p_spu->p_blend->fmt_out.video.i_visible_height =
812                     p_fmt->i_height;
813
814             if( p_subpic->b_fade )
815             {
816                 mtime_t i_fade_start = ( p_subpic->i_stop +
817                                          p_subpic->i_start ) / 2;
818                 mtime_t i_now = mdate();
819                 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
820                 {
821                     i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
822                                    ( p_subpic->i_stop - i_fade_start );
823                 }
824             }
825
826             i_x_offset = __MAX( i_x_offset, 0 );
827             i_y_offset = __MAX( i_y_offset, 0 );
828
829             p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
830                 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
831                 i_fade_alpha * p_subpic->i_alpha / 255 );
832
833             p_region = p_region->p_next;
834         }
835
836         p_subpic = p_subpic->p_next;
837     }
838
839     vlc_mutex_unlock( &p_spu->subpicture_lock );
840 }
841
842 /*****************************************************************************
843  * spu_SortSubpictures: find the subpictures to display
844  *****************************************************************************
845  * This function parses all subpictures and decides which ones need to be
846  * displayed. This operation does not need lock, since only READY_SUBPICTURE
847  * are handled. If no picture has been selected, display_date will depend on
848  * the subpicture.
849  * We also check for ephemer DVD subpictures (subpictures that have
850  * to be removed if a newer one is available), which makes it a lot
851  * more difficult to guess if a subpicture has to be rendered or not.
852  *****************************************************************************/
853 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
854                                    vlc_bool_t b_paused )
855 {
856     int i_index, i_channel;
857     subpicture_t *p_subpic = NULL;
858     subpicture_t *p_ephemer;
859     mtime_t      ephemer_date;
860
861     /* Run subpicture filters */
862     for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
863     {
864         subpicture_t *p_subpic_filter;
865         p_subpic_filter = p_spu->pp_filter[i_index]->
866             pf_sub_filter( p_spu->pp_filter[i_index], display_date );
867         if( p_subpic_filter )
868         {
869             spu_DisplaySubpicture( p_spu, p_subpic_filter );
870         }
871     }
872
873     /* We get an easily parsable chained list of subpictures which
874      * ends with NULL since p_subpic was initialized to NULL. */
875     for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
876     {
877         p_ephemer = 0;
878         ephemer_date = 0;
879
880         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
881         {
882             if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
883                 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
884             {
885                 continue;
886             }
887             if( display_date &&
888                 display_date < p_spu->p_subpicture[i_index].i_start )
889             {
890                 /* Too early, come back next monday */
891                 continue;
892             }
893
894             if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
895                 ephemer_date = p_spu->p_subpicture[i_index].i_start;
896
897             if( display_date > p_spu->p_subpicture[i_index].i_stop &&
898                 ( !p_spu->p_subpicture[i_index].b_ephemer ||
899                   p_spu->p_subpicture[i_index].i_stop >
900                   p_spu->p_subpicture[i_index].i_start ) &&
901                 !( p_spu->p_subpicture[i_index].b_pausable &&
902                    b_paused ) )
903             {
904                 /* Too late, destroy the subpic */
905                 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
906                 continue;
907             }
908
909             /* If this is an ephemer subpic, add it to our list */
910             if( p_spu->p_subpicture[i_index].b_ephemer )
911             {
912                 p_spu->p_subpicture[i_index].p_next = p_ephemer;
913                 p_ephemer = &p_spu->p_subpicture[i_index];
914
915                 continue;
916             }
917
918             p_spu->p_subpicture[i_index].p_next = p_subpic;
919             p_subpic = &p_spu->p_subpicture[i_index];
920         }
921
922         /* If we found ephemer subpictures, check if they have to be
923          * displayed or destroyed */
924         while( p_ephemer != NULL )
925         {
926             subpicture_t *p_tmp = p_ephemer;
927             p_ephemer = p_ephemer->p_next;
928
929             if( p_tmp->i_start < ephemer_date )
930             {
931                 /* Ephemer subpicture has lived too long */
932                 spu_DestroySubpicture( p_spu, p_tmp );
933             }
934             else
935             {
936                 /* Ephemer subpicture can still live a bit */
937                 p_tmp->p_next = p_subpic;
938                 p_subpic = p_tmp;
939             }
940         }
941     }
942
943     return p_subpic;
944 }
945
946 /*****************************************************************************
947  * SpuClearChannel: clear an spu channel
948  *****************************************************************************
949  * This function destroys the subpictures which belong to the spu channel
950  * corresponding to i_channel_id.
951  *****************************************************************************/
952 static void SpuClearChannel( spu_t *p_spu, int i_channel )
953 {
954     int          i_subpic;                               /* subpicture index */
955     subpicture_t *p_subpic = NULL;                  /* first free subpicture */
956
957     vlc_mutex_lock( &p_spu->subpicture_lock );
958
959     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
960     {
961         p_subpic = &p_spu->p_subpicture[i_subpic];
962         if( p_subpic->i_status == FREE_SUBPICTURE
963             || ( p_subpic->i_status != RESERVED_SUBPICTURE
964                  && p_subpic->i_status != READY_SUBPICTURE ) )
965         {
966             continue;
967         }
968
969         if( p_subpic->i_channel == i_channel )
970         {
971             while( p_subpic->p_region )
972             {
973                 subpicture_region_t *p_region = p_subpic->p_region;
974                 p_subpic->p_region = p_region->p_next;
975                 spu_DestroyRegion( p_spu, p_region );
976             }
977
978             if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
979             p_subpic->i_status = FREE_SUBPICTURE;
980         }
981     }
982
983     vlc_mutex_unlock( &p_spu->subpicture_lock );
984 }
985
986 /*****************************************************************************
987  * spu_ControlDefault: default methods for the subpicture unit control.
988  *****************************************************************************/
989 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
990 {
991     int *pi, i;
992
993     switch( i_query )
994     {
995     case SPU_CHANNEL_REGISTER:
996         pi = (int *)va_arg( args, int * );
997         if( pi ) *pi = p_spu->i_channel++;
998         break;
999
1000     case SPU_CHANNEL_CLEAR:
1001         i = (int)va_arg( args, int );
1002         SpuClearChannel( p_spu, i );
1003         break;
1004
1005     default:
1006         msg_Dbg( p_spu, "control query not supported" );
1007         return VLC_EGENERIC;
1008     }
1009
1010     return VLC_SUCCESS;
1011 }
1012
1013 /*****************************************************************************
1014  * Object variables callbacks
1015  *****************************************************************************/
1016
1017 /*****************************************************************************
1018  * UpdateSPU: update subpicture settings
1019  *****************************************************************************
1020  * This function is called from CropCallback and at initialization time, to
1021  * retrieve crop information from the input.
1022  *****************************************************************************/
1023 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1024 {
1025     vlc_value_t val;
1026
1027     p_spu->b_force_palette = VLC_FALSE;
1028     p_spu->b_force_crop = VLC_FALSE;
1029
1030     if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1031
1032     p_spu->b_force_crop = VLC_TRUE;
1033     var_Get( p_object, "x-start", &val );
1034     p_spu->i_crop_x = val.i_int;
1035     var_Get( p_object, "y-start", &val );
1036     p_spu->i_crop_y = val.i_int;
1037     var_Get( p_object, "x-end", &val );
1038     p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1039     var_Get( p_object, "y-end", &val );
1040     p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1041
1042     if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1043     {
1044         memcpy( p_spu->palette, val.p_address, 16 );
1045         p_spu->b_force_palette = VLC_TRUE;
1046     }
1047
1048     msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1049              p_spu->i_crop_x, p_spu->i_crop_y,
1050              p_spu->i_crop_width, p_spu->i_crop_height,
1051              p_spu->b_force_palette );
1052 }
1053
1054 /*****************************************************************************
1055  * CropCallback: called when the highlight properties are changed
1056  *****************************************************************************
1057  * This callback is called from the input thread when we need cropping
1058  *****************************************************************************/
1059 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1060                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1061 {
1062     UpdateSPU( (spu_t *)p_data, p_object );
1063     return VLC_SUCCESS;
1064 }
1065
1066 /*****************************************************************************
1067  * Buffers allocation callbacks for the filters
1068  *****************************************************************************/
1069 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1070 {
1071     filter_owner_sys_t *p_sys = p_filter->p_owner;
1072     subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1073     if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1074     return p_subpicture;
1075 }
1076
1077 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1078 {
1079     filter_owner_sys_t *p_sys = p_filter->p_owner;
1080     spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1081 }
1082
1083 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1084 {
1085     subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1086     memset( p_subpic, 0, sizeof(subpicture_t) );
1087     p_subpic->b_absolute = VLC_TRUE;
1088
1089     p_subpic->pf_create_region = __spu_CreateRegion;
1090     p_subpic->pf_make_region = __spu_MakeRegion;
1091     p_subpic->pf_destroy_region = __spu_DestroyRegion;
1092
1093     return p_subpic;
1094 }
1095
1096 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1097 {
1098     while( p_subpic->p_region )
1099     {
1100         subpicture_region_t *p_region = p_subpic->p_region;
1101         p_subpic->p_region = p_region->p_next;
1102         p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1103     }
1104
1105     free( p_subpic );
1106 }
1107
1108 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1109 {
1110     picture_t *p_picture = malloc( sizeof(picture_t) );
1111
1112     if( vout_AllocatePicture( p_filter, p_picture,
1113                               p_filter->fmt_out.video.i_chroma,
1114                               p_filter->fmt_out.video.i_width,
1115                               p_filter->fmt_out.video.i_height,
1116                               p_filter->fmt_out.video.i_aspect )
1117         != VLC_SUCCESS )
1118     {
1119         free( p_picture );
1120         return NULL;
1121     }
1122
1123     p_picture->pf_release = RegionPictureRelease;
1124
1125     return p_picture;
1126 }
1127
1128 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1129 {
1130     if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1131     if( p_pic ) free( p_pic );
1132 }