]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
Fixed spu_RenderSubpictures prototype.
[vlc] / src / video_output / vout_subpictures.c
1 /*****************************************************************************
2  * vout_subpictures.c : subpicture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_vout.h>
35 #include <vlc_block.h>
36 #include <vlc_filter.h>
37 #include <vlc_osd.h>
38 #include "../libvlc.h"
39
40 #include <assert.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 static int spu_ParseChain( spu_t * );
59 static int SubFilterCallback( vlc_object_t *, char const *,
60                               vlc_value_t, vlc_value_t, void * );
61
62 static int SubFilterAllocationInit( filter_t *, void * );
63 static void SubFilterAllocationClean( filter_t * );
64 struct filter_owner_sys_t
65 {
66     spu_t *p_spu;
67     int i_channel;
68 };
69
70 #define SCALE_UNIT (1000)
71
72 /* */
73 static void SpuRenderCreateAndLoadText( spu_t *p_spu );
74 static void SpuRenderCreateAndLoadScale( spu_t *p_spu );
75 static void FilterRelease( filter_t *p_filter );
76
77 /**
78  * Creates the subpicture unit
79  *
80  * \param p_this the parent object which creates the subpicture unit
81  */
82 spu_t *__spu_Create( vlc_object_t *p_this )
83 {
84     int i_index;
85     spu_t *p_spu = vlc_custom_create( p_this, sizeof( spu_t ),
86                                       VLC_OBJECT_GENERIC, "subpicture" );
87
88     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
89     {
90         p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
91     }
92
93     p_spu->p_blend = NULL;
94     p_spu->p_text = NULL;
95     p_spu->p_scale = NULL;
96     p_spu->p_scale_yuvp = NULL;
97     p_spu->pf_control = spu_vaControlDefault;
98
99     /* Register the default subpicture channel */
100     p_spu->i_channel = 2;
101
102     vlc_mutex_init( &p_spu->subpicture_lock );
103
104     vlc_object_attach( p_spu, p_this );
105
106     p_spu->p_chain = filter_chain_New( p_spu, "sub filter", false,
107                                        SubFilterAllocationInit,
108                                        SubFilterAllocationClean,
109                                        p_spu );
110
111     /* Load text and scale module */
112     SpuRenderCreateAndLoadText( p_spu );
113     SpuRenderCreateAndLoadScale( p_spu );
114
115     return p_spu;
116 }
117
118 /**
119  * Initialise the subpicture unit
120  *
121  * \param p_spu the subpicture unit object
122  */
123 int spu_Init( spu_t *p_spu )
124 {
125     vlc_value_t val;
126
127     /* If the user requested a sub margin, we force the position. */
128     var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
129     var_Get( p_spu, "sub-margin", &val );
130     p_spu->i_margin = val.i_int;
131
132     var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
133     var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
134
135     spu_ParseChain( p_spu );
136
137     return VLC_SUCCESS;
138 }
139
140 int spu_ParseChain( spu_t *p_spu )
141 {
142     char *psz_parser = var_GetString( p_spu, "sub-filter" );
143     if( filter_chain_AppendFromString( p_spu->p_chain, psz_parser ) < 0 )
144     {
145         free( psz_parser );
146         return VLC_EGENERIC;
147     }
148
149     free( psz_parser );
150     return VLC_SUCCESS;
151 }
152
153 /**
154  * Destroy the subpicture unit
155  *
156  * \param p_this the parent object which destroys the subpicture unit
157  */
158 void spu_Destroy( spu_t *p_spu )
159 {
160     int i_index;
161
162     /* Destroy all remaining subpictures */
163     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
164     {
165         if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
166         {
167             spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
168         }
169     }
170
171     if( p_spu->p_blend )
172         FilterRelease( p_spu->p_blend );
173
174     if( p_spu->p_text )
175         FilterRelease( p_spu->p_text );
176
177     if( p_spu->p_scale_yuvp )
178         FilterRelease( p_spu->p_scale_yuvp );
179
180     if( p_spu->p_scale )
181         FilterRelease( p_spu->p_scale );
182
183     filter_chain_Delete( p_spu->p_chain );
184
185     vlc_mutex_destroy( &p_spu->subpicture_lock );
186     vlc_object_release( 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, bool 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 /* */
218 static void RegionPictureRelease( picture_t *p_pic )
219 {
220     free( p_pic->p_data_orig );
221     /* We use pf_release nullity to know if the picture has already been released. */
222     p_pic->pf_release = NULL;
223 }
224
225 /**
226  * Create a subpicture region
227  *
228  * \param p_this vlc_object_t
229  * \param p_fmt the format that this subpicture region should have
230  */
231 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
232                                          video_format_t *p_fmt )
233 {
234     subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
235     if( !p_region )
236         return NULL;
237
238     /* FIXME is that *really* wanted? */
239     if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
240         p_fmt->p_palette = calloc( 1, sizeof(video_palette_t) );
241     else
242         p_fmt->p_palette = NULL;    /* XXX and that above all? */
243
244     p_region->fmt = *p_fmt;
245     p_region->i_alpha = 0xff;
246     p_region->p_next = NULL;
247     p_region->p_cache = NULL;
248     p_region->psz_text = NULL;
249     p_region->p_style = NULL;
250
251     if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') )
252         return p_region;
253
254     vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
255                           p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
256
257     if( !p_region->picture.i_planes )
258     {
259         free( p_fmt->p_palette );
260         free( p_region );
261         return NULL;
262     }
263
264     p_region->picture.pf_release = RegionPictureRelease;
265
266     return p_region;
267 }
268
269 /**
270  * Destroy a subpicture region
271  *
272  * \param p_this vlc_object_t
273  * \param p_region the subpicture region to destroy
274  */
275 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
276 {
277     if( !p_region )
278         return;
279
280     picture_Release( &p_region->picture );
281
282     free( p_region->fmt.p_palette );
283     if( p_region->p_cache )
284         __spu_DestroyRegion( p_this, p_region->p_cache );
285
286     free( p_region->psz_text );
287     free( p_region->psz_html );
288     //free( p_region->p_style ); FIXME --fenrir plugin does not allocate the memory for it. I think it might lead to segfault, video renderer can live longer than the decoder
289     free( p_region );
290 }
291
292 /**
293  * Display a subpicture
294  *
295  * Remove the reservation flag of a subpicture, which will cause it to be
296  * ready for display.
297  * \param p_spu the subpicture unit object
298  * \param p_subpic the subpicture to display
299  */
300 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
301 {
302     /* Check if status is valid */
303     if( p_subpic->i_status != RESERVED_SUBPICTURE )
304     {
305         msg_Err( p_spu, "subpicture %p has invalid status #%d",
306                  p_subpic, p_subpic->i_status );
307     }
308
309     /* Remove reservation flag */
310     p_subpic->i_status = READY_SUBPICTURE;
311
312     if( p_subpic->i_channel == DEFAULT_CHAN )
313     {
314         p_subpic->i_channel = 0xFFFF;
315         spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
316         p_subpic->i_channel = DEFAULT_CHAN;
317     }
318 }
319
320 /**
321  * Allocate a subpicture in the spu heap.
322  *
323  * This function create a reserved subpicture in the spu heap.
324  * A null pointer is returned if the function fails. This method provides an
325  * already allocated zone of memory in the spu data fields. It needs locking
326  * since several pictures can be created by several producers threads.
327  * \param p_spu the subpicture unit in which to create the subpicture
328  * \return NULL on error, a reserved subpicture otherwise
329  */
330 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
331 {
332     int                 i_subpic;                        /* subpicture index */
333     subpicture_t *      p_subpic = NULL;            /* first free subpicture */
334
335     /* Get lock */
336     vlc_mutex_lock( &p_spu->subpicture_lock );
337
338     /*
339      * Look for an empty place
340      */
341     p_subpic = NULL;
342     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
343     {
344         if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
345         {
346             /* Subpicture is empty and ready for allocation */
347             p_subpic = &p_spu->p_subpicture[i_subpic];
348             p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
349             break;
350         }
351     }
352
353     /* If no free subpicture could be found */
354     if( p_subpic == NULL )
355     {
356         msg_Err( p_spu, "subpicture heap is full" );
357         vlc_mutex_unlock( &p_spu->subpicture_lock );
358         return NULL;
359     }
360
361     /* Copy subpicture information, set some default values */
362     memset( p_subpic, 0, sizeof(subpicture_t) );
363     p_subpic->i_status   = RESERVED_SUBPICTURE;
364     p_subpic->b_absolute = true;
365     p_subpic->b_fade     = false;
366     p_subpic->b_subtitle = false;
367     p_subpic->i_alpha    = 0xFF;
368     p_subpic->p_region   = NULL;
369     p_subpic->pf_render  = NULL;
370     p_subpic->pf_destroy = NULL;
371     p_subpic->p_sys      = NULL;
372     vlc_mutex_unlock( &p_spu->subpicture_lock );
373
374     p_subpic->pf_create_region = __spu_CreateRegion;
375     p_subpic->pf_destroy_region = __spu_DestroyRegion;
376
377     return p_subpic;
378 }
379
380 /**
381  * Remove a subpicture from the heap
382  *
383  * This function frees a previously reserved subpicture.
384  * It is meant to be used when the construction of a picture aborted.
385  * This function does not need locking since reserved subpictures are ignored
386  * by the spu.
387  */
388 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
389 {
390     /* Get lock */
391     vlc_mutex_lock( &p_spu->subpicture_lock );
392
393     /* There can be race conditions so we need to check the status */
394     if( p_subpic->i_status == FREE_SUBPICTURE )
395     {
396         vlc_mutex_unlock( &p_spu->subpicture_lock );
397         return;
398     }
399
400     /* Check if status is valid */
401     if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
402            && ( p_subpic->i_status != READY_SUBPICTURE ) )
403     {
404         msg_Err( p_spu, "subpicture %p has invalid status %d",
405                          p_subpic, p_subpic->i_status );
406     }
407
408     while( p_subpic->p_region )
409     {
410         subpicture_region_t *p_region = p_subpic->p_region;
411         p_subpic->p_region = p_region->p_next;
412         spu_DestroyRegion( p_spu, p_region );
413     }
414
415     if( p_subpic->pf_destroy )
416     {
417         p_subpic->pf_destroy( p_subpic );
418     }
419
420     p_subpic->i_status = FREE_SUBPICTURE;
421
422     vlc_mutex_unlock( &p_spu->subpicture_lock );
423 }
424
425 /*****************************************************************************
426  * spu_RenderSubpictures: render a subpicture list
427  *****************************************************************************
428  * This function renders all sub picture units in the list.
429  *****************************************************************************/
430 static void FilterRelease( filter_t *p_filter )
431 {
432     if( p_filter->p_module )
433         module_Unneed( p_filter, p_filter->p_module );
434
435     vlc_object_detach( p_filter );
436     vlc_object_release( p_filter );
437 }
438
439 static void SpuRenderCreateBlend( spu_t *p_spu, vlc_fourcc_t i_chroma, int i_aspect )
440 {
441     filter_t *p_blend;
442
443     assert( !p_spu->p_blend );
444
445     p_spu->p_blend =
446     p_blend        = vlc_custom_create( p_spu, sizeof(filter_t),
447                                         VLC_OBJECT_GENERIC, "blend" );
448     if( !p_blend )
449         return;
450
451     es_format_Init( &p_blend->fmt_in, VIDEO_ES, 0 );
452
453     es_format_Init( &p_blend->fmt_out, VIDEO_ES, 0 );
454     p_blend->fmt_out.video.i_x_offset = 0;
455     p_blend->fmt_out.video.i_y_offset = 0;
456     p_blend->fmt_out.video.i_chroma = i_chroma;
457     p_blend->fmt_out.video.i_aspect = i_aspect;
458
459     /* The blend module will be loaded when needed with the real
460     * input format */
461     p_blend->p_module = NULL;
462
463     /* */
464     vlc_object_attach( p_blend, p_spu );
465 }
466 static void SpuRenderUpdateBlend( spu_t *p_spu, int i_out_width, int i_out_height, const video_format_t *p_in_fmt )
467 {
468     filter_t *p_blend = p_spu->p_blend;
469
470     assert( p_blend );
471
472     /* */
473     if( p_blend->p_module && p_blend->fmt_in.video.i_chroma != p_in_fmt->i_chroma )
474     {
475         /* The chroma is not the same, we need to reload the blend module
476          * XXX to match the old behaviour just test !p_blend->fmt_in.video.i_chroma */
477         module_Unneed( p_blend, p_blend->p_module );
478         p_blend->p_module = NULL;
479     }
480
481     /* */
482     p_blend->fmt_in.video = *p_in_fmt;
483
484     /* */
485     p_blend->fmt_out.video.i_width =
486     p_blend->fmt_out.video.i_visible_width = i_out_width;
487     p_blend->fmt_out.video.i_height =
488     p_blend->fmt_out.video.i_visible_height = i_out_height;
489
490     /* */
491     if( !p_blend->p_module )
492         p_blend->p_module = module_Need( p_blend, "video blending", 0, 0 );
493 }
494 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
495 {
496     filter_t *p_text;
497
498     assert( !p_spu->p_text );
499
500     p_spu->p_text =
501     p_text        = vlc_custom_create( p_spu, sizeof(filter_t),
502                                        VLC_OBJECT_GENERIC, "spu text" );
503     if( !p_text )
504         return;
505
506     es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
507
508     es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
509     p_text->fmt_out.video.i_width =
510     p_text->fmt_out.video.i_visible_width = 32;
511     p_text->fmt_out.video.i_height =
512     p_text->fmt_out.video.i_visible_height = 32;
513
514     p_text->pf_sub_buffer_new = spu_new_buffer;
515     p_text->pf_sub_buffer_del = spu_del_buffer;
516
517     vlc_object_attach( p_text, p_spu );
518
519     /* FIXME TOCHECK shouldn't module_Need( , , psz_modulename, false ) do the
520      * same than these 2 calls ? */
521     char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
522     if( psz_modulename && *psz_modulename )
523     {
524         p_text->p_module = module_Need( p_text, "text renderer",
525                                         psz_modulename, true );
526     }
527     free( psz_modulename );
528
529     if( !p_text->p_module )
530         p_text->p_module = module_Need( p_text, "text renderer", NULL, false );
531 }
532
533 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj, vlc_fourcc_t i_chroma )
534 {
535     filter_t *p_scale;
536
537     p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
538                                  VLC_OBJECT_GENERIC, "scale" );
539     if( !p_scale )
540         return NULL;
541
542     es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
543     p_scale->fmt_in.video.i_chroma = i_chroma;
544     p_scale->fmt_in.video.i_width =
545     p_scale->fmt_in.video.i_height = 32;
546
547     es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
548     p_scale->fmt_out.video.i_chroma = i_chroma;
549     p_scale->fmt_out.video.i_width =
550     p_scale->fmt_out.video.i_height = 16;
551
552     p_scale->pf_vout_buffer_new = spu_new_video_buffer;
553     p_scale->pf_vout_buffer_del = spu_del_video_buffer;
554
555     vlc_object_attach( p_scale, p_obj );
556     p_scale->p_module = module_Need( p_scale, "video filter2", 0, 0 );
557
558     return p_scale;
559 }
560 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
561 {
562     /* FIXME: We'll also be using it for YUVA and RGBA blending ... */
563
564     assert( !p_spu->p_scale );
565     assert( !p_spu->p_scale_yuvp );
566     p_spu->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu), VLC_FOURCC('Y','U','V','A') );
567     p_spu->p_scale_yuvp = p_spu->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu), VLC_FOURCC('Y','U','V','P') );
568 }
569
570 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
571                            subpicture_t *p_subpic, subpicture_region_t *p_region, int i_min_scale_ratio )
572 {
573     assert( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') );
574
575     if( !p_spu->p_text || !p_spu->p_text->p_module )
576         goto exit;
577
578     /* Setup 3 variables which can be used to render
579      * time-dependent text (and effects). The first indicates
580      * the total amount of time the text will be on screen,
581      * the second the amount of time it has already been on
582      * screen (can be a negative value as text is layed out
583      * before it is rendered) and the third is a feedback
584      * variable from the renderer - if the renderer sets it
585      * then this particular text is time-dependent, eg. the
586      * visual progress bar inside the text in karaoke and the
587      * text needs to be rendered multiple times in order for
588      * the effect to work - we therefore need to return the
589      * region to its original state at the end of the loop,
590      * instead of leaving it in YUVA or YUVP.
591      * Any renderer which is unaware of how to render
592      * time-dependent text can happily ignore the variables
593      * and render the text the same as usual - it should at
594      * least show up on screen, but the effect won't change
595      * the text over time.
596      */
597
598     /* FIXME why these variables are recreated every time and not
599      * when text renderer module was created ? */
600     var_Create( p_spu->p_text, "spu-duration", VLC_VAR_TIME );
601     var_Create( p_spu->p_text, "spu-elapsed", VLC_VAR_TIME );
602     var_Create( p_spu->p_text, "text-rerender", VLC_VAR_BOOL );
603     var_Create( p_spu->p_text, "scale", VLC_VAR_INTEGER );
604
605     var_SetTime( p_spu->p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
606     var_SetTime( p_spu->p_text, "spu-elapsed", mdate() - p_subpic->i_start );
607     var_SetBool( p_spu->p_text, "text-rerender", false );
608     var_SetInteger( p_spu->p_text, "scale", i_min_scale_ratio );
609
610     if( p_spu->p_text->pf_render_html && p_region->psz_html )
611     {
612         p_spu->p_text->pf_render_html( p_spu->p_text,
613                                        p_region, p_region );
614     }
615     else if( p_spu->p_text->pf_render_text )
616     {
617         p_spu->p_text->pf_render_text( p_spu->p_text,
618                                        p_region, p_region );
619     }
620     *pb_rerender_text = var_GetBool( p_spu->p_text, "text-rerender" );
621
622     var_Destroy( p_spu->p_text, "spu-duration" );
623     var_Destroy( p_spu->p_text, "spu-elapsed" );
624     var_Destroy( p_spu->p_text, "text-rerender" );
625     var_Destroy( p_spu->p_text, "scale" );
626
627 exit:
628     p_region->i_align |= SUBPICTURE_RENDERED;
629 }
630
631 /**
632  * A few scale functions helpers.
633  */
634 typedef struct
635 {
636     int w;
637     int h;
638 } spu_scale_t;
639
640 static spu_scale_t spu_scale_create( int w, int h )
641 {
642     spu_scale_t s = { .w = w, .h = h };
643     if( s.w <= 0 )
644         s.w = SCALE_UNIT;
645     if( s.h <= 0 )
646         s.h = SCALE_UNIT;
647     return s;
648 }
649 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
650 {
651     return spu_scale_create( wn * SCALE_UNIT / wd,
652                              hn * SCALE_UNIT / hd );
653 }
654 static int spu_scale_w( int v, const spu_scale_t s )
655 {
656     return v * s.w / SCALE_UNIT;
657 }
658 static int spu_scale_h( int v, const spu_scale_t s )
659 {
660     return v * s.h / SCALE_UNIT;
661 }
662
663 /**
664  * Place a region
665  */
666 static void SpuRegionPlace( int *pi_x, int *pi_y,
667                             const video_format_t *p_fmt,
668                             const subpicture_t *p_subpic,
669                             const subpicture_region_t *p_region )
670 {
671     int i_delta_x = p_region->i_x;
672     int i_delta_y = p_region->i_y;
673     int i_x, i_y;
674
675     if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
676     {
677         i_y = i_delta_y;
678     }
679     else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
680     {
681         i_y = p_fmt->i_height - p_region->fmt.i_height - i_delta_y;
682     }
683     else
684     {
685         i_y = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
686     }
687
688     if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
689     {
690         i_x = i_delta_x;
691     }
692     else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
693     {
694         i_x = p_fmt->i_width - p_region->fmt.i_width - i_delta_x;
695     }
696     else
697     {
698         i_x = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
699     }
700
701     if( p_subpic->b_absolute )
702     {
703         i_x = i_delta_x;
704         i_y = i_delta_y;
705     }
706     if( i_x < 0 )
707         i_x = 0;
708     if( i_y < 0 )
709         i_y = 0;
710
711     *pi_x = i_x;
712     *pi_y = i_y;
713 }
714
715 static void SpuRenderRegion( spu_t *p_spu,
716                              picture_t *p_pic_dst,
717                              subpicture_t *p_subpic, subpicture_region_t *p_region,
718                              const spu_scale_t scale_size,
719                              const video_format_t *p_fmt )
720 {
721     video_format_t fmt_original;
722     bool b_rerender_text;
723     bool b_restore_format = false;
724     int i_fade_alpha;
725     int i_x_offset;
726     int i_y_offset;
727     filter_t *p_scale;
728
729     vlc_assert_locked( &p_spu->subpicture_lock );
730
731     fmt_original = p_region->fmt;
732     b_rerender_text = false;
733     if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
734     {
735         SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region, SCALE_UNIT );
736         b_restore_format = b_rerender_text;
737
738         /* Check if the rendering has failed ... */
739         if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
740             goto exit;
741     }
742
743     /* Force palette if requested
744      * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
745      * instead of only the right one (being the dvd spu).
746      */
747     const bool b_using_palette = p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','P');
748     const bool b_force_palette = b_using_palette && p_spu->b_force_palette;
749     const bool b_force_crop    = b_force_palette && p_spu->b_force_crop;
750
751     if( b_force_palette )
752     {
753         /* It looks so wrong I won't comment
754          * p_palette->palette is [256][4] with a int i_entries
755          * p_spu->palette is [4][4]
756          * */
757         p_region->fmt.p_palette->i_entries = 4;
758         memcpy( p_region->fmt.p_palette->palette, p_spu->palette, 4*sizeof(uint32_t) );
759     }
760
761     if( b_using_palette )
762         p_scale = p_spu->p_scale_yuvp;
763     else
764         p_scale = p_spu->p_scale;
765
766     if( p_scale &&
767         ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_force_palette ) )
768     {
769         const unsigned i_dst_width  = spu_scale_w( p_region->fmt.i_width, scale_size );
770         const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
771
772         /* Destroy if cache is unusable */
773         if( p_region->p_cache )
774         {
775             if( p_region->p_cache->fmt.i_width  != i_dst_width ||
776                 p_region->p_cache->fmt.i_height != i_dst_height ||
777                 b_force_palette )
778             {
779                 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
780                                              p_region->p_cache );
781                 p_region->p_cache = NULL;
782             }
783         }
784
785         /* Scale if needed into cache */
786         if( !p_region->p_cache )
787         {
788             picture_t *p_pic;
789
790             p_scale->fmt_in.video = p_region->fmt;
791             p_scale->fmt_out.video = p_region->fmt;
792
793             p_region->p_cache =
794                 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
795                                             &p_scale->fmt_out.video );
796             p_region->p_cache->p_next = p_region->p_next;
797
798             if( p_scale->fmt_out.video.p_palette )
799                 *p_scale->fmt_out.video.p_palette =
800                     *p_region->fmt.p_palette;
801
802             vout_CopyPicture( p_spu, &p_region->p_cache->picture,
803                               &p_region->picture );
804
805             p_scale->fmt_out.video.i_width = i_dst_width;
806             p_scale->fmt_out.video.i_height = i_dst_height;
807
808             p_scale->fmt_out.video.i_visible_width =
809                 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
810             p_scale->fmt_out.video.i_visible_height =
811                 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
812
813             p_region->p_cache->fmt = p_scale->fmt_out.video;
814             p_region->p_cache->i_x = spu_scale_w( p_region->i_x, scale_size );
815             p_region->p_cache->i_y = spu_scale_h( p_region->i_y, scale_size );
816             p_region->p_cache->i_align = p_region->i_align;
817             p_region->p_cache->i_alpha = p_region->i_alpha;
818
819             p_pic = NULL;
820             if( p_scale->p_module )
821                 p_pic = p_scale->pf_video_filter( p_scale, &p_region->p_cache->picture );
822             else
823                 msg_Err( p_spu, "scaling failed (module not loaded)" );
824
825             if( p_pic )
826             {
827                 p_region->p_cache->picture = *p_pic;
828                 free( p_pic );
829             }
830             else
831             {
832                 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
833                                              p_region->p_cache );
834                 p_region->p_cache = NULL;
835             }
836         }
837
838         /* And use the scaled picture */
839         if( p_region->p_cache )
840         {
841             p_region = p_region->p_cache;
842             fmt_original = p_region->fmt;
843         }
844     }
845
846     /* */
847     SpuRegionPlace( &i_x_offset, &i_y_offset,
848                     p_fmt, p_subpic, p_region );
849
850     if( p_spu->i_margin != 0 && !b_force_crop )
851     {
852         int i_diff = 0;
853         int i_low = i_y_offset - p_spu->i_margin;
854         int i_high = i_low + p_region->fmt.i_height;
855
856         /* crop extra margin to keep within bounds */
857         if( i_low < 0 )
858             i_diff = i_low;
859         if( i_high > (int)p_fmt->i_height )
860             i_diff = i_high - p_fmt->i_height;
861         i_y_offset -= p_spu->i_margin + i_diff;
862     }
863
864     /* Force cropping if requested */
865     if( b_force_crop )
866     {
867         video_format_t *p_fmt = &p_region->fmt;
868         int i_crop_x = spu_scale_w( p_spu->i_crop_x, scale_size );
869         int i_crop_y = spu_scale_h( p_spu->i_crop_y, scale_size );
870         int i_crop_width = spu_scale_w( p_spu->i_crop_width, scale_size );
871         int i_crop_height= spu_scale_h( p_spu->i_crop_height,scale_size );
872
873         /* Find the intersection */
874         if( i_crop_x + i_crop_width <= i_x_offset ||
875             i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
876             i_crop_y + i_crop_height <= i_y_offset ||
877             i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
878         {
879             /* No intersection */
880             p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
881         }
882         else
883         {
884             int i_x, i_y, i_x_end, i_y_end;
885             i_x = __MAX( i_crop_x, i_x_offset );
886             i_y = __MAX( i_crop_y, i_y_offset );
887             i_x_end = __MIN( i_crop_x + i_crop_width,
888                            i_x_offset + (int)p_fmt->i_visible_width );
889             i_y_end = __MIN( i_crop_y + i_crop_height,
890                            i_y_offset + (int)p_fmt->i_visible_height );
891
892             p_fmt->i_x_offset = i_x - i_x_offset;
893             p_fmt->i_y_offset = i_y - i_y_offset;
894             p_fmt->i_visible_width = i_x_end - i_x;
895             p_fmt->i_visible_height = i_y_end - i_y;
896
897             i_x_offset = i_x;
898             i_y_offset = i_y;
899         }
900         b_restore_format = true;
901     }
902
903     i_x_offset = __MAX( i_x_offset, 0 );
904     i_y_offset = __MAX( i_y_offset, 0 );
905
906     /* Compute alpha blend value */
907     i_fade_alpha = 255;
908     if( p_subpic->b_fade )
909     {
910         mtime_t i_fade_start = ( p_subpic->i_stop +
911                                  p_subpic->i_start ) / 2;
912         mtime_t i_now = mdate();
913         if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
914         {
915             i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
916                            ( p_subpic->i_stop - i_fade_start );
917         }
918     }
919
920     /* Update the blender */
921     SpuRenderUpdateBlend( p_spu, p_fmt->i_width, p_fmt->i_height, &p_region->fmt );
922
923     if( p_spu->p_blend->p_module )
924     {
925         p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
926             &p_region->picture, i_x_offset, i_y_offset,
927             i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025 );
928     }
929     else
930     {
931         msg_Err( p_spu, "blending %4.4s to %4.4s failed",
932                  (char *)&p_spu->p_blend->fmt_out.video.i_chroma,
933                  (char *)&p_spu->p_blend->fmt_out.video.i_chroma );
934     }
935
936 exit:
937     if( b_rerender_text )
938     {
939         /* Some forms of subtitles need to be re-rendered more than
940          * once, eg. karaoke. We therefore restore the region to its
941          * pre-rendered state, so the next time through everything is
942          * calculated again.
943          */
944         p_region->picture.pf_release( &p_region->picture );
945         memset( &p_region->picture, 0, sizeof( picture_t ) );
946         p_region->i_align &= ~SUBPICTURE_RENDERED;
947     }
948     if( b_restore_format )
949         p_region->fmt = fmt_original;
950 }
951
952 void spu_RenderSubpictures( spu_t *p_spu,
953                             picture_t *p_pic_dst, video_format_t *p_fmt_dst,
954                             subpicture_t *p_subpic_list,
955                             const video_format_t *p_fmt_src )
956 {
957     video_format_t *p_fmt = p_fmt_dst;
958     const mtime_t i_current_date = mdate();
959     int i_source_video_width;
960     int i_source_video_height;
961     subpicture_t *p_subpic;
962
963     /* Get lock */
964     vlc_mutex_lock( &p_spu->subpicture_lock );
965
966     /* Be sure we have at least 1 picture to process */
967     if( !p_subpic_list || p_subpic_list->i_status == FREE_SUBPICTURE )
968     {
969         vlc_mutex_unlock( &p_spu->subpicture_lock );
970         return;
971     }
972
973     i_source_video_width  = p_fmt_src->i_width;
974     i_source_video_height = p_fmt_src->i_height;
975
976     /* */
977     for( p_subpic = p_subpic_list;
978             p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE; /* Check again status (as we where unlocked) */
979                 p_subpic = p_subpic->p_next )
980     {
981         /* */
982         if( p_subpic->pf_pre_render )
983             p_subpic->pf_pre_render( p_fmt, p_spu, p_subpic );
984
985         if( p_subpic->pf_update_regions )
986         {
987             /* TODO do not reverse the scaling that was done before calling
988              * spu_RenderSubpictures, just pass it along (or do it inside
989              * spu_RenderSubpictures) */
990             video_format_t fmt_org = *p_fmt;
991             fmt_org.i_width =
992             fmt_org.i_visible_width = i_source_video_width;
993             fmt_org.i_height =
994             fmt_org.i_visible_height = i_source_video_height;
995
996             p_subpic->pf_update_regions( &fmt_org, p_spu, p_subpic, i_current_date );
997         }
998     }
999
1000     /* Create the blending module */
1001     if( !p_spu->p_blend )
1002         SpuRenderCreateBlend( p_spu, p_fmt->i_chroma, p_fmt->i_aspect );
1003
1004     /* */
1005     for( p_subpic = p_subpic_list; ; p_subpic = p_subpic->p_next )
1006     {
1007         subpicture_region_t *p_region;
1008
1009         if( !p_subpic || p_subpic->i_status == FREE_SUBPICTURE )
1010             break;
1011
1012         if( !p_subpic->p_region )
1013             continue;
1014
1015         /* FIXME when possible use a better rendering size than source size
1016          * (max of display size and source size for example) FIXME */
1017         int i_render_width  = p_subpic->i_original_picture_width;
1018         int i_render_height = p_subpic->i_original_picture_height;
1019         if( !i_render_width || !i_render_height )
1020         {
1021             if( i_render_width != 0 || i_render_height != 0 )
1022                 msg_Err( p_spu, "unsupported original picture size %dx%d",
1023                          i_render_width, i_render_height );
1024
1025             i_render_width = i_source_video_width;
1026             i_render_height = i_source_video_height;
1027         }
1028
1029         if( p_spu->p_text )
1030         {
1031             p_spu->p_text->fmt_out.video.i_width          =
1032             p_spu->p_text->fmt_out.video.i_visible_width  = i_render_width;
1033
1034             p_spu->p_text->fmt_out.video.i_height         =
1035             p_spu->p_text->fmt_out.video.i_visible_height = i_render_height;
1036         }
1037
1038         /* Compute scaling from picture to source size */
1039         spu_scale_t scale = spu_scale_createq( i_source_video_width,  i_render_width,
1040                                                i_source_video_height, i_render_height );
1041
1042         /* Update scaling from source size to display size(p_fmt) */
1043         scale.w = scale.w * p_fmt->i_width  / i_source_video_width;
1044         scale.h = scale.h * p_fmt->i_height / i_source_video_height;
1045
1046         /* Set default subpicture aspect ratio
1047          * FIXME if we only handle 1 aspect ratio per picture, why is it set per
1048          * region ? */
1049         p_region = p_subpic->p_region;
1050         if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
1051         {
1052             if( p_region->fmt.i_aspect != 0 )
1053             {
1054                 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
1055                 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
1056             }
1057             else
1058             {
1059                 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
1060                 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
1061             }
1062         }
1063
1064         /* Take care of the aspect ratio */
1065         if( p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
1066             p_region->fmt.i_sar_den * p_fmt->i_sar_num )
1067         {
1068             /* FIXME FIXME what about region->i_x/i_y ? */
1069             scale.w = scale.w *
1070                 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
1071                 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
1072         }
1073
1074         /* Render all regions */
1075         for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
1076         {
1077             /* Check scale validity */
1078             if( scale.w <= 0 || scale.h <= 0 )
1079                 continue;
1080
1081             /* */
1082             SpuRenderRegion( p_spu, p_pic_dst, p_subpic, p_region,
1083                              scale, p_fmt );
1084         }
1085     }
1086
1087     vlc_mutex_unlock( &p_spu->subpicture_lock );
1088 }
1089
1090 /*****************************************************************************
1091  * spu_SortSubpictures: find the subpictures to display
1092  *****************************************************************************
1093  * This function parses all subpictures and decides which ones need to be
1094  * displayed. This operation does not need lock, since only READY_SUBPICTURE
1095  * are handled. If no picture has been selected, display_date will depend on
1096  * the subpicture.
1097  * We also check for ephemer DVD subpictures (subpictures that have
1098  * to be removed if a newer one is available), which makes it a lot
1099  * more difficult to guess if a subpicture has to be rendered or not.
1100  *****************************************************************************/
1101 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1102                                    bool b_paused, bool b_subtitle_only )
1103 {
1104     int i_channel;
1105     subpicture_t *p_subpic = NULL;
1106
1107     /* Run subpicture filters */
1108     filter_chain_SubFilter( p_spu->p_chain, display_date );
1109
1110     /* We get an easily parsable chained list of subpictures which
1111      * ends with NULL since p_subpic was initialized to NULL. */
1112     for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
1113     {
1114         subpicture_t *p_ephemer = NULL;
1115         mtime_t      ephemer_date = 0;
1116         int i_index;
1117
1118         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1119         {
1120             subpicture_t *p_current = &p_spu->p_subpicture[i_index];
1121
1122             if( p_current->i_channel != i_channel ||
1123                 p_current->i_status != READY_SUBPICTURE ||
1124                 ( b_subtitle_only && !p_current->b_subtitle ) )
1125             {
1126                 continue;
1127             }
1128             if( display_date &&
1129                 display_date < p_current->i_start )
1130             {
1131                 /* Too early, come back next monday */
1132                 continue;
1133             }
1134
1135             if( p_current->i_start > ephemer_date )
1136                 ephemer_date = p_current->i_start;
1137
1138             if( display_date > p_current->i_stop &&
1139                 ( !p_current->b_ephemer || p_current->i_stop > p_current->i_start ) &&
1140                 !( p_current->b_subtitle && b_paused ) ) /* XXX Assume that subtitle are pausable */
1141             {
1142                 /* Too late, destroy the subpic */
1143                 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
1144                 continue;
1145             }
1146
1147             /* If this is an ephemer subpic, add it to our list */
1148             if( p_current->b_ephemer )
1149             {
1150                 p_current->p_next = p_ephemer;
1151                 p_ephemer = p_current;
1152
1153                 continue;
1154             }
1155
1156             p_current->p_next = p_subpic;
1157             p_subpic = p_current;
1158         }
1159
1160         /* If we found ephemer subpictures, check if they have to be
1161          * displayed or destroyed */
1162         while( p_ephemer != NULL )
1163         {
1164             subpicture_t *p_tmp = p_ephemer;
1165             p_ephemer = p_ephemer->p_next;
1166
1167             if( p_tmp->i_start < ephemer_date )
1168             {
1169                 /* Ephemer subpicture has lived too long */
1170                 spu_DestroySubpicture( p_spu, p_tmp );
1171             }
1172             else
1173             {
1174                 /* Ephemer subpicture can still live a bit */
1175                 p_tmp->p_next = p_subpic;
1176                 p_subpic = p_tmp;
1177             }
1178         }
1179     }
1180
1181     return p_subpic;
1182 }
1183
1184 /*****************************************************************************
1185  * SpuClearChannel: clear an spu channel
1186  *****************************************************************************
1187  * This function destroys the subpictures which belong to the spu channel
1188  * corresponding to i_channel_id.
1189  *****************************************************************************/
1190 static void SpuClearChannel( spu_t *p_spu, int i_channel, bool b_locked )
1191 {
1192     int          i_subpic;                               /* subpicture index */
1193     subpicture_t *p_subpic = NULL;                  /* first free subpicture */
1194
1195     if( !b_locked )
1196         vlc_mutex_lock( &p_spu->subpicture_lock );
1197
1198     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1199     {
1200         p_subpic = &p_spu->p_subpicture[i_subpic];
1201         if( p_subpic->i_status == FREE_SUBPICTURE
1202             || ( p_subpic->i_status != RESERVED_SUBPICTURE
1203                  && p_subpic->i_status != READY_SUBPICTURE ) )
1204         {
1205             continue;
1206         }
1207
1208         if( p_subpic->i_channel == i_channel )
1209         {
1210             while( p_subpic->p_region )
1211             {
1212                 subpicture_region_t *p_region = p_subpic->p_region;
1213                 p_subpic->p_region = p_region->p_next;
1214                 spu_DestroyRegion( p_spu, p_region );
1215             }
1216
1217             if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1218             p_subpic->i_status = FREE_SUBPICTURE;
1219         }
1220     }
1221
1222     if( !b_locked )
1223         vlc_mutex_unlock( &p_spu->subpicture_lock );
1224 }
1225
1226 /*****************************************************************************
1227  * spu_ControlDefault: default methods for the subpicture unit control.
1228  *****************************************************************************/
1229 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1230 {
1231     int *pi, i;
1232
1233     switch( i_query )
1234     {
1235     case SPU_CHANNEL_REGISTER:
1236         pi = (int *)va_arg( args, int * );
1237         vlc_mutex_lock( &p_spu->subpicture_lock );
1238         if( pi )
1239             *pi = p_spu->i_channel++;
1240         vlc_mutex_unlock( &p_spu->subpicture_lock );
1241         break;
1242
1243     case SPU_CHANNEL_CLEAR:
1244         i = (int)va_arg( args, int );
1245         SpuClearChannel( p_spu, i, false );
1246         break;
1247
1248     default:
1249         msg_Dbg( p_spu, "control query not supported" );
1250         return VLC_EGENERIC;
1251     }
1252
1253     return VLC_SUCCESS;
1254 }
1255
1256 /*****************************************************************************
1257  * Object variables callbacks
1258  *****************************************************************************/
1259
1260 /*****************************************************************************
1261  * UpdateSPU: update subpicture settings
1262  *****************************************************************************
1263  * This function is called from CropCallback and at initialization time, to
1264  * retrieve crop information from the input.
1265  *****************************************************************************/
1266 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1267 {
1268     vlc_value_t val;
1269
1270     vlc_mutex_lock( &p_spu->subpicture_lock );
1271
1272     p_spu->b_force_palette = false;
1273     p_spu->b_force_crop = false;
1274
1275     if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1276     {
1277         vlc_mutex_unlock( &p_spu->subpicture_lock );
1278         return;
1279     }
1280
1281     p_spu->b_force_crop = true;
1282     var_Get( p_object, "x-start", &val );
1283     p_spu->i_crop_x = val.i_int;
1284     var_Get( p_object, "y-start", &val );
1285     p_spu->i_crop_y = val.i_int;
1286     var_Get( p_object, "x-end", &val );
1287     p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1288     var_Get( p_object, "y-end", &val );
1289     p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1290
1291     if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1292     {
1293         memcpy( p_spu->palette, val.p_address, 16 );
1294         p_spu->b_force_palette = true;
1295     }
1296     vlc_mutex_unlock( &p_spu->subpicture_lock );
1297
1298     msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1299              p_spu->i_crop_x, p_spu->i_crop_y,
1300              p_spu->i_crop_width, p_spu->i_crop_height,
1301              p_spu->b_force_palette );
1302 }
1303
1304 /*****************************************************************************
1305  * CropCallback: called when the highlight properties are changed
1306  *****************************************************************************
1307  * This callback is called from the input thread when we need cropping
1308  *****************************************************************************/
1309 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1310                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1311 {
1312     (void)psz_var; (void)oldval; (void)newval;
1313     UpdateSPU( (spu_t *)p_data, p_object );
1314     return VLC_SUCCESS;
1315 }
1316
1317 /*****************************************************************************
1318  * Buffers allocation callbacks for the filters
1319  *****************************************************************************/
1320 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1321 {
1322     filter_owner_sys_t *p_sys = p_filter->p_owner;
1323     subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1324     if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1325     return p_subpicture;
1326 }
1327
1328 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1329 {
1330     filter_owner_sys_t *p_sys = p_filter->p_owner;
1331     spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1332 }
1333
1334 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1335 {
1336     subpicture_t *p_subpic = calloc( 1, sizeof(subpicture_t) );
1337     if( !p_subpic )
1338         return NULL;
1339
1340     p_subpic->b_absolute = true;
1341     p_subpic->i_alpha    = 0xFF;
1342
1343     p_subpic->pf_create_region = __spu_CreateRegion;
1344     p_subpic->pf_destroy_region = __spu_DestroyRegion;
1345
1346     VLC_UNUSED(p_filter);
1347     return p_subpic;
1348 }
1349
1350 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1351 {
1352     while( p_subpic->p_region )
1353     {
1354         subpicture_region_t *p_region = p_subpic->p_region;
1355         p_subpic->p_region = p_region->p_next;
1356         p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1357     }
1358
1359     free( p_subpic );
1360 }
1361
1362 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1363 {
1364     const video_format_t *p_fmt = &p_filter->fmt_out.video;
1365
1366     VLC_UNUSED(p_filter);
1367     return picture_New( p_fmt->i_chroma,
1368                         p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
1369 }
1370
1371 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1372 {
1373     VLC_UNUSED(p_filter);
1374     picture_Release( p_picture );
1375 }
1376
1377 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1378                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1379 {
1380     spu_t *p_spu = p_data;
1381
1382     VLC_UNUSED(p_object); VLC_UNUSED(oldval);
1383     VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1384
1385     vlc_mutex_lock( &p_spu->subpicture_lock );
1386     filter_chain_Reset( p_spu->p_chain, NULL, NULL );
1387     spu_ParseChain( p_spu );
1388     vlc_mutex_unlock( &p_spu->subpicture_lock );
1389     return VLC_SUCCESS;
1390 }
1391
1392 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1393 {
1394     spu_t *p_spu = p_data;
1395
1396     filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1397     if( !p_sys )
1398         return VLC_EGENERIC;
1399
1400     p_filter->pf_sub_buffer_new = sub_new_buffer;
1401     p_filter->pf_sub_buffer_del = sub_del_buffer;
1402
1403     p_filter->p_owner = p_sys;
1404     spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1405     p_sys->p_spu = p_spu;
1406
1407     return VLC_SUCCESS;
1408 }
1409
1410 static void SubFilterAllocationClean( filter_t *p_filter )
1411 {
1412     filter_owner_sys_t *p_sys = p_filter->p_owner;
1413
1414     SpuClearChannel( p_sys->p_spu, p_sys->i_channel, true );
1415     free( p_filter->p_owner );
1416 }
1417