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