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
76 #define SCALE_UNIT (1000)
79 static void SpuRenderCreateAndLoadText( spu_t *p_spu );
80 static void SpuRenderCreateAndLoadScale( spu_t *p_spu );
81 static void FilterRelease( filter_t *p_filter );
84 * Creates the subpicture unit
86 * \param p_this the parent object which creates the subpicture unit
88 spu_t *__spu_Create( vlc_object_t *p_this )
91 spu_t *p_spu = vlc_custom_create( p_this, sizeof( spu_t ),
92 VLC_OBJECT_GENERIC, "subpicture" );
94 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
96 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
99 p_spu->p_blend = NULL;
100 p_spu->p_text = NULL;
101 p_spu->p_scale = NULL;
102 p_spu->p_scale_yuvp = NULL;
103 p_spu->pf_control = spu_vaControlDefault;
105 /* Register the default subpicture channel */
106 p_spu->i_channel = 2;
108 vlc_mutex_init( &p_spu->subpicture_lock );
110 vlc_object_attach( p_spu, p_this );
112 p_spu->p_chain = filter_chain_New( p_spu, "sub filter", false,
113 SubFilterAllocationInit,
114 SubFilterAllocationClean,
117 /* Load text and scale module */
118 SpuRenderCreateAndLoadText( p_spu );
119 SpuRenderCreateAndLoadScale( p_spu );
125 * Initialise the subpicture unit
127 * \param p_spu the subpicture unit object
129 int spu_Init( spu_t *p_spu )
133 /* If the user requested a sub margin, we force the position. */
134 var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
135 var_Get( p_spu, "sub-margin", &val );
136 p_spu->i_margin = val.i_int;
138 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
139 var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
141 spu_ParseChain( p_spu );
146 int spu_ParseChain( spu_t *p_spu )
148 char *psz_parser = var_GetString( p_spu, "sub-filter" );
149 if( filter_chain_AppendFromString( p_spu->p_chain, psz_parser ) < 0 )
160 * Destroy the subpicture unit
162 * \param p_this the parent object which destroys the subpicture unit
164 void spu_Destroy( spu_t *p_spu )
168 /* Destroy all remaining subpictures */
169 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
171 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
173 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
178 FilterRelease( p_spu->p_blend );
181 FilterRelease( p_spu->p_text );
183 if( p_spu->p_scale_yuvp )
184 FilterRelease( p_spu->p_scale_yuvp );
187 FilterRelease( p_spu->p_scale );
189 filter_chain_Delete( p_spu->p_chain );
191 vlc_mutex_destroy( &p_spu->subpicture_lock );
192 vlc_object_release( p_spu );
196 * Attach/Detach the SPU from any input
198 * \param p_this the object in which to destroy the subpicture unit
199 * \param b_attach to select attach or detach
201 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
203 vlc_object_t *p_input;
205 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
206 if( !p_input ) return;
210 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
211 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
212 vlc_object_release( p_input );
216 /* Delete callback */
217 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
218 vlc_object_release( p_input );
224 static subpicture_region_t *RegionCreate( video_format_t *p_fmt )
226 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
230 /* FIXME is that *really* wanted? */
231 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
232 p_fmt->p_palette = calloc( 1, sizeof(video_palette_t) );
234 p_fmt->p_palette = NULL; /* XXX and that above all? */
236 p_region->fmt = *p_fmt;
237 p_region->i_alpha = 0xff;
238 p_region->p_next = NULL;
239 p_region->p_cache = NULL;
240 p_region->psz_text = NULL;
241 p_region->p_style = NULL;
245 static void RegionPictureRelease( picture_t *p_pic )
247 free( p_pic->p_data_orig );
248 /* We use pf_release nullity to know if the picture has already been released. */
249 p_pic->pf_release = NULL;
253 * Create a subpicture region
255 * \param p_this vlc_object_t
256 * \param p_fmt the format that this subpicture region should have
258 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
259 video_format_t *p_fmt )
261 subpicture_region_t *p_region = RegionCreate( p_fmt );
265 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') )
268 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
269 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
271 if( !p_region->picture.i_planes )
273 free( p_fmt->p_palette );
278 p_region->picture.pf_release = RegionPictureRelease;
284 * Make a subpicture region from an existing picture_t
286 * \param p_this vlc_object_t
287 * \param p_fmt the format that this subpicture region should have
288 * \param p_pic a pointer to the picture creating the region (not freed)
290 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
291 video_format_t *p_fmt,
294 subpicture_region_t *p_region = RegionCreate( p_fmt );
298 /* FIXME overwriting picture.pf_release seems wrong */
299 p_region->picture = *p_pic;
300 p_region->picture.pf_release = RegionPictureRelease;
307 * Destroy a subpicture region
309 * \param p_this vlc_object_t
310 * \param p_region the subpicture region to destroy
312 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
317 picture_Release( &p_region->picture );
319 free( p_region->fmt.p_palette );
320 if( p_region->p_cache )
321 __spu_DestroyRegion( p_this, p_region->p_cache );
323 free( p_region->psz_text );
324 free( p_region->psz_html );
325 //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
330 * Display a subpicture
332 * Remove the reservation flag of a subpicture, which will cause it to be
334 * \param p_spu the subpicture unit object
335 * \param p_subpic the subpicture to display
337 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
339 /* Check if status is valid */
340 if( p_subpic->i_status != RESERVED_SUBPICTURE )
342 msg_Err( p_spu, "subpicture %p has invalid status #%d",
343 p_subpic, p_subpic->i_status );
346 /* Remove reservation flag */
347 p_subpic->i_status = READY_SUBPICTURE;
349 if( p_subpic->i_channel == DEFAULT_CHAN )
351 p_subpic->i_channel = 0xFFFF;
352 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
353 p_subpic->i_channel = DEFAULT_CHAN;
358 * Allocate a subpicture in the spu heap.
360 * This function create a reserved subpicture in the spu heap.
361 * A null pointer is returned if the function fails. This method provides an
362 * already allocated zone of memory in the spu data fields. It needs locking
363 * since several pictures can be created by several producers threads.
364 * \param p_spu the subpicture unit in which to create the subpicture
365 * \return NULL on error, a reserved subpicture otherwise
367 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
369 int i_subpic; /* subpicture index */
370 subpicture_t * p_subpic = NULL; /* first free subpicture */
373 vlc_mutex_lock( &p_spu->subpicture_lock );
376 * Look for an empty place
379 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
381 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
383 /* Subpicture is empty and ready for allocation */
384 p_subpic = &p_spu->p_subpicture[i_subpic];
385 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
390 /* If no free subpicture could be found */
391 if( p_subpic == NULL )
393 msg_Err( p_spu, "subpicture heap is full" );
394 vlc_mutex_unlock( &p_spu->subpicture_lock );
398 /* Copy subpicture information, set some default values */
399 memset( p_subpic, 0, sizeof(subpicture_t) );
400 p_subpic->i_status = RESERVED_SUBPICTURE;
401 p_subpic->b_absolute = true;
402 p_subpic->b_fade = false;
403 p_subpic->b_subtitle = false;
404 p_subpic->i_alpha = 0xFF;
405 p_subpic->p_region = NULL;
406 p_subpic->pf_render = NULL;
407 p_subpic->pf_destroy = NULL;
408 p_subpic->p_sys = NULL;
409 vlc_mutex_unlock( &p_spu->subpicture_lock );
411 p_subpic->pf_create_region = __spu_CreateRegion;
412 p_subpic->pf_make_region = __spu_MakeRegion;
413 p_subpic->pf_destroy_region = __spu_DestroyRegion;
419 * Remove a subpicture from the heap
421 * This function frees a previously reserved subpicture.
422 * It is meant to be used when the construction of a picture aborted.
423 * This function does not need locking since reserved subpictures are ignored
426 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
429 vlc_mutex_lock( &p_spu->subpicture_lock );
431 /* There can be race conditions so we need to check the status */
432 if( p_subpic->i_status == FREE_SUBPICTURE )
434 vlc_mutex_unlock( &p_spu->subpicture_lock );
438 /* Check if status is valid */
439 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
440 && ( p_subpic->i_status != READY_SUBPICTURE ) )
442 msg_Err( p_spu, "subpicture %p has invalid status %d",
443 p_subpic, p_subpic->i_status );
446 while( p_subpic->p_region )
448 subpicture_region_t *p_region = p_subpic->p_region;
449 p_subpic->p_region = p_region->p_next;
450 spu_DestroyRegion( p_spu, p_region );
453 if( p_subpic->pf_destroy )
455 p_subpic->pf_destroy( p_subpic );
458 p_subpic->i_status = FREE_SUBPICTURE;
460 vlc_mutex_unlock( &p_spu->subpicture_lock );
463 /*****************************************************************************
464 * spu_RenderSubpictures: render a subpicture list
465 *****************************************************************************
466 * This function renders all sub picture units in the list.
467 *****************************************************************************/
468 static void FilterRelease( filter_t *p_filter )
470 if( p_filter->p_module )
471 module_Unneed( p_filter, p_filter->p_module );
473 vlc_object_detach( p_filter );
474 vlc_object_release( p_filter );
477 static void SpuRenderCreateBlend( spu_t *p_spu, vlc_fourcc_t i_chroma, int i_aspect )
481 assert( !p_spu->p_blend );
484 p_blend = vlc_custom_create( p_spu, sizeof(filter_t),
485 VLC_OBJECT_GENERIC, "blend" );
489 es_format_Init( &p_blend->fmt_in, VIDEO_ES, 0 );
491 es_format_Init( &p_blend->fmt_out, VIDEO_ES, 0 );
492 p_blend->fmt_out.video.i_x_offset = 0;
493 p_blend->fmt_out.video.i_y_offset = 0;
494 p_blend->fmt_out.video.i_chroma = i_chroma;
495 p_blend->fmt_out.video.i_aspect = i_aspect;
497 /* The blend module will be loaded when needed with the real
499 p_blend->p_module = NULL;
502 vlc_object_attach( p_blend, p_spu );
504 static void SpuRenderUpdateBlend( spu_t *p_spu, int i_out_width, int i_out_height, const video_format_t *p_in_fmt )
506 filter_t *p_blend = p_spu->p_blend;
511 if( p_blend->p_module && p_blend->fmt_in.video.i_chroma != p_in_fmt->i_chroma )
513 /* The chroma is not the same, we need to reload the blend module
514 * XXX to match the old behaviour just test !p_blend->fmt_in.video.i_chroma */
515 module_Unneed( p_blend, p_blend->p_module );
516 p_blend->p_module = NULL;
520 p_blend->fmt_in.video = *p_in_fmt;
523 p_blend->fmt_out.video.i_width =
524 p_blend->fmt_out.video.i_visible_width = i_out_width;
525 p_blend->fmt_out.video.i_height =
526 p_blend->fmt_out.video.i_visible_height = i_out_height;
529 if( !p_blend->p_module )
530 p_blend->p_module = module_Need( p_blend, "video blending", 0, 0 );
532 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
536 assert( !p_spu->p_text );
539 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
540 VLC_OBJECT_GENERIC, "spu text" );
544 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
546 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
547 p_text->fmt_out.video.i_width =
548 p_text->fmt_out.video.i_visible_width = 32;
549 p_text->fmt_out.video.i_height =
550 p_text->fmt_out.video.i_visible_height = 32;
552 p_text->pf_sub_buffer_new = spu_new_buffer;
553 p_text->pf_sub_buffer_del = spu_del_buffer;
555 vlc_object_attach( p_text, p_spu );
557 /* FIXME TOCHECK shouldn't module_Need( , , psz_modulename, false ) do the
558 * same than these 2 calls ? */
559 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
560 if( psz_modulename && *psz_modulename )
562 p_text->p_module = module_Need( p_text, "text renderer",
563 psz_modulename, true );
565 free( psz_modulename );
567 if( !p_text->p_module )
568 p_text->p_module = module_Need( p_text, "text renderer", NULL, false );
571 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj, vlc_fourcc_t i_chroma )
575 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
576 VLC_OBJECT_GENERIC, "scale" );
580 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
581 p_scale->fmt_in.video.i_chroma = i_chroma;
582 p_scale->fmt_in.video.i_width =
583 p_scale->fmt_in.video.i_height = 32;
585 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
586 p_scale->fmt_out.video.i_chroma = i_chroma;
587 p_scale->fmt_out.video.i_width =
588 p_scale->fmt_out.video.i_height = 16;
590 p_scale->pf_vout_buffer_new = spu_new_video_buffer;
591 p_scale->pf_vout_buffer_del = spu_del_video_buffer;
593 vlc_object_attach( p_scale, p_obj );
594 p_scale->p_module = module_Need( p_scale, "video filter2", 0, 0 );
598 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
600 /* FIXME: We'll also be using it for YUVA and RGBA blending ... */
602 assert( !p_spu->p_scale );
603 assert( !p_spu->p_scale_yuvp );
604 p_spu->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu), VLC_FOURCC('Y','U','V','A') );
605 p_spu->p_scale_yuvp = p_spu->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu), VLC_FOURCC('Y','U','V','P') );
608 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
609 subpicture_t *p_subpic, subpicture_region_t *p_region, int i_min_scale_ratio )
611 assert( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') );
613 if( !p_spu->p_text || !p_spu->p_text->p_module )
616 /* Setup 3 variables which can be used to render
617 * time-dependent text (and effects). The first indicates
618 * the total amount of time the text will be on screen,
619 * the second the amount of time it has already been on
620 * screen (can be a negative value as text is layed out
621 * before it is rendered) and the third is a feedback
622 * variable from the renderer - if the renderer sets it
623 * then this particular text is time-dependent, eg. the
624 * visual progress bar inside the text in karaoke and the
625 * text needs to be rendered multiple times in order for
626 * the effect to work - we therefore need to return the
627 * region to its original state at the end of the loop,
628 * instead of leaving it in YUVA or YUVP.
629 * Any renderer which is unaware of how to render
630 * time-dependent text can happily ignore the variables
631 * and render the text the same as usual - it should at
632 * least show up on screen, but the effect won't change
633 * the text over time.
636 /* FIXME why these variables are recreated every time and not
637 * when text renderer module was created ? */
638 var_Create( p_spu->p_text, "spu-duration", VLC_VAR_TIME );
639 var_Create( p_spu->p_text, "spu-elapsed", VLC_VAR_TIME );
640 var_Create( p_spu->p_text, "text-rerender", VLC_VAR_BOOL );
641 var_Create( p_spu->p_text, "scale", VLC_VAR_INTEGER );
643 var_SetTime( p_spu->p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
644 var_SetTime( p_spu->p_text, "spu-elapsed", mdate() - p_subpic->i_start );
645 var_SetBool( p_spu->p_text, "text-rerender", false );
646 var_SetInteger( p_spu->p_text, "scale", i_min_scale_ratio );
648 if( p_spu->p_text->pf_render_html && p_region->psz_html )
650 p_spu->p_text->pf_render_html( p_spu->p_text,
651 p_region, p_region );
653 else if( p_spu->p_text->pf_render_text )
655 p_spu->p_text->pf_render_text( p_spu->p_text,
656 p_region, p_region );
658 *pb_rerender_text = var_GetBool( p_spu->p_text, "text-rerender" );
660 var_Destroy( p_spu->p_text, "spu-duration" );
661 var_Destroy( p_spu->p_text, "spu-elapsed" );
662 var_Destroy( p_spu->p_text, "text-rerender" );
663 var_Destroy( p_spu->p_text, "scale" );
666 p_region->i_align |= SUBPICTURE_RENDERED;
670 * A few scale functions helpers.
678 static spu_scale_t spu_scale_create( int w, int h )
680 spu_scale_t s = { .w = w, .h = h };
687 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
689 return spu_scale_create( wn * SCALE_UNIT / wd,
690 hn * SCALE_UNIT / hd );
692 static int spu_scale_w( int v, const spu_scale_t s )
694 return v * s.w / SCALE_UNIT;
696 static int spu_scale_h( int v, const spu_scale_t s )
698 return v * s.h / SCALE_UNIT;
704 static void SpuRegionPlace( int *pi_x, int *pi_y,
705 const video_format_t *p_fmt,
706 const subpicture_t *p_subpic,
707 const subpicture_region_t *p_region )
709 int i_delta_x = p_region->i_x;
710 int i_delta_y = p_region->i_y;
713 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
717 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
719 i_y = p_fmt->i_height - p_region->fmt.i_height - i_delta_y;
723 i_y = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
726 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
730 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
732 i_x = p_fmt->i_width - p_region->fmt.i_width - i_delta_x;
736 i_x = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
739 if( p_subpic->b_absolute )
753 static void SpuRenderRegion( spu_t *p_spu,
754 picture_t *p_pic_dst,
755 subpicture_t *p_subpic, subpicture_region_t *p_region,
756 const spu_scale_t scale_size,
757 const video_format_t *p_fmt )
759 video_format_t fmt_original;
760 bool b_rerender_text;
761 bool b_restore_format = false;
767 vlc_assert_locked( &p_spu->subpicture_lock );
769 fmt_original = p_region->fmt;
770 b_rerender_text = false;
771 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
773 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region, SCALE_UNIT );
774 b_restore_format = b_rerender_text;
776 /* Check if the rendering has failed ... */
777 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
781 /* Force palette if requested
782 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
783 * instead of only the right one (being the dvd spu).
785 const bool b_using_palette = p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','P');
786 const bool b_force_palette = b_using_palette && p_spu->b_force_palette;
787 const bool b_force_crop = b_force_palette && p_spu->b_force_crop;
789 if( b_force_palette )
791 /* It looks so wrong I won't comment
792 * p_palette->palette is [256][4] with a int i_entries
793 * p_spu->palette is [4][4]
795 p_region->fmt.p_palette->i_entries = 4;
796 memcpy( p_region->fmt.p_palette->palette, p_spu->palette, 4*sizeof(uint32_t) );
799 if( b_using_palette )
800 p_scale = p_spu->p_scale_yuvp;
802 p_scale = p_spu->p_scale;
805 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_force_palette ) )
807 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
808 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
810 /* Destroy if cache is unusable */
811 if( p_region->p_cache )
813 if( p_region->p_cache->fmt.i_width != i_dst_width ||
814 p_region->p_cache->fmt.i_height != i_dst_height ||
817 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
819 p_region->p_cache = NULL;
823 /* Scale if needed into cache */
824 if( !p_region->p_cache )
828 p_scale->fmt_in.video = p_region->fmt;
829 p_scale->fmt_out.video = p_region->fmt;
832 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
833 &p_scale->fmt_out.video );
834 p_region->p_cache->p_next = p_region->p_next;
836 if( p_scale->fmt_out.video.p_palette )
837 *p_scale->fmt_out.video.p_palette =
838 *p_region->fmt.p_palette;
840 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
841 &p_region->picture );
843 p_scale->fmt_out.video.i_width = i_dst_width;
844 p_scale->fmt_out.video.i_height = i_dst_height;
846 p_scale->fmt_out.video.i_visible_width =
847 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
848 p_scale->fmt_out.video.i_visible_height =
849 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
851 p_region->p_cache->fmt = p_scale->fmt_out.video;
852 p_region->p_cache->i_x = spu_scale_w( p_region->i_x, scale_size );
853 p_region->p_cache->i_y = spu_scale_h( p_region->i_y, scale_size );
854 p_region->p_cache->i_align = p_region->i_align;
855 p_region->p_cache->i_alpha = p_region->i_alpha;
858 if( p_scale->p_module )
859 p_pic = p_scale->pf_video_filter( p_scale, &p_region->p_cache->picture );
861 msg_Err( p_spu, "scaling failed (module not loaded)" );
865 p_region->p_cache->picture = *p_pic;
870 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
872 p_region->p_cache = NULL;
876 /* And use the scaled picture */
877 if( p_region->p_cache )
879 p_region = p_region->p_cache;
880 fmt_original = p_region->fmt;
885 SpuRegionPlace( &i_x_offset, &i_y_offset,
886 p_fmt, p_subpic, p_region );
888 if( p_spu->i_margin != 0 && !b_force_crop )
891 int i_low = i_y_offset - p_spu->i_margin;
892 int i_high = i_low + p_region->fmt.i_height;
894 /* crop extra margin to keep within bounds */
897 if( i_high > (int)p_fmt->i_height )
898 i_diff = i_high - p_fmt->i_height;
899 i_y_offset -= p_spu->i_margin + i_diff;
902 /* Force cropping if requested */
905 video_format_t *p_fmt = &p_region->fmt;
906 int i_crop_x = spu_scale_w( p_spu->i_crop_x, scale_size );
907 int i_crop_y = spu_scale_h( p_spu->i_crop_y, scale_size );
908 int i_crop_width = spu_scale_w( p_spu->i_crop_width, scale_size );
909 int i_crop_height= spu_scale_h( p_spu->i_crop_height,scale_size );
911 /* Find the intersection */
912 if( i_crop_x + i_crop_width <= i_x_offset ||
913 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
914 i_crop_y + i_crop_height <= i_y_offset ||
915 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
917 /* No intersection */
918 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
922 int i_x, i_y, i_x_end, i_y_end;
923 i_x = __MAX( i_crop_x, i_x_offset );
924 i_y = __MAX( i_crop_y, i_y_offset );
925 i_x_end = __MIN( i_crop_x + i_crop_width,
926 i_x_offset + (int)p_fmt->i_visible_width );
927 i_y_end = __MIN( i_crop_y + i_crop_height,
928 i_y_offset + (int)p_fmt->i_visible_height );
930 p_fmt->i_x_offset = i_x - i_x_offset;
931 p_fmt->i_y_offset = i_y - i_y_offset;
932 p_fmt->i_visible_width = i_x_end - i_x;
933 p_fmt->i_visible_height = i_y_end - i_y;
938 b_restore_format = true;
941 i_x_offset = __MAX( i_x_offset, 0 );
942 i_y_offset = __MAX( i_y_offset, 0 );
944 /* Compute alpha blend value */
946 if( p_subpic->b_fade )
948 mtime_t i_fade_start = ( p_subpic->i_stop +
949 p_subpic->i_start ) / 2;
950 mtime_t i_now = mdate();
951 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
953 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
954 ( p_subpic->i_stop - i_fade_start );
958 /* Update the blender */
959 SpuRenderUpdateBlend( p_spu, p_fmt->i_width, p_fmt->i_height, &p_region->fmt );
961 if( p_spu->p_blend->p_module )
963 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
964 &p_region->picture, i_x_offset, i_y_offset,
965 i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025 );
969 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
970 (char *)&p_spu->p_blend->fmt_out.video.i_chroma,
971 (char *)&p_spu->p_blend->fmt_out.video.i_chroma );
975 if( b_rerender_text )
977 /* Some forms of subtitles need to be re-rendered more than
978 * once, eg. karaoke. We therefore restore the region to its
979 * pre-rendered state, so the next time through everything is
982 p_region->picture.pf_release( &p_region->picture );
983 memset( &p_region->picture, 0, sizeof( picture_t ) );
984 p_region->i_align &= ~SUBPICTURE_RENDERED;
986 if( b_restore_format )
987 p_region->fmt = fmt_original;
990 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt_a,
991 picture_t *p_pic_dst,
992 subpicture_t *p_subpic_list,
993 int i_scale_width_orig, int i_scale_height_orig )
995 video_format_t fmt = *p_fmt_a, *p_fmt = &fmt;
996 const mtime_t i_current_date = mdate();
997 int i_source_video_width;
998 int i_source_video_height;
999 subpicture_t *p_subpic;
1000 spu_scale_t scale_size_org;
1003 vlc_mutex_lock( &p_spu->subpicture_lock );
1005 /* Be sure we have at least 1 picture to process */
1006 if( !p_subpic_list || p_subpic_list->i_status == FREE_SUBPICTURE )
1008 vlc_mutex_unlock( &p_spu->subpicture_lock );
1012 scale_size_org = spu_scale_create( i_scale_width_orig, i_scale_height_orig );
1014 i_source_video_width = p_fmt->i_width * SCALE_UNIT / scale_size_org.w;
1015 i_source_video_height = p_fmt->i_height * SCALE_UNIT / scale_size_org.h;
1018 for( p_subpic = p_subpic_list;
1019 p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE; /* Check again status (as we where unlocked) */
1020 p_subpic = p_subpic->p_next )
1023 if( p_subpic->pf_pre_render )
1024 p_subpic->pf_pre_render( p_fmt, p_spu, p_subpic );
1026 if( p_subpic->pf_update_regions )
1028 /* TODO do not reverse the scaling that was done before calling
1029 * spu_RenderSubpictures, just pass it along (or do it inside
1030 * spu_RenderSubpictures) */
1031 video_format_t fmt_org = *p_fmt;
1033 fmt_org.i_visible_width = i_source_video_width;
1035 fmt_org.i_visible_height = i_source_video_height;
1037 p_subpic->pf_update_regions( &fmt_org, p_spu, p_subpic, i_current_date );
1041 /* Create the blending module */
1042 if( !p_spu->p_blend )
1043 SpuRenderCreateBlend( p_spu, p_fmt->i_chroma, p_fmt->i_aspect );
1046 for( p_subpic = p_subpic_list; ; p_subpic = p_subpic->p_next )
1048 subpicture_region_t *p_region;
1050 if( !p_subpic || p_subpic->i_status == FREE_SUBPICTURE )
1053 if( !p_subpic->p_region )
1056 /* FIXME when possible use a better rendering size than source size
1057 * (max of display size and source size for example) FIXME */
1058 int i_render_width = p_subpic->i_original_picture_width;
1059 int i_render_height = p_subpic->i_original_picture_height;
1060 if( !i_render_width || !i_render_height )
1062 if( i_render_width != 0 || i_render_height != 0 )
1063 msg_Err( p_spu, "unsupported original picture size %dx%d",
1064 i_render_width, i_render_height );
1066 i_render_width = i_source_video_width;
1067 i_render_height = i_source_video_height;
1072 p_spu->p_text->fmt_out.video.i_width =
1073 p_spu->p_text->fmt_out.video.i_visible_width = i_render_width;
1075 p_spu->p_text->fmt_out.video.i_height =
1076 p_spu->p_text->fmt_out.video.i_visible_height = i_render_height;
1079 /* Compute scaling from picture to source size */
1080 spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
1081 i_source_video_height, i_render_height );
1083 /* Update scaling from source size to display size(p_fmt) */
1084 scale.w = scale.w * p_fmt->i_width / i_source_video_width;
1085 scale.h = scale.h * p_fmt->i_height / i_source_video_height;
1087 /* Set default subpicture aspect ratio
1088 * FIXME if we only handle 1 aspect ratio per picture, why is it set per
1090 p_region = p_subpic->p_region;
1091 if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
1093 if( p_region->fmt.i_aspect != 0 )
1095 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
1096 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
1100 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
1101 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
1105 /* Take care of the aspect ratio */
1106 if( p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
1107 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
1109 /* FIXME FIXME what about region->i_x/i_y ? */
1111 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
1112 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
1115 /* Render all regions */
1116 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
1118 /* Check scale validity */
1119 if( scale.w <= 0 || scale.h <= 0 )
1123 SpuRenderRegion( p_spu, p_pic_dst, p_subpic, p_region,
1128 vlc_mutex_unlock( &p_spu->subpicture_lock );
1131 /*****************************************************************************
1132 * spu_SortSubpictures: find the subpictures to display
1133 *****************************************************************************
1134 * This function parses all subpictures and decides which ones need to be
1135 * displayed. This operation does not need lock, since only READY_SUBPICTURE
1136 * are handled. If no picture has been selected, display_date will depend on
1138 * We also check for ephemer DVD subpictures (subpictures that have
1139 * to be removed if a newer one is available), which makes it a lot
1140 * more difficult to guess if a subpicture has to be rendered or not.
1141 *****************************************************************************/
1142 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1143 bool b_paused, bool b_subtitle_only )
1146 subpicture_t *p_subpic = NULL;
1148 /* Run subpicture filters */
1149 filter_chain_SubFilter( p_spu->p_chain, display_date );
1151 /* We get an easily parsable chained list of subpictures which
1152 * ends with NULL since p_subpic was initialized to NULL. */
1153 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
1155 subpicture_t *p_ephemer = NULL;
1156 mtime_t ephemer_date = 0;
1159 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1161 subpicture_t *p_current = &p_spu->p_subpicture[i_index];
1163 if( p_current->i_channel != i_channel ||
1164 p_current->i_status != READY_SUBPICTURE ||
1165 ( b_subtitle_only && !p_current->b_subtitle ) )
1170 display_date < p_current->i_start )
1172 /* Too early, come back next monday */
1176 if( p_current->i_start > ephemer_date )
1177 ephemer_date = p_current->i_start;
1179 if( display_date > p_current->i_stop &&
1180 ( !p_current->b_ephemer || p_current->i_stop > p_current->i_start ) &&
1181 !( p_current->b_subtitle && b_paused ) ) /* XXX Assume that subtitle are pausable */
1183 /* Too late, destroy the subpic */
1184 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
1188 /* If this is an ephemer subpic, add it to our list */
1189 if( p_current->b_ephemer )
1191 p_current->p_next = p_ephemer;
1192 p_ephemer = p_current;
1197 p_current->p_next = p_subpic;
1198 p_subpic = p_current;
1201 /* If we found ephemer subpictures, check if they have to be
1202 * displayed or destroyed */
1203 while( p_ephemer != NULL )
1205 subpicture_t *p_tmp = p_ephemer;
1206 p_ephemer = p_ephemer->p_next;
1208 if( p_tmp->i_start < ephemer_date )
1210 /* Ephemer subpicture has lived too long */
1211 spu_DestroySubpicture( p_spu, p_tmp );
1215 /* Ephemer subpicture can still live a bit */
1216 p_tmp->p_next = p_subpic;
1225 /*****************************************************************************
1226 * SpuClearChannel: clear an spu channel
1227 *****************************************************************************
1228 * This function destroys the subpictures which belong to the spu channel
1229 * corresponding to i_channel_id.
1230 *****************************************************************************/
1231 static void SpuClearChannel( spu_t *p_spu, int i_channel, bool b_locked )
1233 int i_subpic; /* subpicture index */
1234 subpicture_t *p_subpic = NULL; /* first free subpicture */
1237 vlc_mutex_lock( &p_spu->subpicture_lock );
1239 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1241 p_subpic = &p_spu->p_subpicture[i_subpic];
1242 if( p_subpic->i_status == FREE_SUBPICTURE
1243 || ( p_subpic->i_status != RESERVED_SUBPICTURE
1244 && p_subpic->i_status != READY_SUBPICTURE ) )
1249 if( p_subpic->i_channel == i_channel )
1251 while( p_subpic->p_region )
1253 subpicture_region_t *p_region = p_subpic->p_region;
1254 p_subpic->p_region = p_region->p_next;
1255 spu_DestroyRegion( p_spu, p_region );
1258 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1259 p_subpic->i_status = FREE_SUBPICTURE;
1264 vlc_mutex_unlock( &p_spu->subpicture_lock );
1267 /*****************************************************************************
1268 * spu_ControlDefault: default methods for the subpicture unit control.
1269 *****************************************************************************/
1270 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1276 case SPU_CHANNEL_REGISTER:
1277 pi = (int *)va_arg( args, int * );
1278 vlc_mutex_lock( &p_spu->subpicture_lock );
1280 *pi = p_spu->i_channel++;
1281 vlc_mutex_unlock( &p_spu->subpicture_lock );
1284 case SPU_CHANNEL_CLEAR:
1285 i = (int)va_arg( args, int );
1286 SpuClearChannel( p_spu, i, false );
1290 msg_Dbg( p_spu, "control query not supported" );
1291 return VLC_EGENERIC;
1297 /*****************************************************************************
1298 * Object variables callbacks
1299 *****************************************************************************/
1301 /*****************************************************************************
1302 * UpdateSPU: update subpicture settings
1303 *****************************************************************************
1304 * This function is called from CropCallback and at initialization time, to
1305 * retrieve crop information from the input.
1306 *****************************************************************************/
1307 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1311 vlc_mutex_lock( &p_spu->subpicture_lock );
1313 p_spu->b_force_palette = false;
1314 p_spu->b_force_crop = false;
1316 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1318 vlc_mutex_unlock( &p_spu->subpicture_lock );
1322 p_spu->b_force_crop = true;
1323 var_Get( p_object, "x-start", &val );
1324 p_spu->i_crop_x = val.i_int;
1325 var_Get( p_object, "y-start", &val );
1326 p_spu->i_crop_y = val.i_int;
1327 var_Get( p_object, "x-end", &val );
1328 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1329 var_Get( p_object, "y-end", &val );
1330 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1332 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1334 memcpy( p_spu->palette, val.p_address, 16 );
1335 p_spu->b_force_palette = true;
1337 vlc_mutex_unlock( &p_spu->subpicture_lock );
1339 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1340 p_spu->i_crop_x, p_spu->i_crop_y,
1341 p_spu->i_crop_width, p_spu->i_crop_height,
1342 p_spu->b_force_palette );
1345 /*****************************************************************************
1346 * CropCallback: called when the highlight properties are changed
1347 *****************************************************************************
1348 * This callback is called from the input thread when we need cropping
1349 *****************************************************************************/
1350 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1351 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1353 (void)psz_var; (void)oldval; (void)newval;
1354 UpdateSPU( (spu_t *)p_data, p_object );
1358 /*****************************************************************************
1359 * Buffers allocation callbacks for the filters
1360 *****************************************************************************/
1361 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1363 filter_owner_sys_t *p_sys = p_filter->p_owner;
1364 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1365 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1366 return p_subpicture;
1369 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1371 filter_owner_sys_t *p_sys = p_filter->p_owner;
1372 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1375 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1378 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1379 if( !p_subpic ) return NULL;
1380 memset( p_subpic, 0, sizeof(subpicture_t) );
1381 p_subpic->b_absolute = true;
1383 p_subpic->pf_create_region = __spu_CreateRegion;
1384 p_subpic->pf_make_region = __spu_MakeRegion;
1385 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1390 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1392 while( p_subpic->p_region )
1394 subpicture_region_t *p_region = p_subpic->p_region;
1395 p_subpic->p_region = p_region->p_next;
1396 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1402 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1404 picture_t *p_picture = malloc( sizeof(picture_t) );
1405 if( !p_picture ) return NULL;
1406 if( vout_AllocatePicture( p_filter, p_picture,
1407 p_filter->fmt_out.video.i_chroma,
1408 p_filter->fmt_out.video.i_width,
1409 p_filter->fmt_out.video.i_height,
1410 p_filter->fmt_out.video.i_aspect )
1417 p_picture->pf_release = RegionPictureRelease;
1422 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1427 free( p_pic->p_data_orig );
1432 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1433 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1435 VLC_UNUSED(p_object); VLC_UNUSED(oldval);
1436 VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1438 spu_t *p_spu = (spu_t *)p_data;
1439 vlc_mutex_lock( &p_spu->subpicture_lock );
1440 filter_chain_Reset( p_spu->p_chain, NULL, NULL );
1441 spu_ParseChain( p_spu );
1442 vlc_mutex_unlock( &p_spu->subpicture_lock );
1446 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1448 spu_t *p_spu = (spu_t *)p_data;
1450 p_filter->pf_sub_buffer_new = sub_new_buffer;
1451 p_filter->pf_sub_buffer_del = sub_del_buffer;
1453 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1454 if( !p_sys ) return VLC_EGENERIC;
1456 p_filter->p_owner = p_sys;
1457 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1458 p_sys->p_spu = p_spu;
1463 static void SubFilterAllocationClean( filter_t *p_filter )
1465 filter_owner_sys_t *p_sys = p_filter->p_owner;
1466 SpuClearChannel( p_sys->p_spu, p_sys->i_channel, true );
1467 free( p_filter->p_owner );