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_picture )
220 if( --p_picture->i_refcount > 0 )
223 assert( p_picture->i_refcount == 0 );
224 free( p_picture->p_q );
225 free( p_picture->p_data_orig );
226 free( p_picture->p_sys );
230 * Create a subpicture region
232 * \param p_this vlc_object_t
233 * \param p_fmt the format that this subpicture region should have
235 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
236 video_format_t *p_fmt )
238 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
242 /* FIXME is that *really* wanted? */
243 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
244 p_fmt->p_palette = calloc( 1, sizeof(video_palette_t) );
246 p_fmt->p_palette = NULL; /* XXX and that above all? */
248 p_region->fmt = *p_fmt;
249 p_region->i_alpha = 0xff;
250 p_region->p_next = NULL;
251 p_region->p_cache = NULL;
252 p_region->psz_text = NULL;
253 p_region->p_style = NULL;
255 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') )
258 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
259 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
261 if( !p_region->picture.i_planes )
263 free( p_fmt->p_palette );
268 p_region->picture.i_refcount = 1;
269 p_region->picture.pf_release = RegionPictureRelease;
275 * Destroy a subpicture region
277 * \param p_this vlc_object_t
278 * \param p_region the subpicture region to destroy
280 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
285 picture_Release( &p_region->picture );
287 free( p_region->fmt.p_palette );
288 if( p_region->p_cache )
289 __spu_DestroyRegion( p_this, p_region->p_cache );
291 free( p_region->psz_text );
292 free( p_region->psz_html );
293 //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
298 * Display a subpicture
300 * Remove the reservation flag of a subpicture, which will cause it to be
302 * \param p_spu the subpicture unit object
303 * \param p_subpic the subpicture to display
305 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
307 /* Check if status is valid */
308 if( p_subpic->i_status != RESERVED_SUBPICTURE )
310 msg_Err( p_spu, "subpicture %p has invalid status #%d",
311 p_subpic, p_subpic->i_status );
314 /* Remove reservation flag */
315 p_subpic->i_status = READY_SUBPICTURE;
317 if( p_subpic->i_channel == DEFAULT_CHAN )
319 p_subpic->i_channel = 0xFFFF;
320 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
321 p_subpic->i_channel = DEFAULT_CHAN;
326 * Allocate a subpicture in the spu heap.
328 * This function create a reserved subpicture in the spu heap.
329 * A null pointer is returned if the function fails. This method provides an
330 * already allocated zone of memory in the spu data fields. It needs locking
331 * since several pictures can be created by several producers threads.
332 * \param p_spu the subpicture unit in which to create the subpicture
333 * \return NULL on error, a reserved subpicture otherwise
335 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
337 int i_subpic; /* subpicture index */
338 subpicture_t * p_subpic = NULL; /* first free subpicture */
341 vlc_mutex_lock( &p_spu->subpicture_lock );
344 * Look for an empty place
347 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
349 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
351 /* Subpicture is empty and ready for allocation */
352 p_subpic = &p_spu->p_subpicture[i_subpic];
353 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
358 /* If no free subpicture could be found */
359 if( p_subpic == NULL )
361 msg_Err( p_spu, "subpicture heap is full" );
362 vlc_mutex_unlock( &p_spu->subpicture_lock );
366 /* Copy subpicture information, set some default values */
367 memset( p_subpic, 0, sizeof(subpicture_t) );
368 p_subpic->i_status = RESERVED_SUBPICTURE;
369 p_subpic->b_absolute = true;
370 p_subpic->b_fade = false;
371 p_subpic->b_subtitle = false;
372 p_subpic->i_alpha = 0xFF;
373 p_subpic->p_region = NULL;
374 p_subpic->pf_render = NULL;
375 p_subpic->pf_destroy = NULL;
376 p_subpic->p_sys = NULL;
377 vlc_mutex_unlock( &p_spu->subpicture_lock );
379 p_subpic->pf_create_region = __spu_CreateRegion;
380 p_subpic->pf_destroy_region = __spu_DestroyRegion;
386 * Remove a subpicture from the heap
388 * This function frees a previously reserved subpicture.
389 * It is meant to be used when the construction of a picture aborted.
390 * This function does not need locking since reserved subpictures are ignored
393 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
396 vlc_mutex_lock( &p_spu->subpicture_lock );
398 /* There can be race conditions so we need to check the status */
399 if( p_subpic->i_status == FREE_SUBPICTURE )
401 vlc_mutex_unlock( &p_spu->subpicture_lock );
405 /* Check if status is valid */
406 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
407 && ( p_subpic->i_status != READY_SUBPICTURE ) )
409 msg_Err( p_spu, "subpicture %p has invalid status %d",
410 p_subpic, p_subpic->i_status );
413 while( p_subpic->p_region )
415 subpicture_region_t *p_region = p_subpic->p_region;
416 p_subpic->p_region = p_region->p_next;
417 spu_DestroyRegion( p_spu, p_region );
420 if( p_subpic->pf_destroy )
422 p_subpic->pf_destroy( p_subpic );
425 p_subpic->i_status = FREE_SUBPICTURE;
427 vlc_mutex_unlock( &p_spu->subpicture_lock );
430 /*****************************************************************************
431 * spu_RenderSubpictures: render a subpicture list
432 *****************************************************************************
433 * This function renders all sub picture units in the list.
434 *****************************************************************************/
435 static void FilterRelease( filter_t *p_filter )
437 if( p_filter->p_module )
438 module_Unneed( p_filter, p_filter->p_module );
440 vlc_object_detach( p_filter );
441 vlc_object_release( p_filter );
444 static void SpuRenderCreateBlend( spu_t *p_spu, vlc_fourcc_t i_chroma, int i_aspect )
448 assert( !p_spu->p_blend );
451 p_blend = vlc_custom_create( p_spu, sizeof(filter_t),
452 VLC_OBJECT_GENERIC, "blend" );
456 es_format_Init( &p_blend->fmt_in, VIDEO_ES, 0 );
458 es_format_Init( &p_blend->fmt_out, VIDEO_ES, 0 );
459 p_blend->fmt_out.video.i_x_offset = 0;
460 p_blend->fmt_out.video.i_y_offset = 0;
461 p_blend->fmt_out.video.i_chroma = i_chroma;
462 p_blend->fmt_out.video.i_aspect = i_aspect;
464 /* The blend module will be loaded when needed with the real
466 p_blend->p_module = NULL;
469 vlc_object_attach( p_blend, p_spu );
471 static void SpuRenderUpdateBlend( spu_t *p_spu, int i_out_width, int i_out_height, const video_format_t *p_in_fmt )
473 filter_t *p_blend = p_spu->p_blend;
478 if( p_blend->p_module && p_blend->fmt_in.video.i_chroma != p_in_fmt->i_chroma )
480 /* The chroma is not the same, we need to reload the blend module
481 * XXX to match the old behaviour just test !p_blend->fmt_in.video.i_chroma */
482 module_Unneed( p_blend, p_blend->p_module );
483 p_blend->p_module = NULL;
487 p_blend->fmt_in.video = *p_in_fmt;
490 p_blend->fmt_out.video.i_width =
491 p_blend->fmt_out.video.i_visible_width = i_out_width;
492 p_blend->fmt_out.video.i_height =
493 p_blend->fmt_out.video.i_visible_height = i_out_height;
496 if( !p_blend->p_module )
497 p_blend->p_module = module_Need( p_blend, "video blending", 0, 0 );
499 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
503 assert( !p_spu->p_text );
506 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
507 VLC_OBJECT_GENERIC, "spu text" );
511 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
513 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
514 p_text->fmt_out.video.i_width =
515 p_text->fmt_out.video.i_visible_width = 32;
516 p_text->fmt_out.video.i_height =
517 p_text->fmt_out.video.i_visible_height = 32;
519 p_text->pf_sub_buffer_new = spu_new_buffer;
520 p_text->pf_sub_buffer_del = spu_del_buffer;
522 vlc_object_attach( p_text, p_spu );
524 /* FIXME TOCHECK shouldn't module_Need( , , psz_modulename, false ) do the
525 * same than these 2 calls ? */
526 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
527 if( psz_modulename && *psz_modulename )
529 p_text->p_module = module_Need( p_text, "text renderer",
530 psz_modulename, true );
532 free( psz_modulename );
534 if( !p_text->p_module )
535 p_text->p_module = module_Need( p_text, "text renderer", NULL, false );
538 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj, vlc_fourcc_t i_chroma )
542 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
543 VLC_OBJECT_GENERIC, "scale" );
547 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
548 p_scale->fmt_in.video.i_chroma = i_chroma;
549 p_scale->fmt_in.video.i_width =
550 p_scale->fmt_in.video.i_height = 32;
552 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
553 p_scale->fmt_out.video.i_chroma = i_chroma;
554 p_scale->fmt_out.video.i_width =
555 p_scale->fmt_out.video.i_height = 16;
557 p_scale->pf_vout_buffer_new = spu_new_video_buffer;
558 p_scale->pf_vout_buffer_del = spu_del_video_buffer;
560 vlc_object_attach( p_scale, p_obj );
561 p_scale->p_module = module_Need( p_scale, "video filter2", 0, 0 );
565 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
567 /* FIXME: We'll also be using it for YUVA and RGBA blending ... */
569 assert( !p_spu->p_scale );
570 assert( !p_spu->p_scale_yuvp );
571 p_spu->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu), VLC_FOURCC('Y','U','V','A') );
572 p_spu->p_scale_yuvp = p_spu->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu), VLC_FOURCC('Y','U','V','P') );
575 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
576 subpicture_t *p_subpic, subpicture_region_t *p_region, int i_min_scale_ratio )
578 assert( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') );
580 if( !p_spu->p_text || !p_spu->p_text->p_module )
583 /* Setup 3 variables which can be used to render
584 * time-dependent text (and effects). The first indicates
585 * the total amount of time the text will be on screen,
586 * the second the amount of time it has already been on
587 * screen (can be a negative value as text is layed out
588 * before it is rendered) and the third is a feedback
589 * variable from the renderer - if the renderer sets it
590 * then this particular text is time-dependent, eg. the
591 * visual progress bar inside the text in karaoke and the
592 * text needs to be rendered multiple times in order for
593 * the effect to work - we therefore need to return the
594 * region to its original state at the end of the loop,
595 * instead of leaving it in YUVA or YUVP.
596 * Any renderer which is unaware of how to render
597 * time-dependent text can happily ignore the variables
598 * and render the text the same as usual - it should at
599 * least show up on screen, but the effect won't change
600 * the text over time.
603 /* FIXME why these variables are recreated every time and not
604 * when text renderer module was created ? */
605 var_Create( p_spu->p_text, "spu-duration", VLC_VAR_TIME );
606 var_Create( p_spu->p_text, "spu-elapsed", VLC_VAR_TIME );
607 var_Create( p_spu->p_text, "text-rerender", VLC_VAR_BOOL );
608 var_Create( p_spu->p_text, "scale", VLC_VAR_INTEGER );
610 var_SetTime( p_spu->p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
611 var_SetTime( p_spu->p_text, "spu-elapsed", mdate() - p_subpic->i_start );
612 var_SetBool( p_spu->p_text, "text-rerender", false );
613 var_SetInteger( p_spu->p_text, "scale", i_min_scale_ratio );
615 if( p_spu->p_text->pf_render_html && p_region->psz_html )
617 p_spu->p_text->pf_render_html( p_spu->p_text,
618 p_region, p_region );
620 else if( p_spu->p_text->pf_render_text )
622 p_spu->p_text->pf_render_text( p_spu->p_text,
623 p_region, p_region );
625 *pb_rerender_text = var_GetBool( p_spu->p_text, "text-rerender" );
627 var_Destroy( p_spu->p_text, "spu-duration" );
628 var_Destroy( p_spu->p_text, "spu-elapsed" );
629 var_Destroy( p_spu->p_text, "text-rerender" );
630 var_Destroy( p_spu->p_text, "scale" );
633 p_region->i_align |= SUBPICTURE_RENDERED;
637 * A few scale functions helpers.
645 static spu_scale_t spu_scale_create( int w, int h )
647 spu_scale_t s = { .w = w, .h = h };
654 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
656 return spu_scale_create( wn * SCALE_UNIT / wd,
657 hn * SCALE_UNIT / hd );
659 static int spu_scale_w( int v, const spu_scale_t s )
661 return v * s.w / SCALE_UNIT;
663 static int spu_scale_h( int v, const spu_scale_t s )
665 return v * s.h / SCALE_UNIT;
671 static void SpuRegionPlace( int *pi_x, int *pi_y,
672 const subpicture_t *p_subpic,
673 const subpicture_region_t *p_region,
676 int i_delta_x = p_region->i_x;
677 int i_delta_y = p_region->i_y;
680 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
684 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
686 i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
690 i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
693 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
697 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
699 i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
703 i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
706 if( p_subpic->b_absolute )
713 if( i_margin_y != 0 )
716 int i_low = i_y - i_margin_y;
717 int i_high = i_low + p_region->fmt.i_height;
719 /* crop extra margin to keep within bounds */
722 if( i_high > (int)p_subpic->i_original_picture_height )
723 i_diff = i_high - p_subpic->i_original_picture_height;
724 i_y -= i_margin_y + i_diff;
736 static void SpuRenderRegion( spu_t *p_spu,
737 picture_t *p_pic_dst,
738 subpicture_t *p_subpic, subpicture_region_t *p_region,
739 const spu_scale_t scale_size,
740 const video_format_t *p_fmt )
742 video_format_t fmt_original;
743 bool b_rerender_text;
744 bool b_restore_format = false;
750 vlc_assert_locked( &p_spu->subpicture_lock );
752 fmt_original = p_region->fmt;
753 b_rerender_text = false;
754 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
756 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region, SCALE_UNIT );
757 b_restore_format = b_rerender_text;
759 /* Check if the rendering has failed ... */
760 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
764 /* Force palette if requested
765 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
766 * instead of only the right one (being the dvd spu).
768 const bool b_using_palette = p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','P');
769 const bool b_force_palette = b_using_palette && p_spu->b_force_palette;
770 const bool b_force_crop = b_force_palette && p_spu->b_force_crop;
774 * We compute the position in the rendered size */
775 SpuRegionPlace( &i_x_offset, &i_y_offset,
776 p_subpic, p_region, !b_force_crop ? p_spu->i_margin : 0 );
778 /* Fix the position for the current scale_size */
779 i_x_offset = spu_scale_w( i_x_offset, scale_size );
780 i_y_offset = spu_scale_h( i_y_offset, scale_size );
782 if( b_force_palette )
784 /* It looks so wrong I won't comment
785 * p_palette->palette is [256][4] with a int i_entries
786 * p_spu->palette is [4][4]
788 p_region->fmt.p_palette->i_entries = 4;
789 memcpy( p_region->fmt.p_palette->palette, p_spu->palette, 4*sizeof(uint32_t) );
792 if( b_using_palette )
793 p_scale = p_spu->p_scale_yuvp;
795 p_scale = p_spu->p_scale;
797 /* Scale from rendered size to destination size */
799 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_force_palette ) )
801 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
802 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
804 /* Destroy if cache is unusable */
805 if( p_region->p_cache )
807 if( p_region->p_cache->fmt.i_width != i_dst_width ||
808 p_region->p_cache->fmt.i_height != i_dst_height ||
811 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
813 p_region->p_cache = NULL;
817 /* Scale if needed into cache */
818 if( !p_region->p_cache )
822 p_scale->fmt_in.video = p_region->fmt;
823 p_scale->fmt_out.video = p_region->fmt;
825 if( p_scale->fmt_out.video.p_palette )
826 *p_scale->fmt_out.video.p_palette =
827 *p_region->fmt.p_palette;
829 p_scale->fmt_out.video.i_width = i_dst_width;
830 p_scale->fmt_out.video.i_height = i_dst_height;
832 p_scale->fmt_out.video.i_visible_width =
833 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
834 p_scale->fmt_out.video.i_visible_height =
835 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
838 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
839 &p_scale->fmt_out.video );
842 if( p_scale->p_module )
844 picture_Yield( &p_region->picture );
845 p_pic = p_scale->pf_video_filter( p_scale, &p_region->picture );
849 picture_Copy( &p_region->p_cache->picture, p_pic );
850 picture_Release( p_pic );
852 p_region->p_cache->fmt = p_scale->fmt_out.video;
853 p_region->p_cache->i_x = spu_scale_w( p_region->i_x, scale_size );
854 p_region->p_cache->i_y = spu_scale_h( p_region->i_y, scale_size );
855 p_region->p_cache->i_align = p_region->i_align;
856 p_region->p_cache->i_alpha = p_region->i_alpha;
860 msg_Err( p_spu, "scaling failed (module not loaded)" );
861 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
863 p_region->p_cache = NULL;
867 /* And use the scaled picture */
868 if( p_region->p_cache )
870 p_region = p_region->p_cache;
871 fmt_original = p_region->fmt;
875 /* Force cropping if requested */
878 video_format_t *p_fmt = &p_region->fmt;
879 int i_crop_x = spu_scale_w( p_spu->i_crop_x, scale_size );
880 int i_crop_y = spu_scale_h( p_spu->i_crop_y, scale_size );
881 int i_crop_width = spu_scale_w( p_spu->i_crop_width, scale_size );
882 int i_crop_height= spu_scale_h( p_spu->i_crop_height,scale_size );
884 /* Find the intersection */
885 if( i_crop_x + i_crop_width <= i_x_offset ||
886 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
887 i_crop_y + i_crop_height <= i_y_offset ||
888 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
890 /* No intersection */
891 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
895 int i_x, i_y, i_x_end, i_y_end;
896 i_x = __MAX( i_crop_x, i_x_offset );
897 i_y = __MAX( i_crop_y, i_y_offset );
898 i_x_end = __MIN( i_crop_x + i_crop_width,
899 i_x_offset + (int)p_fmt->i_visible_width );
900 i_y_end = __MIN( i_crop_y + i_crop_height,
901 i_y_offset + (int)p_fmt->i_visible_height );
903 p_fmt->i_x_offset = i_x - i_x_offset;
904 p_fmt->i_y_offset = i_y - i_y_offset;
905 p_fmt->i_visible_width = i_x_end - i_x;
906 p_fmt->i_visible_height = i_y_end - i_y;
911 b_restore_format = true;
914 i_x_offset = __MAX( i_x_offset, 0 );
915 i_y_offset = __MAX( i_y_offset, 0 );
917 /* Compute alpha blend value */
919 if( p_subpic->b_fade )
921 mtime_t i_fade_start = ( p_subpic->i_stop +
922 p_subpic->i_start ) / 2;
923 mtime_t i_now = mdate();
924 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
926 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
927 ( p_subpic->i_stop - i_fade_start );
931 /* Update the blender */
932 SpuRenderUpdateBlend( p_spu, p_fmt->i_width, p_fmt->i_height, &p_region->fmt );
934 if( p_spu->p_blend->p_module )
936 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
937 &p_region->picture, i_x_offset, i_y_offset,
938 i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025 );
942 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
943 (char *)&p_spu->p_blend->fmt_out.video.i_chroma,
944 (char *)&p_spu->p_blend->fmt_out.video.i_chroma );
948 if( b_rerender_text )
950 /* Some forms of subtitles need to be re-rendered more than
951 * once, eg. karaoke. We therefore restore the region to its
952 * pre-rendered state, so the next time through everything is
955 p_region->picture.pf_release( &p_region->picture );
956 memset( &p_region->picture, 0, sizeof( picture_t ) );
957 p_region->i_align &= ~SUBPICTURE_RENDERED;
959 if( b_restore_format )
960 p_region->fmt = fmt_original;
963 void spu_RenderSubpictures( spu_t *p_spu,
964 picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
965 subpicture_t *p_subpic_list,
966 const video_format_t *p_fmt_src )
968 const int i_source_video_width = p_fmt_src->i_width;
969 const int i_source_video_height = p_fmt_src->i_height;
970 const mtime_t i_current_date = mdate();
972 subpicture_t *p_subpic;
975 vlc_mutex_lock( &p_spu->subpicture_lock );
977 /* Be sure we have at least 1 picture to process */
978 if( !p_subpic_list || p_subpic_list->i_status == FREE_SUBPICTURE )
980 vlc_mutex_unlock( &p_spu->subpicture_lock );
985 for( p_subpic = p_subpic_list;
986 p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE; /* Check again status (as we where unlocked) */
987 p_subpic = p_subpic->p_next )
990 if( p_subpic->pf_pre_render )
991 p_subpic->pf_pre_render( p_spu, p_subpic, p_fmt_dst );
993 if( p_subpic->pf_update_regions )
995 video_format_t fmt_org = *p_fmt_dst;
997 fmt_org.i_visible_width = i_source_video_width;
999 fmt_org.i_visible_height = i_source_video_height;
1001 p_subpic->pf_update_regions( p_spu, p_subpic, &fmt_org, i_current_date );
1005 /* Create the blending module */
1006 if( !p_spu->p_blend )
1007 SpuRenderCreateBlend( p_spu, p_fmt_dst->i_chroma, p_fmt_dst->i_aspect );
1010 for( p_subpic = p_subpic_list; ; p_subpic = p_subpic->p_next )
1012 subpicture_region_t *p_region;
1014 if( !p_subpic || p_subpic->i_status == FREE_SUBPICTURE )
1017 if( !p_subpic->p_region )
1020 /* FIXME when possible use a better rendering size than source size
1021 * (max of display size and source size for example) FIXME */
1022 int i_render_width = p_subpic->i_original_picture_width;
1023 int i_render_height = p_subpic->i_original_picture_height;
1024 if( !i_render_width || !i_render_height )
1026 if( i_render_width != 0 || i_render_height != 0 )
1027 msg_Err( p_spu, "unsupported original picture size %dx%d",
1028 i_render_width, i_render_height );
1030 p_subpic->i_original_picture_width = i_render_width = i_source_video_width;
1031 p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
1036 p_spu->p_text->fmt_out.video.i_width =
1037 p_spu->p_text->fmt_out.video.i_visible_width = i_render_width;
1039 p_spu->p_text->fmt_out.video.i_height =
1040 p_spu->p_text->fmt_out.video.i_visible_height = i_render_height;
1043 /* Compute scaling from picture to source size */
1044 spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
1045 i_source_video_height, i_render_height );
1047 /* Update scaling from source size to display size(p_fmt_dst) */
1048 scale.w = scale.w * p_fmt_dst->i_width / i_source_video_width;
1049 scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
1051 /* Set default subpicture aspect ratio
1052 * FIXME if we only handle 1 aspect ratio per picture, why is it set per
1054 p_region = p_subpic->p_region;
1055 if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
1057 if( p_region->fmt.i_aspect != 0 )
1059 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
1060 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
1064 p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
1065 p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
1069 /* Take care of the aspect ratio */
1070 if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
1071 p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
1073 /* FIXME FIXME what about region->i_x/i_y ? */
1075 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
1076 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
1079 /* Render all regions */
1080 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
1082 /* Check scale validity */
1083 if( scale.w <= 0 || scale.h <= 0 )
1087 SpuRenderRegion( p_spu, p_pic_dst, p_subpic, p_region,
1092 vlc_mutex_unlock( &p_spu->subpicture_lock );
1095 /*****************************************************************************
1096 * spu_SortSubpictures: find the subpictures to display
1097 *****************************************************************************
1098 * This function parses all subpictures and decides which ones need to be
1099 * displayed. This operation does not need lock, since only READY_SUBPICTURE
1100 * are handled. If no picture has been selected, display_date will depend on
1102 * We also check for ephemer DVD subpictures (subpictures that have
1103 * to be removed if a newer one is available), which makes it a lot
1104 * more difficult to guess if a subpicture has to be rendered or not.
1105 *****************************************************************************/
1106 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1107 bool b_paused, bool b_subtitle_only )
1110 subpicture_t *p_subpic = NULL;
1112 /* Run subpicture filters */
1113 filter_chain_SubFilter( p_spu->p_chain, display_date );
1115 /* We get an easily parsable chained list of subpictures which
1116 * ends with NULL since p_subpic was initialized to NULL. */
1117 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
1119 subpicture_t *p_ephemer = NULL;
1120 mtime_t ephemer_date = 0;
1123 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1125 subpicture_t *p_current = &p_spu->p_subpicture[i_index];
1127 if( p_current->i_channel != i_channel ||
1128 p_current->i_status != READY_SUBPICTURE ||
1129 ( b_subtitle_only && !p_current->b_subtitle ) )
1134 display_date < p_current->i_start )
1136 /* Too early, come back next monday */
1140 if( p_current->i_start > ephemer_date )
1141 ephemer_date = p_current->i_start;
1143 if( display_date > p_current->i_stop &&
1144 ( !p_current->b_ephemer || p_current->i_stop > p_current->i_start ) &&
1145 !( p_current->b_subtitle && b_paused ) ) /* XXX Assume that subtitle are pausable */
1147 /* Too late, destroy the subpic */
1148 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
1152 /* If this is an ephemer subpic, add it to our list */
1153 if( p_current->b_ephemer )
1155 p_current->p_next = p_ephemer;
1156 p_ephemer = p_current;
1161 p_current->p_next = p_subpic;
1162 p_subpic = p_current;
1165 /* If we found ephemer subpictures, check if they have to be
1166 * displayed or destroyed */
1167 while( p_ephemer != NULL )
1169 subpicture_t *p_tmp = p_ephemer;
1170 p_ephemer = p_ephemer->p_next;
1172 if( p_tmp->i_start < ephemer_date )
1174 /* Ephemer subpicture has lived too long */
1175 spu_DestroySubpicture( p_spu, p_tmp );
1179 /* Ephemer subpicture can still live a bit */
1180 p_tmp->p_next = p_subpic;
1189 /*****************************************************************************
1190 * SpuClearChannel: clear an spu channel
1191 *****************************************************************************
1192 * This function destroys the subpictures which belong to the spu channel
1193 * corresponding to i_channel_id.
1194 *****************************************************************************/
1195 static void SpuClearChannel( spu_t *p_spu, int i_channel, bool b_locked )
1197 int i_subpic; /* subpicture index */
1198 subpicture_t *p_subpic = NULL; /* first free subpicture */
1201 vlc_mutex_lock( &p_spu->subpicture_lock );
1203 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1205 p_subpic = &p_spu->p_subpicture[i_subpic];
1206 if( p_subpic->i_status == FREE_SUBPICTURE
1207 || ( p_subpic->i_status != RESERVED_SUBPICTURE
1208 && p_subpic->i_status != READY_SUBPICTURE ) )
1213 if( p_subpic->i_channel == i_channel )
1215 while( p_subpic->p_region )
1217 subpicture_region_t *p_region = p_subpic->p_region;
1218 p_subpic->p_region = p_region->p_next;
1219 spu_DestroyRegion( p_spu, p_region );
1222 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1223 p_subpic->i_status = FREE_SUBPICTURE;
1228 vlc_mutex_unlock( &p_spu->subpicture_lock );
1231 /*****************************************************************************
1232 * spu_ControlDefault: default methods for the subpicture unit control.
1233 *****************************************************************************/
1234 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1240 case SPU_CHANNEL_REGISTER:
1241 pi = (int *)va_arg( args, int * );
1242 vlc_mutex_lock( &p_spu->subpicture_lock );
1244 *pi = p_spu->i_channel++;
1245 vlc_mutex_unlock( &p_spu->subpicture_lock );
1248 case SPU_CHANNEL_CLEAR:
1249 i = (int)va_arg( args, int );
1250 SpuClearChannel( p_spu, i, false );
1254 msg_Dbg( p_spu, "control query not supported" );
1255 return VLC_EGENERIC;
1261 /*****************************************************************************
1262 * Object variables callbacks
1263 *****************************************************************************/
1265 /*****************************************************************************
1266 * UpdateSPU: update subpicture settings
1267 *****************************************************************************
1268 * This function is called from CropCallback and at initialization time, to
1269 * retrieve crop information from the input.
1270 *****************************************************************************/
1271 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1275 vlc_mutex_lock( &p_spu->subpicture_lock );
1277 p_spu->b_force_palette = false;
1278 p_spu->b_force_crop = false;
1280 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1282 vlc_mutex_unlock( &p_spu->subpicture_lock );
1286 p_spu->b_force_crop = true;
1287 var_Get( p_object, "x-start", &val );
1288 p_spu->i_crop_x = val.i_int;
1289 var_Get( p_object, "y-start", &val );
1290 p_spu->i_crop_y = val.i_int;
1291 var_Get( p_object, "x-end", &val );
1292 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1293 var_Get( p_object, "y-end", &val );
1294 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1296 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1298 memcpy( p_spu->palette, val.p_address, 16 );
1299 p_spu->b_force_palette = true;
1301 vlc_mutex_unlock( &p_spu->subpicture_lock );
1303 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1304 p_spu->i_crop_x, p_spu->i_crop_y,
1305 p_spu->i_crop_width, p_spu->i_crop_height,
1306 p_spu->b_force_palette );
1309 /*****************************************************************************
1310 * CropCallback: called when the highlight properties are changed
1311 *****************************************************************************
1312 * This callback is called from the input thread when we need cropping
1313 *****************************************************************************/
1314 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1315 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1317 (void)psz_var; (void)oldval; (void)newval;
1318 UpdateSPU( (spu_t *)p_data, p_object );
1322 /*****************************************************************************
1323 * Buffers allocation callbacks for the filters
1324 *****************************************************************************/
1325 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1327 filter_owner_sys_t *p_sys = p_filter->p_owner;
1328 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1329 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1330 return p_subpicture;
1333 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1335 filter_owner_sys_t *p_sys = p_filter->p_owner;
1336 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1339 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1341 subpicture_t *p_subpic = calloc( 1, sizeof(subpicture_t) );
1345 p_subpic->b_absolute = true;
1346 p_subpic->i_alpha = 0xFF;
1348 p_subpic->pf_create_region = __spu_CreateRegion;
1349 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1351 VLC_UNUSED(p_filter);
1355 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1357 while( p_subpic->p_region )
1359 subpicture_region_t *p_region = p_subpic->p_region;
1360 p_subpic->p_region = p_region->p_next;
1361 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1367 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1369 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1371 VLC_UNUSED(p_filter);
1372 return picture_New( p_fmt->i_chroma,
1373 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
1376 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1378 VLC_UNUSED(p_filter);
1379 picture_Release( p_picture );
1382 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1383 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1385 spu_t *p_spu = p_data;
1387 VLC_UNUSED(p_object); VLC_UNUSED(oldval);
1388 VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1390 vlc_mutex_lock( &p_spu->subpicture_lock );
1391 filter_chain_Reset( p_spu->p_chain, NULL, NULL );
1392 spu_ParseChain( p_spu );
1393 vlc_mutex_unlock( &p_spu->subpicture_lock );
1397 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1399 spu_t *p_spu = p_data;
1401 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1403 return VLC_EGENERIC;
1405 p_filter->pf_sub_buffer_new = sub_new_buffer;
1406 p_filter->pf_sub_buffer_del = sub_del_buffer;
1408 p_filter->p_owner = p_sys;
1409 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1410 p_sys->p_spu = p_spu;
1415 static void SubFilterAllocationClean( filter_t *p_filter )
1417 filter_owner_sys_t *p_sys = p_filter->p_owner;
1419 SpuClearChannel( p_sys->p_spu, p_sys->i_channel, true );
1420 free( p_filter->p_owner );