1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* free() */
30 #include <stdio.h> /* sprintf() */
31 #include <string.h> /* strerror() */
35 #include <vlc_block.h>
36 #include <vlc_filter.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static void UpdateSPU ( spu_t *, vlc_object_t * );
43 static int CropCallback( vlc_object_t *, char const *,
44 vlc_value_t, vlc_value_t, void * );
46 static int spu_vaControlDefault( spu_t *, int, va_list );
48 static subpicture_t *sub_new_buffer( filter_t * );
49 static void sub_del_buffer( filter_t *, subpicture_t * );
50 static subpicture_t *spu_new_buffer( filter_t * );
51 static void spu_del_buffer( filter_t *, subpicture_t * );
52 static picture_t *spu_new_video_buffer( filter_t * );
53 static void spu_del_video_buffer( filter_t *, picture_t * );
55 struct filter_owner_sys_t
62 * Creates the subpicture unit
64 * \param p_this the parent object which creates the subpicture unit
66 spu_t *__spu_Create( vlc_object_t *p_this )
69 spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
71 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
73 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
76 p_spu->p_blend = NULL;
78 p_spu->p_scale = NULL;
80 p_spu->pf_control = spu_vaControlDefault;
82 /* Register the default subpicture channel */
85 vlc_mutex_init( p_this, &p_spu->subpicture_lock );
87 vlc_object_attach( p_spu, p_this );
93 * Initialise the subpicture unit
95 * \param p_spu the subpicture unit object
97 int spu_Init( spu_t *p_spu )
102 /* If the user requested a sub margin, we force the position. */
103 var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
104 var_Get( p_spu, "sub-margin", &val );
105 p_spu->i_margin = val.i_int;
107 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
108 var_Get( p_spu, "sub-filter", &val );
110 psz_parser = val.psz_string;
112 while( psz_parser && *psz_parser )
114 config_chain_t *p_cfg;
117 psz_parser = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
119 msg_Dbg( p_spu, "adding sub-filter: %s", psz_name );
121 p_spu->pp_filter[p_spu->i_filter] =
122 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
123 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
124 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
125 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
126 p_spu->pp_filter[p_spu->i_filter]->p_cfg = p_cfg;
127 p_spu->pp_filter[p_spu->i_filter]->p_module =
128 module_Need( p_spu->pp_filter[p_spu->i_filter],
129 "sub filter", psz_name, 0 );
130 if( p_spu->pp_filter[p_spu->i_filter]->p_module )
132 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
133 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
134 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
135 p_sys->p_spu = p_spu;
140 msg_Dbg( p_spu, "no sub filter found" );
141 config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
142 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
143 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
146 if( p_spu->i_filter >= 10 )
148 msg_Dbg( p_spu, "can't add anymore filters" );
153 if( val.psz_string ) free( val.psz_string );
159 * Destroy the subpicture unit
161 * \param p_this the parent object which destroys the subpicture unit
163 void spu_Destroy( spu_t *p_spu )
167 vlc_object_detach( p_spu );
169 /* Destroy all remaining subpictures */
170 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
172 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
174 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
180 if( p_spu->p_blend->p_module )
181 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
183 vlc_object_detach( p_spu->p_blend );
184 vlc_object_destroy( p_spu->p_blend );
189 if( p_spu->p_text->p_module )
190 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
192 vlc_object_detach( p_spu->p_text );
193 vlc_object_destroy( p_spu->p_text );
198 if( p_spu->p_scale->p_module )
199 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
201 vlc_object_detach( p_spu->p_scale );
202 vlc_object_destroy( p_spu->p_scale );
205 while( p_spu->i_filter-- )
207 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
208 p_spu->pp_filter[p_spu->i_filter]->p_module );
209 free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
210 config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
211 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
212 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
215 vlc_mutex_destroy( &p_spu->subpicture_lock );
216 vlc_object_destroy( p_spu );
220 * Attach/Detach the SPU from any input
222 * \param p_this the object in which to destroy the subpicture unit
223 * \param b_attach to select attach or detach
225 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
227 vlc_object_t *p_input;
229 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
230 if( !p_input ) return;
234 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
235 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
236 vlc_object_release( p_input );
240 /* Delete callback */
241 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
242 vlc_object_release( p_input );
247 * Create a subpicture region
249 * \param p_this vlc_object_t
250 * \param p_fmt the format that this subpicture region should have
252 static void RegionPictureRelease( picture_t *p_pic )
254 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
256 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
257 video_format_t *p_fmt )
259 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
260 memset( p_region, 0, sizeof(subpicture_region_t) );
261 p_region->p_next = 0;
262 p_region->p_cache = 0;
263 p_region->fmt = *p_fmt;
264 p_region->psz_text = 0;
265 p_region->p_style = NULL;
267 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
268 p_fmt->p_palette = p_region->fmt.p_palette =
269 malloc( sizeof(video_palette_t) );
270 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
272 p_region->picture.p_data_orig = NULL;
274 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
276 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
277 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
279 if( !p_region->picture.i_planes )
282 free( p_fmt->p_palette );
286 p_region->picture.pf_release = RegionPictureRelease;
292 * Make a subpicture region from an existing picture_t
294 * \param p_this vlc_object_t
295 * \param p_fmt the format that this subpicture region should have
296 * \param p_pic a pointer to the picture creating the region (not freed)
298 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
299 video_format_t *p_fmt,
302 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
303 memset( p_region, 0, sizeof(subpicture_region_t) );
304 p_region->p_next = 0;
305 p_region->p_cache = 0;
306 p_region->fmt = *p_fmt;
307 p_region->psz_text = 0;
308 p_region->p_style = NULL;
310 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
311 p_fmt->p_palette = p_region->fmt.p_palette =
312 malloc( sizeof(video_palette_t) );
313 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
315 memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
316 p_region->picture.pf_release = RegionPictureRelease;
322 * Destroy a subpicture region
324 * \param p_this vlc_object_t
325 * \param p_region the subpicture region to destroy
327 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
329 if( !p_region ) return;
330 if( p_region->picture.pf_release )
331 p_region->picture.pf_release( &p_region->picture );
332 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
333 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
335 if( p_region->psz_text )
336 free( p_region->psz_text );
337 //free( p_region->p_style ); FIXME --fenrir plugin does not allocate the memory for it. I think it might lead to segfault, video renderer can live longer than the decoder
342 * Display a subpicture
344 * Remove the reservation flag of a subpicture, which will cause it to be
346 * \param p_spu the subpicture unit object
347 * \param p_subpic the subpicture to display
349 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
351 /* Check if status is valid */
352 if( p_subpic->i_status != RESERVED_SUBPICTURE )
354 msg_Err( p_spu, "subpicture %p has invalid status #%d",
355 p_subpic, p_subpic->i_status );
358 /* Remove reservation flag */
359 p_subpic->i_status = READY_SUBPICTURE;
361 if( p_subpic->i_channel == DEFAULT_CHAN )
363 p_subpic->i_channel = 0xFFFF;
364 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
365 p_subpic->i_channel = DEFAULT_CHAN;
370 * Allocate a subpicture in the spu heap.
372 * This function create a reserved subpicture in the spu heap.
373 * A null pointer is returned if the function fails. This method provides an
374 * already allocated zone of memory in the spu data fields. It needs locking
375 * since several pictures can be created by several producers threads.
376 * \param p_spu the subpicture unit in which to create the subpicture
377 * \return NULL on error, a reserved subpicture otherwise
379 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
381 int i_subpic; /* subpicture index */
382 subpicture_t * p_subpic = NULL; /* first free subpicture */
385 vlc_mutex_lock( &p_spu->subpicture_lock );
388 * Look for an empty place
391 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
393 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
395 /* Subpicture is empty and ready for allocation */
396 p_subpic = &p_spu->p_subpicture[i_subpic];
397 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
402 /* If no free subpicture could be found */
403 if( p_subpic == NULL )
405 msg_Err( p_spu, "subpicture heap is full" );
406 vlc_mutex_unlock( &p_spu->subpicture_lock );
410 /* Copy subpicture information, set some default values */
411 memset( p_subpic, 0, sizeof(subpicture_t) );
412 p_subpic->i_status = RESERVED_SUBPICTURE;
413 p_subpic->b_absolute = VLC_TRUE;
414 p_subpic->b_pausable = VLC_FALSE;
415 p_subpic->b_fade = VLC_FALSE;
416 p_subpic->i_alpha = 0xFF;
417 p_subpic->p_region = 0;
418 p_subpic->pf_render = 0;
419 p_subpic->pf_destroy = 0;
421 vlc_mutex_unlock( &p_spu->subpicture_lock );
423 p_subpic->pf_create_region = __spu_CreateRegion;
424 p_subpic->pf_make_region = __spu_MakeRegion;
425 p_subpic->pf_destroy_region = __spu_DestroyRegion;
431 * Remove a subpicture from the heap
433 * This function frees a previously reserved subpicture.
434 * It is meant to be used when the construction of a picture aborted.
435 * This function does not need locking since reserved subpictures are ignored
438 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
441 vlc_mutex_lock( &p_spu->subpicture_lock );
443 /* There can be race conditions so we need to check the status */
444 if( p_subpic->i_status == FREE_SUBPICTURE )
446 vlc_mutex_unlock( &p_spu->subpicture_lock );
450 /* Check if status is valid */
451 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
452 && ( p_subpic->i_status != READY_SUBPICTURE ) )
454 msg_Err( p_spu, "subpicture %p has invalid status %d",
455 p_subpic, p_subpic->i_status );
458 while( p_subpic->p_region )
460 subpicture_region_t *p_region = p_subpic->p_region;
461 p_subpic->p_region = p_region->p_next;
462 spu_DestroyRegion( p_spu, p_region );
465 if( p_subpic->pf_destroy )
467 p_subpic->pf_destroy( p_subpic );
470 p_subpic->i_status = FREE_SUBPICTURE;
472 vlc_mutex_unlock( &p_spu->subpicture_lock );
475 /*****************************************************************************
476 * spu_RenderSubpictures: render a subpicture list
477 *****************************************************************************
478 * This function renders all sub picture units in the list.
479 *****************************************************************************/
480 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
481 picture_t *p_pic_dst, picture_t *p_pic_src,
482 subpicture_t *p_subpic,
483 int i_scale_width_orig, int i_scale_height_orig )
486 vlc_mutex_lock( &p_spu->subpicture_lock );
488 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
489 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
491 subpicture_region_t *p_region = p_subpic->p_region;
492 int i_scale_width, i_scale_height;
493 int i_subpic_x = p_subpic->i_x;
495 /* Load the blending module */
496 if( !p_spu->p_blend && p_region )
498 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
499 vlc_object_attach( p_spu->p_blend, p_spu );
500 p_spu->p_blend->fmt_out.video.i_x_offset =
501 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
502 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
503 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
504 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
505 /* XXX: We'll also be using it for YUVA and RGBA blending ... */
507 p_spu->p_blend->p_module =
508 module_Need( p_spu->p_blend, "video blending", 0, 0 );
511 /* Load the text rendering module */
512 if( !p_spu->p_text && p_region && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
514 char *psz_modulename = NULL;
516 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
517 vlc_object_attach( p_spu->p_text, p_spu );
519 p_spu->p_text->fmt_out.video.i_width =
520 p_spu->p_text->fmt_out.video.i_visible_width =
522 p_spu->p_text->fmt_out.video.i_height =
523 p_spu->p_text->fmt_out.video.i_visible_height =
526 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
527 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
529 psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
530 if( psz_modulename && *psz_modulename )
532 p_spu->p_text->p_module =
533 module_Need( p_spu->p_text, "text renderer", psz_modulename, VLC_TRUE );
535 if( !p_spu->p_text->p_module )
537 p_spu->p_text->p_module =
538 module_Need( p_spu->p_text, "text renderer", 0, 0 );
540 if( psz_modulename ) free( psz_modulename );
544 if( p_subpic->i_original_picture_height > 0 &&
545 p_subpic->i_original_picture_width > 0 )
547 p_spu->p_text->fmt_out.video.i_width =
548 p_spu->p_text->fmt_out.video.i_visible_width =
549 p_subpic->i_original_picture_width;
550 p_spu->p_text->fmt_out.video.i_height =
551 p_spu->p_text->fmt_out.video.i_visible_height =
552 p_subpic->i_original_picture_height;
556 p_spu->p_text->fmt_out.video.i_width =
557 p_spu->p_text->fmt_out.video.i_visible_width =
559 p_spu->p_text->fmt_out.video.i_height =
560 p_spu->p_text->fmt_out.video.i_visible_height =
565 i_scale_width = i_scale_width_orig;
566 i_scale_height = i_scale_height_orig;
568 if( p_subpic->i_original_picture_height > 0 &&
569 p_subpic->i_original_picture_width > 0 )
571 i_scale_width = i_scale_width * p_fmt->i_width /
572 p_subpic->i_original_picture_width;
573 i_scale_height = i_scale_height * p_fmt->i_height /
574 p_subpic->i_original_picture_height;
576 else if( p_subpic->i_original_picture_height > 0 )
578 i_scale_height = i_scale_height * p_fmt->i_height /
579 p_subpic->i_original_picture_height;
580 i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height;
583 /* Set default subpicture aspect ratio */
584 if( p_region && p_region->fmt.i_aspect &&
585 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
587 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
588 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
591 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
593 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
594 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
597 /* Take care of the aspect ratio */
598 if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
599 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
601 i_scale_width = i_scale_width *
602 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
603 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
604 i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
607 /* Load the scaling module */
608 if( !p_spu->p_scale && (i_scale_width != 1000 ||
609 i_scale_height != 1000) )
611 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
612 vlc_object_attach( p_spu->p_scale, p_spu );
613 p_spu->p_scale->fmt_out.video.i_chroma =
614 p_spu->p_scale->fmt_in.video.i_chroma =
615 VLC_FOURCC('Y','U','V','P');
616 /* XXX: We'll also be using it for YUVA and RGBA blending ... */
617 p_spu->p_scale->fmt_in.video.i_width =
618 p_spu->p_scale->fmt_in.video.i_height = 32;
619 p_spu->p_scale->fmt_out.video.i_width =
620 p_spu->p_scale->fmt_out.video.i_height = 16;
622 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
623 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
624 p_spu->p_scale->p_module =
625 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
628 while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
630 int i_fade_alpha = 255;
631 int i_x_offset = p_region->i_x + i_subpic_x;
632 int i_y_offset = p_region->i_y + p_subpic->i_y;
634 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
636 if( p_spu->p_text && p_spu->p_text->p_module )
638 p_region->i_align = p_subpic->i_flags;
640 if( p_spu->p_text->pf_render_html && p_region->psz_html )
642 p_spu->p_text->pf_render_html( p_spu->p_text,
643 p_region, p_region );
645 else if( p_spu->p_text->pf_render_text )
647 p_spu->p_text->pf_render_text( p_spu->p_text,
648 p_region, p_region );
653 /* Force palette if requested */
654 if( p_spu->b_force_palette && VLC_FOURCC('Y','U','V','P') ==
655 p_region->fmt.i_chroma )
657 memcpy( p_region->fmt.p_palette->palette,
658 p_spu->palette, 16 );
661 /* Scale SPU if necessary */
662 if( p_region->p_cache )
664 if( i_scale_width * p_region->fmt.i_width / 1000 !=
665 p_region->p_cache->fmt.i_width ||
666 i_scale_height * p_region->fmt.i_height / 1000 !=
667 p_region->p_cache->fmt.i_height )
669 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
671 p_region->p_cache = 0;
675 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
676 p_spu->p_scale && !p_region->p_cache )
680 p_spu->p_scale->fmt_in.video = p_region->fmt;
681 p_spu->p_scale->fmt_out.video = p_region->fmt;
684 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
685 &p_spu->p_scale->fmt_out.video );
686 if( p_spu->p_scale->fmt_out.video.p_palette )
687 *p_spu->p_scale->fmt_out.video.p_palette =
688 *p_region->fmt.p_palette;
689 p_region->p_cache->p_next = p_region->p_next;
691 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
692 &p_region->picture );
694 p_spu->p_scale->fmt_out.video.i_width =
695 p_region->fmt.i_width * i_scale_width / 1000;
696 p_spu->p_scale->fmt_out.video.i_visible_width =
697 p_region->fmt.i_visible_width * i_scale_width / 1000;
698 p_spu->p_scale->fmt_out.video.i_height =
699 p_region->fmt.i_height * i_scale_height / 1000;
700 p_spu->p_scale->fmt_out.video.i_visible_height =
701 p_region->fmt.i_visible_height * i_scale_height / 1000;
702 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
703 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
704 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
706 p_pic = p_spu->p_scale->pf_video_filter(
707 p_spu->p_scale, &p_region->p_cache->picture );
710 picture_t p_pic_tmp = p_region->p_cache->picture;
711 p_region->p_cache->picture = *p_pic;
716 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
717 p_spu->p_scale && p_region->p_cache )
719 p_region = p_region->p_cache;
722 if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
724 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
727 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
729 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
732 if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
734 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
737 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
739 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
742 if( p_subpic->b_absolute )
744 i_x_offset = p_region->i_x +
745 i_subpic_x * i_scale_width / 1000;
746 i_y_offset = p_region->i_y +
747 p_subpic->i_y * i_scale_height / 1000;
751 i_x_offset = __MAX( i_x_offset, 0 );
752 i_y_offset = __MAX( i_y_offset, 0 );
754 if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
757 int i_low = i_y_offset - p_spu->i_margin;
758 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
760 /* crop extra margin to keep within bounds */
761 if( i_low < 0 ) i_diff = i_low;
762 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
763 i_y_offset -= ( p_spu->i_margin + i_diff );
766 p_spu->p_blend->fmt_in.video = p_region->fmt;
768 /* Force cropping if requested */
769 if( p_spu->b_force_crop )
771 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
772 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
773 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
774 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
775 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
777 /* Find the intersection */
778 if( i_crop_x + i_crop_width <= i_x_offset ||
779 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
780 i_crop_y + i_crop_height <= i_y_offset ||
781 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
783 /* No intersection */
784 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
788 int i_x, i_y, i_x_end, i_y_end;
789 i_x = __MAX( i_crop_x, i_x_offset );
790 i_y = __MAX( i_crop_y, i_y_offset );
791 i_x_end = __MIN( i_crop_x + i_crop_width,
792 i_x_offset + (int)p_fmt->i_visible_width );
793 i_y_end = __MIN( i_crop_y + i_crop_height,
794 i_y_offset + (int)p_fmt->i_visible_height );
796 p_fmt->i_x_offset = i_x - i_x_offset;
797 p_fmt->i_y_offset = i_y - i_y_offset;
798 p_fmt->i_visible_width = i_x_end - i_x;
799 p_fmt->i_visible_height = i_y_end - i_y;
806 /* Update the output picture size */
807 p_spu->p_blend->fmt_out.video.i_width =
808 p_spu->p_blend->fmt_out.video.i_visible_width =
810 p_spu->p_blend->fmt_out.video.i_height =
811 p_spu->p_blend->fmt_out.video.i_visible_height =
814 if( p_subpic->b_fade )
816 mtime_t i_fade_start = ( p_subpic->i_stop +
817 p_subpic->i_start ) / 2;
818 mtime_t i_now = mdate();
819 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
821 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
822 ( p_subpic->i_stop - i_fade_start );
826 i_x_offset = __MAX( i_x_offset, 0 );
827 i_y_offset = __MAX( i_y_offset, 0 );
829 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
830 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
831 i_fade_alpha * p_subpic->i_alpha / 255 );
833 p_region = p_region->p_next;
836 p_subpic = p_subpic->p_next;
839 vlc_mutex_unlock( &p_spu->subpicture_lock );
842 /*****************************************************************************
843 * spu_SortSubpictures: find the subpictures to display
844 *****************************************************************************
845 * This function parses all subpictures and decides which ones need to be
846 * displayed. This operation does not need lock, since only READY_SUBPICTURE
847 * are handled. If no picture has been selected, display_date will depend on
849 * We also check for ephemer DVD subpictures (subpictures that have
850 * to be removed if a newer one is available), which makes it a lot
851 * more difficult to guess if a subpicture has to be rendered or not.
852 *****************************************************************************/
853 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
854 vlc_bool_t b_paused )
856 int i_index, i_channel;
857 subpicture_t *p_subpic = NULL;
858 subpicture_t *p_ephemer;
859 mtime_t ephemer_date;
861 /* Run subpicture filters */
862 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
864 subpicture_t *p_subpic_filter;
865 p_subpic_filter = p_spu->pp_filter[i_index]->
866 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
867 if( p_subpic_filter )
869 spu_DisplaySubpicture( p_spu, p_subpic_filter );
873 /* We get an easily parsable chained list of subpictures which
874 * ends with NULL since p_subpic was initialized to NULL. */
875 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
880 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
882 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
883 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
888 display_date < p_spu->p_subpicture[i_index].i_start )
890 /* Too early, come back next monday */
894 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
895 ephemer_date = p_spu->p_subpicture[i_index].i_start;
897 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
898 ( !p_spu->p_subpicture[i_index].b_ephemer ||
899 p_spu->p_subpicture[i_index].i_stop >
900 p_spu->p_subpicture[i_index].i_start ) &&
901 !( p_spu->p_subpicture[i_index].b_pausable &&
904 /* Too late, destroy the subpic */
905 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
909 /* If this is an ephemer subpic, add it to our list */
910 if( p_spu->p_subpicture[i_index].b_ephemer )
912 p_spu->p_subpicture[i_index].p_next = p_ephemer;
913 p_ephemer = &p_spu->p_subpicture[i_index];
918 p_spu->p_subpicture[i_index].p_next = p_subpic;
919 p_subpic = &p_spu->p_subpicture[i_index];
922 /* If we found ephemer subpictures, check if they have to be
923 * displayed or destroyed */
924 while( p_ephemer != NULL )
926 subpicture_t *p_tmp = p_ephemer;
927 p_ephemer = p_ephemer->p_next;
929 if( p_tmp->i_start < ephemer_date )
931 /* Ephemer subpicture has lived too long */
932 spu_DestroySubpicture( p_spu, p_tmp );
936 /* Ephemer subpicture can still live a bit */
937 p_tmp->p_next = p_subpic;
946 /*****************************************************************************
947 * SpuClearChannel: clear an spu channel
948 *****************************************************************************
949 * This function destroys the subpictures which belong to the spu channel
950 * corresponding to i_channel_id.
951 *****************************************************************************/
952 static void SpuClearChannel( spu_t *p_spu, int i_channel )
954 int i_subpic; /* subpicture index */
955 subpicture_t *p_subpic = NULL; /* first free subpicture */
957 vlc_mutex_lock( &p_spu->subpicture_lock );
959 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
961 p_subpic = &p_spu->p_subpicture[i_subpic];
962 if( p_subpic->i_status == FREE_SUBPICTURE
963 || ( p_subpic->i_status != RESERVED_SUBPICTURE
964 && p_subpic->i_status != READY_SUBPICTURE ) )
969 if( p_subpic->i_channel == i_channel )
971 while( p_subpic->p_region )
973 subpicture_region_t *p_region = p_subpic->p_region;
974 p_subpic->p_region = p_region->p_next;
975 spu_DestroyRegion( p_spu, p_region );
978 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
979 p_subpic->i_status = FREE_SUBPICTURE;
983 vlc_mutex_unlock( &p_spu->subpicture_lock );
986 /*****************************************************************************
987 * spu_ControlDefault: default methods for the subpicture unit control.
988 *****************************************************************************/
989 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
995 case SPU_CHANNEL_REGISTER:
996 pi = (int *)va_arg( args, int * );
997 if( pi ) *pi = p_spu->i_channel++;
1000 case SPU_CHANNEL_CLEAR:
1001 i = (int)va_arg( args, int );
1002 SpuClearChannel( p_spu, i );
1006 msg_Dbg( p_spu, "control query not supported" );
1007 return VLC_EGENERIC;
1013 /*****************************************************************************
1014 * Object variables callbacks
1015 *****************************************************************************/
1017 /*****************************************************************************
1018 * UpdateSPU: update subpicture settings
1019 *****************************************************************************
1020 * This function is called from CropCallback and at initialization time, to
1021 * retrieve crop information from the input.
1022 *****************************************************************************/
1023 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1027 p_spu->b_force_palette = VLC_FALSE;
1028 p_spu->b_force_crop = VLC_FALSE;
1030 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1032 p_spu->b_force_crop = VLC_TRUE;
1033 var_Get( p_object, "x-start", &val );
1034 p_spu->i_crop_x = val.i_int;
1035 var_Get( p_object, "y-start", &val );
1036 p_spu->i_crop_y = val.i_int;
1037 var_Get( p_object, "x-end", &val );
1038 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1039 var_Get( p_object, "y-end", &val );
1040 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1042 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1044 memcpy( p_spu->palette, val.p_address, 16 );
1045 p_spu->b_force_palette = VLC_TRUE;
1048 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1049 p_spu->i_crop_x, p_spu->i_crop_y,
1050 p_spu->i_crop_width, p_spu->i_crop_height,
1051 p_spu->b_force_palette );
1054 /*****************************************************************************
1055 * CropCallback: called when the highlight properties are changed
1056 *****************************************************************************
1057 * This callback is called from the input thread when we need cropping
1058 *****************************************************************************/
1059 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1060 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1062 UpdateSPU( (spu_t *)p_data, p_object );
1066 /*****************************************************************************
1067 * Buffers allocation callbacks for the filters
1068 *****************************************************************************/
1069 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1071 filter_owner_sys_t *p_sys = p_filter->p_owner;
1072 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1073 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1074 return p_subpicture;
1077 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1079 filter_owner_sys_t *p_sys = p_filter->p_owner;
1080 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1083 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1085 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1086 memset( p_subpic, 0, sizeof(subpicture_t) );
1087 p_subpic->b_absolute = VLC_TRUE;
1089 p_subpic->pf_create_region = __spu_CreateRegion;
1090 p_subpic->pf_make_region = __spu_MakeRegion;
1091 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1096 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1098 while( p_subpic->p_region )
1100 subpicture_region_t *p_region = p_subpic->p_region;
1101 p_subpic->p_region = p_region->p_next;
1102 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1108 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1110 picture_t *p_picture = malloc( sizeof(picture_t) );
1112 if( vout_AllocatePicture( p_filter, p_picture,
1113 p_filter->fmt_out.video.i_chroma,
1114 p_filter->fmt_out.video.i_width,
1115 p_filter->fmt_out.video.i_height,
1116 p_filter->fmt_out.video.i_aspect )
1123 p_picture->pf_release = RegionPictureRelease;
1128 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1130 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1131 if( p_pic ) free( p_pic );