]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
Fixed picture cache creation.
[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             if( p_scale->fmt_out.video.p_palette )
794                 *p_scale->fmt_out.video.p_palette =
795                     *p_region->fmt.p_palette;
796
797             p_scale->fmt_out.video.i_width = i_dst_width;
798             p_scale->fmt_out.video.i_height = i_dst_height;
799
800             p_scale->fmt_out.video.i_visible_width =
801                 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
802             p_scale->fmt_out.video.i_visible_height =
803                 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
804
805             p_region->p_cache =
806                 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
807                                             &p_scale->fmt_out.video );
808
809             p_pic = NULL;
810             if( p_scale->p_module )
811             {
812                 picture_t picture = p_region->picture;
813                 picture.pf_release = NULL;  /* That's an ugly hack */
814                 p_pic = p_scale->pf_video_filter( p_scale, &picture );
815             }
816             if( p_pic )
817             {
818                 picture_Copy( &p_region->p_cache->picture, p_pic );
819                 picture_Release( p_pic );
820
821                 p_region->p_cache->fmt = p_scale->fmt_out.video;
822                 p_region->p_cache->i_x = spu_scale_w( p_region->i_x, scale_size );
823                 p_region->p_cache->i_y = spu_scale_h( p_region->i_y, scale_size );
824                 p_region->p_cache->i_align = p_region->i_align;
825                 p_region->p_cache->i_alpha = p_region->i_alpha;
826             }
827             else
828             {
829                 msg_Err( p_spu, "scaling failed (module not loaded)" );
830                 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
831                                              p_region->p_cache );
832                 p_region->p_cache = NULL;
833             }
834
835         }
836
837         /* And use the scaled picture */
838         if( p_region->p_cache )
839         {
840             p_region = p_region->p_cache;
841             fmt_original = p_region->fmt;
842         }
843     }
844
845     /* */
846     SpuRegionPlace( &i_x_offset, &i_y_offset,
847                     p_fmt, p_subpic, p_region );
848
849     if( p_spu->i_margin != 0 && !b_force_crop )
850     {
851         int i_diff = 0;
852         int i_low = i_y_offset - p_spu->i_margin;
853         int i_high = i_low + p_region->fmt.i_height;
854
855         /* crop extra margin to keep within bounds */
856         if( i_low < 0 )
857             i_diff = i_low;
858         if( i_high > (int)p_fmt->i_height )
859             i_diff = i_high - p_fmt->i_height;
860         i_y_offset -= p_spu->i_margin + i_diff;
861     }
862
863     /* Force cropping if requested */
864     if( b_force_crop )
865     {
866         video_format_t *p_fmt = &p_region->fmt;
867         int i_crop_x = spu_scale_w( p_spu->i_crop_x, scale_size );
868         int i_crop_y = spu_scale_h( p_spu->i_crop_y, scale_size );
869         int i_crop_width = spu_scale_w( p_spu->i_crop_width, scale_size );
870         int i_crop_height= spu_scale_h( p_spu->i_crop_height,scale_size );
871
872         /* Find the intersection */
873         if( i_crop_x + i_crop_width <= i_x_offset ||
874             i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
875             i_crop_y + i_crop_height <= i_y_offset ||
876             i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
877         {
878             /* No intersection */
879             p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
880         }
881         else
882         {
883             int i_x, i_y, i_x_end, i_y_end;
884             i_x = __MAX( i_crop_x, i_x_offset );
885             i_y = __MAX( i_crop_y, i_y_offset );
886             i_x_end = __MIN( i_crop_x + i_crop_width,
887                            i_x_offset + (int)p_fmt->i_visible_width );
888             i_y_end = __MIN( i_crop_y + i_crop_height,
889                            i_y_offset + (int)p_fmt->i_visible_height );
890
891             p_fmt->i_x_offset = i_x - i_x_offset;
892             p_fmt->i_y_offset = i_y - i_y_offset;
893             p_fmt->i_visible_width = i_x_end - i_x;
894             p_fmt->i_visible_height = i_y_end - i_y;
895
896             i_x_offset = i_x;
897             i_y_offset = i_y;
898         }
899         b_restore_format = true;
900     }
901
902     i_x_offset = __MAX( i_x_offset, 0 );
903     i_y_offset = __MAX( i_y_offset, 0 );
904
905     /* Compute alpha blend value */
906     i_fade_alpha = 255;
907     if( p_subpic->b_fade )
908     {
909         mtime_t i_fade_start = ( p_subpic->i_stop +
910                                  p_subpic->i_start ) / 2;
911         mtime_t i_now = mdate();
912         if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
913         {
914             i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
915                            ( p_subpic->i_stop - i_fade_start );
916         }
917     }
918
919     /* Update the blender */
920     SpuRenderUpdateBlend( p_spu, p_fmt->i_width, p_fmt->i_height, &p_region->fmt );
921
922     if( p_spu->p_blend->p_module )
923     {
924         p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
925             &p_region->picture, i_x_offset, i_y_offset,
926             i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025 );
927     }
928     else
929     {
930         msg_Err( p_spu, "blending %4.4s to %4.4s failed",
931                  (char *)&p_spu->p_blend->fmt_out.video.i_chroma,
932                  (char *)&p_spu->p_blend->fmt_out.video.i_chroma );
933     }
934
935 exit:
936     if( b_rerender_text )
937     {
938         /* Some forms of subtitles need to be re-rendered more than
939          * once, eg. karaoke. We therefore restore the region to its
940          * pre-rendered state, so the next time through everything is
941          * calculated again.
942          */
943         p_region->picture.pf_release( &p_region->picture );
944         memset( &p_region->picture, 0, sizeof( picture_t ) );
945         p_region->i_align &= ~SUBPICTURE_RENDERED;
946     }
947     if( b_restore_format )
948         p_region->fmt = fmt_original;
949 }
950
951 void spu_RenderSubpictures( spu_t *p_spu,
952                             picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
953                             subpicture_t *p_subpic_list,
954                             const video_format_t *p_fmt_src )
955 {
956     const int i_source_video_width  = p_fmt_src->i_width;
957     const int i_source_video_height = p_fmt_src->i_height;
958     const mtime_t i_current_date = mdate();
959
960     subpicture_t *p_subpic;
961
962     /* Get lock */
963     vlc_mutex_lock( &p_spu->subpicture_lock );
964
965     /* Be sure we have at least 1 picture to process */
966     if( !p_subpic_list || p_subpic_list->i_status == FREE_SUBPICTURE )
967     {
968         vlc_mutex_unlock( &p_spu->subpicture_lock );
969         return;
970     }
971
972     /* */
973     for( p_subpic = p_subpic_list;
974             p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE; /* Check again status (as we where unlocked) */
975                 p_subpic = p_subpic->p_next )
976     {
977         /* */
978         if( p_subpic->pf_pre_render )
979             p_subpic->pf_pre_render( p_spu, p_subpic, p_fmt_dst );
980
981         if( p_subpic->pf_update_regions )
982         {
983             video_format_t fmt_org = *p_fmt_dst;
984             fmt_org.i_width =
985             fmt_org.i_visible_width = i_source_video_width;
986             fmt_org.i_height =
987             fmt_org.i_visible_height = i_source_video_height;
988
989             p_subpic->pf_update_regions( p_spu, p_subpic, &fmt_org, i_current_date );
990         }
991     }
992
993     /* Create the blending module */
994     if( !p_spu->p_blend )
995         SpuRenderCreateBlend( p_spu, p_fmt_dst->i_chroma, p_fmt_dst->i_aspect );
996
997     /* */
998     for( p_subpic = p_subpic_list; ; p_subpic = p_subpic->p_next )
999     {
1000         subpicture_region_t *p_region;
1001
1002         if( !p_subpic || p_subpic->i_status == FREE_SUBPICTURE )
1003             break;
1004
1005         if( !p_subpic->p_region )
1006             continue;
1007
1008         /* FIXME when possible use a better rendering size than source size
1009          * (max of display size and source size for example) FIXME */
1010         int i_render_width  = p_subpic->i_original_picture_width;
1011         int i_render_height = p_subpic->i_original_picture_height;
1012         if( !i_render_width || !i_render_height )
1013         {
1014             if( i_render_width != 0 || i_render_height != 0 )
1015                 msg_Err( p_spu, "unsupported original picture size %dx%d",
1016                          i_render_width, i_render_height );
1017
1018             i_render_width = i_source_video_width;
1019             i_render_height = i_source_video_height;
1020         }
1021
1022         if( p_spu->p_text )
1023         {
1024             p_spu->p_text->fmt_out.video.i_width          =
1025             p_spu->p_text->fmt_out.video.i_visible_width  = i_render_width;
1026
1027             p_spu->p_text->fmt_out.video.i_height         =
1028             p_spu->p_text->fmt_out.video.i_visible_height = i_render_height;
1029         }
1030
1031         /* Compute scaling from picture to source size */
1032         spu_scale_t scale = spu_scale_createq( i_source_video_width,  i_render_width,
1033                                                i_source_video_height, i_render_height );
1034
1035         /* Update scaling from source size to display size(p_fmt_dst) */
1036         scale.w = scale.w * p_fmt_dst->i_width  / i_source_video_width;
1037         scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
1038
1039         /* Set default subpicture aspect ratio
1040          * FIXME if we only handle 1 aspect ratio per picture, why is it set per
1041          * region ? */
1042         p_region = p_subpic->p_region;
1043         if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
1044         {
1045             if( p_region->fmt.i_aspect != 0 )
1046             {
1047                 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
1048                 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
1049             }
1050             else
1051             {
1052                 p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
1053                 p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
1054             }
1055         }
1056
1057         /* Take care of the aspect ratio */
1058         if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
1059             p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
1060         {
1061             /* FIXME FIXME what about region->i_x/i_y ? */
1062             scale.w = scale.w *
1063                 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
1064                 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
1065         }
1066
1067         /* Render all regions */
1068         for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
1069         {
1070             /* Check scale validity */
1071             if( scale.w <= 0 || scale.h <= 0 )
1072                 continue;
1073
1074             /* */
1075             SpuRenderRegion( p_spu, p_pic_dst, p_subpic, p_region,
1076                              scale, p_fmt_dst );
1077         }
1078     }
1079
1080     vlc_mutex_unlock( &p_spu->subpicture_lock );
1081 }
1082
1083 /*****************************************************************************
1084  * spu_SortSubpictures: find the subpictures to display
1085  *****************************************************************************
1086  * This function parses all subpictures and decides which ones need to be
1087  * displayed. This operation does not need lock, since only READY_SUBPICTURE
1088  * are handled. If no picture has been selected, display_date will depend on
1089  * the subpicture.
1090  * We also check for ephemer DVD subpictures (subpictures that have
1091  * to be removed if a newer one is available), which makes it a lot
1092  * more difficult to guess if a subpicture has to be rendered or not.
1093  *****************************************************************************/
1094 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1095                                    bool b_paused, bool b_subtitle_only )
1096 {
1097     int i_channel;
1098     subpicture_t *p_subpic = NULL;
1099
1100     /* Run subpicture filters */
1101     filter_chain_SubFilter( p_spu->p_chain, display_date );
1102
1103     /* We get an easily parsable chained list of subpictures which
1104      * ends with NULL since p_subpic was initialized to NULL. */
1105     for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
1106     {
1107         subpicture_t *p_ephemer = NULL;
1108         mtime_t      ephemer_date = 0;
1109         int i_index;
1110
1111         for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1112         {
1113             subpicture_t *p_current = &p_spu->p_subpicture[i_index];
1114
1115             if( p_current->i_channel != i_channel ||
1116                 p_current->i_status != READY_SUBPICTURE ||
1117                 ( b_subtitle_only && !p_current->b_subtitle ) )
1118             {
1119                 continue;
1120             }
1121             if( display_date &&
1122                 display_date < p_current->i_start )
1123             {
1124                 /* Too early, come back next monday */
1125                 continue;
1126             }
1127
1128             if( p_current->i_start > ephemer_date )
1129                 ephemer_date = p_current->i_start;
1130
1131             if( display_date > p_current->i_stop &&
1132                 ( !p_current->b_ephemer || p_current->i_stop > p_current->i_start ) &&
1133                 !( p_current->b_subtitle && b_paused ) ) /* XXX Assume that subtitle are pausable */
1134             {
1135                 /* Too late, destroy the subpic */
1136                 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
1137                 continue;
1138             }
1139
1140             /* If this is an ephemer subpic, add it to our list */
1141             if( p_current->b_ephemer )
1142             {
1143                 p_current->p_next = p_ephemer;
1144                 p_ephemer = p_current;
1145
1146                 continue;
1147             }
1148
1149             p_current->p_next = p_subpic;
1150             p_subpic = p_current;
1151         }
1152
1153         /* If we found ephemer subpictures, check if they have to be
1154          * displayed or destroyed */
1155         while( p_ephemer != NULL )
1156         {
1157             subpicture_t *p_tmp = p_ephemer;
1158             p_ephemer = p_ephemer->p_next;
1159
1160             if( p_tmp->i_start < ephemer_date )
1161             {
1162                 /* Ephemer subpicture has lived too long */
1163                 spu_DestroySubpicture( p_spu, p_tmp );
1164             }
1165             else
1166             {
1167                 /* Ephemer subpicture can still live a bit */
1168                 p_tmp->p_next = p_subpic;
1169                 p_subpic = p_tmp;
1170             }
1171         }
1172     }
1173
1174     return p_subpic;
1175 }
1176
1177 /*****************************************************************************
1178  * SpuClearChannel: clear an spu channel
1179  *****************************************************************************
1180  * This function destroys the subpictures which belong to the spu channel
1181  * corresponding to i_channel_id.
1182  *****************************************************************************/
1183 static void SpuClearChannel( spu_t *p_spu, int i_channel, bool b_locked )
1184 {
1185     int          i_subpic;                               /* subpicture index */
1186     subpicture_t *p_subpic = NULL;                  /* first free subpicture */
1187
1188     if( !b_locked )
1189         vlc_mutex_lock( &p_spu->subpicture_lock );
1190
1191     for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1192     {
1193         p_subpic = &p_spu->p_subpicture[i_subpic];
1194         if( p_subpic->i_status == FREE_SUBPICTURE
1195             || ( p_subpic->i_status != RESERVED_SUBPICTURE
1196                  && p_subpic->i_status != READY_SUBPICTURE ) )
1197         {
1198             continue;
1199         }
1200
1201         if( p_subpic->i_channel == i_channel )
1202         {
1203             while( p_subpic->p_region )
1204             {
1205                 subpicture_region_t *p_region = p_subpic->p_region;
1206                 p_subpic->p_region = p_region->p_next;
1207                 spu_DestroyRegion( p_spu, p_region );
1208             }
1209
1210             if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1211             p_subpic->i_status = FREE_SUBPICTURE;
1212         }
1213     }
1214
1215     if( !b_locked )
1216         vlc_mutex_unlock( &p_spu->subpicture_lock );
1217 }
1218
1219 /*****************************************************************************
1220  * spu_ControlDefault: default methods for the subpicture unit control.
1221  *****************************************************************************/
1222 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1223 {
1224     int *pi, i;
1225
1226     switch( i_query )
1227     {
1228     case SPU_CHANNEL_REGISTER:
1229         pi = (int *)va_arg( args, int * );
1230         vlc_mutex_lock( &p_spu->subpicture_lock );
1231         if( pi )
1232             *pi = p_spu->i_channel++;
1233         vlc_mutex_unlock( &p_spu->subpicture_lock );
1234         break;
1235
1236     case SPU_CHANNEL_CLEAR:
1237         i = (int)va_arg( args, int );
1238         SpuClearChannel( p_spu, i, false );
1239         break;
1240
1241     default:
1242         msg_Dbg( p_spu, "control query not supported" );
1243         return VLC_EGENERIC;
1244     }
1245
1246     return VLC_SUCCESS;
1247 }
1248
1249 /*****************************************************************************
1250  * Object variables callbacks
1251  *****************************************************************************/
1252
1253 /*****************************************************************************
1254  * UpdateSPU: update subpicture settings
1255  *****************************************************************************
1256  * This function is called from CropCallback and at initialization time, to
1257  * retrieve crop information from the input.
1258  *****************************************************************************/
1259 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1260 {
1261     vlc_value_t val;
1262
1263     vlc_mutex_lock( &p_spu->subpicture_lock );
1264
1265     p_spu->b_force_palette = false;
1266     p_spu->b_force_crop = false;
1267
1268     if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1269     {
1270         vlc_mutex_unlock( &p_spu->subpicture_lock );
1271         return;
1272     }
1273
1274     p_spu->b_force_crop = true;
1275     var_Get( p_object, "x-start", &val );
1276     p_spu->i_crop_x = val.i_int;
1277     var_Get( p_object, "y-start", &val );
1278     p_spu->i_crop_y = val.i_int;
1279     var_Get( p_object, "x-end", &val );
1280     p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1281     var_Get( p_object, "y-end", &val );
1282     p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1283
1284     if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1285     {
1286         memcpy( p_spu->palette, val.p_address, 16 );
1287         p_spu->b_force_palette = true;
1288     }
1289     vlc_mutex_unlock( &p_spu->subpicture_lock );
1290
1291     msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1292              p_spu->i_crop_x, p_spu->i_crop_y,
1293              p_spu->i_crop_width, p_spu->i_crop_height,
1294              p_spu->b_force_palette );
1295 }
1296
1297 /*****************************************************************************
1298  * CropCallback: called when the highlight properties are changed
1299  *****************************************************************************
1300  * This callback is called from the input thread when we need cropping
1301  *****************************************************************************/
1302 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1303                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1304 {
1305     (void)psz_var; (void)oldval; (void)newval;
1306     UpdateSPU( (spu_t *)p_data, p_object );
1307     return VLC_SUCCESS;
1308 }
1309
1310 /*****************************************************************************
1311  * Buffers allocation callbacks for the filters
1312  *****************************************************************************/
1313 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1314 {
1315     filter_owner_sys_t *p_sys = p_filter->p_owner;
1316     subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1317     if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1318     return p_subpicture;
1319 }
1320
1321 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1322 {
1323     filter_owner_sys_t *p_sys = p_filter->p_owner;
1324     spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1325 }
1326
1327 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1328 {
1329     subpicture_t *p_subpic = calloc( 1, sizeof(subpicture_t) );
1330     if( !p_subpic )
1331         return NULL;
1332
1333     p_subpic->b_absolute = true;
1334     p_subpic->i_alpha    = 0xFF;
1335
1336     p_subpic->pf_create_region = __spu_CreateRegion;
1337     p_subpic->pf_destroy_region = __spu_DestroyRegion;
1338
1339     VLC_UNUSED(p_filter);
1340     return p_subpic;
1341 }
1342
1343 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1344 {
1345     while( p_subpic->p_region )
1346     {
1347         subpicture_region_t *p_region = p_subpic->p_region;
1348         p_subpic->p_region = p_region->p_next;
1349         p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1350     }
1351
1352     free( p_subpic );
1353 }
1354
1355 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1356 {
1357     const video_format_t *p_fmt = &p_filter->fmt_out.video;
1358
1359     VLC_UNUSED(p_filter);
1360     return picture_New( p_fmt->i_chroma,
1361                         p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
1362 }
1363
1364 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1365 {
1366     VLC_UNUSED(p_filter);
1367     picture_Release( p_picture );
1368 }
1369
1370 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1371                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1372 {
1373     spu_t *p_spu = p_data;
1374
1375     VLC_UNUSED(p_object); VLC_UNUSED(oldval);
1376     VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1377
1378     vlc_mutex_lock( &p_spu->subpicture_lock );
1379     filter_chain_Reset( p_spu->p_chain, NULL, NULL );
1380     spu_ParseChain( p_spu );
1381     vlc_mutex_unlock( &p_spu->subpicture_lock );
1382     return VLC_SUCCESS;
1383 }
1384
1385 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1386 {
1387     spu_t *p_spu = p_data;
1388
1389     filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1390     if( !p_sys )
1391         return VLC_EGENERIC;
1392
1393     p_filter->pf_sub_buffer_new = sub_new_buffer;
1394     p_filter->pf_sub_buffer_del = sub_del_buffer;
1395
1396     p_filter->p_owner = p_sys;
1397     spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1398     p_sys->p_spu = p_spu;
1399
1400     return VLC_SUCCESS;
1401 }
1402
1403 static void SubFilterAllocationClean( filter_t *p_filter )
1404 {
1405     filter_owner_sys_t *p_sys = p_filter->p_owner;
1406
1407     SpuClearChannel( p_sys->p_spu, p_sys->i_channel, true );
1408     free( p_filter->p_owner );
1409 }
1410