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 VFILTER_TEXT N_("Video filter")
77 #define VFILTER_LONGTEXT N_( \
78 "Allows you to specify video filters used after the video " \
79 "transcoding and subpictures overlaying." )
81 #define CROPTOP_TEXT N_("Video crop top")
82 #define CROPTOP_LONGTEXT N_( \
83 "Allows you to specify the top coordinate for the video cropping." )
84 #define CROPLEFT_TEXT N_("Video crop left")
85 #define CROPLEFT_LONGTEXT N_( \
86 "Allows you to specify the left coordinate for the video cropping." )
87 #define CROPBOTTOM_TEXT N_("Video crop bottom")
88 #define CROPBOTTOM_LONGTEXT N_( \
89 "Allows you to specify the bottom coordinate for the video cropping." )
90 #define CROPRIGHT_TEXT N_("Video crop right")
91 #define CROPRIGHT_LONGTEXT N_( \
92 "Allows you to specify the right coordinate for the video cropping." )
94 #define AENC_TEXT N_("Audio encoder")
95 #define AENC_LONGTEXT N_( \
96 "Allows you to specify the audio encoder to use and its associated " \
98 #define ACODEC_TEXT N_("Destination audio codec")
99 #define ACODEC_LONGTEXT N_( \
100 "Allows you to specify the destination audio codec used for the " \
101 "streaming output." )
102 #define AB_TEXT N_("Audio bitrate")
103 #define AB_LONGTEXT N_( \
104 "Allows you to specify the audio bitrate used for the streaming " \
106 #define ARATE_TEXT N_("Audio sample rate")
107 #define ARATE_LONGTEXT N_( \
108 "Allows you to specify the audio sample rate used for the streaming " \
110 #define ACHANS_TEXT N_("Audio channels")
111 #define ACHANS_LONGTEXT N_( \
112 "Allows you to specify the number of audio channels used for the " \
113 "streaming output." )
115 #define SENC_TEXT N_("Subtitles encoder")
116 #define SENC_LONGTEXT N_( \
117 "Allows you to specify the subtitles encoder to use and its associated " \
119 #define SCODEC_TEXT N_("Destination subtitles codec")
120 #define SCODEC_LONGTEXT N_( \
121 "Allows you to specify the destination subtitles codec used for the " \
122 "streaming output." )
123 #define SFILTER_TEXT N_("Subpictures filter")
124 #define SFILTER_LONGTEXT N_( \
125 "Allows you to specify subpictures filters used during the video " \
126 "transcoding. The subpictures produced by the filters will be overlayed " \
127 "directly onto the video." )
129 #define THREADS_TEXT N_("Number of threads")
130 #define THREADS_LONGTEXT N_( \
131 "Allows you to specify the number of threads used for the transcoding." )
132 #define HP_TEXT N_("High priority")
133 #define HP_LONGTEXT N_( \
134 "Runs the optional encoder thread at the OUTPUT priority instead of " \
137 #define ASYNC_TEXT N_("Synchronise on audio track")
138 #define ASYNC_LONGTEXT N_( \
139 "This option will drop/duplicate video frames to synchronise the video " \
140 "track on the audio track." )
142 #define HURRYUP_TEXT N_( "Hurry up" )
143 #define HURRYUP_LONGTEXT N_( "Allows you to specify if the transcoder " \
144 "should drop frames if your CPU can't keep up with the encoding rate." )
146 static char *ppsz_deinterlace_type[] =
148 "deinterlace", "ffmpeg-deinterlace"
151 static int Open ( vlc_object_t * );
152 static void Close( vlc_object_t * );
154 #define SOUT_CFG_PREFIX "sout-transcode-"
157 set_shortname( _("Transcode"));
158 set_description( _("Transcode stream output") );
159 set_capability( "sout stream", 50 );
160 add_shortcut( "transcode" );
161 set_callbacks( Open, Close );
162 set_category( CAT_SOUT );
163 set_subcategory( SUBCAT_SOUT_STREAM );
164 set_section( N_("Video"), NULL );
165 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
166 VENC_LONGTEXT, VLC_FALSE );
167 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
168 VCODEC_LONGTEXT, VLC_FALSE );
169 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
170 VB_LONGTEXT, VLC_FALSE );
171 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
172 SCALE_LONGTEXT, VLC_FALSE );
173 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
174 FPS_LONGTEXT, VLC_FALSE );
175 add_bool( SOUT_CFG_PREFIX "hurry-up", VLC_TRUE, NULL, HURRYUP_TEXT,
176 HURRYUP_LONGTEXT, VLC_FALSE );
177 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
178 DEINTERLACE_LONGTEXT, VLC_FALSE );
179 add_string( SOUT_CFG_PREFIX "deinterlace-module", "deinterlace", NULL,
180 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
182 change_string_list( ppsz_deinterlace_type, 0, 0 );
183 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
184 WIDTH_LONGTEXT, VLC_TRUE );
185 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
186 HEIGHT_LONGTEXT, VLC_TRUE );
187 add_module_list_cat( SOUT_CFG_PREFIX "vfilter", SUBCAT_VIDEO_VFILTER,
189 VFILTER_TEXT, VFILTER_LONGTEXT, VLC_FALSE );
191 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
192 CROPTOP_LONGTEXT, VLC_TRUE );
193 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
194 CROPLEFT_LONGTEXT, VLC_TRUE );
195 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
196 CROPBOTTOM_LONGTEXT, VLC_TRUE );
197 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
198 CROPRIGHT_LONGTEXT, VLC_TRUE );
200 set_section( N_("Audio"), NULL );
201 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
202 AENC_LONGTEXT, VLC_FALSE );
203 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
204 ACODEC_LONGTEXT, VLC_FALSE );
205 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
206 AB_LONGTEXT, VLC_FALSE );
207 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
208 ACHANS_LONGTEXT, VLC_FALSE );
209 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
210 ARATE_LONGTEXT, VLC_TRUE );
211 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
212 ASYNC_LONGTEXT, VLC_FALSE );
214 set_section( N_("Overlays/Subtitles"), NULL );
215 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
216 SENC_LONGTEXT, VLC_FALSE );
217 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
218 SCODEC_LONGTEXT, VLC_FALSE );
219 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
220 SCODEC_LONGTEXT, VLC_FALSE );
221 add_module_list_cat( SOUT_CFG_PREFIX "sfilter", SUBCAT_VIDEO_SUBPIC,
223 SFILTER_TEXT, SFILTER_LONGTEXT, VLC_FALSE );
225 set_section( N_("Miscellaneous"), NULL );
226 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
227 THREADS_LONGTEXT, VLC_TRUE );
228 add_bool( SOUT_CFG_PREFIX "high-priority", 0, NULL, HP_TEXT, HP_LONGTEXT,
233 static const char *ppsz_sout_options[] = {
234 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
235 "scale", "fps", "width", "height", "vfilter", "deinterlace",
236 "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab",
237 "samplerate", "channels", "senc", "scodec", "soverlay", "sfilter",
238 "audio-sync", "high-priority", NULL
241 /*****************************************************************************
242 * Exported prototypes
243 *****************************************************************************/
244 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
245 static int Del ( sout_stream_t *, sout_stream_id_t * );
246 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
248 static int transcode_audio_new ( sout_stream_t *, sout_stream_id_t * );
249 static void transcode_audio_close ( sout_stream_t *, sout_stream_id_t * );
250 static int transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
251 block_t *, block_t ** );
253 static aout_buffer_t *audio_new_buffer( decoder_t *, int );
254 static void audio_del_buffer( decoder_t *, aout_buffer_t * );
256 static int transcode_video_new ( sout_stream_t *, sout_stream_id_t * );
257 static void transcode_video_close ( sout_stream_t *, sout_stream_id_t * );
258 static int transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
259 static int transcode_video_process( sout_stream_t *, sout_stream_id_t *,
260 block_t *, block_t ** );
262 static void video_del_buffer( vlc_object_t *, picture_t * );
263 static picture_t *video_new_buffer_decoder( decoder_t * );
264 static void video_del_buffer_decoder( decoder_t *, picture_t * );
265 static void video_link_picture_decoder( decoder_t *, picture_t * );
266 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
267 static picture_t *video_new_buffer_filter( filter_t * );
268 static void video_del_buffer_filter( filter_t *, picture_t * );
270 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
271 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
272 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
273 block_t *, block_t ** );
275 static int transcode_osd_new ( sout_stream_t *, sout_stream_id_t * );
276 static void transcode_osd_close ( sout_stream_t *, sout_stream_id_t * );
277 static int transcode_osd_process( sout_stream_t *, sout_stream_id_t *,
278 block_t *, block_t ** );
280 static int EncoderThread( struct sout_stream_sys_t * p_sys );
282 static int pi_channels_maps[6] =
285 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
286 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
287 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
288 | AOUT_CHAN_REARRIGHT,
289 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
290 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
293 #define PICTURE_RING_SIZE 64
294 #define SUBPICTURE_RING_SIZE 20
296 struct sout_stream_sys_t
300 sout_stream_t *p_out;
301 sout_stream_id_t *id_video;
303 vlc_mutex_t lock_out;
305 picture_t * pp_pics[PICTURE_RING_SIZE];
306 int i_first_pic, i_last_pic;
309 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
311 sout_cfg_t *p_audio_cfg;
317 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
319 sout_cfg_t *p_video_cfg;
325 vlc_bool_t b_deinterlace;
326 char *psz_deinterlace;
327 sout_cfg_t *p_deinterlace_cfg;
329 vlc_bool_t b_high_priority;
330 vlc_bool_t b_hurry_up;
331 char *psz_vfilters[10];
332 sout_cfg_t *p_vfilters_cfg[10];
341 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
343 vlc_bool_t b_soverlay;
344 sout_cfg_t *p_spu_cfg;
348 sout_stream_id_t *id_osd; /* extension for streaming OSD menus */
349 vlc_fourcc_t i_osdcodec; /* codec osd menu (0 if not transcode) */
351 sout_cfg_t *p_osd_cfg;
352 vlc_bool_t b_osd; /* VLC_TRUE when osd es is registered */
355 vlc_bool_t b_master_sync;
356 mtime_t i_master_drift;
359 struct decoder_owner_sys_t
361 picture_t *pp_pics[PICTURE_RING_SIZE];
362 sout_stream_sys_t *p_sys;
364 struct filter_owner_sys_t
366 picture_t *pp_pics[PICTURE_RING_SIZE];
367 sout_stream_sys_t *p_sys;
370 /*****************************************************************************
372 *****************************************************************************/
373 static int Open( vlc_object_t *p_this )
375 sout_stream_t *p_stream = (sout_stream_t*)p_this;
376 sout_stream_sys_t *p_sys;
379 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
381 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
384 msg_Err( p_stream, "cannot create chain" );
385 vlc_object_destroy( p_sys );
389 p_sys->i_master_drift = 0;
391 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
394 /* Audio transcoding parameters */
395 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
396 p_sys->psz_aenc = NULL;
397 p_sys->p_audio_cfg = NULL;
398 if( val.psz_string && *val.psz_string )
401 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
403 if( psz_next ) free( psz_next );
405 if( val.psz_string ) free( val.psz_string );
407 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
409 if( val.psz_string && *val.psz_string )
412 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
413 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
415 if( val.psz_string ) free( val.psz_string );
417 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
418 p_sys->i_abitrate = val.i_int;
419 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
421 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
422 p_sys->i_sample_rate = val.i_int;
424 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
425 p_sys->i_channels = val.i_int;
427 if( p_sys->i_acodec )
429 if( (strncmp( (char *)&p_sys->i_acodec, "mp3", 3) == 0) &&
430 (p_sys->i_channels > 2) )
432 msg_Warn( p_stream, "%d channels invalid for mp3, forcing to 2",
434 p_sys->i_channels = 2;
436 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
437 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
438 p_sys->i_channels, p_sys->i_abitrate / 1000 );
441 /* Video transcoding parameters */
442 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
443 p_sys->psz_venc = NULL;
444 p_sys->p_video_cfg = NULL;
445 if( val.psz_string && *val.psz_string )
448 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
450 if( psz_next ) free( psz_next );
452 if( val.psz_string ) free( val.psz_string );
454 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
456 if( val.psz_string && *val.psz_string )
459 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
460 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
462 if( val.psz_string ) free( val.psz_string );
464 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
465 p_sys->i_vbitrate = val.i_int;
466 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
468 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
469 p_sys->f_scale = val.f_float;
471 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
472 p_sys->f_fps = val.f_float;
474 var_Get( p_stream, SOUT_CFG_PREFIX "hurry-up", &val );
475 p_sys->b_hurry_up = val.b_bool;
477 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
478 p_sys->i_width = val.i_int;
480 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
481 p_sys->i_height = val.i_int;
483 var_Get( p_stream, SOUT_CFG_PREFIX "vfilter", &val );
484 p_sys->i_vfilters = 0;
485 if( val.psz_string && *val.psz_string )
487 char *psz_parser = val.psz_string;
489 while( psz_parser != NULL && *psz_parser != '\0' )
491 psz_parser = sout_CfgCreate(
492 &p_sys->psz_vfilters[p_sys->i_vfilters],
493 &p_sys->p_vfilters_cfg[p_sys->i_vfilters],
496 if( psz_parser != NULL && *psz_parser != '\0' ) psz_parser++;
499 if( val.psz_string ) free( val.psz_string );
500 p_sys->psz_vfilters[p_sys->i_vfilters] = NULL;
501 p_sys->p_vfilters_cfg[p_sys->i_vfilters] = NULL;
503 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
504 p_sys->b_deinterlace = val.b_bool;
506 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace-module", &val );
507 p_sys->psz_deinterlace = NULL;
508 p_sys->p_deinterlace_cfg = NULL;
509 if( val.psz_string && *val.psz_string )
512 psz_next = sout_CfgCreate( &p_sys->psz_deinterlace,
513 &p_sys->p_deinterlace_cfg,
515 if( psz_next ) free( psz_next );
517 if( val.psz_string ) free( val.psz_string );
519 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
520 p_sys->i_crop_top = val.i_int;
521 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
522 p_sys->i_crop_bottom = val.i_int;
523 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
524 p_sys->i_crop_left = val.i_int;
525 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
526 p_sys->i_crop_right = val.i_int;
528 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
529 p_sys->i_threads = val.i_int;
530 var_Get( p_stream, SOUT_CFG_PREFIX "high-priority", &val );
531 p_sys->b_high_priority = val.b_bool;
533 if( p_sys->i_vcodec )
535 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
536 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
537 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
540 /* Subpictures transcoding parameters */
542 p_sys->psz_senc = NULL;
543 p_sys->p_spu_cfg = NULL;
546 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
547 if( val.psz_string && *val.psz_string )
550 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
552 if( psz_next ) free( psz_next );
554 if( val.psz_string ) free( val.psz_string );
556 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
557 if( val.psz_string && *val.psz_string )
560 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
561 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
563 if( val.psz_string ) free( val.psz_string );
565 if( p_sys->i_scodec )
567 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
570 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
571 p_sys->b_soverlay = val.b_bool;
573 var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
574 if( val.psz_string && *val.psz_string )
576 p_sys->p_spu = spu_Create( p_stream );
577 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
578 var_Set( p_sys->p_spu, "sub-filter", val );
579 spu_Init( p_sys->p_spu );
581 if( val.psz_string ) free( val.psz_string );
583 /* OSD menu transcoding parameters */
584 p_sys->psz_osdenc = NULL;
585 p_sys->p_osd_cfg = NULL;
586 p_sys->i_osdcodec = 0;
587 p_sys->b_osd = VLC_FALSE;
588 if( config_GetInt( p_stream, "osd" ) )
590 /* vlc_value_t val;*/
593 psz_next = sout_CfgCreate( &p_sys->psz_osdenc,
594 &p_sys->p_osd_cfg, strdup( "dvbsub") );
595 if( psz_next ) free( psz_next );
597 p_sys->i_osdcodec = VLC_FOURCC('Y','U','V','P' );
599 msg_Dbg( p_stream, "codec osd=%4.4s", (char *)&p_sys->i_osdcodec );
601 /* val.psz_string = strdup("osdmenu");
604 p_sys->p_spu = spu_Create( p_stream );
605 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
606 spu_Init( p_sys->p_spu );
608 var_Set( p_sys->p_spu, "sub-filter", val );
609 if( val.psz_string ) free( val.psz_string );*/
613 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
614 p_sys->b_master_sync = val.b_bool;
615 if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
617 p_stream->pf_add = Add;
618 p_stream->pf_del = Del;
619 p_stream->pf_send = Send;
620 p_stream->p_sys = p_sys;
625 /*****************************************************************************
627 *****************************************************************************/
628 static void Close( vlc_object_t * p_this )
630 sout_stream_t *p_stream = (sout_stream_t*)p_this;
631 sout_stream_sys_t *p_sys = p_stream->p_sys;
633 sout_StreamDelete( p_sys->p_out );
635 while( p_sys->p_audio_cfg != NULL )
637 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
639 if( p_sys->p_audio_cfg->psz_name )
640 free( p_sys->p_audio_cfg->psz_name );
641 if( p_sys->p_audio_cfg->psz_value )
642 free( p_sys->p_audio_cfg->psz_value );
643 free( p_sys->p_audio_cfg );
645 p_sys->p_audio_cfg = p_next;
647 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
649 while( p_sys->p_video_cfg != NULL )
651 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
653 if( p_sys->p_video_cfg->psz_name )
654 free( p_sys->p_video_cfg->psz_name );
655 if( p_sys->p_video_cfg->psz_value )
656 free( p_sys->p_video_cfg->psz_value );
657 free( p_sys->p_video_cfg );
659 p_sys->p_video_cfg = p_next;
661 if( p_sys->psz_venc ) free( p_sys->psz_venc );
663 while( p_sys->p_deinterlace_cfg != NULL )
665 sout_cfg_t *p_next = p_sys->p_deinterlace_cfg->p_next;
667 if( p_sys->p_deinterlace_cfg->psz_name )
668 free( p_sys->p_deinterlace_cfg->psz_name );
669 if( p_sys->p_deinterlace_cfg->psz_value )
670 free( p_sys->p_deinterlace_cfg->psz_value );
671 free( p_sys->p_deinterlace_cfg );
673 p_sys->p_deinterlace_cfg = p_next;
675 if( p_sys->psz_deinterlace ) free( p_sys->psz_deinterlace );
677 while( p_sys->p_spu_cfg != NULL )
679 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
681 if( p_sys->p_spu_cfg->psz_name )
682 free( p_sys->p_spu_cfg->psz_name );
683 if( p_sys->p_spu_cfg->psz_value )
684 free( p_sys->p_spu_cfg->psz_value );
685 free( p_sys->p_spu_cfg );
687 p_sys->p_spu_cfg = p_next;
689 if( p_sys->psz_senc ) free( p_sys->psz_senc );
691 if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
693 while( p_sys->p_osd_cfg != NULL )
695 sout_cfg_t *p_next = p_sys->p_osd_cfg->p_next;
697 if( p_sys->p_osd_cfg->psz_name )
698 free( p_sys->p_osd_cfg->psz_name );
699 if( p_sys->p_osd_cfg->psz_value )
700 free( p_sys->p_osd_cfg->psz_value );
701 free( p_sys->p_osd_cfg );
703 p_sys->p_osd_cfg = p_next;
705 if( p_sys->psz_osdenc ) free( p_sys->psz_osdenc );
707 vlc_object_destroy( p_sys );
710 struct sout_stream_id_t
712 vlc_fourcc_t b_transcode;
714 /* id of the out stream */
718 decoder_t *p_decoder;
721 filter_t *pp_filter[10];
723 filter_t *pp_vfilter[10];
727 encoder_t *p_encoder;
730 date_t interpolated_pts;
733 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
735 sout_stream_sys_t *p_sys = p_stream->p_sys;
736 sout_stream_id_t *id;
738 id = malloc( sizeof( sout_stream_id_t ) );
739 memset( id, 0, sizeof(sout_stream_id_t) );
742 id->p_decoder = NULL;
743 id->p_encoder = NULL;
745 /* Create decoder object */
746 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
749 msg_Err( p_stream, "out of memory" );
752 vlc_object_attach( id->p_decoder, p_stream );
753 id->p_decoder->p_module = NULL;
754 id->p_decoder->fmt_in = *p_fmt;
755 id->p_decoder->b_pace_control = VLC_TRUE;
757 /* Create encoder object */
758 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
761 msg_Err( p_stream, "out of memory" );
764 vlc_object_attach( id->p_encoder, p_stream );
765 id->p_encoder->p_module = NULL;
767 /* Create destination format */
768 es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
769 id->p_encoder->fmt_out.i_id = p_fmt->i_id;
770 id->p_encoder->fmt_out.i_group = p_fmt->i_group;
771 if( p_fmt->psz_language )
772 id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
774 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
777 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
778 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
780 /* Complete destination format */
781 id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
782 id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
783 p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
784 id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
785 id->p_encoder->fmt_out.audio.i_bitspersample =
786 p_fmt->audio.i_bitspersample;
787 id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
788 p_sys->i_channels : p_fmt->audio.i_channels;
789 /* Sanity check for audio channels */
790 id->p_encoder->fmt_out.audio.i_channels =
791 __MIN( id->p_encoder->fmt_out.audio.i_channels,
792 id->p_decoder->fmt_in.audio.i_channels );
793 id->p_encoder->fmt_out.audio.i_original_channels =
794 id->p_decoder->fmt_in.audio.i_physical_channels;
795 if( id->p_decoder->fmt_in.audio.i_channels ==
796 id->p_encoder->fmt_out.audio.i_channels )
798 id->p_encoder->fmt_out.audio.i_physical_channels =
799 id->p_decoder->fmt_in.audio.i_physical_channels;
803 id->p_encoder->fmt_out.audio.i_physical_channels =
804 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
807 /* Build decoder -> filter -> encoder chain */
808 if( transcode_audio_new( p_stream, id ) )
810 msg_Err( p_stream, "cannot create audio chain" );
814 /* Open output stream */
815 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
816 id->b_transcode = VLC_TRUE;
818 if( !id->id ) goto error;
820 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
822 else if( p_fmt->i_cat == VIDEO_ES &&
823 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
826 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
827 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
829 /* Complete destination format */
830 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
831 id->p_encoder->fmt_out.video.i_width = p_sys->i_width;
832 id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
833 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
835 /* Build decoder -> filter -> encoder chain */
836 if( transcode_video_new( p_stream, id ) )
838 msg_Err( p_stream, "cannot create video chain" );
842 /* Stream will be added later on because we don't know
843 * all the characteristics of the decoded stream yet */
844 id->b_transcode = VLC_TRUE;
846 if( p_sys->f_fps > 0 )
848 id->p_encoder->fmt_out.video.i_frame_rate =
849 (p_sys->f_fps * 1001) + 0.5;
850 id->p_encoder->fmt_out.video.i_frame_rate_base = 1001;
853 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
855 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
856 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
857 (char*)&p_sys->i_scodec );
859 /* Complete destination format */
860 id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
862 /* build decoder -> filter -> encoder */
863 if( transcode_spu_new( p_stream, id ) )
865 msg_Err( p_stream, "cannot create subtitles chain" );
869 /* open output stream */
870 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
871 id->b_transcode = VLC_TRUE;
873 if( !id->id ) goto error;
875 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
877 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
878 (char*)&p_fmt->i_codec );
880 id->b_transcode = VLC_TRUE;
882 /* Build decoder -> filter -> overlaying chain */
883 if( transcode_spu_new( p_stream, id ) )
885 msg_Err( p_stream, "cannot create subtitles chain" );
891 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
892 (char*)&p_fmt->i_codec );
893 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
894 id->b_transcode = VLC_FALSE;
896 if( !id->id ) goto error;
899 if( config_GetInt( p_stream, "osd" ) )
901 /* Create a fake OSD menu elementary stream */
902 if( !p_sys->b_osd && (p_sys->i_osdcodec != 0 || p_sys->psz_osdenc) )
904 if( transcode_osd_new( p_stream, p_sys->id_osd ) )
906 msg_Err( p_stream, "cannot create osd chain" );
909 p_sys->b_osd = VLC_TRUE;
917 vlc_object_detach( id->p_decoder );
918 vlc_object_destroy( id->p_decoder );
923 vlc_object_detach( id->p_encoder );
924 vlc_object_destroy( id->p_encoder );
931 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
933 sout_stream_sys_t *p_sys = p_stream->p_sys;
935 if( config_GetInt( p_stream, "osd" ) && p_sys->b_osd )
936 transcode_osd_close( p_stream, p_sys->id_osd );
938 if( id->b_transcode )
940 switch( id->p_decoder->fmt_in.i_cat )
943 transcode_audio_close( p_stream, id );
946 transcode_video_close( p_stream, id );
949 transcode_spu_close( p_stream, id );
954 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
958 vlc_object_detach( id->p_decoder );
959 vlc_object_destroy( id->p_decoder );
964 vlc_object_detach( id->p_encoder );
965 vlc_object_destroy( id->p_encoder );
973 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
976 sout_stream_sys_t *p_sys = p_stream->p_sys;
977 block_t *p_out = NULL;
979 if( !id->b_transcode && id->id )
981 /* Transcode OSD menu pictures. */
984 transcode_osd_process( p_stream, id, p_buffer, &p_out );
986 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
988 else if( !id->b_transcode )
990 block_Release( p_buffer );
994 switch( id->p_decoder->fmt_in.i_cat )
997 transcode_audio_process( p_stream, id, p_buffer, &p_out );
1001 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
1004 return VLC_EGENERIC;
1009 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
1012 return VLC_EGENERIC;
1018 block_Release( p_buffer );
1022 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
1026 /****************************************************************************
1027 * decoder reencoder part
1028 ****************************************************************************/
1029 int audio_BitsPerSample( vlc_fourcc_t i_format )
1033 case VLC_FOURCC('u','8',' ',' '):
1034 case VLC_FOURCC('s','8',' ',' '):
1037 case VLC_FOURCC('u','1','6','l'):
1038 case VLC_FOURCC('s','1','6','l'):
1039 case VLC_FOURCC('u','1','6','b'):
1040 case VLC_FOURCC('s','1','6','b'):
1043 case VLC_FOURCC('u','2','4','l'):
1044 case VLC_FOURCC('s','2','4','l'):
1045 case VLC_FOURCC('u','2','4','b'):
1046 case VLC_FOURCC('s','2','4','b'):
1049 case VLC_FOURCC('u','3','2','l'):
1050 case VLC_FOURCC('s','3','2','l'):
1051 case VLC_FOURCC('u','3','2','b'):
1052 case VLC_FOURCC('s','3','2','b'):
1053 case VLC_FOURCC('f','l','3','2'):
1054 case VLC_FOURCC('f','i','3','2'):
1057 case VLC_FOURCC('f','l','6','4'):
1064 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
1065 sout_stream_id_t *id,
1066 es_format_t *p_fmt_in,
1067 es_format_t *p_fmt_out )
1069 filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1071 vlc_object_attach( p_filter, p_stream );
1072 p_filter->pf_audio_buffer_new = (block_t* (*) (filter_t*, int))__block_New;
1074 p_filter->fmt_in = *p_fmt_in;
1075 p_filter->fmt_out = *p_fmt_out;
1077 p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
1078 if( p_filter->p_module )
1080 p_filter->fmt_out.audio.i_bitspersample =
1081 audio_BitsPerSample( p_filter->fmt_out.i_codec );
1082 *p_fmt_in = p_filter->fmt_out;
1086 vlc_object_detach( p_filter );
1087 vlc_object_destroy( p_filter );
1094 static int transcode_audio_new( sout_stream_t *p_stream,
1095 sout_stream_id_t *id )
1097 sout_stream_sys_t *p_sys = p_stream->p_sys;
1098 es_format_t fmt_last;
1105 /* Initialization of decoder structures */
1106 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1107 id->p_decoder->fmt_out.i_extra = 0;
1108 id->p_decoder->fmt_out.p_extra = 0;
1109 id->p_decoder->pf_decode_audio = 0;
1110 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
1111 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1112 /* id->p_decoder->p_cfg = p_sys->p_audio_cfg; */
1114 id->p_decoder->p_module =
1115 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1117 if( !id->p_decoder->p_module )
1119 msg_Err( p_stream, "cannot find decoder" );
1120 return VLC_EGENERIC;
1122 id->p_decoder->fmt_out.audio.i_bitspersample =
1123 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1124 fmt_last = id->p_decoder->fmt_out;
1125 /* FIX decoders so we don't have to do this */
1126 fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1132 /* Initialization of encoder format structures */
1133 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1134 id->p_decoder->fmt_out.i_codec );
1135 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
1137 /* Initialization of encoder format structures */
1138 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1139 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1140 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
1141 id->p_encoder->fmt_in.audio.i_physical_channels =
1142 id->p_encoder->fmt_out.audio.i_physical_channels;
1143 id->p_encoder->fmt_in.audio.i_original_channels =
1144 id->p_encoder->fmt_out.audio.i_original_channels;
1145 id->p_encoder->fmt_in.audio.i_channels =
1146 id->p_encoder->fmt_out.audio.i_channels;
1147 id->p_encoder->fmt_in.audio.i_bitspersample =
1148 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1150 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1152 id->p_encoder->p_module =
1153 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
1154 if( !id->p_encoder->p_module )
1156 msg_Err( p_stream, "cannot find encoder" );
1157 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1158 id->p_decoder->p_module = 0;
1159 return VLC_EGENERIC;
1161 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1162 id->p_encoder->fmt_in.audio.i_bitspersample =
1163 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1165 /* Load conversion filters */
1166 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1167 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1169 /* We'll have to go through fl32 first */
1170 es_format_t fmt_out = id->p_encoder->fmt_in;
1171 fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1173 id->pp_filter[id->i_filter] =
1174 transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );
1176 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1181 if( fmt_last.audio.i_channels !=
1182 id->p_encoder->fmt_in.audio.i_channels ||
1183 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
1184 fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1186 id->pp_filter[id->i_filter] =
1187 transcode_audio_filter_new( p_stream, id, &fmt_last,
1188 &id->p_encoder->fmt_in );
1190 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1195 /* Final checks to see if conversions were successful */
1196 if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1198 msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1199 (char *)&fmt_last.i_codec,
1200 (char *)&id->p_encoder->fmt_in.i_codec );
1201 transcode_audio_close( p_stream, id );
1202 return VLC_EGENERIC;
1205 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1207 msg_Err( p_stream, "no audio filter found for mixing from"
1208 " %i to %i channels", fmt_last.audio.i_channels,
1209 id->p_encoder->fmt_in.audio.i_channels );
1211 /* FIXME : this might work, but only if the encoder is restarted */
1212 id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1213 id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1215 id->p_encoder->fmt_in.audio.i_physical_channels =
1216 id->p_encoder->fmt_in.audio.i_original_channels =
1217 fmt_last.audio.i_physical_channels;
1218 id->p_encoder->fmt_out.audio.i_physical_channels =
1219 id->p_encoder->fmt_out.audio.i_original_channels =
1220 fmt_last.audio.i_physical_channels;
1222 transcode_audio_close( p_stream, id );
1223 return VLC_EGENERIC;
1227 if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1229 msg_Err( p_stream, "no audio filter found for resampling from"
1230 " %iHz to %iHz", fmt_last.audio.i_rate,
1231 id->p_encoder->fmt_in.audio.i_rate );
1233 /* FIXME : this might work, but only if the encoder is restarted */
1234 id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1235 id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1237 transcode_audio_close( p_stream, id );
1238 return VLC_EGENERIC;
1242 /* FIXME: Hack for mp3 transcoding support */
1243 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1244 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1249 static void transcode_audio_close( sout_stream_t *p_stream,
1250 sout_stream_id_t *id )
1255 if( id->p_decoder->p_module )
1256 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1257 id->p_decoder->p_module = 0;
1260 if( id->p_encoder->p_module )
1261 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1262 id->p_encoder->p_module = 0;
1265 for( i = 0; i < id->i_filter; i++ )
1267 vlc_object_detach( id->pp_filter[i] );
1268 if( id->pp_filter[i]->p_module )
1269 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1270 vlc_object_destroy( id->pp_filter[i] );
1274 static int transcode_audio_process( sout_stream_t *p_stream,
1275 sout_stream_id_t *id,
1276 block_t *in, block_t **out )
1278 sout_stream_sys_t *p_sys = p_stream->p_sys;
1279 aout_buffer_t *p_audio_buf;
1280 block_t *p_block, *p_audio_block;
1284 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1287 if( p_sys->b_master_sync )
1289 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1290 if ( p_audio_buf->start_date - i_dts > MASTER_SYNC_MAX_DRIFT
1291 || p_audio_buf->start_date - i_dts < -MASTER_SYNC_MAX_DRIFT )
1293 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1294 date_Set( &id->interpolated_pts, p_audio_buf->start_date );
1295 i_dts = p_audio_buf->start_date + 1;
1297 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1298 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1299 p_audio_buf->start_date -= p_sys->i_master_drift;
1300 p_audio_buf->end_date -= p_sys->i_master_drift;
1303 p_audio_block = p_audio_buf->p_sys;
1304 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1305 p_audio_block->i_dts = p_audio_block->i_pts =
1306 p_audio_buf->start_date;
1307 p_audio_block->i_length = p_audio_buf->end_date -
1308 p_audio_buf->start_date;
1309 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1311 /* Run filter chain */
1312 for( i = 0; i < id->i_filter; i++ )
1315 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1319 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1320 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1321 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1322 p_audio_buf->start_date = p_audio_block->i_dts;
1323 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1325 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1326 block_ChainAppend( out, p_block );
1327 block_Release( p_audio_block );
1328 free( p_audio_buf );
1334 static void audio_release_buffer( aout_buffer_t *p_buffer )
1336 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1337 if( p_buffer ) free( p_buffer );
1340 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1342 aout_buffer_t *p_buffer;
1346 if( p_dec->fmt_out.audio.i_bitspersample )
1348 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1349 p_dec->fmt_out.audio.i_channels;
1351 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1352 p_dec->fmt_out.audio.i_frame_length )
1354 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1355 p_dec->fmt_out.audio.i_frame_length;
1359 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1362 p_buffer = malloc( sizeof(aout_buffer_t) );
1363 p_buffer->pf_release = audio_release_buffer;
1364 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1366 p_buffer->p_buffer = p_block->p_buffer;
1367 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1368 p_buffer->i_nb_samples = i_samples;
1369 p_block->i_samples = i_samples;
1374 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1376 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1377 if( p_buffer ) free( p_buffer );
1383 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1385 sout_stream_sys_t *p_sys = p_stream->p_sys;
1392 /* Initialization of decoder structures */
1393 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1394 id->p_decoder->fmt_out.i_extra = 0;
1395 id->p_decoder->fmt_out.p_extra = 0;
1396 id->p_decoder->pf_decode_video = 0;
1397 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1398 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1399 id->p_decoder->pf_picture_link = video_link_picture_decoder;
1400 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
1401 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1402 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1403 id->p_decoder->p_owner->pp_pics[i] = 0;
1404 id->p_decoder->p_owner->p_sys = p_sys;
1405 /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
1407 id->p_decoder->p_module =
1408 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1410 if( !id->p_decoder->p_module )
1412 msg_Err( p_stream, "cannot find decoder" );
1413 return VLC_EGENERIC;
1418 * Because some info about the decoded input will only be available
1419 * once the first frame is decoded, we actually only test the availability
1420 * of the encoder here.
1423 /* Initialization of encoder format structures */
1424 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1425 id->p_decoder->fmt_out.i_codec );
1426 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1428 /* The dimensions will be set properly later on.
1429 * Just put sensible values so we can test an encoder is available. */
1430 id->p_encoder->fmt_in.video.i_width =
1431 id->p_encoder->fmt_out.video.i_width ?
1432 id->p_encoder->fmt_out.video.i_width :
1433 id->p_decoder->fmt_in.video.i_width ?
1434 id->p_decoder->fmt_in.video.i_width : 16;
1435 id->p_encoder->fmt_in.video.i_height =
1436 id->p_encoder->fmt_out.video.i_height ?
1437 id->p_encoder->fmt_out.video.i_height :
1438 id->p_decoder->fmt_in.video.i_height ?
1439 id->p_decoder->fmt_in.video.i_height : 16;
1440 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1441 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1443 id->p_encoder->i_threads = p_sys->i_threads;
1444 id->p_encoder->p_cfg = p_sys->p_video_cfg;
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 )
1450 msg_Err( p_stream, "cannot find encoder" );
1451 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1452 id->p_decoder->p_module = 0;
1453 return VLC_EGENERIC;
1456 /* Close the encoder.
1457 * We'll open it only when we have the first frame. */
1458 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1459 id->p_encoder->p_module = NULL;
1461 if( p_sys->i_threads >= 1 )
1463 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
1464 VLC_THREAD_PRIORITY_VIDEO;
1465 p_sys->id_video = id;
1466 vlc_mutex_init( p_stream, &p_sys->lock_out );
1467 vlc_cond_init( p_stream, &p_sys->cond );
1468 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1469 p_sys->i_first_pic = 0;
1470 p_sys->i_last_pic = 0;
1471 p_sys->p_buffers = NULL;
1472 p_sys->b_die = p_sys->b_error = 0;
1473 if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
1476 msg_Err( p_stream, "cannot spawn encoder thread" );
1477 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1478 id->p_decoder->p_module = 0;
1479 return VLC_EGENERIC;
1486 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1487 sout_stream_id_t *id )
1489 sout_stream_sys_t *p_sys = p_stream->p_sys;
1491 /* Hack because of the copy packetizer which can fail to detect the
1492 * proper size (which forces us to wait until the 1st frame
1494 int i_width = id->p_decoder->fmt_out.video.i_width -
1495 p_sys->i_crop_left - p_sys->i_crop_right;
1496 int i_height = id->p_decoder->fmt_out.video.i_height -
1497 p_sys->i_crop_top - p_sys->i_crop_bottom;
1499 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1500 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1502 /* Apply the scaling */
1503 id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1504 id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1506 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1507 id->p_encoder->fmt_out.video.i_height <= 0 )
1509 id->p_encoder->fmt_out.video.i_height =
1510 id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1512 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1513 id->p_encoder->fmt_out.video.i_height > 0 )
1515 id->p_encoder->fmt_out.video.i_width =
1516 id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1519 /* Make sure the size is at least a multiple of 2 */
1520 id->p_encoder->fmt_out.video.i_width =
1521 (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
1522 id->p_encoder->fmt_out.video.i_height =
1523 (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
1525 id->p_encoder->fmt_in.video.i_width =
1526 id->p_encoder->fmt_out.video.i_width;
1527 id->p_encoder->fmt_in.video.i_height =
1528 id->p_encoder->fmt_out.video.i_height;
1530 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1531 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1533 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1534 id->p_decoder->fmt_out.video.i_frame_rate_base )
1536 id->p_encoder->fmt_out.video.i_frame_rate =
1537 id->p_decoder->fmt_out.video.i_frame_rate;
1538 id->p_encoder->fmt_out.video.i_frame_rate_base =
1539 id->p_decoder->fmt_out.video.i_frame_rate_base;
1543 /* Pick a sensible default value */
1544 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1545 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1549 id->p_encoder->fmt_in.video.i_frame_rate =
1550 id->p_encoder->fmt_out.video.i_frame_rate;
1551 id->p_encoder->fmt_in.video.i_frame_rate_base =
1552 id->p_encoder->fmt_out.video.i_frame_rate_base;
1554 date_Init( &id->interpolated_pts,
1555 id->p_encoder->fmt_out.video.i_frame_rate,
1556 id->p_encoder->fmt_out.video.i_frame_rate_base );
1558 /* Check whether a particular aspect ratio was requested */
1559 if( !id->p_encoder->fmt_out.video.i_aspect )
1561 id->p_encoder->fmt_out.video.i_aspect =
1562 id->p_decoder->fmt_out.video.i_aspect;
1564 id->p_encoder->fmt_in.video.i_aspect =
1565 id->p_encoder->fmt_out.video.i_aspect;
1567 id->p_encoder->p_module =
1568 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1569 if( !id->p_encoder->p_module )
1571 msg_Err( p_stream, "cannot find encoder" );
1572 return VLC_EGENERIC;
1575 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1577 /* Hack for mp2v/mp1v transcoding support */
1578 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1579 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1581 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1584 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1585 &id->p_encoder->fmt_out );
1588 msg_Err( p_stream, "cannot add this stream" );
1589 return VLC_EGENERIC;
1595 static void transcode_video_close( sout_stream_t *p_stream,
1596 sout_stream_id_t *id )
1600 if( p_stream->p_sys->i_threads >= 1 )
1602 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1603 p_stream->p_sys->b_die = 1;
1604 vlc_cond_signal( &p_stream->p_sys->cond );
1605 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1606 vlc_thread_join( p_stream->p_sys );
1607 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1608 vlc_cond_destroy( &p_stream->p_sys->cond );
1612 if( id->p_decoder->p_module )
1613 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1615 if( id->p_decoder->p_owner )
1617 /* Clean-up pictures ring buffer */
1618 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1620 if( id->p_decoder->p_owner->pp_pics[i] )
1621 video_del_buffer( VLC_OBJECT(id->p_decoder),
1622 id->p_decoder->p_owner->pp_pics[i] );
1624 free( id->p_decoder->p_owner );
1628 if( id->p_encoder->p_module )
1629 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1632 for( i = 0; i < id->i_filter; i++ )
1634 vlc_object_detach( id->pp_filter[i] );
1635 if( id->pp_filter[i]->p_module )
1636 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1638 /* Clean-up pictures ring buffer */
1639 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1641 if( id->pp_filter[i]->p_owner->pp_pics[j] )
1642 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1643 id->pp_filter[i]->p_owner->pp_pics[j] );
1645 free( id->pp_filter[i]->p_owner );
1647 vlc_object_destroy( id->pp_filter[i] );
1649 for( i = 0; i < id->i_vfilter; i++ )
1651 vlc_object_detach( id->pp_vfilter[i] );
1652 if( id->pp_vfilter[i]->p_module )
1653 module_Unneed( id->pp_vfilter[i], id->pp_vfilter[i]->p_module );
1655 /* Clean-up pictures ring buffer */
1656 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1658 if( id->pp_vfilter[i]->p_owner->pp_pics[j] )
1659 video_del_buffer( VLC_OBJECT(id->pp_vfilter[i]),
1660 id->pp_vfilter[i]->p_owner->pp_pics[j] );
1662 free( id->pp_vfilter[i]->p_owner );
1664 vlc_object_destroy( id->pp_vfilter[i] );
1668 static int transcode_video_process( sout_stream_t *p_stream,
1669 sout_stream_id_t *id,
1670 block_t *in, block_t **out )
1672 sout_stream_sys_t *p_sys = p_stream->p_sys;
1673 int i_duplicate = 1, i;
1677 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1679 subpicture_t *p_subpic = 0;
1681 if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
1683 mtime_t current_date = mdate();
1684 if( current_date + 50000 > p_pic->date )
1686 msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
1687 current_date + 50000 - p_pic->date );
1688 p_pic->pf_release( p_pic );
1693 if( p_sys->b_master_sync )
1695 mtime_t i_video_drift;
1696 mtime_t i_master_drift = p_sys->i_master_drift;
1699 i_pts = date_Get( &id->interpolated_pts ) + 1;
1700 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
1701 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
1703 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1704 date_Set( &id->interpolated_pts, p_pic->date );
1705 i_pts = p_pic->date + 1;
1707 i_video_drift = p_pic->date - i_pts;
1710 /* Set the pts of the frame being encoded */
1711 p_pic->date = i_pts;
1713 if( i_video_drift < i_master_drift - 50000 )
1716 msg_Dbg( p_stream, "dropping frame (%i)",
1717 (int)(i_video_drift - i_master_drift) );
1719 p_pic->pf_release( p_pic );
1722 else if( i_video_drift > i_master_drift + 50000 )
1725 msg_Dbg( p_stream, "adding frame (%i)",
1726 (int)(i_video_drift - i_master_drift) );
1732 if( !id->p_encoder->p_module )
1734 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1736 p_pic->pf_release( p_pic );
1737 transcode_video_close( p_stream, id );
1738 id->b_transcode = VLC_FALSE;
1739 return VLC_EGENERIC;
1743 if( p_stream->p_sys->b_deinterlace )
1745 id->pp_filter[id->i_filter] =
1746 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1747 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1749 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1750 video_new_buffer_filter;
1751 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1752 video_del_buffer_filter;
1754 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1755 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
1756 id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
1757 id->pp_filter[id->i_filter]->p_module =
1758 module_Need( id->pp_filter[id->i_filter],
1759 "video filter2", p_sys->psz_deinterlace, 0 );
1760 if( id->pp_filter[id->i_filter]->p_module )
1762 id->pp_filter[id->i_filter]->p_owner =
1763 malloc( sizeof(filter_owner_sys_t) );
1764 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1765 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1766 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1772 msg_Dbg( p_stream, "no video filter found" );
1773 vlc_object_detach( id->pp_filter[id->i_filter] );
1774 vlc_object_destroy( id->pp_filter[id->i_filter] );
1778 /* Check if we need a filter for chroma conversion or resizing */
1779 if( id->p_decoder->fmt_out.video.i_chroma !=
1780 id->p_encoder->fmt_in.video.i_chroma ||
1781 id->p_decoder->fmt_out.video.i_width !=
1782 id->p_encoder->fmt_out.video.i_width ||
1783 id->p_decoder->fmt_out.video.i_height !=
1784 id->p_encoder->fmt_out.video.i_height ||
1785 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1786 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1788 id->pp_filter[id->i_filter] =
1789 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1790 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1792 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1793 video_new_buffer_filter;
1794 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1795 video_del_buffer_filter;
1797 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1798 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1799 id->pp_filter[id->i_filter]->p_cfg = NULL;
1800 id->pp_filter[id->i_filter]->p_module =
1801 module_Need( id->pp_filter[id->i_filter],
1802 "video filter2", 0, 0 );
1803 if( id->pp_filter[id->i_filter]->p_module )
1805 id->pp_filter[id->i_filter]->p_owner =
1806 malloc( sizeof(filter_owner_sys_t) );
1807 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1808 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1809 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1815 msg_Dbg( p_stream, "no video filter found" );
1816 vlc_object_detach( id->pp_filter[id->i_filter] );
1817 vlc_object_destroy( id->pp_filter[id->i_filter] );
1819 p_pic->pf_release( p_pic );
1820 transcode_video_close( p_stream, id );
1821 id->b_transcode = VLC_FALSE;
1822 return VLC_EGENERIC;
1826 for( i = 0; i < p_sys->i_vfilters; i++ )
1828 id->pp_vfilter[id->i_vfilter] =
1829 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1830 vlc_object_attach( id->pp_vfilter[id->i_vfilter], p_stream );
1832 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_new =
1833 video_new_buffer_filter;
1834 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_del =
1835 video_del_buffer_filter;
1837 id->pp_vfilter[id->i_vfilter]->fmt_in = id->p_encoder->fmt_in;
1838 id->pp_vfilter[id->i_vfilter]->fmt_out = id->p_encoder->fmt_in;
1839 id->pp_vfilter[id->i_vfilter]->p_cfg = p_sys->p_vfilters_cfg[i];
1840 id->pp_vfilter[id->i_vfilter]->p_module =
1841 module_Need( id->pp_vfilter[id->i_vfilter],
1842 "video filter2", p_sys->psz_vfilters[i], 0 );
1843 if( id->pp_vfilter[id->i_vfilter]->p_module )
1845 id->pp_vfilter[id->i_vfilter]->p_owner =
1846 malloc( sizeof(filter_owner_sys_t) );
1847 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1848 id->pp_vfilter[id->i_vfilter]->p_owner->pp_pics[i] = 0;
1849 id->pp_vfilter[id->i_vfilter]->p_owner->p_sys = p_sys;
1855 msg_Dbg( p_stream, "no video filter found" );
1856 vlc_object_detach( id->pp_vfilter[id->i_vfilter] );
1857 vlc_object_destroy( id->pp_vfilter[id->i_vfilter] );
1862 /* Run filter chain */
1863 for( i = 0; i < id->i_filter; i++ )
1865 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1872 /* Check if we have a subpicture to overlay */
1875 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
1876 /* TODO: get another pic */
1879 /* Overlay subpicture */
1882 int i_scale_width, i_scale_height;
1883 video_format_t *p_fmt;
1885 i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
1886 id->p_decoder->fmt_out.video.i_width;
1887 i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
1888 id->p_decoder->fmt_out.video.i_height;
1890 if( p_pic->i_refcount && !id->i_filter )
1892 /* We can't modify the picture, we need to duplicate it */
1893 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1896 vout_CopyPicture( p_stream, p_tmp, p_pic );
1897 p_pic->pf_release( p_pic );
1903 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
1905 p_fmt = &id->p_decoder->fmt_out.video;
1907 /* FIXME (shouldn't have to be done here) */
1908 p_fmt->i_sar_num = p_fmt->i_aspect *
1909 p_fmt->i_height / p_fmt->i_width;
1910 p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
1912 spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
1913 i_scale_width, i_scale_height );
1916 /* Run vfilter chain */
1917 for( i = 0; i < id->i_vfilter; i++ )
1919 p_pic = id->pp_vfilter[i]->pf_video_filter(id->pp_vfilter[i], p_pic);
1922 if( p_sys->i_threads >= 1 )
1924 vlc_mutex_lock( &p_sys->lock_out );
1925 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1926 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1927 *out = p_sys->p_buffers;
1928 p_sys->p_buffers = NULL;
1929 vlc_cond_signal( &p_sys->cond );
1930 vlc_mutex_unlock( &p_sys->lock_out );
1935 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1936 block_ChainAppend( out, p_block );
1939 if( p_sys->b_master_sync )
1941 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1942 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
1943 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
1945 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1946 date_Set( &id->interpolated_pts, p_pic->date );
1947 i_pts = p_pic->date + 1;
1949 date_Increment( &id->interpolated_pts, 1 );
1952 if( p_sys->b_master_sync && i_duplicate > 1 )
1954 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1955 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
1956 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
1958 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1959 date_Set( &id->interpolated_pts, p_pic->date );
1960 i_pts = p_pic->date + 1;
1962 date_Increment( &id->interpolated_pts, 1 );
1964 if( p_sys->i_threads >= 1 )
1966 /* We can't modify the picture, we need to duplicate it */
1967 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1970 vout_CopyPicture( p_stream, p_tmp, p_pic );
1973 vlc_mutex_lock( &p_sys->lock_out );
1974 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1975 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1976 vlc_cond_signal( &p_sys->cond );
1977 vlc_mutex_unlock( &p_sys->lock_out );
1983 p_pic->date = i_pts;
1984 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1985 block_ChainAppend( out, p_block );
1989 if( p_sys->i_threads == 0 )
1990 p_pic->pf_release( p_pic );
1996 static int EncoderThread( sout_stream_sys_t *p_sys )
1998 sout_stream_id_t *id = p_sys->id_video;
2001 while( !p_sys->b_die && !p_sys->b_error )
2005 vlc_mutex_lock( &p_sys->lock_out );
2006 while( p_sys->i_last_pic == p_sys->i_first_pic )
2008 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
2009 if( p_sys->b_die || p_sys->b_error ) break;
2011 if( p_sys->b_die || p_sys->b_error )
2013 vlc_mutex_unlock( &p_sys->lock_out );
2017 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2018 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2019 vlc_mutex_unlock( &p_sys->lock_out );
2021 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2022 vlc_mutex_lock( &p_sys->lock_out );
2023 block_ChainAppend( &p_sys->p_buffers, p_block );
2025 vlc_mutex_unlock( &p_sys->lock_out );
2027 p_pic->pf_release( p_pic );
2030 while( p_sys->i_last_pic != p_sys->i_first_pic )
2032 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2033 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2035 p_pic->pf_release( p_pic );
2038 block_ChainRelease( p_sys->p_buffers );
2043 struct picture_sys_t
2045 vlc_object_t *p_owner;
2048 static void video_release_buffer( picture_t *p_pic )
2050 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
2052 video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
2054 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
2057 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
2058 sout_stream_sys_t *p_sys )
2060 decoder_t *p_dec = (decoder_t *)p_this;
2064 /* Find an empty space in the picture ring buffer */
2065 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2067 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2069 pp_ring[i]->i_status = RESERVED_PICTURE;
2073 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2075 if( pp_ring[i] == 0 ) break;
2078 if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
2080 int i_first_pic = p_sys->i_first_pic;
2082 if( p_sys->i_first_pic != p_sys->i_last_pic )
2084 /* Encoder still has stuff to encode, wait to clear-up the list */
2085 while( p_sys->i_first_pic == i_first_pic )
2089 /* Find an empty space in the picture ring buffer */
2090 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2092 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2094 pp_ring[i]->i_status = RESERVED_PICTURE;
2098 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2100 if( pp_ring[i] == 0 ) break;
2104 if( i == PICTURE_RING_SIZE )
2106 msg_Err( p_this, "decoder/filter is leaking pictures, "
2107 "resetting its ring buffer" );
2109 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2111 pp_ring[i]->pf_release( pp_ring[i] );
2117 p_pic = malloc( sizeof(picture_t) );
2118 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2119 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
2120 p_dec->fmt_out.video.i_chroma,
2121 p_dec->fmt_out.video.i_width,
2122 p_dec->fmt_out.video.i_height,
2123 p_dec->fmt_out.video.i_aspect );
2125 if( !p_pic->i_planes )
2131 p_pic->pf_release = video_release_buffer;
2132 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2133 p_pic->p_sys->p_owner = p_this;
2134 p_pic->i_status = RESERVED_PICTURE;
2141 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
2143 return video_new_buffer( VLC_OBJECT(p_dec),
2144 p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2147 static picture_t *video_new_buffer_filter( filter_t *p_filter )
2149 return video_new_buffer( VLC_OBJECT(p_filter),
2150 p_filter->p_owner->pp_pics,
2151 p_filter->p_owner->p_sys );
2154 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2156 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2157 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
2158 if( p_pic ) free( p_pic );
2161 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
2163 p_pic->i_refcount = 0;
2164 p_pic->i_status = DESTROYED_PICTURE;
2167 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
2169 p_pic->i_refcount = 0;
2170 p_pic->i_status = DESTROYED_PICTURE;
2173 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2175 p_pic->i_refcount++;
2178 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2180 video_release_buffer( p_pic );
2186 static subpicture_t *spu_new_buffer( decoder_t * );
2187 static void spu_del_buffer( decoder_t *, subpicture_t * );
2189 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2191 sout_stream_sys_t *p_sys = p_stream->p_sys;
2197 /* Initialization of decoder structures */
2198 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2199 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2200 id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2201 /* id->p_decoder->p_cfg = p_sys->p_spu_cfg; */
2203 id->p_decoder->p_module =
2204 module_Need( id->p_decoder, "decoder", "$codec", 0 );
2206 if( !id->p_decoder->p_module )
2208 msg_Err( p_stream, "cannot find decoder" );
2209 return VLC_EGENERIC;
2212 if( !p_sys->b_soverlay )
2218 /* Initialization of encoder format structures */
2219 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
2220 id->p_decoder->fmt_in.i_codec );
2222 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2224 id->p_encoder->p_module =
2225 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2227 if( !id->p_encoder->p_module )
2229 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2230 msg_Err( p_stream, "cannot find encoder" );
2231 return VLC_EGENERIC;
2237 p_sys->p_spu = spu_Create( p_stream );
2238 spu_Init( p_sys->p_spu );
2244 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2247 if( id->p_decoder->p_module )
2248 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2251 if( id->p_encoder->p_module )
2252 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2255 static int transcode_spu_process( sout_stream_t *p_stream,
2256 sout_stream_id_t *id,
2257 block_t *in, block_t **out )
2259 sout_stream_sys_t *p_sys = p_stream->p_sys;
2260 subpicture_t *p_subpic;
2263 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2264 if( !p_subpic ) return VLC_EGENERIC;
2266 if( p_sys->b_master_sync && p_sys->i_master_drift )
2268 p_subpic->i_start -= p_sys->i_master_drift;
2269 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2272 if( p_sys->b_soverlay )
2274 spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2280 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2281 spu_del_buffer( id->p_decoder, p_subpic );
2285 block_ChainAppend( out, p_block );
2290 return VLC_EGENERIC;
2293 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2295 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2296 return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2299 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2301 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2302 spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );
2308 static int transcode_osd_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2310 sout_stream_sys_t *p_sys = p_stream->p_sys;
2314 fmt.i_id = 0xbd1f; /* pid ?? */
2315 fmt.i_group = 3; /* pmt entry ?? */
2316 fmt.i_codec = VLC_FOURCC( 'Y', 'U', 'V', 'A' );
2317 fmt.psz_language = strdup( "osd" );
2319 id = malloc( sizeof( sout_stream_id_t ) );
2320 memset( id, 0, sizeof(sout_stream_id_t) );
2323 id->p_decoder = NULL;
2324 id->p_encoder = NULL;
2326 /* Create encoder object */
2327 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
2328 if( !id->p_encoder )
2330 msg_Err( p_stream, "out of memory" );
2333 vlc_object_attach( id->p_encoder, p_stream );
2334 id->p_encoder->p_module = NULL;
2336 /* Create fake destination format */
2337 es_format_Init( &id->p_encoder->fmt_out, fmt.i_cat, 0 );
2338 id->p_encoder->fmt_out.i_id = fmt.i_id;
2339 id->p_encoder->fmt_out.i_group = fmt.i_group;
2340 id->p_encoder->fmt_out.psz_language = strdup( fmt.psz_language );
2342 if( p_sys->i_osdcodec != 0 || p_sys->psz_osdenc )
2344 msg_Dbg( p_stream, "creating osdmenu transcoding from fcc=`%4.4s' "
2345 "to fcc=`%4.4s'", (char*)&fmt.i_codec,
2346 (char*)&p_sys->i_osdcodec );
2348 /* Complete destination format */
2349 id->p_encoder->fmt_out.i_codec = p_sys->i_osdcodec;
2355 /* Initialization of encoder format structures */
2356 es_format_Init( &id->p_encoder->fmt_in, fmt.i_cat, fmt.i_codec );
2357 id->p_encoder->fmt_in.psz_language = strdup( fmt.psz_language );
2359 id->p_encoder->p_cfg = p_sys->p_osd_cfg;
2361 id->p_encoder->p_module =
2362 module_Need( id->p_encoder, "encoder", p_sys->psz_osdenc, VLC_TRUE );
2364 if( !id->p_encoder->p_module )
2366 msg_Err( p_stream, "cannot find encoder" );
2370 /* open output stream */
2371 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
2372 id->b_transcode = VLC_TRUE;
2374 if( !id->id ) goto error;
2378 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
2379 (char*)&fmt.i_codec );
2380 id->id = p_sys->p_out->pf_add( p_sys->p_out, &fmt );
2381 id->b_transcode = VLC_FALSE;
2383 if( !id->id ) goto error;
2387 p_sys->b_osd = VLC_TRUE;
2391 p_sys->p_spu = spu_Create( p_stream );
2392 if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
2393 msg_Err( p_sys, "spu initialisation failed" );
2396 if( fmt.psz_language )
2397 free( fmt.psz_language );
2402 msg_Err( p_stream, "starting osd encoding thread failed" );
2403 if( id->p_encoder->p_module )
2404 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2407 vlc_object_detach( id->p_encoder );
2408 vlc_object_destroy( id->p_encoder );
2410 if( fmt.psz_language ) free( fmt.psz_language );
2411 if( id ) free( id );
2412 p_sys->id_osd = NULL;
2413 p_sys->b_osd = VLC_FALSE;
2414 return VLC_EGENERIC;
2417 static void transcode_osd_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2419 sout_stream_sys_t *p_sys = p_stream->p_sys;
2422 if( p_sys->b_osd && id )
2424 if( id->p_encoder->p_module )
2425 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2427 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
2431 vlc_object_detach( id->p_encoder );
2432 vlc_object_destroy( id->p_encoder );
2435 p_sys->b_osd = VLC_FALSE;
2436 if( id ) free( id );
2439 static int transcode_osd_process( sout_stream_t *p_stream,
2440 sout_stream_id_t *id,
2441 block_t *in, block_t **out )
2443 sout_stream_sys_t *p_sys = p_stream->p_sys;
2444 subpicture_t *p_subpic = NULL;
2446 /* Check if we have a subpicture to send */
2447 if( p_sys->p_spu && in->i_dts > 0)
2449 p_subpic = spu_SortSubpictures( p_sys->p_spu, in->i_dts );
2453 msg_Warn( p_stream, "spu channel not initialized, doing it now" );
2456 p_sys->p_spu = spu_Create( p_stream );
2457 if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
2458 msg_Err( p_stream, "spu initialisation failed" );
2464 block_t *p_block = NULL;
2466 if( p_sys->b_master_sync && p_sys->i_master_drift )
2468 p_subpic->i_start -= p_sys->i_master_drift;
2469 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2472 p_block = p_sys->id_osd->p_encoder->pf_encode_sub( p_sys->id_osd->p_encoder, p_subpic );
2475 p_block->i_dts = p_block->i_pts = in->i_dts;
2476 block_ChainAppend( out, p_block );
2479 if( p_sys->p_out->pf_send( p_sys->p_out, p_sys->id_osd->id, *out ) == VLC_SUCCESS )
2480 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
2485 return VLC_EGENERIC;