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"
42 /*****************************************************************************
44 *****************************************************************************/
45 static void UpdateSPU ( spu_t *, vlc_object_t * );
46 static int CropCallback( vlc_object_t *, char const *,
47 vlc_value_t, vlc_value_t, void * );
49 static int spu_vaControlDefault( spu_t *, int, va_list );
51 static subpicture_t *sub_new_buffer( filter_t * );
52 static void sub_del_buffer( filter_t *, subpicture_t * );
53 static subpicture_t *spu_new_buffer( filter_t * );
54 static void spu_del_buffer( filter_t *, subpicture_t * );
55 static picture_t *spu_new_video_buffer( filter_t * );
56 static void spu_del_video_buffer( filter_t *, picture_t * );
58 static int spu_ParseChain( spu_t * );
59 static int SubFilterCallback( vlc_object_t *, char const *,
60 vlc_value_t, vlc_value_t, void * );
62 static int SubFilterAllocationInit( filter_t *, void * );
63 static void SubFilterAllocationClean( filter_t * );
64 struct filter_owner_sys_t
70 #define SCALE_UNIT (1000)
73 static void SpuRenderCreateAndLoadText( spu_t *p_spu );
74 static void SpuRenderCreateAndLoadScale( spu_t *p_spu );
75 static void FilterRelease( filter_t *p_filter );
78 * Creates the subpicture unit
80 * \param p_this the parent object which creates the subpicture unit
82 spu_t *__spu_Create( vlc_object_t *p_this )
85 spu_t *p_spu = vlc_custom_create( p_this, sizeof( spu_t ),
86 VLC_OBJECT_GENERIC, "subpicture" );
88 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
90 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
93 p_spu->p_blend = NULL;
95 p_spu->p_scale = NULL;
96 p_spu->p_scale_yuvp = NULL;
97 p_spu->pf_control = spu_vaControlDefault;
99 /* Register the default subpicture channel */
100 p_spu->i_channel = 2;
102 vlc_mutex_init( &p_spu->subpicture_lock );
104 vlc_object_attach( p_spu, p_this );
106 p_spu->p_chain = filter_chain_New( p_spu, "sub filter", false,
107 SubFilterAllocationInit,
108 SubFilterAllocationClean,
111 /* Load text and scale module */
112 SpuRenderCreateAndLoadText( p_spu );
113 SpuRenderCreateAndLoadScale( p_spu );
119 * Initialise the subpicture unit
121 * \param p_spu the subpicture unit object
123 int spu_Init( spu_t *p_spu )
127 /* If the user requested a sub margin, we force the position. */
128 var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
129 var_Get( p_spu, "sub-margin", &val );
130 p_spu->i_margin = val.i_int;
132 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
133 var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
135 spu_ParseChain( p_spu );
140 int spu_ParseChain( spu_t *p_spu )
142 char *psz_parser = var_GetString( p_spu, "sub-filter" );
143 if( filter_chain_AppendFromString( p_spu->p_chain, psz_parser ) < 0 )
154 * Destroy the subpicture unit
156 * \param p_this the parent object which destroys the subpicture unit
158 void spu_Destroy( spu_t *p_spu )
162 /* Destroy all remaining subpictures */
163 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
165 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
167 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
172 FilterRelease( p_spu->p_blend );
175 FilterRelease( p_spu->p_text );
177 if( p_spu->p_scale_yuvp )
178 FilterRelease( p_spu->p_scale_yuvp );
181 FilterRelease( p_spu->p_scale );
183 filter_chain_Delete( p_spu->p_chain );
185 vlc_mutex_destroy( &p_spu->subpicture_lock );
186 vlc_object_release( p_spu );
190 * Attach/Detach the SPU from any input
192 * \param p_this the object in which to destroy the subpicture unit
193 * \param b_attach to select attach or detach
195 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
197 vlc_object_t *p_input;
199 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
200 if( !p_input ) return;
204 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
205 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
206 vlc_object_release( p_input );
210 /* Delete callback */
211 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
212 vlc_object_release( p_input );
218 static void RegionPictureRelease( picture_t *p_pic )
220 free( p_pic->p_data_orig );
221 /* We use pf_release nullity to know if the picture has already been released. */
222 p_pic->pf_release = NULL;
226 * Create a subpicture region
228 * \param p_this vlc_object_t
229 * \param p_fmt the format that this subpicture region should have
231 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
232 video_format_t *p_fmt )
234 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
238 /* FIXME is that *really* wanted? */
239 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
240 p_fmt->p_palette = calloc( 1, sizeof(video_palette_t) );
242 p_fmt->p_palette = NULL; /* XXX and that above all? */
244 p_region->fmt = *p_fmt;
245 p_region->i_alpha = 0xff;
246 p_region->p_next = NULL;
247 p_region->p_cache = NULL;
248 p_region->psz_text = NULL;
249 p_region->p_style = NULL;
251 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') )
254 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
255 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
257 if( !p_region->picture.i_planes )
259 free( p_fmt->p_palette );
264 p_region->picture.pf_release = RegionPictureRelease;
270 * Destroy a subpicture region
272 * \param p_this vlc_object_t
273 * \param p_region the subpicture region to destroy
275 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
280 picture_Release( &p_region->picture );
282 free( p_region->fmt.p_palette );
283 if( p_region->p_cache )
284 __spu_DestroyRegion( p_this, p_region->p_cache );
286 free( p_region->psz_text );
287 free( p_region->psz_html );
288 //free( p_region->p_style ); FIXME --fenrir plugin does not allocate the memory for it. I think it might lead to segfault, video renderer can live longer than the decoder
293 * Display a subpicture
295 * Remove the reservation flag of a subpicture, which will cause it to be
297 * \param p_spu the subpicture unit object
298 * \param p_subpic the subpicture to display
300 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
302 /* Check if status is valid */
303 if( p_subpic->i_status != RESERVED_SUBPICTURE )
305 msg_Err( p_spu, "subpicture %p has invalid status #%d",
306 p_subpic, p_subpic->i_status );
309 /* Remove reservation flag */
310 p_subpic->i_status = READY_SUBPICTURE;
312 if( p_subpic->i_channel == DEFAULT_CHAN )
314 p_subpic->i_channel = 0xFFFF;
315 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
316 p_subpic->i_channel = DEFAULT_CHAN;
321 * Allocate a subpicture in the spu heap.
323 * This function create a reserved subpicture in the spu heap.
324 * A null pointer is returned if the function fails. This method provides an
325 * already allocated zone of memory in the spu data fields. It needs locking
326 * since several pictures can be created by several producers threads.
327 * \param p_spu the subpicture unit in which to create the subpicture
328 * \return NULL on error, a reserved subpicture otherwise
330 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
332 int i_subpic; /* subpicture index */
333 subpicture_t * p_subpic = NULL; /* first free subpicture */
336 vlc_mutex_lock( &p_spu->subpicture_lock );
339 * Look for an empty place
342 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
344 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
346 /* Subpicture is empty and ready for allocation */
347 p_subpic = &p_spu->p_subpicture[i_subpic];
348 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
353 /* If no free subpicture could be found */
354 if( p_subpic == NULL )
356 msg_Err( p_spu, "subpicture heap is full" );
357 vlc_mutex_unlock( &p_spu->subpicture_lock );
361 /* Copy subpicture information, set some default values */
362 memset( p_subpic, 0, sizeof(subpicture_t) );
363 p_subpic->i_status = RESERVED_SUBPICTURE;
364 p_subpic->b_absolute = true;
365 p_subpic->b_fade = false;
366 p_subpic->b_subtitle = false;
367 p_subpic->i_alpha = 0xFF;
368 p_subpic->p_region = NULL;
369 p_subpic->pf_render = NULL;
370 p_subpic->pf_destroy = NULL;
371 p_subpic->p_sys = NULL;
372 vlc_mutex_unlock( &p_spu->subpicture_lock );
374 p_subpic->pf_create_region = __spu_CreateRegion;
375 p_subpic->pf_destroy_region = __spu_DestroyRegion;
381 * Remove a subpicture from the heap
383 * This function frees a previously reserved subpicture.
384 * It is meant to be used when the construction of a picture aborted.
385 * This function does not need locking since reserved subpictures are ignored
388 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
391 vlc_mutex_lock( &p_spu->subpicture_lock );
393 /* There can be race conditions so we need to check the status */
394 if( p_subpic->i_status == FREE_SUBPICTURE )
396 vlc_mutex_unlock( &p_spu->subpicture_lock );
400 /* Check if status is valid */
401 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
402 && ( p_subpic->i_status != READY_SUBPICTURE ) )
404 msg_Err( p_spu, "subpicture %p has invalid status %d",
405 p_subpic, p_subpic->i_status );
408 while( p_subpic->p_region )
410 subpicture_region_t *p_region = p_subpic->p_region;
411 p_subpic->p_region = p_region->p_next;
412 spu_DestroyRegion( p_spu, p_region );
415 if( p_subpic->pf_destroy )
417 p_subpic->pf_destroy( p_subpic );
420 p_subpic->i_status = FREE_SUBPICTURE;
422 vlc_mutex_unlock( &p_spu->subpicture_lock );
425 /*****************************************************************************
426 * spu_RenderSubpictures: render a subpicture list
427 *****************************************************************************
428 * This function renders all sub picture units in the list.
429 *****************************************************************************/
430 static void FilterRelease( filter_t *p_filter )
432 if( p_filter->p_module )
433 module_Unneed( p_filter, p_filter->p_module );
435 vlc_object_detach( p_filter );
436 vlc_object_release( p_filter );
439 static void SpuRenderCreateBlend( spu_t *p_spu, vlc_fourcc_t i_chroma, int i_aspect )
443 assert( !p_spu->p_blend );
446 p_blend = vlc_custom_create( p_spu, sizeof(filter_t),
447 VLC_OBJECT_GENERIC, "blend" );
451 es_format_Init( &p_blend->fmt_in, VIDEO_ES, 0 );
453 es_format_Init( &p_blend->fmt_out, VIDEO_ES, 0 );
454 p_blend->fmt_out.video.i_x_offset = 0;
455 p_blend->fmt_out.video.i_y_offset = 0;
456 p_blend->fmt_out.video.i_chroma = i_chroma;
457 p_blend->fmt_out.video.i_aspect = i_aspect;
459 /* The blend module will be loaded when needed with the real
461 p_blend->p_module = NULL;
464 vlc_object_attach( p_blend, p_spu );
466 static void SpuRenderUpdateBlend( spu_t *p_spu, int i_out_width, int i_out_height, const video_format_t *p_in_fmt )
468 filter_t *p_blend = p_spu->p_blend;
473 if( p_blend->p_module && p_blend->fmt_in.video.i_chroma != p_in_fmt->i_chroma )
475 /* The chroma is not the same, we need to reload the blend module
476 * XXX to match the old behaviour just test !p_blend->fmt_in.video.i_chroma */
477 module_Unneed( p_blend, p_blend->p_module );
478 p_blend->p_module = NULL;
482 p_blend->fmt_in.video = *p_in_fmt;
485 p_blend->fmt_out.video.i_width =
486 p_blend->fmt_out.video.i_visible_width = i_out_width;
487 p_blend->fmt_out.video.i_height =
488 p_blend->fmt_out.video.i_visible_height = i_out_height;
491 if( !p_blend->p_module )
492 p_blend->p_module = module_Need( p_blend, "video blending", 0, 0 );
494 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
498 assert( !p_spu->p_text );
501 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
502 VLC_OBJECT_GENERIC, "spu text" );
506 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
508 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
509 p_text->fmt_out.video.i_width =
510 p_text->fmt_out.video.i_visible_width = 32;
511 p_text->fmt_out.video.i_height =
512 p_text->fmt_out.video.i_visible_height = 32;
514 p_text->pf_sub_buffer_new = spu_new_buffer;
515 p_text->pf_sub_buffer_del = spu_del_buffer;
517 vlc_object_attach( p_text, p_spu );
519 /* FIXME TOCHECK shouldn't module_Need( , , psz_modulename, false ) do the
520 * same than these 2 calls ? */
521 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
522 if( psz_modulename && *psz_modulename )
524 p_text->p_module = module_Need( p_text, "text renderer",
525 psz_modulename, true );
527 free( psz_modulename );
529 if( !p_text->p_module )
530 p_text->p_module = module_Need( p_text, "text renderer", NULL, false );
533 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj, vlc_fourcc_t i_chroma )
537 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
538 VLC_OBJECT_GENERIC, "scale" );
542 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
543 p_scale->fmt_in.video.i_chroma = i_chroma;
544 p_scale->fmt_in.video.i_width =
545 p_scale->fmt_in.video.i_height = 32;
547 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
548 p_scale->fmt_out.video.i_chroma = i_chroma;
549 p_scale->fmt_out.video.i_width =
550 p_scale->fmt_out.video.i_height = 16;
552 p_scale->pf_vout_buffer_new = spu_new_video_buffer;
553 p_scale->pf_vout_buffer_del = spu_del_video_buffer;
555 vlc_object_attach( p_scale, p_obj );
556 p_scale->p_module = module_Need( p_scale, "video filter2", 0, 0 );
560 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
562 /* FIXME: We'll also be using it for YUVA and RGBA blending ... */
564 assert( !p_spu->p_scale );
565 assert( !p_spu->p_scale_yuvp );
566 p_spu->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu), VLC_FOURCC('Y','U','V','A') );
567 p_spu->p_scale_yuvp = p_spu->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu), VLC_FOURCC('Y','U','V','P') );
570 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
571 subpicture_t *p_subpic, subpicture_region_t *p_region, int i_min_scale_ratio )
573 assert( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') );
575 if( !p_spu->p_text || !p_spu->p_text->p_module )
578 /* Setup 3 variables which can be used to render
579 * time-dependent text (and effects). The first indicates
580 * the total amount of time the text will be on screen,
581 * the second the amount of time it has already been on
582 * screen (can be a negative value as text is layed out
583 * before it is rendered) and the third is a feedback
584 * variable from the renderer - if the renderer sets it
585 * then this particular text is time-dependent, eg. the
586 * visual progress bar inside the text in karaoke and the
587 * text needs to be rendered multiple times in order for
588 * the effect to work - we therefore need to return the
589 * region to its original state at the end of the loop,
590 * instead of leaving it in YUVA or YUVP.
591 * Any renderer which is unaware of how to render
592 * time-dependent text can happily ignore the variables
593 * and render the text the same as usual - it should at
594 * least show up on screen, but the effect won't change
595 * the text over time.
598 /* FIXME why these variables are recreated every time and not
599 * when text renderer module was created ? */
600 var_Create( p_spu->p_text, "spu-duration", VLC_VAR_TIME );
601 var_Create( p_spu->p_text, "spu-elapsed", VLC_VAR_TIME );
602 var_Create( p_spu->p_text, "text-rerender", VLC_VAR_BOOL );
603 var_Create( p_spu->p_text, "scale", VLC_VAR_INTEGER );
605 var_SetTime( p_spu->p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
606 var_SetTime( p_spu->p_text, "spu-elapsed", mdate() - p_subpic->i_start );
607 var_SetBool( p_spu->p_text, "text-rerender", false );
608 var_SetInteger( p_spu->p_text, "scale", i_min_scale_ratio );
610 if( p_spu->p_text->pf_render_html && p_region->psz_html )
612 p_spu->p_text->pf_render_html( p_spu->p_text,
613 p_region, p_region );
615 else if( p_spu->p_text->pf_render_text )
617 p_spu->p_text->pf_render_text( p_spu->p_text,
618 p_region, p_region );
620 *pb_rerender_text = var_GetBool( p_spu->p_text, "text-rerender" );
622 var_Destroy( p_spu->p_text, "spu-duration" );
623 var_Destroy( p_spu->p_text, "spu-elapsed" );
624 var_Destroy( p_spu->p_text, "text-rerender" );
625 var_Destroy( p_spu->p_text, "scale" );
628 p_region->i_align |= SUBPICTURE_RENDERED;
632 * A few scale functions helpers.
640 static spu_scale_t spu_scale_create( int w, int h )
642 spu_scale_t s = { .w = w, .h = h };
649 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
651 return spu_scale_create( wn * SCALE_UNIT / wd,
652 hn * SCALE_UNIT / hd );
654 static int spu_scale_w( int v, const spu_scale_t s )
656 return v * s.w / SCALE_UNIT;
658 static int spu_scale_h( int v, const spu_scale_t s )
660 return v * s.h / SCALE_UNIT;
666 static void SpuRegionPlace( int *pi_x, int *pi_y,
667 const video_format_t *p_fmt,
668 const subpicture_t *p_subpic,
669 const subpicture_region_t *p_region )
671 int i_delta_x = p_region->i_x;
672 int i_delta_y = p_region->i_y;
675 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
679 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
681 i_y = p_fmt->i_height - p_region->fmt.i_height - i_delta_y;
685 i_y = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
688 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
692 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
694 i_x = p_fmt->i_width - p_region->fmt.i_width - i_delta_x;
698 i_x = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
701 if( p_subpic->b_absolute )
715 static void SpuRenderRegion( spu_t *p_spu,
716 picture_t *p_pic_dst,
717 subpicture_t *p_subpic, subpicture_region_t *p_region,
718 const spu_scale_t scale_size,
719 const video_format_t *p_fmt )
721 video_format_t fmt_original;
722 bool b_rerender_text;
723 bool b_restore_format = false;
729 vlc_assert_locked( &p_spu->subpicture_lock );
731 fmt_original = p_region->fmt;
732 b_rerender_text = false;
733 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
735 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region, SCALE_UNIT );
736 b_restore_format = b_rerender_text;
738 /* Check if the rendering has failed ... */
739 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
743 /* Force palette if requested
744 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
745 * instead of only the right one (being the dvd spu).
747 const bool b_using_palette = p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','P');
748 const bool b_force_palette = b_using_palette && p_spu->b_force_palette;
749 const bool b_force_crop = b_force_palette && p_spu->b_force_crop;
751 if( b_force_palette )
753 /* It looks so wrong I won't comment
754 * p_palette->palette is [256][4] with a int i_entries
755 * p_spu->palette is [4][4]
757 p_region->fmt.p_palette->i_entries = 4;
758 memcpy( p_region->fmt.p_palette->palette, p_spu->palette, 4*sizeof(uint32_t) );
761 if( b_using_palette )
762 p_scale = p_spu->p_scale_yuvp;
764 p_scale = p_spu->p_scale;
767 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_force_palette ) )
769 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
770 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
772 /* Destroy if cache is unusable */
773 if( p_region->p_cache )
775 if( p_region->p_cache->fmt.i_width != i_dst_width ||
776 p_region->p_cache->fmt.i_height != i_dst_height ||
779 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
781 p_region->p_cache = NULL;
785 /* Scale if needed into cache */
786 if( !p_region->p_cache )
790 p_scale->fmt_in.video = p_region->fmt;
791 p_scale->fmt_out.video = p_region->fmt;
794 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
795 &p_scale->fmt_out.video );
796 p_region->p_cache->p_next = p_region->p_next;
798 if( p_scale->fmt_out.video.p_palette )
799 *p_scale->fmt_out.video.p_palette =
800 *p_region->fmt.p_palette;
802 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
803 &p_region->picture );
805 p_scale->fmt_out.video.i_width = i_dst_width;
806 p_scale->fmt_out.video.i_height = i_dst_height;
808 p_scale->fmt_out.video.i_visible_width =
809 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
810 p_scale->fmt_out.video.i_visible_height =
811 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
813 p_region->p_cache->fmt = p_scale->fmt_out.video;
814 p_region->p_cache->i_x = spu_scale_w( p_region->i_x, scale_size );
815 p_region->p_cache->i_y = spu_scale_h( p_region->i_y, scale_size );
816 p_region->p_cache->i_align = p_region->i_align;
817 p_region->p_cache->i_alpha = p_region->i_alpha;
820 if( p_scale->p_module )
821 p_pic = p_scale->pf_video_filter( p_scale, &p_region->p_cache->picture );
823 msg_Err( p_spu, "scaling failed (module not loaded)" );
827 p_region->p_cache->picture = *p_pic;
832 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
834 p_region->p_cache = NULL;
838 /* And use the scaled picture */
839 if( p_region->p_cache )
841 p_region = p_region->p_cache;
842 fmt_original = p_region->fmt;
847 SpuRegionPlace( &i_x_offset, &i_y_offset,
848 p_fmt, p_subpic, p_region );
850 if( p_spu->i_margin != 0 && !b_force_crop )
853 int i_low = i_y_offset - p_spu->i_margin;
854 int i_high = i_low + p_region->fmt.i_height;
856 /* crop extra margin to keep within bounds */
859 if( i_high > (int)p_fmt->i_height )
860 i_diff = i_high - p_fmt->i_height;
861 i_y_offset -= p_spu->i_margin + i_diff;
864 /* Force cropping if requested */
867 video_format_t *p_fmt = &p_region->fmt;
868 int i_crop_x = spu_scale_w( p_spu->i_crop_x, scale_size );
869 int i_crop_y = spu_scale_h( p_spu->i_crop_y, scale_size );
870 int i_crop_width = spu_scale_w( p_spu->i_crop_width, scale_size );
871 int i_crop_height= spu_scale_h( p_spu->i_crop_height,scale_size );
873 /* Find the intersection */
874 if( i_crop_x + i_crop_width <= i_x_offset ||
875 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
876 i_crop_y + i_crop_height <= i_y_offset ||
877 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
879 /* No intersection */
880 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
884 int i_x, i_y, i_x_end, i_y_end;
885 i_x = __MAX( i_crop_x, i_x_offset );
886 i_y = __MAX( i_crop_y, i_y_offset );
887 i_x_end = __MIN( i_crop_x + i_crop_width,
888 i_x_offset + (int)p_fmt->i_visible_width );
889 i_y_end = __MIN( i_crop_y + i_crop_height,
890 i_y_offset + (int)p_fmt->i_visible_height );
892 p_fmt->i_x_offset = i_x - i_x_offset;
893 p_fmt->i_y_offset = i_y - i_y_offset;
894 p_fmt->i_visible_width = i_x_end - i_x;
895 p_fmt->i_visible_height = i_y_end - i_y;
900 b_restore_format = true;
903 i_x_offset = __MAX( i_x_offset, 0 );
904 i_y_offset = __MAX( i_y_offset, 0 );
906 /* Compute alpha blend value */
908 if( p_subpic->b_fade )
910 mtime_t i_fade_start = ( p_subpic->i_stop +
911 p_subpic->i_start ) / 2;
912 mtime_t i_now = mdate();
913 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
915 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
916 ( p_subpic->i_stop - i_fade_start );
920 /* Update the blender */
921 SpuRenderUpdateBlend( p_spu, p_fmt->i_width, p_fmt->i_height, &p_region->fmt );
923 if( p_spu->p_blend->p_module )
925 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
926 &p_region->picture, i_x_offset, i_y_offset,
927 i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025 );
931 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
932 (char *)&p_spu->p_blend->fmt_out.video.i_chroma,
933 (char *)&p_spu->p_blend->fmt_out.video.i_chroma );
937 if( b_rerender_text )
939 /* Some forms of subtitles need to be re-rendered more than
940 * once, eg. karaoke. We therefore restore the region to its
941 * pre-rendered state, so the next time through everything is
944 p_region->picture.pf_release( &p_region->picture );
945 memset( &p_region->picture, 0, sizeof( picture_t ) );
946 p_region->i_align &= ~SUBPICTURE_RENDERED;
948 if( b_restore_format )
949 p_region->fmt = fmt_original;
952 void spu_RenderSubpictures( spu_t *p_spu,
953 picture_t *p_pic_dst, video_format_t *p_fmt_dst,
954 subpicture_t *p_subpic_list,
955 const video_format_t *p_fmt_src )
957 video_format_t *p_fmt = p_fmt_dst;
958 const mtime_t i_current_date = mdate();
959 int i_source_video_width;
960 int i_source_video_height;
961 subpicture_t *p_subpic;
964 vlc_mutex_lock( &p_spu->subpicture_lock );
966 /* Be sure we have at least 1 picture to process */
967 if( !p_subpic_list || p_subpic_list->i_status == FREE_SUBPICTURE )
969 vlc_mutex_unlock( &p_spu->subpicture_lock );
973 i_source_video_width = p_fmt_src->i_width;
974 i_source_video_height = p_fmt_src->i_height;
977 for( p_subpic = p_subpic_list;
978 p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE; /* Check again status (as we where unlocked) */
979 p_subpic = p_subpic->p_next )
982 if( p_subpic->pf_pre_render )
983 p_subpic->pf_pre_render( p_fmt, p_spu, p_subpic );
985 if( p_subpic->pf_update_regions )
987 /* TODO do not reverse the scaling that was done before calling
988 * spu_RenderSubpictures, just pass it along (or do it inside
989 * spu_RenderSubpictures) */
990 video_format_t fmt_org = *p_fmt;
992 fmt_org.i_visible_width = i_source_video_width;
994 fmt_org.i_visible_height = i_source_video_height;
996 p_subpic->pf_update_regions( &fmt_org, p_spu, p_subpic, i_current_date );
1000 /* Create the blending module */
1001 if( !p_spu->p_blend )
1002 SpuRenderCreateBlend( p_spu, p_fmt->i_chroma, p_fmt->i_aspect );
1005 for( p_subpic = p_subpic_list; ; p_subpic = p_subpic->p_next )
1007 subpicture_region_t *p_region;
1009 if( !p_subpic || p_subpic->i_status == FREE_SUBPICTURE )
1012 if( !p_subpic->p_region )
1015 /* FIXME when possible use a better rendering size than source size
1016 * (max of display size and source size for example) FIXME */
1017 int i_render_width = p_subpic->i_original_picture_width;
1018 int i_render_height = p_subpic->i_original_picture_height;
1019 if( !i_render_width || !i_render_height )
1021 if( i_render_width != 0 || i_render_height != 0 )
1022 msg_Err( p_spu, "unsupported original picture size %dx%d",
1023 i_render_width, i_render_height );
1025 i_render_width = i_source_video_width;
1026 i_render_height = i_source_video_height;
1031 p_spu->p_text->fmt_out.video.i_width =
1032 p_spu->p_text->fmt_out.video.i_visible_width = i_render_width;
1034 p_spu->p_text->fmt_out.video.i_height =
1035 p_spu->p_text->fmt_out.video.i_visible_height = i_render_height;
1038 /* Compute scaling from picture to source size */
1039 spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
1040 i_source_video_height, i_render_height );
1042 /* Update scaling from source size to display size(p_fmt) */
1043 scale.w = scale.w * p_fmt->i_width / i_source_video_width;
1044 scale.h = scale.h * p_fmt->i_height / i_source_video_height;
1046 /* Set default subpicture aspect ratio
1047 * FIXME if we only handle 1 aspect ratio per picture, why is it set per
1049 p_region = p_subpic->p_region;
1050 if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
1052 if( p_region->fmt.i_aspect != 0 )
1054 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
1055 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
1059 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
1060 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
1064 /* Take care of the aspect ratio */
1065 if( p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
1066 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
1068 /* FIXME FIXME what about region->i_x/i_y ? */
1070 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
1071 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
1074 /* Render all regions */
1075 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
1077 /* Check scale validity */
1078 if( scale.w <= 0 || scale.h <= 0 )
1082 SpuRenderRegion( p_spu, p_pic_dst, p_subpic, p_region,
1087 vlc_mutex_unlock( &p_spu->subpicture_lock );
1090 /*****************************************************************************
1091 * spu_SortSubpictures: find the subpictures to display
1092 *****************************************************************************
1093 * This function parses all subpictures and decides which ones need to be
1094 * displayed. This operation does not need lock, since only READY_SUBPICTURE
1095 * are handled. If no picture has been selected, display_date will depend on
1097 * We also check for ephemer DVD subpictures (subpictures that have
1098 * to be removed if a newer one is available), which makes it a lot
1099 * more difficult to guess if a subpicture has to be rendered or not.
1100 *****************************************************************************/
1101 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1102 bool b_paused, bool b_subtitle_only )
1105 subpicture_t *p_subpic = NULL;
1107 /* Run subpicture filters */
1108 filter_chain_SubFilter( p_spu->p_chain, display_date );
1110 /* We get an easily parsable chained list of subpictures which
1111 * ends with NULL since p_subpic was initialized to NULL. */
1112 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
1114 subpicture_t *p_ephemer = NULL;
1115 mtime_t ephemer_date = 0;
1118 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1120 subpicture_t *p_current = &p_spu->p_subpicture[i_index];
1122 if( p_current->i_channel != i_channel ||
1123 p_current->i_status != READY_SUBPICTURE ||
1124 ( b_subtitle_only && !p_current->b_subtitle ) )
1129 display_date < p_current->i_start )
1131 /* Too early, come back next monday */
1135 if( p_current->i_start > ephemer_date )
1136 ephemer_date = p_current->i_start;
1138 if( display_date > p_current->i_stop &&
1139 ( !p_current->b_ephemer || p_current->i_stop > p_current->i_start ) &&
1140 !( p_current->b_subtitle && b_paused ) ) /* XXX Assume that subtitle are pausable */
1142 /* Too late, destroy the subpic */
1143 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
1147 /* If this is an ephemer subpic, add it to our list */
1148 if( p_current->b_ephemer )
1150 p_current->p_next = p_ephemer;
1151 p_ephemer = p_current;
1156 p_current->p_next = p_subpic;
1157 p_subpic = p_current;
1160 /* If we found ephemer subpictures, check if they have to be
1161 * displayed or destroyed */
1162 while( p_ephemer != NULL )
1164 subpicture_t *p_tmp = p_ephemer;
1165 p_ephemer = p_ephemer->p_next;
1167 if( p_tmp->i_start < ephemer_date )
1169 /* Ephemer subpicture has lived too long */
1170 spu_DestroySubpicture( p_spu, p_tmp );
1174 /* Ephemer subpicture can still live a bit */
1175 p_tmp->p_next = p_subpic;
1184 /*****************************************************************************
1185 * SpuClearChannel: clear an spu channel
1186 *****************************************************************************
1187 * This function destroys the subpictures which belong to the spu channel
1188 * corresponding to i_channel_id.
1189 *****************************************************************************/
1190 static void SpuClearChannel( spu_t *p_spu, int i_channel, bool b_locked )
1192 int i_subpic; /* subpicture index */
1193 subpicture_t *p_subpic = NULL; /* first free subpicture */
1196 vlc_mutex_lock( &p_spu->subpicture_lock );
1198 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1200 p_subpic = &p_spu->p_subpicture[i_subpic];
1201 if( p_subpic->i_status == FREE_SUBPICTURE
1202 || ( p_subpic->i_status != RESERVED_SUBPICTURE
1203 && p_subpic->i_status != READY_SUBPICTURE ) )
1208 if( p_subpic->i_channel == i_channel )
1210 while( p_subpic->p_region )
1212 subpicture_region_t *p_region = p_subpic->p_region;
1213 p_subpic->p_region = p_region->p_next;
1214 spu_DestroyRegion( p_spu, p_region );
1217 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1218 p_subpic->i_status = FREE_SUBPICTURE;
1223 vlc_mutex_unlock( &p_spu->subpicture_lock );
1226 /*****************************************************************************
1227 * spu_ControlDefault: default methods for the subpicture unit control.
1228 *****************************************************************************/
1229 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1235 case SPU_CHANNEL_REGISTER:
1236 pi = (int *)va_arg( args, int * );
1237 vlc_mutex_lock( &p_spu->subpicture_lock );
1239 *pi = p_spu->i_channel++;
1240 vlc_mutex_unlock( &p_spu->subpicture_lock );
1243 case SPU_CHANNEL_CLEAR:
1244 i = (int)va_arg( args, int );
1245 SpuClearChannel( p_spu, i, false );
1249 msg_Dbg( p_spu, "control query not supported" );
1250 return VLC_EGENERIC;
1256 /*****************************************************************************
1257 * Object variables callbacks
1258 *****************************************************************************/
1260 /*****************************************************************************
1261 * UpdateSPU: update subpicture settings
1262 *****************************************************************************
1263 * This function is called from CropCallback and at initialization time, to
1264 * retrieve crop information from the input.
1265 *****************************************************************************/
1266 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1270 vlc_mutex_lock( &p_spu->subpicture_lock );
1272 p_spu->b_force_palette = false;
1273 p_spu->b_force_crop = false;
1275 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1277 vlc_mutex_unlock( &p_spu->subpicture_lock );
1281 p_spu->b_force_crop = true;
1282 var_Get( p_object, "x-start", &val );
1283 p_spu->i_crop_x = val.i_int;
1284 var_Get( p_object, "y-start", &val );
1285 p_spu->i_crop_y = val.i_int;
1286 var_Get( p_object, "x-end", &val );
1287 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1288 var_Get( p_object, "y-end", &val );
1289 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1291 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1293 memcpy( p_spu->palette, val.p_address, 16 );
1294 p_spu->b_force_palette = true;
1296 vlc_mutex_unlock( &p_spu->subpicture_lock );
1298 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1299 p_spu->i_crop_x, p_spu->i_crop_y,
1300 p_spu->i_crop_width, p_spu->i_crop_height,
1301 p_spu->b_force_palette );
1304 /*****************************************************************************
1305 * CropCallback: called when the highlight properties are changed
1306 *****************************************************************************
1307 * This callback is called from the input thread when we need cropping
1308 *****************************************************************************/
1309 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1310 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1312 (void)psz_var; (void)oldval; (void)newval;
1313 UpdateSPU( (spu_t *)p_data, p_object );
1317 /*****************************************************************************
1318 * Buffers allocation callbacks for the filters
1319 *****************************************************************************/
1320 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1322 filter_owner_sys_t *p_sys = p_filter->p_owner;
1323 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1324 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1325 return p_subpicture;
1328 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1330 filter_owner_sys_t *p_sys = p_filter->p_owner;
1331 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1334 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1336 subpicture_t *p_subpic = calloc( 1, sizeof(subpicture_t) );
1340 p_subpic->b_absolute = true;
1341 p_subpic->i_alpha = 0xFF;
1343 p_subpic->pf_create_region = __spu_CreateRegion;
1344 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1346 VLC_UNUSED(p_filter);
1350 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1352 while( p_subpic->p_region )
1354 subpicture_region_t *p_region = p_subpic->p_region;
1355 p_subpic->p_region = p_region->p_next;
1356 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1362 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1364 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1366 VLC_UNUSED(p_filter);
1367 return picture_New( p_fmt->i_chroma,
1368 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
1371 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1373 VLC_UNUSED(p_filter);
1374 picture_Release( p_picture );
1377 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1378 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1380 spu_t *p_spu = p_data;
1382 VLC_UNUSED(p_object); VLC_UNUSED(oldval);
1383 VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1385 vlc_mutex_lock( &p_spu->subpicture_lock );
1386 filter_chain_Reset( p_spu->p_chain, NULL, NULL );
1387 spu_ParseChain( p_spu );
1388 vlc_mutex_unlock( &p_spu->subpicture_lock );
1392 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1394 spu_t *p_spu = p_data;
1396 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1398 return VLC_EGENERIC;
1400 p_filter->pf_sub_buffer_new = sub_new_buffer;
1401 p_filter->pf_sub_buffer_del = sub_del_buffer;
1403 p_filter->p_owner = p_sys;
1404 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1405 p_sys->p_spu = p_spu;
1410 static void SubFilterAllocationClean( filter_t *p_filter )
1412 filter_owner_sys_t *p_sys = p_filter->p_owner;
1414 SpuClearChannel( p_sys->p_spu, p_sys->i_channel, true );
1415 free( p_filter->p_owner );