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 *****************************************************************************/
31 #include <vlc_block.h>
32 #include <vlc_filter.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static void UpdateSPU ( spu_t *, vlc_object_t * );
39 static int CropCallback( vlc_object_t *, char const *,
40 vlc_value_t, vlc_value_t, void * );
42 static int spu_vaControlDefault( spu_t *, int, va_list );
44 static subpicture_t *sub_new_buffer( filter_t * );
45 static void sub_del_buffer( filter_t *, subpicture_t * );
46 static subpicture_t *spu_new_buffer( filter_t * );
47 static void spu_del_buffer( filter_t *, subpicture_t * );
48 static picture_t *spu_new_video_buffer( filter_t * );
49 static void spu_del_video_buffer( filter_t *, picture_t * );
51 static int spu_ParseChain( spu_t * );
52 static void spu_DeleteChain( spu_t * );
53 static int SubFilterCallback( vlc_object_t *, char const *,
54 vlc_value_t, vlc_value_t, void * );
56 struct filter_owner_sys_t
68 * Creates the subpicture unit
70 * \param p_this the parent object which creates the subpicture unit
72 spu_t *__spu_Create( vlc_object_t *p_this )
75 spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
77 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
79 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
82 p_spu->p_blend = NULL;
84 p_spu->p_scale = NULL;
86 p_spu->pf_control = spu_vaControlDefault;
88 /* Register the default subpicture channel */
91 vlc_mutex_init( p_this, &p_spu->subpicture_lock );
93 vlc_object_attach( p_spu, p_this );
99 * Initialise the subpicture unit
101 * \param p_spu the subpicture unit object
103 int spu_Init( spu_t *p_spu )
107 /* If the user requested a sub margin, we force the position. */
108 var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
109 var_Get( p_spu, "sub-margin", &val );
110 p_spu->i_margin = val.i_int;
112 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
113 var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
115 spu_ParseChain( p_spu );
120 int spu_ParseChain( spu_t *p_spu )
124 var_Get( p_spu, "sub-filter", &val );
125 psz_parser = val.psz_string;
127 while( psz_parser && *psz_parser )
129 config_chain_t *p_cfg;
132 psz_parser = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
134 msg_Dbg( p_spu, "adding sub-filter: %s", psz_name );
136 p_spu->pp_filter[p_spu->i_filter] =
137 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
138 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
139 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
140 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
141 p_spu->pp_filter[p_spu->i_filter]->p_cfg = p_cfg;
142 p_spu->pp_filter[p_spu->i_filter]->p_module =
143 module_Need( p_spu->pp_filter[p_spu->i_filter],
144 "sub filter", psz_name, VLC_TRUE );
145 if( p_spu->pp_filter[p_spu->i_filter]->p_module )
147 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
148 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
149 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
150 p_sys->p_spu = p_spu;
155 msg_Dbg( p_spu, "no sub filter found" );
156 config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
157 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
158 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
161 if( p_spu->i_filter >= 10 )
163 msg_Dbg( p_spu, "can't add anymore filters" );
168 if( val.psz_string ) free( val.psz_string );
174 * Destroy the subpicture unit
176 * \param p_this the parent object which destroys the subpicture unit
178 void spu_Destroy( spu_t *p_spu )
182 vlc_object_detach( p_spu );
184 /* Destroy all remaining subpictures */
185 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
187 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
189 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
195 if( p_spu->p_blend->p_module )
196 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
198 vlc_object_detach( p_spu->p_blend );
199 vlc_object_destroy( p_spu->p_blend );
204 if( p_spu->p_text->p_module )
205 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
207 vlc_object_detach( p_spu->p_text );
208 vlc_object_destroy( p_spu->p_text );
213 if( p_spu->p_scale->p_module )
214 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
216 vlc_object_detach( p_spu->p_scale );
217 vlc_object_destroy( p_spu->p_scale );
220 spu_DeleteChain( p_spu );
222 vlc_mutex_destroy( &p_spu->subpicture_lock );
223 vlc_object_destroy( p_spu );
226 static void spu_DeleteChain( spu_t *p_spu )
228 if( p_spu->i_filter )
229 while( p_spu->i_filter )
232 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
233 p_spu->pp_filter[p_spu->i_filter]->p_module );
234 free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
235 config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
236 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
237 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
242 * Attach/Detach the SPU from any input
244 * \param p_this the object in which to destroy the subpicture unit
245 * \param b_attach to select attach or detach
247 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
249 vlc_object_t *p_input;
251 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
252 if( !p_input ) return;
256 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
257 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
258 vlc_object_release( p_input );
262 /* Delete callback */
263 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
264 vlc_object_release( p_input );
269 * Create a subpicture region
271 * \param p_this vlc_object_t
272 * \param p_fmt the format that this subpicture region should have
274 static void RegionPictureRelease( picture_t *p_pic )
276 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
278 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
279 video_format_t *p_fmt )
281 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
282 if( !p_region ) return NULL;
284 memset( p_region, 0, sizeof(subpicture_region_t) );
285 p_region->p_next = NULL;
286 p_region->p_cache = NULL;
287 p_region->fmt = *p_fmt;
288 p_region->psz_text = NULL;
289 p_region->p_style = NULL;
291 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
292 p_fmt->p_palette = p_region->fmt.p_palette =
293 malloc( sizeof(video_palette_t) );
294 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
296 p_region->picture.p_data_orig = NULL;
298 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
300 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
301 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
303 if( !p_region->picture.i_planes )
306 if( p_fmt->p_palette ) free( p_fmt->p_palette );
310 p_region->picture.pf_release = RegionPictureRelease;
316 * Make a subpicture region from an existing picture_t
318 * \param p_this vlc_object_t
319 * \param p_fmt the format that this subpicture region should have
320 * \param p_pic a pointer to the picture creating the region (not freed)
322 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
323 video_format_t *p_fmt,
326 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
328 memset( p_region, 0, sizeof(subpicture_region_t) );
329 p_region->p_next = 0;
330 p_region->p_cache = 0;
331 p_region->fmt = *p_fmt;
332 p_region->psz_text = 0;
333 p_region->p_style = NULL;
335 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
336 p_fmt->p_palette = p_region->fmt.p_palette =
337 malloc( sizeof(video_palette_t) );
338 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
340 memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
341 p_region->picture.pf_release = RegionPictureRelease;
347 * Destroy a subpicture region
349 * \param p_this vlc_object_t
350 * \param p_region the subpicture region to destroy
352 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
354 if( !p_region ) return;
355 if( p_region->picture.pf_release )
356 p_region->picture.pf_release( &p_region->picture );
357 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
358 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
360 if( p_region->psz_text )
361 free( p_region->psz_text );
362 if( p_region->psz_html )
363 free( p_region->psz_html );
364 //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
369 * Display a subpicture
371 * Remove the reservation flag of a subpicture, which will cause it to be
373 * \param p_spu the subpicture unit object
374 * \param p_subpic the subpicture to display
376 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
378 /* Check if status is valid */
379 if( p_subpic->i_status != RESERVED_SUBPICTURE )
381 msg_Err( p_spu, "subpicture %p has invalid status #%d",
382 p_subpic, p_subpic->i_status );
385 /* Remove reservation flag */
386 p_subpic->i_status = READY_SUBPICTURE;
388 if( p_subpic->i_channel == DEFAULT_CHAN )
390 p_subpic->i_channel = 0xFFFF;
391 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
392 p_subpic->i_channel = DEFAULT_CHAN;
397 * Allocate a subpicture in the spu heap.
399 * This function create a reserved subpicture in the spu heap.
400 * A null pointer is returned if the function fails. This method provides an
401 * already allocated zone of memory in the spu data fields. It needs locking
402 * since several pictures can be created by several producers threads.
403 * \param p_spu the subpicture unit in which to create the subpicture
404 * \return NULL on error, a reserved subpicture otherwise
406 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
408 int i_subpic; /* subpicture index */
409 subpicture_t * p_subpic = NULL; /* first free subpicture */
412 vlc_mutex_lock( &p_spu->subpicture_lock );
415 * Look for an empty place
418 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
420 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
422 /* Subpicture is empty and ready for allocation */
423 p_subpic = &p_spu->p_subpicture[i_subpic];
424 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
429 /* If no free subpicture could be found */
430 if( p_subpic == NULL )
432 msg_Err( p_spu, "subpicture heap is full" );
433 vlc_mutex_unlock( &p_spu->subpicture_lock );
437 /* Copy subpicture information, set some default values */
438 memset( p_subpic, 0, sizeof(subpicture_t) );
439 p_subpic->i_status = RESERVED_SUBPICTURE;
440 p_subpic->b_absolute = VLC_TRUE;
441 p_subpic->b_pausable = VLC_FALSE;
442 p_subpic->b_fade = VLC_FALSE;
443 p_subpic->i_alpha = 0xFF;
444 p_subpic->p_region = NULL;
445 p_subpic->pf_render = NULL;
446 p_subpic->pf_destroy = NULL;
447 p_subpic->p_sys = NULL;
448 vlc_mutex_unlock( &p_spu->subpicture_lock );
450 p_subpic->pf_create_region = __spu_CreateRegion;
451 p_subpic->pf_make_region = __spu_MakeRegion;
452 p_subpic->pf_destroy_region = __spu_DestroyRegion;
458 * Remove a subpicture from the heap
460 * This function frees a previously reserved subpicture.
461 * It is meant to be used when the construction of a picture aborted.
462 * This function does not need locking since reserved subpictures are ignored
465 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
468 vlc_mutex_lock( &p_spu->subpicture_lock );
470 /* There can be race conditions so we need to check the status */
471 if( p_subpic->i_status == FREE_SUBPICTURE )
473 vlc_mutex_unlock( &p_spu->subpicture_lock );
477 /* Check if status is valid */
478 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
479 && ( p_subpic->i_status != READY_SUBPICTURE ) )
481 msg_Err( p_spu, "subpicture %p has invalid status %d",
482 p_subpic, p_subpic->i_status );
485 while( p_subpic->p_region )
487 subpicture_region_t *p_region = p_subpic->p_region;
488 p_subpic->p_region = p_region->p_next;
489 spu_DestroyRegion( p_spu, p_region );
492 if( p_subpic->pf_destroy )
494 p_subpic->pf_destroy( p_subpic );
497 p_subpic->i_status = FREE_SUBPICTURE;
499 vlc_mutex_unlock( &p_spu->subpicture_lock );
502 /*****************************************************************************
503 * spu_RenderSubpictures: render a subpicture list
504 *****************************************************************************
505 * This function renders all sub picture units in the list.
506 *****************************************************************************/
507 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
508 picture_t *p_pic_dst, picture_t *p_pic_src,
509 subpicture_t *p_subpic,
510 int i_scale_width_orig, int i_scale_height_orig )
512 int i_source_video_width = p_fmt->i_width * 1000 / i_scale_width_orig;
513 int i_source_video_height = p_fmt->i_height * 1000 / i_scale_height_orig;
516 vlc_mutex_lock( &p_spu->subpicture_lock );
518 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
519 while( ( p_subpic != NULL ) && ( p_subpic->i_status != FREE_SUBPICTURE ) )
521 subpicture_region_t *p_region = p_subpic->p_region;
522 int pi_scale_width[ SCALE_SIZE ];
523 int pi_scale_height[ SCALE_SIZE ];
524 int pi_subpic_x[ SCALE_SIZE ];
527 for( k = 0; k < SCALE_SIZE ; k++ )
528 pi_subpic_x[ k ] = p_subpic->i_x;
530 /* Load the blending module */
531 if( !p_spu->p_blend && p_region )
533 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
534 vlc_object_attach( p_spu->p_blend, p_spu );
535 p_spu->p_blend->fmt_out.video.i_x_offset =
536 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
537 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
538 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
539 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
540 /* FIXME: We'll also be using it for YUVA and RGBA blending ... */
542 p_spu->p_blend->p_module =
543 module_Need( p_spu->p_blend, "video blending", 0, 0 );
546 /* Load the text rendering module; it is possible there is a
547 * text region somewhere in the subpicture other than the first
548 * element in the region list, so just load it anyway as we'll
549 * probably want it sooner or later. */
550 if( !p_spu->p_text && p_region )
552 char *psz_modulename = NULL;
554 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
555 vlc_object_attach( p_spu->p_text, p_spu );
557 p_spu->p_text->fmt_out.video.i_width =
558 p_spu->p_text->fmt_out.video.i_visible_width =
560 p_spu->p_text->fmt_out.video.i_height =
561 p_spu->p_text->fmt_out.video.i_visible_height =
564 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
565 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
567 psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
568 if( psz_modulename && *psz_modulename )
570 p_spu->p_text->p_module =
571 module_Need( p_spu->p_text, "text renderer",
572 psz_modulename, VLC_TRUE );
574 if( !p_spu->p_text->p_module )
576 p_spu->p_text->p_module =
577 module_Need( p_spu->p_text, "text renderer", 0, 0 );
579 if( psz_modulename ) free( psz_modulename );
584 subpicture_region_t *p_text_region = p_subpic->p_region;
586 /* Only overwrite the size fields if the region is still in pre-rendered
587 * TEXT format. We have to traverse the subregion list because if more
588 * than one subregion is present, the text region isn't guarentteed to
589 * be the first in the list, and only text regions use this flag.
590 * All of this effort assists with the rescaling of text that has been
591 * rendered at native resolution, rather than video resolution.
593 while( p_text_region &&
594 ( p_text_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
596 p_text_region = p_text_region->p_next;
600 ( ( p_text_region->i_align & SUBPICTURE_RENDERED ) == 0 ) )
602 p_spu->p_text->fmt_out.video.i_width =
603 p_spu->p_text->fmt_out.video.i_visible_width =
605 p_spu->p_text->fmt_out.video.i_height =
606 p_spu->p_text->fmt_out.video.i_visible_height =
611 pi_scale_width[ SCALE_DEFAULT ] = i_scale_width_orig;
612 pi_scale_height[ SCALE_DEFAULT ] = i_scale_height_orig;
616 pi_scale_width[ SCALE_TEXT ] = ( p_fmt->i_width * 1000 ) /
617 p_spu->p_text->fmt_out.video.i_width;
618 pi_scale_height[ SCALE_TEXT ] = ( p_fmt->i_height * 1000 ) /
619 p_spu->p_text->fmt_out.video.i_height;
622 for( k = 0; k < SCALE_SIZE ; k++ )
624 if( (p_subpic->i_original_picture_height > 0) &&
625 (p_subpic->i_original_picture_width > 0) )
627 pi_scale_width[ k ] = pi_scale_width[ k ] * i_source_video_width /
628 p_subpic->i_original_picture_width;
629 pi_scale_height[ k ] = pi_scale_height[ k ] * i_source_video_height /
630 p_subpic->i_original_picture_height;
632 else if( p_subpic->i_original_picture_height > 0 )
634 pi_scale_height[ k ] = pi_scale_height[ k ] * i_source_video_height /
635 p_subpic->i_original_picture_height;
636 pi_scale_width[ k ] = pi_scale_width[ k ] * i_source_video_height /
637 p_subpic->i_original_picture_height;
641 /* Set default subpicture aspect ratio */
642 if( p_region && p_region->fmt.i_aspect &&
643 ( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den ) )
645 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
646 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
649 ( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den ) )
651 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
652 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
655 /* Take care of the aspect ratio */
657 ( ( p_region->fmt.i_sar_num * p_fmt->i_sar_den ) !=
658 ( p_region->fmt.i_sar_den * p_fmt->i_sar_num ) ) )
660 for( k = 0; k < SCALE_SIZE ; k++ )
662 pi_scale_width[ k ] = pi_scale_width[ k ] *
663 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
664 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
665 pi_subpic_x[ k ] = p_subpic->i_x * pi_scale_width[ k ] / 1000;
669 /* Load the scaling module */
670 if( !p_spu->p_scale &&
671 ((((pi_scale_width[ SCALE_TEXT ] > 0) || (pi_scale_height[ SCALE_TEXT ] > 0)) &&
672 ((pi_scale_width[ SCALE_TEXT ] != 1000) || (pi_scale_height[ SCALE_TEXT ] != 1000))) ||
673 (((pi_scale_width[ SCALE_DEFAULT ] > 0) || (pi_scale_height[ SCALE_DEFAULT ] > 0)) &&
674 ((pi_scale_width[ SCALE_DEFAULT ] != 1000) || (pi_scale_height[ SCALE_DEFAULT ] != 1000)))) )
676 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
677 vlc_object_attach( p_spu->p_scale, p_spu );
678 p_spu->p_scale->fmt_out.video.i_chroma =
679 p_spu->p_scale->fmt_in.video.i_chroma =
680 VLC_FOURCC('Y','U','V','P');
681 /* FIXME: We'll also be using it for YUVA and RGBA blending ... */
683 p_spu->p_scale->fmt_in.video.i_width =
684 p_spu->p_scale->fmt_in.video.i_height = 32;
685 p_spu->p_scale->fmt_out.video.i_width =
686 p_spu->p_scale->fmt_out.video.i_height = 16;
688 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
689 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
690 p_spu->p_scale->p_module =
691 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
694 while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
696 video_format_t orig_fmt = p_region->fmt;
697 vlc_bool_t b_rerender_text = VLC_FALSE;
698 int i_fade_alpha = 255;
701 int i_scale_idx = SCALE_DEFAULT;
702 int i_inv_scale_x = 1000;
703 int i_inv_scale_y = 1000;
705 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
707 if( p_spu->p_text && p_spu->p_text->p_module )
711 /* Setup 3 variables which can be used to render
712 * time-dependent text (and effects). The first indicates
713 * the total amount of time the text will be on screen,
714 * the second the amount of time it has already been on
715 * screen (can be a negative value as text is layed out
716 * before it is rendered) and the third is a feedback
717 * variable from the renderer - if the renderer sets it
718 * then this particular text is time-dependent, eg. the
719 * visual progress bar inside the text in karaoke and the
720 * text needs to be rendered multiple times in order for
721 * the effect to work - we therefore need to return the
722 * region to its original state at the end of the loop,
723 * instead of leaving it in YUVA or YUVP.
724 * Any renderer which is unaware of how to render
725 * time-dependent text can happily ignore the variables
726 * and render the text the same as usual - it should at
727 * least show up on screen, but the effect won't change
728 * the text over time.
731 var_Create( p_spu->p_text, "spu-duration", VLC_VAR_TIME );
732 val.i_time = p_subpic->i_stop - p_subpic->i_start;
733 var_Set( p_spu->p_text, "spu-duration", val );
735 var_Create( p_spu->p_text, "spu-elapsed", VLC_VAR_TIME );
736 val.i_time = mdate() - p_subpic->i_start;
737 var_Set( p_spu->p_text, "spu-elapsed", val );
739 var_Create( p_spu->p_text, "text-rerender", VLC_VAR_BOOL );
740 var_SetBool( p_spu->p_text, "text-rerender", VLC_FALSE );
742 var_Create( p_spu->p_text, "scale", VLC_VAR_INTEGER );
743 var_SetInteger( p_spu->p_text, "scale",
744 __MIN(i_scale_width_orig, i_scale_height_orig) );
746 if( p_spu->p_text->pf_render_html && p_region->psz_html )
748 p_spu->p_text->pf_render_html( p_spu->p_text,
749 p_region, p_region );
751 else if( p_spu->p_text->pf_render_text )
753 p_spu->p_text->pf_render_text( p_spu->p_text,
754 p_region, p_region );
756 b_rerender_text = var_GetBool( p_spu->p_text, "text-rerender" );
758 var_Destroy( p_spu->p_text, "spu-duration" );
759 var_Destroy( p_spu->p_text, "spu-elapsed" );
760 var_Destroy( p_spu->p_text, "text-rerender" );
761 var_Destroy( p_spu->p_text, "scale" );
763 p_region->i_align |= SUBPICTURE_RENDERED;
766 if( p_region->i_align & SUBPICTURE_RENDERED )
768 i_scale_idx = SCALE_TEXT;
769 i_inv_scale_x = i_scale_width_orig;
770 i_inv_scale_y = i_scale_height_orig;
773 i_x_offset = (p_region->i_x + pi_subpic_x[ i_scale_idx ]) * i_inv_scale_x / 1000;
774 i_y_offset = (p_region->i_y + p_subpic->i_y) * i_inv_scale_y / 1000;
776 /* Force palette if requested */
777 if( p_spu->b_force_palette &&
778 ( VLC_FOURCC('Y','U','V','P') == p_region->fmt.i_chroma ) )
780 memcpy( p_region->fmt.p_palette->palette,
781 p_spu->palette, 16 );
784 /* Scale SPU if necessary */
785 if( p_region->p_cache &&
786 ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
788 if( pi_scale_width[ i_scale_idx ] * p_region->fmt.i_width / 1000 !=
789 p_region->p_cache->fmt.i_width ||
790 pi_scale_height[ i_scale_idx ] * p_region->fmt.i_height / 1000 !=
791 p_region->p_cache->fmt.i_height )
793 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
795 p_region->p_cache = 0;
799 if( ( ( pi_scale_width[ i_scale_idx ] != 1000 ) ||
800 ( pi_scale_height[ i_scale_idx ] != 1000 ) ) &&
801 ( ( pi_scale_width[ i_scale_idx ] > 0 ) ||
802 ( pi_scale_height[ i_scale_idx ] > 0 ) ) &&
803 p_spu->p_scale && !p_region->p_cache &&
804 ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
808 p_spu->p_scale->fmt_in.video = p_region->fmt;
809 p_spu->p_scale->fmt_out.video = p_region->fmt;
812 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
813 &p_spu->p_scale->fmt_out.video );
814 if( p_spu->p_scale->fmt_out.video.p_palette )
815 *p_spu->p_scale->fmt_out.video.p_palette =
816 *p_region->fmt.p_palette;
817 p_region->p_cache->p_next = p_region->p_next;
819 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
820 &p_region->picture );
822 p_spu->p_scale->fmt_out.video.i_width =
823 p_region->fmt.i_width * pi_scale_width[ i_scale_idx ] / 1000;
824 p_spu->p_scale->fmt_out.video.i_visible_width =
825 p_region->fmt.i_visible_width * pi_scale_width[ i_scale_idx ] / 1000;
826 p_spu->p_scale->fmt_out.video.i_height =
827 p_region->fmt.i_height * pi_scale_height[ i_scale_idx ] / 1000;
828 p_spu->p_scale->fmt_out.video.i_visible_height =
829 p_region->fmt.i_visible_height * pi_scale_height[ i_scale_idx ] / 1000;
830 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
831 p_region->p_cache->i_x = p_region->i_x * pi_scale_width[ i_scale_idx ] / 1000;
832 p_region->p_cache->i_y = p_region->i_y * pi_scale_height[ i_scale_idx ] / 1000;
833 p_region->p_cache->i_align = p_region->i_align;
835 p_pic = p_spu->p_scale->pf_video_filter(
836 p_spu->p_scale, &p_region->p_cache->picture );
839 picture_t p_pic_tmp = p_region->p_cache->picture;
840 p_region->p_cache->picture = *p_pic;
846 if( ( ( pi_scale_width[ i_scale_idx ] != 1000 ) ||
847 ( pi_scale_height[ i_scale_idx ] != 1000 ) ) &&
848 ( ( pi_scale_width[ i_scale_idx ] > 0 ) ||
849 ( pi_scale_height[ i_scale_idx ] > 0 ) ) &&
850 p_spu->p_scale && p_region->p_cache &&
851 ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
853 p_region = p_region->p_cache;
856 if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
858 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
859 (p_subpic->i_y + p_region->i_y) * i_inv_scale_y / 1000;
861 else if ( !(p_region->i_align & SUBPICTURE_ALIGN_TOP) )
863 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
866 if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
868 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
869 (pi_subpic_x[ i_scale_idx ] + p_region->i_x)
870 * i_inv_scale_x / 1000;
872 else if ( !(p_region->i_align & SUBPICTURE_ALIGN_LEFT) )
874 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
877 if( p_subpic->b_absolute )
879 i_x_offset = (p_region->i_x +
880 pi_subpic_x[ i_scale_idx ] *
881 pi_scale_width[ i_scale_idx ] / 1000)
882 * i_inv_scale_x / 1000;
883 i_y_offset = (p_region->i_y +
884 p_subpic->i_y * pi_scale_height[ i_scale_idx ] / 1000)
885 * i_inv_scale_y / 1000;
889 i_x_offset = __MAX( i_x_offset, 0 );
890 i_y_offset = __MAX( i_y_offset, 0 );
892 if( ( p_spu->i_margin != 0 ) &&
893 ( p_spu->b_force_crop == VLC_FALSE ) )
896 int i_low = (i_y_offset - p_spu->i_margin) * i_inv_scale_y / 1000;
897 int i_high = i_low + p_region->fmt.i_height;
899 /* crop extra margin to keep within bounds */
902 if( i_high > (int)p_fmt->i_height )
903 i_diff = i_high - p_fmt->i_height;
904 i_y_offset -= ( p_spu->i_margin * i_inv_scale_y / 1000 + i_diff );
907 p_spu->p_blend->fmt_in.video = p_region->fmt;
909 /* Force cropping if requested */
910 if( p_spu->b_force_crop )
912 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
913 int i_crop_x = p_spu->i_crop_x * pi_scale_width[ i_scale_idx ] / 1000
914 * i_inv_scale_x / 1000;
915 int i_crop_y = p_spu->i_crop_y * pi_scale_height[ i_scale_idx ] / 1000
916 * i_inv_scale_y / 1000;
917 int i_crop_width = p_spu->i_crop_width * pi_scale_width[ i_scale_idx ] / 1000
918 * i_inv_scale_x / 1000;
919 int i_crop_height = p_spu->i_crop_height * pi_scale_height[ i_scale_idx ] / 1000
920 * i_inv_scale_y / 1000;
922 /* Find the intersection */
923 if( i_crop_x + i_crop_width <= i_x_offset ||
924 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
925 i_crop_y + i_crop_height <= i_y_offset ||
926 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
928 /* No intersection */
929 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
933 int i_x, i_y, i_x_end, i_y_end;
934 i_x = __MAX( i_crop_x, i_x_offset );
935 i_y = __MAX( i_crop_y, i_y_offset );
936 i_x_end = __MIN( i_crop_x + i_crop_width,
937 i_x_offset + (int)p_fmt->i_visible_width );
938 i_y_end = __MIN( i_crop_y + i_crop_height,
939 i_y_offset + (int)p_fmt->i_visible_height );
941 p_fmt->i_x_offset = i_x - i_x_offset;
942 p_fmt->i_y_offset = i_y - i_y_offset;
943 p_fmt->i_visible_width = i_x_end - i_x;
944 p_fmt->i_visible_height = i_y_end - i_y;
951 if( p_subpic->b_fade )
953 mtime_t i_fade_start = ( p_subpic->i_stop +
954 p_subpic->i_start ) / 2;
955 mtime_t i_now = mdate();
956 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
958 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
959 ( p_subpic->i_stop - i_fade_start );
963 i_x_offset = __MAX( i_x_offset, 0 );
964 i_y_offset = __MAX( i_y_offset, 0 );
966 if( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') )
968 /* Update the output picture size */
969 p_spu->p_blend->fmt_out.video.i_width =
970 p_spu->p_blend->fmt_out.video.i_visible_width =
972 p_spu->p_blend->fmt_out.video.i_height =
973 p_spu->p_blend->fmt_out.video.i_visible_height =
976 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
977 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
978 i_fade_alpha * p_subpic->i_alpha / 255 );
981 if( b_rerender_text )
983 /* Some forms of subtitles need to be re-rendered more than
984 * once, eg. karaoke. We therefore restore the region to its
985 * pre-rendered state, so the next time through everything is
988 p_region->picture.pf_release( &p_region->picture );
989 memset( &p_region->picture, 0, sizeof( picture_t ) );
990 p_region->fmt = orig_fmt;
991 p_region->i_align &= ~SUBPICTURE_RENDERED;
993 p_region = p_region->p_next;
996 p_subpic = p_subpic->p_next;
999 vlc_mutex_unlock( &p_spu->subpicture_lock );
1002 /*****************************************************************************
1003 * spu_SortSubpictures: find the subpictures to display
1004 *****************************************************************************
1005 * This function parses all subpictures and decides which ones need to be
1006 * displayed. This operation does not need lock, since only READY_SUBPICTURE
1007 * are handled. If no picture has been selected, display_date will depend on
1009 * We also check for ephemer DVD subpictures (subpictures that have
1010 * to be removed if a newer one is available), which makes it a lot
1011 * more difficult to guess if a subpicture has to be rendered or not.
1012 *****************************************************************************/
1013 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1014 vlc_bool_t b_paused )
1016 int i_index, i_channel;
1017 subpicture_t *p_subpic = NULL;
1018 subpicture_t *p_ephemer;
1019 mtime_t ephemer_date;
1021 /* Run subpicture filters */
1022 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
1024 subpicture_t *p_subpic_filter;
1025 p_subpic_filter = p_spu->pp_filter[i_index]->
1026 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
1027 if( p_subpic_filter )
1029 spu_DisplaySubpicture( p_spu, p_subpic_filter );
1033 /* We get an easily parsable chained list of subpictures which
1034 * ends with NULL since p_subpic was initialized to NULL. */
1035 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
1040 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1042 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
1043 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
1048 display_date < p_spu->p_subpicture[i_index].i_start )
1050 /* Too early, come back next monday */
1054 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
1055 ephemer_date = p_spu->p_subpicture[i_index].i_start;
1057 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
1058 ( !p_spu->p_subpicture[i_index].b_ephemer ||
1059 p_spu->p_subpicture[i_index].i_stop >
1060 p_spu->p_subpicture[i_index].i_start ) &&
1061 !( p_spu->p_subpicture[i_index].b_pausable &&
1064 /* Too late, destroy the subpic */
1065 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
1069 /* If this is an ephemer subpic, add it to our list */
1070 if( p_spu->p_subpicture[i_index].b_ephemer )
1072 p_spu->p_subpicture[i_index].p_next = p_ephemer;
1073 p_ephemer = &p_spu->p_subpicture[i_index];
1078 p_spu->p_subpicture[i_index].p_next = p_subpic;
1079 p_subpic = &p_spu->p_subpicture[i_index];
1082 /* If we found ephemer subpictures, check if they have to be
1083 * displayed or destroyed */
1084 while( p_ephemer != NULL )
1086 subpicture_t *p_tmp = p_ephemer;
1087 p_ephemer = p_ephemer->p_next;
1089 if( p_tmp->i_start < ephemer_date )
1091 /* Ephemer subpicture has lived too long */
1092 spu_DestroySubpicture( p_spu, p_tmp );
1096 /* Ephemer subpicture can still live a bit */
1097 p_tmp->p_next = p_subpic;
1106 /*****************************************************************************
1107 * SpuClearChannel: clear an spu channel
1108 *****************************************************************************
1109 * This function destroys the subpictures which belong to the spu channel
1110 * corresponding to i_channel_id.
1111 *****************************************************************************/
1112 static void SpuClearChannel( spu_t *p_spu, int i_channel )
1114 int i_subpic; /* subpicture index */
1115 subpicture_t *p_subpic = NULL; /* first free subpicture */
1117 vlc_mutex_lock( &p_spu->subpicture_lock );
1119 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1121 p_subpic = &p_spu->p_subpicture[i_subpic];
1122 if( p_subpic->i_status == FREE_SUBPICTURE
1123 || ( p_subpic->i_status != RESERVED_SUBPICTURE
1124 && p_subpic->i_status != READY_SUBPICTURE ) )
1129 if( p_subpic->i_channel == i_channel )
1131 while( p_subpic->p_region )
1133 subpicture_region_t *p_region = p_subpic->p_region;
1134 p_subpic->p_region = p_region->p_next;
1135 spu_DestroyRegion( p_spu, p_region );
1138 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1139 p_subpic->i_status = FREE_SUBPICTURE;
1143 vlc_mutex_unlock( &p_spu->subpicture_lock );
1146 /*****************************************************************************
1147 * spu_ControlDefault: default methods for the subpicture unit control.
1148 *****************************************************************************/
1149 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1155 case SPU_CHANNEL_REGISTER:
1156 pi = (int *)va_arg( args, int * );
1157 if( pi ) *pi = p_spu->i_channel++;
1160 case SPU_CHANNEL_CLEAR:
1161 i = (int)va_arg( args, int );
1162 SpuClearChannel( p_spu, i );
1166 msg_Dbg( p_spu, "control query not supported" );
1167 return VLC_EGENERIC;
1173 /*****************************************************************************
1174 * Object variables callbacks
1175 *****************************************************************************/
1177 /*****************************************************************************
1178 * UpdateSPU: update subpicture settings
1179 *****************************************************************************
1180 * This function is called from CropCallback and at initialization time, to
1181 * retrieve crop information from the input.
1182 *****************************************************************************/
1183 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1187 p_spu->b_force_palette = VLC_FALSE;
1188 p_spu->b_force_crop = VLC_FALSE;
1190 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1192 p_spu->b_force_crop = VLC_TRUE;
1193 var_Get( p_object, "x-start", &val );
1194 p_spu->i_crop_x = val.i_int;
1195 var_Get( p_object, "y-start", &val );
1196 p_spu->i_crop_y = val.i_int;
1197 var_Get( p_object, "x-end", &val );
1198 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1199 var_Get( p_object, "y-end", &val );
1200 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1202 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1204 memcpy( p_spu->palette, val.p_address, 16 );
1205 p_spu->b_force_palette = VLC_TRUE;
1208 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1209 p_spu->i_crop_x, p_spu->i_crop_y,
1210 p_spu->i_crop_width, p_spu->i_crop_height,
1211 p_spu->b_force_palette );
1214 /*****************************************************************************
1215 * CropCallback: called when the highlight properties are changed
1216 *****************************************************************************
1217 * This callback is called from the input thread when we need cropping
1218 *****************************************************************************/
1219 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1220 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1222 (void)psz_var; (void)oldval; (void)newval;
1223 UpdateSPU( (spu_t *)p_data, p_object );
1227 /*****************************************************************************
1228 * Buffers allocation callbacks for the filters
1229 *****************************************************************************/
1230 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1232 filter_owner_sys_t *p_sys = p_filter->p_owner;
1233 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1234 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1235 return p_subpicture;
1238 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1240 filter_owner_sys_t *p_sys = p_filter->p_owner;
1241 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1244 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1246 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1248 memset( p_subpic, 0, sizeof(subpicture_t) );
1249 p_subpic->b_absolute = VLC_TRUE;
1251 p_subpic->pf_create_region = __spu_CreateRegion;
1252 p_subpic->pf_make_region = __spu_MakeRegion;
1253 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1258 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1260 while( p_subpic->p_region )
1262 subpicture_region_t *p_region = p_subpic->p_region;
1263 p_subpic->p_region = p_region->p_next;
1264 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1270 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1272 picture_t *p_picture = malloc( sizeof(picture_t) );
1274 if( vout_AllocatePicture( p_filter, p_picture,
1275 p_filter->fmt_out.video.i_chroma,
1276 p_filter->fmt_out.video.i_width,
1277 p_filter->fmt_out.video.i_height,
1278 p_filter->fmt_out.video.i_aspect )
1285 p_picture->pf_release = RegionPictureRelease;
1290 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1293 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1294 if( p_pic ) free( p_pic );
1297 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1298 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1300 (void)p_object; (void)oldval; (void)newval;
1302 if( !strcmp( psz_var, "sub-filter" ) )
1304 spu_t *p_spu = (spu_t *)p_data;
1305 vlc_mutex_lock( &p_spu->subpicture_lock );
1306 spu_DeleteChain( p_spu );
1307 spu_ParseChain( p_spu );
1308 vlc_mutex_unlock( &p_spu->subpicture_lock );