1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2004 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"
42 /*****************************************************************************
44 *****************************************************************************/
45 static void UpdateSPU ( spu_t *, vlc_object_t * );
46 static int CropCallback( vlc_object_t *, char const *,
47 vlc_value_t, vlc_value_t, void * );
49 static int spu_vaControlDefault( spu_t *, int, va_list );
51 static subpicture_t *sub_new_buffer( filter_t * );
52 static void sub_del_buffer( filter_t *, subpicture_t * );
53 static subpicture_t *spu_new_buffer( filter_t * );
54 static void spu_del_buffer( filter_t *, subpicture_t * );
55 static picture_t *spu_new_video_buffer( filter_t * );
56 static void spu_del_video_buffer( filter_t *, picture_t * );
58 struct filter_owner_sys_t
65 * Creates the subpicture unit
67 * \param p_this the parent object which creates the subpicture unit
69 spu_t *__spu_Create( vlc_object_t *p_this )
72 spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
74 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
76 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
79 p_spu->p_blend = NULL;
81 p_spu->p_scale = NULL;
83 p_spu->pf_control = spu_vaControlDefault;
85 /* Register the default subpicture channel */
88 vlc_mutex_init( p_this, &p_spu->subpicture_lock );
90 vlc_object_attach( p_spu, p_this );
96 * Initialise the subpicture unit
98 * \param p_spu the subpicture unit object
100 int spu_Init( spu_t *p_spu )
102 char *psz_filter, *psz_filter_orig;
105 /* If the user requested an SPU margin, we force the position. */
106 var_Create( p_spu, "spumargin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
107 var_Get( p_spu, "spumargin", &val );
108 p_spu->i_margin = val.i_int;
110 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
111 var_Get( p_spu, "sub-filter", &val );
112 psz_filter = psz_filter_orig = val.psz_string;
113 while( psz_filter && *psz_filter )
115 char *psz_parser = strchr( psz_filter, ',' );
116 if( !psz_parser ) psz_parser = strchr( psz_filter, ':' );
118 if( psz_parser ) *psz_parser++ = 0;
120 p_spu->pp_filter[p_spu->i_filter] =
121 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
122 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
123 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
124 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
125 p_spu->pp_filter[p_spu->i_filter]->p_module =
126 module_Need( p_spu->pp_filter[p_spu->i_filter],
127 "sub filter", psz_filter, 0 );
128 if( p_spu->pp_filter[p_spu->i_filter]->p_module )
130 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
131 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
132 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
133 p_sys->p_spu = p_spu;
138 msg_Dbg( p_spu, "no sub filter found" );
139 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
140 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
143 if( p_spu->i_filter >= 10 )
145 msg_Dbg( p_spu, "can't add anymore filters" );
148 psz_filter = psz_parser;
150 if( psz_filter_orig ) free( psz_filter_orig );
156 * Destroy the subpicture unit
158 * \param p_this the parent object which destroys the subpicture unit
160 void spu_Destroy( spu_t *p_spu )
164 vlc_object_detach( p_spu );
166 /* Destroy all remaining subpictures */
167 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
169 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
171 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
177 if( p_spu->p_blend->p_module )
178 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
180 vlc_object_detach( p_spu->p_blend );
181 vlc_object_destroy( p_spu->p_blend );
186 if( p_spu->p_text->p_module )
187 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
189 vlc_object_detach( p_spu->p_text );
190 vlc_object_destroy( p_spu->p_text );
195 if( p_spu->p_scale->p_module )
196 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
198 vlc_object_detach( p_spu->p_scale );
199 vlc_object_destroy( p_spu->p_scale );
202 while( p_spu->i_filter-- )
204 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
205 p_spu->pp_filter[p_spu->i_filter]->p_module );
206 free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
207 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
208 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
211 vlc_mutex_destroy( &p_spu->subpicture_lock );
212 vlc_object_destroy( p_spu );
216 * Attach/Detach the SPU from any input
218 * \param p_this the object in which to destroy the subpicture unit
219 * \param b_attach to select attach or detach
221 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
223 vlc_object_t *p_input;
225 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
226 if( !p_input ) return;
230 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
231 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
232 vlc_object_release( p_input );
236 /* Delete callback */
237 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
238 vlc_object_release( p_input );
243 * Create a subpicture region
245 * \param p_this vlc_object_t
246 * \param p_fmt the format that this subpicture region should have
248 static void RegionPictureRelease( picture_t *p_pic )
250 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
252 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
253 video_format_t *p_fmt )
255 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
256 memset( p_region, 0, sizeof(subpicture_region_t) );
257 p_region->p_next = 0;
258 p_region->p_cache = 0;
259 p_region->fmt = *p_fmt;
260 p_region->psz_text = 0;
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 * Destroy a subpicture region
289 * \param p_this vlc_object_t
290 * \param p_region the subpicture region to destroy
292 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
294 if( !p_region ) return;
295 if( p_region->picture.pf_release )
296 p_region->picture.pf_release( &p_region->picture );
297 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
298 if( p_region->psz_text ) free( p_region->psz_text );
299 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
304 * Display a subpicture
306 * Remove the reservation flag of a subpicture, which will cause it to be
308 * \param p_spu the subpicture unit object
309 * \param p_subpic the subpicture to display
311 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
313 /* Check if status is valid */
314 if( p_subpic->i_status != RESERVED_SUBPICTURE )
316 msg_Err( p_spu, "subpicture %p has invalid status #%d",
317 p_subpic, p_subpic->i_status );
320 /* Remove reservation flag */
321 p_subpic->i_status = READY_SUBPICTURE;
323 if( p_subpic->i_channel == DEFAULT_CHAN )
325 p_subpic->i_channel = 0xFFFF;
326 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
327 p_subpic->i_channel = DEFAULT_CHAN;
332 * Allocate a subpicture in the spu heap.
334 * This function create a reserved subpicture in the spu heap.
335 * A null pointer is returned if the function fails. This method provides an
336 * already allocated zone of memory in the spu data fields. It needs locking
337 * since several pictures can be created by several producers threads.
338 * \param p_spu the subpicture unit in which to create the subpicture
339 * \return NULL on error, a reserved subpicture otherwise
341 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
343 int i_subpic; /* subpicture index */
344 subpicture_t * p_subpic = NULL; /* first free subpicture */
347 vlc_mutex_lock( &p_spu->subpicture_lock );
350 * Look for an empty place
353 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
355 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
357 /* Subpicture is empty and ready for allocation */
358 p_subpic = &p_spu->p_subpicture[i_subpic];
359 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
364 /* If no free subpicture could be found */
365 if( p_subpic == NULL )
367 msg_Err( p_spu, "subpicture heap is full" );
368 vlc_mutex_unlock( &p_spu->subpicture_lock );
372 /* Copy subpicture information, set some default values */
373 memset( p_subpic, 0, sizeof(subpicture_t) );
374 p_subpic->i_status = RESERVED_SUBPICTURE;
375 p_subpic->b_absolute = VLC_TRUE;
376 p_subpic->pf_render = 0;
377 p_subpic->pf_destroy = 0;
380 vlc_mutex_unlock( &p_spu->subpicture_lock );
382 p_subpic->pf_create_region = __spu_CreateRegion;
383 p_subpic->pf_destroy_region = __spu_DestroyRegion;
389 * Remove a subpicture from the heap
391 * This function frees a previously reserved subpicture.
392 * It is meant to be used when the construction of a picture aborted.
393 * This function does not need locking since reserved subpictures are ignored
396 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
399 vlc_mutex_lock( &p_spu->subpicture_lock );
401 /* There can be race conditions so we need to check the status */
402 if( p_subpic->i_status == FREE_SUBPICTURE )
404 vlc_mutex_unlock( &p_spu->subpicture_lock );
408 /* Check if status is valid */
409 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
410 && ( p_subpic->i_status != READY_SUBPICTURE ) )
412 msg_Err( p_spu, "subpicture %p has invalid status %d",
413 p_subpic, p_subpic->i_status );
416 while( p_subpic->p_region )
418 subpicture_region_t *p_region = p_subpic->p_region;
419 p_subpic->p_region = p_region->p_next;
420 spu_DestroyRegion( p_spu, p_region );
423 if( p_subpic->pf_destroy )
425 p_subpic->pf_destroy( p_subpic );
428 p_subpic->i_status = FREE_SUBPICTURE;
430 vlc_mutex_unlock( &p_spu->subpicture_lock );
433 /*****************************************************************************
434 * spu_RenderSubpictures: render a subpicture list
435 *****************************************************************************
436 * This function renders all sub picture units in the list.
437 *****************************************************************************/
438 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
439 picture_t *p_pic_dst, picture_t *p_pic_src,
440 subpicture_t *p_subpic,
441 int i_scale_width_orig, int i_scale_height_orig )
444 vlc_mutex_lock( &p_spu->subpicture_lock );
446 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
447 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
449 subpicture_region_t *p_region = p_subpic->p_region;
450 int i_scale_width, i_scale_height;
452 /* Load the blending module */
453 if( !p_spu->p_blend && p_region )
455 p_spu->p_blend = vlc_object_create( p_spu, sizeof(filter_t) );
456 vlc_object_attach( p_spu->p_blend, p_spu );
457 p_spu->p_blend->fmt_out.video.i_x_offset =
458 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
459 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
460 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
462 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
464 p_spu->p_blend->p_module =
465 module_Need( p_spu->p_blend, "video blending", 0, 0 );
468 /* Load the text rendering module */
469 if( !p_spu->p_text && p_region )
471 p_spu->p_text = vlc_object_create( p_spu, sizeof(filter_t) );
472 vlc_object_attach( p_spu->p_text, p_spu );
474 p_spu->p_text->fmt_out.video.i_width =
475 p_spu->p_text->fmt_out.video.i_visible_width =
477 p_spu->p_text->fmt_out.video.i_height =
478 p_spu->p_text->fmt_out.video.i_visible_height =
481 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
482 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
484 p_spu->p_text->p_module =
485 module_Need( p_spu->p_text, "text renderer", 0, 0 );
488 i_scale_width = i_scale_width_orig;
489 i_scale_height = i_scale_height_orig;
491 if( p_subpic->i_original_picture_width ||
492 p_subpic->i_original_picture_height )
494 i_scale_width = i_scale_width * p_fmt->i_width /
495 p_subpic->i_original_picture_width;
496 i_scale_height = i_scale_height * p_fmt->i_height /
497 p_subpic->i_original_picture_height;
500 /* Load the scaling module */
501 if( !p_spu->p_scale && (i_scale_width != 1000 ||
502 i_scale_height != 1000) )
504 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
505 vlc_object_attach( p_spu->p_scale, p_spu );
506 p_spu->p_scale->fmt_out.video.i_chroma =
507 p_spu->p_scale->fmt_in.video.i_chroma =
508 VLC_FOURCC('Y','U','V','P');
509 p_spu->p_scale->fmt_in.video.i_width =
510 p_spu->p_scale->fmt_in.video.i_height = 32;
511 p_spu->p_scale->fmt_out.video.i_width =
512 p_spu->p_scale->fmt_out.video.i_height = 16;
514 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
515 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
516 p_spu->p_scale->p_module =
517 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
520 if( p_subpic->pf_render )
522 /* HACK to remove when the ogt subpic decoder is gone */
523 if( p_spu->p_parent &&
524 p_spu->p_parent->i_object_type == VLC_OBJECT_VOUT )
526 vout_thread_t *p_vout = (vout_thread_t *)p_spu->p_parent;
527 p_subpic->pf_render( p_vout, p_pic_dst, p_subpic );
530 else while( p_region && p_spu->p_blend &&
531 p_spu->p_blend->pf_video_blend )
533 int i_x_offset = p_region->i_x + p_subpic->i_x;
534 int i_y_offset = p_region->i_y + p_subpic->i_y;
536 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
538 if( p_spu->p_text && p_spu->p_text->p_module &&
539 p_spu->p_text->pf_render_string )
541 /* TODO: do it in a less hacky way
542 * (modify text renderer API) */
543 subpicture_t *p_subpic_tmp;
544 subpicture_region_t tmp_region;
545 block_t *p_new_block =
546 block_New( p_spu, strlen(p_region->psz_text) + 1 );
550 memcpy( p_new_block->p_buffer, p_region->psz_text,
551 p_new_block->i_buffer );
552 p_new_block->i_pts = p_new_block->i_dts =
554 p_new_block->i_length =
555 p_subpic->i_start - p_subpic->i_stop;
556 p_subpic_tmp = p_spu->p_text->pf_render_string(
557 p_spu->p_text, p_new_block );
561 tmp_region = *p_region;
562 *p_region = *p_subpic_tmp->p_region;
563 p_region->p_next = tmp_region.p_next;
564 *p_subpic_tmp->p_region = tmp_region;
565 p_spu->p_text->pf_sub_buffer_del( p_spu->p_text,
572 if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
574 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
577 else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
579 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
582 if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
584 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
587 else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
589 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
592 if( p_subpic->b_absolute )
594 i_x_offset = p_region->i_x + p_subpic->i_x;
595 i_y_offset = p_region->i_y + p_subpic->i_y;
597 if( p_spu->i_margin >= 0 )
599 if( p_subpic->i_height + (unsigned int)p_spu->i_margin <=
602 i_y_offset = p_fmt->i_height -
603 p_spu->i_margin - p_subpic->i_height;
608 /* Force palette if requested */
609 if( p_spu->b_force_alpha && VLC_FOURCC('Y','U','V','P') ==
610 p_region->fmt.i_chroma )
612 p_region->fmt.p_palette->palette[0][3] = p_spu->pi_alpha[0];
613 p_region->fmt.p_palette->palette[1][3] = p_spu->pi_alpha[1];
614 p_region->fmt.p_palette->palette[2][3] = p_spu->pi_alpha[2];
615 p_region->fmt.p_palette->palette[3][3] = p_spu->pi_alpha[3];
618 /* Scale SPU if necessary */
619 if( p_region->p_cache )
621 if( i_scale_width * p_region->fmt.i_width / 1000 !=
622 p_region->p_cache->fmt.i_width ||
623 i_scale_height * p_region->fmt.i_height / 1000 !=
624 p_region->p_cache->fmt.i_height )
626 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
628 p_region->p_cache = 0;
631 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
632 p_spu->p_scale && !p_region->p_cache )
636 p_spu->p_scale->fmt_in.video = p_region->fmt;
637 p_spu->p_scale->fmt_out.video = p_region->fmt;
640 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
641 &p_spu->p_scale->fmt_out.video );
642 if( p_spu->p_scale->fmt_out.video.p_palette )
643 *p_spu->p_scale->fmt_out.video.p_palette =
644 *p_region->fmt.p_palette;
645 p_region->p_cache->p_next = p_region->p_next;
647 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
648 &p_region->picture );
650 p_spu->p_scale->fmt_out.video.i_width =
651 p_region->fmt.i_width * i_scale_width / 1000;
652 p_spu->p_scale->fmt_out.video.i_visible_width =
653 p_region->fmt.i_visible_width * i_scale_width / 1000;
654 p_spu->p_scale->fmt_out.video.i_height =
655 p_region->fmt.i_height * i_scale_height / 1000;
656 p_spu->p_scale->fmt_out.video.i_visible_height =
657 p_region->fmt.i_visible_height * i_scale_height / 1000;
658 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
660 p_pic = p_spu->p_scale->pf_video_filter(
661 p_spu->p_scale, &p_region->p_cache->picture );
664 picture_t p_pic_tmp = p_region->p_cache->picture;
665 p_region->p_cache->picture = *p_pic;
670 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
671 p_spu->p_scale && p_region->p_cache )
673 p_region = p_region->p_cache;
676 if( p_subpic->b_absolute )
678 i_x_offset = i_x_offset * i_scale_width / 1000;
679 i_y_offset = i_y_offset * i_scale_height / 1000;
682 p_spu->p_blend->fmt_in.video = p_region->fmt;
684 /* Force cropping if requested */
685 if( p_spu->b_force_crop )
687 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
688 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
689 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
690 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
691 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
693 /* Find the intersection */
694 if( i_crop_x + i_crop_width <= i_x_offset ||
695 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
696 i_crop_y + i_crop_height <= i_y_offset ||
697 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
699 /* No intersection */
700 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
704 int i_x, i_y, i_x_end, i_y_end;
705 i_x = __MAX( i_crop_x, i_x_offset );
706 i_y = __MAX( i_crop_y, i_y_offset );
707 i_x_end = __MIN( i_crop_x + i_crop_width,
708 i_x_offset + (int)p_fmt->i_visible_width );
709 i_y_end = __MIN( i_crop_y + i_crop_height,
710 i_y_offset + (int)p_fmt->i_visible_height );
712 p_fmt->i_x_offset = i_x - i_x_offset;
713 p_fmt->i_y_offset = i_y - i_y_offset;
714 p_fmt->i_visible_width = i_x_end - i_x;
715 p_fmt->i_visible_height = i_y_end - i_y;
722 /* Update the output picture size */
723 p_spu->p_blend->fmt_out.video.i_width =
724 p_spu->p_blend->fmt_out.video.i_visible_width =
726 p_spu->p_blend->fmt_out.video.i_height =
727 p_spu->p_blend->fmt_out.video.i_visible_height =
730 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
731 p_pic_src, &p_region->picture, i_x_offset, i_y_offset );
733 p_region = p_region->p_next;
736 p_subpic = p_subpic->p_next;
739 vlc_mutex_unlock( &p_spu->subpicture_lock );
742 /*****************************************************************************
743 * spu_SortSubpictures: find the subpictures to display
744 *****************************************************************************
745 * This function parses all subpictures and decides which ones need to be
746 * displayed. This operation does not need lock, since only READY_SUBPICTURE
747 * are handled. If no picture has been selected, display_date will depend on
749 * We also check for ephemer DVD subpictures (subpictures that have
750 * to be removed if a newer one is available), which makes it a lot
751 * more difficult to guess if a subpicture has to be rendered or not.
752 *****************************************************************************/
753 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
755 int i_index, i_channel;
756 subpicture_t *p_subpic = NULL;
757 subpicture_t *p_ephemer;
758 mtime_t ephemer_date;
760 /* Run subpicture filters */
761 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
763 subpicture_t *p_subpic_filter;
764 p_subpic_filter = p_spu->pp_filter[i_index]->
765 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
766 if( p_subpic_filter )
768 spu_DisplaySubpicture( p_spu, p_subpic_filter );
772 /* We get an easily parsable chained list of subpictures which
773 * ends with NULL since p_subpic was initialized to NULL. */
774 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
779 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
781 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
782 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
788 display_date < p_spu->p_subpicture[i_index].i_start )
790 /* Too early, come back next monday */
794 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
795 ephemer_date = p_spu->p_subpicture[i_index].i_start;
797 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
798 ( !p_spu->p_subpicture[i_index].b_ephemer ||
799 p_spu->p_subpicture[i_index].i_stop >
800 p_spu->p_subpicture[i_index].i_start ) )
802 /* Too late, destroy the subpic */
803 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
807 /* If this is an ephemer subpic, add it to our list */
808 if( p_spu->p_subpicture[i_index].b_ephemer )
810 p_spu->p_subpicture[i_index].p_next = p_ephemer;
811 p_ephemer = &p_spu->p_subpicture[i_index];
816 p_spu->p_subpicture[i_index].p_next = p_subpic;
817 p_subpic = &p_spu->p_subpicture[i_index];
820 /* If we found ephemer subpictures, check if they have to be
821 * displayed or destroyed */
822 while( p_ephemer != NULL )
824 subpicture_t *p_tmp = p_ephemer;
825 p_ephemer = p_ephemer->p_next;
827 if( p_tmp->i_start < ephemer_date )
829 /* Ephemer subpicture has lived too long */
830 spu_DestroySubpicture( p_spu, p_tmp );
834 /* Ephemer subpicture can still live a bit */
835 p_tmp->p_next = p_subpic;
844 /*****************************************************************************
845 * SpuClearChannel: clear an spu channel
846 *****************************************************************************
847 * This function destroys the subpictures which belong to the spu channel
848 * corresponding to i_channel_id.
849 *****************************************************************************/
850 static void SpuClearChannel( spu_t *p_spu, int i_channel )
852 int i_subpic; /* subpicture index */
853 subpicture_t *p_subpic = NULL; /* first free subpicture */
855 vlc_mutex_lock( &p_spu->subpicture_lock );
857 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
859 p_subpic = &p_spu->p_subpicture[i_subpic];
860 if( p_subpic->i_status == FREE_SUBPICTURE
861 || ( p_subpic->i_status != RESERVED_SUBPICTURE
862 && p_subpic->i_status != READY_SUBPICTURE ) )
867 if( p_subpic->i_channel == i_channel )
869 while( p_subpic->p_region )
871 subpicture_region_t *p_region = p_subpic->p_region;
872 p_subpic->p_region = p_region->p_next;
873 spu_DestroyRegion( p_spu, p_region );
876 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
877 p_subpic->i_status = FREE_SUBPICTURE;
881 vlc_mutex_unlock( &p_spu->subpicture_lock );
884 /*****************************************************************************
885 * spu_ControlDefault: default methods for the subpicture unit control.
886 *****************************************************************************/
887 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
893 case SPU_CHANNEL_REGISTER:
894 pi = (int *)va_arg( args, int * );
895 if( pi ) *pi = p_spu->i_channel++;
896 msg_Dbg( p_spu, "Registering subpicture channel, ID: %i",
897 p_spu->i_channel - 1 );
900 case SPU_CHANNEL_CLEAR:
901 i = (int)va_arg( args, int );
902 SpuClearChannel( p_spu, i );
906 msg_Dbg( p_spu, "control query not supported" );
913 /*****************************************************************************
914 * Object variables callbacks
915 *****************************************************************************/
917 /*****************************************************************************
918 * UpdateSPU: update subpicture settings
919 *****************************************************************************
920 * This function is called from CropCallback and at initialization time, to
921 * retrieve crop information from the input.
922 *****************************************************************************/
923 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
927 p_spu->b_force_alpha = VLC_FALSE;
928 p_spu->b_force_crop = VLC_FALSE;
930 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
932 p_spu->b_force_crop = VLC_TRUE;
933 var_Get( p_object, "x-start", &val );
934 p_spu->i_crop_x = val.i_int;
935 var_Get( p_object, "y-start", &val );
936 p_spu->i_crop_y = val.i_int;
937 var_Get( p_object, "x-end", &val );
938 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
939 var_Get( p_object, "y-end", &val );
940 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
943 if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
946 for( i = 0; i < 4; i++ )
948 p_spu->pi_color[i] = ((uint8_t *)val.p_address)[i];
953 if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
956 for( i = 0; i < 4; i++ )
958 p_spu->pi_alpha[i] = ((uint8_t *)val.p_address)[i];
959 p_spu->pi_alpha[i] = p_spu->pi_alpha[i] == 0xf ?
960 0xff : p_spu->pi_alpha[i] << 4;
962 p_spu->b_force_alpha = VLC_TRUE;
965 msg_Dbg( p_object, "crop: %i,%i,%i,%i, alpha: %i",
966 p_spu->i_crop_x, p_spu->i_crop_y,
967 p_spu->i_crop_width, p_spu->i_crop_height, p_spu->b_force_alpha );
970 /*****************************************************************************
971 * CropCallback: called when the highlight properties are changed
972 *****************************************************************************
973 * This callback is called from the input thread when we need cropping
974 *****************************************************************************/
975 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
976 vlc_value_t oldval, vlc_value_t newval, void *p_data )
978 UpdateSPU( (spu_t *)p_data, p_object );
982 /*****************************************************************************
983 * Buffers allocation callbacks for the filters
984 *****************************************************************************/
985 static subpicture_t *sub_new_buffer( filter_t *p_filter )
987 filter_owner_sys_t *p_sys = p_filter->p_owner;
988 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
989 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
993 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
995 filter_owner_sys_t *p_sys = p_filter->p_owner;
996 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
999 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1001 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1002 memset( p_subpic, 0, sizeof(subpicture_t) );
1003 p_subpic->b_absolute = VLC_TRUE;
1005 p_subpic->pf_create_region = __spu_CreateRegion;
1006 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1011 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1013 while( p_subpic->p_region )
1015 subpicture_region_t *p_region = p_subpic->p_region;
1016 p_subpic->p_region = p_region->p_next;
1017 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1023 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1025 picture_t *p_picture = malloc( sizeof(picture_t) );
1027 if( vout_AllocatePicture( p_filter, p_picture,
1028 p_filter->fmt_out.video.i_chroma,
1029 p_filter->fmt_out.video.i_width,
1030 p_filter->fmt_out.video.i_height,
1031 p_filter->fmt_out.video.i_aspect )
1038 p_picture->pf_release = RegionPictureRelease;
1043 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1045 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1046 if( p_pic ) free( p_pic );