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