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