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