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