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