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