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