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 "../misc/subpicture.h"
47 /*****************************************************************************
49 *****************************************************************************/
51 /* Number of simultaneous subpictures */
52 #define VOUT_MAX_SUBPICTURES (__MAX(VOUT_MAX_PICTURES, SPU_MAX_PREPARE_TIME/5000))
57 subpicture_t *p_subpicture;
63 spu_heap_entry_t p_entry[VOUT_MAX_SUBPICTURES];
67 static void SpuHeapInit( spu_heap_t * );
68 static int SpuHeapPush( spu_heap_t *, subpicture_t * );
69 static void SpuHeapDeleteAt( spu_heap_t *, int i_index );
70 static int SpuHeapDeleteSubpicture( spu_heap_t *, subpicture_t * );
71 static void SpuHeapClean( spu_heap_t *p_heap );
75 vlc_mutex_t lock; /* lock to protect all followings fields */
76 vlc_object_t *p_input;
80 int i_channel; /**< number of subpicture channels registered */
81 filter_t *p_blend; /**< alpha blending module */
82 filter_t *p_text; /**< text renderer module */
83 filter_t *p_scale_yuvp; /**< scaling module for YUVP */
84 filter_t *p_scale; /**< scaling module (all but YUVP) */
85 bool b_force_crop; /**< force cropping of subpicture */
86 int i_crop_x, i_crop_y, i_crop_width, i_crop_height; /**< cropping */
88 int i_margin; /**< force position of a subpicture */
89 bool b_force_palette; /**< force palette of subpicture */
90 uint8_t palette[4][4]; /**< forced palette */
92 /* Subpiture filters */
93 char *psz_chain_update;
94 vlc_mutex_t chain_lock;
95 filter_chain_t *p_chain;
98 mtime_t i_last_sort_date;
107 static spu_scale_t spu_scale_create( int w, int h );
108 static spu_scale_t spu_scale_unit(void );
109 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd );
110 static int spu_scale_w( int v, const spu_scale_t s );
111 static int spu_scale_h( int v, const spu_scale_t s );
112 static int spu_invscale_w( int v, const spu_scale_t s );
113 static int spu_invscale_h( int v, const spu_scale_t s );
125 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t );
126 static spu_area_t spu_area_scaled( spu_area_t );
127 static spu_area_t spu_area_unscaled( spu_area_t, spu_scale_t );
128 static bool spu_area_overlap( spu_area_t, spu_area_t );
131 /* Subpicture rendered flag
132 * FIXME ? it could be moved to private ? */
133 #define SUBPICTURE_RENDERED (0x1000)
134 #if SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
135 # error SUBPICTURE_RENDERED too low
138 #define SCALE_UNIT (1000)
140 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic );
141 static int SubpictureCmp( const void *s0, const void *s1 );
143 static void SpuRenderRegion( spu_t *,
144 picture_t *p_pic_dst, spu_area_t *,
145 subpicture_t *, subpicture_region_t *,
146 const spu_scale_t scale_size,
147 const video_format_t *p_fmt,
148 const spu_area_t *p_subtitle_area, int i_subtitle_area,
149 mtime_t render_date );
151 static void UpdateSPU ( spu_t *, vlc_object_t * );
152 static int CropCallback( vlc_object_t *, char const *,
153 vlc_value_t, vlc_value_t, void * );
155 /* Buffer allocation for SPU filter (blend, scale, ...) */
156 struct filter_owner_sys_t
161 static int spu_get_attachments( filter_t *,
162 input_attachment_t ***, int * );
163 static picture_t *spu_new_video_buffer( filter_t * );
164 static void spu_del_video_buffer( filter_t *, picture_t * );
166 /* Buffer aloccation fir SUB filter */
167 static int SubFilterAllocationInit( filter_t *, void * );
168 static void SubFilterAllocationClean( filter_t * );
171 static void SpuRenderCreateAndLoadText( spu_t * );
172 static void SpuRenderCreateAndLoadScale( spu_t * );
173 static void FilterRelease( filter_t *p_filter );
175 /*****************************************************************************
177 *****************************************************************************/
181 * Creates the subpicture unit
183 * \param p_this the parent object which creates the subpicture unit
185 spu_t *spu_Create( vlc_object_t *p_this )
188 spu_private_t *p_sys;
190 p_spu = vlc_custom_create( p_this, sizeof(spu_t) + sizeof(spu_private_t),
191 VLC_OBJECT_GENERIC, "subpicture" );
194 vlc_object_attach( p_spu, p_this );
196 /* Initialize spu fields */
197 p_spu->p = p_sys = (spu_private_t*)&p_spu[1];
199 /* Initialize private fields */
200 vlc_mutex_init( &p_sys->lock );
202 SpuHeapInit( &p_sys->heap );
204 p_sys->p_blend = NULL;
205 p_sys->p_text = NULL;
206 p_sys->p_scale = NULL;
207 p_sys->p_scale_yuvp = NULL;
209 p_sys->i_margin = var_InheritInteger( p_spu, "sub-margin" );
211 /* Register the default subpicture channel */
212 p_sys->i_channel = SPU_DEFAULT_CHANNEL + 1;
214 p_sys->psz_chain_update = NULL;
215 vlc_mutex_init( &p_sys->chain_lock );
216 p_sys->p_chain = filter_chain_New( p_spu, "sub filter", false,
217 SubFilterAllocationInit,
218 SubFilterAllocationClean,
221 /* Load text and scale module */
222 SpuRenderCreateAndLoadText( p_spu );
223 SpuRenderCreateAndLoadScale( p_spu );
226 p_sys->i_last_sort_date = -1;
232 * Destroy the subpicture unit
234 * \param p_this the parent object which destroys the subpicture unit
236 void spu_Destroy( spu_t *p_spu )
238 spu_private_t *p_sys = p_spu->p;
241 filter_DeleteBlend( p_sys->p_blend );
244 FilterRelease( p_sys->p_text );
246 if( p_sys->p_scale_yuvp )
247 FilterRelease( p_sys->p_scale_yuvp );
250 FilterRelease( p_sys->p_scale );
252 filter_chain_Delete( p_sys->p_chain );
253 vlc_mutex_destroy( &p_sys->chain_lock );
254 free( p_sys->psz_chain_update );
256 /* Destroy all remaining subpictures */
257 SpuHeapClean( &p_sys->heap );
259 vlc_mutex_destroy( &p_sys->lock );
261 vlc_object_release( p_spu );
265 * Attach/Detach the SPU from any input
267 * \param p_this the object in which to destroy the subpicture unit
268 * \param b_attach to select attach or detach
270 void spu_Attach( spu_t *p_spu, vlc_object_t *p_input, bool b_attach )
274 UpdateSPU( p_spu, p_input );
275 var_Create( p_input, "highlight", VLC_VAR_BOOL );
276 var_AddCallback( p_input, "highlight", CropCallback, p_spu );
278 vlc_mutex_lock( &p_spu->p->lock );
279 p_spu->p->p_input = p_input;
281 FilterRelease( p_spu->p->p_text );
282 p_spu->p->p_text = NULL;
283 SpuRenderCreateAndLoadText( p_spu );
285 vlc_mutex_unlock( &p_spu->p->lock );
289 vlc_mutex_lock( &p_spu->p->lock );
290 p_spu->p->p_input = NULL;
291 vlc_mutex_unlock( &p_spu->p->lock );
293 /* Delete callbacks */
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 subpicture_Update( 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 void spu_ChangeMargin( spu_t *p_spu, int i_margin )
753 spu_private_t *p_sys = p_spu->p;
755 vlc_mutex_lock( &p_sys->lock );
756 p_sys->i_margin = i_margin;
757 vlc_mutex_unlock( &p_sys->lock );
761 static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic )
763 p_subpic->p_next = *pp_head;
768 /*****************************************************************************
770 *****************************************************************************/
771 static void SpuHeapInit( spu_heap_t *p_heap )
773 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
775 spu_heap_entry_t *e = &p_heap->p_entry[i];
777 e->p_subpicture = NULL;
782 static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
784 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
786 spu_heap_entry_t *e = &p_heap->p_entry[i];
788 if( e->p_subpicture )
791 e->p_subpicture = p_subpic;
798 static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
800 spu_heap_entry_t *e = &p_heap->p_entry[i_index];
802 if( e->p_subpicture )
803 subpicture_Delete( e->p_subpicture );
805 e->p_subpicture = NULL;
808 static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
810 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
812 spu_heap_entry_t *e = &p_heap->p_entry[i];
814 if( e->p_subpicture != p_subpic )
817 SpuHeapDeleteAt( p_heap, i );
823 static void SpuHeapClean( spu_heap_t *p_heap )
825 for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
827 spu_heap_entry_t *e = &p_heap->p_entry[i];
828 if( e->p_subpicture )
829 subpicture_Delete( e->p_subpicture );
833 static void FilterRelease( filter_t *p_filter )
835 if( p_filter->p_module )
836 module_unneed( p_filter, p_filter->p_module );
837 if( p_filter->p_owner )
838 free( p_filter->p_owner );
840 vlc_object_release( p_filter );
843 static void SpuRenderCreateAndLoadText( spu_t *p_spu )
847 assert( !p_spu->p->p_text );
850 p_text = vlc_custom_create( p_spu, sizeof(filter_t),
851 VLC_OBJECT_GENERIC, "spu text" );
855 p_text->p_owner = xmalloc( sizeof(*p_text->p_owner) );
856 p_text->p_owner->p_spu = p_spu;
858 es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
860 es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
861 p_text->fmt_out.video.i_width =
862 p_text->fmt_out.video.i_visible_width = 32;
863 p_text->fmt_out.video.i_height =
864 p_text->fmt_out.video.i_visible_height = 32;
866 p_text->pf_get_attachments = spu_get_attachments;
868 vlc_object_attach( p_text, p_spu );
870 /* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
871 * same than these 2 calls ? */
872 char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
873 if( psz_modulename && *psz_modulename )
875 p_text->p_module = module_need( p_text, "text renderer",
876 psz_modulename, true );
878 free( psz_modulename );
880 if( !p_text->p_module )
881 p_text->p_module = module_need( p_text, "text renderer", NULL, false );
883 /* Create a few variables used for enhanced text rendering */
884 var_Create( p_text, "spu-duration", VLC_VAR_TIME );
885 var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
886 var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
887 var_Create( p_text, "scale", VLC_VAR_INTEGER );
890 static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
891 vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
896 p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
897 VLC_OBJECT_GENERIC, "scale" );
901 es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
902 p_scale->fmt_in.video.i_chroma = i_src_chroma;
903 p_scale->fmt_in.video.i_width =
904 p_scale->fmt_in.video.i_height = 32;
906 es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
907 p_scale->fmt_out.video.i_chroma = i_dst_chroma;
908 p_scale->fmt_out.video.i_width =
909 p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
911 p_scale->pf_video_buffer_new = spu_new_video_buffer;
912 p_scale->pf_video_buffer_del = spu_del_video_buffer;
914 vlc_object_attach( p_scale, p_obj );
915 p_scale->p_module = module_need( p_scale, "video filter2", NULL, false );
919 static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
921 assert( !p_spu->p->p_scale );
922 assert( !p_spu->p->p_scale_yuvp );
923 /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
925 p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
926 VLC_CODEC_YUVA, VLC_CODEC_YUVA, true );
927 /* This one is used for YUVP to YUVA/RGBA without scaling
929 p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
930 VLC_CODEC_YUVP, VLC_CODEC_YUVA, false );
933 static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
934 subpicture_t *p_subpic, subpicture_region_t *p_region,
935 int i_min_scale_ratio, mtime_t render_date )
937 filter_t *p_text = p_spu->p->p_text;
939 assert( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
941 if( !p_text || !p_text->p_module )
944 /* Setup 3 variables which can be used to render
945 * time-dependent text (and effects). The first indicates
946 * the total amount of time the text will be on screen,
947 * the second the amount of time it has already been on
948 * screen (can be a negative value as text is layed out
949 * before it is rendered) and the third is a feedback
950 * variable from the renderer - if the renderer sets it
951 * then this particular text is time-dependent, eg. the
952 * visual progress bar inside the text in karaoke and the
953 * text needs to be rendered multiple times in order for
954 * the effect to work - we therefore need to return the
955 * region to its original state at the end of the loop,
956 * instead of leaving it in YUVA or YUVP.
957 * Any renderer which is unaware of how to render
958 * time-dependent text can happily ignore the variables
959 * and render the text the same as usual - it should at
960 * least show up on screen, but the effect won't change
961 * the text over time.
963 var_SetTime( p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
964 var_SetTime( p_text, "spu-elapsed", render_date );
965 var_SetBool( p_text, "text-rerender", false );
966 var_SetInteger( p_text, "scale", i_min_scale_ratio );
968 if( p_text->pf_render_html && p_region->psz_html )
970 p_text->pf_render_html( p_text, p_region, p_region );
972 else if( p_text->pf_render_text )
974 p_text->pf_render_text( p_text, p_region, p_region );
976 *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
979 p_region->i_align |= SUBPICTURE_RENDERED;
983 * A few scale functions helpers.
985 static spu_scale_t spu_scale_create( int w, int h )
987 spu_scale_t s = { .w = w, .h = h };
994 static spu_scale_t spu_scale_unit( void )
996 return spu_scale_create( SCALE_UNIT, SCALE_UNIT );
998 static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd )
1000 return spu_scale_create( wn * SCALE_UNIT / wd,
1001 hn * SCALE_UNIT / hd );
1003 static int spu_scale_w( int v, const spu_scale_t s )
1005 return v * s.w / SCALE_UNIT;
1007 static int spu_scale_h( int v, const spu_scale_t s )
1009 return v * s.h / SCALE_UNIT;
1011 static int spu_invscale_w( int v, const spu_scale_t s )
1013 return v * SCALE_UNIT / s.w;
1015 static int spu_invscale_h( int v, const spu_scale_t s )
1017 return v * SCALE_UNIT / s.h;
1021 * A few area functions helpers
1023 static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t s )
1025 spu_area_t a = { .i_x = x, .i_y = y, .i_width = w, .i_height = h, .scale = s };
1028 static spu_area_t spu_area_scaled( spu_area_t a )
1030 if( a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT )
1033 a.i_x = spu_scale_w( a.i_x, a.scale );
1034 a.i_y = spu_scale_h( a.i_y, a.scale );
1036 a.i_width = spu_scale_w( a.i_width, a.scale );
1037 a.i_height = spu_scale_h( a.i_height, a.scale );
1039 a.scale = spu_scale_unit();
1042 static spu_area_t spu_area_unscaled( spu_area_t a, spu_scale_t s )
1044 if( a.scale.w == s.w && a.scale.h == s.h )
1047 a = spu_area_scaled( a );
1049 a.i_x = spu_invscale_w( a.i_x, s );
1050 a.i_y = spu_invscale_h( a.i_y, s );
1052 a.i_width = spu_invscale_w( a.i_width, s );
1053 a.i_height = spu_invscale_h( a.i_height, s );
1058 static bool spu_area_overlap( spu_area_t a, spu_area_t b )
1063 a = spu_area_scaled( a );
1064 b = spu_area_scaled( b );
1066 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 ) &&
1067 __MAX( a.i_y-i_dy, b.i_y ) < __MIN( a.i_y+a.i_height+i_dy, b.i_y+b.i_height );
1071 * Avoid area overlapping
1073 static void SpuAreaFixOverlap( spu_area_t *p_dst,
1074 const spu_area_t *p_sub, int i_sub, int i_align )
1076 spu_area_t a = spu_area_scaled( *p_dst );
1077 bool b_moved = false;
1080 /* Check for overlap
1081 * XXX It is not fast O(n^2) but we should not have a lot of region */
1085 for( int i = 0; i < i_sub; i++ )
1087 spu_area_t sub = spu_area_scaled( p_sub[i] );
1089 if( !spu_area_overlap( a, sub ) )
1092 if( i_align & SUBPICTURE_ALIGN_TOP )
1095 int i_y = sub.i_y + sub.i_height;
1099 else if( i_align & SUBPICTURE_ALIGN_BOTTOM )
1102 int i_y = sub.i_y - a.i_height;
1108 /* TODO what to do in this case? */
1109 //fprintf( stderr, "Overlap with unsupported alignment\n" );
1119 *p_dst = spu_area_unscaled( a, p_dst->scale );
1123 static void SpuAreaFitInside( spu_area_t *p_area, const spu_area_t *p_boundary )
1125 spu_area_t a = spu_area_scaled( *p_area );
1127 const int i_error_x = (a.i_x + a.i_width) - p_boundary->i_width;
1133 const int i_error_y = (a.i_y + a.i_height) - p_boundary->i_height;
1139 *p_area = spu_area_unscaled( a, p_area->scale );
1145 static void SpuRegionPlace( int *pi_x, int *pi_y,
1146 const subpicture_t *p_subpic,
1147 const subpicture_region_t *p_region )
1149 const int i_delta_x = p_region->i_x;
1150 const int i_delta_y = p_region->i_y;
1153 assert( p_region->i_x != INT_MAX && p_region->i_y != INT_MAX );
1154 if( p_region->i_align & SUBPICTURE_ALIGN_TOP )
1158 else if( p_region->i_align & SUBPICTURE_ALIGN_BOTTOM )
1160 i_y = p_subpic->i_original_picture_height - p_region->fmt.i_height - i_delta_y;
1164 i_y = p_subpic->i_original_picture_height / 2 - p_region->fmt.i_height / 2;
1167 if( p_region->i_align & SUBPICTURE_ALIGN_LEFT )
1171 else if( p_region->i_align & SUBPICTURE_ALIGN_RIGHT )
1173 i_x = p_subpic->i_original_picture_width - p_region->fmt.i_width - i_delta_x;
1177 i_x = p_subpic->i_original_picture_width / 2 - p_region->fmt.i_width / 2;
1180 if( p_subpic->b_absolute )
1191 * This function computes the current alpha value for a given region.
1193 static int SpuRegionAlpha( subpicture_t *p_subpic, subpicture_region_t *p_region )
1195 /* Compute alpha blend value */
1196 int i_fade_alpha = 255;
1197 if( p_subpic->b_fade )
1199 mtime_t i_fade_start = ( p_subpic->i_stop +
1200 p_subpic->i_start ) / 2;
1201 mtime_t i_now = mdate();
1203 if( i_now >= i_fade_start && p_subpic->i_stop > i_fade_start )
1205 i_fade_alpha = 255 * ( p_subpic->i_stop - i_now ) /
1206 ( p_subpic->i_stop - i_fade_start );
1209 return i_fade_alpha * p_subpic->i_alpha * p_region->i_alpha / 65025;
1213 * It will render the provided region onto p_pic_dst.
1216 static void SpuRenderRegion( spu_t *p_spu,
1217 picture_t *p_pic_dst, spu_area_t *p_area,
1218 subpicture_t *p_subpic, subpicture_region_t *p_region,
1219 const spu_scale_t scale_size,
1220 const video_format_t *p_fmt,
1221 const spu_area_t *p_subtitle_area, int i_subtitle_area,
1222 mtime_t render_date )
1224 spu_private_t *p_sys = p_spu->p;
1226 video_format_t fmt_original = p_region->fmt;
1227 bool b_rerender_text = false;
1228 bool b_restore_format = false;
1232 video_format_t region_fmt;
1233 picture_t *p_region_picture;
1235 /* Invalidate area by default */
1236 *p_area = spu_area_create( 0,0, 0,0, scale_size );
1238 /* Render text region */
1239 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1241 const int i_min_scale_ratio = SCALE_UNIT; /* FIXME what is the right value? (scale_size is not) */
1242 SpuRenderText( p_spu, &b_rerender_text, p_subpic, p_region,
1243 i_min_scale_ratio, render_date );
1244 b_restore_format = b_rerender_text;
1246 /* Check if the rendering has failed ... */
1247 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
1251 /* Force palette if requested
1252 * FIXME b_force_palette and b_force_crop are applied to all subpictures using palette
1253 * instead of only the right one (being the dvd spu).
1255 const bool b_using_palette = p_region->fmt.i_chroma == VLC_CODEC_YUVP;
1256 const bool b_force_palette = b_using_palette && p_sys->b_force_palette;
1257 const bool b_force_crop = b_force_palette && p_sys->b_force_crop;
1258 bool b_changed_palette = false;
1261 /* Compute the margin which is expressed in destination pixel unit
1262 * The margin is applied only to subtitle and when no forced crop is
1263 * requested (dvd menu) */
1265 if( !b_force_crop && p_subpic->b_subtitle )
1266 i_margin_y = spu_invscale_h( p_sys->i_margin, scale_size );
1268 /* Place the picture
1269 * We compute the position in the rendered size */
1270 SpuRegionPlace( &i_x_offset, &i_y_offset,
1271 p_subpic, p_region );
1273 /* Save this position for subtitle overlap support
1274 * it is really important that there are given without scale_size applied */
1275 *p_area = spu_area_create( i_x_offset, i_y_offset,
1276 p_region->fmt.i_width, p_region->fmt.i_height,
1279 /* Handle overlapping subtitles when possible */
1280 if( p_subpic->b_subtitle && !p_subpic->b_absolute )
1282 SpuAreaFixOverlap( p_area, p_subtitle_area, i_subtitle_area,
1283 p_region->i_align );
1286 /* we copy the area: for the subtitle overlap support we want
1287 * to only save the area without margin applied */
1288 spu_area_t restrained = *p_area;
1290 /* apply margin to subtitles and correct if they go over the picture edge */
1291 if( p_subpic->b_subtitle )
1292 restrained.i_y -= i_margin_y;
1294 spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
1296 SpuAreaFitInside( &restrained, &display );
1298 /* Fix the position for the current scale_size */
1299 i_x_offset = spu_scale_w( restrained.i_x, restrained.scale );
1300 i_y_offset = spu_scale_h( restrained.i_y, restrained.scale );
1303 if( b_force_palette )
1305 video_palette_t *p_palette = p_region->fmt.p_palette;
1306 video_palette_t palette;
1308 /* We suppose DVD palette here */
1309 palette.i_entries = 4;
1310 for( int i = 0; i < 4; i++ )
1311 for( int j = 0; j < 4; j++ )
1312 palette.palette[i][j] = p_sys->palette[i][j];
1314 if( p_palette->i_entries == palette.i_entries )
1316 for( int i = 0; i < p_palette->i_entries; i++ )
1317 for( int j = 0; j < 4; j++ )
1318 b_changed_palette |= p_palette->palette[i][j] != palette.palette[i][j];
1322 b_changed_palette = true;
1324 *p_palette = palette;
1328 region_fmt = p_region->fmt;
1329 p_region_picture = p_region->p_picture;
1332 /* Scale from rendered size to destination size */
1333 if( p_sys->p_scale && p_sys->p_scale->p_module &&
1334 ( !b_using_palette || ( p_sys->p_scale_yuvp && p_sys->p_scale_yuvp->p_module ) ) &&
1335 ( scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT || b_using_palette ) )
1337 const unsigned i_dst_width = spu_scale_w( p_region->fmt.i_width, scale_size );
1338 const unsigned i_dst_height = spu_scale_h( p_region->fmt.i_height, scale_size );
1340 /* Destroy the cache if unusable */
1341 if( p_region->p_private )
1343 subpicture_region_private_t *p_private = p_region->p_private;
1344 bool b_changed = false;
1346 /* Check resize changes */
1347 if( i_dst_width != p_private->fmt.i_width ||
1348 i_dst_height != p_private->fmt.i_height )
1351 /* Check forced palette changes */
1352 if( b_changed_palette )
1357 subpicture_region_private_Delete( p_private );
1358 p_region->p_private = NULL;
1362 /* Scale if needed into cache */
1363 if( !p_region->p_private && i_dst_width > 0 && i_dst_height > 0 )
1365 filter_t *p_scale = p_sys->p_scale;
1367 picture_t *p_picture = p_region->p_picture;
1368 picture_Hold( p_picture );
1370 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
1371 if( b_using_palette )
1373 filter_t *p_scale_yuvp = p_sys->p_scale_yuvp;
1375 p_scale_yuvp->fmt_in.video = p_region->fmt;
1377 /* TODO converting to RGBA for RGB video output is better */
1378 p_scale_yuvp->fmt_out.video = p_region->fmt;
1379 p_scale_yuvp->fmt_out.video.i_chroma = VLC_CODEC_YUVA;
1381 p_picture = p_scale_yuvp->pf_video_filter( p_scale_yuvp, p_picture );
1384 /* Well we will try conversion+scaling */
1385 msg_Warn( p_spu, "%4.4s to %4.4s conversion failed",
1386 (const char*)&p_scale_yuvp->fmt_in.video.i_chroma,
1387 (const char*)&p_scale_yuvp->fmt_out.video.i_chroma );
1391 /* Conversion(except from YUVP)/Scaling */
1393 ( p_picture->format.i_width != i_dst_width ||
1394 p_picture->format.i_height != i_dst_height ) )
1396 p_scale->fmt_in.video = p_picture->format;
1397 p_scale->fmt_out.video = p_picture->format;
1399 p_scale->fmt_out.video.i_width = i_dst_width;
1400 p_scale->fmt_out.video.i_height = i_dst_height;
1402 p_scale->fmt_out.video.i_visible_width =
1403 spu_scale_w( p_region->fmt.i_visible_width, scale_size );
1404 p_scale->fmt_out.video.i_visible_height =
1405 spu_scale_h( p_region->fmt.i_visible_height, scale_size );
1407 p_picture = p_scale->pf_video_filter( p_scale, p_picture );
1409 msg_Err( p_spu, "scaling failed" );
1415 p_region->p_private = subpicture_region_private_New( &p_picture->format );
1416 if( p_region->p_private )
1418 p_region->p_private->p_picture = p_picture;
1419 if( !p_region->p_private->p_picture )
1421 subpicture_region_private_Delete( p_region->p_private );
1422 p_region->p_private = NULL;
1427 picture_Release( p_picture );
1432 /* And use the scaled picture */
1433 if( p_region->p_private )
1435 region_fmt = p_region->p_private->fmt;
1436 p_region_picture = p_region->p_private->p_picture;
1440 /* Force cropping if requested */
1443 int i_crop_x = spu_scale_w( p_sys->i_crop_x, scale_size );
1444 int i_crop_y = spu_scale_h( p_sys->i_crop_y, scale_size );
1445 int i_crop_width = spu_scale_w( p_sys->i_crop_width, scale_size );
1446 int i_crop_height= spu_scale_h( p_sys->i_crop_height,scale_size );
1448 /* Find the intersection */
1449 if( i_crop_x + i_crop_width <= i_x_offset ||
1450 i_x_offset + (int)region_fmt.i_visible_width < i_crop_x ||
1451 i_crop_y + i_crop_height <= i_y_offset ||
1452 i_y_offset + (int)region_fmt.i_visible_height < i_crop_y )
1454 /* No intersection */
1455 region_fmt.i_visible_width =
1456 region_fmt.i_visible_height = 0;
1460 int i_x, i_y, i_x_end, i_y_end;
1461 i_x = __MAX( i_crop_x, i_x_offset );
1462 i_y = __MAX( i_crop_y, i_y_offset );
1463 i_x_end = __MIN( i_crop_x + i_crop_width,
1464 i_x_offset + (int)region_fmt.i_visible_width );
1465 i_y_end = __MIN( i_crop_y + i_crop_height,
1466 i_y_offset + (int)region_fmt.i_visible_height );
1468 region_fmt.i_x_offset = i_x - i_x_offset;
1469 region_fmt.i_y_offset = i_y - i_y_offset;
1470 region_fmt.i_visible_width = i_x_end - i_x;
1471 region_fmt.i_visible_height = i_y_end - i_y;
1473 i_x_offset = __MAX( i_x, 0 );
1474 i_y_offset = __MAX( i_y, 0 );
1478 /* Update the blender */
1479 if( filter_ConfigureBlend( p_spu->p->p_blend,
1480 p_fmt->i_width, p_fmt->i_height,
1482 filter_Blend( p_spu->p->p_blend,
1483 p_pic_dst, i_x_offset, i_y_offset,
1484 p_region_picture, SpuRegionAlpha( p_subpic, p_region ) ) )
1486 msg_Err( p_spu, "blending %4.4s to %4.4s failed",
1487 (char *)&p_sys->p_blend->fmt_in.video.i_chroma,
1488 (char *)&p_sys->p_blend->fmt_out.video.i_chroma );
1492 if( b_rerender_text )
1494 /* Some forms of subtitles need to be re-rendered more than
1495 * once, eg. karaoke. We therefore restore the region to its
1496 * pre-rendered state, so the next time through everything is
1499 if( p_region->p_picture )
1501 picture_Release( p_region->p_picture );
1502 p_region->p_picture = NULL;
1504 if( p_region->p_private )
1506 subpicture_region_private_Delete( p_region->p_private );
1507 p_region->p_private = NULL;
1509 p_region->i_align &= ~SUBPICTURE_RENDERED;
1511 if( b_restore_format )
1512 p_region->fmt = fmt_original;
1516 * This function compares two 64 bits integers.
1517 * It can be used by qsort.
1519 static int IntegerCmp( int64_t i0, int64_t i1 )
1521 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
1524 * This function compares 2 subpictures using the following properties
1525 * (ordered by priority)
1526 * 1. absolute positionning
1528 * 3. creation order (per channel)
1530 * It can be used by qsort.
1532 * XXX spu_RenderSubpictures depends heavily on this order.
1534 static int SubpictureCmp( const void *s0, const void *s1 )
1536 subpicture_t *p_subpic0 = *(subpicture_t**)s0;
1537 subpicture_t *p_subpic1 = *(subpicture_t**)s1;
1540 r = IntegerCmp( !p_subpic0->b_absolute, !p_subpic1->b_absolute );
1542 r = IntegerCmp( p_subpic0->i_start, p_subpic1->i_start );
1544 r = IntegerCmp( p_subpic0->i_channel, p_subpic1->i_channel );
1546 r = IntegerCmp( p_subpic0->i_order, p_subpic1->i_order );
1550 /*****************************************************************************
1551 * Object variables callbacks
1552 *****************************************************************************/
1554 /*****************************************************************************
1555 * UpdateSPU: update subpicture settings
1556 *****************************************************************************
1557 * This function is called from CropCallback and at initialization time, to
1558 * retrieve crop information from the input.
1559 *****************************************************************************/
1560 static void UpdateSPU( spu_t *p_spu, vlc_object_t *p_object )
1562 spu_private_t *p_sys = p_spu->p;
1565 vlc_mutex_lock( &p_sys->lock );
1567 p_sys->b_force_palette = false;
1568 p_sys->b_force_crop = false;
1570 if( var_Get( p_object, "highlight", &val ) || !val.b_bool )
1572 vlc_mutex_unlock( &p_sys->lock );
1576 p_sys->b_force_crop = true;
1577 p_sys->i_crop_x = var_GetInteger( p_object, "x-start" );
1578 p_sys->i_crop_y = var_GetInteger( p_object, "y-start" );
1579 p_sys->i_crop_width = var_GetInteger( p_object, "x-end" ) - p_sys->i_crop_x;
1580 p_sys->i_crop_height = var_GetInteger( p_object, "y-end" ) - p_sys->i_crop_y;
1582 if( var_Get( p_object, "menu-palette", &val ) == VLC_SUCCESS )
1584 memcpy( p_sys->palette, val.p_address, 16 );
1585 p_sys->b_force_palette = true;
1587 vlc_mutex_unlock( &p_sys->lock );
1589 msg_Dbg( p_object, "crop: %i,%i,%i,%i, palette forced: %i",
1590 p_sys->i_crop_x, p_sys->i_crop_y,
1591 p_sys->i_crop_width, p_sys->i_crop_height,
1592 p_sys->b_force_palette );
1595 /*****************************************************************************
1596 * CropCallback: called when the highlight properties are changed
1597 *****************************************************************************
1598 * This callback is called from the input thread when we need cropping
1599 *****************************************************************************/
1600 static int CropCallback( vlc_object_t *p_object, char const *psz_var,
1601 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1603 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);
1605 UpdateSPU( (spu_t *)p_data, p_object );
1609 /*****************************************************************************
1610 * Buffers allocation callbacks for the filters
1611 *****************************************************************************/
1612 static int spu_get_attachments( filter_t *p_filter,
1613 input_attachment_t ***ppp_attachment,
1614 int *pi_attachment )
1616 spu_t *p_spu = p_filter->p_owner->p_spu;
1618 int i_ret = VLC_EGENERIC;
1619 if( p_spu->p->p_input )
1620 i_ret = input_Control( (input_thread_t*)p_spu->p->p_input,
1621 INPUT_GET_ATTACHMENTS,
1622 ppp_attachment, pi_attachment );
1626 static subpicture_t *sub_new_buffer( filter_t *p_filter )
1628 filter_owner_sys_t *p_sys = p_filter->p_owner;
1630 subpicture_t *p_subpicture = subpicture_New( NULL );
1632 p_subpicture->i_channel = p_sys->i_channel;
1633 return p_subpicture;
1635 static void sub_del_buffer( filter_t *p_filter, subpicture_t *p_subpic )
1637 VLC_UNUSED( p_filter );
1638 subpicture_Delete( p_subpic );
1640 static picture_t *spu_new_video_buffer( filter_t *p_filter )
1642 const video_format_t *p_fmt = &p_filter->fmt_out.video;
1644 VLC_UNUSED(p_filter);
1645 return picture_NewFromFormat( p_fmt );
1647 static void spu_del_video_buffer( filter_t *p_filter, picture_t *p_picture )
1649 VLC_UNUSED(p_filter);
1650 picture_Release( p_picture );
1653 static int SubFilterAllocationInit( filter_t *p_filter, void *p_data )
1655 spu_t *p_spu = p_data;
1657 filter_owner_sys_t *p_sys = malloc( sizeof(filter_owner_sys_t) );
1659 return VLC_EGENERIC;
1661 p_filter->pf_sub_buffer_new = sub_new_buffer;
1662 p_filter->pf_sub_buffer_del = sub_del_buffer;
1664 p_filter->p_owner = p_sys;
1665 p_sys->i_channel = spu_RegisterChannel( p_spu );
1666 p_sys->p_spu = p_spu;
1671 static void SubFilterAllocationClean( filter_t *p_filter )
1673 filter_owner_sys_t *p_sys = p_filter->p_owner;
1675 spu_ClearChannel( p_sys->p_spu, p_sys->i_channel );
1676 free( p_filter->p_owner );