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_font_color = -1; /* default to using freetype-color -opacity */
261 p_region->i_font_opacity = -1;
262 p_region->i_font_size = -1; /* and the freetype fontsize */
264 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
265 p_fmt->p_palette = p_region->fmt.p_palette =
266 malloc( sizeof(video_palette_t) );
267 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
269 p_region->picture.p_data_orig = 0;
271 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
273 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
274 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
276 if( !p_region->picture.i_planes )
279 free( p_fmt->p_palette );
283 p_region->picture.pf_release = RegionPictureRelease;
289 * Destroy a subpicture region
291 * \param p_this vlc_object_t
292 * \param p_region the subpicture region to destroy
294 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
296 if( !p_region ) return;
297 if( p_region->picture.pf_release )
298 p_region->picture.pf_release( &p_region->picture );
299 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
300 if( p_region->psz_text ) free( p_region->psz_text );
301 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
306 * Display a subpicture
308 * Remove the reservation flag of a subpicture, which will cause it to be
310 * \param p_spu the subpicture unit object
311 * \param p_subpic the subpicture to display
313 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
315 /* Check if status is valid */
316 if( p_subpic->i_status != RESERVED_SUBPICTURE )
318 msg_Err( p_spu, "subpicture %p has invalid status #%d",
319 p_subpic, p_subpic->i_status );
322 /* Remove reservation flag */
323 p_subpic->i_status = READY_SUBPICTURE;
325 if( p_subpic->i_channel == DEFAULT_CHAN )
327 p_subpic->i_channel = 0xFFFF;
328 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
329 p_subpic->i_channel = DEFAULT_CHAN;
334 * Allocate a subpicture in the spu heap.
336 * This function create a reserved subpicture in the spu heap.
337 * A null pointer is returned if the function fails. This method provides an
338 * already allocated zone of memory in the spu data fields. It needs locking
339 * since several pictures can be created by several producers threads.
340 * \param p_spu the subpicture unit in which to create the subpicture
341 * \return NULL on error, a reserved subpicture otherwise
343 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
345 int i_subpic; /* subpicture index */
346 subpicture_t * p_subpic = NULL; /* first free subpicture */
349 vlc_mutex_lock( &p_spu->subpicture_lock );
352 * Look for an empty place
355 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
357 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
359 /* Subpicture is empty and ready for allocation */
360 p_subpic = &p_spu->p_subpicture[i_subpic];
361 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
366 /* If no free subpicture could be found */
367 if( p_subpic == NULL )
369 msg_Err( p_spu, "subpicture heap is full" );
370 vlc_mutex_unlock( &p_spu->subpicture_lock );
374 /* Copy subpicture information, set some default values */
375 memset( p_subpic, 0, sizeof(subpicture_t) );
376 p_subpic->i_status = RESERVED_SUBPICTURE;
377 p_subpic->b_absolute = VLC_TRUE;
378 p_subpic->b_fade = VLC_FALSE;
379 p_subpic->i_alpha = 0xFF;
380 p_subpic->p_region = 0;
381 p_subpic->pf_render = 0;
382 p_subpic->pf_destroy = 0;
384 vlc_mutex_unlock( &p_spu->subpicture_lock );
386 p_subpic->pf_create_region = __spu_CreateRegion;
387 p_subpic->pf_destroy_region = __spu_DestroyRegion;
393 * Remove a subpicture from the heap
395 * This function frees a previously reserved subpicture.
396 * It is meant to be used when the construction of a picture aborted.
397 * This function does not need locking since reserved subpictures are ignored
400 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
403 vlc_mutex_lock( &p_spu->subpicture_lock );
405 /* There can be race conditions so we need to check the status */
406 if( p_subpic->i_status == FREE_SUBPICTURE )
408 vlc_mutex_unlock( &p_spu->subpicture_lock );
412 /* Check if status is valid */
413 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
414 && ( p_subpic->i_status != READY_SUBPICTURE ) )
416 msg_Err( p_spu, "subpicture %p has invalid status %d",
417 p_subpic, p_subpic->i_status );
420 while( p_subpic->p_region )
422 subpicture_region_t *p_region = p_subpic->p_region;
423 p_subpic->p_region = p_region->p_next;
424 spu_DestroyRegion( p_spu, p_region );
427 if( p_subpic->pf_destroy )
429 p_subpic->pf_destroy( p_subpic );
432 p_subpic->i_status = FREE_SUBPICTURE;
434 vlc_mutex_unlock( &p_spu->subpicture_lock );
437 /*****************************************************************************
438 * spu_RenderSubpictures: render a subpicture list
439 *****************************************************************************
440 * This function renders all sub picture units in the list.
441 *****************************************************************************/
442 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
443 picture_t *p_pic_dst, picture_t *p_pic_src,
444 subpicture_t *p_subpic,
445 int i_scale_width_orig, int i_scale_height_orig )
449 vlc_mutex_lock( &p_spu->subpicture_lock );
451 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
452 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
454 subpicture_region_t *p_region = p_subpic->p_region;
455 int i_scale_width, i_scale_height;
457 /* Load the blending module */
458 if( !p_spu->p_blend && p_region )
460 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
461 vlc_object_attach( p_spu->p_blend, p_spu );
462 p_spu->p_blend->fmt_out.video.i_x_offset =
463 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
464 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
465 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
466 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
468 p_spu->p_blend->p_module =
469 module_Need( p_spu->p_blend, "video blending", 0, 0 );
472 /* Load the text rendering module */
473 if( !p_spu->p_text && p_region )
475 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
476 vlc_object_attach( p_spu->p_text, p_spu );
478 p_spu->p_text->fmt_out.video.i_width =
479 p_spu->p_text->fmt_out.video.i_visible_width =
481 p_spu->p_text->fmt_out.video.i_height =
482 p_spu->p_text->fmt_out.video.i_visible_height =
485 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
486 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
487 p_spu->p_text->p_module =
488 module_Need( p_spu->p_text, "text renderer", 0, 0 );
491 i_scale_width = i_scale_width_orig;
492 i_scale_height = i_scale_height_orig;
494 if( p_subpic->i_original_picture_width &&
495 p_subpic->i_original_picture_height )
497 i_scale_width = i_scale_width * p_fmt->i_width /
498 p_subpic->i_original_picture_width;
499 i_scale_height = i_scale_height * p_fmt->i_height /
500 p_subpic->i_original_picture_height;
503 /* Take care of the aspect ratio */
504 if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
505 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
507 i_scale_width = i_scale_width *
508 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
509 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
512 /* Load the scaling module */
513 if( !p_spu->p_scale && (i_scale_width != 1000 ||
514 i_scale_height != 1000) )
516 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
517 vlc_object_attach( p_spu->p_scale, p_spu );
518 p_spu->p_scale->fmt_out.video.i_chroma =
519 p_spu->p_scale->fmt_in.video.i_chroma =
520 VLC_FOURCC('Y','U','V','P');
521 p_spu->p_scale->fmt_in.video.i_width =
522 p_spu->p_scale->fmt_in.video.i_height = 32;
523 p_spu->p_scale->fmt_out.video.i_width =
524 p_spu->p_scale->fmt_out.video.i_height = 16;
526 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
527 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
528 p_spu->p_scale->p_module =
529 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
532 if( p_subpic->pf_render )
534 /* HACK to remove when the ogt subpic decoder is gone */
535 if( p_spu->p_parent &&
536 p_spu->p_parent->i_object_type == VLC_OBJECT_VOUT )
538 vout_thread_t *p_vout = (vout_thread_t *)p_spu->p_parent;
539 p_subpic->pf_render( p_vout, p_pic_dst, p_subpic );
542 else while( p_region && p_spu->p_blend &&
543 p_spu->p_blend->pf_video_blend )
545 int i_fade_alpha = 255;
546 int i_x_offset = p_region->i_x + p_subpic->i_x;
547 int i_y_offset = p_region->i_y + p_subpic->i_y;
549 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
551 if( p_spu->p_text && p_spu->p_text->p_module &&
552 p_spu->p_text->pf_render_string )
554 /* TODO: do it in a less hacky way
555 * (modify text renderer API) */
556 subpicture_region_t *p_tmp_region;
557 /* the actual call to RenderText in freetype.c: */
558 p_tmp_region = p_spu->p_text->pf_render_string(
559 p_spu->p_text, p_subpic, p_region );
563 // p_subpic->pf_destroy_region( p_spu, p_region );
564 p_region = p_tmp_region;
569 /* Force palette if requested */
570 if( p_spu->b_force_alpha && VLC_FOURCC('Y','U','V','P') ==
571 p_region->fmt.i_chroma )
573 p_region->fmt.p_palette->palette[0][3] = p_spu->pi_alpha[0];
574 p_region->fmt.p_palette->palette[1][3] = p_spu->pi_alpha[1];
575 p_region->fmt.p_palette->palette[2][3] = p_spu->pi_alpha[2];
576 p_region->fmt.p_palette->palette[3][3] = p_spu->pi_alpha[3];
579 /* Scale SPU if necessary */
580 if( p_region->p_cache )
582 if( i_scale_width * p_region->fmt.i_width / 1000 !=
583 p_region->p_cache->fmt.i_width ||
584 i_scale_height * p_region->fmt.i_height / 1000 !=
585 p_region->p_cache->fmt.i_height )
587 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
589 p_region->p_cache = 0;
593 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
594 p_spu->p_scale && !p_region->p_cache )
598 p_spu->p_scale->fmt_in.video = p_region->fmt;
599 p_spu->p_scale->fmt_out.video = p_region->fmt;
602 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
603 &p_spu->p_scale->fmt_out.video );
604 if( p_spu->p_scale->fmt_out.video.p_palette )
605 *p_spu->p_scale->fmt_out.video.p_palette =
606 *p_region->fmt.p_palette;
607 p_region->p_cache->p_next = p_region->p_next;
609 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
610 &p_region->picture );
612 p_spu->p_scale->fmt_out.video.i_width =
613 p_region->fmt.i_width * i_scale_width / 1000;
614 p_spu->p_scale->fmt_out.video.i_visible_width =
615 p_region->fmt.i_visible_width * i_scale_width / 1000;
616 p_spu->p_scale->fmt_out.video.i_height =
617 p_region->fmt.i_height * i_scale_height / 1000;
618 p_spu->p_scale->fmt_out.video.i_visible_height =
619 p_region->fmt.i_visible_height * i_scale_height / 1000;
620 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
621 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
622 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
624 p_pic = p_spu->p_scale->pf_video_filter(
625 p_spu->p_scale, &p_region->p_cache->picture );
628 picture_t p_pic_tmp = p_region->p_cache->picture;
629 p_region->p_cache->picture = *p_pic;
634 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
635 p_spu->p_scale && p_region->p_cache )
637 p_region = p_region->p_cache;
640 if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
642 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
645 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
647 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
650 if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
652 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
655 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
657 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
660 if( p_subpic->b_absolute )
662 i_x_offset = p_region->i_x +
663 p_subpic->i_x * i_scale_width / 1000;
664 i_y_offset = p_region->i_y +
665 p_subpic->i_y * i_scale_height / 1000;
667 if( p_spu->i_margin >= 0 )
669 if( p_subpic->i_height + (unsigned int)p_spu->i_margin <=
672 i_y_offset = p_fmt->i_height -
673 p_spu->i_margin - p_subpic->i_height;
678 p_spu->p_blend->fmt_in.video = p_region->fmt;
680 /* Force cropping if requested */
681 if( p_spu->b_force_crop )
683 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
684 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
685 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
686 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
687 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
689 /* Find the intersection */
690 if( i_crop_x + i_crop_width <= i_x_offset ||
691 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
692 i_crop_y + i_crop_height <= i_y_offset ||
693 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
695 /* No intersection */
696 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
700 int i_x, i_y, i_x_end, i_y_end;
701 i_x = __MAX( i_crop_x, i_x_offset );
702 i_y = __MAX( i_crop_y, i_y_offset );
703 i_x_end = __MIN( i_crop_x + i_crop_width,
704 i_x_offset + (int)p_fmt->i_visible_width );
705 i_y_end = __MIN( i_crop_y + i_crop_height,
706 i_y_offset + (int)p_fmt->i_visible_height );
708 p_fmt->i_x_offset = i_x - i_x_offset;
709 p_fmt->i_y_offset = i_y - i_y_offset;
710 p_fmt->i_visible_width = i_x_end - i_x;
711 p_fmt->i_visible_height = i_y_end - i_y;
718 /* Update the output picture size */
719 p_spu->p_blend->fmt_out.video.i_width =
720 p_spu->p_blend->fmt_out.video.i_visible_width =
722 p_spu->p_blend->fmt_out.video.i_height =
723 p_spu->p_blend->fmt_out.video.i_visible_height =
726 if( p_subpic->b_fade )
728 mtime_t i_fade_start = ( p_subpic->i_stop +
729 p_subpic->i_start ) / 2;
730 mtime_t i_now = mdate();
731 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
733 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
734 ( p_subpic->i_stop - i_fade_start );
738 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
739 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
740 i_fade_alpha * p_subpic->i_alpha / 255 );
742 p_region = p_region->p_next;
745 p_subpic = p_subpic->p_next;
748 vlc_mutex_unlock( &p_spu->subpicture_lock );
751 /*****************************************************************************
752 * spu_SortSubpictures: find the subpictures to display
753 *****************************************************************************
754 * This function parses all subpictures and decides which ones need to be
755 * displayed. This operation does not need lock, since only READY_SUBPICTURE
756 * are handled. If no picture has been selected, display_date will depend on
758 * We also check for ephemer DVD subpictures (subpictures that have
759 * to be removed if a newer one is available), which makes it a lot
760 * more difficult to guess if a subpicture has to be rendered or not.
761 *****************************************************************************/
762 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
764 int i_index, i_channel;
765 subpicture_t *p_subpic = NULL;
766 subpicture_t *p_ephemer;
767 mtime_t ephemer_date;
769 /* Run subpicture filters */
770 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
772 subpicture_t *p_subpic_filter;
773 p_subpic_filter = p_spu->pp_filter[i_index]->
774 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
775 if( p_subpic_filter )
777 spu_DisplaySubpicture( p_spu, p_subpic_filter );
781 /* We get an easily parsable chained list of subpictures which
782 * ends with NULL since p_subpic was initialized to NULL. */
783 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
788 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
790 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
791 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
796 display_date < p_spu->p_subpicture[i_index].i_start )
798 /* Too early, come back next monday */
802 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
803 ephemer_date = p_spu->p_subpicture[i_index].i_start;
805 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
806 ( !p_spu->p_subpicture[i_index].b_ephemer ||
807 p_spu->p_subpicture[i_index].i_stop >
808 p_spu->p_subpicture[i_index].i_start ) )
810 /* Too late, destroy the subpic */
811 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
815 /* If this is an ephemer subpic, add it to our list */
816 if( p_spu->p_subpicture[i_index].b_ephemer )
818 p_spu->p_subpicture[i_index].p_next = p_ephemer;
819 p_ephemer = &p_spu->p_subpicture[i_index];
824 p_spu->p_subpicture[i_index].p_next = p_subpic;
825 p_subpic = &p_spu->p_subpicture[i_index];
828 /* If we found ephemer subpictures, check if they have to be
829 * displayed or destroyed */
830 while( p_ephemer != NULL )
832 subpicture_t *p_tmp = p_ephemer;
833 p_ephemer = p_ephemer->p_next;
835 if( p_tmp->i_start < ephemer_date )
837 /* Ephemer subpicture has lived too long */
838 spu_DestroySubpicture( p_spu, p_tmp );
842 /* Ephemer subpicture can still live a bit */
843 p_tmp->p_next = p_subpic;
852 /*****************************************************************************
853 * SpuClearChannel: clear an spu channel
854 *****************************************************************************
855 * This function destroys the subpictures which belong to the spu channel
856 * corresponding to i_channel_id.
857 *****************************************************************************/
858 static void SpuClearChannel( spu_t *p_spu, int i_channel )
860 int i_subpic; /* subpicture index */
861 subpicture_t *p_subpic = NULL; /* first free subpicture */
863 vlc_mutex_lock( &p_spu->subpicture_lock );
865 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
867 p_subpic = &p_spu->p_subpicture[i_subpic];
868 if( p_subpic->i_status == FREE_SUBPICTURE
869 || ( p_subpic->i_status != RESERVED_SUBPICTURE
870 && p_subpic->i_status != READY_SUBPICTURE ) )
875 if( p_subpic->i_channel == i_channel )
877 while( p_subpic->p_region )
879 subpicture_region_t *p_region = p_subpic->p_region;
880 p_subpic->p_region = p_region->p_next;
881 spu_DestroyRegion( p_spu, p_region );
884 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
885 p_subpic->i_status = FREE_SUBPICTURE;
889 vlc_mutex_unlock( &p_spu->subpicture_lock );
892 /*****************************************************************************
893 * spu_ControlDefault: default methods for the subpicture unit control.
894 *****************************************************************************/
895 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
901 case SPU_CHANNEL_REGISTER:
902 pi = (int *)va_arg( args, int * );
903 if( pi ) *pi = p_spu->i_channel++;
904 msg_Dbg( p_spu, "Registering subpicture channel, ID: %i",
905 p_spu->i_channel - 1 );
908 case SPU_CHANNEL_CLEAR:
909 i = (int)va_arg( args, int );
910 SpuClearChannel( p_spu, i );
914 msg_Dbg( p_spu, "control query not supported" );
921 /*****************************************************************************
922 * Object variables callbacks
923 *****************************************************************************/
925 /*****************************************************************************
926 * UpdateSPU: update subpicture settings
927 *****************************************************************************
928 * This function is called from CropCallback and at initialization time, to
929 * retrieve crop information from the input.
930 *****************************************************************************/
931 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
935 p_spu->b_force_alpha = VLC_FALSE;
936 p_spu->b_force_crop = VLC_FALSE;
938 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
940 p_spu->b_force_crop = VLC_TRUE;
941 var_Get( p_object, "x-start", &val );
942 p_spu->i_crop_x = val.i_int;
943 var_Get( p_object, "y-start", &val );
944 p_spu->i_crop_y = val.i_int;
945 var_Get( p_object, "x-end", &val );
946 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
947 var_Get( p_object, "y-end", &val );
948 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
951 if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
954 for( i = 0; i < 4; i++ )
956 p_spu->pi_color[i] = ((uint8_t *)val.p_address)[i];
961 if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
964 for( i = 0; i < 4; i++ )
966 p_spu->pi_alpha[i] = ((uint8_t *)val.p_address)[i];
967 p_spu->pi_alpha[i] = p_spu->pi_alpha[i] == 0xf ?
968 0xff : p_spu->pi_alpha[i] << 4;
970 p_spu->b_force_alpha = VLC_TRUE;
973 msg_Dbg( p_object, "crop: %i,%i,%i,%i, alpha: %i",
974 p_spu->i_crop_x, p_spu->i_crop_y,
975 p_spu->i_crop_width, p_spu->i_crop_height, p_spu->b_force_alpha );
978 /*****************************************************************************
979 * CropCallback: called when the highlight properties are changed
980 *****************************************************************************
981 * This callback is called from the input thread when we need cropping
982 *****************************************************************************/
983 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
984 vlc_value_t oldval, vlc_value_t newval, void *p_data )
986 UpdateSPU( (spu_t *)p_data, p_object );
990 /*****************************************************************************
991 * Buffers allocation callbacks for the filters
992 *****************************************************************************/
993 static subpicture_t *sub_new_buffer( filter_t *p_filter )
995 filter_owner_sys_t *p_sys = p_filter->p_owner;
996 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
997 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1001 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1003 filter_owner_sys_t *p_sys = p_filter->p_owner;
1004 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1007 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1009 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1010 memset( p_subpic, 0, sizeof(subpicture_t) );
1011 p_subpic->b_absolute = VLC_TRUE;
1013 p_subpic->pf_create_region = __spu_CreateRegion;
1014 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1019 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1021 while( p_subpic->p_region )
1023 subpicture_region_t *p_region = p_subpic->p_region;
1024 p_subpic->p_region = p_region->p_next;
1025 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1031 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1033 picture_t *p_picture = malloc( sizeof(picture_t) );
1035 if( vout_AllocatePicture( p_filter, p_picture,
1036 p_filter->fmt_out.video.i_chroma,
1037 p_filter->fmt_out.video.i_width,
1038 p_filter->fmt_out.video.i_height,
1039 p_filter->fmt_out.video.i_aspect )
1046 p_picture->pf_release = RegionPictureRelease;
1051 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1053 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1054 if( p_pic ) free( p_pic );