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