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;
263 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
264 p_fmt->p_palette = p_region->fmt.p_palette =
265 malloc( sizeof(video_palette_t) );
266 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
268 p_region->picture.p_data_orig = 0;
270 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
272 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
273 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
275 if( !p_region->picture.i_planes )
278 free( p_fmt->p_palette );
282 p_region->picture.pf_release = RegionPictureRelease;
288 * Destroy a subpicture region
290 * \param p_this vlc_object_t
291 * \param p_region the subpicture region to destroy
293 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
295 if( !p_region ) return;
296 if( p_region->picture.pf_release )
297 p_region->picture.pf_release( &p_region->picture );
298 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
299 if( p_region->psz_text ) free( p_region->psz_text );
300 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
305 * Display a subpicture
307 * Remove the reservation flag of a subpicture, which will cause it to be
309 * \param p_spu the subpicture unit object
310 * \param p_subpic the subpicture to display
312 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
314 /* Check if status is valid */
315 if( p_subpic->i_status != RESERVED_SUBPICTURE )
317 msg_Err( p_spu, "subpicture %p has invalid status #%d",
318 p_subpic, p_subpic->i_status );
321 /* Remove reservation flag */
322 p_subpic->i_status = READY_SUBPICTURE;
324 if( p_subpic->i_channel == DEFAULT_CHAN )
326 p_subpic->i_channel = 0xFFFF;
327 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
328 p_subpic->i_channel = DEFAULT_CHAN;
333 * Allocate a subpicture in the spu heap.
335 * This function create a reserved subpicture in the spu heap.
336 * A null pointer is returned if the function fails. This method provides an
337 * already allocated zone of memory in the spu data fields. It needs locking
338 * since several pictures can be created by several producers threads.
339 * \param p_spu the subpicture unit in which to create the subpicture
340 * \return NULL on error, a reserved subpicture otherwise
342 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
344 int i_subpic; /* subpicture index */
345 subpicture_t * p_subpic = NULL; /* first free subpicture */
348 vlc_mutex_lock( &p_spu->subpicture_lock );
351 * Look for an empty place
354 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
356 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
358 /* Subpicture is empty and ready for allocation */
359 p_subpic = &p_spu->p_subpicture[i_subpic];
360 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
365 /* If no free subpicture could be found */
366 if( p_subpic == NULL )
368 msg_Err( p_spu, "subpicture heap is full" );
369 vlc_mutex_unlock( &p_spu->subpicture_lock );
373 /* Copy subpicture information, set some default values */
374 memset( p_subpic, 0, sizeof(subpicture_t) );
375 p_subpic->i_status = RESERVED_SUBPICTURE;
376 p_subpic->b_absolute = VLC_TRUE;
377 p_subpic->b_fade = VLC_FALSE;
378 p_subpic->i_alpha = 0xFF;
379 p_subpic->p_region = 0;
380 p_subpic->pf_render = 0;
381 p_subpic->pf_destroy = 0;
383 vlc_mutex_unlock( &p_spu->subpicture_lock );
385 p_subpic->pf_create_region = __spu_CreateRegion;
386 p_subpic->pf_destroy_region = __spu_DestroyRegion;
392 * Remove a subpicture from the heap
394 * This function frees a previously reserved subpicture.
395 * It is meant to be used when the construction of a picture aborted.
396 * This function does not need locking since reserved subpictures are ignored
399 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
402 vlc_mutex_lock( &p_spu->subpicture_lock );
404 /* There can be race conditions so we need to check the status */
405 if( p_subpic->i_status == FREE_SUBPICTURE )
407 vlc_mutex_unlock( &p_spu->subpicture_lock );
411 /* Check if status is valid */
412 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
413 && ( p_subpic->i_status != READY_SUBPICTURE ) )
415 msg_Err( p_spu, "subpicture %p has invalid status %d",
416 p_subpic, p_subpic->i_status );
419 while( p_subpic->p_region )
421 subpicture_region_t *p_region = p_subpic->p_region;
422 p_subpic->p_region = p_region->p_next;
423 spu_DestroyRegion( p_spu, p_region );
426 if( p_subpic->pf_destroy )
428 p_subpic->pf_destroy( p_subpic );
431 p_subpic->i_status = FREE_SUBPICTURE;
433 vlc_mutex_unlock( &p_spu->subpicture_lock );
436 /*****************************************************************************
437 * spu_RenderSubpictures: render a subpicture list
438 *****************************************************************************
439 * This function renders all sub picture units in the list.
440 *****************************************************************************/
441 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
442 picture_t *p_pic_dst, picture_t *p_pic_src,
443 subpicture_t *p_subpic,
444 int i_scale_width_orig, int i_scale_height_orig )
448 vlc_mutex_lock( &p_spu->subpicture_lock );
450 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
451 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
453 subpicture_region_t *p_region = p_subpic->p_region;
454 int i_scale_width, i_scale_height;
456 /* Load the blending module */
457 if( !p_spu->p_blend && p_region )
459 p_spu->p_blend = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
460 vlc_object_attach( p_spu->p_blend, p_spu );
461 p_spu->p_blend->fmt_out.video.i_x_offset =
462 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
463 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
464 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
465 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
467 p_spu->p_blend->p_module =
468 module_Need( p_spu->p_blend, "video blending", 0, 0 );
471 /* Load the text rendering module */
472 if( !p_spu->p_text && p_region )
474 p_spu->p_text = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
475 vlc_object_attach( p_spu->p_text, p_spu );
477 p_spu->p_text->fmt_out.video.i_width =
478 p_spu->p_text->fmt_out.video.i_visible_width =
480 p_spu->p_text->fmt_out.video.i_height =
481 p_spu->p_text->fmt_out.video.i_visible_height =
484 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
485 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
486 p_spu->p_text->p_module =
487 module_Need( p_spu->p_text, "text renderer", 0, 0 );
490 i_scale_width = i_scale_width_orig;
491 i_scale_height = i_scale_height_orig;
493 if( p_subpic->i_original_picture_width &&
494 p_subpic->i_original_picture_height )
496 i_scale_width = i_scale_width * p_fmt->i_width /
497 p_subpic->i_original_picture_width;
498 i_scale_height = i_scale_height * p_fmt->i_height /
499 p_subpic->i_original_picture_height;
502 /* Take care of the aspect ratio */
503 if( p_region && p_region->fmt.i_sar_num * p_fmt->i_sar_den !=
504 p_region->fmt.i_sar_den * p_fmt->i_sar_num )
506 i_scale_width = i_scale_width *
507 (int64_t)p_region->fmt.i_sar_num * p_fmt->i_sar_den /
508 p_region->fmt.i_sar_den / p_fmt->i_sar_num;
511 /* Load the scaling module */
512 if( !p_spu->p_scale && (i_scale_width != 1000 ||
513 i_scale_height != 1000) )
515 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
516 vlc_object_attach( p_spu->p_scale, p_spu );
517 p_spu->p_scale->fmt_out.video.i_chroma =
518 p_spu->p_scale->fmt_in.video.i_chroma =
519 VLC_FOURCC('Y','U','V','P');
520 p_spu->p_scale->fmt_in.video.i_width =
521 p_spu->p_scale->fmt_in.video.i_height = 32;
522 p_spu->p_scale->fmt_out.video.i_width =
523 p_spu->p_scale->fmt_out.video.i_height = 16;
525 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
526 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
527 p_spu->p_scale->p_module =
528 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
531 if( p_subpic->pf_render )
533 /* HACK to remove when the ogt subpic decoder is gone */
534 if( p_spu->p_parent &&
535 p_spu->p_parent->i_object_type == VLC_OBJECT_VOUT )
537 vout_thread_t *p_vout = (vout_thread_t *)p_spu->p_parent;
538 p_subpic->pf_render( p_vout, p_pic_dst, p_subpic );
541 else while( p_region && p_spu->p_blend &&
542 p_spu->p_blend->pf_video_blend )
544 int i_fade_alpha = 255;
545 int i_x_offset = p_region->i_x + p_subpic->i_x;
546 int i_y_offset = p_region->i_y + p_subpic->i_y;
548 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
550 if( p_spu->p_text && p_spu->p_text->p_module &&
551 p_spu->p_text->pf_render_string )
553 /* TODO: do it in a less hacky way
554 * (modify text renderer API) */
555 subpicture_t *p_subpic_tmp;
556 subpicture_region_t tmp_region;
557 block_t *p_new_block =
558 block_New( p_spu, strlen(p_region->psz_text) + 1 );
561 memcpy( p_new_block->p_buffer, p_region->psz_text,
562 p_new_block->i_buffer );
563 p_new_block->i_pts = p_new_block->i_dts =
565 p_new_block->i_length =
566 p_subpic->i_start - p_subpic->i_stop;
567 /* the actual call to RenderText in freetype.c: */
568 p_subpic_tmp = p_spu->p_text->pf_render_string(
569 p_spu->p_text, p_new_block,
570 p_region->i_font_color, p_region->i_font_opacity);
574 tmp_region = *p_region;
575 *p_region = *p_subpic_tmp->p_region;
576 p_region->p_next = tmp_region.p_next;
577 *p_subpic_tmp->p_region = tmp_region;
578 p_spu->p_text->pf_sub_buffer_del( p_spu->p_text,
585 /* Force palette if requested */
586 if( p_spu->b_force_alpha && VLC_FOURCC('Y','U','V','P') ==
587 p_region->fmt.i_chroma )
589 p_region->fmt.p_palette->palette[0][3] = p_spu->pi_alpha[0];
590 p_region->fmt.p_palette->palette[1][3] = p_spu->pi_alpha[1];
591 p_region->fmt.p_palette->palette[2][3] = p_spu->pi_alpha[2];
592 p_region->fmt.p_palette->palette[3][3] = p_spu->pi_alpha[3];
595 /* Scale SPU if necessary */
596 if( p_region->p_cache )
598 if( i_scale_width * p_region->fmt.i_width / 1000 !=
599 p_region->p_cache->fmt.i_width ||
600 i_scale_height * p_region->fmt.i_height / 1000 !=
601 p_region->p_cache->fmt.i_height )
603 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
605 p_region->p_cache = 0;
609 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
610 p_spu->p_scale && !p_region->p_cache )
615 p_spu->p_scale->fmt_in.video = p_region->fmt;
616 p_spu->p_scale->fmt_out.video = p_region->fmt;
619 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
620 &p_spu->p_scale->fmt_out.video );
621 if( p_spu->p_scale->fmt_out.video.p_palette )
622 *p_spu->p_scale->fmt_out.video.p_palette =
623 *p_region->fmt.p_palette;
624 p_region->p_cache->p_next = p_region->p_next;
626 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
627 &p_region->picture );
629 p_spu->p_scale->fmt_out.video.i_width =
630 p_region->fmt.i_width * i_scale_width / 1000;
631 p_spu->p_scale->fmt_out.video.i_visible_width =
632 p_region->fmt.i_visible_width * i_scale_width / 1000;
633 p_spu->p_scale->fmt_out.video.i_height =
634 p_region->fmt.i_height * i_scale_height / 1000;
635 p_spu->p_scale->fmt_out.video.i_visible_height =
636 p_region->fmt.i_visible_height * i_scale_height / 1000;
637 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
638 p_region->p_cache->i_x = p_region->i_x * i_scale_width / 1000;
639 p_region->p_cache->i_y = p_region->i_y * i_scale_height / 1000;
641 p_pic = p_spu->p_scale->pf_video_filter(
642 p_spu->p_scale, &p_region->p_cache->picture );
645 picture_t p_pic_tmp = p_region->p_cache->picture;
646 p_region->p_cache->picture = *p_pic;
651 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
652 p_spu->p_scale && p_region->p_cache )
654 p_region = p_region->p_cache;
657 if( p_subpic->i_flags & SUBPICTURE_ALIGN_BOTTOM )
659 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
662 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_TOP) )
664 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
667 if( p_subpic->i_flags & SUBPICTURE_ALIGN_RIGHT )
669 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
672 else if ( !(p_subpic->i_flags & SUBPICTURE_ALIGN_LEFT) )
674 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
677 if( p_subpic->b_absolute )
679 i_x_offset = p_region->i_x +
680 p_subpic->i_x * i_scale_width / 1000;
681 i_y_offset = p_region->i_y +
682 p_subpic->i_y * i_scale_height / 1000;
684 if( p_spu->i_margin >= 0 )
686 if( p_subpic->i_height + (unsigned int)p_spu->i_margin <=
689 i_y_offset = p_fmt->i_height -
690 p_spu->i_margin - p_subpic->i_height;
695 p_spu->p_blend->fmt_in.video = p_region->fmt;
697 /* Force cropping if requested */
698 if( p_spu->b_force_crop )
700 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
701 int i_crop_x = p_spu->i_crop_x * i_scale_width / 1000;
702 int i_crop_y = p_spu->i_crop_y * i_scale_height / 1000;
703 int i_crop_width = p_spu->i_crop_width * i_scale_width / 1000;
704 int i_crop_height = p_spu->i_crop_height * i_scale_height/1000;
706 /* Find the intersection */
707 if( i_crop_x + i_crop_width <= i_x_offset ||
708 i_x_offset + (int)p_fmt->i_visible_width < i_crop_x ||
709 i_crop_y + i_crop_height <= i_y_offset ||
710 i_y_offset + (int)p_fmt->i_visible_height < i_crop_y )
712 /* No intersection */
713 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
717 int i_x, i_y, i_x_end, i_y_end;
718 i_x = __MAX( i_crop_x, i_x_offset );
719 i_y = __MAX( i_crop_y, i_y_offset );
720 i_x_end = __MIN( i_crop_x + i_crop_width,
721 i_x_offset + (int)p_fmt->i_visible_width );
722 i_y_end = __MIN( i_crop_y + i_crop_height,
723 i_y_offset + (int)p_fmt->i_visible_height );
725 p_fmt->i_x_offset = i_x - i_x_offset;
726 p_fmt->i_y_offset = i_y - i_y_offset;
727 p_fmt->i_visible_width = i_x_end - i_x;
728 p_fmt->i_visible_height = i_y_end - i_y;
735 /* Update the output picture size */
736 p_spu->p_blend->fmt_out.video.i_width =
737 p_spu->p_blend->fmt_out.video.i_visible_width =
739 p_spu->p_blend->fmt_out.video.i_height =
740 p_spu->p_blend->fmt_out.video.i_visible_height =
743 if( p_subpic->b_fade )
745 mtime_t i_fade_start = ( p_subpic->i_stop +
746 p_subpic->i_start ) / 2;
747 mtime_t i_now = mdate();
748 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
750 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
751 ( p_subpic->i_stop - i_fade_start );
755 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
756 p_pic_src, &p_region->picture, i_x_offset, i_y_offset,
757 i_fade_alpha * p_subpic->i_alpha / 255 );
759 p_region = p_region->p_next;
762 p_subpic = p_subpic->p_next;
765 vlc_mutex_unlock( &p_spu->subpicture_lock );
768 /*****************************************************************************
769 * spu_SortSubpictures: find the subpictures to display
770 *****************************************************************************
771 * This function parses all subpictures and decides which ones need to be
772 * displayed. This operation does not need lock, since only READY_SUBPICTURE
773 * are handled. If no picture has been selected, display_date will depend on
775 * We also check for ephemer DVD subpictures (subpictures that have
776 * to be removed if a newer one is available), which makes it a lot
777 * more difficult to guess if a subpicture has to be rendered or not.
778 *****************************************************************************/
779 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
781 int i_index, i_channel;
782 subpicture_t *p_subpic = NULL;
783 subpicture_t *p_ephemer;
784 mtime_t ephemer_date;
786 /* Run subpicture filters */
787 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
789 subpicture_t *p_subpic_filter;
790 p_subpic_filter = p_spu->pp_filter[i_index]->
791 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
792 if( p_subpic_filter )
794 spu_DisplaySubpicture( p_spu, p_subpic_filter );
798 /* We get an easily parsable chained list of subpictures which
799 * ends with NULL since p_subpic was initialized to NULL. */
800 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
805 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
807 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
808 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
813 display_date < p_spu->p_subpicture[i_index].i_start )
815 /* Too early, come back next monday */
819 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
820 ephemer_date = p_spu->p_subpicture[i_index].i_start;
822 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
823 ( !p_spu->p_subpicture[i_index].b_ephemer ||
824 p_spu->p_subpicture[i_index].i_stop >
825 p_spu->p_subpicture[i_index].i_start ) )
827 /* Too late, destroy the subpic */
828 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
832 /* If this is an ephemer subpic, add it to our list */
833 if( p_spu->p_subpicture[i_index].b_ephemer )
835 p_spu->p_subpicture[i_index].p_next = p_ephemer;
836 p_ephemer = &p_spu->p_subpicture[i_index];
841 p_spu->p_subpicture[i_index].p_next = p_subpic;
842 p_subpic = &p_spu->p_subpicture[i_index];
845 /* If we found ephemer subpictures, check if they have to be
846 * displayed or destroyed */
847 while( p_ephemer != NULL )
849 subpicture_t *p_tmp = p_ephemer;
850 p_ephemer = p_ephemer->p_next;
852 if( p_tmp->i_start < ephemer_date )
854 /* Ephemer subpicture has lived too long */
855 spu_DestroySubpicture( p_spu, p_tmp );
859 /* Ephemer subpicture can still live a bit */
860 p_tmp->p_next = p_subpic;
869 /*****************************************************************************
870 * SpuClearChannel: clear an spu channel
871 *****************************************************************************
872 * This function destroys the subpictures which belong to the spu channel
873 * corresponding to i_channel_id.
874 *****************************************************************************/
875 static void SpuClearChannel( spu_t *p_spu, int i_channel )
877 int i_subpic; /* subpicture index */
878 subpicture_t *p_subpic = NULL; /* first free subpicture */
880 vlc_mutex_lock( &p_spu->subpicture_lock );
882 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
884 p_subpic = &p_spu->p_subpicture[i_subpic];
885 if( p_subpic->i_status == FREE_SUBPICTURE
886 || ( p_subpic->i_status != RESERVED_SUBPICTURE
887 && p_subpic->i_status != READY_SUBPICTURE ) )
892 if( p_subpic->i_channel == i_channel )
894 while( p_subpic->p_region )
896 subpicture_region_t *p_region = p_subpic->p_region;
897 p_subpic->p_region = p_region->p_next;
898 spu_DestroyRegion( p_spu, p_region );
901 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
902 p_subpic->i_status = FREE_SUBPICTURE;
906 vlc_mutex_unlock( &p_spu->subpicture_lock );
909 /*****************************************************************************
910 * spu_ControlDefault: default methods for the subpicture unit control.
911 *****************************************************************************/
912 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
918 case SPU_CHANNEL_REGISTER:
919 pi = (int *)va_arg( args, int * );
920 if( pi ) *pi = p_spu->i_channel++;
921 msg_Dbg( p_spu, "Registering subpicture channel, ID: %i",
922 p_spu->i_channel - 1 );
925 case SPU_CHANNEL_CLEAR:
926 i = (int)va_arg( args, int );
927 SpuClearChannel( p_spu, i );
931 msg_Dbg( p_spu, "control query not supported" );
938 /*****************************************************************************
939 * Object variables callbacks
940 *****************************************************************************/
942 /*****************************************************************************
943 * UpdateSPU: update subpicture settings
944 *****************************************************************************
945 * This function is called from CropCallback and at initialization time, to
946 * retrieve crop information from the input.
947 *****************************************************************************/
948 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
952 p_spu->b_force_alpha = VLC_FALSE;
953 p_spu->b_force_crop = VLC_FALSE;
955 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
957 p_spu->b_force_crop = VLC_TRUE;
958 var_Get( p_object, "x-start", &val );
959 p_spu->i_crop_x = val.i_int;
960 var_Get( p_object, "y-start", &val );
961 p_spu->i_crop_y = val.i_int;
962 var_Get( p_object, "x-end", &val );
963 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
964 var_Get( p_object, "y-end", &val );
965 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
968 if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
971 for( i = 0; i < 4; i++ )
973 p_spu->pi_color[i] = ((uint8_t *)val.p_address)[i];
978 if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
981 for( i = 0; i < 4; i++ )
983 p_spu->pi_alpha[i] = ((uint8_t *)val.p_address)[i];
984 p_spu->pi_alpha[i] = p_spu->pi_alpha[i] == 0xf ?
985 0xff : p_spu->pi_alpha[i] << 4;
987 p_spu->b_force_alpha = VLC_TRUE;
990 msg_Dbg( p_object, "crop: %i,%i,%i,%i, alpha: %i",
991 p_spu->i_crop_x, p_spu->i_crop_y,
992 p_spu->i_crop_width, p_spu->i_crop_height, p_spu->b_force_alpha );
995 /*****************************************************************************
996 * CropCallback: called when the highlight properties are changed
997 *****************************************************************************
998 * This callback is called from the input thread when we need cropping
999 *****************************************************************************/
1000 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1001 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1003 UpdateSPU( (spu_t *)p_data, p_object );
1007 /*****************************************************************************
1008 * Buffers allocation callbacks for the filters
1009 *****************************************************************************/
1010 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1012 filter_owner_sys_t *p_sys = p_filter->p_owner;
1013 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
1014 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
1015 return p_subpicture;
1018 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1020 filter_owner_sys_t *p_sys = p_filter->p_owner;
1021 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
1024 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1026 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1027 memset( p_subpic, 0, sizeof(subpicture_t) );
1028 p_subpic->b_absolute = VLC_TRUE;
1030 p_subpic->pf_create_region = __spu_CreateRegion;
1031 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1036 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1038 while( p_subpic->p_region )
1040 subpicture_region_t *p_region = p_subpic->p_region;
1041 p_subpic->p_region = p_region->p_next;
1042 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1048 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1050 picture_t *p_picture = malloc( sizeof(picture_t) );
1052 if( vout_AllocatePicture( p_filter, p_picture,
1053 p_filter->fmt_out.video.i_chroma,
1054 p_filter->fmt_out.video.i_width,
1055 p_filter->fmt_out.video.i_height,
1056 p_filter->fmt_out.video.i_aspect )
1063 p_picture->pf_release = RegionPictureRelease;
1068 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1070 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1071 if( p_pic ) free( p_pic );