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_video.h"
37 #include "video_output.h"
39 #include "vlc_filter.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static void UpdateSPU ( spu_t *, vlc_object_t * );
45 static int CropCallback( vlc_object_t *, char const *,
46 vlc_value_t, vlc_value_t, void * );
48 static int spu_vaControlDefault( spu_t *, int, va_list );
50 static subpicture_t *sub_new_buffer( filter_t * );
51 static void sub_del_buffer( filter_t *, subpicture_t * );
52 static subpicture_t *spu_new_buffer( filter_t * );
53 static void spu_del_buffer( filter_t *, subpicture_t * );
54 static picture_t *spu_new_video_buffer( filter_t * );
55 static void spu_del_video_buffer( filter_t *, picture_t * );
57 struct filter_owner_sys_t
64 * Creates the subpicture unit
66 * \param p_this the parent object which creates the subpicture unit
68 spu_t *__spu_Create( vlc_object_t *p_this )
71 spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
73 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
75 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
78 p_spu->p_blend = NULL;
80 p_spu->p_scale = NULL;
82 p_spu->pf_control = spu_vaControlDefault;
84 /* Register the default subpicture channel */
87 vlc_mutex_init( p_this, &p_spu->subpicture_lock );
89 vlc_object_attach( p_spu, p_this );
95 * Initialise the subpicture unit
97 * \param p_spu the subpicture unit object
99 int spu_Init( spu_t *p_spu )
104 /* If the user requested a sub margin, we force the position. */
105 var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
106 var_Get( p_spu, "sub-margin", &val );
107 p_spu->i_margin = val.i_int;
109 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
110 var_Get( p_spu, "sub-filter", &val );
112 psz_parser = val.psz_string;
114 while( psz_parser && *psz_parser )
116 config_chain_t *p_cfg; /* Do we ever need to free this ? */
119 psz_parser = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
121 msg_Dbg( p_spu, "adding sub-filter: %s", psz_name );
123 p_spu->pp_filter[p_spu->i_filter] =
124 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
125 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
126 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
127 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
128 p_spu->pp_filter[p_spu->i_filter]->p_cfg = p_cfg;;
129 p_spu->pp_filter[p_spu->i_filter]->p_module =
130 module_Need( p_spu->pp_filter[p_spu->i_filter],
131 "sub filter", psz_name, 0 );
132 if( p_spu->pp_filter[p_spu->i_filter]->p_module )
134 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
135 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
136 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
137 p_sys->p_spu = p_spu;
142 msg_Dbg( p_spu, "no sub filter found" );
143 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
144 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
147 if( p_spu->i_filter >= 10 )
149 msg_Dbg( p_spu, "can't add anymore filters" );
154 if( val.psz_string ) free( val.psz_string );
160 * Destroy the subpicture unit
162 * \param p_this the parent object which destroys the subpicture unit
164 void spu_Destroy( spu_t *p_spu )
168 vlc_object_detach( p_spu );
170 /* Destroy all remaining subpictures */
171 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
173 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
175 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
181 if( p_spu->p_blend->p_module )
182 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
184 vlc_object_detach( p_spu->p_blend );
185 vlc_object_destroy( p_spu->p_blend );
190 if( p_spu->p_text->p_module )
191 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
193 vlc_object_detach( p_spu->p_text );
194 vlc_object_destroy( p_spu->p_text );
199 if( p_spu->p_scale->p_module )
200 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
202 vlc_object_detach( p_spu->p_scale );
203 vlc_object_destroy( p_spu->p_scale );
206 while( p_spu->i_filter-- )
208 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
209 p_spu->pp_filter[p_spu->i_filter]->p_module );
210 free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
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 );
338 * Display a subpicture
340 * Remove the reservation flag of a subpicture, which will cause it to be
342 * \param p_spu the subpicture unit object
343 * \param p_subpic the subpicture to display
345 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
347 /* Check if status is valid */
348 if( p_subpic->i_status != RESERVED_SUBPICTURE )
350 msg_Err( p_spu, "subpicture %p has invalid status #%d",
351 p_subpic, p_subpic->i_status );
354 /* Remove reservation flag */
355 p_subpic->i_status = READY_SUBPICTURE;
357 if( p_subpic->i_channel == DEFAULT_CHAN )
359 p_subpic->i_channel = 0xFFFF;
360 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
361 p_subpic->i_channel = DEFAULT_CHAN;
366 * Allocate a subpicture in the spu heap.
368 * This function create a reserved subpicture in the spu heap.
369 * A null pointer is returned if the function fails. This method provides an
370 * already allocated zone of memory in the spu data fields. It needs locking
371 * since several pictures can be created by several producers threads.
372 * \param p_spu the subpicture unit in which to create the subpicture
373 * \return NULL on error, a reserved subpicture otherwise
375 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
377 int i_subpic; /* subpicture index */
378 subpicture_t * p_subpic = NULL; /* first free subpicture */
381 vlc_mutex_lock( &p_spu->subpicture_lock );
384 * Look for an empty place
387 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
389 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
391 /* Subpicture is empty and ready for allocation */
392 p_subpic = &p_spu->p_subpicture[i_subpic];
393 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
398 /* If no free subpicture could be found */
399 if( p_subpic == NULL )
401 msg_Err( p_spu, "subpicture heap is full" );
402 vlc_mutex_unlock( &p_spu->subpicture_lock );
406 /* Copy subpicture information, set some default values */
407 memset( p_subpic, 0, sizeof(subpicture_t) );
408 p_subpic->i_status = RESERVED_SUBPICTURE;
409 p_subpic->b_absolute = VLC_TRUE;
410 p_subpic->b_pausable = VLC_FALSE;
411 p_subpic->b_fade = VLC_FALSE;
412 p_subpic->i_alpha = 0xFF;
413 p_subpic->p_region = 0;
414 p_subpic->pf_render = 0;
415 p_subpic->pf_destroy = 0;
417 vlc_mutex_unlock( &p_spu->subpicture_lock );
419 p_subpic->pf_create_region = __spu_CreateRegion;
420 p_subpic->pf_make_region = __spu_MakeRegion;
421 p_subpic->pf_destroy_region = __spu_DestroyRegion;
427 * Remove a subpicture from the heap
429 * This function frees a previously reserved subpicture.
430 * It is meant to be used when the construction of a picture aborted.
431 * This function does not need locking since reserved subpictures are ignored
434 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
437 vlc_mutex_lock( &p_spu->subpicture_lock );
439 /* There can be race conditions so we need to check the status */
440 if( p_subpic->i_status == FREE_SUBPICTURE )
442 vlc_mutex_unlock( &p_spu->subpicture_lock );
446 /* Check if status is valid */
447 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
448 && ( p_subpic->i_status != READY_SUBPICTURE ) )
450 msg_Err( p_spu, "subpicture %p has invalid status %d",
451 p_subpic, p_subpic->i_status );
454 while( p_subpic->p_region )
456 subpicture_region_t *p_region = p_subpic->p_region;
457 p_subpic->p_region = p_region->p_next;
458 spu_DestroyRegion( p_spu, p_region );
461 if( p_subpic->pf_destroy )
463 p_subpic->pf_destroy( p_subpic );
466 p_subpic->i_status = FREE_SUBPICTURE;
468 vlc_mutex_unlock( &p_spu->subpicture_lock );
471 /*****************************************************************************
472 * spu_RenderSubpictures: render a subpicture list
473 *****************************************************************************
474 * This function renders all sub picture units in the list.
475 *****************************************************************************/
476 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
477 picture_t *p_pic_dst, picture_t *p_pic_src,
478 subpicture_t *p_subpic,
479 int i_scale_width_orig, int i_scale_height_orig )
482 vlc_mutex_lock( &p_spu->subpicture_lock );
484 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
485 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
487 subpicture_region_t *p_region = p_subpic->p_region;
488 int i_scale_width, i_scale_height;
489 int i_subpic_x = p_subpic->i_x;
491 /* Load the blending module */
492 if( !p_spu->p_blend && p_region )
494 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
495 vlc_object_attach( p_spu->p_blend, p_spu );
496 p_spu->p_blend->fmt_out.video.i_x_offset =
497 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
498 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
499 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
500 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
502 p_spu->p_blend->p_module =
503 module_Need( p_spu->p_blend, "video blending", 0, 0 );
506 /* Load the text rendering module */
507 if( !p_spu->p_text && p_region && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
509 char *psz_modulename = NULL;
511 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
512 vlc_object_attach( p_spu->p_text, p_spu );
514 p_spu->p_text->fmt_out.video.i_width =
515 p_spu->p_text->fmt_out.video.i_visible_width =
517 p_spu->p_text->fmt_out.video.i_height =
518 p_spu->p_text->fmt_out.video.i_visible_height =
521 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
522 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
524 psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
525 if( psz_modulename && *psz_modulename )
527 p_spu->p_text->p_module =
528 module_Need( p_spu->p_text, "text renderer", psz_modulename, VLC_TRUE );
530 if( !p_spu->p_text->p_module )
532 p_spu->p_text->p_module =
533 module_Need( p_spu->p_text, "text renderer", 0, 0 );
535 if( psz_modulename ) free( psz_modulename );
539 if( p_subpic->i_original_picture_height > 0 &&
540 p_subpic->i_original_picture_width > 0 )
542 p_spu->p_text->fmt_out.video.i_width =
543 p_spu->p_text->fmt_out.video.i_visible_width =
544 p_subpic->i_original_picture_width;
545 p_spu->p_text->fmt_out.video.i_height =
546 p_spu->p_text->fmt_out.video.i_visible_height =
547 p_subpic->i_original_picture_height;
551 p_spu->p_text->fmt_out.video.i_width =
552 p_spu->p_text->fmt_out.video.i_visible_width =
554 p_spu->p_text->fmt_out.video.i_height =
555 p_spu->p_text->fmt_out.video.i_visible_height =
560 i_scale_width = i_scale_width_orig;
561 i_scale_height = i_scale_height_orig;
563 if( p_subpic->i_original_picture_height > 0 &&
564 p_subpic->i_original_picture_width > 0 )
566 i_scale_width = i_scale_width * p_fmt->i_width /
567 p_subpic->i_original_picture_width;
568 i_scale_height = i_scale_height * p_fmt->i_height /
569 p_subpic->i_original_picture_height;
571 else if( p_subpic->i_original_picture_height > 0 )
573 i_scale_height = i_scale_height * p_fmt->i_height /
574 p_subpic->i_original_picture_height;
575 i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height;
578 /* Set default subpicture aspect ratio */
579 if( p_region && p_region->fmt.i_aspect &&
580 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
582 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
583 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
586 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
588 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
589 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
592 /* Take care of the aspect ratio */
593 if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
594 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
596 i_scale_width = i_scale_width *
597 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
598 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
599 i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
602 /* Load the scaling module */
603 if( !p_spu->p_scale && (i_scale_width != 1000 ||
604 i_scale_height != 1000) )
606 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
607 vlc_object_attach( p_spu->p_scale, p_spu );
608 p_spu->p_scale->fmt_out.video.i_chroma =
609 p_spu->p_scale->fmt_in.video.i_chroma =
610 VLC_FOURCC('Y','U','V','P');
611 p_spu->p_scale->fmt_in.video.i_width =
612 p_spu->p_scale->fmt_in.video.i_height = 32;
613 p_spu->p_scale->fmt_out.video.i_width =
614 p_spu->p_scale->fmt_out.video.i_height = 16;
616 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
617 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
618 p_spu->p_scale->p_module =
619 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
622 while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
624 int i_fade_alpha = 255;
625 int i_x_offset = p_region->i_x + i_subpic_x;
626 int i_y_offset = p_region->i_y + p_subpic->i_y;
628 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
630 if( p_spu->p_text && p_spu->p_text->p_module &&
631 p_spu->p_text->pf_render_text )
633 p_region->i_align = p_subpic->i_flags;
634 p_spu->p_text->pf_render_text( p_spu->p_text,
635 p_region, p_region );
639 /* Force palette if requested */
640 if( p_spu->b_force_palette && VLC_FOURCC('Y','U','V','P') ==
641 p_region->fmt.i_chroma )
643 memcpy( p_region->fmt.p_palette->palette,
644 p_spu->palette, 16 );
647 /* Scale SPU if necessary */
648 if( p_region->p_cache )
650 if( i_scale_width * p_region->fmt.i_width / 1000 !=
651 p_region->p_cache->fmt.i_width ||
652 i_scale_height * p_region->fmt.i_height / 1000 !=
653 p_region->p_cache->fmt.i_height )
655 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
657 p_region->p_cache = 0;
661 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
662 p_spu->p_scale && !p_region->p_cache )
666 p_spu->p_scale->fmt_in.video = p_region->fmt;
667 p_spu->p_scale->fmt_out.video = p_region->fmt;
670 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
671 &p_spu->p_scale->fmt_out.video );
672 if( p_spu->p_scale->fmt_out.video.p_palette )
673 *p_spu->p_scale->fmt_out.video.p_palette =
674 *p_region->fmt.p_palette;
675 p_region->p_cache->p_next = p_region->p_next;
677 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
678 &p_region->picture );
680 p_spu->p_scale->fmt_out.video.i_width =
681 p_region->fmt.i_width * i_scale_width / 1000;
682 p_spu->p_scale->fmt_out.video.i_visible_width =
683 p_region->fmt.i_visible_width * i_scale_width / 1000;
684 p_spu->p_scale->fmt_out.video.i_height =
685 p_region->fmt.i_height * i_scale_height / 1000;
686 p_spu->p_scale->fmt_out.video.i_visible_height =
687 p_region->fmt.i_visible_height * i_scale_height / 1000;
688 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
689 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
690 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
692 p_pic = p_spu->p_scale->pf_video_filter(
693 p_spu->p_scale, &p_region->p_cache->picture );
696 picture_t p_pic_tmp = p_region->p_cache->picture;
697 p_region->p_cache->picture = *p_pic;
702 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
703 p_spu->p_scale && p_region->p_cache )
705 p_region = p_region->p_cache;
708 if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
710 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
713 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
715 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
718 if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
720 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
723 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
725 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
728 if( p_subpic->b_absolute )
730 i_x_offset = p_region->i_x +
731 i_subpic_x * i_scale_width / 1000;
732 i_y_offset = p_region->i_y +
733 p_subpic->i_y * i_scale_height / 1000;
737 if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
740 int i_low = i_y_offset - p_spu->i_margin;
741 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
743 /* crop extra margin to keep within bounds */
744 if( i_low < 0 ) i_diff = i_low;
745 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
746 i_y_offset -= ( p_spu->i_margin + i_diff );
749 p_spu->p_blend->fmt_in.video = p_region->fmt;
751 /* Force cropping if requested */
752 if( p_spu->b_force_crop )
754 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
755 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
756 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
757 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
758 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
760 /* Find the intersection */
761 if( i_crop_x + i_crop_width <= i_x_offset ||
762 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
763 i_crop_y + i_crop_height <= i_y_offset ||
764 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
766 /* No intersection */
767 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
771 int i_x, i_y, i_x_end, i_y_end;
772 i_x = __MAX( i_crop_x, i_x_offset );
773 i_y = __MAX( i_crop_y, i_y_offset );
774 i_x_end = __MIN( i_crop_x + i_crop_width,
775 i_x_offset + (int)p_fmt->i_visible_width );
776 i_y_end = __MIN( i_crop_y + i_crop_height,
777 i_y_offset + (int)p_fmt->i_visible_height );
779 p_fmt->i_x_offset = i_x - i_x_offset;
780 p_fmt->i_y_offset = i_y - i_y_offset;
781 p_fmt->i_visible_width = i_x_end - i_x;
782 p_fmt->i_visible_height = i_y_end - i_y;
789 /* Update the output picture size */
790 p_spu->p_blend->fmt_out.video.i_width =
791 p_spu->p_blend->fmt_out.video.i_visible_width =
793 p_spu->p_blend->fmt_out.video.i_height =
794 p_spu->p_blend->fmt_out.video.i_visible_height =
797 if( p_subpic->b_fade )
799 mtime_t i_fade_start = ( p_subpic->i_stop +
800 p_subpic->i_start ) / 2;
801 mtime_t i_now = mdate();
802 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
804 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
805 ( p_subpic->i_stop - i_fade_start );
809 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
810 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
811 i_fade_alpha * p_subpic->i_alpha / 255 );
813 p_region = p_region->p_next;
816 p_subpic = p_subpic->p_next;
819 vlc_mutex_unlock( &p_spu->subpicture_lock );
822 /*****************************************************************************
823 * spu_SortSubpictures: find the subpictures to display
824 *****************************************************************************
825 * This function parses all subpictures and decides which ones need to be
826 * displayed. This operation does not need lock, since only READY_SUBPICTURE
827 * are handled. If no picture has been selected, display_date will depend on
829 * We also check for ephemer DVD subpictures (subpictures that have
830 * to be removed if a newer one is available), which makes it a lot
831 * more difficult to guess if a subpicture has to be rendered or not.
832 *****************************************************************************/
833 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
834 vlc_bool_t b_paused )
836 int i_index, i_channel;
837 subpicture_t *p_subpic = NULL;
838 subpicture_t *p_ephemer;
839 mtime_t ephemer_date;
841 /* Run subpicture filters */
842 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
844 subpicture_t *p_subpic_filter;
845 p_subpic_filter = p_spu->pp_filter[i_index]->
846 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
847 if( p_subpic_filter )
849 spu_DisplaySubpicture( p_spu, p_subpic_filter );
853 /* We get an easily parsable chained list of subpictures which
854 * ends with NULL since p_subpic was initialized to NULL. */
855 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
860 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
862 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
863 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
868 display_date < p_spu->p_subpicture[i_index].i_start )
870 /* Too early, come back next monday */
874 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
875 ephemer_date = p_spu->p_subpicture[i_index].i_start;
877 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
878 ( !p_spu->p_subpicture[i_index].b_ephemer ||
879 p_spu->p_subpicture[i_index].i_stop >
880 p_spu->p_subpicture[i_index].i_start ) &&
881 !( p_spu->p_subpicture[i_index].b_pausable &&
884 /* Too late, destroy the subpic */
885 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
889 /* If this is an ephemer subpic, add it to our list */
890 if( p_spu->p_subpicture[i_index].b_ephemer )
892 p_spu->p_subpicture[i_index].p_next = p_ephemer;
893 p_ephemer = &p_spu->p_subpicture[i_index];
898 p_spu->p_subpicture[i_index].p_next = p_subpic;
899 p_subpic = &p_spu->p_subpicture[i_index];
902 /* If we found ephemer subpictures, check if they have to be
903 * displayed or destroyed */
904 while( p_ephemer != NULL )
906 subpicture_t *p_tmp = p_ephemer;
907 p_ephemer = p_ephemer->p_next;
909 if( p_tmp->i_start < ephemer_date )
911 /* Ephemer subpicture has lived too long */
912 spu_DestroySubpicture( p_spu, p_tmp );
916 /* Ephemer subpicture can still live a bit */
917 p_tmp->p_next = p_subpic;
926 /*****************************************************************************
927 * SpuClearChannel: clear an spu channel
928 *****************************************************************************
929 * This function destroys the subpictures which belong to the spu channel
930 * corresponding to i_channel_id.
931 *****************************************************************************/
932 static void SpuClearChannel( spu_t *p_spu, int i_channel )
934 int i_subpic; /* subpicture index */
935 subpicture_t *p_subpic = NULL; /* first free subpicture */
937 vlc_mutex_lock( &p_spu->subpicture_lock );
939 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
941 p_subpic = &p_spu->p_subpicture[i_subpic];
942 if( p_subpic->i_status == FREE_SUBPICTURE
943 || ( p_subpic->i_status != RESERVED_SUBPICTURE
944 && p_subpic->i_status != READY_SUBPICTURE ) )
949 if( p_subpic->i_channel == i_channel )
951 while( p_subpic->p_region )
953 subpicture_region_t *p_region = p_subpic->p_region;
954 p_subpic->p_region = p_region->p_next;
955 spu_DestroyRegion( p_spu, p_region );
958 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
959 p_subpic->i_status = FREE_SUBPICTURE;
963 vlc_mutex_unlock( &p_spu->subpicture_lock );
966 /*****************************************************************************
967 * spu_ControlDefault: default methods for the subpicture unit control.
968 *****************************************************************************/
969 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
975 case SPU_CHANNEL_REGISTER:
976 pi = (int *)va_arg( args, int * );
977 if( pi ) *pi = p_spu->i_channel++;
980 case SPU_CHANNEL_CLEAR:
981 i = (int)va_arg( args, int );
982 SpuClearChannel( p_spu, i );
986 msg_Dbg( p_spu, "control query not supported" );
993 /*****************************************************************************
994 * Object variables callbacks
995 *****************************************************************************/
997 /*****************************************************************************
998 * UpdateSPU: update subpicture settings
999 *****************************************************************************
1000 * This function is called from CropCallback and at initialization time, to
1001 * retrieve crop information from the input.
1002 *****************************************************************************/
1003 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1007 p_spu->b_force_palette = VLC_FALSE;
1008 p_spu->b_force_crop = VLC_FALSE;
1010 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1012 p_spu->b_force_crop = VLC_TRUE;
1013 var_Get( p_object, "x-start", &val );
1014 p_spu->i_crop_x = val.i_int;
1015 var_Get( p_object, "y-start", &val );
1016 p_spu->i_crop_y = val.i_int;
1017 var_Get( p_object, "x-end", &val );
1018 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1019 var_Get( p_object, "y-end", &val );
1020 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1022 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1024 memcpy( p_spu->palette, val.p_address, 16 );
1025 p_spu->b_force_palette = VLC_TRUE;
1028 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1029 p_spu->i_crop_x, p_spu->i_crop_y,
1030 p_spu->i_crop_width, p_spu->i_crop_height,
1031 p_spu->b_force_palette );
1034 /*****************************************************************************
1035 * CropCallback: called when the highlight properties are changed
1036 *****************************************************************************
1037 * This callback is called from the input thread when we need cropping
1038 *****************************************************************************/
1039 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1040 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1042 UpdateSPU( (spu_t *)p_data, p_object );
1046 /*****************************************************************************
1047 * Buffers allocation callbacks for the filters
1048 *****************************************************************************/
1049 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1051 filter_owner_sys_t *p_sys = p_filter->p_owner;
1052 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1053 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1054 return p_subpicture;
1057 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1059 filter_owner_sys_t *p_sys = p_filter->p_owner;
1060 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1063 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1065 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1066 memset( p_subpic, 0, sizeof(subpicture_t) );
1067 p_subpic->b_absolute = VLC_TRUE;
1069 p_subpic->pf_create_region = __spu_CreateRegion;
1070 p_subpic->pf_make_region = __spu_MakeRegion;
1071 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1076 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1078 while( p_subpic->p_region )
1080 subpicture_region_t *p_region = p_subpic->p_region;
1081 p_subpic->p_region = p_region->p_next;
1082 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1088 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1090 picture_t *p_picture = malloc( sizeof(picture_t) );
1092 if( vout_AllocatePicture( p_filter, p_picture,
1093 p_filter->fmt_out.video.i_chroma,
1094 p_filter->fmt_out.video.i_width,
1095 p_filter->fmt_out.video.i_height,
1096 p_filter->fmt_out.video.i_aspect )
1103 p_picture->pf_release = RegionPictureRelease;
1108 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1110 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1111 if( p_pic ) free( p_pic );