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 *****************************************************************************/
36 #include <vlc_common.h>
38 #include <vlc_block.h>
39 #include <vlc_filter.h>
41 #include "../libvlc.h"
42 #include "vout_internal.h"
43 #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 SubpictureUpdate( subpicture_t *,
146 const video_format_t *p_fmt_src,
147 const video_format_t *p_fmt_dst,
149 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic );
150 static int SubpictureCmp( const void *s0, const void *s1 );
152 static void SpuRenderRegion( spu_t *,
153 picture_t *p_pic_dst, spu_area_t *,
154 subpicture_t *, subpicture_region_t *,
155 const spu_scale_t scale_size,
156 const video_format_t *p_fmt,
157 const spu_area_t *p_subtitle_area, int i_subtitle_area,
158 mtime_t render_date );
160 static void UpdateSPU ( spu_t *, vlc_object_t * );
161 static int CropCallback( vlc_object_t *, char const *,
162 vlc_value_t, vlc_value_t, void * );
163 static int MarginCallback( vlc_object_t *, char const *,
164 vlc_value_t, vlc_value_t, void * );
166 static int SpuControl( spu_t *, int, va_list );
168 static void SpuClearChannel( spu_t *p_spu, int i_channel );
170 /* Buffer allocation for SPU filter (blend, scale, ...) */
171 static subpicture_t *spu_new_buffer( filter_t * );
172 static void spu_del_buffer( filter_t *, subpicture_t * );
173 static picture_t *spu_new_video_buffer( filter_t * );
174 static void spu_del_video_buffer( filter_t *, picture_t * );
176 /* Buffer aloccation fir SUB filter */
177 static int SubFilterCallback( vlc_object_t *, char const *,
178 vlc_value_t, vlc_value_t, void * );
180 static int SubFilterAllocationInit( filter_t *, void * );
181 static void SubFilterAllocationClean( filter_t * );
184 static void SpuRenderCreateAndLoadText( spu_t * );
185 static void SpuRenderCreateAndLoadScale( spu_t * );
186 static void FilterRelease( filter_t *p_filter );
188 /*****************************************************************************
190 *****************************************************************************/
194 * Creates the subpicture unit
196 * \param p_this the parent object which creates the subpicture unit
198 spu_t *spu_Create( vlc_object_t *p_this )
201 spu_private_t *p_sys;
203 p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
204 VLC_OBJECT_GENERIC, "subpicture" );
207 vlc_object_attach( p_spu, p_this );
209 /* Initialize spu fields */
210 p_spu->pf_control = SpuControl;
211 p_spu->p = p_sys = (spu_private_t*)&p_spu[1];
213 /* Initialize private fields */
214 vlc_mutex_init( &p_sys->lock );
216 SpuHeapInit( &p_sys->heap );
218 p_sys->p_blend = NULL;
219 p_sys->p_text = NULL;
220 p_sys->p_scale = NULL;
221 p_sys->p_scale_yuvp = NULL;
223 p_sys->i_margin = var_InheritInteger( p_spu, "sub-margin" );
225 /* Register the default subpicture channel */
226 p_sys->i_channel = 2;
228 p_sys->psz_chain_update = NULL;
229 p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
230 SubFilterAllocationInit,
231 SubFilterAllocationClean,
234 /* Load text and scale module */
235 SpuRenderCreateAndLoadText( p_spu );
236 SpuRenderCreateAndLoadScale( p_spu );
239 p_sys->i_last_sort_date = -1;
245 * Initialise the subpicture unit
247 * \param p_spu the subpicture unit object
249 int spu_Init( spu_t *p_spu )
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 filter_DeleteBlend( 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_Create( p_input, "highlight", VLC_VAR_BOOL );
310 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
311 var_AddCallback( p_input, "sub-margin", MarginCallback, p_spu->p );
313 vlc_mutex_lock( &p_spu->p->lock );
314 p_spu->p->i_margin = var_GetInteger( p_input, "sub-margin" );
315 vlc_mutex_unlock( &p_spu->p->lock );
317 vlc_object_release( p_input );
321 /* Delete callbacks */
322 var_DelCallback( p_input, "sub-margin", MarginCallback, p_spu->p );
323 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
324 var_Destroy( p_input, "highlight" );
325 vlc_object_release( p_input );
330 * Display a subpicture
332 * Remove the reservation flag of a subpicture, which will cause it to be
334 * \param p_spu the subpicture unit object
335 * \param p_subpic the subpicture to display
337 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
339 spu_private_t *p_sys = p_spu->p;
341 /* DEFAULT_CHAN always reset itself */
342 if( p_subpic->i_channel == DEFAULT_CHAN )
343 SpuClearChannel( p_spu, DEFAULT_CHAN );
345 /* p_private is for spu only and cannot be non NULL here */
346 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
347 assert( r->p_private == NULL );
350 vlc_mutex_lock( &p_sys->lock );
351 if( SpuHeapPush( &p_sys->heap, p_subpic ) )
353 vlc_mutex_unlock( &p_sys->lock );
354 msg_Err( p_spu, "subpicture heap full" );
355 subpicture_Delete( p_subpic );
358 vlc_mutex_unlock( &p_sys->lock );
362 * This function renders all sub picture units in the list.
364 void spu_RenderSubpictures( spu_t *p_spu,
365 picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
366 subpicture_t *p_subpic_list,
367 const video_format_t *p_fmt_src,
368 mtime_t render_subtitle_date )
370 spu_private_t *p_sys = p_spu->p;
372 const mtime_t render_osd_date = mdate();
374 const int i_source_video_width = p_fmt_src->i_width;
375 const int i_source_video_height = p_fmt_src->i_height;
377 unsigned int i_subpicture;
378 subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
380 unsigned int i_subtitle_region_count;
381 spu_area_t p_subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
382 spu_area_t *p_subtitle_area;
385 vlc_mutex_lock( &p_sys->lock );
387 /* Preprocess subpictures */
389 i_subtitle_region_count = 0;
390 for( subpicture_t * p_subpic = p_subpic_list;
392 p_subpic = p_subpic->p_next )
394 SubpictureUpdate( p_subpic,
395 p_fmt_src, p_fmt_dst,
396 p_subpic->b_subtitle ? render_subtitle_date : render_osd_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 p_spu->p->p_blend = filter_NewBlend( VLC_OBJECT(p_spu), p_fmt_dst );
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 p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
477 p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
480 /* Take care of the aspect ratio */
481 if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
482 p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
484 /* FIXME FIXME what about region->i_x/i_y ? */
486 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
487 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
490 /* Render all regions
491 * We always transform non absolute subtitle into absolute one on the
492 * first rendering to allow good subtitle overlap support.
494 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
498 /* Check scale validity */
499 if( scale.w <= 0 || scale.h <= 0 )
503 SpuRenderRegion( p_spu, p_pic_dst, &area,
504 p_subpic, p_region, scale, p_fmt_dst,
505 p_subtitle_area, i_subtitle_area,
506 p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
508 if( p_subpic->b_subtitle )
510 area = spu_area_unscaled( area, scale );
511 if( !p_subpic->b_absolute && area.i_width > 0 && area.i_height > 0 )
513 p_region->i_x = area.i_x;
514 p_region->i_y = area.i_y;
516 if( p_subtitle_area )
517 p_subtitle_area[i_subtitle_area++] = area;
520 if( p_subpic->b_subtitle )
521 p_subpic->b_absolute = true;
525 if( p_subtitle_area != p_subtitle_area_buffer )
526 free( p_subtitle_area );
528 vlc_mutex_unlock( &p_sys->lock );
531 /*****************************************************************************
532 * spu_SortSubpictures: find the subpictures to display
533 *****************************************************************************
534 * This function parses all subpictures and decides which ones need to be
535 * displayed. If no picture has been selected, display_date will depend on
537 * We also check for ephemer DVD subpictures (subpictures that have
538 * to be removed if a newer one is available), which makes it a lot
539 * more difficult to guess if a subpicture has to be rendered or not.
540 *****************************************************************************/
541 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t render_subtitle_date,
542 bool b_subtitle_only )
544 spu_private_t *p_sys = p_spu->p;
546 subpicture_t *p_subpic = NULL;
547 const mtime_t render_osd_date = mdate();
549 /* Update sub-filter chain */
550 vlc_mutex_lock( &p_sys->lock );
551 char *psz_chain_update = p_sys->psz_chain_update;
552 p_sys->psz_chain_update = NULL;
553 vlc_mutex_unlock( &p_sys->lock );
555 if( psz_chain_update )
557 filter_chain_Reset( p_sys->p_chain, NULL, NULL );
559 filter_chain_AppendFromString( p_spu->p->p_chain, psz_chain_update );
561 free( psz_chain_update );
564 /* Run subpicture filters */
565 filter_chain_SubFilter( p_sys->p_chain, render_osd_date );
567 vlc_mutex_lock( &p_sys->lock );
569 /* We get an easily parsable chained list of subpictures which
570 * ends with NULL since p_subpic was initialized to NULL. */
571 for( i_channel = 0; i_channel < p_sys->i_channel; i_channel++ )
573 subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
574 bool pb_available_late[VOUT_MAX_SUBPICTURES];
577 mtime_t start_date = render_subtitle_date;
578 mtime_t ephemer_subtitle_date = 0;
579 mtime_t ephemer_osd_date = 0;
580 int64_t i_ephemer_subtitle_order = INT64_MIN;
581 int64_t i_ephemer_system_order = INT64_MIN;
584 /* Select available pictures */
585 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
587 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
588 subpicture_t *p_current = p_entry->p_subpicture;
592 if( !p_current || p_entry->b_reject )
594 if( p_entry->b_reject )
595 SpuHeapDeleteAt( &p_sys->heap, i_index );
599 if( p_current->i_channel != i_channel ||
600 ( b_subtitle_only && !p_current->b_subtitle ) )
604 const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
606 render_date < p_current->i_start )
608 /* Too early, come back next monday */
612 mtime_t *pi_ephemer_date = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
613 int64_t *pi_ephemer_order = p_current->b_subtitle ? &i_ephemer_subtitle_order : &i_ephemer_system_order;
614 if( p_current->i_start >= *pi_ephemer_date )
616 *pi_ephemer_date = p_current->i_start;
617 if( p_current->i_order > *pi_ephemer_order )
618 *pi_ephemer_order = p_current->i_order;
621 b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
623 b_late = b_stop_valid && p_current->i_stop <= render_date;
625 /* start_date will be used for correct automatic overlap support
626 * in case picture that should not be displayed anymore (display_time)
627 * overlap with a picture to be displayed (p_current->i_start) */
628 if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
629 start_date = p_current->i_start;
632 p_available_subpic[i_available] = p_current;
633 pb_available_late[i_available] = b_late;
637 /* Only forced old picture display at the transition */
638 if( start_date < p_sys->i_last_sort_date )
639 start_date = p_sys->i_last_sort_date;
640 if( start_date <= 0 )
641 start_date = INT64_MAX;
643 /* Select pictures to be displayed */
644 for( i_index = 0; i_index < i_available; i_index++ )
646 subpicture_t *p_current = p_available_subpic[i_index];
647 bool b_late = pb_available_late[i_index];
649 const mtime_t stop_date = p_current->b_subtitle ? __MAX( start_date, p_sys->i_last_sort_date ) : render_osd_date;
650 const mtime_t ephemer_date = p_current->b_subtitle ? ephemer_subtitle_date : ephemer_osd_date;
651 const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
653 /* Destroy late and obsolete ephemer subpictures */
654 bool b_rejet = b_late && p_current->i_stop <= stop_date;
655 if( p_current->b_ephemer )
657 if( p_current->i_start < ephemer_date )
659 else if( p_current->i_start == ephemer_date &&
660 p_current->i_order < i_ephemer_order )
665 SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
667 SubpictureChain( &p_subpic, p_current );
671 p_sys->i_last_sort_date = render_subtitle_date;
672 vlc_mutex_unlock( &p_sys->lock );
677 void spu_OffsetSubtitleDate( spu_t *p_spu, mtime_t i_duration )
679 spu_private_t *p_sys = p_spu->p;
681 vlc_mutex_lock( &p_sys->lock );
682 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
684 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i];
685 subpicture_t *p_current = p_entry->p_subpicture;
687 if( p_current && p_current->b_subtitle )
689 if( p_current->i_start > 0 )
690 p_current->i_start += i_duration;
691 if( p_current->i_stop > 0 )
692 p_current->i_stop += i_duration;
695 vlc_mutex_unlock( &p_sys->lock );
698 /*****************************************************************************
699 * subpicture_t allocation
700 *****************************************************************************/
701 struct subpicture_private_t
707 subpicture_t *subpicture_New( const subpicture_updater_t *p_upd )
709 subpicture_t *p_subpic = calloc( 1, sizeof(*p_subpic) );
713 p_subpic->i_order = 0;
714 p_subpic->b_absolute = true;
715 p_subpic->b_fade = false;
716 p_subpic->b_subtitle = false;
717 p_subpic->i_alpha = 0xFF;
718 p_subpic->p_region = NULL;
722 subpicture_private_t *p_private = malloc( sizeof(*p_private) );
728 video_format_Init( &p_private->src, 0 );
729 video_format_Init( &p_private->dst, 0 );
731 p_subpic->updater = *p_upd;
732 p_subpic->p_private = p_private;
736 p_subpic->p_private = NULL;
738 p_subpic->updater.pf_validate = NULL;
739 p_subpic->updater.pf_update = NULL;
740 p_subpic->updater.pf_destroy = NULL;
741 p_subpic->updater.p_sys = NULL;
746 void subpicture_Delete( subpicture_t *p_subpic )
748 subpicture_region_ChainDelete( p_subpic->p_region );
749 p_subpic->p_region = NULL;
751 if( p_subpic->updater.pf_destroy )
752 p_subpic->updater.pf_destroy( p_subpic );
754 free( p_subpic->p_private );
758 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
760 p_subpic->p_next = *pp_head;
765 subpicture_t *subpicture_NewFromPicture( vlc_object_t *p_obj,
766 picture_t *p_picture, vlc_fourcc_t i_chroma )
769 video_format_t fmt_in = p_picture->format;
772 video_format_t fmt_out;
774 fmt_out.i_chroma = i_chroma;
777 image_handler_t *p_image = image_HandlerCreate( p_obj );
781 picture_t *p_pip = image_Convert( p_image, p_picture, &fmt_in, &fmt_out );
783 image_HandlerDelete( p_image );
788 subpicture_t *p_subpic = subpicture_New( NULL );
791 picture_Release( p_pip );
795 p_subpic->i_original_picture_width = fmt_out.i_width;
796 p_subpic->i_original_picture_height = fmt_out.i_height;
799 fmt_out.i_sar_den = 0;
801 p_subpic->p_region = subpicture_region_New( &fmt_out );
802 if( p_subpic->p_region )
804 picture_Release( p_subpic->p_region->p_picture );
805 p_subpic->p_region->p_picture = p_pip;
809 picture_Release( p_pip );
814 static void SubpictureUpdate( subpicture_t *p_subpicture,
815 const video_format_t *p_fmt_src,
816 const video_format_t *p_fmt_dst,
819 subpicture_updater_t *p_upd = &p_subpicture->updater;
820 subpicture_private_t *p_private = p_subpicture->p_private;
822 if( !p_upd->pf_validate )
824 if( !p_upd->pf_validate( p_subpicture,
825 !video_format_IsSimilar( p_fmt_src,
826 &p_private->src ), p_fmt_src,
827 !video_format_IsSimilar( p_fmt_dst,
828 &p_private->dst ), p_fmt_dst,
832 subpicture_region_ChainDelete( p_subpicture->p_region );
833 p_subpicture->p_region = NULL;
835 p_upd->pf_update( p_subpicture, p_fmt_src, p_fmt_dst, i_ts );
837 video_format_Clean( &p_private->src );
838 video_format_Clean( &p_private->dst );
840 video_format_Copy( &p_private->src, p_fmt_src );
841 video_format_Copy( &p_private->dst, p_fmt_dst );
844 /*****************************************************************************
845 * subpicture_region_t allocation
846 *****************************************************************************/
847 subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
849 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
853 p_region->fmt = *p_fmt;
854 p_region->fmt.p_palette = NULL;
855 if( p_fmt->i_chroma == VLC_CODEC_YUVP )
857 p_region->fmt.p_palette = calloc( 1, sizeof(*p_region->fmt.p_palette) );
858 if( p_fmt->p_palette )
859 *p_region->fmt.p_palette = *p_fmt->p_palette;
861 p_region->i_alpha = 0xff;
862 p_region->p_next = NULL;
863 p_region->p_private = NULL;
864 p_region->psz_text = NULL;
865 p_region->p_style = NULL;
866 p_region->p_picture = NULL;
868 if( p_fmt->i_chroma == VLC_CODEC_TEXT )
871 p_region->p_picture = picture_NewFromFormat( p_fmt );
872 if( !p_region->p_picture )
874 free( p_region->fmt.p_palette );
883 void subpicture_region_Delete( subpicture_region_t *p_region )
888 if( p_region->p_private )
889 SpuRegionPrivateDelete( p_region->p_private );
891 if( p_region->p_picture )
892 picture_Release( p_region->p_picture );
894 free( p_region->fmt.p_palette );
896 free( p_region->psz_text );
897 free( p_region->psz_html );
898 if( p_region->p_style )
899 text_style_Delete( p_region->p_style );
904 void subpicture_region_ChainDelete( subpicture_region_t *p_head )
908 subpicture_region_t *p_next = p_head->p_next;
910 subpicture_region_Delete( p_head );
918 /*****************************************************************************
920 *****************************************************************************/
921 static void SpuHeapInit( spu_heap_t *p_heap )
923 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
925 spu_heap_entry_t *e = &p_heap->p_entry[i];
927 e->p_subpicture = NULL;
932 static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
934 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
936 spu_heap_entry_t *e = &p_heap->p_entry[i];
938 if( e->p_subpicture )
941 e->p_subpicture = p_subpic;
948 static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
950 spu_heap_entry_t *e = &p_heap->p_entry[i_index];
952 if( e->p_subpicture )
953 subpicture_Delete( e->p_subpicture );
955 e->p_subpicture = NULL;
958 static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
960 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
962 spu_heap_entry_t *e = &p_heap->p_entry[i];
964 if( e->p_subpicture != p_subpic )
967 SpuHeapDeleteAt( p_heap, i );
973 static void SpuHeapClean( spu_heap_t *p_heap )
975 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
977 spu_heap_entry_t *e = &p_heap->p_entry[i];
978 if( e->p_subpicture )
979 subpicture_Delete( e->p_subpicture );
983 static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t *p_fmt )
985 subpicture_region_private_t *p_private = malloc( sizeof(*p_private) );
990 p_private->fmt = *p_fmt;
991 if( p_fmt->p_palette )
993 p_private->fmt.p_palette = malloc( sizeof(*p_private->fmt.p_palette) );
994 if( p_private->fmt.p_palette )
995 *p_private->fmt.p_palette = *p_fmt->p_palette;
997 p_private->p_picture = NULL;
1001 static void SpuRegionPrivateDelete( subpicture_region_private_t *p_private )
1003 if( p_private->p_picture )
1004 picture_Release( p_private->p_picture );
1005 free( p_private->fmt.p_palette );
1009 static void FilterRelease( filter_t *p_filter )
1011 if( p_filter->p_module )
1012 module_unneed( p_filter, p_filter->p_module );
1014 vlc_object_release( p_filter );
1017 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
1021 assert( !p_spu->p->p_text );
1024 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
1025 VLC_OBJECT_GENERIC, "spu text" );
1029 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
1031 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
1032 p_text->fmt_out.video.i_width =
1033 p_text->fmt_out.video.i_visible_width = 32;
1034 p_text->fmt_out.video.i_height =
1035 p_text->fmt_out.video.i_visible_height = 32;
1037 p_text->pf_sub_buffer_new = spu_new_buffer;
1038 p_text->pf_sub_buffer_del = spu_del_buffer;
1040 vlc_object_attach( p_text, p_spu );
1042 /* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
1043 * same than these 2 calls ? */
1044 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
1045 if( psz_modulename && *psz_modulename )
1047 p_text->p_module = module_need( p_text, "text renderer",
1048 psz_modulename, true );
1050 free( psz_modulename );
1052 if( !p_text->p_module )
1053 p_text->p_module = module_need( p_text, "text renderer", NULL, false );
1055 /* Create a few variables used for enhanced text rendering */
1056 var_Create( p_text, "spu-duration", VLC_VAR_TIME );
1057 var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
1058 var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
1059 var_Create( p_text, "scale", VLC_VAR_INTEGER );
1062 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
1063 vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
1068 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
1069 VLC_OBJECT_GENERIC, "scale" );
1073 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
1074 p_scale->fmt_in.video.i_chroma = i_src_chroma;
1075 p_scale->fmt_in.video.i_width =
1076 p_scale->fmt_in.video.i_height = 32;
1078 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
1079 p_scale->fmt_out.video.i_chroma = i_dst_chroma;
1080 p_scale->fmt_out.video.i_width =
1081 p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
1083 p_scale->pf_video_buffer_new = spu_new_video_buffer;
1084 p_scale->pf_video_buffer_del = spu_del_video_buffer;
1086 vlc_object_attach( p_scale, p_obj );
1087 p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
1091 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
1093 assert( !p_spu->p->p_scale );
1094 assert( !p_spu->p->p_scale_yuvp );
1095 /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
1097 p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
1098 VLC_CODEC_YUVA, VLC_CODEC_YUVA, true );
1099 /* This one is used for YUVP to YUVA/RGBA without scaling
1100 * FIXME rename it */
1101 p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
1102 VLC_CODEC_YUVP, VLC_CODEC_YUVA, false );
1105 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
1106 subpicture_t *p_subpic, subpicture_region_t *p_region,
1107 int i_min_scale_ratio, mtime_t render_date )
1109 filter_t *p_text = p_spu->p->p_text;
1111 assert( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
1113 if( !p_text || !p_text->p_module )
1116 /* Setup 3 variables which can be used to render
1117 * time-dependent text (and effects). The first indicates
1118 * the total amount of time the text will be on screen,
1119 * the second the amount of time it has already been on
1120 * screen (can be a negative value as text is layed out
1121 * before it is rendered) and the third is a feedback
1122 * variable from the renderer - if the renderer sets it
1123 * then this particular text is time-dependent, eg. the
1124 * visual progress bar inside the text in karaoke and the
1125 * text needs to be rendered multiple times in order for
1126 * the effect to work - we therefore need to return the
1127 * region to its original state at the end of the loop,
1128 * instead of leaving it in YUVA or YUVP.
1129 * Any renderer which is unaware of how to render
1130 * time-dependent text can happily ignore the variables
1131 * and render the text the same as usual - it should at
1132 * least show up on screen, but the effect won't change
1133 * the text over time.
1135 var_SetTime( p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
1136 var_SetTime( p_text, "spu-elapsed", render_date );
1137 var_SetBool( p_text, "text-rerender", false );
1138 var_SetInteger( p_text, "scale", i_min_scale_ratio );
1140 if( p_text->pf_render_html && p_region->psz_html )
1142 p_text->pf_render_html( p_text, p_region, p_region );
1144 else if( p_text->pf_render_text )
1146 p_text->pf_render_text( p_text, p_region, p_region );
1148 *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
1151 p_region->i_align |= SUBPICTURE_RENDERED;
1155 * A few scale functions helpers.
1157 static spu_scale_t spu_scale_create( int w, int h )
1159 spu_scale_t s = { .w = w, .h = h };
1166 static spu_scale_t spu_scale_unit( void )
1168 return spu_scale_create( SCALE_UNIT, SCALE_UNIT );
1170 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
1172 return spu_scale_create( wn * SCALE_UNIT / wd,
1173 hn * SCALE_UNIT / hd );
1175 static int spu_scale_w( int v, const spu_scale_t s )
1177 return v * s.w / SCALE_UNIT;
1179 static int spu_scale_h( int v, const spu_scale_t s )
1181 return v * s.h / SCALE_UNIT;
1183 static int spu_invscale_w( int v, const spu_scale_t s )
1185 return v * SCALE_UNIT / s.w;
1187 static int spu_invscale_h( int v, const spu_scale_t s )
1189 return v * SCALE_UNIT / s.h;
1193 * A few area functions helpers
1195 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t s )
1197 spu_area_t a = { .i_x = x, .i_y = y, .i_width = w, .i_height = h, .scale = s };
1200 static spu_area_t spu_area_scaled( spu_area_t a )
1202 if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
1205 a.i_x = spu_scale_w( a.i_x, a.scale );
1206 a.i_y = spu_scale_h( a.i_y, a.scale );
1208 a.i_width = spu_scale_w( a.i_width, a.scale );
1209 a.i_height = spu_scale_h( a.i_height, a.scale );
1211 a.scale = spu_scale_unit();
1214 static spu_area_t spu_area_unscaled( spu_area_t a, spu_scale_t s )
1216 if( a.scale.w == s.w && a.scale.h == s.h )
1219 a = spu_area_scaled( a );
1221 a.i_x = spu_invscale_w( a.i_x, s );
1222 a.i_y = spu_invscale_h( a.i_y, s );
1224 a.i_width = spu_invscale_w( a.i_width, s );
1225 a.i_height = spu_invscale_h( a.i_height, s );
1230 static bool spu_area_overlap( spu_area_t a, spu_area_t b )
1235 a = spu_area_scaled( a );
1236 b = spu_area_scaled( b );
1238 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 ) &&
1239 __MAX( a.i_y-i_dy, b.i_y ) < __MIN( a.i_y+a.i_height+i_dy, b.i_y+b.i_height );
1243 * Avoid area overlapping
1245 static void SpuAreaFixOverlap( spu_area_t *p_dst,
1246 const spu_area_t *p_sub, int i_sub, int i_align )
1248 spu_area_t a = spu_area_scaled( *p_dst );
1249 bool b_moved = false;
1252 /* Check for overlap
1253 * XXX It is not fast O(n^2) but we should not have a lot of region */
1257 for( int i = 0; i < i_sub; i++ )
1259 spu_area_t sub = spu_area_scaled( p_sub[i] );
1261 if( !spu_area_overlap( a, sub ) )
1264 if( i_align & SUBPICTURE_ALIGN_TOP )
1267 int i_y = sub.i_y + sub.i_height;
1271 else if( i_align & SUBPICTURE_ALIGN_BOTTOM )
1274 int i_y = sub.i_y - a.i_height;
1280 /* TODO what to do in this case? */
1281 //fprintf( stderr, "Overlap with unsupported alignment\n" );
1291 *p_dst = spu_area_unscaled( a, p_dst->scale );
1295 static void SpuAreaFitInside( spu_area_t *p_area, const spu_area_t *p_boundary )
1297 spu_area_t a = spu_area_scaled( *p_area );
1299 const int i_error_x = (a.i_x + a.i_width) - p_boundary->i_width;
1305 const int i_error_y = (a.i_y + a.i_height) - p_boundary->i_height;
1311 *p_area = spu_area_unscaled( a, p_area->scale );
1317 static void SpuRegionPlace( int *pi_x, int *pi_y,
1318 const subpicture_t *p_subpic,
1319 const subpicture_region_t *p_region )
1321 const int i_delta_x = p_region->i_x;
1322 const int i_delta_y = p_region->i_y;
1325 assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
1326 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
1330 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
1332 i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
1336 i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
1339 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
1343 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
1345 i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
1349 i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
1352 if( p_subpic->b_absolute )
1358 /* Margin shifts all subpictures */
1359 /* NOTE We have margin only for subtitles, so we don't really need this here
1360 if( i_margin_y != 0 )
1361 i_y -= i_margin_y;*/
1363 /* Clamp offset to not go out of the screen (when possible) */
1364 /* NOTE Again, useful only for subtitles, otherwise goes against the alignment logic above
1365 const int i_error_x = (i_x + p_region->fmt.i_width) - p_subpic->i_original_picture_width;
1371 const int i_error_y = (i_y + p_region->fmt.i_height) - p_subpic->i_original_picture_height;
1382 * This function computes the current alpha value for a given region.
1384 static int SpuRegionAlpha( subpicture_t *p_subpic, subpicture_region_t *p_region )
1386 /* Compute alpha blend value */
1387 int i_fade_alpha = 255;
1388 if( p_subpic->b_fade )
1390 mtime_t i_fade_start = ( p_subpic->i_stop +
1391 p_subpic->i_start ) / 2;
1392 mtime_t i_now = mdate();
1394 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
1396 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
1397 ( p_subpic->i_stop - i_fade_start );
1400 return i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
1404 * It will render the provided region onto p_pic_dst.
1407 static void SpuRenderRegion( spu_t *p_spu,
1408 picture_t *p_pic_dst, spu_area_t *p_area,
1409 subpicture_t *p_subpic, subpicture_region_t *p_region,
1410 const spu_scale_t scale_size,
1411 const video_format_t *p_fmt,
1412 const spu_area_t *p_subtitle_area, int i_subtitle_area,
1413 mtime_t render_date )
1415 spu_private_t *p_sys = p_spu->p;
1417 video_format_t fmt_original = p_region->fmt;
1418 bool b_rerender_text = false;
1419 bool b_restore_format = false;
1423 video_format_t region_fmt;
1424 picture_t *p_region_picture;
1426 /* Invalidate area by default */
1427 *p_area = spu_area_create( 0,0, 0,0, scale_size );
1429 /* Render text region */
1430 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1432 const int i_min_scale_ratio = SCALE_UNIT; /* FIXME what is the right value? (scale_size is not) */
1433 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region,
1434 i_min_scale_ratio, render_date );
1435 b_restore_format = b_rerender_text;
1437 /* Check if the rendering has failed ... */
1438 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1442 /* Force palette if requested
1443 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
1444 * instead of only the right one (being the dvd spu).
1446 const bool b_using_palette = p_region->fmt.i_chroma == VLC_CODEC_YUVP;
1447 const bool b_force_palette = b_using_palette && p_sys->b_force_palette;
1448 const bool b_force_crop = b_force_palette && p_sys->b_force_crop;
1449 bool b_changed_palette = false;
1452 /* Compute the margin which is expressed in destination pixel unit
1453 * The margin is applied only to subtitle and when no forced crop is
1454 * requested (dvd menu) */
1456 if( !b_force_crop && p_subpic->b_subtitle )
1457 i_margin_y = spu_invscale_h( p_sys->i_margin, scale_size );
1459 /* Place the picture
1460 * We compute the position in the rendered size */
1461 SpuRegionPlace( &i_x_offset, &i_y_offset,
1462 p_subpic, p_region );
1464 /* Save this position for subtitle overlap support
1465 * it is really important that there are given without scale_size applied */
1466 *p_area = spu_area_create( i_x_offset, i_y_offset,
1467 p_region->fmt.i_width, p_region->fmt.i_height,
1470 /* Handle overlapping subtitles when possible */
1471 if( p_subpic->b_subtitle && !p_subpic->b_absolute )
1473 SpuAreaFixOverlap( p_area, p_subtitle_area, i_subtitle_area,
1474 p_region->i_align );
1477 /* we copy the area: for the subtitle overlap support we want
1478 * to only save the area without margin applied */
1479 spu_area_t restrained = *p_area;
1481 /* apply margin to subtitles and correct if they go over the picture edge */
1482 if( p_subpic->b_subtitle )
1484 restrained.i_y -= i_margin_y;
1485 spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
1487 SpuAreaFitInside( &restrained, &display );
1490 /* Fix the position for the current scale_size */
1491 i_x_offset = spu_scale_w( restrained.i_x, restrained.scale );
1492 i_y_offset = spu_scale_h( restrained.i_y, restrained.scale );
1495 if( b_force_palette )
1497 video_palette_t *p_palette = p_region->fmt.p_palette;
1498 video_palette_t palette;
1500 /* We suppose DVD palette here */
1501 palette.i_entries = 4;
1502 for( int i = 0; i < 4; i++ )
1503 for( int j = 0; j < 4; j++ )
1504 palette.palette[i][j] = p_sys->palette[i][j];
1506 if( p_palette->i_entries == palette.i_entries )
1508 for( int i = 0; i < p_palette->i_entries; i++ )
1509 for( int j = 0; j < 4; j++ )
1510 b_changed_palette |= p_palette->palette[i][j] != palette.palette[i][j];
1514 b_changed_palette = true;
1516 *p_palette = palette;
1520 region_fmt = p_region->fmt;
1521 p_region_picture = p_region->p_picture;
1524 /* Scale from rendered size to destination size */
1525 if( p_sys->p_scale && p_sys->p_scale->p_module &&
1526 ( !b_using_palette || ( p_sys->p_scale_yuvp && p_sys->p_scale_yuvp->p_module ) ) &&
1527 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_using_palette ) )
1529 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
1530 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
1532 /* Destroy the cache if unusable */
1533 if( p_region->p_private )
1535 subpicture_region_private_t *p_private = p_region->p_private;
1536 bool b_changed = false;
1538 /* Check resize changes */
1539 if( i_dst_width != p_private->fmt.i_width ||
1540 i_dst_height != p_private->fmt.i_height )
1543 /* Check forced palette changes */
1544 if( b_changed_palette )
1549 SpuRegionPrivateDelete( p_private );
1550 p_region->p_private = NULL;
1554 /* Scale if needed into cache */
1555 if( !p_region->p_private && i_dst_width > 0 && i_dst_height > 0 )
1557 filter_t *p_scale = p_sys->p_scale;
1559 picture_t *p_picture = p_region->p_picture;
1560 picture_Hold( p_picture );
1562 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
1563 if( b_using_palette )
1565 filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
1567 p_scale_yuvp->fmt_in.video = p_region->fmt;
1569 /* TODO converting to RGBA for RGB video output is better */
1570 p_scale_yuvp->fmt_out.video = p_region->fmt;
1571 p_scale_yuvp->fmt_out.video.i_chroma = VLC_CODEC_YUVA;
1573 p_picture = p_scale_yuvp->pf_video_filter( p_scale_yuvp, p_picture );
1576 /* Well we will try conversion+scaling */
1577 msg_Warn( p_spu, "%4.4s to %4.4s conversion failed",
1578 (const char*)&p_scale_yuvp->fmt_in.video.i_chroma,
1579 (const char*)&p_scale_yuvp->fmt_out.video.i_chroma );
1583 /* Conversion(except from YUVP)/Scaling */
1585 ( p_picture->format.i_width != i_dst_width ||
1586 p_picture->format.i_height != i_dst_height ) )
1588 p_scale->fmt_in.video = p_picture->format;
1589 p_scale->fmt_out.video = p_picture->format;
1591 p_scale->fmt_out.video.i_width = i_dst_width;
1592 p_scale->fmt_out.video.i_height = i_dst_height;
1594 p_scale->fmt_out.video.i_visible_width =
1595 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
1596 p_scale->fmt_out.video.i_visible_height =
1597 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
1599 p_picture = p_scale->pf_video_filter( p_scale, p_picture );
1601 msg_Err( p_spu, "scaling failed" );
1607 p_region->p_private = SpuRegionPrivateNew( &p_picture->format );
1608 if( p_region->p_private )
1610 p_region->p_private->p_picture = p_picture;
1611 if( !p_region->p_private->p_picture )
1613 SpuRegionPrivateDelete( p_region->p_private );
1614 p_region->p_private = NULL;
1619 picture_Release( p_picture );
1624 /* And use the scaled picture */
1625 if( p_region->p_private )
1627 region_fmt = p_region->p_private->fmt;
1628 p_region_picture = p_region->p_private->p_picture;
1632 /* Force cropping if requested */
1635 int i_crop_x = spu_scale_w( p_sys->i_crop_x, scale_size );
1636 int i_crop_y = spu_scale_h( p_sys->i_crop_y, scale_size );
1637 int i_crop_width = spu_scale_w( p_sys->i_crop_width, scale_size );
1638 int i_crop_height= spu_scale_h( p_sys->i_crop_height,scale_size );
1640 /* Find the intersection */
1641 if( i_crop_x + i_crop_width <= i_x_offset ||
1642 i_x_offset + (int)region_fmt.i_visible_width < i_crop_x ||
1643 i_crop_y + i_crop_height <= i_y_offset ||
1644 i_y_offset + (int)region_fmt.i_visible_height < i_crop_y )
1646 /* No intersection */
1647 region_fmt.i_visible_width =
1648 region_fmt.i_visible_height = 0;
1652 int i_x, i_y, i_x_end, i_y_end;
1653 i_x = __MAX( i_crop_x, i_x_offset );
1654 i_y = __MAX( i_crop_y, i_y_offset );
1655 i_x_end = __MIN( i_crop_x + i_crop_width,
1656 i_x_offset + (int)region_fmt.i_visible_width );
1657 i_y_end = __MIN( i_crop_y + i_crop_height,
1658 i_y_offset + (int)region_fmt.i_visible_height );
1660 region_fmt.i_x_offset = i_x - i_x_offset;
1661 region_fmt.i_y_offset = i_y - i_y_offset;
1662 region_fmt.i_visible_width = i_x_end - i_x;
1663 region_fmt.i_visible_height = i_y_end - i_y;
1665 i_x_offset = __MAX( i_x, 0 );
1666 i_y_offset = __MAX( i_y, 0 );
1670 /* Update the blender */
1671 if( filter_ConfigureBlend( p_spu->p->p_blend,
1672 p_fmt->i_width, p_fmt->i_height,
1674 filter_Blend( p_spu->p->p_blend,
1675 p_pic_dst, i_x_offset, i_y_offset,
1676 p_region_picture, SpuRegionAlpha( p_subpic, p_region ) ) )
1678 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1679 (char *)&p_sys->p_blend->fmt_in.video.i_chroma,
1680 (char *)&p_sys->p_blend->fmt_out.video.i_chroma );
1684 if( b_rerender_text )
1686 /* Some forms of subtitles need to be re-rendered more than
1687 * once, eg. karaoke. We therefore restore the region to its
1688 * pre-rendered state, so the next time through everything is
1691 if( p_region->p_picture )
1693 picture_Release( p_region->p_picture );
1694 p_region->p_picture = NULL;
1696 if( p_region->p_private )
1698 SpuRegionPrivateDelete( p_region->p_private );
1699 p_region->p_private = NULL;
1701 p_region->i_align &= ~SUBPICTURE_RENDERED;
1703 if( b_restore_format )
1704 p_region->fmt = fmt_original;
1708 * This function compares two 64 bits integers.
1709 * It can be used by qsort.
1711 static int IntegerCmp( int64_t i0, int64_t i1 )
1713 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
1716 * This function compares 2 subpictures using the following properties
1717 * (ordered by priority)
1718 * 1. absolute positionning
1720 * 3. creation order (per channel)
1722 * It can be used by qsort.
1724 * XXX spu_RenderSubpictures depends heavily on this order.
1726 static int SubpictureCmp( const void *s0, const void *s1 )
1728 subpicture_t *p_subpic0 = *(subpicture_t**)s0;
1729 subpicture_t *p_subpic1 = *(subpicture_t**)s1;
1732 r = IntegerCmp( !p_subpic0->b_absolute, !p_subpic1->b_absolute );
1734 r = IntegerCmp( p_subpic0->i_start, p_subpic1->i_start );
1736 r = IntegerCmp( p_subpic0->i_channel, p_subpic1->i_channel );
1738 r = IntegerCmp( p_subpic0->i_order, p_subpic1->i_order );
1742 /*****************************************************************************
1743 * SpuClearChannel: clear an spu channel
1744 *****************************************************************************
1745 * This function destroys the subpictures which belong to the spu channel
1746 * corresponding to i_channel_id.
1747 *****************************************************************************/
1748 static void SpuClearChannel( spu_t *p_spu, int i_channel )
1750 spu_private_t *p_sys = p_spu->p;
1751 int i_subpic; /* subpicture index */
1753 vlc_mutex_lock( &p_sys->lock );
1755 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
1757 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_subpic];
1758 subpicture_t *p_subpic = p_entry->p_subpicture;
1762 if( p_subpic->i_channel != i_channel && ( i_channel != -1 || p_subpic->i_channel == DEFAULT_CHAN ) )
1765 /* You cannot delete subpicture outside of spu_SortSubpictures */
1766 p_entry->b_reject = true;
1769 vlc_mutex_unlock( &p_sys->lock );
1772 /*****************************************************************************
1773 * spu_ControlDefault: default methods for the subpicture unit control.
1774 *****************************************************************************/
1775 static int SpuControl( spu_t *p_spu, int i_query, va_list args )
1777 spu_private_t *p_sys = p_spu->p;
1782 case SPU_CHANNEL_REGISTER:
1783 pi = (int *)va_arg( args, int * );
1784 vlc_mutex_lock( &p_sys->lock );
1786 *pi = p_sys->i_channel++;
1787 vlc_mutex_unlock( &p_sys->lock );
1790 case SPU_CHANNEL_CLEAR:
1791 i = (int)va_arg( args, int );
1792 SpuClearChannel( p_spu, i );
1796 msg_Dbg( p_spu, "control query not supported" );
1797 return VLC_EGENERIC;
1803 /*****************************************************************************
1804 * Object variables callbacks
1805 *****************************************************************************/
1807 /*****************************************************************************
1808 * UpdateSPU: update subpicture settings
1809 *****************************************************************************
1810 * This function is called from CropCallback and at initialization time, to
1811 * retrieve crop information from the input.
1812 *****************************************************************************/
1813 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1815 spu_private_t *p_sys = p_spu->p;
1818 vlc_mutex_lock( &p_sys->lock );
1820 p_sys->b_force_palette = false;
1821 p_sys->b_force_crop = false;
1823 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1825 vlc_mutex_unlock( &p_sys->lock );
1829 p_sys->b_force_crop = true;
1830 p_sys->i_crop_x = var_GetInteger( p_object, "x-start" );
1831 p_sys->i_crop_y = var_GetInteger( p_object, "y-start" );
1832 p_sys->i_crop_width = var_GetInteger( p_object, "x-end" ) - p_sys->i_crop_x;
1833 p_sys->i_crop_height = var_GetInteger( p_object, "y-end" ) - p_sys->i_crop_y;
1835 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1837 memcpy( p_sys->palette, val.p_address, 16 );
1838 p_sys->b_force_palette = true;
1840 vlc_mutex_unlock( &p_sys->lock );
1842 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1843 p_sys->i_crop_x, p_sys->i_crop_y,
1844 p_sys->i_crop_width, p_sys->i_crop_height,
1845 p_sys->b_force_palette );
1848 /*****************************************************************************
1849 * CropCallback: called when the highlight properties are changed
1850 *****************************************************************************
1851 * This callback is called from the input thread when we need cropping
1852 *****************************************************************************/
1853 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1854 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1856 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1858 UpdateSPU( (spu_t *)p_data, p_object );
1862 /*****************************************************************************
1863 * MarginCallback: called when requested subtitle position has changed *
1864 *****************************************************************************/
1866 static int MarginCallback( vlc_object_t *p_object, char const *psz_var,
1867 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1869 VLC_UNUSED( psz_var ); VLC_UNUSED( oldval ); VLC_UNUSED( p_object );
1870 spu_private_t *p_sys = ( spu_private_t* ) p_data;
1872 vlc_mutex_lock( &p_sys->lock );
1873 p_sys->i_margin = newval.i_int;
1874 vlc_mutex_unlock( &p_sys->lock );
1878 /*****************************************************************************
1879 * Buffers allocation callbacks for the filters
1880 *****************************************************************************/
1881 struct filter_owner_sys_t
1887 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1889 filter_owner_sys_t *p_sys = p_filter->p_owner;
1891 subpicture_t *p_subpicture = subpicture_New( NULL );
1893 p_subpicture->i_channel = p_sys->i_channel;
1894 return p_subpicture;
1896 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1898 VLC_UNUSED( p_filter );
1899 subpicture_Delete( p_subpic );
1902 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1904 VLC_UNUSED(p_filter);
1905 return subpicture_New( NULL );
1907 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1909 VLC_UNUSED(p_filter);
1910 subpicture_Delete( p_subpic );
1913 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1915 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1917 VLC_UNUSED(p_filter);
1918 return picture_NewFromFormat( p_fmt );
1920 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1922 VLC_UNUSED(p_filter);
1923 picture_Release( p_picture );
1926 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1928 spu_t *p_spu = p_data;
1930 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1932 return VLC_EGENERIC;
1934 p_filter->pf_sub_buffer_new = sub_new_buffer;
1935 p_filter->pf_sub_buffer_del = sub_del_buffer;
1937 p_filter->p_owner = p_sys;
1938 spu_Control( p_spu, SPU_CHANNEL_REGISTER, &p_sys->i_channel );
1939 p_sys->p_spu = p_spu;
1944 static void SubFilterAllocationClean( filter_t *p_filter )
1946 filter_owner_sys_t *p_sys = p_filter->p_owner;
1948 SpuClearChannel( p_sys->p_spu, p_sys->i_channel );
1949 free( p_filter->p_owner );
1952 static int SubFilterCallback( vlc_object_t *p_object, char const *psz_var,
1953 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1955 spu_t *p_spu = p_data;
1956 spu_private_t *p_sys = p_spu->p;
1958 VLC_UNUSED(p_object); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
1960 vlc_mutex_lock( &p_sys->lock );
1962 free( p_sys->psz_chain_update );
1963 p_sys->psz_chain_update = strdup( newval.psz_string );
1965 vlc_mutex_unlock( &p_sys->lock );