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