1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
35 #include <vlc_block.h>
36 #include <vlc_filter.h>
38 #include "../libvlc.h"
39 #include "vout_internal.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 /* Number of simultaneous subpictures */
48 #define VOUT_MAX_SUBPICTURES (VOUT_MAX_PICTURES)
50 #define VLC_FOURCC_YUVP VLC_FOURCC('Y','U','V','P')
51 #define VLC_FOURCC_YUVA VLC_FOURCC('Y','U','V','A')
52 #define VLC_FOURCC_RGBA VLC_FOURCC('R','G','B','A')
53 #define VLC_FOURCC_TEXT VLC_FOURCC('T','E','X','T')
58 subpicture_t *p_subpicture;
64 spu_heap_entry_t p_entry[VOUT_MAX_SUBPICTURES];
68 static void SpuHeapInit( spu_heap_t * );
69 static int SpuHeapPush( spu_heap_t *, subpicture_t * );
70 static void SpuHeapDeleteAt( spu_heap_t *, int i_index );
71 static int SpuHeapDeleteSubpicture( spu_heap_t *, subpicture_t * );
72 static void SpuHeapClean( spu_heap_t *p_heap );
76 vlc_mutex_t lock; /* lock to protect all followings fields */
80 int i_channel; /**< number of subpicture channels registered */
81 filter_t *p_blend; /**< alpha blending module */
82 filter_t *p_text; /**< text renderer module */
83 filter_t *p_scale_yuvp; /**< scaling module for YUVP */
84 filter_t *p_scale; /**< scaling module (all but YUVP) */
85 bool b_force_crop; /**< force cropping of subpicture */
86 int i_crop_x, i_crop_y, i_crop_width, i_crop_height; /**< cropping */
88 int i_margin; /**< force position of a subpicture */
89 bool b_force_palette; /**< force palette of subpicture */
90 uint8_t palette[4][4]; /**< forced palette */
92 /* Subpiture filters */
93 char *psz_chain_update;
94 filter_chain_t *p_chain;
97 mtime_t i_last_sort_date;
101 struct subpicture_region_private_t
104 picture_t *p_picture;
106 static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t * );
107 static void SpuRegionPrivateDelete( subpicture_region_private_t * );
115 static spu_scale_t spu_scale_create( int w, int h );
116 static spu_scale_t spu_scale_unit(void );
117 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd );
118 static int spu_scale_w( int v, const spu_scale_t s );
119 static int spu_scale_h( int v, const spu_scale_t s );
120 static int spu_invscale_w( int v, const spu_scale_t s );
121 static int spu_invscale_h( int v, const spu_scale_t s );
133 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t );
134 static spu_area_t spu_area_scaled( spu_area_t );
135 static spu_area_t spu_area_unscaled( spu_area_t, spu_scale_t );
136 static bool spu_area_overlap( spu_area_t, spu_area_t );
139 /* Subpicture rendered flag
140 * FIXME ? it could be moved to private ? */
141 #define SUBPICTURE_RENDERED (0x1000)
142 #if SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
143 # error SUBPICTURE_RENDERED too low
146 #define SCALE_UNIT (1000)
148 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic );
149 static int SubpictureCmp( const void *s0, const void *s1 );
151 static void SpuRenderRegion( spu_t *,
152 picture_t *p_pic_dst, spu_area_t *,
153 subpicture_t *, subpicture_region_t *,
154 const spu_scale_t scale_size,
155 const video_format_t *p_fmt,
156 const spu_area_t *p_subtitle_area, int i_subtitle_area );
158 static void UpdateSPU ( spu_t *, vlc_object_t * );
159 static int CropCallback( vlc_object_t *, char const *,
160 vlc_value_t, vlc_value_t, void * );
162 static int SpuControl( spu_t *, int, va_list );
164 static void SpuClearChannel( spu_t *p_spu, int i_channel );
166 /* Buffer allocation for SPU filter (blend, scale, ...) */
167 static subpicture_t *spu_new_buffer( filter_t * );
168 static void spu_del_buffer( filter_t *, subpicture_t * );
169 static picture_t *spu_new_video_buffer( filter_t * );
170 static void spu_del_video_buffer( filter_t *, picture_t * );
172 /* Buffer aloccation fir SUB filter */
173 static int SubFilterCallback( vlc_object_t *, char const *,
174 vlc_value_t, vlc_value_t, void * );
176 static int SubFilterAllocationInit( filter_t *, void * );
177 static void SubFilterAllocationClean( filter_t * );
180 static void SpuRenderCreateAndLoadText( spu_t * );
181 static void SpuRenderCreateAndLoadScale( spu_t * );
182 static void SpuRenderCreateBlend( spu_t *, vlc_fourcc_t i_chroma, int i_aspect );
183 static void FilterRelease( filter_t *p_filter );
185 /*****************************************************************************
187 *****************************************************************************/
189 * Creates the subpicture unit
191 * \param p_this the parent object which creates the subpicture unit
193 spu_t *__spu_Create( vlc_object_t *p_this )
196 spu_private_t *p_sys;
198 p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
199 VLC_OBJECT_GENERIC, "subpicture" );
204 /* Initialize spu fields */
205 p_spu->pf_control = SpuControl;
206 p_spu->p = p_sys = (spu_private_t*)&p_spu[1];
208 /* Initialize private fields */
209 vlc_mutex_init( &p_sys->lock );
211 SpuHeapInit( &p_sys->heap );
213 p_sys->p_blend = NULL;
214 p_sys->p_text = NULL;
215 p_sys->p_scale = NULL;
216 p_sys->p_scale_yuvp = NULL;
218 /* Register the default subpicture channel */
219 p_sys->i_channel = 2;
221 vlc_object_attach( p_spu, p_this );
223 p_sys->psz_chain_update = NULL;
224 p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
225 SubFilterAllocationInit,
226 SubFilterAllocationClean,
229 /* Load text and scale module */
230 SpuRenderCreateAndLoadText( p_spu );
231 SpuRenderCreateAndLoadScale( p_spu );
234 p_sys->i_last_sort_date = -1;
240 * Initialise the subpicture unit
242 * \param p_spu the subpicture unit object
244 int spu_Init( spu_t *p_spu )
246 spu_private_t *p_sys = p_spu->p;
248 /* If the user requested a sub margin, we force the position. */
249 p_sys->i_margin = var_CreateGetInteger( p_spu, "sub-margin" );
251 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
252 var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
253 var_TriggerCallback( p_spu, "sub-filter" );
259 * Destroy the subpicture unit
261 * \param p_this the parent object which destroys the subpicture unit
263 void spu_Destroy( spu_t *p_spu )
265 spu_private_t *p_sys = p_spu->p;
267 var_DelCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
270 FilterRelease( p_sys->p_blend );
273 FilterRelease( p_sys->p_text );
275 if( p_sys->p_scale_yuvp )
276 FilterRelease( p_sys->p_scale_yuvp );
279 FilterRelease( p_sys->p_scale );
281 filter_chain_Delete( p_sys->p_chain );
282 free( p_sys->psz_chain_update );
284 /* Destroy all remaining subpictures */
285 SpuHeapClean( &p_sys->heap );
287 vlc_mutex_destroy( &p_sys->lock );
289 vlc_object_release( p_spu );
293 * Attach/Detach the SPU from any input
295 * \param p_this the object in which to destroy the subpicture unit
296 * \param b_attach to select attach or detach
298 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
300 vlc_object_t *p_input;
302 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
308 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
309 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
310 vlc_object_release( p_input );
314 /* Delete callback */
315 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
316 vlc_object_release( p_input );
321 * Display a subpicture
323 * Remove the reservation flag of a subpicture, which will cause it to be
325 * \param p_spu the subpicture unit object
326 * \param p_subpic the subpicture to display
328 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
330 spu_private_t *p_sys = p_spu->p;
332 /* DEFAULT_CHAN always reset itself */
333 if( p_subpic->i_channel == DEFAULT_CHAN )
334 SpuClearChannel( p_spu, DEFAULT_CHAN );
336 /* p_private is for spu only and cannot be non NULL here */
337 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
338 assert( r->p_private == NULL );
341 vlc_mutex_lock( &p_sys->lock );
342 if( SpuHeapPush( &p_sys->heap, p_subpic ) )
344 vlc_mutex_unlock( &p_sys->lock );
345 msg_Err( p_spu, "subpicture heap full" );
346 subpicture_Delete( p_subpic );
349 vlc_mutex_unlock( &p_sys->lock );
353 * This function renders all sub picture units in the list.
355 void spu_RenderSubpictures( spu_t *p_spu,
356 picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
357 subpicture_t *p_subpic_list,
358 const video_format_t *p_fmt_src, bool b_paused )
360 spu_private_t *p_sys = p_spu->p;
362 const int i_source_video_width = p_fmt_src->i_width;
363 const int i_source_video_height = p_fmt_src->i_height;
364 const mtime_t i_current_date = mdate();
366 unsigned int i_subpicture;
367 subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
369 unsigned int i_subtitle_region_count;
370 spu_area_t p_subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
371 spu_area_t *p_subtitle_area;
374 vlc_mutex_lock( &p_sys->lock );
376 /* Preprocess subpictures */
378 i_subtitle_region_count = 0;
379 for( subpicture_t * p_subpic = p_subpic_list;
381 p_subpic = p_subpic->p_next )
384 if( !b_paused && p_subpic->pf_pre_render )
385 p_subpic->pf_pre_render( p_spu, p_subpic, p_fmt_dst );
387 if( !b_paused && p_subpic->pf_update_regions )
389 video_format_t fmt_org = *p_fmt_dst;
391 fmt_org.i_visible_width = i_source_video_width;
393 fmt_org.i_visible_height = i_source_video_height;
395 p_subpic->pf_update_regions( p_spu, p_subpic, &fmt_org, i_current_date );
399 if( p_subpic->b_subtitle )
401 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
402 i_subtitle_region_count++;
406 pp_subpicture[i_subpicture++] = p_subpic;
409 /* Be sure we have at least 1 picture to process */
410 if( i_subpicture <= 0 )
412 vlc_mutex_unlock( &p_sys->lock );
416 /* Now order subpicture array
417 * XXX The order is *really* important for overlap subtitles positionning */
418 qsort( pp_subpicture, i_subpicture, sizeof(*pp_subpicture), SubpictureCmp );
420 /* Allocate area array for subtitle overlap */
422 p_subtitle_area = p_subtitle_area_buffer;
423 if( i_subtitle_region_count > sizeof(p_subtitle_area_buffer)/sizeof(*p_subtitle_area_buffer) )
424 p_subtitle_area = calloc( i_subtitle_region_count, sizeof(*p_subtitle_area) );
426 /* Create the blending module */
427 if( !p_sys->p_blend )
428 SpuRenderCreateBlend( p_spu, p_fmt_dst->i_chroma, p_fmt_dst->i_aspect );
430 /* Process all subpictures and regions (in the right order) */
431 for( unsigned int i_index = 0; i_index < i_subpicture; i_index++ )
433 subpicture_t *p_subpic = pp_subpicture[i_index];
434 subpicture_region_t *p_region;
436 if( !p_subpic->p_region )
439 /* FIXME when possible use a better rendering size than source size
440 * (max of display size and source size for example) FIXME */
441 int i_render_width = p_subpic->i_original_picture_width;
442 int i_render_height = p_subpic->i_original_picture_height;
443 if( !i_render_width || !i_render_height )
445 if( i_render_width != 0 || i_render_height != 0 )
446 msg_Err( p_spu, "unsupported original picture size %dx%d",
447 i_render_width, i_render_height );
449 p_subpic->i_original_picture_width = i_render_width = i_source_video_width;
450 p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
455 p_sys->p_text->fmt_out.video.i_width =
456 p_sys->p_text->fmt_out.video.i_visible_width = i_render_width;
458 p_sys->p_text->fmt_out.video.i_height =
459 p_sys->p_text->fmt_out.video.i_visible_height = i_render_height;
462 /* Compute scaling from picture to source size */
463 spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
464 i_source_video_height, i_render_height );
466 /* Update scaling from source size to display size(p_fmt_dst) */
467 scale.w = scale.w * p_fmt_dst->i_width / i_source_video_width;
468 scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
470 /* Set default subpicture aspect ratio
471 * FIXME if we only handle 1 aspect ratio per picture, why is it set per
473 p_region = p_subpic->p_region;
474 if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
476 if( p_region->fmt.i_aspect != 0 )
478 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
479 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
483 p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
484 p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
488 /* Take care of the aspect ratio */
489 if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
490 p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
492 /* FIXME FIXME what about region->i_x/i_y ? */
494 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
495 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
498 /* Render all regions
499 * We always transform non absolute subtitle into absolute one on the
500 * first rendering to allow good subtitle overlap support.
502 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
506 /* Check scale validity */
507 if( scale.w <= 0 || scale.h <= 0 )
511 SpuRenderRegion( p_spu, p_pic_dst, &area,
512 p_subpic, p_region, scale, p_fmt_dst,
513 p_subtitle_area, i_subtitle_area );
515 if( p_subpic->b_subtitle )
517 area = spu_area_unscaled( area, scale );
518 if( !p_subpic->b_absolute && area.i_width > 0 && area.i_height > 0 )
520 p_region->i_x = area.i_x;
521 p_region->i_y = area.i_y;
523 if( p_subtitle_area )
524 p_subtitle_area[i_subtitle_area++] = area;
527 if( p_subpic->b_subtitle )
528 p_subpic->b_absolute = true;
532 if( p_subtitle_area != p_subtitle_area_buffer )
533 free( p_subtitle_area );
535 vlc_mutex_unlock( &p_sys->lock );
538 /*****************************************************************************
539 * spu_SortSubpictures: find the subpictures to display
540 *****************************************************************************
541 * This function parses all subpictures and decides which ones need to be
542 * displayed. If no picture has been selected, display_date will depend on
544 * We also check for ephemer DVD subpictures (subpictures that have
545 * to be removed if a newer one is available), which makes it a lot
546 * more difficult to guess if a subpicture has to be rendered or not.
547 *****************************************************************************/
548 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t display_date,
549 bool b_paused, bool b_subtitle_only )
551 spu_private_t *p_sys = p_spu->p;
553 subpicture_t *p_subpic = NULL;
555 /* Update sub-filter chain */
556 vlc_mutex_lock( &p_sys->lock );
557 char *psz_chain_update = p_sys->psz_chain_update;
558 p_sys->psz_chain_update = NULL;
559 vlc_mutex_unlock( &p_sys->lock );
561 if( psz_chain_update )
563 filter_chain_Reset( p_sys->p_chain, NULL, NULL );
565 filter_chain_AppendFromString( p_spu->p->p_chain, psz_chain_update );
567 free( psz_chain_update );
570 /* Run subpicture filters */
571 filter_chain_SubFilter( p_sys->p_chain, display_date );
573 vlc_mutex_lock( &p_sys->lock );
575 /* We get an easily parsable chained list of subpictures which
576 * ends with NULL since p_subpic was initialized to NULL. */
577 for( i_channel = 0; i_channel < p_sys->i_channel; i_channel++ )
579 subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
580 bool pb_available_late[VOUT_MAX_SUBPICTURES];
583 mtime_t start_date = display_date;
584 mtime_t ephemer_date = 0;
587 /* Select available pictures */
588 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
590 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
591 subpicture_t *p_current = p_entry->p_subpicture;
595 if( !p_current || p_entry->b_reject )
597 if( p_entry->b_reject )
598 SpuHeapDeleteAt( &p_sys->heap, i_index );
602 if( p_current->i_channel != i_channel ||
603 ( b_subtitle_only && !p_current->b_subtitle ) )
608 display_date < p_current->i_start )
610 /* Too early, come back next monday */
614 if( p_current->i_start > ephemer_date )
615 ephemer_date = p_current->i_start;
617 b_stop_valid = ( !p_current->b_ephemer || p_current->i_stop > p_current->i_start ) &&
618 ( !p_current->b_subtitle || !b_paused ); /* XXX Assume that subtitle are pausable */
620 b_late = b_stop_valid && p_current->i_stop <= display_date;
622 /* start_date will be used for correct automatic overlap support
623 * in case picture that should not be displayed anymore (display_time)
624 * overlap with a picture to be displayed (p_current->i_start) */
625 if( !b_late && !p_current->b_ephemer )
626 start_date = p_current->i_start;
629 p_available_subpic[i_available] = p_current;
630 pb_available_late[i_available] = b_late;
634 /* Only forced old picture display at the transition */
635 if( start_date < p_sys->i_last_sort_date )
636 start_date = p_sys->i_last_sort_date;
637 if( start_date <= 0 )
638 start_date = INT64_MAX;
640 /* Select pictures to be displayed */
641 for( i_index = 0; i_index < i_available; i_index++ )
643 subpicture_t *p_current = p_available_subpic[i_index];
644 bool b_late = pb_available_late[i_index];
646 if( ( b_late && p_current->i_stop <= __MAX( start_date, p_sys->i_last_sort_date ) ) ||
647 ( p_current->b_ephemer && p_current->i_start < ephemer_date ) )
649 /* Destroy late and obsolete ephemer subpictures */
650 SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
654 SubpictureChain( &p_subpic, p_current );
659 p_sys->i_last_sort_date = display_date;
660 vlc_mutex_unlock( &p_sys->lock );
665 void spu_OffsetSubtitleDate( spu_t *p_spu, mtime_t i_duration )
667 spu_private_t *p_sys = p_spu->p;
669 vlc_mutex_lock( &p_sys->lock );
670 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
672 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i];
673 subpicture_t *p_current = p_entry->p_subpicture;
675 if( p_current && p_current->b_subtitle )
677 if( p_current->i_start > 0 )
678 p_current->i_start += i_duration;
679 if( p_current->i_stop > 0 )
680 p_current->i_stop += i_duration;
683 vlc_mutex_unlock( &p_sys->lock );
686 /*****************************************************************************
687 * subpicture_t allocation
688 *****************************************************************************/
689 subpicture_t *subpicture_New( void )
691 subpicture_t *p_subpic = calloc( 1, sizeof(*p_subpic) );
695 p_subpic->i_order = 0;
696 p_subpic->b_absolute = true;
697 p_subpic->b_fade = false;
698 p_subpic->b_subtitle = false;
699 p_subpic->i_alpha = 0xFF;
700 p_subpic->p_region = NULL;
701 p_subpic->pf_render = NULL;
702 p_subpic->pf_destroy = NULL;
703 p_subpic->p_sys = NULL;
708 void subpicture_Delete( subpicture_t *p_subpic )
710 subpicture_region_ChainDelete( p_subpic->p_region );
711 p_subpic->p_region = NULL;
713 if( p_subpic->pf_destroy )
715 p_subpic->pf_destroy( p_subpic );
720 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
722 p_subpic->p_next = *pp_head;
727 /*****************************************************************************
728 * subpicture_region_t allocation
729 *****************************************************************************/
730 subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
732 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
736 p_region->fmt = *p_fmt;
737 p_region->fmt.p_palette = NULL;
738 if( p_fmt->i_chroma == VLC_FOURCC_YUVP )
740 p_region->fmt.p_palette = calloc( 1, sizeof(*p_region->fmt.p_palette) );
741 if( p_fmt->p_palette )
742 *p_region->fmt.p_palette = *p_fmt->p_palette;
744 p_region->i_alpha = 0xff;
745 p_region->p_next = NULL;
746 p_region->p_private = NULL;
747 p_region->psz_text = NULL;
748 p_region->p_style = NULL;
749 p_region->p_picture = NULL;
751 if( p_fmt->i_chroma == VLC_FOURCC_TEXT )
754 p_region->p_picture = picture_New( p_fmt->i_chroma, p_fmt->i_width, p_fmt->i_height,
756 if( !p_region->p_picture )
758 free( p_fmt->p_palette );
767 void subpicture_region_Delete( subpicture_region_t *p_region )
772 if( p_region->p_private )
773 SpuRegionPrivateDelete( p_region->p_private );
775 if( p_region->p_picture )
776 picture_Release( p_region->p_picture );
778 free( p_region->fmt.p_palette );
780 free( p_region->psz_text );
781 free( p_region->psz_html );
782 if( p_region->p_style )
783 text_style_Delete( p_region->p_style );
788 void subpicture_region_ChainDelete( subpicture_region_t *p_head )
792 subpicture_region_t *p_next = p_head->p_next;
794 subpicture_region_Delete( p_head );
802 /*****************************************************************************
804 *****************************************************************************/
805 static void SpuHeapInit( spu_heap_t *p_heap )
807 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
809 spu_heap_entry_t *e = &p_heap->p_entry[i];
811 e->p_subpicture = NULL;
816 static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
818 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
820 spu_heap_entry_t *e = &p_heap->p_entry[i];
822 if( e->p_subpicture )
825 e->p_subpicture = p_subpic;
832 static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
834 spu_heap_entry_t *e = &p_heap->p_entry[i_index];
836 if( e->p_subpicture )
837 subpicture_Delete( e->p_subpicture );
839 e->p_subpicture = NULL;
842 static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
844 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
846 spu_heap_entry_t *e = &p_heap->p_entry[i];
848 if( e->p_subpicture != p_subpic )
851 SpuHeapDeleteAt( p_heap, i );
857 static void SpuHeapClean( spu_heap_t *p_heap )
859 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
861 spu_heap_entry_t *e = &p_heap->p_entry[i];
862 if( e->p_subpicture )
863 subpicture_Delete( e->p_subpicture );
867 static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t *p_fmt )
869 subpicture_region_private_t *p_private = malloc( sizeof(*p_private) );
874 p_private->fmt = *p_fmt;
875 if( p_fmt->p_palette )
877 p_private->fmt.p_palette = malloc( sizeof(*p_private->fmt.p_palette) );
878 if( p_private->fmt.p_palette )
879 *p_private->fmt.p_palette = *p_fmt->p_palette;
881 p_private->p_picture = NULL;
885 static void SpuRegionPrivateDelete( subpicture_region_private_t *p_private )
887 if( p_private->p_picture )
888 picture_Release( p_private->p_picture );
889 free( p_private->fmt.p_palette );
893 static void FilterRelease( filter_t *p_filter )
895 if( p_filter->p_module )
896 module_unneed( p_filter, p_filter->p_module );
898 vlc_object_detach( p_filter );
899 vlc_object_release( p_filter );
902 static void SpuRenderCreateBlend( spu_t *p_spu, vlc_fourcc_t i_chroma, int i_aspect )
906 assert( !p_spu->p->p_blend );
909 p_blend = vlc_custom_create( p_spu, sizeof(filter_t),
910 VLC_OBJECT_GENERIC, "blend" );
914 es_format_Init( &p_blend->fmt_in, VIDEO_ES, 0 );
916 es_format_Init( &p_blend->fmt_out, VIDEO_ES, 0 );
917 p_blend->fmt_out.video.i_x_offset = 0;
918 p_blend->fmt_out.video.i_y_offset = 0;
919 p_blend->fmt_out.video.i_chroma = i_chroma;
920 p_blend->fmt_out.video.i_aspect = i_aspect;
922 /* The blend module will be loaded when needed with the real
924 p_blend->p_module = NULL;
927 vlc_object_attach( p_blend, p_spu );
929 static void SpuRenderUpdateBlend( spu_t *p_spu, int i_out_width, int i_out_height,
930 const video_format_t *p_in_fmt )
932 filter_t *p_blend = p_spu->p->p_blend;
937 if( p_blend->p_module && p_blend->fmt_in.video.i_chroma != p_in_fmt->i_chroma )
939 /* The chroma is not the same, we need to reload the blend module
940 * XXX to match the old behaviour just test !p_blend->fmt_in.video.i_chroma */
941 module_unneed( p_blend, p_blend->p_module );
942 p_blend->p_module = NULL;
946 p_blend->fmt_in.video = *p_in_fmt;
949 p_blend->fmt_out.video.i_width =
950 p_blend->fmt_out.video.i_visible_width = i_out_width;
951 p_blend->fmt_out.video.i_height =
952 p_blend->fmt_out.video.i_visible_height = i_out_height;
955 if( !p_blend->p_module )
956 p_blend->p_module = module_need( p_blend, "video blending", NULL, false );
958 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
962 assert( !p_spu->p->p_text );
965 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
966 VLC_OBJECT_GENERIC, "spu text" );
970 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
972 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
973 p_text->fmt_out.video.i_width =
974 p_text->fmt_out.video.i_visible_width = 32;
975 p_text->fmt_out.video.i_height =
976 p_text->fmt_out.video.i_visible_height = 32;
978 p_text->pf_sub_buffer_new = spu_new_buffer;
979 p_text->pf_sub_buffer_del = spu_del_buffer;
981 vlc_object_attach( p_text, p_spu );
983 /* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
984 * same than these 2 calls ? */
985 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
986 if( psz_modulename && *psz_modulename )
988 p_text->p_module = module_need( p_text, "text renderer",
989 psz_modulename, true );
991 free( psz_modulename );
993 if( !p_text->p_module )
994 p_text->p_module = module_need( p_text, "text renderer", NULL, false );
996 /* Create a few variables used for enhanced text rendering */
997 var_Create( p_text, "spu-duration", VLC_VAR_TIME );
998 var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
999 var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
1000 var_Create( p_text, "scale", VLC_VAR_INTEGER );
1003 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
1004 vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
1009 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
1010 VLC_OBJECT_GENERIC, "scale" );
1014 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
1015 p_scale->fmt_in.video.i_chroma = i_src_chroma;
1016 p_scale->fmt_in.video.i_width =
1017 p_scale->fmt_in.video.i_height = 32;
1019 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
1020 p_scale->fmt_out.video.i_chroma = i_dst_chroma;
1021 p_scale->fmt_out.video.i_width =
1022 p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
1024 p_scale->pf_vout_buffer_new = spu_new_video_buffer;
1025 p_scale->pf_vout_buffer_del = spu_del_video_buffer;
1027 vlc_object_attach( p_scale, p_obj );
1028 p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
1032 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
1034 assert( !p_spu->p->p_scale );
1035 assert( !p_spu->p->p_scale_yuvp );
1036 /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
1038 p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
1039 VLC_FOURCC_YUVA, VLC_FOURCC_YUVA, true );
1040 /* This one is used for YUVP to YUVA/RGBA without scaling
1041 * FIXME rename it */
1042 p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
1043 VLC_FOURCC_YUVP, VLC_FOURCC_YUVA, false );
1046 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
1047 subpicture_t *p_subpic, subpicture_region_t *p_region,
1048 int i_min_scale_ratio )
1050 filter_t *p_text = p_spu->p->p_text;
1052 assert( p_region->fmt.i_chroma == VLC_FOURCC_TEXT );
1054 if( !p_text || !p_text->p_module )
1057 /* Setup 3 variables which can be used to render
1058 * time-dependent text (and effects). The first indicates
1059 * the total amount of time the text will be on screen,
1060 * the second the amount of time it has already been on
1061 * screen (can be a negative value as text is layed out
1062 * before it is rendered) and the third is a feedback
1063 * variable from the renderer - if the renderer sets it
1064 * then this particular text is time-dependent, eg. the
1065 * visual progress bar inside the text in karaoke and the
1066 * text needs to be rendered multiple times in order for
1067 * the effect to work - we therefore need to return the
1068 * region to its original state at the end of the loop,
1069 * instead of leaving it in YUVA or YUVP.
1070 * Any renderer which is unaware of how to render
1071 * time-dependent text can happily ignore the variables
1072 * and render the text the same as usual - it should at
1073 * least show up on screen, but the effect won't change
1074 * the text over time.
1076 var_SetTime( p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
1077 var_SetTime( p_text, "spu-elapsed", mdate() - p_subpic->i_start );
1078 var_SetBool( p_text, "text-rerender", false );
1079 var_SetInteger( p_text, "scale", i_min_scale_ratio );
1081 if( p_text->pf_render_html && p_region->psz_html )
1083 p_text->pf_render_html( p_text, p_region, p_region );
1085 else if( p_text->pf_render_text )
1087 p_text->pf_render_text( p_text, p_region, p_region );
1089 *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
1092 p_region->i_align |= SUBPICTURE_RENDERED;
1096 * A few scale functions helpers.
1098 static spu_scale_t spu_scale_create( int w, int h )
1100 spu_scale_t s = { .w = w, .h = h };
1107 static spu_scale_t spu_scale_unit( void )
1109 return spu_scale_create( SCALE_UNIT, SCALE_UNIT );
1111 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
1113 return spu_scale_create( wn * SCALE_UNIT / wd,
1114 hn * SCALE_UNIT / hd );
1116 static int spu_scale_w( int v, const spu_scale_t s )
1118 return v * s.w / SCALE_UNIT;
1120 static int spu_scale_h( int v, const spu_scale_t s )
1122 return v * s.h / SCALE_UNIT;
1124 static int spu_invscale_w( int v, const spu_scale_t s )
1126 return v * SCALE_UNIT / s.w;
1128 static int spu_invscale_h( int v, const spu_scale_t s )
1130 return v * SCALE_UNIT / s.h;
1134 * A few area functions helpers
1136 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t s )
1138 spu_area_t a = { .i_x = x, .i_y = y, .i_width = w, .i_height = h, .scale = s };
1141 static spu_area_t spu_area_scaled( spu_area_t a )
1143 if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
1146 a.i_x = spu_scale_w( a.i_x, a.scale );
1147 a.i_y = spu_scale_h( a.i_y, a.scale );
1149 a.i_width = spu_scale_w( a.i_width, a.scale );
1150 a.i_height = spu_scale_h( a.i_height, a.scale );
1152 a.scale = spu_scale_unit();
1155 static spu_area_t spu_area_unscaled( spu_area_t a, spu_scale_t s )
1157 if( a.scale.w == s.w && a.scale.h == s.h )
1160 a = spu_area_scaled( a );
1162 a.i_x = spu_invscale_w( a.i_x, s );
1163 a.i_y = spu_invscale_h( a.i_y, s );
1165 a.i_width = spu_invscale_w( a.i_width, s );
1166 a.i_height = spu_invscale_h( a.i_height, s );
1171 static bool spu_area_overlap( spu_area_t a, spu_area_t b )
1176 a = spu_area_scaled( a );
1177 b = spu_area_scaled( b );
1179 return __MAX( a.i_x-i_dx, b.i_x ) < __MIN( a.i_x+a.i_width +i_dx, b.i_x+b.i_width ) &&
1180 __MAX( a.i_y-i_dy, b.i_y ) < __MIN( a.i_y+a.i_height+i_dy, b.i_y+b.i_height );
1184 * Avoid area overlapping
1186 static void SpuAreaFixOverlap( spu_area_t *p_dst,
1187 const spu_area_t *p_master,
1188 const spu_area_t *p_sub, int i_sub, int i_align )
1190 spu_area_t a = spu_area_scaled( *p_dst );
1191 bool b_moved = false;
1194 assert( p_master->i_x == 0 && p_master->i_y == 0 );
1196 /* Check for overlap
1197 * XXX It is not fast O(n^2) but we should not have a lot of region */
1201 for( int i = 0; i < i_sub; i++ )
1203 spu_area_t sub = spu_area_scaled( p_sub[i] );
1205 if( !spu_area_overlap( a, sub ) )
1208 if( i_align & SUBPICTURE_ALIGN_TOP )
1211 int i_y = sub.i_y + sub.i_height;
1212 if( i_y + a.i_height > p_master->i_height )
1217 else if( i_align & SUBPICTURE_ALIGN_BOTTOM )
1220 int i_y = sub.i_y - a.i_height;
1228 /* TODO what to do in this case? */
1229 //fprintf( stderr, "Overlap with unsupported alignment\n" );
1239 *p_dst = spu_area_unscaled( a, p_dst->scale );
1246 static void SpuRegionPlace( int *pi_x, int *pi_y,
1247 const subpicture_t *p_subpic,
1248 const subpicture_region_t *p_region,
1251 const int i_delta_x = p_region->i_x;
1252 const int i_delta_y = p_region->i_y;
1255 assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
1256 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
1260 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
1262 i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
1266 i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
1269 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
1273 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
1275 i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
1279 i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
1282 if( p_subpic->b_absolute )
1288 /* Margin shifts all subpictures */
1289 if( i_margin_y != 0 )
1292 /* Clamp offset to not go out of the screen (when possible) */
1293 const int i_error_x = (i_x + p_region->fmt.i_width) - p_subpic->i_original_picture_width;
1299 const int i_error_y = (i_y + p_region->fmt.i_height) - p_subpic->i_original_picture_height;
1310 * This function computes the current alpha value for a given region.
1312 static int SpuRegionAlpha( subpicture_t *p_subpic, subpicture_region_t *p_region )
1314 /* Compute alpha blend value */
1315 int i_fade_alpha = 255;
1316 if( p_subpic->b_fade )
1318 mtime_t i_fade_start = ( p_subpic->i_stop +
1319 p_subpic->i_start ) / 2;
1320 mtime_t i_now = mdate();
1322 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
1324 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
1325 ( p_subpic->i_stop - i_fade_start );
1328 return i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
1332 * It will render the provided region onto p_pic_dst.
1335 static void SpuRenderRegion( spu_t *p_spu,
1336 picture_t *p_pic_dst, spu_area_t *p_area,
1337 subpicture_t *p_subpic, subpicture_region_t *p_region,
1338 const spu_scale_t scale_size,
1339 const video_format_t *p_fmt,
1340 const spu_area_t *p_subtitle_area, int i_subtitle_area )
1342 spu_private_t *p_sys = p_spu->p;
1344 video_format_t fmt_original = p_region->fmt;
1345 bool b_rerender_text = false;
1346 bool b_restore_format = false;
1350 video_format_t region_fmt;
1351 picture_t *p_region_picture;
1353 /* Invalidate area by default */
1354 *p_area = spu_area_create( 0,0, 0,0, scale_size );
1356 /* Render text region */
1357 if( p_region->fmt.i_chroma == VLC_FOURCC_TEXT )
1359 const int i_min_scale_ratio = SCALE_UNIT; /* FIXME what is the right value? (scale_size is not) */
1360 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region, i_min_scale_ratio );
1361 b_restore_format = b_rerender_text;
1363 /* Check if the rendering has failed ... */
1364 if( p_region->fmt.i_chroma == VLC_FOURCC_TEXT )
1368 /* Force palette if requested
1369 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
1370 * instead of only the right one (being the dvd spu).
1372 const bool b_using_palette = p_region->fmt.i_chroma == VLC_FOURCC_YUVP;
1373 const bool b_force_palette = b_using_palette && p_sys->b_force_palette;
1374 const bool b_force_crop = b_force_palette && p_sys->b_force_crop;
1375 bool b_changed_palette = false;
1378 /* Compute the margin which is expressed in destination pixel unit
1379 * The margin is applied only to subtitle and when no forced crop is
1380 * requested (dvd menu) */
1382 if( !b_force_crop && p_subpic->b_subtitle )
1383 i_margin_y = spu_invscale_h( p_sys->i_margin, scale_size );
1385 /* Place the picture
1386 * We compute the position in the rendered size */
1387 SpuRegionPlace( &i_x_offset, &i_y_offset,
1388 p_subpic, p_region, i_margin_y );
1390 /* Save this position for subtitle overlap support
1391 * it is really important that there are given without scale_size applied */
1392 *p_area = spu_area_create( i_x_offset, i_y_offset,
1393 p_region->fmt.i_width, p_region->fmt.i_height,
1396 /* Handle overlapping subtitles when possible */
1397 if( p_subpic->b_subtitle && !p_subpic->b_absolute )
1399 spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
1402 SpuAreaFixOverlap( p_area, &display, p_subtitle_area, i_subtitle_area,
1403 p_region->i_align );
1406 /* Fix the position for the current scale_size */
1407 i_x_offset = spu_scale_w( p_area->i_x, p_area->scale );
1408 i_y_offset = spu_scale_h( p_area->i_y, p_area->scale );
1411 if( b_force_palette )
1413 video_palette_t *p_palette = p_region->fmt.p_palette;
1414 video_palette_t palette;
1416 /* We suppose DVD palette here */
1417 palette.i_entries = 4;
1418 for( int i = 0; i < 4; i++ )
1419 for( int j = 0; j < 4; j++ )
1420 palette.palette[i][j] = p_sys->palette[i][j];
1422 if( p_palette->i_entries == palette.i_entries )
1424 for( int i = 0; i < p_palette->i_entries; i++ )
1425 for( int j = 0; j < 4; j++ )
1426 b_changed_palette |= p_palette->palette[i][j] != palette.palette[i][j];
1430 b_changed_palette = true;
1432 *p_palette = palette;
1436 region_fmt = p_region->fmt;
1437 p_region_picture = p_region->p_picture;
1440 /* Scale from rendered size to destination size */
1441 if( p_sys->p_scale && p_sys->p_scale->p_module &&
1442 ( !b_using_palette || ( p_sys->p_scale_yuvp && p_sys->p_scale_yuvp->p_module ) ) &&
1443 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_using_palette ) )
1445 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
1446 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
1448 /* Destroy the cache if unusable */
1449 if( p_region->p_private )
1451 subpicture_region_private_t *p_private = p_region->p_private;
1452 bool b_changed = false;
1454 /* Check resize changes */
1455 if( i_dst_width != p_private->fmt.i_width ||
1456 i_dst_height != p_private->fmt.i_height )
1459 /* Check forced palette changes */
1460 if( b_changed_palette )
1465 SpuRegionPrivateDelete( p_private );
1466 p_region->p_private = NULL;
1470 /* Scale if needed into cache */
1471 if( !p_region->p_private && i_dst_width > 0 && i_dst_height > 0 )
1473 filter_t *p_scale = p_sys->p_scale;
1475 picture_t *p_picture = p_region->p_picture;
1476 picture_Hold( p_picture );
1478 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
1479 if( b_using_palette )
1481 filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
1483 p_scale_yuvp->fmt_in.video = p_region->fmt;
1485 /* TODO converting to RGBA for RGB video output is better */
1486 p_scale_yuvp->fmt_out.video = p_region->fmt;
1487 p_scale_yuvp->fmt_out.video.i_chroma = VLC_FOURCC_YUVA;
1489 p_picture = p_scale_yuvp->pf_video_filter( p_scale_yuvp, p_picture );
1492 /* Well we will try conversion+scaling */
1493 msg_Warn( p_spu, "%4.4s to %4.4s conversion failed",
1494 (const char*)&p_scale_yuvp->fmt_in.video.i_chroma,
1495 (const char*)&p_scale_yuvp->fmt_out.video.i_chroma );
1499 /* Conversion(except from YUVP)/Scaling */
1501 ( p_picture->format.i_width != i_dst_width ||
1502 p_picture->format.i_height != i_dst_height ) )
1504 p_scale->fmt_in.video = p_picture->format;
1505 p_scale->fmt_out.video = p_picture->format;
1507 p_scale->fmt_out.video.i_width = i_dst_width;
1508 p_scale->fmt_out.video.i_height = i_dst_height;
1510 p_scale->fmt_out.video.i_visible_width =
1511 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
1512 p_scale->fmt_out.video.i_visible_height =
1513 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
1515 p_picture = p_scale->pf_video_filter( p_scale, p_picture );
1517 msg_Err( p_spu, "scaling failed" );
1523 p_region->p_private = SpuRegionPrivateNew( &p_picture->format );
1524 if( p_region->p_private )
1526 p_region->p_private->p_picture = p_picture;
1527 if( !p_region->p_private->p_picture )
1529 SpuRegionPrivateDelete( p_region->p_private );
1530 p_region->p_private = NULL;
1535 picture_Release( p_picture );
1540 /* And use the scaled picture */
1541 if( p_region->p_private )
1543 region_fmt = p_region->p_private->fmt;
1544 p_region_picture = p_region->p_private->p_picture;
1548 /* Force cropping if requested */
1551 int i_crop_x = spu_scale_w( p_sys->i_crop_x, scale_size );
1552 int i_crop_y = spu_scale_h( p_sys->i_crop_y, scale_size );
1553 int i_crop_width = spu_scale_w( p_sys->i_crop_width, scale_size );
1554 int i_crop_height= spu_scale_h( p_sys->i_crop_height,scale_size );
1556 /* Find the intersection */
1557 if( i_crop_x + i_crop_width <= i_x_offset ||
1558 i_x_offset + (int)region_fmt.i_visible_width < i_crop_x ||
1559 i_crop_y + i_crop_height <= i_y_offset ||
1560 i_y_offset + (int)region_fmt.i_visible_height < i_crop_y )
1562 /* No intersection */
1563 region_fmt.i_visible_width =
1564 region_fmt.i_visible_height = 0;
1568 int i_x, i_y, i_x_end, i_y_end;
1569 i_x = __MAX( i_crop_x, i_x_offset );
1570 i_y = __MAX( i_crop_y, i_y_offset );
1571 i_x_end = __MIN( i_crop_x + i_crop_width,
1572 i_x_offset + (int)region_fmt.i_visible_width );
1573 i_y_end = __MIN( i_crop_y + i_crop_height,
1574 i_y_offset + (int)region_fmt.i_visible_height );
1576 region_fmt.i_x_offset = i_x - i_x_offset;
1577 region_fmt.i_y_offset = i_y - i_y_offset;
1578 region_fmt.i_visible_width = i_x_end - i_x;
1579 region_fmt.i_visible_height = i_y_end - i_y;
1581 i_x_offset = __MAX( i_x, 0 );
1582 i_y_offset = __MAX( i_y, 0 );
1586 /* Update the blender */
1587 SpuRenderUpdateBlend( p_spu, p_fmt->i_width, p_fmt->i_height, ®ion_fmt );
1589 if( p_sys->p_blend->p_module )
1591 const int i_alpha = SpuRegionAlpha( p_subpic, p_region );
1593 p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_pic_dst,
1594 p_region_picture, i_x_offset, i_y_offset, i_alpha );
1598 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1599 (char *)&p_sys->p_blend->fmt_out.video.i_chroma,
1600 (char *)&p_sys->p_blend->fmt_out.video.i_chroma );
1604 if( b_rerender_text )
1606 /* Some forms of subtitles need to be re-rendered more than
1607 * once, eg. karaoke. We therefore restore the region to its
1608 * pre-rendered state, so the next time through everything is
1611 if( p_region->p_picture )
1613 picture_Release( p_region->p_picture );
1614 p_region->p_picture = NULL;
1616 if( p_region->p_private )
1618 SpuRegionPrivateDelete( p_region->p_private );
1619 p_region->p_private = NULL;
1621 p_region->i_align &= ~SUBPICTURE_RENDERED;
1623 if( b_restore_format )
1624 p_region->fmt = fmt_original;
1628 * This function compares two 64 bits integers.
1629 * It can be used by qsort.
1631 static int IntegerCmp( int64_t i0, int64_t i1 )
1633 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
1636 * This function compares 2 subpictures using the following properties
1637 * (ordered by priority)
1638 * 1. absolute positionning
1640 * 3. creation order (per channel)
1642 * It can be used by qsort.
1644 * XXX spu_RenderSubpictures depends heavily on this order.
1646 static int SubpictureCmp( const void *s0, const void *s1 )
1648 subpicture_t *p_subpic0 = *(subpicture_t**)s0;
1649 subpicture_t *p_subpic1 = *(subpicture_t**)s1;
1652 r = IntegerCmp( !p_subpic0->b_absolute, !p_subpic1->b_absolute );
1654 r = IntegerCmp( p_subpic0->i_start, p_subpic1->i_start );
1656 r = IntegerCmp( p_subpic0->i_channel, p_subpic1->i_channel );
1658 r = IntegerCmp( p_subpic0->i_order, p_subpic1->i_order );
1662 /*****************************************************************************
1663 * SpuClearChannel: clear an spu channel
1664 *****************************************************************************
1665 * This function destroys the subpictures which belong to the spu channel
1666 * corresponding to i_channel_id.
1667 *****************************************************************************/
1668 static void SpuClearChannel( spu_t *p_spu, int i_channel )
1670 spu_private_t *p_sys = p_spu->p;
1671 int i_subpic; /* subpicture index */
1673 vlc_mutex_lock( &p_sys->lock );
1675 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1677 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_subpic];
1678 subpicture_t *p_subpic = p_entry->p_subpicture;
1682 if( p_subpic->i_channel != i_channel && ( i_channel != -1 || p_subpic->i_channel == DEFAULT_CHAN ) )
1685 /* You cannot delete subpicture outside of spu_SortSubpictures */
1686 p_entry->b_reject = true;
1689 vlc_mutex_unlock( &p_sys->lock );
1692 /*****************************************************************************
1693 * spu_ControlDefault: default methods for the subpicture unit control.
1694 *****************************************************************************/
1695 static int SpuControl( spu_t *p_spu, int i_query, va_list args )
1697 spu_private_t *p_sys = p_spu->p;
1702 case SPU_CHANNEL_REGISTER:
1703 pi = (int *)va_arg( args, int * );
1704 vlc_mutex_lock( &p_sys->lock );
1706 *pi = p_sys->i_channel++;
1707 vlc_mutex_unlock( &p_sys->lock );
1710 case SPU_CHANNEL_CLEAR:
1711 i = (int)va_arg( args, int );
1712 SpuClearChannel( p_spu, i );
1716 msg_Dbg( p_spu, "control query not supported" );
1717 return VLC_EGENERIC;
1723 /*****************************************************************************
1724 * Object variables callbacks
1725 *****************************************************************************/
1727 /*****************************************************************************
1728 * UpdateSPU: update subpicture settings
1729 *****************************************************************************
1730 * This function is called from CropCallback and at initialization time, to
1731 * retrieve crop information from the input.
1732 *****************************************************************************/
1733 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1735 spu_private_t *p_sys = p_spu->p;
1738 vlc_mutex_lock( &p_sys->lock );
1740 p_sys->b_force_palette = false;
1741 p_sys->b_force_crop = false;
1743 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1745 vlc_mutex_unlock( &p_sys->lock );
1749 p_sys->b_force_crop = true;
1750 p_sys->i_crop_x = var_GetInteger( p_object, "x-start" );
1751 p_sys->i_crop_y = var_GetInteger( p_object, "y-start" );
1752 p_sys->i_crop_width = var_GetInteger( p_object, "x-end" ) - p_sys->i_crop_x;
1753 p_sys->i_crop_height = var_GetInteger( p_object, "y-end" ) - p_sys->i_crop_y;
1755 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1757 memcpy( p_sys->palette, val.p_address, 16 );
1758 p_sys->b_force_palette = true;
1760 vlc_mutex_unlock( &p_sys->lock );
1762 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1763 p_sys->i_crop_x, p_sys->i_crop_y,
1764 p_sys->i_crop_width, p_sys->i_crop_height,
1765 p_sys->b_force_palette );
1768 /*****************************************************************************
1769 * CropCallback: called when the highlight properties are changed
1770 *****************************************************************************
1771 * This callback is called from the input thread when we need cropping
1772 *****************************************************************************/
1773 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1774 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1776 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1778 UpdateSPU( (spu_t *)p_data, p_object );
1782 /*****************************************************************************
1783 * Buffers allocation callbacks for the filters
1784 *****************************************************************************/
1785 struct filter_owner_sys_t
1791 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1793 filter_owner_sys_t *p_sys = p_filter->p_owner;
1795 subpicture_t *p_subpicture = subpicture_New();
1797 p_subpicture->i_channel = p_sys->i_channel;
1798 return p_subpicture;
1800 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1802 VLC_UNUSED( p_filter );
1803 subpicture_Delete( p_subpic );
1806 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1808 VLC_UNUSED(p_filter);
1809 return subpicture_New();
1811 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1813 VLC_UNUSED(p_filter);
1814 subpicture_Delete( p_subpic );
1817 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1819 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1821 VLC_UNUSED(p_filter);
1822 return picture_New( p_fmt->i_chroma,
1823 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
1825 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1827 VLC_UNUSED(p_filter);
1828 picture_Release( p_picture );
1831 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1833 spu_t *p_spu = p_data;
1835 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1837 return VLC_EGENERIC;
1839 p_filter->pf_sub_buffer_new = sub_new_buffer;
1840 p_filter->pf_sub_buffer_del = sub_del_buffer;
1842 p_filter->p_owner = p_sys;
1843 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1844 p_sys->p_spu = p_spu;
1849 static void SubFilterAllocationClean( filter_t *p_filter )
1851 filter_owner_sys_t *p_sys = p_filter->p_owner;
1853 SpuClearChannel( p_sys->p_spu, p_sys->i_channel );
1854 free( p_filter->p_owner );
1857 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1858 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1860 spu_t *p_spu = p_data;
1861 spu_private_t *p_sys = p_spu->p;
1863 VLC_UNUSED(p_object); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
1865 vlc_mutex_lock( &p_sys->lock );
1867 free( p_sys->psz_chain_update );
1868 p_sys->psz_chain_update = strdup( newval.psz_string );
1870 vlc_mutex_unlock( &p_sys->lock );