]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
Don't include config.h from the headers - refs #297.
[vlc] / src / video_output / vout_subpictures.c
1 /*****************************************************************************
2  * vout_subpictures.c : subpicture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_vout.h>
35 #include <vlc_block.h>
36 #include <vlc_filter.h>
37 #include <vlc_osd.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static void UpdateSPU   ( spu_t *, vlc_object_t * );
43 static int  CropCallback( vlc_object_t *, char const *,
44                           vlc_value_t, vlc_value_t, void * );
45
46 static int spu_vaControlDefault( spu_t *, int, va_list );
47
48 static subpicture_t *sub_new_buffer( filter_t * );
49 static void sub_del_buffer( filter_t *, subpicture_t * );
50 static subpicture_t *spu_new_buffer( filter_t * );
51 static void spu_del_buffer( filter_t *, subpicture_t * );
52 static picture_t *spu_new_video_buffer( filter_t * );
53 static void spu_del_video_buffer( filter_t *, picture_t * );
54
55 static int spu_ParseChain( spu_t * );
56 static void spu_DeleteChain( spu_t * );
57 static int SubFilterCallback( vlc_object_t *, char const *,
58                               vlc_value_t, vlc_value_t, void * );
59
60 struct filter_owner_sys_t
61 {
62     spu_t *p_spu;
63     int i_channel;
64 };
65
66 enum {
67     SCALE_DEFAULT,
68     SCALE_TEXT,
69     SCALE_SIZE
70 };
71 /**
72  * Creates the subpicture unit
73  *
74  * \param p_this the parent object which creates the subpicture unit
75  */
76 spu_t *__spu_Create( vlc_object_t *p_this )
77 {
78     int i_index;
79     spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
80
81     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
82     {
83         p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
84     }
85
86     p_spu->p_blend = NULL;
87     p_spu->p_text = NULL;
88     p_spu->p_scale = NULL;
89     p_spu->i_filter = 0;
90     p_spu->pf_control = spu_vaControlDefault;
91
92     /* Register the default subpicture channel */
93     p_spu->i_channel = 2;
94
95     vlc_mutex_init( p_this, &p_spu->subpicture_lock );
96
97     vlc_object_attach( p_spu, p_this );
98
99     return p_spu;
100 }
101
102 /**
103  * Initialise the subpicture unit
104  *
105  * \param p_spu the subpicture unit object
106  */
107 int spu_Init( spu_t *p_spu )
108 {
109     vlc_value_t val;
110
111     /* If the user requested a sub margin, we force the position. */
112     var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
113     var_Get( p_spu, "sub-margin", &val );
114     p_spu->i_margin = val.i_int;
115
116     var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
117     var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
118
119     spu_ParseChain( p_spu );
120
121     return VLC_SUCCESS;
122 }
123
124 int spu_ParseChain( spu_t *p_spu )
125 {
126     char *psz_parser;
127     vlc_value_t val;
128     var_Get( p_spu, "sub-filter", &val );
129     psz_parser = val.psz_string;
130
131     while( psz_parser && *psz_parser )
132     {
133         config_chain_t *p_cfg;
134         char *psz_name;
135
136         psz_parser = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
137
138         msg_Dbg( p_spu, "adding sub-filter: %s", psz_name );
139
140         p_spu->pp_filter[p_spu->i_filter] =
141             vlc_object_create( p_spu, VLC_OBJECT_FILTER );
142         vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
143         p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
144         p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
145         p_spu->pp_filter[p_spu->i_filter]->p_cfg = p_cfg;
146         p_spu->pp_filter[p_spu->i_filter]->p_module =
147             module_Need( p_spu->pp_filter[p_spu->i_filter],
148                          "sub filter", psz_name, VLC_TRUE );
149         if( p_spu->pp_filter[p_spu->i_filter]->p_module )
150         {
151             filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
152             if( p_sys )
153             {
154                 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
155                 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
156                 p_sys->p_spu = p_spu;
157                 p_spu->i_filter++;
158             }
159         }
160         else
161         {
162             msg_Dbg( p_spu, "no sub filter found" );
163             config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
164             vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
165             vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
166         }
167
168         if( p_spu->i_filter >= 10 )
169         {
170             msg_Dbg( p_spu, "can't add anymore filters" );
171         }
172
173         free( psz_name );
174     }
175     if( val.psz_string ) free( val.psz_string );
176
177     return VLC_SUCCESS;
178 }
179
180 /**
181  * Destroy the subpicture unit
182  *
183  * \param p_this the parent object which destroys the subpicture unit
184  */
185 void spu_Destroy( spu_t *p_spu )
186 {
187     int i_index;
188
189     vlc_object_detach( p_spu );
190
191     /* Destroy all remaining subpictures */
192     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
193     {
194         if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
195         {
196             spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
197         }
198     }
199
200     if( p_spu->p_blend )
201     {
202         if( p_spu->p_blend->p_module )
203             module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
204
205         vlc_object_detach( p_spu->p_blend );
206         vlc_object_destroy( p_spu->p_blend );
207     }
208
209     if( p_spu->p_text )
210     {
211         if( p_spu->p_text->p_module )
212             module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
213
214         vlc_object_detach( p_spu->p_text );
215         vlc_object_destroy( p_spu->p_text );
216     }
217
218     if( p_spu->p_scale )
219     {
220         if( p_spu->p_scale->p_module )
221             module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
222
223         vlc_object_detach( p_spu->p_scale );
224         vlc_object_destroy( p_spu->p_scale );
225     }
226
227     spu_DeleteChain( p_spu );
228
229     vlc_mutex_destroy( &p_spu->subpicture_lock );
230     vlc_object_destroy( p_spu );
231 }
232
233 static void spu_DeleteChain( spu_t *p_spu )
234 {
235     if( p_spu->i_filter )
236     while( p_spu->i_filter )
237     {
238         p_spu->i_filter--;
239         module_Unneed( p_spu->pp_filter[p_spu->i_filter],
240                        p_spu->pp_filter[p_spu->i_filter]->p_module );
241         free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
242         config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
243         vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
244         vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
245     }
246 }
247
248 /**
249  * Attach/Detach the SPU from any input
250  *
251  * \param p_this the object in which to destroy the subpicture unit
252  * \param b_attach to select attach or detach
253  */
254 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
255 {
256     vlc_object_t *p_input;
257
258     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
259     if( !p_input ) return;
260
261     if( b_attach )
262     {
263         UpdateSPU( p_spu, VLC_OBJECT(p_input) );
264         var_AddCallback( p_input, "highlight", CropCallback, p_spu );
265         vlc_object_release( p_input );
266     }
267     else
268     {
269         /* Delete callback */
270         var_DelCallback( p_input, "highlight", CropCallback, p_spu );
271         vlc_object_release( p_input );
272     }
273 }
274
275 /**
276  * Create a subpicture region
277  *
278  * \param p_this vlc_object_t
279  * \param p_fmt the format that this subpicture region should have
280  */
281 static void RegionPictureRelease( picture_t *p_pic )
282 {
283     if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
284 }
285 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
286                                          video_format_t *p_fmt )
287 {
288     subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
289     if( !p_region ) return NULL;
290
291     memset( p_region, 0, sizeof(subpicture_region_t) );
292     p_region->p_next = NULL;
293     p_region->p_cache = NULL;
294     p_region->fmt = *p_fmt;
295     p_region->psz_text = NULL;
296     p_region->p_style = NULL;
297
298     if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
299         p_fmt->p_palette = p_region->fmt.p_palette =
300             malloc( sizeof(video_palette_t) );
301     else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
302
303     p_region->picture.p_data_orig = NULL;
304
305     if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
306
307     vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
308                           p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
309
310     if( !p_region->picture.i_planes )
311     {
312         free( p_region );
313         if( p_fmt->p_palette ) free( p_fmt->p_palette );
314         return NULL;
315     }
316
317     p_region->picture.pf_release = RegionPictureRelease;
318
319     return p_region;
320 }
321
322 /**
323  * Make a subpicture region from an existing picture_t
324  *
325  * \param p_this vlc_object_t
326  * \param p_fmt the format that this subpicture region should have
327  * \param p_pic a pointer to the picture creating the region (not freed)
328  */
329 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
330                                        video_format_t *p_fmt,
331                                        picture_t *p_pic )
332 {
333     subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
334     (void)p_this;
335     if( !p_region ) return NULL;
336     memset( p_region, 0, sizeof(subpicture_region_t) );
337     p_region->p_next = 0;
338     p_region->p_cache = 0;
339     p_region->fmt = *p_fmt;
340     p_region->psz_text = 0;
341     p_region->p_style = NULL;
342
343     if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
344         p_fmt->p_palette = p_region->fmt.p_palette =
345             malloc( sizeof(video_palette_t) );
346     else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
347
348     memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
349     p_region->picture.pf_release = RegionPictureRelease;
350
351     return p_region;
352 }
353
354 /**
355  * Destroy a subpicture region
356  *
357  * \param p_this vlc_object_t
358  * \param p_region the subpicture region to destroy
359  */
360 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
361 {
362     if( !p_region ) return;
363     if( p_region->picture.pf_release )
364         p_region->picture.pf_release( &p_region->picture );
365     if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
366     if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
367
368     if( p_region->psz_text )
369         free( p_region->psz_text );
370     if( p_region->psz_html )
371         free( p_region->psz_html );
372     //free( p_region->p_style ); FIXME --fenrir plugin does not allocate the memory for it. I think it might lead to segfault, video renderer can live longer than the decoder
373     free( p_region );
374 }
375
376 /**
377  * Display a subpicture
378  *
379  * Remove the reservation flag of a subpicture, which will cause it to be
380  * ready for display.
381  * \param p_spu the subpicture unit object
382  * \param p_subpic the subpicture to display
383  */
384 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
385 {
386     /* Check if status is valid */
387     if( p_subpic->i_status != RESERVED_SUBPICTURE )
388     {
389         msg_Err( p_spu, "subpicture %p has invalid status #%d",
390                  p_subpic, p_subpic->i_status );
391     }
392
393     /* Remove reservation flag */
394     p_subpic->i_status = READY_SUBPICTURE;
395
396     if( p_subpic->i_channel == DEFAULT_CHAN )
397     {
398         p_subpic->i_channel = 0xFFFF;
399         spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
400         p_subpic->i_channel = DEFAULT_CHAN;
401     }
402 }
403
404 /**
405  * Allocate a subpicture in the spu heap.
406  *
407  * This function create a reserved subpicture in the spu heap.
408  * A null pointer is returned if the function fails. This method provides an
409  * already allocated zone of memory in the spu data fields. It needs locking
410  * since several pictures can be created by several producers threads.
411  * \param p_spu the subpicture unit in which to create the subpicture
412  * \return NULL on error, a reserved subpicture otherwise
413  */
414 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
415 {
416     int                 i_subpic;                        /* subpicture index */
417     subpicture_t *      p_subpic = NULL;            /* first free subpicture */
418
419     /* Get lock */
420     vlc_mutex_lock( &p_spu->subpicture_lock );
421
422     /*
423      * Look for an empty place
424      */
425     p_subpic = NULL;
426     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
427     {
428         if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
429         {
430             /* Subpicture is empty and ready for allocation */
431             p_subpic = &p_spu->p_subpicture[i_subpic];
432             p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
433             break;
434         }
435     }
436
437     /* If no free subpicture could be found */
438     if( p_subpic == NULL )
439     {
440         msg_Err( p_spu, "subpicture heap is full" );
441         vlc_mutex_unlock( &p_spu->subpicture_lock );
442         return NULL;
443     }
444
445     /* Copy subpicture information, set some default values */
446     memset( p_subpic, 0, sizeof(subpicture_t) );
447     p_subpic->i_status   = RESERVED_SUBPICTURE;
448     p_subpic->b_absolute = VLC_TRUE;
449     p_subpic->b_pausable = VLC_FALSE;
450     p_subpic->b_fade     = VLC_FALSE;
451     p_subpic->i_alpha    = 0xFF;
452     p_subpic->p_region   = NULL;
453     p_subpic->pf_render  = NULL;
454     p_subpic->pf_destroy = NULL;
455     p_subpic->p_sys      = NULL;
456     vlc_mutex_unlock( &p_spu->subpicture_lock );
457
458     p_subpic->pf_create_region = __spu_CreateRegion;
459     p_subpic->pf_make_region = __spu_MakeRegion;
460     p_subpic->pf_destroy_region = __spu_DestroyRegion;
461
462     return p_subpic;
463 }
464
465 /**
466  * Remove a subpicture from the heap
467  *
468  * This function frees a previously reserved subpicture.
469  * It is meant to be used when the construction of a picture aborted.
470  * This function does not need locking since reserved subpictures are ignored
471  * by the spu.
472  */
473 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
474 {
475     /* Get lock */
476     vlc_mutex_lock( &p_spu->subpicture_lock );
477
478     /* There can be race conditions so we need to check the status */
479     if( p_subpic->i_status == FREE_SUBPICTURE )
480     {
481         vlc_mutex_unlock( &p_spu->subpicture_lock );
482         return;
483     }
484
485     /* Check if status is valid */
486     if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
487            && ( p_subpic->i_status != READY_SUBPICTURE ) )
488     {
489         msg_Err( p_spu, "subpicture %p has invalid status %d",
490                          p_subpic, p_subpic->i_status );
491     }
492
493     while( p_subpic->p_region )
494     {
495         subpicture_region_t *p_region = p_subpic->p_region;
496         p_subpic->p_region = p_region->p_next;
497         spu_DestroyRegion( p_spu, p_region );
498     }
499
500     if( p_subpic->pf_destroy )
501     {
502         p_subpic->pf_destroy( p_subpic );
503     }
504
505     p_subpic->i_status = FREE_SUBPICTURE;
506
507     vlc_mutex_unlock( &p_spu->subpicture_lock );
508 }
509
510 /*****************************************************************************
511  * spu_RenderSubpictures: render a subpicture list
512  *****************************************************************************
513  * This function renders all sub picture units in the list.
514  *****************************************************************************/
515 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
516                             picture_t *p_pic_dst, picture_t *p_pic_src,
517                             subpicture_t *p_subpic,
518                             int i_scale_width_orig, int i_scale_height_orig )
519 {
520     int i_source_video_width;
521     int i_source_video_height;
522
523     /* Get lock */
524     vlc_mutex_lock( &p_spu->subpicture_lock );
525
526     if( i_scale_width_orig <= 0 )
527         i_scale_width_orig = 1;
528     if( i_scale_height_orig <= 0 )
529         i_scale_height_orig = 1;
530
531     i_source_video_width  = p_fmt->i_width  * 1000 / i_scale_width_orig;
532     i_source_video_height = p_fmt->i_height * 1000 / i_scale_height_orig;
533
534     /* Check i_status again to make sure spudec hasn't destroyed the subpic */
535     while( ( p_subpic != NULL ) && ( p_subpic->i_status != FREE_SUBPICTURE ) )
536     {
537         subpicture_region_t *p_region = p_subpic->p_region;
538         int pi_scale_width[ SCALE_SIZE ];
539         int pi_scale_height[ SCALE_SIZE ];
540         int pi_subpic_x[ SCALE_SIZE ];
541         int k;
542
543         /* If the source video and subtitles stream agree on the size of
544          * the video then disregard all further references to the subtitle
545          * stream.
546          */
547         if( ( i_source_video_height == p_subpic->i_original_picture_height ) &&
548             ( i_source_video_width  == p_subpic->i_original_picture_width ) )
549         {
550             p_subpic->i_original_picture_height = 0;
551             p_subpic->i_original_picture_width = 0;
552         }
553
554         for( k = 0; k < SCALE_SIZE ; k++ )
555             pi_subpic_x[ k ] = p_subpic->i_x;
556
557         /* Load the blending module */
558         if( !p_spu->p_blend && p_region )
559         {
560             p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
561             vlc_object_attach( p_spu->p_blend, p_spu );
562             p_spu->p_blend->fmt_out.video.i_x_offset =
563                 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
564             p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
565             p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
566
567             /* The blend module will be loaded when needed with the real
568             * input format */
569             memset( &p_spu->p_blend->fmt_in, 0, sizeof(p_spu->p_blend->fmt_in) );
570             p_spu->p_blend->p_module = NULL;
571         }
572
573         /* Load the text rendering module; it is possible there is a
574          * text region somewhere in the subpicture other than the first
575          * element in the region list, so just load it anyway as we'll
576          * probably want it sooner or later. */
577         if( !p_spu->p_text && p_region )
578         {
579             char *psz_modulename = NULL;
580
581             p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
582             vlc_object_attach( p_spu->p_text, p_spu );
583
584             p_spu->p_text->fmt_out.video.i_width =
585                 p_spu->p_text->fmt_out.video.i_visible_width =
586                 p_fmt->i_width;
587             p_spu->p_text->fmt_out.video.i_height =
588                 p_spu->p_text->fmt_out.video.i_visible_height =
589                 p_fmt->i_height;
590
591             p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
592             p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
593
594             psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
595             if( psz_modulename && *psz_modulename )
596             {
597                 p_spu->p_text->p_module =
598                     module_Need( p_spu->p_text, "text renderer",
599                                  psz_modulename, VLC_TRUE );
600             }
601             if( !p_spu->p_text->p_module )
602             {
603                 p_spu->p_text->p_module =
604                     module_Need( p_spu->p_text, "text renderer", 0, 0 );
605             }
606             if( psz_modulename ) free( psz_modulename );
607         }
608
609         if( p_spu->p_text )
610         {
611             subpicture_region_t *p_text_region = p_subpic->p_region;
612
613             /* Only overwrite the size fields if the region is still in
614              * pre-rendered TEXT format. We have to traverse the subregion
615              * list because if more than one subregion is present, the text
616              * region isn't guarentteed to be the first in the list, and
617              * only text regions use this flag. All of this effort assists
618              * with the rescaling of text that has been rendered at native
619              * resolution, rather than video resolution.
620              */
621             while( p_text_region &&
622                    ( p_text_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
623             {
624                 p_text_region = p_text_region->p_next;
625             }
626
627             if( p_text_region &&
628                 ( ( p_text_region->i_align & SUBPICTURE_RENDERED ) == 0 ) )
629             {
630                 if( (p_subpic->i_original_picture_height > 0) &&
631                     (p_subpic->i_original_picture_width  > 0) )
632                 {
633                     p_spu->p_text->fmt_out.video.i_width =
634                         p_spu->p_text->fmt_out.video.i_visible_width =
635                         p_subpic->i_original_picture_width;
636                     p_spu->p_text->fmt_out.video.i_height =
637                         p_spu->p_text->fmt_out.video.i_visible_height =
638                         p_subpic->i_original_picture_height;
639                 }
640                 else
641                 {
642                     p_spu->p_text->fmt_out.video.i_width =
643                         p_spu->p_text->fmt_out.video.i_visible_width =
644                         p_fmt->i_width;
645                     p_spu->p_text->fmt_out.video.i_height =
646                         p_spu->p_text->fmt_out.video.i_visible_height =
647                         p_fmt->i_height;
648                 }
649             }
650         }
651
652         pi_scale_width[ SCALE_DEFAULT ]  = i_scale_width_orig;
653         pi_scale_height[ SCALE_DEFAULT ] = i_scale_height_orig;
654
655         if( p_spu->p_text )
656         {
657             pi_scale_width[ SCALE_TEXT ]     = ( p_fmt->i_width * 1000 ) /
658                                           p_spu->p_text->fmt_out.video.i_width;
659             pi_scale_height[ SCALE_TEXT ]    = ( p_fmt->i_height * 1000 ) /
660                                           p_spu->p_text->fmt_out.video.i_height;
661         }
662         /* If we have an explicit size plane to render to, then turn off
663          * the fontsize rescaling.
664          */
665         if( (p_subpic->i_original_picture_height > 0) &&
666             (p_subpic->i_original_picture_width  > 0) )
667         {
668             i_scale_width_orig  = 1000;
669             i_scale_height_orig = 1000;
670         }
671
672         for( k = 0; k < SCALE_SIZE ; k++ )
673         {
674             /* Case of both width and height being specified has been dealt
675              * with above by instead rendering to an output pane of the
676              * explicit dimensions specified - we don't need to scale it.
677              */
678             if( (p_subpic->i_original_picture_height > 0) &&
679                 (p_subpic->i_original_picture_width <= 0) )
680             {
681                 pi_scale_height[ k ] = pi_scale_height[ k ] * i_source_video_height /
682                                  p_subpic->i_original_picture_height;
683                 pi_scale_width[ k ]  = pi_scale_width[ k ]  * i_source_video_height /
684                                  p_subpic->i_original_picture_height;
685             }
686         }
687
688         /* Set default subpicture aspect ratio */
689         if( p_region && p_region->fmt.i_aspect &&
690             ( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den ) )
691         {
692             p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
693             p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
694         }
695         if( p_region &&
696             ( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den ) )
697         {
698             p_region->fmt.i_sar_den = p_fmt->i_sar_den;
699             p_region->fmt.i_sar_num = p_fmt->i_sar_num;
700         }
701
702         /* Take care of the aspect ratio */
703         if( p_region &&
704             ( ( p_region->fmt.i_sar_num * p_fmt->i_sar_den ) !=
705               ( p_region->fmt.i_sar_den * p_fmt->i_sar_num ) ) )
706         {
707             for( k = 0; k < SCALE_SIZE ; k++ )
708             {
709                 pi_scale_width[ k ] = pi_scale_width[ k ] *
710                     (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
711                     p_region->fmt.i_sar_den / p_fmt->i_sar_num;
712                 pi_subpic_x[ k ] = p_subpic->i_x * pi_scale_width[ k ] / 1000;
713             }
714         }
715
716         /* Load the scaling module */
717         if( !p_spu->p_scale &&
718            ((((pi_scale_width[ SCALE_TEXT ]    > 0)     || (pi_scale_height[ SCALE_TEXT ]    > 0)) &&
719              ((pi_scale_width[ SCALE_TEXT ]    != 1000) || (pi_scale_height[ SCALE_TEXT ]    != 1000))) ||
720             (((pi_scale_width[ SCALE_DEFAULT ] > 0)     || (pi_scale_height[ SCALE_DEFAULT ] > 0)) &&
721              ((pi_scale_width[ SCALE_DEFAULT ] != 1000) || (pi_scale_height[ SCALE_DEFAULT ] != 1000)))) )
722         {
723             p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
724             vlc_object_attach( p_spu->p_scale, p_spu );
725             p_spu->p_scale->fmt_out.video.i_chroma =
726                 p_spu->p_scale->fmt_in.video.i_chroma =
727                     VLC_FOURCC('Y','U','V','P');
728             /* FIXME: We'll also be using it for YUVA and RGBA blending ... */
729
730             p_spu->p_scale->fmt_in.video.i_width =
731                 p_spu->p_scale->fmt_in.video.i_height = 32;
732             p_spu->p_scale->fmt_out.video.i_width =
733                 p_spu->p_scale->fmt_out.video.i_height = 16;
734
735             p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
736             p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
737             p_spu->p_scale->p_module =
738                 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
739         }
740
741         while( p_region )
742         {
743             video_format_t orig_fmt = p_region->fmt;
744             vlc_bool_t b_rerender_text = VLC_FALSE;
745             int i_fade_alpha = 255;
746             int i_x_offset;
747             int i_y_offset;
748             int i_scale_idx   = SCALE_DEFAULT;
749             int i_inv_scale_x = 1000;
750             int i_inv_scale_y = 1000;
751
752             if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
753             {
754                 if( p_spu->p_text && p_spu->p_text->p_module )
755                 {
756                     vlc_value_t  val;
757
758                     /* Setup 3 variables which can be used to render
759                      * time-dependent text (and effects). The first indicates
760                      * the total amount of time the text will be on screen,
761                      * the second the amount of time it has already been on
762                      * screen (can be a negative value as text is layed out
763                      * before it is rendered) and the third is a feedback
764                      * variable from the renderer - if the renderer sets it
765                      * then this particular text is time-dependent, eg. the
766                      * visual progress bar inside the text in karaoke and the
767                      * text needs to be rendered multiple times in order for
768                      * the effect to work - we therefore need to return the
769                      * region to its original state at the end of the loop,
770                      * instead of leaving it in YUVA or YUVP.
771                      * Any renderer which is unaware of how to render
772                      * time-dependent text can happily ignore the variables
773                      * and render the text the same as usual - it should at
774                      * least show up on screen, but the effect won't change
775                      * the text over time.
776                      */
777
778                     var_Create( p_spu->p_text, "spu-duration", VLC_VAR_TIME );
779                     val.i_time = p_subpic->i_stop - p_subpic->i_start;
780                     var_Set( p_spu->p_text, "spu-duration", val );
781
782                     var_Create( p_spu->p_text, "spu-elapsed", VLC_VAR_TIME );
783                     val.i_time = mdate() - p_subpic->i_start;
784                     var_Set( p_spu->p_text, "spu-elapsed", val );
785
786                     var_Create( p_spu->p_text, "text-rerender", VLC_VAR_BOOL );
787                     var_SetBool( p_spu->p_text, "text-rerender", VLC_FALSE );
788
789                     var_Create( p_spu->p_text, "scale", VLC_VAR_INTEGER );
790                     var_SetInteger( p_spu->p_text, "scale",
791                               __MIN(i_scale_width_orig, i_scale_height_orig) );
792
793                     if( p_spu->p_text->pf_render_html && p_region->psz_html )
794                     {
795                         p_spu->p_text->pf_render_html( p_spu->p_text,
796                                                        p_region, p_region );
797                     }
798                     else if( p_spu->p_text->pf_render_text )
799                     {
800                         p_spu->p_text->pf_render_text( p_spu->p_text,
801                                                        p_region, p_region );
802                     }
803                     b_rerender_text = var_GetBool( p_spu->p_text, "text-rerender" );
804
805                     var_Destroy( p_spu->p_text, "spu-duration" );
806                     var_Destroy( p_spu->p_text, "spu-elapsed" );
807                     var_Destroy( p_spu->p_text, "text-rerender" );
808                     var_Destroy( p_spu->p_text, "scale" );
809                 }
810                 p_region->i_align |= SUBPICTURE_RENDERED;
811             }
812
813             if( p_region->i_align & SUBPICTURE_RENDERED )
814             {
815                 i_scale_idx   = SCALE_TEXT;
816                 i_inv_scale_x = i_scale_width_orig;
817                 i_inv_scale_y = i_scale_height_orig;
818             }
819
820             i_x_offset = (p_region->i_x + pi_subpic_x[ i_scale_idx ]) * i_inv_scale_x / 1000;
821             i_y_offset = (p_region->i_y + p_subpic->i_y) * i_inv_scale_y / 1000;
822
823             /* Force palette if requested */
824             if( p_spu->b_force_palette &&
825                 ( VLC_FOURCC('Y','U','V','P') == p_region->fmt.i_chroma ) )
826             {
827                 memcpy( p_region->fmt.p_palette->palette,
828                         p_spu->palette, 16 );
829             }
830
831             /* Scale SPU if necessary */
832             if( p_region->p_cache &&
833                 ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
834             {
835                 if( pi_scale_width[ i_scale_idx ] * p_region->fmt.i_width / 1000 !=
836                     p_region->p_cache->fmt.i_width ||
837                     pi_scale_height[ i_scale_idx ] * p_region->fmt.i_height / 1000 !=
838                     p_region->p_cache->fmt.i_height )
839                 {
840                     p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
841                                                  p_region->p_cache );
842                     p_region->p_cache = 0;
843                 }
844             }
845
846             if( ( ( pi_scale_width[ i_scale_idx ] != 1000 ) ||
847                   ( pi_scale_height[ i_scale_idx ] != 1000 ) ) &&
848                 ( ( pi_scale_width[ i_scale_idx ] > 0 ) ||
849                   ( pi_scale_height[ i_scale_idx ] > 0 ) ) &&
850                 p_spu->p_scale && !p_region->p_cache &&
851                 ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
852             {
853                 picture_t *p_pic;
854
855                 p_spu->p_scale->fmt_in.video = p_region->fmt;
856                 p_spu->p_scale->fmt_out.video = p_region->fmt;
857
858                 p_region->p_cache =
859                     p_subpic->pf_create_region( VLC_OBJECT(p_spu),
860                         &p_spu->p_scale->fmt_out.video );
861                 if( p_spu->p_scale->fmt_out.video.p_palette )
862                     *p_spu->p_scale->fmt_out.video.p_palette =
863                         *p_region->fmt.p_palette;
864                 p_region->p_cache->p_next = p_region->p_next;
865
866                 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
867                                   &p_region->picture );
868
869                 p_spu->p_scale->fmt_out.video.i_width =
870                     p_region->fmt.i_width * pi_scale_width[ i_scale_idx ] / 1000;
871                 p_spu->p_scale->fmt_out.video.i_visible_width =
872                     p_region->fmt.i_visible_width * pi_scale_width[ i_scale_idx ] / 1000;
873                 p_spu->p_scale->fmt_out.video.i_height =
874                     p_region->fmt.i_height * pi_scale_height[ i_scale_idx ] / 1000;
875                 p_spu->p_scale->fmt_out.video.i_visible_height =
876                     p_region->fmt.i_visible_height * pi_scale_height[ i_scale_idx ] / 1000;
877                 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
878                 p_region->p_cache->i_x = p_region->i_x * pi_scale_width[ i_scale_idx ] / 1000;
879                 p_region->p_cache->i_y = p_region->i_y * pi_scale_height[ i_scale_idx ] / 1000;
880                 p_region->p_cache->i_align = p_region->i_align;
881
882                 p_pic = p_spu->p_scale->pf_video_filter(
883                                  p_spu->p_scale, &p_region->p_cache->picture );
884                 if( p_pic )
885                 {
886                     picture_t p_pic_tmp = p_region->p_cache->picture;
887                     p_region->p_cache->picture = *p_pic;
888                     *p_pic = p_pic_tmp;
889                     free( p_pic );
890                 }
891             }
892
893             if( ( ( pi_scale_width[ i_scale_idx ] != 1000 ) ||
894                   ( pi_scale_height[ i_scale_idx ] != 1000 ) ) &&
895                 ( ( pi_scale_width[ i_scale_idx ] > 0 ) ||
896                   ( pi_scale_height[ i_scale_idx ] > 0 ) ) &&
897                 p_spu->p_scale && p_region->p_cache &&
898                 ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') )  )
899             {
900                 p_region = p_region->p_cache;
901             }
902
903             if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
904             {
905                 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
906                     (p_subpic->i_y + p_region->i_y) * i_inv_scale_y / 1000;
907             }
908             else if ( !(p_region->i_align & SUBPICTURE_ALIGN_TOP) )
909             {
910                 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
911             }
912
913             if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
914             {
915                 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
916                     (pi_subpic_x[ i_scale_idx ] + p_region->i_x)
917                     * i_inv_scale_x / 1000;
918             }
919             else if ( !(p_region->i_align & SUBPICTURE_ALIGN_LEFT) )
920             {
921                 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
922             }
923
924             if( p_subpic->b_absolute )
925             {
926                 i_x_offset = (p_region->i_x +
927                     pi_subpic_x[ i_scale_idx ] *
928                                      pi_scale_width[ i_scale_idx ] / 1000)
929                     * i_inv_scale_x / 1000;
930                 i_y_offset = (p_region->i_y +
931                     p_subpic->i_y * pi_scale_height[ i_scale_idx ] / 1000)
932                     * i_inv_scale_y / 1000;
933
934             }
935
936             i_x_offset = __MAX( i_x_offset, 0 );
937             i_y_offset = __MAX( i_y_offset, 0 );
938
939             if( ( p_spu->i_margin != 0 ) &&
940                 ( p_spu->b_force_crop == VLC_FALSE ) )
941             {
942                 int i_diff = 0;
943                 int i_low = (i_y_offset - p_spu->i_margin) * i_inv_scale_y / 1000;
944                 int i_high = i_low + p_region->fmt.i_height;
945
946                 /* crop extra margin to keep within bounds */
947                 if( i_low < 0 )
948                     i_diff = i_low;
949                 if( i_high > (int)p_fmt->i_height )
950                     i_diff = i_high - p_fmt->i_height;
951                 i_y_offset -= ( p_spu->i_margin * i_inv_scale_y / 1000 + i_diff );
952             }
953
954             if( p_subpic->b_fade )
955             {
956                 mtime_t i_fade_start = ( p_subpic->i_stop +
957                                          p_subpic->i_start ) / 2;
958                 mtime_t i_now = mdate();
959                 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
960                 {
961                     i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
962                                    ( p_subpic->i_stop - i_fade_start );
963                 }
964             }
965
966             if( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') )
967             {
968                 if( p_spu->p_blend->fmt_in.video.i_chroma != p_region->fmt.i_chroma )
969                 {
970                     /* The chroma is not the same, we need to reload the blend module
971                      * XXX to match the old behaviour just test !p_spu->p_blend->fmt_in.video.i_chroma */
972                     if( p_spu->p_blend->p_module )
973                         module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
974
975                     p_spu->p_blend->fmt_in.video = p_region->fmt;
976                     p_spu->p_blend->p_module = module_Need( p_spu->p_blend, "video blending", 0, 0 );
977                 }
978                 else
979                 {
980                     p_spu->p_blend->fmt_in.video = p_region->fmt;
981                 }
982
983                 /* Force cropping if requested */
984                 if( p_spu->b_force_crop )
985                 {
986                     video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
987                     int i_crop_x = p_spu->i_crop_x * pi_scale_width[ i_scale_idx ] / 1000
988                                         * i_inv_scale_x / 1000;
989                     int i_crop_y = p_spu->i_crop_y * pi_scale_height[ i_scale_idx ] / 1000
990                                         * i_inv_scale_y / 1000;
991                     int i_crop_width = p_spu->i_crop_width * pi_scale_width[ i_scale_idx ] / 1000
992                                         * i_inv_scale_x / 1000;
993                     int i_crop_height = p_spu->i_crop_height * pi_scale_height[ i_scale_idx ] / 1000
994                                         * i_inv_scale_y / 1000;
995
996                     /* Find the intersection */
997                     if( i_crop_x + i_crop_width <= i_x_offset ||
998                         i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
999                         i_crop_y + i_crop_height <= i_y_offset ||
1000                         i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
1001                     {
1002                         /* No intersection */
1003                         p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
1004                     }
1005                     else
1006                     {
1007                         int i_x, i_y, i_x_end, i_y_end;
1008                         i_x = __MAX( i_crop_x, i_x_offset );
1009                         i_y = __MAX( i_crop_y, i_y_offset );
1010                         i_x_end = __MIN( i_crop_x + i_crop_width,
1011                                        i_x_offset + (int)p_fmt->i_visible_width );
1012                         i_y_end = __MIN( i_crop_y + i_crop_height,
1013                                        i_y_offset + (int)p_fmt->i_visible_height );
1014
1015                         p_fmt->i_x_offset = i_x - i_x_offset;
1016                         p_fmt->i_y_offset = i_y - i_y_offset;
1017                         p_fmt->i_visible_width = i_x_end - i_x;
1018                         p_fmt->i_visible_height = i_y_end - i_y;
1019
1020                         i_x_offset = i_x;
1021                         i_y_offset = i_y;
1022                     }
1023                 }
1024
1025                 i_x_offset = __MAX( i_x_offset, 0 );
1026                 i_y_offset = __MAX( i_y_offset, 0 );
1027
1028                 /* Update the output picture size */
1029                 p_spu->p_blend->fmt_out.video.i_width =
1030                     p_spu->p_blend->fmt_out.video.i_visible_width =
1031                         p_fmt->i_width;
1032                 p_spu->p_blend->fmt_out.video.i_height =
1033                     p_spu->p_blend->fmt_out.video.i_visible_height =
1034                         p_fmt->i_height;
1035
1036                 if( p_spu->p_blend->p_module )
1037                 {
1038                     p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
1039                         p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
1040                         i_fade_alpha * p_subpic->i_alpha / 255 );
1041                 }
1042                 else
1043                 {
1044                     msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1045                              (char *)&p_spu->p_blend->fmt_out.video.i_chroma,
1046                              (char *)&p_spu->p_blend->fmt_out.video.i_chroma );
1047                 }
1048             }
1049
1050             if( b_rerender_text )
1051             {
1052                 /* Some forms of subtitles need to be re-rendered more than
1053                  * once, eg. karaoke. We therefore restore the region to its
1054                  * pre-rendered state, so the next time through everything is
1055                  * calculated again.
1056                  */
1057                 p_region->picture.pf_release( &p_region->picture );
1058                 memset( &p_region->picture, 0, sizeof( picture_t ) );
1059                 p_region->fmt = orig_fmt;
1060                 p_region->i_align &= ~SUBPICTURE_RENDERED;
1061             }
1062             p_region = p_region->p_next;
1063         }
1064
1065         p_subpic = p_subpic->p_next;
1066     }
1067
1068     vlc_mutex_unlock( &p_spu->subpicture_lock );
1069 }
1070
1071 /*****************************************************************************
1072  * spu_SortSubpictures: find the subpictures to display
1073  *****************************************************************************
1074  * This function parses all subpictures and decides which ones need to be
1075  * displayed. This operation does not need lock, since only READY_SUBPICTURE
1076  * are handled. If no picture has been selected, display_date will depend on
1077  * the subpicture.
1078  * We also check for ephemer DVD subpictures (subpictures that have
1079  * to be removed if a newer one is available), which makes it a lot
1080  * more difficult to guess if a subpicture has to be rendered or not.
1081  *****************************************************************************/
1082 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1083                                    vlc_bool_t b_paused )
1084 {
1085     int i_index, i_channel;
1086     subpicture_t *p_subpic = NULL;
1087     subpicture_t *p_ephemer;
1088     mtime_t      ephemer_date;
1089
1090     /* Run subpicture filters */
1091     for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
1092     {
1093         subpicture_t *p_subpic_filter;
1094         p_subpic_filter = p_spu->pp_filter[i_index]->
1095             pf_sub_filter( p_spu->pp_filter[i_index], display_date );
1096         if( p_subpic_filter )
1097         {
1098             spu_DisplaySubpicture( p_spu, p_subpic_filter );
1099         }
1100     }
1101
1102     /* We get an easily parsable chained list of subpictures which
1103      * ends with NULL since p_subpic was initialized to NULL. */
1104     for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
1105     {
1106         p_ephemer = 0;
1107         ephemer_date = 0;
1108
1109         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1110         {
1111             if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
1112                 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
1113             {
1114                 continue;
1115             }
1116             if( display_date &&
1117                 display_date < p_spu->p_subpicture[i_index].i_start )
1118             {
1119                 /* Too early, come back next monday */
1120                 continue;
1121             }
1122
1123             if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
1124                 ephemer_date = p_spu->p_subpicture[i_index].i_start;
1125
1126             if( display_date > p_spu->p_subpicture[i_index].i_stop &&
1127                 ( !p_spu->p_subpicture[i_index].b_ephemer ||
1128                   p_spu->p_subpicture[i_index].i_stop >
1129                   p_spu->p_subpicture[i_index].i_start ) &&
1130                 !( p_spu->p_subpicture[i_index].b_pausable &&
1131                    b_paused ) )
1132             {
1133                 /* Too late, destroy the subpic */
1134                 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
1135                 continue;
1136             }
1137
1138             /* If this is an ephemer subpic, add it to our list */
1139             if( p_spu->p_subpicture[i_index].b_ephemer )
1140             {
1141                 p_spu->p_subpicture[i_index].p_next = p_ephemer;
1142                 p_ephemer = &p_spu->p_subpicture[i_index];
1143
1144                 continue;
1145             }
1146
1147             p_spu->p_subpicture[i_index].p_next = p_subpic;
1148             p_subpic = &p_spu->p_subpicture[i_index];
1149         }
1150
1151         /* If we found ephemer subpictures, check if they have to be
1152          * displayed or destroyed */
1153         while( p_ephemer != NULL )
1154         {
1155             subpicture_t *p_tmp = p_ephemer;
1156             p_ephemer = p_ephemer->p_next;
1157
1158             if( p_tmp->i_start < ephemer_date )
1159             {
1160                 /* Ephemer subpicture has lived too long */
1161                 spu_DestroySubpicture( p_spu, p_tmp );
1162             }
1163             else
1164             {
1165                 /* Ephemer subpicture can still live a bit */
1166                 p_tmp->p_next = p_subpic;
1167                 p_subpic = p_tmp;
1168             }
1169         }
1170     }
1171
1172     return p_subpic;
1173 }
1174
1175 /*****************************************************************************
1176  * SpuClearChannel: clear an spu channel
1177  *****************************************************************************
1178  * This function destroys the subpictures which belong to the spu channel
1179  * corresponding to i_channel_id.
1180  *****************************************************************************/
1181 static void SpuClearChannel( spu_t *p_spu, int i_channel )
1182 {
1183     int          i_subpic;                               /* subpicture index */
1184     subpicture_t *p_subpic = NULL;                  /* first free subpicture */
1185
1186     vlc_mutex_lock( &p_spu->subpicture_lock );
1187
1188     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1189     {
1190         p_subpic = &p_spu->p_subpicture[i_subpic];
1191         if( p_subpic->i_status == FREE_SUBPICTURE
1192             || ( p_subpic->i_status != RESERVED_SUBPICTURE
1193                  && p_subpic->i_status != READY_SUBPICTURE ) )
1194         {
1195             continue;
1196         }
1197
1198         if( p_subpic->i_channel == i_channel )
1199         {
1200             while( p_subpic->p_region )
1201             {
1202                 subpicture_region_t *p_region = p_subpic->p_region;
1203                 p_subpic->p_region = p_region->p_next;
1204                 spu_DestroyRegion( p_spu, p_region );
1205             }
1206
1207             if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1208             p_subpic->i_status = FREE_SUBPICTURE;
1209         }
1210     }
1211
1212     vlc_mutex_unlock( &p_spu->subpicture_lock );
1213 }
1214
1215 /*****************************************************************************
1216  * spu_ControlDefault: default methods for the subpicture unit control.
1217  *****************************************************************************/
1218 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1219 {
1220     int *pi, i;
1221
1222     switch( i_query )
1223     {
1224     case SPU_CHANNEL_REGISTER:
1225         pi = (int *)va_arg( args, int * );
1226         if( pi ) *pi = p_spu->i_channel++;
1227         break;
1228
1229     case SPU_CHANNEL_CLEAR:
1230         i = (int)va_arg( args, int );
1231         SpuClearChannel( p_spu, i );
1232         break;
1233
1234     default:
1235         msg_Dbg( p_spu, "control query not supported" );
1236         return VLC_EGENERIC;
1237     }
1238
1239     return VLC_SUCCESS;
1240 }
1241
1242 /*****************************************************************************
1243  * Object variables callbacks
1244  *****************************************************************************/
1245
1246 /*****************************************************************************
1247  * UpdateSPU: update subpicture settings
1248  *****************************************************************************
1249  * This function is called from CropCallback and at initialization time, to
1250  * retrieve crop information from the input.
1251  *****************************************************************************/
1252 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1253 {
1254     vlc_value_t val;
1255
1256     p_spu->b_force_palette = VLC_FALSE;
1257     p_spu->b_force_crop = VLC_FALSE;
1258
1259     if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1260
1261     p_spu->b_force_crop = VLC_TRUE;
1262     var_Get( p_object, "x-start", &val );
1263     p_spu->i_crop_x = val.i_int;
1264     var_Get( p_object, "y-start", &val );
1265     p_spu->i_crop_y = val.i_int;
1266     var_Get( p_object, "x-end", &val );
1267     p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1268     var_Get( p_object, "y-end", &val );
1269     p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1270
1271     if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1272     {
1273         memcpy( p_spu->palette, val.p_address, 16 );
1274         p_spu->b_force_palette = VLC_TRUE;
1275     }
1276
1277     msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1278              p_spu->i_crop_x, p_spu->i_crop_y,
1279              p_spu->i_crop_width, p_spu->i_crop_height,
1280              p_spu->b_force_palette );
1281 }
1282
1283 /*****************************************************************************
1284  * CropCallback: called when the highlight properties are changed
1285  *****************************************************************************
1286  * This callback is called from the input thread when we need cropping
1287  *****************************************************************************/
1288 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1289                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1290 {
1291     (void)psz_var; (void)oldval; (void)newval;
1292     UpdateSPU( (spu_t *)p_data, p_object );
1293     return VLC_SUCCESS;
1294 }
1295
1296 /*****************************************************************************
1297  * Buffers allocation callbacks for the filters
1298  *****************************************************************************/
1299 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1300 {
1301     filter_owner_sys_t *p_sys = p_filter->p_owner;
1302     subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1303     if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1304     return p_subpicture;
1305 }
1306
1307 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1308 {
1309     filter_owner_sys_t *p_sys = p_filter->p_owner;
1310     spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1311 }
1312
1313 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1314 {
1315     (void)p_filter;
1316     subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1317     if( !p_subpic ) return NULL;
1318     memset( p_subpic, 0, sizeof(subpicture_t) );
1319     p_subpic->b_absolute = VLC_TRUE;
1320
1321     p_subpic->pf_create_region = __spu_CreateRegion;
1322     p_subpic->pf_make_region = __spu_MakeRegion;
1323     p_subpic->pf_destroy_region = __spu_DestroyRegion;
1324
1325     return p_subpic;
1326 }
1327
1328 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1329 {
1330     while( p_subpic->p_region )
1331     {
1332         subpicture_region_t *p_region = p_subpic->p_region;
1333         p_subpic->p_region = p_region->p_next;
1334         p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1335     }
1336
1337     free( p_subpic );
1338 }
1339
1340 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1341 {
1342     picture_t *p_picture = malloc( sizeof(picture_t) );
1343     if( !p_picture ) return NULL;
1344     if( vout_AllocatePicture( p_filter, p_picture,
1345                               p_filter->fmt_out.video.i_chroma,
1346                               p_filter->fmt_out.video.i_width,
1347                               p_filter->fmt_out.video.i_height,
1348                               p_filter->fmt_out.video.i_aspect )
1349         != VLC_SUCCESS )
1350     {
1351         free( p_picture );
1352         return NULL;
1353     }
1354
1355     p_picture->pf_release = RegionPictureRelease;
1356
1357     return p_picture;
1358 }
1359
1360 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1361 {
1362     (void)p_filter;
1363     if( p_pic )
1364     {
1365         if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
1366         free( p_pic );
1367     }
1368 }
1369
1370 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1371                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1372 {
1373     (void)p_object; (void)oldval; (void)newval;
1374
1375     spu_t *p_spu = (spu_t *)p_data;
1376     vlc_mutex_lock( &p_spu->subpicture_lock );
1377     spu_DeleteChain( p_spu );
1378     spu_ParseChain( p_spu );
1379     vlc_mutex_unlock( &p_spu->subpicture_lock );
1380     return VLC_SUCCESS;
1381 }