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 if( psz_filter && *psz_filter )
115 p_spu->pp_filter[p_spu->i_filter] =
116 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
117 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
118 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
119 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
120 p_spu->pp_filter[p_spu->i_filter]->p_module =
121 module_Need( p_spu->pp_filter[p_spu->i_filter],
122 "sub filter", psz_filter, 0 );
123 if( p_spu->pp_filter[p_spu->i_filter]->p_module )
125 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
126 p_spu->pp_filter[p_spu->i_filter]->p_owner = p_sys;
127 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
128 p_sys->p_spu = p_spu;
133 msg_Dbg( p_spu, "no sub filter found" );
134 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
135 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
138 if( psz_filter_orig ) free( psz_filter_orig );
144 * Destroy the subpicture unit
146 * \param p_this the parent object which destroys the subpicture unit
148 void spu_Destroy( spu_t *p_spu )
152 vlc_object_detach( p_spu );
154 /* Destroy all remaining subpictures */
155 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
157 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
159 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
165 if( p_spu->p_blend->p_module )
166 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
168 vlc_object_detach( p_spu->p_blend );
169 vlc_object_destroy( p_spu->p_blend );
174 if( p_spu->p_text->p_module )
175 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
177 vlc_object_detach( p_spu->p_text );
178 vlc_object_destroy( p_spu->p_text );
183 if( p_spu->p_scale->p_module )
184 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
186 vlc_object_detach( p_spu->p_scale );
187 vlc_object_destroy( p_spu->p_scale );
190 while( p_spu->i_filter-- )
192 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
193 p_spu->pp_filter[p_spu->i_filter]->p_module );
194 free( p_spu->pp_filter[p_spu->i_filter]->p_owner );
195 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
196 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
199 vlc_mutex_destroy( &p_spu->subpicture_lock );
200 vlc_object_destroy( p_spu );
204 * Attach/Detach the SPU from any input
206 * \param p_this the object in which to destroy the subpicture unit
207 * \param b_attach to select attach or detach
209 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
211 vlc_object_t *p_input;
213 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
214 if( !p_input ) return;
218 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
219 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
220 vlc_object_release( p_input );
224 /* Delete callback */
225 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
226 vlc_object_release( p_input );
231 * Create a subpicture region
233 * \param p_this vlc_object_t
234 * \param p_fmt the format that this subpicture region should have
236 static void RegionPictureRelease( picture_t *p_pic )
238 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
240 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
241 video_format_t *p_fmt )
243 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
244 memset( p_region, 0, sizeof(subpicture_region_t) );
245 p_region->p_next = 0;
246 p_region->p_cache = 0;
247 p_region->fmt = *p_fmt;
248 p_region->psz_text = 0;
250 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
251 p_fmt->p_palette = p_region->fmt.p_palette =
252 malloc( sizeof(video_palette_t) );
253 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
255 p_region->picture.p_data_orig = 0;
257 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
259 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
260 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
262 if( !p_region->picture.i_planes )
265 free( p_fmt->p_palette );
269 p_region->picture.pf_release = RegionPictureRelease;
275 * Destroy a subpicture region
277 * \param p_this vlc_object_t
278 * \param p_region the subpicture region to destroy
280 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
282 if( !p_region ) return;
283 if( p_region->picture.pf_release )
284 p_region->picture.pf_release( &p_region->picture );
285 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
286 if( p_region->psz_text ) free( p_region->psz_text );
287 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
292 * Display a subpicture
294 * Remove the reservation flag of a subpicture, which will cause it to be
296 * \param p_spu the subpicture unit object
297 * \param p_subpic the subpicture to display
299 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
301 /* Check if status is valid */
302 if( p_subpic->i_status != RESERVED_SUBPICTURE )
304 msg_Err( p_spu, "subpicture %p has invalid status #%d",
305 p_subpic, p_subpic->i_status );
308 /* Remove reservation flag */
309 p_subpic->i_status = READY_SUBPICTURE;
311 if( p_subpic->i_channel == DEFAULT_CHAN )
313 p_subpic->i_channel = 0xFFFF;
314 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
315 p_subpic->i_channel = DEFAULT_CHAN;
320 * Allocate a subpicture in the spu heap.
322 * This function create a reserved subpicture in the spu heap.
323 * A null pointer is returned if the function fails. This method provides an
324 * already allocated zone of memory in the spu data fields. It needs locking
325 * since several pictures can be created by several producers threads.
326 * \param p_spu the subpicture unit in which to create the subpicture
327 * \return NULL on error, a reserved subpicture otherwise
329 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
331 int i_subpic; /* subpicture index */
332 subpicture_t * p_subpic = NULL; /* first free subpicture */
335 vlc_mutex_lock( &p_spu->subpicture_lock );
338 * Look for an empty place
341 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
343 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
345 /* Subpicture is empty and ready for allocation */
346 p_subpic = &p_spu->p_subpicture[i_subpic];
347 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
352 /* If no free subpicture could be found */
353 if( p_subpic == NULL )
355 msg_Err( p_spu, "subpicture heap is full" );
356 vlc_mutex_unlock( &p_spu->subpicture_lock );
360 /* Copy subpicture information, set some default values */
361 memset( p_subpic, 0, sizeof(subpicture_t) );
362 p_subpic->i_status = RESERVED_SUBPICTURE;
363 p_subpic->b_absolute = VLC_TRUE;
364 p_subpic->pf_render = 0;
365 p_subpic->pf_destroy = 0;
368 vlc_mutex_unlock( &p_spu->subpicture_lock );
370 p_subpic->pf_create_region = __spu_CreateRegion;
371 p_subpic->pf_destroy_region = __spu_DestroyRegion;
377 * Remove a subpicture from the heap
379 * This function frees a previously reserved subpicture.
380 * It is meant to be used when the construction of a picture aborted.
381 * This function does not need locking since reserved subpictures are ignored
384 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
387 vlc_mutex_lock( &p_spu->subpicture_lock );
389 /* There can be race conditions so we need to check the status */
390 if( p_subpic->i_status == FREE_SUBPICTURE )
392 vlc_mutex_unlock( &p_spu->subpicture_lock );
396 /* Check if status is valid */
397 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
398 && ( p_subpic->i_status != READY_SUBPICTURE ) )
400 msg_Err( p_spu, "subpicture %p has invalid status %d",
401 p_subpic, p_subpic->i_status );
404 while( p_subpic->p_region )
406 subpicture_region_t *p_region = p_subpic->p_region;
407 p_subpic->p_region = p_region->p_next;
408 spu_DestroyRegion( p_spu, p_region );
411 if( p_subpic->pf_destroy )
413 p_subpic->pf_destroy( p_subpic );
416 p_subpic->i_status = FREE_SUBPICTURE;
418 vlc_mutex_unlock( &p_spu->subpicture_lock );
421 /*****************************************************************************
422 * spu_RenderSubpictures: render a subpicture list
423 *****************************************************************************
424 * This function renders all sub picture units in the list.
425 *****************************************************************************/
426 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
427 picture_t *p_pic_dst, picture_t *p_pic_src,
428 subpicture_t *p_subpic,
429 int i_scale_width, int i_scale_height )
432 vlc_mutex_lock( &p_spu->subpicture_lock );
434 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
435 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
437 subpicture_region_t *p_region = p_subpic->p_region;
439 /* Load the blending module */
440 if( !p_spu->p_blend && p_region )
442 p_spu->p_blend = vlc_object_create( p_spu, sizeof(filter_t) );
443 vlc_object_attach( p_spu->p_blend, p_spu );
444 p_spu->p_blend->fmt_out.video.i_x_offset =
445 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
446 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
447 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
449 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
451 p_spu->p_blend->p_module =
452 module_Need( p_spu->p_blend, "video blending", 0, 0 );
455 /* Load the text rendering module */
456 if( !p_spu->p_text && p_region )
458 p_spu->p_text = vlc_object_create( p_spu, sizeof(filter_t) );
459 vlc_object_attach( p_spu->p_text, p_spu );
461 p_spu->p_text->fmt_out.video.i_width =
462 p_spu->p_text->fmt_out.video.i_visible_width =
464 p_spu->p_text->fmt_out.video.i_height =
465 p_spu->p_text->fmt_out.video.i_visible_height =
468 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
469 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
471 p_spu->p_text->p_module =
472 module_Need( p_spu->p_text, "text renderer", 0, 0 );
475 /* Load the scaling module */
476 if( !p_spu->p_scale && (i_scale_width != 1000 ||
477 i_scale_height != 1000) )
479 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
480 vlc_object_attach( p_spu->p_scale, p_spu );
481 p_spu->p_scale->fmt_out.video.i_chroma =
482 p_spu->p_scale->fmt_in.video.i_chroma =
483 VLC_FOURCC('Y','U','V','P');
484 p_spu->p_scale->fmt_in.video.i_width =
485 p_spu->p_scale->fmt_in.video.i_height = 32;
486 p_spu->p_scale->fmt_out.video.i_width =
487 p_spu->p_scale->fmt_out.video.i_height = 16;
489 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
490 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
491 p_spu->p_scale->p_module =
492 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
495 if( p_subpic->pf_render )
497 /* HACK to remove when the ogt subpic decoder is gone */
498 if( p_spu->p_parent &&
499 p_spu->p_parent->i_object_type == VLC_OBJECT_VOUT )
501 vout_thread_t *p_vout = (vout_thread_t *)p_spu->p_parent;
502 p_subpic->pf_render( p_vout, p_pic_dst, p_subpic );
505 else while( p_region && p_spu->p_blend &&
506 p_spu->p_blend->pf_video_blend )
508 int i_x_offset = p_region->i_x + p_subpic->i_x;
509 int i_y_offset = p_region->i_y + p_subpic->i_y;
511 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
513 if( p_spu->p_text && p_spu->p_text->p_module &&
514 p_spu->p_text->pf_render_string )
516 /* TODO: do it in a less hacky way
517 * (modify text renderer API) */
518 subpicture_t *p_subpic_tmp;
519 subpicture_region_t tmp_region;
520 block_t *p_new_block =
521 block_New( p_spu, strlen(p_region->psz_text) + 1 );
525 memcpy( p_new_block->p_buffer, p_region->psz_text,
526 p_new_block->i_buffer );
527 p_new_block->i_pts = p_new_block->i_dts =
529 p_new_block->i_length =
530 p_subpic->i_start - p_subpic->i_stop;
531 p_subpic_tmp = p_spu->p_text->pf_render_string(
532 p_spu->p_text, p_new_block );
536 tmp_region = *p_region;
537 *p_region = *p_subpic_tmp->p_region;
538 p_region->p_next = tmp_region.p_next;
539 *p_subpic_tmp->p_region = tmp_region;
540 p_spu->p_text->pf_sub_buffer_del( p_spu->p_text,
547 if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
549 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
552 else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
554 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
557 if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
559 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
562 else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
564 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
567 if( p_subpic->b_absolute )
569 i_x_offset = p_region->i_x + p_subpic->i_x;
570 i_y_offset = p_region->i_y + p_subpic->i_y;
572 if( p_spu->i_margin >= 0 )
574 if( p_subpic->i_height + (unsigned int)p_spu->i_margin <=
577 i_y_offset = p_fmt->i_height -
578 p_spu->i_margin - p_subpic->i_height;
583 /* Force cropping if requested */
584 if( p_spu->b_force_crop )
586 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
588 /* Find the intersection */
589 if( p_spu->i_crop_x + p_spu->i_crop_width <= i_x_offset ||
590 i_x_offset + (int)p_fmt->i_visible_width <
592 p_spu->i_crop_y + p_spu->i_crop_height <= i_y_offset ||
593 i_y_offset + (int)p_fmt->i_visible_height <
596 /* No intersection */
597 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
601 int i_x, i_y, i_x_end, i_y_end;
602 i_x = __MAX( p_spu->i_crop_x, i_x_offset );
603 i_y = __MAX( p_spu->i_crop_y, i_y_offset );
604 i_x_end = __MIN( p_spu->i_crop_x + p_spu->i_crop_width,
605 i_x_offset + (int)p_fmt->i_visible_width );
606 i_y_end = __MIN( p_spu->i_crop_y + p_spu->i_crop_height,
607 i_y_offset + (int)p_fmt->i_visible_height );
609 p_fmt->i_x_offset = i_x - i_x_offset;
610 p_fmt->i_y_offset = i_y - i_y_offset;
611 p_fmt->i_visible_width = i_x_end - i_x;
612 p_fmt->i_visible_height = i_y_end - i_y;
619 /* Force palette if requested */
620 if( p_spu->b_force_alpha && VLC_FOURCC('Y','U','V','P') ==
621 p_spu->p_blend->fmt_in.video.i_chroma )
623 p_spu->p_blend->fmt_in.video.p_palette->palette[0][3] =
625 p_spu->p_blend->fmt_in.video.p_palette->palette[1][3] =
627 p_spu->p_blend->fmt_in.video.p_palette->palette[2][3] =
629 p_spu->p_blend->fmt_in.video.p_palette->palette[3][3] =
633 /* Scale SPU if necessary */
634 if( p_region->p_cache )
636 if( i_scale_width * p_region->fmt.i_width / 1000 !=
637 p_region->p_cache->fmt.i_width ||
638 i_scale_height * p_region->fmt.i_height / 1000 !=
639 p_region->p_cache->fmt.i_height )
641 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
643 p_region->p_cache = 0;
646 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
647 p_spu->p_scale && !p_region->p_cache )
651 p_spu->p_scale->fmt_in.video = p_region->fmt;
652 p_spu->p_scale->fmt_out.video = p_region->fmt;
655 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
656 &p_spu->p_scale->fmt_out.video );
657 if( p_spu->p_scale->fmt_out.video.p_palette )
658 *p_spu->p_scale->fmt_out.video.p_palette =
659 *p_region->fmt.p_palette;
660 p_region->p_cache->p_next = p_region->p_next;
662 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
663 &p_region->picture );
665 p_spu->p_scale->fmt_out.video.i_width =
666 p_region->fmt.i_width * i_scale_width / 1000;
667 p_spu->p_scale->fmt_out.video.i_visible_width =
668 p_region->fmt.i_visible_width * i_scale_width / 1000;
669 p_spu->p_scale->fmt_out.video.i_height =
670 p_region->fmt.i_height * i_scale_height / 1000;
671 p_spu->p_scale->fmt_out.video.i_visible_height =
672 p_region->fmt.i_visible_height * i_scale_height / 1000;
673 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
675 p_pic = p_spu->p_scale->pf_video_filter(
676 p_spu->p_scale, &p_region->p_cache->picture );
679 picture_t p_pic_tmp = p_region->p_cache->picture;
680 p_region->p_cache->picture = *p_pic;
685 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
686 p_spu->p_scale && p_region->p_cache )
688 p_region = p_region->p_cache;
691 if( p_subpic->b_absolute )
693 i_x_offset = i_x_offset * i_scale_width / 1000;
694 i_y_offset = i_y_offset * i_scale_height / 1000;
697 p_spu->p_blend->fmt_in.video = p_region->fmt;
699 /* Update the output picture size */
700 p_spu->p_blend->fmt_out.video.i_width =
701 p_spu->p_blend->fmt_out.video.i_visible_width =
703 p_spu->p_blend->fmt_out.video.i_height =
704 p_spu->p_blend->fmt_out.video.i_visible_height =
707 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
708 p_pic_src, &p_region->picture, i_x_offset, i_y_offset );
710 p_region = p_region->p_next;
713 p_subpic = p_subpic->p_next;
716 vlc_mutex_unlock( &p_spu->subpicture_lock );
719 /*****************************************************************************
720 * spu_SortSubpictures: find the subpictures to display
721 *****************************************************************************
722 * This function parses all subpictures and decides which ones need to be
723 * displayed. This operation does not need lock, since only READY_SUBPICTURE
724 * are handled. If no picture has been selected, display_date will depend on
726 * We also check for ephemer DVD subpictures (subpictures that have
727 * to be removed if a newer one is available), which makes it a lot
728 * more difficult to guess if a subpicture has to be rendered or not.
729 *****************************************************************************/
730 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
732 int i_index, i_channel;
733 subpicture_t *p_subpic = NULL;
734 subpicture_t *p_ephemer;
735 mtime_t ephemer_date;
737 /* Run subpicture filters */
738 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
740 subpicture_t *p_subpic_filter;
741 p_subpic_filter = p_spu->pp_filter[i_index]->
742 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
743 if( p_subpic_filter )
745 spu_DisplaySubpicture( p_spu, p_subpic_filter );
749 /* We get an easily parsable chained list of subpictures which
750 * ends with NULL since p_subpic was initialized to NULL. */
751 for( i_channel = 0; i_channel < p_spu->i_channel; i_channel++ )
756 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
758 if( p_spu->p_subpicture[i_index].i_channel != i_channel ||
759 p_spu->p_subpicture[i_index].i_status != READY_SUBPICTURE )
765 display_date < p_spu->p_subpicture[i_index].i_start )
767 /* Too early, come back next monday */
771 if( p_spu->p_subpicture[i_index].i_start > ephemer_date )
772 ephemer_date = p_spu->p_subpicture[i_index].i_start;
774 if( display_date > p_spu->p_subpicture[i_index].i_stop &&
775 ( !p_spu->p_subpicture[i_index].b_ephemer ||
776 p_spu->p_subpicture[i_index].i_stop >
777 p_spu->p_subpicture[i_index].i_start ) )
779 /* Too late, destroy the subpic */
780 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
784 /* If this is an ephemer subpic, add it to our list */
785 if( p_spu->p_subpicture[i_index].b_ephemer )
787 p_spu->p_subpicture[i_index].p_next = p_ephemer;
788 p_ephemer = &p_spu->p_subpicture[i_index];
793 p_spu->p_subpicture[i_index].p_next = p_subpic;
794 p_subpic = &p_spu->p_subpicture[i_index];
797 /* If we found ephemer subpictures, check if they have to be
798 * displayed or destroyed */
799 while( p_ephemer != NULL )
801 subpicture_t *p_tmp = p_ephemer;
802 p_ephemer = p_ephemer->p_next;
804 if( p_tmp->i_start < ephemer_date )
806 /* Ephemer subpicture has lived too long */
807 spu_DestroySubpicture( p_spu, p_tmp );
811 /* Ephemer subpicture can still live a bit */
812 p_tmp->p_next = p_subpic;
821 /*****************************************************************************
822 * SpuClearChannel: clear an spu channel
823 *****************************************************************************
824 * This function destroys the subpictures which belong to the spu channel
825 * corresponding to i_channel_id.
826 *****************************************************************************/
827 static void SpuClearChannel( spu_t *p_spu, int i_channel )
829 int i_subpic; /* subpicture index */
830 subpicture_t *p_subpic = NULL; /* first free subpicture */
832 vlc_mutex_lock( &p_spu->subpicture_lock );
834 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
836 p_subpic = &p_spu->p_subpicture[i_subpic];
837 if( p_subpic->i_status == FREE_SUBPICTURE
838 || ( p_subpic->i_status != RESERVED_SUBPICTURE
839 && p_subpic->i_status != READY_SUBPICTURE ) )
844 if( p_subpic->i_channel == i_channel )
846 while( p_subpic->p_region )
848 subpicture_region_t *p_region = p_subpic->p_region;
849 p_subpic->p_region = p_region->p_next;
850 spu_DestroyRegion( p_spu, p_region );
853 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
854 p_subpic->i_status = FREE_SUBPICTURE;
858 vlc_mutex_unlock( &p_spu->subpicture_lock );
861 /*****************************************************************************
862 * spu_ControlDefault: default methods for the subpicture unit control.
863 *****************************************************************************/
864 static int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
870 case SPU_CHANNEL_REGISTER:
871 pi = (int *)va_arg( args, int * );
872 if( pi ) *pi = p_spu->i_channel++;
873 msg_Dbg( p_spu, "Registering subpicture channel, ID: %i",
874 p_spu->i_channel - 1 );
877 case SPU_CHANNEL_CLEAR:
878 i = (int)va_arg( args, int );
879 SpuClearChannel( p_spu, i );
883 msg_Dbg( p_spu, "control query not supported" );
890 /*****************************************************************************
891 * Object variables callbacks
892 *****************************************************************************/
894 /*****************************************************************************
895 * UpdateSPU: update subpicture settings
896 *****************************************************************************
897 * This function is called from CropCallback and at initialization time, to
898 * retrieve crop information from the input.
899 *****************************************************************************/
900 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
904 p_spu->b_force_alpha = VLC_FALSE;
905 p_spu->b_force_crop = VLC_FALSE;
907 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
909 p_spu->b_force_crop = VLC_TRUE;
910 var_Get( p_object, "x-start", &val );
911 p_spu->i_crop_x = val.i_int;
912 var_Get( p_object, "y-start", &val );
913 p_spu->i_crop_y = val.i_int;
914 var_Get( p_object, "x-end", &val );
915 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
916 var_Get( p_object, "y-end", &val );
917 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
920 if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
923 for( i = 0; i < 4; i++ )
925 p_spu->pi_color[i] = ((uint8_t *)val.p_address)[i];
930 if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
933 for( i = 0; i < 4; i++ )
935 p_spu->pi_alpha[i] = ((uint8_t *)val.p_address)[i];
936 p_spu->pi_alpha[i] = p_spu->pi_alpha[i] == 0xf ?
937 0xff : p_spu->pi_alpha[i] << 4;
939 p_spu->b_force_alpha = VLC_TRUE;
942 msg_Dbg( p_object, "crop: %i,%i,%i,%i, alpha: %i",
943 p_spu->i_crop_x, p_spu->i_crop_y,
944 p_spu->i_crop_width, p_spu->i_crop_height, p_spu->b_force_alpha );
947 /*****************************************************************************
948 * CropCallback: called when the highlight properties are changed
949 *****************************************************************************
950 * This callback is called from the input thread when we need cropping
951 *****************************************************************************/
952 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
953 vlc_value_t oldval, vlc_value_t newval, void *p_data )
955 UpdateSPU( (spu_t *)p_data, p_object );
959 /*****************************************************************************
960 * Buffers allocation callbacks for the filters
961 *****************************************************************************/
962 static subpicture_t *sub_new_buffer( filter_t *p_filter )
964 filter_owner_sys_t *p_sys = p_filter->p_owner;
965 subpicture_t *p_subpicture = spu_CreateSubpicture( p_sys->p_spu );
966 if( p_subpicture ) p_subpicture->i_channel = p_sys->i_channel;
970 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
972 filter_owner_sys_t *p_sys = p_filter->p_owner;
973 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
976 static subpicture_t *spu_new_buffer( filter_t *p_filter )
978 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
979 memset( p_subpic, 0, sizeof(subpicture_t) );
980 p_subpic->b_absolute = VLC_TRUE;
982 p_subpic->pf_create_region = __spu_CreateRegion;
983 p_subpic->pf_destroy_region = __spu_DestroyRegion;
988 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
990 while( p_subpic->p_region )
992 subpicture_region_t *p_region = p_subpic->p_region;
993 p_subpic->p_region = p_region->p_next;
994 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1000 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1002 picture_t *p_picture = malloc( sizeof(picture_t) );
1004 if( vout_AllocatePicture( p_filter, p_picture,
1005 p_filter->fmt_out.video.i_chroma,
1006 p_filter->fmt_out.video.i_width,
1007 p_filter->fmt_out.video.i_height,
1008 p_filter->fmt_out.video.i_aspect )
1015 p_picture->pf_release = RegionPictureRelease;
1020 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1022 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1023 if( p_pic ) free( p_pic );