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