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