+/* */
+void subpicture_region_Delete( subpicture_region_t *p_region )
+{
+ if( !p_region )
+ return;
+
+ if( p_region->p_private )
+ SpuRegionPrivateDelete( p_region->p_private );
+
+ if( p_region->p_picture )
+ picture_Release( p_region->p_picture );
+
+ free( p_region->fmt.p_palette );
+
+ free( p_region->psz_text );
+ free( p_region->psz_html );
+ //free( p_region->p_style ); FIXME --fenrir plugin does not allocate the memory for it. I think it might lead to segfault, video renderer can live longer than the decoder
+ free( p_region );
+}
+
+/* */
+void subpicture_region_ChainDelete( subpicture_region_t *p_head )
+{
+ while( p_head )
+ {
+ subpicture_region_t *p_next = p_head->p_next;
+
+ subpicture_region_Delete( p_head );
+
+ p_head = p_next;
+ }
+}
+
+
+
+/*****************************************************************************
+ * heap managment
+ *****************************************************************************/
+static void SpuHeapInit( spu_heap_t *p_heap )
+{
+ for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
+ {
+ spu_heap_entry_t *e = &p_heap->p_entry[i];
+
+ e->p_subpicture = NULL;
+ e->b_reject = false;
+ }
+}
+
+static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
+{
+ for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
+ {
+ spu_heap_entry_t *e = &p_heap->p_entry[i];
+
+ if( e->p_subpicture )
+ continue;
+
+ e->p_subpicture = p_subpic;
+ e->b_reject = false;
+ return VLC_SUCCESS;
+ }
+ return VLC_EGENERIC;
+}
+
+static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
+{
+ spu_heap_entry_t *e = &p_heap->p_entry[i_index];
+
+ if( e->p_subpicture )
+ subpicture_Delete( e->p_subpicture );
+
+ e->p_subpicture = NULL;
+}
+
+static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
+{
+ for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
+ {
+ spu_heap_entry_t *e = &p_heap->p_entry[i];
+
+ if( e->p_subpicture != p_subpic )
+ continue;
+
+ SpuHeapDeleteAt( p_heap, i );
+ return VLC_SUCCESS;
+ }
+ return VLC_EGENERIC;
+}
+
+static void SpuHeapClean( spu_heap_t *p_heap )
+{
+ for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
+ {
+ spu_heap_entry_t *e = &p_heap->p_entry[i];
+ if( e->p_subpicture )
+ subpicture_Delete( e->p_subpicture );
+ }
+}
+
+static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t *p_fmt )
+{
+ subpicture_region_private_t *p_private = malloc( sizeof(*p_private) );
+
+ if( !p_private )
+ return NULL;
+
+ p_private->fmt = *p_fmt;
+ if( p_fmt->p_palette )
+ {
+ p_private->fmt.p_palette = malloc( sizeof(*p_private->fmt.p_palette) );
+ if( p_private->fmt.p_palette )
+ *p_private->fmt.p_palette = *p_fmt->p_palette;
+ }
+ p_private->p_picture = NULL;
+
+ return p_private;
+}
+static void SpuRegionPrivateDelete( subpicture_region_private_t *p_private )
+{
+ if( p_private->p_picture )
+ picture_Release( p_private->p_picture );
+ free( p_private->fmt.p_palette );
+ free( p_private );
+}
+
+static void FilterRelease( filter_t *p_filter )
+{
+ if( p_filter->p_module )
+ module_unneed( p_filter, p_filter->p_module );
+
+ vlc_object_detach( p_filter );
+ vlc_object_release( p_filter );
+}
+
+static void SpuRenderCreateBlend( spu_t *p_spu, vlc_fourcc_t i_chroma, int i_aspect )
+{
+ filter_t *p_blend;
+
+ assert( !p_spu->p->p_blend );
+
+ p_spu->p->p_blend =
+ p_blend = vlc_custom_create( p_spu, sizeof(filter_t),
+ VLC_OBJECT_GENERIC, "blend" );
+ if( !p_blend )
+ return;
+
+ es_format_Init( &p_blend->fmt_in, VIDEO_ES, 0 );
+
+ es_format_Init( &p_blend->fmt_out, VIDEO_ES, 0 );
+ p_blend->fmt_out.video.i_x_offset = 0;
+ p_blend->fmt_out.video.i_y_offset = 0;
+ p_blend->fmt_out.video.i_chroma = i_chroma;
+ p_blend->fmt_out.video.i_aspect = i_aspect;
+
+ /* The blend module will be loaded when needed with the real
+ * input format */
+ p_blend->p_module = NULL;
+
+ /* */
+ vlc_object_attach( p_blend, p_spu );
+}
+static void SpuRenderUpdateBlend( spu_t *p_spu, int i_out_width, int i_out_height,
+ const video_format_t *p_in_fmt )
+{
+ filter_t *p_blend = p_spu->p->p_blend;
+
+ assert( p_blend );
+
+ /* */
+ if( p_blend->p_module && p_blend->fmt_in.video.i_chroma != p_in_fmt->i_chroma )
+ {
+ /* The chroma is not the same, we need to reload the blend module
+ * XXX to match the old behaviour just test !p_blend->fmt_in.video.i_chroma */
+ module_unneed( p_blend, p_blend->p_module );
+ p_blend->p_module = NULL;
+ }
+
+ /* */
+ p_blend->fmt_in.video = *p_in_fmt;
+
+ /* */
+ p_blend->fmt_out.video.i_width =
+ p_blend->fmt_out.video.i_visible_width = i_out_width;
+ p_blend->fmt_out.video.i_height =
+ p_blend->fmt_out.video.i_visible_height = i_out_height;
+
+ /* */
+ if( !p_blend->p_module )
+ p_blend->p_module = module_need( p_blend, "video blending", 0, 0 );
+}
+static void SpuRenderCreateAndLoadText( spu_t *p_spu )
+{
+ filter_t *p_text;
+
+ assert( !p_spu->p->p_text );
+
+ p_spu->p->p_text =
+ p_text = vlc_custom_create( p_spu, sizeof(filter_t),
+ VLC_OBJECT_GENERIC, "spu text" );
+ if( !p_text )
+ return;
+
+ es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
+
+ es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
+ p_text->fmt_out.video.i_width =
+ p_text->fmt_out.video.i_visible_width = 32;
+ p_text->fmt_out.video.i_height =
+ p_text->fmt_out.video.i_visible_height = 32;
+
+ p_text->pf_sub_buffer_new = spu_new_buffer;
+ p_text->pf_sub_buffer_del = spu_del_buffer;
+
+ vlc_object_attach( p_text, p_spu );
+
+ /* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
+ * same than these 2 calls ? */
+ char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
+ if( psz_modulename && *psz_modulename )
+ {
+ p_text->p_module = module_need( p_text, "text renderer",
+ psz_modulename, true );
+ }
+ free( psz_modulename );
+
+ if( !p_text->p_module )
+ p_text->p_module = module_need( p_text, "text renderer", NULL, false );
+
+ /* Create a few variables used for enhanced text rendering */
+ var_Create( p_text, "spu-duration", VLC_VAR_TIME );
+ var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
+ var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
+ var_Create( p_text, "scale", VLC_VAR_INTEGER );
+}
+
+static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
+ vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
+ bool b_resize )
+{
+ filter_t *p_scale;
+
+ p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
+ VLC_OBJECT_GENERIC, "scale" );
+ if( !p_scale )
+ return NULL;
+
+ es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
+ p_scale->fmt_in.video.i_chroma = i_src_chroma;
+ p_scale->fmt_in.video.i_width =
+ p_scale->fmt_in.video.i_height = 32;
+
+ es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
+ p_scale->fmt_out.video.i_chroma = i_dst_chroma;
+ p_scale->fmt_out.video.i_width =
+ p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
+
+ p_scale->pf_vout_buffer_new = spu_new_video_buffer;
+ p_scale->pf_vout_buffer_del = spu_del_video_buffer;
+
+ vlc_object_attach( p_scale, p_obj );
+ p_scale->p_module = module_need( p_scale, "video filter2", 0, 0 );
+
+ return p_scale;
+}
+static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
+{
+ assert( !p_spu->p->p_scale );
+ assert( !p_spu->p->p_scale_yuvp );
+ /* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
+ * yuva/rgba */
+ p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
+ VLC_FOURCC_YUVA, VLC_FOURCC_YUVA, true );
+ /* This one is used for YUVP to YUVA/RGBA without scaling
+ * FIXME rename it */
+ p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
+ VLC_FOURCC_YUVP, VLC_FOURCC_YUVA, false );
+}
+
+static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
+ subpicture_t *p_subpic, subpicture_region_t *p_region,
+ int i_min_scale_ratio )
+{
+ filter_t *p_text = p_spu->p->p_text;
+
+ assert( p_region->fmt.i_chroma == VLC_FOURCC_TEXT );
+
+ if( !p_text || !p_text->p_module )
+ goto exit;
+
+ /* Setup 3 variables which can be used to render
+ * time-dependent text (and effects). The first indicates
+ * the total amount of time the text will be on screen,
+ * the second the amount of time it has already been on
+ * screen (can be a negative value as text is layed out
+ * before it is rendered) and the third is a feedback
+ * variable from the renderer - if the renderer sets it
+ * then this particular text is time-dependent, eg. the
+ * visual progress bar inside the text in karaoke and the
+ * text needs to be rendered multiple times in order for
+ * the effect to work - we therefore need to return the
+ * region to its original state at the end of the loop,
+ * instead of leaving it in YUVA or YUVP.
+ * Any renderer which is unaware of how to render
+ * time-dependent text can happily ignore the variables
+ * and render the text the same as usual - it should at
+ * least show up on screen, but the effect won't change
+ * the text over time.
+ */
+ var_SetTime( p_text, "spu-duration", p_subpic->i_stop - p_subpic->i_start );
+ var_SetTime( p_text, "spu-elapsed", mdate() - p_subpic->i_start );
+ var_SetBool( p_text, "text-rerender", false );
+ var_SetInteger( p_text, "scale", i_min_scale_ratio );
+
+ if( p_text->pf_render_html && p_region->psz_html )
+ {
+ p_text->pf_render_html( p_text, p_region, p_region );
+ }
+ else if( p_text->pf_render_text )
+ {
+ p_text->pf_render_text( p_text, p_region, p_region );
+ }
+ *pb_rerender_text = var_GetBool( p_text, "text-rerender" );
+
+exit:
+ p_region->i_align |= SUBPICTURE_RENDERED;
+}
+