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 subpicture_t *sub_new_buffer( filter_t * );
50 static void sub_del_buffer( filter_t *, subpicture_t * );
51 static subpicture_t *spu_new_buffer( filter_t * );
52 static void spu_del_buffer( filter_t *, subpicture_t * );
53 static picture_t *spu_new_video_buffer( filter_t * );
54 static void spu_del_video_buffer( filter_t *, picture_t * );
57 * Creates the subpicture unit
59 * \param p_this the parent object which creates the subpicture unit
61 spu_t *__spu_Create( vlc_object_t *p_this )
64 spu_t *p_spu = vlc_object_create( p_this, VLC_OBJECT_SPU );
66 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
68 p_spu->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
69 p_spu->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
72 p_spu->p_blend = NULL;
74 p_spu->p_scale = NULL;
77 /* Register the default subpicture channel */
80 vlc_mutex_init( p_this, &p_spu->subpicture_lock );
82 vlc_object_attach( p_spu, p_this );
88 * Initialise the subpicture unit
90 * \param p_spu the subpicture unit object
92 int spu_Init( spu_t *p_spu )
94 char *psz_filter, *psz_filter_orig;
97 /* If the user requested an SPU margin, we force the position. */
98 var_Create( p_spu, "spumargin", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
99 var_Get( p_spu, "spumargin", &val );
100 p_spu->i_margin = val.i_int;
102 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
103 var_Get( p_spu, "sub-filter", &val );
104 psz_filter = psz_filter_orig = val.psz_string;
105 if( psz_filter && *psz_filter )
107 p_spu->pp_filter[p_spu->i_filter] =
108 vlc_object_create( p_spu, VLC_OBJECT_FILTER );
109 vlc_object_attach( p_spu->pp_filter[p_spu->i_filter], p_spu );
110 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_new = sub_new_buffer;
111 p_spu->pp_filter[p_spu->i_filter]->pf_sub_buffer_del = sub_del_buffer;
112 p_spu->pp_filter[p_spu->i_filter]->p_owner =
113 (filter_owner_sys_t *)p_spu;
114 p_spu->pp_filter[p_spu->i_filter]->p_module =
115 module_Need( p_spu->pp_filter[p_spu->i_filter],
116 "sub filter", psz_filter, 0 );
117 if( p_spu->pp_filter[p_spu->i_filter]->p_module ) p_spu->i_filter++;
120 msg_Dbg( p_spu, "no sub filter found" );
121 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
122 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
125 if( psz_filter_orig ) free( psz_filter_orig );
131 * Destroy the subpicture unit
133 * \param p_this the parent object which destroys the subpicture unit
135 void spu_Destroy( spu_t *p_spu )
139 vlc_object_detach( p_spu );
141 /* Destroy all remaining subpictures */
142 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
144 if( p_spu->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
146 spu_DestroySubpicture( p_spu, &p_spu->p_subpicture[i_index] );
152 if( p_spu->p_blend->p_module )
153 module_Unneed( p_spu->p_blend, p_spu->p_blend->p_module );
155 vlc_object_detach( p_spu->p_blend );
156 vlc_object_destroy( p_spu->p_blend );
161 if( p_spu->p_text->p_module )
162 module_Unneed( p_spu->p_text, p_spu->p_text->p_module );
164 vlc_object_detach( p_spu->p_text );
165 vlc_object_destroy( p_spu->p_text );
170 if( p_spu->p_scale->p_module )
171 module_Unneed( p_spu->p_scale, p_spu->p_scale->p_module );
173 vlc_object_detach( p_spu->p_scale );
174 vlc_object_destroy( p_spu->p_scale );
177 while( p_spu->i_filter-- )
179 module_Unneed( p_spu->pp_filter[p_spu->i_filter],
180 p_spu->pp_filter[p_spu->i_filter]->p_module );
181 vlc_object_detach( p_spu->pp_filter[p_spu->i_filter] );
182 vlc_object_destroy( p_spu->pp_filter[p_spu->i_filter] );
185 vlc_mutex_destroy( &p_spu->subpicture_lock );
186 vlc_object_destroy( p_spu );
190 * Attach/Detach the SPU from any input
192 * \param p_this the object in which to destroy the subpicture unit
193 * \param b_attach to select attach or detach
195 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, vlc_bool_t b_attach )
197 vlc_object_t *p_input;
199 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
200 if( !p_input ) return;
204 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
205 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
206 vlc_object_release( p_input );
210 /* Delete callback */
211 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
212 vlc_object_release( p_input );
217 * Create a subpicture region
219 * \param p_this vlc_object_t
220 * \param p_fmt the format that this subpicture region should have
222 static void RegionPictureRelease( picture_t *p_pic )
224 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
226 subpicture_region_t *__spu_CreateRegion( vlc_object_t *p_this,
227 video_format_t *p_fmt )
229 subpicture_region_t *p_region = malloc( sizeof(subpicture_region_t) );
230 memset( p_region, 0, sizeof(subpicture_region_t) );
231 p_region->p_next = 0;
232 p_region->p_cache = 0;
233 p_region->fmt = *p_fmt;
234 p_region->psz_text = 0;
236 if( p_fmt->i_chroma == VLC_FOURCC('Y','U','V','P') )
237 p_fmt->p_palette = p_region->fmt.p_palette =
238 malloc( sizeof(video_palette_t) );
239 else p_fmt->p_palette = p_region->fmt.p_palette = NULL;
241 p_region->picture.p_data_orig = 0;
243 if( p_fmt->i_chroma == VLC_FOURCC('T','E','X','T') ) return p_region;
245 vout_AllocatePicture( p_this, &p_region->picture, p_fmt->i_chroma,
246 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
248 if( !p_region->picture.i_planes )
251 free( p_fmt->p_palette );
255 p_region->picture.pf_release = RegionPictureRelease;
261 * Destroy a subpicture region
263 * \param p_this vlc_object_t
264 * \param p_region the subpicture region to destroy
266 void __spu_DestroyRegion( vlc_object_t *p_this, subpicture_region_t *p_region )
268 if( !p_region ) return;
269 if( p_region->picture.pf_release )
270 p_region->picture.pf_release( &p_region->picture );
271 if( p_region->fmt.p_palette ) free( p_region->fmt.p_palette );
272 if( p_region->psz_text ) free( p_region->psz_text );
273 if( p_region->p_cache ) __spu_DestroyRegion( p_this, p_region->p_cache );
278 * Display a subpicture
280 * Remove the reservation flag of a subpicture, which will cause it to be
282 * \param p_spu the subpicture unit object
283 * \param p_subpic the subpicture to display
285 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
287 /* Check if status is valid */
288 if( p_subpic->i_status != RESERVED_SUBPICTURE )
290 msg_Err( p_spu, "subpicture %p has invalid status #%d",
291 p_subpic, p_subpic->i_status );
294 /* Remove reservation flag */
295 p_subpic->i_status = READY_SUBPICTURE;
297 if( p_subpic->i_channel == DEFAULT_CHAN )
299 p_subpic->i_channel = 0xFFFF;
300 spu_Control( p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
301 p_subpic->i_channel = DEFAULT_CHAN;
306 * Allocate a subpicture in the spu heap.
308 * This function create a reserved subpicture in the spu heap.
309 * A null pointer is returned if the function fails. This method provides an
310 * already allocated zone of memory in the spu data fields. It needs locking
311 * since several pictures can be created by several producers threads.
312 * \param p_spu the subpicture unit in which to create the subpicture
313 * \return NULL on error, a reserved subpicture otherwise
315 subpicture_t *spu_CreateSubpicture( spu_t *p_spu )
317 int i_subpic; /* subpicture index */
318 subpicture_t * p_subpic = NULL; /* first free subpicture */
321 vlc_mutex_lock( &p_spu->subpicture_lock );
324 * Look for an empty place
327 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
329 if( p_spu->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE )
331 /* Subpicture is empty and ready for allocation */
332 p_subpic = &p_spu->p_subpicture[i_subpic];
333 p_spu->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
338 /* If no free subpicture could be found */
339 if( p_subpic == NULL )
341 msg_Err( p_spu, "subpicture heap is full" );
342 vlc_mutex_unlock( &p_spu->subpicture_lock );
346 /* Copy subpicture information, set some default values */
347 memset( p_subpic, 0, sizeof(subpicture_t) );
348 p_subpic->i_type = MEMORY_SUBPICTURE;
349 p_subpic->i_status = RESERVED_SUBPICTURE;
350 p_subpic->b_absolute = VLC_TRUE;
351 p_subpic->pf_render = 0;
352 p_subpic->pf_destroy = 0;
355 vlc_mutex_unlock( &p_spu->subpicture_lock );
357 p_subpic->pf_create_region = __spu_CreateRegion;
358 p_subpic->pf_destroy_region = __spu_DestroyRegion;
364 * Remove a subpicture from the heap
366 * This function frees a previously reserved subpicture.
367 * It is meant to be used when the construction of a picture aborted.
368 * This function does not need locking since reserved subpictures are ignored
371 void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
374 vlc_mutex_lock( &p_spu->subpicture_lock );
376 /* There can be race conditions so we need to check the status */
377 if( p_subpic->i_status == FREE_SUBPICTURE )
379 vlc_mutex_unlock( &p_spu->subpicture_lock );
383 /* Check if status is valid */
384 if( ( p_subpic->i_status != RESERVED_SUBPICTURE )
385 && ( p_subpic->i_status != READY_SUBPICTURE ) )
387 msg_Err( p_spu, "subpicture %p has invalid status %d",
388 p_subpic, p_subpic->i_status );
391 while( p_subpic->p_region )
393 subpicture_region_t *p_region = p_subpic->p_region;
394 p_subpic->p_region = p_region->p_next;
395 spu_DestroyRegion( p_spu, p_region );
398 if( p_subpic->pf_destroy )
400 p_subpic->pf_destroy( p_subpic );
403 p_subpic->i_status = FREE_SUBPICTURE;
405 vlc_mutex_unlock( &p_spu->subpicture_lock );
408 /*****************************************************************************
409 * spu_RenderSubpictures: render a subpicture list
410 *****************************************************************************
411 * This function renders all sub picture units in the list.
412 *****************************************************************************/
413 void spu_RenderSubpictures( spu_t *p_spu, video_format_t *p_fmt,
414 picture_t *p_pic_dst, picture_t *p_pic_src,
415 subpicture_t *p_subpic,
416 int i_scale_width, int i_scale_height )
419 vlc_mutex_lock( &p_spu->subpicture_lock );
421 /* Check i_status again to make sure spudec hasn't destroyed the subpic */
422 while( p_subpic != NULL && p_subpic->i_status != FREE_SUBPICTURE )
424 subpicture_region_t *p_region = p_subpic->p_region;
426 /* Load the blending module */
427 if( !p_spu->p_blend && p_region )
429 p_spu->p_blend = vlc_object_create( p_spu, sizeof(filter_t) );
430 vlc_object_attach( p_spu->p_blend, p_spu );
431 p_spu->p_blend->fmt_out.video.i_x_offset =
432 p_spu->p_blend->fmt_out.video.i_y_offset = 0;
433 p_spu->p_blend->fmt_out.video.i_aspect = p_fmt->i_aspect;
434 p_spu->p_blend->fmt_out.video.i_chroma = p_fmt->i_chroma;
436 p_spu->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','P');
438 p_spu->p_blend->p_module =
439 module_Need( p_spu->p_blend, "video blending", 0, 0 );
442 /* Load the text rendering module */
443 if( !p_spu->p_text && p_region )
445 p_spu->p_text = vlc_object_create( p_spu, sizeof(filter_t) );
446 vlc_object_attach( p_spu->p_text, p_spu );
448 p_spu->p_text->fmt_out.video.i_width =
449 p_spu->p_text->fmt_out.video.i_visible_width =
451 p_spu->p_text->fmt_out.video.i_height =
452 p_spu->p_text->fmt_out.video.i_visible_height =
455 p_spu->p_text->pf_sub_buffer_new = spu_new_buffer;
456 p_spu->p_text->pf_sub_buffer_del = spu_del_buffer;
458 p_spu->p_text->p_module =
459 module_Need( p_spu->p_text, "text renderer", 0, 0 );
462 /* Load the scaling module */
463 if( !p_spu->p_scale && (i_scale_width != 1000 ||
464 i_scale_height != 1000) )
466 p_spu->p_scale = vlc_object_create( p_spu, VLC_OBJECT_FILTER );
467 vlc_object_attach( p_spu->p_scale, p_spu );
468 p_spu->p_scale->fmt_out.video.i_chroma =
469 p_spu->p_scale->fmt_in.video.i_chroma =
470 VLC_FOURCC('Y','U','V','P');
471 p_spu->p_scale->fmt_in.video.i_width =
472 p_spu->p_scale->fmt_in.video.i_height = 32;
473 p_spu->p_scale->fmt_out.video.i_width =
474 p_spu->p_scale->fmt_out.video.i_height = 16;
476 p_spu->p_scale->pf_vout_buffer_new = spu_new_video_buffer;
477 p_spu->p_scale->pf_vout_buffer_del = spu_del_video_buffer;
478 p_spu->p_scale->p_module =
479 module_Need( p_spu->p_scale, "video filter2", 0, 0 );
482 if( p_subpic->pf_render )
484 /* HACK to remove when the ogt subpic decoder is gone */
485 if( p_spu->p_parent &&
486 p_spu->p_parent->i_object_type == VLC_OBJECT_VOUT )
488 vout_thread_t *p_vout = (vout_thread_t *)p_spu->p_parent;
489 p_subpic->pf_render( p_vout, p_pic_dst, p_subpic );
492 else while( p_region && p_spu->p_blend &&
493 p_spu->p_blend->pf_video_blend )
495 int i_x_offset = p_region->i_x + p_subpic->i_x;
496 int i_y_offset = p_region->i_y + p_subpic->i_y;
498 if( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') )
500 if( p_spu->p_text && p_spu->p_text->p_module &&
501 p_spu->p_text->pf_render_string )
503 /* TODO: do it in a less hacky way
504 * (modify text renderer API) */
505 subpicture_t *p_subpic_tmp;
506 subpicture_region_t tmp_region;
507 block_t *p_new_block =
508 block_New( p_spu, strlen(p_region->psz_text) + 1 );
512 memcpy( p_new_block->p_buffer, p_region->psz_text,
513 p_new_block->i_buffer );
514 p_new_block->i_pts = p_new_block->i_dts =
516 p_new_block->i_length =
517 p_subpic->i_start - p_subpic->i_stop;
518 p_subpic_tmp = p_spu->p_text->pf_render_string(
519 p_spu->p_text, p_new_block );
523 tmp_region = *p_region;
524 *p_region = *p_subpic_tmp->p_region;
525 p_region->p_next = tmp_region.p_next;
526 *p_subpic_tmp->p_region = tmp_region;
527 p_spu->p_text->pf_sub_buffer_del( p_spu->p_text,
534 if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
536 i_y_offset = p_fmt->i_height - p_region->fmt.i_height -
539 else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
541 i_y_offset = p_fmt->i_height / 2 - p_region->fmt.i_height / 2;
544 if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
546 i_x_offset = p_fmt->i_width - p_region->fmt.i_width -
549 else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
551 i_x_offset = p_fmt->i_width / 2 - p_region->fmt.i_width / 2;
554 if( p_subpic->b_absolute )
556 i_x_offset = p_region->i_x + p_subpic->i_x;
557 i_y_offset = p_region->i_y + p_subpic->i_y;
559 if( p_spu->i_margin >= 0 )
561 if( p_subpic->i_height + (unsigned int)p_spu->i_margin <=
564 i_y_offset = p_fmt->i_height -
565 p_spu->i_margin - p_subpic->i_height;
570 /* Force cropping if requested */
571 if( p_spu->b_force_crop )
573 video_format_t *p_fmt = &p_spu->p_blend->fmt_in.video;
575 /* Find the intersection */
576 if( p_spu->i_crop_x + p_spu->i_crop_width <= i_x_offset ||
577 i_x_offset + (int)p_fmt->i_visible_width <
579 p_spu->i_crop_y + p_spu->i_crop_height <= i_y_offset ||
580 i_y_offset + (int)p_fmt->i_visible_height <
583 /* No intersection */
584 p_fmt->i_visible_width = p_fmt->i_visible_height = 0;
588 int i_x, i_y, i_x_end, i_y_end;
589 i_x = __MAX( p_spu->i_crop_x, i_x_offset );
590 i_y = __MAX( p_spu->i_crop_y, i_y_offset );
591 i_x_end = __MIN( p_spu->i_crop_x + p_spu->i_crop_width,
592 i_x_offset + (int)p_fmt->i_visible_width );
593 i_y_end = __MIN( p_spu->i_crop_y + p_spu->i_crop_height,
594 i_y_offset + (int)p_fmt->i_visible_height );
596 p_fmt->i_x_offset = i_x - i_x_offset;
597 p_fmt->i_y_offset = i_y - i_y_offset;
598 p_fmt->i_visible_width = i_x_end - i_x;
599 p_fmt->i_visible_height = i_y_end - i_y;
606 /* Force palette if requested */
607 if( p_spu->b_force_alpha && VLC_FOURCC('Y','U','V','P') ==
608 p_spu->p_blend->fmt_in.video.i_chroma )
610 p_spu->p_blend->fmt_in.video.p_palette->palette[0][3] =
612 p_spu->p_blend->fmt_in.video.p_palette->palette[1][3] =
614 p_spu->p_blend->fmt_in.video.p_palette->palette[2][3] =
616 p_spu->p_blend->fmt_in.video.p_palette->palette[3][3] =
620 /* Scale SPU if necessary */
621 if( p_region->p_cache )
623 if( i_scale_width * p_region->fmt.i_width / 1000 !=
624 p_region->p_cache->fmt.i_width ||
625 i_scale_height * p_region->fmt.i_height / 1000 !=
626 p_region->p_cache->fmt.i_height )
628 p_subpic->pf_destroy_region( VLC_OBJECT(p_spu),
630 p_region->p_cache = 0;
633 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
634 p_spu->p_scale && !p_region->p_cache )
638 p_spu->p_scale->fmt_in.video = p_region->fmt;
639 p_spu->p_scale->fmt_out.video = p_region->fmt;
642 p_subpic->pf_create_region( VLC_OBJECT(p_spu),
643 &p_spu->p_scale->fmt_out.video );
644 if( p_spu->p_scale->fmt_out.video.p_palette )
645 *p_spu->p_scale->fmt_out.video.p_palette =
646 *p_region->fmt.p_palette;
647 p_region->p_cache->p_next = p_region->p_next;
649 vout_CopyPicture( p_spu, &p_region->p_cache->picture,
650 &p_region->picture );
652 p_spu->p_scale->fmt_out.video.i_width =
653 p_region->fmt.i_width * i_scale_width / 1000;
654 p_spu->p_scale->fmt_out.video.i_visible_width =
655 p_region->fmt.i_visible_width * i_scale_width / 1000;
656 p_spu->p_scale->fmt_out.video.i_height =
657 p_region->fmt.i_height * i_scale_height / 1000;
658 p_spu->p_scale->fmt_out.video.i_visible_height =
659 p_region->fmt.i_visible_height * i_scale_height / 1000;
660 p_region->p_cache->fmt = p_spu->p_scale->fmt_out.video;
662 p_pic = p_spu->p_scale->pf_video_filter(
663 p_spu->p_scale, &p_region->p_cache->picture );
666 picture_t p_pic_tmp = p_region->p_cache->picture;
667 p_region->p_cache->picture = *p_pic;
672 if( (i_scale_width != 1000 || i_scale_height != 1000) &&
673 p_spu->p_scale && p_region->p_cache )
675 p_region = p_region->p_cache;
678 if( p_subpic->b_absolute )
680 i_x_offset = i_x_offset * i_scale_width / 1000;
681 i_y_offset = i_y_offset * i_scale_height / 1000;
684 p_spu->p_blend->fmt_in.video = p_region->fmt;
686 /* Update the output picture size */
687 p_spu->p_blend->fmt_out.video.i_width =
688 p_spu->p_blend->fmt_out.video.i_visible_width =
690 p_spu->p_blend->fmt_out.video.i_height =
691 p_spu->p_blend->fmt_out.video.i_visible_height =
694 p_spu->p_blend->pf_video_blend( p_spu->p_blend, p_pic_dst,
695 p_pic_src, &p_region->picture, i_x_offset, i_y_offset );
697 p_region = p_region->p_next;
700 p_subpic = p_subpic->p_next;
703 vlc_mutex_unlock( &p_spu->subpicture_lock );
706 /*****************************************************************************
707 * spu_SortSubpictures: find the subpictures to display
708 *****************************************************************************
709 * This function parses all subpictures and decides which ones need to be
710 * displayed. This operation does not need lock, since only READY_SUBPICTURE
711 * are handled. If no picture has been selected, display_date will depend on
713 * We also check for ephemer DVD subpictures (subpictures that have
714 * to be removed if a newer one is available), which makes it a lot
715 * more difficult to guess if a subpicture has to be rendered or not.
716 *****************************************************************************/
717 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date )
720 subpicture_t *p_subpic = NULL;
721 subpicture_t *p_ephemer = NULL;
722 mtime_t ephemer_date = 0;
724 /* Run subpicture filters */
725 for( i_index = 0; i_index < p_spu->i_filter; i_index++ )
727 subpicture_t *p_subpic_filter;
728 p_subpic_filter = p_spu->pp_filter[i_index]->
729 pf_sub_filter( p_spu->pp_filter[i_index], display_date );
730 if( p_subpic_filter )
732 spu_DisplaySubpicture( p_spu, p_subpic_filter );
736 /* We get an easily parsable chained list of subpictures which
737 * ends with NULL since p_subpic was initialized to NULL. */
738 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
740 if( p_spu->p_subpicture[i_index].i_status == READY_SUBPICTURE )
742 /* If it is a DVD subpicture, check its date */
743 if( p_spu->p_subpicture[i_index].i_type == MEMORY_SUBPICTURE )
745 if( !p_spu->p_subpicture[i_index].b_ephemer
746 && display_date > p_spu->p_subpicture[i_index].i_stop )
748 /* Too late, destroy the subpic */
749 spu_DestroySubpicture(p_spu,&p_spu->p_subpicture[i_index]);
754 && display_date < p_spu->p_subpicture[i_index].i_start )
756 /* Too early, come back next monday */
760 /* If this is an ephemer subpic, see if it's the
761 * youngest we have */
762 if( p_spu->p_subpicture[i_index].b_ephemer )
764 if( p_ephemer == NULL )
766 p_ephemer = &p_spu->p_subpicture[i_index];
770 if( p_spu->p_subpicture[i_index].i_start
771 < p_ephemer->i_start )
773 /* Link the previous ephemer subpicture and
774 * replace it with the current one */
775 p_ephemer->p_next = p_subpic;
776 p_subpic = p_ephemer;
777 p_ephemer = &p_spu->p_subpicture[i_index];
779 /* If it's the 2nd youngest subpicture,
780 * register its date */
782 || ephemer_date > p_subpic->i_start )
784 ephemer_date = p_subpic->i_start;
791 p_spu->p_subpicture[i_index].p_next = p_subpic;
792 p_subpic = &p_spu->p_subpicture[i_index];
794 /* If it's the 2nd youngest subpicture, register its date */
795 if( !ephemer_date || ephemer_date > p_subpic->i_start )
797 ephemer_date = p_subpic->i_start;
800 /* If it's not a DVD subpicture, just register it */
803 p_spu->p_subpicture[i_index].p_next = p_subpic;
804 p_subpic = &p_spu->p_subpicture[i_index];
809 /* If we found an ephemer subpicture, check if it has to be
811 if( p_ephemer != NULL )
813 if( p_ephemer->i_start <= ephemer_date )
815 /* Ephemer subpicture has lived too long */
816 spu_DestroySubpicture( p_spu, p_ephemer );
820 /* Ephemer subpicture can still live a bit */
821 p_ephemer->p_next = p_subpic;
829 /*****************************************************************************
830 * SpuClearChannel: clear an spu channel
831 *****************************************************************************
832 * This function destroys the subpictures which belong to the spu channel
833 * corresponding to i_channel_id.
834 *****************************************************************************/
835 static void SpuClearChannel( spu_t *p_spu, int i_channel )
837 int i_subpic; /* subpicture index */
838 subpicture_t * p_subpic = NULL; /* first free subpicture */
840 vlc_mutex_lock( &p_spu->subpicture_lock );
842 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
844 p_subpic = &p_spu->p_subpicture[i_subpic];
845 if( p_subpic->i_status == FREE_SUBPICTURE
846 || ( p_subpic->i_status != RESERVED_SUBPICTURE
847 && p_subpic->i_status != READY_SUBPICTURE ) )
852 if( p_subpic->i_channel == i_channel )
854 while( p_subpic->p_region )
856 subpicture_region_t *p_region = p_subpic->p_region;
857 p_subpic->p_region = p_region->p_next;
858 spu_DestroyRegion( p_spu, p_region );
861 if( p_subpic->pf_destroy ) p_subpic->pf_destroy( p_subpic );
862 p_subpic->i_status = FREE_SUBPICTURE;
866 vlc_mutex_unlock( &p_spu->subpicture_lock );
869 /*****************************************************************************
870 * spu_ControlDefault: default methods for the subpicture unit control.
871 *****************************************************************************/
872 int spu_vaControlDefault( spu_t *p_spu, int i_query, va_list args )
878 case SPU_CHANNEL_REGISTER:
879 pi = (int *)va_arg( args, int * );
881 if( pi ) *pi = p_spu->i_channel;
882 msg_Dbg( p_spu, "Registering subpicture channel, ID: %i",
886 case SPU_CHANNEL_CLEAR:
887 i = (int)va_arg( args, int );
888 SpuClearChannel( p_spu, i );
892 msg_Dbg( p_spu, "control query not supported" );
899 /*****************************************************************************
900 * Object variables callbacks
901 *****************************************************************************/
903 /*****************************************************************************
904 * UpdateSPU: update subpicture settings
905 *****************************************************************************
906 * This function is called from CropCallback and at initialization time, to
907 * retrieve crop information from the input.
908 *****************************************************************************/
909 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
913 p_spu->b_force_alpha = VLC_FALSE;
914 p_spu->b_force_crop = VLC_FALSE;
916 if( var_Get( p_object, "highlight", &val ) || !val.b_bool ) return;
918 p_spu->b_force_crop = VLC_TRUE;
919 var_Get( p_object, "x-start", &val );
920 p_spu->i_crop_x = val.i_int;
921 var_Get( p_object, "y-start", &val );
922 p_spu->i_crop_y = val.i_int;
923 var_Get( p_object, "x-end", &val );
924 p_spu->i_crop_width = val.i_int - p_spu->i_crop_x;
925 var_Get( p_object, "y-end", &val );
926 p_spu->i_crop_height = val.i_int - p_spu->i_crop_y;
929 if( var_Get( p_object, "color", &val ) == VLC_SUCCESS )
932 for( i = 0; i < 4; i++ )
934 p_spu->pi_color[i] = ((uint8_t *)val.p_address)[i];
939 if( var_Get( p_object, "contrast", &val ) == VLC_SUCCESS )
942 for( i = 0; i < 4; i++ )
944 p_spu->pi_alpha[i] = ((uint8_t *)val.p_address)[i];
945 p_spu->pi_alpha[i] = p_spu->pi_alpha[i] == 0xf ?
946 0xff : p_spu->pi_alpha[i] << 4;
948 p_spu->b_force_alpha = VLC_TRUE;
951 msg_Dbg( p_object, "crop: %i,%i,%i,%i, alpha: %i",
952 p_spu->i_crop_x, p_spu->i_crop_y,
953 p_spu->i_crop_width, p_spu->i_crop_height, p_spu->b_force_alpha );
956 /*****************************************************************************
957 * CropCallback: called when the highlight properties are changed
958 *****************************************************************************
959 * This callback is called from the input thread when we need cropping
960 *****************************************************************************/
961 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
962 vlc_value_t oldval, vlc_value_t newval, void *p_data )
964 UpdateSPU( (spu_t *)p_data, p_object );
968 /*****************************************************************************
969 * Buffers allocation callbacks for the filters
970 *****************************************************************************/
971 static subpicture_t *sub_new_buffer( filter_t *p_filter )
973 spu_t *p_spu = (spu_t *)p_filter->p_owner;
974 return spu_CreateSubpicture( p_spu );
977 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
979 spu_t *p_spu = (spu_t *)p_filter->p_owner;
980 spu_DestroySubpicture( p_spu, p_subpic );
983 static subpicture_t *spu_new_buffer( filter_t *p_filter )
985 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
986 memset( p_subpic, 0, sizeof(subpicture_t) );
987 p_subpic->b_absolute = VLC_TRUE;
989 p_subpic->pf_create_region = __spu_CreateRegion;
990 p_subpic->pf_destroy_region = __spu_DestroyRegion;
995 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
997 while( p_subpic->p_region )
999 subpicture_region_t *p_region = p_subpic->p_region;
1000 p_subpic->p_region = p_region->p_next;
1001 p_subpic->pf_destroy_region( VLC_OBJECT(p_filter), p_region );
1007 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1009 picture_t *p_picture = malloc( sizeof(picture_t) );
1011 if( vout_AllocatePicture( p_filter, p_picture,
1012 p_filter->fmt_out.video.i_chroma,
1013 p_filter->fmt_out.video.i_width,
1014 p_filter->fmt_out.video.i_height,
1015 p_filter->fmt_out.video.i_aspect )
1022 p_picture->pf_release = RegionPictureRelease;
1027 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_pic )
1029 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1030 if( p_pic ) free( p_pic );