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