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"
43 /*****************************************************************************
45 *****************************************************************************/
46 static void UpdateSPU ( spu_t *, vlc_object_t * );
47 static int CropCallback( vlc_object_t *, char const *,
48 vlc_value_t, vlc_value_t, void * );
50 static int spu_vaControlDefault( spu_t *, int, va_list );
52 static subpicture_t *sub_new_buffer( filter_t * );
53 static void sub_del_buffer( filter_t *, subpicture_t * );
54 static subpicture_t *spu_new_buffer( filter_t * );
55 static void spu_del_buffer( filter_t *, subpicture_t * );
56 static picture_t *spu_new_video_buffer( filter_t * );
57 static void spu_del_video_buffer( filter_t *, picture_t * );
59 static int spu_ParseChain( spu_t * );
60 static int SubFilterCallback( vlc_object_t *, char const *,
61 vlc_value_t, vlc_value_t, void * );
63 static int SubFilterAllocationInit( filter_t *, void * );
64 static void SubFilterAllocationClean( filter_t * );
65 struct filter_owner_sys_t
71 #define SCALE_UNIT (1000)
74 static void SpuRenderCreateAndLoadText( spu_t *p_spu );
75 static void SpuRenderCreateAndLoadScale( spu_t *p_spu );
76 static void FilterRelease( filter_t *p_filter );
79 * Creates the subpicture unit
81 * \param p_this the parent object which creates the subpicture unit
83 spu_t *__spu_Create( vlc_object_t *p_this )
86 spu_t *p_spu = vlc_custom_create( p_this, sizeof( spu_t ),
87 VLC_OBJECT_GENERIC, "subpicture" );
89 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
91 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
94 p_spu->p_blend = NULL;
96 p_spu->p_scale = NULL;
97 p_spu->p_scale_yuvp = NULL;
98 p_spu->pf_control = spu_vaControlDefault;
100 /* Register the default subpicture channel */
101 p_spu->i_channel = 2;
103 vlc_mutex_init( &p_spu->subpicture_lock );
105 vlc_object_attach( p_spu, p_this );
107 p_spu->p_chain = filter_chain_New( p_spu, "sub filter", false,
108 SubFilterAllocationInit,
109 SubFilterAllocationClean,
112 /* Load text and scale module */
113 SpuRenderCreateAndLoadText( p_spu );
114 SpuRenderCreateAndLoadScale( p_spu );
120 * Initialise the subpicture unit
122 * \param p_spu the subpicture unit object
124 int spu_Init( spu_t *p_spu )
128 /* If the user requested a sub margin, we force the position. */
129 var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
130 var_Get( p_spu, "sub-margin", &val );
131 p_spu->i_margin = val.i_int;
133 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
134 var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
136 spu_ParseChain( p_spu );
141 int spu_ParseChain( spu_t *p_spu )
143 char *psz_parser = var_GetString( p_spu, "sub-filter" );
144 if( filter_chain_AppendFromString( p_spu->p_chain, psz_parser ) < 0 )
155 * Destroy the subpicture unit
157 * \param p_this the parent object which destroys the subpicture unit
159 void spu_Destroy( spu_t *p_spu )
163 /* Destroy all remaining subpictures */
164 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
166 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
168 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
173 FilterRelease( p_spu->p_blend );
176 FilterRelease( p_spu->p_text );
178 if( p_spu->p_scale_yuvp )
179 FilterRelease( p_spu->p_scale_yuvp );
182 FilterRelease( p_spu->p_scale );
184 filter_chain_Delete( p_spu->p_chain );
186 vlc_mutex_destroy( &p_spu->subpicture_lock );
187 vlc_object_release( p_spu );
191 * Attach/Detach the SPU from any input
193 * \param p_this the object in which to destroy the subpicture unit
194 * \param b_attach to select attach or detach
196 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
198 vlc_object_t *p_input;
200 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
201 if( !p_input ) return;
205 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
206 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
207 vlc_object_release( p_input );
211 /* Delete callback */
212 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
213 vlc_object_release( p_input );
219 static void RegionPictureRelease( picture_t *p_picture )
221 if( --p_picture->i_refcount > 0 )
224 assert( p_picture->i_refcount == 0 );
225 free( p_picture->p_q );
226 free( p_picture->p_data_orig );
227 free( p_picture->p_sys );
231 * Create a subpicture region
233 * \param p_this vlc_object_t
234 * \param p_fmt the format that this subpicture region should have
236 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
237 video_format_t *p_fmt )
239 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
243 /* FIXME is that *really* wanted? */
244 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
245 p_fmt->p_palette = calloc( 1, sizeof(video_palette_t) );
247 p_fmt->p_palette = NULL; /* XXX and that above all? */
249 p_region->fmt = *p_fmt;
250 p_region->i_alpha = 0xff;
251 p_region->p_next = NULL;
252 p_region->p_cache = NULL;
253 p_region->psz_text = NULL;
254 p_region->p_style = NULL;
256 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') )
259 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
260 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
262 if( !p_region->picture.i_planes )
264 free( p_fmt->p_palette );
269 p_region->picture.i_refcount = 1;
270 p_region->picture.pf_release = RegionPictureRelease;
276 * Destroy a subpicture region
278 * \param p_this vlc_object_t
279 * \param p_region the subpicture region to destroy
281 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
286 picture_Release( &p_region->picture );
288 free( p_region->fmt.p_palette );
289 if( p_region->p_cache )
290 __spu_DestroyRegion( p_this, p_region->p_cache );
292 free( p_region->psz_text );
293 free( p_region->psz_html );
294 //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
299 * Display a subpicture
301 * Remove the reservation flag of a subpicture, which will cause it to be
303 * \param p_spu the subpicture unit object
304 * \param p_subpic the subpicture to display
306 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
308 /* Check if status is valid */
309 if( p_subpic->i_status != RESERVED_SUBPICTURE )
311 msg_Err( p_spu, "subpicture %p has invalid status #%d",
312 p_subpic, p_subpic->i_status );
315 /* Remove reservation flag */
316 p_subpic->i_status = READY_SUBPICTURE;
318 if( p_subpic->i_channel == DEFAULT_CHAN )
320 p_subpic->i_channel = 0xFFFF;
321 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
322 p_subpic->i_channel = DEFAULT_CHAN;
327 * Allocate a subpicture in the spu heap.
329 * This function create a reserved subpicture in the spu heap.
330 * A null pointer is returned if the function fails. This method provides an
331 * already allocated zone of memory in the spu data fields. It needs locking
332 * since several pictures can be created by several producers threads.
333 * \param p_spu the subpicture unit in which to create the subpicture
334 * \return NULL on error, a reserved subpicture otherwise
336 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
338 int i_subpic; /* subpicture index */
339 subpicture_t * p_subpic = NULL; /* first free subpicture */
342 vlc_mutex_lock( &p_spu->subpicture_lock );
345 * Look for an empty place
348 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
350 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
352 /* Subpicture is empty and ready for allocation */
353 p_subpic = &p_spu->p_subpicture[i_subpic];
354 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
359 /* If no free subpicture could be found */
360 if( p_subpic == NULL )
362 msg_Err( p_spu, "subpicture heap is full" );
363 vlc_mutex_unlock( &p_spu->subpicture_lock );
367 /* Copy subpicture information, set some default values */
368 memset( p_subpic, 0, sizeof(subpicture_t) );
369 p_subpic->i_status = RESERVED_SUBPICTURE;
370 p_subpic->b_absolute = true;
371 p_subpic->b_fade = false;
372 p_subpic->b_subtitle = false;
373 p_subpic->i_alpha = 0xFF;
374 p_subpic->p_region = NULL;
375 p_subpic->pf_render = NULL;
376 p_subpic->pf_destroy = NULL;
377 p_subpic->p_sys = NULL;
378 vlc_mutex_unlock( &p_spu->subpicture_lock );
380 p_subpic->pf_create_region = __spu_CreateRegion;
381 p_subpic->pf_destroy_region = __spu_DestroyRegion;
387 * Remove a subpicture from the heap
389 * This function frees a previously reserved subpicture.
390 * It is meant to be used when the construction of a picture aborted.
391 * This function does not need locking since reserved subpictures are ignored
394 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
397 vlc_mutex_lock( &p_spu->subpicture_lock );
399 /* There can be race conditions so we need to check the status */
400 if( p_subpic->i_status == FREE_SUBPICTURE )
402 vlc_mutex_unlock( &p_spu->subpicture_lock );
406 /* Check if status is valid */
407 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
408 && ( p_subpic->i_status != READY_SUBPICTURE ) )
410 msg_Err( p_spu, "subpicture %p has invalid status %d",
411 p_subpic, p_subpic->i_status );
414 while( p_subpic->p_region )
416 subpicture_region_t *p_region = p_subpic->p_region;
417 p_subpic->p_region = p_region->p_next;
418 spu_DestroyRegion( p_spu, p_region );
421 if( p_subpic->pf_destroy )
423 p_subpic->pf_destroy( p_subpic );
426 p_subpic->i_status = FREE_SUBPICTURE;
428 vlc_mutex_unlock( &p_spu->subpicture_lock );
431 /*****************************************************************************
432 * spu_RenderSubpictures: render a subpicture list
433 *****************************************************************************
434 * This function renders all sub picture units in the list.
435 *****************************************************************************/
436 static void FilterRelease( filter_t *p_filter )
438 if( p_filter->p_module )
439 module_Unneed( p_filter, p_filter->p_module );
441 vlc_object_detach( p_filter );
442 vlc_object_release( p_filter );
445 static void SpuRenderCreateBlend( spu_t *p_spu, vlc_fourcc_t i_chroma, int i_aspect )
449 assert( !p_spu->p_blend );
452 p_blend = vlc_custom_create( p_spu, sizeof(filter_t),
453 VLC_OBJECT_GENERIC, "blend" );
457 es_format_Init( &p_blend->fmt_in, VIDEO_ES, 0 );
459 es_format_Init( &p_blend->fmt_out, VIDEO_ES, 0 );
460 p_blend->fmt_out.video.i_x_offset = 0;
461 p_blend->fmt_out.video.i_y_offset = 0;
462 p_blend->fmt_out.video.i_chroma = i_chroma;
463 p_blend->fmt_out.video.i_aspect = i_aspect;
465 /* The blend module will be loaded when needed with the real
467 p_blend->p_module = NULL;
470 vlc_object_attach( p_blend, p_spu );
472 static void SpuRenderUpdateBlend( spu_t *p_spu, int i_out_width, int i_out_height, const video_format_t *p_in_fmt )
474 filter_t *p_blend = p_spu->p_blend;
479 if( p_blend->p_module && p_blend->fmt_in.video.i_chroma != p_in_fmt->i_chroma )
481 /* The chroma is not the same, we need to reload the blend module
482 * XXX to match the old behaviour just test !p_blend->fmt_in.video.i_chroma */
483 module_Unneed( p_blend, p_blend->p_module );
484 p_blend->p_module = NULL;
488 p_blend->fmt_in.video = *p_in_fmt;
491 p_blend->fmt_out.video.i_width =
492 p_blend->fmt_out.video.i_visible_width = i_out_width;
493 p_blend->fmt_out.video.i_height =
494 p_blend->fmt_out.video.i_visible_height = i_out_height;
497 if( !p_blend->p_module )
498 p_blend->p_module = module_Need( p_blend, "video blending", 0, 0 );
500 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
504 assert( !p_spu->p_text );
507 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
508 VLC_OBJECT_GENERIC, "spu text" );
512 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
514 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
515 p_text->fmt_out.video.i_width =
516 p_text->fmt_out.video.i_visible_width = 32;
517 p_text->fmt_out.video.i_height =
518 p_text->fmt_out.video.i_visible_height = 32;
520 p_text->pf_sub_buffer_new = spu_new_buffer;
521 p_text->pf_sub_buffer_del = spu_del_buffer;
523 vlc_object_attach( p_text, p_spu );
525 /* FIXME TOCHECK shouldn't module_Need( , , psz_modulename, false ) do the
526 * same than these 2 calls ? */
527 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
528 if( psz_modulename && *psz_modulename )
530 p_text->p_module = module_Need( p_text, "text renderer",
531 psz_modulename, true );
533 free( psz_modulename );
535 if( !p_text->p_module )
536 p_text->p_module = module_Need( p_text, "text renderer", NULL, false );
539 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj, vlc_fourcc_t i_chroma )
543 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
544 VLC_OBJECT_GENERIC, "scale" );
548 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
549 p_scale->fmt_in.video.i_chroma = i_chroma;
550 p_scale->fmt_in.video.i_width =
551 p_scale->fmt_in.video.i_height = 32;
553 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
554 p_scale->fmt_out.video.i_chroma = i_chroma;
555 p_scale->fmt_out.video.i_width =
556 p_scale->fmt_out.video.i_height = 16;
558 p_scale->pf_vout_buffer_new = spu_new_video_buffer;
559 p_scale->pf_vout_buffer_del = spu_del_video_buffer;
561 vlc_object_attach( p_scale, p_obj );
562 p_scale->p_module = module_Need( p_scale, "video filter2", 0, 0 );
566 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
568 /* FIXME: We'll also be using it for YUVA and RGBA blending ... */
570 assert( !p_spu->p_scale );
571 assert( !p_spu->p_scale_yuvp );
572 p_spu->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu), VLC_FOURCC('Y','U','V','A') );
573 p_spu->p_scale_yuvp = p_spu->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu), VLC_FOURCC('Y','U','V','P') );
576 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
577 subpicture_t *p_subpic, subpicture_region_t *p_region, int i_min_scale_ratio )
579 assert( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') );
581 if( !p_spu->p_text || !p_spu->p_text->p_module )
584 /* Setup 3 variables which can be used to render
585 * time-dependent text (and effects). The first indicates
586 * the total amount of time the text will be on screen,
587 * the second the amount of time it has already been on
588 * screen (can be a negative value as text is layed out
589 * before it is rendered) and the third is a feedback
590 * variable from the renderer - if the renderer sets it
591 * then this particular text is time-dependent, eg. the
592 * visual progress bar inside the text in karaoke and the
593 * text needs to be rendered multiple times in order for
594 * the effect to work - we therefore need to return the
595 * region to its original state at the end of the loop,
596 * instead of leaving it in YUVA or YUVP.
597 * Any renderer which is unaware of how to render
598 * time-dependent text can happily ignore the variables
599 * and render the text the same as usual - it should at
600 * least show up on screen, but the effect won't change
601 * the text over time.
604 /* FIXME why these variables are recreated every time and not
605 * when text renderer module was created ? */
606 var_Create( p_spu->p_text, "spu-duration", VLC_VAR_TIME );
607 var_Create( p_spu->p_text, "spu-elapsed", VLC_VAR_TIME );
608 var_Create( p_spu->p_text, "text-rerender", VLC_VAR_BOOL );
609 var_Create( p_spu->p_text, "scale", VLC_VAR_INTEGER );
611 var_SetTime( p_spu->p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
612 var_SetTime( p_spu->p_text, "spu-elapsed", mdate() - p_subpic->i_start );
613 var_SetBool( p_spu->p_text, "text-rerender", false );
614 var_SetInteger( p_spu->p_text, "scale", i_min_scale_ratio );
616 if( p_spu->p_text->pf_render_html && p_region->psz_html )
618 p_spu->p_text->pf_render_html( p_spu->p_text,
619 p_region, p_region );
621 else if( p_spu->p_text->pf_render_text )
623 p_spu->p_text->pf_render_text( p_spu->p_text,
624 p_region, p_region );
626 *pb_rerender_text = var_GetBool( p_spu->p_text, "text-rerender" );
628 var_Destroy( p_spu->p_text, "spu-duration" );
629 var_Destroy( p_spu->p_text, "spu-elapsed" );
630 var_Destroy( p_spu->p_text, "text-rerender" );
631 var_Destroy( p_spu->p_text, "scale" );
634 p_region->i_align |= SUBPICTURE_RENDERED;
638 * A few scale functions helpers.
646 static spu_scale_t spu_scale_create( int w, int h )
648 spu_scale_t s = { .w = w, .h = h };
655 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
657 return spu_scale_create( wn * SCALE_UNIT / wd,
658 hn * SCALE_UNIT / hd );
660 static int spu_scale_w( int v, const spu_scale_t s )
662 return v * s.w / SCALE_UNIT;
664 static int spu_scale_h( int v, const spu_scale_t s )
666 return v * s.h / SCALE_UNIT;
668 static int spu_invscale_h( int v, const spu_scale_t s )
670 return v * SCALE_UNIT / s.h;
676 static void SpuRegionPlace( int *pi_x, int *pi_y,
677 const subpicture_t *p_subpic,
678 const subpicture_region_t *p_region,
681 const int i_delta_x = p_region->i_x;
682 const int i_delta_y = p_region->i_y;
685 assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
686 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
690 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
692 i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
696 i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
699 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
703 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
705 i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
709 i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
712 if( p_subpic->b_absolute )
718 /* Margin shifts all subpictures */
719 if( i_margin_y != 0 )
722 /* Clamp offset to not go out of the screen (when possible) */
723 const int i_error_x = (i_x + p_region->fmt.i_width) - p_subpic->i_original_picture_width;
729 const int i_error_y = (i_y + p_region->fmt.i_height) - p_subpic->i_original_picture_height;
740 * This function computes the current alpha value for a given region.
742 static int SpuRegionAlpha( subpicture_t *p_subpic, subpicture_region_t *p_region )
744 /* Compute alpha blend value */
745 int i_fade_alpha = 255;
746 if( p_subpic->b_fade )
748 mtime_t i_fade_start = ( p_subpic->i_stop +
749 p_subpic->i_start ) / 2;
750 mtime_t i_now = mdate();
752 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
754 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
755 ( p_subpic->i_stop - i_fade_start );
758 return i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
761 static void SpuRenderRegion( spu_t *p_spu,
762 picture_t *p_pic_dst,
763 subpicture_t *p_subpic, subpicture_region_t *p_region,
764 const spu_scale_t scale_size,
765 const video_format_t *p_fmt )
767 video_format_t fmt_original;
768 bool b_rerender_text;
769 bool b_restore_format = false;
774 vlc_assert_locked( &p_spu->subpicture_lock );
776 fmt_original = p_region->fmt;
777 b_rerender_text = false;
778 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
780 const int i_min_scale_ratio = __MIN( scale_size.w, scale_size.h );
781 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region, i_min_scale_ratio );
782 b_restore_format = b_rerender_text;
784 /* Check if the rendering has failed ... */
785 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
789 /* Force palette if requested
790 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
791 * instead of only the right one (being the dvd spu).
793 const bool b_using_palette = p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','P');
794 const bool b_force_palette = b_using_palette && p_spu->b_force_palette;
795 const bool b_force_crop = b_force_palette && p_spu->b_force_crop;
798 /* Compute the margin which is expressed in destination pixel unit
799 * The margin is applied only to subtitle and when no forced crop is
800 * requested (dvd menu) */
802 if( !b_force_crop && p_subpic->b_subtitle )
803 i_margin_y = spu_invscale_h( p_spu->i_margin, scale_size );
806 * We compute the position in the rendered size */
807 SpuRegionPlace( &i_x_offset, &i_y_offset,
808 p_subpic, p_region, i_margin_y );
810 /* TODO save this position for subtitle overlap support */
812 /* Fix the position for the current scale_size */
813 i_x_offset = spu_scale_w( i_x_offset, scale_size );
814 i_y_offset = spu_scale_h( i_y_offset, scale_size );
816 if( b_force_palette )
818 /* It looks so wrong I won't comment
819 * p_palette->palette is [256][4] with a int i_entries
820 * p_spu->palette is [4][4]
822 p_region->fmt.p_palette->i_entries = 4;
823 memcpy( p_region->fmt.p_palette->palette, p_spu->palette, 4*sizeof(uint32_t) );
826 if( b_using_palette )
827 p_scale = p_spu->p_scale_yuvp;
829 p_scale = p_spu->p_scale;
831 /* Scale from rendered size to destination size */
833 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_force_palette ) )
835 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
836 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
838 /* TODO when b_using_palette is true, we should first convert it to YUVA to allow
839 * a proper rescaling */
841 /* Destroy if cache is unusable */
842 if( p_region->p_cache )
844 if( p_region->p_cache->fmt.i_width != i_dst_width ||
845 p_region->p_cache->fmt.i_height != i_dst_height ||
848 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
850 p_region->p_cache = NULL;
854 /* Scale if needed into cache */
855 if( !p_region->p_cache )
859 p_scale->fmt_in.video = p_region->fmt;
860 p_scale->fmt_out.video = p_region->fmt;
862 if( p_scale->fmt_out.video.p_palette )
863 *p_scale->fmt_out.video.p_palette =
864 *p_region->fmt.p_palette;
866 p_scale->fmt_out.video.i_width = i_dst_width;
867 p_scale->fmt_out.video.i_height = i_dst_height;
869 p_scale->fmt_out.video.i_visible_width =
870 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
871 p_scale->fmt_out.video.i_visible_height =
872 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
875 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
876 &p_scale->fmt_out.video );
879 if( p_scale->p_module )
881 picture_Yield( &p_region->picture );
882 p_pic = p_scale->pf_video_filter( p_scale, &p_region->picture );
886 picture_Copy( &p_region->p_cache->picture, p_pic );
887 picture_Release( p_pic );
889 p_region->p_cache->fmt = p_scale->fmt_out.video;
890 /* i_x/i_y of cached region should NOT be used. I set them to
891 * an invalid value to catch it (assert) */
892 p_region->p_cache->i_x = INT_MAX;
893 p_region->p_cache->i_y = INT_MAX;
894 p_region->p_cache->i_align = p_region->i_align;
895 p_region->p_cache->i_alpha = p_region->i_alpha;
899 msg_Err( p_spu, "scaling failed (module not loaded)" );
900 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
902 p_region->p_cache = NULL;
906 /* And use the scaled picture */
907 if( p_region->p_cache )
909 p_region = p_region->p_cache;
910 fmt_original = p_region->fmt;
914 /* Force cropping if requested */
917 video_format_t *p_fmt = &p_region->fmt;
918 int i_crop_x = spu_scale_w( p_spu->i_crop_x, scale_size );
919 int i_crop_y = spu_scale_h( p_spu->i_crop_y, scale_size );
920 int i_crop_width = spu_scale_w( p_spu->i_crop_width, scale_size );
921 int i_crop_height= spu_scale_h( p_spu->i_crop_height,scale_size );
923 /* Find the intersection */
924 if( i_crop_x + i_crop_width <= i_x_offset ||
925 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
926 i_crop_y + i_crop_height <= i_y_offset ||
927 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
929 /* No intersection */
930 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
934 int i_x, i_y, i_x_end, i_y_end;
935 i_x = __MAX( i_crop_x, i_x_offset );
936 i_y = __MAX( i_crop_y, i_y_offset );
937 i_x_end = __MIN( i_crop_x + i_crop_width,
938 i_x_offset + (int)p_fmt->i_visible_width );
939 i_y_end = __MIN( i_crop_y + i_crop_height,
940 i_y_offset + (int)p_fmt->i_visible_height );
942 p_fmt->i_x_offset = i_x - i_x_offset;
943 p_fmt->i_y_offset = i_y - i_y_offset;
944 p_fmt->i_visible_width = i_x_end - i_x;
945 p_fmt->i_visible_height = i_y_end - i_y;
947 i_x_offset = __MAX( i_x, 0 );
948 i_y_offset = __MAX( i_y, 0 );
950 b_restore_format = true;
953 /* Update the blender */
954 SpuRenderUpdateBlend( p_spu, p_fmt->i_width, p_fmt->i_height, &p_region->fmt );
956 if( p_spu->p_blend->p_module )
958 const int i_alpha = SpuRegionAlpha( p_subpic, p_region );
960 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
961 &p_region->picture, i_x_offset, i_y_offset, i_alpha );
965 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
966 (char *)&p_spu->p_blend->fmt_out.video.i_chroma,
967 (char *)&p_spu->p_blend->fmt_out.video.i_chroma );
971 if( b_rerender_text )
973 /* Some forms of subtitles need to be re-rendered more than
974 * once, eg. karaoke. We therefore restore the region to its
975 * pre-rendered state, so the next time through everything is
978 p_region->picture.pf_release( &p_region->picture );
979 memset( &p_region->picture, 0, sizeof( picture_t ) );
980 p_region->i_align &= ~SUBPICTURE_RENDERED;
982 if( b_restore_format )
983 p_region->fmt = fmt_original;
986 void spu_RenderSubpictures( spu_t *p_spu,
987 picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
988 subpicture_t *p_subpic_list,
989 const video_format_t *p_fmt_src )
991 const int i_source_video_width = p_fmt_src->i_width;
992 const int i_source_video_height = p_fmt_src->i_height;
993 const mtime_t i_current_date = mdate();
995 subpicture_t *p_subpic;
998 vlc_mutex_lock( &p_spu->subpicture_lock );
1000 /* Be sure we have at least 1 picture to process */
1001 if( !p_subpic_list || p_subpic_list->i_status == FREE_SUBPICTURE )
1003 vlc_mutex_unlock( &p_spu->subpicture_lock );
1008 for( p_subpic = p_subpic_list;
1009 p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE; /* Check again status (as we where unlocked) */
1010 p_subpic = p_subpic->p_next )
1013 if( p_subpic->pf_pre_render )
1014 p_subpic->pf_pre_render( p_spu, p_subpic, p_fmt_dst );
1016 if( p_subpic->pf_update_regions )
1018 video_format_t fmt_org = *p_fmt_dst;
1020 fmt_org.i_visible_width = i_source_video_width;
1022 fmt_org.i_visible_height = i_source_video_height;
1024 p_subpic->pf_update_regions( p_spu, p_subpic, &fmt_org, i_current_date );
1028 /* Create the blending module */
1029 if( !p_spu->p_blend )
1030 SpuRenderCreateBlend( p_spu, p_fmt_dst->i_chroma, p_fmt_dst->i_aspect );
1033 for( p_subpic = p_subpic_list; ; p_subpic = p_subpic->p_next )
1035 subpicture_region_t *p_region;
1037 if( !p_subpic || p_subpic->i_status == FREE_SUBPICTURE )
1040 if( !p_subpic->p_region )
1043 /* FIXME when possible use a better rendering size than source size
1044 * (max of display size and source size for example) FIXME */
1045 int i_render_width = p_subpic->i_original_picture_width;
1046 int i_render_height = p_subpic->i_original_picture_height;
1047 if( !i_render_width || !i_render_height )
1049 if( i_render_width != 0 || i_render_height != 0 )
1050 msg_Err( p_spu, "unsupported original picture size %dx%d",
1051 i_render_width, i_render_height );
1053 p_subpic->i_original_picture_width = i_render_width = i_source_video_width;
1054 p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
1059 p_spu->p_text->fmt_out.video.i_width =
1060 p_spu->p_text->fmt_out.video.i_visible_width = i_render_width;
1062 p_spu->p_text->fmt_out.video.i_height =
1063 p_spu->p_text->fmt_out.video.i_visible_height = i_render_height;
1066 /* Compute scaling from picture to source size */
1067 spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
1068 i_source_video_height, i_render_height );
1070 /* Update scaling from source size to display size(p_fmt_dst) */
1071 scale.w = scale.w * p_fmt_dst->i_width / i_source_video_width;
1072 scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
1074 /* Set default subpicture aspect ratio
1075 * FIXME if we only handle 1 aspect ratio per picture, why is it set per
1077 p_region = p_subpic->p_region;
1078 if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
1080 if( p_region->fmt.i_aspect != 0 )
1082 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
1083 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
1087 p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
1088 p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
1092 /* Take care of the aspect ratio */
1093 if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
1094 p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
1096 /* FIXME FIXME what about region->i_x/i_y ? */
1098 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
1099 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
1102 /* Render all regions */
1103 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
1105 /* Check scale validity */
1106 if( scale.w <= 0 || scale.h <= 0 )
1110 SpuRenderRegion( p_spu, p_pic_dst, p_subpic, p_region,
1115 vlc_mutex_unlock( &p_spu->subpicture_lock );
1118 /*****************************************************************************
1119 * spu_SortSubpictures: find the subpictures to display
1120 *****************************************************************************
1121 * This function parses all subpictures and decides which ones need to be
1122 * displayed. This operation does not need lock, since only READY_SUBPICTURE
1123 * are handled. If no picture has been selected, display_date will depend on
1125 * We also check for ephemer DVD subpictures (subpictures that have
1126 * to be removed if a newer one is available), which makes it a lot
1127 * more difficult to guess if a subpicture has to be rendered or not.
1128 *****************************************************************************/
1129 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1130 bool b_paused, bool b_subtitle_only )
1133 subpicture_t *p_subpic = NULL;
1135 /* Run subpicture filters */
1136 filter_chain_SubFilter( p_spu->p_chain, display_date );
1138 /* We get an easily parsable chained list of subpictures which
1139 * ends with NULL since p_subpic was initialized to NULL. */
1140 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
1142 subpicture_t *p_ephemer = NULL;
1143 mtime_t ephemer_date = 0;
1146 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1148 subpicture_t *p_current = &p_spu->p_subpicture[i_index];
1150 if( p_current->i_channel != i_channel ||
1151 p_current->i_status != READY_SUBPICTURE ||
1152 ( b_subtitle_only && !p_current->b_subtitle ) )
1157 display_date < p_current->i_start )
1159 /* Too early, come back next monday */
1163 if( p_current->i_start > ephemer_date )
1164 ephemer_date = p_current->i_start;
1166 if( display_date > p_current->i_stop &&
1167 ( !p_current->b_ephemer || p_current->i_stop > p_current->i_start ) &&
1168 !( p_current->b_subtitle && b_paused ) ) /* XXX Assume that subtitle are pausable */
1170 /* Too late, destroy the subpic */
1171 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
1175 /* If this is an ephemer subpic, add it to our list */
1176 if( p_current->b_ephemer )
1178 p_current->p_next = p_ephemer;
1179 p_ephemer = p_current;
1184 p_current->p_next = p_subpic;
1185 p_subpic = p_current;
1188 /* If we found ephemer subpictures, check if they have to be
1189 * displayed or destroyed */
1190 while( p_ephemer != NULL )
1192 subpicture_t *p_tmp = p_ephemer;
1193 p_ephemer = p_ephemer->p_next;
1195 if( p_tmp->i_start < ephemer_date )
1197 /* Ephemer subpicture has lived too long */
1198 spu_DestroySubpicture( p_spu, p_tmp );
1202 /* Ephemer subpicture can still live a bit */
1203 p_tmp->p_next = p_subpic;
1212 /*****************************************************************************
1213 * SpuClearChannel: clear an spu channel
1214 *****************************************************************************
1215 * This function destroys the subpictures which belong to the spu channel
1216 * corresponding to i_channel_id.
1217 *****************************************************************************/
1218 static void SpuClearChannel( spu_t *p_spu, int i_channel, bool b_locked )
1220 int i_subpic; /* subpicture index */
1221 subpicture_t *p_subpic = NULL; /* first free subpicture */
1224 vlc_mutex_lock( &p_spu->subpicture_lock );
1226 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1228 p_subpic = &p_spu->p_subpicture[i_subpic];
1229 if( p_subpic->i_status == FREE_SUBPICTURE
1230 || ( p_subpic->i_status != RESERVED_SUBPICTURE
1231 && p_subpic->i_status != READY_SUBPICTURE ) )
1236 if( p_subpic->i_channel == i_channel )
1238 while( p_subpic->p_region )
1240 subpicture_region_t *p_region = p_subpic->p_region;
1241 p_subpic->p_region = p_region->p_next;
1242 spu_DestroyRegion( p_spu, p_region );
1245 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1246 p_subpic->i_status = FREE_SUBPICTURE;
1251 vlc_mutex_unlock( &p_spu->subpicture_lock );
1254 /*****************************************************************************
1255 * spu_ControlDefault: default methods for the subpicture unit control.
1256 *****************************************************************************/
1257 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1263 case SPU_CHANNEL_REGISTER:
1264 pi = (int *)va_arg( args, int * );
1265 vlc_mutex_lock( &p_spu->subpicture_lock );
1267 *pi = p_spu->i_channel++;
1268 vlc_mutex_unlock( &p_spu->subpicture_lock );
1271 case SPU_CHANNEL_CLEAR:
1272 i = (int)va_arg( args, int );
1273 SpuClearChannel( p_spu, i, false );
1277 msg_Dbg( p_spu, "control query not supported" );
1278 return VLC_EGENERIC;
1284 /*****************************************************************************
1285 * Object variables callbacks
1286 *****************************************************************************/
1288 /*****************************************************************************
1289 * UpdateSPU: update subpicture settings
1290 *****************************************************************************
1291 * This function is called from CropCallback and at initialization time, to
1292 * retrieve crop information from the input.
1293 *****************************************************************************/
1294 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1298 vlc_mutex_lock( &p_spu->subpicture_lock );
1300 p_spu->b_force_palette = false;
1301 p_spu->b_force_crop = false;
1303 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1305 vlc_mutex_unlock( &p_spu->subpicture_lock );
1309 p_spu->b_force_crop = true;
1310 var_Get( p_object, "x-start", &val );
1311 p_spu->i_crop_x = val.i_int;
1312 var_Get( p_object, "y-start", &val );
1313 p_spu->i_crop_y = val.i_int;
1314 var_Get( p_object, "x-end", &val );
1315 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1316 var_Get( p_object, "y-end", &val );
1317 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1319 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1321 memcpy( p_spu->palette, val.p_address, 16 );
1322 p_spu->b_force_palette = true;
1324 vlc_mutex_unlock( &p_spu->subpicture_lock );
1326 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1327 p_spu->i_crop_x, p_spu->i_crop_y,
1328 p_spu->i_crop_width, p_spu->i_crop_height,
1329 p_spu->b_force_palette );
1332 /*****************************************************************************
1333 * CropCallback: called when the highlight properties are changed
1334 *****************************************************************************
1335 * This callback is called from the input thread when we need cropping
1336 *****************************************************************************/
1337 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1338 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1340 (void)psz_var; (void)oldval; (void)newval;
1341 UpdateSPU( (spu_t *)p_data, p_object );
1345 /*****************************************************************************
1346 * Buffers allocation callbacks for the filters
1347 *****************************************************************************/
1348 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1350 filter_owner_sys_t *p_sys = p_filter->p_owner;
1351 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1352 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1353 return p_subpicture;
1356 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1358 filter_owner_sys_t *p_sys = p_filter->p_owner;
1359 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1362 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1364 subpicture_t *p_subpic = calloc( 1, sizeof(subpicture_t) );
1368 p_subpic->b_absolute = true;
1369 p_subpic->i_alpha = 0xFF;
1371 p_subpic->pf_create_region = __spu_CreateRegion;
1372 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1374 VLC_UNUSED(p_filter);
1378 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1380 while( p_subpic->p_region )
1382 subpicture_region_t *p_region = p_subpic->p_region;
1383 p_subpic->p_region = p_region->p_next;
1384 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1390 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1392 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1394 VLC_UNUSED(p_filter);
1395 return picture_New( p_fmt->i_chroma,
1396 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
1399 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1401 VLC_UNUSED(p_filter);
1402 picture_Release( p_picture );
1405 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1406 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1408 spu_t *p_spu = p_data;
1410 VLC_UNUSED(p_object); VLC_UNUSED(oldval);
1411 VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1413 vlc_mutex_lock( &p_spu->subpicture_lock );
1414 filter_chain_Reset( p_spu->p_chain, NULL, NULL );
1415 spu_ParseChain( p_spu );
1416 vlc_mutex_unlock( &p_spu->subpicture_lock );
1420 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1422 spu_t *p_spu = p_data;
1424 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1426 return VLC_EGENERIC;
1428 p_filter->pf_sub_buffer_new = sub_new_buffer;
1429 p_filter->pf_sub_buffer_del = sub_del_buffer;
1431 p_filter->p_owner = p_sys;
1432 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1433 p_sys->p_spu = p_spu;
1438 static void SubFilterAllocationClean( filter_t *p_filter )
1440 filter_owner_sys_t *p_sys = p_filter->p_owner;
1442 SpuClearChannel( p_sys->p_spu, p_sys->i_channel, true );
1443 free( p_filter->p_owner );