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