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"
40 /*****************************************************************************
42 *****************************************************************************/
43 static void UpdateSPU ( spu_t *, vlc_object_t * );
44 static int CropCallback( vlc_object_t *, char const *,
45 vlc_value_t, vlc_value_t, void * );
47 static int spu_vaControlDefault( spu_t *, int, va_list );
49 static subpicture_t *sub_new_buffer( filter_t * );
50 static void sub_del_buffer( filter_t *, subpicture_t * );
51 static subpicture_t *spu_new_buffer( filter_t * );
52 static void spu_del_buffer( filter_t *, subpicture_t * );
53 static picture_t *spu_new_video_buffer( filter_t * );
54 static void spu_del_video_buffer( filter_t *, picture_t * );
56 static int spu_ParseChain( spu_t * );
57 static void spu_DeleteChain( spu_t * );
58 static int SubFilterCallback( vlc_object_t *, char const *,
59 vlc_value_t, vlc_value_t, void * );
61 static int sub_filter_allocation_init( filter_t *, void * );
62 static void sub_filter_allocation_clear( filter_t * );
63 struct filter_owner_sys_t
76 * Creates the subpicture unit
78 * \param p_this the parent object which creates the subpicture unit
80 spu_t *__spu_Create( vlc_object_t *p_this )
83 spu_t *p_spu = vlc_custom_create( p_this, sizeof( spu_t ),
84 VLC_OBJECT_GENERIC, "subpicture" );
86 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
88 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
91 p_spu->p_blend = NULL;
93 p_spu->p_scale = NULL;
94 p_spu->pf_control = spu_vaControlDefault;
96 /* Register the default subpicture channel */
99 vlc_mutex_init( &p_spu->subpicture_lock );
101 vlc_object_attach( p_spu, p_this );
103 p_spu->p_chain = filter_chain_New( p_spu, "sub filter", false,
104 sub_filter_allocation_init,
105 sub_filter_allocation_clear,
111 * Initialise the subpicture unit
113 * \param p_spu the subpicture unit object
115 int spu_Init( spu_t *p_spu )
119 /* If the user requested a sub margin, we force the position. */
120 var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
121 var_Get( p_spu, "sub-margin", &val );
122 p_spu->i_margin = val.i_int;
124 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
125 var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
127 spu_ParseChain( p_spu );
132 int spu_ParseChain( spu_t *p_spu )
134 char *psz_parser = var_GetString( p_spu, "sub-filter" );
135 if( filter_chain_AppendFromString( p_spu->p_chain, psz_parser ) < 0 )
146 * Destroy the subpicture unit
148 * \param p_this the parent object which destroys the subpicture unit
150 void spu_Destroy( spu_t *p_spu )
154 vlc_object_detach( p_spu );
156 /* Destroy all remaining subpictures */
157 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
159 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
161 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
167 if( p_spu->p_blend->p_module )
168 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
170 vlc_object_detach( p_spu->p_blend );
171 vlc_object_release( p_spu->p_blend );
176 if( p_spu->p_text->p_module )
177 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
179 vlc_object_detach( p_spu->p_text );
180 vlc_object_release( p_spu->p_text );
185 if( p_spu->p_scale->p_module )
186 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
188 vlc_object_detach( p_spu->p_scale );
189 vlc_object_release( p_spu->p_scale );
192 spu_DeleteChain( p_spu );
194 vlc_mutex_destroy( &p_spu->subpicture_lock );
195 vlc_object_release( p_spu );
198 static void spu_DeleteChain( spu_t *p_spu )
200 filter_chain_Delete( p_spu->p_chain );
204 * Attach/Detach the SPU from any input
206 * \param p_this the object in which to destroy the subpicture unit
207 * \param b_attach to select attach or detach
209 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
211 vlc_object_t *p_input;
213 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
214 if( !p_input ) return;
218 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
219 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
220 vlc_object_release( p_input );
224 /* Delete callback */
225 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
226 vlc_object_release( p_input );
231 * Create a subpicture region
233 * \param p_this vlc_object_t
234 * \param p_fmt the format that this subpicture region should have
236 static void RegionPictureRelease( picture_t *p_pic )
238 free( p_pic->p_data_orig );
240 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
241 video_format_t *p_fmt )
243 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
244 if( !p_region ) return NULL;
246 memset( p_region, 0, sizeof(subpicture_region_t) );
247 p_region->i_alpha = 0xff;
248 p_region->p_next = NULL;
249 p_region->p_cache = NULL;
250 p_region->fmt = *p_fmt;
251 p_region->psz_text = NULL;
252 p_region->p_style = NULL;
254 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
255 p_fmt->p_palette = p_region->fmt.p_palette =
256 malloc( sizeof(video_palette_t) );
257 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
259 p_region->picture.p_data_orig = NULL;
261 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
263 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
264 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
266 if( !p_region->picture.i_planes )
269 free( p_fmt->p_palette );
273 p_region->picture.pf_release = RegionPictureRelease;
279 * Make a subpicture region from an existing picture_t
281 * \param p_this vlc_object_t
282 * \param p_fmt the format that this subpicture region should have
283 * \param p_pic a pointer to the picture creating the region (not freed)
285 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
286 video_format_t *p_fmt,
289 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
291 if( !p_region ) return NULL;
292 memset( p_region, 0, sizeof(subpicture_region_t) );
293 p_region->i_alpha = 0xff;
294 p_region->p_next = 0;
295 p_region->p_cache = 0;
296 p_region->fmt = *p_fmt;
297 p_region->psz_text = 0;
298 p_region->p_style = NULL;
300 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
301 p_fmt->p_palette = p_region->fmt.p_palette =
302 malloc( sizeof(video_palette_t) );
303 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
305 memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
306 p_region->picture.pf_release = RegionPictureRelease;
312 * Destroy a subpicture region
314 * \param p_this vlc_object_t
315 * \param p_region the subpicture region to destroy
317 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
319 if( !p_region ) return;
320 if( p_region->picture.pf_release )
321 p_region->picture.pf_release( &p_region->picture );
322 free( p_region->fmt.p_palette );
323 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
325 free( p_region->psz_text );
326 free( p_region->psz_html );
327 //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
332 * Display a subpicture
334 * Remove the reservation flag of a subpicture, which will cause it to be
336 * \param p_spu the subpicture unit object
337 * \param p_subpic the subpicture to display
339 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
341 /* Check if status is valid */
342 if( p_subpic->i_status != RESERVED_SUBPICTURE )
344 msg_Err( p_spu, "subpicture %p has invalid status #%d",
345 p_subpic, p_subpic->i_status );
348 /* Remove reservation flag */
349 p_subpic->i_status = READY_SUBPICTURE;
351 if( p_subpic->i_channel == DEFAULT_CHAN )
353 p_subpic->i_channel = 0xFFFF;
354 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
355 p_subpic->i_channel = DEFAULT_CHAN;
360 * Allocate a subpicture in the spu heap.
362 * This function create a reserved subpicture in the spu heap.
363 * A null pointer is returned if the function fails. This method provides an
364 * already allocated zone of memory in the spu data fields. It needs locking
365 * since several pictures can be created by several producers threads.
366 * \param p_spu the subpicture unit in which to create the subpicture
367 * \return NULL on error, a reserved subpicture otherwise
369 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
371 int i_subpic; /* subpicture index */
372 subpicture_t * p_subpic = NULL; /* first free subpicture */
375 vlc_mutex_lock( &p_spu->subpicture_lock );
378 * Look for an empty place
381 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
383 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
385 /* Subpicture is empty and ready for allocation */
386 p_subpic = &p_spu->p_subpicture[i_subpic];
387 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
392 /* If no free subpicture could be found */
393 if( p_subpic == NULL )
395 msg_Err( p_spu, "subpicture heap is full" );
396 vlc_mutex_unlock( &p_spu->subpicture_lock );
400 /* Copy subpicture information, set some default values */
401 memset( p_subpic, 0, sizeof(subpicture_t) );
402 p_subpic->i_status = RESERVED_SUBPICTURE;
403 p_subpic->b_absolute = true;
404 p_subpic->b_pausable = false;
405 p_subpic->b_fade = false;
406 p_subpic->i_alpha = 0xFF;
407 p_subpic->p_region = NULL;
408 p_subpic->pf_render = NULL;
409 p_subpic->pf_destroy = NULL;
410 p_subpic->p_sys = NULL;
411 vlc_mutex_unlock( &p_spu->subpicture_lock );
413 p_subpic->pf_create_region = __spu_CreateRegion;
414 p_subpic->pf_make_region = __spu_MakeRegion;
415 p_subpic->pf_destroy_region = __spu_DestroyRegion;
421 * Remove a subpicture from the heap
423 * This function frees a previously reserved subpicture.
424 * It is meant to be used when the construction of a picture aborted.
425 * This function does not need locking since reserved subpictures are ignored
428 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
431 vlc_mutex_lock( &p_spu->subpicture_lock );
433 /* There can be race conditions so we need to check the status */
434 if( p_subpic->i_status == FREE_SUBPICTURE )
436 vlc_mutex_unlock( &p_spu->subpicture_lock );
440 /* Check if status is valid */
441 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
442 && ( p_subpic->i_status != READY_SUBPICTURE ) )
444 msg_Err( p_spu, "subpicture %p has invalid status %d",
445 p_subpic, p_subpic->i_status );
448 while( p_subpic->p_region )
450 subpicture_region_t *p_region = p_subpic->p_region;
451 p_subpic->p_region = p_region->p_next;
452 spu_DestroyRegion( p_spu, p_region );
455 if( p_subpic->pf_destroy )
457 p_subpic->pf_destroy( p_subpic );
460 p_subpic->i_status = FREE_SUBPICTURE;
462 vlc_mutex_unlock( &p_spu->subpicture_lock );
465 /*****************************************************************************
466 * spu_RenderSubpictures: render a subpicture list
467 *****************************************************************************
468 * This function renders all sub picture units in the list.
469 *****************************************************************************/
470 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
471 picture_t *p_pic_dst, picture_t *p_pic_src,
472 subpicture_t *p_subpic,
473 int i_scale_width_orig, int i_scale_height_orig )
475 int i_source_video_width;
476 int i_source_video_height;
477 subpicture_t *p_subpic_v = p_subpic;
480 vlc_mutex_lock( &p_spu->subpicture_lock );
482 for( p_subpic_v = p_subpic;
483 p_subpic_v != NULL && p_subpic_v->i_status != FREE_SUBPICTURE;
484 p_subpic_v = p_subpic_v->p_next )
486 if( p_subpic_v->pf_pre_render )
488 p_subpic_v->pf_pre_render( p_fmt, p_spu, p_subpic_v, mdate() );
492 if( i_scale_width_orig <= 0 )
493 i_scale_width_orig = 1;
494 if( i_scale_height_orig <= 0 )
495 i_scale_height_orig = 1;
497 i_source_video_width = p_fmt->i_width * 1000 / i_scale_width_orig;
498 i_source_video_height = p_fmt->i_height * 1000 / i_scale_height_orig;
500 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
501 while( ( p_subpic != NULL ) && ( p_subpic->i_status != FREE_SUBPICTURE ) )
503 subpicture_region_t *p_region;
504 int pi_scale_width[ SCALE_SIZE ];
505 int pi_scale_height[ SCALE_SIZE ];
506 int pi_subpic_x[ SCALE_SIZE ];
509 /* If the source video and subtitles stream agree on the size of
510 * the video then disregard all further references to the subtitle
513 if( ( i_source_video_height == p_subpic->i_original_picture_height ) &&
514 ( i_source_video_width == p_subpic->i_original_picture_width ) )
516 p_subpic->i_original_picture_height = 0;
517 p_subpic->i_original_picture_width = 0;
520 for( k = 0; k < SCALE_SIZE ; k++ )
521 pi_subpic_x[ k ] = p_subpic->i_x;
523 if( p_subpic->pf_update_regions )
525 if ( p_subpic->p_region ) {
526 spu_DestroyRegion( p_spu, p_subpic->p_region );
528 p_subpic->p_region = p_region = p_subpic->pf_update_regions( p_fmt, p_spu, p_subpic, mdate() );
532 p_region = p_subpic->p_region;
535 /* Load the blending module */
536 if( !p_spu->p_blend && p_region )
538 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
539 vlc_object_attach( p_spu->p_blend, p_spu );
540 p_spu->p_blend->fmt_out.video.i_x_offset =
541 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
542 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
543 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
545 /* The blend module will be loaded when needed with the real
547 memset( &p_spu->p_blend->fmt_in, 0, sizeof(p_spu->p_blend->fmt_in) );
548 p_spu->p_blend->p_module = NULL;
551 /* Load the text rendering module; it is possible there is a
552 * text region somewhere in the subpicture other than the first
553 * element in the region list, so just load it anyway as we'll
554 * probably want it sooner or later. */
555 if( !p_spu->p_text && p_region )
557 char *psz_modulename = NULL;
559 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
560 vlc_object_attach( p_spu->p_text, p_spu );
562 p_spu->p_text->fmt_out.video.i_width =
563 p_spu->p_text->fmt_out.video.i_visible_width =
565 p_spu->p_text->fmt_out.video.i_height =
566 p_spu->p_text->fmt_out.video.i_visible_height =
569 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
570 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
572 psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
573 if( psz_modulename && *psz_modulename )
575 p_spu->p_text->p_module =
576 module_Need( p_spu->p_text, "text renderer",
577 psz_modulename, true );
579 if( !p_spu->p_text->p_module )
581 p_spu->p_text->p_module =
582 module_Need( p_spu->p_text, "text renderer", 0, 0 );
584 free( psz_modulename );
589 subpicture_region_t *p_text_region = p_subpic->p_region;
591 /* Only overwrite the size fields if the region is still in
592 * pre-rendered TEXT format. We have to traverse the subregion
593 * list because if more than one subregion is present, the text
594 * region isn't guarentteed to be the first in the list, and
595 * only text regions use this flag. All of this effort assists
596 * with the rescaling of text that has been rendered at native
597 * resolution, rather than video resolution.
599 while( p_text_region &&
600 ( p_text_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
602 p_text_region = p_text_region->p_next;
606 ( ( p_text_region->i_align & SUBPICTURE_RENDERED ) == 0 ) )
608 if( (p_subpic->i_original_picture_height > 0) &&
609 (p_subpic->i_original_picture_width > 0) )
611 p_spu->p_text->fmt_out.video.i_width =
612 p_spu->p_text->fmt_out.video.i_visible_width =
613 p_subpic->i_original_picture_width;
614 p_spu->p_text->fmt_out.video.i_height =
615 p_spu->p_text->fmt_out.video.i_visible_height =
616 p_subpic->i_original_picture_height;
620 p_spu->p_text->fmt_out.video.i_width =
621 p_spu->p_text->fmt_out.video.i_visible_width =
623 p_spu->p_text->fmt_out.video.i_height =
624 p_spu->p_text->fmt_out.video.i_visible_height =
630 pi_scale_width[ SCALE_DEFAULT ] = i_scale_width_orig;
631 pi_scale_height[ SCALE_DEFAULT ] = i_scale_height_orig;
635 pi_scale_width[ SCALE_TEXT ] = ( p_fmt->i_width * 1000 ) /
636 p_spu->p_text->fmt_out.video.i_width;
637 pi_scale_height[ SCALE_TEXT ] = ( p_fmt->i_height * 1000 ) /
638 p_spu->p_text->fmt_out.video.i_height;
640 /* If we have an explicit size plane to render to, then turn off
641 * the fontsize rescaling.
643 if( (p_subpic->i_original_picture_height > 0) &&
644 (p_subpic->i_original_picture_width > 0) )
646 i_scale_width_orig = 1000;
647 i_scale_height_orig = 1000;
650 for( k = 0; k < SCALE_SIZE ; k++ )
652 /* Case of both width and height being specified has been dealt
653 * with above by instead rendering to an output pane of the
654 * explicit dimensions specified - we don't need to scale it.
656 if( (p_subpic->i_original_picture_height > 0) &&
657 (p_subpic->i_original_picture_width <= 0) )
659 pi_scale_height[ k ] = pi_scale_height[ k ] * i_source_video_height /
660 p_subpic->i_original_picture_height;
661 pi_scale_width[ k ] = pi_scale_width[ k ] * i_source_video_height /
662 p_subpic->i_original_picture_height;
666 /* Set default subpicture aspect ratio */
667 if( p_region && p_region->fmt.i_aspect &&
668 ( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den ) )
670 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
671 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
674 ( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den ) )
676 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
677 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
680 /* Take care of the aspect ratio */
682 ( ( p_region->fmt.i_sar_num * p_fmt->i_sar_den ) !=
683 ( p_region->fmt.i_sar_den * p_fmt->i_sar_num ) ) )
685 for( k = 0; k < SCALE_SIZE ; k++ )
687 pi_scale_width[ k ] = pi_scale_width[ k ] *
688 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
689 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
690 pi_subpic_x[ k ] = p_subpic->i_x * pi_scale_width[ k ] / 1000;
694 /* Load the scaling module */
695 if( !p_spu->p_scale &&
696 ((((pi_scale_width[ SCALE_TEXT ] > 0) || (pi_scale_height[ SCALE_TEXT ] > 0)) &&
697 ((pi_scale_width[ SCALE_TEXT ] != 1000) || (pi_scale_height[ SCALE_TEXT ] != 1000))) ||
698 (((pi_scale_width[ SCALE_DEFAULT ] > 0) || (pi_scale_height[ SCALE_DEFAULT ] > 0)) &&
699 ((pi_scale_width[ SCALE_DEFAULT ] != 1000) || (pi_scale_height[ SCALE_DEFAULT ] != 1000)))) )
701 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
702 vlc_object_attach( p_spu->p_scale, p_spu );
703 p_spu->p_scale->fmt_out.video.i_chroma =
704 p_spu->p_scale->fmt_in.video.i_chroma =
705 VLC_FOURCC('Y','U','V','P');
706 /* FIXME: We'll also be using it for YUVA and RGBA blending ... */
708 p_spu->p_scale->fmt_in.video.i_width =
709 p_spu->p_scale->fmt_in.video.i_height = 32;
710 p_spu->p_scale->fmt_out.video.i_width =
711 p_spu->p_scale->fmt_out.video.i_height = 16;
713 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
714 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
715 p_spu->p_scale->p_module =
716 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
721 video_format_t orig_fmt = p_region->fmt;
722 bool b_rerender_text = false;
723 int i_fade_alpha = 255;
726 int i_scale_idx = SCALE_DEFAULT;
727 int i_inv_scale_x = 1000;
728 int i_inv_scale_y = 1000;
730 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
732 if( p_spu->p_text && p_spu->p_text->p_module )
736 /* Setup 3 variables which can be used to render
737 * time-dependent text (and effects). The first indicates
738 * the total amount of time the text will be on screen,
739 * the second the amount of time it has already been on
740 * screen (can be a negative value as text is layed out
741 * before it is rendered) and the third is a feedback
742 * variable from the renderer - if the renderer sets it
743 * then this particular text is time-dependent, eg. the
744 * visual progress bar inside the text in karaoke and the
745 * text needs to be rendered multiple times in order for
746 * the effect to work - we therefore need to return the
747 * region to its original state at the end of the loop,
748 * instead of leaving it in YUVA or YUVP.
749 * Any renderer which is unaware of how to render
750 * time-dependent text can happily ignore the variables
751 * and render the text the same as usual - it should at
752 * least show up on screen, but the effect won't change
753 * the text over time.
756 var_Create( p_spu->p_text, "spu-duration", VLC_VAR_TIME );
757 val.i_time = p_subpic->i_stop - p_subpic->i_start;
758 var_Set( p_spu->p_text, "spu-duration", val );
760 var_Create( p_spu->p_text, "spu-elapsed", VLC_VAR_TIME );
761 val.i_time = mdate() - p_subpic->i_start;
762 var_Set( p_spu->p_text, "spu-elapsed", val );
764 var_Create( p_spu->p_text, "text-rerender", VLC_VAR_BOOL );
765 var_SetBool( p_spu->p_text, "text-rerender", false );
767 var_Create( p_spu->p_text, "scale", VLC_VAR_INTEGER );
768 var_SetInteger( p_spu->p_text, "scale",
769 __MIN(i_scale_width_orig, i_scale_height_orig) );
771 if( p_spu->p_text->pf_render_html && p_region->psz_html )
773 p_spu->p_text->pf_render_html( p_spu->p_text,
774 p_region, p_region );
776 else if( p_spu->p_text->pf_render_text )
778 p_spu->p_text->pf_render_text( p_spu->p_text,
779 p_region, p_region );
781 b_rerender_text = var_GetBool( p_spu->p_text, "text-rerender" );
783 var_Destroy( p_spu->p_text, "spu-duration" );
784 var_Destroy( p_spu->p_text, "spu-elapsed" );
785 var_Destroy( p_spu->p_text, "text-rerender" );
786 var_Destroy( p_spu->p_text, "scale" );
788 p_region->i_align |= SUBPICTURE_RENDERED;
791 if( p_region->i_align & SUBPICTURE_RENDERED )
793 i_scale_idx = SCALE_TEXT;
794 i_inv_scale_x = i_scale_width_orig;
795 i_inv_scale_y = i_scale_height_orig;
798 i_x_offset = (p_region->i_x + pi_subpic_x[ i_scale_idx ]) * i_inv_scale_x / 1000;
799 i_y_offset = (p_region->i_y + p_subpic->i_y) * i_inv_scale_y / 1000;
801 /* Force palette if requested */
802 if( p_spu->b_force_palette &&
803 ( VLC_FOURCC('Y','U','V','P') == p_region->fmt.i_chroma ) )
805 memcpy( p_region->fmt.p_palette->palette,
806 p_spu->palette, 16 );
809 /* Scale SPU if necessary */
810 if( p_region->p_cache &&
811 ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
813 if( pi_scale_width[ i_scale_idx ] * p_region->fmt.i_width / 1000 !=
814 p_region->p_cache->fmt.i_width ||
815 pi_scale_height[ i_scale_idx ] * p_region->fmt.i_height / 1000 !=
816 p_region->p_cache->fmt.i_height )
818 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
820 p_region->p_cache = 0;
824 if( ( ( pi_scale_width[ i_scale_idx ] != 1000 ) ||
825 ( pi_scale_height[ i_scale_idx ] != 1000 ) ) &&
826 ( ( pi_scale_width[ i_scale_idx ] > 0 ) ||
827 ( pi_scale_height[ i_scale_idx ] > 0 ) ) &&
828 p_spu->p_scale && !p_region->p_cache &&
829 ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
833 p_spu->p_scale->fmt_in.video = p_region->fmt;
834 p_spu->p_scale->fmt_out.video = p_region->fmt;
837 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
838 &p_spu->p_scale->fmt_out.video );
839 if( p_spu->p_scale->fmt_out.video.p_palette )
840 *p_spu->p_scale->fmt_out.video.p_palette =
841 *p_region->fmt.p_palette;
842 p_region->p_cache->p_next = p_region->p_next;
844 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
845 &p_region->picture );
847 p_spu->p_scale->fmt_out.video.i_width =
848 p_region->fmt.i_width * pi_scale_width[ i_scale_idx ] / 1000;
849 p_spu->p_scale->fmt_out.video.i_visible_width =
850 p_region->fmt.i_visible_width * pi_scale_width[ i_scale_idx ] / 1000;
851 p_spu->p_scale->fmt_out.video.i_height =
852 p_region->fmt.i_height * pi_scale_height[ i_scale_idx ] / 1000;
853 p_spu->p_scale->fmt_out.video.i_visible_height =
854 p_region->fmt.i_visible_height * pi_scale_height[ i_scale_idx ] / 1000;
855 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
856 p_region->p_cache->i_x = p_region->i_x * pi_scale_width[ i_scale_idx ] / 1000;
857 p_region->p_cache->i_y = p_region->i_y * pi_scale_height[ i_scale_idx ] / 1000;
858 p_region->p_cache->i_align = p_region->i_align;
859 p_region->p_cache->i_alpha = p_region->i_alpha;
861 p_pic = p_spu->p_scale->pf_video_filter(
862 p_spu->p_scale, &p_region->p_cache->picture );
865 picture_t p_pic_tmp = p_region->p_cache->picture;
866 p_region->p_cache->picture = *p_pic;
872 if( ( ( pi_scale_width[ i_scale_idx ] != 1000 ) ||
873 ( pi_scale_height[ i_scale_idx ] != 1000 ) ) &&
874 ( ( pi_scale_width[ i_scale_idx ] > 0 ) ||
875 ( pi_scale_height[ i_scale_idx ] > 0 ) ) &&
876 p_spu->p_scale && p_region->p_cache &&
877 ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) )
879 p_region = p_region->p_cache;
882 if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
884 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
885 (p_subpic->i_y + p_region->i_y) * i_inv_scale_y / 1000;
887 else if ( !(p_region->i_align & SUBPICTURE_ALIGN_TOP) )
889 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
892 if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
894 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
895 (pi_subpic_x[ i_scale_idx ] + p_region->i_x)
896 * i_inv_scale_x / 1000;
898 else if ( !(p_region->i_align & SUBPICTURE_ALIGN_LEFT) )
900 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
903 if( p_subpic->b_absolute )
905 i_x_offset = (p_region->i_x +
906 pi_subpic_x[ i_scale_idx ] *
907 pi_scale_width[ i_scale_idx ] / 1000)
908 * i_inv_scale_x / 1000;
909 i_y_offset = (p_region->i_y +
910 p_subpic->i_y * pi_scale_height[ i_scale_idx ] / 1000)
911 * i_inv_scale_y / 1000;
915 i_x_offset = __MAX( i_x_offset, 0 );
916 i_y_offset = __MAX( i_y_offset, 0 );
918 if( ( p_spu->i_margin != 0 ) &&
919 ( p_spu->b_force_crop == false ) )
922 int i_low = (i_y_offset - p_spu->i_margin) * i_inv_scale_y / 1000;
923 int i_high = i_low + p_region->fmt.i_height;
925 /* crop extra margin to keep within bounds */
928 if( i_high > (int)p_fmt->i_height )
929 i_diff = i_high - p_fmt->i_height;
930 i_y_offset -= ( p_spu->i_margin * i_inv_scale_y / 1000 + i_diff );
933 if( p_subpic->b_fade )
935 mtime_t i_fade_start = ( p_subpic->i_stop +
936 p_subpic->i_start ) / 2;
937 mtime_t i_now = mdate();
938 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
940 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
941 ( p_subpic->i_stop - i_fade_start );
945 if( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') )
947 if( p_spu->p_blend->fmt_in.video.i_chroma != p_region->fmt.i_chroma )
949 /* The chroma is not the same, we need to reload the blend module
950 * XXX to match the old behaviour just test !p_spu->p_blend->fmt_in.video.i_chroma */
951 if( p_spu->p_blend->p_module )
952 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
954 p_spu->p_blend->fmt_in.video = p_region->fmt;
955 p_spu->p_blend->p_module = module_Need( p_spu->p_blend, "video blending", 0, 0 );
959 p_spu->p_blend->fmt_in.video = p_region->fmt;
962 /* Force cropping if requested */
963 if( p_spu->b_force_crop )
965 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
966 int i_crop_x = p_spu->i_crop_x * pi_scale_width[ i_scale_idx ] / 1000
967 * i_inv_scale_x / 1000;
968 int i_crop_y = p_spu->i_crop_y * pi_scale_height[ i_scale_idx ] / 1000
969 * i_inv_scale_y / 1000;
970 int i_crop_width = p_spu->i_crop_width * pi_scale_width[ i_scale_idx ] / 1000
971 * i_inv_scale_x / 1000;
972 int i_crop_height = p_spu->i_crop_height * pi_scale_height[ i_scale_idx ] / 1000
973 * i_inv_scale_y / 1000;
975 /* Find the intersection */
976 if( i_crop_x + i_crop_width <= i_x_offset ||
977 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
978 i_crop_y + i_crop_height <= i_y_offset ||
979 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
981 /* No intersection */
982 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
986 int i_x, i_y, i_x_end, i_y_end;
987 i_x = __MAX( i_crop_x, i_x_offset );
988 i_y = __MAX( i_crop_y, i_y_offset );
989 i_x_end = __MIN( i_crop_x + i_crop_width,
990 i_x_offset + (int)p_fmt->i_visible_width );
991 i_y_end = __MIN( i_crop_y + i_crop_height,
992 i_y_offset + (int)p_fmt->i_visible_height );
994 p_fmt->i_x_offset = i_x - i_x_offset;
995 p_fmt->i_y_offset = i_y - i_y_offset;
996 p_fmt->i_visible_width = i_x_end - i_x;
997 p_fmt->i_visible_height = i_y_end - i_y;
1004 i_x_offset = __MAX( i_x_offset, 0 );
1005 i_y_offset = __MAX( i_y_offset, 0 );
1007 /* Update the output picture size */
1008 p_spu->p_blend->fmt_out.video.i_width =
1009 p_spu->p_blend->fmt_out.video.i_visible_width =
1011 p_spu->p_blend->fmt_out.video.i_height =
1012 p_spu->p_blend->fmt_out.video.i_visible_height =
1015 if( p_spu->p_blend->p_module )
1017 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
1018 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
1019 i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025 );
1023 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1024 (char *)&p_spu->p_blend->fmt_out.video.i_chroma,
1025 (char *)&p_spu->p_blend->fmt_out.video.i_chroma );
1029 if( b_rerender_text )
1031 /* Some forms of subtitles need to be re-rendered more than
1032 * once, eg. karaoke. We therefore restore the region to its
1033 * pre-rendered state, so the next time through everything is
1036 p_region->picture.pf_release( &p_region->picture );
1037 memset( &p_region->picture, 0, sizeof( picture_t ) );
1038 p_region->fmt = orig_fmt;
1039 p_region->i_align &= ~SUBPICTURE_RENDERED;
1041 p_region = p_region->p_next;
1044 p_subpic = p_subpic->p_next;
1047 vlc_mutex_unlock( &p_spu->subpicture_lock );
1050 /*****************************************************************************
1051 * spu_SortSubpictures: find the subpictures to display
1052 *****************************************************************************
1053 * This function parses all subpictures and decides which ones need to be
1054 * displayed. This operation does not need lock, since only READY_SUBPICTURE
1055 * are handled. If no picture has been selected, display_date will depend on
1057 * We also check for ephemer DVD subpictures (subpictures that have
1058 * to be removed if a newer one is available), which makes it a lot
1059 * more difficult to guess if a subpicture has to be rendered or not.
1060 *****************************************************************************/
1061 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1064 int i_index, i_channel;
1065 subpicture_t *p_subpic = NULL;
1066 subpicture_t *p_ephemer;
1067 mtime_t ephemer_date;
1069 /* Run subpicture filters */
1070 filter_chain_SubFilter( p_spu->p_chain, display_date );
1072 /* We get an easily parsable chained list of subpictures which
1073 * ends with NULL since p_subpic was initialized to NULL. */
1074 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
1079 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1081 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
1082 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
1087 display_date < p_spu->p_subpicture[i_index].i_start )
1089 /* Too early, come back next monday */
1093 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
1094 ephemer_date = p_spu->p_subpicture[i_index].i_start;
1096 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
1097 ( !p_spu->p_subpicture[i_index].b_ephemer ||
1098 p_spu->p_subpicture[i_index].i_stop >
1099 p_spu->p_subpicture[i_index].i_start ) &&
1100 !( p_spu->p_subpicture[i_index].b_pausable &&
1103 /* Too late, destroy the subpic */
1104 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
1108 /* If this is an ephemer subpic, add it to our list */
1109 if( p_spu->p_subpicture[i_index].b_ephemer )
1111 p_spu->p_subpicture[i_index].p_next = p_ephemer;
1112 p_ephemer = &p_spu->p_subpicture[i_index];
1117 p_spu->p_subpicture[i_index].p_next = p_subpic;
1118 p_subpic = &p_spu->p_subpicture[i_index];
1121 /* If we found ephemer subpictures, check if they have to be
1122 * displayed or destroyed */
1123 while( p_ephemer != NULL )
1125 subpicture_t *p_tmp = p_ephemer;
1126 p_ephemer = p_ephemer->p_next;
1128 if( p_tmp->i_start < ephemer_date )
1130 /* Ephemer subpicture has lived too long */
1131 spu_DestroySubpicture( p_spu, p_tmp );
1135 /* Ephemer subpicture can still live a bit */
1136 p_tmp->p_next = p_subpic;
1145 /*****************************************************************************
1146 * SpuClearChannel: clear an spu channel
1147 *****************************************************************************
1148 * This function destroys the subpictures which belong to the spu channel
1149 * corresponding to i_channel_id.
1150 *****************************************************************************/
1151 static void SpuClearChannel( spu_t *p_spu, int i_channel )
1153 int i_subpic; /* subpicture index */
1154 subpicture_t *p_subpic = NULL; /* first free subpicture */
1156 vlc_mutex_lock( &p_spu->subpicture_lock );
1158 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1160 p_subpic = &p_spu->p_subpicture[i_subpic];
1161 if( p_subpic->i_status == FREE_SUBPICTURE
1162 || ( p_subpic->i_status != RESERVED_SUBPICTURE
1163 && p_subpic->i_status != READY_SUBPICTURE ) )
1168 if( p_subpic->i_channel == i_channel )
1170 while( p_subpic->p_region )
1172 subpicture_region_t *p_region = p_subpic->p_region;
1173 p_subpic->p_region = p_region->p_next;
1174 spu_DestroyRegion( p_spu, p_region );
1177 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1178 p_subpic->i_status = FREE_SUBPICTURE;
1182 vlc_mutex_unlock( &p_spu->subpicture_lock );
1185 /*****************************************************************************
1186 * spu_ControlDefault: default methods for the subpicture unit control.
1187 *****************************************************************************/
1188 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1194 case SPU_CHANNEL_REGISTER:
1195 pi = (int *)va_arg( args, int * );
1196 if( pi ) *pi = p_spu->i_channel++;
1199 case SPU_CHANNEL_CLEAR:
1200 i = (int)va_arg( args, int );
1201 SpuClearChannel( p_spu, i );
1205 msg_Dbg( p_spu, "control query not supported" );
1206 return VLC_EGENERIC;
1212 /*****************************************************************************
1213 * Object variables callbacks
1214 *****************************************************************************/
1216 /*****************************************************************************
1217 * UpdateSPU: update subpicture settings
1218 *****************************************************************************
1219 * This function is called from CropCallback and at initialization time, to
1220 * retrieve crop information from the input.
1221 *****************************************************************************/
1222 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1226 p_spu->b_force_palette = false;
1227 p_spu->b_force_crop = false;
1229 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1231 p_spu->b_force_crop = true;
1232 var_Get( p_object, "x-start", &val );
1233 p_spu->i_crop_x = val.i_int;
1234 var_Get( p_object, "y-start", &val );
1235 p_spu->i_crop_y = val.i_int;
1236 var_Get( p_object, "x-end", &val );
1237 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1238 var_Get( p_object, "y-end", &val );
1239 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1241 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1243 memcpy( p_spu->palette, val.p_address, 16 );
1244 p_spu->b_force_palette = true;
1247 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1248 p_spu->i_crop_x, p_spu->i_crop_y,
1249 p_spu->i_crop_width, p_spu->i_crop_height,
1250 p_spu->b_force_palette );
1253 /*****************************************************************************
1254 * CropCallback: called when the highlight properties are changed
1255 *****************************************************************************
1256 * This callback is called from the input thread when we need cropping
1257 *****************************************************************************/
1258 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1259 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1261 (void)psz_var; (void)oldval; (void)newval;
1262 UpdateSPU( (spu_t *)p_data, p_object );
1266 /*****************************************************************************
1267 * Buffers allocation callbacks for the filters
1268 *****************************************************************************/
1269 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1271 filter_owner_sys_t *p_sys = p_filter->p_owner;
1272 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1273 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1274 return p_subpicture;
1277 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1279 filter_owner_sys_t *p_sys = p_filter->p_owner;
1280 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1283 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1286 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1287 if( !p_subpic ) return NULL;
1288 memset( p_subpic, 0, sizeof(subpicture_t) );
1289 p_subpic->b_absolute = true;
1291 p_subpic->pf_create_region = __spu_CreateRegion;
1292 p_subpic->pf_make_region = __spu_MakeRegion;
1293 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1298 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1300 while( p_subpic->p_region )
1302 subpicture_region_t *p_region = p_subpic->p_region;
1303 p_subpic->p_region = p_region->p_next;
1304 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1310 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1312 picture_t *p_picture = malloc( sizeof(picture_t) );
1313 if( !p_picture ) return NULL;
1314 if( vout_AllocatePicture( p_filter, p_picture,
1315 p_filter->fmt_out.video.i_chroma,
1316 p_filter->fmt_out.video.i_width,
1317 p_filter->fmt_out.video.i_height,
1318 p_filter->fmt_out.video.i_aspect )
1325 p_picture->pf_release = RegionPictureRelease;
1330 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1335 free( p_pic->p_data_orig );
1340 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1341 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1343 VLC_UNUSED(p_object); VLC_UNUSED(oldval);
1344 VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1346 spu_t *p_spu = (spu_t *)p_data;
1347 vlc_mutex_lock( &p_spu->subpicture_lock );
1348 spu_DeleteChain( p_spu );
1349 spu_ParseChain( p_spu );
1350 vlc_mutex_unlock( &p_spu->subpicture_lock );
1354 static int sub_filter_allocation_init( filter_t *p_filter, void *p_data )
1356 spu_t *p_spu = (spu_t *)p_data;
1358 p_filter->pf_sub_buffer_new = sub_new_buffer;
1359 p_filter->pf_sub_buffer_del = sub_del_buffer;
1361 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1362 if( !p_sys ) return VLC_EGENERIC;
1364 p_filter->p_owner = p_sys;
1365 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1366 p_sys->p_spu = p_spu;
1371 static void sub_filter_allocation_clear( filter_t *p_filter )
1373 free( p_filter->p_owner );