1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2007 the VideoLAN team
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
35 #include <vlc_block.h>
36 #include <vlc_filter.h>
38 #include "../libvlc.h"
43 /*****************************************************************************
45 *****************************************************************************/
48 vlc_mutex_t subpicture_lock; /**< subpicture heap lock */
49 subpicture_t p_subpicture[VOUT_MAX_SUBPICTURES]; /**< subpictures */
50 int i_channel; /**< number of subpicture channels registered */
51 int64_t i_subpicture_order; /**< number of created subpicture since spu creation */
53 filter_t *p_blend; /**< alpha blending module */
54 filter_t *p_text; /**< text renderer module */
55 filter_t *p_scale_yuvp; /**< scaling module for YUVP */
56 filter_t *p_scale; /**< scaling module (all but YUVP) */
57 bool b_force_crop; /**< force cropping of subpicture */
58 int i_crop_x, i_crop_y, i_crop_width, i_crop_height; /**< cropping */
60 int i_margin; /**< force position of a subpicture */
61 bool b_force_palette; /**< force palette of subpicture */
62 uint8_t palette[4][4]; /**< forced palette */
64 /* Supciture filters */
65 filter_chain_t *p_chain;
68 static void UpdateSPU ( spu_t *, vlc_object_t * );
69 static int CropCallback( vlc_object_t *, char const *,
70 vlc_value_t, vlc_value_t, void * );
72 static int SpuControl( spu_t *, int, va_list );
74 static subpicture_t *sub_new_buffer( filter_t * );
75 static void sub_del_buffer( filter_t *, subpicture_t * );
76 static subpicture_t *spu_new_buffer( filter_t * );
77 static void spu_del_buffer( filter_t *, subpicture_t * );
78 static picture_t *spu_new_video_buffer( filter_t * );
79 static void spu_del_video_buffer( filter_t *, picture_t * );
81 static int spu_ParseChain( spu_t * );
82 static int SubFilterCallback( vlc_object_t *, char const *,
83 vlc_value_t, vlc_value_t, void * );
85 static int SubFilterAllocationInit( filter_t *, void * );
86 static void SubFilterAllocationClean( filter_t * );
87 struct filter_owner_sys_t
93 #define SCALE_UNIT (1000)
95 #define VLC_FOURCC_YUVP VLC_FOURCC('Y','U','V','P')
96 #define VLC_FOURCC_YUVA VLC_FOURCC('Y','U','V','A')
97 #define VLC_FOURCC_RGBA VLC_FOURCC('R','G','B','A')
98 #define VLC_FOURCC_TEXT VLC_FOURCC('T','E','X','T')
101 struct subpicture_region_private_t
104 picture_t *p_picture;
107 static subpicture_region_private_t *SpuRegionPrivateCreate( video_format_t *p_fmt )
109 subpicture_region_private_t *p_private = malloc( sizeof(*p_private) );
114 p_private->fmt = *p_fmt;
115 if( p_fmt->p_palette )
117 p_private->fmt.p_palette = malloc( sizeof(*p_private->fmt.p_palette) );
118 if( p_private->fmt.p_palette )
119 *p_private->fmt.p_palette = *p_fmt->p_palette;
121 p_private->p_picture = NULL;
125 static void SpuRegionPrivateDestroy( subpicture_region_private_t *p_private )
127 if( p_private->p_picture )
128 picture_Release( p_private->p_picture );
129 free( p_private->fmt.p_palette );
134 static void SpuRenderCreateAndLoadText( spu_t *p_spu );
135 static void SpuRenderCreateAndLoadScale( spu_t *p_spu );
136 static void FilterRelease( filter_t *p_filter );
139 * Creates the subpicture unit
141 * \param p_this the parent object which creates the subpicture unit
143 spu_t *__spu_Create( vlc_object_t *p_this )
148 spu_private_t *p_sys;
150 p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
151 VLC_OBJECT_GENERIC, "subpicture" );
156 /* Initialize spu fields */
157 p_spu->pf_control = SpuControl;
158 p_spu->p = p_sys = (spu_private_t*)&p_spu[1];
160 /* Initialize private fields */
161 p_sys->i_subpicture_order = 1;
162 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
164 p_sys->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
167 p_sys->p_blend = NULL;
168 p_sys->p_text = NULL;
169 p_sys->p_scale = NULL;
170 p_sys->p_scale_yuvp = NULL;
172 /* Register the default subpicture channel */
173 p_sys->i_channel = 2;
175 vlc_mutex_init( &p_sys->subpicture_lock );
177 vlc_object_attach( p_spu, p_this );
179 p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
180 SubFilterAllocationInit,
181 SubFilterAllocationClean,
184 /* Load text and scale module */
185 SpuRenderCreateAndLoadText( p_spu );
186 SpuRenderCreateAndLoadScale( p_spu );
192 * Initialise the subpicture unit
194 * \param p_spu the subpicture unit object
196 int spu_Init( spu_t *p_spu )
198 spu_private_t *p_sys = p_spu->p;
200 /* If the user requested a sub margin, we force the position. */
201 p_sys->i_margin = var_CreateGetInteger( p_spu, "sub-margin" );
203 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
204 var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
206 spu_ParseChain( p_spu );
211 int spu_ParseChain( spu_t *p_spu )
213 char *psz_parser = var_GetString( p_spu, "sub-filter" );
219 i_ret = filter_chain_AppendFromString( p_spu->p->p_chain, psz_parser );
227 * Destroy the subpicture unit
229 * \param p_this the parent object which destroys the subpicture unit
231 void spu_Destroy( spu_t *p_spu )
233 spu_private_t *p_sys = p_spu->p;
236 /* Destroy all remaining subpictures */
237 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
239 if( p_sys->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
241 spu_DestroySubpicture( p_spu, &p_sys->p_subpicture[i_index] );
246 FilterRelease( p_sys->p_blend );
249 FilterRelease( p_sys->p_text );
251 if( p_sys->p_scale_yuvp )
252 FilterRelease( p_sys->p_scale_yuvp );
255 FilterRelease( p_sys->p_scale );
257 filter_chain_Delete( p_sys->p_chain );
259 vlc_mutex_destroy( &p_sys->subpicture_lock );
261 vlc_object_release( p_spu );
265 * Attach/Detach the SPU from any input
267 * \param p_this the object in which to destroy the subpicture unit
268 * \param b_attach to select attach or detach
270 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
272 vlc_object_t *p_input;
274 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
275 if( !p_input ) return;
279 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
280 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
281 vlc_object_release( p_input );
285 /* Delete callback */
286 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
287 vlc_object_release( p_input );
292 subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
294 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
298 p_region->fmt = *p_fmt;
299 p_region->fmt.p_palette = NULL;
300 if( p_fmt->i_chroma == VLC_FOURCC_YUVP )
302 p_region->fmt.p_palette = calloc( 1, sizeof(*p_region->fmt.p_palette) );
303 if( p_fmt->p_palette )
304 *p_region->fmt.p_palette = *p_fmt->p_palette;
306 p_region->i_alpha = 0xff;
307 p_region->p_next = NULL;
308 p_region->p_private = NULL;
309 p_region->psz_text = NULL;
310 p_region->p_style = NULL;
311 p_region->p_picture = NULL;
313 if( p_fmt->i_chroma == VLC_FOURCC_TEXT )
316 p_region->p_picture = picture_New( p_fmt->i_chroma, p_fmt->i_width, p_fmt->i_height,
318 if( !p_region->p_picture )
320 free( p_fmt->p_palette );
329 void subpicture_region_Delete( subpicture_region_t *p_region )
334 if( p_region->p_private )
335 SpuRegionPrivateDestroy( p_region->p_private );
337 if( p_region->p_picture )
338 picture_Release( p_region->p_picture );
340 free( p_region->fmt.p_palette );
342 free( p_region->psz_text );
343 free( p_region->psz_html );
344 //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
349 void subpicture_region_ChainDelete( subpicture_region_t *p_head )
353 subpicture_region_t *p_next = p_head->p_next;
355 subpicture_region_Delete( p_head );
362 * Display a subpicture
364 * Remove the reservation flag of a subpicture, which will cause it to be
366 * \param p_spu the subpicture unit object
367 * \param p_subpic the subpicture to display
369 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
371 /* Check if status is valid */
372 if( p_subpic->i_status != RESERVED_SUBPICTURE )
374 msg_Err( p_spu, "subpicture %p has invalid status #%d",
375 p_subpic, p_subpic->i_status );
378 if( p_subpic->i_channel == DEFAULT_CHAN )
380 p_subpic->i_channel = 0xFFFF;
381 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
382 p_subpic->i_channel = DEFAULT_CHAN;
385 /* Remove reservation flag */
386 p_subpic->i_status = READY_SUBPICTURE;
390 * Allocate a subpicture in the spu heap.
392 * This function create a reserved subpicture in the spu heap.
393 * A null pointer is returned if the function fails. This method provides an
394 * already allocated zone of memory in the spu data fields. It needs locking
395 * since several pictures can be created by several producers threads.
396 * \param p_spu the subpicture unit in which to create the subpicture
397 * \return NULL on error, a reserved subpicture otherwise
399 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
401 spu_private_t *p_sys = p_spu->p;
402 int i_subpic; /* subpicture index */
403 subpicture_t * p_subpic = NULL; /* first free subpicture */
406 vlc_mutex_lock( &p_sys->subpicture_lock );
409 * Look for an empty place
412 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
414 if( p_sys->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
416 /* Subpicture is empty and ready for allocation */
417 p_subpic = &p_sys->p_subpicture[i_subpic];
418 p_sys->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
423 /* If no free subpicture could be found */
424 if( p_subpic == NULL )
426 msg_Err( p_spu, "subpicture heap is full" );
427 vlc_mutex_unlock( &p_sys->subpicture_lock );
431 /* Copy subpicture information, set some default values */
432 memset( p_subpic, 0, sizeof(subpicture_t) );
433 p_subpic->i_order = p_sys->i_subpicture_order++;
434 p_subpic->i_status = RESERVED_SUBPICTURE;
435 p_subpic->b_absolute = true;
436 p_subpic->b_fade = false;
437 p_subpic->b_subtitle = false;
438 p_subpic->i_alpha = 0xFF;
439 p_subpic->p_region = NULL;
440 p_subpic->pf_render = NULL;
441 p_subpic->pf_destroy = NULL;
442 p_subpic->p_sys = NULL;
443 vlc_mutex_unlock( &p_sys->subpicture_lock );
449 * Remove a subpicture from the heap
451 * This function frees a previously reserved subpicture.
452 * It is meant to be used when the construction of a picture aborted.
453 * This function does not need locking since reserved subpictures are ignored
456 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
458 spu_private_t *p_sys = p_spu->p;
461 vlc_mutex_lock( &p_sys->subpicture_lock );
463 /* There can be race conditions so we need to check the status */
464 if( p_subpic->i_status == FREE_SUBPICTURE )
466 vlc_mutex_unlock( &p_sys->subpicture_lock );
470 /* Check if status is valid */
471 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
472 && ( p_subpic->i_status != READY_SUBPICTURE ) )
474 msg_Err( p_spu, "subpicture %p has invalid status %d",
475 p_subpic, p_subpic->i_status );
478 subpicture_region_ChainDelete( p_subpic->p_region );
479 p_subpic->p_region = NULL;
481 if( p_subpic->pf_destroy )
483 p_subpic->pf_destroy( p_subpic );
486 p_subpic->i_status = FREE_SUBPICTURE;
488 vlc_mutex_unlock( &p_sys->subpicture_lock );
491 static void FilterRelease( filter_t *p_filter )
493 if( p_filter->p_module )
494 module_Unneed( p_filter, p_filter->p_module );
496 vlc_object_detach( p_filter );
497 vlc_object_release( p_filter );
500 static void SpuRenderCreateBlend( spu_t *p_spu, vlc_fourcc_t i_chroma, int i_aspect )
504 assert( !p_spu->p->p_blend );
507 p_blend = vlc_custom_create( p_spu, sizeof(filter_t),
508 VLC_OBJECT_GENERIC, "blend" );
512 es_format_Init( &p_blend->fmt_in, VIDEO_ES, 0 );
514 es_format_Init( &p_blend->fmt_out, VIDEO_ES, 0 );
515 p_blend->fmt_out.video.i_x_offset = 0;
516 p_blend->fmt_out.video.i_y_offset = 0;
517 p_blend->fmt_out.video.i_chroma = i_chroma;
518 p_blend->fmt_out.video.i_aspect = i_aspect;
520 /* The blend module will be loaded when needed with the real
522 p_blend->p_module = NULL;
525 vlc_object_attach( p_blend, p_spu );
527 static void SpuRenderUpdateBlend( spu_t *p_spu, int i_out_width, int i_out_height,
528 const video_format_t *p_in_fmt )
530 filter_t *p_blend = p_spu->p->p_blend;
535 if( p_blend->p_module && p_blend->fmt_in.video.i_chroma != p_in_fmt->i_chroma )
537 /* The chroma is not the same, we need to reload the blend module
538 * XXX to match the old behaviour just test !p_blend->fmt_in.video.i_chroma */
539 module_Unneed( p_blend, p_blend->p_module );
540 p_blend->p_module = NULL;
544 p_blend->fmt_in.video = *p_in_fmt;
547 p_blend->fmt_out.video.i_width =
548 p_blend->fmt_out.video.i_visible_width = i_out_width;
549 p_blend->fmt_out.video.i_height =
550 p_blend->fmt_out.video.i_visible_height = i_out_height;
553 if( !p_blend->p_module )
554 p_blend->p_module = module_Need( p_blend, "video blending", 0, 0 );
556 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
560 assert( !p_spu->p->p_text );
563 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
564 VLC_OBJECT_GENERIC, "spu text" );
568 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
570 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
571 p_text->fmt_out.video.i_width =
572 p_text->fmt_out.video.i_visible_width = 32;
573 p_text->fmt_out.video.i_height =
574 p_text->fmt_out.video.i_visible_height = 32;
576 p_text->pf_sub_buffer_new = spu_new_buffer;
577 p_text->pf_sub_buffer_del = spu_del_buffer;
579 vlc_object_attach( p_text, p_spu );
581 /* FIXME TOCHECK shouldn't module_Need( , , psz_modulename, false ) do the
582 * same than these 2 calls ? */
583 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
584 if( psz_modulename && *psz_modulename )
586 p_text->p_module = module_Need( p_text, "text renderer",
587 psz_modulename, true );
589 free( psz_modulename );
591 if( !p_text->p_module )
592 p_text->p_module = module_Need( p_text, "text renderer", NULL, false );
595 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
596 vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
601 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
602 VLC_OBJECT_GENERIC, "scale" );
606 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
607 p_scale->fmt_in.video.i_chroma = i_src_chroma;
608 p_scale->fmt_in.video.i_width =
609 p_scale->fmt_in.video.i_height = 32;
611 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
612 p_scale->fmt_out.video.i_chroma = i_dst_chroma;
613 p_scale->fmt_out.video.i_width =
614 p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
616 p_scale->pf_vout_buffer_new = spu_new_video_buffer;
617 p_scale->pf_vout_buffer_del = spu_del_video_buffer;
619 vlc_object_attach( p_scale, p_obj );
620 p_scale->p_module = module_Need( p_scale, "video filter2", 0, 0 );
624 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
626 assert( !p_spu->p->p_scale );
627 assert( !p_spu->p->p_scale_yuvp );
628 /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
630 p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
631 VLC_FOURCC_YUVA, VLC_FOURCC_YUVA, true );
632 /* This one is used for YUVP to YUVA/RGBA without scaling
634 p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
635 VLC_FOURCC_YUVP, VLC_FOURCC_YUVA, false );
638 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
639 subpicture_t *p_subpic, subpicture_region_t *p_region,
640 int i_min_scale_ratio )
642 filter_t *p_text = p_spu->p->p_text;
644 assert( p_region->fmt.i_chroma == VLC_FOURCC_TEXT );
646 if( !p_text || !p_text->p_module )
649 /* Setup 3 variables which can be used to render
650 * time-dependent text (and effects). The first indicates
651 * the total amount of time the text will be on screen,
652 * the second the amount of time it has already been on
653 * screen (can be a negative value as text is layed out
654 * before it is rendered) and the third is a feedback
655 * variable from the renderer - if the renderer sets it
656 * then this particular text is time-dependent, eg. the
657 * visual progress bar inside the text in karaoke and the
658 * text needs to be rendered multiple times in order for
659 * the effect to work - we therefore need to return the
660 * region to its original state at the end of the loop,
661 * instead of leaving it in YUVA or YUVP.
662 * Any renderer which is unaware of how to render
663 * time-dependent text can happily ignore the variables
664 * and render the text the same as usual - it should at
665 * least show up on screen, but the effect won't change
666 * the text over time.
669 /* FIXME why these variables are recreated every time and not
670 * when text renderer module was created ? */
671 var_Create( p_text, "spu-duration", VLC_VAR_TIME );
672 var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
673 var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
674 var_Create( p_text, "scale", VLC_VAR_INTEGER );
676 var_SetTime( p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
677 var_SetTime( p_text, "spu-elapsed", mdate() - p_subpic->i_start );
678 var_SetBool( p_text, "text-rerender", false );
679 var_SetInteger( p_text, "scale", i_min_scale_ratio );
681 if( p_text->pf_render_html && p_region->psz_html )
683 p_text->pf_render_html( p_text, p_region, p_region );
685 else if( p_text->pf_render_text )
687 p_text->pf_render_text( p_text, p_region, p_region );
689 *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
691 var_Destroy( p_text, "spu-duration" );
692 var_Destroy( p_text, "spu-elapsed" );
693 var_Destroy( p_text, "text-rerender" );
694 var_Destroy( p_text, "scale" );
697 p_region->i_align |= SUBPICTURE_RENDERED;
701 * A few scale functions helpers.
709 static spu_scale_t spu_scale_create( int w, int h )
711 spu_scale_t s = { .w = w, .h = h };
718 static spu_scale_t spu_scale_unit(void )
720 return spu_scale_create( SCALE_UNIT, SCALE_UNIT );
722 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
724 return spu_scale_create( wn * SCALE_UNIT / wd,
725 hn * SCALE_UNIT / hd );
727 static int spu_scale_w( int v, const spu_scale_t s )
729 return v * s.w / SCALE_UNIT;
731 static int spu_scale_h( int v, const spu_scale_t s )
733 return v * s.h / SCALE_UNIT;
735 static int spu_invscale_w( int v, const spu_scale_t s )
737 return v * SCALE_UNIT / s.w;
739 static int spu_invscale_h( int v, const spu_scale_t s )
741 return v * SCALE_UNIT / s.h;
745 * A few area functions helpers
758 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t s )
760 spu_area_t a = { .i_x = x, .i_y = y, .i_width = w, .i_height = h, .scale = s };
763 static spu_area_t spu_area_scaled( spu_area_t a )
765 if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
768 a.i_x = spu_scale_w( a.i_x, a.scale );
769 a.i_y = spu_scale_h( a.i_y, a.scale );
771 a.i_width = spu_scale_w( a.i_width, a.scale );
772 a.i_height = spu_scale_h( a.i_height, a.scale );
774 a.scale = spu_scale_unit();
777 static spu_area_t spu_area_unscaled( spu_area_t a, spu_scale_t s )
779 if( a.scale.w == s.w && a.scale.h == s.h )
782 a = spu_area_scaled( a );
784 a.i_x = spu_invscale_w( a.i_x, s );
785 a.i_y = spu_invscale_h( a.i_y, s );
787 a.i_width = spu_invscale_w( a.i_width, s );
788 a.i_height = spu_invscale_h( a.i_height, s );
793 static bool spu_area_overlap( spu_area_t a, spu_area_t b )
798 a = spu_area_scaled( a );
799 b = spu_area_scaled( b );
801 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 ) &&
802 __MAX( a.i_y-i_dy, b.i_y ) < __MIN( a.i_y+a.i_height+i_dy, b.i_y+b.i_height );
806 * Avoid area overlapping
808 static void SpuAreaFixOverlap( spu_area_t *p_dst,
809 const spu_area_t *p_master,
810 const spu_area_t *p_sub, int i_sub, int i_align )
812 spu_area_t a = spu_area_scaled( *p_dst );
813 bool b_moved = false;
816 assert( p_master->i_x == 0 && p_master->i_y == 0 );
819 * XXX It is not fast O(n^2) but we should not have a lot of region */
823 for( int i = 0; i < i_sub; i++ )
825 spu_area_t sub = spu_area_scaled( p_sub[i] );
827 if( !spu_area_overlap( a, sub ) )
830 if( i_align & SUBPICTURE_ALIGN_TOP )
833 int i_y = sub.i_y + sub.i_height;
834 if( i_y + a.i_height > p_master->i_height )
839 else if( i_align & SUBPICTURE_ALIGN_BOTTOM )
842 int i_y = sub.i_y - a.i_height;
850 /* TODO what to do in this case? */
851 //fprintf( stderr, "Overlap with unsupported alignment\n" );
861 *p_dst = spu_area_unscaled( a, p_dst->scale );
868 static void SpuRegionPlace( int *pi_x, int *pi_y,
869 const subpicture_t *p_subpic,
870 const subpicture_region_t *p_region,
873 const int i_delta_x = p_region->i_x;
874 const int i_delta_y = p_region->i_y;
877 assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
878 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
882 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
884 i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
888 i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
891 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
895 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
897 i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
901 i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
904 if( p_subpic->b_absolute )
910 /* Margin shifts all subpictures */
911 if( i_margin_y != 0 )
914 /* Clamp offset to not go out of the screen (when possible) */
915 const int i_error_x = (i_x + p_region->fmt.i_width) - p_subpic->i_original_picture_width;
921 const int i_error_y = (i_y + p_region->fmt.i_height) - p_subpic->i_original_picture_height;
932 * This function computes the current alpha value for a given region.
934 static int SpuRegionAlpha( subpicture_t *p_subpic, subpicture_region_t *p_region )
936 /* Compute alpha blend value */
937 int i_fade_alpha = 255;
938 if( p_subpic->b_fade )
940 mtime_t i_fade_start = ( p_subpic->i_stop +
941 p_subpic->i_start ) / 2;
942 mtime_t i_now = mdate();
944 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
946 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
947 ( p_subpic->i_stop - i_fade_start );
950 return i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
954 * It will render the provided region onto p_pic_dst.
957 static void SpuRenderRegion( spu_t *p_spu,
958 picture_t *p_pic_dst, spu_area_t *p_area,
959 subpicture_t *p_subpic, subpicture_region_t *p_region,
960 const spu_scale_t scale_size,
961 const video_format_t *p_fmt,
962 const spu_area_t *p_subtitle_area, int i_subtitle_area )
964 spu_private_t *p_sys = p_spu->p;
966 video_format_t fmt_original = p_region->fmt;
967 bool b_rerender_text = false;
968 bool b_restore_format = false;
972 video_format_t region_fmt;
973 picture_t *p_region_picture;
975 vlc_assert_locked( &p_sys->subpicture_lock );
977 /* Invalidate area by default */
978 *p_area = spu_area_create( 0,0, 0,0, scale_size );
980 /* Render text region */
981 if( p_region->fmt.i_chroma == VLC_FOURCC_TEXT )
983 const int i_min_scale_ratio = SCALE_UNIT; /* FIXME what is the right value? (scale_size is not) */
984 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region, i_min_scale_ratio );
985 b_restore_format = b_rerender_text;
987 /* Check if the rendering has failed ... */
988 if( p_region->fmt.i_chroma == VLC_FOURCC_TEXT )
992 /* Force palette if requested
993 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
994 * instead of only the right one (being the dvd spu).
996 const bool b_using_palette = p_region->fmt.i_chroma == VLC_FOURCC_YUVP;
997 const bool b_force_palette = b_using_palette && p_sys->b_force_palette;
998 const bool b_force_crop = b_force_palette && p_sys->b_force_crop;
999 bool b_changed_palette = false;
1002 /* Compute the margin which is expressed in destination pixel unit
1003 * The margin is applied only to subtitle and when no forced crop is
1004 * requested (dvd menu) */
1006 if( !b_force_crop && p_subpic->b_subtitle )
1007 i_margin_y = spu_invscale_h( p_sys->i_margin, scale_size );
1009 /* Place the picture
1010 * We compute the position in the rendered size */
1011 SpuRegionPlace( &i_x_offset, &i_y_offset,
1012 p_subpic, p_region, i_margin_y );
1014 /* Save this position for subtitle overlap support
1015 * it is really important that there are given without scale_size applied */
1016 *p_area = spu_area_create( i_x_offset, i_y_offset,
1017 p_region->fmt.i_width, p_region->fmt.i_height,
1020 /* Handle overlapping subtitles when possible */
1021 if( p_subpic->b_subtitle && !p_subpic->b_absolute )
1023 spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
1026 SpuAreaFixOverlap( p_area, &display, p_subtitle_area, i_subtitle_area,
1027 p_region->i_align );
1030 /* Fix the position for the current scale_size */
1031 i_x_offset = spu_scale_w( p_area->i_x, p_area->scale );
1032 i_y_offset = spu_scale_h( p_area->i_y, p_area->scale );
1035 if( b_force_palette )
1037 video_palette_t *p_palette = p_region->fmt.p_palette;
1038 video_palette_t palette;
1040 /* We suppose DVD palette here */
1041 palette.i_entries = 4;
1042 for( int i = 0; i < 4; i++ )
1043 for( int j = 0; j < 4; j++ )
1044 palette.palette[i][j] = p_sys->palette[i][j];
1046 if( p_palette->i_entries == palette.i_entries )
1048 for( int i = 0; i < p_palette->i_entries; i++ )
1049 for( int j = 0; j < 4; j++ )
1050 b_changed_palette |= p_palette->palette[i][j] != palette.palette[i][j];
1054 b_changed_palette = true;
1056 *p_palette = palette;
1060 region_fmt = p_region->fmt;
1061 p_region_picture = p_region->p_picture;
1064 /* Scale from rendered size to destination size */
1065 if( p_sys->p_scale && p_sys->p_scale->p_module &&
1066 ( !b_using_palette || ( p_sys->p_scale_yuvp && p_sys->p_scale_yuvp->p_module ) ) &&
1067 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_using_palette ) )
1069 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
1070 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
1072 /* Destroy the cache if unusable */
1073 if( p_region->p_private )
1075 subpicture_region_private_t *p_private = p_region->p_private;
1076 bool b_changed = false;
1078 /* Check resize changes */
1079 if( i_dst_width != p_private->fmt.i_width ||
1080 i_dst_height != p_private->fmt.i_height )
1083 /* Check forced palette changes */
1084 if( b_changed_palette )
1089 SpuRegionPrivateDestroy( p_private );
1090 p_region->p_private = NULL;
1094 /* Scale if needed into cache */
1095 if( !p_region->p_private )
1097 filter_t *p_scale = p_sys->p_scale;
1099 picture_t *p_picture = p_region->p_picture;
1100 picture_Hold( p_picture );
1102 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
1103 if( b_using_palette )
1105 filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
1107 p_scale_yuvp->fmt_in.video = p_region->fmt;
1109 /* TODO converting to RGBA for RGB video output is better */
1110 p_scale_yuvp->fmt_out.video = p_region->fmt;
1111 p_scale_yuvp->fmt_out.video.i_chroma = VLC_FOURCC_YUVA;
1113 p_picture = p_scale_yuvp->pf_video_filter( p_scale_yuvp, p_picture );
1116 /* Well we will try conversion+scaling */
1117 msg_Warn( p_spu, "%4.4s to %4.4s conversion failed",
1118 (const char*)&p_scale_yuvp->fmt_in.video.i_chroma,
1119 (const char*)&p_scale_yuvp->fmt_out.video.i_chroma );
1123 /* Conversion(except from YUVP)/Scaling */
1125 ( p_picture->format.i_width != i_dst_width ||
1126 p_picture->format.i_height != i_dst_height ) )
1128 p_scale->fmt_in.video = p_picture->format;
1129 p_scale->fmt_out.video = p_picture->format;
1131 p_scale->fmt_out.video.i_width = i_dst_width;
1132 p_scale->fmt_out.video.i_height = i_dst_height;
1134 p_scale->fmt_out.video.i_visible_width =
1135 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
1136 p_scale->fmt_out.video.i_visible_height =
1137 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
1139 p_picture = p_scale->pf_video_filter( p_scale, p_picture );
1141 msg_Err( p_spu, "scaling failed" );
1145 p_region->p_private = SpuRegionPrivateCreate( &p_picture->format );
1146 if( p_region->p_private )
1148 p_region->p_private->p_picture = p_picture;
1149 if( !p_region->p_private->p_picture )
1151 SpuRegionPrivateDestroy( p_region->p_private );
1152 p_region->p_private = NULL;
1157 picture_Release( p_picture );
1161 /* And use the scaled picture */
1162 if( p_region->p_private )
1164 region_fmt = p_region->p_private->fmt;
1165 p_region_picture = p_region->p_private->p_picture;
1169 /* Force cropping if requested */
1172 int i_crop_x = spu_scale_w( p_sys->i_crop_x, scale_size );
1173 int i_crop_y = spu_scale_h( p_sys->i_crop_y, scale_size );
1174 int i_crop_width = spu_scale_w( p_sys->i_crop_width, scale_size );
1175 int i_crop_height= spu_scale_h( p_sys->i_crop_height,scale_size );
1177 /* Find the intersection */
1178 if( i_crop_x + i_crop_width <= i_x_offset ||
1179 i_x_offset + (int)region_fmt.i_visible_width < i_crop_x ||
1180 i_crop_y + i_crop_height <= i_y_offset ||
1181 i_y_offset + (int)region_fmt.i_visible_height < i_crop_y )
1183 /* No intersection */
1184 region_fmt.i_visible_width =
1185 region_fmt.i_visible_height = 0;
1189 int i_x, i_y, i_x_end, i_y_end;
1190 i_x = __MAX( i_crop_x, i_x_offset );
1191 i_y = __MAX( i_crop_y, i_y_offset );
1192 i_x_end = __MIN( i_crop_x + i_crop_width,
1193 i_x_offset + (int)region_fmt.i_visible_width );
1194 i_y_end = __MIN( i_crop_y + i_crop_height,
1195 i_y_offset + (int)region_fmt.i_visible_height );
1197 region_fmt.i_x_offset = i_x - i_x_offset;
1198 region_fmt.i_y_offset = i_y - i_y_offset;
1199 region_fmt.i_visible_width = i_x_end - i_x;
1200 region_fmt.i_visible_height = i_y_end - i_y;
1202 i_x_offset = __MAX( i_x, 0 );
1203 i_y_offset = __MAX( i_y, 0 );
1207 /* Update the blender */
1208 SpuRenderUpdateBlend( p_spu, p_fmt->i_width, p_fmt->i_height, ®ion_fmt );
1210 if( p_sys->p_blend->p_module )
1212 const int i_alpha = SpuRegionAlpha( p_subpic, p_region );
1214 p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_pic_dst,
1215 p_region_picture, i_x_offset, i_y_offset, i_alpha );
1219 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1220 (char *)&p_sys->p_blend->fmt_out.video.i_chroma,
1221 (char *)&p_sys->p_blend->fmt_out.video.i_chroma );
1225 if( b_rerender_text )
1227 /* Some forms of subtitles need to be re-rendered more than
1228 * once, eg. karaoke. We therefore restore the region to its
1229 * pre-rendered state, so the next time through everything is
1232 picture_Release( p_region->p_picture );
1233 p_region->p_picture = NULL;
1234 if( p_region->p_private )
1236 SpuRegionPrivateDestroy( p_region->p_private );
1237 p_region->p_private = NULL;
1239 p_region->i_align &= ~SUBPICTURE_RENDERED;
1241 if( b_restore_format )
1242 p_region->fmt = fmt_original;
1246 * This function compares two 64 bits integers.
1247 * It can be used by qsort.
1249 static int IntegerCmp( int64_t i0, int64_t i1 )
1251 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
1254 * This function compares 2 subpictures using the following properties
1255 * (ordered by priority)
1256 * 1. absolute positionning
1260 * It can be used by qsort.
1262 * XXX spu_RenderSubpictures depends heavily on this order.
1264 static int SubpictureCmp( const void *s0, const void *s1 )
1266 subpicture_t *p_subpic0 = *(subpicture_t**)s0;
1267 subpicture_t *p_subpic1 = *(subpicture_t**)s1;
1270 r = IntegerCmp( !p_subpic0->b_absolute, !p_subpic1->b_absolute );
1272 r = IntegerCmp( p_subpic0->i_start, p_subpic1->i_start );
1274 r = IntegerCmp( p_subpic0->i_order, p_subpic1->i_order );
1278 * This function renders all sub picture units in the list.
1280 void spu_RenderSubpictures( spu_t *p_spu,
1281 picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
1282 subpicture_t *p_subpic_list,
1283 const video_format_t *p_fmt_src )
1285 spu_private_t *p_sys = p_spu->p;
1287 const int i_source_video_width = p_fmt_src->i_width;
1288 const int i_source_video_height = p_fmt_src->i_height;
1289 const mtime_t i_current_date = mdate();
1291 unsigned int i_subpicture;
1292 subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
1294 unsigned int i_subtitle_region_count;
1295 spu_area_t p_subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
1296 spu_area_t *p_subtitle_area;
1297 int i_subtitle_area;
1300 vlc_mutex_lock( &p_sys->subpicture_lock );
1302 /* Preprocess subpictures */
1304 i_subtitle_region_count = 0;
1305 for( subpicture_t * p_subpic = p_subpic_list;
1306 p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE; /* Check again status (as we where unlocked) */
1307 p_subpic = p_subpic->p_next )
1310 if( p_subpic->pf_pre_render )
1311 p_subpic->pf_pre_render( p_spu, p_subpic, p_fmt_dst );
1313 if( p_subpic->pf_update_regions )
1315 video_format_t fmt_org = *p_fmt_dst;
1317 fmt_org.i_visible_width = i_source_video_width;
1319 fmt_org.i_visible_height = i_source_video_height;
1321 p_subpic->pf_update_regions( p_spu, p_subpic, &fmt_org, i_current_date );
1325 if( p_subpic->b_subtitle )
1327 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
1328 i_subtitle_region_count++;
1332 pp_subpicture[i_subpicture++] = p_subpic;
1335 /* Be sure we have at least 1 picture to process */
1336 if( i_subpicture <= 0 )
1338 vlc_mutex_unlock( &p_sys->subpicture_lock );
1342 /* Now order subpicture array
1343 * XXX The order is *really* important for overlap subtitles positionning */
1344 qsort( pp_subpicture, i_subpicture, sizeof(*pp_subpicture), SubpictureCmp );
1346 /* Allocate area array for subtitle overlap */
1347 i_subtitle_area = 0;
1348 p_subtitle_area = p_subtitle_area_buffer;
1349 if( i_subtitle_region_count > sizeof(p_subtitle_area_buffer)/sizeof(*p_subtitle_area_buffer) )
1350 p_subtitle_area = calloc( i_subtitle_region_count, sizeof(*p_subtitle_area) );
1352 /* Create the blending module */
1353 if( !p_sys->p_blend )
1354 SpuRenderCreateBlend( p_spu, p_fmt_dst->i_chroma, p_fmt_dst->i_aspect );
1356 /* Process all subpictures and regions (in the right order) */
1357 for( unsigned int i_index = 0; i_index < i_subpicture; i_index++ )
1359 subpicture_t *p_subpic = pp_subpicture[i_index];
1360 subpicture_region_t *p_region;
1362 if( !p_subpic->p_region )
1365 /* FIXME when possible use a better rendering size than source size
1366 * (max of display size and source size for example) FIXME */
1367 int i_render_width = p_subpic->i_original_picture_width;
1368 int i_render_height = p_subpic->i_original_picture_height;
1369 if( !i_render_width || !i_render_height )
1371 if( i_render_width != 0 || i_render_height != 0 )
1372 msg_Err( p_spu, "unsupported original picture size %dx%d",
1373 i_render_width, i_render_height );
1375 p_subpic->i_original_picture_width = i_render_width = i_source_video_width;
1376 p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
1381 p_sys->p_text->fmt_out.video.i_width =
1382 p_sys->p_text->fmt_out.video.i_visible_width = i_render_width;
1384 p_sys->p_text->fmt_out.video.i_height =
1385 p_sys->p_text->fmt_out.video.i_visible_height = i_render_height;
1388 /* Compute scaling from picture to source size */
1389 spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
1390 i_source_video_height, i_render_height );
1392 /* Update scaling from source size to display size(p_fmt_dst) */
1393 scale.w = scale.w * p_fmt_dst->i_width / i_source_video_width;
1394 scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
1396 /* Set default subpicture aspect ratio
1397 * FIXME if we only handle 1 aspect ratio per picture, why is it set per
1399 p_region = p_subpic->p_region;
1400 if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
1402 if( p_region->fmt.i_aspect != 0 )
1404 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
1405 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
1409 p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
1410 p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
1414 /* Take care of the aspect ratio */
1415 if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
1416 p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
1418 /* FIXME FIXME what about region->i_x/i_y ? */
1420 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
1421 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
1424 /* Render all regions
1425 * We always transform non absolute subtitle into absolute one on the
1426 * first rendering to allow good subtitle overlap support.
1428 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
1432 /* Check scale validity */
1433 if( scale.w <= 0 || scale.h <= 0 )
1437 SpuRenderRegion( p_spu, p_pic_dst, &area,
1438 p_subpic, p_region, scale, p_fmt_dst,
1439 p_subtitle_area, i_subtitle_area );
1441 if( p_subpic->b_subtitle )
1443 area = spu_area_unscaled( area, scale );
1444 if( !p_subpic->b_absolute && area.i_width > 0 && area.i_height > 0 )
1446 p_region->i_x = area.i_x;
1447 p_region->i_y = area.i_y;
1449 if( p_subtitle_area )
1450 p_subtitle_area[i_subtitle_area++] = area;
1453 if( p_subpic->b_subtitle )
1454 p_subpic->b_absolute = true;
1458 if( p_subtitle_area != p_subtitle_area_buffer )
1459 free( p_subtitle_area );
1461 vlc_mutex_unlock( &p_sys->subpicture_lock );
1464 /*****************************************************************************
1465 * spu_SortSubpictures: find the subpictures to display
1466 *****************************************************************************
1467 * This function parses all subpictures and decides which ones need to be
1468 * displayed. This operation does not need lock, since only READY_SUBPICTURE
1469 * are handled. If no picture has been selected, display_date will depend on
1471 * We also check for ephemer DVD subpictures (subpictures that have
1472 * to be removed if a newer one is available), which makes it a lot
1473 * more difficult to guess if a subpicture has to be rendered or not.
1474 *****************************************************************************/
1475 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1476 bool b_paused, bool b_subtitle_only )
1478 spu_private_t *p_sys = p_spu->p;
1480 subpicture_t *p_subpic = NULL;
1482 /* Run subpicture filters */
1483 filter_chain_SubFilter( p_sys->p_chain, display_date );
1485 /* We get an easily parsable chained list of subpictures which
1486 * ends with NULL since p_subpic was initialized to NULL. */
1487 for( i_channel = 0; i_channel < p_sys->i_channel; i_channel++ )
1489 subpicture_t *p_ephemer = NULL;
1490 mtime_t ephemer_date = 0;
1493 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1495 subpicture_t *p_current = &p_sys->p_subpicture[i_index];
1497 if( p_current->i_channel != i_channel ||
1498 p_current->i_status != READY_SUBPICTURE ||
1499 ( b_subtitle_only && !p_current->b_subtitle ) )
1504 display_date < p_current->i_start )
1506 /* Too early, come back next monday */
1510 if( p_current->i_start > ephemer_date )
1511 ephemer_date = p_current->i_start;
1513 if( display_date > p_current->i_stop &&
1514 ( !p_current->b_ephemer || p_current->i_stop > p_current->i_start ) &&
1515 !( p_current->b_subtitle && b_paused ) ) /* XXX Assume that subtitle are pausable */
1517 /* Too late, destroy the subpic */
1518 spu_DestroySubpicture( p_spu, &p_sys->p_subpicture[i_index] );
1522 /* If this is an ephemer subpic, add it to our list */
1523 if( p_current->b_ephemer )
1525 p_current->p_next = p_ephemer;
1526 p_ephemer = p_current;
1531 p_current->p_next = p_subpic;
1532 p_subpic = p_current;
1535 /* If we found ephemer subpictures, check if they have to be
1536 * displayed or destroyed */
1537 while( p_ephemer != NULL )
1539 subpicture_t *p_tmp = p_ephemer;
1540 p_ephemer = p_ephemer->p_next;
1542 if( p_tmp->i_start < ephemer_date )
1544 /* Ephemer subpicture has lived too long */
1545 spu_DestroySubpicture( p_spu, p_tmp );
1549 /* Ephemer subpicture can still live a bit */
1550 p_tmp->p_next = p_subpic;
1559 /*****************************************************************************
1560 * SpuClearChannel: clear an spu channel
1561 *****************************************************************************
1562 * This function destroys the subpictures which belong to the spu channel
1563 * corresponding to i_channel_id.
1564 *****************************************************************************/
1565 static void SpuClearChannel( spu_t *p_spu, int i_channel, bool b_locked )
1567 spu_private_t *p_sys = p_spu->p;
1568 int i_subpic; /* subpicture index */
1569 subpicture_t *p_subpic = NULL; /* first free subpicture */
1572 vlc_mutex_lock( &p_sys->subpicture_lock );
1574 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1576 p_subpic = &p_sys->p_subpicture[i_subpic];
1577 if( p_subpic->i_status == FREE_SUBPICTURE
1578 || ( p_subpic->i_status != RESERVED_SUBPICTURE
1579 && p_subpic->i_status != READY_SUBPICTURE ) )
1584 if( p_subpic->i_channel == i_channel )
1586 subpicture_region_ChainDelete( p_subpic->p_region );
1587 p_subpic->p_region = NULL;
1589 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1590 p_subpic->i_status = FREE_SUBPICTURE;
1595 vlc_mutex_unlock( &p_sys->subpicture_lock );
1598 /*****************************************************************************
1599 * spu_ControlDefault: default methods for the subpicture unit control.
1600 *****************************************************************************/
1601 static int SpuControl( spu_t *p_spu, int i_query, va_list args )
1603 spu_private_t *p_sys = p_spu->p;
1608 case SPU_CHANNEL_REGISTER:
1609 pi = (int *)va_arg( args, int * );
1610 vlc_mutex_lock( &p_sys->subpicture_lock );
1612 *pi = p_sys->i_channel++;
1613 vlc_mutex_unlock( &p_sys->subpicture_lock );
1616 case SPU_CHANNEL_CLEAR:
1617 i = (int)va_arg( args, int );
1618 SpuClearChannel( p_spu, i, false );
1622 msg_Dbg( p_spu, "control query not supported" );
1623 return VLC_EGENERIC;
1629 /*****************************************************************************
1630 * Object variables callbacks
1631 *****************************************************************************/
1633 /*****************************************************************************
1634 * UpdateSPU: update subpicture settings
1635 *****************************************************************************
1636 * This function is called from CropCallback and at initialization time, to
1637 * retrieve crop information from the input.
1638 *****************************************************************************/
1639 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1641 spu_private_t *p_sys = p_spu->p;
1644 vlc_mutex_lock( &p_sys->subpicture_lock );
1646 p_sys->b_force_palette = false;
1647 p_sys->b_force_crop = false;
1649 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1651 vlc_mutex_unlock( &p_sys->subpicture_lock );
1655 p_sys->b_force_crop = true;
1656 p_sys->i_crop_x = var_GetInteger( p_object, "x-start" );
1657 p_sys->i_crop_y = var_GetInteger( p_object, "y-start" );
1658 p_sys->i_crop_width = var_GetInteger( p_object, "x-end" ) - p_sys->i_crop_x;
1659 p_sys->i_crop_height = var_GetInteger( p_object, "y-end" ) - p_sys->i_crop_y;
1661 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1663 memcpy( p_sys->palette, val.p_address, 16 );
1664 p_sys->b_force_palette = true;
1666 vlc_mutex_unlock( &p_sys->subpicture_lock );
1668 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1669 p_sys->i_crop_x, p_sys->i_crop_y,
1670 p_sys->i_crop_width, p_sys->i_crop_height,
1671 p_sys->b_force_palette );
1674 /*****************************************************************************
1675 * CropCallback: called when the highlight properties are changed
1676 *****************************************************************************
1677 * This callback is called from the input thread when we need cropping
1678 *****************************************************************************/
1679 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1680 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1682 (void)psz_var; (void)oldval; (void)newval;
1683 UpdateSPU( (spu_t *)p_data, p_object );
1687 /*****************************************************************************
1688 * Buffers allocation callbacks for the filters
1689 *****************************************************************************/
1690 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1692 filter_owner_sys_t *p_sys = p_filter->p_owner;
1693 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1694 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1695 return p_subpicture;
1698 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1700 filter_owner_sys_t *p_sys = p_filter->p_owner;
1701 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1704 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1706 subpicture_t *p_subpic = calloc( 1, sizeof(subpicture_t) );
1710 p_subpic->b_absolute = true;
1711 p_subpic->i_alpha = 0xFF;
1713 VLC_UNUSED(p_filter);
1717 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1719 subpicture_region_ChainDelete( p_subpic->p_region );
1721 VLC_UNUSED(p_filter);
1725 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1727 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1729 VLC_UNUSED(p_filter);
1730 return picture_New( p_fmt->i_chroma,
1731 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
1734 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1736 VLC_UNUSED(p_filter);
1737 picture_Release( p_picture );
1740 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1741 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1743 spu_t *p_spu = p_data;
1744 spu_private_t *p_sys = p_spu->p;
1746 VLC_UNUSED(p_object); VLC_UNUSED(oldval);
1747 VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1749 vlc_mutex_lock( &p_sys->subpicture_lock );
1750 filter_chain_Reset( p_sys->p_chain, NULL, NULL );
1751 spu_ParseChain( p_spu );
1752 vlc_mutex_unlock( &p_sys->subpicture_lock );
1756 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1758 spu_t *p_spu = p_data;
1760 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1762 return VLC_EGENERIC;
1764 p_filter->pf_sub_buffer_new = sub_new_buffer;
1765 p_filter->pf_sub_buffer_del = sub_del_buffer;
1767 p_filter->p_owner = p_sys;
1768 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1769 p_sys->p_spu = p_spu;
1774 static void SubFilterAllocationClean( filter_t *p_filter )
1776 filter_owner_sys_t *p_sys = p_filter->p_owner;
1778 SpuClearChannel( p_sys->p_spu, p_sys->i_channel, true );
1779 free( p_filter->p_owner );