+
+/*
+ * OSD menu
+ */
+static int transcode_osd_new( sout_stream_t *p_stream, sout_stream_id_t *id )
+{
+ sout_stream_sys_t *p_sys = p_stream->p_sys;
+ es_format_t fmt;
+
+ fmt.i_cat = SPU_ES;
+ fmt.i_id = 0xbd1f; /* pid ?? */
+ fmt.i_group = 3; /* pmt entry ?? */
+ fmt.i_codec = VLC_FOURCC( 'Y', 'U', 'V', 'A' );
+ fmt.psz_language = strdup( "osd" );
+
+ id = malloc( sizeof( sout_stream_id_t ) );
+ memset( id, 0, sizeof(sout_stream_id_t) );
+
+ id->id = NULL;
+ id->p_decoder = NULL;
+ id->p_encoder = NULL;
+
+ /* Create encoder object */
+ id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
+ if( !id->p_encoder )
+ {
+ msg_Err( p_stream, "out of memory" );
+ goto error;
+ }
+ vlc_object_attach( id->p_encoder, p_stream );
+ id->p_encoder->p_module = NULL;
+
+ /* Create fake destination format */
+ es_format_Init( &id->p_encoder->fmt_out, fmt.i_cat, 0 );
+ id->p_encoder->fmt_out.i_id = fmt.i_id;
+ id->p_encoder->fmt_out.i_group = fmt.i_group;
+ id->p_encoder->fmt_out.psz_language = strdup( fmt.psz_language );
+
+ if( p_sys->i_osdcodec != 0 || p_sys->psz_osdenc )
+ {
+ msg_Dbg( p_stream, "creating osdmenu transcoding from fcc=`%4.4s' "
+ "to fcc=`%4.4s'", (char*)&fmt.i_codec,
+ (char*)&p_sys->i_osdcodec );
+
+ /* Complete destination format */
+ id->p_encoder->fmt_out.i_codec = p_sys->i_osdcodec;
+
+ /*
+ * Open encoder
+ */
+
+ /* Initialization of encoder format structures */
+ es_format_Init( &id->p_encoder->fmt_in, fmt.i_cat, fmt.i_codec );
+ id->p_encoder->fmt_in.psz_language = strdup( fmt.psz_language );
+
+ id->p_encoder->p_cfg = p_sys->p_osd_cfg;
+
+ id->p_encoder->p_module =
+ module_Need( id->p_encoder, "encoder", p_sys->psz_osdenc, VLC_TRUE );
+
+ if( !id->p_encoder->p_module )
+ {
+ msg_Err( p_stream, "cannot find encoder" );
+ goto error;
+ }
+
+ /* open output stream */
+ id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
+ id->b_transcode = VLC_TRUE;
+
+ if( !id->id ) goto error;
+ }
+ else
+ {
+ msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
+ (char*)&fmt.i_codec );
+ id->id = p_sys->p_out->pf_add( p_sys->p_out, &fmt );
+ id->b_transcode = VLC_FALSE;
+
+ if( !id->id ) goto error;
+ }
+
+ p_sys->id_osd = id;
+ p_sys->b_es_osd = VLC_TRUE;
+
+ if( !p_sys->p_spu )
+ {
+ p_sys->p_spu = spu_Create( p_stream );
+ if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
+ msg_Err( p_sys, "spu initialisation failed" );
+ }
+
+ if( fmt.psz_language )
+ free( fmt.psz_language );
+
+ return VLC_SUCCESS;
+
+ error:
+ msg_Err( p_stream, "starting osd encoding thread failed" );
+ if( id->p_encoder->p_module )
+ module_Unneed( id->p_encoder, id->p_encoder->p_module );
+ if( id->p_encoder )
+ {
+ vlc_object_detach( id->p_encoder );
+ vlc_object_destroy( id->p_encoder );
+ }
+ if( fmt.psz_language ) free( fmt.psz_language );
+ if( id ) free( id );
+ p_sys->id_osd = NULL;
+ p_sys->b_es_osd = VLC_FALSE;
+ return VLC_EGENERIC;
+}
+
+static void transcode_osd_close( sout_stream_t *p_stream, sout_stream_id_t *id)
+{
+ sout_stream_sys_t *p_sys = p_stream->p_sys;
+
+ /* Close encoder */
+ if( p_sys->b_es_osd && id )
+ {
+ if( id->p_encoder->p_module )
+ module_Unneed( id->p_encoder, id->p_encoder->p_module );
+
+ if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
+
+ if( id->p_encoder )
+ {
+ vlc_object_detach( id->p_encoder );
+ vlc_object_destroy( id->p_encoder );
+ }
+ }
+ p_sys->b_es_osd = VLC_FALSE;
+ if( id ) free( id );
+}
+
+static int transcode_osd_process( sout_stream_t *p_stream,
+ sout_stream_id_t *id,
+ block_t *in, block_t **out )
+{
+ sout_stream_sys_t *p_sys = p_stream->p_sys;
+ subpicture_t *p_subpic = NULL;
+
+ /* Check if we have a subpicture to send */
+ if( p_sys->p_spu && in->i_dts > 0)
+ {
+ p_subpic = spu_SortSubpictures( p_sys->p_spu, in->i_dts );
+ }
+ else
+ {
+ msg_Warn( p_stream, "spu channel not initialized, doing it now" );
+ if( !p_sys->p_spu )
+ {
+ p_sys->p_spu = spu_Create( p_stream );
+ if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
+ msg_Err( p_stream, "spu initialisation failed" );
+ }
+ }
+
+ if( p_subpic )
+ {
+ block_t *p_block = NULL;
+
+ if( p_sys->b_master_sync && p_sys->i_master_drift )
+ {
+ p_subpic->i_start -= p_sys->i_master_drift;
+ if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
+ }
+
+ p_block = p_sys->id_osd->p_encoder->pf_encode_sub( p_sys->id_osd->p_encoder, p_subpic );
+ if( p_block )
+ {
+ p_block->i_dts = p_block->i_pts = in->i_dts;
+ block_ChainAppend( out, p_block );
+ if( *out )
+ {
+ if( p_sys->p_out->pf_send( p_sys->p_out, p_sys->id_osd->id, *out ) == VLC_SUCCESS )
+ spu_DestroySubpicture( p_sys->p_spu, p_subpic );
+ }
+ return VLC_SUCCESS;
+ }
+ }
+ return VLC_EGENERIC;
+}