]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
* src/video_output/*: fixed race condition in spu initialization which affected DVD...
[vlc] / src / video_output / vout_subpictures.c
1 /*****************************************************************************
2  * vout_subpictures.c : subpicture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2004 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_filter.h"
39 #include "osd.h"
40
41 static void UpdateSPU        ( vout_thread_t *, vlc_object_t * );
42 static int  CropCallback     ( vlc_object_t *, char const *,
43                                vlc_value_t, vlc_value_t, void * );
44
45 static subpicture_t *spu_new_buffer( filter_t * );
46 static void spu_del_buffer( filter_t *, subpicture_t * );
47
48 /**
49  * Initialise the subpicture decoder unit
50  *
51  * \param p_vout the vout in which to create the subpicture unit
52  */
53 void vout_InitSPU( vout_thread_t *p_vout )
54 {
55     int i_index;
56
57     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
58     {
59         p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
60         p_vout->p_subpicture[i_index].i_type   = EMPTY_SUBPICTURE;
61     }
62
63     p_vout->p_blend = NULL;
64     p_vout->p_text = NULL;
65
66     p_vout->i_crop_x = p_vout->i_crop_y =
67         p_vout->i_crop_width = p_vout->i_crop_height = 0;
68     p_vout->b_force_alpha = VLC_FALSE;
69     p_vout->b_force_crop = VLC_FALSE;
70 }
71
72 /**
73  * Destroy the subpicture decoder unit
74  *
75  * \param p_vout the vout in which to destroy the subpicture unit
76  */
77 void vout_DestroySPU( vout_thread_t *p_vout )
78 {
79     int i_index;
80
81     /* Destroy all remaining subpictures */
82     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
83     {
84         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
85         {
86             vout_DestroySubPicture( p_vout,
87                                     &p_vout->p_subpicture[i_index] );
88         }
89     }
90
91     if( p_vout->p_blend )
92     {
93         if( p_vout->p_blend->p_module )
94             module_Unneed( p_vout->p_blend, p_vout->p_blend->p_module );
95
96         vlc_object_detach( p_vout->p_blend );
97         vlc_object_destroy( p_vout->p_blend );
98     }
99
100     if( p_vout->p_text )
101     {
102         if( p_vout->p_text->p_module )
103             module_Unneed( p_vout->p_text, p_vout->p_text->p_module );
104
105         vlc_object_detach( p_vout->p_text );
106         vlc_object_destroy( p_vout->p_text );
107     }
108
109     vout_AttachSPU( p_vout, VLC_OBJECT(p_vout), VLC_FALSE );
110 }
111
112 /**
113  * Attach/Detach the SPU from any input
114  *
115  * \param p_vout the vout in which to destroy the subpicture unit
116  * \param b_attach to select attach or detach
117  */
118 void vout_AttachSPU( vout_thread_t *p_vout, vlc_object_t *p_this,
119                      vlc_bool_t b_attach )
120 {
121     vlc_object_t *p_input;
122
123     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
124     if( !p_input ) return;
125
126     if( b_attach )
127     {
128         UpdateSPU( p_vout, VLC_OBJECT(p_input) );
129         var_AddCallback( p_input, "highlight", CropCallback, p_vout );
130         vlc_object_release( p_input );
131     }
132     else
133     {
134         /* Delete callback */
135         var_DelCallback( p_input, "highlight", CropCallback, p_vout );
136         vlc_object_release( p_input );
137     }
138
139 }
140
141 /**
142  * Create a subpicture region
143  *
144  * \param p_this vlc_object_t
145  * \param p_fmt the format that this subpicture region should have
146  */
147 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
148                                          video_format_t *p_fmt )
149 {
150     subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
151     memset( p_region, 0, sizeof(subpicture_region_t) );
152     p_region->p_next = 0;
153     p_region->fmt = *p_fmt;
154     p_region->psz_text = 0;
155
156     if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
157         p_fmt->p_palette = p_region->fmt.p_palette =
158             malloc( sizeof(video_palette_t) );
159     else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
160
161     p_region->picture.p_data_orig = 0;
162
163     if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
164
165     vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
166                           p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
167
168     if( !p_region->picture.i_planes )
169     {
170         free( p_region );
171         free( p_fmt->p_palette );
172         return NULL;
173     }
174
175     return p_region;
176 }
177
178 /**
179  * Destroy a subpicture region
180  *
181  * \param p_this vlc_object_t
182  * \param p_region the subpicture region to destroy
183  */
184 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
185 {
186     if( !p_region ) return;
187     if( p_region->picture.p_data_orig ) free( p_region->picture.p_data_orig );
188     if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
189     if( p_region->psz_text ) free( p_region->psz_text );
190     free( p_region );
191 }
192
193 /**
194  * Display a subpicture unit
195  *
196  * Remove the reservation flag of a subpicture, which will cause it to be
197  * ready for display.
198  * \param p_vout the video output this subpicture should be displayed on
199  * \param p_subpic the subpicture to display
200  */
201 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
202 {
203     int         i_margin;
204
205     /* Check if status is valid */
206     if( p_subpic->i_status != RESERVED_SUBPICTURE )
207     {
208         msg_Err( p_vout, "subpicture %p has invalid status #%d",
209                          p_subpic, p_subpic->i_status );
210     }
211
212     /* If the user requested an SPU margin, we force the position after
213      * having checked that it was a valid value. */
214     i_margin = config_GetInt( p_vout, "spumargin" );
215
216     if( i_margin >= 0 )
217     {
218         if( p_subpic->i_height + (unsigned int)i_margin
219                                                  <= p_vout->output.i_height )
220         {
221             p_subpic->i_y = p_vout->output.i_height
222                              - i_margin - p_subpic->i_height;
223         }
224     }
225
226     /* Remove reservation flag */
227     p_subpic->i_status = READY_SUBPICTURE;
228 }
229
230 /**
231  * Allocate a subpicture in the video output heap.
232  *
233  * This function create a reserved subpicture in the video output heap.
234  * A null pointer is returned if the function fails. This method provides an
235  * already allocated zone of memory in the spu data fields. It needs locking
236  * since several pictures can be created by several producers threads.
237  * \param p_vout the vout in which to create the subpicture
238  * \param i_channel the channel this subpicture should belong to
239  * \param i_type the type of the subpicture
240  * \return NULL on error, a reserved subpicture otherwise
241  */
242 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_channel,
243                                      int i_type )
244 {
245     int                 i_subpic;                        /* subpicture index */
246     subpicture_t *      p_subpic = NULL;            /* first free subpicture */
247
248     /* Clear the default channel before writing into it */
249     if( i_channel == DEFAULT_CHAN )
250     {
251         vout_ClearOSDChannel( p_vout, DEFAULT_CHAN );
252     }
253
254     /* Get lock */
255     vlc_mutex_lock( &p_vout->subpicture_lock );
256
257     /*
258      * Look for an empty place
259      */
260     p_subpic = NULL;
261     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
262     {
263         if( p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
264         {
265             /* Subpicture is empty and ready for allocation */
266             p_subpic = &p_vout->p_subpicture[i_subpic];
267             p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
268             break;
269         }
270     }
271
272     /* If no free subpicture could be found */
273     if( p_subpic == NULL )
274     {
275         msg_Err( p_vout, "subpicture heap is full" );
276         vlc_mutex_unlock( &p_vout->subpicture_lock );
277         return NULL;
278     }
279
280     /* Copy subpicture information, set some default values */
281     p_subpic->i_channel = i_channel;
282     p_subpic->i_type    = i_type;
283     p_subpic->i_status  = RESERVED_SUBPICTURE;
284
285     p_subpic->i_start   = 0;
286     p_subpic->i_stop    = 0;
287     p_subpic->b_ephemer = VLC_FALSE;
288
289     p_subpic->i_x       = 0;
290     p_subpic->i_y       = 0;
291     p_subpic->i_width   = 0;
292     p_subpic->i_height  = 0;
293     p_subpic->b_absolute= VLC_TRUE;
294     p_subpic->i_flags   = 0;
295     p_subpic->pf_render = 0;
296     p_subpic->pf_destroy= 0;
297     p_subpic->p_sys     = 0;
298
299     /* Remain last subpicture displayed in DEFAULT_CHAN */
300     if( i_channel == DEFAULT_CHAN )
301     {
302         p_vout->p_default_channel = p_subpic;
303     }
304
305     vlc_mutex_unlock( &p_vout->subpicture_lock );
306
307     p_subpic->pf_create_region = __spu_CreateRegion;
308     p_subpic->pf_destroy_region = __spu_DestroyRegion;
309
310     return p_subpic;
311 }
312
313 /**
314  * Remove a subpicture from the heap
315  *
316  * This function frees a previously reserved subpicture.
317  * It is meant to be used when the construction of a picture aborted.
318  * This function does not need locking since reserved subpictures are ignored
319  * by the output thread.
320  */
321 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
322 {
323     /* Get lock */
324     vlc_mutex_lock( &p_vout->subpicture_lock );
325
326     /* There can be race conditions so we need to check the status */
327     if( p_subpic->i_status == FREE_SUBPICTURE )
328     {
329         vlc_mutex_unlock( &p_vout->subpicture_lock );
330         return;
331     }
332
333     /* Check if status is valid */
334     if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
335            && ( p_subpic->i_status != READY_SUBPICTURE ) )
336     {
337         msg_Err( p_vout, "subpicture %p has invalid status %d",
338                          p_subpic, p_subpic->i_status );
339     }
340
341     while( p_subpic->p_region )
342     {
343         subpicture_region_t *p_region = p_subpic->p_region;
344         p_subpic->p_region = p_region->p_next;
345         spu_DestroyRegion( p_vout, p_region );
346     }
347
348     if( p_subpic->pf_destroy )
349     {
350         p_subpic->pf_destroy( p_subpic );
351     }
352
353     p_subpic->i_status = FREE_SUBPICTURE;
354
355     vlc_mutex_unlock( &p_vout->subpicture_lock );
356 }
357
358 /*****************************************************************************
359  * vout_RenderSubPictures: render a subpicture list
360  *****************************************************************************
361  * This function renders all sub picture units in the list.
362  *****************************************************************************/
363 void vout_RenderSubPictures( vout_thread_t *p_vout, picture_t *p_pic_dst,
364                              picture_t *p_pic_src, subpicture_t *p_subpic )
365 {
366     /* Get lock */
367     vlc_mutex_lock( &p_vout->subpicture_lock );
368
369     /* Check i_status again to make sure spudec hasn't destroyed the subpic */
370     while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
371     {
372         subpicture_region_t *p_region = p_subpic->p_region;
373
374         /* Load the blending module */
375         if( !p_vout->p_blend && p_region )
376         {
377             p_vout->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
378             vlc_object_attach( p_vout->p_blend, p_vout );
379             p_vout->p_blend->fmt_out.video.i_x_offset =
380                 p_vout->p_blend->fmt_out.video.i_y_offset = 0;
381             p_vout->p_blend->fmt_out.video.i_aspect =
382                 p_vout->render.i_aspect;
383             p_vout->p_blend->fmt_out.video.i_chroma =
384                 p_vout->output.i_chroma;
385     
386             p_vout->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
387     
388             p_vout->p_blend->p_module =
389                 module_Need( p_vout->p_blend, "video blending", 0, 0 );
390         }
391     
392         /* Load the text rendering module */
393         if( !p_vout->p_text && p_region )
394         {
395             p_vout->p_text = vlc_object_create( p_vout, sizeof(filter_t) );
396             vlc_object_attach( p_vout->p_text, p_vout );
397     
398             p_vout->p_text->fmt_out.video.i_width =
399                 p_vout->p_text->fmt_out.video.i_visible_width =
400                     p_vout->output.i_width;
401             p_vout->p_text->fmt_out.video.i_height =
402                 p_vout->p_text->fmt_out.video.i_visible_height =
403                     p_vout->output.i_height;
404     
405             p_vout->p_text->pf_spu_buffer_new = spu_new_buffer;
406             p_vout->p_text->pf_spu_buffer_del = spu_del_buffer;
407     
408             p_vout->p_text->p_module =
409                 module_Need( p_vout->p_text, "text renderer", 0, 0 );
410         }
411
412         if( p_subpic->pf_render )
413         {
414             p_subpic->pf_render( p_vout, p_pic_dst, p_subpic );
415         }
416         else while( p_region && p_vout->p_blend &&
417                     p_vout->p_blend->pf_video_blend )
418         {
419             int i_x_offset = p_region->i_x + p_subpic->i_x;
420             int i_y_offset = p_region->i_y + p_subpic->i_y;
421
422             if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
423             {
424                 if( p_vout->p_text && p_vout->p_text->p_module &&
425                     p_vout->p_text->pf_render_string )
426                 {
427                     /* TODO: do it in a less hacky way
428                      * (modify text renderer API) */
429                     subpicture_t *p_spu;
430                     subpicture_region_t tmp_region;
431                     block_t *p_new_block =
432                         block_New( p_vout, strlen(p_region->psz_text) + 1 );
433
434                     if( p_new_block )
435                     {
436                         memcpy( p_new_block->p_buffer, p_region->psz_text,
437                                 p_new_block->i_buffer );
438                         p_new_block->i_pts = p_new_block->i_dts =
439                             p_subpic->i_start;
440                         p_new_block->i_length =
441                             p_subpic->i_start - p_subpic->i_stop;
442                         p_spu = p_vout->p_text->pf_render_string(
443                             p_vout->p_text, p_new_block );
444
445                         if( p_spu )
446                         {
447                             tmp_region = *p_region;
448                             *p_region = *p_spu->p_region;
449                             p_region->p_next = tmp_region.p_next;
450                             *p_spu->p_region = tmp_region;
451                             p_vout->p_text->pf_spu_buffer_del( p_vout->p_text,
452                                                                p_spu );
453                         }
454                     }
455                 }
456             }
457
458             if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
459             {
460                 i_y_offset = p_vout->output.i_height - p_region->fmt.i_height -
461                     p_subpic->i_y;
462             }
463             else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
464             {
465                 i_y_offset = p_vout->output.i_height / 2 -
466                     p_region->fmt.i_height / 2;
467             }
468
469             if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
470             {
471                 i_x_offset = p_vout->output.i_width - p_region->fmt.i_width -
472                     p_subpic->i_x;
473             }
474             else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
475             {
476                 i_x_offset = p_vout->output.i_width / 2 -
477                     p_region->fmt.i_width / 2;
478             }
479
480             if( p_subpic->b_absolute )
481             {
482                 i_x_offset = p_region->i_x + p_subpic->i_x;
483                 i_y_offset = p_region->i_y + p_subpic->i_y;
484             }
485
486             p_vout->p_blend->fmt_in.video = p_region->fmt;
487
488             /* Force cropping if requested */
489             if( p_vout->b_force_crop )
490             {
491                 video_format_t *p_fmt = &p_vout->p_blend->fmt_in.video;
492
493                 /* Find the intersection */
494                 if( p_vout->i_crop_x + p_vout->i_crop_width <= i_x_offset ||
495                     i_x_offset + (int)p_fmt->i_visible_width <
496                         p_vout->i_crop_x ||
497                     p_vout->i_crop_y + p_vout->i_crop_height <= i_y_offset ||
498                     i_y_offset + (int)p_fmt->i_visible_height <
499                         p_vout->i_crop_y )
500                 {
501                     /* No intersection */
502                     p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
503                 }
504                 else
505                 {
506                     int i_x, i_y, i_x_end, i_y_end;
507                     i_x = __MAX( p_vout->i_crop_x, i_x_offset );
508                     i_y = __MAX( p_vout->i_crop_y, i_y_offset );
509                     i_x_end = __MIN( p_vout->i_crop_x + p_vout->i_crop_width,
510                                    i_x_offset + (int)p_fmt->i_visible_width );
511                     i_y_end = __MIN( p_vout->i_crop_y + p_vout->i_crop_height,
512                                    i_y_offset + (int)p_fmt->i_visible_height );
513
514                     p_fmt->i_x_offset = i_x - i_x_offset;
515                     p_fmt->i_y_offset = i_y - i_y_offset;
516                     p_fmt->i_visible_width = i_x_end - i_x;
517                     p_fmt->i_visible_height = i_y_end - i_y;
518
519                     i_x_offset = i_x;
520                     i_y_offset = i_y;
521                 }
522             }
523
524             /* Force palette if requested */
525             if( p_vout->b_force_alpha && VLC_FOURCC('Y','U','V','P') ==
526                 p_vout->p_blend->fmt_in.video.i_chroma )
527             {
528                 p_vout->p_blend->fmt_in.video.p_palette->palette[0][3] =
529                     p_vout->pi_alpha[0];
530                 p_vout->p_blend->fmt_in.video.p_palette->palette[1][3] =
531                     p_vout->pi_alpha[1];
532                 p_vout->p_blend->fmt_in.video.p_palette->palette[2][3] =
533                     p_vout->pi_alpha[2];
534                 p_vout->p_blend->fmt_in.video.p_palette->palette[3][3] =
535                     p_vout->pi_alpha[3];
536             }
537
538             /* Update the output picture size */
539             p_vout->p_blend->fmt_out.video.i_width =
540                 p_vout->p_blend->fmt_out.video.i_visible_width =
541                     p_vout->output.i_width;
542             p_vout->p_blend->fmt_out.video.i_height =
543                 p_vout->p_blend->fmt_out.video.i_visible_height =
544                     p_vout->output.i_height;
545
546            p_vout->p_blend->pf_video_blend( p_vout->p_blend, p_pic_dst,
547                 p_pic_src, &p_region->picture, i_x_offset, i_y_offset );
548
549             p_region = p_region->p_next;
550         }
551
552         p_subpic = p_subpic->p_next;
553     }
554
555     vlc_mutex_unlock( &p_vout->subpicture_lock );
556 }
557
558 /*****************************************************************************
559  * vout_SortSubPictures: find the subpictures to display
560  *****************************************************************************
561  * This function parses all subpictures and decides which ones need to be
562  * displayed. This operation does not need lock, since only READY_SUBPICTURE
563  * are handled. If no picture has been selected, display_date will depend on
564  * the subpicture.
565  * We also check for ephemer DVD subpictures (subpictures that have
566  * to be removed if a newer one is available), which makes it a lot
567  * more difficult to guess if a subpicture has to be rendered or not.
568  *****************************************************************************/
569 subpicture_t *vout_SortSubPictures( vout_thread_t *p_vout,
570                                     mtime_t display_date )
571 {
572     int i_index;
573     subpicture_t *p_subpic     = NULL;
574     subpicture_t *p_ephemer    = NULL;
575     mtime_t       ephemer_date = 0;
576
577     /* We get an easily parsable chained list of subpictures which
578      * ends with NULL since p_subpic was initialized to NULL. */
579     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
580     {
581         if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
582         {
583             /* If it is a DVD subpicture, check its date */
584             if( p_vout->p_subpicture[i_index].i_type == MEMORY_SUBPICTURE )
585             {
586                 if( !p_vout->p_subpicture[i_index].b_ephemer
587                      && display_date > p_vout->p_subpicture[i_index].i_stop )
588                 {
589                     /* Too late, destroy the subpic */
590                     vout_DestroySubPicture( p_vout,
591                                     &p_vout->p_subpicture[i_index] );
592                     continue;
593                 }
594
595                 if( display_date
596                      && display_date < p_vout->p_subpicture[i_index].i_start )
597                 {
598                     /* Too early, come back next monday */
599                     continue;
600                 }
601
602                 /* If this is an ephemer subpic, see if it's the
603                  * youngest we have */
604                 if( p_vout->p_subpicture[i_index].b_ephemer )
605                 {
606                     if( p_ephemer == NULL )
607                     {
608                         p_ephemer = &p_vout->p_subpicture[i_index];
609                         continue;
610                     }
611
612                     if( p_vout->p_subpicture[i_index].i_start
613                                                      < p_ephemer->i_start )
614                     {
615                         /* Link the previous ephemer subpicture and
616                          * replace it with the current one */
617                         p_ephemer->p_next = p_subpic;
618                         p_subpic = p_ephemer;
619                         p_ephemer = &p_vout->p_subpicture[i_index];
620
621                         /* If it's the 2nd youngest subpicture,
622                          * register its date */
623                         if( !ephemer_date
624                               || ephemer_date > p_subpic->i_start )
625                         {
626                             ephemer_date = p_subpic->i_start;
627                         }
628
629                         continue;
630                     }
631                 }
632
633                 p_vout->p_subpicture[i_index].p_next = p_subpic;
634                 p_subpic = &p_vout->p_subpicture[i_index];
635
636                 /* If it's the 2nd youngest subpicture, register its date */
637                 if( !ephemer_date || ephemer_date > p_subpic->i_start )
638                 {
639                     ephemer_date = p_subpic->i_start;
640                 }
641             }
642             /* If it's not a DVD subpicture, just register it */
643             else
644             {
645                 p_vout->p_subpicture[i_index].p_next = p_subpic;
646                 p_subpic = &p_vout->p_subpicture[i_index];
647             }
648         }
649     }
650
651     /* If we found an ephemer subpicture, check if it has to be
652      * displayed */
653     if( p_ephemer != NULL )
654     {
655         if( p_ephemer->i_start <= ephemer_date )
656         {
657             /* Ephemer subpicture has lived too long */
658             vout_DestroySubPicture( p_vout, p_ephemer );
659         }
660         else
661         {
662             /* Ephemer subpicture can still live a bit */
663             p_ephemer->p_next = p_subpic;
664             return p_ephemer;
665         }
666     }
667
668     return p_subpic;
669 }
670
671 /*****************************************************************************
672  * vout_RegisterOSDChannel: register an OSD channel
673  *****************************************************************************
674  * This function affects an ID to an OSD channel
675  *****************************************************************************/
676 int vout_RegisterOSDChannel( vout_thread_t *p_vout )
677 {
678     msg_Dbg( p_vout, "Registering OSD channel, ID: %i",
679              p_vout->i_channel_count + 1 );
680     return ++p_vout->i_channel_count;
681 }
682
683 /*****************************************************************************
684  * vout_ClearOSDChannel: clear an OSD channel
685  *****************************************************************************
686  * This function destroys the subpictures which belong to the OSD channel
687  * corresponding to i_channel_id.
688  *****************************************************************************/
689 void vout_ClearOSDChannel( vout_thread_t *p_vout, int i_channel )
690 {
691     int                 i_subpic;                        /* subpicture index */
692     subpicture_t *      p_subpic = NULL;            /* first free subpicture */
693
694     if( i_channel == DEFAULT_CHAN )
695     {
696         if( p_vout->p_default_channel != NULL )
697         {
698             vout_DestroySubPicture( p_vout, p_vout->p_default_channel );
699         }
700         p_vout->p_default_channel = NULL;
701         return;
702     }
703
704     vlc_mutex_lock( &p_vout->subpicture_lock );
705
706     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
707     {
708         p_subpic = &p_vout->p_subpicture[i_subpic];
709         if( p_subpic->i_status == FREE_SUBPICTURE
710             || ( p_subpic->i_status != RESERVED_SUBPICTURE
711                  && p_subpic->i_status != READY_SUBPICTURE ) )
712         {
713             continue;
714         }
715
716         if( p_subpic->i_channel == i_channel )
717         {
718             while( p_subpic->p_region )
719             {
720                 subpicture_region_t *p_region = p_subpic->p_region;
721                 p_subpic->p_region = p_region->p_next;
722                 spu_DestroyRegion( p_vout, p_region );
723             }
724
725             if( p_subpic->pf_destroy )
726             {
727                 p_subpic->pf_destroy( p_subpic );
728             }
729             p_subpic->i_status = FREE_SUBPICTURE;
730         }
731     }
732
733     vlc_mutex_unlock( &p_vout->subpicture_lock );
734 }
735
736 /*****************************************************************************
737  * UpdateSPU: update subpicture settings
738  *****************************************************************************
739  * This function is called from CropCallback and at initialization time, to
740  * retrieve crop information from the input.
741  *****************************************************************************/
742 static void UpdateSPU( vout_thread_t *p_vout, vlc_object_t *p_object )
743 {
744     vlc_value_t val;
745
746     p_vout->b_force_alpha = VLC_FALSE;
747     p_vout->b_force_crop = VLC_FALSE;
748
749     if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
750
751     p_vout->b_force_crop = VLC_TRUE;
752     var_Get( p_object, "x-start", &val );
753     p_vout->i_crop_x = val.i_int;
754     var_Get( p_object, "y-start", &val );
755     p_vout->i_crop_y = val.i_int;
756     var_Get( p_object, "x-end", &val );
757     p_vout->i_crop_width = val.i_int - p_vout->i_crop_x;
758     var_Get( p_object, "y-end", &val );
759     p_vout->i_crop_height = val.i_int - p_vout->i_crop_y;
760
761 #if 0
762     if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
763     {
764         int i;
765         for( i = 0; i < 4; i++ )
766         {
767             p_vout->pi_color[i] = ((uint8_t *)val.p_address)[i];
768         }
769     }
770 #endif
771
772     if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
773     {
774         int i;
775         for( i = 0; i < 4; i++ )
776         {
777             p_vout->pi_alpha[i] = ((uint8_t *)val.p_address)[i];
778             p_vout->pi_alpha[i] = p_vout->pi_alpha[i] == 0xf ?
779                 0xff : p_vout->pi_alpha[i] << 4;
780         }
781         p_vout->b_force_alpha = VLC_TRUE;
782     }
783
784     msg_Dbg( p_vout, "crop: %i,%i,%i,%i, alpha: %i",
785              p_vout->i_crop_x, p_vout->i_crop_y,
786              p_vout->i_crop_width, p_vout->i_crop_height,
787              p_vout->b_force_alpha );
788 }
789
790 /*****************************************************************************
791  * CropCallback: called when the highlight properties are changed
792  *****************************************************************************
793  * This callback is called from the input thread when we need cropping
794  *****************************************************************************/
795 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
796                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
797 {
798     UpdateSPU( (vout_thread_t *)p_data, p_object );
799     return VLC_SUCCESS;
800 }
801
802 /*****************************************************************************
803  * Buffers allocation callbacks for the filters
804  *****************************************************************************/
805 static subpicture_t *spu_new_buffer( filter_t *p_filter )
806 {
807     subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
808     memset( p_subpic, 0, sizeof(subpicture_t) );
809     p_subpic->b_absolute = VLC_TRUE;
810
811     p_subpic->pf_create_region = __spu_CreateRegion;
812     p_subpic->pf_destroy_region = __spu_DestroyRegion;
813
814     return p_subpic;
815 }
816
817 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
818 {
819     while( p_subpic->p_region )
820     {
821         subpicture_region_t *p_region = p_subpic->p_region;
822         p_subpic->p_region = p_region->p_next;
823         p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
824     }
825
826     free( p_subpic );
827 }