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