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"
40 #include <vlc_image.h>
45 /*****************************************************************************
47 *****************************************************************************/
49 /* Number of simultaneous subpictures */
50 #define VOUT_MAX_SUBPICTURES (__MAX(VOUT_MAX_PICTURES, SPU_MAX_PREPARE_TIME/5000))
55 subpicture_t *p_subpicture;
61 spu_heap_entry_t p_entry[VOUT_MAX_SUBPICTURES];
65 static void SpuHeapInit( spu_heap_t * );
66 static int SpuHeapPush( spu_heap_t *, subpicture_t * );
67 static void SpuHeapDeleteAt( spu_heap_t *, int i_index );
68 static int SpuHeapDeleteSubpicture( spu_heap_t *, subpicture_t * );
69 static void SpuHeapClean( spu_heap_t *p_heap );
73 vlc_mutex_t lock; /* lock to protect all followings fields */
77 int i_channel; /**< number of subpicture channels registered */
78 filter_t *p_blend; /**< alpha blending module */
79 filter_t *p_text; /**< text renderer module */
80 filter_t *p_scale_yuvp; /**< scaling module for YUVP */
81 filter_t *p_scale; /**< scaling module (all but YUVP) */
82 bool b_force_crop; /**< force cropping of subpicture */
83 int i_crop_x, i_crop_y, i_crop_width, i_crop_height; /**< cropping */
85 int i_margin; /**< force position of a subpicture */
86 bool b_force_palette; /**< force palette of subpicture */
87 uint8_t palette[4][4]; /**< forced palette */
89 /* Subpiture filters */
90 char *psz_chain_update;
91 filter_chain_t *p_chain;
94 mtime_t i_last_sort_date;
98 struct subpicture_region_private_t
101 picture_t *p_picture;
103 static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t * );
104 static void SpuRegionPrivateDelete( subpicture_region_private_t * );
112 static spu_scale_t spu_scale_create( int w, int h );
113 static spu_scale_t spu_scale_unit(void );
114 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd );
115 static int spu_scale_w( int v, const spu_scale_t s );
116 static int spu_scale_h( int v, const spu_scale_t s );
117 static int spu_invscale_w( int v, const spu_scale_t s );
118 static int spu_invscale_h( int v, const spu_scale_t s );
130 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t );
131 static spu_area_t spu_area_scaled( spu_area_t );
132 static spu_area_t spu_area_unscaled( spu_area_t, spu_scale_t );
133 static bool spu_area_overlap( spu_area_t, spu_area_t );
136 /* Subpicture rendered flag
137 * FIXME ? it could be moved to private ? */
138 #define SUBPICTURE_RENDERED (0x1000)
139 #if SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
140 # error SUBPICTURE_RENDERED too low
143 #define SCALE_UNIT (1000)
145 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic );
146 static int SubpictureCmp( const void *s0, const void *s1 );
148 static void SpuRenderRegion( spu_t *,
149 picture_t *p_pic_dst, spu_area_t *,
150 subpicture_t *, subpicture_region_t *,
151 const spu_scale_t scale_size,
152 const video_format_t *p_fmt,
153 const spu_area_t *p_subtitle_area, int i_subtitle_area,
154 mtime_t render_date );
156 static void UpdateSPU ( spu_t *, vlc_object_t * );
157 static int CropCallback( vlc_object_t *, char const *,
158 vlc_value_t, vlc_value_t, void * );
160 static int SpuControl( spu_t *, int, va_list );
162 static void SpuClearChannel( spu_t *p_spu, int i_channel );
164 /* Buffer allocation for SPU filter (blend, scale, ...) */
165 static subpicture_t *spu_new_buffer( filter_t * );
166 static void spu_del_buffer( filter_t *, subpicture_t * );
167 static picture_t *spu_new_video_buffer( filter_t * );
168 static void spu_del_video_buffer( filter_t *, picture_t * );
170 /* Buffer aloccation fir SUB filter */
171 static int SubFilterCallback( vlc_object_t *, char const *,
172 vlc_value_t, vlc_value_t, void * );
174 static int SubFilterAllocationInit( filter_t *, void * );
175 static void SubFilterAllocationClean( filter_t * );
178 static void SpuRenderCreateAndLoadText( spu_t * );
179 static void SpuRenderCreateAndLoadScale( spu_t * );
180 static void FilterRelease( filter_t *p_filter );
182 /*****************************************************************************
184 *****************************************************************************/
186 * Creates the subpicture unit
188 * \param p_this the parent object which creates the subpicture unit
190 spu_t *__spu_Create( vlc_object_t *p_this )
193 spu_private_t *p_sys;
195 p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
196 VLC_OBJECT_GENERIC, "subpicture" );
201 /* Initialize spu fields */
202 p_spu->pf_control = SpuControl;
203 p_spu->p = p_sys = (spu_private_t*)&p_spu[1];
205 /* Initialize private fields */
206 vlc_mutex_init( &p_sys->lock );
208 SpuHeapInit( &p_sys->heap );
210 p_sys->p_blend = NULL;
211 p_sys->p_text = NULL;
212 p_sys->p_scale = NULL;
213 p_sys->p_scale_yuvp = NULL;
215 /* Register the default subpicture channel */
216 p_sys->i_channel = 2;
218 vlc_object_attach( p_spu, p_this );
220 p_sys->psz_chain_update = NULL;
221 p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
222 SubFilterAllocationInit,
223 SubFilterAllocationClean,
226 /* Load text and scale module */
227 SpuRenderCreateAndLoadText( p_spu );
228 SpuRenderCreateAndLoadScale( p_spu );
231 p_sys->i_last_sort_date = -1;
237 * Initialise the subpicture unit
239 * \param p_spu the subpicture unit object
241 int spu_Init( spu_t *p_spu )
243 spu_private_t *p_sys = p_spu->p;
245 /* If the user requested a sub margin, we force the position. */
246 p_sys->i_margin = var_CreateGetInteger( p_spu, "sub-margin" );
248 var_Create( p_spu, "sub-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
249 var_AddCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
250 var_TriggerCallback( p_spu, "sub-filter" );
256 * Destroy the subpicture unit
258 * \param p_this the parent object which destroys the subpicture unit
260 void spu_Destroy( spu_t *p_spu )
262 spu_private_t *p_sys = p_spu->p;
264 var_DelCallback( p_spu, "sub-filter", SubFilterCallback, p_spu );
267 filter_DeleteBlend( p_sys->p_blend );
270 FilterRelease( p_sys->p_text );
272 if( p_sys->p_scale_yuvp )
273 FilterRelease( p_sys->p_scale_yuvp );
276 FilterRelease( p_sys->p_scale );
278 filter_chain_Delete( p_sys->p_chain );
279 free( p_sys->psz_chain_update );
281 /* Destroy all remaining subpictures */
282 SpuHeapClean( &p_sys->heap );
284 vlc_mutex_destroy( &p_sys->lock );
286 vlc_object_release( p_spu );
290 * Attach/Detach the SPU from any input
292 * \param p_this the object in which to destroy the subpicture unit
293 * \param b_attach to select attach or detach
295 void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
297 vlc_object_t *p_input;
299 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
305 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
306 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
307 vlc_object_release( p_input );
311 /* Delete callback */
312 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
313 vlc_object_release( p_input );
318 * Display a subpicture
320 * Remove the reservation flag of a subpicture, which will cause it to be
322 * \param p_spu the subpicture unit object
323 * \param p_subpic the subpicture to display
325 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
327 spu_private_t *p_sys = p_spu->p;
329 /* DEFAULT_CHAN always reset itself */
330 if( p_subpic->i_channel == DEFAULT_CHAN )
331 SpuClearChannel( p_spu, DEFAULT_CHAN );
333 /* p_private is for spu only and cannot be non NULL here */
334 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
335 assert( r->p_private == NULL );
338 vlc_mutex_lock( &p_sys->lock );
339 if( SpuHeapPush( &p_sys->heap, p_subpic ) )
341 vlc_mutex_unlock( &p_sys->lock );
342 msg_Err( p_spu, "subpicture heap full" );
343 subpicture_Delete( p_subpic );
346 vlc_mutex_unlock( &p_sys->lock );
350 * This function renders all sub picture units in the list.
352 void spu_RenderSubpictures( spu_t *p_spu,
353 picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
354 subpicture_t *p_subpic_list,
355 const video_format_t *p_fmt_src,
356 mtime_t render_subtitle_date )
358 spu_private_t *p_sys = p_spu->p;
360 const mtime_t render_osd_date = mdate();
362 const int i_source_video_width = p_fmt_src->i_width;
363 const int i_source_video_height = p_fmt_src->i_height;
365 unsigned int i_subpicture;
366 subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
368 unsigned int i_subtitle_region_count;
369 spu_area_t p_subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
370 spu_area_t *p_subtitle_area;
373 vlc_mutex_lock( &p_sys->lock );
375 /* Preprocess subpictures */
377 i_subtitle_region_count = 0;
378 for( subpicture_t * p_subpic = p_subpic_list;
380 p_subpic = p_subpic->p_next )
382 if( p_subpic->pf_update_regions )
384 video_format_t fmt_org = *p_fmt_dst;
386 fmt_org.i_visible_width = i_source_video_width;
388 fmt_org.i_visible_height = i_source_video_height;
390 p_subpic->pf_update_regions( p_spu, p_subpic, &fmt_org,
391 p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
395 if( p_subpic->b_subtitle )
397 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
398 i_subtitle_region_count++;
402 pp_subpicture[i_subpicture++] = p_subpic;
405 /* Be sure we have at least 1 picture to process */
406 if( i_subpicture <= 0 )
408 vlc_mutex_unlock( &p_sys->lock );
412 /* Now order subpicture array
413 * XXX The order is *really* important for overlap subtitles positionning */
414 qsort( pp_subpicture, i_subpicture, sizeof(*pp_subpicture), SubpictureCmp );
416 /* Allocate area array for subtitle overlap */
418 p_subtitle_area = p_subtitle_area_buffer;
419 if( i_subtitle_region_count > sizeof(p_subtitle_area_buffer)/sizeof(*p_subtitle_area_buffer) )
420 p_subtitle_area = calloc( i_subtitle_region_count, sizeof(*p_subtitle_area) );
422 /* Create the blending module */
423 if( !p_sys->p_blend )
424 p_spu->p->p_blend = filter_NewBlend( VLC_OBJECT(p_spu), p_fmt_dst );
426 /* Process all subpictures and regions (in the right order) */
427 for( unsigned int i_index = 0; i_index < i_subpicture; i_index++ )
429 subpicture_t *p_subpic = pp_subpicture[i_index];
430 subpicture_region_t *p_region;
432 if( !p_subpic->p_region )
435 /* FIXME when possible use a better rendering size than source size
436 * (max of display size and source size for example) FIXME */
437 int i_render_width = p_subpic->i_original_picture_width;
438 int i_render_height = p_subpic->i_original_picture_height;
439 if( !i_render_width || !i_render_height )
441 if( i_render_width != 0 || i_render_height != 0 )
442 msg_Err( p_spu, "unsupported original picture size %dx%d",
443 i_render_width, i_render_height );
445 p_subpic->i_original_picture_width = i_render_width = i_source_video_width;
446 p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
451 p_sys->p_text->fmt_out.video.i_width =
452 p_sys->p_text->fmt_out.video.i_visible_width = i_render_width;
454 p_sys->p_text->fmt_out.video.i_height =
455 p_sys->p_text->fmt_out.video.i_visible_height = i_render_height;
458 /* Compute scaling from picture to source size */
459 spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
460 i_source_video_height, i_render_height );
462 /* Update scaling from source size to display size(p_fmt_dst) */
463 scale.w = scale.w * p_fmt_dst->i_width / i_source_video_width;
464 scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
466 /* Set default subpicture aspect ratio
467 * FIXME if we only handle 1 aspect ratio per picture, why is it set per
469 p_region = p_subpic->p_region;
470 if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
472 if( p_region->fmt.i_aspect != 0 )
474 p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
475 p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
479 p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
480 p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
484 /* Take care of the aspect ratio */
485 if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
486 p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
488 /* FIXME FIXME what about region->i_x/i_y ? */
490 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
491 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
494 /* Render all regions
495 * We always transform non absolute subtitle into absolute one on the
496 * first rendering to allow good subtitle overlap support.
498 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
502 /* Check scale validity */
503 if( scale.w <= 0 || scale.h <= 0 )
507 SpuRenderRegion( p_spu, p_pic_dst, &area,
508 p_subpic, p_region, scale, p_fmt_dst,
509 p_subtitle_area, i_subtitle_area,
510 p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
512 if( p_subpic->b_subtitle )
514 area = spu_area_unscaled( area, scale );
515 if( !p_subpic->b_absolute && area.i_width > 0 && area.i_height > 0 )
517 p_region->i_x = area.i_x;
518 p_region->i_y = area.i_y;
520 if( p_subtitle_area )
521 p_subtitle_area[i_subtitle_area++] = area;
524 if( p_subpic->b_subtitle )
525 p_subpic->b_absolute = true;
529 if( p_subtitle_area != p_subtitle_area_buffer )
530 free( p_subtitle_area );
532 vlc_mutex_unlock( &p_sys->lock );
535 /*****************************************************************************
536 * spu_SortSubpictures: find the subpictures to display
537 *****************************************************************************
538 * This function parses all subpictures and decides which ones need to be
539 * displayed. If no picture has been selected, display_date will depend on
541 * We also check for ephemer DVD subpictures (subpictures that have
542 * to be removed if a newer one is available), which makes it a lot
543 * more difficult to guess if a subpicture has to be rendered or not.
544 *****************************************************************************/
545 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t render_subtitle_date,
546 bool b_subtitle_only )
548 spu_private_t *p_sys = p_spu->p;
550 subpicture_t *p_subpic = NULL;
551 const mtime_t render_osd_date = mdate();
553 /* Update sub-filter chain */
554 vlc_mutex_lock( &p_sys->lock );
555 char *psz_chain_update = p_sys->psz_chain_update;
556 p_sys->psz_chain_update = NULL;
557 vlc_mutex_unlock( &p_sys->lock );
559 if( psz_chain_update )
561 filter_chain_Reset( p_sys->p_chain, NULL, NULL );
563 filter_chain_AppendFromString( p_spu->p->p_chain, psz_chain_update );
565 free( psz_chain_update );
568 /* Run subpicture filters */
569 filter_chain_SubFilter( p_sys->p_chain, render_osd_date );
571 vlc_mutex_lock( &p_sys->lock );
573 /* We get an easily parsable chained list of subpictures which
574 * ends with NULL since p_subpic was initialized to NULL. */
575 for( i_channel = 0; i_channel < p_sys->i_channel; i_channel++ )
577 subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
578 bool pb_available_late[VOUT_MAX_SUBPICTURES];
581 mtime_t start_date = render_subtitle_date;
582 mtime_t ephemer_subtitle_date = 0;
583 mtime_t ephemer_osd_date = 0;
584 int64_t i_ephemer_subtitle_order = INT64_MIN;
585 int64_t i_ephemer_system_order = INT64_MIN;
588 /* Select available pictures */
589 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
591 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
592 subpicture_t *p_current = p_entry->p_subpicture;
596 if( !p_current || p_entry->b_reject )
598 if( p_entry->b_reject )
599 SpuHeapDeleteAt( &p_sys->heap, i_index );
603 if( p_current->i_channel != i_channel ||
604 ( b_subtitle_only && !p_current->b_subtitle ) )
608 const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
610 render_date < p_current->i_start )
612 /* Too early, come back next monday */
616 mtime_t *pi_ephemer_date = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
617 int64_t *pi_ephemer_order = p_current->b_subtitle ? &i_ephemer_subtitle_order : &i_ephemer_system_order;
618 if( p_current->i_start >= *pi_ephemer_date )
620 *pi_ephemer_date = p_current->i_start;
621 if( p_current->i_order > *pi_ephemer_order )
622 *pi_ephemer_order = p_current->i_order;
625 b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
627 b_late = b_stop_valid && p_current->i_stop <= render_date;
629 /* start_date will be used for correct automatic overlap support
630 * in case picture that should not be displayed anymore (display_time)
631 * overlap with a picture to be displayed (p_current->i_start) */
632 if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
633 start_date = p_current->i_start;
636 p_available_subpic[i_available] = p_current;
637 pb_available_late[i_available] = b_late;
641 /* Only forced old picture display at the transition */
642 if( start_date < p_sys->i_last_sort_date )
643 start_date = p_sys->i_last_sort_date;
644 if( start_date <= 0 )
645 start_date = INT64_MAX;
647 /* Select pictures to be displayed */
648 for( i_index = 0; i_index < i_available; i_index++ )
650 subpicture_t *p_current = p_available_subpic[i_index];
651 bool b_late = pb_available_late[i_index];
653 const mtime_t stop_date = p_current->b_subtitle ? __MAX( start_date, p_sys->i_last_sort_date ) : render_osd_date;
654 const mtime_t ephemer_date = p_current->b_subtitle ? ephemer_subtitle_date : ephemer_osd_date;
655 const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
657 /* Destroy late and obsolete ephemer subpictures */
658 bool b_rejet = b_late && p_current->i_stop <= stop_date;
659 if( p_current->b_ephemer )
661 if( p_current->i_start < ephemer_date )
663 else if( p_current->i_start == ephemer_date &&
664 p_current->i_order < i_ephemer_order )
669 SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
671 SubpictureChain( &p_subpic, p_current );
675 p_sys->i_last_sort_date = render_subtitle_date;
676 vlc_mutex_unlock( &p_sys->lock );
681 void spu_OffsetSubtitleDate( spu_t *p_spu, mtime_t i_duration )
683 spu_private_t *p_sys = p_spu->p;
685 vlc_mutex_lock( &p_sys->lock );
686 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
688 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i];
689 subpicture_t *p_current = p_entry->p_subpicture;
691 if( p_current && p_current->b_subtitle )
693 if( p_current->i_start > 0 )
694 p_current->i_start += i_duration;
695 if( p_current->i_stop > 0 )
696 p_current->i_stop += i_duration;
699 vlc_mutex_unlock( &p_sys->lock );
702 /*****************************************************************************
703 * subpicture_t allocation
704 *****************************************************************************/
705 subpicture_t *subpicture_New( void )
707 subpicture_t *p_subpic = calloc( 1, sizeof(*p_subpic) );
711 p_subpic->i_order = 0;
712 p_subpic->b_absolute = true;
713 p_subpic->b_fade = false;
714 p_subpic->b_subtitle = false;
715 p_subpic->i_alpha = 0xFF;
716 p_subpic->p_region = NULL;
717 p_subpic->pf_destroy = NULL;
718 p_subpic->p_sys = NULL;
723 void subpicture_Delete( subpicture_t *p_subpic )
725 subpicture_region_ChainDelete( p_subpic->p_region );
726 p_subpic->p_region = NULL;
728 if( p_subpic->pf_destroy )
730 p_subpic->pf_destroy( p_subpic );
735 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
737 p_subpic->p_next = *pp_head;
742 subpicture_t *subpicture_NewFromPicture( vlc_object_t *p_obj,
743 picture_t *p_picture, vlc_fourcc_t i_chroma )
746 video_format_t fmt_in = p_picture->format;
749 video_format_t fmt_out;
751 fmt_out.i_chroma = i_chroma;
754 image_handler_t *p_image = image_HandlerCreate( p_obj );
758 picture_t *p_pip = image_Convert( p_image, p_picture, &fmt_in, &fmt_out );
760 image_HandlerDelete( p_image );
765 subpicture_t *p_subpic = subpicture_New();
768 picture_Release( p_pip );
772 p_subpic->i_original_picture_width = fmt_out.i_width;
773 p_subpic->i_original_picture_height = fmt_out.i_height;
775 fmt_out.i_aspect = 0;
777 fmt_out.i_sar_den = 0;
779 p_subpic->p_region = subpicture_region_New( &fmt_out );
780 if( p_subpic->p_region )
782 picture_Release( p_subpic->p_region->p_picture );
783 p_subpic->p_region->p_picture = p_pip;
787 picture_Release( p_pip );
792 /*****************************************************************************
793 * subpicture_region_t allocation
794 *****************************************************************************/
795 subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
797 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
801 p_region->fmt = *p_fmt;
802 p_region->fmt.p_palette = NULL;
803 if( p_fmt->i_chroma == VLC_CODEC_YUVP )
805 p_region->fmt.p_palette = calloc( 1, sizeof(*p_region->fmt.p_palette) );
806 if( p_fmt->p_palette )
807 *p_region->fmt.p_palette = *p_fmt->p_palette;
809 p_region->i_alpha = 0xff;
810 p_region->p_next = NULL;
811 p_region->p_private = NULL;
812 p_region->psz_text = NULL;
813 p_region->p_style = NULL;
814 p_region->p_picture = NULL;
816 if( p_fmt->i_chroma == VLC_CODEC_TEXT )
819 p_region->p_picture = picture_New( p_fmt->i_chroma, p_fmt->i_width, p_fmt->i_height,
821 if( !p_region->p_picture )
823 free( p_fmt->p_palette );
832 void subpicture_region_Delete( subpicture_region_t *p_region )
837 if( p_region->p_private )
838 SpuRegionPrivateDelete( p_region->p_private );
840 if( p_region->p_picture )
841 picture_Release( p_region->p_picture );
843 free( p_region->fmt.p_palette );
845 free( p_region->psz_text );
846 free( p_region->psz_html );
847 if( p_region->p_style )
848 text_style_Delete( p_region->p_style );
853 void subpicture_region_ChainDelete( subpicture_region_t *p_head )
857 subpicture_region_t *p_next = p_head->p_next;
859 subpicture_region_Delete( p_head );
867 /*****************************************************************************
869 *****************************************************************************/
870 static void SpuHeapInit( spu_heap_t *p_heap )
872 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
874 spu_heap_entry_t *e = &p_heap->p_entry[i];
876 e->p_subpicture = NULL;
881 static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
883 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
885 spu_heap_entry_t *e = &p_heap->p_entry[i];
887 if( e->p_subpicture )
890 e->p_subpicture = p_subpic;
897 static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
899 spu_heap_entry_t *e = &p_heap->p_entry[i_index];
901 if( e->p_subpicture )
902 subpicture_Delete( e->p_subpicture );
904 e->p_subpicture = NULL;
907 static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
909 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
911 spu_heap_entry_t *e = &p_heap->p_entry[i];
913 if( e->p_subpicture != p_subpic )
916 SpuHeapDeleteAt( p_heap, i );
922 static void SpuHeapClean( spu_heap_t *p_heap )
924 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
926 spu_heap_entry_t *e = &p_heap->p_entry[i];
927 if( e->p_subpicture )
928 subpicture_Delete( e->p_subpicture );
932 static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t *p_fmt )
934 subpicture_region_private_t *p_private = malloc( sizeof(*p_private) );
939 p_private->fmt = *p_fmt;
940 if( p_fmt->p_palette )
942 p_private->fmt.p_palette = malloc( sizeof(*p_private->fmt.p_palette) );
943 if( p_private->fmt.p_palette )
944 *p_private->fmt.p_palette = *p_fmt->p_palette;
946 p_private->p_picture = NULL;
950 static void SpuRegionPrivateDelete( subpicture_region_private_t *p_private )
952 if( p_private->p_picture )
953 picture_Release( p_private->p_picture );
954 free( p_private->fmt.p_palette );
958 static void FilterRelease( filter_t *p_filter )
960 if( p_filter->p_module )
961 module_unneed( p_filter, p_filter->p_module );
963 vlc_object_detach( p_filter );
964 vlc_object_release( p_filter );
967 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
971 assert( !p_spu->p->p_text );
974 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
975 VLC_OBJECT_GENERIC, "spu text" );
979 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
981 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
982 p_text->fmt_out.video.i_width =
983 p_text->fmt_out.video.i_visible_width = 32;
984 p_text->fmt_out.video.i_height =
985 p_text->fmt_out.video.i_visible_height = 32;
987 p_text->pf_sub_buffer_new = spu_new_buffer;
988 p_text->pf_sub_buffer_del = spu_del_buffer;
990 vlc_object_attach( p_text, p_spu );
992 /* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
993 * same than these 2 calls ? */
994 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
995 if( psz_modulename && *psz_modulename )
997 p_text->p_module = module_need( p_text, "text renderer",
998 psz_modulename, true );
1000 free( psz_modulename );
1002 if( !p_text->p_module )
1003 p_text->p_module = module_need( p_text, "text renderer", NULL, false );
1005 /* Create a few variables used for enhanced text rendering */
1006 var_Create( p_text, "spu-duration", VLC_VAR_TIME );
1007 var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
1008 var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
1009 var_Create( p_text, "scale", VLC_VAR_INTEGER );
1012 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
1013 vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
1018 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
1019 VLC_OBJECT_GENERIC, "scale" );
1023 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
1024 p_scale->fmt_in.video.i_chroma = i_src_chroma;
1025 p_scale->fmt_in.video.i_width =
1026 p_scale->fmt_in.video.i_height = 32;
1028 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
1029 p_scale->fmt_out.video.i_chroma = i_dst_chroma;
1030 p_scale->fmt_out.video.i_width =
1031 p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
1033 p_scale->pf_vout_buffer_new = spu_new_video_buffer;
1034 p_scale->pf_vout_buffer_del = spu_del_video_buffer;
1036 vlc_object_attach( p_scale, p_obj );
1037 p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
1041 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
1043 assert( !p_spu->p->p_scale );
1044 assert( !p_spu->p->p_scale_yuvp );
1045 /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
1047 p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
1048 VLC_CODEC_YUVA, VLC_CODEC_YUVA, true );
1049 /* This one is used for YUVP to YUVA/RGBA without scaling
1050 * FIXME rename it */
1051 p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
1052 VLC_CODEC_YUVP, VLC_CODEC_YUVA, false );
1055 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
1056 subpicture_t *p_subpic, subpicture_region_t *p_region,
1057 int i_min_scale_ratio, mtime_t render_date )
1059 filter_t *p_text = p_spu->p->p_text;
1061 assert( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
1063 if( !p_text || !p_text->p_module )
1066 /* Setup 3 variables which can be used to render
1067 * time-dependent text (and effects). The first indicates
1068 * the total amount of time the text will be on screen,
1069 * the second the amount of time it has already been on
1070 * screen (can be a negative value as text is layed out
1071 * before it is rendered) and the third is a feedback
1072 * variable from the renderer - if the renderer sets it
1073 * then this particular text is time-dependent, eg. the
1074 * visual progress bar inside the text in karaoke and the
1075 * text needs to be rendered multiple times in order for
1076 * the effect to work - we therefore need to return the
1077 * region to its original state at the end of the loop,
1078 * instead of leaving it in YUVA or YUVP.
1079 * Any renderer which is unaware of how to render
1080 * time-dependent text can happily ignore the variables
1081 * and render the text the same as usual - it should at
1082 * least show up on screen, but the effect won't change
1083 * the text over time.
1085 var_SetTime( p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
1086 var_SetTime( p_text, "spu-elapsed", render_date );
1087 var_SetBool( p_text, "text-rerender", false );
1088 var_SetInteger( p_text, "scale", i_min_scale_ratio );
1090 if( p_text->pf_render_html && p_region->psz_html )
1092 p_text->pf_render_html( p_text, p_region, p_region );
1094 else if( p_text->pf_render_text )
1096 p_text->pf_render_text( p_text, p_region, p_region );
1098 *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
1101 p_region->i_align |= SUBPICTURE_RENDERED;
1105 * A few scale functions helpers.
1107 static spu_scale_t spu_scale_create( int w, int h )
1109 spu_scale_t s = { .w = w, .h = h };
1116 static spu_scale_t spu_scale_unit( void )
1118 return spu_scale_create( SCALE_UNIT, SCALE_UNIT );
1120 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
1122 return spu_scale_create( wn * SCALE_UNIT / wd,
1123 hn * SCALE_UNIT / hd );
1125 static int spu_scale_w( int v, const spu_scale_t s )
1127 return v * s.w / SCALE_UNIT;
1129 static int spu_scale_h( int v, const spu_scale_t s )
1131 return v * s.h / SCALE_UNIT;
1133 static int spu_invscale_w( int v, const spu_scale_t s )
1135 return v * SCALE_UNIT / s.w;
1137 static int spu_invscale_h( int v, const spu_scale_t s )
1139 return v * SCALE_UNIT / s.h;
1143 * A few area functions helpers
1145 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t s )
1147 spu_area_t a = { .i_x = x, .i_y = y, .i_width = w, .i_height = h, .scale = s };
1150 static spu_area_t spu_area_scaled( spu_area_t a )
1152 if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
1155 a.i_x = spu_scale_w( a.i_x, a.scale );
1156 a.i_y = spu_scale_h( a.i_y, a.scale );
1158 a.i_width = spu_scale_w( a.i_width, a.scale );
1159 a.i_height = spu_scale_h( a.i_height, a.scale );
1161 a.scale = spu_scale_unit();
1164 static spu_area_t spu_area_unscaled( spu_area_t a, spu_scale_t s )
1166 if( a.scale.w == s.w && a.scale.h == s.h )
1169 a = spu_area_scaled( a );
1171 a.i_x = spu_invscale_w( a.i_x, s );
1172 a.i_y = spu_invscale_h( a.i_y, s );
1174 a.i_width = spu_invscale_w( a.i_width, s );
1175 a.i_height = spu_invscale_h( a.i_height, s );
1180 static bool spu_area_overlap( spu_area_t a, spu_area_t b )
1185 a = spu_area_scaled( a );
1186 b = spu_area_scaled( b );
1188 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 ) &&
1189 __MAX( a.i_y-i_dy, b.i_y ) < __MIN( a.i_y+a.i_height+i_dy, b.i_y+b.i_height );
1193 * Avoid area overlapping
1195 static void SpuAreaFixOverlap( spu_area_t *p_dst,
1196 const spu_area_t *p_master,
1197 const spu_area_t *p_sub, int i_sub, int i_align )
1199 spu_area_t a = spu_area_scaled( *p_dst );
1200 bool b_moved = false;
1203 assert( p_master->i_x == 0 && p_master->i_y == 0 );
1205 /* Check for overlap
1206 * XXX It is not fast O(n^2) but we should not have a lot of region */
1210 for( int i = 0; i < i_sub; i++ )
1212 spu_area_t sub = spu_area_scaled( p_sub[i] );
1214 if( !spu_area_overlap( a, sub ) )
1217 if( i_align & SUBPICTURE_ALIGN_TOP )
1220 int i_y = sub.i_y + sub.i_height;
1221 if( i_y + a.i_height > p_master->i_height )
1226 else if( i_align & SUBPICTURE_ALIGN_BOTTOM )
1229 int i_y = sub.i_y - a.i_height;
1237 /* TODO what to do in this case? */
1238 //fprintf( stderr, "Overlap with unsupported alignment\n" );
1248 *p_dst = spu_area_unscaled( a, p_dst->scale );
1255 static void SpuRegionPlace( int *pi_x, int *pi_y,
1256 const subpicture_t *p_subpic,
1257 const subpicture_region_t *p_region,
1260 const int i_delta_x = p_region->i_x;
1261 const int i_delta_y = p_region->i_y;
1264 assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
1265 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
1269 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
1271 i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
1275 i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
1278 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
1282 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
1284 i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
1288 i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
1291 if( p_subpic->b_absolute )
1297 /* Margin shifts all subpictures */
1298 if( i_margin_y != 0 )
1301 /* Clamp offset to not go out of the screen (when possible) */
1302 const int i_error_x = (i_x + p_region->fmt.i_width) - p_subpic->i_original_picture_width;
1308 const int i_error_y = (i_y + p_region->fmt.i_height) - p_subpic->i_original_picture_height;
1319 * This function computes the current alpha value for a given region.
1321 static int SpuRegionAlpha( subpicture_t *p_subpic, subpicture_region_t *p_region )
1323 /* Compute alpha blend value */
1324 int i_fade_alpha = 255;
1325 if( p_subpic->b_fade )
1327 mtime_t i_fade_start = ( p_subpic->i_stop +
1328 p_subpic->i_start ) / 2;
1329 mtime_t i_now = mdate();
1331 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
1333 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
1334 ( p_subpic->i_stop - i_fade_start );
1337 return i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
1341 * It will render the provided region onto p_pic_dst.
1344 static void SpuRenderRegion( spu_t *p_spu,
1345 picture_t *p_pic_dst, spu_area_t *p_area,
1346 subpicture_t *p_subpic, subpicture_region_t *p_region,
1347 const spu_scale_t scale_size,
1348 const video_format_t *p_fmt,
1349 const spu_area_t *p_subtitle_area, int i_subtitle_area,
1350 mtime_t render_date )
1352 spu_private_t *p_sys = p_spu->p;
1354 video_format_t fmt_original = p_region->fmt;
1355 bool b_rerender_text = false;
1356 bool b_restore_format = false;
1360 video_format_t region_fmt;
1361 picture_t *p_region_picture;
1363 /* Invalidate area by default */
1364 *p_area = spu_area_create( 0,0, 0,0, scale_size );
1366 /* Render text region */
1367 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1369 const int i_min_scale_ratio = SCALE_UNIT; /* FIXME what is the right value? (scale_size is not) */
1370 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region,
1371 i_min_scale_ratio, render_date );
1372 b_restore_format = b_rerender_text;
1374 /* Check if the rendering has failed ... */
1375 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1379 /* Force palette if requested
1380 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
1381 * instead of only the right one (being the dvd spu).
1383 const bool b_using_palette = p_region->fmt.i_chroma == VLC_CODEC_YUVP;
1384 const bool b_force_palette = b_using_palette && p_sys->b_force_palette;
1385 const bool b_force_crop = b_force_palette && p_sys->b_force_crop;
1386 bool b_changed_palette = false;
1389 /* Compute the margin which is expressed in destination pixel unit
1390 * The margin is applied only to subtitle and when no forced crop is
1391 * requested (dvd menu) */
1393 if( !b_force_crop && p_subpic->b_subtitle )
1394 i_margin_y = spu_invscale_h( p_sys->i_margin, scale_size );
1396 /* Place the picture
1397 * We compute the position in the rendered size */
1398 SpuRegionPlace( &i_x_offset, &i_y_offset,
1399 p_subpic, p_region, i_margin_y );
1401 /* Save this position for subtitle overlap support
1402 * it is really important that there are given without scale_size applied */
1403 *p_area = spu_area_create( i_x_offset, i_y_offset,
1404 p_region->fmt.i_width, p_region->fmt.i_height,
1407 /* Handle overlapping subtitles when possible */
1408 if( p_subpic->b_subtitle && !p_subpic->b_absolute )
1410 spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
1413 SpuAreaFixOverlap( p_area, &display, p_subtitle_area, i_subtitle_area,
1414 p_region->i_align );
1417 /* Fix the position for the current scale_size */
1418 i_x_offset = spu_scale_w( p_area->i_x, p_area->scale );
1419 i_y_offset = spu_scale_h( p_area->i_y, p_area->scale );
1422 if( b_force_palette )
1424 video_palette_t *p_palette = p_region->fmt.p_palette;
1425 video_palette_t palette;
1427 /* We suppose DVD palette here */
1428 palette.i_entries = 4;
1429 for( int i = 0; i < 4; i++ )
1430 for( int j = 0; j < 4; j++ )
1431 palette.palette[i][j] = p_sys->palette[i][j];
1433 if( p_palette->i_entries == palette.i_entries )
1435 for( int i = 0; i < p_palette->i_entries; i++ )
1436 for( int j = 0; j < 4; j++ )
1437 b_changed_palette |= p_palette->palette[i][j] != palette.palette[i][j];
1441 b_changed_palette = true;
1443 *p_palette = palette;
1447 region_fmt = p_region->fmt;
1448 p_region_picture = p_region->p_picture;
1451 /* Scale from rendered size to destination size */
1452 if( p_sys->p_scale && p_sys->p_scale->p_module &&
1453 ( !b_using_palette || ( p_sys->p_scale_yuvp && p_sys->p_scale_yuvp->p_module ) ) &&
1454 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_using_palette ) )
1456 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
1457 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
1459 /* Destroy the cache if unusable */
1460 if( p_region->p_private )
1462 subpicture_region_private_t *p_private = p_region->p_private;
1463 bool b_changed = false;
1465 /* Check resize changes */
1466 if( i_dst_width != p_private->fmt.i_width ||
1467 i_dst_height != p_private->fmt.i_height )
1470 /* Check forced palette changes */
1471 if( b_changed_palette )
1476 SpuRegionPrivateDelete( p_private );
1477 p_region->p_private = NULL;
1481 /* Scale if needed into cache */
1482 if( !p_region->p_private && i_dst_width > 0 && i_dst_height > 0 )
1484 filter_t *p_scale = p_sys->p_scale;
1486 picture_t *p_picture = p_region->p_picture;
1487 picture_Hold( p_picture );
1489 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
1490 if( b_using_palette )
1492 filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
1494 p_scale_yuvp->fmt_in.video = p_region->fmt;
1496 /* TODO converting to RGBA for RGB video output is better */
1497 p_scale_yuvp->fmt_out.video = p_region->fmt;
1498 p_scale_yuvp->fmt_out.video.i_chroma = VLC_CODEC_YUVA;
1500 p_picture = p_scale_yuvp->pf_video_filter( p_scale_yuvp, p_picture );
1503 /* Well we will try conversion+scaling */
1504 msg_Warn( p_spu, "%4.4s to %4.4s conversion failed",
1505 (const char*)&p_scale_yuvp->fmt_in.video.i_chroma,
1506 (const char*)&p_scale_yuvp->fmt_out.video.i_chroma );
1510 /* Conversion(except from YUVP)/Scaling */
1512 ( p_picture->format.i_width != i_dst_width ||
1513 p_picture->format.i_height != i_dst_height ) )
1515 p_scale->fmt_in.video = p_picture->format;
1516 p_scale->fmt_out.video = p_picture->format;
1518 p_scale->fmt_out.video.i_width = i_dst_width;
1519 p_scale->fmt_out.video.i_height = i_dst_height;
1521 p_scale->fmt_out.video.i_visible_width =
1522 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
1523 p_scale->fmt_out.video.i_visible_height =
1524 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
1526 p_picture = p_scale->pf_video_filter( p_scale, p_picture );
1528 msg_Err( p_spu, "scaling failed" );
1534 p_region->p_private = SpuRegionPrivateNew( &p_picture->format );
1535 if( p_region->p_private )
1537 p_region->p_private->p_picture = p_picture;
1538 if( !p_region->p_private->p_picture )
1540 SpuRegionPrivateDelete( p_region->p_private );
1541 p_region->p_private = NULL;
1546 picture_Release( p_picture );
1551 /* And use the scaled picture */
1552 if( p_region->p_private )
1554 region_fmt = p_region->p_private->fmt;
1555 p_region_picture = p_region->p_private->p_picture;
1559 /* Force cropping if requested */
1562 int i_crop_x = spu_scale_w( p_sys->i_crop_x, scale_size );
1563 int i_crop_y = spu_scale_h( p_sys->i_crop_y, scale_size );
1564 int i_crop_width = spu_scale_w( p_sys->i_crop_width, scale_size );
1565 int i_crop_height= spu_scale_h( p_sys->i_crop_height,scale_size );
1567 /* Find the intersection */
1568 if( i_crop_x + i_crop_width <= i_x_offset ||
1569 i_x_offset + (int)region_fmt.i_visible_width < i_crop_x ||
1570 i_crop_y + i_crop_height <= i_y_offset ||
1571 i_y_offset + (int)region_fmt.i_visible_height < i_crop_y )
1573 /* No intersection */
1574 region_fmt.i_visible_width =
1575 region_fmt.i_visible_height = 0;
1579 int i_x, i_y, i_x_end, i_y_end;
1580 i_x = __MAX( i_crop_x, i_x_offset );
1581 i_y = __MAX( i_crop_y, i_y_offset );
1582 i_x_end = __MIN( i_crop_x + i_crop_width,
1583 i_x_offset + (int)region_fmt.i_visible_width );
1584 i_y_end = __MIN( i_crop_y + i_crop_height,
1585 i_y_offset + (int)region_fmt.i_visible_height );
1587 region_fmt.i_x_offset = i_x - i_x_offset;
1588 region_fmt.i_y_offset = i_y - i_y_offset;
1589 region_fmt.i_visible_width = i_x_end - i_x;
1590 region_fmt.i_visible_height = i_y_end - i_y;
1592 i_x_offset = __MAX( i_x, 0 );
1593 i_y_offset = __MAX( i_y, 0 );
1597 /* Update the blender */
1598 if( filter_ConfigureBlend( p_spu->p->p_blend,
1599 p_fmt->i_width, p_fmt->i_height,
1601 filter_Blend( p_spu->p->p_blend,
1602 p_pic_dst, i_x_offset, i_y_offset,
1603 p_region_picture, SpuRegionAlpha( p_subpic, p_region ) ) )
1605 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1606 (char *)&p_sys->p_blend->fmt_in.video.i_chroma,
1607 (char *)&p_sys->p_blend->fmt_out.video.i_chroma );
1611 if( b_rerender_text )
1613 /* Some forms of subtitles need to be re-rendered more than
1614 * once, eg. karaoke. We therefore restore the region to its
1615 * pre-rendered state, so the next time through everything is
1618 if( p_region->p_picture )
1620 picture_Release( p_region->p_picture );
1621 p_region->p_picture = NULL;
1623 if( p_region->p_private )
1625 SpuRegionPrivateDelete( p_region->p_private );
1626 p_region->p_private = NULL;
1628 p_region->i_align &= ~SUBPICTURE_RENDERED;
1630 if( b_restore_format )
1631 p_region->fmt = fmt_original;
1635 * This function compares two 64 bits integers.
1636 * It can be used by qsort.
1638 static int IntegerCmp( int64_t i0, int64_t i1 )
1640 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
1643 * This function compares 2 subpictures using the following properties
1644 * (ordered by priority)
1645 * 1. absolute positionning
1647 * 3. creation order (per channel)
1649 * It can be used by qsort.
1651 * XXX spu_RenderSubpictures depends heavily on this order.
1653 static int SubpictureCmp( const void *s0, const void *s1 )
1655 subpicture_t *p_subpic0 = *(subpicture_t**)s0;
1656 subpicture_t *p_subpic1 = *(subpicture_t**)s1;
1659 r = IntegerCmp( !p_subpic0->b_absolute, !p_subpic1->b_absolute );
1661 r = IntegerCmp( p_subpic0->i_start, p_subpic1->i_start );
1663 r = IntegerCmp( p_subpic0->i_channel, p_subpic1->i_channel );
1665 r = IntegerCmp( p_subpic0->i_order, p_subpic1->i_order );
1669 /*****************************************************************************
1670 * SpuClearChannel: clear an spu channel
1671 *****************************************************************************
1672 * This function destroys the subpictures which belong to the spu channel
1673 * corresponding to i_channel_id.
1674 *****************************************************************************/
1675 static void SpuClearChannel( spu_t *p_spu, int i_channel )
1677 spu_private_t *p_sys = p_spu->p;
1678 int i_subpic; /* subpicture index */
1680 vlc_mutex_lock( &p_sys->lock );
1682 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1684 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_subpic];
1685 subpicture_t *p_subpic = p_entry->p_subpicture;
1689 if( p_subpic->i_channel != i_channel && ( i_channel != -1 || p_subpic->i_channel == DEFAULT_CHAN ) )
1692 /* You cannot delete subpicture outside of spu_SortSubpictures */
1693 p_entry->b_reject = true;
1696 vlc_mutex_unlock( &p_sys->lock );
1699 /*****************************************************************************
1700 * spu_ControlDefault: default methods for the subpicture unit control.
1701 *****************************************************************************/
1702 static int SpuControl( spu_t *p_spu, int i_query, va_list args )
1704 spu_private_t *p_sys = p_spu->p;
1709 case SPU_CHANNEL_REGISTER:
1710 pi = (int *)va_arg( args, int * );
1711 vlc_mutex_lock( &p_sys->lock );
1713 *pi = p_sys->i_channel++;
1714 vlc_mutex_unlock( &p_sys->lock );
1717 case SPU_CHANNEL_CLEAR:
1718 i = (int)va_arg( args, int );
1719 SpuClearChannel( p_spu, i );
1723 msg_Dbg( p_spu, "control query not supported" );
1724 return VLC_EGENERIC;
1730 /*****************************************************************************
1731 * Object variables callbacks
1732 *****************************************************************************/
1734 /*****************************************************************************
1735 * UpdateSPU: update subpicture settings
1736 *****************************************************************************
1737 * This function is called from CropCallback and at initialization time, to
1738 * retrieve crop information from the input.
1739 *****************************************************************************/
1740 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1742 spu_private_t *p_sys = p_spu->p;
1745 vlc_mutex_lock( &p_sys->lock );
1747 p_sys->b_force_palette = false;
1748 p_sys->b_force_crop = false;
1750 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1752 vlc_mutex_unlock( &p_sys->lock );
1756 p_sys->b_force_crop = true;
1757 p_sys->i_crop_x = var_GetInteger( p_object, "x-start" );
1758 p_sys->i_crop_y = var_GetInteger( p_object, "y-start" );
1759 p_sys->i_crop_width = var_GetInteger( p_object, "x-end" ) - p_sys->i_crop_x;
1760 p_sys->i_crop_height = var_GetInteger( p_object, "y-end" ) - p_sys->i_crop_y;
1762 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1764 memcpy( p_sys->palette, val.p_address, 16 );
1765 p_sys->b_force_palette = true;
1767 vlc_mutex_unlock( &p_sys->lock );
1769 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1770 p_sys->i_crop_x, p_sys->i_crop_y,
1771 p_sys->i_crop_width, p_sys->i_crop_height,
1772 p_sys->b_force_palette );
1775 /*****************************************************************************
1776 * CropCallback: called when the highlight properties are changed
1777 *****************************************************************************
1778 * This callback is called from the input thread when we need cropping
1779 *****************************************************************************/
1780 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1781 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1783 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1785 UpdateSPU( (spu_t *)p_data, p_object );
1789 /*****************************************************************************
1790 * Buffers allocation callbacks for the filters
1791 *****************************************************************************/
1792 struct filter_owner_sys_t
1798 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1800 filter_owner_sys_t *p_sys = p_filter->p_owner;
1802 subpicture_t *p_subpicture = subpicture_New();
1804 p_subpicture->i_channel = p_sys->i_channel;
1805 return p_subpicture;
1807 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1809 VLC_UNUSED( p_filter );
1810 subpicture_Delete( p_subpic );
1813 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1815 VLC_UNUSED(p_filter);
1816 return subpicture_New();
1818 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1820 VLC_UNUSED(p_filter);
1821 subpicture_Delete( p_subpic );
1824 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1826 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1828 VLC_UNUSED(p_filter);
1829 return picture_New( p_fmt->i_chroma,
1830 p_fmt->i_width, p_fmt->i_height, p_fmt->i_aspect );
1832 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1834 VLC_UNUSED(p_filter);
1835 picture_Release( p_picture );
1838 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1840 spu_t *p_spu = p_data;
1842 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1844 return VLC_EGENERIC;
1846 p_filter->pf_sub_buffer_new = sub_new_buffer;
1847 p_filter->pf_sub_buffer_del = sub_del_buffer;
1849 p_filter->p_owner = p_sys;
1850 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1851 p_sys->p_spu = p_spu;
1856 static void SubFilterAllocationClean( filter_t *p_filter )
1858 filter_owner_sys_t *p_sys = p_filter->p_owner;
1860 SpuClearChannel( p_sys->p_spu, p_sys->i_channel );
1861 free( p_filter->p_owner );
1864 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1865 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1867 spu_t *p_spu = p_data;
1868 spu_private_t *p_sys = p_spu->p;
1870 VLC_UNUSED(p_object); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
1872 vlc_mutex_lock( &p_sys->lock );
1874 free( p_sys->psz_chain_update );
1875 p_sys->psz_chain_update = strdup( newval.psz_string );
1877 vlc_mutex_unlock( &p_sys->lock );