1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2007 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 static int spu_ParseChain( spu_t * );
56 static void spu_DeleteChain( spu_t * );
57 static int SubFilterCallback( vlc_object_t *, char const *,
58 vlc_value_t, vlc_value_t, void * );
60 struct filter_owner_sys_t
67 * Creates the subpicture unit
69 * \param p_this the parent object which creates the subpicture unit
71 spu_t *__spu_Create( vlc_object_t *p_this )
74 spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
76 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
78 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
81 p_spu->p_blend = NULL;
83 p_spu->p_scale = NULL;
85 p_spu->pf_control = spu_vaControlDefault;
87 /* Register the default subpicture channel */
90 vlc_mutex_init( p_this, &p_spu->subpicture_lock );
92 vlc_object_attach( p_spu, p_this );
98 * Initialise the subpicture unit
100 * \param p_spu the subpicture unit object
102 int spu_Init( spu_t *p_spu )
106 /* If the user requested a sub margin, we force the position. */
107 var_Create( p_spu, "sub-margin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
108 var_Get( p_spu, "sub-margin", &val );
109 p_spu->i_margin = val.i_int;
111 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
112 var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
114 spu_ParseChain( p_spu );
119 int spu_ParseChain( spu_t *p_spu )
123 var_Get( p_spu, "sub-filter", &val );
124 psz_parser = val.psz_string;
126 while( psz_parser && *psz_parser )
128 config_chain_t *p_cfg;
131 psz_parser = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
133 msg_Dbg( p_spu, "adding sub-filter: %s", psz_name );
135 p_spu->pp_filter[p_spu->i_filter] =
136 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
137 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
138 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
139 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
140 p_spu->pp_filter[p_spu->i_filter]->p_cfg = p_cfg;
141 p_spu->pp_filter[p_spu->i_filter]->p_module =
142 module_Need( p_spu->pp_filter[p_spu->i_filter],
143 "sub filter", psz_name, VLC_TRUE );
144 if( p_spu->pp_filter[p_spu->i_filter]->p_module )
146 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
147 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
148 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
149 p_sys->p_spu = p_spu;
154 msg_Dbg( p_spu, "no sub filter found" );
155 config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
156 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
157 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
160 if( p_spu->i_filter >= 10 )
162 msg_Dbg( p_spu, "can't add anymore filters" );
167 if( val.psz_string ) free( val.psz_string );
173 * Destroy the subpicture unit
175 * \param p_this the parent object which destroys the subpicture unit
177 void spu_Destroy( spu_t *p_spu )
181 vlc_object_detach( p_spu );
183 /* Destroy all remaining subpictures */
184 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
186 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
188 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
194 if( p_spu->p_blend->p_module )
195 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
197 vlc_object_detach( p_spu->p_blend );
198 vlc_object_destroy( p_spu->p_blend );
203 if( p_spu->p_text->p_module )
204 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
206 vlc_object_detach( p_spu->p_text );
207 vlc_object_destroy( p_spu->p_text );
212 if( p_spu->p_scale->p_module )
213 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
215 vlc_object_detach( p_spu->p_scale );
216 vlc_object_destroy( p_spu->p_scale );
219 spu_DeleteChain( p_spu );
221 vlc_mutex_destroy( &p_spu->subpicture_lock );
222 vlc_object_destroy( p_spu );
225 static void spu_DeleteChain( spu_t *p_spu )
227 if( p_spu->i_filter )
228 while( p_spu->i_filter-- )
230 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
231 p_spu->pp_filter[p_spu->i_filter]->p_module );
232 free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
233 config_ChainDestroy( p_spu->pp_filter[p_spu->i_filter]->p_cfg );
234 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
235 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
240 * Attach/Detach the SPU from any input
242 * \param p_this the object in which to destroy the subpicture unit
243 * \param b_attach to select attach or detach
245 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
247 vlc_object_t *p_input;
249 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
250 if( !p_input ) return;
254 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
255 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
256 vlc_object_release( p_input );
260 /* Delete callback */
261 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
262 vlc_object_release( p_input );
267 * Create a subpicture region
269 * \param p_this vlc_object_t
270 * \param p_fmt the format that this subpicture region should have
272 static void RegionPictureRelease( picture_t *p_pic )
274 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
276 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
277 video_format_t *p_fmt )
279 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
280 memset( p_region, 0, sizeof(subpicture_region_t) );
281 p_region->p_next = 0;
282 p_region->p_cache = 0;
283 p_region->fmt = *p_fmt;
284 p_region->psz_text = 0;
285 p_region->p_style = NULL;
287 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
288 p_fmt->p_palette = p_region->fmt.p_palette =
289 malloc( sizeof(video_palette_t) );
290 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
292 p_region->picture.p_data_orig = NULL;
294 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
296 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
297 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
299 if( !p_region->picture.i_planes )
302 free( p_fmt->p_palette );
306 p_region->picture.pf_release = RegionPictureRelease;
312 * Make a subpicture region from an existing picture_t
314 * \param p_this vlc_object_t
315 * \param p_fmt the format that this subpicture region should have
316 * \param p_pic a pointer to the picture creating the region (not freed)
318 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
319 video_format_t *p_fmt,
322 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
324 memset( p_region, 0, sizeof(subpicture_region_t) );
325 p_region->p_next = 0;
326 p_region->p_cache = 0;
327 p_region->fmt = *p_fmt;
328 p_region->psz_text = 0;
329 p_region->p_style = NULL;
331 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
332 p_fmt->p_palette = p_region->fmt.p_palette =
333 malloc( sizeof(video_palette_t) );
334 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
336 memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
337 p_region->picture.pf_release = RegionPictureRelease;
343 * Destroy a subpicture region
345 * \param p_this vlc_object_t
346 * \param p_region the subpicture region to destroy
348 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
350 if( !p_region ) return;
351 if( p_region->picture.pf_release )
352 p_region->picture.pf_release( &p_region->picture );
353 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
354 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
356 if( p_region->psz_text )
357 free( p_region->psz_text );
358 if( p_region->psz_html )
359 free( p_region->psz_html );
360 //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
365 * Display a subpicture
367 * Remove the reservation flag of a subpicture, which will cause it to be
369 * \param p_spu the subpicture unit object
370 * \param p_subpic the subpicture to display
372 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
374 /* Check if status is valid */
375 if( p_subpic->i_status != RESERVED_SUBPICTURE )
377 msg_Err( p_spu, "subpicture %p has invalid status #%d",
378 p_subpic, p_subpic->i_status );
381 /* Remove reservation flag */
382 p_subpic->i_status = READY_SUBPICTURE;
384 if( p_subpic->i_channel == DEFAULT_CHAN )
386 p_subpic->i_channel = 0xFFFF;
387 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
388 p_subpic->i_channel = DEFAULT_CHAN;
393 * Allocate a subpicture in the spu heap.
395 * This function create a reserved subpicture in the spu heap.
396 * A null pointer is returned if the function fails. This method provides an
397 * already allocated zone of memory in the spu data fields. It needs locking
398 * since several pictures can be created by several producers threads.
399 * \param p_spu the subpicture unit in which to create the subpicture
400 * \return NULL on error, a reserved subpicture otherwise
402 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
404 int i_subpic; /* subpicture index */
405 subpicture_t * p_subpic = NULL; /* first free subpicture */
408 vlc_mutex_lock( &p_spu->subpicture_lock );
411 * Look for an empty place
414 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
416 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
418 /* Subpicture is empty and ready for allocation */
419 p_subpic = &p_spu->p_subpicture[i_subpic];
420 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
425 /* If no free subpicture could be found */
426 if( p_subpic == NULL )
428 msg_Err( p_spu, "subpicture heap is full" );
429 vlc_mutex_unlock( &p_spu->subpicture_lock );
433 /* Copy subpicture information, set some default values */
434 memset( p_subpic, 0, sizeof(subpicture_t) );
435 p_subpic->i_status = RESERVED_SUBPICTURE;
436 p_subpic->b_absolute = VLC_TRUE;
437 p_subpic->b_pausable = VLC_FALSE;
438 p_subpic->b_fade = VLC_FALSE;
439 p_subpic->i_alpha = 0xFF;
440 p_subpic->p_region = 0;
441 p_subpic->pf_render = 0;
442 p_subpic->pf_destroy = 0;
444 vlc_mutex_unlock( &p_spu->subpicture_lock );
446 p_subpic->pf_create_region = __spu_CreateRegion;
447 p_subpic->pf_make_region = __spu_MakeRegion;
448 p_subpic->pf_destroy_region = __spu_DestroyRegion;
454 * Remove a subpicture from the heap
456 * This function frees a previously reserved subpicture.
457 * It is meant to be used when the construction of a picture aborted.
458 * This function does not need locking since reserved subpictures are ignored
461 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
464 vlc_mutex_lock( &p_spu->subpicture_lock );
466 /* There can be race conditions so we need to check the status */
467 if( p_subpic->i_status == FREE_SUBPICTURE )
469 vlc_mutex_unlock( &p_spu->subpicture_lock );
473 /* Check if status is valid */
474 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
475 && ( p_subpic->i_status != READY_SUBPICTURE ) )
477 msg_Err( p_spu, "subpicture %p has invalid status %d",
478 p_subpic, p_subpic->i_status );
481 while( p_subpic->p_region )
483 subpicture_region_t *p_region = p_subpic->p_region;
484 p_subpic->p_region = p_region->p_next;
485 spu_DestroyRegion( p_spu, p_region );
488 if( p_subpic->pf_destroy )
490 p_subpic->pf_destroy( p_subpic );
493 p_subpic->i_status = FREE_SUBPICTURE;
495 vlc_mutex_unlock( &p_spu->subpicture_lock );
498 /*****************************************************************************
499 * spu_RenderSubpictures: render a subpicture list
500 *****************************************************************************
501 * This function renders all sub picture units in the list.
502 *****************************************************************************/
503 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
504 picture_t *p_pic_dst, picture_t *p_pic_src,
505 subpicture_t *p_subpic,
506 int i_scale_width_orig, int i_scale_height_orig )
509 vlc_mutex_lock( &p_spu->subpicture_lock );
511 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
512 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
514 subpicture_region_t *p_region = p_subpic->p_region;
515 int i_scale_width, i_scale_height;
516 int i_subpic_x = p_subpic->i_x;
518 /* Load the blending module */
519 if( !p_spu->p_blend && p_region )
521 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
522 vlc_object_attach( p_spu->p_blend, p_spu );
523 p_spu->p_blend->fmt_out.video.i_x_offset =
524 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
525 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
526 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
527 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
528 /* XXX: We'll also be using it for YUVA and RGBA blending ... */
530 p_spu->p_blend->p_module =
531 module_Need( p_spu->p_blend, "video blending", 0, 0 );
534 /* Load the text rendering module */
535 if( !p_spu->p_text && p_region && p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
537 char *psz_modulename = NULL;
539 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
540 vlc_object_attach( p_spu->p_text, p_spu );
542 p_spu->p_text->fmt_out.video.i_width =
543 p_spu->p_text->fmt_out.video.i_visible_width =
545 p_spu->p_text->fmt_out.video.i_height =
546 p_spu->p_text->fmt_out.video.i_visible_height =
549 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
550 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
552 psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
553 if( psz_modulename && *psz_modulename )
555 p_spu->p_text->p_module =
556 module_Need( p_spu->p_text, "text renderer",
557 psz_modulename, VLC_TRUE );
559 if( !p_spu->p_text->p_module )
561 p_spu->p_text->p_module =
562 module_Need( p_spu->p_text, "text renderer", 0, 0 );
564 if( psz_modulename ) free( psz_modulename );
568 if( p_subpic->i_original_picture_height > 0 &&
569 p_subpic->i_original_picture_width > 0 )
571 p_spu->p_text->fmt_out.video.i_width =
572 p_spu->p_text->fmt_out.video.i_visible_width =
573 p_subpic->i_original_picture_width;
574 p_spu->p_text->fmt_out.video.i_height =
575 p_spu->p_text->fmt_out.video.i_visible_height =
576 p_subpic->i_original_picture_height;
580 p_spu->p_text->fmt_out.video.i_width =
581 p_spu->p_text->fmt_out.video.i_visible_width =
583 p_spu->p_text->fmt_out.video.i_height =
584 p_spu->p_text->fmt_out.video.i_visible_height =
589 i_scale_width = i_scale_width_orig;
590 i_scale_height = i_scale_height_orig;
592 if( p_subpic->i_original_picture_height > 0 &&
593 p_subpic->i_original_picture_width > 0 )
595 i_scale_width = i_scale_width * p_fmt->i_width /
596 p_subpic->i_original_picture_width;
597 i_scale_height = i_scale_height * p_fmt->i_height /
598 p_subpic->i_original_picture_height;
600 else if( p_subpic->i_original_picture_height > 0 )
602 i_scale_height = i_scale_height * p_fmt->i_height /
603 p_subpic->i_original_picture_height;
604 i_scale_width = i_scale_height * i_scale_height / p_fmt->i_height;
607 /* Set default subpicture aspect ratio */
608 if( p_region && p_region->fmt.i_aspect &&
609 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
611 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
612 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
615 (!p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den) )
617 p_region->fmt.i_sar_den = p_fmt->i_sar_den;
618 p_region->fmt.i_sar_num = p_fmt->i_sar_num;
621 /* Take care of the aspect ratio */
622 if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
623 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
625 i_scale_width = i_scale_width *
626 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
627 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
628 i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
631 /* Load the scaling module */
632 if( !p_spu->p_scale && (i_scale_width != 1000 ||
633 i_scale_height != 1000) )
635 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
636 vlc_object_attach( p_spu->p_scale, p_spu );
637 p_spu->p_scale->fmt_out.video.i_chroma =
638 p_spu->p_scale->fmt_in.video.i_chroma =
639 VLC_FOURCC('Y','U','V','P');
640 /* XXX: We'll also be using it for YUVA and RGBA blending ... */
641 p_spu->p_scale->fmt_in.video.i_width =
642 p_spu->p_scale->fmt_in.video.i_height = 32;
643 p_spu->p_scale->fmt_out.video.i_width =
644 p_spu->p_scale->fmt_out.video.i_height = 16;
646 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
647 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
648 p_spu->p_scale->p_module =
649 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
652 while( p_region && p_spu->p_blend && p_spu->p_blend->pf_video_blend )
654 int i_fade_alpha = 255;
655 int i_x_offset = p_region->i_x + i_subpic_x;
656 int i_y_offset = p_region->i_y + p_subpic->i_y;
658 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
660 if( p_spu->p_text && p_spu->p_text->p_module )
662 p_region->i_align = p_subpic->i_flags;
664 if( p_spu->p_text->pf_render_html && p_region->psz_html )
666 p_spu->p_text->pf_render_html( p_spu->p_text,
667 p_region, p_region );
669 else if( p_spu->p_text->pf_render_text )
671 p_spu->p_text->pf_render_text( p_spu->p_text,
672 p_region, p_region );
677 /* Force palette if requested */
678 if( p_spu->b_force_palette && VLC_FOURCC('Y','U','V','P') ==
679 p_region->fmt.i_chroma )
681 memcpy( p_region->fmt.p_palette->palette,
682 p_spu->palette, 16 );
685 /* Scale SPU if necessary */
686 if( p_region->p_cache )
688 if( i_scale_width * p_region->fmt.i_width / 1000 !=
689 p_region->p_cache->fmt.i_width ||
690 i_scale_height * p_region->fmt.i_height / 1000 !=
691 p_region->p_cache->fmt.i_height )
693 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
695 p_region->p_cache = 0;
699 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
700 p_spu->p_scale && !p_region->p_cache )
704 p_spu->p_scale->fmt_in.video = p_region->fmt;
705 p_spu->p_scale->fmt_out.video = p_region->fmt;
708 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
709 &p_spu->p_scale->fmt_out.video );
710 if( p_spu->p_scale->fmt_out.video.p_palette )
711 *p_spu->p_scale->fmt_out.video.p_palette =
712 *p_region->fmt.p_palette;
713 p_region->p_cache->p_next = p_region->p_next;
715 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
716 &p_region->picture );
718 p_spu->p_scale->fmt_out.video.i_width =
719 p_region->fmt.i_width * i_scale_width / 1000;
720 p_spu->p_scale->fmt_out.video.i_visible_width =
721 p_region->fmt.i_visible_width * i_scale_width / 1000;
722 p_spu->p_scale->fmt_out.video.i_height =
723 p_region->fmt.i_height * i_scale_height / 1000;
724 p_spu->p_scale->fmt_out.video.i_visible_height =
725 p_region->fmt.i_visible_height * i_scale_height / 1000;
726 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
727 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
728 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
730 p_pic = p_spu->p_scale->pf_video_filter(
731 p_spu->p_scale, &p_region->p_cache->picture );
734 picture_t p_pic_tmp = p_region->p_cache->picture;
735 p_region->p_cache->picture = *p_pic;
740 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
741 p_spu->p_scale && p_region->p_cache )
743 p_region = p_region->p_cache;
746 if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
748 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
751 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
753 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
756 if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
758 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
761 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
763 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
766 if( p_subpic->b_absolute )
768 i_x_offset = p_region->i_x +
769 i_subpic_x * i_scale_width / 1000;
770 i_y_offset = p_region->i_y +
771 p_subpic->i_y * i_scale_height / 1000;
775 i_x_offset = __MAX( i_x_offset, 0 );
776 i_y_offset = __MAX( i_y_offset, 0 );
778 if( p_spu->i_margin != 0 && p_spu->b_force_crop == VLC_FALSE )
781 int i_low = i_y_offset - p_spu->i_margin;
782 int i_high = i_y_offset + p_region->fmt.i_height - p_spu->i_margin;
784 /* crop extra margin to keep within bounds */
785 if( i_low < 0 ) i_diff = i_low;
786 if( i_high > (int)p_fmt->i_height ) i_diff = i_high - p_fmt->i_height;
787 i_y_offset -= ( p_spu->i_margin + i_diff );
790 p_spu->p_blend->fmt_in.video = p_region->fmt;
792 /* Force cropping if requested */
793 if( p_spu->b_force_crop )
795 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
796 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
797 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
798 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
799 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
801 /* Find the intersection */
802 if( i_crop_x + i_crop_width <= i_x_offset ||
803 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
804 i_crop_y + i_crop_height <= i_y_offset ||
805 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
807 /* No intersection */
808 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
812 int i_x, i_y, i_x_end, i_y_end;
813 i_x = __MAX( i_crop_x, i_x_offset );
814 i_y = __MAX( i_crop_y, i_y_offset );
815 i_x_end = __MIN( i_crop_x + i_crop_width,
816 i_x_offset + (int)p_fmt->i_visible_width );
817 i_y_end = __MIN( i_crop_y + i_crop_height,
818 i_y_offset + (int)p_fmt->i_visible_height );
820 p_fmt->i_x_offset = i_x - i_x_offset;
821 p_fmt->i_y_offset = i_y - i_y_offset;
822 p_fmt->i_visible_width = i_x_end - i_x;
823 p_fmt->i_visible_height = i_y_end - i_y;
830 /* Update the output picture size */
831 p_spu->p_blend->fmt_out.video.i_width =
832 p_spu->p_blend->fmt_out.video.i_visible_width =
834 p_spu->p_blend->fmt_out.video.i_height =
835 p_spu->p_blend->fmt_out.video.i_visible_height =
838 if( p_subpic->b_fade )
840 mtime_t i_fade_start = ( p_subpic->i_stop +
841 p_subpic->i_start ) / 2;
842 mtime_t i_now = mdate();
843 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
845 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
846 ( p_subpic->i_stop - i_fade_start );
850 i_x_offset = __MAX( i_x_offset, 0 );
851 i_y_offset = __MAX( i_y_offset, 0 );
853 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
854 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
855 i_fade_alpha * p_subpic->i_alpha / 255 );
857 p_region = p_region->p_next;
860 p_subpic = p_subpic->p_next;
863 vlc_mutex_unlock( &p_spu->subpicture_lock );
866 /*****************************************************************************
867 * spu_SortSubpictures: find the subpictures to display
868 *****************************************************************************
869 * This function parses all subpictures and decides which ones need to be
870 * displayed. This operation does not need lock, since only READY_SUBPICTURE
871 * are handled. If no picture has been selected, display_date will depend on
873 * We also check for ephemer DVD subpictures (subpictures that have
874 * to be removed if a newer one is available), which makes it a lot
875 * more difficult to guess if a subpicture has to be rendered or not.
876 *****************************************************************************/
877 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
878 vlc_bool_t b_paused )
880 int i_index, i_channel;
881 subpicture_t *p_subpic = NULL;
882 subpicture_t *p_ephemer;
883 mtime_t ephemer_date;
885 /* Run subpicture filters */
886 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
888 subpicture_t *p_subpic_filter;
889 p_subpic_filter = p_spu->pp_filter[i_index]->
890 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
891 if( p_subpic_filter )
893 spu_DisplaySubpicture( p_spu, p_subpic_filter );
897 /* We get an easily parsable chained list of subpictures which
898 * ends with NULL since p_subpic was initialized to NULL. */
899 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
904 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
906 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
907 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
912 display_date < p_spu->p_subpicture[i_index].i_start )
914 /* Too early, come back next monday */
918 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
919 ephemer_date = p_spu->p_subpicture[i_index].i_start;
921 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
922 ( !p_spu->p_subpicture[i_index].b_ephemer ||
923 p_spu->p_subpicture[i_index].i_stop >
924 p_spu->p_subpicture[i_index].i_start ) &&
925 !( p_spu->p_subpicture[i_index].b_pausable &&
928 /* Too late, destroy the subpic */
929 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
933 /* If this is an ephemer subpic, add it to our list */
934 if( p_spu->p_subpicture[i_index].b_ephemer )
936 p_spu->p_subpicture[i_index].p_next = p_ephemer;
937 p_ephemer = &p_spu->p_subpicture[i_index];
942 p_spu->p_subpicture[i_index].p_next = p_subpic;
943 p_subpic = &p_spu->p_subpicture[i_index];
946 /* If we found ephemer subpictures, check if they have to be
947 * displayed or destroyed */
948 while( p_ephemer != NULL )
950 subpicture_t *p_tmp = p_ephemer;
951 p_ephemer = p_ephemer->p_next;
953 if( p_tmp->i_start < ephemer_date )
955 /* Ephemer subpicture has lived too long */
956 spu_DestroySubpicture( p_spu, p_tmp );
960 /* Ephemer subpicture can still live a bit */
961 p_tmp->p_next = p_subpic;
970 /*****************************************************************************
971 * SpuClearChannel: clear an spu channel
972 *****************************************************************************
973 * This function destroys the subpictures which belong to the spu channel
974 * corresponding to i_channel_id.
975 *****************************************************************************/
976 static void SpuClearChannel( spu_t *p_spu, int i_channel )
978 int i_subpic; /* subpicture index */
979 subpicture_t *p_subpic = NULL; /* first free subpicture */
981 vlc_mutex_lock( &p_spu->subpicture_lock );
983 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
985 p_subpic = &p_spu->p_subpicture[i_subpic];
986 if( p_subpic->i_status == FREE_SUBPICTURE
987 || ( p_subpic->i_status != RESERVED_SUBPICTURE
988 && p_subpic->i_status != READY_SUBPICTURE ) )
993 if( p_subpic->i_channel == i_channel )
995 while( p_subpic->p_region )
997 subpicture_region_t *p_region = p_subpic->p_region;
998 p_subpic->p_region = p_region->p_next;
999 spu_DestroyRegion( p_spu, p_region );
1002 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
1003 p_subpic->i_status = FREE_SUBPICTURE;
1007 vlc_mutex_unlock( &p_spu->subpicture_lock );
1010 /*****************************************************************************
1011 * spu_ControlDefault: default methods for the subpicture unit control.
1012 *****************************************************************************/
1013 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
1019 case SPU_CHANNEL_REGISTER:
1020 pi = (int *)va_arg( args, int * );
1021 if( pi ) *pi = p_spu->i_channel++;
1024 case SPU_CHANNEL_CLEAR:
1025 i = (int)va_arg( args, int );
1026 SpuClearChannel( p_spu, i );
1030 msg_Dbg( p_spu, "control query not supported" );
1031 return VLC_EGENERIC;
1037 /*****************************************************************************
1038 * Object variables callbacks
1039 *****************************************************************************/
1041 /*****************************************************************************
1042 * UpdateSPU: update subpicture settings
1043 *****************************************************************************
1044 * This function is called from CropCallback and at initialization time, to
1045 * retrieve crop information from the input.
1046 *****************************************************************************/
1047 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1051 p_spu->b_force_palette = VLC_FALSE;
1052 p_spu->b_force_crop = VLC_FALSE;
1054 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
1056 p_spu->b_force_crop = VLC_TRUE;
1057 var_Get( p_object, "x-start", &val );
1058 p_spu->i_crop_x = val.i_int;
1059 var_Get( p_object, "y-start", &val );
1060 p_spu->i_crop_y = val.i_int;
1061 var_Get( p_object, "x-end", &val );
1062 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
1063 var_Get( p_object, "y-end", &val );
1064 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
1066 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1068 memcpy( p_spu->palette, val.p_address, 16 );
1069 p_spu->b_force_palette = VLC_TRUE;
1072 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1073 p_spu->i_crop_x, p_spu->i_crop_y,
1074 p_spu->i_crop_width, p_spu->i_crop_height,
1075 p_spu->b_force_palette );
1078 /*****************************************************************************
1079 * CropCallback: called when the highlight properties are changed
1080 *****************************************************************************
1081 * This callback is called from the input thread when we need cropping
1082 *****************************************************************************/
1083 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1084 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1086 (void)psz_var; (void)oldval; (void)newval;
1087 UpdateSPU( (spu_t *)p_data, p_object );
1091 /*****************************************************************************
1092 * Buffers allocation callbacks for the filters
1093 *****************************************************************************/
1094 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1096 filter_owner_sys_t *p_sys = p_filter->p_owner;
1097 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1098 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1099 return p_subpicture;
1102 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1104 filter_owner_sys_t *p_sys = p_filter->p_owner;
1105 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1108 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1110 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1112 memset( p_subpic, 0, sizeof(subpicture_t) );
1113 p_subpic->b_absolute = VLC_TRUE;
1115 p_subpic->pf_create_region = __spu_CreateRegion;
1116 p_subpic->pf_make_region = __spu_MakeRegion;
1117 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1122 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1124 while( p_subpic->p_region )
1126 subpicture_region_t *p_region = p_subpic->p_region;
1127 p_subpic->p_region = p_region->p_next;
1128 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1134 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1136 picture_t *p_picture = malloc( sizeof(picture_t) );
1138 if( vout_AllocatePicture( p_filter, p_picture,
1139 p_filter->fmt_out.video.i_chroma,
1140 p_filter->fmt_out.video.i_width,
1141 p_filter->fmt_out.video.i_height,
1142 p_filter->fmt_out.video.i_aspect )
1149 p_picture->pf_release = RegionPictureRelease;
1154 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1157 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1158 if( p_pic ) free( p_pic );
1161 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1162 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1164 (void)p_object; (void)oldval; (void)newval;
1166 if( !strcmp( psz_var, "sub-filter" ) )
1168 spu_t *p_spu = (spu_t *)p_data;
1169 vlc_mutex_lock( &p_spu->subpicture_lock );
1170 spu_DeleteChain( p_spu );
1171 spu_ParseChain( p_spu );
1172 vlc_mutex_unlock( &p_spu->subpicture_lock );