1 /*****************************************************************************
2 * transcode.c: transcoding stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc/input.h>
37 #include <vlc/decoder.h>
38 #include "vlc_filter.h"
41 #define MASTER_SYNC_MAX_DRIFT 100000
43 /*****************************************************************************
45 *****************************************************************************/
46 #define VENC_TEXT N_("Video encoder")
47 #define VENC_LONGTEXT N_( \
48 "Allows you to specify the video encoder to use and its associated " \
50 #define VCODEC_TEXT N_("Destination video codec")
51 #define VCODEC_LONGTEXT N_( \
52 "Allows you to specify the destination video codec used for the " \
54 #define VB_TEXT N_("Video bitrate")
55 #define VB_LONGTEXT N_( \
56 "Allows you to specify the video bitrate used for the streaming " \
58 #define SCALE_TEXT N_("Video scaling")
59 #define SCALE_LONGTEXT N_( \
60 "Allows you to scale the video before encoding." )
61 #define FPS_TEXT N_("Video frame-rate")
62 #define FPS_LONGTEXT N_( \
63 "Allows you to specify an output frame rate for the video." )
64 #define DEINTERLACE_TEXT N_("Deinterlace video")
65 #define DEINTERLACE_LONGTEXT N_( \
66 "Allows you to deinterlace the video before encoding." )
67 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
68 #define DEINTERLACE_MODULE_LONGTEXT N_( \
69 "Specifies the deinterlace module to use." )
70 #define WIDTH_TEXT N_("Video width")
71 #define WIDTH_LONGTEXT N_( \
72 "Allows you to specify the output video width." )
73 #define HEIGHT_TEXT N_("Video height")
74 #define HEIGHT_LONGTEXT N_( \
75 "Allows you to specify the output video height." )
76 #define MAXWIDTH_TEXT N_("Maximum video width")
77 #define MAXWIDTH_LONGTEXT N_( \
78 "Allows you to specify a maximum output video width." )
79 #define MAXHEIGHT_TEXT N_("Maximum video height")
80 #define MAXHEIGHT_LONGTEXT N_( \
81 "Allows you to specify a maximum output video height." )
82 #define VFILTER_TEXT N_("Video filter")
83 #define VFILTER_LONGTEXT N_( \
84 "Allows you to specify video filters used after the video " \
85 "transcoding and subpictures overlaying." )
87 #define CROPTOP_TEXT N_("Video crop top")
88 #define CROPTOP_LONGTEXT N_( \
89 "Allows you to specify the top coordinate for the video cropping." )
90 #define CROPLEFT_TEXT N_("Video crop left")
91 #define CROPLEFT_LONGTEXT N_( \
92 "Allows you to specify the left coordinate for the video cropping." )
93 #define CROPBOTTOM_TEXT N_("Video crop bottom")
94 #define CROPBOTTOM_LONGTEXT N_( \
95 "Allows you to specify the bottom coordinate for the video cropping." )
96 #define CROPRIGHT_TEXT N_("Video crop right")
97 #define CROPRIGHT_LONGTEXT N_( \
98 "Allows you to specify the right coordinate for the video cropping." )
100 #define AENC_TEXT N_("Audio encoder")
101 #define AENC_LONGTEXT N_( \
102 "Allows you to specify the audio encoder to use and its associated " \
104 #define ACODEC_TEXT N_("Destination audio codec")
105 #define ACODEC_LONGTEXT N_( \
106 "Allows you to specify the destination audio codec used for the " \
107 "streaming output." )
108 #define AB_TEXT N_("Audio bitrate")
109 #define AB_LONGTEXT N_( \
110 "Allows you to specify the audio bitrate used for the streaming " \
112 #define ARATE_TEXT N_("Audio sample rate")
113 #define ARATE_LONGTEXT N_( \
114 "Allows you to specify the audio sample rate used for the streaming " \
116 #define ACHANS_TEXT N_("Audio channels")
117 #define ACHANS_LONGTEXT N_( \
118 "Allows you to specify the number of audio channels used for the " \
119 "streaming output." )
121 #define SENC_TEXT N_("Subtitles encoder")
122 #define SENC_LONGTEXT N_( \
123 "Allows you to specify the subtitles encoder to use and its associated " \
125 #define SCODEC_TEXT N_("Destination subtitles codec")
126 #define SCODEC_LONGTEXT N_( \
127 "Allows you to specify the destination subtitles codec used for the " \
128 "streaming output." )
129 #define SFILTER_TEXT N_("Subpictures filter")
130 #define SFILTER_LONGTEXT N_( \
131 "Allows you to specify subpictures filters used during the video " \
132 "transcoding. The subpictures produced by the filters will be overlayed " \
133 "directly onto the video." )
135 #define OSD_TEXT N_("OSD menu")
136 #define OSD_LONGTEXT N_(\
137 "Enable streaming of the On Screen Display. It uses the osdmenu subfilter." )
139 #define THREADS_TEXT N_("Number of threads")
140 #define THREADS_LONGTEXT N_( \
141 "Allows you to specify the number of threads used for the transcoding." )
142 #define HP_TEXT N_("High priority")
143 #define HP_LONGTEXT N_( \
144 "Runs the optional encoder thread at the OUTPUT priority instead of " \
147 #define ASYNC_TEXT N_("Synchronise on audio track")
148 #define ASYNC_LONGTEXT N_( \
149 "This option will drop/duplicate video frames to synchronise the video " \
150 "track on the audio track." )
152 #define HURRYUP_TEXT N_( "Hurry up" )
153 #define HURRYUP_LONGTEXT N_( "Allows you to specify if the transcoder " \
154 "should drop frames if your CPU can't keep up with the encoding rate." )
156 static char *ppsz_deinterlace_type[] =
158 "deinterlace", "ffmpeg-deinterlace"
161 static int Open ( vlc_object_t * );
162 static void Close( vlc_object_t * );
164 #define SOUT_CFG_PREFIX "sout-transcode-"
167 set_shortname( _("Transcode"));
168 set_description( _("Transcode stream output") );
169 set_capability( "sout stream", 50 );
170 add_shortcut( "transcode" );
171 set_callbacks( Open, Close );
172 set_category( CAT_SOUT );
173 set_subcategory( SUBCAT_SOUT_STREAM );
174 set_section( N_("Video"), NULL );
175 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
176 VENC_LONGTEXT, VLC_FALSE );
177 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
178 VCODEC_LONGTEXT, VLC_FALSE );
179 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
180 VB_LONGTEXT, VLC_FALSE );
181 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
182 SCALE_LONGTEXT, VLC_FALSE );
183 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
184 FPS_LONGTEXT, VLC_FALSE );
185 add_bool( SOUT_CFG_PREFIX "hurry-up", VLC_TRUE, NULL, HURRYUP_TEXT,
186 HURRYUP_LONGTEXT, VLC_FALSE );
187 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
188 DEINTERLACE_LONGTEXT, VLC_FALSE );
189 add_string( SOUT_CFG_PREFIX "deinterlace-module", "deinterlace", NULL,
190 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
192 change_string_list( ppsz_deinterlace_type, 0, 0 );
193 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
194 WIDTH_LONGTEXT, VLC_TRUE );
195 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
196 HEIGHT_LONGTEXT, VLC_TRUE );
197 add_integer( SOUT_CFG_PREFIX "maxwidth", 0, NULL, MAXWIDTH_TEXT,
198 MAXWIDTH_LONGTEXT, VLC_TRUE );
199 add_integer( SOUT_CFG_PREFIX "maxheight", 0, NULL, MAXHEIGHT_TEXT,
200 MAXHEIGHT_LONGTEXT, VLC_TRUE );
201 add_module_list_cat( SOUT_CFG_PREFIX "vfilter", SUBCAT_VIDEO_VFILTER,
203 VFILTER_TEXT, VFILTER_LONGTEXT, VLC_FALSE );
205 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
206 CROPTOP_LONGTEXT, VLC_TRUE );
207 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
208 CROPLEFT_LONGTEXT, VLC_TRUE );
209 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
210 CROPBOTTOM_LONGTEXT, VLC_TRUE );
211 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
212 CROPRIGHT_LONGTEXT, VLC_TRUE );
214 set_section( N_("Audio"), NULL );
215 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
216 AENC_LONGTEXT, VLC_FALSE );
217 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
218 ACODEC_LONGTEXT, VLC_FALSE );
219 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
220 AB_LONGTEXT, VLC_FALSE );
221 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
222 ACHANS_LONGTEXT, VLC_FALSE );
223 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
224 ARATE_LONGTEXT, VLC_TRUE );
225 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
226 ASYNC_LONGTEXT, VLC_FALSE );
228 set_section( N_("Overlays/Subtitles"), NULL );
229 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
230 SENC_LONGTEXT, VLC_FALSE );
231 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
232 SCODEC_LONGTEXT, VLC_FALSE );
233 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
234 SCODEC_LONGTEXT, VLC_FALSE );
235 add_module_list_cat( SOUT_CFG_PREFIX "sfilter", SUBCAT_VIDEO_SUBPIC,
237 SFILTER_TEXT, SFILTER_LONGTEXT, VLC_FALSE );
239 set_section( N_("On Screen Display"), NULL );
240 add_bool( SOUT_CFG_PREFIX "osd", 0, NULL, OSD_TEXT,
241 OSD_LONGTEXT, VLC_FALSE );
243 set_section( N_("Miscellaneous"), NULL );
244 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
245 THREADS_LONGTEXT, VLC_TRUE );
246 add_bool( SOUT_CFG_PREFIX "high-priority", 0, NULL, HP_TEXT, HP_LONGTEXT,
251 static const char *ppsz_sout_options[] = {
252 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
253 "scale", "fps", "width", "height", "vfilter", "deinterlace",
254 "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab",
255 "samplerate", "channels", "senc", "scodec", "soverlay", "sfilter",
256 "osd", "audio-sync", "high-priority", "maxwidth", "maxheight", NULL
259 /*****************************************************************************
260 * Exported prototypes
261 *****************************************************************************/
262 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
263 static int Del ( sout_stream_t *, sout_stream_id_t * );
264 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
266 static int transcode_audio_new ( sout_stream_t *, sout_stream_id_t * );
267 static void transcode_audio_close ( sout_stream_t *, sout_stream_id_t * );
268 static int transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
269 block_t *, block_t ** );
271 static aout_buffer_t *audio_new_buffer( decoder_t *, int );
272 static void audio_del_buffer( decoder_t *, aout_buffer_t * );
274 static int transcode_video_new ( sout_stream_t *, sout_stream_id_t * );
275 static void transcode_video_close ( sout_stream_t *, sout_stream_id_t * );
276 static int transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
277 static int transcode_video_process( sout_stream_t *, sout_stream_id_t *,
278 block_t *, block_t ** );
280 static void video_del_buffer( vlc_object_t *, picture_t * );
281 static picture_t *video_new_buffer_decoder( decoder_t * );
282 static void video_del_buffer_decoder( decoder_t *, picture_t * );
283 static void video_link_picture_decoder( decoder_t *, picture_t * );
284 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
285 static picture_t *video_new_buffer_filter( filter_t * );
286 static void video_del_buffer_filter( filter_t *, picture_t * );
288 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
289 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
290 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
291 block_t *, block_t ** );
293 static int transcode_osd_new ( sout_stream_t *, sout_stream_id_t * );
294 static void transcode_osd_close ( sout_stream_t *, sout_stream_id_t * );
295 static int transcode_osd_process( sout_stream_t *, sout_stream_id_t *,
296 block_t *, block_t ** );
298 static int EncoderThread( struct sout_stream_sys_t * p_sys );
300 static int pi_channels_maps[6] =
303 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
304 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
305 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
306 | AOUT_CHAN_REARRIGHT,
307 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
308 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
311 #define PICTURE_RING_SIZE 64
312 #define SUBPICTURE_RING_SIZE 20
314 struct sout_stream_sys_t
318 sout_stream_t *p_out;
319 sout_stream_id_t *id_video;
321 vlc_mutex_t lock_out;
323 picture_t * pp_pics[PICTURE_RING_SIZE];
324 int i_first_pic, i_last_pic;
327 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
329 sout_cfg_t *p_audio_cfg;
335 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
337 sout_cfg_t *p_video_cfg;
341 unsigned int i_width, i_maxwidth;
342 unsigned int i_height, i_maxheight;
343 vlc_bool_t b_deinterlace;
344 char *psz_deinterlace;
345 sout_cfg_t *p_deinterlace_cfg;
347 vlc_bool_t b_high_priority;
348 vlc_bool_t b_hurry_up;
349 char *psz_vfilters[10];
350 sout_cfg_t *p_vfilters_cfg[10];
359 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
361 vlc_bool_t b_soverlay;
362 sout_cfg_t *p_spu_cfg;
366 sout_stream_id_t *id_osd; /* extension for streaming OSD menus */
367 vlc_fourcc_t i_osdcodec; /* codec osd menu (0 if not transcode) */
369 sout_cfg_t *p_osd_cfg;
370 vlc_bool_t b_es_osd; /* VLC_TRUE when osd es is registered */
371 vlc_bool_t b_sout_osd;
374 vlc_bool_t b_master_sync;
375 mtime_t i_master_drift;
378 struct decoder_owner_sys_t
380 picture_t *pp_pics[PICTURE_RING_SIZE];
381 sout_stream_sys_t *p_sys;
383 struct filter_owner_sys_t
385 picture_t *pp_pics[PICTURE_RING_SIZE];
386 sout_stream_sys_t *p_sys;
389 /*****************************************************************************
391 *****************************************************************************/
392 static int Open( vlc_object_t *p_this )
394 sout_stream_t *p_stream = (sout_stream_t*)p_this;
395 sout_stream_sys_t *p_sys;
398 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
400 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
403 msg_Err( p_stream, "cannot create chain" );
404 vlc_object_destroy( p_sys );
408 p_sys->i_master_drift = 0;
410 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
413 /* Audio transcoding parameters */
414 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
415 p_sys->psz_aenc = NULL;
416 p_sys->p_audio_cfg = NULL;
417 if( val.psz_string && *val.psz_string )
420 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
422 if( psz_next ) free( psz_next );
424 if( val.psz_string ) free( val.psz_string );
426 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
428 if( val.psz_string && *val.psz_string )
431 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
432 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
434 if( val.psz_string ) free( val.psz_string );
436 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
437 p_sys->i_abitrate = val.i_int;
438 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
440 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
441 p_sys->i_sample_rate = val.i_int;
443 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
444 p_sys->i_channels = val.i_int;
446 if( p_sys->i_acodec )
448 if( (strncmp( (char *)&p_sys->i_acodec, "mp3", 3) == 0) &&
449 (p_sys->i_channels > 2) )
451 msg_Warn( p_stream, "%d channels invalid for mp3, forcing to 2",
453 p_sys->i_channels = 2;
455 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
456 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
457 p_sys->i_channels, p_sys->i_abitrate / 1000 );
460 /* Video transcoding parameters */
461 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
462 p_sys->psz_venc = NULL;
463 p_sys->p_video_cfg = NULL;
464 if( val.psz_string && *val.psz_string )
467 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
469 if( psz_next ) free( psz_next );
471 if( val.psz_string ) free( val.psz_string );
473 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
475 if( val.psz_string && *val.psz_string )
478 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
479 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
481 if( val.psz_string ) free( val.psz_string );
483 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
484 p_sys->i_vbitrate = val.i_int;
485 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
487 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
488 p_sys->f_scale = val.f_float;
490 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
491 p_sys->f_fps = val.f_float;
493 var_Get( p_stream, SOUT_CFG_PREFIX "hurry-up", &val );
494 p_sys->b_hurry_up = val.b_bool;
496 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
497 p_sys->i_width = val.i_int;
499 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
500 p_sys->i_height = val.i_int;
502 var_Get( p_stream, SOUT_CFG_PREFIX "maxwidth", &val );
503 p_sys->i_maxwidth = val.i_int;
505 var_Get( p_stream, SOUT_CFG_PREFIX "maxheight", &val );
506 p_sys->i_maxheight = val.i_int;
508 var_Get( p_stream, SOUT_CFG_PREFIX "vfilter", &val );
509 p_sys->i_vfilters = 0;
510 if( val.psz_string && *val.psz_string )
512 char *psz_parser = val.psz_string;
514 while( psz_parser != NULL && *psz_parser != '\0' )
516 psz_parser = sout_CfgCreate(
517 &p_sys->psz_vfilters[p_sys->i_vfilters],
518 &p_sys->p_vfilters_cfg[p_sys->i_vfilters],
521 if( psz_parser != NULL && *psz_parser != '\0' ) psz_parser++;
524 if( val.psz_string ) free( val.psz_string );
525 p_sys->psz_vfilters[p_sys->i_vfilters] = NULL;
526 p_sys->p_vfilters_cfg[p_sys->i_vfilters] = NULL;
528 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
529 p_sys->b_deinterlace = val.b_bool;
531 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace-module", &val );
532 p_sys->psz_deinterlace = NULL;
533 p_sys->p_deinterlace_cfg = NULL;
534 if( val.psz_string && *val.psz_string )
537 psz_next = sout_CfgCreate( &p_sys->psz_deinterlace,
538 &p_sys->p_deinterlace_cfg,
540 if( psz_next ) free( psz_next );
542 if( val.psz_string ) free( val.psz_string );
544 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
545 p_sys->i_crop_top = val.i_int;
546 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
547 p_sys->i_crop_bottom = val.i_int;
548 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
549 p_sys->i_crop_left = val.i_int;
550 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
551 p_sys->i_crop_right = val.i_int;
553 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
554 p_sys->i_threads = val.i_int;
555 var_Get( p_stream, SOUT_CFG_PREFIX "high-priority", &val );
556 p_sys->b_high_priority = val.b_bool;
558 if( p_sys->i_vcodec )
560 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
561 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
562 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
565 /* Subpictures transcoding parameters */
567 p_sys->psz_senc = NULL;
568 p_sys->p_spu_cfg = NULL;
571 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
572 if( val.psz_string && *val.psz_string )
575 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
577 if( psz_next ) free( psz_next );
579 if( val.psz_string ) free( val.psz_string );
581 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
582 if( val.psz_string && *val.psz_string )
585 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
586 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
588 if( val.psz_string ) free( val.psz_string );
590 if( p_sys->i_scodec )
592 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
595 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
596 p_sys->b_soverlay = val.b_bool;
598 var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
599 if( val.psz_string && *val.psz_string )
601 p_sys->p_spu = spu_Create( p_stream );
602 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
603 var_Set( p_sys->p_spu, "sub-filter", val );
604 spu_Init( p_sys->p_spu );
606 if( val.psz_string ) free( val.psz_string );
608 /* OSD menu transcoding parameters */
609 p_sys->psz_osdenc = NULL;
610 p_sys->p_osd_cfg = NULL;
611 p_sys->i_osdcodec = 0;
612 p_sys->b_es_osd = VLC_FALSE;
614 var_Get( p_stream, SOUT_CFG_PREFIX "osd", &val );
615 p_sys->b_sout_osd = val.b_bool;
616 if( p_sys->b_sout_osd )
621 psz_next = sout_CfgCreate( &p_sys->psz_osdenc,
622 &p_sys->p_osd_cfg, strdup( "dvbsub") );
623 if( psz_next ) free( psz_next );
625 p_sys->i_osdcodec = VLC_FOURCC('Y','U','V','P' );
627 msg_Dbg( p_stream, "codec osd=%4.4s", (char *)&p_sys->i_osdcodec );
631 osd_val.psz_string = strdup("osdmenu");
632 p_sys->p_spu = spu_Create( p_stream );
633 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
634 var_Set( p_sys->p_spu, "sub-filter", osd_val );
635 spu_Init( p_sys->p_spu );
636 if( osd_val.psz_string ) free( osd_val.psz_string );
640 osd_val.psz_string = strdup("osdmenu");
641 var_Set( p_sys->p_spu, "sub-filter", osd_val );
642 if( osd_val.psz_string ) free( osd_val.psz_string );
647 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
648 p_sys->b_master_sync = val.b_bool;
649 if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
651 p_stream->pf_add = Add;
652 p_stream->pf_del = Del;
653 p_stream->pf_send = Send;
654 p_stream->p_sys = p_sys;
659 /*****************************************************************************
661 *****************************************************************************/
662 static void Close( vlc_object_t * p_this )
664 sout_stream_t *p_stream = (sout_stream_t*)p_this;
665 sout_stream_sys_t *p_sys = p_stream->p_sys;
667 sout_StreamDelete( p_sys->p_out );
669 while( p_sys->p_audio_cfg != NULL )
671 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
673 if( p_sys->p_audio_cfg->psz_name )
674 free( p_sys->p_audio_cfg->psz_name );
675 if( p_sys->p_audio_cfg->psz_value )
676 free( p_sys->p_audio_cfg->psz_value );
677 free( p_sys->p_audio_cfg );
679 p_sys->p_audio_cfg = p_next;
681 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
683 while( p_sys->p_video_cfg != NULL )
685 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
687 if( p_sys->p_video_cfg->psz_name )
688 free( p_sys->p_video_cfg->psz_name );
689 if( p_sys->p_video_cfg->psz_value )
690 free( p_sys->p_video_cfg->psz_value );
691 free( p_sys->p_video_cfg );
693 p_sys->p_video_cfg = p_next;
695 if( p_sys->psz_venc ) free( p_sys->psz_venc );
697 while( p_sys->p_deinterlace_cfg != NULL )
699 sout_cfg_t *p_next = p_sys->p_deinterlace_cfg->p_next;
701 if( p_sys->p_deinterlace_cfg->psz_name )
702 free( p_sys->p_deinterlace_cfg->psz_name );
703 if( p_sys->p_deinterlace_cfg->psz_value )
704 free( p_sys->p_deinterlace_cfg->psz_value );
705 free( p_sys->p_deinterlace_cfg );
707 p_sys->p_deinterlace_cfg = p_next;
709 if( p_sys->psz_deinterlace ) free( p_sys->psz_deinterlace );
711 while( p_sys->p_spu_cfg != NULL )
713 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
715 if( p_sys->p_spu_cfg->psz_name )
716 free( p_sys->p_spu_cfg->psz_name );
717 if( p_sys->p_spu_cfg->psz_value )
718 free( p_sys->p_spu_cfg->psz_value );
719 free( p_sys->p_spu_cfg );
721 p_sys->p_spu_cfg = p_next;
723 if( p_sys->psz_senc ) free( p_sys->psz_senc );
725 if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
727 while( p_sys->p_osd_cfg != NULL )
729 sout_cfg_t *p_next = p_sys->p_osd_cfg->p_next;
731 if( p_sys->p_osd_cfg->psz_name )
732 free( p_sys->p_osd_cfg->psz_name );
733 if( p_sys->p_osd_cfg->psz_value )
734 free( p_sys->p_osd_cfg->psz_value );
735 free( p_sys->p_osd_cfg );
737 p_sys->p_osd_cfg = p_next;
739 if( p_sys->psz_osdenc ) free( p_sys->psz_osdenc );
741 vlc_object_destroy( p_sys );
744 struct sout_stream_id_t
746 vlc_fourcc_t b_transcode;
748 /* id of the out stream */
752 decoder_t *p_decoder;
755 filter_t *pp_filter[10];
757 filter_t *pp_vfilter[10];
761 encoder_t *p_encoder;
764 date_t interpolated_pts;
767 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
769 sout_stream_sys_t *p_sys = p_stream->p_sys;
770 sout_stream_id_t *id;
772 id = malloc( sizeof( sout_stream_id_t ) );
773 memset( id, 0, sizeof(sout_stream_id_t) );
776 id->p_decoder = NULL;
777 id->p_encoder = NULL;
779 /* Create decoder object */
780 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
783 msg_Err( p_stream, "out of memory" );
786 vlc_object_attach( id->p_decoder, p_stream );
787 id->p_decoder->p_module = NULL;
788 id->p_decoder->fmt_in = *p_fmt;
789 id->p_decoder->b_pace_control = VLC_TRUE;
791 /* Create encoder object */
792 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
795 msg_Err( p_stream, "out of memory" );
798 vlc_object_attach( id->p_encoder, p_stream );
799 id->p_encoder->p_module = NULL;
801 /* Create destination format */
802 es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
803 id->p_encoder->fmt_out.i_id = p_fmt->i_id;
804 id->p_encoder->fmt_out.i_group = p_fmt->i_group;
805 if( p_fmt->psz_language )
806 id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
808 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
811 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
812 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
814 /* Complete destination format */
815 id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
816 id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
817 p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
818 id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
819 id->p_encoder->fmt_out.audio.i_bitspersample =
820 p_fmt->audio.i_bitspersample;
821 id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
822 p_sys->i_channels : p_fmt->audio.i_channels;
823 /* Sanity check for audio channels */
824 id->p_encoder->fmt_out.audio.i_channels =
825 __MIN( id->p_encoder->fmt_out.audio.i_channels,
826 id->p_decoder->fmt_in.audio.i_channels );
827 id->p_encoder->fmt_out.audio.i_original_channels =
828 id->p_decoder->fmt_in.audio.i_physical_channels;
829 if( id->p_decoder->fmt_in.audio.i_channels ==
830 id->p_encoder->fmt_out.audio.i_channels )
832 id->p_encoder->fmt_out.audio.i_physical_channels =
833 id->p_decoder->fmt_in.audio.i_physical_channels;
837 id->p_encoder->fmt_out.audio.i_physical_channels =
838 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
841 /* Build decoder -> filter -> encoder chain */
842 if( transcode_audio_new( p_stream, id ) )
844 msg_Err( p_stream, "cannot create audio chain" );
848 /* Open output stream */
849 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
850 id->b_transcode = VLC_TRUE;
852 if( !id->id ) goto error;
854 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
856 else if( p_fmt->i_cat == VIDEO_ES &&
857 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
860 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
861 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
863 /* Complete destination format */
864 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
865 id->p_encoder->fmt_out.video.i_width = p_sys->i_width;
866 id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
867 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
869 /* Build decoder -> filter -> encoder chain */
870 if( transcode_video_new( p_stream, id ) )
872 msg_Err( p_stream, "cannot create video chain" );
876 /* Stream will be added later on because we don't know
877 * all the characteristics of the decoded stream yet */
878 id->b_transcode = VLC_TRUE;
880 if( p_sys->f_fps > 0 )
882 id->p_encoder->fmt_out.video.i_frame_rate =
883 (p_sys->f_fps * 1001) + 0.5;
884 id->p_encoder->fmt_out.video.i_frame_rate_base = 1001;
887 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
889 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
890 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
891 (char*)&p_sys->i_scodec );
893 /* Complete destination format */
894 id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
896 /* build decoder -> filter -> encoder */
897 if( transcode_spu_new( p_stream, id ) )
899 msg_Err( p_stream, "cannot create subtitles chain" );
903 /* open output stream */
904 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
905 id->b_transcode = VLC_TRUE;
907 if( !id->id ) goto error;
909 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
911 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
912 (char*)&p_fmt->i_codec );
914 id->b_transcode = VLC_TRUE;
916 /* Build decoder -> filter -> overlaying chain */
917 if( transcode_spu_new( p_stream, id ) )
919 msg_Err( p_stream, "cannot create subtitles chain" );
925 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
926 (char*)&p_fmt->i_codec );
927 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
928 id->b_transcode = VLC_FALSE;
930 if( !id->id ) goto error;
933 if( p_sys->b_sout_osd )
935 /* Create a fake OSD menu elementary stream */
936 if( !p_sys->b_es_osd && (p_sys->i_osdcodec != 0 || p_sys->psz_osdenc) )
938 if( transcode_osd_new( p_stream, p_sys->id_osd ) )
940 msg_Err( p_stream, "cannot create osd chain" );
943 p_sys->b_es_osd = VLC_TRUE;
951 vlc_object_detach( id->p_decoder );
952 vlc_object_destroy( id->p_decoder );
957 vlc_object_detach( id->p_encoder );
958 vlc_object_destroy( id->p_encoder );
965 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
967 sout_stream_sys_t *p_sys = p_stream->p_sys;
969 if( p_sys->b_es_osd )
970 transcode_osd_close( p_stream, p_sys->id_osd );
972 if( id->b_transcode )
974 switch( id->p_decoder->fmt_in.i_cat )
977 transcode_audio_close( p_stream, id );
980 transcode_video_close( p_stream, id );
983 transcode_spu_close( p_stream, id );
988 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
992 vlc_object_detach( id->p_decoder );
993 vlc_object_destroy( id->p_decoder );
998 vlc_object_detach( id->p_encoder );
999 vlc_object_destroy( id->p_encoder );
1007 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1010 sout_stream_sys_t *p_sys = p_stream->p_sys;
1011 block_t *p_out = NULL;
1013 if( !id->b_transcode && id->id )
1015 /* Transcode OSD menu pictures. */
1016 if( p_sys->b_es_osd )
1018 transcode_osd_process( p_stream, id, p_buffer, &p_out );
1020 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
1022 else if( !id->b_transcode )
1024 block_Release( p_buffer );
1025 return VLC_EGENERIC;
1028 switch( id->p_decoder->fmt_in.i_cat )
1031 transcode_audio_process( p_stream, id, p_buffer, &p_out );
1035 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
1038 return VLC_EGENERIC;
1043 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
1046 return VLC_EGENERIC;
1052 block_Release( p_buffer );
1056 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
1060 /****************************************************************************
1061 * decoder reencoder part
1062 ****************************************************************************/
1063 int audio_BitsPerSample( vlc_fourcc_t i_format )
1067 case VLC_FOURCC('u','8',' ',' '):
1068 case VLC_FOURCC('s','8',' ',' '):
1071 case VLC_FOURCC('u','1','6','l'):
1072 case VLC_FOURCC('s','1','6','l'):
1073 case VLC_FOURCC('u','1','6','b'):
1074 case VLC_FOURCC('s','1','6','b'):
1077 case VLC_FOURCC('u','2','4','l'):
1078 case VLC_FOURCC('s','2','4','l'):
1079 case VLC_FOURCC('u','2','4','b'):
1080 case VLC_FOURCC('s','2','4','b'):
1083 case VLC_FOURCC('u','3','2','l'):
1084 case VLC_FOURCC('s','3','2','l'):
1085 case VLC_FOURCC('u','3','2','b'):
1086 case VLC_FOURCC('s','3','2','b'):
1087 case VLC_FOURCC('f','l','3','2'):
1088 case VLC_FOURCC('f','i','3','2'):
1091 case VLC_FOURCC('f','l','6','4'):
1098 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
1099 sout_stream_id_t *id,
1100 es_format_t *p_fmt_in,
1101 es_format_t *p_fmt_out )
1103 filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1105 vlc_object_attach( p_filter, p_stream );
1106 p_filter->pf_audio_buffer_new = (block_t* (*) (filter_t*, int))__block_New;
1108 p_filter->fmt_in = *p_fmt_in;
1109 p_filter->fmt_out = *p_fmt_out;
1111 p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
1112 if( p_filter->p_module )
1114 p_filter->fmt_out.audio.i_bitspersample =
1115 audio_BitsPerSample( p_filter->fmt_out.i_codec );
1116 *p_fmt_in = p_filter->fmt_out;
1120 vlc_object_detach( p_filter );
1121 vlc_object_destroy( p_filter );
1128 static int transcode_audio_new( sout_stream_t *p_stream,
1129 sout_stream_id_t *id )
1131 sout_stream_sys_t *p_sys = p_stream->p_sys;
1132 es_format_t fmt_last;
1139 /* Initialization of decoder structures */
1140 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1141 id->p_decoder->fmt_out.i_extra = 0;
1142 id->p_decoder->fmt_out.p_extra = 0;
1143 id->p_decoder->pf_decode_audio = 0;
1144 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
1145 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1146 /* id->p_decoder->p_cfg = p_sys->p_audio_cfg; */
1148 id->p_decoder->p_module =
1149 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1151 if( !id->p_decoder->p_module )
1153 msg_Err( p_stream, "cannot find decoder" );
1154 return VLC_EGENERIC;
1156 id->p_decoder->fmt_out.audio.i_bitspersample =
1157 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1158 fmt_last = id->p_decoder->fmt_out;
1159 /* FIX decoders so we don't have to do this */
1160 fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1166 /* Initialization of encoder format structures */
1167 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1168 id->p_decoder->fmt_out.i_codec );
1169 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
1171 /* Initialization of encoder format structures */
1172 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1173 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1174 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
1175 id->p_encoder->fmt_in.audio.i_physical_channels =
1176 id->p_encoder->fmt_out.audio.i_physical_channels;
1177 id->p_encoder->fmt_in.audio.i_original_channels =
1178 id->p_encoder->fmt_out.audio.i_original_channels;
1179 id->p_encoder->fmt_in.audio.i_channels =
1180 id->p_encoder->fmt_out.audio.i_channels;
1181 id->p_encoder->fmt_in.audio.i_bitspersample =
1182 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1184 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1186 id->p_encoder->p_module =
1187 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
1188 if( !id->p_encoder->p_module )
1190 msg_Err( p_stream, "cannot find encoder" );
1191 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1192 id->p_decoder->p_module = 0;
1193 return VLC_EGENERIC;
1195 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1196 id->p_encoder->fmt_in.audio.i_bitspersample =
1197 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1199 /* Load conversion filters */
1200 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1201 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1203 /* We'll have to go through fl32 first */
1204 es_format_t fmt_out = id->p_encoder->fmt_in;
1205 fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1207 id->pp_filter[id->i_filter] =
1208 transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );
1210 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1215 if( fmt_last.audio.i_channels !=
1216 id->p_encoder->fmt_in.audio.i_channels ||
1217 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
1218 fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1220 id->pp_filter[id->i_filter] =
1221 transcode_audio_filter_new( p_stream, id, &fmt_last,
1222 &id->p_encoder->fmt_in );
1224 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1229 /* Final checks to see if conversions were successful */
1230 if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1232 msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1233 (char *)&fmt_last.i_codec,
1234 (char *)&id->p_encoder->fmt_in.i_codec );
1235 transcode_audio_close( p_stream, id );
1236 return VLC_EGENERIC;
1239 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1241 msg_Err( p_stream, "no audio filter found for mixing from"
1242 " %i to %i channels", fmt_last.audio.i_channels,
1243 id->p_encoder->fmt_in.audio.i_channels );
1245 /* FIXME : this might work, but only if the encoder is restarted */
1246 id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1247 id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1249 id->p_encoder->fmt_in.audio.i_physical_channels =
1250 id->p_encoder->fmt_in.audio.i_original_channels =
1251 fmt_last.audio.i_physical_channels;
1252 id->p_encoder->fmt_out.audio.i_physical_channels =
1253 id->p_encoder->fmt_out.audio.i_original_channels =
1254 fmt_last.audio.i_physical_channels;
1256 transcode_audio_close( p_stream, id );
1257 return VLC_EGENERIC;
1261 if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1263 msg_Err( p_stream, "no audio filter found for resampling from"
1264 " %iHz to %iHz", fmt_last.audio.i_rate,
1265 id->p_encoder->fmt_in.audio.i_rate );
1267 /* FIXME : this might work, but only if the encoder is restarted */
1268 id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1269 id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1271 transcode_audio_close( p_stream, id );
1272 return VLC_EGENERIC;
1276 /* FIXME: Hack for mp3 transcoding support */
1277 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1278 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1283 static void transcode_audio_close( sout_stream_t *p_stream,
1284 sout_stream_id_t *id )
1289 if( id->p_decoder->p_module )
1290 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1291 id->p_decoder->p_module = 0;
1294 if( id->p_encoder->p_module )
1295 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1296 id->p_encoder->p_module = 0;
1299 for( i = 0; i < id->i_filter; i++ )
1301 vlc_object_detach( id->pp_filter[i] );
1302 if( id->pp_filter[i]->p_module )
1303 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1304 vlc_object_destroy( id->pp_filter[i] );
1308 static int transcode_audio_process( sout_stream_t *p_stream,
1309 sout_stream_id_t *id,
1310 block_t *in, block_t **out )
1312 sout_stream_sys_t *p_sys = p_stream->p_sys;
1313 aout_buffer_t *p_audio_buf;
1314 block_t *p_block, *p_audio_block;
1318 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1321 if( p_sys->b_master_sync )
1323 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1324 if ( p_audio_buf->start_date - i_dts > MASTER_SYNC_MAX_DRIFT
1325 || p_audio_buf->start_date - i_dts < -MASTER_SYNC_MAX_DRIFT )
1327 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1328 date_Set( &id->interpolated_pts, p_audio_buf->start_date );
1329 i_dts = p_audio_buf->start_date + 1;
1331 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1332 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1333 p_audio_buf->start_date -= p_sys->i_master_drift;
1334 p_audio_buf->end_date -= p_sys->i_master_drift;
1337 p_audio_block = p_audio_buf->p_sys;
1338 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1339 p_audio_block->i_dts = p_audio_block->i_pts =
1340 p_audio_buf->start_date;
1341 p_audio_block->i_length = p_audio_buf->end_date -
1342 p_audio_buf->start_date;
1343 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1345 /* Run filter chain */
1346 for( i = 0; i < id->i_filter; i++ )
1349 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1353 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1354 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1355 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1356 p_audio_buf->start_date = p_audio_block->i_dts;
1357 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1359 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1360 block_ChainAppend( out, p_block );
1361 block_Release( p_audio_block );
1362 free( p_audio_buf );
1368 static void audio_release_buffer( aout_buffer_t *p_buffer )
1370 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1371 if( p_buffer ) free( p_buffer );
1374 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1376 aout_buffer_t *p_buffer;
1380 if( p_dec->fmt_out.audio.i_bitspersample )
1382 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1383 p_dec->fmt_out.audio.i_channels;
1385 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1386 p_dec->fmt_out.audio.i_frame_length )
1388 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1389 p_dec->fmt_out.audio.i_frame_length;
1393 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1396 p_buffer = malloc( sizeof(aout_buffer_t) );
1397 p_buffer->pf_release = audio_release_buffer;
1398 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1400 p_buffer->p_buffer = p_block->p_buffer;
1401 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1402 p_buffer->i_nb_samples = i_samples;
1403 p_block->i_samples = i_samples;
1408 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1410 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1411 if( p_buffer ) free( p_buffer );
1417 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1419 sout_stream_sys_t *p_sys = p_stream->p_sys;
1426 /* Initialization of decoder structures */
1427 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1428 id->p_decoder->fmt_out.i_extra = 0;
1429 id->p_decoder->fmt_out.p_extra = 0;
1430 id->p_decoder->pf_decode_video = 0;
1431 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1432 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1433 id->p_decoder->pf_picture_link = video_link_picture_decoder;
1434 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
1435 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1436 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1437 id->p_decoder->p_owner->pp_pics[i] = 0;
1438 id->p_decoder->p_owner->p_sys = p_sys;
1439 /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
1441 id->p_decoder->p_module =
1442 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1444 if( !id->p_decoder->p_module )
1446 msg_Err( p_stream, "cannot find decoder" );
1447 return VLC_EGENERIC;
1452 * Because some info about the decoded input will only be available
1453 * once the first frame is decoded, we actually only test the availability
1454 * of the encoder here.
1457 /* Initialization of encoder format structures */
1458 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1459 id->p_decoder->fmt_out.i_codec );
1460 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1462 /* The dimensions will be set properly later on.
1463 * Just put sensible values so we can test an encoder is available. */
1464 id->p_encoder->fmt_in.video.i_width =
1465 id->p_encoder->fmt_out.video.i_width ?
1466 id->p_encoder->fmt_out.video.i_width :
1467 id->p_decoder->fmt_in.video.i_width ?
1468 id->p_decoder->fmt_in.video.i_width : 16;
1469 id->p_encoder->fmt_in.video.i_height =
1470 id->p_encoder->fmt_out.video.i_height ?
1471 id->p_encoder->fmt_out.video.i_height :
1472 id->p_decoder->fmt_in.video.i_height ?
1473 id->p_decoder->fmt_in.video.i_height : 16;
1474 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1475 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1477 id->p_encoder->i_threads = p_sys->i_threads;
1478 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1480 id->p_encoder->p_module =
1481 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1482 if( !id->p_encoder->p_module )
1484 msg_Err( p_stream, "cannot find encoder" );
1485 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1486 id->p_decoder->p_module = 0;
1487 return VLC_EGENERIC;
1490 /* Close the encoder.
1491 * We'll open it only when we have the first frame. */
1492 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1493 id->p_encoder->p_module = NULL;
1495 if( p_sys->i_threads >= 1 )
1497 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
1498 VLC_THREAD_PRIORITY_VIDEO;
1499 p_sys->id_video = id;
1500 vlc_mutex_init( p_stream, &p_sys->lock_out );
1501 vlc_cond_init( p_stream, &p_sys->cond );
1502 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1503 p_sys->i_first_pic = 0;
1504 p_sys->i_last_pic = 0;
1505 p_sys->p_buffers = NULL;
1506 p_sys->b_die = p_sys->b_error = 0;
1507 if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
1510 msg_Err( p_stream, "cannot spawn encoder thread" );
1511 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1512 id->p_decoder->p_module = 0;
1513 return VLC_EGENERIC;
1520 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1521 sout_stream_id_t *id )
1523 sout_stream_sys_t *p_sys = p_stream->p_sys;
1525 /* Hack because of the copy packetizer which can fail to detect the
1526 * proper size (which forces us to wait until the 1st frame
1528 int i_width = id->p_decoder->fmt_out.video.i_width -
1529 p_sys->i_crop_left - p_sys->i_crop_right;
1530 int i_height = id->p_decoder->fmt_out.video.i_height -
1531 p_sys->i_crop_top - p_sys->i_crop_bottom;
1533 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1534 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1536 /* Apply the scaling */
1537 id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1538 id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1540 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1541 id->p_encoder->fmt_out.video.i_height <= 0 )
1543 id->p_encoder->fmt_out.video.i_height =
1544 id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1546 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1547 id->p_encoder->fmt_out.video.i_height > 0 )
1549 id->p_encoder->fmt_out.video.i_width =
1550 id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1553 if( p_sys->i_maxwidth
1554 && id->p_encoder->fmt_out.video.i_width > p_sys->i_maxwidth )
1555 id->p_encoder->fmt_out.video.i_width = p_sys->i_maxwidth;
1556 if( p_sys->i_maxheight
1557 && id->p_encoder->fmt_out.video.i_height > p_sys->i_maxheight )
1558 id->p_encoder->fmt_out.video.i_height = p_sys->i_maxheight;
1560 /* Make sure the size is at least a multiple of 2 */
1561 id->p_encoder->fmt_out.video.i_width =
1562 (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
1563 id->p_encoder->fmt_out.video.i_height =
1564 (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
1566 id->p_encoder->fmt_in.video.i_width =
1567 id->p_encoder->fmt_out.video.i_width;
1568 id->p_encoder->fmt_in.video.i_height =
1569 id->p_encoder->fmt_out.video.i_height;
1571 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1572 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1574 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1575 id->p_decoder->fmt_out.video.i_frame_rate_base )
1577 id->p_encoder->fmt_out.video.i_frame_rate =
1578 id->p_decoder->fmt_out.video.i_frame_rate;
1579 id->p_encoder->fmt_out.video.i_frame_rate_base =
1580 id->p_decoder->fmt_out.video.i_frame_rate_base;
1584 /* Pick a sensible default value */
1585 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1586 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1590 id->p_encoder->fmt_in.video.i_frame_rate =
1591 id->p_encoder->fmt_out.video.i_frame_rate;
1592 id->p_encoder->fmt_in.video.i_frame_rate_base =
1593 id->p_encoder->fmt_out.video.i_frame_rate_base;
1595 date_Init( &id->interpolated_pts,
1596 id->p_encoder->fmt_out.video.i_frame_rate,
1597 id->p_encoder->fmt_out.video.i_frame_rate_base );
1599 /* Check whether a particular aspect ratio was requested */
1600 if( !id->p_encoder->fmt_out.video.i_aspect )
1602 id->p_encoder->fmt_out.video.i_aspect =
1603 id->p_decoder->fmt_out.video.i_aspect;
1605 id->p_encoder->fmt_in.video.i_aspect =
1606 id->p_encoder->fmt_out.video.i_aspect;
1608 id->p_encoder->p_module =
1609 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1610 if( !id->p_encoder->p_module )
1612 msg_Err( p_stream, "cannot find encoder" );
1613 return VLC_EGENERIC;
1616 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1618 /* Hack for mp2v/mp1v transcoding support */
1619 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1620 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1622 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1625 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1626 &id->p_encoder->fmt_out );
1629 msg_Err( p_stream, "cannot add this stream" );
1630 return VLC_EGENERIC;
1636 static void transcode_video_close( sout_stream_t *p_stream,
1637 sout_stream_id_t *id )
1641 if( p_stream->p_sys->i_threads >= 1 )
1643 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1644 p_stream->p_sys->b_die = 1;
1645 vlc_cond_signal( &p_stream->p_sys->cond );
1646 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1647 vlc_thread_join( p_stream->p_sys );
1648 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1649 vlc_cond_destroy( &p_stream->p_sys->cond );
1653 if( id->p_decoder->p_module )
1654 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1656 if( id->p_decoder->p_owner )
1658 /* Clean-up pictures ring buffer */
1659 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1661 if( id->p_decoder->p_owner->pp_pics[i] )
1662 video_del_buffer( VLC_OBJECT(id->p_decoder),
1663 id->p_decoder->p_owner->pp_pics[i] );
1665 free( id->p_decoder->p_owner );
1669 if( id->p_encoder->p_module )
1670 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1673 for( i = 0; i < id->i_filter; i++ )
1675 vlc_object_detach( id->pp_filter[i] );
1676 if( id->pp_filter[i]->p_module )
1677 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1679 /* Clean-up pictures ring buffer */
1680 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1682 if( id->pp_filter[i]->p_owner->pp_pics[j] )
1683 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1684 id->pp_filter[i]->p_owner->pp_pics[j] );
1686 free( id->pp_filter[i]->p_owner );
1688 vlc_object_destroy( id->pp_filter[i] );
1690 for( i = 0; i < id->i_vfilter; i++ )
1692 vlc_object_detach( id->pp_vfilter[i] );
1693 if( id->pp_vfilter[i]->p_module )
1694 module_Unneed( id->pp_vfilter[i], id->pp_vfilter[i]->p_module );
1696 /* Clean-up pictures ring buffer */
1697 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1699 if( id->pp_vfilter[i]->p_owner->pp_pics[j] )
1700 video_del_buffer( VLC_OBJECT(id->pp_vfilter[i]),
1701 id->pp_vfilter[i]->p_owner->pp_pics[j] );
1703 free( id->pp_vfilter[i]->p_owner );
1705 vlc_object_destroy( id->pp_vfilter[i] );
1709 static int transcode_video_process( sout_stream_t *p_stream,
1710 sout_stream_id_t *id,
1711 block_t *in, block_t **out )
1713 sout_stream_sys_t *p_sys = p_stream->p_sys;
1714 int i_duplicate = 1, i;
1715 picture_t *p_pic, *p_pic2 = NULL;
1718 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1720 subpicture_t *p_subpic = 0;
1722 if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
1724 mtime_t current_date = mdate();
1725 if( current_date + 50000 > p_pic->date )
1727 msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
1728 current_date + 50000 - p_pic->date );
1729 p_pic->pf_release( p_pic );
1734 if( p_sys->b_master_sync )
1736 mtime_t i_video_drift;
1737 mtime_t i_master_drift = p_sys->i_master_drift;
1740 i_pts = date_Get( &id->interpolated_pts ) + 1;
1741 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
1742 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
1744 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1745 date_Set( &id->interpolated_pts, p_pic->date );
1746 i_pts = p_pic->date + 1;
1748 i_video_drift = p_pic->date - i_pts;
1751 /* Set the pts of the frame being encoded */
1752 p_pic->date = i_pts;
1754 if( i_video_drift < i_master_drift - 50000 )
1757 msg_Dbg( p_stream, "dropping frame (%i)",
1758 (int)(i_video_drift - i_master_drift) );
1760 p_pic->pf_release( p_pic );
1763 else if( i_video_drift > i_master_drift + 50000 )
1766 msg_Dbg( p_stream, "adding frame (%i)",
1767 (int)(i_video_drift - i_master_drift) );
1773 if( !id->p_encoder->p_module )
1775 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1777 p_pic->pf_release( p_pic );
1778 transcode_video_close( p_stream, id );
1779 id->b_transcode = VLC_FALSE;
1780 return VLC_EGENERIC;
1784 if( p_stream->p_sys->b_deinterlace )
1786 id->pp_filter[id->i_filter] =
1787 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1788 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1790 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1791 video_new_buffer_filter;
1792 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1793 video_del_buffer_filter;
1795 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1796 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
1797 id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
1798 id->pp_filter[id->i_filter]->p_module =
1799 module_Need( id->pp_filter[id->i_filter],
1800 "video filter2", p_sys->psz_deinterlace, 0 );
1801 if( id->pp_filter[id->i_filter]->p_module )
1803 id->pp_filter[id->i_filter]->p_owner =
1804 malloc( sizeof(filter_owner_sys_t) );
1805 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1806 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1807 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1813 msg_Dbg( p_stream, "no video filter found" );
1814 vlc_object_detach( id->pp_filter[id->i_filter] );
1815 vlc_object_destroy( id->pp_filter[id->i_filter] );
1819 /* Check if we need a filter for chroma conversion or resizing */
1820 if( id->p_decoder->fmt_out.video.i_chroma !=
1821 id->p_encoder->fmt_in.video.i_chroma ||
1822 id->p_decoder->fmt_out.video.i_width !=
1823 id->p_encoder->fmt_out.video.i_width ||
1824 id->p_decoder->fmt_out.video.i_height !=
1825 id->p_encoder->fmt_out.video.i_height ||
1826 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1827 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1829 id->pp_filter[id->i_filter] =
1830 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1831 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1833 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1834 video_new_buffer_filter;
1835 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1836 video_del_buffer_filter;
1838 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1839 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1840 id->pp_filter[id->i_filter]->p_cfg = NULL;
1841 id->pp_filter[id->i_filter]->p_module =
1842 module_Need( id->pp_filter[id->i_filter],
1843 "video filter2", 0, 0 );
1844 if( id->pp_filter[id->i_filter]->p_module )
1846 id->pp_filter[id->i_filter]->p_owner =
1847 malloc( sizeof(filter_owner_sys_t) );
1848 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1849 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1850 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1856 msg_Dbg( p_stream, "no video filter found" );
1857 vlc_object_detach( id->pp_filter[id->i_filter] );
1858 vlc_object_destroy( id->pp_filter[id->i_filter] );
1860 p_pic->pf_release( p_pic );
1861 transcode_video_close( p_stream, id );
1862 id->b_transcode = VLC_FALSE;
1863 return VLC_EGENERIC;
1867 for( i = 0; i < p_sys->i_vfilters; i++ )
1869 id->pp_vfilter[id->i_vfilter] =
1870 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1871 vlc_object_attach( id->pp_vfilter[id->i_vfilter], p_stream );
1873 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_new =
1874 video_new_buffer_filter;
1875 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_del =
1876 video_del_buffer_filter;
1878 id->pp_vfilter[id->i_vfilter]->fmt_in = id->p_encoder->fmt_in;
1879 id->pp_vfilter[id->i_vfilter]->fmt_out = id->p_encoder->fmt_in;
1880 id->pp_vfilter[id->i_vfilter]->p_cfg = p_sys->p_vfilters_cfg[i];
1881 id->pp_vfilter[id->i_vfilter]->p_module =
1882 module_Need( id->pp_vfilter[id->i_vfilter],
1883 "video filter2", p_sys->psz_vfilters[i], 0 );
1884 if( id->pp_vfilter[id->i_vfilter]->p_module )
1886 id->pp_vfilter[id->i_vfilter]->p_owner =
1887 malloc( sizeof(filter_owner_sys_t) );
1888 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1889 id->pp_vfilter[id->i_vfilter]->p_owner->pp_pics[i] = 0;
1890 id->pp_vfilter[id->i_vfilter]->p_owner->p_sys = p_sys;
1896 msg_Dbg( p_stream, "no video filter found" );
1897 vlc_object_detach( id->pp_vfilter[id->i_vfilter] );
1898 vlc_object_destroy( id->pp_vfilter[id->i_vfilter] );
1903 /* Run filter chain */
1904 for( i = 0; i < id->i_filter; i++ )
1906 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1913 /* Check if we have a subpicture to overlay */
1916 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
1917 /* TODO: get another pic */
1920 /* Overlay subpicture */
1923 int i_scale_width, i_scale_height;
1924 video_format_t *p_fmt;
1926 i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
1927 id->p_decoder->fmt_out.video.i_width;
1928 i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
1929 id->p_decoder->fmt_out.video.i_height;
1931 if( p_pic->i_refcount && !id->i_filter )
1933 /* We can't modify the picture, we need to duplicate it */
1934 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1937 vout_CopyPicture( p_stream, p_tmp, p_pic );
1938 p_pic->pf_release( p_pic );
1944 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
1946 p_fmt = &id->p_decoder->fmt_out.video;
1948 /* FIXME (shouldn't have to be done here) */
1949 p_fmt->i_sar_num = p_fmt->i_aspect *
1950 p_fmt->i_height / p_fmt->i_width;
1951 p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
1953 spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
1954 i_scale_width, i_scale_height );
1957 /* Run vfilter chain */
1958 for( i = 0; i < id->i_vfilter; i++ )
1960 p_pic = id->pp_vfilter[i]->pf_video_filter(id->pp_vfilter[i], p_pic);
1963 if( p_sys->i_threads == 0 )
1966 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1967 block_ChainAppend( out, p_block );
1970 if( p_sys->b_master_sync )
1972 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1973 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
1974 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
1976 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1977 date_Set( &id->interpolated_pts, p_pic->date );
1978 i_pts = p_pic->date + 1;
1980 date_Increment( &id->interpolated_pts, 1 );
1983 if( p_sys->b_master_sync && i_duplicate > 1 )
1985 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1986 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
1987 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
1989 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1990 date_Set( &id->interpolated_pts, p_pic->date );
1991 i_pts = p_pic->date + 1;
1993 date_Increment( &id->interpolated_pts, 1 );
1995 if( p_sys->i_threads >= 1 )
1997 /* We can't modify the picture, we need to duplicate it */
1998 p_pic2 = video_new_buffer_decoder( id->p_decoder );
1999 if( p_pic2 != NULL )
2001 vout_CopyPicture( p_stream, p_pic2, p_pic );
2002 p_pic2->date = i_pts;
2008 p_pic->date = i_pts;
2009 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
2010 block_ChainAppend( out, p_block );
2014 if( p_sys->i_threads == 0 )
2016 p_pic->pf_release( p_pic );
2020 vlc_mutex_lock( &p_sys->lock_out );
2021 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
2022 p_sys->i_last_pic %= PICTURE_RING_SIZE;
2023 *out = p_sys->p_buffers;
2024 p_sys->p_buffers = NULL;
2025 if( p_pic2 != NULL )
2027 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic2;
2028 p_sys->i_last_pic %= PICTURE_RING_SIZE;
2030 vlc_cond_signal( &p_sys->cond );
2031 vlc_mutex_unlock( &p_sys->lock_out );
2039 static int EncoderThread( sout_stream_sys_t *p_sys )
2041 sout_stream_id_t *id = p_sys->id_video;
2044 while( !p_sys->b_die && !p_sys->b_error )
2048 vlc_mutex_lock( &p_sys->lock_out );
2049 while( p_sys->i_last_pic == p_sys->i_first_pic )
2051 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
2052 if( p_sys->b_die || p_sys->b_error ) break;
2054 if( p_sys->b_die || p_sys->b_error )
2056 vlc_mutex_unlock( &p_sys->lock_out );
2060 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2061 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2062 vlc_mutex_unlock( &p_sys->lock_out );
2064 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2065 vlc_mutex_lock( &p_sys->lock_out );
2066 block_ChainAppend( &p_sys->p_buffers, p_block );
2068 vlc_mutex_unlock( &p_sys->lock_out );
2070 p_pic->pf_release( p_pic );
2073 while( p_sys->i_last_pic != p_sys->i_first_pic )
2075 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2076 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2078 p_pic->pf_release( p_pic );
2081 block_ChainRelease( p_sys->p_buffers );
2086 struct picture_sys_t
2088 vlc_object_t *p_owner;
2091 static void video_release_buffer( picture_t *p_pic )
2093 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
2095 video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
2097 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
2100 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
2101 sout_stream_sys_t *p_sys )
2103 decoder_t *p_dec = (decoder_t *)p_this;
2107 /* Find an empty space in the picture ring buffer */
2108 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2110 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2112 pp_ring[i]->i_status = RESERVED_PICTURE;
2116 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2118 if( pp_ring[i] == 0 ) break;
2121 if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
2123 int i_first_pic = p_sys->i_first_pic;
2125 if( p_sys->i_first_pic != p_sys->i_last_pic )
2127 /* Encoder still has stuff to encode, wait to clear-up the list */
2128 while( p_sys->i_first_pic == i_first_pic )
2132 /* Find an empty space in the picture ring buffer */
2133 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2135 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2137 pp_ring[i]->i_status = RESERVED_PICTURE;
2141 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2143 if( pp_ring[i] == 0 ) break;
2147 if( i == PICTURE_RING_SIZE )
2149 msg_Err( p_this, "decoder/filter is leaking pictures, "
2150 "resetting its ring buffer" );
2152 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2154 pp_ring[i]->pf_release( pp_ring[i] );
2160 p_pic = malloc( sizeof(picture_t) );
2161 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2162 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
2163 p_dec->fmt_out.video.i_chroma,
2164 p_dec->fmt_out.video.i_width,
2165 p_dec->fmt_out.video.i_height,
2166 p_dec->fmt_out.video.i_aspect );
2168 if( !p_pic->i_planes )
2174 p_pic->pf_release = video_release_buffer;
2175 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2176 p_pic->p_sys->p_owner = p_this;
2177 p_pic->i_status = RESERVED_PICTURE;
2184 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
2186 return video_new_buffer( VLC_OBJECT(p_dec),
2187 p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2190 static picture_t *video_new_buffer_filter( filter_t *p_filter )
2192 return video_new_buffer( VLC_OBJECT(p_filter),
2193 p_filter->p_owner->pp_pics,
2194 p_filter->p_owner->p_sys );
2197 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2199 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2200 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
2201 if( p_pic ) free( p_pic );
2204 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
2206 p_pic->i_refcount = 0;
2207 p_pic->i_status = DESTROYED_PICTURE;
2210 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
2212 p_pic->i_refcount = 0;
2213 p_pic->i_status = DESTROYED_PICTURE;
2216 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2218 p_pic->i_refcount++;
2221 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2223 video_release_buffer( p_pic );
2229 static subpicture_t *spu_new_buffer( decoder_t * );
2230 static void spu_del_buffer( decoder_t *, subpicture_t * );
2232 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2234 sout_stream_sys_t *p_sys = p_stream->p_sys;
2240 /* Initialization of decoder structures */
2241 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2242 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2243 id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2244 /* id->p_decoder->p_cfg = p_sys->p_spu_cfg; */
2246 id->p_decoder->p_module =
2247 module_Need( id->p_decoder, "decoder", "$codec", 0 );
2249 if( !id->p_decoder->p_module )
2251 msg_Err( p_stream, "cannot find decoder" );
2252 return VLC_EGENERIC;
2255 if( !p_sys->b_soverlay )
2261 /* Initialization of encoder format structures */
2262 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
2263 id->p_decoder->fmt_in.i_codec );
2265 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2267 id->p_encoder->p_module =
2268 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2270 if( !id->p_encoder->p_module )
2272 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2273 msg_Err( p_stream, "cannot find encoder" );
2274 return VLC_EGENERIC;
2280 p_sys->p_spu = spu_Create( p_stream );
2281 spu_Init( p_sys->p_spu );
2287 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2290 if( id->p_decoder->p_module )
2291 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2294 if( id->p_encoder->p_module )
2295 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2298 static int transcode_spu_process( sout_stream_t *p_stream,
2299 sout_stream_id_t *id,
2300 block_t *in, block_t **out )
2302 sout_stream_sys_t *p_sys = p_stream->p_sys;
2303 subpicture_t *p_subpic;
2306 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2307 if( !p_subpic ) return VLC_EGENERIC;
2309 if( p_sys->b_master_sync && p_sys->i_master_drift )
2311 p_subpic->i_start -= p_sys->i_master_drift;
2312 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2315 if( p_sys->b_soverlay )
2317 spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2323 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2324 spu_del_buffer( id->p_decoder, p_subpic );
2328 block_ChainAppend( out, p_block );
2333 return VLC_EGENERIC;
2336 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2338 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2339 return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2342 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2344 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2345 spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );
2351 static int transcode_osd_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2353 sout_stream_sys_t *p_sys = p_stream->p_sys;
2357 fmt.i_id = 0xbd1f; /* pid ?? */
2358 fmt.i_group = 3; /* pmt entry ?? */
2359 fmt.i_codec = VLC_FOURCC( 'Y', 'U', 'V', 'A' );
2360 fmt.psz_language = strdup( "osd" );
2362 id = malloc( sizeof( sout_stream_id_t ) );
2363 memset( id, 0, sizeof(sout_stream_id_t) );
2366 id->p_decoder = NULL;
2367 id->p_encoder = NULL;
2369 /* Create encoder object */
2370 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
2371 if( !id->p_encoder )
2373 msg_Err( p_stream, "out of memory" );
2376 vlc_object_attach( id->p_encoder, p_stream );
2377 id->p_encoder->p_module = NULL;
2379 /* Create fake destination format */
2380 es_format_Init( &id->p_encoder->fmt_out, fmt.i_cat, 0 );
2381 id->p_encoder->fmt_out.i_id = fmt.i_id;
2382 id->p_encoder->fmt_out.i_group = fmt.i_group;
2383 id->p_encoder->fmt_out.psz_language = strdup( fmt.psz_language );
2385 if( p_sys->i_osdcodec != 0 || p_sys->psz_osdenc )
2387 msg_Dbg( p_stream, "creating osdmenu transcoding from fcc=`%4.4s' "
2388 "to fcc=`%4.4s'", (char*)&fmt.i_codec,
2389 (char*)&p_sys->i_osdcodec );
2391 /* Complete destination format */
2392 id->p_encoder->fmt_out.i_codec = p_sys->i_osdcodec;
2398 /* Initialization of encoder format structures */
2399 es_format_Init( &id->p_encoder->fmt_in, fmt.i_cat, fmt.i_codec );
2400 id->p_encoder->fmt_in.psz_language = strdup( fmt.psz_language );
2402 id->p_encoder->p_cfg = p_sys->p_osd_cfg;
2404 id->p_encoder->p_module =
2405 module_Need( id->p_encoder, "encoder", p_sys->psz_osdenc, VLC_TRUE );
2407 if( !id->p_encoder->p_module )
2409 msg_Err( p_stream, "cannot find encoder" );
2413 /* open output stream */
2414 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
2415 id->b_transcode = VLC_TRUE;
2417 if( !id->id ) goto error;
2421 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
2422 (char*)&fmt.i_codec );
2423 id->id = p_sys->p_out->pf_add( p_sys->p_out, &fmt );
2424 id->b_transcode = VLC_FALSE;
2426 if( !id->id ) goto error;
2430 p_sys->b_es_osd = VLC_TRUE;
2434 p_sys->p_spu = spu_Create( p_stream );
2435 if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
2436 msg_Err( p_sys, "spu initialisation failed" );
2439 if( fmt.psz_language )
2440 free( fmt.psz_language );
2445 msg_Err( p_stream, "starting osd encoding thread failed" );
2446 if( id->p_encoder->p_module )
2447 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2450 vlc_object_detach( id->p_encoder );
2451 vlc_object_destroy( id->p_encoder );
2453 if( fmt.psz_language ) free( fmt.psz_language );
2454 if( id ) free( id );
2455 p_sys->id_osd = NULL;
2456 p_sys->b_es_osd = VLC_FALSE;
2457 return VLC_EGENERIC;
2460 static void transcode_osd_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2462 sout_stream_sys_t *p_sys = p_stream->p_sys;
2465 if( p_sys->b_es_osd && id )
2467 if( id->p_encoder->p_module )
2468 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2470 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
2474 vlc_object_detach( id->p_encoder );
2475 vlc_object_destroy( id->p_encoder );
2478 p_sys->b_es_osd = VLC_FALSE;
2479 if( id ) free( id );
2482 static int transcode_osd_process( sout_stream_t *p_stream,
2483 sout_stream_id_t *id,
2484 block_t *in, block_t **out )
2486 sout_stream_sys_t *p_sys = p_stream->p_sys;
2487 subpicture_t *p_subpic = NULL;
2489 /* Check if we have a subpicture to send */
2490 if( p_sys->p_spu && in->i_dts > 0)
2492 p_subpic = spu_SortSubpictures( p_sys->p_spu, in->i_dts );
2496 msg_Warn( p_stream, "spu channel not initialized, doing it now" );
2499 p_sys->p_spu = spu_Create( p_stream );
2500 if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
2501 msg_Err( p_stream, "spu initialisation failed" );
2507 block_t *p_block = NULL;
2509 if( p_sys->b_master_sync && p_sys->i_master_drift )
2511 p_subpic->i_start -= p_sys->i_master_drift;
2512 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2515 p_block = p_sys->id_osd->p_encoder->pf_encode_sub( p_sys->id_osd->p_encoder, p_subpic );
2518 p_block->i_dts = p_block->i_pts = in->i_dts;
2519 block_ChainAppend( out, p_block );
2522 if( p_sys->p_out->pf_send( p_sys->p_out, p_sys->id_osd->id, *out ) == VLC_SUCCESS )
2523 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
2528 return VLC_EGENERIC;