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 )
105 /* If the user requested a sub margin, we force the position. */
106 var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
107 var_Get( p_spu, "sub-margin", &val );
108 p_spu->i_margin = val.i_int;
110 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
111 var_Get( p_spu, "sub-filter", &val );
113 psz_filter = val.psz_string;
114 psz_parser = psz_filter;
116 while( psz_parser && *psz_parser )
118 config_chain_t *p_cfg; /* Do we ever need to free this ? */
121 psz_parser = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
123 msg_Dbg( p_spu, "adding sub-filter: %s", psz_name );
125 p_spu->pp_filter[p_spu->i_filter] =
126 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
127 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
128 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
129 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
130 p_spu->pp_filter[p_spu->i_filter]->p_cfg = p_cfg;;
131 p_spu->pp_filter[p_spu->i_filter]->p_module =
132 module_Need( p_spu->pp_filter[p_spu->i_filter],
133 "sub filter", psz_name, 0 );
134 if( p_spu->pp_filter[p_spu->i_filter]->p_module )
136 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
137 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
138 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
139 p_sys->p_spu = p_spu;
144 msg_Dbg( p_spu, "no sub filter found" );
145 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
146 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
149 if( p_spu->i_filter >= 10 )
151 msg_Dbg( p_spu, "can't add anymore filters" );
161 * Destroy the subpicture unit
163 * \param p_this the parent object which destroys the subpicture unit
165 void spu_Destroy( spu_t *p_spu )
169 vlc_object_detach( p_spu );
171 /* Destroy all remaining subpictures */
172 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
174 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
176 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
182 if( p_spu->p_blend->p_module )
183 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
185 vlc_object_detach( p_spu->p_blend );
186 vlc_object_destroy( p_spu->p_blend );
191 if( p_spu->p_text->p_module )
192 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
194 vlc_object_detach( p_spu->p_text );
195 vlc_object_destroy( p_spu->p_text );
200 if( p_spu->p_scale->p_module )
201 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
203 vlc_object_detach( p_spu->p_scale );
204 vlc_object_destroy( p_spu->p_scale );
207 while( p_spu->i_filter-- )
209 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
210 p_spu->pp_filter[p_spu->i_filter]->p_module );
211 free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
212 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
213 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
216 vlc_mutex_destroy( &p_spu->subpicture_lock );
217 vlc_object_destroy( p_spu );
221 * Attach/Detach the SPU from any input
223 * \param p_this the object in which to destroy the subpicture unit
224 * \param b_attach to select attach or detach
226 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
228 vlc_object_t *p_input;
230 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
231 if( !p_input ) return;
235 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
236 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
237 vlc_object_release( p_input );
241 /* Delete callback */
242 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
243 vlc_object_release( p_input );
248 * Create a subpicture region
250 * \param p_this vlc_object_t
251 * \param p_fmt the format that this subpicture region should have
253 static void RegionPictureRelease( picture_t *p_pic )
255 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
257 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
258 video_format_t *p_fmt )
260 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
261 memset( p_region, 0, sizeof(subpicture_region_t) );
262 p_region->p_next = 0;
263 p_region->p_cache = 0;
264 p_region->fmt = *p_fmt;
265 p_region->psz_text = 0;
266 p_region->p_style = NULL;
268 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
269 p_fmt->p_palette = p_region->fmt.p_palette =
270 malloc( sizeof(video_palette_t) );
271 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
273 p_region->picture.p_data_orig = NULL;
275 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
277 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
278 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
280 if( !p_region->picture.i_planes )
283 free( p_fmt->p_palette );
287 p_region->picture.pf_release = RegionPictureRelease;
293 * Make a subpicture region from an existing picture_t
295 * \param p_this vlc_object_t
296 * \param p_fmt the format that this subpicture region should have
297 * \param p_pic a pointer to the picture creating the region (not freed)
299 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
300 video_format_t *p_fmt,
303 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
304 memset( p_region, 0, sizeof(subpicture_region_t) );
305 p_region->p_next = 0;
306 p_region->p_cache = 0;
307 p_region->fmt = *p_fmt;
308 p_region->psz_text = 0;
309 p_region->p_style = NULL;
311 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
312 p_fmt->p_palette = p_region->fmt.p_palette =
313 malloc( sizeof(video_palette_t) );
314 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
316 memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
317 p_region->picture.pf_release = RegionPictureRelease;
323 * Destroy a subpicture region
325 * \param p_this vlc_object_t
326 * \param p_region the subpicture region to destroy
328 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
330 if( !p_region ) return;
331 if( p_region->picture.pf_release )
332 p_region->picture.pf_release( &p_region->picture );
333 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
334 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
339 * Display a subpicture
341 * Remove the reservation flag of a subpicture, which will cause it to be
343 * \param p_spu the subpicture unit object
344 * \param p_subpic the subpicture to display
346 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
348 /* Check if status is valid */
349 if( p_subpic->i_status != RESERVED_SUBPICTURE )
351 msg_Err( p_spu, "subpicture %p has invalid status #%d",
352 p_subpic, p_subpic->i_status );
355 /* Remove reservation flag */
356 p_subpic->i_status = READY_SUBPICTURE;
358 if( p_subpic->i_channel == DEFAULT_CHAN )
360 p_subpic->i_channel = 0xFFFF;
361 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
362 p_subpic->i_channel = DEFAULT_CHAN;
367 * Allocate a subpicture in the spu heap.
369 * This function create a reserved subpicture in the spu heap.
370 * A null pointer is returned if the function fails. This method provides an
371 * already allocated zone of memory in the spu data fields. It needs locking
372 * since several pictures can be created by several producers threads.
373 * \param p_spu the subpicture unit in which to create the subpicture
374 * \return NULL on error, a reserved subpicture otherwise
376 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
378 int i_subpic; /* subpicture index */
379 subpicture_t * p_subpic = NULL; /* first free subpicture */
382 vlc_mutex_lock( &p_spu->subpicture_lock );
385 * Look for an empty place
388 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
390 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
392 /* Subpicture is empty and ready for allocation */
393 p_subpic = &p_spu->p_subpicture[i_subpic];
394 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
399 /* If no free subpicture could be found */
400 if( p_subpic == NULL )
402 msg_Err( p_spu, "subpicture heap is full" );
403 vlc_mutex_unlock( &p_spu->subpicture_lock );
407 /* Copy subpicture information, set some default values */
408 memset( p_subpic, 0, sizeof(subpicture_t) );
409 p_subpic->i_status = RESERVED_SUBPICTURE;
410 p_subpic->b_absolute = VLC_TRUE;
411 p_subpic->b_pausable = VLC_FALSE;
412 p_subpic->b_fade = VLC_FALSE;
413 p_subpic->i_alpha = 0xFF;
414 p_subpic->p_region = 0;
415 p_subpic->pf_render = 0;
416 p_subpic->pf_destroy = 0;
418 vlc_mutex_unlock( &p_spu->subpicture_lock );
420 p_subpic->pf_create_region = __spu_CreateRegion;
421 p_subpic->pf_make_region = __spu_MakeRegion;
422 p_subpic->pf_destroy_region = __spu_DestroyRegion;
428 * Remove a subpicture from the heap
430 * This function frees a previously reserved subpicture.
431 * It is meant to be used when the construction of a picture aborted.
432 * This function does not need locking since reserved subpictures are ignored
435 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
438 vlc_mutex_lock( &p_spu->subpicture_lock );
440 /* There can be race conditions so we need to check the status */
441 if( p_subpic->i_status == FREE_SUBPICTURE )
443 vlc_mutex_unlock( &p_spu->subpicture_lock );
447 /* Check if status is valid */
448 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
449 && ( p_subpic->i_status != READY_SUBPICTURE ) )
451 msg_Err( p_spu, "subpicture %p has invalid status %d",
452 p_subpic, p_subpic->i_status );
455 while( p_subpic->p_region )
457 subpicture_region_t *p_region = p_subpic->p_region;
458 p_subpic->p_region = p_region->p_next;
459 spu_DestroyRegion( p_spu, p_region );
462 if( p_subpic->pf_destroy )
464 p_subpic->pf_destroy( p_subpic );
467 p_subpic->i_status = FREE_SUBPICTURE;
469 vlc_mutex_unlock( &p_spu->subpicture_lock );
472 /*****************************************************************************
473 * spu_RenderSubpictures: render a subpicture list
474 *****************************************************************************
475 * This function renders all sub picture units in the list.
476 *****************************************************************************/
477 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
478 picture_t *p_pic_dst, picture_t *p_pic_src,
479 subpicture_t *p_subpic,
480 int i_scale_width_orig, int i_scale_height_orig )
483 vlc_mutex_lock( &p_spu->subpicture_lock );
485 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
486 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
488 subpicture_region_t *p_region = p_subpic->p_region;
489 int i_scale_width, i_scale_height;
490 int i_subpic_x = p_subpic->i_x;
492 /* Load the blending module */
493 if( !p_spu->p_blend && p_region )
495 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
496 vlc_object_attach( p_spu->p_blend, p_spu );
497 p_spu->p_blend->fmt_out.video.i_x_offset =
498 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
499 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
500 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
501 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
503 p_spu->p_blend->p_module =
504 module_Need( p_spu->p_blend, "video blending", 0, 0 );
507 /* Load the text rendering module */
508 if( !p_spu->p_text && p_region && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
510 char *psz_modulename = NULL;
512 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
513 vlc_object_attach( p_spu->p_text, p_spu );
515 p_spu->p_text->fmt_out.video.i_width =
516 p_spu->p_text->fmt_out.video.i_visible_width =
518 p_spu->p_text->fmt_out.video.i_height =
519 p_spu->p_text->fmt_out.video.i_visible_height =
522 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
523 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
525 psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
526 if( psz_modulename && *psz_modulename )
528 p_spu->p_text->p_module =
529 module_Need( p_spu->p_text, "text renderer", psz_modulename, VLC_TRUE );
531 if( !p_spu->p_text->p_module )
533 p_spu->p_text->p_module =
534 module_Need( p_spu->p_text, "text renderer", 0, 0 );
536 if( psz_modulename ) free( psz_modulename );
540 if( p_subpic->i_original_picture_height > 0 &&
541 p_subpic->i_original_picture_width > 0 )
543 p_spu->p_text->fmt_out.video.i_width =
544 p_spu->p_text->fmt_out.video.i_visible_width =
545 p_subpic->i_original_picture_width;
546 p_spu->p_text->fmt_out.video.i_height =
547 p_spu->p_text->fmt_out.video.i_visible_height =
548 p_subpic->i_original_picture_height;
552 p_spu->p_text->fmt_out.video.i_width =
553 p_spu->p_text->fmt_out.video.i_visible_width =
555 p_spu->p_text->fmt_out.video.i_height =
556 p_spu->p_text->fmt_out.video.i_visible_height =
561 i_scale_width = i_scale_width_orig;
562 i_scale_height = i_scale_height_orig;
564 if( p_subpic->i_original_picture_height > 0 &&
565 p_subpic->i_original_picture_width > 0 )
567 i_scale_width = i_scale_width * p_fmt->i_width /
568 p_subpic->i_original_picture_width;
569 i_scale_height = i_scale_height * p_fmt->i_height /
570 p_subpic->i_original_picture_height;
572 else if( p_subpic->i_original_picture_height > 0 )
574 i_scale_height = i_scale_height * p_fmt->i_height /
575 p_subpic->i_original_picture_height;
576 i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height;
579 /* Set default subpicture aspect ratio */
580 if( p_region && p_region->fmt.i_aspect &&
581 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
583 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
584 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
587 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
589 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
590 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
593 /* Take care of the aspect ratio */
594 if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
595 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
597 i_scale_width = i_scale_width *
598 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
599 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
600 i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
603 /* Load the scaling module */
604 if( !p_spu->p_scale && (i_scale_width != 1000 ||
605 i_scale_height != 1000) )
607 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
608 vlc_object_attach( p_spu->p_scale, p_spu );
609 p_spu->p_scale->fmt_out.video.i_chroma =
610 p_spu->p_scale->fmt_in.video.i_chroma =
611 VLC_FOURCC('Y','U','V','P');
612 p_spu->p_scale->fmt_in.video.i_width =
613 p_spu->p_scale->fmt_in.video.i_height = 32;
614 p_spu->p_scale->fmt_out.video.i_width =
615 p_spu->p_scale->fmt_out.video.i_height = 16;
617 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
618 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
619 p_spu->p_scale->p_module =
620 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
623 while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
625 int i_fade_alpha = 255;
626 int i_x_offset = p_region->i_x + i_subpic_x;
627 int i_y_offset = p_region->i_y + p_subpic->i_y;
629 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
631 if( p_spu->p_text && p_spu->p_text->p_module &&
632 p_spu->p_text->pf_render_text )
634 p_region->i_align = p_subpic->i_flags;
635 p_spu->p_text->pf_render_text( p_spu->p_text,
636 p_region, p_region );
640 /* Force palette if requested */
641 if( p_spu->b_force_palette && VLC_FOURCC('Y','U','V','P') ==
642 p_region->fmt.i_chroma )
644 memcpy( p_region->fmt.p_palette->palette,
645 p_spu->palette, 16 );
648 /* Scale SPU if necessary */
649 if( p_region->p_cache )
651 if( i_scale_width * p_region->fmt.i_width / 1000 !=
652 p_region->p_cache->fmt.i_width ||
653 i_scale_height * p_region->fmt.i_height / 1000 !=
654 p_region->p_cache->fmt.i_height )
656 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
658 p_region->p_cache = 0;
662 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
663 p_spu->p_scale && !p_region->p_cache )
667 p_spu->p_scale->fmt_in.video = p_region->fmt;
668 p_spu->p_scale->fmt_out.video = p_region->fmt;
671 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
672 &p_spu->p_scale->fmt_out.video );
673 if( p_spu->p_scale->fmt_out.video.p_palette )
674 *p_spu->p_scale->fmt_out.video.p_palette =
675 *p_region->fmt.p_palette;
676 p_region->p_cache->p_next = p_region->p_next;
678 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
679 &p_region->picture );
681 p_spu->p_scale->fmt_out.video.i_width =
682 p_region->fmt.i_width * i_scale_width / 1000;
683 p_spu->p_scale->fmt_out.video.i_visible_width =
684 p_region->fmt.i_visible_width * i_scale_width / 1000;
685 p_spu->p_scale->fmt_out.video.i_height =
686 p_region->fmt.i_height * i_scale_height / 1000;
687 p_spu->p_scale->fmt_out.video.i_visible_height =
688 p_region->fmt.i_visible_height * i_scale_height / 1000;
689 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
690 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
691 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
693 p_pic = p_spu->p_scale->pf_video_filter(
694 p_spu->p_scale, &p_region->p_cache->picture );
697 picture_t p_pic_tmp = p_region->p_cache->picture;
698 p_region->p_cache->picture = *p_pic;
703 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
704 p_spu->p_scale && p_region->p_cache )
706 p_region = p_region->p_cache;
709 if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
711 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
714 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
716 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
719 if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
721 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
724 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
726 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
729 if( p_subpic->b_absolute )
731 i_x_offset = p_region->i_x +
732 i_subpic_x * i_scale_width / 1000;
733 i_y_offset = p_region->i_y +
734 p_subpic->i_y * i_scale_height / 1000;
738 if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
741 int i_low = i_y_offset - p_spu->i_margin;
742 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
744 /* crop extra margin to keep within bounds */
745 if( i_low < 0 ) i_diff = i_low;
746 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
747 i_y_offset -= ( p_spu->i_margin + i_diff );
750 p_spu->p_blend->fmt_in.video = p_region->fmt;
752 /* Force cropping if requested */
753 if( p_spu->b_force_crop )
755 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
756 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
757 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
758 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
759 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
761 /* Find the intersection */
762 if( i_crop_x + i_crop_width <= i_x_offset ||
763 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
764 i_crop_y + i_crop_height <= i_y_offset ||
765 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
767 /* No intersection */
768 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
772 int i_x, i_y, i_x_end, i_y_end;
773 i_x = __MAX( i_crop_x, i_x_offset );
774 i_y = __MAX( i_crop_y, i_y_offset );
775 i_x_end = __MIN( i_crop_x + i_crop_width,
776 i_x_offset + (int)p_fmt->i_visible_width );
777 i_y_end = __MIN( i_crop_y + i_crop_height,
778 i_y_offset + (int)p_fmt->i_visible_height );
780 p_fmt->i_x_offset = i_x - i_x_offset;
781 p_fmt->i_y_offset = i_y - i_y_offset;
782 p_fmt->i_visible_width = i_x_end - i_x;
783 p_fmt->i_visible_height = i_y_end - i_y;
790 /* Update the output picture size */
791 p_spu->p_blend->fmt_out.video.i_width =
792 p_spu->p_blend->fmt_out.video.i_visible_width =
794 p_spu->p_blend->fmt_out.video.i_height =
795 p_spu->p_blend->fmt_out.video.i_visible_height =
798 if( p_subpic->b_fade )
800 mtime_t i_fade_start = ( p_subpic->i_stop +
801 p_subpic->i_start ) / 2;
802 mtime_t i_now = mdate();
803 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
805 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
806 ( p_subpic->i_stop - i_fade_start );
810 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
811 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
812 i_fade_alpha * p_subpic->i_alpha / 255 );
814 p_region = p_region->p_next;
817 p_subpic = p_subpic->p_next;
820 vlc_mutex_unlock( &p_spu->subpicture_lock );
823 /*****************************************************************************
824 * spu_SortSubpictures: find the subpictures to display
825 *****************************************************************************
826 * This function parses all subpictures and decides which ones need to be
827 * displayed. This operation does not need lock, since only READY_SUBPICTURE
828 * are handled. If no picture has been selected, display_date will depend on
830 * We also check for ephemer DVD subpictures (subpictures that have
831 * to be removed if a newer one is available), which makes it a lot
832 * more difficult to guess if a subpicture has to be rendered or not.
833 *****************************************************************************/
834 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
835 vlc_bool_t b_paused )
837 int i_index, i_channel;
838 subpicture_t *p_subpic = NULL;
839 subpicture_t *p_ephemer;
840 mtime_t ephemer_date;
842 /* Run subpicture filters */
843 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
845 subpicture_t *p_subpic_filter;
846 p_subpic_filter = p_spu->pp_filter[i_index]->
847 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
848 if( p_subpic_filter )
850 spu_DisplaySubpicture( p_spu, p_subpic_filter );
854 /* We get an easily parsable chained list of subpictures which
855 * ends with NULL since p_subpic was initialized to NULL. */
856 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
861 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
863 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
864 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
869 display_date < p_spu->p_subpicture[i_index].i_start )
871 /* Too early, come back next monday */
875 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
876 ephemer_date = p_spu->p_subpicture[i_index].i_start;
878 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
879 ( !p_spu->p_subpicture[i_index].b_ephemer ||
880 p_spu->p_subpicture[i_index].i_stop >
881 p_spu->p_subpicture[i_index].i_start ) &&
882 !( p_spu->p_subpicture[i_index].b_pausable &&
885 /* Too late, destroy the subpic */
886 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
890 /* If this is an ephemer subpic, add it to our list */
891 if( p_spu->p_subpicture[i_index].b_ephemer )
893 p_spu->p_subpicture[i_index].p_next = p_ephemer;
894 p_ephemer = &p_spu->p_subpicture[i_index];
899 p_spu->p_subpicture[i_index].p_next = p_subpic;
900 p_subpic = &p_spu->p_subpicture[i_index];
903 /* If we found ephemer subpictures, check if they have to be
904 * displayed or destroyed */
905 while( p_ephemer != NULL )
907 subpicture_t *p_tmp = p_ephemer;
908 p_ephemer = p_ephemer->p_next;
910 if( p_tmp->i_start < ephemer_date )
912 /* Ephemer subpicture has lived too long */
913 spu_DestroySubpicture( p_spu, p_tmp );
917 /* Ephemer subpicture can still live a bit */
918 p_tmp->p_next = p_subpic;
927 /*****************************************************************************
928 * SpuClearChannel: clear an spu channel
929 *****************************************************************************
930 * This function destroys the subpictures which belong to the spu channel
931 * corresponding to i_channel_id.
932 *****************************************************************************/
933 static void SpuClearChannel( spu_t *p_spu, int i_channel )
935 int i_subpic; /* subpicture index */
936 subpicture_t *p_subpic = NULL; /* first free subpicture */
938 vlc_mutex_lock( &p_spu->subpicture_lock );
940 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
942 p_subpic = &p_spu->p_subpicture[i_subpic];
943 if( p_subpic->i_status == FREE_SUBPICTURE
944 || ( p_subpic->i_status != RESERVED_SUBPICTURE
945 && p_subpic->i_status != READY_SUBPICTURE ) )
950 if( p_subpic->i_channel == i_channel )
952 while( p_subpic->p_region )
954 subpicture_region_t *p_region = p_subpic->p_region;
955 p_subpic->p_region = p_region->p_next;
956 spu_DestroyRegion( p_spu, p_region );
959 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
960 p_subpic->i_status = FREE_SUBPICTURE;
964 vlc_mutex_unlock( &p_spu->subpicture_lock );
967 /*****************************************************************************
968 * spu_ControlDefault: default methods for the subpicture unit control.
969 *****************************************************************************/
970 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
976 case SPU_CHANNEL_REGISTER:
977 pi = (int *)va_arg( args, int * );
978 if( pi ) *pi = p_spu->i_channel++;
981 case SPU_CHANNEL_CLEAR:
982 i = (int)va_arg( args, int );
983 SpuClearChannel( p_spu, i );
987 msg_Dbg( p_spu, "control query not supported" );
994 /*****************************************************************************
995 * Object variables callbacks
996 *****************************************************************************/
998 /*****************************************************************************
999 * UpdateSPU: update subpicture settings
1000 *****************************************************************************
1001 * This function is called from CropCallback and at initialization time, to
1002 * retrieve crop information from the input.
1003 *****************************************************************************/
1004 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1008 p_spu->b_force_palette = VLC_FALSE;
1009 p_spu->b_force_crop = VLC_FALSE;
1011 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1013 p_spu->b_force_crop = VLC_TRUE;
1014 var_Get( p_object, "x-start", &val );
1015 p_spu->i_crop_x = val.i_int;
1016 var_Get( p_object, "y-start", &val );
1017 p_spu->i_crop_y = val.i_int;
1018 var_Get( p_object, "x-end", &val );
1019 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1020 var_Get( p_object, "y-end", &val );
1021 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1023 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1025 memcpy( p_spu->palette, val.p_address, 16 );
1026 p_spu->b_force_palette = VLC_TRUE;
1029 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1030 p_spu->i_crop_x, p_spu->i_crop_y,
1031 p_spu->i_crop_width, p_spu->i_crop_height,
1032 p_spu->b_force_palette );
1035 /*****************************************************************************
1036 * CropCallback: called when the highlight properties are changed
1037 *****************************************************************************
1038 * This callback is called from the input thread when we need cropping
1039 *****************************************************************************/
1040 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1041 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1043 UpdateSPU( (spu_t *)p_data, p_object );
1047 /*****************************************************************************
1048 * Buffers allocation callbacks for the filters
1049 *****************************************************************************/
1050 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1052 filter_owner_sys_t *p_sys = p_filter->p_owner;
1053 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1054 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1055 return p_subpicture;
1058 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1060 filter_owner_sys_t *p_sys = p_filter->p_owner;
1061 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1064 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1066 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1067 memset( p_subpic, 0, sizeof(subpicture_t) );
1068 p_subpic->b_absolute = VLC_TRUE;
1070 p_subpic->pf_create_region = __spu_CreateRegion;
1071 p_subpic->pf_make_region = __spu_MakeRegion;
1072 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1077 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1079 while( p_subpic->p_region )
1081 subpicture_region_t *p_region = p_subpic->p_region;
1082 p_subpic->p_region = p_region->p_next;
1083 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1089 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1091 picture_t *p_picture = malloc( sizeof(picture_t) );
1093 if( vout_AllocatePicture( p_filter, p_picture,
1094 p_filter->fmt_out.video.i_chroma,
1095 p_filter->fmt_out.video.i_width,
1096 p_filter->fmt_out.video.i_height,
1097 p_filter->fmt_out.video.i_aspect )
1104 p_picture->pf_release = RegionPictureRelease;
1109 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1111 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1112 if( p_pic ) free( p_pic );