stream_out/*: String review (refs #438)
[vlc.git] / modules / stream_out / transcode.c
1 /*****************************************************************************
2  * transcode.c: transcoding stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 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  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/input.h>
35 #include <vlc/sout.h>
36 #include <vlc/vout.h>
37 #include <vlc/decoder.h>
38 #include "vlc_filter.h"
39 #include "vlc_osd.h"
40
41 #define MASTER_SYNC_MAX_DRIFT 100000
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 #define VENC_TEXT N_("Video encoder")
47 #define VENC_LONGTEXT N_( \
48     "Allows you to specify the video encoder to use and its associated " \
49     "options." )
50 #define VCODEC_TEXT N_("Destination video codec")
51 #define VCODEC_LONGTEXT N_( \
52     "Allows you to specify the destination video codec used for the " \
53     "streaming output." )
54 #define VB_TEXT N_("Video bitrate")
55 #define VB_LONGTEXT N_( \
56     "Allows you to specify the video bitrate used for the streaming " \
57     "output." )
58 #define SCALE_TEXT N_("Video scaling")
59 #define SCALE_LONGTEXT N_( \
60     "Allows you to scale the video before encoding." )
61 #define FPS_TEXT N_("Video frame-rate")
62 #define FPS_LONGTEXT N_( \
63     "Allows you to specify an output frame rate for the video." )
64 #define DEINTERLACE_TEXT N_("Deinterlace video")
65 #define DEINTERLACE_LONGTEXT N_( \
66     "Allows you to deinterlace the video before encoding." )
67 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
68 #define DEINTERLACE_MODULE_LONGTEXT N_( \
69     "Specifies the deinterlace module to use." )
70 #define WIDTH_TEXT N_("Video width")
71 #define WIDTH_LONGTEXT N_( \
72     "Allows you to specify the output video width." )
73 #define HEIGHT_TEXT N_("Video height")
74 #define HEIGHT_LONGTEXT N_( \
75     "Allows you to specify the output video height." )
76 #define MAXWIDTH_TEXT N_("Maximum video width")
77 #define MAXWIDTH_LONGTEXT N_( \
78     "Allows you to specify a maximum output video width." )
79 #define MAXHEIGHT_TEXT N_("Maximum video height")
80 #define MAXHEIGHT_LONGTEXT N_( \
81     "Allows you to specify a maximum output video height." )
82 #define VFILTER_TEXT N_("Video filter")
83 #define VFILTER_LONGTEXT N_( \
84     "Allows you to specify video filters used after the video " \
85     "transcoding and subpictures overlaying." )
86
87 #define CROPTOP_TEXT N_("Video crop top")
88 #define CROPTOP_LONGTEXT N_( \
89     "Allows you to specify the top coordinate for the video cropping." )
90 #define CROPLEFT_TEXT N_("Video crop left")
91 #define CROPLEFT_LONGTEXT N_( \
92     "Allows you to specify the left coordinate for the video cropping." )
93 #define CROPBOTTOM_TEXT N_("Video crop bottom")
94 #define CROPBOTTOM_LONGTEXT N_( \
95     "Allows you to specify the bottom coordinate for the video cropping." )
96 #define CROPRIGHT_TEXT N_("Video crop right")
97 #define CROPRIGHT_LONGTEXT N_( \
98     "Allows you to specify the right coordinate for the video cropping." )
99
100 #define PADDTOP_TEXT N_("Video padd top")
101 #define PADDTOP_LONGTEXT N_( \
102     "Allows you to specify padding of black lines at the top." )
103 #define PADDLEFT_TEXT N_("Video padd left")
104 #define PADDLEFT_LONGTEXT N_( \
105     "Allows you to specify padding of black lines on the left." )
106 #define PADDBOTTOM_TEXT N_("Video padd bottom")
107 #define PADDBOTTOM_LONGTEXT N_( \
108     "Allows you to specify padding of black lines at the top." )
109 #define PADDRIGHT_TEXT N_("Video padd right")
110 #define PADDRIGHT_LONGTEXT N_( \
111     "Allows you to specify padding of black lines on the right." )
112
113 #define CANVAS_WIDTH_TEXT N_("Video canvas width")
114 #define CANVAS_WIDTH_LONGTEXT N_( \
115     "Allows to padd or crop the frame to a specified width." )
116 #define CANVAS_HEIGHT_TEXT N_("Video canvas height")
117 #define CANVAS_HEIGHT_LONGTEXT N_( \
118     "Allows to padd or crop the frame to a specified height." )
119 #define CANVAS_ASPECT_TEXT N_("Video canvas aspect ratio")
120 #define CANVAS_ASPECT_LONGTEXT N_( \
121     "Set aspect (like 4:3) of video canvas and letterbox accordingly." )
122
123 #define AENC_TEXT N_("Audio encoder")
124 #define AENC_LONGTEXT N_( \
125     "Allows you to specify the audio encoder to use and its associated " \
126     "options." )
127 #define ACODEC_TEXT N_("Destination audio codec")
128 #define ACODEC_LONGTEXT N_( \
129     "Allows you to specify the destination audio codec used for the " \
130     "streaming output." )
131 #define AB_TEXT N_("Audio bitrate")
132 #define AB_LONGTEXT N_( \
133     "Allows you to specify the audio bitrate used for the streaming " \
134     "output." )
135 #define ARATE_TEXT N_("Audio sample rate")
136 #define ARATE_LONGTEXT N_( \
137     "Allows you to specify the audio sample rate used for the streaming " \
138     "output." )
139 #define ACHANS_TEXT N_("Audio channels")
140 #define ACHANS_LONGTEXT N_( \
141     "Allows you to specify the number of audio channels used for the " \
142     "streaming output." )
143
144 #define SENC_TEXT N_("Subtitles encoder")
145 #define SENC_LONGTEXT N_( \
146     "Allows you to specify the subtitles encoder to use and its associated " \
147     "options." )
148 #define SCODEC_TEXT N_("Destination subtitles codec")
149 #define SCODEC_LONGTEXT N_( \
150     "Allows you to specify the destination subtitles codec used for the " \
151     "streaming output." )
152 #define SFILTER_TEXT N_("Subpictures filter")
153 #define SFILTER_LONGTEXT N_( \
154     "Allows you to specify subpictures filters used during the video " \
155     "transcoding. The subpictures produced by the filters will be overlayed " \
156     "directly onto the video." )
157
158 #define OSD_TEXT N_("OSD menu")
159 #define OSD_LONGTEXT N_(\
160     "Enable streaming of the On Screen Display. It uses the osdmenu subfilter." )
161
162 #define THREADS_TEXT N_("Number of threads")
163 #define THREADS_LONGTEXT N_( \
164     "Allows you to specify the number of threads used for the transcoding." )
165 #define HP_TEXT N_("High priority")
166 #define HP_LONGTEXT N_( \
167     "Runs the optional encoder thread at the OUTPUT priority instead of " \
168     "VIDEO." )
169
170 #define ASYNC_TEXT N_("Synchronise on audio track")
171 #define ASYNC_LONGTEXT N_( \
172     "This option will drop/duplicate video frames to synchronise the video " \
173     "track on the audio track." )
174
175 #define HURRYUP_TEXT N_( "Hurry up" )
176 #define HURRYUP_LONGTEXT N_( "Allows you to specify if the transcoder " \
177   "should drop frames if your CPU can't keep up with the encoding rate." )
178
179 static char *ppsz_deinterlace_type[] =
180 {
181     "deinterlace", "ffmpeg-deinterlace"
182 };
183
184 static int  Open ( vlc_object_t * );
185 static void Close( vlc_object_t * );
186
187 #define SOUT_CFG_PREFIX "sout-transcode-"
188
189 vlc_module_begin();
190     set_shortname( _("Transcode"));
191     set_description( _("Transcode stream output") );
192     set_capability( "sout stream", 50 );
193     add_shortcut( "transcode" );
194     set_callbacks( Open, Close );
195     set_category( CAT_SOUT );
196     set_subcategory( SUBCAT_SOUT_STREAM );
197     set_section( N_("Video"), NULL );
198     add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
199                 VENC_LONGTEXT, VLC_FALSE );
200     add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
201                 VCODEC_LONGTEXT, VLC_FALSE );
202     add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
203                  VB_LONGTEXT, VLC_FALSE );
204     add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
205                SCALE_LONGTEXT, VLC_FALSE );
206     add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
207                FPS_LONGTEXT, VLC_FALSE );
208     add_bool( SOUT_CFG_PREFIX "hurry-up", VLC_TRUE, NULL, HURRYUP_TEXT,
209                HURRYUP_LONGTEXT, VLC_FALSE );
210     add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
211               DEINTERLACE_LONGTEXT, VLC_FALSE );
212     add_string( SOUT_CFG_PREFIX "deinterlace-module", "deinterlace", NULL,
213                 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
214                 VLC_FALSE );
215         change_string_list( ppsz_deinterlace_type, 0, 0 );
216     add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
217                  WIDTH_LONGTEXT, VLC_TRUE );
218     add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
219                  HEIGHT_LONGTEXT, VLC_TRUE );
220     add_integer( SOUT_CFG_PREFIX "maxwidth", 0, NULL, MAXWIDTH_TEXT,
221                  MAXWIDTH_LONGTEXT, VLC_TRUE );
222     add_integer( SOUT_CFG_PREFIX "maxheight", 0, NULL, MAXHEIGHT_TEXT,
223                  MAXHEIGHT_LONGTEXT, VLC_TRUE );
224     add_module_list_cat( SOUT_CFG_PREFIX "vfilter", SUBCAT_VIDEO_VFILTER,
225                      NULL, NULL,
226                      VFILTER_TEXT, VFILTER_LONGTEXT, VLC_FALSE );
227
228     add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
229                  CROPTOP_LONGTEXT, VLC_TRUE );
230     add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
231                  CROPLEFT_LONGTEXT, VLC_TRUE );
232     add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
233                  CROPBOTTOM_LONGTEXT, VLC_TRUE );
234     add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
235                  CROPRIGHT_LONGTEXT, VLC_TRUE );
236
237     add_integer( SOUT_CFG_PREFIX "paddtop", 0, NULL, PADDTOP_TEXT,
238                  PADDTOP_LONGTEXT, VLC_TRUE );
239     add_integer( SOUT_CFG_PREFIX "paddleft", 0, NULL, PADDLEFT_TEXT,
240                  PADDLEFT_LONGTEXT, VLC_TRUE );
241     add_integer( SOUT_CFG_PREFIX "paddbottom", 0, NULL, PADDBOTTOM_TEXT,
242                  PADDBOTTOM_LONGTEXT, VLC_TRUE );
243     add_integer( SOUT_CFG_PREFIX "paddright", 0, NULL, PADDRIGHT_TEXT,
244                  PADDRIGHT_LONGTEXT, VLC_TRUE );
245
246     add_integer( SOUT_CFG_PREFIX "canvas-width", 0, NULL, CANVAS_WIDTH_TEXT,
247                  CANVAS_WIDTH_LONGTEXT, VLC_TRUE );
248     add_integer( SOUT_CFG_PREFIX "canvas-height", 0, NULL, CANVAS_HEIGHT_TEXT,
249                  CANVAS_HEIGHT_LONGTEXT, VLC_TRUE );
250     add_string( SOUT_CFG_PREFIX "canvas-aspect", NULL, NULL, CANVAS_ASPECT_TEXT,
251                 CANVAS_ASPECT_LONGTEXT, VLC_FALSE );
252
253     set_section( N_("Audio"), NULL );
254     add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
255                 AENC_LONGTEXT, VLC_FALSE );
256     add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
257                 ACODEC_LONGTEXT, VLC_FALSE );
258     add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
259                  AB_LONGTEXT, VLC_FALSE );
260     add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
261                  ACHANS_LONGTEXT, VLC_FALSE );
262     add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
263                  ARATE_LONGTEXT, VLC_TRUE );
264     add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
265               ASYNC_LONGTEXT, VLC_FALSE );
266
267     set_section( N_("Overlays/Subtitles"), NULL );
268     add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
269                 SENC_LONGTEXT, VLC_FALSE );
270     add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
271                 SCODEC_LONGTEXT, VLC_FALSE );
272     add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
273                SCODEC_LONGTEXT, VLC_FALSE );
274     add_module_list_cat( SOUT_CFG_PREFIX "sfilter", SUBCAT_VIDEO_SUBPIC,
275                      NULL, NULL,
276                      SFILTER_TEXT, SFILTER_LONGTEXT, VLC_FALSE );
277
278     set_section( N_("On Screen Display"), NULL );
279     add_bool( SOUT_CFG_PREFIX "osd", 0, NULL, OSD_TEXT,
280               OSD_LONGTEXT, VLC_FALSE );
281     
282     set_section( N_("Miscellaneous"), NULL );
283     add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
284                  THREADS_LONGTEXT, VLC_TRUE );
285     add_bool( SOUT_CFG_PREFIX "high-priority", 0, NULL, HP_TEXT, HP_LONGTEXT,
286               VLC_TRUE );
287
288 vlc_module_end();
289
290 static const char *ppsz_sout_options[] = {
291     "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
292     "paddtop", "paddbottom", "paddleft", "paddright", 
293     "canvas-width", "canvas-height", "canvas-aspect", 
294     "scale", "fps", "width", "height", "vfilter", "deinterlace",
295     "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab",
296     "samplerate", "channels", "senc", "scodec", "soverlay", "sfilter",
297     "osd", "audio-sync", "high-priority", "maxwidth", "maxheight", NULL
298 };
299
300 /*****************************************************************************
301  * Exported prototypes
302  *****************************************************************************/
303 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
304 static int               Del ( sout_stream_t *, sout_stream_id_t * );
305 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
306
307 static int  transcode_audio_new    ( sout_stream_t *, sout_stream_id_t * );
308 static void transcode_audio_close  ( sout_stream_t *, sout_stream_id_t * );
309 static int  transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
310                                      block_t *, block_t ** );
311
312 static aout_buffer_t *audio_new_buffer( decoder_t *, int );
313 static void audio_del_buffer( decoder_t *, aout_buffer_t * );
314
315 static int  transcode_video_new    ( sout_stream_t *, sout_stream_id_t * );
316 static void transcode_video_close  ( sout_stream_t *, sout_stream_id_t * );
317 static int  transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
318 static int  transcode_video_process( sout_stream_t *, sout_stream_id_t *,
319                                      block_t *, block_t ** );
320
321 static void video_del_buffer( vlc_object_t *, picture_t * );
322 static picture_t *video_new_buffer_decoder( decoder_t * );
323 static void video_del_buffer_decoder( decoder_t *, picture_t * );
324 static void video_link_picture_decoder( decoder_t *, picture_t * );
325 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
326 static picture_t *video_new_buffer_filter( filter_t * );
327 static void video_del_buffer_filter( filter_t *, picture_t * );
328
329 static int  transcode_spu_new    ( sout_stream_t *, sout_stream_id_t * );
330 static void transcode_spu_close  ( sout_stream_t *, sout_stream_id_t * );
331 static int  transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
332                                    block_t *, block_t ** );
333
334 static int  transcode_osd_new    ( sout_stream_t *, sout_stream_id_t * );
335 static void transcode_osd_close  ( sout_stream_t *, sout_stream_id_t * );
336 static int  transcode_osd_process( sout_stream_t *, sout_stream_id_t *,
337                                    block_t *, block_t ** );
338
339 static int  EncoderThread( struct sout_stream_sys_t * p_sys );
340
341 static int pi_channels_maps[6] =
342 {
343     0,
344     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
345     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
346     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
347      | AOUT_CHAN_REARRIGHT,
348     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
349      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
350 };
351
352 #define PICTURE_RING_SIZE 64
353 #define SUBPICTURE_RING_SIZE 20
354
355 struct sout_stream_sys_t
356 {
357     VLC_COMMON_MEMBERS
358
359     sout_stream_t   *p_out;
360     sout_stream_id_t *id_video;
361     block_t         *p_buffers;
362     vlc_mutex_t     lock_out;
363     vlc_cond_t      cond;
364     picture_t *     pp_pics[PICTURE_RING_SIZE];
365     int             i_first_pic, i_last_pic;
366
367     /* Audio */
368     vlc_fourcc_t    i_acodec;   /* codec audio (0 if not transcode) */
369     char            *psz_aenc;
370     sout_cfg_t      *p_audio_cfg;
371     int             i_sample_rate;
372     int             i_channels;
373     int             i_abitrate;
374
375     /* Video */
376     vlc_fourcc_t    i_vcodec;   /* codec video (0 if not transcode) */
377     char            *psz_venc;
378     sout_cfg_t      *p_video_cfg;
379     int             i_vbitrate;
380     double          f_scale;
381     double          f_fps;
382     unsigned int    i_width, i_maxwidth;
383     unsigned int    i_height, i_maxheight;
384     vlc_bool_t      b_deinterlace;
385     char            *psz_deinterlace;
386     sout_cfg_t      *p_deinterlace_cfg;
387     int             i_threads;
388     vlc_bool_t      b_high_priority;
389     vlc_bool_t      b_hurry_up;
390     char            *psz_vfilters[10];
391     sout_cfg_t      *p_vfilters_cfg[10];
392     int             i_vfilters;
393
394     int             i_crop_top;
395     int             i_crop_bottom;
396     int             i_crop_right;
397     int             i_crop_left;
398
399     int             i_padd_top;
400     int             i_padd_bottom;
401     int             i_padd_right;
402     int             i_padd_left;
403
404     int             i_canvas_width;
405     int             i_canvas_height;
406     int             i_canvas_aspect;
407     
408     /* Video, calculated */
409     int             i_src_x_offset;
410     int             i_src_y_offset;
411     int             i_crop_width;
412     int             i_crop_height;
413
414     int             i_dst_x_offset;
415     int             i_dst_y_offset;
416     int             i_nopadd_width;
417     int             i_nopadd_height;
418
419     /* SPU */
420     vlc_fourcc_t    i_scodec;   /* codec spu (0 if not transcode) */
421     char            *psz_senc;
422     vlc_bool_t      b_soverlay;
423     sout_cfg_t      *p_spu_cfg;
424     spu_t           *p_spu;
425
426     /* OSD Menu */
427     sout_stream_id_t *id_osd;   /* extension for streaming OSD menus */
428     vlc_fourcc_t    i_osdcodec; /* codec osd menu (0 if not transcode) */
429     char            *psz_osdenc;
430     sout_cfg_t      *p_osd_cfg;
431     vlc_bool_t      b_es_osd;      /* VLC_TRUE when osd es is registered */
432     vlc_bool_t      b_sout_osd;
433
434     /* Sync */
435     vlc_bool_t      b_master_sync;
436     mtime_t         i_master_drift;
437 };
438
439 struct decoder_owner_sys_t
440 {
441     picture_t *pp_pics[PICTURE_RING_SIZE];
442     sout_stream_sys_t *p_sys;
443 };
444 struct filter_owner_sys_t
445 {
446     picture_t *pp_pics[PICTURE_RING_SIZE];
447     sout_stream_sys_t *p_sys;
448 };
449
450 /*****************************************************************************
451  * Open:
452  *****************************************************************************/
453 static int Open( vlc_object_t *p_this )
454 {
455     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
456     sout_stream_sys_t *p_sys;
457     vlc_value_t       val;
458
459     p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
460
461     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
462     if( !p_sys->p_out )
463     {
464         msg_Err( p_stream, "cannot create chain" );
465         vlc_object_destroy( p_sys );
466         return VLC_EGENERIC;
467     }
468
469     p_sys->i_master_drift = 0;
470
471     sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
472                    p_stream->p_cfg );
473
474     /* Audio transcoding parameters */
475     var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
476     p_sys->psz_aenc = NULL;
477     p_sys->p_audio_cfg = NULL;
478     if( val.psz_string && *val.psz_string )
479     {
480         char *psz_next;
481         psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
482                                    val.psz_string );
483         if( psz_next ) free( psz_next );
484     }
485     if( val.psz_string ) free( val.psz_string );
486
487     var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
488     p_sys->i_acodec = 0;
489     if( val.psz_string && *val.psz_string )
490     {
491         char fcc[4] = "    ";
492         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
493         p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
494     }
495     if( val.psz_string ) free( val.psz_string );
496
497     var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
498     p_sys->i_abitrate = val.i_int;
499     if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
500
501     var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
502     p_sys->i_sample_rate = val.i_int;
503
504     var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
505     p_sys->i_channels = val.i_int;
506
507     if( p_sys->i_acodec )
508     {
509         if( (strncmp( (char *)&p_sys->i_acodec, "mp3", 3) == 0) &&
510                             (p_sys->i_channels > 2) )
511         {
512             msg_Warn( p_stream, "%d channels invalid for mp3, forcing to 2",
513                       p_sys->i_channels );
514             p_sys->i_channels = 2;
515         }
516         msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
517                  (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
518                  p_sys->i_channels, p_sys->i_abitrate / 1000 );
519     }
520
521     /* Video transcoding parameters */
522     var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
523     p_sys->psz_venc = NULL;
524     p_sys->p_video_cfg = NULL;
525     if( val.psz_string && *val.psz_string )
526     {
527         char *psz_next;
528         psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
529                                    val.psz_string );
530         if( psz_next ) free( psz_next );
531     }
532     if( val.psz_string ) free( val.psz_string );
533
534     var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
535     p_sys->i_vcodec = 0;
536     if( val.psz_string && *val.psz_string )
537     {
538         char fcc[4] = "    ";
539         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
540         p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
541     }
542     if( val.psz_string ) free( val.psz_string );
543
544     var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
545     p_sys->i_vbitrate = val.i_int;
546     if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
547
548     var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
549     p_sys->f_scale = val.f_float;
550
551     var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
552     p_sys->f_fps = val.f_float;
553
554     var_Get( p_stream, SOUT_CFG_PREFIX "hurry-up", &val );
555     p_sys->b_hurry_up = val.b_bool;
556
557     var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
558     p_sys->i_width = val.i_int;
559
560     var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
561     p_sys->i_height = val.i_int;
562
563     var_Get( p_stream, SOUT_CFG_PREFIX "maxwidth", &val );
564     p_sys->i_maxwidth = val.i_int;
565
566     var_Get( p_stream, SOUT_CFG_PREFIX "maxheight", &val );
567     p_sys->i_maxheight = val.i_int;
568
569     var_Get( p_stream, SOUT_CFG_PREFIX "vfilter", &val );
570     p_sys->i_vfilters = 0;
571     if( val.psz_string && *val.psz_string )
572     {
573         char *psz_parser = val.psz_string;
574
575         while( psz_parser != NULL && *psz_parser != '\0' )
576         {
577             psz_parser = sout_CfgCreate(
578                                    &p_sys->psz_vfilters[p_sys->i_vfilters],
579                                    &p_sys->p_vfilters_cfg[p_sys->i_vfilters],
580                                    psz_parser );
581             p_sys->i_vfilters++;
582             if( psz_parser != NULL && *psz_parser != '\0' ) psz_parser++;
583         }
584     }
585     if( val.psz_string ) free( val.psz_string );
586     p_sys->psz_vfilters[p_sys->i_vfilters] = NULL;
587     p_sys->p_vfilters_cfg[p_sys->i_vfilters] = NULL;
588
589     var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
590     p_sys->b_deinterlace = val.b_bool;
591
592     var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace-module", &val );
593     p_sys->psz_deinterlace = NULL;
594     p_sys->p_deinterlace_cfg = NULL;
595     if( val.psz_string && *val.psz_string )
596     {
597         char *psz_next;
598         psz_next = sout_CfgCreate( &p_sys->psz_deinterlace,
599                                    &p_sys->p_deinterlace_cfg,
600                                    val.psz_string );
601         if( psz_next ) free( psz_next );
602     }
603     if( val.psz_string ) free( val.psz_string );
604
605     var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
606     p_sys->i_crop_top = val.i_int;
607     var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
608     p_sys->i_crop_bottom = val.i_int;
609     var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
610     p_sys->i_crop_left = val.i_int;
611     var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
612     p_sys->i_crop_right = val.i_int;
613
614     var_Get( p_stream, SOUT_CFG_PREFIX "paddtop", &val );
615     p_sys->i_padd_top = val.i_int;
616     var_Get( p_stream, SOUT_CFG_PREFIX "paddbottom", &val );
617     p_sys->i_padd_bottom = val.i_int;
618     var_Get( p_stream, SOUT_CFG_PREFIX "paddleft", &val );
619     p_sys->i_padd_left = val.i_int;
620     var_Get( p_stream, SOUT_CFG_PREFIX "paddright", &val );
621     p_sys->i_padd_right = val.i_int;
622     
623     var_Get( p_stream, SOUT_CFG_PREFIX "canvas-width", &val );
624     p_sys->i_canvas_width = val.i_int;
625     var_Get( p_stream, SOUT_CFG_PREFIX "canvas-height", &val );
626     p_sys->i_canvas_height = val.i_int;
627     
628     var_Get( p_stream, SOUT_CFG_PREFIX "canvas-aspect", &val );
629     if ( val.psz_string )
630     {
631         char *psz_parser = strchr( val.psz_string, ':' );
632
633         if( psz_parser )
634         {
635             *psz_parser++ = '\0';
636             p_sys->i_canvas_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
637                 / atoi( psz_parser );
638         }
639         else
640         {
641             msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
642             p_sys->i_canvas_aspect = 0;
643         }
644
645         free( val.psz_string );
646     }
647     else
648     {
649         p_sys->i_canvas_aspect = 0;
650     }
651
652     var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
653     p_sys->i_threads = val.i_int;
654     var_Get( p_stream, SOUT_CFG_PREFIX "high-priority", &val );
655     p_sys->b_high_priority = val.b_bool;
656
657     if( p_sys->i_vcodec )
658     {
659         msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
660                  (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
661                  p_sys->f_scale, p_sys->i_vbitrate / 1000 );
662     }
663
664     /* Subpictures transcoding parameters */
665     p_sys->p_spu = NULL;
666     p_sys->psz_senc = NULL;
667     p_sys->p_spu_cfg = NULL;
668     p_sys->i_scodec = 0;
669
670     var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
671     if( val.psz_string && *val.psz_string )
672     {
673         char *psz_next;
674         psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
675                                    val.psz_string );
676         if( psz_next ) free( psz_next );
677     }
678     if( val.psz_string ) free( val.psz_string );
679
680     var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
681     if( val.psz_string && *val.psz_string )
682     {
683         char fcc[4] = "    ";
684         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
685         p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
686     }
687     if( val.psz_string ) free( val.psz_string );
688
689     if( p_sys->i_scodec )
690     {
691         msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
692     }
693
694     var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
695     p_sys->b_soverlay = val.b_bool;
696
697     var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
698     if( val.psz_string && *val.psz_string )
699     {
700         p_sys->p_spu = spu_Create( p_stream );
701         var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
702         var_Set( p_sys->p_spu, "sub-filter", val );
703         spu_Init( p_sys->p_spu );
704     }
705     if( val.psz_string ) free( val.psz_string );
706
707     /* OSD menu transcoding parameters */
708     p_sys->psz_osdenc = NULL;
709     p_sys->p_osd_cfg  = NULL;
710     p_sys->i_osdcodec = 0;
711     p_sys->b_es_osd   = VLC_FALSE;
712
713     var_Get( p_stream, SOUT_CFG_PREFIX "osd", &val );
714     p_sys->b_sout_osd = val.b_bool;
715     if( p_sys->b_sout_osd )
716     {
717         vlc_value_t osd_val;
718         char *psz_next;
719
720         psz_next = sout_CfgCreate( &p_sys->psz_osdenc,
721                                    &p_sys->p_osd_cfg, strdup( "dvbsub") );
722         if( psz_next ) free( psz_next );
723
724         p_sys->i_osdcodec = VLC_FOURCC('Y','U','V','P' );
725
726         msg_Dbg( p_stream, "codec osd=%4.4s", (char *)&p_sys->i_osdcodec );
727
728         if( !p_sys->p_spu )
729         {
730             osd_val.psz_string = strdup("osdmenu");
731             p_sys->p_spu = spu_Create( p_stream );
732             var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
733             var_Set( p_sys->p_spu, "sub-filter", osd_val );
734             spu_Init( p_sys->p_spu );
735             if( osd_val.psz_string ) free( osd_val.psz_string );
736         }
737         else
738         {
739             osd_val.psz_string = strdup("osdmenu");
740             var_Set( p_sys->p_spu, "sub-filter", osd_val );
741             if( osd_val.psz_string ) free( osd_val.psz_string );
742         }
743     }
744
745     /* Audio settings */
746     var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
747     p_sys->b_master_sync = val.b_bool;
748     if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
749
750     p_stream->pf_add    = Add;
751     p_stream->pf_del    = Del;
752     p_stream->pf_send   = Send;
753     p_stream->p_sys     = p_sys;
754
755     return VLC_SUCCESS;
756 }
757
758 /*****************************************************************************
759  * Close:
760  *****************************************************************************/
761 static void Close( vlc_object_t * p_this )
762 {
763     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
764     sout_stream_sys_t   *p_sys = p_stream->p_sys;
765
766     sout_StreamDelete( p_sys->p_out );
767
768     while( p_sys->p_audio_cfg != NULL )
769     {
770         sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
771
772         if( p_sys->p_audio_cfg->psz_name )
773             free( p_sys->p_audio_cfg->psz_name );
774         if( p_sys->p_audio_cfg->psz_value )
775             free( p_sys->p_audio_cfg->psz_value );
776         free( p_sys->p_audio_cfg );
777
778         p_sys->p_audio_cfg = p_next;
779     }
780     if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
781
782     while( p_sys->p_video_cfg != NULL )
783     {
784         sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
785
786         if( p_sys->p_video_cfg->psz_name )
787             free( p_sys->p_video_cfg->psz_name );
788         if( p_sys->p_video_cfg->psz_value )
789             free( p_sys->p_video_cfg->psz_value );
790         free( p_sys->p_video_cfg );
791
792         p_sys->p_video_cfg = p_next;
793     }
794     if( p_sys->psz_venc ) free( p_sys->psz_venc );
795
796     while( p_sys->p_deinterlace_cfg != NULL )
797     {
798         sout_cfg_t *p_next = p_sys->p_deinterlace_cfg->p_next;
799
800         if( p_sys->p_deinterlace_cfg->psz_name )
801             free( p_sys->p_deinterlace_cfg->psz_name );
802         if( p_sys->p_deinterlace_cfg->psz_value )
803             free( p_sys->p_deinterlace_cfg->psz_value );
804         free( p_sys->p_deinterlace_cfg );
805
806         p_sys->p_deinterlace_cfg = p_next;
807     }
808     if( p_sys->psz_deinterlace ) free( p_sys->psz_deinterlace );
809
810     while( p_sys->p_spu_cfg != NULL )
811     {
812         sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
813
814         if( p_sys->p_spu_cfg->psz_name )
815             free( p_sys->p_spu_cfg->psz_name );
816         if( p_sys->p_spu_cfg->psz_value )
817             free( p_sys->p_spu_cfg->psz_value );
818         free( p_sys->p_spu_cfg );
819
820         p_sys->p_spu_cfg = p_next;
821     }
822     if( p_sys->psz_senc ) free( p_sys->psz_senc );
823
824     if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
825
826     while( p_sys->p_osd_cfg != NULL )
827     {
828         sout_cfg_t *p_next = p_sys->p_osd_cfg->p_next;
829
830         if( p_sys->p_osd_cfg->psz_name )
831             free( p_sys->p_osd_cfg->psz_name );
832         if( p_sys->p_osd_cfg->psz_value )
833             free( p_sys->p_osd_cfg->psz_value );
834         free( p_sys->p_osd_cfg );
835
836         p_sys->p_osd_cfg = p_next;
837     }
838     if( p_sys->psz_osdenc ) free( p_sys->psz_osdenc );
839
840     vlc_object_destroy( p_sys );
841 }
842
843 struct sout_stream_id_t
844 {
845     vlc_fourcc_t  b_transcode;
846
847     /* id of the out stream */
848     void *id;
849
850     /* Decoder */
851     decoder_t       *p_decoder;
852
853     /* Filters */
854     filter_t        *pp_filter[10];
855     int             i_filter;
856     filter_t        *pp_vfilter[10];
857     int             i_vfilter;
858
859     /* Encoder */
860     encoder_t       *p_encoder;
861
862     /* Sync */
863     date_t          interpolated_pts;
864 };
865
866 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
867 {
868     sout_stream_sys_t *p_sys = p_stream->p_sys;
869     sout_stream_id_t *id;
870
871     id = malloc( sizeof( sout_stream_id_t ) );
872     memset( id, 0, sizeof(sout_stream_id_t) );
873
874     id->id = NULL;
875     id->p_decoder = NULL;
876     id->p_encoder = NULL;
877
878     /* Create decoder object */
879     id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
880     if( !id->p_decoder )
881     {
882         msg_Err( p_stream, "out of memory" );
883         goto error;
884     }
885     vlc_object_attach( id->p_decoder, p_stream );
886     id->p_decoder->p_module = NULL;
887     id->p_decoder->fmt_in = *p_fmt;
888     id->p_decoder->b_pace_control = VLC_TRUE;
889
890     /* Create encoder object */
891     id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
892     if( !id->p_encoder )
893     {
894         msg_Err( p_stream, "out of memory" );
895         goto error;
896     }
897     vlc_object_attach( id->p_encoder, p_stream );
898     id->p_encoder->p_module = NULL;
899
900     /* Create destination format */
901     es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
902     id->p_encoder->fmt_out.i_id    = p_fmt->i_id;
903     id->p_encoder->fmt_out.i_group = p_fmt->i_group;
904     if( p_fmt->psz_language )
905         id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
906
907     if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
908     {
909         msg_Dbg( p_stream,
910                  "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
911                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
912
913         /* Complete destination format */
914         id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
915         id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
916             p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
917         id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
918         id->p_encoder->fmt_out.audio.i_bitspersample =
919             p_fmt->audio.i_bitspersample;
920         id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
921             p_sys->i_channels : p_fmt->audio.i_channels;
922         /* Sanity check for audio channels */
923         id->p_encoder->fmt_out.audio.i_channels =
924             __MIN( id->p_encoder->fmt_out.audio.i_channels,
925                    id->p_decoder->fmt_in.audio.i_channels );
926         id->p_encoder->fmt_out.audio.i_original_channels =
927             id->p_decoder->fmt_in.audio.i_physical_channels;
928         if( id->p_decoder->fmt_in.audio.i_channels ==
929             id->p_encoder->fmt_out.audio.i_channels )
930         {
931             id->p_encoder->fmt_out.audio.i_physical_channels =
932                 id->p_decoder->fmt_in.audio.i_physical_channels;
933         }
934         else
935         {
936             id->p_encoder->fmt_out.audio.i_physical_channels =
937                 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
938         }
939
940         /* Build decoder -> filter -> encoder chain */
941         if( transcode_audio_new( p_stream, id ) )
942         {
943             msg_Err( p_stream, "cannot create audio chain" );
944             goto error;
945         }
946
947         /* Open output stream */
948         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
949         id->b_transcode = VLC_TRUE;
950
951         if( !id->id )
952         {
953             transcode_audio_close( p_stream, id );
954             goto error;
955         }
956
957         date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
958     }
959     else if( p_fmt->i_cat == VIDEO_ES &&
960              (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
961     {
962         msg_Dbg( p_stream,
963                  "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
964                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
965
966         /* Complete destination format */
967         id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
968         id->p_encoder->fmt_out.video.i_width  = p_sys->i_width & ~1;
969         id->p_encoder->fmt_out.video.i_height = p_sys->i_height & ~1;
970         id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
971
972         /* Build decoder -> filter -> encoder chain */
973         if( transcode_video_new( p_stream, id ) )
974         {
975             msg_Err( p_stream, "cannot create video chain" );
976             goto error;
977         }
978
979         /* Stream will be added later on because we don't know
980          * all the characteristics of the decoded stream yet */
981         id->b_transcode = VLC_TRUE;
982
983         if( p_sys->f_fps > 0 )
984         {
985             id->p_encoder->fmt_out.video.i_frame_rate =
986                 (p_sys->f_fps * 1001) + 0.5;
987             id->p_encoder->fmt_out.video.i_frame_rate_base = 1001;
988         }
989     }
990     else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
991     {
992         msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
993                  "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
994                  (char*)&p_sys->i_scodec );
995
996         /* Complete destination format */
997         id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
998
999         /* build decoder -> filter -> encoder */
1000         if( transcode_spu_new( p_stream, id ) )
1001         {
1002             msg_Err( p_stream, "cannot create subtitles chain" );
1003             goto error;
1004         }
1005
1006         /* open output stream */
1007         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
1008         id->b_transcode = VLC_TRUE;
1009
1010         if( !id->id )
1011         {
1012             transcode_spu_close( p_stream, id );
1013             goto error;
1014         }
1015     }
1016     else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
1017     {
1018         msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
1019                  (char*)&p_fmt->i_codec );
1020
1021         id->b_transcode = VLC_TRUE;
1022
1023         /* Build decoder -> filter -> overlaying chain */
1024         if( transcode_spu_new( p_stream, id ) )
1025         {
1026             msg_Err( p_stream, "cannot create subtitles chain" );
1027             goto error;
1028         }
1029     }
1030     else
1031     {
1032         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
1033                  (char*)&p_fmt->i_codec );
1034         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
1035         id->b_transcode = VLC_FALSE;
1036
1037         if( !id->id ) goto error;
1038     }
1039
1040     if( p_sys->b_sout_osd )
1041     {
1042         /* Create a fake OSD menu elementary stream */
1043         if( !p_sys->b_es_osd && (p_sys->i_osdcodec != 0 || p_sys->psz_osdenc) )
1044         {
1045             if( transcode_osd_new( p_stream, p_sys->id_osd ) )
1046             {
1047                 msg_Err( p_stream, "cannot create osd chain" );
1048                 goto error;
1049             }
1050             p_sys->b_es_osd = VLC_TRUE;
1051         }
1052     }
1053     return id;
1054
1055  error:
1056     if( id->p_decoder )
1057     {
1058         vlc_object_detach( id->p_decoder );
1059         vlc_object_destroy( id->p_decoder );
1060     }
1061
1062     if( id->p_encoder )
1063     {
1064         vlc_object_detach( id->p_encoder );
1065         es_format_Clean( &id->p_encoder->fmt_out );
1066         vlc_object_destroy( id->p_encoder );
1067     }
1068
1069     free( id );
1070     return NULL;
1071 }
1072
1073 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1074 {
1075     sout_stream_sys_t *p_sys = p_stream->p_sys;
1076
1077     if( p_sys->b_es_osd )
1078         transcode_osd_close( p_stream, p_sys->id_osd );
1079
1080     if( id->b_transcode )
1081     {
1082         switch( id->p_decoder->fmt_in.i_cat )
1083         {
1084         case AUDIO_ES:
1085             transcode_audio_close( p_stream, id );
1086             break;
1087         case VIDEO_ES:
1088             transcode_video_close( p_stream, id );
1089             break;
1090         case SPU_ES:
1091             transcode_spu_close( p_stream, id );
1092             break;
1093         }
1094     }
1095
1096     if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
1097
1098     if( id->p_decoder )
1099     {
1100         vlc_object_detach( id->p_decoder );
1101         vlc_object_destroy( id->p_decoder );
1102     }
1103
1104     if( id->p_encoder )
1105     {
1106         vlc_object_detach( id->p_encoder );
1107         es_format_Clean( &id->p_encoder->fmt_out );
1108         vlc_object_destroy( id->p_encoder );
1109     }
1110
1111     free( id );
1112
1113     return VLC_SUCCESS;
1114 }
1115
1116 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1117                  block_t *p_buffer )
1118 {
1119     sout_stream_sys_t *p_sys = p_stream->p_sys;
1120     block_t *p_out = NULL;
1121
1122     if( !id->b_transcode && id->id )
1123     {
1124         /* Transcode OSD menu pictures. */
1125         if( p_sys->b_es_osd )
1126         {
1127             transcode_osd_process( p_stream, id, p_buffer, &p_out );
1128         }
1129         return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
1130     }
1131     else if( !id->b_transcode )
1132     {
1133         block_Release( p_buffer );
1134         return VLC_EGENERIC;
1135     }
1136
1137     switch( id->p_decoder->fmt_in.i_cat )
1138     {
1139     case AUDIO_ES:
1140         transcode_audio_process( p_stream, id, p_buffer, &p_out );
1141         break;
1142
1143     case VIDEO_ES:
1144         if( transcode_video_process( p_stream, id, p_buffer, &p_out )
1145             != VLC_SUCCESS )
1146         {
1147             return VLC_EGENERIC;
1148         }
1149         break;
1150
1151     case SPU_ES:
1152         if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
1153             VLC_SUCCESS )
1154         {
1155             return VLC_EGENERIC;
1156         }
1157         break;
1158
1159     default:
1160         p_out = NULL;
1161         block_Release( p_buffer );
1162         break;
1163     }
1164
1165     if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
1166     return VLC_SUCCESS;
1167 }
1168
1169 /****************************************************************************
1170  * decoder reencoder part
1171  ****************************************************************************/
1172 int audio_BitsPerSample( vlc_fourcc_t i_format )
1173 {
1174     switch( i_format )
1175     {
1176     case VLC_FOURCC('u','8',' ',' '):
1177     case VLC_FOURCC('s','8',' ',' '):
1178         return 8;
1179
1180     case VLC_FOURCC('u','1','6','l'):
1181     case VLC_FOURCC('s','1','6','l'):
1182     case VLC_FOURCC('u','1','6','b'):
1183     case VLC_FOURCC('s','1','6','b'):
1184         return 16;
1185
1186     case VLC_FOURCC('u','2','4','l'):
1187     case VLC_FOURCC('s','2','4','l'):
1188     case VLC_FOURCC('u','2','4','b'):
1189     case VLC_FOURCC('s','2','4','b'):
1190         return 24;
1191
1192     case VLC_FOURCC('u','3','2','l'):
1193     case VLC_FOURCC('s','3','2','l'):
1194     case VLC_FOURCC('u','3','2','b'):
1195     case VLC_FOURCC('s','3','2','b'):
1196     case VLC_FOURCC('f','l','3','2'):
1197     case VLC_FOURCC('f','i','3','2'):
1198         return 32;
1199
1200     case VLC_FOURCC('f','l','6','4'):
1201         return 64;
1202     }
1203
1204     return 0;
1205 }
1206
1207 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
1208                                              sout_stream_id_t *id,
1209                                              es_format_t *p_fmt_in,
1210                                              es_format_t *p_fmt_out )
1211 {
1212     filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1213
1214     vlc_object_attach( p_filter, p_stream );
1215     p_filter->pf_audio_buffer_new = (block_t* (*) (filter_t*, int))__block_New;
1216
1217     p_filter->fmt_in = *p_fmt_in;
1218     p_filter->fmt_out = *p_fmt_out;
1219
1220     p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
1221     if( p_filter->p_module )
1222     {
1223         p_filter->fmt_out.audio.i_bitspersample = 
1224             audio_BitsPerSample( p_filter->fmt_out.i_codec );
1225         *p_fmt_in = p_filter->fmt_out;
1226     }
1227     else
1228     {
1229         vlc_object_detach( p_filter );
1230         vlc_object_destroy( p_filter );
1231         p_filter = 0;
1232     }
1233
1234     return p_filter;
1235 }
1236
1237 static int transcode_audio_new( sout_stream_t *p_stream,
1238                                 sout_stream_id_t *id )
1239 {
1240     sout_stream_sys_t *p_sys = p_stream->p_sys;
1241     es_format_t fmt_last;
1242     int i_pass = 6;
1243
1244     /*
1245      * Open decoder
1246      */
1247
1248     /* Initialization of decoder structures */
1249     id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1250     id->p_decoder->fmt_out.i_extra = 0;
1251     id->p_decoder->fmt_out.p_extra = 0;
1252     id->p_decoder->pf_decode_audio = 0;
1253     id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
1254     id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1255     /* id->p_decoder->p_cfg = p_sys->p_audio_cfg; */
1256
1257     id->p_decoder->p_module =
1258         module_Need( id->p_decoder, "decoder", "$codec", 0 );
1259
1260     if( !id->p_decoder->p_module )
1261     {
1262         msg_Err( p_stream, "cannot find decoder" );
1263         return VLC_EGENERIC;
1264     }
1265     id->p_decoder->fmt_out.audio.i_bitspersample = 
1266         audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1267     fmt_last = id->p_decoder->fmt_out;
1268     /* FIX decoders so we don't have to do this */
1269     fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1270
1271     /*
1272      * Open encoder
1273      */
1274
1275     /* Initialization of encoder format structures */
1276     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1277                     id->p_decoder->fmt_out.i_codec );
1278     id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
1279
1280     /* Initialization of encoder format structures */
1281     es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1282     id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1283     id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
1284     id->p_encoder->fmt_in.audio.i_physical_channels =
1285         id->p_encoder->fmt_out.audio.i_physical_channels;
1286     id->p_encoder->fmt_in.audio.i_original_channels =
1287         id->p_encoder->fmt_out.audio.i_original_channels;
1288     id->p_encoder->fmt_in.audio.i_channels =
1289         id->p_encoder->fmt_out.audio.i_channels;
1290     id->p_encoder->fmt_in.audio.i_bitspersample =
1291         audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1292
1293     id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1294
1295     id->p_encoder->p_module =
1296         module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
1297     if( !id->p_encoder->p_module )
1298     {
1299         msg_Err( p_stream, "cannot find encoder" );
1300         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1301         id->p_decoder->p_module = 0;
1302         return VLC_EGENERIC;
1303     }
1304     id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1305     id->p_encoder->fmt_in.audio.i_bitspersample =
1306         audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1307
1308     /* Load conversion filters */
1309     if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1310         fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1311     {
1312         /* We'll have to go through fl32 first */
1313         es_format_t fmt_out = id->p_encoder->fmt_in;
1314         fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1315
1316         id->pp_filter[id->i_filter] =
1317             transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );
1318
1319         if( id->pp_filter[id->i_filter] ) id->i_filter++;
1320     }
1321
1322     while( i_pass-- )
1323     {
1324         if( fmt_last.audio.i_channels !=
1325             id->p_encoder->fmt_in.audio.i_channels ||
1326             fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
1327             fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1328         {
1329             id->pp_filter[id->i_filter] =
1330                 transcode_audio_filter_new( p_stream, id, &fmt_last,
1331                                             &id->p_encoder->fmt_in );
1332
1333             if( id->pp_filter[id->i_filter] ) id->i_filter++;
1334             else break;
1335         }
1336     }
1337
1338     /* Final checks to see if conversions were successful */
1339     if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1340     {
1341         msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1342                  (char *)&fmt_last.i_codec,
1343                  (char *)&id->p_encoder->fmt_in.i_codec );
1344         transcode_audio_close( p_stream, id );
1345         return VLC_EGENERIC;
1346     }
1347
1348     if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1349     {
1350         msg_Err( p_stream, "no audio filter found for mixing from"
1351                  " %i to %i channels", fmt_last.audio.i_channels,
1352                  id->p_encoder->fmt_in.audio.i_channels );
1353 #if 0
1354         /* FIXME : this might work, but only if the encoder is restarted */
1355         id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1356         id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1357
1358         id->p_encoder->fmt_in.audio.i_physical_channels =
1359             id->p_encoder->fmt_in.audio.i_original_channels =
1360                 fmt_last.audio.i_physical_channels;
1361         id->p_encoder->fmt_out.audio.i_physical_channels =
1362             id->p_encoder->fmt_out.audio.i_original_channels =
1363                 fmt_last.audio.i_physical_channels;
1364 #else
1365         transcode_audio_close( p_stream, id );
1366         return VLC_EGENERIC;
1367 #endif
1368     }
1369
1370     if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1371     {
1372         msg_Err( p_stream, "no audio filter found for resampling from"
1373                  " %iHz to %iHz", fmt_last.audio.i_rate,
1374                  id->p_encoder->fmt_in.audio.i_rate );
1375 #if 0
1376         /* FIXME : this might work, but only if the encoder is restarted */
1377         id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1378         id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1379 #else
1380         transcode_audio_close( p_stream, id );
1381         return VLC_EGENERIC;
1382 #endif
1383     }
1384
1385     /* FIXME: Hack for mp3 transcoding support */
1386     if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1387         id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1388
1389     return VLC_SUCCESS;
1390 }
1391
1392 static void transcode_audio_close( sout_stream_t *p_stream,
1393                                    sout_stream_id_t *id )
1394 {
1395     int i;
1396
1397     /* Close decoder */
1398     if( id->p_decoder->p_module )
1399         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1400     id->p_decoder->p_module = 0;
1401
1402     /* Close encoder */
1403     if( id->p_encoder->p_module )
1404         module_Unneed( id->p_encoder, id->p_encoder->p_module );
1405     id->p_encoder->p_module = 0;
1406
1407     /* Close filters */
1408     for( i = 0; i < id->i_filter; i++ )
1409     {
1410         vlc_object_detach( id->pp_filter[i] );
1411         if( id->pp_filter[i]->p_module )
1412             module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1413         vlc_object_destroy( id->pp_filter[i] );
1414     }
1415 }
1416
1417 static int transcode_audio_process( sout_stream_t *p_stream,
1418                                     sout_stream_id_t *id,
1419                                     block_t *in, block_t **out )
1420 {
1421     sout_stream_sys_t *p_sys = p_stream->p_sys;
1422     aout_buffer_t *p_audio_buf;
1423     block_t *p_block, *p_audio_block;
1424     int i;
1425     *out = NULL;
1426
1427     while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1428                                                           &in )) )
1429     {
1430         stats_UpdateInteger( p_stream->p_parent->p_parent, STATS_DECODED_AUDIO,
1431                              1, NULL );
1432         if( p_sys->b_master_sync )
1433         {
1434             mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1435             if ( p_audio_buf->start_date - i_dts > MASTER_SYNC_MAX_DRIFT
1436                   || p_audio_buf->start_date - i_dts < -MASTER_SYNC_MAX_DRIFT )
1437             {
1438                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1439                 date_Set( &id->interpolated_pts, p_audio_buf->start_date );
1440                 i_dts = p_audio_buf->start_date + 1;
1441             }
1442             p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1443             date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1444             p_audio_buf->start_date -= p_sys->i_master_drift;
1445             p_audio_buf->end_date -= p_sys->i_master_drift;
1446         }
1447
1448         p_audio_block = p_audio_buf->p_sys;
1449         p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1450         p_audio_block->i_dts = p_audio_block->i_pts =
1451             p_audio_buf->start_date;
1452         p_audio_block->i_length = p_audio_buf->end_date -
1453             p_audio_buf->start_date;
1454         p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1455
1456         /* Run filter chain */
1457         for( i = 0; i < id->i_filter; i++ )
1458         {
1459             p_audio_block =
1460                 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1461                                                    p_audio_block );
1462         }
1463
1464         p_audio_buf->p_buffer = p_audio_block->p_buffer;
1465         p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1466         p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1467         p_audio_buf->start_date = p_audio_block->i_dts;
1468         p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1469
1470         p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1471         block_ChainAppend( out, p_block );
1472         block_Release( p_audio_block );
1473         free( p_audio_buf );
1474     }
1475
1476     return VLC_SUCCESS;
1477 }
1478
1479 static void audio_release_buffer( aout_buffer_t *p_buffer )
1480 {
1481     if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1482     if( p_buffer ) free( p_buffer );
1483 }
1484
1485 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1486 {
1487     aout_buffer_t *p_buffer;
1488     block_t *p_block;
1489     int i_size;
1490
1491     if( p_dec->fmt_out.audio.i_bitspersample )
1492     {
1493         i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1494             p_dec->fmt_out.audio.i_channels;
1495     }
1496     else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1497              p_dec->fmt_out.audio.i_frame_length )
1498     {
1499         i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1500             p_dec->fmt_out.audio.i_frame_length;
1501     }
1502     else
1503     {
1504         i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1505     }
1506
1507     p_buffer = malloc( sizeof(aout_buffer_t) );
1508     p_buffer->pf_release = audio_release_buffer;
1509     p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1510
1511     p_buffer->p_buffer = p_block->p_buffer;
1512     p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1513     p_buffer->i_nb_samples = i_samples;
1514     p_block->i_samples = i_samples;
1515
1516     return p_buffer;
1517 }
1518
1519 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1520 {
1521     if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1522     if( p_buffer ) free( p_buffer );
1523 }
1524
1525 /*
1526  * video
1527  */
1528 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1529 {
1530     sout_stream_sys_t *p_sys = p_stream->p_sys;
1531     int i;
1532
1533     /*
1534      * Open decoder
1535      */
1536
1537     /* Initialization of decoder structures */
1538     id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1539     id->p_decoder->fmt_out.i_extra = 0;
1540     id->p_decoder->fmt_out.p_extra = 0;
1541     id->p_decoder->pf_decode_video = 0;
1542     id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1543     id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1544     id->p_decoder->pf_picture_link    = video_link_picture_decoder;
1545     id->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
1546     id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1547     for( i = 0; i < PICTURE_RING_SIZE; i++ )
1548         id->p_decoder->p_owner->pp_pics[i] = 0;
1549     id->p_decoder->p_owner->p_sys = p_sys;
1550     /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
1551
1552     id->p_decoder->p_module =
1553         module_Need( id->p_decoder, "decoder", "$codec", 0 );
1554
1555     if( !id->p_decoder->p_module )
1556     {
1557         msg_Err( p_stream, "cannot find decoder" );
1558         return VLC_EGENERIC;
1559     }
1560
1561     /*
1562      * Open encoder.
1563      * Because some info about the decoded input will only be available
1564      * once the first frame is decoded, we actually only test the availability
1565      * of the encoder here.
1566      */
1567
1568     /* Initialization of encoder format structures */
1569     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1570                     id->p_decoder->fmt_out.i_codec );
1571     id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1572
1573     /* The dimensions will be set properly later on.
1574      * Just put sensible values so we can test an encoder is available. */
1575     id->p_encoder->fmt_in.video.i_width =
1576         id->p_encoder->fmt_out.video.i_width ?
1577         id->p_encoder->fmt_out.video.i_width :
1578         id->p_decoder->fmt_in.video.i_width ?
1579         id->p_decoder->fmt_in.video.i_width : 16;
1580     id->p_encoder->fmt_in.video.i_height =
1581         id->p_encoder->fmt_out.video.i_height ?
1582         id->p_encoder->fmt_out.video.i_height :
1583         id->p_decoder->fmt_in.video.i_height ?
1584         id->p_decoder->fmt_in.video.i_height : 16;
1585     id->p_encoder->fmt_in.video.i_frame_rate = 25;
1586     id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1587
1588     id->p_encoder->i_threads = p_sys->i_threads;
1589     id->p_encoder->p_cfg = p_sys->p_video_cfg;
1590
1591     id->p_encoder->p_module =
1592         module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1593     if( !id->p_encoder->p_module )
1594     {
1595         msg_Err( p_stream, "cannot find encoder" );
1596         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1597         id->p_decoder->p_module = 0;
1598         return VLC_EGENERIC;
1599     }
1600
1601     /* Close the encoder.
1602      * We'll open it only when we have the first frame. */
1603     module_Unneed( id->p_encoder, id->p_encoder->p_module );
1604     if( id->p_encoder->fmt_out.p_extra )
1605         free( id->p_encoder->fmt_out.p_extra );
1606     id->p_encoder->p_module = NULL;
1607
1608     if( p_sys->i_threads >= 1 )
1609     {
1610         int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
1611                            VLC_THREAD_PRIORITY_VIDEO;
1612         p_sys->id_video = id;
1613         vlc_mutex_init( p_stream, &p_sys->lock_out );
1614         vlc_cond_init( p_stream, &p_sys->cond );
1615         memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1616         p_sys->i_first_pic = 0;
1617         p_sys->i_last_pic = 0;
1618         p_sys->p_buffers = NULL;
1619         p_sys->b_die = p_sys->b_error = 0;
1620         if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
1621                                VLC_FALSE ) )
1622         {
1623             msg_Err( p_stream, "cannot spawn encoder thread" );
1624             module_Unneed( id->p_decoder, id->p_decoder->p_module );
1625             id->p_decoder->p_module = 0;
1626             return VLC_EGENERIC;
1627         }
1628     }
1629
1630     return VLC_SUCCESS;
1631 }
1632
1633 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1634                                          sout_stream_id_t *id )
1635 {
1636     sout_stream_sys_t *p_sys = p_stream->p_sys;
1637
1638      /* Calculate scaling, padding, cropping etc. */
1639
1640      /* width/height of source */
1641      int i_src_width = id->p_decoder->fmt_out.video.i_width;
1642      int i_src_height = id->p_decoder->fmt_out.video.i_height;
1643
1644      /* with/height scaling */
1645      float f_scale_width = 1;
1646      float f_scale_height = 1;
1647
1648      /* width/height of output stream */
1649      int i_dst_width;
1650      int i_dst_height;
1651
1652      /* aspect ratio */
1653      float f_aspect = (float)id->p_decoder->fmt_out.video.i_aspect /
1654                              VOUT_ASPECT_FACTOR;
1655
1656      msg_Dbg( p_stream, "decoder aspect is %i:%i",
1657                   id->p_decoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );
1658
1659      /* Change f_aspect from source frame to source pixel */
1660      f_aspect = f_aspect * i_src_height / i_src_width;
1661      msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );
1662
1663      /* width/height after cropping */
1664      p_sys->i_src_x_offset = p_sys->i_crop_left & ~1;
1665      p_sys->i_src_y_offset = p_sys->i_crop_top & ~1;
1666      p_sys->i_crop_width = i_src_width - ( p_sys->i_crop_left & ~1 ) -
1667                             ( p_sys->i_crop_right & ~1 );
1668      p_sys->i_crop_height = i_src_height - ( p_sys->i_crop_top & ~1 ) -
1669                             ( p_sys->i_crop_bottom & ~1 );
1670
1671     /* Calculate scaling factor for specified parameters */
1672     if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1673         id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1674     {
1675         /* Global scaling. Make sure width will remain a factor of 16 */
1676         float f_real_scale;
1677         int  i_new_height;
1678         int i_new_width = i_src_width * p_sys->f_scale;
1679
1680         if( i_new_width % 16 <= 7 && i_new_width >= 16 )
1681             i_new_width -= i_new_width % 16;
1682         else
1683             i_new_width += 16 - i_new_width % 16;
1684
1685         f_real_scale = (float)( i_new_width ) / (float) i_src_width;
1686
1687         i_new_height = __MAX( 16, i_src_height * (float)f_real_scale );
1688
1689         f_scale_width = f_real_scale;
1690         f_scale_height = (float) i_new_height / (float) i_src_height;
1691     }
1692     else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1693              id->p_encoder->fmt_out.video.i_height <= 0 )
1694     {
1695         /* Only width specified */
1696         f_scale_width = (float)id->p_encoder->fmt_out.video.i_width /
1697                              p_sys->i_crop_width;
1698         f_scale_height = f_scale_width;
1699     }
1700     else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1701              id->p_encoder->fmt_out.video.i_height > 0 )
1702     {
1703          /* Only height specified */
1704          f_scale_height = (float)id->p_encoder->fmt_out.video.i_height /
1705                               p_sys->i_crop_height;
1706          f_scale_width = f_scale_height;
1707      }
1708      else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1709               id->p_encoder->fmt_out.video.i_height > 0 )
1710      {
1711          /* Width and height specified */
1712          f_scale_width = (float)id->p_encoder->fmt_out.video.i_width
1713                                / p_sys->i_crop_width;
1714          f_scale_height = (float)id->p_encoder->fmt_out.video.i_height
1715                                / p_sys->i_crop_height;
1716      }
1717
1718      /* check maxwidth and maxheight */
1719      /* note: maxwidth and maxheight currently does not handle
1720       * canvas and padding, just scaling and cropping. */
1721
1722      if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
1723                                                      p_sys->i_crop_width )
1724      {
1725          f_scale_width = (float)p_sys->i_maxwidth / p_sys->i_crop_width;
1726      }
1727      if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
1728                                                        p_sys->i_crop_height )
1729      {
1730          f_scale_height = (float)p_sys->i_maxheight / p_sys->i_crop_height;
1731      }
1732
1733      /* Change aspect ratio from source pixel to scaled pixel */
1734      f_aspect = f_aspect * f_scale_height / f_scale_width;
1735      msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );
1736
1737      /* Correct scaling for target aspect ratio */
1738      /* Shrink video if necessary */
1739      if ( p_sys->i_canvas_aspect > 0 )
1740      {
1741          float f_target_aspect = (float)p_sys->i_canvas_aspect /
1742                                         VOUT_ASPECT_FACTOR;
1743
1744          if( p_sys->i_canvas_width > 0 && p_sys->i_canvas_height > 0)
1745          {
1746              /* Calculate pixel aspect of canvas */
1747              f_target_aspect = f_target_aspect / p_sys->i_canvas_width *
1748                                p_sys->i_canvas_height;
1749          }
1750          if( f_target_aspect > f_aspect )
1751          {
1752              /* Reduce width scale to increase aspect */
1753              f_scale_width = f_scale_width * f_aspect / f_target_aspect;
1754          }
1755          else
1756          {
1757              /* Reduce height scale to decrease aspect */
1758              f_scale_height = f_scale_height * f_target_aspect / f_aspect;
1759          }
1760          f_aspect = f_target_aspect;
1761          msg_Dbg( p_stream, "canvas scaled pixel aspect is %f:1", f_aspect );
1762      }
1763
1764      /* f_scale_width and f_scale_height are now final */
1765
1766      /* Calculate width, height from scaling */
1767      /* Make sure its multiple of 2 */
1768
1769      i_dst_width = 2 * (int)( p_sys->i_crop_width * f_scale_width / 2 + 0.5 );
1770      i_dst_height = 2 *
1771                     (int)( p_sys->i_crop_height * f_scale_height / 2 + 0.5 );
1772
1773      p_sys->i_nopadd_width = i_dst_width;
1774      p_sys->i_nopadd_height = i_dst_height;
1775      p_sys->i_dst_x_offset = 0;
1776      p_sys->i_dst_y_offset = 0;
1777
1778      /* Handle canvas and padding */
1779
1780      if( p_sys->i_canvas_width <= 0 )
1781      {
1782          /* No canvas width set, add explicit padding border */
1783          i_dst_width = p_sys->i_nopadd_width + ( p_sys->i_padd_left & ~1 ) +
1784                       ( p_sys->i_padd_right & ~1 );
1785          p_sys->i_dst_x_offset = ( p_sys->i_padd_left & ~1 );
1786      }
1787      else
1788      {
1789          /* Canvas set, check if we have to padd or crop */
1790
1791          if( p_sys->i_canvas_width < p_sys->i_nopadd_width )
1792          {
1793              /* need to crop more, but keep same scaling */
1794              int i_crop = 2 * (int)( ( p_sys->i_canvas_width & ~1 ) /
1795                                        f_scale_width / 2 + 0.5 );
1796
1797              p_sys->i_src_x_offset += ( ( p_sys->i_crop_width - i_crop ) / 2 )
1798                                            & ~1;
1799              p_sys->i_crop_width = i_crop;
1800              i_dst_width = p_sys->i_canvas_width & ~1;
1801              p_sys->i_nopadd_width = i_dst_width;
1802          }
1803          else if( p_sys->i_canvas_width > p_sys->i_nopadd_width )
1804          {
1805              /* need to padd */
1806              i_dst_width = p_sys->i_canvas_width & ~1;
1807              p_sys->i_dst_x_offset = ( i_dst_width - p_sys->i_nopadd_width )/2;
1808              p_sys->i_dst_x_offset = p_sys->i_dst_x_offset & ~1;
1809          }
1810      }
1811
1812      if( p_sys->i_canvas_height <= 0 )
1813      {
1814          /* No canvas set, add padding border */
1815          i_dst_height = p_sys->i_nopadd_height + ( p_sys->i_padd_top & ~1 ) +
1816                         ( p_sys->i_padd_bottom & ~1 );
1817          p_sys->i_dst_y_offset = ( p_sys->i_padd_top & ~1 );
1818      }
1819      else
1820      {
1821          /* Canvas set, check if we have to padd or crop */
1822
1823          if( p_sys->i_canvas_height < p_sys->i_nopadd_height )
1824          {
1825              /* need to crop more, but keep same scaling */
1826              int i_crop = 2 * (int)( ( p_sys->i_canvas_height & ~1 ) /
1827                                         f_scale_height / 2 + 0.5 );
1828
1829              p_sys->i_src_y_offset += ( ( p_sys->i_crop_height - i_crop ) / 2 )
1830                                                 & ~1;
1831              p_sys->i_crop_height = i_crop;
1832              i_dst_height = p_sys->i_canvas_height & ~1;
1833              p_sys->i_nopadd_height = i_dst_height;
1834          }
1835          else if( p_sys->i_canvas_height > p_sys->i_nopadd_height )
1836          {
1837              /* need to padd */
1838              i_dst_height = p_sys->i_canvas_height & ~1;
1839              p_sys->i_dst_y_offset = ( i_dst_height - p_sys->i_nopadd_height )
1840                                         /2;
1841              p_sys->i_dst_y_offset = p_sys->i_dst_y_offset & ~1;
1842          }
1843      }
1844
1845
1846      /* Change aspect ratio from scaled pixel to output frame */
1847
1848      f_aspect = f_aspect * i_dst_width / i_dst_height;
1849
1850      /* Store calculated values */
1851      id->p_encoder->fmt_out.video.i_width = i_dst_width;
1852      id->p_encoder->fmt_out.video.i_height = i_dst_height;
1853
1854      id->p_encoder->fmt_in.video.i_width = i_dst_width;
1855      id->p_encoder->fmt_in.video.i_height = i_dst_height;
1856
1857      msg_Dbg( p_stream, "source %ix%i, crop %ix%i, "
1858                         "destination %ix%i, padding %ix%i",
1859          i_src_width, i_src_height,
1860          p_sys->i_crop_width, p_sys->i_crop_height,
1861          p_sys->i_nopadd_width, p_sys->i_nopadd_height,
1862          i_dst_width, i_dst_height
1863      );
1864
1865     /* Handle frame rate conversion */
1866     if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1867         !id->p_encoder->fmt_out.video.i_frame_rate_base )
1868     {
1869         if( id->p_decoder->fmt_out.video.i_frame_rate &&
1870             id->p_decoder->fmt_out.video.i_frame_rate_base )
1871         {
1872             id->p_encoder->fmt_out.video.i_frame_rate =
1873                 id->p_decoder->fmt_out.video.i_frame_rate;
1874             id->p_encoder->fmt_out.video.i_frame_rate_base =
1875                 id->p_decoder->fmt_out.video.i_frame_rate_base;
1876         }
1877         else
1878         {
1879             /* Pick a sensible default value */
1880             id->p_encoder->fmt_out.video.i_frame_rate = 25;
1881             id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1882         }
1883     }
1884
1885     id->p_encoder->fmt_in.video.i_frame_rate =
1886         id->p_encoder->fmt_out.video.i_frame_rate;
1887     id->p_encoder->fmt_in.video.i_frame_rate_base =
1888         id->p_encoder->fmt_out.video.i_frame_rate_base;
1889
1890     date_Init( &id->interpolated_pts,
1891                id->p_encoder->fmt_out.video.i_frame_rate,
1892                id->p_encoder->fmt_out.video.i_frame_rate_base );
1893
1894     /* Check whether a particular aspect ratio was requested */
1895     if( !id->p_encoder->fmt_out.video.i_aspect )
1896     {
1897         id->p_encoder->fmt_out.video.i_aspect = (int)( f_aspect * VOUT_ASPECT_FACTOR + 0.5 );
1898     }
1899     id->p_encoder->fmt_in.video.i_aspect =
1900         id->p_encoder->fmt_out.video.i_aspect;
1901
1902     msg_Dbg( p_stream, "encoder aspect is %i:%i", id->p_encoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );
1903
1904     id->p_encoder->p_module =
1905         module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1906     if( !id->p_encoder->p_module )
1907     {
1908         msg_Err( p_stream, "cannot find encoder" );
1909         return VLC_EGENERIC;
1910     }
1911
1912     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1913
1914     /* Hack for mp2v/mp1v transcoding support */
1915     if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1916         id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1917     {
1918         id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1919     }
1920
1921     id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1922                                              &id->p_encoder->fmt_out );
1923     if( !id->id )
1924     {
1925         msg_Err( p_stream, "cannot add this stream" );
1926         return VLC_EGENERIC;
1927     }
1928
1929     return VLC_SUCCESS;
1930 }
1931
1932 static void transcode_video_close( sout_stream_t *p_stream,
1933                                    sout_stream_id_t *id )
1934 {
1935     int i, j;
1936
1937     if( p_stream->p_sys->i_threads >= 1 )
1938     {
1939         vlc_mutex_lock( &p_stream->p_sys->lock_out );
1940         p_stream->p_sys->b_die = 1;
1941         vlc_cond_signal( &p_stream->p_sys->cond );
1942         vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1943         vlc_thread_join( p_stream->p_sys );
1944         vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1945         vlc_cond_destroy( &p_stream->p_sys->cond );
1946     }
1947
1948     /* Close decoder */
1949     if( id->p_decoder->p_module )
1950         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1951
1952     if( id->p_decoder->p_owner )
1953     {
1954         /* Clean-up pictures ring buffer */
1955         for( i = 0; i < PICTURE_RING_SIZE; i++ )
1956         {
1957             if( id->p_decoder->p_owner->pp_pics[i] )
1958                 video_del_buffer( VLC_OBJECT(id->p_decoder),
1959                                   id->p_decoder->p_owner->pp_pics[i] );
1960         }
1961         free( id->p_decoder->p_owner );
1962     }
1963
1964     /* Close encoder */
1965     if( id->p_encoder->p_module )
1966         module_Unneed( id->p_encoder, id->p_encoder->p_module );
1967
1968     /* Close filters */
1969     for( i = 0; i < id->i_filter; i++ )
1970     {
1971         vlc_object_detach( id->pp_filter[i] );
1972         if( id->pp_filter[i]->p_module )
1973             module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1974
1975         /* Clean-up pictures ring buffer */
1976         for( j = 0; j < PICTURE_RING_SIZE; j++ )
1977         {
1978             if( id->pp_filter[i]->p_owner->pp_pics[j] )
1979                 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1980                                   id->pp_filter[i]->p_owner->pp_pics[j] );
1981         }
1982         free( id->pp_filter[i]->p_owner );
1983
1984         vlc_object_destroy( id->pp_filter[i] );
1985     }
1986     for( i = 0; i < id->i_vfilter; i++ )
1987     {
1988         vlc_object_detach( id->pp_vfilter[i] );
1989         if( id->pp_vfilter[i]->p_module )
1990             module_Unneed( id->pp_vfilter[i], id->pp_vfilter[i]->p_module );
1991
1992         /* Clean-up pictures ring buffer */
1993         for( j = 0; j < PICTURE_RING_SIZE; j++ )
1994         {
1995             if( id->pp_vfilter[i]->p_owner->pp_pics[j] )
1996                 video_del_buffer( VLC_OBJECT(id->pp_vfilter[i]),
1997                                   id->pp_vfilter[i]->p_owner->pp_pics[j] );
1998         }
1999         free( id->pp_vfilter[i]->p_owner );
2000
2001         vlc_object_destroy( id->pp_vfilter[i] );
2002     }
2003 }
2004
2005 static int transcode_video_process( sout_stream_t *p_stream,
2006                                     sout_stream_id_t *id,
2007                                     block_t *in, block_t **out )
2008 {
2009     sout_stream_sys_t *p_sys = p_stream->p_sys;
2010     int i_duplicate = 1, i;
2011     picture_t *p_pic, *p_pic2 = NULL;
2012     *out = NULL;
2013
2014     while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
2015     {
2016         subpicture_t *p_subpic = 0;
2017         stats_UpdateInteger( p_stream->p_parent->p_parent, STATS_DECODED_VIDEO,
2018                               1, NULL );
2019
2020         if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
2021         {
2022             mtime_t current_date = mdate();
2023             if( current_date + 50000 > p_pic->date )
2024             {
2025                 msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
2026                          current_date + 50000 - p_pic->date );
2027                 p_pic->pf_release( p_pic );
2028                 continue;
2029             }
2030         }
2031
2032         if( p_sys->b_master_sync )
2033         {
2034             mtime_t i_video_drift;
2035             mtime_t i_master_drift = p_sys->i_master_drift;
2036             mtime_t i_pts;
2037
2038             i_pts = date_Get( &id->interpolated_pts ) + 1;
2039             if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
2040                   || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2041             {
2042                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2043                 date_Set( &id->interpolated_pts, p_pic->date );
2044                 i_pts = p_pic->date + 1;
2045             }
2046             i_video_drift = p_pic->date - i_pts;
2047             i_duplicate = 1;
2048
2049             /* Set the pts of the frame being encoded */
2050             p_pic->date = i_pts;
2051
2052             if( i_video_drift < i_master_drift - 50000 )
2053             {
2054 #if 0
2055                 msg_Dbg( p_stream, "dropping frame (%i)",
2056                          (int)(i_video_drift - i_master_drift) );
2057 #endif
2058                 p_pic->pf_release( p_pic );
2059                 continue;
2060             }
2061             else if( i_video_drift > i_master_drift + 50000 )
2062             {
2063 #if 0
2064                 msg_Dbg( p_stream, "adding frame (%i)",
2065                          (int)(i_video_drift - i_master_drift) );
2066 #endif
2067                 i_duplicate = 2;
2068             }
2069         }
2070
2071         if( !id->p_encoder->p_module )
2072         {
2073             if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
2074             {
2075                 p_pic->pf_release( p_pic );
2076                 transcode_video_close( p_stream, id );
2077                 id->b_transcode = VLC_FALSE;
2078                 return VLC_EGENERIC;
2079             }
2080
2081             /* Deinterlace */
2082             if( p_stream->p_sys->b_deinterlace )
2083             {
2084                 id->pp_filter[id->i_filter] =
2085                     vlc_object_create( p_stream, VLC_OBJECT_FILTER );
2086                 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
2087
2088                 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
2089                     video_new_buffer_filter;
2090                 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
2091                     video_del_buffer_filter;
2092
2093                 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
2094                 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
2095                 id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
2096                 id->pp_filter[id->i_filter]->p_module =
2097                     module_Need( id->pp_filter[id->i_filter],
2098                                  "video filter2", p_sys->psz_deinterlace, 0 );
2099                 if( id->pp_filter[id->i_filter]->p_module )
2100                 {
2101                     id->pp_filter[id->i_filter]->p_owner =
2102                         malloc( sizeof(filter_owner_sys_t) );
2103                     for( i = 0; i < PICTURE_RING_SIZE; i++ )
2104                         id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
2105                     id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
2106
2107                     id->i_filter++;
2108                 }
2109                 else
2110                 {
2111                     msg_Dbg( p_stream, "no video filter found" );
2112                     vlc_object_detach( id->pp_filter[id->i_filter] );
2113                     vlc_object_destroy( id->pp_filter[id->i_filter] );
2114                 }
2115             }
2116
2117             /* Check if we need a filter for chroma conversion or resizing */
2118             if( id->p_decoder->fmt_out.video.i_chroma !=
2119                 id->p_encoder->fmt_in.video.i_chroma ||
2120                 
2121                 (int)id->p_decoder->fmt_out.video.i_width != p_sys->i_crop_width ||
2122                 p_sys->i_crop_width != p_sys->i_nopadd_width ||
2123                 p_sys->i_nopadd_width != (int)id->p_encoder->fmt_out.video.i_width ||
2124                 
2125                 (int)id->p_decoder->fmt_out.video.i_height != p_sys->i_crop_height ||
2126                 p_sys->i_crop_height != p_sys->i_nopadd_height ||
2127                 p_sys->i_nopadd_height != (int)id->p_encoder->fmt_out.video.i_height)
2128             {
2129                 id->pp_filter[id->i_filter] =
2130                     vlc_object_create( p_stream, VLC_OBJECT_FILTER );
2131                 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
2132
2133                 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
2134                     video_new_buffer_filter;
2135                 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
2136                     video_del_buffer_filter;
2137
2138                 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
2139                 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
2140                 id->pp_filter[id->i_filter]->p_cfg = NULL;
2141
2142
2143                 id->pp_filter[id->i_filter]->fmt_in.video.i_x_offset = p_sys->i_src_x_offset;
2144                 id->pp_filter[id->i_filter]->fmt_in.video.i_y_offset = p_sys->i_src_y_offset;
2145                 id->pp_filter[id->i_filter]->fmt_in.video.i_visible_width = p_sys->i_crop_width;
2146                 id->pp_filter[id->i_filter]->fmt_in.video.i_visible_height = p_sys->i_crop_height;
2147
2148                 id->pp_filter[id->i_filter]->fmt_out.video.i_x_offset = p_sys->i_dst_x_offset;
2149                 id->pp_filter[id->i_filter]->fmt_out.video.i_y_offset = p_sys->i_dst_y_offset;
2150                 id->pp_filter[id->i_filter]->fmt_out.video.i_visible_width = p_sys->i_nopadd_width;
2151                 id->pp_filter[id->i_filter]->fmt_out.video.i_visible_height = p_sys->i_nopadd_height;
2152
2153                 id->pp_filter[id->i_filter]->p_module =
2154                     module_Need( id->pp_filter[id->i_filter],
2155                                  "crop padd", 0, 0 );
2156                 if( id->pp_filter[id->i_filter]->p_module )
2157                 {
2158                     id->pp_filter[id->i_filter]->p_owner =
2159                         malloc( sizeof(filter_owner_sys_t) );
2160                     for( i = 0; i < PICTURE_RING_SIZE; i++ )
2161                         id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
2162                     id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
2163
2164                     id->i_filter++;
2165                 }
2166                 else
2167                 {
2168                     msg_Dbg( p_stream, "no video filter found" );
2169                     vlc_object_detach( id->pp_filter[id->i_filter] );
2170                     vlc_object_destroy( id->pp_filter[id->i_filter] );
2171
2172                     p_pic->pf_release( p_pic );
2173                     transcode_video_close( p_stream, id );
2174                     id->b_transcode = VLC_FALSE;
2175                     return VLC_EGENERIC;
2176                 }
2177             }
2178
2179             for( i = 0; i < p_sys->i_vfilters; i++ )
2180             {
2181                 id->pp_vfilter[id->i_vfilter] =
2182                     vlc_object_create( p_stream, VLC_OBJECT_FILTER );
2183                 vlc_object_attach( id->pp_vfilter[id->i_vfilter], p_stream );
2184
2185                 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_new =
2186                     video_new_buffer_filter;
2187                 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_del =
2188                     video_del_buffer_filter;
2189
2190                 id->pp_vfilter[id->i_vfilter]->fmt_in = id->p_encoder->fmt_in;
2191                 id->pp_vfilter[id->i_vfilter]->fmt_out = id->p_encoder->fmt_in;
2192                 id->pp_vfilter[id->i_vfilter]->p_cfg = p_sys->p_vfilters_cfg[i];
2193                 id->pp_vfilter[id->i_vfilter]->p_module =
2194                     module_Need( id->pp_vfilter[id->i_vfilter],
2195                           "video filter2", p_sys->psz_vfilters[i], 0 );
2196                 if( id->pp_vfilter[id->i_vfilter]->p_module )
2197                 {
2198                     id->pp_vfilter[id->i_vfilter]->p_owner =
2199                         malloc( sizeof(filter_owner_sys_t) );
2200                     for( i = 0; i < PICTURE_RING_SIZE; i++ )
2201                         id->pp_vfilter[id->i_vfilter]->p_owner->pp_pics[i] = 0;
2202                     id->pp_vfilter[id->i_vfilter]->p_owner->p_sys = p_sys;
2203
2204                     id->i_vfilter++;
2205                 }
2206                 else
2207                 {
2208                     msg_Dbg( p_stream, "no video filter found" );
2209                     vlc_object_detach( id->pp_vfilter[id->i_vfilter] );
2210                     vlc_object_destroy( id->pp_vfilter[id->i_vfilter] );
2211                 }
2212             }
2213         }
2214
2215         /* Run filter chain */
2216         for( i = 0; i < id->i_filter; i++ )
2217         {
2218             p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
2219         }
2220
2221         /*
2222          * Encoding
2223          */
2224
2225         /* Check if we have a subpicture to overlay */
2226         if( p_sys->p_spu )
2227         {
2228             p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
2229             /* TODO: get another pic */
2230         }
2231
2232         /* Overlay subpicture */
2233         if( p_subpic )
2234         {
2235             int i_scale_width, i_scale_height;
2236             video_format_t *p_fmt;
2237
2238             i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
2239                 id->p_decoder->fmt_out.video.i_width;
2240             i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
2241                 id->p_decoder->fmt_out.video.i_height;
2242
2243             if( p_pic->i_refcount && !id->i_filter )
2244             {
2245                 /* We can't modify the picture, we need to duplicate it */
2246                 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
2247                 if( p_tmp )
2248                 {
2249                     vout_CopyPicture( p_stream, p_tmp, p_pic );
2250                     p_pic->pf_release( p_pic );
2251                     p_pic = p_tmp;
2252                 }
2253             }
2254
2255             if( id->i_filter )
2256                 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
2257             else
2258                 p_fmt = &id->p_decoder->fmt_out.video;
2259
2260             /* FIXME (shouldn't have to be done here) */
2261             p_fmt->i_sar_num = p_fmt->i_aspect *
2262                 p_fmt->i_height / p_fmt->i_width;
2263             p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
2264
2265             spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
2266                                    i_scale_width, i_scale_height );
2267         }
2268
2269         /* Run vfilter chain */
2270         for( i = 0; i < id->i_vfilter; i++ )
2271         {
2272             p_pic = id->pp_vfilter[i]->pf_video_filter(id->pp_vfilter[i], p_pic);
2273         }
2274
2275         if( p_sys->i_threads == 0 )
2276         {
2277             block_t *p_block;
2278             p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2279             block_ChainAppend( out, p_block );
2280         }
2281
2282         if( p_sys->b_master_sync )
2283         {
2284             mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
2285             if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
2286                   || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2287             {
2288                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2289                 date_Set( &id->interpolated_pts, p_pic->date );
2290                 i_pts = p_pic->date + 1;
2291             }
2292             date_Increment( &id->interpolated_pts, 1 );
2293         }
2294
2295         if( p_sys->b_master_sync && i_duplicate > 1 )
2296         {
2297             mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
2298             if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
2299                   || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2300             {
2301                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2302                 date_Set( &id->interpolated_pts, p_pic->date );
2303                 i_pts = p_pic->date + 1;
2304             }
2305             date_Increment( &id->interpolated_pts, 1 );
2306
2307             if( p_sys->i_threads >= 1 )
2308             {
2309                 /* We can't modify the picture, we need to duplicate it */
2310                 p_pic2 = video_new_buffer_decoder( id->p_decoder );
2311                 if( p_pic2 != NULL )
2312                 {
2313                     vout_CopyPicture( p_stream, p_pic2, p_pic );
2314                     p_pic2->date = i_pts;
2315                 }
2316             }
2317             else
2318             {
2319                 block_t *p_block;
2320                 p_pic->date = i_pts;
2321                 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
2322                 block_ChainAppend( out, p_block );
2323             }
2324         }
2325
2326         if( p_sys->i_threads == 0 )
2327         {
2328             p_pic->pf_release( p_pic );
2329         }
2330         else
2331         {
2332             vlc_mutex_lock( &p_sys->lock_out );
2333             p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
2334             p_sys->i_last_pic %= PICTURE_RING_SIZE;
2335             *out = p_sys->p_buffers;
2336             p_sys->p_buffers = NULL;
2337             if( p_pic2 != NULL )
2338             {
2339                 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic2;
2340                 p_sys->i_last_pic %= PICTURE_RING_SIZE;
2341             }
2342             vlc_cond_signal( &p_sys->cond );
2343             vlc_mutex_unlock( &p_sys->lock_out );
2344         }
2345     }
2346
2347     return VLC_SUCCESS;
2348 }
2349
2350 static int EncoderThread( sout_stream_sys_t *p_sys )
2351 {
2352     sout_stream_id_t *id = p_sys->id_video;
2353     picture_t *p_pic;
2354
2355     while( !p_sys->b_die && !p_sys->b_error )
2356     {
2357         block_t *p_block;
2358
2359         vlc_mutex_lock( &p_sys->lock_out );
2360         while( p_sys->i_last_pic == p_sys->i_first_pic )
2361         {
2362             vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
2363             if( p_sys->b_die || p_sys->b_error ) break;
2364         }
2365         if( p_sys->b_die || p_sys->b_error )
2366         {
2367             vlc_mutex_unlock( &p_sys->lock_out );
2368             break;
2369         }
2370
2371         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2372         p_sys->i_first_pic %= PICTURE_RING_SIZE;
2373         vlc_mutex_unlock( &p_sys->lock_out );
2374
2375         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2376         vlc_mutex_lock( &p_sys->lock_out );
2377         block_ChainAppend( &p_sys->p_buffers, p_block );
2378
2379         vlc_mutex_unlock( &p_sys->lock_out );
2380
2381         p_pic->pf_release( p_pic );
2382     }
2383
2384     while( p_sys->i_last_pic != p_sys->i_first_pic )
2385     {
2386         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2387         p_sys->i_first_pic %= PICTURE_RING_SIZE;
2388
2389         p_pic->pf_release( p_pic );
2390     }
2391
2392     block_ChainRelease( p_sys->p_buffers );
2393
2394     return 0;
2395 }
2396
2397 struct picture_sys_t
2398 {
2399     vlc_object_t *p_owner;
2400 };
2401
2402 static void video_release_buffer( picture_t *p_pic )
2403 {
2404     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
2405     {
2406         video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
2407     }
2408     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
2409 }
2410
2411 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
2412                                     sout_stream_sys_t *p_sys )
2413 {
2414     decoder_t *p_dec = (decoder_t *)p_this;
2415     picture_t *p_pic;
2416     int i;
2417
2418     /* Find an empty space in the picture ring buffer */
2419     for( i = 0; i < PICTURE_RING_SIZE; i++ )
2420     {
2421         if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2422         {
2423             pp_ring[i]->i_status = RESERVED_PICTURE;
2424             return pp_ring[i];
2425         }
2426     }
2427     for( i = 0; i < PICTURE_RING_SIZE; i++ )
2428     {
2429         if( pp_ring[i] == 0 ) break;
2430     }
2431
2432     if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
2433     {
2434         int i_first_pic = p_sys->i_first_pic;
2435
2436         if( p_sys->i_first_pic != p_sys->i_last_pic )
2437         {
2438             /* Encoder still has stuff to encode, wait to clear-up the list */
2439             while( p_sys->i_first_pic == i_first_pic )
2440                 msleep( 100000 );
2441         }
2442
2443         /* Find an empty space in the picture ring buffer */
2444         for( i = 0; i < PICTURE_RING_SIZE; i++ )
2445         {
2446             if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2447             {
2448                 pp_ring[i]->i_status = RESERVED_PICTURE;
2449                 return pp_ring[i];
2450             }
2451         }
2452         for( i = 0; i < PICTURE_RING_SIZE; i++ )
2453         {
2454             if( pp_ring[i] == 0 ) break;
2455         }
2456     }
2457
2458     if( i == PICTURE_RING_SIZE )
2459     {
2460         msg_Err( p_this, "decoder/filter is leaking pictures, "
2461                  "resetting its ring buffer" );
2462
2463         for( i = 0; i < PICTURE_RING_SIZE; i++ )
2464         {
2465             pp_ring[i]->pf_release( pp_ring[i] );
2466         }
2467
2468         i = 0;
2469     }
2470
2471     p_pic = malloc( sizeof(picture_t) );
2472     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2473     vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
2474                           p_dec->fmt_out.video.i_chroma,
2475                           p_dec->fmt_out.video.i_width,
2476                           p_dec->fmt_out.video.i_height,
2477                           p_dec->fmt_out.video.i_aspect );
2478
2479     if( !p_pic->i_planes )
2480     {
2481         free( p_pic );
2482         return 0;
2483     }
2484
2485     p_pic->pf_release = video_release_buffer;
2486     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2487     p_pic->p_sys->p_owner = p_this;
2488     p_pic->i_status = RESERVED_PICTURE;
2489
2490     pp_ring[i] = p_pic;
2491
2492     return p_pic;
2493 }
2494
2495 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
2496 {
2497     return video_new_buffer( VLC_OBJECT(p_dec),
2498                              p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2499 }
2500
2501 static picture_t *video_new_buffer_filter( filter_t *p_filter )
2502 {
2503     return video_new_buffer( VLC_OBJECT(p_filter),
2504                              p_filter->p_owner->pp_pics,
2505                              p_filter->p_owner->p_sys );
2506 }
2507
2508 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2509 {
2510     if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2511     if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
2512     if( p_pic ) free( p_pic );
2513 }
2514
2515 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
2516 {
2517     p_pic->i_refcount = 0;
2518     p_pic->i_status = DESTROYED_PICTURE;
2519 }
2520
2521 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
2522 {
2523     p_pic->i_refcount = 0;
2524     p_pic->i_status = DESTROYED_PICTURE;
2525 }
2526
2527 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2528 {
2529     p_pic->i_refcount++;
2530 }
2531
2532 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2533 {
2534     video_release_buffer( p_pic );
2535 }
2536
2537 /*
2538  * SPU
2539  */
2540 static subpicture_t *spu_new_buffer( decoder_t * );
2541 static void spu_del_buffer( decoder_t *, subpicture_t * );
2542
2543 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2544 {
2545     sout_stream_sys_t *p_sys = p_stream->p_sys;
2546
2547     /*
2548      * Open decoder
2549      */
2550
2551     /* Initialization of decoder structures */
2552     id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2553     id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2554     id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2555     /* id->p_decoder->p_cfg = p_sys->p_spu_cfg; */
2556
2557     id->p_decoder->p_module =
2558         module_Need( id->p_decoder, "decoder", "$codec", 0 );
2559
2560     if( !id->p_decoder->p_module )
2561     {
2562         msg_Err( p_stream, "cannot find decoder" );
2563         return VLC_EGENERIC;
2564     }
2565
2566     if( !p_sys->b_soverlay )
2567     {
2568         /*
2569          * Open encoder
2570          */
2571
2572         /* Initialization of encoder format structures */
2573         es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
2574                         id->p_decoder->fmt_in.i_codec );
2575
2576         id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2577
2578         id->p_encoder->p_module =
2579             module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2580
2581         if( !id->p_encoder->p_module )
2582         {
2583             module_Unneed( id->p_decoder, id->p_decoder->p_module );
2584             msg_Err( p_stream, "cannot find encoder" );
2585             return VLC_EGENERIC;
2586         }
2587     }
2588
2589     if( !p_sys->p_spu )
2590     {
2591         p_sys->p_spu = spu_Create( p_stream );
2592         spu_Init( p_sys->p_spu );
2593     }
2594
2595     return VLC_SUCCESS;
2596 }
2597
2598 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2599 {
2600     /* Close decoder */
2601     if( id->p_decoder->p_module )
2602         module_Unneed( id->p_decoder, id->p_decoder->p_module );
2603
2604     /* Close encoder */
2605     if( id->p_encoder->p_module )
2606         module_Unneed( id->p_encoder, id->p_encoder->p_module );
2607 }
2608
2609 static int transcode_spu_process( sout_stream_t *p_stream,
2610                                   sout_stream_id_t *id,
2611                                   block_t *in, block_t **out )
2612 {
2613     sout_stream_sys_t *p_sys = p_stream->p_sys;
2614     subpicture_t *p_subpic;
2615     *out = NULL;
2616
2617     p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2618     if( !p_subpic ) return VLC_EGENERIC;
2619
2620     if( p_sys->b_master_sync && p_sys->i_master_drift )
2621     {
2622         p_subpic->i_start -= p_sys->i_master_drift;
2623         if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2624     }
2625
2626     if( p_sys->b_soverlay )
2627     {
2628         spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2629     }
2630     else
2631     {
2632         block_t *p_block;
2633
2634         p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2635         spu_del_buffer( id->p_decoder, p_subpic );
2636
2637         if( p_block )
2638         {
2639             block_ChainAppend( out, p_block );
2640             return VLC_SUCCESS;
2641         }
2642     }
2643
2644     return VLC_EGENERIC;
2645 }
2646
2647 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2648 {
2649     sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2650     return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2651 }
2652
2653 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2654 {
2655     sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2656     spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );
2657 }
2658
2659 /*
2660  * OSD menu
2661  */
2662 static int transcode_osd_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2663 {
2664     sout_stream_sys_t *p_sys = p_stream->p_sys;
2665     es_format_t fmt;
2666
2667     fmt.i_cat = SPU_ES;
2668     fmt.i_id = 0xbd1f; /* pid ?? */
2669     fmt.i_group = 3;   /* pmt entry ?? */
2670     fmt.i_codec = VLC_FOURCC( 'Y', 'U', 'V', 'A' );
2671     fmt.psz_language = strdup( "osd" );
2672
2673     id = malloc( sizeof( sout_stream_id_t ) );
2674     memset( id, 0, sizeof(sout_stream_id_t) );
2675
2676     id->id = NULL;
2677     id->p_decoder = NULL;
2678     id->p_encoder = NULL;
2679
2680     /* Create encoder object */
2681     id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
2682     if( !id->p_encoder )
2683     {
2684         msg_Err( p_stream, "out of memory" );
2685         goto error;
2686     }
2687     vlc_object_attach( id->p_encoder, p_stream );
2688     id->p_encoder->p_module = NULL;
2689
2690     /* Create fake destination format */
2691     es_format_Init( &id->p_encoder->fmt_out, fmt.i_cat, 0 );
2692     id->p_encoder->fmt_out.i_id    = fmt.i_id;
2693     id->p_encoder->fmt_out.i_group = fmt.i_group;
2694     id->p_encoder->fmt_out.psz_language = strdup( fmt.psz_language );
2695
2696     if( p_sys->i_osdcodec != 0 || p_sys->psz_osdenc )
2697     {
2698         msg_Dbg( p_stream, "creating osdmenu transcoding from fcc=`%4.4s' "
2699                  "to fcc=`%4.4s'", (char*)&fmt.i_codec,
2700                  (char*)&p_sys->i_osdcodec );
2701
2702         /* Complete destination format */
2703         id->p_encoder->fmt_out.i_codec = p_sys->i_osdcodec;
2704
2705         /*
2706          * Open encoder
2707          */
2708
2709         /* Initialization of encoder format structures */
2710         es_format_Init( &id->p_encoder->fmt_in, fmt.i_cat, fmt.i_codec );
2711         id->p_encoder->fmt_in.psz_language = strdup( fmt.psz_language );
2712
2713         id->p_encoder->p_cfg = p_sys->p_osd_cfg;
2714
2715         id->p_encoder->p_module =
2716             module_Need( id->p_encoder, "encoder", p_sys->psz_osdenc, VLC_TRUE );
2717
2718         if( !id->p_encoder->p_module )
2719         {
2720             msg_Err( p_stream, "cannot find encoder" );
2721             goto error;
2722         }
2723
2724         /* open output stream */
2725         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
2726         id->b_transcode = VLC_TRUE;
2727
2728         if( !id->id ) goto error;
2729     }
2730     else
2731     {
2732         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
2733                  (char*)&fmt.i_codec );
2734         id->id = p_sys->p_out->pf_add( p_sys->p_out, &fmt );
2735         id->b_transcode = VLC_FALSE;
2736
2737         if( !id->id ) goto error;
2738     }
2739
2740     p_sys->id_osd = id;
2741     p_sys->b_es_osd = VLC_TRUE;
2742
2743     if( !p_sys->p_spu )
2744     {
2745         p_sys->p_spu = spu_Create( p_stream );
2746         if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
2747             msg_Err( p_sys, "spu initialisation failed" );
2748     }
2749
2750     if( fmt.psz_language )
2751         free( fmt.psz_language );
2752
2753     return VLC_SUCCESS;
2754
2755  error:
2756     msg_Err( p_stream, "starting osd encoding thread failed" );
2757     if( id->p_encoder->p_module )
2758             module_Unneed( id->p_encoder, id->p_encoder->p_module );
2759     if( id->p_encoder )
2760     {
2761         vlc_object_detach( id->p_encoder );
2762         vlc_object_destroy( id->p_encoder );
2763     }
2764     if( fmt.psz_language ) free( fmt.psz_language );
2765     if( id ) free( id );
2766     p_sys->id_osd = NULL;
2767     p_sys->b_es_osd = VLC_FALSE;
2768     return VLC_EGENERIC;
2769 }
2770
2771 static void transcode_osd_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2772 {
2773     sout_stream_sys_t *p_sys = p_stream->p_sys;
2774
2775     /* Close encoder */
2776     if( p_sys->b_es_osd && id )
2777     {
2778         if( id->p_encoder->p_module )
2779             module_Unneed( id->p_encoder, id->p_encoder->p_module );
2780
2781         if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
2782
2783         if( id->p_encoder )
2784         {
2785             vlc_object_detach( id->p_encoder );
2786             vlc_object_destroy( id->p_encoder );
2787         }
2788     }
2789     p_sys->b_es_osd = VLC_FALSE;
2790     if( id ) free( id );
2791 }
2792
2793 static int transcode_osd_process( sout_stream_t *p_stream,
2794                                   sout_stream_id_t *id,
2795                                   block_t *in, block_t **out )
2796 {
2797     sout_stream_sys_t *p_sys = p_stream->p_sys;
2798     subpicture_t *p_subpic = NULL;
2799
2800     /* Check if we have a subpicture to send */
2801     if( p_sys->p_spu && in->i_dts > 0)
2802     {
2803         p_subpic = spu_SortSubpictures( p_sys->p_spu, in->i_dts );
2804     }
2805     else
2806     {
2807         msg_Warn( p_stream, "spu channel not initialized, doing it now" );
2808         if( !p_sys->p_spu )
2809         {
2810             p_sys->p_spu = spu_Create( p_stream );
2811             if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
2812                 msg_Err( p_stream, "spu initialisation failed" );
2813         }
2814     }
2815
2816     if( p_subpic )
2817     {
2818         block_t *p_block = NULL;
2819
2820         if( p_sys->b_master_sync && p_sys->i_master_drift )
2821         {
2822             p_subpic->i_start -= p_sys->i_master_drift;
2823             if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2824         }
2825
2826         p_block = p_sys->id_osd->p_encoder->pf_encode_sub( p_sys->id_osd->p_encoder, p_subpic );
2827         if( p_block )
2828         {
2829             p_block->i_dts = p_block->i_pts = in->i_dts;
2830             block_ChainAppend( out, p_block );
2831             if( *out )
2832             {
2833                 if( p_sys->p_out->pf_send( p_sys->p_out, p_sys->id_osd->id, *out ) == VLC_SUCCESS )
2834                     spu_DestroySubpicture( p_sys->p_spu, p_subpic );
2835             }
2836             return VLC_SUCCESS;
2837         }
2838     }
2839     return VLC_EGENERIC;
2840 }