]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
* src/video_output/vout_subpictures.c: modify the scaling factor based on the origina...
[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_orig, int i_scale_height_orig )
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         int i_scale_width, i_scale_height;
451
452         /* Load the blending module */
453         if( !p_spu->p_blend && p_region )
454         {
455             p_spu->p_blend = vlc_object_create( p_spu, sizeof(filter_t) );
456             vlc_object_attach( p_spu->p_blend, p_spu );
457             p_spu->p_blend->fmt_out.video.i_x_offset =
458                 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
459             p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
460             p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
461
462             p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
463
464             p_spu->p_blend->p_module =
465                 module_Need( p_spu->p_blend, "video blending", 0, 0 );
466         }
467
468         /* Load the text rendering module */
469         if( !p_spu->p_text && p_region )
470         {
471             p_spu->p_text = vlc_object_create( p_spu, sizeof(filter_t) );
472             vlc_object_attach( p_spu->p_text, p_spu );
473
474             p_spu->p_text->fmt_out.video.i_width =
475                 p_spu->p_text->fmt_out.video.i_visible_width =
476                     p_fmt->i_width;
477             p_spu->p_text->fmt_out.video.i_height =
478                 p_spu->p_text->fmt_out.video.i_visible_height =
479                     p_fmt->i_height;
480
481             p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
482             p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
483
484             p_spu->p_text->p_module =
485                 module_Need( p_spu->p_text, "text renderer", 0, 0 );
486         }
487
488         i_scale_width = i_scale_width_orig;
489         i_scale_height = i_scale_height_orig;
490
491         if( p_subpic->i_original_picture_width ||
492             p_subpic->i_original_picture_height )
493         {
494             i_scale_width = i_scale_width * p_fmt->i_width /
495                 p_subpic->i_original_picture_width;
496             i_scale_height = i_scale_height * p_fmt->i_height /
497                 p_subpic->i_original_picture_height;
498         }
499
500         /* Load the scaling module */
501         if( !p_spu->p_scale && (i_scale_width != 1000 ||
502             i_scale_height != 1000) )
503         {
504             p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
505             vlc_object_attach( p_spu->p_scale, p_spu );
506             p_spu->p_scale->fmt_out.video.i_chroma =
507                 p_spu->p_scale->fmt_in.video.i_chroma =
508                     VLC_FOURCC('Y','U','V','P');
509             p_spu->p_scale->fmt_in.video.i_width =
510                 p_spu->p_scale->fmt_in.video.i_height = 32;
511             p_spu->p_scale->fmt_out.video.i_width =
512                 p_spu->p_scale->fmt_out.video.i_height = 16;
513
514             p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
515             p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
516             p_spu->p_scale->p_module =
517                 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
518         }
519
520         if( p_subpic->pf_render )
521         {
522             /* HACK to remove when the ogt subpic decoder is gone */
523             if( p_spu->p_parent &&
524                 p_spu->p_parent->i_object_type == VLC_OBJECT_VOUT )
525             {
526                 vout_thread_t *p_vout = (vout_thread_t *)p_spu->p_parent;
527                 p_subpic->pf_render( p_vout, p_pic_dst, p_subpic );
528             }
529         }
530         else while( p_region && p_spu->p_blend &&
531                     p_spu->p_blend->pf_video_blend )
532         {
533             int i_x_offset = p_region->i_x + p_subpic->i_x;
534             int i_y_offset = p_region->i_y + p_subpic->i_y;
535
536             if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
537             {
538                 if( p_spu->p_text && p_spu->p_text->p_module &&
539                     p_spu->p_text->pf_render_string )
540                 {
541                     /* TODO: do it in a less hacky way
542                      * (modify text renderer API) */
543                     subpicture_t *p_subpic_tmp;
544                     subpicture_region_t tmp_region;
545                     block_t *p_new_block =
546                         block_New( p_spu, strlen(p_region->psz_text) + 1 );
547
548                     if( p_new_block )
549                     {
550                         memcpy( p_new_block->p_buffer, p_region->psz_text,
551                                 p_new_block->i_buffer );
552                         p_new_block->i_pts = p_new_block->i_dts =
553                             p_subpic->i_start;
554                         p_new_block->i_length =
555                             p_subpic->i_start - p_subpic->i_stop;
556                         p_subpic_tmp = p_spu->p_text->pf_render_string(
557                             p_spu->p_text, p_new_block );
558
559                         if( p_subpic_tmp )
560                         {
561                             tmp_region = *p_region;
562                             *p_region = *p_subpic_tmp->p_region;
563                             p_region->p_next = tmp_region.p_next;
564                             *p_subpic_tmp->p_region = tmp_region;
565                             p_spu->p_text->pf_sub_buffer_del( p_spu->p_text,
566                                                               p_subpic_tmp );
567                         }
568                     }
569                 }
570             }
571
572             if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
573             {
574                 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
575                     p_subpic->i_y;
576             }
577             else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
578             {
579                 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
580             }
581
582             if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
583             {
584                 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
585                     p_subpic->i_x;
586             }
587             else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
588             {
589                 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
590             }
591
592             if( p_subpic->b_absolute )
593             {
594                 i_x_offset = p_region->i_x + p_subpic->i_x;
595                 i_y_offset = p_region->i_y + p_subpic->i_y;
596
597                 if( p_spu->i_margin >= 0 )
598                 {
599                     if( p_subpic->i_height + (unsigned int)p_spu->i_margin <=
600                         p_fmt->i_height )
601                     {
602                         i_y_offset = p_fmt->i_height -
603                             p_spu->i_margin - p_subpic->i_height;
604                     }
605                 }
606             }
607
608             /* Force cropping if requested */
609             if( p_spu->b_force_crop )
610             {
611                 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
612
613                 /* Find the intersection */
614                 if( p_spu->i_crop_x + p_spu->i_crop_width <= i_x_offset ||
615                     i_x_offset + (int)p_fmt->i_visible_width <
616                         p_spu->i_crop_x ||
617                     p_spu->i_crop_y + p_spu->i_crop_height <= i_y_offset ||
618                     i_y_offset + (int)p_fmt->i_visible_height <
619                         p_spu->i_crop_y )
620                 {
621                     /* No intersection */
622                     p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
623                 }
624                 else
625                 {
626                     int i_x, i_y, i_x_end, i_y_end;
627                     i_x = __MAX( p_spu->i_crop_x, i_x_offset );
628                     i_y = __MAX( p_spu->i_crop_y, i_y_offset );
629                     i_x_end = __MIN( p_spu->i_crop_x + p_spu->i_crop_width,
630                                    i_x_offset + (int)p_fmt->i_visible_width );
631                     i_y_end = __MIN( p_spu->i_crop_y + p_spu->i_crop_height,
632                                    i_y_offset + (int)p_fmt->i_visible_height );
633
634                     p_fmt->i_x_offset = i_x - i_x_offset;
635                     p_fmt->i_y_offset = i_y - i_y_offset;
636                     p_fmt->i_visible_width = i_x_end - i_x;
637                     p_fmt->i_visible_height = i_y_end - i_y;
638
639                     i_x_offset = i_x;
640                     i_y_offset = i_y;
641                 }
642             }
643
644             /* Force palette if requested */
645             if( p_spu->b_force_alpha && VLC_FOURCC('Y','U','V','P') ==
646                 p_spu->p_blend->fmt_in.video.i_chroma )
647             {
648                 p_spu->p_blend->fmt_in.video.p_palette->palette[0][3] =
649                     p_spu->pi_alpha[0];
650                 p_spu->p_blend->fmt_in.video.p_palette->palette[1][3] =
651                     p_spu->pi_alpha[1];
652                 p_spu->p_blend->fmt_in.video.p_palette->palette[2][3] =
653                     p_spu->pi_alpha[2];
654                 p_spu->p_blend->fmt_in.video.p_palette->palette[3][3] =
655                     p_spu->pi_alpha[3];
656             }
657
658             /* Scale SPU if necessary */
659             if( p_region->p_cache )
660             {
661                 if( i_scale_width * p_region->fmt.i_width / 1000 !=
662                     p_region->p_cache->fmt.i_width ||
663                     i_scale_height * p_region->fmt.i_height / 1000 !=
664                     p_region->p_cache->fmt.i_height )
665                 {
666                     p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
667                                                  p_region->p_cache );
668                     p_region->p_cache = 0;
669                 }
670             }
671             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
672                 p_spu->p_scale && !p_region->p_cache )
673             {
674                 picture_t *p_pic;
675
676                 p_spu->p_scale->fmt_in.video = p_region->fmt;
677                 p_spu->p_scale->fmt_out.video = p_region->fmt;
678
679                 p_region->p_cache =
680                     p_subpic->pf_create_region( VLC_OBJECT(p_spu),
681                         &p_spu->p_scale->fmt_out.video );
682                 if( p_spu->p_scale->fmt_out.video.p_palette )
683                     *p_spu->p_scale->fmt_out.video.p_palette =
684                         *p_region->fmt.p_palette;
685                 p_region->p_cache->p_next = p_region->p_next;
686
687                 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
688                                   &p_region->picture );
689
690                 p_spu->p_scale->fmt_out.video.i_width =
691                     p_region->fmt.i_width * i_scale_width / 1000;
692                 p_spu->p_scale->fmt_out.video.i_visible_width =
693                     p_region->fmt.i_visible_width * i_scale_width / 1000;
694                 p_spu->p_scale->fmt_out.video.i_height =
695                     p_region->fmt.i_height * i_scale_height / 1000;
696                 p_spu->p_scale->fmt_out.video.i_visible_height =
697                     p_region->fmt.i_visible_height * i_scale_height / 1000;
698                 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
699
700                 p_pic = p_spu->p_scale->pf_video_filter(
701                                  p_spu->p_scale, &p_region->p_cache->picture );
702                 if( p_pic )
703                 {
704                     picture_t p_pic_tmp = p_region->p_cache->picture;
705                     p_region->p_cache->picture = *p_pic;
706                     *p_pic = p_pic_tmp;
707                     free( p_pic );
708                 }
709             }
710             if( (i_scale_width != 1000 || i_scale_height != 1000) &&
711                 p_spu->p_scale && p_region->p_cache )
712             {
713                 p_region = p_region->p_cache;
714             }
715
716             if( p_subpic->b_absolute )
717             {
718                 i_x_offset = i_x_offset * i_scale_width / 1000;
719                 i_y_offset = i_y_offset * i_scale_height / 1000;
720             }
721
722             p_spu->p_blend->fmt_in.video = p_region->fmt;
723
724             /* Update the output picture size */
725             p_spu->p_blend->fmt_out.video.i_width =
726                 p_spu->p_blend->fmt_out.video.i_visible_width =
727                     p_fmt->i_width;
728             p_spu->p_blend->fmt_out.video.i_height =
729                 p_spu->p_blend->fmt_out.video.i_visible_height =
730                     p_fmt->i_height;
731
732            p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
733                 p_pic_src, &p_region->picture, i_x_offset, i_y_offset );
734
735             p_region = p_region->p_next;
736         }
737
738         p_subpic = p_subpic->p_next;
739     }
740
741     vlc_mutex_unlock( &p_spu->subpicture_lock );
742 }
743
744 /*****************************************************************************
745  * spu_SortSubpictures: find the subpictures to display
746  *****************************************************************************
747  * This function parses all subpictures and decides which ones need to be
748  * displayed. This operation does not need lock, since only READY_SUBPICTURE
749  * are handled. If no picture has been selected, display_date will depend on
750  * the subpicture.
751  * We also check for ephemer DVD subpictures (subpictures that have
752  * to be removed if a newer one is available), which makes it a lot
753  * more difficult to guess if a subpicture has to be rendered or not.
754  *****************************************************************************/
755 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
756 {
757     int i_index, i_channel;
758     subpicture_t *p_subpic = NULL;
759     subpicture_t *p_ephemer;
760     mtime_t      ephemer_date;
761
762     /* Run subpicture filters */
763     for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
764     {
765         subpicture_t *p_subpic_filter;
766         p_subpic_filter = p_spu->pp_filter[i_index]->
767             pf_sub_filter( p_spu->pp_filter[i_index], display_date );
768         if( p_subpic_filter )
769         {
770             spu_DisplaySubpicture( p_spu, p_subpic_filter );
771         }
772     }
773
774     /* We get an easily parsable chained list of subpictures which
775      * ends with NULL since p_subpic was initialized to NULL. */
776     for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
777     {
778         p_ephemer = 0;
779         ephemer_date = 0;
780
781         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
782         {
783             if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
784                 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
785             {
786                 continue;
787             }
788
789             if( display_date &&
790                 display_date < p_spu->p_subpicture[i_index].i_start )
791             {
792                 /* Too early, come back next monday */
793                 continue;
794             }
795
796             if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
797                 ephemer_date = p_spu->p_subpicture[i_index].i_start;
798
799             if( display_date > p_spu->p_subpicture[i_index].i_stop &&
800                 ( !p_spu->p_subpicture[i_index].b_ephemer ||
801                   p_spu->p_subpicture[i_index].i_stop >
802                   p_spu->p_subpicture[i_index].i_start ) )
803             {
804                 /* Too late, destroy the subpic */
805                 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
806                 continue;
807             }
808
809             /* If this is an ephemer subpic, add it to our list */
810             if( p_spu->p_subpicture[i_index].b_ephemer )
811             {
812                 p_spu->p_subpicture[i_index].p_next = p_ephemer;
813                 p_ephemer = &p_spu->p_subpicture[i_index];
814
815                 continue;
816             }
817
818             p_spu->p_subpicture[i_index].p_next = p_subpic;
819             p_subpic = &p_spu->p_subpicture[i_index];
820         }
821
822         /* If we found ephemer subpictures, check if they have to be
823          * displayed or destroyed */
824         while( p_ephemer != NULL )
825         {
826             subpicture_t *p_tmp = p_ephemer;
827             p_ephemer = p_ephemer->p_next;
828
829             if( p_tmp->i_start < ephemer_date )
830             {
831                 /* Ephemer subpicture has lived too long */
832                 spu_DestroySubpicture( p_spu, p_tmp );
833             }
834             else
835             {
836                 /* Ephemer subpicture can still live a bit */
837                 p_tmp->p_next = p_subpic;
838                 p_subpic = p_tmp;
839             }
840         }
841     }
842
843     return p_subpic;
844 }
845
846 /*****************************************************************************
847  * SpuClearChannel: clear an spu channel
848  *****************************************************************************
849  * This function destroys the subpictures which belong to the spu channel
850  * corresponding to i_channel_id.
851  *****************************************************************************/
852 static void SpuClearChannel( spu_t *p_spu, int i_channel )
853 {
854     int          i_subpic;                               /* subpicture index */
855     subpicture_t *p_subpic = NULL;                  /* first free subpicture */
856
857     vlc_mutex_lock( &p_spu->subpicture_lock );
858
859     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
860     {
861         p_subpic = &p_spu->p_subpicture[i_subpic];
862         if( p_subpic->i_status == FREE_SUBPICTURE
863             || ( p_subpic->i_status != RESERVED_SUBPICTURE
864                  && p_subpic->i_status != READY_SUBPICTURE ) )
865         {
866             continue;
867         }
868
869         if( p_subpic->i_channel == i_channel )
870         {
871             while( p_subpic->p_region )
872             {
873                 subpicture_region_t *p_region = p_subpic->p_region;
874                 p_subpic->p_region = p_region->p_next;
875                 spu_DestroyRegion( p_spu, p_region );
876             }
877
878             if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
879             p_subpic->i_status = FREE_SUBPICTURE;
880         }
881     }
882
883     vlc_mutex_unlock( &p_spu->subpicture_lock );
884 }
885
886 /*****************************************************************************
887  * spu_ControlDefault: default methods for the subpicture unit control.
888  *****************************************************************************/
889 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
890 {
891     int *pi, i;
892
893     switch( i_query )
894     {
895     case SPU_CHANNEL_REGISTER:
896         pi = (int *)va_arg( args, int * );
897         if( pi ) *pi = p_spu->i_channel++;
898         msg_Dbg( p_spu, "Registering subpicture channel, ID: %i",
899                  p_spu->i_channel - 1 );
900         break;
901
902     case SPU_CHANNEL_CLEAR:
903         i = (int)va_arg( args, int );
904         SpuClearChannel( p_spu, i );
905         break;
906
907     default:
908         msg_Dbg( p_spu, "control query not supported" );
909         return VLC_EGENERIC;
910     }
911
912     return VLC_SUCCESS;
913 }
914
915 /*****************************************************************************
916  * Object variables callbacks
917  *****************************************************************************/
918
919 /*****************************************************************************
920  * UpdateSPU: update subpicture settings
921  *****************************************************************************
922  * This function is called from CropCallback and at initialization time, to
923  * retrieve crop information from the input.
924  *****************************************************************************/
925 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
926 {
927     vlc_value_t val;
928
929     p_spu->b_force_alpha = VLC_FALSE;
930     p_spu->b_force_crop = VLC_FALSE;
931
932     if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
933
934     p_spu->b_force_crop = VLC_TRUE;
935     var_Get( p_object, "x-start", &val );
936     p_spu->i_crop_x = val.i_int;
937     var_Get( p_object, "y-start", &val );
938     p_spu->i_crop_y = val.i_int;
939     var_Get( p_object, "x-end", &val );
940     p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
941     var_Get( p_object, "y-end", &val );
942     p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
943
944 #if 0
945     if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
946     {
947         int i;
948         for( i = 0; i < 4; i++ )
949         {
950             p_spu->pi_color[i] = ((uint8_t *)val.p_address)[i];
951         }
952     }
953 #endif
954
955     if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
956     {
957         int i;
958         for( i = 0; i < 4; i++ )
959         {
960             p_spu->pi_alpha[i] = ((uint8_t *)val.p_address)[i];
961             p_spu->pi_alpha[i] = p_spu->pi_alpha[i] == 0xf ?
962                 0xff : p_spu->pi_alpha[i] << 4;
963         }
964         p_spu->b_force_alpha = VLC_TRUE;
965     }
966
967     msg_Dbg( p_object, "crop: %i,%i,%i,%i, alpha: %i",
968              p_spu->i_crop_x, p_spu->i_crop_y,
969              p_spu->i_crop_width, p_spu->i_crop_height, p_spu->b_force_alpha );
970 }
971
972 /*****************************************************************************
973  * CropCallback: called when the highlight properties are changed
974  *****************************************************************************
975  * This callback is called from the input thread when we need cropping
976  *****************************************************************************/
977 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
978                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
979 {
980     UpdateSPU( (spu_t *)p_data, p_object );
981     return VLC_SUCCESS;
982 }
983
984 /*****************************************************************************
985  * Buffers allocation callbacks for the filters
986  *****************************************************************************/
987 static subpicture_t *sub_new_buffer( filter_t *p_filter )
988 {
989     filter_owner_sys_t *p_sys = p_filter->p_owner;
990     subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
991     if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
992     return p_subpicture;
993 }
994
995 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
996 {
997     filter_owner_sys_t *p_sys = p_filter->p_owner;
998     spu_DestroySubpicture( p_sys->p_spu, p_subpic );
999 }
1000
1001 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1002 {
1003     subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1004     memset( p_subpic, 0, sizeof(subpicture_t) );
1005     p_subpic->b_absolute = VLC_TRUE;
1006
1007     p_subpic->pf_create_region = __spu_CreateRegion;
1008     p_subpic->pf_destroy_region = __spu_DestroyRegion;
1009
1010     return p_subpic;
1011 }
1012
1013 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1014 {
1015     while( p_subpic->p_region )
1016     {
1017         subpicture_region_t *p_region = p_subpic->p_region;
1018         p_subpic->p_region = p_region->p_next;
1019         p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1020     }
1021
1022     free( p_subpic );
1023 }
1024
1025 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1026 {
1027     picture_t *p_picture = malloc( sizeof(picture_t) );
1028
1029     if( vout_AllocatePicture( p_filter, p_picture,
1030                               p_filter->fmt_out.video.i_chroma,
1031                               p_filter->fmt_out.video.i_width,
1032                               p_filter->fmt_out.video.i_height,
1033                               p_filter->fmt_out.video.i_aspect )
1034         != VLC_SUCCESS )
1035     {
1036         free( p_picture );
1037         return NULL;
1038     }
1039
1040     p_picture->pf_release = RegionPictureRelease;
1041
1042     return p_picture;
1043 }
1044
1045 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1046 {
1047     if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1048     if( p_pic ) free( p_pic );
1049 }