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