filter_chain: introduce dedicated filter_chain_NewVideo() for video filters
[vlc.git] / modules / stream_out / transcode / spu.c
1 /*****************************************************************************
2  * spu.c: transcoding stream output module (spu)
3  *****************************************************************************
4  * Copyright (C) 2003-2009 VLC authors and VideoLAN
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 it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * 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_meta.h>
34 #include <vlc_spu.h>
35 #include <vlc_modules.h>
36 #include <assert.h>
37
38 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
39                                      const subpicture_updater_t *p_upd )
40 {
41     VLC_UNUSED( p_dec );
42     subpicture_t *p_subpicture = subpicture_New( p_upd );
43     if( likely(p_subpicture != NULL) )
44         p_subpicture->b_subtitle = true;
45     return p_subpicture;
46 }
47
48 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
49 {
50     VLC_UNUSED( p_dec );
51     subpicture_Delete( p_subpic );
52 }
53 int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
54 {
55     sout_stream_sys_t *p_sys = p_stream->p_sys;
56
57     /*
58      * Open decoder
59      */
60
61     /* Initialization of decoder structures */
62     id->p_decoder->pf_decode_sub = NULL;
63     id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
64     id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
65     id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
66     /* id->p_decoder->p_cfg = p_sys->p_spu_cfg; */
67
68     id->p_decoder->p_module =
69         module_need( id->p_decoder, "decoder", "$codec", false );
70
71     if( !id->p_decoder->p_module )
72     {
73         msg_Err( p_stream, "cannot find spu decoder" );
74         return VLC_EGENERIC;
75     }
76
77     if( !p_sys->b_soverlay )
78     {
79         /* Open encoder */
80         /* Initialization of encoder format structures */
81         es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
82                         id->p_decoder->fmt_in.i_codec );
83
84         id->p_encoder->p_cfg = p_sys->p_spu_cfg;
85
86         id->p_encoder->p_module =
87             module_need( id->p_encoder, "encoder", p_sys->psz_senc, true );
88
89         if( !id->p_encoder->p_module )
90         {
91             module_unneed( id->p_decoder, id->p_decoder->p_module );
92             msg_Err( p_stream, "cannot find spu encoder (%s)", p_sys->psz_senc );
93             return VLC_EGENERIC;
94         }
95     }
96
97     if( !p_sys->p_spu )
98         p_sys->p_spu = spu_Create( p_stream );
99
100     return VLC_SUCCESS;
101 }
102
103 void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_sys_t *id)
104 {
105     sout_stream_sys_t *p_sys = p_stream->p_sys;
106     /* Close decoder */
107     if( id->p_decoder->p_module )
108         module_unneed( id->p_decoder, id->p_decoder->p_module );
109     if( id->p_decoder->p_description )
110         vlc_meta_Delete( id->p_decoder->p_description );
111
112     /* Close encoder */
113     if( id->p_encoder->p_module )
114         module_unneed( id->p_encoder, id->p_encoder->p_module );
115
116     if( p_sys->p_spu )
117     {
118         spu_Destroy( p_sys->p_spu );
119         p_sys->p_spu = NULL;
120     }
121 }
122
123 int transcode_spu_process( sout_stream_t *p_stream,
124                                   sout_stream_id_sys_t *id,
125                                   block_t *in, block_t **out )
126 {
127     sout_stream_sys_t *p_sys = p_stream->p_sys;
128     subpicture_t *p_subpic;
129     *out = NULL;
130
131     p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
132     if( !p_subpic )
133     {
134         /* We just don't have anything to handle now, go own*/
135         return VLC_SUCCESS;
136     }
137
138     if( p_sys->b_master_sync && p_sys->i_master_drift )
139     {
140         p_subpic->i_start -= p_sys->i_master_drift;
141         if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
142     }
143
144     if( p_sys->b_soverlay )
145     {
146         spu_PutSubpicture( p_sys->p_spu, p_subpic );
147         return VLC_SUCCESS;
148     }
149     else
150     {
151         block_t *p_block;
152
153         p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
154         spu_del_buffer( id->p_decoder, p_subpic );
155         if( p_block )
156         {
157             block_ChainAppend( out, p_block );
158             return VLC_SUCCESS;
159         }
160     }
161
162     return VLC_EGENERIC;
163 }
164
165 bool transcode_spu_add( sout_stream_t *p_stream, es_format_t *p_fmt,
166                         sout_stream_id_sys_t *id )
167 {
168     sout_stream_sys_t *p_sys = p_stream->p_sys;
169
170     if( p_sys->i_scodec )
171     {
172         msg_Dbg( p_stream, "creating subtitle transcoding from fcc=`%4.4s' "
173                  "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
174                  (char*)&p_sys->i_scodec );
175
176         /* Complete destination format */
177         id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
178
179         /* build decoder -> filter -> encoder */
180         if( transcode_spu_new( p_stream, id ) )
181         {
182             msg_Err( p_stream, "cannot create subtitle chain" );
183             return false;
184         }
185
186         /* open output stream */
187         id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
188         id->b_transcode = true;
189
190         if( !id->id )
191         {
192             transcode_spu_close( p_stream, id );
193             return false;
194         }
195     }
196     else
197     {
198         assert( p_sys->b_soverlay );
199         msg_Dbg( p_stream, "subtitle (fcc=`%4.4s') overlaying",
200                  (char*)&p_fmt->i_codec );
201
202         id->b_transcode = true;
203
204         /* Build decoder -> filter -> overlaying chain */
205         if( transcode_spu_new( p_stream, id ) )
206         {
207             msg_Err( p_stream, "cannot create subtitle chain" );
208             return false;
209         }
210     }
211
212     return true;
213 }