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