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