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 vlc_mutex_t chain_lock;
92 filter_chain_t *p_chain;
95 mtime_t i_last_sort_date;
99 struct subpicture_region_private_t
102 picture_t *p_picture;
104 static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t * );
105 static void SpuRegionPrivateDelete( subpicture_region_private_t * );
113 static spu_scale_t spu_scale_create( int w, int h );
114 static spu_scale_t spu_scale_unit(void );
115 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd );
116 static int spu_scale_w( int v, const spu_scale_t s );
117 static int spu_scale_h( int v, const spu_scale_t s );
118 static int spu_invscale_w( int v, const spu_scale_t s );
119 static int spu_invscale_h( int v, const spu_scale_t s );
131 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t );
132 static spu_area_t spu_area_scaled( spu_area_t );
133 static spu_area_t spu_area_unscaled( spu_area_t, spu_scale_t );
134 static bool spu_area_overlap( spu_area_t, spu_area_t );
137 /* Subpicture rendered flag
138 * FIXME ? it could be moved to private ? */
139 #define SUBPICTURE_RENDERED (0x1000)
140 #if SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
141 # error SUBPICTURE_RENDERED too low
144 #define SCALE_UNIT (1000)
146 static void SubpictureUpdate( subpicture_t *,
147 const video_format_t *p_fmt_src,
148 const video_format_t *p_fmt_dst,
150 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic );
151 static int SubpictureCmp( const void *s0, const void *s1 );
153 static void SpuRenderRegion( spu_t *,
154 picture_t *p_pic_dst, spu_area_t *,
155 subpicture_t *, subpicture_region_t *,
156 const spu_scale_t scale_size,
157 const video_format_t *p_fmt,
158 const spu_area_t *p_subtitle_area, int i_subtitle_area,
159 mtime_t render_date );
161 static void UpdateSPU ( spu_t *, vlc_object_t * );
162 static int CropCallback( vlc_object_t *, char const *,
163 vlc_value_t, vlc_value_t, void * );
164 static int MarginCallback( vlc_object_t *, char const *,
165 vlc_value_t, vlc_value_t, void * );
167 /* Buffer allocation for SPU filter (blend, scale, ...) */
168 static subpicture_t *spu_new_buffer( filter_t * );
169 static void spu_del_buffer( filter_t *, subpicture_t * );
170 static picture_t *spu_new_video_buffer( filter_t * );
171 static void spu_del_video_buffer( filter_t *, picture_t * );
173 /* Buffer aloccation fir SUB filter */
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 *****************************************************************************/
188 * Creates the subpicture unit
190 * \param p_this the parent object which creates the subpicture unit
192 spu_t *spu_Create( vlc_object_t *p_this )
195 spu_private_t *p_sys;
197 p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
198 VLC_OBJECT_GENERIC, "subpicture" );
201 vlc_object_attach( p_spu, p_this );
203 /* Initialize spu fields */
204 p_spu->p = p_sys = (spu_private_t*)&p_spu[1];
206 /* Initialize private fields */
207 vlc_mutex_init( &p_sys->lock );
209 SpuHeapInit( &p_sys->heap );
211 p_sys->p_blend = NULL;
212 p_sys->p_text = NULL;
213 p_sys->p_scale = NULL;
214 p_sys->p_scale_yuvp = NULL;
216 p_sys->i_margin = var_InheritInteger( p_spu, "sub-margin" );
218 /* Register the default subpicture channel */
219 p_sys->i_channel = SPU_DEFAULT_CHANNEL + 1;
221 p_sys->psz_chain_update = NULL;
222 vlc_mutex_init( &p_sys->chain_lock );
223 p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
224 SubFilterAllocationInit,
225 SubFilterAllocationClean,
228 /* Load text and scale module */
229 SpuRenderCreateAndLoadText( p_spu );
230 SpuRenderCreateAndLoadScale( p_spu );
233 p_sys->i_last_sort_date = -1;
239 * Destroy the subpicture unit
241 * \param p_this the parent object which destroys the subpicture unit
243 void spu_Destroy( spu_t *p_spu )
245 spu_private_t *p_sys = p_spu->p;
248 filter_DeleteBlend( p_sys->p_blend );
251 FilterRelease( p_sys->p_text );
253 if( p_sys->p_scale_yuvp )
254 FilterRelease( p_sys->p_scale_yuvp );
257 FilterRelease( p_sys->p_scale );
259 filter_chain_Delete( p_sys->p_chain );
260 vlc_mutex_destroy( &p_sys->chain_lock );
261 free( p_sys->psz_chain_update );
263 /* Destroy all remaining subpictures */
264 SpuHeapClean( &p_sys->heap );
266 vlc_mutex_destroy( &p_sys->lock );
268 vlc_object_release( p_spu );
272 * Attach/Detach the SPU from any input
274 * \param p_this the object in which to destroy the subpicture unit
275 * \param b_attach to select attach or detach
277 void spu_Attach( spu_t *p_spu, vlc_object_t *p_input, bool b_attach )
281 UpdateSPU( p_spu, VLC_OBJECT(p_input) );
282 var_Create( p_input, "highlight", VLC_VAR_BOOL );
283 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
284 var_AddCallback( p_input, "sub-margin", MarginCallback, p_spu->p );
286 vlc_mutex_lock( &p_spu->p->lock );
287 p_spu->p->i_margin = var_GetInteger( p_input, "sub-margin" );
288 vlc_mutex_unlock( &p_spu->p->lock );
292 /* Delete callbacks */
293 var_DelCallback( p_input, "sub-margin", MarginCallback, p_spu->p );
294 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
295 var_Destroy( p_input, "highlight" );
300 * Inform the SPU filters of mouse event
302 int spu_ProcessMouse( spu_t *p_spu,
303 const vlc_mouse_t *p_mouse,
304 const video_format_t *p_fmt )
306 spu_private_t *p_sys = p_spu->p;
308 vlc_mutex_lock( &p_sys->chain_lock );
309 filter_chain_MouseEvent( p_sys->p_chain, p_mouse, p_fmt );
310 vlc_mutex_unlock( &p_sys->chain_lock );
316 * Display a subpicture
318 * Remove the reservation flag of a subpicture, which will cause it to be
320 * \param p_spu the subpicture unit object
321 * \param p_subpic the subpicture to display
323 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
325 spu_private_t *p_sys = p_spu->p;
327 /* SPU_DEFAULT_CHANNEL always reset itself */
328 if( p_subpic->i_channel == SPU_DEFAULT_CHANNEL )
329 spu_ClearChannel( p_spu, SPU_DEFAULT_CHANNEL );
331 /* p_private is for spu only and cannot be non NULL here */
332 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
333 assert( r->p_private == NULL );
336 vlc_mutex_lock( &p_sys->lock );
337 if( SpuHeapPush( &p_sys->heap, p_subpic ) )
339 vlc_mutex_unlock( &p_sys->lock );
340 msg_Err( p_spu, "subpicture heap full" );
341 subpicture_Delete( p_subpic );
344 vlc_mutex_unlock( &p_sys->lock );
348 * This function renders all sub picture units in the list.
350 void spu_RenderSubpictures( spu_t *p_spu,
351 picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
352 subpicture_t *p_subpic_list,
353 const video_format_t *p_fmt_src,
354 mtime_t render_subtitle_date )
356 spu_private_t *p_sys = p_spu->p;
358 const mtime_t render_osd_date = mdate();
360 const int i_source_video_width = p_fmt_src->i_width;
361 const int i_source_video_height = p_fmt_src->i_height;
363 unsigned int i_subpicture;
364 subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
366 unsigned int i_subtitle_region_count;
367 spu_area_t p_subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
368 spu_area_t *p_subtitle_area;
371 vlc_mutex_lock( &p_sys->lock );
373 /* Preprocess subpictures */
375 i_subtitle_region_count = 0;
376 for( subpicture_t * p_subpic = p_subpic_list;
378 p_subpic = p_subpic->p_next )
380 SubpictureUpdate( p_subpic,
381 p_fmt_src, p_fmt_dst,
382 p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
385 if( p_subpic->b_subtitle )
387 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
388 i_subtitle_region_count++;
392 pp_subpicture[i_subpicture++] = p_subpic;
395 /* Be sure we have at least 1 picture to process */
396 if( i_subpicture <= 0 )
398 vlc_mutex_unlock( &p_sys->lock );
402 /* Now order subpicture array
403 * XXX The order is *really* important for overlap subtitles positionning */
404 qsort( pp_subpicture, i_subpicture, sizeof(*pp_subpicture), SubpictureCmp );
406 /* Allocate area array for subtitle overlap */
408 p_subtitle_area = p_subtitle_area_buffer;
409 if( i_subtitle_region_count > sizeof(p_subtitle_area_buffer)/sizeof(*p_subtitle_area_buffer) )
410 p_subtitle_area = calloc( i_subtitle_region_count, sizeof(*p_subtitle_area) );
412 /* Create the blending module */
413 if( !p_sys->p_blend )
414 p_spu->p->p_blend = filter_NewBlend( VLC_OBJECT(p_spu), p_fmt_dst );
416 /* Process all subpictures and regions (in the right order) */
417 for( unsigned int i_index = 0; i_index < i_subpicture; i_index++ )
419 subpicture_t *p_subpic = pp_subpicture[i_index];
420 subpicture_region_t *p_region;
422 if( !p_subpic->p_region )
425 /* FIXME when possible use a better rendering size than source size
426 * (max of display size and source size for example) FIXME */
427 int i_render_width = p_subpic->i_original_picture_width;
428 int i_render_height = p_subpic->i_original_picture_height;
429 if( !i_render_width || !i_render_height )
431 if( i_render_width != 0 || i_render_height != 0 )
432 msg_Err( p_spu, "unsupported original picture size %dx%d",
433 i_render_width, i_render_height );
435 p_subpic->i_original_picture_width = i_render_width = i_source_video_width;
436 p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
441 p_sys->p_text->fmt_out.video.i_width =
442 p_sys->p_text->fmt_out.video.i_visible_width = i_render_width;
444 p_sys->p_text->fmt_out.video.i_height =
445 p_sys->p_text->fmt_out.video.i_visible_height = i_render_height;
448 /* Compute scaling from picture to source size */
449 spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
450 i_source_video_height, i_render_height );
452 /* Update scaling from source size to display size(p_fmt_dst) */
453 scale.w = scale.w * p_fmt_dst->i_width / i_source_video_width;
454 scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
456 /* Set default subpicture aspect ratio
457 * FIXME if we only handle 1 aspect ratio per picture, why is it set per
459 p_region = p_subpic->p_region;
460 if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
462 p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
463 p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
466 /* Take care of the aspect ratio */
467 if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
468 p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
470 /* FIXME FIXME what about region->i_x/i_y ? */
472 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
473 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
476 /* Render all regions
477 * We always transform non absolute subtitle into absolute one on the
478 * first rendering to allow good subtitle overlap support.
480 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
484 /* Check scale validity */
485 if( scale.w <= 0 || scale.h <= 0 )
489 SpuRenderRegion( p_spu, p_pic_dst, &area,
490 p_subpic, p_region, scale, p_fmt_dst,
491 p_subtitle_area, i_subtitle_area,
492 p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
494 if( p_subpic->b_subtitle )
496 area = spu_area_unscaled( area, scale );
497 if( !p_subpic->b_absolute && area.i_width > 0 && area.i_height > 0 )
499 p_region->i_x = area.i_x;
500 p_region->i_y = area.i_y;
502 if( p_subtitle_area )
503 p_subtitle_area[i_subtitle_area++] = area;
506 if( p_subpic->b_subtitle )
507 p_subpic->b_absolute = true;
511 if( p_subtitle_area != p_subtitle_area_buffer )
512 free( p_subtitle_area );
514 vlc_mutex_unlock( &p_sys->lock );
517 /*****************************************************************************
518 * spu_SortSubpictures: find the subpictures to display
519 *****************************************************************************
520 * This function parses all subpictures and decides which ones need to be
521 * displayed. If no picture has been selected, display_date will depend on
523 * We also check for ephemer DVD subpictures (subpictures that have
524 * to be removed if a newer one is available), which makes it a lot
525 * more difficult to guess if a subpicture has to be rendered or not.
526 *****************************************************************************/
527 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t render_subtitle_date,
528 bool b_subtitle_only )
530 spu_private_t *p_sys = p_spu->p;
531 subpicture_t *p_subpic = NULL;
532 const mtime_t render_osd_date = mdate();
534 /* Update sub-filter chain */
535 vlc_mutex_lock( &p_sys->lock );
536 char *psz_chain_update = p_sys->psz_chain_update;
537 p_sys->psz_chain_update = NULL;
538 vlc_mutex_unlock( &p_sys->lock );
540 vlc_mutex_lock( &p_sys->chain_lock );
541 if( psz_chain_update )
543 filter_chain_Reset( p_sys->p_chain, NULL, NULL );
545 filter_chain_AppendFromString( p_spu->p->p_chain, psz_chain_update );
547 free( psz_chain_update );
549 /* Run subpicture filters */
550 filter_chain_SubFilter( p_sys->p_chain, render_osd_date );
551 vlc_mutex_unlock( &p_sys->chain_lock );
553 vlc_mutex_lock( &p_sys->lock );
555 /* Create a list of channels */
556 int pi_channel[VOUT_MAX_SUBPICTURES];
557 int i_channel_count = 0;
559 for( int i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
561 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
562 if( !p_entry->p_subpicture || p_entry->b_reject )
564 const int i_channel = p_entry->p_subpicture->i_channel;
566 for( i = 0; i < i_channel_count; i++ )
568 if( pi_channel[i] == i_channel )
571 if( i_channel_count <= i )
572 pi_channel[i_channel_count++] = i_channel;
575 /* We get an easily parsable chained list of subpictures which
576 * ends with NULL since p_subpic was initialized to NULL. */
577 for( int i = 0; i < i_channel_count; i++ )
579 const int i_channel = pi_channel[i];
580 subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
581 bool pb_available_late[VOUT_MAX_SUBPICTURES];
584 mtime_t start_date = render_subtitle_date;
585 mtime_t ephemer_subtitle_date = 0;
586 mtime_t ephemer_osd_date = 0;
587 int64_t i_ephemer_subtitle_order = INT64_MIN;
588 int64_t i_ephemer_system_order = INT64_MIN;
591 /* Select available pictures */
592 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
594 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
595 subpicture_t *p_current = p_entry->p_subpicture;
599 if( !p_current || p_entry->b_reject )
601 if( p_entry->b_reject )
602 SpuHeapDeleteAt( &p_sys->heap, i_index );
606 if( p_current->i_channel != i_channel ||
607 ( b_subtitle_only && !p_current->b_subtitle ) )
611 const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
613 render_date < p_current->i_start )
615 /* Too early, come back next monday */
619 mtime_t *pi_ephemer_date = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
620 int64_t *pi_ephemer_order = p_current->b_subtitle ? &i_ephemer_subtitle_order : &i_ephemer_system_order;
621 if( p_current->i_start >= *pi_ephemer_date )
623 *pi_ephemer_date = p_current->i_start;
624 if( p_current->i_order > *pi_ephemer_order )
625 *pi_ephemer_order = p_current->i_order;
628 b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
630 b_late = b_stop_valid && p_current->i_stop <= render_date;
632 /* start_date will be used for correct automatic overlap support
633 * in case picture that should not be displayed anymore (display_time)
634 * overlap with a picture to be displayed (p_current->i_start) */
635 if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
636 start_date = p_current->i_start;
639 p_available_subpic[i_available] = p_current;
640 pb_available_late[i_available] = b_late;
644 /* Only forced old picture display at the transition */
645 if( start_date < p_sys->i_last_sort_date )
646 start_date = p_sys->i_last_sort_date;
647 if( start_date <= 0 )
648 start_date = INT64_MAX;
650 /* Select pictures to be displayed */
651 for( i_index = 0; i_index < i_available; i_index++ )
653 subpicture_t *p_current = p_available_subpic[i_index];
654 bool b_late = pb_available_late[i_index];
656 const mtime_t stop_date = p_current->b_subtitle ? __MAX( start_date, p_sys->i_last_sort_date ) : render_osd_date;
657 const mtime_t ephemer_date = p_current->b_subtitle ? ephemer_subtitle_date : ephemer_osd_date;
658 const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
660 /* Destroy late and obsolete ephemer subpictures */
661 bool b_rejet = b_late && p_current->i_stop <= stop_date;
662 if( p_current->b_ephemer )
664 if( p_current->i_start < ephemer_date )
666 else if( p_current->i_start == ephemer_date &&
667 p_current->i_order < i_ephemer_order )
672 SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
674 SubpictureChain( &p_subpic, p_current );
678 p_sys->i_last_sort_date = render_subtitle_date;
679 vlc_mutex_unlock( &p_sys->lock );
684 void spu_OffsetSubtitleDate( spu_t *p_spu, mtime_t i_duration )
686 spu_private_t *p_sys = p_spu->p;
688 vlc_mutex_lock( &p_sys->lock );
689 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
691 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i];
692 subpicture_t *p_current = p_entry->p_subpicture;
694 if( p_current && p_current->b_subtitle )
696 if( p_current->i_start > 0 )
697 p_current->i_start += i_duration;
698 if( p_current->i_stop > 0 )
699 p_current->i_stop += i_duration;
702 vlc_mutex_unlock( &p_sys->lock );
705 int spu_RegisterChannel( spu_t *p_spu )
707 spu_private_t *p_sys = p_spu->p;
709 vlc_mutex_lock( &p_sys->lock );
710 int i_channel = p_sys->i_channel++;
711 vlc_mutex_unlock( &p_sys->lock );
716 void spu_ClearChannel( spu_t *p_spu, int i_channel )
718 spu_private_t *p_sys = p_spu->p;
720 vlc_mutex_lock( &p_sys->lock );
722 for( int i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
724 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_subpic];
725 subpicture_t *p_subpic = p_entry->p_subpicture;
729 if( p_subpic->i_channel != i_channel && ( i_channel != -1 || p_subpic->i_channel == SPU_DEFAULT_CHANNEL ) )
732 /* You cannot delete subpicture outside of spu_SortSubpictures */
733 p_entry->b_reject = true;
736 vlc_mutex_unlock( &p_sys->lock );
739 void spu_ChangeFilters( spu_t *p_spu, const char *psz_filters )
741 spu_private_t *p_sys = p_spu->p;
743 vlc_mutex_lock( &p_sys->lock );
745 free( p_sys->psz_chain_update );
746 p_sys->psz_chain_update = strdup( psz_filters );
748 vlc_mutex_unlock( &p_sys->lock );
751 /*****************************************************************************
752 * subpicture_t allocation
753 *****************************************************************************/
754 struct subpicture_private_t
760 subpicture_t *subpicture_New( const subpicture_updater_t *p_upd )
762 subpicture_t *p_subpic = calloc( 1, sizeof(*p_subpic) );
766 p_subpic->i_order = 0;
767 p_subpic->b_absolute = true;
768 p_subpic->b_fade = false;
769 p_subpic->b_subtitle = false;
770 p_subpic->i_alpha = 0xFF;
771 p_subpic->p_region = NULL;
775 subpicture_private_t *p_private = malloc( sizeof(*p_private) );
781 video_format_Init( &p_private->src, 0 );
782 video_format_Init( &p_private->dst, 0 );
784 p_subpic->updater = *p_upd;
785 p_subpic->p_private = p_private;
789 p_subpic->p_private = NULL;
791 p_subpic->updater.pf_validate = NULL;
792 p_subpic->updater.pf_update = NULL;
793 p_subpic->updater.pf_destroy = NULL;
794 p_subpic->updater.p_sys = NULL;
799 void subpicture_Delete( subpicture_t *p_subpic )
801 subpicture_region_ChainDelete( p_subpic->p_region );
802 p_subpic->p_region = NULL;
804 if( p_subpic->updater.pf_destroy )
805 p_subpic->updater.pf_destroy( p_subpic );
807 free( p_subpic->p_private );
811 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
813 p_subpic->p_next = *pp_head;
818 subpicture_t *subpicture_NewFromPicture( vlc_object_t *p_obj,
819 picture_t *p_picture, vlc_fourcc_t i_chroma )
822 video_format_t fmt_in = p_picture->format;
825 video_format_t fmt_out;
827 fmt_out.i_chroma = i_chroma;
830 image_handler_t *p_image = image_HandlerCreate( p_obj );
834 picture_t *p_pip = image_Convert( p_image, p_picture, &fmt_in, &fmt_out );
836 image_HandlerDelete( p_image );
841 subpicture_t *p_subpic = subpicture_New( NULL );
844 picture_Release( p_pip );
848 p_subpic->i_original_picture_width = fmt_out.i_width;
849 p_subpic->i_original_picture_height = fmt_out.i_height;
852 fmt_out.i_sar_den = 0;
854 p_subpic->p_region = subpicture_region_New( &fmt_out );
855 if( p_subpic->p_region )
857 picture_Release( p_subpic->p_region->p_picture );
858 p_subpic->p_region->p_picture = p_pip;
862 picture_Release( p_pip );
867 static void SubpictureUpdate( subpicture_t *p_subpicture,
868 const video_format_t *p_fmt_src,
869 const video_format_t *p_fmt_dst,
872 subpicture_updater_t *p_upd = &p_subpicture->updater;
873 subpicture_private_t *p_private = p_subpicture->p_private;
875 if( !p_upd->pf_validate )
877 if( !p_upd->pf_validate( p_subpicture,
878 !video_format_IsSimilar( p_fmt_src,
879 &p_private->src ), p_fmt_src,
880 !video_format_IsSimilar( p_fmt_dst,
881 &p_private->dst ), p_fmt_dst,
885 subpicture_region_ChainDelete( p_subpicture->p_region );
886 p_subpicture->p_region = NULL;
888 p_upd->pf_update( p_subpicture, p_fmt_src, p_fmt_dst, i_ts );
890 video_format_Clean( &p_private->src );
891 video_format_Clean( &p_private->dst );
893 video_format_Copy( &p_private->src, p_fmt_src );
894 video_format_Copy( &p_private->dst, p_fmt_dst );
897 /*****************************************************************************
898 * subpicture_region_t allocation
899 *****************************************************************************/
900 subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
902 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
906 p_region->fmt = *p_fmt;
907 p_region->fmt.p_palette = NULL;
908 if( p_fmt->i_chroma == VLC_CODEC_YUVP )
910 p_region->fmt.p_palette = calloc( 1, sizeof(*p_region->fmt.p_palette) );
911 if( p_fmt->p_palette )
912 *p_region->fmt.p_palette = *p_fmt->p_palette;
914 p_region->i_alpha = 0xff;
915 p_region->p_next = NULL;
916 p_region->p_private = NULL;
917 p_region->psz_text = NULL;
918 p_region->p_style = NULL;
919 p_region->p_picture = NULL;
921 if( p_fmt->i_chroma == VLC_CODEC_TEXT )
924 p_region->p_picture = picture_NewFromFormat( p_fmt );
925 if( !p_region->p_picture )
927 free( p_region->fmt.p_palette );
936 void subpicture_region_Delete( subpicture_region_t *p_region )
941 if( p_region->p_private )
942 SpuRegionPrivateDelete( p_region->p_private );
944 if( p_region->p_picture )
945 picture_Release( p_region->p_picture );
947 free( p_region->fmt.p_palette );
949 free( p_region->psz_text );
950 free( p_region->psz_html );
951 if( p_region->p_style )
952 text_style_Delete( p_region->p_style );
957 void subpicture_region_ChainDelete( subpicture_region_t *p_head )
961 subpicture_region_t *p_next = p_head->p_next;
963 subpicture_region_Delete( p_head );
971 /*****************************************************************************
973 *****************************************************************************/
974 static void SpuHeapInit( spu_heap_t *p_heap )
976 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
978 spu_heap_entry_t *e = &p_heap->p_entry[i];
980 e->p_subpicture = NULL;
985 static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
987 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
989 spu_heap_entry_t *e = &p_heap->p_entry[i];
991 if( e->p_subpicture )
994 e->p_subpicture = p_subpic;
1001 static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
1003 spu_heap_entry_t *e = &p_heap->p_entry[i_index];
1005 if( e->p_subpicture )
1006 subpicture_Delete( e->p_subpicture );
1008 e->p_subpicture = NULL;
1011 static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
1013 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
1015 spu_heap_entry_t *e = &p_heap->p_entry[i];
1017 if( e->p_subpicture != p_subpic )
1020 SpuHeapDeleteAt( p_heap, i );
1023 return VLC_EGENERIC;
1026 static void SpuHeapClean( spu_heap_t *p_heap )
1028 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
1030 spu_heap_entry_t *e = &p_heap->p_entry[i];
1031 if( e->p_subpicture )
1032 subpicture_Delete( e->p_subpicture );
1036 static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t *p_fmt )
1038 subpicture_region_private_t *p_private = malloc( sizeof(*p_private) );
1043 p_private->fmt = *p_fmt;
1044 if( p_fmt->p_palette )
1046 p_private->fmt.p_palette = malloc( sizeof(*p_private->fmt.p_palette) );
1047 if( p_private->fmt.p_palette )
1048 *p_private->fmt.p_palette = *p_fmt->p_palette;
1050 p_private->p_picture = NULL;
1054 static void SpuRegionPrivateDelete( subpicture_region_private_t *p_private )
1056 if( p_private->p_picture )
1057 picture_Release( p_private->p_picture );
1058 free( p_private->fmt.p_palette );
1062 static void FilterRelease( filter_t *p_filter )
1064 if( p_filter->p_module )
1065 module_unneed( p_filter, p_filter->p_module );
1067 vlc_object_release( p_filter );
1070 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
1074 assert( !p_spu->p->p_text );
1077 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
1078 VLC_OBJECT_GENERIC, "spu text" );
1082 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
1084 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
1085 p_text->fmt_out.video.i_width =
1086 p_text->fmt_out.video.i_visible_width = 32;
1087 p_text->fmt_out.video.i_height =
1088 p_text->fmt_out.video.i_visible_height = 32;
1090 p_text->pf_sub_buffer_new = spu_new_buffer;
1091 p_text->pf_sub_buffer_del = spu_del_buffer;
1093 vlc_object_attach( p_text, p_spu );
1095 /* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
1096 * same than these 2 calls ? */
1097 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
1098 if( psz_modulename && *psz_modulename )
1100 p_text->p_module = module_need( p_text, "text renderer",
1101 psz_modulename, true );
1103 free( psz_modulename );
1105 if( !p_text->p_module )
1106 p_text->p_module = module_need( p_text, "text renderer", NULL, false );
1108 /* Create a few variables used for enhanced text rendering */
1109 var_Create( p_text, "spu-duration", VLC_VAR_TIME );
1110 var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
1111 var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
1112 var_Create( p_text, "scale", VLC_VAR_INTEGER );
1115 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
1116 vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
1121 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
1122 VLC_OBJECT_GENERIC, "scale" );
1126 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
1127 p_scale->fmt_in.video.i_chroma = i_src_chroma;
1128 p_scale->fmt_in.video.i_width =
1129 p_scale->fmt_in.video.i_height = 32;
1131 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
1132 p_scale->fmt_out.video.i_chroma = i_dst_chroma;
1133 p_scale->fmt_out.video.i_width =
1134 p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
1136 p_scale->pf_video_buffer_new = spu_new_video_buffer;
1137 p_scale->pf_video_buffer_del = spu_del_video_buffer;
1139 vlc_object_attach( p_scale, p_obj );
1140 p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
1144 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
1146 assert( !p_spu->p->p_scale );
1147 assert( !p_spu->p->p_scale_yuvp );
1148 /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
1150 p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
1151 VLC_CODEC_YUVA, VLC_CODEC_YUVA, true );
1152 /* This one is used for YUVP to YUVA/RGBA without scaling
1153 * FIXME rename it */
1154 p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
1155 VLC_CODEC_YUVP, VLC_CODEC_YUVA, false );
1158 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
1159 subpicture_t *p_subpic, subpicture_region_t *p_region,
1160 int i_min_scale_ratio, mtime_t render_date )
1162 filter_t *p_text = p_spu->p->p_text;
1164 assert( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
1166 if( !p_text || !p_text->p_module )
1169 /* Setup 3 variables which can be used to render
1170 * time-dependent text (and effects). The first indicates
1171 * the total amount of time the text will be on screen,
1172 * the second the amount of time it has already been on
1173 * screen (can be a negative value as text is layed out
1174 * before it is rendered) and the third is a feedback
1175 * variable from the renderer - if the renderer sets it
1176 * then this particular text is time-dependent, eg. the
1177 * visual progress bar inside the text in karaoke and the
1178 * text needs to be rendered multiple times in order for
1179 * the effect to work - we therefore need to return the
1180 * region to its original state at the end of the loop,
1181 * instead of leaving it in YUVA or YUVP.
1182 * Any renderer which is unaware of how to render
1183 * time-dependent text can happily ignore the variables
1184 * and render the text the same as usual - it should at
1185 * least show up on screen, but the effect won't change
1186 * the text over time.
1188 var_SetTime( p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
1189 var_SetTime( p_text, "spu-elapsed", render_date );
1190 var_SetBool( p_text, "text-rerender", false );
1191 var_SetInteger( p_text, "scale", i_min_scale_ratio );
1193 if( p_text->pf_render_html && p_region->psz_html )
1195 p_text->pf_render_html( p_text, p_region, p_region );
1197 else if( p_text->pf_render_text )
1199 p_text->pf_render_text( p_text, p_region, p_region );
1201 *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
1204 p_region->i_align |= SUBPICTURE_RENDERED;
1208 * A few scale functions helpers.
1210 static spu_scale_t spu_scale_create( int w, int h )
1212 spu_scale_t s = { .w = w, .h = h };
1219 static spu_scale_t spu_scale_unit( void )
1221 return spu_scale_create( SCALE_UNIT, SCALE_UNIT );
1223 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
1225 return spu_scale_create( wn * SCALE_UNIT / wd,
1226 hn * SCALE_UNIT / hd );
1228 static int spu_scale_w( int v, const spu_scale_t s )
1230 return v * s.w / SCALE_UNIT;
1232 static int spu_scale_h( int v, const spu_scale_t s )
1234 return v * s.h / SCALE_UNIT;
1236 static int spu_invscale_w( int v, const spu_scale_t s )
1238 return v * SCALE_UNIT / s.w;
1240 static int spu_invscale_h( int v, const spu_scale_t s )
1242 return v * SCALE_UNIT / s.h;
1246 * A few area functions helpers
1248 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t s )
1250 spu_area_t a = { .i_x = x, .i_y = y, .i_width = w, .i_height = h, .scale = s };
1253 static spu_area_t spu_area_scaled( spu_area_t a )
1255 if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
1258 a.i_x = spu_scale_w( a.i_x, a.scale );
1259 a.i_y = spu_scale_h( a.i_y, a.scale );
1261 a.i_width = spu_scale_w( a.i_width, a.scale );
1262 a.i_height = spu_scale_h( a.i_height, a.scale );
1264 a.scale = spu_scale_unit();
1267 static spu_area_t spu_area_unscaled( spu_area_t a, spu_scale_t s )
1269 if( a.scale.w == s.w && a.scale.h == s.h )
1272 a = spu_area_scaled( a );
1274 a.i_x = spu_invscale_w( a.i_x, s );
1275 a.i_y = spu_invscale_h( a.i_y, s );
1277 a.i_width = spu_invscale_w( a.i_width, s );
1278 a.i_height = spu_invscale_h( a.i_height, s );
1283 static bool spu_area_overlap( spu_area_t a, spu_area_t b )
1288 a = spu_area_scaled( a );
1289 b = spu_area_scaled( b );
1291 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 ) &&
1292 __MAX( a.i_y-i_dy, b.i_y ) < __MIN( a.i_y+a.i_height+i_dy, b.i_y+b.i_height );
1296 * Avoid area overlapping
1298 static void SpuAreaFixOverlap( spu_area_t *p_dst,
1299 const spu_area_t *p_sub, int i_sub, int i_align )
1301 spu_area_t a = spu_area_scaled( *p_dst );
1302 bool b_moved = false;
1305 /* Check for overlap
1306 * XXX It is not fast O(n^2) but we should not have a lot of region */
1310 for( int i = 0; i < i_sub; i++ )
1312 spu_area_t sub = spu_area_scaled( p_sub[i] );
1314 if( !spu_area_overlap( a, sub ) )
1317 if( i_align & SUBPICTURE_ALIGN_TOP )
1320 int i_y = sub.i_y + sub.i_height;
1324 else if( i_align & SUBPICTURE_ALIGN_BOTTOM )
1327 int i_y = sub.i_y - a.i_height;
1333 /* TODO what to do in this case? */
1334 //fprintf( stderr, "Overlap with unsupported alignment\n" );
1344 *p_dst = spu_area_unscaled( a, p_dst->scale );
1348 static void SpuAreaFitInside( spu_area_t *p_area, const spu_area_t *p_boundary )
1350 spu_area_t a = spu_area_scaled( *p_area );
1352 const int i_error_x = (a.i_x + a.i_width) - p_boundary->i_width;
1358 const int i_error_y = (a.i_y + a.i_height) - p_boundary->i_height;
1364 *p_area = spu_area_unscaled( a, p_area->scale );
1370 static void SpuRegionPlace( int *pi_x, int *pi_y,
1371 const subpicture_t *p_subpic,
1372 const subpicture_region_t *p_region )
1374 const int i_delta_x = p_region->i_x;
1375 const int i_delta_y = p_region->i_y;
1378 assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
1379 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
1383 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
1385 i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
1389 i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
1392 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
1396 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
1398 i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
1402 i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
1405 if( p_subpic->b_absolute )
1416 * This function computes the current alpha value for a given region.
1418 static int SpuRegionAlpha( subpicture_t *p_subpic, subpicture_region_t *p_region )
1420 /* Compute alpha blend value */
1421 int i_fade_alpha = 255;
1422 if( p_subpic->b_fade )
1424 mtime_t i_fade_start = ( p_subpic->i_stop +
1425 p_subpic->i_start ) / 2;
1426 mtime_t i_now = mdate();
1428 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
1430 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
1431 ( p_subpic->i_stop - i_fade_start );
1434 return i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
1438 * It will render the provided region onto p_pic_dst.
1441 static void SpuRenderRegion( spu_t *p_spu,
1442 picture_t *p_pic_dst, spu_area_t *p_area,
1443 subpicture_t *p_subpic, subpicture_region_t *p_region,
1444 const spu_scale_t scale_size,
1445 const video_format_t *p_fmt,
1446 const spu_area_t *p_subtitle_area, int i_subtitle_area,
1447 mtime_t render_date )
1449 spu_private_t *p_sys = p_spu->p;
1451 video_format_t fmt_original = p_region->fmt;
1452 bool b_rerender_text = false;
1453 bool b_restore_format = false;
1457 video_format_t region_fmt;
1458 picture_t *p_region_picture;
1460 /* Invalidate area by default */
1461 *p_area = spu_area_create( 0,0, 0,0, scale_size );
1463 /* Render text region */
1464 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1466 const int i_min_scale_ratio = SCALE_UNIT; /* FIXME what is the right value? (scale_size is not) */
1467 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region,
1468 i_min_scale_ratio, render_date );
1469 b_restore_format = b_rerender_text;
1471 /* Check if the rendering has failed ... */
1472 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1476 /* Force palette if requested
1477 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
1478 * instead of only the right one (being the dvd spu).
1480 const bool b_using_palette = p_region->fmt.i_chroma == VLC_CODEC_YUVP;
1481 const bool b_force_palette = b_using_palette && p_sys->b_force_palette;
1482 const bool b_force_crop = b_force_palette && p_sys->b_force_crop;
1483 bool b_changed_palette = false;
1486 /* Compute the margin which is expressed in destination pixel unit
1487 * The margin is applied only to subtitle and when no forced crop is
1488 * requested (dvd menu) */
1490 if( !b_force_crop && p_subpic->b_subtitle )
1491 i_margin_y = spu_invscale_h( p_sys->i_margin, scale_size );
1493 /* Place the picture
1494 * We compute the position in the rendered size */
1495 SpuRegionPlace( &i_x_offset, &i_y_offset,
1496 p_subpic, p_region );
1498 /* Save this position for subtitle overlap support
1499 * it is really important that there are given without scale_size applied */
1500 *p_area = spu_area_create( i_x_offset, i_y_offset,
1501 p_region->fmt.i_width, p_region->fmt.i_height,
1504 /* Handle overlapping subtitles when possible */
1505 if( p_subpic->b_subtitle && !p_subpic->b_absolute )
1507 SpuAreaFixOverlap( p_area, p_subtitle_area, i_subtitle_area,
1508 p_region->i_align );
1511 /* we copy the area: for the subtitle overlap support we want
1512 * to only save the area without margin applied */
1513 spu_area_t restrained = *p_area;
1515 /* apply margin to subtitles and correct if they go over the picture edge */
1516 if( p_subpic->b_subtitle )
1517 restrained.i_y -= i_margin_y;
1519 spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
1521 SpuAreaFitInside( &restrained, &display );
1523 /* Fix the position for the current scale_size */
1524 i_x_offset = spu_scale_w( restrained.i_x, restrained.scale );
1525 i_y_offset = spu_scale_h( restrained.i_y, restrained.scale );
1528 if( b_force_palette )
1530 video_palette_t *p_palette = p_region->fmt.p_palette;
1531 video_palette_t palette;
1533 /* We suppose DVD palette here */
1534 palette.i_entries = 4;
1535 for( int i = 0; i < 4; i++ )
1536 for( int j = 0; j < 4; j++ )
1537 palette.palette[i][j] = p_sys->palette[i][j];
1539 if( p_palette->i_entries == palette.i_entries )
1541 for( int i = 0; i < p_palette->i_entries; i++ )
1542 for( int j = 0; j < 4; j++ )
1543 b_changed_palette |= p_palette->palette[i][j] != palette.palette[i][j];
1547 b_changed_palette = true;
1549 *p_palette = palette;
1553 region_fmt = p_region->fmt;
1554 p_region_picture = p_region->p_picture;
1557 /* Scale from rendered size to destination size */
1558 if( p_sys->p_scale && p_sys->p_scale->p_module &&
1559 ( !b_using_palette || ( p_sys->p_scale_yuvp && p_sys->p_scale_yuvp->p_module ) ) &&
1560 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_using_palette ) )
1562 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
1563 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
1565 /* Destroy the cache if unusable */
1566 if( p_region->p_private )
1568 subpicture_region_private_t *p_private = p_region->p_private;
1569 bool b_changed = false;
1571 /* Check resize changes */
1572 if( i_dst_width != p_private->fmt.i_width ||
1573 i_dst_height != p_private->fmt.i_height )
1576 /* Check forced palette changes */
1577 if( b_changed_palette )
1582 SpuRegionPrivateDelete( p_private );
1583 p_region->p_private = NULL;
1587 /* Scale if needed into cache */
1588 if( !p_region->p_private && i_dst_width > 0 && i_dst_height > 0 )
1590 filter_t *p_scale = p_sys->p_scale;
1592 picture_t *p_picture = p_region->p_picture;
1593 picture_Hold( p_picture );
1595 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
1596 if( b_using_palette )
1598 filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
1600 p_scale_yuvp->fmt_in.video = p_region->fmt;
1602 /* TODO converting to RGBA for RGB video output is better */
1603 p_scale_yuvp->fmt_out.video = p_region->fmt;
1604 p_scale_yuvp->fmt_out.video.i_chroma = VLC_CODEC_YUVA;
1606 p_picture = p_scale_yuvp->pf_video_filter( p_scale_yuvp, p_picture );
1609 /* Well we will try conversion+scaling */
1610 msg_Warn( p_spu, "%4.4s to %4.4s conversion failed",
1611 (const char*)&p_scale_yuvp->fmt_in.video.i_chroma,
1612 (const char*)&p_scale_yuvp->fmt_out.video.i_chroma );
1616 /* Conversion(except from YUVP)/Scaling */
1618 ( p_picture->format.i_width != i_dst_width ||
1619 p_picture->format.i_height != i_dst_height ) )
1621 p_scale->fmt_in.video = p_picture->format;
1622 p_scale->fmt_out.video = p_picture->format;
1624 p_scale->fmt_out.video.i_width = i_dst_width;
1625 p_scale->fmt_out.video.i_height = i_dst_height;
1627 p_scale->fmt_out.video.i_visible_width =
1628 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
1629 p_scale->fmt_out.video.i_visible_height =
1630 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
1632 p_picture = p_scale->pf_video_filter( p_scale, p_picture );
1634 msg_Err( p_spu, "scaling failed" );
1640 p_region->p_private = SpuRegionPrivateNew( &p_picture->format );
1641 if( p_region->p_private )
1643 p_region->p_private->p_picture = p_picture;
1644 if( !p_region->p_private->p_picture )
1646 SpuRegionPrivateDelete( p_region->p_private );
1647 p_region->p_private = NULL;
1652 picture_Release( p_picture );
1657 /* And use the scaled picture */
1658 if( p_region->p_private )
1660 region_fmt = p_region->p_private->fmt;
1661 p_region_picture = p_region->p_private->p_picture;
1665 /* Force cropping if requested */
1668 int i_crop_x = spu_scale_w( p_sys->i_crop_x, scale_size );
1669 int i_crop_y = spu_scale_h( p_sys->i_crop_y, scale_size );
1670 int i_crop_width = spu_scale_w( p_sys->i_crop_width, scale_size );
1671 int i_crop_height= spu_scale_h( p_sys->i_crop_height,scale_size );
1673 /* Find the intersection */
1674 if( i_crop_x + i_crop_width <= i_x_offset ||
1675 i_x_offset + (int)region_fmt.i_visible_width < i_crop_x ||
1676 i_crop_y + i_crop_height <= i_y_offset ||
1677 i_y_offset + (int)region_fmt.i_visible_height < i_crop_y )
1679 /* No intersection */
1680 region_fmt.i_visible_width =
1681 region_fmt.i_visible_height = 0;
1685 int i_x, i_y, i_x_end, i_y_end;
1686 i_x = __MAX( i_crop_x, i_x_offset );
1687 i_y = __MAX( i_crop_y, i_y_offset );
1688 i_x_end = __MIN( i_crop_x + i_crop_width,
1689 i_x_offset + (int)region_fmt.i_visible_width );
1690 i_y_end = __MIN( i_crop_y + i_crop_height,
1691 i_y_offset + (int)region_fmt.i_visible_height );
1693 region_fmt.i_x_offset = i_x - i_x_offset;
1694 region_fmt.i_y_offset = i_y - i_y_offset;
1695 region_fmt.i_visible_width = i_x_end - i_x;
1696 region_fmt.i_visible_height = i_y_end - i_y;
1698 i_x_offset = __MAX( i_x, 0 );
1699 i_y_offset = __MAX( i_y, 0 );
1703 /* Update the blender */
1704 if( filter_ConfigureBlend( p_spu->p->p_blend,
1705 p_fmt->i_width, p_fmt->i_height,
1707 filter_Blend( p_spu->p->p_blend,
1708 p_pic_dst, i_x_offset, i_y_offset,
1709 p_region_picture, SpuRegionAlpha( p_subpic, p_region ) ) )
1711 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1712 (char *)&p_sys->p_blend->fmt_in.video.i_chroma,
1713 (char *)&p_sys->p_blend->fmt_out.video.i_chroma );
1717 if( b_rerender_text )
1719 /* Some forms of subtitles need to be re-rendered more than
1720 * once, eg. karaoke. We therefore restore the region to its
1721 * pre-rendered state, so the next time through everything is
1724 if( p_region->p_picture )
1726 picture_Release( p_region->p_picture );
1727 p_region->p_picture = NULL;
1729 if( p_region->p_private )
1731 SpuRegionPrivateDelete( p_region->p_private );
1732 p_region->p_private = NULL;
1734 p_region->i_align &= ~SUBPICTURE_RENDERED;
1736 if( b_restore_format )
1737 p_region->fmt = fmt_original;
1741 * This function compares two 64 bits integers.
1742 * It can be used by qsort.
1744 static int IntegerCmp( int64_t i0, int64_t i1 )
1746 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
1749 * This function compares 2 subpictures using the following properties
1750 * (ordered by priority)
1751 * 1. absolute positionning
1753 * 3. creation order (per channel)
1755 * It can be used by qsort.
1757 * XXX spu_RenderSubpictures depends heavily on this order.
1759 static int SubpictureCmp( const void *s0, const void *s1 )
1761 subpicture_t *p_subpic0 = *(subpicture_t**)s0;
1762 subpicture_t *p_subpic1 = *(subpicture_t**)s1;
1765 r = IntegerCmp( !p_subpic0->b_absolute, !p_subpic1->b_absolute );
1767 r = IntegerCmp( p_subpic0->i_start, p_subpic1->i_start );
1769 r = IntegerCmp( p_subpic0->i_channel, p_subpic1->i_channel );
1771 r = IntegerCmp( p_subpic0->i_order, p_subpic1->i_order );
1775 /*****************************************************************************
1776 * Object variables callbacks
1777 *****************************************************************************/
1779 /*****************************************************************************
1780 * UpdateSPU: update subpicture settings
1781 *****************************************************************************
1782 * This function is called from CropCallback and at initialization time, to
1783 * retrieve crop information from the input.
1784 *****************************************************************************/
1785 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1787 spu_private_t *p_sys = p_spu->p;
1790 vlc_mutex_lock( &p_sys->lock );
1792 p_sys->b_force_palette = false;
1793 p_sys->b_force_crop = false;
1795 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1797 vlc_mutex_unlock( &p_sys->lock );
1801 p_sys->b_force_crop = true;
1802 p_sys->i_crop_x = var_GetInteger( p_object, "x-start" );
1803 p_sys->i_crop_y = var_GetInteger( p_object, "y-start" );
1804 p_sys->i_crop_width = var_GetInteger( p_object, "x-end" ) - p_sys->i_crop_x;
1805 p_sys->i_crop_height = var_GetInteger( p_object, "y-end" ) - p_sys->i_crop_y;
1807 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1809 memcpy( p_sys->palette, val.p_address, 16 );
1810 p_sys->b_force_palette = true;
1812 vlc_mutex_unlock( &p_sys->lock );
1814 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1815 p_sys->i_crop_x, p_sys->i_crop_y,
1816 p_sys->i_crop_width, p_sys->i_crop_height,
1817 p_sys->b_force_palette );
1820 /*****************************************************************************
1821 * CropCallback: called when the highlight properties are changed
1822 *****************************************************************************
1823 * This callback is called from the input thread when we need cropping
1824 *****************************************************************************/
1825 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1826 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1828 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1830 UpdateSPU( (spu_t *)p_data, p_object );
1834 /*****************************************************************************
1835 * MarginCallback: called when requested subtitle position has changed *
1836 *****************************************************************************/
1838 static int MarginCallback( vlc_object_t *p_object, char const *psz_var,
1839 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1841 VLC_UNUSED( psz_var ); VLC_UNUSED( oldval ); VLC_UNUSED( p_object );
1842 spu_private_t *p_sys = ( spu_private_t* ) p_data;
1844 vlc_mutex_lock( &p_sys->lock );
1845 p_sys->i_margin = newval.i_int;
1846 vlc_mutex_unlock( &p_sys->lock );
1850 /*****************************************************************************
1851 * Buffers allocation callbacks for the filters
1852 *****************************************************************************/
1853 struct filter_owner_sys_t
1859 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1861 filter_owner_sys_t *p_sys = p_filter->p_owner;
1863 subpicture_t *p_subpicture = subpicture_New( NULL );
1865 p_subpicture->i_channel = p_sys->i_channel;
1866 return p_subpicture;
1868 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1870 VLC_UNUSED( p_filter );
1871 subpicture_Delete( p_subpic );
1874 static subpicture_t *spu_new_buffer( filter_t *p_filter )
1876 VLC_UNUSED(p_filter);
1877 return subpicture_New( NULL );
1879 static void spu_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1881 VLC_UNUSED(p_filter);
1882 subpicture_Delete( p_subpic );
1885 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1887 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1889 VLC_UNUSED(p_filter);
1890 return picture_NewFromFormat( p_fmt );
1892 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1894 VLC_UNUSED(p_filter);
1895 picture_Release( p_picture );
1898 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1900 spu_t *p_spu = p_data;
1902 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1904 return VLC_EGENERIC;
1906 p_filter->pf_sub_buffer_new = sub_new_buffer;
1907 p_filter->pf_sub_buffer_del = sub_del_buffer;
1909 p_filter->p_owner = p_sys;
1910 p_sys->i_channel = spu_RegisterChannel( p_spu );
1911 p_sys->p_spu = p_spu;
1916 static void SubFilterAllocationClean( filter_t *p_filter )
1918 filter_owner_sys_t *p_sys = p_filter->p_owner;
1920 spu_ClearChannel( p_sys->p_spu, p_sys->i_channel );
1921 free( p_filter->p_owner );