]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode/spu.c
transcode: initialize audio encoder after first packet arrives
[vlc] / 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     p_subpicture->b_subtitle = true;
44     return p_subpicture;
45 }
46
47 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
48 {
49     VLC_UNUSED( p_dec );
50     subpicture_Delete( p_subpic );
51 }
52 int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
53 {
54     sout_stream_sys_t *p_sys = p_stream->p_sys;
55
56     /*
57      * Open decoder
58      */
59
60     /* Initialization of decoder structures */
61     id->p_decoder->pf_decode_sub = NULL;
62     id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
63     id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
64     id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
65     /* id->p_decoder->p_cfg = p_sys->p_spu_cfg; */
66
67     id->p_decoder->p_module =
68         module_need( id->p_decoder, "decoder", "$codec", false );
69
70     if( !id->p_decoder->p_module )
71     {
72         msg_Err( p_stream, "cannot find spu decoder" );
73         return VLC_EGENERIC;
74     }
75
76     if( !p_sys->b_soverlay )
77     {
78         /* Open encoder */
79         /* Initialization of encoder format structures */
80         es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
81                         id->p_decoder->fmt_in.i_codec );
82
83         id->p_encoder->p_cfg = p_sys->p_spu_cfg;
84
85         id->p_encoder->p_module =
86             module_need( id->p_encoder, "encoder", p_sys->psz_senc, true );
87
88         if( !id->p_encoder->p_module )
89         {
90             module_unneed( id->p_decoder, id->p_decoder->p_module );
91             msg_Err( p_stream, "cannot find spu encoder (%s)", p_sys->psz_senc );
92             return VLC_EGENERIC;
93         }
94     }
95
96     if( !p_sys->p_spu )
97         p_sys->p_spu = spu_Create( p_stream );
98
99     return VLC_SUCCESS;
100 }
101
102 void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
103 {
104     sout_stream_sys_t *p_sys = p_stream->p_sys;
105     /* Close decoder */
106     if( id->p_decoder->p_module )
107         module_unneed( id->p_decoder, id->p_decoder->p_module );
108     if( id->p_decoder->p_description )
109         vlc_meta_Delete( id->p_decoder->p_description );
110
111     /* Close encoder */
112     if( id->p_encoder->p_module )
113         module_unneed( id->p_encoder, id->p_encoder->p_module );
114
115     if( p_sys->p_spu )
116     {
117         spu_Destroy( p_sys->p_spu );
118         p_sys->p_spu = NULL;
119     }
120 }
121
122 int transcode_spu_process( sout_stream_t *p_stream,
123                                   sout_stream_id_t *id,
124                                   block_t *in, block_t **out )
125 {
126     sout_stream_sys_t *p_sys = p_stream->p_sys;
127     subpicture_t *p_subpic;
128     *out = NULL;
129
130     p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
131     if( !p_subpic )
132         return VLC_EGENERIC;
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     if( p_sys->b_soverlay )
141     {
142         spu_PutSubpicture( p_sys->p_spu, p_subpic );
143     }
144     else
145     {
146         block_t *p_block;
147
148         p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
149         spu_del_buffer( id->p_decoder, p_subpic );
150         if( p_block )
151         {
152             block_ChainAppend( out, p_block );
153             return VLC_SUCCESS;
154         }
155     }
156
157     return VLC_EGENERIC;
158 }
159
160 bool transcode_spu_add( sout_stream_t *p_stream, es_format_t *p_fmt,
161                         sout_stream_id_t *id )
162 {
163     sout_stream_sys_t *p_sys = p_stream->p_sys;
164
165     if( p_sys->i_scodec || p_sys->psz_senc )
166     {
167         msg_Dbg( p_stream, "creating subtitle transcoding from fcc=`%4.4s' "
168                  "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
169                  (char*)&p_sys->i_scodec );
170
171         /* Complete destination format */
172         id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
173
174         /* build decoder -> filter -> encoder */
175         if( transcode_spu_new( p_stream, id ) )
176         {
177             msg_Err( p_stream, "cannot create subtitle chain" );
178             return false;
179         }
180
181         /* open output stream */
182         id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
183         id->b_transcode = true;
184
185         if( !id->id )
186         {
187             transcode_spu_close( p_stream, id );
188             return false;
189         }
190     }
191     else
192     {
193         assert( p_sys->b_soverlay );
194         msg_Dbg( p_stream, "subtitle (fcc=`%4.4s') overlaying",
195                  (char*)&p_fmt->i_codec );
196
197         id->b_transcode = true;
198
199         /* Build decoder -> filter -> overlaying chain */
200         if( transcode_spu_new( p_stream, id ) )
201         {
202             msg_Err( p_stream, "cannot create subtitle chain" );
203             return false;
204         }
205     }
206
207     return true;
208 }