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