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