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>
44 #include <vlc_input.h>
45 #include <vlc_modules.h>
46 #include "../misc/subpicture.h"
48 /*****************************************************************************
50 *****************************************************************************/
52 /* Number of simultaneous subpictures */
53 #define VOUT_MAX_SUBPICTURES (__MAX(VOUT_MAX_PICTURES, SPU_MAX_PREPARE_TIME/5000))
58 subpicture_t *p_subpicture;
64 spu_heap_entry_t p_entry[VOUT_MAX_SUBPICTURES];
68 static void SpuHeapInit( spu_heap_t * );
69 static int SpuHeapPush( spu_heap_t *, subpicture_t * );
70 static void SpuHeapDeleteAt( spu_heap_t *, int i_index );
71 static int SpuHeapDeleteSubpicture( spu_heap_t *, subpicture_t * );
72 static void SpuHeapClean( spu_heap_t *p_heap );
76 vlc_mutex_t lock; /* lock to protect all followings fields */
77 vlc_object_t *p_input;
81 int i_channel; /**< number of subpicture channels registered */
82 filter_t *p_blend; /**< alpha blending module */
83 filter_t *p_text; /**< text renderer module */
84 filter_t *p_scale_yuvp; /**< scaling module for YUVP */
85 filter_t *p_scale; /**< scaling module (all but YUVP) */
86 bool b_force_crop; /**< force cropping of subpicture */
87 int i_crop_x, i_crop_y, i_crop_width, i_crop_height; /**< cropping */
89 int i_margin; /**< force position of a subpicture */
90 bool b_force_palette; /**< force palette of subpicture */
91 uint8_t palette[4][4]; /**< forced palette */
93 /* Subpiture filters */
94 char *psz_chain_update;
95 vlc_mutex_t chain_lock;
96 filter_chain_t *p_chain;
99 mtime_t i_last_sort_date;
108 static spu_scale_t spu_scale_create( int w, int h );
109 static spu_scale_t spu_scale_unit(void );
110 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd );
111 static int spu_scale_w( int v, const spu_scale_t s );
112 static int spu_scale_h( int v, const spu_scale_t s );
113 static int spu_invscale_w( int v, const spu_scale_t s );
114 static int spu_invscale_h( int v, const spu_scale_t s );
126 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t );
127 static spu_area_t spu_area_scaled( spu_area_t );
128 static spu_area_t spu_area_unscaled( spu_area_t, spu_scale_t );
129 static bool spu_area_overlap( spu_area_t, spu_area_t );
132 /* Subpicture rendered flag
133 * FIXME ? it could be moved to private ? */
134 #define SUBPICTURE_RENDERED (0x1000)
135 #if SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
136 # error SUBPICTURE_RENDERED too low
139 #define SCALE_UNIT (1000)
141 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic );
142 static int SubpictureCmp( const void *s0, const void *s1 );
144 static void SpuRenderRegion( spu_t *,
145 picture_t *p_pic_dst, spu_area_t *,
146 subpicture_t *, subpicture_region_t *,
147 const spu_scale_t scale_size,
148 const video_format_t *p_fmt,
149 const spu_area_t *p_subtitle_area, int i_subtitle_area,
150 mtime_t render_date );
152 static void UpdateSPU ( spu_t *, vlc_object_t * );
153 static int CropCallback( vlc_object_t *, char const *,
154 vlc_value_t, vlc_value_t, void * );
156 /* Buffer allocation for SPU filter (blend, scale, ...) */
157 struct filter_owner_sys_t
162 static int spu_get_attachments( filter_t *,
163 input_attachment_t ***, int * );
164 static picture_t *spu_new_video_buffer( filter_t * );
165 static void spu_del_video_buffer( filter_t *, picture_t * );
167 /* Buffer aloccation fir SUB filter */
168 static int SubFilterAllocationInit( filter_t *, void * );
169 static void SubFilterAllocationClean( filter_t * );
172 static void SpuRenderCreateAndLoadText( spu_t * );
173 static void SpuRenderCreateAndLoadScale( spu_t * );
174 static void FilterRelease( filter_t *p_filter );
176 /*****************************************************************************
178 *****************************************************************************/
182 * Creates the subpicture unit
184 * \param p_this the parent object which creates the subpicture unit
186 spu_t *spu_Create( vlc_object_t *p_this )
189 spu_private_t *p_sys;
191 p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
192 VLC_OBJECT_GENERIC, "subpicture" );
195 vlc_object_attach( p_spu, p_this );
197 /* Initialize spu fields */
198 p_spu->p = p_sys = (spu_private_t*)&p_spu[1];
200 /* Initialize private fields */
201 vlc_mutex_init( &p_sys->lock );
203 SpuHeapInit( &p_sys->heap );
205 p_sys->p_blend = NULL;
206 p_sys->p_text = NULL;
207 p_sys->p_scale = NULL;
208 p_sys->p_scale_yuvp = NULL;
210 p_sys->i_margin = var_InheritInteger( p_spu, "sub-margin" );
212 /* Register the default subpicture channel */
213 p_sys->i_channel = SPU_DEFAULT_CHANNEL + 1;
215 p_sys->psz_chain_update = NULL;
216 vlc_mutex_init( &p_sys->chain_lock );
217 p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
218 SubFilterAllocationInit,
219 SubFilterAllocationClean,
222 /* Load text and scale module */
223 SpuRenderCreateAndLoadText( p_spu );
224 SpuRenderCreateAndLoadScale( p_spu );
227 p_sys->i_last_sort_date = -1;
233 * Destroy the subpicture unit
235 * \param p_this the parent object which destroys the subpicture unit
237 void spu_Destroy( spu_t *p_spu )
239 spu_private_t *p_sys = p_spu->p;
242 filter_DeleteBlend( p_sys->p_blend );
245 FilterRelease( p_sys->p_text );
247 if( p_sys->p_scale_yuvp )
248 FilterRelease( p_sys->p_scale_yuvp );
251 FilterRelease( p_sys->p_scale );
253 filter_chain_Delete( p_sys->p_chain );
254 vlc_mutex_destroy( &p_sys->chain_lock );
255 free( p_sys->psz_chain_update );
257 /* Destroy all remaining subpictures */
258 SpuHeapClean( &p_sys->heap );
260 vlc_mutex_destroy( &p_sys->lock );
262 vlc_object_release( p_spu );
266 * Attach/Detach the SPU from any input
268 * \param p_this the object in which to destroy the subpicture unit
269 * \param b_attach to select attach or detach
271 void spu_Attach( spu_t *p_spu, vlc_object_t *p_input, bool b_attach )
275 UpdateSPU( p_spu, p_input );
276 var_Create( p_input, "highlight", VLC_VAR_BOOL );
277 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
279 vlc_mutex_lock( &p_spu->p->lock );
280 p_spu->p->p_input = p_input;
282 FilterRelease( p_spu->p->p_text );
283 p_spu->p->p_text = NULL;
284 SpuRenderCreateAndLoadText( p_spu );
286 vlc_mutex_unlock( &p_spu->p->lock );
290 vlc_mutex_lock( &p_spu->p->lock );
291 p_spu->p->p_input = NULL;
292 vlc_mutex_unlock( &p_spu->p->lock );
294 /* Delete callbacks */
295 var_DelCallback( p_input, "highlight", CropCallback, p_spu );
296 var_Destroy( p_input, "highlight" );
301 * Inform the SPU filters of mouse event
303 int spu_ProcessMouse( spu_t *p_spu,
304 const vlc_mouse_t *p_mouse,
305 const video_format_t *p_fmt )
307 spu_private_t *p_sys = p_spu->p;
309 vlc_mutex_lock( &p_sys->chain_lock );
310 filter_chain_MouseEvent( p_sys->p_chain, p_mouse, p_fmt );
311 vlc_mutex_unlock( &p_sys->chain_lock );
317 * Display a subpicture
319 * Remove the reservation flag of a subpicture, which will cause it to be
321 * \param p_spu the subpicture unit object
322 * \param p_subpic the subpicture to display
324 void spu_DisplaySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
326 spu_private_t *p_sys = p_spu->p;
328 /* SPU_DEFAULT_CHANNEL always reset itself */
329 if( p_subpic->i_channel == SPU_DEFAULT_CHANNEL )
330 spu_ClearChannel( p_spu, SPU_DEFAULT_CHANNEL );
332 /* p_private is for spu only and cannot be non NULL here */
333 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
334 assert( r->p_private == NULL );
337 vlc_mutex_lock( &p_sys->lock );
338 if( SpuHeapPush( &p_sys->heap, p_subpic ) )
340 vlc_mutex_unlock( &p_sys->lock );
341 msg_Err( p_spu, "subpicture heap full" );
342 subpicture_Delete( p_subpic );
345 vlc_mutex_unlock( &p_sys->lock );
349 * This function renders all sub picture units in the list.
351 void spu_RenderSubpictures( spu_t *p_spu,
352 picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
353 subpicture_t *p_subpic_list,
354 const video_format_t *p_fmt_src,
355 mtime_t render_subtitle_date )
357 spu_private_t *p_sys = p_spu->p;
359 const mtime_t render_osd_date = mdate();
361 const int i_source_video_width = p_fmt_src->i_width;
362 const int i_source_video_height = p_fmt_src->i_height;
364 unsigned int i_subpicture;
365 subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
367 unsigned int i_subtitle_region_count;
368 spu_area_t p_subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
369 spu_area_t *p_subtitle_area;
372 vlc_mutex_lock( &p_sys->lock );
374 /* Preprocess subpictures */
376 i_subtitle_region_count = 0;
377 for( subpicture_t * p_subpic = p_subpic_list;
379 p_subpic = p_subpic->p_next )
381 subpicture_Update( p_subpic,
382 p_fmt_src, p_fmt_dst,
383 p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
386 if( p_subpic->b_subtitle )
388 for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
389 i_subtitle_region_count++;
393 pp_subpicture[i_subpicture++] = p_subpic;
396 /* Be sure we have at least 1 picture to process */
397 if( i_subpicture <= 0 )
399 vlc_mutex_unlock( &p_sys->lock );
403 /* Now order subpicture array
404 * XXX The order is *really* important for overlap subtitles positionning */
405 qsort( pp_subpicture, i_subpicture, sizeof(*pp_subpicture), SubpictureCmp );
407 /* Allocate area array for subtitle overlap */
409 p_subtitle_area = p_subtitle_area_buffer;
410 if( i_subtitle_region_count > sizeof(p_subtitle_area_buffer)/sizeof(*p_subtitle_area_buffer) )
411 p_subtitle_area = calloc( i_subtitle_region_count, sizeof(*p_subtitle_area) );
413 /* Create the blending module */
414 if( !p_sys->p_blend )
415 p_spu->p->p_blend = filter_NewBlend( VLC_OBJECT(p_spu), p_fmt_dst );
417 /* Process all subpictures and regions (in the right order) */
418 for( unsigned int i_index = 0; i_index < i_subpicture; i_index++ )
420 subpicture_t *p_subpic = pp_subpicture[i_index];
421 subpicture_region_t *p_region;
423 if( !p_subpic->p_region )
426 /* FIXME when possible use a better rendering size than source size
427 * (max of display size and source size for example) FIXME */
428 int i_render_width = p_subpic->i_original_picture_width;
429 int i_render_height = p_subpic->i_original_picture_height;
430 if( !i_render_width || !i_render_height )
432 if( i_render_width != 0 || i_render_height != 0 )
433 msg_Err( p_spu, "unsupported original picture size %dx%d",
434 i_render_width, i_render_height );
436 p_subpic->i_original_picture_width = i_render_width = i_source_video_width;
437 p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
442 p_sys->p_text->fmt_out.video.i_width =
443 p_sys->p_text->fmt_out.video.i_visible_width = i_render_width;
445 p_sys->p_text->fmt_out.video.i_height =
446 p_sys->p_text->fmt_out.video.i_visible_height = i_render_height;
449 /* Compute scaling from picture to source size */
450 spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
451 i_source_video_height, i_render_height );
453 /* Update scaling from source size to display size(p_fmt_dst) */
454 scale.w = scale.w * p_fmt_dst->i_width / i_source_video_width;
455 scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
457 /* Set default subpicture aspect ratio
458 * FIXME if we only handle 1 aspect ratio per picture, why is it set per
460 p_region = p_subpic->p_region;
461 if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
463 p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
464 p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
467 /* Take care of the aspect ratio */
468 if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
469 p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
471 /* FIXME FIXME what about region->i_x/i_y ? */
473 (int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
474 p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
477 /* Render all regions
478 * We always transform non absolute subtitle into absolute one on the
479 * first rendering to allow good subtitle overlap support.
481 for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
485 /* Check scale validity */
486 if( scale.w <= 0 || scale.h <= 0 )
490 SpuRenderRegion( p_spu, p_pic_dst, &area,
491 p_subpic, p_region, scale, p_fmt_dst,
492 p_subtitle_area, i_subtitle_area,
493 p_subpic->b_subtitle ? render_subtitle_date : render_osd_date );
495 if( p_subpic->b_subtitle )
497 area = spu_area_unscaled( area, scale );
498 if( !p_subpic->b_absolute && area.i_width > 0 && area.i_height > 0 )
500 p_region->i_x = area.i_x;
501 p_region->i_y = area.i_y;
503 if( p_subtitle_area )
504 p_subtitle_area[i_subtitle_area++] = area;
507 if( p_subpic->b_subtitle )
508 p_subpic->b_absolute = true;
512 if( p_subtitle_area != p_subtitle_area_buffer )
513 free( p_subtitle_area );
515 vlc_mutex_unlock( &p_sys->lock );
518 /*****************************************************************************
519 * spu_SortSubpictures: find the subpictures to display
520 *****************************************************************************
521 * This function parses all subpictures and decides which ones need to be
522 * displayed. If no picture has been selected, display_date will depend on
524 * We also check for ephemer DVD subpictures (subpictures that have
525 * to be removed if a newer one is available), which makes it a lot
526 * more difficult to guess if a subpicture has to be rendered or not.
527 *****************************************************************************/
528 subpicture_t *spu_SortSubpictures( spu_t *p_spu, mtime_t render_subtitle_date,
529 bool b_subtitle_only )
531 spu_private_t *p_sys = p_spu->p;
532 subpicture_t *p_subpic = NULL;
533 const mtime_t render_osd_date = mdate();
535 /* Update sub-filter chain */
536 vlc_mutex_lock( &p_sys->lock );
537 char *psz_chain_update = p_sys->psz_chain_update;
538 p_sys->psz_chain_update = NULL;
539 vlc_mutex_unlock( &p_sys->lock );
541 vlc_mutex_lock( &p_sys->chain_lock );
542 if( psz_chain_update )
544 filter_chain_Reset( p_sys->p_chain, NULL, NULL );
546 filter_chain_AppendFromString( p_spu->p->p_chain, psz_chain_update );
548 free( psz_chain_update );
550 /* Run subpicture filters */
551 filter_chain_SubFilter( p_sys->p_chain, render_osd_date );
552 vlc_mutex_unlock( &p_sys->chain_lock );
554 vlc_mutex_lock( &p_sys->lock );
556 /* Create a list of channels */
557 int pi_channel[VOUT_MAX_SUBPICTURES];
558 int i_channel_count = 0;
560 for( int i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
562 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
563 if( !p_entry->p_subpicture || p_entry->b_reject )
565 const int i_channel = p_entry->p_subpicture->i_channel;
567 for( i = 0; i < i_channel_count; i++ )
569 if( pi_channel[i] == i_channel )
572 if( i_channel_count <= i )
573 pi_channel[i_channel_count++] = i_channel;
576 /* We get an easily parsable chained list of subpictures which
577 * ends with NULL since p_subpic was initialized to NULL. */
578 for( int i = 0; i < i_channel_count; i++ )
580 const int i_channel = pi_channel[i];
581 subpicture_t *p_available_subpic[VOUT_MAX_SUBPICTURES];
582 bool pb_available_late[VOUT_MAX_SUBPICTURES];
585 mtime_t start_date = render_subtitle_date;
586 mtime_t ephemer_subtitle_date = 0;
587 mtime_t ephemer_osd_date = 0;
588 int64_t i_ephemer_subtitle_order = INT64_MIN;
589 int64_t i_ephemer_system_order = INT64_MIN;
592 /* Select available pictures */
593 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
595 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_index];
596 subpicture_t *p_current = p_entry->p_subpicture;
600 if( !p_current || p_entry->b_reject )
602 if( p_entry->b_reject )
603 SpuHeapDeleteAt( &p_sys->heap, i_index );
607 if( p_current->i_channel != i_channel ||
608 ( b_subtitle_only && !p_current->b_subtitle ) )
612 const mtime_t render_date = p_current->b_subtitle ? render_subtitle_date : render_osd_date;
614 render_date < p_current->i_start )
616 /* Too early, come back next monday */
620 mtime_t *pi_ephemer_date = p_current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
621 int64_t *pi_ephemer_order = p_current->b_subtitle ? &i_ephemer_subtitle_order : &i_ephemer_system_order;
622 if( p_current->i_start >= *pi_ephemer_date )
624 *pi_ephemer_date = p_current->i_start;
625 if( p_current->i_order > *pi_ephemer_order )
626 *pi_ephemer_order = p_current->i_order;
629 b_stop_valid = !p_current->b_ephemer || p_current->i_stop > p_current->i_start;
631 b_late = b_stop_valid && p_current->i_stop <= render_date;
633 /* start_date will be used for correct automatic overlap support
634 * in case picture that should not be displayed anymore (display_time)
635 * overlap with a picture to be displayed (p_current->i_start) */
636 if( p_current->b_subtitle && !b_late && !p_current->b_ephemer )
637 start_date = p_current->i_start;
640 p_available_subpic[i_available] = p_current;
641 pb_available_late[i_available] = b_late;
645 /* Only forced old picture display at the transition */
646 if( start_date < p_sys->i_last_sort_date )
647 start_date = p_sys->i_last_sort_date;
648 if( start_date <= 0 )
649 start_date = INT64_MAX;
651 /* Select pictures to be displayed */
652 for( i_index = 0; i_index < i_available; i_index++ )
654 subpicture_t *p_current = p_available_subpic[i_index];
655 bool b_late = pb_available_late[i_index];
657 const mtime_t stop_date = p_current->b_subtitle ? __MAX( start_date, p_sys->i_last_sort_date ) : render_osd_date;
658 const mtime_t ephemer_date = p_current->b_subtitle ? ephemer_subtitle_date : ephemer_osd_date;
659 const int64_t i_ephemer_order = p_current->b_subtitle ? i_ephemer_subtitle_order : i_ephemer_system_order;
661 /* Destroy late and obsolete ephemer subpictures */
662 bool b_rejet = b_late && p_current->i_stop <= stop_date;
663 if( p_current->b_ephemer )
665 if( p_current->i_start < ephemer_date )
667 else if( p_current->i_start == ephemer_date &&
668 p_current->i_order < i_ephemer_order )
673 SpuHeapDeleteSubpicture( &p_sys->heap, p_current );
675 SubpictureChain( &p_subpic, p_current );
679 p_sys->i_last_sort_date = render_subtitle_date;
680 vlc_mutex_unlock( &p_sys->lock );
685 void spu_OffsetSubtitleDate( spu_t *p_spu, mtime_t i_duration )
687 spu_private_t *p_sys = p_spu->p;
689 vlc_mutex_lock( &p_sys->lock );
690 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
692 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i];
693 subpicture_t *p_current = p_entry->p_subpicture;
695 if( p_current && p_current->b_subtitle )
697 if( p_current->i_start > 0 )
698 p_current->i_start += i_duration;
699 if( p_current->i_stop > 0 )
700 p_current->i_stop += i_duration;
703 vlc_mutex_unlock( &p_sys->lock );
706 int spu_RegisterChannel( spu_t *p_spu )
708 spu_private_t *p_sys = p_spu->p;
710 vlc_mutex_lock( &p_sys->lock );
711 int i_channel = p_sys->i_channel++;
712 vlc_mutex_unlock( &p_sys->lock );
717 void spu_ClearChannel( spu_t *p_spu, int i_channel )
719 spu_private_t *p_sys = p_spu->p;
721 vlc_mutex_lock( &p_sys->lock );
723 for( int i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
725 spu_heap_entry_t *p_entry = &p_sys->heap.p_entry[i_subpic];
726 subpicture_t *p_subpic = p_entry->p_subpicture;
730 if( p_subpic->i_channel != i_channel && ( i_channel != -1 || p_subpic->i_channel == SPU_DEFAULT_CHANNEL ) )
733 /* You cannot delete subpicture outside of spu_SortSubpictures */
734 p_entry->b_reject = true;
737 vlc_mutex_unlock( &p_sys->lock );
740 void spu_ChangeFilters( spu_t *p_spu, const char *psz_filters )
742 spu_private_t *p_sys = p_spu->p;
744 vlc_mutex_lock( &p_sys->lock );
746 free( p_sys->psz_chain_update );
747 p_sys->psz_chain_update = strdup( psz_filters );
749 vlc_mutex_unlock( &p_sys->lock );
752 void spu_ChangeMargin( spu_t *p_spu, int i_margin )
754 spu_private_t *p_sys = p_spu->p;
756 vlc_mutex_lock( &p_sys->lock );
757 p_sys->i_margin = i_margin;
758 vlc_mutex_unlock( &p_sys->lock );
762 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
764 p_subpic->p_next = *pp_head;
769 /*****************************************************************************
771 *****************************************************************************/
772 static void SpuHeapInit( spu_heap_t *p_heap )
774 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
776 spu_heap_entry_t *e = &p_heap->p_entry[i];
778 e->p_subpicture = NULL;
783 static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
785 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
787 spu_heap_entry_t *e = &p_heap->p_entry[i];
789 if( e->p_subpicture )
792 e->p_subpicture = p_subpic;
799 static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
801 spu_heap_entry_t *e = &p_heap->p_entry[i_index];
803 if( e->p_subpicture )
804 subpicture_Delete( e->p_subpicture );
806 e->p_subpicture = NULL;
809 static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
811 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
813 spu_heap_entry_t *e = &p_heap->p_entry[i];
815 if( e->p_subpicture != p_subpic )
818 SpuHeapDeleteAt( p_heap, i );
824 static void SpuHeapClean( spu_heap_t *p_heap )
826 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
828 spu_heap_entry_t *e = &p_heap->p_entry[i];
829 if( e->p_subpicture )
830 subpicture_Delete( e->p_subpicture );
834 static void FilterRelease( filter_t *p_filter )
836 if( p_filter->p_module )
837 module_unneed( p_filter, p_filter->p_module );
838 if( p_filter->p_owner )
839 free( p_filter->p_owner );
841 vlc_object_release( p_filter );
844 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
848 assert( !p_spu->p->p_text );
851 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
852 VLC_OBJECT_GENERIC, "spu text" );
856 p_text->p_owner = xmalloc( sizeof(*p_text->p_owner) );
857 p_text->p_owner->p_spu = p_spu;
859 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
861 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
862 p_text->fmt_out.video.i_width =
863 p_text->fmt_out.video.i_visible_width = 32;
864 p_text->fmt_out.video.i_height =
865 p_text->fmt_out.video.i_visible_height = 32;
867 p_text->pf_get_attachments = spu_get_attachments;
869 vlc_object_attach( p_text, p_spu );
871 /* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
872 * same than these 2 calls ? */
873 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
874 if( psz_modulename && *psz_modulename )
876 p_text->p_module = module_need( p_text, "text renderer",
877 psz_modulename, true );
879 free( psz_modulename );
881 if( !p_text->p_module )
882 p_text->p_module = module_need( p_text, "text renderer", NULL, false );
884 /* Create a few variables used for enhanced text rendering */
885 var_Create( p_text, "spu-duration", VLC_VAR_TIME );
886 var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
887 var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
888 var_Create( p_text, "scale", VLC_VAR_INTEGER );
891 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
892 vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
897 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
898 VLC_OBJECT_GENERIC, "scale" );
902 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
903 p_scale->fmt_in.video.i_chroma = i_src_chroma;
904 p_scale->fmt_in.video.i_width =
905 p_scale->fmt_in.video.i_height = 32;
907 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
908 p_scale->fmt_out.video.i_chroma = i_dst_chroma;
909 p_scale->fmt_out.video.i_width =
910 p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
912 p_scale->pf_video_buffer_new = spu_new_video_buffer;
913 p_scale->pf_video_buffer_del = spu_del_video_buffer;
915 vlc_object_attach( p_scale, p_obj );
916 p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
920 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
922 assert( !p_spu->p->p_scale );
923 assert( !p_spu->p->p_scale_yuvp );
924 /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
926 p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
927 VLC_CODEC_YUVA, VLC_CODEC_YUVA, true );
928 /* This one is used for YUVP to YUVA/RGBA without scaling
930 p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
931 VLC_CODEC_YUVP, VLC_CODEC_YUVA, false );
934 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
935 subpicture_t *p_subpic, subpicture_region_t *p_region,
936 int i_min_scale_ratio, mtime_t render_date )
938 filter_t *p_text = p_spu->p->p_text;
940 assert( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
942 if( !p_text || !p_text->p_module )
945 /* Setup 3 variables which can be used to render
946 * time-dependent text (and effects). The first indicates
947 * the total amount of time the text will be on screen,
948 * the second the amount of time it has already been on
949 * screen (can be a negative value as text is layed out
950 * before it is rendered) and the third is a feedback
951 * variable from the renderer - if the renderer sets it
952 * then this particular text is time-dependent, eg. the
953 * visual progress bar inside the text in karaoke and the
954 * text needs to be rendered multiple times in order for
955 * the effect to work - we therefore need to return the
956 * region to its original state at the end of the loop,
957 * instead of leaving it in YUVA or YUVP.
958 * Any renderer which is unaware of how to render
959 * time-dependent text can happily ignore the variables
960 * and render the text the same as usual - it should at
961 * least show up on screen, but the effect won't change
962 * the text over time.
964 var_SetTime( p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
965 var_SetTime( p_text, "spu-elapsed", render_date );
966 var_SetBool( p_text, "text-rerender", false );
967 var_SetInteger( p_text, "scale", i_min_scale_ratio );
969 if( p_text->pf_render_html && p_region->psz_html )
971 p_text->pf_render_html( p_text, p_region, p_region );
973 else if( p_text->pf_render_text )
975 p_text->pf_render_text( p_text, p_region, p_region );
977 *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
980 p_region->i_align |= SUBPICTURE_RENDERED;
984 * A few scale functions helpers.
986 static spu_scale_t spu_scale_create( int w, int h )
988 spu_scale_t s = { .w = w, .h = h };
995 static spu_scale_t spu_scale_unit( void )
997 return spu_scale_create( SCALE_UNIT, SCALE_UNIT );
999 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
1001 return spu_scale_create( wn * SCALE_UNIT / wd,
1002 hn * SCALE_UNIT / hd );
1004 static int spu_scale_w( int v, const spu_scale_t s )
1006 return v * s.w / SCALE_UNIT;
1008 static int spu_scale_h( int v, const spu_scale_t s )
1010 return v * s.h / SCALE_UNIT;
1012 static int spu_invscale_w( int v, const spu_scale_t s )
1014 return v * SCALE_UNIT / s.w;
1016 static int spu_invscale_h( int v, const spu_scale_t s )
1018 return v * SCALE_UNIT / s.h;
1022 * A few area functions helpers
1024 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t s )
1026 spu_area_t a = { .i_x = x, .i_y = y, .i_width = w, .i_height = h, .scale = s };
1029 static spu_area_t spu_area_scaled( spu_area_t a )
1031 if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
1034 a.i_x = spu_scale_w( a.i_x, a.scale );
1035 a.i_y = spu_scale_h( a.i_y, a.scale );
1037 a.i_width = spu_scale_w( a.i_width, a.scale );
1038 a.i_height = spu_scale_h( a.i_height, a.scale );
1040 a.scale = spu_scale_unit();
1043 static spu_area_t spu_area_unscaled( spu_area_t a, spu_scale_t s )
1045 if( a.scale.w == s.w && a.scale.h == s.h )
1048 a = spu_area_scaled( a );
1050 a.i_x = spu_invscale_w( a.i_x, s );
1051 a.i_y = spu_invscale_h( a.i_y, s );
1053 a.i_width = spu_invscale_w( a.i_width, s );
1054 a.i_height = spu_invscale_h( a.i_height, s );
1059 static bool spu_area_overlap( spu_area_t a, spu_area_t b )
1064 a = spu_area_scaled( a );
1065 b = spu_area_scaled( b );
1067 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 ) &&
1068 __MAX( a.i_y-i_dy, b.i_y ) < __MIN( a.i_y+a.i_height+i_dy, b.i_y+b.i_height );
1072 * Avoid area overlapping
1074 static void SpuAreaFixOverlap( spu_area_t *p_dst,
1075 const spu_area_t *p_sub, int i_sub, int i_align )
1077 spu_area_t a = spu_area_scaled( *p_dst );
1078 bool b_moved = false;
1081 /* Check for overlap
1082 * XXX It is not fast O(n^2) but we should not have a lot of region */
1086 for( int i = 0; i < i_sub; i++ )
1088 spu_area_t sub = spu_area_scaled( p_sub[i] );
1090 if( !spu_area_overlap( a, sub ) )
1093 if( i_align & SUBPICTURE_ALIGN_TOP )
1096 int i_y = sub.i_y + sub.i_height;
1100 else if( i_align & SUBPICTURE_ALIGN_BOTTOM )
1103 int i_y = sub.i_y - a.i_height;
1109 /* TODO what to do in this case? */
1110 //fprintf( stderr, "Overlap with unsupported alignment\n" );
1120 *p_dst = spu_area_unscaled( a, p_dst->scale );
1124 static void SpuAreaFitInside( spu_area_t *p_area, const spu_area_t *p_boundary )
1126 spu_area_t a = spu_area_scaled( *p_area );
1128 const int i_error_x = (a.i_x + a.i_width) - p_boundary->i_width;
1134 const int i_error_y = (a.i_y + a.i_height) - p_boundary->i_height;
1140 *p_area = spu_area_unscaled( a, p_area->scale );
1146 static void SpuRegionPlace( int *pi_x, int *pi_y,
1147 const subpicture_t *p_subpic,
1148 const subpicture_region_t *p_region )
1150 const int i_delta_x = p_region->i_x;
1151 const int i_delta_y = p_region->i_y;
1154 assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
1155 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
1159 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
1161 i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
1165 i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
1168 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
1172 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
1174 i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
1178 i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
1181 if( p_subpic->b_absolute )
1192 * This function computes the current alpha value for a given region.
1194 static int SpuRegionAlpha( subpicture_t *p_subpic, subpicture_region_t *p_region )
1196 /* Compute alpha blend value */
1197 int i_fade_alpha = 255;
1198 if( p_subpic->b_fade )
1200 mtime_t i_fade_start = ( p_subpic->i_stop +
1201 p_subpic->i_start ) / 2;
1202 mtime_t i_now = mdate();
1204 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
1206 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
1207 ( p_subpic->i_stop - i_fade_start );
1210 return i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
1214 * It will render the provided region onto p_pic_dst.
1217 static void SpuRenderRegion( spu_t *p_spu,
1218 picture_t *p_pic_dst, spu_area_t *p_area,
1219 subpicture_t *p_subpic, subpicture_region_t *p_region,
1220 const spu_scale_t scale_size,
1221 const video_format_t *p_fmt,
1222 const spu_area_t *p_subtitle_area, int i_subtitle_area,
1223 mtime_t render_date )
1225 spu_private_t *p_sys = p_spu->p;
1227 video_format_t fmt_original = p_region->fmt;
1228 bool b_rerender_text = false;
1229 bool b_restore_format = false;
1233 video_format_t region_fmt;
1234 picture_t *p_region_picture;
1236 /* Invalidate area by default */
1237 *p_area = spu_area_create( 0,0, 0,0, scale_size );
1239 /* Render text region */
1240 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1242 const int i_min_scale_ratio = SCALE_UNIT; /* FIXME what is the right value? (scale_size is not) */
1243 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region,
1244 i_min_scale_ratio, render_date );
1245 b_restore_format = b_rerender_text;
1247 /* Check if the rendering has failed ... */
1248 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1252 /* Force palette if requested
1253 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
1254 * instead of only the right one (being the dvd spu).
1256 const bool b_using_palette = p_region->fmt.i_chroma == VLC_CODEC_YUVP;
1257 const bool b_force_palette = b_using_palette && p_sys->b_force_palette;
1258 const bool b_force_crop = b_force_palette && p_sys->b_force_crop;
1259 bool b_changed_palette = false;
1262 /* Compute the margin which is expressed in destination pixel unit
1263 * The margin is applied only to subtitle and when no forced crop is
1264 * requested (dvd menu) */
1266 if( !b_force_crop && p_subpic->b_subtitle )
1267 i_margin_y = spu_invscale_h( p_sys->i_margin, scale_size );
1269 /* Place the picture
1270 * We compute the position in the rendered size */
1271 SpuRegionPlace( &i_x_offset, &i_y_offset,
1272 p_subpic, p_region );
1274 /* Save this position for subtitle overlap support
1275 * it is really important that there are given without scale_size applied */
1276 *p_area = spu_area_create( i_x_offset, i_y_offset,
1277 p_region->fmt.i_width, p_region->fmt.i_height,
1280 /* Handle overlapping subtitles when possible */
1281 if( p_subpic->b_subtitle && !p_subpic->b_absolute )
1283 SpuAreaFixOverlap( p_area, p_subtitle_area, i_subtitle_area,
1284 p_region->i_align );
1287 /* we copy the area: for the subtitle overlap support we want
1288 * to only save the area without margin applied */
1289 spu_area_t restrained = *p_area;
1291 /* apply margin to subtitles and correct if they go over the picture edge */
1292 if( p_subpic->b_subtitle )
1293 restrained.i_y -= i_margin_y;
1295 spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
1297 SpuAreaFitInside( &restrained, &display );
1299 /* Fix the position for the current scale_size */
1300 i_x_offset = spu_scale_w( restrained.i_x, restrained.scale );
1301 i_y_offset = spu_scale_h( restrained.i_y, restrained.scale );
1304 if( b_force_palette )
1306 video_palette_t *p_palette = p_region->fmt.p_palette;
1307 video_palette_t palette;
1309 /* We suppose DVD palette here */
1310 palette.i_entries = 4;
1311 for( int i = 0; i < 4; i++ )
1312 for( int j = 0; j < 4; j++ )
1313 palette.palette[i][j] = p_sys->palette[i][j];
1315 if( p_palette->i_entries == palette.i_entries )
1317 for( int i = 0; i < p_palette->i_entries; i++ )
1318 for( int j = 0; j < 4; j++ )
1319 b_changed_palette |= p_palette->palette[i][j] != palette.palette[i][j];
1323 b_changed_palette = true;
1325 *p_palette = palette;
1329 region_fmt = p_region->fmt;
1330 p_region_picture = p_region->p_picture;
1333 /* Scale from rendered size to destination size */
1334 if( p_sys->p_scale && p_sys->p_scale->p_module &&
1335 ( !b_using_palette || ( p_sys->p_scale_yuvp && p_sys->p_scale_yuvp->p_module ) ) &&
1336 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_using_palette ) )
1338 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
1339 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
1341 /* Destroy the cache if unusable */
1342 if( p_region->p_private )
1344 subpicture_region_private_t *p_private = p_region->p_private;
1345 bool b_changed = false;
1347 /* Check resize changes */
1348 if( i_dst_width != p_private->fmt.i_width ||
1349 i_dst_height != p_private->fmt.i_height )
1352 /* Check forced palette changes */
1353 if( b_changed_palette )
1358 subpicture_region_private_Delete( p_private );
1359 p_region->p_private = NULL;
1363 /* Scale if needed into cache */
1364 if( !p_region->p_private && i_dst_width > 0 && i_dst_height > 0 )
1366 filter_t *p_scale = p_sys->p_scale;
1368 picture_t *p_picture = p_region->p_picture;
1369 picture_Hold( p_picture );
1371 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
1372 if( b_using_palette )
1374 filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
1376 p_scale_yuvp->fmt_in.video = p_region->fmt;
1378 /* TODO converting to RGBA for RGB video output is better */
1379 p_scale_yuvp->fmt_out.video = p_region->fmt;
1380 p_scale_yuvp->fmt_out.video.i_chroma = VLC_CODEC_YUVA;
1382 p_picture = p_scale_yuvp->pf_video_filter( p_scale_yuvp, p_picture );
1385 /* Well we will try conversion+scaling */
1386 msg_Warn( p_spu, "%4.4s to %4.4s conversion failed",
1387 (const char*)&p_scale_yuvp->fmt_in.video.i_chroma,
1388 (const char*)&p_scale_yuvp->fmt_out.video.i_chroma );
1392 /* Conversion(except from YUVP)/Scaling */
1394 ( p_picture->format.i_width != i_dst_width ||
1395 p_picture->format.i_height != i_dst_height ) )
1397 p_scale->fmt_in.video = p_picture->format;
1398 p_scale->fmt_out.video = p_picture->format;
1400 p_scale->fmt_out.video.i_width = i_dst_width;
1401 p_scale->fmt_out.video.i_height = i_dst_height;
1403 p_scale->fmt_out.video.i_visible_width =
1404 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
1405 p_scale->fmt_out.video.i_visible_height =
1406 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
1408 p_picture = p_scale->pf_video_filter( p_scale, p_picture );
1410 msg_Err( p_spu, "scaling failed" );
1416 p_region->p_private = subpicture_region_private_New( &p_picture->format );
1417 if( p_region->p_private )
1419 p_region->p_private->p_picture = p_picture;
1420 if( !p_region->p_private->p_picture )
1422 subpicture_region_private_Delete( p_region->p_private );
1423 p_region->p_private = NULL;
1428 picture_Release( p_picture );
1433 /* And use the scaled picture */
1434 if( p_region->p_private )
1436 region_fmt = p_region->p_private->fmt;
1437 p_region_picture = p_region->p_private->p_picture;
1441 /* Force cropping if requested */
1444 int i_crop_x = spu_scale_w( p_sys->i_crop_x, scale_size );
1445 int i_crop_y = spu_scale_h( p_sys->i_crop_y, scale_size );
1446 int i_crop_width = spu_scale_w( p_sys->i_crop_width, scale_size );
1447 int i_crop_height= spu_scale_h( p_sys->i_crop_height,scale_size );
1449 /* Find the intersection */
1450 if( i_crop_x + i_crop_width <= i_x_offset ||
1451 i_x_offset + (int)region_fmt.i_visible_width < i_crop_x ||
1452 i_crop_y + i_crop_height <= i_y_offset ||
1453 i_y_offset + (int)region_fmt.i_visible_height < i_crop_y )
1455 /* No intersection */
1456 region_fmt.i_visible_width =
1457 region_fmt.i_visible_height = 0;
1461 int i_x, i_y, i_x_end, i_y_end;
1462 i_x = __MAX( i_crop_x, i_x_offset );
1463 i_y = __MAX( i_crop_y, i_y_offset );
1464 i_x_end = __MIN( i_crop_x + i_crop_width,
1465 i_x_offset + (int)region_fmt.i_visible_width );
1466 i_y_end = __MIN( i_crop_y + i_crop_height,
1467 i_y_offset + (int)region_fmt.i_visible_height );
1469 region_fmt.i_x_offset = i_x - i_x_offset;
1470 region_fmt.i_y_offset = i_y - i_y_offset;
1471 region_fmt.i_visible_width = i_x_end - i_x;
1472 region_fmt.i_visible_height = i_y_end - i_y;
1474 i_x_offset = __MAX( i_x, 0 );
1475 i_y_offset = __MAX( i_y, 0 );
1479 /* Update the blender */
1480 if( filter_ConfigureBlend( p_spu->p->p_blend,
1481 p_fmt->i_width, p_fmt->i_height,
1483 filter_Blend( p_spu->p->p_blend,
1484 p_pic_dst, i_x_offset, i_y_offset,
1485 p_region_picture, SpuRegionAlpha( p_subpic, p_region ) ) )
1487 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1488 (char *)&p_sys->p_blend->fmt_in.video.i_chroma,
1489 (char *)&p_sys->p_blend->fmt_out.video.i_chroma );
1493 if( b_rerender_text )
1495 /* Some forms of subtitles need to be re-rendered more than
1496 * once, eg. karaoke. We therefore restore the region to its
1497 * pre-rendered state, so the next time through everything is
1500 if( p_region->p_picture )
1502 picture_Release( p_region->p_picture );
1503 p_region->p_picture = NULL;
1505 if( p_region->p_private )
1507 subpicture_region_private_Delete( p_region->p_private );
1508 p_region->p_private = NULL;
1510 p_region->i_align &= ~SUBPICTURE_RENDERED;
1512 if( b_restore_format )
1513 p_region->fmt = fmt_original;
1517 * This function compares two 64 bits integers.
1518 * It can be used by qsort.
1520 static int IntegerCmp( int64_t i0, int64_t i1 )
1522 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
1525 * This function compares 2 subpictures using the following properties
1526 * (ordered by priority)
1527 * 1. absolute positionning
1529 * 3. creation order (per channel)
1531 * It can be used by qsort.
1533 * XXX spu_RenderSubpictures depends heavily on this order.
1535 static int SubpictureCmp( const void *s0, const void *s1 )
1537 subpicture_t *p_subpic0 = *(subpicture_t**)s0;
1538 subpicture_t *p_subpic1 = *(subpicture_t**)s1;
1541 r = IntegerCmp( !p_subpic0->b_absolute, !p_subpic1->b_absolute );
1543 r = IntegerCmp( p_subpic0->i_start, p_subpic1->i_start );
1545 r = IntegerCmp( p_subpic0->i_channel, p_subpic1->i_channel );
1547 r = IntegerCmp( p_subpic0->i_order, p_subpic1->i_order );
1551 /*****************************************************************************
1552 * Object variables callbacks
1553 *****************************************************************************/
1555 /*****************************************************************************
1556 * UpdateSPU: update subpicture settings
1557 *****************************************************************************
1558 * This function is called from CropCallback and at initialization time, to
1559 * retrieve crop information from the input.
1560 *****************************************************************************/
1561 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1563 spu_private_t *p_sys = p_spu->p;
1566 vlc_mutex_lock( &p_sys->lock );
1568 p_sys->b_force_palette = false;
1569 p_sys->b_force_crop = false;
1571 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1573 vlc_mutex_unlock( &p_sys->lock );
1577 p_sys->b_force_crop = true;
1578 p_sys->i_crop_x = var_GetInteger( p_object, "x-start" );
1579 p_sys->i_crop_y = var_GetInteger( p_object, "y-start" );
1580 p_sys->i_crop_width = var_GetInteger( p_object, "x-end" ) - p_sys->i_crop_x;
1581 p_sys->i_crop_height = var_GetInteger( p_object, "y-end" ) - p_sys->i_crop_y;
1583 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1585 memcpy( p_sys->palette, val.p_address, 16 );
1586 p_sys->b_force_palette = true;
1588 vlc_mutex_unlock( &p_sys->lock );
1590 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1591 p_sys->i_crop_x, p_sys->i_crop_y,
1592 p_sys->i_crop_width, p_sys->i_crop_height,
1593 p_sys->b_force_palette );
1596 /*****************************************************************************
1597 * CropCallback: called when the highlight properties are changed
1598 *****************************************************************************
1599 * This callback is called from the input thread when we need cropping
1600 *****************************************************************************/
1601 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1602 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1604 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1606 UpdateSPU( (spu_t *)p_data, p_object );
1610 /*****************************************************************************
1611 * Buffers allocation callbacks for the filters
1612 *****************************************************************************/
1613 static int spu_get_attachments( filter_t *p_filter,
1614 input_attachment_t ***ppp_attachment,
1615 int *pi_attachment )
1617 spu_t *p_spu = p_filter->p_owner->p_spu;
1619 int i_ret = VLC_EGENERIC;
1620 if( p_spu->p->p_input )
1621 i_ret = input_Control( (input_thread_t*)p_spu->p->p_input,
1622 INPUT_GET_ATTACHMENTS,
1623 ppp_attachment, pi_attachment );
1627 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1629 filter_owner_sys_t *p_sys = p_filter->p_owner;
1631 subpicture_t *p_subpicture = subpicture_New( NULL );
1633 p_subpicture->i_channel = p_sys->i_channel;
1634 return p_subpicture;
1636 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1638 VLC_UNUSED( p_filter );
1639 subpicture_Delete( p_subpic );
1641 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1643 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1645 VLC_UNUSED(p_filter);
1646 return picture_NewFromFormat( p_fmt );
1648 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1650 VLC_UNUSED(p_filter);
1651 picture_Release( p_picture );
1654 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1656 spu_t *p_spu = p_data;
1658 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1660 return VLC_EGENERIC;
1662 p_filter->pf_sub_buffer_new = sub_new_buffer;
1663 p_filter->pf_sub_buffer_del = sub_del_buffer;
1665 p_filter->p_owner = p_sys;
1666 p_sys->i_channel = spu_RegisterChannel( p_spu );
1667 p_sys->p_spu = p_spu;
1672 static void SubFilterAllocationClean( filter_t *p_filter )
1674 filter_owner_sys_t *p_sys = p_filter->p_owner;
1676 spu_ClearChannel( p_sys->p_spu, p_sys->i_channel );
1677 free( p_filter->p_owner );