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