1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2005 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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, ',' );
115 if( !psz_parser ) psz_parser = strchr( psz_filter, ':' );
117 if( psz_parser ) *psz_parser++ = 0;
119 p_spu->pp_filter[p_spu->i_filter] =
120 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
121 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
122 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
123 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
124 p_spu->pp_filter[p_spu->i_filter]->p_module =
125 module_Need( p_spu->pp_filter[p_spu->i_filter],
126 "sub filter", psz_filter, 0 );
127 if( p_spu->pp_filter[p_spu->i_filter]->p_module )
129 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
130 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
131 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
132 p_sys->p_spu = p_spu;
137 msg_Dbg( p_spu, "no sub filter found" );
138 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
139 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
142 if( p_spu->i_filter >= 10 )
144 msg_Dbg( p_spu, "can't add anymore filters" );
147 psz_filter = psz_parser;
149 if( psz_filter_orig ) free( psz_filter_orig );
155 * Destroy the subpicture unit
157 * \param p_this the parent object which destroys the subpicture unit
159 void spu_Destroy( spu_t *p_spu )
163 vlc_object_detach( p_spu );
165 /* Destroy all remaining subpictures */
166 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
168 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
170 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
176 if( p_spu->p_blend->p_module )
177 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
179 vlc_object_detach( p_spu->p_blend );
180 vlc_object_destroy( p_spu->p_blend );
185 if( p_spu->p_text->p_module )
186 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
188 vlc_object_detach( p_spu->p_text );
189 vlc_object_destroy( p_spu->p_text );
194 if( p_spu->p_scale->p_module )
195 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
197 vlc_object_detach( p_spu->p_scale );
198 vlc_object_destroy( p_spu->p_scale );
201 while( p_spu->i_filter-- )
203 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
204 p_spu->pp_filter[p_spu->i_filter]->p_module );
205 free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
206 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
207 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
210 vlc_mutex_destroy( &p_spu->subpicture_lock );
211 vlc_object_destroy( p_spu );
215 * Attach/Detach the SPU from any input
217 * \param p_this the object in which to destroy the subpicture unit
218 * \param b_attach to select attach or detach
220 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
222 vlc_object_t *p_input;
224 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
225 if( !p_input ) return;
229 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
230 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
231 vlc_object_release( p_input );
235 /* Delete callback */
236 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
237 vlc_object_release( p_input );
242 * Create a subpicture region
244 * \param p_this vlc_object_t
245 * \param p_fmt the format that this subpicture region should have
247 static void RegionPictureRelease( picture_t *p_pic )
249 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
251 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
252 video_format_t *p_fmt )
254 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
255 memset( p_region, 0, sizeof(subpicture_region_t) );
256 p_region->p_next = 0;
257 p_region->p_cache = 0;
258 p_region->fmt = *p_fmt;
259 p_region->psz_text = 0;
260 p_region->i_text_color = 0xFFFFFF;
262 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
263 p_fmt->p_palette = p_region->fmt.p_palette =
264 malloc( sizeof(video_palette_t) );
265 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
267 p_region->picture.p_data_orig = 0;
269 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
271 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
272 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
274 if( !p_region->picture.i_planes )
277 free( p_fmt->p_palette );
281 p_region->picture.pf_release = RegionPictureRelease;
287 * Make a subpicture region from an existing picture_t
289 * \param p_this vlc_object_t
290 * \param p_fmt the format that this subpicture region should have
291 * \param p_pic a pointer to the picture creating the region (not freed)
293 subpicture_region_t *__spu_MakeRegion( vlc_object_t *p_this,
294 video_format_t *p_fmt,
297 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
298 memset( p_region, 0, sizeof(subpicture_region_t) );
299 p_region->p_next = 0;
300 p_region->p_cache = 0;
301 p_region->fmt = *p_fmt;
302 p_region->psz_text = 0;
303 p_region->i_text_color = 0xFFFFFF;
305 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
306 p_fmt->p_palette = p_region->fmt.p_palette =
307 malloc( sizeof(video_palette_t) );
308 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
310 memcpy( &p_region->picture, p_pic, sizeof(picture_t) );
311 p_region->picture.pf_release = RegionPictureRelease;
317 * Destroy a subpicture region
319 * \param p_this vlc_object_t
320 * \param p_region the subpicture region to destroy
322 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
324 if( !p_region ) return;
325 if( p_region->picture.pf_release )
326 p_region->picture.pf_release( &p_region->picture );
327 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
328 if( p_region->psz_text ) free( p_region->psz_text );
329 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
334 * Display a subpicture
336 * Remove the reservation flag of a subpicture, which will cause it to be
338 * \param p_spu the subpicture unit object
339 * \param p_subpic the subpicture to display
341 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
343 /* Check if status is valid */
344 if( p_subpic->i_status != RESERVED_SUBPICTURE )
346 msg_Err( p_spu, "subpicture %p has invalid status #%d",
347 p_subpic, p_subpic->i_status );
350 /* Remove reservation flag */
351 p_subpic->i_status = READY_SUBPICTURE;
353 if( p_subpic->i_channel == DEFAULT_CHAN )
355 p_subpic->i_channel = 0xFFFF;
356 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
357 p_subpic->i_channel = DEFAULT_CHAN;
362 * Allocate a subpicture in the spu heap.
364 * This function create a reserved subpicture in the spu heap.
365 * A null pointer is returned if the function fails. This method provides an
366 * already allocated zone of memory in the spu data fields. It needs locking
367 * since several pictures can be created by several producers threads.
368 * \param p_spu the subpicture unit in which to create the subpicture
369 * \return NULL on error, a reserved subpicture otherwise
371 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
373 int i_subpic; /* subpicture index */
374 subpicture_t * p_subpic = NULL; /* first free subpicture */
377 vlc_mutex_lock( &p_spu->subpicture_lock );
380 * Look for an empty place
383 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
385 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
387 /* Subpicture is empty and ready for allocation */
388 p_subpic = &p_spu->p_subpicture[i_subpic];
389 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
394 /* If no free subpicture could be found */
395 if( p_subpic == NULL )
397 msg_Err( p_spu, "subpicture heap is full" );
398 vlc_mutex_unlock( &p_spu->subpicture_lock );
402 /* Copy subpicture information, set some default values */
403 memset( p_subpic, 0, sizeof(subpicture_t) );
404 p_subpic->i_status = RESERVED_SUBPICTURE;
405 p_subpic->b_absolute = VLC_TRUE;
406 p_subpic->b_fade = VLC_FALSE;
407 p_subpic->i_alpha = 0xFF;
408 p_subpic->p_region = 0;
409 p_subpic->pf_render = 0;
410 p_subpic->pf_destroy = 0;
412 vlc_mutex_unlock( &p_spu->subpicture_lock );
414 p_subpic->pf_create_region = __spu_CreateRegion;
415 p_subpic->pf_make_region = __spu_MakeRegion;
416 p_subpic->pf_destroy_region = __spu_DestroyRegion;
422 * Remove a subpicture from the heap
424 * This function frees a previously reserved subpicture.
425 * It is meant to be used when the construction of a picture aborted.
426 * This function does not need locking since reserved subpictures are ignored
429 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
432 vlc_mutex_lock( &p_spu->subpicture_lock );
434 /* There can be race conditions so we need to check the status */
435 if( p_subpic->i_status == FREE_SUBPICTURE )
437 vlc_mutex_unlock( &p_spu->subpicture_lock );
441 /* Check if status is valid */
442 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
443 && ( p_subpic->i_status != READY_SUBPICTURE ) )
445 msg_Err( p_spu, "subpicture %p has invalid status %d",
446 p_subpic, p_subpic->i_status );
449 while( p_subpic->p_region )
451 subpicture_region_t *p_region = p_subpic->p_region;
452 p_subpic->p_region = p_region->p_next;
453 spu_DestroyRegion( p_spu, p_region );
456 if( p_subpic->pf_destroy )
458 p_subpic->pf_destroy( p_subpic );
461 p_subpic->i_status = FREE_SUBPICTURE;
463 vlc_mutex_unlock( &p_spu->subpicture_lock );
466 /*****************************************************************************
467 * spu_RenderSubpictures: render a subpicture list
468 *****************************************************************************
469 * This function renders all sub picture units in the list.
470 *****************************************************************************/
471 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
472 picture_t *p_pic_dst, picture_t *p_pic_src,
473 subpicture_t *p_subpic,
474 int i_scale_width_orig, int i_scale_height_orig )
480 vlc_mutex_lock( &p_spu->subpicture_lock );
482 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
483 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
485 subpicture_region_t *p_region = p_subpic->p_region;
486 int i_scale_width, i_scale_height;
488 /* Load the blending module */
489 if( !p_spu->p_blend && p_region )
491 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
492 vlc_object_attach( p_spu->p_blend, p_spu );
493 p_spu->p_blend->fmt_out.video.i_x_offset =
494 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
495 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
496 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
497 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
499 p_spu->p_blend->p_module =
500 module_Need( p_spu->p_blend, "video blending", 0, 0 );
503 /* Load the text rendering module */
504 if( !p_spu->p_text && p_region )
506 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
507 vlc_object_attach( p_spu->p_text, p_spu );
509 p_spu->p_text->fmt_out.video.i_width =
510 p_spu->p_text->fmt_out.video.i_visible_width =
512 p_spu->p_text->fmt_out.video.i_height =
513 p_spu->p_text->fmt_out.video.i_visible_height =
516 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
517 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
518 p_spu->p_text->p_module =
519 module_Need( p_spu->p_text, "text renderer", 0, 0 );
523 p_spu->p_text->fmt_out.video.i_width =
524 p_spu->p_text->fmt_out.video.i_visible_width =
526 p_spu->p_text->fmt_out.video.i_height =
527 p_spu->p_text->fmt_out.video.i_visible_height =
531 if (p_subpic->p_region->fmt.i_aspect == 0)
532 /* Set subtitle to be the same aspect ratio as the background
535 p_subpic->p_region->fmt.i_aspect = p_pic_src->format.i_aspect;
537 i_scale_width = i_scale_width_orig;
538 i_scale_height = i_scale_height_orig;
540 if( p_subpic->i_original_picture_width &&
541 p_subpic->i_original_picture_height )
543 i_scale_width = i_scale_width * p_fmt->i_width /
544 p_subpic->i_original_picture_width;
545 i_scale_height = i_scale_height * p_fmt->i_height /
546 p_subpic->i_original_picture_height;
549 /* Take care of the aspect ratio */
551 if( p_subpic->p_region->fmt.i_aspect != p_pic_src->format.i_aspect)
553 i_scale_width = (p_subpic->p_region->fmt.i_aspect*1000) /
554 p_pic_src->format.i_aspect;
555 i_subpic_x = p_subpic->i_x * i_scale_width / 1000;
557 i_subpic_x = p_subpic->i_x;
560 if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
561 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
563 i_scale_width = i_scale_width *
564 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
565 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
568 if( !p_spu->p_scale && (i_scale_width != 1000 ||
569 i_scale_height != 1000) )
571 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
572 vlc_object_attach( p_spu->p_scale, p_spu );
573 p_spu->p_scale->fmt_out.video.i_chroma =
574 p_spu->p_scale->fmt_in.video.i_chroma =
575 VLC_FOURCC('Y','U','V','P');
576 p_spu->p_scale->fmt_in.video.i_width =
577 p_spu->p_scale->fmt_in.video.i_height = 32;
578 p_spu->p_scale->fmt_out.video.i_width =
579 p_spu->p_scale->fmt_out.video.i_height = 16;
581 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
582 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
583 p_spu->p_scale->p_module =
584 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
587 if( p_subpic->pf_render )
589 /* HACK to remove when the ogt subpic decoder is gone */
590 if( p_spu->p_parent &&
591 p_spu->p_parent->i_object_type == VLC_OBJECT_VOUT )
593 vout_thread_t *p_vout = (vout_thread_t *)p_spu->p_parent;
594 p_subpic->pf_render( p_vout, p_pic_dst, p_subpic );
597 else while( p_region && p_spu->p_blend &&
598 p_spu->p_blend->pf_video_blend )
600 int i_fade_alpha = 255;
601 int i_x_offset = p_region->i_x + i_subpic_x;
602 int i_y_offset = p_region->i_y + p_subpic->i_y;
604 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
606 if( p_spu->p_text && p_spu->p_text->p_module &&
607 p_spu->p_text->pf_render_text )
609 p_region->i_text_align = p_subpic->i_flags & 0x3;
610 p_spu->p_text->pf_render_text( p_spu->p_text,
611 p_region, p_region );
615 /* Force palette if requested */
616 if( p_spu->b_force_alpha && VLC_FOURCC('Y','U','V','P') ==
617 p_region->fmt.i_chroma )
619 p_region->fmt.p_palette->palette[0][3] = p_spu->pi_alpha[0];
620 p_region->fmt.p_palette->palette[1][3] = p_spu->pi_alpha[1];
621 p_region->fmt.p_palette->palette[2][3] = p_spu->pi_alpha[2];
622 p_region->fmt.p_palette->palette[3][3] = p_spu->pi_alpha[3];
625 /* Scale SPU if necessary */
626 if( p_region->p_cache )
628 if( i_scale_width * p_region->fmt.i_width / 1000 !=
629 p_region->p_cache->fmt.i_width ||
630 i_scale_height * p_region->fmt.i_height / 1000 !=
631 p_region->p_cache->fmt.i_height )
633 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
635 p_region->p_cache = 0;
639 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
640 p_spu->p_scale && !p_region->p_cache )
644 p_spu->p_scale->fmt_in.video = p_region->fmt;
645 p_spu->p_scale->fmt_out.video = p_region->fmt;
648 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
649 &p_spu->p_scale->fmt_out.video );
650 if( p_spu->p_scale->fmt_out.video.p_palette )
651 *p_spu->p_scale->fmt_out.video.p_palette =
652 *p_region->fmt.p_palette;
653 p_region->p_cache->p_next = p_region->p_next;
655 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
656 &p_region->picture );
658 p_spu->p_scale->fmt_out.video.i_width =
659 p_region->fmt.i_width * i_scale_width / 1000;
660 p_spu->p_scale->fmt_out.video.i_visible_width =
661 p_region->fmt.i_visible_width * i_scale_width / 1000;
662 p_spu->p_scale->fmt_out.video.i_height =
663 p_region->fmt.i_height * i_scale_height / 1000;
664 p_spu->p_scale->fmt_out.video.i_visible_height =
665 p_region->fmt.i_visible_height * i_scale_height / 1000;
666 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
667 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
668 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
670 p_pic = p_spu->p_scale->pf_video_filter(
671 p_spu->p_scale, &p_region->p_cache->picture );
674 picture_t p_pic_tmp = p_region->p_cache->picture;
675 p_region->p_cache->picture = *p_pic;
680 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
681 p_spu->p_scale && p_region->p_cache )
683 p_region = p_region->p_cache;
686 if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
688 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
691 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
693 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
696 if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
698 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
701 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
703 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
706 if( p_subpic->b_absolute )
708 i_x_offset = p_region->i_x +
709 i_subpic_x * i_scale_width / 1000;
710 i_y_offset = p_region->i_y +
711 p_subpic->i_y * i_scale_height / 1000;
713 if( p_spu->i_margin >= 0 )
715 if( p_subpic->i_height + (unsigned int)p_spu->i_margin <=
718 i_y_offset = p_fmt->i_height -
719 p_spu->i_margin - p_subpic->i_height;
724 p_spu->p_blend->fmt_in.video = p_region->fmt;
726 /* Force cropping if requested */
727 if( p_spu->b_force_crop )
729 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
730 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
731 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
732 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
733 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
735 /* Find the intersection */
736 if( i_crop_x + i_crop_width <= i_x_offset ||
737 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
738 i_crop_y + i_crop_height <= i_y_offset ||
739 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
741 /* No intersection */
742 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
746 int i_x, i_y, i_x_end, i_y_end;
747 i_x = __MAX( i_crop_x, i_x_offset );
748 i_y = __MAX( i_crop_y, i_y_offset );
749 i_x_end = __MIN( i_crop_x + i_crop_width,
750 i_x_offset + (int)p_fmt->i_visible_width );
751 i_y_end = __MIN( i_crop_y + i_crop_height,
752 i_y_offset + (int)p_fmt->i_visible_height );
754 p_fmt->i_x_offset = i_x - i_x_offset;
755 p_fmt->i_y_offset = i_y - i_y_offset;
756 p_fmt->i_visible_width = i_x_end - i_x;
757 p_fmt->i_visible_height = i_y_end - i_y;
764 /* Update the output picture size */
765 p_spu->p_blend->fmt_out.video.i_width =
766 p_spu->p_blend->fmt_out.video.i_visible_width =
768 p_spu->p_blend->fmt_out.video.i_height =
769 p_spu->p_blend->fmt_out.video.i_visible_height =
772 if( p_subpic->b_fade )
774 mtime_t i_fade_start = ( p_subpic->i_stop +
775 p_subpic->i_start ) / 2;
776 mtime_t i_now = mdate();
777 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
779 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
780 ( p_subpic->i_stop - i_fade_start );
784 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
785 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
786 i_fade_alpha * p_subpic->i_alpha / 255 );
788 p_region = p_region->p_next;
791 p_subpic = p_subpic->p_next;
794 vlc_mutex_unlock( &p_spu->subpicture_lock );
797 /*****************************************************************************
798 * spu_SortSubpictures: find the subpictures to display
799 *****************************************************************************
800 * This function parses all subpictures and decides which ones need to be
801 * displayed. This operation does not need lock, since only READY_SUBPICTURE
802 * are handled. If no picture has been selected, display_date will depend on
804 * We also check for ephemer DVD subpictures (subpictures that have
805 * to be removed if a newer one is available), which makes it a lot
806 * more difficult to guess if a subpicture has to be rendered or not.
807 *****************************************************************************/
808 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
810 int i_index, i_channel;
811 subpicture_t *p_subpic = NULL;
812 subpicture_t *p_ephemer;
813 mtime_t ephemer_date;
815 /* Run subpicture filters */
816 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
818 subpicture_t *p_subpic_filter;
819 p_subpic_filter = p_spu->pp_filter[i_index]->
820 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
821 if( p_subpic_filter )
823 spu_DisplaySubpicture( p_spu, p_subpic_filter );
827 /* We get an easily parsable chained list of subpictures which
828 * ends with NULL since p_subpic was initialized to NULL. */
829 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
834 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
836 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
837 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
842 display_date < p_spu->p_subpicture[i_index].i_start )
844 /* Too early, come back next monday */
848 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
849 ephemer_date = p_spu->p_subpicture[i_index].i_start;
851 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
852 ( !p_spu->p_subpicture[i_index].b_ephemer ||
853 p_spu->p_subpicture[i_index].i_stop >
854 p_spu->p_subpicture[i_index].i_start ) )
856 /* Too late, destroy the subpic */
857 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
861 /* If this is an ephemer subpic, add it to our list */
862 if( p_spu->p_subpicture[i_index].b_ephemer )
864 p_spu->p_subpicture[i_index].p_next = p_ephemer;
865 p_ephemer = &p_spu->p_subpicture[i_index];
870 p_spu->p_subpicture[i_index].p_next = p_subpic;
871 p_subpic = &p_spu->p_subpicture[i_index];
874 /* If we found ephemer subpictures, check if they have to be
875 * displayed or destroyed */
876 while( p_ephemer != NULL )
878 subpicture_t *p_tmp = p_ephemer;
879 p_ephemer = p_ephemer->p_next;
881 if( p_tmp->i_start < ephemer_date )
883 /* Ephemer subpicture has lived too long */
884 spu_DestroySubpicture( p_spu, p_tmp );
888 /* Ephemer subpicture can still live a bit */
889 p_tmp->p_next = p_subpic;
898 /*****************************************************************************
899 * SpuClearChannel: clear an spu channel
900 *****************************************************************************
901 * This function destroys the subpictures which belong to the spu channel
902 * corresponding to i_channel_id.
903 *****************************************************************************/
904 static void SpuClearChannel( spu_t *p_spu, int i_channel )
906 int i_subpic; /* subpicture index */
907 subpicture_t *p_subpic = NULL; /* first free subpicture */
909 vlc_mutex_lock( &p_spu->subpicture_lock );
911 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
913 p_subpic = &p_spu->p_subpicture[i_subpic];
914 if( p_subpic->i_status == FREE_SUBPICTURE
915 || ( p_subpic->i_status != RESERVED_SUBPICTURE
916 && p_subpic->i_status != READY_SUBPICTURE ) )
921 if( p_subpic->i_channel == i_channel )
923 while( p_subpic->p_region )
925 subpicture_region_t *p_region = p_subpic->p_region;
926 p_subpic->p_region = p_region->p_next;
927 spu_DestroyRegion( p_spu, p_region );
930 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
931 p_subpic->i_status = FREE_SUBPICTURE;
935 vlc_mutex_unlock( &p_spu->subpicture_lock );
938 /*****************************************************************************
939 * spu_ControlDefault: default methods for the subpicture unit control.
940 *****************************************************************************/
941 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
947 case SPU_CHANNEL_REGISTER:
948 pi = (int *)va_arg( args, int * );
949 if( pi ) *pi = p_spu->i_channel++;
950 msg_Dbg( p_spu, "Registering subpicture channel, ID: %i",
951 p_spu->i_channel - 1 );
954 case SPU_CHANNEL_CLEAR:
955 i = (int)va_arg( args, int );
956 SpuClearChannel( p_spu, i );
960 msg_Dbg( p_spu, "control query not supported" );
967 /*****************************************************************************
968 * Object variables callbacks
969 *****************************************************************************/
971 /*****************************************************************************
972 * UpdateSPU: update subpicture settings
973 *****************************************************************************
974 * This function is called from CropCallback and at initialization time, to
975 * retrieve crop information from the input.
976 *****************************************************************************/
977 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
981 p_spu->b_force_alpha = VLC_FALSE;
982 p_spu->b_force_crop = VLC_FALSE;
984 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
986 p_spu->b_force_crop = VLC_TRUE;
987 var_Get( p_object, "x-start", &val );
988 p_spu->i_crop_x = val.i_int;
989 var_Get( p_object, "y-start", &val );
990 p_spu->i_crop_y = val.i_int;
991 var_Get( p_object, "x-end", &val );
992 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
993 var_Get( p_object, "y-end", &val );
994 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
997 if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
1000 for( i = 0; i < 4; i++ )
1002 p_spu->pi_color[i] = ((uint8_t *)val.p_address)[i];
1007 if( var_Get( p_object, "menu-contrast", &val ) == VLC_SUCCESS )
1010 for( i = 0; i < 4; i++ )
1012 p_spu->pi_alpha[i] = ((uint8_t *)val.p_address)[i];
1013 p_spu->pi_alpha[i] = p_spu->pi_alpha[i] == 0xf ?
1014 0xff : p_spu->pi_alpha[i] << 4;
1016 p_spu->b_force_alpha = VLC_TRUE;
1019 msg_Dbg( p_object, "crop: %i,%i,%i,%i, alpha: %i",
1020 p_spu->i_crop_x, p_spu->i_crop_y,
1021 p_spu->i_crop_width, p_spu->i_crop_height, p_spu->b_force_alpha );
1024 /*****************************************************************************
1025 * CropCallback: called when the highlight properties are changed
1026 *****************************************************************************
1027 * This callback is called from the input thread when we need cropping
1028 *****************************************************************************/
1029 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1030 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1032 UpdateSPU( (spu_t *)p_data, p_object );
1036 /*****************************************************************************
1037 * Buffers allocation callbacks for the filters
1038 *****************************************************************************/
1039 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1041 filter_owner_sys_t *p_sys = p_filter->p_owner;
1042 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1043 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1044 return p_subpicture;
1047 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1049 filter_owner_sys_t *p_sys = p_filter->p_owner;
1050 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1053 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1055 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1056 memset( p_subpic, 0, sizeof(subpicture_t) );
1057 p_subpic->b_absolute = VLC_TRUE;
1059 p_subpic->pf_create_region = __spu_CreateRegion;
1060 p_subpic->pf_make_region = __spu_MakeRegion;
1061 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1066 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1068 while( p_subpic->p_region )
1070 subpicture_region_t *p_region = p_subpic->p_region;
1071 p_subpic->p_region = p_region->p_next;
1072 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1078 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1080 picture_t *p_picture = malloc( sizeof(picture_t) );
1082 if( vout_AllocatePicture( p_filter, p_picture,
1083 p_filter->fmt_out.video.i_chroma,
1084 p_filter->fmt_out.video.i_width,
1085 p_filter->fmt_out.video.i_height,
1086 p_filter->fmt_out.video.i_aspect )
1093 p_picture->pf_release = RegionPictureRelease;
1098 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1100 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1101 if( p_pic ) free( p_pic );