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