]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode.c
* modules/stream_out/transcode.c: Attempt at having --sout-transcode-fps
[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         return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
901     }
902     else if( !id->b_transcode )
903     {
904         block_Release( p_buffer );
905         return VLC_EGENERIC;
906     }
907
908     switch( id->p_decoder->fmt_in.i_cat )
909     {
910     case AUDIO_ES:
911         transcode_audio_process( p_stream, id, p_buffer, &p_out );
912         break;
913
914     case VIDEO_ES:
915         if( transcode_video_process( p_stream, id, p_buffer, &p_out )
916             != VLC_SUCCESS )
917         {
918             return VLC_EGENERIC;
919         }
920         break;
921
922     case SPU_ES:
923         if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
924             VLC_SUCCESS )
925         {
926             return VLC_EGENERIC;
927         }
928         break;
929
930     default:
931         p_out = NULL;
932         block_Release( p_buffer );
933         break;
934     }
935
936     if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
937     return VLC_SUCCESS;
938 }
939
940 /****************************************************************************
941  * decoder reencoder part
942  ****************************************************************************/
943 int audio_BitsPerSample( vlc_fourcc_t i_format )
944 {
945     switch( i_format )
946     {
947     case VLC_FOURCC('u','8',' ',' '):
948     case VLC_FOURCC('s','8',' ',' '):
949         return 8;
950
951     case VLC_FOURCC('u','1','6','l'):
952     case VLC_FOURCC('s','1','6','l'):
953     case VLC_FOURCC('u','1','6','b'):
954     case VLC_FOURCC('s','1','6','b'):
955         return 16;
956
957     case VLC_FOURCC('u','2','4','l'):
958     case VLC_FOURCC('s','2','4','l'):
959     case VLC_FOURCC('u','2','4','b'):
960     case VLC_FOURCC('s','2','4','b'):
961         return 24;
962
963     case VLC_FOURCC('u','3','2','l'):
964     case VLC_FOURCC('s','3','2','l'):
965     case VLC_FOURCC('u','3','2','b'):
966     case VLC_FOURCC('s','3','2','b'):
967     case VLC_FOURCC('f','l','3','2'):
968     case VLC_FOURCC('f','i','3','2'):
969         return 32;
970
971     case VLC_FOURCC('f','l','6','4'):
972         return 64;
973     }
974
975     return 0;
976 }
977
978 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
979                                              sout_stream_id_t *id,
980                                              es_format_t *p_fmt_in,
981                                              es_format_t *p_fmt_out )
982 {
983     filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
984
985     vlc_object_attach( p_filter, p_stream );
986     p_filter->pf_audio_buffer_new = (block_t* (*) (filter_t*, int))__block_New;
987
988     p_filter->fmt_in = *p_fmt_in;
989     p_filter->fmt_out = *p_fmt_out;
990
991     p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
992     if( p_filter->p_module )
993     {
994         p_filter->fmt_out.audio.i_bitspersample = 
995             audio_BitsPerSample( p_filter->fmt_out.i_codec );
996         *p_fmt_in = p_filter->fmt_out;
997     }
998     else
999     {
1000         vlc_object_detach( p_filter );
1001         vlc_object_destroy( p_filter );
1002         p_filter = 0;
1003     }
1004
1005     return p_filter;
1006 }
1007
1008 static int transcode_audio_new( sout_stream_t *p_stream,
1009                                 sout_stream_id_t *id )
1010 {
1011     sout_stream_sys_t *p_sys = p_stream->p_sys;
1012     es_format_t fmt_last;
1013     int i_pass = 6;
1014
1015     /*
1016      * Open decoder
1017      */
1018
1019     /* Initialization of decoder structures */
1020     id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1021     id->p_decoder->fmt_out.i_extra = 0;
1022     id->p_decoder->fmt_out.p_extra = 0;
1023     id->p_decoder->pf_decode_audio = 0;
1024     id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
1025     id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1026     //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1027
1028     id->p_decoder->p_module =
1029         module_Need( id->p_decoder, "decoder", "$codec", 0 );
1030
1031     if( !id->p_decoder->p_module )
1032     {
1033         msg_Err( p_stream, "cannot find decoder" );
1034         return VLC_EGENERIC;
1035     }
1036     id->p_decoder->fmt_out.audio.i_bitspersample = 
1037         audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1038     fmt_last = id->p_decoder->fmt_out;
1039     /* FIX decoders so we don't have to do this */
1040     fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1041
1042     /*
1043      * Open encoder
1044      */
1045
1046     /* Initialization of encoder format structures */
1047     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1048                     id->p_decoder->fmt_out.i_codec );
1049     id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
1050
1051     /* Initialization of encoder format structures */
1052     es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1053     id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1054     id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
1055     id->p_encoder->fmt_in.audio.i_physical_channels =
1056         id->p_encoder->fmt_out.audio.i_physical_channels;
1057     id->p_encoder->fmt_in.audio.i_original_channels =
1058         id->p_encoder->fmt_out.audio.i_original_channels;
1059     id->p_encoder->fmt_in.audio.i_channels =
1060         id->p_encoder->fmt_out.audio.i_channels;
1061     id->p_encoder->fmt_in.audio.i_bitspersample =
1062         audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1063
1064     id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1065
1066     id->p_encoder->p_module =
1067         module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
1068     if( !id->p_encoder->p_module )
1069     {
1070         msg_Err( p_stream, "cannot find encoder" );
1071         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1072         id->p_decoder->p_module = 0;
1073         return VLC_EGENERIC;
1074     }
1075     id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1076     id->p_encoder->fmt_in.audio.i_bitspersample =
1077         audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1078
1079     /* Load conversion filters */
1080     if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1081         fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1082     {
1083         /* We'll have to go through fl32 first */
1084         es_format_t fmt_out = id->p_encoder->fmt_in;
1085         fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1086
1087         id->pp_filter[id->i_filter] =
1088             transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );
1089
1090         if( id->pp_filter[id->i_filter] ) id->i_filter++;
1091     }
1092
1093     while( i_pass-- )
1094     {
1095         if( fmt_last.audio.i_channels !=
1096             id->p_encoder->fmt_in.audio.i_channels ||
1097             fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
1098             fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1099         {
1100             id->pp_filter[id->i_filter] =
1101                 transcode_audio_filter_new( p_stream, id, &fmt_last,
1102                                             &id->p_encoder->fmt_in );
1103
1104             if( id->pp_filter[id->i_filter] ) id->i_filter++;
1105             else break;
1106         }
1107     }
1108
1109     /* Final checks to see if conversions were successful */
1110     if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1111     {
1112         msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1113                  (char *)&fmt_last.i_codec,
1114                  (char *)&id->p_encoder->fmt_in.i_codec );
1115         transcode_audio_close( p_stream, id );
1116         return VLC_EGENERIC;
1117     }
1118
1119     if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1120     {
1121         msg_Err( p_stream, "no audio filter found for mixing from"
1122                  " %i to %i channels", fmt_last.audio.i_channels,
1123                  id->p_encoder->fmt_in.audio.i_channels );
1124 #if 0
1125         /* FIXME : this might work, but only if the encoder is restarted */
1126         id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1127         id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1128
1129         id->p_encoder->fmt_in.audio.i_physical_channels =
1130             id->p_encoder->fmt_in.audio.i_original_channels =
1131                 fmt_last.audio.i_physical_channels;
1132         id->p_encoder->fmt_out.audio.i_physical_channels =
1133             id->p_encoder->fmt_out.audio.i_original_channels =
1134                 fmt_last.audio.i_physical_channels;
1135 #else
1136         transcode_audio_close( p_stream, id );
1137         return VLC_EGENERIC;
1138 #endif
1139     }
1140
1141     if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1142     {
1143         msg_Err( p_stream, "no audio filter found for resampling from"
1144                  " %iHz to %iHz", fmt_last.audio.i_rate,
1145                  id->p_encoder->fmt_in.audio.i_rate );
1146 #if 0
1147         /* FIXME : this might work, but only if the encoder is restarted */
1148         id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1149         id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1150 #else
1151         transcode_audio_close( p_stream, id );
1152         return VLC_EGENERIC;
1153 #endif
1154     }
1155
1156     /* FIXME: Hack for mp3 transcoding support */
1157     if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1158         id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1159
1160     return VLC_SUCCESS;
1161 }
1162
1163 static void transcode_audio_close( sout_stream_t *p_stream,
1164                                    sout_stream_id_t *id )
1165 {
1166     int i;
1167
1168     /* Close decoder */
1169     if( id->p_decoder->p_module )
1170         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1171     id->p_decoder->p_module = 0;
1172
1173     /* Close encoder */
1174     if( id->p_encoder->p_module )
1175         module_Unneed( id->p_encoder, id->p_encoder->p_module );
1176     id->p_encoder->p_module = 0;
1177
1178     /* Close filters */
1179     for( i = 0; i < id->i_filter; i++ )
1180     {
1181         vlc_object_detach( id->pp_filter[i] );
1182         if( id->pp_filter[i]->p_module )
1183             module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1184         vlc_object_destroy( id->pp_filter[i] );
1185     }
1186 }
1187
1188 static int transcode_audio_process( sout_stream_t *p_stream,
1189                                     sout_stream_id_t *id,
1190                                     block_t *in, block_t **out )
1191 {
1192     sout_stream_sys_t *p_sys = p_stream->p_sys;
1193     aout_buffer_t *p_audio_buf;
1194     block_t *p_block, *p_audio_block;
1195     int i;
1196     *out = NULL;
1197
1198     while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1199                                                           &in )) )
1200     {
1201         if( p_sys->b_master_sync )
1202         {
1203             mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1204             if ( i_dts == 1 )
1205             {
1206                 date_Set( &id->interpolated_pts, p_audio_buf->start_date );
1207                 i_dts = p_audio_buf->start_date + 1;
1208             }
1209             p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1210             date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1211             p_audio_buf->start_date -= p_sys->i_master_drift;
1212             p_audio_buf->end_date -= p_sys->i_master_drift;
1213         }
1214
1215         p_audio_block = p_audio_buf->p_sys;
1216         p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1217         p_audio_block->i_dts = p_audio_block->i_pts =
1218             p_audio_buf->start_date;
1219         p_audio_block->i_length = p_audio_buf->end_date -
1220             p_audio_buf->start_date;
1221         p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1222
1223         /* Run filter chain */
1224         for( i = 0; i < id->i_filter; i++ )
1225         {
1226             p_audio_block =
1227                 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1228                                                    p_audio_block );
1229         }
1230
1231         p_audio_buf->p_buffer = p_audio_block->p_buffer;
1232         p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1233         p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1234         p_audio_buf->start_date = p_audio_block->i_dts;
1235         p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1236
1237         p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1238         block_ChainAppend( out, p_block );
1239         block_Release( p_audio_block );
1240         free( p_audio_buf );
1241     }
1242
1243     return VLC_SUCCESS;
1244 }
1245
1246 static void audio_release_buffer( aout_buffer_t *p_buffer )
1247 {
1248     if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1249     if( p_buffer ) free( p_buffer );
1250 }
1251
1252 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1253 {
1254     aout_buffer_t *p_buffer;
1255     block_t *p_block;
1256     int i_size;
1257
1258     if( p_dec->fmt_out.audio.i_bitspersample )
1259     {
1260         i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1261             p_dec->fmt_out.audio.i_channels;
1262     }
1263     else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1264              p_dec->fmt_out.audio.i_frame_length )
1265     {
1266         i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1267             p_dec->fmt_out.audio.i_frame_length;
1268     }
1269     else
1270     {
1271         i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1272     }
1273
1274     p_buffer = malloc( sizeof(aout_buffer_t) );
1275     p_buffer->pf_release = audio_release_buffer;
1276     p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1277
1278     p_buffer->p_buffer = p_block->p_buffer;
1279     p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1280     p_buffer->i_nb_samples = i_samples;
1281     p_block->i_samples = i_samples;
1282
1283     return p_buffer;
1284 }
1285
1286 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1287 {
1288     if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1289     if( p_buffer ) free( p_buffer );
1290 }
1291
1292 /*
1293  * video
1294  */
1295 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1296 {
1297     sout_stream_sys_t *p_sys = p_stream->p_sys;
1298     int i;
1299
1300     /*
1301      * Open decoder
1302      */
1303
1304     /* Initialization of decoder structures */
1305     id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1306     id->p_decoder->fmt_out.i_extra = 0;
1307     id->p_decoder->fmt_out.p_extra = 0;
1308     id->p_decoder->pf_decode_video = 0;
1309     id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1310     id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1311     id->p_decoder->pf_picture_link    = video_link_picture_decoder;
1312     id->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
1313     id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1314     for( i = 0; i < PICTURE_RING_SIZE; i++ )
1315         id->p_decoder->p_owner->pp_pics[i] = 0;
1316     id->p_decoder->p_owner->p_sys = p_sys;
1317     //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1318
1319     id->p_decoder->p_module =
1320         module_Need( id->p_decoder, "decoder", "$codec", 0 );
1321
1322     if( !id->p_decoder->p_module )
1323     {
1324         msg_Err( p_stream, "cannot find decoder" );
1325         return VLC_EGENERIC;
1326     }
1327
1328     /*
1329      * Open encoder.
1330      * Because some info about the decoded input will only be available
1331      * once the first frame is decoded, we actually only test the availability
1332      * of the encoder here.
1333      */
1334
1335     /* Initialization of encoder format structures */
1336     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1337                     id->p_decoder->fmt_out.i_codec );
1338     id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1339
1340     /* The dimensions will be set properly later on.
1341      * Just put sensible values so we can test an encoder is available. */
1342     id->p_encoder->fmt_in.video.i_width =
1343         id->p_encoder->fmt_out.video.i_width ?
1344         id->p_encoder->fmt_out.video.i_width :
1345         id->p_decoder->fmt_in.video.i_width ?
1346         id->p_decoder->fmt_in.video.i_width : 16;
1347     id->p_encoder->fmt_in.video.i_height =
1348         id->p_encoder->fmt_out.video.i_height ?
1349         id->p_encoder->fmt_out.video.i_height :
1350         id->p_decoder->fmt_in.video.i_height ?
1351         id->p_decoder->fmt_in.video.i_height : 16;
1352     id->p_encoder->fmt_in.video.i_frame_rate = 25;
1353     id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1354
1355     id->p_encoder->i_threads = p_sys->i_threads;
1356     id->p_encoder->p_cfg = p_sys->p_video_cfg;
1357
1358     id->p_encoder->p_module =
1359         module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1360     if( !id->p_encoder->p_module )
1361     {
1362         msg_Err( p_stream, "cannot find encoder" );
1363         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1364         id->p_decoder->p_module = 0;
1365         return VLC_EGENERIC;
1366     }
1367
1368     /* Close the encoder.
1369      * We'll open it only when we have the first frame. */
1370     module_Unneed( id->p_encoder, id->p_encoder->p_module );
1371     id->p_encoder->p_module = NULL;
1372
1373     if( p_sys->i_threads >= 1 )
1374     {
1375         int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
1376                            VLC_THREAD_PRIORITY_VIDEO;
1377         p_sys->id_video = id;
1378         vlc_mutex_init( p_stream, &p_sys->lock_out );
1379         vlc_cond_init( p_stream, &p_sys->cond );
1380         memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1381         p_sys->i_first_pic = 0;
1382         p_sys->i_last_pic = 0;
1383         p_sys->p_buffers = NULL;
1384         p_sys->b_die = p_sys->b_error = 0;
1385         if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
1386                                VLC_FALSE ) )
1387         {
1388             msg_Err( p_stream, "cannot spawn encoder thread" );
1389             module_Unneed( id->p_decoder, id->p_decoder->p_module );
1390             id->p_decoder->p_module = 0;
1391             return VLC_EGENERIC;
1392         }
1393     }
1394
1395     return VLC_SUCCESS;
1396 }
1397
1398 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1399                                          sout_stream_id_t *id )
1400 {
1401     sout_stream_sys_t *p_sys = p_stream->p_sys;
1402
1403     /* Hack because of the copy packetizer which can fail to detect the
1404      * proper size (which forces us to wait until the 1st frame
1405      * is decoded) */
1406     int i_width = id->p_decoder->fmt_out.video.i_width -
1407         p_sys->i_crop_left - p_sys->i_crop_right;
1408     int i_height = id->p_decoder->fmt_out.video.i_height -
1409         p_sys->i_crop_top - p_sys->i_crop_bottom;
1410
1411     if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1412         id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1413     {
1414         /* Apply the scaling */
1415         id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1416         id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1417     }
1418     else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1419              id->p_encoder->fmt_out.video.i_height <= 0 )
1420     {
1421         id->p_encoder->fmt_out.video.i_height =
1422             id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1423     }
1424     else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1425              id->p_encoder->fmt_out.video.i_height > 0 )
1426     {
1427         id->p_encoder->fmt_out.video.i_width =
1428             id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1429     }
1430
1431     /* Make sure the size is at least a multiple of 2 */
1432     id->p_encoder->fmt_out.video.i_width =
1433         (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
1434     id->p_encoder->fmt_out.video.i_height =
1435         (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
1436
1437     id->p_encoder->fmt_in.video.i_width =
1438         id->p_encoder->fmt_out.video.i_width;
1439     id->p_encoder->fmt_in.video.i_height =
1440         id->p_encoder->fmt_out.video.i_height;
1441
1442     if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1443         !id->p_encoder->fmt_out.video.i_frame_rate_base )
1444     {
1445         if( id->p_decoder->fmt_out.video.i_frame_rate &&
1446             id->p_decoder->fmt_out.video.i_frame_rate_base )
1447         {
1448             id->p_encoder->fmt_out.video.i_frame_rate =
1449                 id->p_decoder->fmt_out.video.i_frame_rate;
1450             id->p_encoder->fmt_out.video.i_frame_rate_base =
1451                 id->p_decoder->fmt_out.video.i_frame_rate_base;
1452         }
1453         else
1454         {
1455             /* Pick a sensible default value */
1456             id->p_encoder->fmt_out.video.i_frame_rate = 25;
1457             id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1458         }
1459     }
1460
1461     id->p_encoder->fmt_in.video.i_frame_rate =
1462         id->p_encoder->fmt_out.video.i_frame_rate;
1463     id->p_encoder->fmt_in.video.i_frame_rate_base =
1464         id->p_encoder->fmt_out.video.i_frame_rate_base;
1465
1466     date_Init( &id->interpolated_pts,
1467                id->p_encoder->fmt_out.video.i_frame_rate,
1468                id->p_encoder->fmt_out.video.i_frame_rate_base );
1469
1470     /* Check whether a particular aspect ratio was requested */
1471     if( !id->p_encoder->fmt_out.video.i_aspect )
1472     {
1473         id->p_encoder->fmt_out.video.i_aspect =
1474             id->p_decoder->fmt_out.video.i_aspect;
1475     }
1476     id->p_encoder->fmt_in.video.i_aspect =
1477         id->p_encoder->fmt_out.video.i_aspect;
1478
1479     id->p_encoder->p_module =
1480         module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1481     if( !id->p_encoder->p_module )
1482     {
1483         msg_Err( p_stream, "cannot find encoder" );
1484         return VLC_EGENERIC;
1485     }
1486
1487     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1488
1489     /* Hack for mp2v/mp1v transcoding support */
1490     if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1491         id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1492     {
1493         id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1494     }
1495
1496     id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1497                                              &id->p_encoder->fmt_out );
1498     if( !id->id )
1499     {
1500         msg_Err( p_stream, "cannot add this stream" );
1501         return VLC_EGENERIC;
1502     }
1503
1504     return VLC_SUCCESS;
1505 }
1506
1507 static void transcode_video_close( sout_stream_t *p_stream,
1508                                    sout_stream_id_t *id )
1509 {
1510     int i, j;
1511
1512     if( p_stream->p_sys->i_threads >= 1 )
1513     {
1514         vlc_mutex_lock( &p_stream->p_sys->lock_out );
1515         p_stream->p_sys->b_die = 1;
1516         vlc_cond_signal( &p_stream->p_sys->cond );
1517         vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1518         vlc_thread_join( p_stream->p_sys );
1519         vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1520         vlc_cond_destroy( &p_stream->p_sys->cond );
1521     }
1522
1523     /* Close decoder */
1524     if( id->p_decoder->p_module )
1525         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1526
1527     if( id->p_decoder->p_owner )
1528     {
1529         /* Clean-up pictures ring buffer */
1530         for( i = 0; i < PICTURE_RING_SIZE; i++ )
1531         {
1532             if( id->p_decoder->p_owner->pp_pics[i] )
1533                 video_del_buffer( VLC_OBJECT(id->p_decoder),
1534                                   id->p_decoder->p_owner->pp_pics[i] );
1535         }
1536         free( id->p_decoder->p_owner );
1537     }
1538
1539     /* Close encoder */
1540     if( id->p_encoder->p_module )
1541         module_Unneed( id->p_encoder, id->p_encoder->p_module );
1542
1543     /* Close filters */
1544     for( i = 0; i < id->i_filter; i++ )
1545     {
1546         vlc_object_detach( id->pp_filter[i] );
1547         if( id->pp_filter[i]->p_module )
1548             module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1549
1550         /* Clean-up pictures ring buffer */
1551         for( j = 0; j < PICTURE_RING_SIZE; j++ )
1552         {
1553             if( id->pp_filter[i]->p_owner->pp_pics[j] )
1554                 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1555                                   id->pp_filter[i]->p_owner->pp_pics[j] );
1556         }
1557         free( id->pp_filter[i]->p_owner );
1558
1559         vlc_object_destroy( id->pp_filter[i] );
1560     }
1561     for( i = 0; i < id->i_vfilter; i++ )
1562     {
1563         vlc_object_detach( id->pp_vfilter[i] );
1564         if( id->pp_vfilter[i]->p_module )
1565             module_Unneed( id->pp_vfilter[i], id->pp_vfilter[i]->p_module );
1566
1567         /* Clean-up pictures ring buffer */
1568         for( j = 0; j < PICTURE_RING_SIZE; j++ )
1569         {
1570             if( id->pp_vfilter[i]->p_owner->pp_pics[j] )
1571                 video_del_buffer( VLC_OBJECT(id->pp_vfilter[i]),
1572                                   id->pp_vfilter[i]->p_owner->pp_pics[j] );
1573         }
1574         free( id->pp_vfilter[i]->p_owner );
1575
1576         vlc_object_destroy( id->pp_vfilter[i] );
1577     }
1578 }
1579
1580 static int transcode_video_process( sout_stream_t *p_stream,
1581                                     sout_stream_id_t *id,
1582                                     block_t *in, block_t **out )
1583 {
1584     sout_stream_sys_t *p_sys = p_stream->p_sys;
1585     int i_duplicate = 1, i;
1586     picture_t *p_pic;
1587     *out = NULL;
1588
1589     while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1590     {
1591         subpicture_t *p_subpic = 0;
1592
1593         if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
1594         {
1595             mtime_t current_date = mdate();
1596             if( current_date + 50000 > p_pic->date )
1597             {
1598                 msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
1599                          current_date + 50000 - p_pic->date );
1600                 p_pic->pf_release( p_pic );
1601                 continue;
1602             }
1603         }
1604
1605         if( p_sys->b_master_sync )
1606         {
1607             mtime_t i_video_drift;
1608             mtime_t i_master_drift = p_sys->i_master_drift;
1609             mtime_t i_pts;
1610
1611             i_pts = date_Get( &id->interpolated_pts ) + 1;
1612             if ( i_pts == 1 )
1613             {
1614                 date_Set( &id->interpolated_pts, p_pic->date );
1615                 i_pts = p_pic->date + 1;
1616             }
1617             i_video_drift = p_pic->date - i_pts;
1618             i_duplicate = 1;
1619
1620             /* Set the pts of the frame being encoded */
1621             p_pic->date = i_pts;
1622
1623             if( i_video_drift < i_master_drift - 50000 )
1624             {
1625 #if 0
1626                 msg_Dbg( p_stream, "dropping frame (%i)",
1627                          (int)(i_video_drift - i_master_drift) );
1628 #endif
1629                 p_pic->pf_release( p_pic );
1630                 continue;
1631             }
1632             else if( i_video_drift > i_master_drift + 50000 )
1633             {
1634 #if 0
1635                 msg_Dbg( p_stream, "adding frame (%i)",
1636                          (int)(i_video_drift - i_master_drift) );
1637 #endif
1638                 i_duplicate = 2;
1639             }
1640         }
1641
1642         if( !id->p_encoder->p_module )
1643         {
1644             if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1645             {
1646                 p_pic->pf_release( p_pic );
1647                 transcode_video_close( p_stream, id );
1648                 id->b_transcode = VLC_FALSE;
1649                 return VLC_EGENERIC;
1650             }
1651
1652             /* Deinterlace */
1653             if( p_stream->p_sys->b_deinterlace )
1654             {
1655                 id->pp_filter[id->i_filter] =
1656                     vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1657                 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1658
1659                 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1660                     video_new_buffer_filter;
1661                 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1662                     video_del_buffer_filter;
1663
1664                 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1665                 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
1666                 id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
1667                 id->pp_filter[id->i_filter]->p_module =
1668                     module_Need( id->pp_filter[id->i_filter],
1669                                  "video filter2", p_sys->psz_deinterlace, 0 );
1670                 if( id->pp_filter[id->i_filter]->p_module )
1671                 {
1672                     id->pp_filter[id->i_filter]->p_owner =
1673                         malloc( sizeof(filter_owner_sys_t) );
1674                     for( i = 0; i < PICTURE_RING_SIZE; i++ )
1675                         id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1676                     id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1677
1678                     id->i_filter++;
1679                 }
1680                 else
1681                 {
1682                     msg_Dbg( p_stream, "no video filter found" );
1683                     vlc_object_detach( id->pp_filter[id->i_filter] );
1684                     vlc_object_destroy( id->pp_filter[id->i_filter] );
1685                 }
1686             }
1687
1688             /* Check if we need a filter for chroma conversion or resizing */
1689             if( id->p_decoder->fmt_out.video.i_chroma !=
1690                 id->p_encoder->fmt_in.video.i_chroma ||
1691                 id->p_decoder->fmt_out.video.i_width !=
1692                 id->p_encoder->fmt_out.video.i_width ||
1693                 id->p_decoder->fmt_out.video.i_height !=
1694                 id->p_encoder->fmt_out.video.i_height ||
1695                 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1696                 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1697             {
1698                 id->pp_filter[id->i_filter] =
1699                     vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1700                 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1701
1702                 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1703                     video_new_buffer_filter;
1704                 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1705                     video_del_buffer_filter;
1706
1707                 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1708                 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1709                 id->pp_filter[id->i_filter]->p_cfg = NULL;
1710                 id->pp_filter[id->i_filter]->p_module =
1711                     module_Need( id->pp_filter[id->i_filter],
1712                                  "video filter2", 0, 0 );
1713                 if( id->pp_filter[id->i_filter]->p_module )
1714                 {
1715                     id->pp_filter[id->i_filter]->p_owner =
1716                         malloc( sizeof(filter_owner_sys_t) );
1717                     for( i = 0; i < PICTURE_RING_SIZE; i++ )
1718                         id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1719                     id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1720
1721                     id->i_filter++;
1722                 }
1723                 else
1724                 {
1725                     msg_Dbg( p_stream, "no video filter found" );
1726                     vlc_object_detach( id->pp_filter[id->i_filter] );
1727                     vlc_object_destroy( id->pp_filter[id->i_filter] );
1728
1729                     p_pic->pf_release( p_pic );
1730                     transcode_video_close( p_stream, id );
1731                     id->b_transcode = VLC_FALSE;
1732                     return VLC_EGENERIC;
1733                 }
1734             }
1735
1736             for( i = 0; i < p_sys->i_vfilters; i++ )
1737             {
1738                 id->pp_vfilter[id->i_vfilter] =
1739                     vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1740                 vlc_object_attach( id->pp_vfilter[id->i_vfilter], p_stream );
1741
1742                 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_new =
1743                     video_new_buffer_filter;
1744                 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_del =
1745                     video_del_buffer_filter;
1746
1747                 id->pp_vfilter[id->i_vfilter]->fmt_in = id->p_encoder->fmt_in;
1748                 id->pp_vfilter[id->i_vfilter]->fmt_out = id->p_encoder->fmt_in;
1749                 id->pp_vfilter[id->i_vfilter]->p_cfg = p_sys->p_vfilters_cfg[i];
1750                 id->pp_vfilter[id->i_vfilter]->p_module =
1751                     module_Need( id->pp_vfilter[id->i_vfilter],
1752                           "video filter2", p_sys->psz_vfilters[i], 0 );
1753                 if( id->pp_vfilter[id->i_vfilter]->p_module )
1754                 {
1755                     id->pp_vfilter[id->i_vfilter]->p_owner =
1756                         malloc( sizeof(filter_owner_sys_t) );
1757                     for( i = 0; i < PICTURE_RING_SIZE; i++ )
1758                         id->pp_vfilter[id->i_vfilter]->p_owner->pp_pics[i] = 0;
1759                     id->pp_vfilter[id->i_vfilter]->p_owner->p_sys = p_sys;
1760
1761                     id->i_vfilter++;
1762                 }
1763                 else
1764                 {
1765                     msg_Dbg( p_stream, "no video filter found" );
1766                     vlc_object_detach( id->pp_vfilter[id->i_vfilter] );
1767                     vlc_object_destroy( id->pp_vfilter[id->i_vfilter] );
1768                 }
1769             }
1770         }
1771
1772         /* Run filter chain */
1773         for( i = 0; i < id->i_filter; i++ )
1774         {
1775             p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1776         }
1777
1778         /*
1779          * Encoding
1780          */
1781
1782         /* Check if we have a subpicture to overlay */
1783         if( p_sys->p_spu )
1784         {
1785             p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
1786             /* TODO: get another pic */
1787         }
1788
1789         /* Overlay subpicture */
1790         if( p_subpic )
1791         {
1792             int i_scale_width, i_scale_height;
1793             video_format_t *p_fmt;
1794
1795             i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
1796                 id->p_decoder->fmt_out.video.i_width;
1797             i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
1798                 id->p_decoder->fmt_out.video.i_height;
1799
1800             if( p_pic->i_refcount && !id->i_filter )
1801             {
1802                 /* We can't modify the picture, we need to duplicate it */
1803                 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1804                 if( p_tmp )
1805                 {
1806                     vout_CopyPicture( p_stream, p_tmp, p_pic );
1807                     p_pic->pf_release( p_pic );
1808                     p_pic = p_tmp;
1809                 }
1810             }
1811
1812             if( id->i_filter )
1813                 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
1814             else
1815                 p_fmt = &id->p_decoder->fmt_out.video;
1816
1817             /* FIXME (shouldn't have to be done here) */
1818             p_fmt->i_sar_num = p_fmt->i_aspect *
1819                 p_fmt->i_height / p_fmt->i_width;
1820             p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
1821
1822             spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
1823                                    i_scale_width, i_scale_height );
1824         }
1825
1826         /* Run vfilter chain */
1827         for( i = 0; i < id->i_vfilter; i++ )
1828         {
1829             p_pic = id->pp_vfilter[i]->pf_video_filter(id->pp_vfilter[i], p_pic);
1830         }
1831
1832         if( p_sys->i_threads >= 1 )
1833         {
1834             vlc_mutex_lock( &p_sys->lock_out );
1835             p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1836             p_sys->i_last_pic %= PICTURE_RING_SIZE;
1837             *out = p_sys->p_buffers;
1838             p_sys->p_buffers = NULL;
1839             vlc_cond_signal( &p_sys->cond );
1840             vlc_mutex_unlock( &p_sys->lock_out );
1841         }
1842         else
1843         {
1844             block_t *p_block;
1845             p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1846             block_ChainAppend( out, p_block );
1847         }
1848
1849         if( p_sys->b_master_sync )
1850         {
1851             mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1852             if ( i_pts == 1 )
1853             {
1854                 date_Set( &id->interpolated_pts, p_pic->date );
1855                 i_pts = p_pic->date + 1;
1856             }
1857             date_Increment( &id->interpolated_pts, 1 );
1858         }
1859
1860         if( p_sys->b_master_sync && i_duplicate > 1 )
1861         {
1862             mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1863             if ( i_pts == 1 )
1864             {
1865                 date_Set( &id->interpolated_pts, p_pic->date );
1866                 i_pts = p_pic->date + 1;
1867             }
1868             date_Increment( &id->interpolated_pts, 1 );
1869
1870             if( p_sys->i_threads >= 1 )
1871             {
1872                 /* We can't modify the picture, we need to duplicate it */
1873                 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1874                 if( p_tmp != NULL )
1875                 {
1876                     vout_CopyPicture( p_stream, p_tmp, p_pic );
1877                     p_pic = p_tmp;
1878
1879                     vlc_mutex_lock( &p_sys->lock_out );
1880                     p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1881                     p_sys->i_last_pic %= PICTURE_RING_SIZE;
1882                     vlc_cond_signal( &p_sys->cond );
1883                     vlc_mutex_unlock( &p_sys->lock_out );
1884                 }
1885             }
1886             else
1887             {
1888                 block_t *p_block;
1889                 p_pic->date = i_pts;
1890                 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1891                 block_ChainAppend( out, p_block );
1892             }
1893         }
1894
1895         if( p_sys->i_threads == 0 )
1896             p_pic->pf_release( p_pic );
1897     }
1898
1899     return VLC_SUCCESS;
1900 }
1901
1902 static int EncoderThread( sout_stream_sys_t *p_sys )
1903 {
1904     sout_stream_id_t *id = p_sys->id_video;
1905     picture_t *p_pic;
1906
1907     while( !p_sys->b_die && !p_sys->b_error )
1908     {
1909         block_t *p_block;
1910
1911         vlc_mutex_lock( &p_sys->lock_out );
1912         while( p_sys->i_last_pic == p_sys->i_first_pic )
1913         {
1914             vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1915             if( p_sys->b_die || p_sys->b_error ) break;
1916         }
1917         if( p_sys->b_die || p_sys->b_error )
1918         {
1919             vlc_mutex_unlock( &p_sys->lock_out );
1920             break;
1921         }
1922
1923         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1924         p_sys->i_first_pic %= PICTURE_RING_SIZE;
1925         vlc_mutex_unlock( &p_sys->lock_out );
1926
1927         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1928         vlc_mutex_lock( &p_sys->lock_out );
1929         block_ChainAppend( &p_sys->p_buffers, p_block );
1930
1931         vlc_mutex_unlock( &p_sys->lock_out );
1932
1933         p_pic->pf_release( p_pic );
1934     }
1935
1936     while( p_sys->i_last_pic != p_sys->i_first_pic )
1937     {
1938         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1939         p_sys->i_first_pic %= PICTURE_RING_SIZE;
1940
1941         p_pic->pf_release( p_pic );
1942     }
1943
1944     block_ChainRelease( p_sys->p_buffers );
1945
1946     return 0;
1947 }
1948
1949 struct picture_sys_t
1950 {
1951     vlc_object_t *p_owner;
1952 };
1953
1954 static void video_release_buffer( picture_t *p_pic )
1955 {
1956     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
1957     {
1958         video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
1959     }
1960     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
1961 }
1962
1963 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
1964                                     sout_stream_sys_t *p_sys )
1965 {
1966     decoder_t *p_dec = (decoder_t *)p_this;
1967     picture_t *p_pic;
1968     int i;
1969
1970     /* Find an empty space in the picture ring buffer */
1971     for( i = 0; i < PICTURE_RING_SIZE; i++ )
1972     {
1973         if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
1974         {
1975             pp_ring[i]->i_status = RESERVED_PICTURE;
1976             return pp_ring[i];
1977         }
1978     }
1979     for( i = 0; i < PICTURE_RING_SIZE; i++ )
1980     {
1981         if( pp_ring[i] == 0 ) break;
1982     }
1983
1984     if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
1985     {
1986         int i_first_pic = p_sys->i_first_pic;
1987
1988         if( p_sys->i_first_pic != p_sys->i_last_pic )
1989         {
1990             /* Encoder still has stuff to encode, wait to clear-up the list */
1991             while( p_sys->i_first_pic == i_first_pic )
1992                 msleep( 100000 );
1993         }
1994
1995         /* Find an empty space in the picture ring buffer */
1996         for( i = 0; i < PICTURE_RING_SIZE; i++ )
1997         {
1998             if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
1999             {
2000                 pp_ring[i]->i_status = RESERVED_PICTURE;
2001                 return pp_ring[i];
2002             }
2003         }
2004         for( i = 0; i < PICTURE_RING_SIZE; i++ )
2005         {
2006             if( pp_ring[i] == 0 ) break;
2007         }
2008     }
2009
2010     if( i == PICTURE_RING_SIZE )
2011     {
2012         msg_Err( p_this, "decoder/filter is leaking pictures, "
2013                  "resetting its ring buffer" );
2014
2015         for( i = 0; i < PICTURE_RING_SIZE; i++ )
2016         {
2017             pp_ring[i]->pf_release( pp_ring[i] );
2018         }
2019
2020         i = 0;
2021     }
2022
2023     p_pic = malloc( sizeof(picture_t) );
2024     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2025     vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
2026                           p_dec->fmt_out.video.i_chroma,
2027                           p_dec->fmt_out.video.i_width,
2028                           p_dec->fmt_out.video.i_height,
2029                           p_dec->fmt_out.video.i_aspect );
2030
2031     if( !p_pic->i_planes )
2032     {
2033         free( p_pic );
2034         return 0;
2035     }
2036
2037     p_pic->pf_release = video_release_buffer;
2038     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2039     p_pic->p_sys->p_owner = p_this;
2040     p_pic->i_status = RESERVED_PICTURE;
2041
2042     pp_ring[i] = p_pic;
2043
2044     return p_pic;
2045 }
2046
2047 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
2048 {
2049     return video_new_buffer( VLC_OBJECT(p_dec),
2050                              p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2051 }
2052
2053 static picture_t *video_new_buffer_filter( filter_t *p_filter )
2054 {
2055     return video_new_buffer( VLC_OBJECT(p_filter),
2056                              p_filter->p_owner->pp_pics,
2057                              p_filter->p_owner->p_sys );
2058 }
2059
2060 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2061 {
2062     if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2063     if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
2064     if( p_pic ) free( p_pic );
2065 }
2066
2067 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
2068 {
2069     p_pic->i_refcount = 0;
2070     p_pic->i_status = DESTROYED_PICTURE;
2071 }
2072
2073 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
2074 {
2075     p_pic->i_refcount = 0;
2076     p_pic->i_status = DESTROYED_PICTURE;
2077 }
2078
2079 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2080 {
2081     p_pic->i_refcount++;
2082 }
2083
2084 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2085 {
2086     video_release_buffer( p_pic );
2087 }
2088
2089 /*
2090  * SPU
2091  */
2092 static subpicture_t *spu_new_buffer( decoder_t * );
2093 static void spu_del_buffer( decoder_t *, subpicture_t * );
2094
2095 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2096 {
2097     sout_stream_sys_t *p_sys = p_stream->p_sys;
2098
2099     /*
2100      * Open decoder
2101      */
2102
2103     /* Initialization of decoder structures */
2104     id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2105     id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2106     id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2107     //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
2108
2109     id->p_decoder->p_module =
2110         module_Need( id->p_decoder, "decoder", "$codec", 0 );
2111
2112     if( !id->p_decoder->p_module )
2113     {
2114         msg_Err( p_stream, "cannot find decoder" );
2115         return VLC_EGENERIC;
2116     }
2117
2118     if( !p_sys->b_soverlay )
2119     {
2120         /*
2121          * Open encoder
2122          */
2123
2124         /* Initialization of encoder format structures */
2125         es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
2126                         id->p_decoder->fmt_in.i_codec );
2127
2128         id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2129
2130         id->p_encoder->p_module =
2131             module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2132
2133         if( !id->p_encoder->p_module )
2134         {
2135             module_Unneed( id->p_decoder, id->p_decoder->p_module );
2136             msg_Err( p_stream, "cannot find encoder" );
2137             return VLC_EGENERIC;
2138         }
2139     }
2140
2141     if( !p_sys->p_spu )
2142     {
2143         p_sys->p_spu = spu_Create( p_stream );
2144         spu_Init( p_sys->p_spu );
2145     }
2146
2147     return VLC_SUCCESS;
2148 }
2149
2150 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2151 {
2152     /* Close decoder */
2153     if( id->p_decoder->p_module )
2154         module_Unneed( id->p_decoder, id->p_decoder->p_module );
2155
2156     /* Close encoder */
2157     if( id->p_encoder->p_module )
2158         module_Unneed( id->p_encoder, id->p_encoder->p_module );
2159 }
2160
2161 static int transcode_spu_process( sout_stream_t *p_stream,
2162                                   sout_stream_id_t *id,
2163                                   block_t *in, block_t **out )
2164 {
2165     sout_stream_sys_t *p_sys = p_stream->p_sys;
2166     subpicture_t *p_subpic;
2167     *out = NULL;
2168
2169     p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2170     if( !p_subpic ) return VLC_EGENERIC;
2171
2172     if( p_sys->b_master_sync && p_sys->i_master_drift )
2173     {
2174         p_subpic->i_start -= p_sys->i_master_drift;
2175         if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2176     }
2177
2178     if( p_sys->b_soverlay )
2179     {
2180         spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2181     }
2182     else
2183     {
2184         block_t *p_block;
2185
2186         p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2187         spu_del_buffer( id->p_decoder, p_subpic );
2188
2189         if( p_block )
2190         {
2191             block_ChainAppend( out, p_block );
2192             return VLC_SUCCESS;
2193         }
2194     }
2195
2196     return VLC_EGENERIC;
2197 }
2198
2199 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2200 {
2201     sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2202     return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2203 }
2204
2205 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2206 {
2207     sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2208     spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );
2209 }