]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode/osd.c
a8950841fc63d652e235f3bad8213361e89e939f
[vlc] / modules / stream_out / transcode / osd.c
1 /*****************************************************************************
2  * osd.c: transcoding stream output module (osd)
3  *****************************************************************************
4  * Copyright (C) 2003-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
10  *          Antoine Cellerier <dionoea at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #include "transcode.h"
32
33 #include <vlc_spu.h>
34
35 /*
36  * OSD menu
37  */
38 int transcode_osd_new( sout_stream_t *p_stream, sout_stream_id_t *id )
39 {
40     sout_stream_sys_t *p_sys = p_stream->p_sys;
41
42     id->p_decoder->fmt_in.i_cat = SPU_ES;
43     id->p_encoder->fmt_out.psz_language = strdup( "osd" );
44
45     if( p_sys->i_osdcodec != 0 || p_sys->psz_osdenc )
46     {
47         msg_Dbg( p_stream, "creating osdmenu transcoding from fcc=`%4.4s' "
48                  "to fcc=`%4.4s'", (char*)&id->p_encoder->fmt_out.i_codec,
49                  (char*)&p_sys->i_osdcodec );
50
51         /* Complete destination format */
52         id->p_encoder->fmt_out.i_codec = p_sys->i_osdcodec;
53
54         /* Open encoder */
55         es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
56                         VLC_CODEC_YUVA );
57         id->p_encoder->fmt_in.psz_language = strdup( "osd" );
58
59         id->p_encoder->p_cfg = p_sys->p_osd_cfg;
60
61         id->p_encoder->p_module =
62             module_need( id->p_encoder, "encoder", p_sys->psz_osdenc, true );
63
64         if( !id->p_encoder->p_module )
65         {
66             msg_Err( p_stream, "cannot find spu encoder (%s)", p_sys->psz_osdenc );
67             goto error;
68         }
69
70         /* open output stream */
71         id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
72         id->b_transcode = true;
73
74         if( !id->id ) goto error;
75     }
76     else
77     {
78         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
79                  (char*)&id->p_decoder->fmt_out.i_codec );
80         id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_decoder->fmt_out );
81         id->b_transcode = false;
82
83         if( !id->id ) goto error;
84     }
85
86     if( !p_sys->p_spu )
87         p_sys->p_spu = spu_Create( p_stream );
88
89     return VLC_SUCCESS;
90
91  error:
92     msg_Err( p_stream, "starting osd encoding thread failed" );
93     if( id->p_encoder->p_module )
94             module_unneed( id->p_encoder, id->p_encoder->p_module );
95     p_sys->b_osd = false;
96     return VLC_EGENERIC;
97 }
98
99 void transcode_osd_close( sout_stream_t *p_stream, sout_stream_id_t *id)
100 {
101     sout_stream_sys_t *p_sys = p_stream->p_sys;
102
103     /* Close encoder */
104     if( id )
105     {
106         if( id->p_encoder->p_module )
107             module_unneed( id->p_encoder, id->p_encoder->p_module );
108     }
109     p_sys->b_osd = false;
110 }
111
112 int transcode_osd_process( sout_stream_t *p_stream, sout_stream_id_t *id,
113                                   block_t *in, block_t **out )
114 {
115     sout_stream_sys_t *p_sys = p_stream->p_sys;
116     subpicture_t *p_subpic = NULL;
117
118     /* Check if we have a subpicture to send */
119     if( p_sys->p_spu && in->i_dts > VLC_TS_INVALID )
120     {
121         p_subpic = spu_SortSubpictures( p_sys->p_spu, in->i_dts, false );
122     }
123     else
124     {
125         msg_Warn( p_stream, "spu channel not initialized, doing it now" );
126         if( !p_sys->p_spu )
127             p_sys->p_spu = spu_Create( p_stream );
128     }
129
130     if( p_subpic )
131     {
132         block_t *p_block = NULL;
133
134         if( p_sys->b_master_sync && p_sys->i_master_drift )
135         {
136             p_subpic->i_start -= p_sys->i_master_drift;
137             if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
138         }
139
140         p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
141         subpicture_Delete( p_subpic );
142         if( p_block )
143         {
144             p_block->i_dts = p_block->i_pts = in->i_dts;
145             block_ChainAppend( out, p_block );
146             return VLC_SUCCESS;
147         }
148     }
149     return VLC_EGENERIC;
150 }
151
152 bool transcode_osd_add( sout_stream_t *p_stream, es_format_t *p_fmt,
153                                 sout_stream_id_t *id)
154 {
155     sout_stream_sys_t *p_sys = p_stream->p_sys;
156
157     msg_Dbg( p_stream, "creating osd transcoding from fcc=`%4.4s' "
158              "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
159              (char*)&p_sys->i_scodec );
160
161     id->b_transcode = true;
162
163     /* Create a fake OSD menu elementary stream */
164     if( transcode_osd_new( p_stream, id ) )
165     {
166         msg_Err( p_stream, "cannot create osd chain" );
167         return false;
168     }
169     p_sys->b_osd = true;
170
171     return true;
172 }