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