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 #define VLC_FOURCC_YUVP VLC_FOURCC('Y','U','V','P')
47 #define VLC_FOURCC_YUVA VLC_FOURCC('Y','U','V','A')
48 #define VLC_FOURCC_RGBA VLC_FOURCC('R','G','B','A')
49 #define VLC_FOURCC_TEXT VLC_FOURCC('T','E','X','T')
53 subpicture_t *p_subpicture;
59 spu_heap_entry_t p_entry[VOUT_MAX_SUBPICTURES];
65 vlc_mutex_t lock; /* lock to protect all followings fields */
69 int i_channel; /**< number of subpicture channels registered */
70 int64_t i_subpicture_order; /**< number of created subpicture since spu creation */
72 filter_t *p_blend; /**< alpha blending module */
73 filter_t *p_text; /**< text renderer module */
74 filter_t *p_scale_yuvp; /**< scaling module for YUVP */
75 filter_t *p_scale; /**< scaling module (all but YUVP) */
76 bool b_force_crop; /**< force cropping of subpicture */
77 int i_crop_x, i_crop_y, i_crop_width, i_crop_height; /**< cropping */
79 int i_margin; /**< force position of a subpicture */
80 bool b_force_palette; /**< force palette of subpicture */
81 uint8_t palette[4][4]; /**< forced palette */
83 /* Supciture filters */
84 filter_chain_t *p_chain;
88 struct subpicture_region_private_t
94 /* Subpicture rendered flag
95 * FIXME ? it could be moved to private ? */
96 #define SUBPICTURE_RENDERED (0x1000)
97 #ifdef SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
98 # error SUBPICTURE_RENDERED too low
101 static void SpuRegionPrivateDestroy( subpicture_region_private_t *p_private );
103 static void UpdateSPU ( spu_t *, vlc_object_t * );
104 static int CropCallback( vlc_object_t *, char const *,
105 vlc_value_t, vlc_value_t, void * );
107 static int SpuControl( spu_t *, int, va_list );
109 static void SpuClearChannel( spu_t *p_spu, int i_channel, bool b_locked );
111 /* Buffer allocation for SPU filter (blend, scale, ...) */
112 static subpicture_t *spu_new_buffer( filter_t * );
113 static void spu_del_buffer( filter_t *, subpicture_t * );
114 static picture_t *spu_new_video_buffer( filter_t * );
115 static void spu_del_video_buffer( filter_t *, picture_t * );
117 static int spu_ParseChain( spu_t * );
119 /* Buffer aloccation fir SUB filter */
120 static int SubFilterCallback( vlc_object_t *, char const *,
121 vlc_value_t, vlc_value_t, void * );
123 static int SubFilterAllocationInit( filter_t *, void * );
124 static void SubFilterAllocationClean( filter_t * );
126 #define SCALE_UNIT (1000)
129 subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
131 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
135 p_region->fmt = *p_fmt;
136 p_region->fmt.p_palette = NULL;
137 if( p_fmt->i_chroma == VLC_FOURCC_YUVP )
139 p_region->fmt.p_palette = calloc( 1, sizeof(*p_region->fmt.p_palette) );
140 if( p_fmt->p_palette )
141 *p_region->fmt.p_palette = *p_fmt->p_palette;
143 p_region->i_alpha = 0xff;
144 p_region->p_next = NULL;
145 p_region->p_private = NULL;
146 p_region->psz_text = NULL;
147 p_region->p_style = NULL;
148 p_region->p_picture = NULL;
150 if( p_fmt->i_chroma == VLC_FOURCC_TEXT )
153 p_region->p_picture = picture_New( p_fmt->i_chroma, p_fmt->i_width, p_fmt->i_height,
155 if( !p_region->p_picture )
157 free( p_fmt->p_palette );
166 void subpicture_region_Delete( subpicture_region_t *p_region )
171 if( p_region->p_private )
172 SpuRegionPrivateDestroy( p_region->p_private );
174 if( p_region->p_picture )
175 picture_Release( p_region->p_picture );
177 free( p_region->fmt.p_palette );
179 free( p_region->psz_text );
180 free( p_region->psz_html );
181 //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
186 void subpicture_region_ChainDelete( subpicture_region_t *p_head )
190 subpicture_region_t *p_next = p_head->p_next;
192 subpicture_region_Delete( p_head );
199 * This function create a new empty subpicture.
201 static subpicture_t *subpicture_New( void )
203 subpicture_t *p_subpic = calloc( 1, sizeof(*p_subpic) );
207 p_subpic->i_order = 0;
208 p_subpic->b_absolute = true;
209 p_subpic->b_fade = false;
210 p_subpic->b_subtitle = false;
211 p_subpic->i_alpha = 0xFF;
212 p_subpic->p_region = NULL;
213 p_subpic->pf_render = NULL;
214 p_subpic->pf_destroy = NULL;
215 p_subpic->p_sys = NULL;
220 static void subpicture_Delete( subpicture_t *p_subpic )
222 subpicture_region_ChainDelete( p_subpic->p_region );
223 p_subpic->p_region = NULL;
225 if( p_subpic->pf_destroy )
227 p_subpic->pf_destroy( p_subpic );
233 static void SpuHeapInit( spu_heap_t *p_heap )
235 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
237 spu_heap_entry_t *e = &p_heap->p_entry[i];
239 e->p_subpicture = NULL;
244 static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
246 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
248 spu_heap_entry_t *e = &p_heap->p_entry[i];
250 if( e->p_subpicture )
253 e->p_subpicture = p_subpic;
260 static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
262 spu_heap_entry_t *e = &p_heap->p_entry[i_index];
264 if( e->p_subpicture )
265 subpicture_Delete( e->p_subpicture );
267 e->p_subpicture = NULL;
270 static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
272 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
274 spu_heap_entry_t *e = &p_heap->p_entry[i];
276 if( e->p_subpicture != p_subpic )
279 SpuHeapDeleteAt( p_heap, i );
285 static void SpuHeapClean( spu_heap_t *p_heap )
287 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
289 spu_heap_entry_t *e = &p_heap->p_entry[i];
290 if( e->p_subpicture )
291 subpicture_Delete( e->p_subpicture );
294 static subpicture_region_private_t *SpuRegionPrivateCreate( video_format_t *p_fmt )
296 subpicture_region_private_t *p_private = malloc( sizeof(*p_private) );
301 p_private->fmt = *p_fmt;
302 if( p_fmt->p_palette )
304 p_private->fmt.p_palette = malloc( sizeof(*p_private->fmt.p_palette) );
305 if( p_private->fmt.p_palette )
306 *p_private->fmt.p_palette = *p_fmt->p_palette;
308 p_private->p_picture = NULL;
312 static void SpuRegionPrivateDestroy( subpicture_region_private_t *p_private )
314 if( p_private->p_picture )
315 picture_Release( p_private->p_picture );
316 free( p_private->fmt.p_palette );
321 static void SpuRenderCreateAndLoadText( spu_t *p_spu );
322 static void SpuRenderCreateAndLoadScale( spu_t *p_spu );
323 static void FilterRelease( filter_t *p_filter );
326 * Creates the subpicture unit
328 * \param p_this the parent object which creates the subpicture unit
330 spu_t *__spu_Create( vlc_object_t *p_this )
333 spu_private_t *p_sys;
335 p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
336 VLC_OBJECT_GENERIC, "subpicture" );
341 /* Initialize spu fields */
342 p_spu->pf_control = SpuControl;
343 p_spu->p = p_sys = (spu_private_t*)&p_spu[1];
345 /* Initialize private fields */
346 vlc_mutex_init( &p_sys->lock );
348 SpuHeapInit( &p_sys->heap );
350 p_sys->i_subpicture_order = 1;
352 p_sys->p_blend = NULL;
353 p_sys->p_text = NULL;
354 p_sys->p_scale = NULL;
355 p_sys->p_scale_yuvp = NULL;
357 /* Register the default subpicture channel */
358 p_sys->i_channel = 2;
360 vlc_object_attach( p_spu, p_this );
362 p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
363 SubFilterAllocationInit,
364 SubFilterAllocationClean,
367 /* Load text and scale module */
368 SpuRenderCreateAndLoadText( p_spu );
369 SpuRenderCreateAndLoadScale( p_spu );
375 * Initialise the subpicture unit
377 * \param p_spu the subpicture unit object
379 int spu_Init( spu_t *p_spu )
381 spu_private_t *p_sys = p_spu->p;
383 /* If the user requested a sub margin, we force the position. */
384 p_sys->i_margin = var_CreateGetInteger( p_spu, "sub-margin" );
386 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
387 var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
389 spu_ParseChain( p_spu );
394 int spu_ParseChain( spu_t *p_spu )
396 char *psz_parser = var_GetString( p_spu, "sub-filter" );
402 i_ret = filter_chain_AppendFromString( p_spu->p->p_chain, psz_parser );
410 * Destroy the subpicture unit
412 * \param p_this the parent object which destroys the subpicture unit
414 void spu_Destroy( spu_t *p_spu )
416 spu_private_t *p_sys = p_spu->p;
419 FilterRelease( p_sys->p_blend );
422 FilterRelease( p_sys->p_text );
424 if( p_sys->p_scale_yuvp )
425 FilterRelease( p_sys->p_scale_yuvp );
428 FilterRelease( p_sys->p_scale );
430 filter_chain_Delete( p_sys->p_chain );
432 /* Destroy all remaining subpictures */
433 SpuHeapClean( &p_sys->heap );
435 vlc_mutex_destroy( &p_sys->lock );
437 vlc_object_release( p_spu );
441 * Attach/Detach the SPU from any input
443 * \param p_this the object in which to destroy the subpicture unit
444 * \param b_attach to select attach or detach
446 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
448 vlc_object_t *p_input;
450 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
451 if( !p_input ) return;
455 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
456 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
457 vlc_object_release( p_input );
461 /* Delete callback */
462 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
463 vlc_object_release( p_input );
468 * Display a subpicture
470 * Remove the reservation flag of a subpicture, which will cause it to be
472 * \param p_spu the subpicture unit object
473 * \param p_subpic the subpicture to display
475 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
477 spu_private_t *p_sys = p_spu->p;
479 /* DEFAULT_CHAN always reset itself */
480 if( p_subpic->i_channel == DEFAULT_CHAN )
481 SpuClearChannel( p_spu, DEFAULT_CHAN, false );
483 /* p_private is for spu only and cannot be non NULL here */
484 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
485 assert( r->p_private == NULL );
488 vlc_mutex_lock( &p_sys->lock );
489 if( SpuHeapPush( &p_sys->heap, p_subpic ) )
491 vlc_mutex_unlock( &p_sys->lock );
492 msg_Err( p_spu, "subpicture heap full" );
493 subpicture_Delete( p_subpic );
496 vlc_mutex_unlock( &p_sys->lock );
500 * Allocate a subpicture in the spu heap.
502 * This function create a reserved subpicture in the spu heap.
503 * A null pointer is returned if the function fails. This method provides an
504 * already allocated zone of memory in the spu data fields. It needs locking
505 * since several pictures can be created by several producers threads.
506 * \param p_spu the subpicture unit in which to create the subpicture
507 * \return NULL on error, a reserved subpicture otherwise
509 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
513 return subpicture_New();
517 * Remove a subpicture from the heap
519 * This function frees a previously reserved subpicture.
520 * It is meant to be used when the construction of a picture aborted.
521 * This function does not need locking since reserved subpictures are ignored
524 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
528 subpicture_Delete( p_subpic );
531 static void FilterRelease( filter_t *p_filter )
533 if( p_filter->p_module )
534 module_unneed( p_filter, p_filter->p_module );
536 vlc_object_detach( p_filter );
537 vlc_object_release( p_filter );
540 static void SpuRenderCreateBlend( spu_t *p_spu, vlc_fourcc_t i_chroma, int i_aspect )
544 assert( !p_spu->p->p_blend );
547 p_blend = vlc_custom_create( p_spu, sizeof(filter_t),
548 VLC_OBJECT_GENERIC, "blend" );
552 es_format_Init( &p_blend->fmt_in, VIDEO_ES, 0 );
554 es_format_Init( &p_blend->fmt_out, VIDEO_ES, 0 );
555 p_blend->fmt_out.video.i_x_offset = 0;
556 p_blend->fmt_out.video.i_y_offset = 0;
557 p_blend->fmt_out.video.i_chroma = i_chroma;
558 p_blend->fmt_out.video.i_aspect = i_aspect;
560 /* The blend module will be loaded when needed with the real
562 p_blend->p_module = NULL;
565 vlc_object_attach( p_blend, p_spu );
567 static void SpuRenderUpdateBlend( spu_t *p_spu, int i_out_width, int i_out_height,
568 const video_format_t *p_in_fmt )
570 filter_t *p_blend = p_spu->p->p_blend;
575 if( p_blend->p_module && p_blend->fmt_in.video.i_chroma != p_in_fmt->i_chroma )
577 /* The chroma is not the same, we need to reload the blend module
578 * XXX to match the old behaviour just test !p_blend->fmt_in.video.i_chroma */
579 module_unneed( p_blend, p_blend->p_module );
580 p_blend->p_module = NULL;
584 p_blend->fmt_in.video = *p_in_fmt;
587 p_blend->fmt_out.video.i_width =
588 p_blend->fmt_out.video.i_visible_width = i_out_width;
589 p_blend->fmt_out.video.i_height =
590 p_blend->fmt_out.video.i_visible_height = i_out_height;
593 if( !p_blend->p_module )
594 p_blend->p_module = module_need( p_blend, "video blending", 0, 0 );
596 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
600 assert( !p_spu->p->p_text );
603 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
604 VLC_OBJECT_GENERIC, "spu text" );
608 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
610 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
611 p_text->fmt_out.video.i_width =
612 p_text->fmt_out.video.i_visible_width = 32;
613 p_text->fmt_out.video.i_height =
614 p_text->fmt_out.video.i_visible_height = 32;
616 p_text->pf_sub_buffer_new = spu_new_buffer;
617 p_text->pf_sub_buffer_del = spu_del_buffer;
619 vlc_object_attach( p_text, p_spu );
621 /* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
622 * same than these 2 calls ? */
623 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
624 if( psz_modulename && *psz_modulename )
626 p_text->p_module = module_need( p_text, "text renderer",
627 psz_modulename, true );
629 free( psz_modulename );
631 if( !p_text->p_module )
632 p_text->p_module = module_need( p_text, "text renderer", NULL, false );
634 /* Create a few variables used for enhanced text rendering */
635 var_Create( p_text, "spu-duration", VLC_VAR_TIME );
636 var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
637 var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
638 var_Create( p_text, "scale", VLC_VAR_INTEGER );
641 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
642 vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
647 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
648 VLC_OBJECT_GENERIC, "scale" );
652 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
653 p_scale->fmt_in.video.i_chroma = i_src_chroma;
654 p_scale->fmt_in.video.i_width =
655 p_scale->fmt_in.video.i_height = 32;
657 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
658 p_scale->fmt_out.video.i_chroma = i_dst_chroma;
659 p_scale->fmt_out.video.i_width =
660 p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
662 p_scale->pf_vout_buffer_new = spu_new_video_buffer;
663 p_scale->pf_vout_buffer_del = spu_del_video_buffer;
665 vlc_object_attach( p_scale, p_obj );
666 p_scale->p_module = module_need( p_scale, "video filter2", 0, 0 );
670 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
672 assert( !p_spu->p->p_scale );
673 assert( !p_spu->p->p_scale_yuvp );
674 /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
676 p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
677 VLC_FOURCC_YUVA, VLC_FOURCC_YUVA, true );
678 /* This one is used for YUVP to YUVA/RGBA without scaling
680 p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
681 VLC_FOURCC_YUVP, VLC_FOURCC_YUVA, false );
684 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
685 subpicture_t *p_subpic, subpicture_region_t *p_region,
686 int i_min_scale_ratio )
688 filter_t *p_text = p_spu->p->p_text;
690 assert( p_region->fmt.i_chroma == VLC_FOURCC_TEXT );
692 if( !p_text || !p_text->p_module )
695 /* Setup 3 variables which can be used to render
696 * time-dependent text (and effects). The first indicates
697 * the total amount of time the text will be on screen,
698 * the second the amount of time it has already been on
699 * screen (can be a negative value as text is layed out
700 * before it is rendered) and the third is a feedback
701 * variable from the renderer - if the renderer sets it
702 * then this particular text is time-dependent, eg. the
703 * visual progress bar inside the text in karaoke and the
704 * text needs to be rendered multiple times in order for
705 * the effect to work - we therefore need to return the
706 * region to its original state at the end of the loop,
707 * instead of leaving it in YUVA or YUVP.
708 * Any renderer which is unaware of how to render
709 * time-dependent text can happily ignore the variables
710 * and render the text the same as usual - it should at
711 * least show up on screen, but the effect won't change
712 * the text over time.
714 var_SetTime( p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
715 var_SetTime( p_text, "spu-elapsed", mdate() - p_subpic->i_start );
716 var_SetBool( p_text, "text-rerender", false );
717 var_SetInteger( p_text, "scale", i_min_scale_ratio );
719 if( p_text->pf_render_html && p_region->psz_html )
721 p_text->pf_render_html( p_text, p_region, p_region );
723 else if( p_text->pf_render_text )
725 p_text->pf_render_text( p_text, p_region, p_region );
727 *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
730 p_region->i_align |= SUBPICTURE_RENDERED;
734 * A few scale functions helpers.
742 static spu_scale_t spu_scale_create( int w, int h )
744 spu_scale_t s = { .w = w, .h = h };
751 static spu_scale_t spu_scale_unit(void )
753 return spu_scale_create( SCALE_UNIT, SCALE_UNIT );
755 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
757 return spu_scale_create( wn * SCALE_UNIT / wd,
758 hn * SCALE_UNIT / hd );
760 static int spu_scale_w( int v, const spu_scale_t s )
762 return v * s.w / SCALE_UNIT;
764 static int spu_scale_h( int v, const spu_scale_t s )
766 return v * s.h / SCALE_UNIT;
768 static int spu_invscale_w( int v, const spu_scale_t s )
770 return v * SCALE_UNIT / s.w;
772 static int spu_invscale_h( int v, const spu_scale_t s )
774 return v * SCALE_UNIT / s.h;
778 * A few area functions helpers
791 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t s )
793 spu_area_t a = { .i_x = x, .i_y = y, .i_width = w, .i_height = h, .scale = s };
796 static spu_area_t spu_area_scaled( spu_area_t a )
798 if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
801 a.i_x = spu_scale_w( a.i_x, a.scale );
802 a.i_y = spu_scale_h( a.i_y, a.scale );
804 a.i_width = spu_scale_w( a.i_width, a.scale );
805 a.i_height = spu_scale_h( a.i_height, a.scale );
807 a.scale = spu_scale_unit();
810 static spu_area_t spu_area_unscaled( spu_area_t a, spu_scale_t s )
812 if( a.scale.w == s.w && a.scale.h == s.h )
815 a = spu_area_scaled( a );
817 a.i_x = spu_invscale_w( a.i_x, s );
818 a.i_y = spu_invscale_h( a.i_y, s );
820 a.i_width = spu_invscale_w( a.i_width, s );
821 a.i_height = spu_invscale_h( a.i_height, s );
826 static bool spu_area_overlap( spu_area_t a, spu_area_t b )
831 a = spu_area_scaled( a );
832 b = spu_area_scaled( b );
834 return __MAX( a.i_x-i_dx, b.i_x ) < __MIN( a.i_x+a.i_width +i_dx, b.i_x+b.i_width ) &&
835 __MAX( a.i_y-i_dy, b.i_y ) < __MIN( a.i_y+a.i_height+i_dy, b.i_y+b.i_height );
839 * Avoid area overlapping
841 static void SpuAreaFixOverlap( spu_area_t *p_dst,
842 const spu_area_t *p_master,
843 const spu_area_t *p_sub, int i_sub, int i_align )
845 spu_area_t a = spu_area_scaled( *p_dst );
846 bool b_moved = false;
849 assert( p_master->i_x == 0 && p_master->i_y == 0 );
852 * XXX It is not fast O(n^2) but we should not have a lot of region */
856 for( int i = 0; i < i_sub; i++ )
858 spu_area_t sub = spu_area_scaled( p_sub[i] );
860 if( !spu_area_overlap( a, sub ) )
863 if( i_align & SUBPICTURE_ALIGN_TOP )
866 int i_y = sub.i_y + sub.i_height;
867 if( i_y + a.i_height > p_master->i_height )
872 else if( i_align & SUBPICTURE_ALIGN_BOTTOM )
875 int i_y = sub.i_y - a.i_height;
883 /* TODO what to do in this case? */
884 //fprintf( stderr, "Overlap with unsupported alignment\n" );
894 *p_dst = spu_area_unscaled( a, p_dst->scale );
901 static void SpuRegionPlace( int *pi_x, int *pi_y,
902 const subpicture_t *p_subpic,
903 const subpicture_region_t *p_region,
906 const int i_delta_x = p_region->i_x;
907 const int i_delta_y = p_region->i_y;
910 assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
911 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
915 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
917 i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
921 i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
924 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
928 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
930 i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
934 i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
937 if( p_subpic->b_absolute )
943 /* Margin shifts all subpictures */
944 if( i_margin_y != 0 )
947 /* Clamp offset to not go out of the screen (when possible) */
948 const int i_error_x = (i_x + p_region->fmt.i_width) - p_subpic->i_original_picture_width;
954 const int i_error_y = (i_y + p_region->fmt.i_height) - p_subpic->i_original_picture_height;
965 * This function computes the current alpha value for a given region.
967 static int SpuRegionAlpha( subpicture_t *p_subpic, subpicture_region_t *p_region )
969 /* Compute alpha blend value */
970 int i_fade_alpha = 255;
971 if( p_subpic->b_fade )
973 mtime_t i_fade_start = ( p_subpic->i_stop +
974 p_subpic->i_start ) / 2;
975 mtime_t i_now = mdate();
977 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
979 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
980 ( p_subpic->i_stop - i_fade_start );
983 return i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
987 * It will render the provided region onto p_pic_dst.
990 static void SpuRenderRegion( spu_t *p_spu,
991 picture_t *p_pic_dst, spu_area_t *p_area,
992 subpicture_t *p_subpic, subpicture_region_t *p_region,
993 const spu_scale_t scale_size,
994 const video_format_t *p_fmt,
995 const spu_area_t *p_subtitle_area, int i_subtitle_area )
997 spu_private_t *p_sys = p_spu->p;
999 video_format_t fmt_original = p_region->fmt;
1000 bool b_rerender_text = false;
1001 bool b_restore_format = false;
1005 video_format_t region_fmt;
1006 picture_t *p_region_picture;
1008 /* Invalidate area by default */
1009 *p_area = spu_area_create( 0,0, 0,0, scale_size );
1011 /* Render text region */
1012 if( p_region->fmt.i_chroma == VLC_FOURCC_TEXT )
1014 const int i_min_scale_ratio = SCALE_UNIT; /* FIXME what is the right value? (scale_size is not) */
1015 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region, i_min_scale_ratio );
1016 b_restore_format = b_rerender_text;
1018 /* Check if the rendering has failed ... */
1019 if( p_region->fmt.i_chroma == VLC_FOURCC_TEXT )
1023 /* Force palette if requested
1024 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
1025 * instead of only the right one (being the dvd spu).
1027 const bool b_using_palette = p_region->fmt.i_chroma == VLC_FOURCC_YUVP;
1028 const bool b_force_palette = b_using_palette && p_sys->b_force_palette;
1029 const bool b_force_crop = b_force_palette && p_sys->b_force_crop;
1030 bool b_changed_palette = false;
1033 /* Compute the margin which is expressed in destination pixel unit
1034 * The margin is applied only to subtitle and when no forced crop is
1035 * requested (dvd menu) */
1037 if( !b_force_crop && p_subpic->b_subtitle )
1038 i_margin_y = spu_invscale_h( p_sys->i_margin, scale_size );
1040 /* Place the picture
1041 * We compute the position in the rendered size */
1042 SpuRegionPlace( &i_x_offset, &i_y_offset,
1043 p_subpic, p_region, i_margin_y );
1045 /* Save this position for subtitle overlap support
1046 * it is really important that there are given without scale_size applied */
1047 *p_area = spu_area_create( i_x_offset, i_y_offset,
1048 p_region->fmt.i_width, p_region->fmt.i_height,
1051 /* Handle overlapping subtitles when possible */
1052 if( p_subpic->b_subtitle && !p_subpic->b_absolute )
1054 spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
1057 SpuAreaFixOverlap( p_area, &display, p_subtitle_area, i_subtitle_area,
1058 p_region->i_align );
1061 /* Fix the position for the current scale_size */
1062 i_x_offset = spu_scale_w( p_area->i_x, p_area->scale );
1063 i_y_offset = spu_scale_h( p_area->i_y, p_area->scale );
1066 if( b_force_palette )
1068 video_palette_t *p_palette = p_region->fmt.p_palette;
1069 video_palette_t palette;
1071 /* We suppose DVD palette here */
1072 palette.i_entries = 4;
1073 for( int i = 0; i < 4; i++ )
1074 for( int j = 0; j < 4; j++ )
1075 palette.palette[i][j] = p_sys->palette[i][j];
1077 if( p_palette->i_entries == palette.i_entries )
1079 for( int i = 0; i < p_palette->i_entries; i++ )
1080 for( int j = 0; j < 4; j++ )
1081 b_changed_palette |= p_palette->palette[i][j] != palette.palette[i][j];
1085 b_changed_palette = true;
1087 *p_palette = palette;
1091 region_fmt = p_region->fmt;
1092 p_region_picture = p_region->p_picture;
1095 /* Scale from rendered size to destination size */
1096 if( p_sys->p_scale && p_sys->p_scale->p_module &&
1097 ( !b_using_palette || ( p_sys->p_scale_yuvp && p_sys->p_scale_yuvp->p_module ) ) &&
1098 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_using_palette ) )
1100 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
1101 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
1103 /* Destroy the cache if unusable */
1104 if( p_region->p_private )
1106 subpicture_region_private_t *p_private = p_region->p_private;
1107 bool b_changed = false;
1109 /* Check resize changes */
1110 if( i_dst_width != p_private->fmt.i_width ||
1111 i_dst_height != p_private->fmt.i_height )
1114 /* Check forced palette changes */
1115 if( b_changed_palette )
1120 SpuRegionPrivateDestroy( p_private );
1121 p_region->p_private = NULL;
1125 /* Scale if needed into cache */
1126 if( !p_region->p_private )
1128 filter_t *p_scale = p_sys->p_scale;
1130 picture_t *p_picture = p_region->p_picture;
1131 picture_Hold( p_picture );
1133 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
1134 if( b_using_palette )
1136 filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
1138 p_scale_yuvp->fmt_in.video = p_region->fmt;
1140 /* TODO converting to RGBA for RGB video output is better */
1141 p_scale_yuvp->fmt_out.video = p_region->fmt;
1142 p_scale_yuvp->fmt_out.video.i_chroma = VLC_FOURCC_YUVA;
1144 p_picture = p_scale_yuvp->pf_video_filter( p_scale_yuvp, p_picture );
1147 /* Well we will try conversion+scaling */
1148 msg_Warn( p_spu, "%4.4s to %4.4s conversion failed",
1149 (const char*)&p_scale_yuvp->fmt_in.video.i_chroma,
1150 (const char*)&p_scale_yuvp->fmt_out.video.i_chroma );
1154 /* Conversion(except from YUVP)/Scaling */
1156 ( p_picture->format.i_width != i_dst_width ||
1157 p_picture->format.i_height != i_dst_height ) )
1159 p_scale->fmt_in.video = p_picture->format;
1160 p_scale->fmt_out.video = p_picture->format;
1162 p_scale->fmt_out.video.i_width = i_dst_width;
1163 p_scale->fmt_out.video.i_height = i_dst_height;
1165 p_scale->fmt_out.video.i_visible_width =
1166 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
1167 p_scale->fmt_out.video.i_visible_height =
1168 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
1170 p_picture = p_scale->pf_video_filter( p_scale, p_picture );
1172 msg_Err( p_spu, "scaling failed" );
1178 p_region->p_private = SpuRegionPrivateCreate( &p_picture->format );
1179 if( p_region->p_private )
1181 p_region->p_private->p_picture = p_picture;
1182 if( !p_region->p_private->p_picture )
1184 SpuRegionPrivateDestroy( p_region->p_private );
1185 p_region->p_private = NULL;
1190 picture_Release( p_picture );
1195 /* And use the scaled picture */
1196 if( p_region->p_private )
1198 region_fmt = p_region->p_private->fmt;
1199 p_region_picture = p_region->p_private->p_picture;
1203 /* Force cropping if requested */
1206 int i_crop_x = spu_scale_w( p_sys->i_crop_x, scale_size );
1207 int i_crop_y = spu_scale_h( p_sys->i_crop_y, scale_size );
1208 int i_crop_width = spu_scale_w( p_sys->i_crop_width, scale_size );
1209 int i_crop_height= spu_scale_h( p_sys->i_crop_height,scale_size );
1211 /* Find the intersection */
1212 if( i_crop_x + i_crop_width <= i_x_offset ||
1213 i_x_offset + (int)region_fmt.i_visible_width < i_crop_x ||
1214 i_crop_y + i_crop_height <= i_y_offset ||
1215 i_y_offset + (int)region_fmt.i_visible_height < i_crop_y )
1217 /* No intersection */
1218 region_fmt.i_visible_width =
1219 region_fmt.i_visible_height = 0;
1223 int i_x, i_y, i_x_end, i_y_end;
1224 i_x = __MAX( i_crop_x, i_x_offset );
1225 i_y = __MAX( i_crop_y, i_y_offset );
1226 i_x_end = __MIN( i_crop_x + i_crop_width,
1227 i_x_offset + (int)region_fmt.i_visible_width );
1228 i_y_end = __MIN( i_crop_y + i_crop_height,
1229 i_y_offset + (int)region_fmt.i_visible_height );
1231 region_fmt.i_x_offset = i_x - i_x_offset;
1232 region_fmt.i_y_offset = i_y - i_y_offset;
1233 region_fmt.i_visible_width = i_x_end - i_x;
1234 region_fmt.i_visible_height = i_y_end - i_y;
1236 i_x_offset = __MAX( i_x, 0 );
1237 i_y_offset = __MAX( i_y, 0 );
1241 /* Update the blender */
1242 SpuRenderUpdateBlend( p_spu, p_fmt->i_width, p_fmt->i_height, ®ion_fmt );
1244 if( p_sys->p_blend->p_module )
1246 const int i_alpha = SpuRegionAlpha( p_subpic, p_region );
1248 p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_pic_dst,
1249 p_region_picture, i_x_offset, i_y_offset, i_alpha );
1253 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1254 (char *)&p_sys->p_blend->fmt_out.video.i_chroma,
1255 (char *)&p_sys->p_blend->fmt_out.video.i_chroma );
1259 if( b_rerender_text )
1261 /* Some forms of subtitles need to be re-rendered more than
1262 * once, eg. karaoke. We therefore restore the region to its
1263 * pre-rendered state, so the next time through everything is
1266 picture_Release( p_region->p_picture );
1267 p_region->p_picture = NULL;
1268 if( p_region->p_private )
1270 SpuRegionPrivateDestroy( p_region->p_private );
1271 p_region->p_private = NULL;
1273 p_region->i_align &= ~SUBPICTURE_RENDERED;
1275 if( b_restore_format )
1276 p_region->fmt = fmt_original;
1280 * This function compares two 64 bits integers.
1281 * It can be used by qsort.
1283 static int IntegerCmp( int64_t i0, int64_t i1 )
1285 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
1288 * This function compares 2 subpictures using the following properties
1289 * (ordered by priority)
1290 * 1. absolute positionning
1294 * It can be used by qsort.
1296 * XXX spu_RenderSubpictures depends heavily on this order.
1298 static int SubpictureCmp( const void *s0, const void *s1 )
1300 subpicture_t *p_subpic0 = *(subpicture_t**)s0;
1301 subpicture_t *p_subpic1 = *(subpicture_t**)s1;
1304 r = IntegerCmp( !p_subpic0->b_absolute, !p_subpic1->b_absolute );
1306 r = IntegerCmp( p_subpic0->i_start, p_subpic1->i_start );
1308 r = IntegerCmp( p_subpic0->i_order, p_subpic1->i_order );
1312 * This function renders all sub picture units in the list.
1314 void spu_RenderSubpictures( spu_t *p_spu,
1315 picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
1316 subpicture_t *p_subpic_list,
1317 const video_format_t *p_fmt_src )
1319 spu_private_t *p_sys = p_spu->p;
1321 const int i_source_video_width = p_fmt_src->i_width;
1322 const int i_source_video_height = p_fmt_src->i_height;
1323 const mtime_t i_current_date = mdate();
1325 unsigned int i_subpicture;
1326 subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
1328 unsigned int i_subtitle_region_count;
1329 spu_area_t p_subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
1330 spu_area_t *p_subtitle_area;
1331 int i_subtitle_area;
1333 vlc_mutex_lock( &p_sys->lock );
1335 /* Preprocess subpictures */
1337 i_subtitle_region_count = 0;
1338 for( subpicture_t * p_subpic = p_subpic_list;
1340 p_subpic = p_subpic->p_next )
1343 if( p_subpic->pf_pre_render )
1344 p_subpic->pf_pre_render( p_spu, p_subpic, p_fmt_dst );
1346 if( p_subpic->pf_update_regions )
1348 video_format_t fmt_org = *p_fmt_dst;
1350 fmt_org.i_visible_width = i_source_video_width;
1352 fmt_org.i_visible_height = i_source_video_height;
1354 p_subpic->pf_update_regions( p_spu, p_subpic, &fmt_org, i_current_date );
1358 if( p_subpic->b_subtitle )
1360 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
1361 i_subtitle_region_count++;
1365 pp_subpicture[i_subpicture++] = p_subpic;
1368 /* Be sure we have at least 1 picture to process */
1369 if( i_subpicture <= 0 )
1371 vlc_mutex_unlock( &p_sys->lock );
1375 /* Now order subpicture array
1376 * XXX The order is *really* important for overlap subtitles positionning */
1377 qsort( pp_subpicture, i_subpicture, sizeof(*pp_subpicture), SubpictureCmp );
1379 /* Allocate area array for subtitle overlap */
1380 i_subtitle_area = 0;
1381 p_subtitle_area = p_subtitle_area_buffer;
1382 if( i_subtitle_region_count > sizeof(p_subtitle_area_buffer)/sizeof(*p_subtitle_area_buffer) )
1383 p_subtitle_area = calloc( i_subtitle_region_count, sizeof(*p_subtitle_area) );
1385 /* Create the blending module */
1386 if( !p_sys->p_blend )
1387 SpuRenderCreateBlend( p_spu, p_fmt_dst->i_chroma, p_fmt_dst->i_aspect );
1389 /* Process all subpictures and regions (in the right order) */
1390 for( unsigned int i_index = 0; i_index < i_subpicture; i_index++ )
1392 subpicture_t *p_subpic = pp_subpicture[i_index];
1393 subpicture_region_t *p_region;
1395 if( !p_subpic->p_region )
1398 /* FIXME when possible use a better rendering size than source size
1399 * (max of display size and source size for example) FIXME */
1400 int i_render_width = p_subpic->i_original_picture_width;
1401 int i_render_height = p_subpic->i_original_picture_height;
1402 if( !i_render_width || !i_render_height )
1404 if( i_render_width != 0 || i_render_height != 0 )
1405 msg_Err( p_spu, "unsupported original picture size %dx%d",
1406 i_render_width, i_render_height );
1408 p_subpic->i_original_picture_width = i_render_width = i_source_video_width;
1409 p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
1414 p_sys->p_text->fmt_out.video.i_width =
1415 p_sys->p_text->fmt_out.video.i_visible_width = i_render_width;
1417 p_sys->p_text->fmt_out.video.i_height =
1418 p_sys->p_text->fmt_out.video.i_visible_height = i_render_height;
1421 /* Compute scaling from picture to source size */
1422 spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
1423 i_source_video_height, i_render_height );
1425 /* Update scaling from source size to display size(p_fmt_dst) */
1426 scale.w = scale.w * p_fmt_dst->i_width / i_source_video_width;
1427 scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
1429 /* Set default subpicture aspect ratio
1430 * FIXME if we only handle 1 aspect ratio per picture, why is it set per
1432 p_region = p_subpic->p_region;
1433 if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
1435 if( p_region->fmt.i_aspect != 0 )
1437 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
1438 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
1442 p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
1443 p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
1447 /* Take care of the aspect ratio */
1448 if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
1449 p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
1451 /* FIXME FIXME what about region->i_x/i_y ? */
1453 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
1454 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
1457 /* Render all regions
1458 * We always transform non absolute subtitle into absolute one on the
1459 * first rendering to allow good subtitle overlap support.
1461 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
1465 /* Check scale validity */
1466 if( scale.w <= 0 || scale.h <= 0 )
1470 SpuRenderRegion( p_spu, p_pic_dst, &area,
1471 p_subpic, p_region, scale, p_fmt_dst,
1472 p_subtitle_area, i_subtitle_area );
1474 if( p_subpic->b_subtitle )
1476 area = spu_area_unscaled( area, scale );
1477 if( !p_subpic->b_absolute && area.i_width > 0 && area.i_height > 0 )
1479 p_region->i_x = area.i_x;
1480 p_region->i_y = area.i_y;
1482 if( p_subtitle_area )
1483 p_subtitle_area[i_subtitle_area++] = area;
1486 if( p_subpic->b_subtitle )
1487 p_subpic->b_absolute = true;
1491 if( p_subtitle_area != p_subtitle_area_buffer )
1492 free( p_subtitle_area );
1494 vlc_mutex_unlock( &p_sys->lock );
1497 /*****************************************************************************
1498 * spu_SortSubpictures: find the subpictures to display
1499 *****************************************************************************
1500 * This function parses all subpictures and decides which ones need to be
1501 * displayed. If no picture has been selected, display_date will depend on
1503 * We also check for ephemer DVD subpictures (subpictures that have
1504 * to be removed if a newer one is available), which makes it a lot
1505 * more difficult to guess if a subpicture has to be rendered or not.
1506 *****************************************************************************/
1507 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
1509 p_subpic->p_next = *pp_head;
1511 *pp_head = p_subpic;
1514 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
1515 bool b_paused, bool b_subtitle_only )
1517 spu_private_t *p_sys = p_spu->p;
1519 subpicture_t *p_subpic = NULL;
1521 /* Run subpicture filters */
1522 filter_chain_SubFilter( p_sys->p_chain, display_date );
1524 vlc_mutex_lock( &p_sys->lock );
1526 /* We get an easily parsable chained list of subpictures which
1527 * ends with NULL since p_subpic was initialized to NULL. */
1528 for( i_channel = 0; i_channel < p_sys->i_channel; i_channel++ )
1530 subpicture_t *p_ephemer = NULL;
1531 mtime_t ephemer_date = 0;
1534 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1536 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
1537 subpicture_t *p_current = p_entry->p_subpicture;
1539 if( !p_current || p_entry->b_reject )
1541 if( p_entry->b_reject )
1542 SpuHeapDeleteAt( &p_sys->heap, i_index );
1546 if( p_current->i_channel != i_channel ||
1547 ( b_subtitle_only && !p_current->b_subtitle ) )
1552 display_date < p_current->i_start )
1554 /* Too early, come back next monday */
1558 if( p_current->i_start > ephemer_date )
1559 ephemer_date = p_current->i_start;
1561 if( display_date > p_current->i_stop &&
1562 ( !p_current->b_ephemer || p_current->i_stop > p_current->i_start ) &&
1563 !( p_current->b_subtitle && b_paused ) ) /* XXX Assume that subtitle are pausable */
1565 SpuHeapDeleteAt( &p_sys->heap, i_index );
1567 else if( p_current->b_ephemer )
1569 SubpictureChain( &p_ephemer, p_current );
1573 SubpictureChain( &p_subpic, p_current );
1577 /* If we found ephemer subpictures, check if they have to be
1578 * displayed or destroyed */
1579 while( p_ephemer != NULL )
1581 subpicture_t *p_tmp = p_ephemer;
1582 p_ephemer = p_ephemer->p_next;
1584 if( p_tmp->i_start < ephemer_date )
1586 SpuHeapDeleteSubpicture( &p_sys->heap, p_tmp );
1590 SubpictureChain( &p_subpic, p_tmp );
1594 vlc_mutex_unlock( &p_sys->lock );
1599 /*****************************************************************************
1600 * SpuClearChannel: clear an spu channel
1601 *****************************************************************************
1602 * This function destroys the subpictures which belong to the spu channel
1603 * corresponding to i_channel_id.
1604 *****************************************************************************/
1605 static void SpuClearChannel( spu_t *p_spu, int i_channel, bool b_locked )
1607 spu_private_t *p_sys = p_spu->p;
1608 int i_subpic; /* subpicture index */
1611 vlc_mutex_lock( &p_sys->lock );
1613 vlc_assert_locked( &p_sys->lock );
1615 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1617 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_subpic];
1618 subpicture_t *p_subpic = p_entry->p_subpicture;
1620 if( !p_subpic || p_subpic->i_channel != i_channel )
1623 /* You cannot delete subpicture outside of spu_SortSubpictures */
1624 p_entry->b_reject = true;
1628 vlc_mutex_unlock( &p_sys->lock );
1631 /*****************************************************************************
1632 * spu_ControlDefault: default methods for the subpicture unit control.
1633 *****************************************************************************/
1634 static int SpuControl( spu_t *p_spu, int i_query, va_list args )
1636 spu_private_t *p_sys = p_spu->p;
1641 case SPU_CHANNEL_REGISTER:
1642 pi = (int *)va_arg( args, int * );
1643 vlc_mutex_lock( &p_sys->lock );
1645 *pi = p_sys->i_channel++;
1646 vlc_mutex_unlock( &p_sys->lock );
1649 case SPU_CHANNEL_CLEAR:
1650 i = (int)va_arg( args, int );
1651 SpuClearChannel( p_spu, i, false );
1655 msg_Dbg( p_spu, "control query not supported" );
1656 return VLC_EGENERIC;
1662 /*****************************************************************************
1663 * Object variables callbacks
1664 *****************************************************************************/
1666 /*****************************************************************************
1667 * UpdateSPU: update subpicture settings
1668 *****************************************************************************
1669 * This function is called from CropCallback and at initialization time, to
1670 * retrieve crop information from the input.
1671 *****************************************************************************/
1672 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1674 spu_private_t *p_sys = p_spu->p;
1677 vlc_mutex_lock( &p_sys->lock );
1679 p_sys->b_force_palette = false;
1680 p_sys->b_force_crop = false;
1682 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1684 vlc_mutex_unlock( &p_sys->lock );
1688 p_sys->b_force_crop = true;
1689 p_sys->i_crop_x = var_GetInteger( p_object, "x-start" );
1690 p_sys->i_crop_y = var_GetInteger( p_object, "y-start" );
1691 p_sys->i_crop_width = var_GetInteger( p_object, "x-end" ) - p_sys->i_crop_x;
1692 p_sys->i_crop_height = var_GetInteger( p_object, "y-end" ) - p_sys->i_crop_y;
1694 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1696 memcpy( p_sys->palette, val.p_address, 16 );
1697 p_sys->b_force_palette = true;
1699 vlc_mutex_unlock( &p_sys->lock );
1701 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1702 p_sys->i_crop_x, p_sys->i_crop_y,
1703 p_sys->i_crop_width, p_sys->i_crop_height,
1704 p_sys->b_force_palette );
1707 /*****************************************************************************
1708 * CropCallback: called when the highlight properties are changed
1709 *****************************************************************************
1710 * This callback is called from the input thread when we need cropping
1711 *****************************************************************************/
1712 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1713 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1715 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1717 UpdateSPU( (spu_t *)p_data, p_object );
1721 /*****************************************************************************
1722 * Buffers allocation callbacks for the filters
1723 *****************************************************************************/
1724 struct filter_owner_sys_t
1730 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1732 filter_owner_sys_t *p_sys = p_filter->p_owner;
1734 subpicture_t *p_subpicture = subpicture_New();
1736 p_subpicture->i_channel = p_sys->i_channel;
1737 return p_subpicture;
1739 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1741 VLC_UNUSED( p_filter );
1742 subpicture_Delete( p_subpic );
1745 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1747 VLC_UNUSED(p_filter);
1748 return subpicture_New();
1750 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1752 VLC_UNUSED(p_filter);
1753 subpicture_Delete( p_subpic );
1756 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1758 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1760 VLC_UNUSED(p_filter);
1761 return picture_New( p_fmt->i_chroma,
1762 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
1764 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1766 VLC_UNUSED(p_filter);
1767 picture_Release( p_picture );
1770 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1772 spu_t *p_spu = p_data;
1774 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1776 return VLC_EGENERIC;
1778 p_filter->pf_sub_buffer_new = sub_new_buffer;
1779 p_filter->pf_sub_buffer_del = sub_del_buffer;
1781 p_filter->p_owner = p_sys;
1782 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1783 p_sys->p_spu = p_spu;
1788 static void SubFilterAllocationClean( filter_t *p_filter )
1790 filter_owner_sys_t *p_sys = p_filter->p_owner;
1792 SpuClearChannel( p_sys->p_spu, p_sys->i_channel, true );
1793 free( p_filter->p_owner );
1796 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1797 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1799 spu_t *p_spu = p_data;
1800 spu_private_t *p_sys = p_spu->p;
1802 VLC_UNUSED(p_object); VLC_UNUSED(oldval);
1803 VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1805 vlc_mutex_lock( &p_sys->lock );
1806 filter_chain_Reset( p_sys->p_chain, NULL, NULL );
1807 spu_ParseChain( p_spu );
1808 vlc_mutex_unlock( &p_sys->lock );