1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2005 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 *****************************************************************************/
29 #include <stdlib.h> /* free() */
30 #include <stdio.h> /* sprintf() */
31 #include <string.h> /* strerror() */
35 #include <vlc_block.h>
36 #include <vlc_filter.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static void UpdateSPU ( spu_t *, vlc_object_t * );
43 static int CropCallback( vlc_object_t *, char const *,
44 vlc_value_t, vlc_value_t, void * );
46 static int spu_vaControlDefault( spu_t *, int, va_list );
48 static subpicture_t *sub_new_buffer( filter_t * );
49 static void sub_del_buffer( filter_t *, subpicture_t * );
50 static subpicture_t *spu_new_buffer( filter_t * );
51 static void spu_del_buffer( filter_t *, subpicture_t * );
52 static picture_t *spu_new_video_buffer( filter_t * );
53 static void spu_del_video_buffer( filter_t *, picture_t * );
55 struct filter_owner_sys_t
62 * Creates the subpicture unit
64 * \param p_this the parent object which creates the subpicture unit
66 spu_t *__spu_Create( vlc_object_t *p_this )
69 spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
71 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
73 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
76 p_spu->p_blend = NULL;
78 p_spu->p_scale = NULL;
80 p_spu->pf_control = spu_vaControlDefault;
82 /* Register the default subpicture channel */
85 vlc_mutex_init( p_this, &p_spu->subpicture_lock );
87 vlc_object_attach( p_spu, p_this );
93 * Initialise the subpicture unit
95 * \param p_spu the subpicture unit object
97 int spu_Init( spu_t *p_spu )
102 /* If the user requested a sub margin, we force the position. */
103 var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
104 var_Get( p_spu, "sub-margin", &val );
105 p_spu->i_margin = val.i_int;
107 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
108 var_Get( p_spu, "sub-filter", &val );
110 psz_parser = val.psz_string;
112 while( psz_parser && *psz_parser )
114 config_chain_t *p_cfg;
117 psz_parser = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
119 msg_Dbg( p_spu, "adding sub-filter: %s", psz_name );
121 p_spu->pp_filter[p_spu->i_filter] =
122 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
123 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
124 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
125 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
126 p_spu->pp_filter[p_spu->i_filter]->p_cfg = p_cfg;
127 p_spu->pp_filter[p_spu->i_filter]->p_module =
128 module_Need( p_spu->pp_filter[p_spu->i_filter],
129 "sub filter", psz_name, 0 );
130 if( p_spu->pp_filter[p_spu->i_filter]->p_module )
132 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
133 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
134 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
135 p_sys->p_spu = p_spu;
140 msg_Dbg( p_spu, "no sub filter found" );
141 config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
142 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
143 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
146 if( p_spu->i_filter >= 10 )
148 msg_Dbg( p_spu, "can't add anymore filters" );
153 if( val.psz_string ) free( val.psz_string );
159 * Destroy the subpicture unit
161 * \param p_this the parent object which destroys the subpicture unit
163 void spu_Destroy( spu_t *p_spu )
167 vlc_object_detach( p_spu );
169 /* Destroy all remaining subpictures */
170 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
172 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
174 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
180 if( p_spu->p_blend->p_module )
181 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
183 vlc_object_detach( p_spu->p_blend );
184 vlc_object_destroy( p_spu->p_blend );
189 if( p_spu->p_text->p_module )
190 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
192 vlc_object_detach( p_spu->p_text );
193 vlc_object_destroy( p_spu->p_text );
198 if( p_spu->p_scale->p_module )
199 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
201 vlc_object_detach( p_spu->p_scale );
202 vlc_object_destroy( p_spu->p_scale );
205 while( p_spu->i_filter-- )
207 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
208 p_spu->pp_filter[p_spu->i_filter]->p_module );
209 free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
210 config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
211 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
212 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
215 vlc_mutex_destroy( &p_spu->subpicture_lock );
216 vlc_object_destroy( p_spu );
220 * Attach/Detach the SPU from any input
222 * \param p_this the object in which to destroy the subpicture unit
223 * \param b_attach to select attach or detach
225 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
227 vlc_object_t *p_input;
229 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
230 if( !p_input ) return;
234 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
235 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
236 vlc_object_release( p_input );
240 /* Delete callback */
241 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
242 vlc_object_release( p_input );
247 * Create a subpicture region
249 * \param p_this vlc_object_t
250 * \param p_fmt the format that this subpicture region should have
252 static void RegionPictureRelease( picture_t *p_pic )
254 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
256 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
257 video_format_t *p_fmt )
259 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
260 memset( p_region, 0, sizeof(subpicture_region_t) );
261 p_region->p_next = 0;
262 p_region->p_cache = 0;
263 p_region->fmt = *p_fmt;
264 p_region->psz_text = 0;
265 p_region->p_style = NULL;
267 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
268 p_fmt->p_palette = p_region->fmt.p_palette =
269 malloc( sizeof(video_palette_t) );
270 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
272 p_region->picture.p_data_orig = NULL;
274 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
276 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
277 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
279 if( !p_region->picture.i_planes )
282 free( p_fmt->p_palette );
286 p_region->picture.pf_release = RegionPictureRelease;
292 * Make a subpicture region from an existing picture_t
294 * \param p_this vlc_object_t
295 * \param p_fmt the format that this subpicture region should have
296 * \param p_pic a pointer to the picture creating the region (not freed)
298 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
299 video_format_t *p_fmt,
302 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
303 memset( p_region, 0, sizeof(subpicture_region_t) );
304 p_region->p_next = 0;
305 p_region->p_cache = 0;
306 p_region->fmt = *p_fmt;
307 p_region->psz_text = 0;
308 p_region->p_style = NULL;
310 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
311 p_fmt->p_palette = p_region->fmt.p_palette =
312 malloc( sizeof(video_palette_t) );
313 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
315 memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
316 p_region->picture.pf_release = RegionPictureRelease;
322 * Destroy a subpicture region
324 * \param p_this vlc_object_t
325 * \param p_region the subpicture region to destroy
327 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
329 if( !p_region ) return;
330 if( p_region->picture.pf_release )
331 p_region->picture.pf_release( &p_region->picture );
332 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
333 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
335 if( p_region->psz_text )
336 free( p_region->psz_text );
337 //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
342 * Display a subpicture
344 * Remove the reservation flag of a subpicture, which will cause it to be
346 * \param p_spu the subpicture unit object
347 * \param p_subpic the subpicture to display
349 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
351 /* Check if status is valid */
352 if( p_subpic->i_status != RESERVED_SUBPICTURE )
354 msg_Err( p_spu, "subpicture %p has invalid status #%d",
355 p_subpic, p_subpic->i_status );
358 /* Remove reservation flag */
359 p_subpic->i_status = READY_SUBPICTURE;
361 if( p_subpic->i_channel == DEFAULT_CHAN )
363 p_subpic->i_channel = 0xFFFF;
364 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
365 p_subpic->i_channel = DEFAULT_CHAN;
370 * Allocate a subpicture in the spu heap.
372 * This function create a reserved subpicture in the spu heap.
373 * A null pointer is returned if the function fails. This method provides an
374 * already allocated zone of memory in the spu data fields. It needs locking
375 * since several pictures can be created by several producers threads.
376 * \param p_spu the subpicture unit in which to create the subpicture
377 * \return NULL on error, a reserved subpicture otherwise
379 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
381 int i_subpic; /* subpicture index */
382 subpicture_t * p_subpic = NULL; /* first free subpicture */
385 vlc_mutex_lock( &p_spu->subpicture_lock );
388 * Look for an empty place
391 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
393 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
395 /* Subpicture is empty and ready for allocation */
396 p_subpic = &p_spu->p_subpicture[i_subpic];
397 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
402 /* If no free subpicture could be found */
403 if( p_subpic == NULL )
405 msg_Err( p_spu, "subpicture heap is full" );
406 vlc_mutex_unlock( &p_spu->subpicture_lock );
410 /* Copy subpicture information, set some default values */
411 memset( p_subpic, 0, sizeof(subpicture_t) );
412 p_subpic->i_status = RESERVED_SUBPICTURE;
413 p_subpic->b_absolute = VLC_TRUE;
414 p_subpic->b_pausable = VLC_FALSE;
415 p_subpic->b_fade = VLC_FALSE;
416 p_subpic->i_alpha = 0xFF;
417 p_subpic->p_region = 0;
418 p_subpic->pf_render = 0;
419 p_subpic->pf_destroy = 0;
421 vlc_mutex_unlock( &p_spu->subpicture_lock );
423 p_subpic->pf_create_region = __spu_CreateRegion;
424 p_subpic->pf_make_region = __spu_MakeRegion;
425 p_subpic->pf_destroy_region = __spu_DestroyRegion;
431 * Remove a subpicture from the heap
433 * This function frees a previously reserved subpicture.
434 * It is meant to be used when the construction of a picture aborted.
435 * This function does not need locking since reserved subpictures are ignored
438 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
441 vlc_mutex_lock( &p_spu->subpicture_lock );
443 /* There can be race conditions so we need to check the status */
444 if( p_subpic->i_status == FREE_SUBPICTURE )
446 vlc_mutex_unlock( &p_spu->subpicture_lock );
450 /* Check if status is valid */
451 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
452 && ( p_subpic->i_status != READY_SUBPICTURE ) )
454 msg_Err( p_spu, "subpicture %p has invalid status %d",
455 p_subpic, p_subpic->i_status );
458 while( p_subpic->p_region )
460 subpicture_region_t *p_region = p_subpic->p_region;
461 p_subpic->p_region = p_region->p_next;
462 spu_DestroyRegion( p_spu, p_region );
465 if( p_subpic->pf_destroy )
467 p_subpic->pf_destroy( p_subpic );
470 p_subpic->i_status = FREE_SUBPICTURE;
472 vlc_mutex_unlock( &p_spu->subpicture_lock );
475 /*****************************************************************************
476 * spu_RenderSubpictures: render a subpicture list
477 *****************************************************************************
478 * This function renders all sub picture units in the list.
479 *****************************************************************************/
480 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
481 picture_t *p_pic_dst, picture_t *p_pic_src,
482 subpicture_t *p_subpic,
483 int i_scale_width_orig, int i_scale_height_orig )
486 vlc_mutex_lock( &p_spu->subpicture_lock );
488 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
489 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
491 subpicture_region_t *p_region = p_subpic->p_region;
492 int i_scale_width, i_scale_height;
493 int i_subpic_x = p_subpic->i_x;
495 /* Load the blending module */
496 if( !p_spu->p_blend && p_region )
498 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
499 vlc_object_attach( p_spu->p_blend, p_spu );
500 p_spu->p_blend->fmt_out.video.i_x_offset =
501 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
502 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
503 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
504 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
506 p_spu->p_blend->p_module =
507 module_Need( p_spu->p_blend, "video blending", 0, 0 );
510 /* Load the text rendering module */
511 if( !p_spu->p_text && p_region && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
513 char *psz_modulename = NULL;
515 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
516 vlc_object_attach( p_spu->p_text, p_spu );
518 p_spu->p_text->fmt_out.video.i_width =
519 p_spu->p_text->fmt_out.video.i_visible_width =
521 p_spu->p_text->fmt_out.video.i_height =
522 p_spu->p_text->fmt_out.video.i_visible_height =
525 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
526 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
528 psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
529 if( psz_modulename && *psz_modulename )
531 p_spu->p_text->p_module =
532 module_Need( p_spu->p_text, "text renderer", psz_modulename, VLC_TRUE );
534 if( !p_spu->p_text->p_module )
536 p_spu->p_text->p_module =
537 module_Need( p_spu->p_text, "text renderer", 0, 0 );
539 if( psz_modulename ) free( psz_modulename );
543 if( p_subpic->i_original_picture_height > 0 &&
544 p_subpic->i_original_picture_width > 0 )
546 p_spu->p_text->fmt_out.video.i_width =
547 p_spu->p_text->fmt_out.video.i_visible_width =
548 p_subpic->i_original_picture_width;
549 p_spu->p_text->fmt_out.video.i_height =
550 p_spu->p_text->fmt_out.video.i_visible_height =
551 p_subpic->i_original_picture_height;
555 p_spu->p_text->fmt_out.video.i_width =
556 p_spu->p_text->fmt_out.video.i_visible_width =
558 p_spu->p_text->fmt_out.video.i_height =
559 p_spu->p_text->fmt_out.video.i_visible_height =
564 i_scale_width = i_scale_width_orig;
565 i_scale_height = i_scale_height_orig;
567 if( p_subpic->i_original_picture_height > 0 &&
568 p_subpic->i_original_picture_width > 0 )
570 i_scale_width = i_scale_width * p_fmt->i_width /
571 p_subpic->i_original_picture_width;
572 i_scale_height = i_scale_height * p_fmt->i_height /
573 p_subpic->i_original_picture_height;
575 else if( p_subpic->i_original_picture_height > 0 )
577 i_scale_height = i_scale_height * p_fmt->i_height /
578 p_subpic->i_original_picture_height;
579 i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height;
582 /* Set default subpicture aspect ratio */
583 if( p_region && p_region->fmt.i_aspect &&
584 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
586 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
587 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
590 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
592 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
593 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
596 /* Take care of the aspect ratio */
597 if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
598 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
600 i_scale_width = i_scale_width *
601 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
602 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
603 i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
606 /* Load the scaling module */
607 if( !p_spu->p_scale && (i_scale_width != 1000 ||
608 i_scale_height != 1000) )
610 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
611 vlc_object_attach( p_spu->p_scale, p_spu );
612 p_spu->p_scale->fmt_out.video.i_chroma =
613 p_spu->p_scale->fmt_in.video.i_chroma =
614 VLC_FOURCC('Y','U','V','P');
615 p_spu->p_scale->fmt_in.video.i_width =
616 p_spu->p_scale->fmt_in.video.i_height = 32;
617 p_spu->p_scale->fmt_out.video.i_width =
618 p_spu->p_scale->fmt_out.video.i_height = 16;
620 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
621 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
622 p_spu->p_scale->p_module =
623 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
626 while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
628 int i_fade_alpha = 255;
629 int i_x_offset = p_region->i_x + i_subpic_x;
630 int i_y_offset = p_region->i_y + p_subpic->i_y;
632 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
634 if( p_spu->p_text && p_spu->p_text->p_module )
636 p_region->i_align = p_subpic->i_flags;
638 if( p_spu->p_text->pf_render_html && p_region->psz_html )
640 p_spu->p_text->pf_render_html( p_spu->p_text,
641 p_region, p_region );
643 else if( p_spu->p_text->pf_render_text )
645 p_spu->p_text->pf_render_text( p_spu->p_text,
646 p_region, p_region );
651 /* Force palette if requested */
652 if( p_spu->b_force_palette && VLC_FOURCC('Y','U','V','P') ==
653 p_region->fmt.i_chroma )
655 memcpy( p_region->fmt.p_palette->palette,
656 p_spu->palette, 16 );
659 /* Scale SPU if necessary */
660 if( p_region->p_cache )
662 if( i_scale_width * p_region->fmt.i_width / 1000 !=
663 p_region->p_cache->fmt.i_width ||
664 i_scale_height * p_region->fmt.i_height / 1000 !=
665 p_region->p_cache->fmt.i_height )
667 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
669 p_region->p_cache = 0;
673 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
674 p_spu->p_scale && !p_region->p_cache )
678 p_spu->p_scale->fmt_in.video = p_region->fmt;
679 p_spu->p_scale->fmt_out.video = p_region->fmt;
682 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
683 &p_spu->p_scale->fmt_out.video );
684 if( p_spu->p_scale->fmt_out.video.p_palette )
685 *p_spu->p_scale->fmt_out.video.p_palette =
686 *p_region->fmt.p_palette;
687 p_region->p_cache->p_next = p_region->p_next;
689 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
690 &p_region->picture );
692 p_spu->p_scale->fmt_out.video.i_width =
693 p_region->fmt.i_width * i_scale_width / 1000;
694 p_spu->p_scale->fmt_out.video.i_visible_width =
695 p_region->fmt.i_visible_width * i_scale_width / 1000;
696 p_spu->p_scale->fmt_out.video.i_height =
697 p_region->fmt.i_height * i_scale_height / 1000;
698 p_spu->p_scale->fmt_out.video.i_visible_height =
699 p_region->fmt.i_visible_height * i_scale_height / 1000;
700 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
701 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
702 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
704 p_pic = p_spu->p_scale->pf_video_filter(
705 p_spu->p_scale, &p_region->p_cache->picture );
708 picture_t p_pic_tmp = p_region->p_cache->picture;
709 p_region->p_cache->picture = *p_pic;
714 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
715 p_spu->p_scale && p_region->p_cache )
717 p_region = p_region->p_cache;
720 if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
722 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
725 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
727 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
730 if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
732 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
735 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
737 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
740 if( p_subpic->b_absolute )
742 i_x_offset = p_region->i_x +
743 i_subpic_x * i_scale_width / 1000;
744 i_y_offset = p_region->i_y +
745 p_subpic->i_y * i_scale_height / 1000;
749 i_x_offset = __MAX( i_x_offset, 0 );
750 i_y_offset = __MAX( i_y_offset, 0 );
752 if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
755 int i_low = i_y_offset - p_spu->i_margin;
756 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
758 /* crop extra margin to keep within bounds */
759 if( i_low < 0 ) i_diff = i_low;
760 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
761 i_y_offset -= ( p_spu->i_margin + i_diff );
764 p_spu->p_blend->fmt_in.video = p_region->fmt;
766 /* Force cropping if requested */
767 if( p_spu->b_force_crop )
769 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
770 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
771 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
772 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
773 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
775 /* Find the intersection */
776 if( i_crop_x + i_crop_width <= i_x_offset ||
777 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
778 i_crop_y + i_crop_height <= i_y_offset ||
779 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
781 /* No intersection */
782 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
786 int i_x, i_y, i_x_end, i_y_end;
787 i_x = __MAX( i_crop_x, i_x_offset );
788 i_y = __MAX( i_crop_y, i_y_offset );
789 i_x_end = __MIN( i_crop_x + i_crop_width,
790 i_x_offset + (int)p_fmt->i_visible_width );
791 i_y_end = __MIN( i_crop_y + i_crop_height,
792 i_y_offset + (int)p_fmt->i_visible_height );
794 p_fmt->i_x_offset = i_x - i_x_offset;
795 p_fmt->i_y_offset = i_y - i_y_offset;
796 p_fmt->i_visible_width = i_x_end - i_x;
797 p_fmt->i_visible_height = i_y_end - i_y;
804 /* Update the output picture size */
805 p_spu->p_blend->fmt_out.video.i_width =
806 p_spu->p_blend->fmt_out.video.i_visible_width =
808 p_spu->p_blend->fmt_out.video.i_height =
809 p_spu->p_blend->fmt_out.video.i_visible_height =
812 if( p_subpic->b_fade )
814 mtime_t i_fade_start = ( p_subpic->i_stop +
815 p_subpic->i_start ) / 2;
816 mtime_t i_now = mdate();
817 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
819 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
820 ( p_subpic->i_stop - i_fade_start );
824 i_x_offset = __MAX( i_x_offset, 0 );
825 i_y_offset = __MAX( i_y_offset, 0 );
827 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
828 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
829 i_fade_alpha * p_subpic->i_alpha / 255 );
831 p_region = p_region->p_next;
834 p_subpic = p_subpic->p_next;
837 vlc_mutex_unlock( &p_spu->subpicture_lock );
840 /*****************************************************************************
841 * spu_SortSubpictures: find the subpictures to display
842 *****************************************************************************
843 * This function parses all subpictures and decides which ones need to be
844 * displayed. This operation does not need lock, since only READY_SUBPICTURE
845 * are handled. If no picture has been selected, display_date will depend on
847 * We also check for ephemer DVD subpictures (subpictures that have
848 * to be removed if a newer one is available), which makes it a lot
849 * more difficult to guess if a subpicture has to be rendered or not.
850 *****************************************************************************/
851 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
852 vlc_bool_t b_paused )
854 int i_index, i_channel;
855 subpicture_t *p_subpic = NULL;
856 subpicture_t *p_ephemer;
857 mtime_t ephemer_date;
859 /* Run subpicture filters */
860 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
862 subpicture_t *p_subpic_filter;
863 p_subpic_filter = p_spu->pp_filter[i_index]->
864 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
865 if( p_subpic_filter )
867 spu_DisplaySubpicture( p_spu, p_subpic_filter );
871 /* We get an easily parsable chained list of subpictures which
872 * ends with NULL since p_subpic was initialized to NULL. */
873 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
878 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
880 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
881 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
886 display_date < p_spu->p_subpicture[i_index].i_start )
888 /* Too early, come back next monday */
892 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
893 ephemer_date = p_spu->p_subpicture[i_index].i_start;
895 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
896 ( !p_spu->p_subpicture[i_index].b_ephemer ||
897 p_spu->p_subpicture[i_index].i_stop >
898 p_spu->p_subpicture[i_index].i_start ) &&
899 !( p_spu->p_subpicture[i_index].b_pausable &&
902 /* Too late, destroy the subpic */
903 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
907 /* If this is an ephemer subpic, add it to our list */
908 if( p_spu->p_subpicture[i_index].b_ephemer )
910 p_spu->p_subpicture[i_index].p_next = p_ephemer;
911 p_ephemer = &p_spu->p_subpicture[i_index];
916 p_spu->p_subpicture[i_index].p_next = p_subpic;
917 p_subpic = &p_spu->p_subpicture[i_index];
920 /* If we found ephemer subpictures, check if they have to be
921 * displayed or destroyed */
922 while( p_ephemer != NULL )
924 subpicture_t *p_tmp = p_ephemer;
925 p_ephemer = p_ephemer->p_next;
927 if( p_tmp->i_start < ephemer_date )
929 /* Ephemer subpicture has lived too long */
930 spu_DestroySubpicture( p_spu, p_tmp );
934 /* Ephemer subpicture can still live a bit */
935 p_tmp->p_next = p_subpic;
944 /*****************************************************************************
945 * SpuClearChannel: clear an spu channel
946 *****************************************************************************
947 * This function destroys the subpictures which belong to the spu channel
948 * corresponding to i_channel_id.
949 *****************************************************************************/
950 static void SpuClearChannel( spu_t *p_spu, int i_channel )
952 int i_subpic; /* subpicture index */
953 subpicture_t *p_subpic = NULL; /* first free subpicture */
955 vlc_mutex_lock( &p_spu->subpicture_lock );
957 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
959 p_subpic = &p_spu->p_subpicture[i_subpic];
960 if( p_subpic->i_status == FREE_SUBPICTURE
961 || ( p_subpic->i_status != RESERVED_SUBPICTURE
962 && p_subpic->i_status != READY_SUBPICTURE ) )
967 if( p_subpic->i_channel == i_channel )
969 while( p_subpic->p_region )
971 subpicture_region_t *p_region = p_subpic->p_region;
972 p_subpic->p_region = p_region->p_next;
973 spu_DestroyRegion( p_spu, p_region );
976 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
977 p_subpic->i_status = FREE_SUBPICTURE;
981 vlc_mutex_unlock( &p_spu->subpicture_lock );
984 /*****************************************************************************
985 * spu_ControlDefault: default methods for the subpicture unit control.
986 *****************************************************************************/
987 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
993 case SPU_CHANNEL_REGISTER:
994 pi = (int *)va_arg( args, int * );
995 if( pi ) *pi = p_spu->i_channel++;
998 case SPU_CHANNEL_CLEAR:
999 i = (int)va_arg( args, int );
1000 SpuClearChannel( p_spu, i );
1004 msg_Dbg( p_spu, "control query not supported" );
1005 return VLC_EGENERIC;
1011 /*****************************************************************************
1012 * Object variables callbacks
1013 *****************************************************************************/
1015 /*****************************************************************************
1016 * UpdateSPU: update subpicture settings
1017 *****************************************************************************
1018 * This function is called from CropCallback and at initialization time, to
1019 * retrieve crop information from the input.
1020 *****************************************************************************/
1021 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1025 p_spu->b_force_palette = VLC_FALSE;
1026 p_spu->b_force_crop = VLC_FALSE;
1028 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1030 p_spu->b_force_crop = VLC_TRUE;
1031 var_Get( p_object, "x-start", &val );
1032 p_spu->i_crop_x = val.i_int;
1033 var_Get( p_object, "y-start", &val );
1034 p_spu->i_crop_y = val.i_int;
1035 var_Get( p_object, "x-end", &val );
1036 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1037 var_Get( p_object, "y-end", &val );
1038 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1040 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1042 memcpy( p_spu->palette, val.p_address, 16 );
1043 p_spu->b_force_palette = VLC_TRUE;
1046 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1047 p_spu->i_crop_x, p_spu->i_crop_y,
1048 p_spu->i_crop_width, p_spu->i_crop_height,
1049 p_spu->b_force_palette );
1052 /*****************************************************************************
1053 * CropCallback: called when the highlight properties are changed
1054 *****************************************************************************
1055 * This callback is called from the input thread when we need cropping
1056 *****************************************************************************/
1057 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1058 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1060 UpdateSPU( (spu_t *)p_data, p_object );
1064 /*****************************************************************************
1065 * Buffers allocation callbacks for the filters
1066 *****************************************************************************/
1067 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1069 filter_owner_sys_t *p_sys = p_filter->p_owner;
1070 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1071 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1072 return p_subpicture;
1075 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1077 filter_owner_sys_t *p_sys = p_filter->p_owner;
1078 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1081 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1083 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1084 memset( p_subpic, 0, sizeof(subpicture_t) );
1085 p_subpic->b_absolute = VLC_TRUE;
1087 p_subpic->pf_create_region = __spu_CreateRegion;
1088 p_subpic->pf_make_region = __spu_MakeRegion;
1089 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1094 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1096 while( p_subpic->p_region )
1098 subpicture_region_t *p_region = p_subpic->p_region;
1099 p_subpic->p_region = p_region->p_next;
1100 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1106 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1108 picture_t *p_picture = malloc( sizeof(picture_t) );
1110 if( vout_AllocatePicture( p_filter, p_picture,
1111 p_filter->fmt_out.video.i_chroma,
1112 p_filter->fmt_out.video.i_width,
1113 p_filter->fmt_out.video.i_height,
1114 p_filter->fmt_out.video.i_aspect )
1121 p_picture->pf_release = RegionPictureRelease;
1126 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1128 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1129 if( p_pic ) free( p_pic );