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 )
101 char *psz_filter, *psz_filter_orig;
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 );
111 psz_filter = psz_filter_orig = val.psz_string;
112 while( psz_filter && *psz_filter )
114 char *psz_parser = strchr( psz_filter, ':' );
116 if( psz_parser ) *psz_parser++ = 0;
118 p_spu->pp_filter[p_spu->i_filter] =
119 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
120 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
121 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
122 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
123 p_spu->pp_filter[p_spu->i_filter]->p_module =
124 module_Need( p_spu->pp_filter[p_spu->i_filter],
125 "sub filter", psz_filter, 0 );
126 if( p_spu->pp_filter[p_spu->i_filter]->p_module )
128 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
129 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
130 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
131 p_sys->p_spu = p_spu;
136 msg_Dbg( p_spu, "no sub filter found" );
137 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
138 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
141 if( p_spu->i_filter >= 10 )
143 msg_Dbg( p_spu, "can't add anymore filters" );
146 psz_filter = psz_parser;
148 if( psz_filter_orig ) free( psz_filter_orig );
154 * Destroy the subpicture unit
156 * \param p_this the parent object which destroys the subpicture unit
158 void spu_Destroy( spu_t *p_spu )
162 vlc_object_detach( p_spu );
164 /* Destroy all remaining subpictures */
165 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
167 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
169 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
175 if( p_spu->p_blend->p_module )
176 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
178 vlc_object_detach( p_spu->p_blend );
179 vlc_object_destroy( p_spu->p_blend );
184 if( p_spu->p_text->p_module )
185 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
187 vlc_object_detach( p_spu->p_text );
188 vlc_object_destroy( p_spu->p_text );
193 if( p_spu->p_scale->p_module )
194 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
196 vlc_object_detach( p_spu->p_scale );
197 vlc_object_destroy( p_spu->p_scale );
200 while( p_spu->i_filter-- )
202 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
203 p_spu->pp_filter[p_spu->i_filter]->p_module );
204 free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
205 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
206 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
209 vlc_mutex_destroy( &p_spu->subpicture_lock );
210 vlc_object_destroy( p_spu );
214 * Attach/Detach the SPU from any input
216 * \param p_this the object in which to destroy the subpicture unit
217 * \param b_attach to select attach or detach
219 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
221 vlc_object_t *p_input;
223 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
224 if( !p_input ) return;
228 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
229 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
230 vlc_object_release( p_input );
234 /* Delete callback */
235 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
236 vlc_object_release( p_input );
241 * Create a subpicture region
243 * \param p_this vlc_object_t
244 * \param p_fmt the format that this subpicture region should have
246 static void RegionPictureRelease( picture_t *p_pic )
248 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
250 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
251 video_format_t *p_fmt )
253 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
254 memset( p_region, 0, sizeof(subpicture_region_t) );
255 p_region->p_next = 0;
256 p_region->p_cache = 0;
257 p_region->fmt = *p_fmt;
258 p_region->psz_text = 0;
259 p_region->p_style = NULL;
261 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
262 p_fmt->p_palette = p_region->fmt.p_palette =
263 malloc( sizeof(video_palette_t) );
264 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
266 p_region->picture.p_data_orig = NULL;
268 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
270 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
271 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
273 if( !p_region->picture.i_planes )
276 free( p_fmt->p_palette );
280 p_region->picture.pf_release = RegionPictureRelease;
286 * Make a subpicture region from an existing picture_t
288 * \param p_this vlc_object_t
289 * \param p_fmt the format that this subpicture region should have
290 * \param p_pic a pointer to the picture creating the region (not freed)
292 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
293 video_format_t *p_fmt,
296 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
297 memset( p_region, 0, sizeof(subpicture_region_t) );
298 p_region->p_next = 0;
299 p_region->p_cache = 0;
300 p_region->fmt = *p_fmt;
301 p_region->psz_text = 0;
302 p_region->p_style = NULL;
304 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
305 p_fmt->p_palette = p_region->fmt.p_palette =
306 malloc( sizeof(video_palette_t) );
307 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
309 memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
310 p_region->picture.pf_release = RegionPictureRelease;
316 * Destroy a subpicture region
318 * \param p_this vlc_object_t
319 * \param p_region the subpicture region to destroy
321 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
323 if( !p_region ) return;
324 if( p_region->picture.pf_release )
325 p_region->picture.pf_release( &p_region->picture );
326 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
327 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
332 * Display a subpicture
334 * Remove the reservation flag of a subpicture, which will cause it to be
336 * \param p_spu the subpicture unit object
337 * \param p_subpic the subpicture to display
339 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
341 /* Check if status is valid */
342 if( p_subpic->i_status != RESERVED_SUBPICTURE )
344 msg_Err( p_spu, "subpicture %p has invalid status #%d",
345 p_subpic, p_subpic->i_status );
348 /* Remove reservation flag */
349 p_subpic->i_status = READY_SUBPICTURE;
351 if( p_subpic->i_channel == DEFAULT_CHAN )
353 p_subpic->i_channel = 0xFFFF;
354 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
355 p_subpic->i_channel = DEFAULT_CHAN;
360 * Allocate a subpicture in the spu heap.
362 * This function create a reserved subpicture in the spu heap.
363 * A null pointer is returned if the function fails. This method provides an
364 * already allocated zone of memory in the spu data fields. It needs locking
365 * since several pictures can be created by several producers threads.
366 * \param p_spu the subpicture unit in which to create the subpicture
367 * \return NULL on error, a reserved subpicture otherwise
369 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
371 int i_subpic; /* subpicture index */
372 subpicture_t * p_subpic = NULL; /* first free subpicture */
375 vlc_mutex_lock( &p_spu->subpicture_lock );
378 * Look for an empty place
381 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
383 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
385 /* Subpicture is empty and ready for allocation */
386 p_subpic = &p_spu->p_subpicture[i_subpic];
387 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
392 /* If no free subpicture could be found */
393 if( p_subpic == NULL )
395 msg_Err( p_spu, "subpicture heap is full" );
396 vlc_mutex_unlock( &p_spu->subpicture_lock );
400 /* Copy subpicture information, set some default values */
401 memset( p_subpic, 0, sizeof(subpicture_t) );
402 p_subpic->i_status = RESERVED_SUBPICTURE;
403 p_subpic->b_absolute = VLC_TRUE;
404 p_subpic->b_fade = VLC_FALSE;
405 p_subpic->i_alpha = 0xFF;
406 p_subpic->p_region = 0;
407 p_subpic->pf_render = 0;
408 p_subpic->pf_destroy = 0;
410 vlc_mutex_unlock( &p_spu->subpicture_lock );
412 p_subpic->pf_create_region = __spu_CreateRegion;
413 p_subpic->pf_make_region = __spu_MakeRegion;
414 p_subpic->pf_destroy_region = __spu_DestroyRegion;
420 * Remove a subpicture from the heap
422 * This function frees a previously reserved subpicture.
423 * It is meant to be used when the construction of a picture aborted.
424 * This function does not need locking since reserved subpictures are ignored
427 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
430 vlc_mutex_lock( &p_spu->subpicture_lock );
432 /* There can be race conditions so we need to check the status */
433 if( p_subpic->i_status == FREE_SUBPICTURE )
435 vlc_mutex_unlock( &p_spu->subpicture_lock );
439 /* Check if status is valid */
440 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
441 && ( p_subpic->i_status != READY_SUBPICTURE ) )
443 msg_Err( p_spu, "subpicture %p has invalid status %d",
444 p_subpic, p_subpic->i_status );
447 while( p_subpic->p_region )
449 subpicture_region_t *p_region = p_subpic->p_region;
450 p_subpic->p_region = p_region->p_next;
451 spu_DestroyRegion( p_spu, p_region );
454 if( p_subpic->pf_destroy )
456 p_subpic->pf_destroy( p_subpic );
459 p_subpic->i_status = FREE_SUBPICTURE;
461 vlc_mutex_unlock( &p_spu->subpicture_lock );
464 /*****************************************************************************
465 * spu_RenderSubpictures: render a subpicture list
466 *****************************************************************************
467 * This function renders all sub picture units in the list.
468 *****************************************************************************/
469 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
470 picture_t *p_pic_dst, picture_t *p_pic_src,
471 subpicture_t *p_subpic,
472 int i_scale_width_orig, int i_scale_height_orig )
475 vlc_mutex_lock( &p_spu->subpicture_lock );
477 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
478 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
480 subpicture_region_t *p_region = p_subpic->p_region;
481 int i_scale_width, i_scale_height;
482 int i_subpic_x = p_subpic->i_x;
484 /* Load the blending module */
485 if( !p_spu->p_blend && p_region )
487 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
488 vlc_object_attach( p_spu->p_blend, p_spu );
489 p_spu->p_blend->fmt_out.video.i_x_offset =
490 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
491 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
492 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
493 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
495 p_spu->p_blend->p_module =
496 module_Need( p_spu->p_blend, "video blending", 0, 0 );
499 /* Load the text rendering module */
500 if( !p_spu->p_text && p_region && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
502 char *psz_modulename = NULL;
504 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
505 vlc_object_attach( p_spu->p_text, p_spu );
507 p_spu->p_text->fmt_out.video.i_width =
508 p_spu->p_text->fmt_out.video.i_visible_width =
510 p_spu->p_text->fmt_out.video.i_height =
511 p_spu->p_text->fmt_out.video.i_visible_height =
514 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
515 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
517 psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
518 if( psz_modulename && *psz_modulename )
520 p_spu->p_text->p_module =
521 module_Need( p_spu->p_text, "text renderer", psz_modulename, VLC_TRUE );
523 if( !p_spu->p_text->p_module )
525 p_spu->p_text->p_module =
526 module_Need( p_spu->p_text, "text renderer", 0, 0 );
528 if( psz_modulename ) free( psz_modulename );
532 if( p_subpic->i_original_picture_height > 0 &&
533 p_subpic->i_original_picture_width > 0 )
535 p_spu->p_text->fmt_out.video.i_width =
536 p_spu->p_text->fmt_out.video.i_visible_width =
537 p_subpic->i_original_picture_width;
538 p_spu->p_text->fmt_out.video.i_height =
539 p_spu->p_text->fmt_out.video.i_visible_height =
540 p_subpic->i_original_picture_height;
544 p_spu->p_text->fmt_out.video.i_width =
545 p_spu->p_text->fmt_out.video.i_visible_width =
547 p_spu->p_text->fmt_out.video.i_height =
548 p_spu->p_text->fmt_out.video.i_visible_height =
553 i_scale_width = i_scale_width_orig;
554 i_scale_height = i_scale_height_orig;
556 if( p_subpic->i_original_picture_height > 0 &&
557 p_subpic->i_original_picture_width > 0 )
559 i_scale_width = i_scale_width * p_fmt->i_width /
560 p_subpic->i_original_picture_width;
561 i_scale_height = i_scale_height * p_fmt->i_height /
562 p_subpic->i_original_picture_height;
564 else if( p_subpic->i_original_picture_height > 0 )
566 i_scale_height = i_scale_height * p_fmt->i_height /
567 p_subpic->i_original_picture_height;
568 i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height;
571 /* Set default subpicture aspect ratio */
572 if( p_region && p_region->fmt.i_aspect &&
573 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
575 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
576 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
579 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
581 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
582 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
585 /* Take care of the aspect ratio */
586 if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
587 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
589 i_scale_width = i_scale_width *
590 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
591 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
592 i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
595 /* Load the scaling module */
596 if( !p_spu->p_scale && (i_scale_width != 1000 ||
597 i_scale_height != 1000) )
599 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
600 vlc_object_attach( p_spu->p_scale, p_spu );
601 p_spu->p_scale->fmt_out.video.i_chroma =
602 p_spu->p_scale->fmt_in.video.i_chroma =
603 VLC_FOURCC('Y','U','V','P');
604 p_spu->p_scale->fmt_in.video.i_width =
605 p_spu->p_scale->fmt_in.video.i_height = 32;
606 p_spu->p_scale->fmt_out.video.i_width =
607 p_spu->p_scale->fmt_out.video.i_height = 16;
609 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
610 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
611 p_spu->p_scale->p_module =
612 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
615 while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
617 int i_fade_alpha = 255;
618 int i_x_offset = p_region->i_x + i_subpic_x + p_fmt->i_x_offset;
619 int i_y_offset = p_region->i_y + p_subpic->i_y + p_fmt->i_y_offset;
621 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
623 if( p_spu->p_text && p_spu->p_text->p_module &&
624 p_spu->p_text->pf_render_text )
626 p_region->i_align = p_subpic->i_flags;
627 p_spu->p_text->pf_render_text( p_spu->p_text,
628 p_region, p_region );
632 /* Force palette if requested */
633 if( p_spu->b_force_palette && VLC_FOURCC('Y','U','V','P') ==
634 p_region->fmt.i_chroma )
636 memcpy( p_region->fmt.p_palette->palette,
637 p_spu->palette, 16 );
640 /* Scale SPU if necessary */
641 if( p_region->p_cache )
643 if( i_scale_width * p_region->fmt.i_width / 1000 !=
644 p_region->p_cache->fmt.i_width ||
645 i_scale_height * p_region->fmt.i_height / 1000 !=
646 p_region->p_cache->fmt.i_height )
648 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
650 p_region->p_cache = 0;
654 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
655 p_spu->p_scale && !p_region->p_cache )
659 p_spu->p_scale->fmt_in.video = p_region->fmt;
660 p_spu->p_scale->fmt_out.video = p_region->fmt;
663 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
664 &p_spu->p_scale->fmt_out.video );
665 if( p_spu->p_scale->fmt_out.video.p_palette )
666 *p_spu->p_scale->fmt_out.video.p_palette =
667 *p_region->fmt.p_palette;
668 p_region->p_cache->p_next = p_region->p_next;
670 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
671 &p_region->picture );
673 p_spu->p_scale->fmt_out.video.i_width =
674 p_region->fmt.i_width * i_scale_width / 1000;
675 p_spu->p_scale->fmt_out.video.i_visible_width =
676 p_region->fmt.i_visible_width * i_scale_width / 1000;
677 p_spu->p_scale->fmt_out.video.i_height =
678 p_region->fmt.i_height * i_scale_height / 1000;
679 p_spu->p_scale->fmt_out.video.i_visible_height =
680 p_region->fmt.i_visible_height * i_scale_height / 1000;
681 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
682 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
683 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
685 p_pic = p_spu->p_scale->pf_video_filter(
686 p_spu->p_scale, &p_region->p_cache->picture );
689 picture_t p_pic_tmp = p_region->p_cache->picture;
690 p_region->p_cache->picture = *p_pic;
695 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
696 p_spu->p_scale && p_region->p_cache )
698 p_region = p_region->p_cache;
701 if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
703 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
706 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
708 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
711 if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
713 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
716 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
718 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
721 if( p_subpic->b_absolute )
723 i_x_offset = p_region->i_x +
724 i_subpic_x * i_scale_width / 1000;
725 i_y_offset = p_region->i_y +
726 p_subpic->i_y * i_scale_height / 1000;
730 if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
733 int i_low = i_y_offset - p_spu->i_margin;
734 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
736 /* crop extra margin to keep within bounds */
737 if( i_low < 0 ) i_diff = i_low;
738 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
739 i_y_offset -= ( p_spu->i_margin + i_diff );
742 p_spu->p_blend->fmt_in.video = p_region->fmt;
744 /* Force cropping if requested */
745 if( p_spu->b_force_crop )
747 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
748 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
749 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
750 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
751 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
753 /* Find the intersection */
754 if( i_crop_x + i_crop_width <= i_x_offset ||
755 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
756 i_crop_y + i_crop_height <= i_y_offset ||
757 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
759 /* No intersection */
760 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
764 int i_x, i_y, i_x_end, i_y_end;
765 i_x = __MAX( i_crop_x, i_x_offset );
766 i_y = __MAX( i_crop_y, i_y_offset );
767 i_x_end = __MIN( i_crop_x + i_crop_width,
768 i_x_offset + (int)p_fmt->i_visible_width );
769 i_y_end = __MIN( i_crop_y + i_crop_height,
770 i_y_offset + (int)p_fmt->i_visible_height );
772 p_fmt->i_x_offset = i_x - i_x_offset;
773 p_fmt->i_y_offset = i_y - i_y_offset;
774 p_fmt->i_visible_width = i_x_end - i_x;
775 p_fmt->i_visible_height = i_y_end - i_y;
782 /* Update the output picture size */
783 p_spu->p_blend->fmt_out.video.i_width =
784 p_spu->p_blend->fmt_out.video.i_visible_width =
786 p_spu->p_blend->fmt_out.video.i_height =
787 p_spu->p_blend->fmt_out.video.i_visible_height =
790 if( p_subpic->b_fade )
792 mtime_t i_fade_start = ( p_subpic->i_stop +
793 p_subpic->i_start ) / 2;
794 mtime_t i_now = mdate();
795 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
797 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
798 ( p_subpic->i_stop - i_fade_start );
802 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
803 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
804 i_fade_alpha * p_subpic->i_alpha / 255 );
806 p_region = p_region->p_next;
809 p_subpic = p_subpic->p_next;
812 vlc_mutex_unlock( &p_spu->subpicture_lock );
815 /*****************************************************************************
816 * spu_SortSubpictures: find the subpictures to display
817 *****************************************************************************
818 * This function parses all subpictures and decides which ones need to be
819 * displayed. This operation does not need lock, since only READY_SUBPICTURE
820 * are handled. If no picture has been selected, display_date will depend on
822 * We also check for ephemer DVD subpictures (subpictures that have
823 * to be removed if a newer one is available), which makes it a lot
824 * more difficult to guess if a subpicture has to be rendered or not.
825 *****************************************************************************/
826 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
828 int i_index, i_channel;
829 subpicture_t *p_subpic = NULL;
830 subpicture_t *p_ephemer;
831 mtime_t ephemer_date;
833 /* Run subpicture filters */
834 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
836 subpicture_t *p_subpic_filter;
837 p_subpic_filter = p_spu->pp_filter[i_index]->
838 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
839 if( p_subpic_filter )
841 spu_DisplaySubpicture( p_spu, p_subpic_filter );
845 /* We get an easily parsable chained list of subpictures which
846 * ends with NULL since p_subpic was initialized to NULL. */
847 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
852 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
854 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
855 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
860 display_date < p_spu->p_subpicture[i_index].i_start )
862 /* Too early, come back next monday */
866 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
867 ephemer_date = p_spu->p_subpicture[i_index].i_start;
869 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
870 ( !p_spu->p_subpicture[i_index].b_ephemer ||
871 p_spu->p_subpicture[i_index].i_stop >
872 p_spu->p_subpicture[i_index].i_start ) )
874 /* Too late, destroy the subpic */
875 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
879 /* If this is an ephemer subpic, add it to our list */
880 if( p_spu->p_subpicture[i_index].b_ephemer )
882 p_spu->p_subpicture[i_index].p_next = p_ephemer;
883 p_ephemer = &p_spu->p_subpicture[i_index];
888 p_spu->p_subpicture[i_index].p_next = p_subpic;
889 p_subpic = &p_spu->p_subpicture[i_index];
892 /* If we found ephemer subpictures, check if they have to be
893 * displayed or destroyed */
894 while( p_ephemer != NULL )
896 subpicture_t *p_tmp = p_ephemer;
897 p_ephemer = p_ephemer->p_next;
899 if( p_tmp->i_start < ephemer_date )
901 /* Ephemer subpicture has lived too long */
902 spu_DestroySubpicture( p_spu, p_tmp );
906 /* Ephemer subpicture can still live a bit */
907 p_tmp->p_next = p_subpic;
916 /*****************************************************************************
917 * SpuClearChannel: clear an spu channel
918 *****************************************************************************
919 * This function destroys the subpictures which belong to the spu channel
920 * corresponding to i_channel_id.
921 *****************************************************************************/
922 static void SpuClearChannel( spu_t *p_spu, int i_channel )
924 int i_subpic; /* subpicture index */
925 subpicture_t *p_subpic = NULL; /* first free subpicture */
927 vlc_mutex_lock( &p_spu->subpicture_lock );
929 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
931 p_subpic = &p_spu->p_subpicture[i_subpic];
932 if( p_subpic->i_status == FREE_SUBPICTURE
933 || ( p_subpic->i_status != RESERVED_SUBPICTURE
934 && p_subpic->i_status != READY_SUBPICTURE ) )
939 if( p_subpic->i_channel == i_channel )
941 while( p_subpic->p_region )
943 subpicture_region_t *p_region = p_subpic->p_region;
944 p_subpic->p_region = p_region->p_next;
945 spu_DestroyRegion( p_spu, p_region );
948 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
949 p_subpic->i_status = FREE_SUBPICTURE;
953 vlc_mutex_unlock( &p_spu->subpicture_lock );
956 /*****************************************************************************
957 * spu_ControlDefault: default methods for the subpicture unit control.
958 *****************************************************************************/
959 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
965 case SPU_CHANNEL_REGISTER:
966 pi = (int *)va_arg( args, int * );
967 if( pi ) *pi = p_spu->i_channel++;
968 msg_Dbg( p_spu, "Registering subpicture channel, ID: %i",
969 p_spu->i_channel - 1 );
972 case SPU_CHANNEL_CLEAR:
973 i = (int)va_arg( args, int );
974 SpuClearChannel( p_spu, i );
978 msg_Dbg( p_spu, "control query not supported" );
985 /*****************************************************************************
986 * Object variables callbacks
987 *****************************************************************************/
989 /*****************************************************************************
990 * UpdateSPU: update subpicture settings
991 *****************************************************************************
992 * This function is called from CropCallback and at initialization time, to
993 * retrieve crop information from the input.
994 *****************************************************************************/
995 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
999 p_spu->b_force_palette = VLC_FALSE;
1000 p_spu->b_force_crop = VLC_FALSE;
1002 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1004 p_spu->b_force_crop = VLC_TRUE;
1005 var_Get( p_object, "x-start", &val );
1006 p_spu->i_crop_x = val.i_int;
1007 var_Get( p_object, "y-start", &val );
1008 p_spu->i_crop_y = val.i_int;
1009 var_Get( p_object, "x-end", &val );
1010 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1011 var_Get( p_object, "y-end", &val );
1012 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1014 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1016 memcpy( p_spu->palette, val.p_address, 16 );
1017 p_spu->b_force_palette = VLC_TRUE;
1020 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1021 p_spu->i_crop_x, p_spu->i_crop_y,
1022 p_spu->i_crop_width, p_spu->i_crop_height,
1023 p_spu->b_force_palette );
1026 /*****************************************************************************
1027 * CropCallback: called when the highlight properties are changed
1028 *****************************************************************************
1029 * This callback is called from the input thread when we need cropping
1030 *****************************************************************************/
1031 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1032 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1034 UpdateSPU( (spu_t *)p_data, p_object );
1038 /*****************************************************************************
1039 * Buffers allocation callbacks for the filters
1040 *****************************************************************************/
1041 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1043 filter_owner_sys_t *p_sys = p_filter->p_owner;
1044 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1045 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1046 return p_subpicture;
1049 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1051 filter_owner_sys_t *p_sys = p_filter->p_owner;
1052 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1055 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1057 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1058 memset( p_subpic, 0, sizeof(subpicture_t) );
1059 p_subpic->b_absolute = VLC_TRUE;
1061 p_subpic->pf_create_region = __spu_CreateRegion;
1062 p_subpic->pf_make_region = __spu_MakeRegion;
1063 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1068 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1070 while( p_subpic->p_region )
1072 subpicture_region_t *p_region = p_subpic->p_region;
1073 p_subpic->p_region = p_region->p_next;
1074 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1080 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1082 picture_t *p_picture = malloc( sizeof(picture_t) );
1084 if( vout_AllocatePicture( p_filter, p_picture,
1085 p_filter->fmt_out.video.i_chroma,
1086 p_filter->fmt_out.video.i_width,
1087 p_filter->fmt_out.video.i_height,
1088 p_filter->fmt_out.video.i_aspect )
1095 p_picture->pf_release = RegionPictureRelease;
1100 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1102 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1103 if( p_pic ) free( p_pic );