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