]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode/transcode.c
e6db2de8af537988398c245530e71727a1a9b793
[vlc] / modules / stream_out / transcode / transcode.c
1 /*****************************************************************************
2  * transcode.c: transcoding stream output module
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 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36
37 #include <vlc_spu.h>
38
39 #include "transcode.h"
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 #define VENC_TEXT N_("Video encoder")
45 #define VENC_LONGTEXT N_( \
46     "This is the video encoder module that will be used (and its associated "\
47     "options).")
48 #define VCODEC_TEXT N_("Destination video codec")
49 #define VCODEC_LONGTEXT N_( \
50     "This is the video codec that will be used.")
51 #define VB_TEXT N_("Video bitrate")
52 #define VB_LONGTEXT N_( \
53     "Target bitrate of the transcoded video stream." )
54 #define SCALE_TEXT N_("Video scaling")
55 #define SCALE_LONGTEXT N_( \
56     "Scale factor to apply to the video while transcoding (eg: 0.25)")
57 #define FPS_TEXT N_("Video frame-rate")
58 #define FPS_LONGTEXT N_( \
59     "Target output frame rate for the video stream." )
60 #define DEINTERLACE_TEXT N_("Deinterlace video")
61 #define DEINTERLACE_LONGTEXT N_( \
62     "Deinterlace the video before encoding." )
63 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
64 #define DEINTERLACE_MODULE_LONGTEXT N_( \
65     "Specify the deinterlace module to use." )
66 #define WIDTH_TEXT N_("Video width")
67 #define WIDTH_LONGTEXT N_( \
68     "Output video width." )
69 #define HEIGHT_TEXT N_("Video height")
70 #define HEIGHT_LONGTEXT N_( \
71     "Output video height." )
72 #define MAXWIDTH_TEXT N_("Maximum video width")
73 #define MAXWIDTH_LONGTEXT N_( \
74     "Maximum output video width." )
75 #define MAXHEIGHT_TEXT N_("Maximum video height")
76 #define MAXHEIGHT_LONGTEXT N_( \
77     "Maximum output video height." )
78 #define VFILTER_TEXT N_("Video filter")
79 #define VFILTER_LONGTEXT N_( \
80     "Video filters will be applied to the video streams (after overlays " \
81     "are applied). You can enter a colon-separated list of filters." )
82
83 #define AENC_TEXT N_("Audio encoder")
84 #define AENC_LONGTEXT N_( \
85     "This is the audio encoder module that will be used (and its associated "\
86     "options).")
87 #define ACODEC_TEXT N_("Destination audio codec")
88 #define ACODEC_LONGTEXT N_( \
89     "This is the audio codec that will be used.")
90 #define AB_TEXT N_("Audio bitrate")
91 #define AB_LONGTEXT N_( \
92     "Target bitrate of the transcoded audio stream." )
93 #define ARATE_TEXT N_("Audio sample rate")
94 #define ARATE_LONGTEXT N_( \
95  "Sample rate of the transcoded audio stream (11250, 22500, 44100 or 48000).")
96 #define ALANG_TEXT N_("Audio Language")
97 #define ALANG_LONGTEXT N_( \
98     "This is the language of the audio stream.")
99 #define ACHANS_TEXT N_("Audio channels")
100 #define ACHANS_LONGTEXT N_( \
101     "Number of audio channels in the transcoded streams." )
102 #define AFILTER_TEXT N_("Audio filter")
103 #define AFILTER_LONGTEXT N_( \
104     "Audio filters will be applied to the audio streams (after conversion " \
105     "filters are applied). You can enter a colon-separated list of filters." )
106
107 #define SENC_TEXT N_("Subtitles encoder")
108 #define SENC_LONGTEXT N_( \
109     "This is the subtitles encoder module that will be used (and its " \
110     "associated options)." )
111 #define SCODEC_TEXT N_("Destination subtitles codec")
112 #define SCODEC_LONGTEXT N_( \
113     "This is the subtitles codec that will be used." )
114
115 #define SFILTER_TEXT N_("Overlays")
116 #define SFILTER_LONGTEXT N_( \
117     "This allows you to add overlays (also known as \"subpictures\" on the "\
118     "transcoded video stream. The subpictures produced by the filters will "\
119     "be overlayed directly onto the video. You can specify a colon-separated "\
120     "list of subpicture modules" )
121
122 #define OSD_TEXT N_("OSD menu")
123 #define OSD_LONGTEXT N_(\
124     "Stream the On Screen Display menu (using the osdmenu subpicture module)." )
125
126 #define THREADS_TEXT N_("Number of threads")
127 #define THREADS_LONGTEXT N_( \
128     "Number of threads used for the transcoding." )
129 #define HP_TEXT N_("High priority")
130 #define HP_LONGTEXT N_( \
131     "Runs the optional encoder thread at the OUTPUT priority instead of " \
132     "VIDEO." )
133
134 #define ASYNC_TEXT N_("Synchronise on audio track")
135 #define ASYNC_LONGTEXT N_( \
136     "This option will drop/duplicate video frames to synchronise the video " \
137     "track on the audio track." )
138
139 #define HURRYUP_TEXT N_( "Hurry up" )
140 #define HURRYUP_LONGTEXT N_( "The transcoder will drop frames if your CPU " \
141                 "can't keep up with the encoding rate." )
142
143 static const char *const ppsz_deinterlace_type[] =
144 {
145     "deinterlace", "ffmpeg-deinterlace"
146 };
147
148 static int  Open ( vlc_object_t * );
149 static void Close( vlc_object_t * );
150
151 #define SOUT_CFG_PREFIX "sout-transcode-"
152
153 vlc_module_begin ()
154     set_shortname( N_("Transcode"))
155     set_description( N_("Transcode stream output") )
156     set_capability( "sout stream", 50 )
157     add_shortcut( "transcode" )
158     set_callbacks( Open, Close )
159     set_category( CAT_SOUT )
160     set_subcategory( SUBCAT_SOUT_STREAM )
161     set_section( N_("Video"), NULL )
162     add_module( SOUT_CFG_PREFIX "venc", "encoder", NULL, VENC_TEXT,
163                 VENC_LONGTEXT, false )
164     add_string( SOUT_CFG_PREFIX "vcodec", NULL, VCODEC_TEXT,
165                 VCODEC_LONGTEXT, false )
166     add_integer( SOUT_CFG_PREFIX "vb", 0, VB_TEXT,
167                  VB_LONGTEXT, false )
168     add_float( SOUT_CFG_PREFIX "scale", 0, SCALE_TEXT,
169                SCALE_LONGTEXT, false )
170     add_float( SOUT_CFG_PREFIX "fps", 0, FPS_TEXT,
171                FPS_LONGTEXT, false )
172     add_bool( SOUT_CFG_PREFIX "hurry-up", true, HURRYUP_TEXT,
173                HURRYUP_LONGTEXT, false )
174     add_bool( SOUT_CFG_PREFIX "deinterlace", false, DEINTERLACE_TEXT,
175               DEINTERLACE_LONGTEXT, false )
176     add_string( SOUT_CFG_PREFIX "deinterlace-module", "deinterlace",
177                 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
178                 false )
179         change_string_list( ppsz_deinterlace_type, ppsz_deinterlace_type )
180     add_integer( SOUT_CFG_PREFIX "width", 0, WIDTH_TEXT,
181                  WIDTH_LONGTEXT, true )
182     add_integer( SOUT_CFG_PREFIX "height", 0, HEIGHT_TEXT,
183                  HEIGHT_LONGTEXT, true )
184     add_integer( SOUT_CFG_PREFIX "maxwidth", 0, MAXWIDTH_TEXT,
185                  MAXWIDTH_LONGTEXT, true )
186     add_integer( SOUT_CFG_PREFIX "maxheight", 0, MAXHEIGHT_TEXT,
187                  MAXHEIGHT_LONGTEXT, true )
188     add_module_list( SOUT_CFG_PREFIX "vfilter", "video filter2",
189                      NULL, VFILTER_TEXT, VFILTER_LONGTEXT, false )
190
191     set_section( N_("Audio"), NULL )
192     add_module( SOUT_CFG_PREFIX "aenc", "encoder", NULL, AENC_TEXT,
193                 AENC_LONGTEXT, false )
194     add_string( SOUT_CFG_PREFIX "acodec", NULL, ACODEC_TEXT,
195                 ACODEC_LONGTEXT, false )
196     add_integer( SOUT_CFG_PREFIX "ab", 96, AB_TEXT,
197                  AB_LONGTEXT, false )
198     add_string( SOUT_CFG_PREFIX "alang", NULL, ALANG_TEXT,
199                 ALANG_LONGTEXT, true )
200     add_integer( SOUT_CFG_PREFIX "channels", 0, ACHANS_TEXT,
201                  ACHANS_LONGTEXT, false )
202     add_integer( SOUT_CFG_PREFIX "samplerate", 0, ARATE_TEXT,
203                  ARATE_LONGTEXT, true )
204     add_bool( SOUT_CFG_PREFIX "audio-sync", false, ASYNC_TEXT,
205               ASYNC_LONGTEXT, false )
206     add_module_list( SOUT_CFG_PREFIX "afilter",  "audio filter",
207                      NULL, AFILTER_TEXT, AFILTER_LONGTEXT, false )
208
209     set_section( N_("Overlays/Subtitles"), NULL )
210     add_module( SOUT_CFG_PREFIX "senc", "encoder", NULL, SENC_TEXT,
211                 SENC_LONGTEXT, false )
212     add_string( SOUT_CFG_PREFIX "scodec", NULL, SCODEC_TEXT,
213                 SCODEC_LONGTEXT, false )
214     add_bool( SOUT_CFG_PREFIX "soverlay", false, SCODEC_TEXT,
215                SCODEC_LONGTEXT, false )
216     add_module_list( SOUT_CFG_PREFIX "sfilter", "video filter",
217                      NULL, SFILTER_TEXT, SFILTER_LONGTEXT, false )
218
219     set_section( N_("On Screen Display"), NULL )
220     add_bool( SOUT_CFG_PREFIX "osd", false, OSD_TEXT,
221               OSD_LONGTEXT, false )
222
223     set_section( N_("Miscellaneous"), NULL )
224     add_integer( SOUT_CFG_PREFIX "threads", 0, THREADS_TEXT,
225                  THREADS_LONGTEXT, true )
226     add_bool( SOUT_CFG_PREFIX "high-priority", false, HP_TEXT, HP_LONGTEXT,
227               true )
228
229 vlc_module_end ()
230
231 static const char *const ppsz_sout_options[] = {
232     "venc", "vcodec", "vb",
233     "scale", "fps", "width", "height", "vfilter", "deinterlace",
234     "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab", "alang",
235     "afilter", "samplerate", "channels", "senc", "scodec", "soverlay",
236     "sfilter", "osd", "audio-sync", "high-priority", "maxwidth", "maxheight",
237     NULL
238 };
239
240 /*****************************************************************************
241  * Exported prototypes
242  *****************************************************************************/
243 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
244 static int               Del ( sout_stream_t *, sout_stream_id_t * );
245 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
246
247 /*****************************************************************************
248  * Open:
249  *****************************************************************************/
250 static int Open( vlc_object_t *p_this )
251 {
252     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
253     sout_stream_sys_t *p_sys;
254     char              *psz_string;
255
256     if( !p_stream->p_next )
257     {
258         msg_Err( p_stream, "cannot create chain" );
259         return VLC_EGENERIC;
260     }
261     p_sys = calloc( 1, sizeof( *p_sys ) );
262     p_sys->i_master_drift = 0;
263
264     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
265                    p_stream->p_cfg );
266
267     /* Audio transcoding parameters */
268     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "aenc" );
269     p_sys->psz_aenc = NULL;
270     p_sys->p_audio_cfg = NULL;
271     if( psz_string && *psz_string )
272     {
273         char *psz_next;
274         psz_next = config_ChainCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
275                                        psz_string );
276         free( psz_next );
277     }
278     free( psz_string );
279
280     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "acodec" );
281     p_sys->i_acodec = 0;
282     if( psz_string && *psz_string )
283     {
284         char fcc[4] = "    ";
285         memcpy( fcc, psz_string, __MIN( strlen( psz_string ), 4 ) );
286         p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
287     }
288     free( psz_string );
289
290     p_sys->psz_alang = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "alang" );
291
292     p_sys->i_abitrate = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ab" );
293     if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
294
295     p_sys->i_sample_rate = var_GetInteger( p_stream, SOUT_CFG_PREFIX "samplerate" );
296
297     p_sys->i_channels = var_GetInteger( p_stream, SOUT_CFG_PREFIX "channels" );
298
299     if( p_sys->i_acodec )
300     {
301         if( ( p_sys->i_acodec == VLC_CODEC_MP3 ||
302               p_sys->i_acodec == VLC_CODEC_MP2 ||
303               p_sys->i_acodec == VLC_CODEC_MPGA ) && p_sys->i_channels > 2 )
304         {
305             msg_Warn( p_stream, "%d channels invalid for mp2/mp3, forcing to 2",
306                       p_sys->i_channels );
307             p_sys->i_channels = 2;
308         }
309         msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
310                  (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
311                  p_sys->i_channels, p_sys->i_abitrate / 1000 );
312     }
313
314     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "afilter" );
315     if( psz_string && *psz_string )
316         p_sys->psz_af = strdup( psz_string );
317     else
318         p_sys->psz_af = NULL;
319     free( psz_string );
320
321     /* Video transcoding parameters */
322     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "venc" );
323     p_sys->psz_venc = NULL;
324     p_sys->p_video_cfg = NULL;
325     if( psz_string && *psz_string )
326     {
327         char *psz_next;
328         psz_next = config_ChainCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
329                                    psz_string );
330         free( psz_next );
331     }
332     free( psz_string );
333
334     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "vcodec" );
335     p_sys->i_vcodec = 0;
336     if( psz_string && *psz_string )
337     {
338         char fcc[4] = "    ";
339         memcpy( fcc, psz_string, __MIN( strlen( psz_string ), 4 ) );
340         p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
341     }
342     free( psz_string );
343
344     p_sys->i_vbitrate = var_GetInteger( p_stream, SOUT_CFG_PREFIX "vb" );
345     if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
346
347     p_sys->f_scale = var_GetFloat( p_stream, SOUT_CFG_PREFIX "scale" );
348
349     p_sys->f_fps = var_GetFloat( p_stream, SOUT_CFG_PREFIX "fps" );
350
351     p_sys->b_hurry_up = var_GetBool( p_stream, SOUT_CFG_PREFIX "hurry-up" );
352
353     p_sys->i_width = var_GetInteger( p_stream, SOUT_CFG_PREFIX "width" );
354
355     p_sys->i_height = var_GetInteger( p_stream, SOUT_CFG_PREFIX "height" );
356
357     p_sys->i_maxwidth = var_GetInteger( p_stream, SOUT_CFG_PREFIX "maxwidth" );
358
359     p_sys->i_maxheight = var_GetInteger( p_stream, SOUT_CFG_PREFIX "maxheight" );
360
361     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "vfilter" );
362     if( psz_string && *psz_string )
363         p_sys->psz_vf2 = strdup(psz_string );
364     else
365         p_sys->psz_vf2 = NULL;
366     free( psz_string );
367
368     p_sys->b_deinterlace = var_GetBool( p_stream, SOUT_CFG_PREFIX "deinterlace" );
369
370     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "deinterlace-module" );
371     p_sys->psz_deinterlace = NULL;
372     p_sys->p_deinterlace_cfg = NULL;
373     if( psz_string && *psz_string )
374     {
375         char *psz_next;
376         psz_next = config_ChainCreate( &p_sys->psz_deinterlace,
377                                    &p_sys->p_deinterlace_cfg,
378                                    psz_string );
379         free( psz_next );
380     }
381     free( psz_string );
382
383     p_sys->i_threads = var_GetInteger( p_stream, SOUT_CFG_PREFIX "threads" );
384     p_sys->b_high_priority = var_GetBool( p_stream, SOUT_CFG_PREFIX "high-priority" );
385
386     if( p_sys->i_vcodec )
387     {
388         msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
389                  (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
390                  p_sys->f_scale, p_sys->i_vbitrate / 1000 );
391     }
392
393     /* Subpictures transcoding parameters */
394     p_sys->p_spu = NULL;
395     p_sys->p_spu_blend = NULL;
396     p_sys->psz_senc = NULL;
397     p_sys->p_spu_cfg = NULL;
398     p_sys->i_scodec = 0;
399
400     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "senc" );
401     if( psz_string && *psz_string )
402     {
403         char *psz_next;
404         psz_next = config_ChainCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
405                                    psz_string );
406         free( psz_next );
407     }
408     free( psz_string );
409
410     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "scodec" );
411     if( psz_string && *psz_string )
412     {
413         char fcc[4] = "    ";
414         memcpy( fcc, psz_string, __MIN( strlen( psz_string ), 4 ) );
415         p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
416     }
417     free( psz_string );
418
419     if( p_sys->i_scodec )
420     {
421         msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
422     }
423
424     p_sys->b_soverlay = var_GetBool( p_stream, SOUT_CFG_PREFIX "soverlay" );
425
426     psz_string = var_GetString( p_stream, SOUT_CFG_PREFIX "sfilter" );
427     if( psz_string && *psz_string )
428     {
429         p_sys->p_spu = spu_Create( p_stream );
430         if( p_sys->p_spu )
431             spu_ChangeSources( p_sys->p_spu, psz_string );
432     }
433     free( psz_string );
434
435     /* OSD menu transcoding parameters */
436     p_sys->psz_osdenc = NULL;
437     p_sys->p_osd_cfg  = NULL;
438     p_sys->i_osdcodec = 0;
439     p_sys->b_osd   = var_GetBool( p_stream, SOUT_CFG_PREFIX "osd" );
440
441     if( p_sys->b_osd )
442     {
443         char *psz_next;
444
445         psz_next = config_ChainCreate( &p_sys->psz_osdenc,
446                                    &p_sys->p_osd_cfg, strdup( "dvbsub") );
447         free( psz_next );
448
449         p_sys->i_osdcodec = VLC_CODEC_YUVP;
450
451         msg_Dbg( p_stream, "codec osd=%4.4s", (char *)&p_sys->i_osdcodec );
452
453         if( !p_sys->p_spu )
454         {
455             p_sys->p_spu = spu_Create( p_stream );
456             if( p_sys->p_spu )
457                 spu_ChangeSources( p_sys->p_spu, "osdmenu" );
458         }
459         else
460         {
461             spu_ChangeSources( p_sys->p_spu, "osdmenu" );
462         }
463     }
464
465     /* Audio settings */
466     p_sys->b_master_sync = var_GetBool( p_stream, SOUT_CFG_PREFIX "audio-sync" );
467     if( p_sys->f_fps > 0 ) p_sys->b_master_sync = true;
468
469     p_stream->pf_add    = Add;
470     p_stream->pf_del    = Del;
471     p_stream->pf_send   = Send;
472     p_stream->p_sys     = p_sys;
473
474     return VLC_SUCCESS;
475 }
476
477 /*****************************************************************************
478  * Close:
479  *****************************************************************************/
480 static void Close( vlc_object_t * p_this )
481 {
482     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
483     sout_stream_sys_t   *p_sys = p_stream->p_sys;
484
485     free( p_sys->psz_af );
486
487     config_ChainDestroy( p_sys->p_audio_cfg );
488     free( p_sys->psz_aenc );
489     free( p_sys->psz_alang );
490
491     free( p_sys->psz_vf2 );
492
493     config_ChainDestroy( p_sys->p_video_cfg );
494     free( p_sys->psz_venc );
495
496     config_ChainDestroy( p_sys->p_deinterlace_cfg );
497     free( p_sys->psz_deinterlace );
498
499     config_ChainDestroy( p_sys->p_spu_cfg );
500     free( p_sys->psz_senc );
501
502     if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
503     if( p_sys->p_spu_blend ) filter_DeleteBlend( p_sys->p_spu_blend );
504
505     config_ChainDestroy( p_sys->p_osd_cfg );
506     free( p_sys->psz_osdenc );
507
508     free( p_sys );
509 }
510
511 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
512 {
513     sout_stream_sys_t *p_sys = p_stream->p_sys;
514     sout_stream_id_t *id;
515
516     id = calloc( 1, sizeof( sout_stream_id_t ) );
517     if( !id )
518         goto error;
519
520     id->id = NULL;
521     id->p_decoder = NULL;
522     id->p_encoder = NULL;
523
524     /* Create decoder object */
525     id->p_decoder = vlc_object_create( p_stream, sizeof( decoder_t ) );
526     if( !id->p_decoder )
527         goto error;
528     id->p_decoder->p_module = NULL;
529     id->p_decoder->fmt_in = *p_fmt;
530     id->p_decoder->b_pace_control = true;
531
532     /* Create encoder object */
533     id->p_encoder = sout_EncoderCreate( p_stream );
534     if( !id->p_encoder )
535         goto error;
536     id->p_encoder->p_module = NULL;
537
538     /* Create destination format */
539     es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
540     id->p_encoder->fmt_out.i_id    = p_fmt->i_id;
541     id->p_encoder->fmt_out.i_group = p_fmt->i_group;
542
543     if( p_sys->psz_alang )
544         id->p_encoder->fmt_out.psz_language = strdup( p_sys->psz_alang );
545     else if( p_fmt->psz_language )
546         id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
547
548     bool success;
549
550     if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
551         success = transcode_audio_add(p_stream, p_fmt, id);
552     else if( p_fmt->i_cat == VIDEO_ES && (p_sys->i_vcodec || p_sys->psz_venc) )
553         success = transcode_video_add(p_stream, p_fmt, id);
554     else if( ( p_fmt->i_cat == SPU_ES ) &&
555              ( p_sys->i_scodec || p_sys->psz_senc || p_sys->b_soverlay ) )
556         success = transcode_spu_add(p_stream, p_fmt, id);
557     else if( !p_sys->b_osd && (p_sys->i_osdcodec != 0 || p_sys->psz_osdenc) )
558         success = transcode_osd_add(p_stream, p_fmt, id);
559     else
560     {
561         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
562                  (char*)&p_fmt->i_codec );
563         id->id = sout_StreamIdAdd( p_stream->p_next, p_fmt );
564         id->b_transcode = false;
565
566         success = id->id;
567     }
568
569     if(!success)
570         goto error;
571
572     return id;
573
574 error:
575     if( id )
576     {
577         if( id->p_decoder )
578         {
579             vlc_object_release( id->p_decoder );
580             id->p_decoder = NULL;
581         }
582
583         if( id->p_encoder )
584         {
585             es_format_Clean( &id->p_encoder->fmt_out );
586             vlc_object_release( id->p_encoder );
587             id->p_encoder = NULL;
588         }
589
590         free( id );
591     }
592     return NULL;
593 }
594
595 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
596 {
597     sout_stream_sys_t *p_sys = p_stream->p_sys;
598
599     if( id->b_transcode )
600     {
601         switch( id->p_decoder->fmt_in.i_cat )
602         {
603         case AUDIO_ES:
604             transcode_audio_close( id );
605             break;
606         case VIDEO_ES:
607             Send( p_stream, id, NULL );
608             transcode_video_close( p_stream, id );
609             break;
610         case SPU_ES:
611             if( p_sys->b_osd )
612                 transcode_osd_close( p_stream, id );
613             else
614                 transcode_spu_close( p_stream, id );
615             break;
616         }
617     }
618
619     if( id->id ) sout_StreamIdDel( p_stream->p_next, id->id );
620
621     if( id->p_decoder )
622     {
623         vlc_object_release( id->p_decoder );
624         id->p_decoder = NULL;
625     }
626
627     if( id->p_encoder )
628     {
629         es_format_Clean( &id->p_encoder->fmt_out );
630         vlc_object_release( id->p_encoder );
631         id->p_encoder = NULL;
632     }
633     free( id );
634
635     return VLC_SUCCESS;
636 }
637
638 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
639                  block_t *p_buffer )
640 {
641     sout_stream_sys_t *p_sys = p_stream->p_sys;
642     block_t *p_out = NULL;
643
644     if( !id->b_transcode )
645     {
646         if( id->id )
647             return sout_StreamIdSend( p_stream->p_next, id->id, p_buffer );
648
649         block_Release( p_buffer );
650         return VLC_EGENERIC;
651     }
652
653     switch( id->p_decoder->fmt_in.i_cat )
654     {
655     case AUDIO_ES:
656         transcode_audio_process( p_stream, id, p_buffer, &p_out );
657         break;
658
659     case VIDEO_ES:
660         if( transcode_video_process( p_stream, id, p_buffer, &p_out )
661             != VLC_SUCCESS )
662         {
663             return VLC_EGENERIC;
664         }
665         break;
666
667     case SPU_ES:
668         /* Transcode OSD menu pictures. */
669         if( p_sys->b_osd )
670         {
671             if( transcode_osd_process( p_stream, id, p_buffer, &p_out ) !=
672                 VLC_SUCCESS )
673             {
674                 return VLC_EGENERIC;
675             }
676         }
677         else if ( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
678             VLC_SUCCESS )
679         {
680             return VLC_EGENERIC;
681         }
682         break;
683
684     default:
685         p_out = NULL;
686         block_Release( p_buffer );
687         break;
688     }
689
690     if( p_out )
691         return sout_StreamIdSend( p_stream->p_next, id->id, p_out );
692     return VLC_SUCCESS;
693 }