]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode/transcode.c
transcode: default audio bitrate to 96k
[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, NULL, VENC_TEXT,
163                 VENC_LONGTEXT, false )
164     add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
165                 VCODEC_LONGTEXT, false )
166     add_integer( SOUT_CFG_PREFIX "vb", 0, NULL, VB_TEXT,
167                  VB_LONGTEXT, false )
168     add_float( SOUT_CFG_PREFIX "scale", 0, NULL, SCALE_TEXT,
169                SCALE_LONGTEXT, false )
170     add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
171                FPS_LONGTEXT, false )
172     add_bool( SOUT_CFG_PREFIX "hurry-up", true, NULL, HURRYUP_TEXT,
173                HURRYUP_LONGTEXT, false )
174     add_bool( SOUT_CFG_PREFIX "deinterlace", false, NULL, DEINTERLACE_TEXT,
175               DEINTERLACE_LONGTEXT, false )
176     add_string( SOUT_CFG_PREFIX "deinterlace-module", "deinterlace", NULL,
177                 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
178                 false )
179         change_string_list( ppsz_deinterlace_type, 0, 0 )
180     add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
181                  WIDTH_LONGTEXT, true )
182     add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
183                  HEIGHT_LONGTEXT, true )
184     add_integer( SOUT_CFG_PREFIX "maxwidth", 0, NULL, MAXWIDTH_TEXT,
185                  MAXWIDTH_LONGTEXT, true )
186     add_integer( SOUT_CFG_PREFIX "maxheight", 0, NULL, MAXHEIGHT_TEXT,
187                  MAXHEIGHT_LONGTEXT, true )
188     add_module_list( SOUT_CFG_PREFIX "vfilter", "video filter2",
189                      NULL, NULL,
190                      VFILTER_TEXT, VFILTER_LONGTEXT, false )
191
192     set_section( N_("Audio"), NULL )
193     add_module( SOUT_CFG_PREFIX "aenc", "encoder", NULL, NULL, AENC_TEXT,
194                 AENC_LONGTEXT, false )
195     add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
196                 ACODEC_LONGTEXT, false )
197     add_integer( SOUT_CFG_PREFIX "ab", 96, NULL, AB_TEXT,
198                  AB_LONGTEXT, false )
199     add_string( SOUT_CFG_PREFIX "alang", NULL, NULL, ALANG_TEXT,
200                 ALANG_LONGTEXT, true )
201     add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
202                  ACHANS_LONGTEXT, false )
203     add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
204                  ARATE_LONGTEXT, true )
205     add_bool( SOUT_CFG_PREFIX "audio-sync", false, NULL, ASYNC_TEXT,
206               ASYNC_LONGTEXT, false )
207     add_module_list( SOUT_CFG_PREFIX "afilter",  "audio filter",
208                      NULL, NULL,
209                      AFILTER_TEXT, AFILTER_LONGTEXT, false )
210
211     set_section( N_("Overlays/Subtitles"), NULL )
212     add_module( SOUT_CFG_PREFIX "senc", "encoder", NULL, NULL, SENC_TEXT,
213                 SENC_LONGTEXT, false )
214     add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
215                 SCODEC_LONGTEXT, false )
216     add_bool( SOUT_CFG_PREFIX "soverlay", false, NULL, SCODEC_TEXT,
217                SCODEC_LONGTEXT, false )
218     add_module_list( SOUT_CFG_PREFIX "sfilter", "video filter",
219                      NULL, NULL,
220                      SFILTER_TEXT, SFILTER_LONGTEXT, false )
221
222     set_section( N_("On Screen Display"), NULL )
223     add_bool( SOUT_CFG_PREFIX "osd", false, NULL, OSD_TEXT,
224               OSD_LONGTEXT, false )
225
226     set_section( N_("Miscellaneous"), NULL )
227     add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
228                  THREADS_LONGTEXT, true )
229     add_bool( SOUT_CFG_PREFIX "high-priority", false, NULL, HP_TEXT, HP_LONGTEXT,
230               true )
231
232 vlc_module_end ()
233
234 static const char *const ppsz_sout_options[] = {
235     "venc", "vcodec", "vb",
236     "scale", "fps", "width", "height", "vfilter", "deinterlace",
237     "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab", "alang",
238     "afilter", "samplerate", "channels", "senc", "scodec", "soverlay",
239     "sfilter", "osd", "audio-sync", "high-priority", "maxwidth", "maxheight",
240     NULL
241 };
242
243 /*****************************************************************************
244  * Exported prototypes
245  *****************************************************************************/
246 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
247 static int               Del ( sout_stream_t *, sout_stream_id_t * );
248 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
249
250 /*****************************************************************************
251  * Open:
252  *****************************************************************************/
253 static int Open( vlc_object_t *p_this )
254 {
255     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
256     sout_stream_sys_t *p_sys;
257     vlc_value_t       val;
258
259     p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
260
261     if( !p_stream->p_next )
262     {
263         msg_Err( p_stream, "cannot create chain" );
264         vlc_object_release( p_sys );
265         return VLC_EGENERIC;
266     }
267
268     p_sys->i_master_drift = 0;
269
270     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
271                    p_stream->p_cfg );
272
273     /* Audio transcoding parameters */
274     var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
275     p_sys->psz_aenc = NULL;
276     p_sys->p_audio_cfg = NULL;
277     if( val.psz_string && *val.psz_string )
278     {
279         char *psz_next;
280         psz_next = config_ChainCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
281                                        val.psz_string );
282         free( psz_next );
283     }
284     free( val.psz_string );
285
286     var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
287     p_sys->i_acodec = 0;
288     if( val.psz_string && *val.psz_string )
289     {
290         char fcc[4] = "    ";
291         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
292         p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
293     }
294     free( val.psz_string );
295
296     p_sys->psz_alang = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "alang" );
297
298     var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
299     p_sys->i_abitrate = val.i_int;
300     if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
301
302     var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
303     p_sys->i_sample_rate = val.i_int;
304
305     var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
306     p_sys->i_channels = val.i_int;
307
308     if( p_sys->i_acodec )
309     {
310         if( ( p_sys->i_acodec == VLC_CODEC_MP3 ||
311               p_sys->i_acodec == VLC_CODEC_MPGA ) && p_sys->i_channels > 2 )
312         {
313             msg_Warn( p_stream, "%d channels invalid for mp3, forcing to 2",
314                       p_sys->i_channels );
315             p_sys->i_channels = 2;
316         }
317         msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
318                  (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
319                  p_sys->i_channels, p_sys->i_abitrate / 1000 );
320     }
321
322     var_Get( p_stream, SOUT_CFG_PREFIX "afilter", &val );
323     if( val.psz_string && *val.psz_string )
324         p_sys->psz_af = val.psz_string;
325     else
326     {
327         free( val.psz_string );
328         p_sys->psz_af = NULL;
329     }
330
331     /* Video transcoding parameters */
332     var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
333     p_sys->psz_venc = NULL;
334     p_sys->p_video_cfg = NULL;
335     if( val.psz_string && *val.psz_string )
336     {
337         char *psz_next;
338         psz_next = config_ChainCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
339                                    val.psz_string );
340         free( psz_next );
341     }
342     free( val.psz_string );
343
344     var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
345     p_sys->i_vcodec = 0;
346     if( val.psz_string && *val.psz_string )
347     {
348         char fcc[4] = "    ";
349         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
350         p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
351     }
352     free( val.psz_string );
353
354     var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
355     p_sys->i_vbitrate = val.i_int;
356     if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
357
358     var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
359     p_sys->f_scale = val.f_float;
360
361     var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
362     p_sys->f_fps = val.f_float;
363
364     var_Get( p_stream, SOUT_CFG_PREFIX "hurry-up", &val );
365     p_sys->b_hurry_up = val.b_bool;
366
367     var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
368     p_sys->i_width = val.i_int;
369
370     var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
371     p_sys->i_height = val.i_int;
372
373     var_Get( p_stream, SOUT_CFG_PREFIX "maxwidth", &val );
374     p_sys->i_maxwidth = val.i_int;
375
376     var_Get( p_stream, SOUT_CFG_PREFIX "maxheight", &val );
377     p_sys->i_maxheight = val.i_int;
378
379     var_Get( p_stream, SOUT_CFG_PREFIX "vfilter", &val );
380     if( val.psz_string && *val.psz_string )
381         p_sys->psz_vf2 = val.psz_string;
382     else
383     {
384         free( val.psz_string );
385         p_sys->psz_vf2 = NULL;
386     }
387
388     var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
389     p_sys->b_deinterlace = val.b_bool;
390
391     var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace-module", &val );
392     p_sys->psz_deinterlace = NULL;
393     p_sys->p_deinterlace_cfg = NULL;
394     if( val.psz_string && *val.psz_string )
395     {
396         char *psz_next;
397         psz_next = config_ChainCreate( &p_sys->psz_deinterlace,
398                                    &p_sys->p_deinterlace_cfg,
399                                    val.psz_string );
400         free( psz_next );
401     }
402     free( val.psz_string );
403
404     var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
405     p_sys->i_threads = val.i_int;
406     var_Get( p_stream, SOUT_CFG_PREFIX "high-priority", &val );
407     p_sys->b_high_priority = val.b_bool;
408
409     if( p_sys->i_vcodec )
410     {
411         msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
412                  (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
413                  p_sys->f_scale, p_sys->i_vbitrate / 1000 );
414     }
415
416     /* Subpictures transcoding parameters */
417     p_sys->p_spu = NULL;
418     p_sys->psz_senc = NULL;
419     p_sys->p_spu_cfg = NULL;
420     p_sys->i_scodec = 0;
421
422     var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
423     if( val.psz_string && *val.psz_string )
424     {
425         char *psz_next;
426         psz_next = config_ChainCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
427                                    val.psz_string );
428         free( psz_next );
429     }
430     free( val.psz_string );
431
432     var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
433     if( val.psz_string && *val.psz_string )
434     {
435         char fcc[4] = "    ";
436         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
437         p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
438     }
439     free( val.psz_string );
440
441     if( p_sys->i_scodec )
442     {
443         msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
444     }
445
446     var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
447     p_sys->b_soverlay = val.b_bool;
448
449     var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
450     if( val.psz_string && *val.psz_string )
451     {
452         p_sys->p_spu = spu_Create( p_stream );
453         if( p_sys->p_spu )
454             spu_ChangeFilters( p_sys->p_spu, val.psz_string );
455     }
456     free( val.psz_string );
457
458     /* OSD menu transcoding parameters */
459     p_sys->psz_osdenc = NULL;
460     p_sys->p_osd_cfg  = NULL;
461     p_sys->i_osdcodec = 0;
462     p_sys->b_osd   = false;
463
464     var_Get( p_stream, SOUT_CFG_PREFIX "osd", &val );
465     if( val.b_bool )
466     {
467         char *psz_next;
468
469         psz_next = config_ChainCreate( &p_sys->psz_osdenc,
470                                    &p_sys->p_osd_cfg, strdup( "dvbsub") );
471         free( psz_next );
472
473         p_sys->i_osdcodec = VLC_CODEC_YUVP;
474
475         msg_Dbg( p_stream, "codec osd=%4.4s", (char *)&p_sys->i_osdcodec );
476
477         if( !p_sys->p_spu )
478         {
479             p_sys->p_spu = spu_Create( p_stream );
480             if( p_sys->p_spu )
481                 spu_ChangeFilters( p_sys->p_spu, "osdmenu" );
482         }
483         else
484         {
485             spu_ChangeFilters( p_sys->p_spu, "osdmenu" );
486         }
487     }
488
489     /* Audio settings */
490     var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
491     p_sys->b_master_sync = val.b_bool;
492     if( p_sys->f_fps > 0 ) p_sys->b_master_sync = true;
493
494     p_stream->pf_add    = Add;
495     p_stream->pf_del    = Del;
496     p_stream->pf_send   = Send;
497     p_stream->p_sys     = p_sys;
498
499     return VLC_SUCCESS;
500 }
501
502 /*****************************************************************************
503  * Close:
504  *****************************************************************************/
505 static void Close( vlc_object_t * p_this )
506 {
507     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
508     sout_stream_sys_t   *p_sys = p_stream->p_sys;
509
510     free( p_sys->psz_af );
511
512     config_ChainDestroy( p_sys->p_audio_cfg );
513     free( p_sys->psz_aenc );
514     free( p_sys->psz_alang );
515
516     free( p_sys->psz_vf2 );
517
518     config_ChainDestroy( p_sys->p_video_cfg );
519     free( p_sys->psz_venc );
520
521     config_ChainDestroy( p_sys->p_deinterlace_cfg );
522     free( p_sys->psz_deinterlace );
523
524     config_ChainDestroy( p_sys->p_spu_cfg );
525     free( p_sys->psz_senc );
526
527     if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
528
529     config_ChainDestroy( p_sys->p_osd_cfg );
530     free( p_sys->psz_osdenc );
531
532     vlc_object_release( p_sys );
533 }
534
535 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
536 {
537     sout_stream_sys_t *p_sys = p_stream->p_sys;
538     sout_stream_id_t *id;
539
540     id = calloc( 1, sizeof( sout_stream_id_t ) );
541     if( !id )
542         goto error;
543
544     id->id = NULL;
545     id->p_decoder = NULL;
546     id->p_encoder = NULL;
547
548     /* Create decoder object */
549     id->p_decoder = vlc_object_create( p_stream, sizeof( decoder_t ) );
550     if( !id->p_decoder )
551         goto error;
552     vlc_object_attach( id->p_decoder, p_stream );
553     id->p_decoder->p_module = NULL;
554     id->p_decoder->fmt_in = *p_fmt;
555     id->p_decoder->b_pace_control = true;
556
557     /* Create encoder object */
558     id->p_encoder = sout_EncoderCreate( p_stream );
559     if( !id->p_encoder )
560         goto error;
561     vlc_object_attach( id->p_encoder, p_stream );
562     id->p_encoder->p_module = NULL;
563
564     /* Create destination format */
565     es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
566     id->p_encoder->fmt_out.i_id    = p_fmt->i_id;
567     id->p_encoder->fmt_out.i_group = p_fmt->i_group;
568
569     if( p_sys->psz_alang )
570         id->p_encoder->fmt_out.psz_language = strdup( p_sys->psz_alang );
571     else if( p_fmt->psz_language )
572         id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
573
574     bool success;
575
576     if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
577         success = transcode_audio_add(p_stream, p_fmt, id);
578     else if( p_fmt->i_cat == VIDEO_ES && (p_sys->i_vcodec || p_sys->psz_venc) )
579         success = transcode_video_add(p_stream, p_fmt, id);
580     else if( ( p_fmt->i_cat == SPU_ES ) &&
581              ( p_sys->i_scodec || p_sys->psz_senc || p_sys->b_soverlay ) )
582         success = transcode_spu_add(p_stream, p_fmt, id);
583     else if( !p_sys->b_osd && (p_sys->i_osdcodec != 0 || p_sys->psz_osdenc) )
584         success = transcode_osd_add(p_stream, p_fmt, id);
585     else
586     {
587         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
588                  (char*)&p_fmt->i_codec );
589         id->id = sout_StreamIdAdd( p_stream->p_next, p_fmt );
590         id->b_transcode = false;
591
592         success = id->id;
593     }
594
595     if(!success)
596         goto error;
597
598     return id;
599
600 error:
601     if( id )
602     {
603         if( id->p_decoder )
604         {
605             vlc_object_release( id->p_decoder );
606             id->p_decoder = NULL;
607         }
608
609         if( id->p_encoder )
610         {
611             es_format_Clean( &id->p_encoder->fmt_out );
612             vlc_object_release( id->p_encoder );
613             id->p_encoder = NULL;
614         }
615
616         free( id );
617     }
618     return NULL;
619 }
620
621 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
622 {
623     sout_stream_sys_t *p_sys = p_stream->p_sys;
624
625     if( id->b_transcode )
626     {
627         switch( id->p_decoder->fmt_in.i_cat )
628         {
629         case AUDIO_ES:
630             transcode_audio_close( id );
631             break;
632         case VIDEO_ES:
633             Send( p_stream, id, NULL );
634             transcode_video_close( p_stream, id );
635             break;
636         case SPU_ES:
637             if( p_sys->b_osd )
638                 transcode_osd_close( p_stream, id );
639             else
640                 transcode_spu_close( id );
641             break;
642         }
643     }
644
645     if( id->id ) sout_StreamIdDel( p_stream->p_next, id->id );
646
647     if( id->p_decoder )
648     {
649         vlc_object_release( id->p_decoder );
650         id->p_decoder = NULL;
651     }
652
653     if( id->p_encoder )
654     {
655         es_format_Clean( &id->p_encoder->fmt_out );
656         vlc_object_release( id->p_encoder );
657         id->p_encoder = NULL;
658     }
659     free( id );
660
661     return VLC_SUCCESS;
662 }
663
664 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
665                  block_t *p_buffer )
666 {
667     sout_stream_sys_t *p_sys = p_stream->p_sys;
668     block_t *p_out = NULL;
669
670     if( !id->b_transcode )
671     {
672         if( id->id )
673             return sout_StreamIdSend( p_stream->p_next, id->id, p_buffer );
674
675         block_Release( p_buffer );
676         return VLC_EGENERIC;
677     }
678
679     switch( id->p_decoder->fmt_in.i_cat )
680     {
681     case AUDIO_ES:
682         transcode_audio_process( p_stream, id, p_buffer, &p_out );
683         break;
684
685     case VIDEO_ES:
686         if( transcode_video_process( p_stream, id, p_buffer, &p_out )
687             != VLC_SUCCESS )
688         {
689             return VLC_EGENERIC;
690         }
691         break;
692
693     case SPU_ES:
694         /* Transcode OSD menu pictures. */
695         if( p_sys->b_osd )
696         {
697             if( transcode_osd_process( p_stream, id, p_buffer, &p_out ) !=
698                 VLC_SUCCESS )
699             {
700                 return VLC_EGENERIC;
701             }
702         }
703         else if ( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
704             VLC_SUCCESS )
705         {
706             return VLC_EGENERIC;
707         }
708         break;
709
710     default:
711         p_out = NULL;
712         block_Release( p_buffer );
713         break;
714     }
715
716     if( p_out )
717         return sout_StreamIdSend( p_stream->p_next, id->id, p_out );
718     return VLC_SUCCESS;
719 }