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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 "This is the video encoder module that will be used (and its associated "\
50 #define VCODEC_TEXT N_("Destination video codec")
51 #define VCODEC_LONGTEXT N_( \
52 "This is the video codec that will be used.")
53 #define VB_TEXT N_("Video bitrate")
54 #define VB_LONGTEXT N_( \
55 "Target bitrate of the transcoded video stream." )
56 #define SCALE_TEXT N_("Video scaling")
57 #define SCALE_LONGTEXT N_( \
58 "Scale factor to apply to the video while transcoding (eg: 0.25)")
59 #define FPS_TEXT N_("Video frame-rate")
60 #define FPS_LONGTEXT N_( \
61 "Target output frame rate for the video stream." )
62 #define DEINTERLACE_TEXT N_("Deinterlace video")
63 #define DEINTERLACE_LONGTEXT N_( \
64 "Deinterlace the video before encoding." )
65 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
66 #define DEINTERLACE_MODULE_LONGTEXT N_( \
67 "Specify the deinterlace module to use." )
68 #define WIDTH_TEXT N_("Video width")
69 #define WIDTH_LONGTEXT N_( \
70 "Output video width." )
71 #define HEIGHT_TEXT N_("Video height")
72 #define HEIGHT_LONGTEXT N_( \
73 "Output video height." )
74 #define MAXWIDTH_TEXT N_("Maximum video width")
75 #define MAXWIDTH_LONGTEXT N_( \
76 "Maximum output video width." )
77 #define MAXHEIGHT_TEXT N_("Maximum video height")
78 #define MAXHEIGHT_LONGTEXT N_( \
79 "Maximum output video height." )
80 #define VFILTER_TEXT N_("Video filter")
81 #define VFILTER_LONGTEXT N_( \
82 "Video filters will be applied to the video streams (after overlays " \
83 "are applied). You must enter a comma-separated list of filters." )
85 #define CROPTOP_TEXT N_("Video crop (top)")
86 #define CROPTOP_LONGTEXT N_( \
87 "Number of pixels to crop at the top of the video." )
88 #define CROPLEFT_TEXT N_("Video crop (left)")
89 #define CROPLEFT_LONGTEXT N_( \
90 "Number of pixels to crop at the left of the video." )
91 #define CROPBOTTOM_TEXT N_("Video crop (bottom)")
92 #define CROPBOTTOM_LONGTEXT N_( \
93 "Number of pixels to crop at the bottom of the video." )
94 #define CROPRIGHT_TEXT N_("Video crop (right)")
95 #define CROPRIGHT_LONGTEXT N_( \
96 "Number of pixels to crop at the right of the video." )
98 #define PADDTOP_TEXT N_("Video padding (top)")
99 #define PADDTOP_LONGTEXT N_( \
100 "Size of the black border to add at the top of the video." )
101 #define PADDLEFT_TEXT N_("Video padding (left)")
102 #define PADDLEFT_LONGTEXT N_( \
103 "Size of the black border to add at the left of the video." )
104 #define PADDBOTTOM_TEXT N_("Video padding (bottom)")
105 #define PADDBOTTOM_LONGTEXT N_( \
106 "Size of the black border to add at the bottom of the video." )
107 #define PADDRIGHT_TEXT N_("Video padding (right)")
108 #define PADDRIGHT_LONGTEXT N_( \
109 "Size of the black border to add at the right of the video." )
111 #define CANVAS_WIDTH_TEXT N_("Video canvas width")
112 #define CANVAS_WIDTH_LONGTEXT N_( \
113 "This will automatically crod and pad the video to a specified width." )
114 #define CANVAS_HEIGHT_TEXT N_("Video canvas height")
115 #define CANVAS_HEIGHT_LONGTEXT N_( \
116 "This will automatically crod and pad the video to a specified height." )
117 #define CANVAS_ASPECT_TEXT N_("Video canvas aspect ratio")
118 #define CANVAS_ASPECT_LONGTEXT N_( \
119 "This sets aspect (like 4:3) of the video canvas and letterbox the video "\
122 #define AENC_TEXT N_("Audio encoder")
123 #define AENC_LONGTEXT N_( \
124 "This is the audio encoder module that will be used (and its associated "\
126 #define ACODEC_TEXT N_("Destination audio codec")
127 #define ACODEC_LONGTEXT N_( \
128 "This is the audio codec that will be used.")
129 #define AB_TEXT N_("Audio bitrate")
130 #define AB_LONGTEXT N_( \
131 "Target bitrate of the transcoded audio stream." )
132 #define ARATE_TEXT N_("Audio sample rate")
133 #define ARATE_LONGTEXT N_( \
134 "Sample rate of the transcoded audio stream (11250, 22500, 44100 or 48000).")
135 #define ACHANS_TEXT N_("Audio channels")
136 #define ACHANS_LONGTEXT N_( \
137 "Number of audio channels in the transcoded streams." )
138 #define AFILTER_TEXT N_("Audio filter")
139 #define AFILTER_LONGTEXT N_( \
140 "Audio filters will be applied to the audio streams (after conversion " \
141 "filters are applied). You must enter a comma-separated list of filters." )
143 #define SENC_TEXT N_("Subtitles encoder")
144 #define SENC_LONGTEXT N_( \
145 "This is the subtitles encoder module that will be used (and its " \
146 "associated options)." )
147 #define SCODEC_TEXT N_("Destination subtitles codec")
148 #define SCODEC_LONGTEXT N_( \
149 "This is the subtitles coded that will be used." )
151 #define SFILTER_TEXT N_("Overlays")
152 #define SFILTER_LONGTEXT N_( \
153 "This allows you to add overlays (also known as \"subpictures\" on the "\
154 "transcoded video stream. The subpictures produced by the filters will "\
155 "be overlayed directly onto the video. You must specify a comma-separated "\
156 "list of subpicture modules" )
158 #define OSD_TEXT N_("OSD menu")
159 #define OSD_LONGTEXT N_(\
160 "Stream the On Screen Display menu (using the osdmenu subpicture module)." )
162 #define THREADS_TEXT N_("Number of threads")
163 #define THREADS_LONGTEXT N_( \
164 "Number of threads used for the transcoding." )
165 #define HP_TEXT N_("High priority")
166 #define HP_LONGTEXT N_( \
167 "Runs the optional encoder thread at the OUTPUT priority instead of " \
170 #define ASYNC_TEXT N_("Synchronise on audio track")
171 #define ASYNC_LONGTEXT N_( \
172 "This option will drop/duplicate video frames to synchronise the video " \
173 "track on the audio track." )
175 #define HURRYUP_TEXT N_( "Hurry up" )
176 #define HURRYUP_LONGTEXT N_( "The transcoder will drop frames if your CPU " \
177 "can't keep up with the encoding rate." )
179 static char *ppsz_deinterlace_type[] =
181 "deinterlace", "ffmpeg-deinterlace"
184 static int Open ( vlc_object_t * );
185 static void Close( vlc_object_t * );
187 #define SOUT_CFG_PREFIX "sout-transcode-"
190 set_shortname( _("Transcode"));
191 set_description( _("Transcode stream output") );
192 set_capability( "sout stream", 50 );
193 add_shortcut( "transcode" );
194 set_callbacks( Open, Close );
195 set_category( CAT_SOUT );
196 set_subcategory( SUBCAT_SOUT_STREAM );
197 set_section( N_("Video"), NULL );
198 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
199 VENC_LONGTEXT, VLC_FALSE );
200 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
201 VCODEC_LONGTEXT, VLC_FALSE );
202 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
203 VB_LONGTEXT, VLC_FALSE );
204 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
205 SCALE_LONGTEXT, VLC_FALSE );
206 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
207 FPS_LONGTEXT, VLC_FALSE );
208 add_bool( SOUT_CFG_PREFIX "hurry-up", VLC_TRUE, NULL, HURRYUP_TEXT,
209 HURRYUP_LONGTEXT, VLC_FALSE );
210 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
211 DEINTERLACE_LONGTEXT, VLC_FALSE );
212 add_string( SOUT_CFG_PREFIX "deinterlace-module", "deinterlace", NULL,
213 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
215 change_string_list( ppsz_deinterlace_type, 0, 0 );
216 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
217 WIDTH_LONGTEXT, VLC_TRUE );
218 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
219 HEIGHT_LONGTEXT, VLC_TRUE );
220 add_integer( SOUT_CFG_PREFIX "maxwidth", 0, NULL, MAXWIDTH_TEXT,
221 MAXWIDTH_LONGTEXT, VLC_TRUE );
222 add_integer( SOUT_CFG_PREFIX "maxheight", 0, NULL, MAXHEIGHT_TEXT,
223 MAXHEIGHT_LONGTEXT, VLC_TRUE );
224 add_module_list_cat( SOUT_CFG_PREFIX "vfilter", SUBCAT_VIDEO_VFILTER2,
226 VFILTER_TEXT, VFILTER_LONGTEXT, VLC_FALSE );
228 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
229 CROPTOP_LONGTEXT, VLC_TRUE );
230 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
231 CROPLEFT_LONGTEXT, VLC_TRUE );
232 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
233 CROPBOTTOM_LONGTEXT, VLC_TRUE );
234 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
235 CROPRIGHT_LONGTEXT, VLC_TRUE );
237 add_integer( SOUT_CFG_PREFIX "paddtop", 0, NULL, PADDTOP_TEXT,
238 PADDTOP_LONGTEXT, VLC_TRUE );
239 add_integer( SOUT_CFG_PREFIX "paddleft", 0, NULL, PADDLEFT_TEXT,
240 PADDLEFT_LONGTEXT, VLC_TRUE );
241 add_integer( SOUT_CFG_PREFIX "paddbottom", 0, NULL, PADDBOTTOM_TEXT,
242 PADDBOTTOM_LONGTEXT, VLC_TRUE );
243 add_integer( SOUT_CFG_PREFIX "paddright", 0, NULL, PADDRIGHT_TEXT,
244 PADDRIGHT_LONGTEXT, VLC_TRUE );
246 add_integer( SOUT_CFG_PREFIX "canvas-width", 0, NULL, CANVAS_WIDTH_TEXT,
247 CANVAS_WIDTH_LONGTEXT, VLC_TRUE );
248 add_integer( SOUT_CFG_PREFIX "canvas-height", 0, NULL, CANVAS_HEIGHT_TEXT,
249 CANVAS_HEIGHT_LONGTEXT, VLC_TRUE );
250 add_string( SOUT_CFG_PREFIX "canvas-aspect", NULL, NULL, CANVAS_ASPECT_TEXT,
251 CANVAS_ASPECT_LONGTEXT, VLC_FALSE );
253 set_section( N_("Audio"), NULL );
254 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
255 AENC_LONGTEXT, VLC_FALSE );
256 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
257 ACODEC_LONGTEXT, VLC_FALSE );
258 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
259 AB_LONGTEXT, VLC_FALSE );
260 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
261 ACHANS_LONGTEXT, VLC_FALSE );
262 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
263 ARATE_LONGTEXT, VLC_TRUE );
264 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
265 ASYNC_LONGTEXT, VLC_FALSE );
266 add_module_list_cat( SOUT_CFG_PREFIX "afilter", SUBCAT_AUDIO_MISC,
268 AFILTER_TEXT, AFILTER_LONGTEXT, VLC_FALSE );
270 set_section( N_("Overlays/Subtitles"), NULL );
271 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
272 SENC_LONGTEXT, VLC_FALSE );
273 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
274 SCODEC_LONGTEXT, VLC_FALSE );
275 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
276 SCODEC_LONGTEXT, VLC_FALSE );
277 add_module_list_cat( SOUT_CFG_PREFIX "sfilter", SUBCAT_VIDEO_SUBPIC,
279 SFILTER_TEXT, SFILTER_LONGTEXT, VLC_FALSE );
281 set_section( N_("On Screen Display"), NULL );
282 add_bool( SOUT_CFG_PREFIX "osd", 0, NULL, OSD_TEXT,
283 OSD_LONGTEXT, VLC_FALSE );
285 set_section( N_("Miscellaneous"), NULL );
286 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
287 THREADS_LONGTEXT, VLC_TRUE );
288 add_bool( SOUT_CFG_PREFIX "high-priority", 0, NULL, HP_TEXT, HP_LONGTEXT,
293 static const char *ppsz_sout_options[] = {
294 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
295 "paddtop", "paddbottom", "paddleft", "paddright",
296 "canvas-width", "canvas-height", "canvas-aspect",
297 "scale", "fps", "width", "height", "vfilter", "deinterlace",
298 "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab",
299 "afilter", "samplerate", "channels", "senc", "scodec", "soverlay",
300 "sfilter", "osd", "audio-sync", "high-priority", "maxwidth", "maxheight",
304 /*****************************************************************************
305 * Exported prototypes
306 *****************************************************************************/
307 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
308 static int Del ( sout_stream_t *, sout_stream_id_t * );
309 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
311 static int transcode_audio_new ( sout_stream_t *, sout_stream_id_t * );
312 static void transcode_audio_close ( sout_stream_t *, sout_stream_id_t * );
313 static int transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
314 block_t *, block_t ** );
316 static aout_buffer_t *audio_new_buffer( decoder_t *, int );
317 static void audio_del_buffer( decoder_t *, aout_buffer_t * );
319 static int transcode_video_new ( sout_stream_t *, sout_stream_id_t * );
320 static void transcode_video_close ( sout_stream_t *, sout_stream_id_t * );
321 static int transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
322 static int transcode_video_process( sout_stream_t *, sout_stream_id_t *,
323 block_t *, block_t ** );
325 static void video_del_buffer( vlc_object_t *, picture_t * );
326 static picture_t *video_new_buffer_decoder( decoder_t * );
327 static void video_del_buffer_decoder( decoder_t *, picture_t * );
328 static void video_link_picture_decoder( decoder_t *, picture_t * );
329 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
330 static picture_t *video_new_buffer_filter( filter_t * );
331 static void video_del_buffer_filter( filter_t *, picture_t * );
333 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
334 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
335 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
336 block_t *, block_t ** );
338 static int transcode_osd_new ( sout_stream_t *, sout_stream_id_t * );
339 static void transcode_osd_close ( sout_stream_t *, sout_stream_id_t * );
340 static int transcode_osd_process( sout_stream_t *, sout_stream_id_t *,
341 block_t *, block_t ** );
343 static int EncoderThread( struct sout_stream_sys_t * p_sys );
345 static int pi_channels_maps[6] =
348 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
349 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
350 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
351 | AOUT_CHAN_REARRIGHT,
352 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
353 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
356 #define PICTURE_RING_SIZE 64
357 #define SUBPICTURE_RING_SIZE 20
358 #define TRANSCODE_FILTERS 10
360 struct sout_stream_sys_t
364 sout_stream_t *p_out;
365 sout_stream_id_t *id_video;
367 vlc_mutex_t lock_out;
369 picture_t * pp_pics[PICTURE_RING_SIZE];
370 int i_first_pic, i_last_pic;
373 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
375 sout_cfg_t *p_audio_cfg;
379 char *psz_afilters[TRANSCODE_FILTERS];
380 sout_cfg_t *p_afilters_cfg[TRANSCODE_FILTERS];
384 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
386 sout_cfg_t *p_video_cfg;
390 unsigned int i_width, i_maxwidth;
391 unsigned int i_height, i_maxheight;
392 vlc_bool_t b_deinterlace;
393 char *psz_deinterlace;
394 sout_cfg_t *p_deinterlace_cfg;
396 vlc_bool_t b_high_priority;
397 vlc_bool_t b_hurry_up;
398 char *psz_vfilters[TRANSCODE_FILTERS];
399 sout_cfg_t *p_vfilters_cfg[TRANSCODE_FILTERS];
416 /* Video, calculated */
428 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
430 vlc_bool_t b_soverlay;
431 sout_cfg_t *p_spu_cfg;
435 sout_stream_id_t *id_osd; /* extension for streaming OSD menus */
436 vlc_fourcc_t i_osdcodec; /* codec osd menu (0 if not transcode) */
438 sout_cfg_t *p_osd_cfg;
439 vlc_bool_t b_es_osd; /* VLC_TRUE when osd es is registered */
440 vlc_bool_t b_sout_osd;
443 vlc_bool_t b_master_sync;
444 mtime_t i_master_drift;
447 struct decoder_owner_sys_t
449 picture_t *pp_pics[PICTURE_RING_SIZE];
450 sout_stream_sys_t *p_sys;
452 struct filter_owner_sys_t
454 picture_t *pp_pics[PICTURE_RING_SIZE];
455 sout_stream_sys_t *p_sys;
458 /*****************************************************************************
460 *****************************************************************************/
461 static int Open( vlc_object_t *p_this )
463 sout_stream_t *p_stream = (sout_stream_t*)p_this;
464 sout_stream_sys_t *p_sys;
467 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
469 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
472 msg_Err( p_stream, "cannot create chain" );
473 vlc_object_destroy( p_sys );
477 p_sys->i_master_drift = 0;
479 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
482 /* Audio transcoding parameters */
483 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
484 p_sys->psz_aenc = NULL;
485 p_sys->p_audio_cfg = NULL;
486 if( val.psz_string && *val.psz_string )
489 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
491 if( psz_next ) free( psz_next );
493 if( val.psz_string ) free( val.psz_string );
495 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
497 if( val.psz_string && *val.psz_string )
500 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
501 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
503 if( val.psz_string ) free( val.psz_string );
505 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
506 p_sys->i_abitrate = val.i_int;
507 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
509 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
510 p_sys->i_sample_rate = val.i_int;
512 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
513 p_sys->i_channels = val.i_int;
515 if( p_sys->i_acodec )
517 if( p_sys->i_acodec == VLC_FOURCC('m','p','3',0) &&
518 p_sys->i_channels > 2 )
520 msg_Warn( p_stream, "%d channels invalid for mp3, forcing to 2",
522 p_sys->i_channels = 2;
524 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
525 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
526 p_sys->i_channels, p_sys->i_abitrate / 1000 );
529 var_Get( p_stream, SOUT_CFG_PREFIX "afilter", &val );
530 p_sys->i_afilters = 0;
531 if( val.psz_string && *val.psz_string )
533 char *psz_parser = val.psz_string;
535 while( (psz_parser != NULL) && (*psz_parser != '\0')
536 && (p_sys->i_afilters < TRANSCODE_FILTERS) )
538 psz_parser = sout_CfgCreate(
539 &p_sys->psz_afilters[p_sys->i_afilters],
540 &p_sys->p_afilters_cfg[p_sys->i_afilters],
543 if( (psz_parser != NULL) && (*psz_parser != '\0') ) psz_parser++;
546 if( val.psz_string ) free( val.psz_string );
547 if( p_sys->i_afilters < TRANSCODE_FILTERS-1 )
549 p_sys->psz_afilters[p_sys->i_afilters] = NULL;
550 p_sys->p_afilters_cfg[p_sys->i_afilters] = NULL;
553 /* Video transcoding parameters */
554 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
555 p_sys->psz_venc = NULL;
556 p_sys->p_video_cfg = NULL;
557 if( val.psz_string && *val.psz_string )
560 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
562 if( psz_next ) free( psz_next );
564 if( val.psz_string ) free( val.psz_string );
566 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
568 if( val.psz_string && *val.psz_string )
571 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
572 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
574 if( val.psz_string ) free( val.psz_string );
576 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
577 p_sys->i_vbitrate = val.i_int;
578 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
580 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
581 p_sys->f_scale = val.f_float;
583 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
584 p_sys->f_fps = val.f_float;
586 var_Get( p_stream, SOUT_CFG_PREFIX "hurry-up", &val );
587 p_sys->b_hurry_up = val.b_bool;
589 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
590 p_sys->i_width = val.i_int;
592 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
593 p_sys->i_height = val.i_int;
595 var_Get( p_stream, SOUT_CFG_PREFIX "maxwidth", &val );
596 p_sys->i_maxwidth = val.i_int;
598 var_Get( p_stream, SOUT_CFG_PREFIX "maxheight", &val );
599 p_sys->i_maxheight = val.i_int;
601 var_Get( p_stream, SOUT_CFG_PREFIX "vfilter", &val );
602 p_sys->i_vfilters = 0;
603 if( val.psz_string && *val.psz_string )
605 char *psz_parser = val.psz_string;
607 while( (psz_parser != NULL) && (*psz_parser != '\0')
608 && (p_sys->i_vfilters < TRANSCODE_FILTERS) )
610 psz_parser = sout_CfgCreate(
611 &p_sys->psz_vfilters[p_sys->i_vfilters],
612 &p_sys->p_vfilters_cfg[p_sys->i_vfilters],
615 if( psz_parser != NULL && *psz_parser != '\0' ) psz_parser++;
618 if( val.psz_string ) free( val.psz_string );
619 if( p_sys->i_vfilters < TRANSCODE_FILTERS-1 )
621 p_sys->psz_vfilters[p_sys->i_vfilters] = NULL;
622 p_sys->p_vfilters_cfg[p_sys->i_vfilters] = NULL;
625 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
626 p_sys->b_deinterlace = val.b_bool;
628 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace-module", &val );
629 p_sys->psz_deinterlace = NULL;
630 p_sys->p_deinterlace_cfg = NULL;
631 if( val.psz_string && *val.psz_string )
634 psz_next = sout_CfgCreate( &p_sys->psz_deinterlace,
635 &p_sys->p_deinterlace_cfg,
637 if( psz_next ) free( psz_next );
639 if( val.psz_string ) free( val.psz_string );
641 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
642 p_sys->i_crop_top = val.i_int;
643 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
644 p_sys->i_crop_bottom = val.i_int;
645 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
646 p_sys->i_crop_left = val.i_int;
647 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
648 p_sys->i_crop_right = val.i_int;
650 var_Get( p_stream, SOUT_CFG_PREFIX "paddtop", &val );
651 p_sys->i_padd_top = val.i_int;
652 var_Get( p_stream, SOUT_CFG_PREFIX "paddbottom", &val );
653 p_sys->i_padd_bottom = val.i_int;
654 var_Get( p_stream, SOUT_CFG_PREFIX "paddleft", &val );
655 p_sys->i_padd_left = val.i_int;
656 var_Get( p_stream, SOUT_CFG_PREFIX "paddright", &val );
657 p_sys->i_padd_right = val.i_int;
659 var_Get( p_stream, SOUT_CFG_PREFIX "canvas-width", &val );
660 p_sys->i_canvas_width = val.i_int;
661 var_Get( p_stream, SOUT_CFG_PREFIX "canvas-height", &val );
662 p_sys->i_canvas_height = val.i_int;
664 var_Get( p_stream, SOUT_CFG_PREFIX "canvas-aspect", &val );
665 p_sys->i_canvas_aspect = 0;
666 if( val.psz_string && *val.psz_string )
668 char *psz_parser = strchr( val.psz_string, ':' );
671 *psz_parser++ = '\0';
672 p_sys->i_canvas_aspect = atoi( val.psz_string ) *
673 VOUT_ASPECT_FACTOR / atoi( psz_parser );
675 else msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
678 if( val.psz_string ) free( val.psz_string );
680 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
681 p_sys->i_threads = val.i_int;
682 var_Get( p_stream, SOUT_CFG_PREFIX "high-priority", &val );
683 p_sys->b_high_priority = val.b_bool;
685 if( p_sys->i_vcodec )
687 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
688 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
689 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
692 /* Subpictures transcoding parameters */
694 p_sys->psz_senc = NULL;
695 p_sys->p_spu_cfg = NULL;
698 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
699 if( val.psz_string && *val.psz_string )
702 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
704 if( psz_next ) free( psz_next );
706 if( val.psz_string ) free( val.psz_string );
708 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
709 if( val.psz_string && *val.psz_string )
712 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
713 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
715 if( val.psz_string ) free( val.psz_string );
717 if( p_sys->i_scodec )
719 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
722 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
723 p_sys->b_soverlay = val.b_bool;
725 var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
726 if( val.psz_string && *val.psz_string )
728 p_sys->p_spu = spu_Create( p_stream );
729 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
730 var_Set( p_sys->p_spu, "sub-filter", val );
731 spu_Init( p_sys->p_spu );
733 if( val.psz_string ) free( val.psz_string );
735 /* OSD menu transcoding parameters */
736 p_sys->psz_osdenc = NULL;
737 p_sys->p_osd_cfg = NULL;
738 p_sys->i_osdcodec = 0;
739 p_sys->b_es_osd = VLC_FALSE;
741 var_Get( p_stream, SOUT_CFG_PREFIX "osd", &val );
742 p_sys->b_sout_osd = val.b_bool;
743 if( p_sys->b_sout_osd )
748 psz_next = sout_CfgCreate( &p_sys->psz_osdenc,
749 &p_sys->p_osd_cfg, strdup( "dvbsub") );
750 if( psz_next ) free( psz_next );
752 p_sys->i_osdcodec = VLC_FOURCC('Y','U','V','P' );
754 msg_Dbg( p_stream, "codec osd=%4.4s", (char *)&p_sys->i_osdcodec );
758 osd_val.psz_string = strdup("osdmenu");
759 p_sys->p_spu = spu_Create( p_stream );
760 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
761 var_Set( p_sys->p_spu, "sub-filter", osd_val );
762 spu_Init( p_sys->p_spu );
763 if( osd_val.psz_string ) free( osd_val.psz_string );
767 osd_val.psz_string = strdup("osdmenu");
768 var_Set( p_sys->p_spu, "sub-filter", osd_val );
769 if( osd_val.psz_string ) free( osd_val.psz_string );
774 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
775 p_sys->b_master_sync = val.b_bool;
776 if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
778 p_stream->pf_add = Add;
779 p_stream->pf_del = Del;
780 p_stream->pf_send = Send;
781 p_stream->p_sys = p_sys;
786 /*****************************************************************************
788 *****************************************************************************/
789 static void Close( vlc_object_t * p_this )
791 sout_stream_t *p_stream = (sout_stream_t*)p_this;
792 sout_stream_sys_t *p_sys = p_stream->p_sys;
794 sout_StreamDelete( p_sys->p_out );
796 while( p_sys->p_audio_cfg != NULL )
798 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
800 if( p_sys->p_audio_cfg->psz_name )
801 free( p_sys->p_audio_cfg->psz_name );
802 if( p_sys->p_audio_cfg->psz_value )
803 free( p_sys->p_audio_cfg->psz_value );
804 free( p_sys->p_audio_cfg );
806 p_sys->p_audio_cfg = p_next;
808 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
810 while( p_sys->p_video_cfg != NULL )
812 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
814 if( p_sys->p_video_cfg->psz_name )
815 free( p_sys->p_video_cfg->psz_name );
816 if( p_sys->p_video_cfg->psz_value )
817 free( p_sys->p_video_cfg->psz_value );
818 free( p_sys->p_video_cfg );
820 p_sys->p_video_cfg = p_next;
822 if( p_sys->psz_venc ) free( p_sys->psz_venc );
824 while( p_sys->p_deinterlace_cfg != NULL )
826 sout_cfg_t *p_next = p_sys->p_deinterlace_cfg->p_next;
828 if( p_sys->p_deinterlace_cfg->psz_name )
829 free( p_sys->p_deinterlace_cfg->psz_name );
830 if( p_sys->p_deinterlace_cfg->psz_value )
831 free( p_sys->p_deinterlace_cfg->psz_value );
832 free( p_sys->p_deinterlace_cfg );
834 p_sys->p_deinterlace_cfg = p_next;
836 if( p_sys->psz_deinterlace ) free( p_sys->psz_deinterlace );
838 while( p_sys->p_spu_cfg != NULL )
840 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
842 if( p_sys->p_spu_cfg->psz_name )
843 free( p_sys->p_spu_cfg->psz_name );
844 if( p_sys->p_spu_cfg->psz_value )
845 free( p_sys->p_spu_cfg->psz_value );
846 free( p_sys->p_spu_cfg );
848 p_sys->p_spu_cfg = p_next;
850 if( p_sys->psz_senc ) free( p_sys->psz_senc );
852 if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
854 while( p_sys->p_osd_cfg != NULL )
856 sout_cfg_t *p_next = p_sys->p_osd_cfg->p_next;
858 if( p_sys->p_osd_cfg->psz_name )
859 free( p_sys->p_osd_cfg->psz_name );
860 if( p_sys->p_osd_cfg->psz_value )
861 free( p_sys->p_osd_cfg->psz_value );
862 free( p_sys->p_osd_cfg );
864 p_sys->p_osd_cfg = p_next;
866 if( p_sys->psz_osdenc ) free( p_sys->psz_osdenc );
868 vlc_object_destroy( p_sys );
871 struct sout_stream_id_t
873 vlc_fourcc_t b_transcode;
875 /* id of the out stream */
879 decoder_t *p_decoder;
882 filter_t *pp_filter[TRANSCODE_FILTERS];
884 /* User specified filters */
885 filter_t *pp_ufilter[TRANSCODE_FILTERS];
889 encoder_t *p_encoder;
892 date_t interpolated_pts;
895 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
897 sout_stream_sys_t *p_sys = p_stream->p_sys;
898 sout_stream_id_t *id;
900 id = malloc( sizeof( sout_stream_id_t ) );
901 memset( id, 0, sizeof(sout_stream_id_t) );
904 id->p_decoder = NULL;
905 id->p_encoder = NULL;
907 /* Create decoder object */
908 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
911 msg_Err( p_stream, "out of memory" );
914 vlc_object_attach( id->p_decoder, p_stream );
915 id->p_decoder->p_module = NULL;
916 id->p_decoder->fmt_in = *p_fmt;
917 id->p_decoder->b_pace_control = VLC_TRUE;
919 /* Create encoder object */
920 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
923 msg_Err( p_stream, "out of memory" );
926 vlc_object_attach( id->p_encoder, p_stream );
927 id->p_encoder->p_module = NULL;
929 /* Create destination format */
930 es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
931 id->p_encoder->fmt_out.i_id = p_fmt->i_id;
932 id->p_encoder->fmt_out.i_group = p_fmt->i_group;
933 if( p_fmt->psz_language )
934 id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
936 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
939 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
940 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
942 /* Complete destination format */
943 id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
944 id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
945 p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
946 id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
947 id->p_encoder->fmt_out.audio.i_bitspersample =
948 p_fmt->audio.i_bitspersample;
949 id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
950 p_sys->i_channels : p_fmt->audio.i_channels;
951 /* Sanity check for audio channels */
952 id->p_encoder->fmt_out.audio.i_channels =
953 __MIN( id->p_encoder->fmt_out.audio.i_channels,
954 id->p_decoder->fmt_in.audio.i_channels );
955 id->p_encoder->fmt_out.audio.i_original_channels =
956 id->p_decoder->fmt_in.audio.i_physical_channels;
957 if( id->p_decoder->fmt_in.audio.i_channels ==
958 id->p_encoder->fmt_out.audio.i_channels )
960 id->p_encoder->fmt_out.audio.i_physical_channels =
961 id->p_decoder->fmt_in.audio.i_physical_channels;
965 id->p_encoder->fmt_out.audio.i_physical_channels =
966 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
969 /* Build decoder -> filter -> encoder chain */
970 if( transcode_audio_new( p_stream, id ) )
972 msg_Err( p_stream, "cannot create audio chain" );
976 /* Open output stream */
977 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
978 id->b_transcode = VLC_TRUE;
982 transcode_audio_close( p_stream, id );
986 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
988 else if( p_fmt->i_cat == VIDEO_ES &&
989 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
992 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
993 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
995 /* Complete destination format */
996 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
997 id->p_encoder->fmt_out.video.i_width = p_sys->i_width & ~1;
998 id->p_encoder->fmt_out.video.i_height = p_sys->i_height & ~1;
999 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
1001 /* Build decoder -> filter -> encoder chain */
1002 if( transcode_video_new( p_stream, id ) )
1004 msg_Err( p_stream, "cannot create video chain" );
1008 /* Stream will be added later on because we don't know
1009 * all the characteristics of the decoded stream yet */
1010 id->b_transcode = VLC_TRUE;
1012 if( p_sys->f_fps > 0 )
1014 id->p_encoder->fmt_out.video.i_frame_rate =
1015 (p_sys->f_fps * 1001) + 0.5;
1016 id->p_encoder->fmt_out.video.i_frame_rate_base = 1001;
1019 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
1021 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
1022 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
1023 (char*)&p_sys->i_scodec );
1025 /* Complete destination format */
1026 id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
1028 /* build decoder -> filter -> encoder */
1029 if( transcode_spu_new( p_stream, id ) )
1031 msg_Err( p_stream, "cannot create subtitles chain" );
1035 /* open output stream */
1036 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
1037 id->b_transcode = VLC_TRUE;
1041 transcode_spu_close( p_stream, id );
1045 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
1047 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
1048 (char*)&p_fmt->i_codec );
1050 id->b_transcode = VLC_TRUE;
1052 /* Build decoder -> filter -> overlaying chain */
1053 if( transcode_spu_new( p_stream, id ) )
1055 msg_Err( p_stream, "cannot create subtitles chain" );
1061 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
1062 (char*)&p_fmt->i_codec );
1063 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
1064 id->b_transcode = VLC_FALSE;
1066 if( !id->id ) goto error;
1069 if( p_sys->b_sout_osd )
1071 /* Create a fake OSD menu elementary stream */
1072 if( !p_sys->b_es_osd && (p_sys->i_osdcodec != 0 || p_sys->psz_osdenc) )
1074 if( transcode_osd_new( p_stream, p_sys->id_osd ) )
1076 msg_Err( p_stream, "cannot create osd chain" );
1079 p_sys->b_es_osd = VLC_TRUE;
1087 vlc_object_detach( id->p_decoder );
1088 vlc_object_destroy( id->p_decoder );
1093 vlc_object_detach( id->p_encoder );
1094 es_format_Clean( &id->p_encoder->fmt_out );
1095 vlc_object_destroy( id->p_encoder );
1102 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1104 sout_stream_sys_t *p_sys = p_stream->p_sys;
1106 if( p_sys->b_es_osd )
1107 transcode_osd_close( p_stream, p_sys->id_osd );
1109 if( id->b_transcode )
1111 switch( id->p_decoder->fmt_in.i_cat )
1114 transcode_audio_close( p_stream, id );
1117 transcode_video_close( p_stream, id );
1120 transcode_spu_close( p_stream, id );
1125 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
1129 vlc_object_detach( id->p_decoder );
1130 vlc_object_destroy( id->p_decoder );
1135 vlc_object_detach( id->p_encoder );
1136 es_format_Clean( &id->p_encoder->fmt_out );
1137 vlc_object_destroy( id->p_encoder );
1145 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1148 sout_stream_sys_t *p_sys = p_stream->p_sys;
1149 block_t *p_out = NULL;
1151 if( !id->b_transcode && id->id )
1153 /* Transcode OSD menu pictures. */
1154 if( p_sys->b_es_osd )
1156 transcode_osd_process( p_stream, id, p_buffer, &p_out );
1158 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
1160 else if( !id->b_transcode )
1162 block_Release( p_buffer );
1163 return VLC_EGENERIC;
1166 switch( id->p_decoder->fmt_in.i_cat )
1169 transcode_audio_process( p_stream, id, p_buffer, &p_out );
1173 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
1176 return VLC_EGENERIC;
1181 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
1184 return VLC_EGENERIC;
1190 block_Release( p_buffer );
1194 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
1198 /****************************************************************************
1199 * decoder reencoder part
1200 ****************************************************************************/
1201 int audio_BitsPerSample( vlc_fourcc_t i_format )
1205 case VLC_FOURCC('u','8',' ',' '):
1206 case VLC_FOURCC('s','8',' ',' '):
1209 case VLC_FOURCC('u','1','6','l'):
1210 case VLC_FOURCC('s','1','6','l'):
1211 case VLC_FOURCC('u','1','6','b'):
1212 case VLC_FOURCC('s','1','6','b'):
1215 case VLC_FOURCC('u','2','4','l'):
1216 case VLC_FOURCC('s','2','4','l'):
1217 case VLC_FOURCC('u','2','4','b'):
1218 case VLC_FOURCC('s','2','4','b'):
1221 case VLC_FOURCC('u','3','2','l'):
1222 case VLC_FOURCC('s','3','2','l'):
1223 case VLC_FOURCC('u','3','2','b'):
1224 case VLC_FOURCC('s','3','2','b'):
1225 case VLC_FOURCC('f','l','3','2'):
1226 case VLC_FOURCC('f','i','3','2'):
1229 case VLC_FOURCC('f','l','6','4'):
1236 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
1237 sout_stream_id_t *id,
1238 es_format_t *p_fmt_in,
1239 es_format_t *p_fmt_out,
1242 sout_stream_sys_t *p_sys = p_stream->p_sys;
1243 filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1245 vlc_object_attach( p_filter, p_stream );
1246 p_filter->pf_audio_buffer_new = (block_t* (*) (filter_t*, int))__block_New;
1248 p_filter->fmt_in = *p_fmt_in;
1249 p_filter->fmt_out = *p_fmt_out;
1251 p_filter->p_cfg = p_sys->p_afilters_cfg[id->i_ufilter];
1253 p_filter->p_module = module_Need( p_filter, "audio filter2", psz_name, 0 );
1254 if( p_filter->p_module )
1256 p_filter->fmt_out.audio.i_bitspersample =
1257 audio_BitsPerSample( p_filter->fmt_out.i_codec );
1258 *p_fmt_in = p_filter->fmt_out;
1262 vlc_object_detach( p_filter );
1263 vlc_object_destroy( p_filter );
1270 static int transcode_audio_new( sout_stream_t *p_stream,
1271 sout_stream_id_t *id )
1273 sout_stream_sys_t *p_sys = p_stream->p_sys;
1274 es_format_t fmt_last;
1281 /* Initialization of decoder structures */
1282 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1283 id->p_decoder->fmt_out.i_extra = 0;
1284 id->p_decoder->fmt_out.p_extra = 0;
1285 id->p_decoder->pf_decode_audio = 0;
1286 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
1287 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1288 /* id->p_decoder->p_cfg = p_sys->p_audio_cfg; */
1290 id->p_decoder->p_module =
1291 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1293 if( !id->p_decoder->p_module )
1295 msg_Err( p_stream, "cannot find decoder" );
1296 return VLC_EGENERIC;
1298 id->p_decoder->fmt_out.audio.i_bitspersample =
1299 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1300 fmt_last = id->p_decoder->fmt_out;
1301 /* FIX decoders so we don't have to do this */
1302 fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1308 /* Initialization of encoder format structures */
1309 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1310 id->p_decoder->fmt_out.i_codec );
1311 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
1313 /* Initialization of encoder format structures */
1314 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1315 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1316 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
1317 id->p_encoder->fmt_in.audio.i_physical_channels =
1318 id->p_encoder->fmt_out.audio.i_physical_channels;
1319 id->p_encoder->fmt_in.audio.i_original_channels =
1320 id->p_encoder->fmt_out.audio.i_original_channels;
1321 id->p_encoder->fmt_in.audio.i_channels =
1322 id->p_encoder->fmt_out.audio.i_channels;
1323 id->p_encoder->fmt_in.audio.i_bitspersample =
1324 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1326 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1328 id->p_encoder->p_module =
1329 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
1330 if( !id->p_encoder->p_module )
1332 msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_aenc );
1333 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1334 id->p_decoder->p_module = 0;
1335 return VLC_EGENERIC;
1337 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1338 id->p_encoder->fmt_in.audio.i_bitspersample =
1339 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1341 /* Load conversion filters */
1342 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1343 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1345 /* We'll have to go through fl32 first */
1346 es_format_t fmt_out = id->p_encoder->fmt_in;
1347 fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1349 id->pp_filter[id->i_filter] =
1350 transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out, NULL );
1352 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1355 for( i = 0; i < TRANSCODE_FILTERS; i++ )
1357 if( (fmt_last.audio.i_channels !=
1358 id->p_encoder->fmt_in.audio.i_channels) ||
1359 (fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate) ||
1360 (fmt_last.i_codec != id->p_encoder->fmt_in.i_codec) )
1362 id->pp_filter[id->i_filter] =
1363 transcode_audio_filter_new( p_stream, id, &fmt_last,
1364 &id->p_encoder->fmt_in, NULL );
1366 if( id->pp_filter[id->i_filter] )
1373 /* Final checks to see if conversions were successful */
1374 if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1376 msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1377 (char *)&fmt_last.i_codec,
1378 (char *)&id->p_encoder->fmt_in.i_codec );
1379 transcode_audio_close( p_stream, id );
1380 return VLC_EGENERIC;
1383 /* Load user specified audio filters now */
1384 for( i = 0; (i < p_sys->i_afilters) &&
1385 (id->i_ufilter < TRANSCODE_FILTERS); i++ )
1387 id->pp_ufilter[id->i_ufilter] =
1388 transcode_audio_filter_new( p_stream, id, &fmt_last,
1389 &id->p_encoder->fmt_in,
1390 p_sys->psz_afilters[i] );
1392 if( id->pp_ufilter[id->i_ufilter] )
1398 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1400 msg_Err( p_stream, "no audio filter found for mixing from"
1401 " %i to %i channels", fmt_last.audio.i_channels,
1402 id->p_encoder->fmt_in.audio.i_channels );
1404 /* FIXME : this might work, but only if the encoder is restarted */
1405 id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1406 id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1408 id->p_encoder->fmt_in.audio.i_physical_channels =
1409 id->p_encoder->fmt_in.audio.i_original_channels =
1410 fmt_last.audio.i_physical_channels;
1411 id->p_encoder->fmt_out.audio.i_physical_channels =
1412 id->p_encoder->fmt_out.audio.i_original_channels =
1413 fmt_last.audio.i_physical_channels;
1415 transcode_audio_close( p_stream, id );
1416 return VLC_EGENERIC;
1420 if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1422 msg_Err( p_stream, "no audio filter found for resampling from"
1423 " %iHz to %iHz", fmt_last.audio.i_rate,
1424 id->p_encoder->fmt_in.audio.i_rate );
1426 /* FIXME : this might work, but only if the encoder is restarted */
1427 id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1428 id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1430 transcode_audio_close( p_stream, id );
1431 return VLC_EGENERIC;
1435 /* FIXME: Hack for mp3 transcoding support */
1436 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1437 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1442 static void transcode_audio_close( sout_stream_t *p_stream,
1443 sout_stream_id_t *id )
1448 if( id->p_decoder->p_module )
1449 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1450 id->p_decoder->p_module = 0;
1453 if( id->p_encoder->p_module )
1454 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1455 id->p_encoder->p_module = 0;
1458 for( i = 0; i < id->i_filter; i++ )
1460 vlc_object_detach( id->pp_filter[i] );
1461 if( id->pp_filter[i]->p_module )
1462 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1463 vlc_object_destroy( id->pp_filter[i] );
1465 for( i = 0; i < id->i_ufilter; i++ )
1467 vlc_object_detach( id->pp_ufilter[i] );
1468 if( id->pp_ufilter[i]->p_module )
1469 module_Unneed( id->pp_ufilter[i], id->pp_ufilter[i]->p_module );
1470 vlc_object_destroy( id->pp_ufilter[i] );
1474 static int transcode_audio_process( sout_stream_t *p_stream,
1475 sout_stream_id_t *id,
1476 block_t *in, block_t **out )
1478 sout_stream_sys_t *p_sys = p_stream->p_sys;
1479 aout_buffer_t *p_audio_buf;
1480 block_t *p_block, *p_audio_block;
1483 input_thread_t *p_input = NULL;
1485 if( p_stream->p_parent->p_parent && p_stream->p_parent->p_parent->
1486 i_object_type == VLC_OBJECT_INPUT )
1487 p_input = (input_thread_t *)p_stream->p_parent->p_parent;
1489 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1493 stats_UpdateInteger( p_input, p_input->counters.p_decoded_audio,
1495 if( p_sys->b_master_sync )
1497 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1498 if ( p_audio_buf->start_date - i_dts > MASTER_SYNC_MAX_DRIFT
1499 || p_audio_buf->start_date - i_dts < -MASTER_SYNC_MAX_DRIFT )
1501 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1502 date_Set( &id->interpolated_pts, p_audio_buf->start_date );
1503 i_dts = p_audio_buf->start_date + 1;
1505 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1506 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1507 p_audio_buf->start_date -= p_sys->i_master_drift;
1508 p_audio_buf->end_date -= p_sys->i_master_drift;
1511 p_audio_block = p_audio_buf->p_sys;
1512 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1513 p_audio_block->i_dts = p_audio_block->i_pts =
1514 p_audio_buf->start_date;
1515 p_audio_block->i_length = p_audio_buf->end_date -
1516 p_audio_buf->start_date;
1517 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1519 /* Run filter chain */
1520 for( i = 0; i < id->i_filter; i++ )
1523 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1527 /* Run user specified filter chain */
1528 for( i = 0; i < id->i_ufilter; i++ )
1531 id->pp_ufilter[i]->pf_audio_filter( id->pp_ufilter[i],
1535 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1536 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1537 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1538 p_audio_buf->start_date = p_audio_block->i_dts;
1539 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1541 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1542 block_ChainAppend( out, p_block );
1543 block_Release( p_audio_block );
1544 free( p_audio_buf );
1550 static void audio_release_buffer( aout_buffer_t *p_buffer )
1552 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1553 if( p_buffer ) free( p_buffer );
1556 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1558 aout_buffer_t *p_buffer;
1562 if( p_dec->fmt_out.audio.i_bitspersample )
1564 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1565 p_dec->fmt_out.audio.i_channels;
1567 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1568 p_dec->fmt_out.audio.i_frame_length )
1570 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1571 p_dec->fmt_out.audio.i_frame_length;
1575 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1578 p_buffer = malloc( sizeof(aout_buffer_t) );
1579 p_buffer->pf_release = audio_release_buffer;
1580 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1582 p_buffer->p_buffer = p_block->p_buffer;
1583 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1584 p_buffer->i_nb_samples = i_samples;
1585 p_block->i_samples = i_samples;
1590 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1592 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1593 if( p_buffer ) free( p_buffer );
1599 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1601 sout_stream_sys_t *p_sys = p_stream->p_sys;
1605 * Initialization of decoder structures
1607 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1608 id->p_decoder->fmt_out.i_extra = 0;
1609 id->p_decoder->fmt_out.p_extra = 0;
1610 id->p_decoder->pf_decode_video = 0;
1611 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1612 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1613 id->p_decoder->pf_picture_link = video_link_picture_decoder;
1614 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
1615 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1616 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1617 id->p_decoder->p_owner->pp_pics[i] = 0;
1618 id->p_decoder->p_owner->p_sys = p_sys;
1619 /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
1621 id->p_decoder->p_module =
1622 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1624 if( !id->p_decoder->p_module )
1626 msg_Err( p_stream, "cannot find decoder" );
1627 return VLC_EGENERIC;
1632 * Because some info about the decoded input will only be available
1633 * once the first frame is decoded, we actually only test the availability
1634 * of the encoder here.
1637 /* Initialization of encoder format structures */
1638 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1639 id->p_decoder->fmt_out.i_codec );
1640 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1642 /* The dimensions will be set properly later on.
1643 * Just put sensible values so we can test an encoder is available. */
1644 id->p_encoder->fmt_in.video.i_width =
1645 id->p_encoder->fmt_out.video.i_width ?
1646 id->p_encoder->fmt_out.video.i_width :
1647 id->p_decoder->fmt_in.video.i_width ?
1648 id->p_decoder->fmt_in.video.i_width : 16;
1649 id->p_encoder->fmt_in.video.i_height =
1650 id->p_encoder->fmt_out.video.i_height ?
1651 id->p_encoder->fmt_out.video.i_height :
1652 id->p_decoder->fmt_in.video.i_height ?
1653 id->p_decoder->fmt_in.video.i_height : 16;
1654 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1655 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1657 id->p_encoder->i_threads = p_sys->i_threads;
1658 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1660 id->p_encoder->p_module =
1661 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1662 if( !id->p_encoder->p_module )
1664 msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_venc );
1665 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1666 id->p_decoder->p_module = 0;
1667 return VLC_EGENERIC;
1670 /* Close the encoder.
1671 * We'll open it only when we have the first frame. */
1672 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1673 if( id->p_encoder->fmt_out.p_extra )
1674 free( id->p_encoder->fmt_out.p_extra );
1675 id->p_encoder->p_module = NULL;
1677 if( p_sys->i_threads >= 1 )
1679 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
1680 VLC_THREAD_PRIORITY_VIDEO;
1681 p_sys->id_video = id;
1682 vlc_mutex_init( p_stream, &p_sys->lock_out );
1683 vlc_cond_init( p_stream, &p_sys->cond );
1684 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1685 p_sys->i_first_pic = 0;
1686 p_sys->i_last_pic = 0;
1687 p_sys->p_buffers = NULL;
1688 p_sys->b_die = p_sys->b_error = 0;
1689 if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
1692 msg_Err( p_stream, "cannot spawn encoder thread" );
1693 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1694 id->p_decoder->p_module = 0;
1695 return VLC_EGENERIC;
1702 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1703 sout_stream_id_t *id )
1705 sout_stream_sys_t *p_sys = p_stream->p_sys;
1707 /* Calculate scaling, padding, cropping etc. */
1708 /* width/height of source */
1709 int i_src_width = id->p_decoder->fmt_out.video.i_width;
1710 int i_src_height = id->p_decoder->fmt_out.video.i_height;
1712 /* with/height scaling */
1713 float f_scale_width = 1;
1714 float f_scale_height = 1;
1716 /* width/height of output stream */
1721 float f_aspect = (float)id->p_decoder->fmt_out.video.i_aspect /
1724 msg_Dbg( p_stream, "decoder aspect is %i:%i",
1725 id->p_decoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );
1727 /* Change f_aspect from source frame to source pixel */
1728 f_aspect = f_aspect * i_src_height / i_src_width;
1729 msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );
1731 /* width/height after cropping */
1732 p_sys->i_src_x_offset = p_sys->i_crop_left & ~1;
1733 p_sys->i_src_y_offset = p_sys->i_crop_top & ~1;
1734 p_sys->i_crop_width = i_src_width - ( p_sys->i_crop_left & ~1 ) -
1735 ( p_sys->i_crop_right & ~1 );
1736 p_sys->i_crop_height = i_src_height - ( p_sys->i_crop_top & ~1 ) -
1737 ( p_sys->i_crop_bottom & ~1 );
1739 /* Calculate scaling factor for specified parameters */
1740 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1741 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1743 /* Global scaling. Make sure width will remain a factor of 16 */
1746 int i_new_width = i_src_width * p_sys->f_scale;
1748 if( i_new_width % 16 <= 7 && i_new_width >= 16 )
1749 i_new_width -= i_new_width % 16;
1751 i_new_width += 16 - i_new_width % 16;
1753 f_real_scale = (float)( i_new_width ) / (float) i_src_width;
1755 i_new_height = __MAX( 16, i_src_height * (float)f_real_scale );
1757 f_scale_width = f_real_scale;
1758 f_scale_height = (float) i_new_height / (float) i_src_height;
1760 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1761 id->p_encoder->fmt_out.video.i_height <= 0 )
1763 /* Only width specified */
1764 f_scale_width = (float)id->p_encoder->fmt_out.video.i_width /
1765 p_sys->i_crop_width;
1766 f_scale_height = f_scale_width;
1768 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1769 id->p_encoder->fmt_out.video.i_height > 0 )
1771 /* Only height specified */
1772 f_scale_height = (float)id->p_encoder->fmt_out.video.i_height /
1773 p_sys->i_crop_height;
1774 f_scale_width = f_scale_height;
1776 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1777 id->p_encoder->fmt_out.video.i_height > 0 )
1779 /* Width and height specified */
1780 f_scale_width = (float)id->p_encoder->fmt_out.video.i_width
1781 / p_sys->i_crop_width;
1782 f_scale_height = (float)id->p_encoder->fmt_out.video.i_height
1783 / p_sys->i_crop_height;
1786 /* check maxwidth and maxheight
1787 * note: maxwidth and maxheight currently does not handle
1788 * canvas and padding, just scaling and cropping.
1790 if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
1791 p_sys->i_crop_width )
1793 f_scale_width = (float)p_sys->i_maxwidth / p_sys->i_crop_width;
1795 if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
1796 p_sys->i_crop_height )
1798 f_scale_height = (float)p_sys->i_maxheight / p_sys->i_crop_height;
1801 /* Change aspect ratio from source pixel to scaled pixel */
1802 f_aspect = f_aspect * f_scale_height / f_scale_width;
1803 msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );
1805 /* Correct scaling for target aspect ratio
1806 * Shrink video if necessary
1808 if ( p_sys->i_canvas_aspect > 0 )
1810 float f_target_aspect = (float)p_sys->i_canvas_aspect /
1813 if( p_sys->i_canvas_width > 0 && p_sys->i_canvas_height > 0)
1815 /* Calculate pixel aspect of canvas */
1816 f_target_aspect = f_target_aspect / p_sys->i_canvas_width *
1817 p_sys->i_canvas_height;
1819 if( f_target_aspect > f_aspect )
1821 /* Reduce width scale to increase aspect */
1822 f_scale_width = f_scale_width * f_aspect / f_target_aspect;
1826 /* Reduce height scale to decrease aspect */
1827 f_scale_height = f_scale_height * f_target_aspect / f_aspect;
1829 f_aspect = f_target_aspect;
1830 msg_Dbg( p_stream, "canvas scaled pixel aspect is %f:1", f_aspect );
1833 /* f_scale_width and f_scale_height are now final */
1834 /* Calculate width, height from scaling
1835 * Make sure its multiple of 2
1837 i_dst_width = 2 * (int)( p_sys->i_crop_width * f_scale_width / 2 + 0.5 );
1839 (int)( p_sys->i_crop_height * f_scale_height / 2 + 0.5 );
1841 p_sys->i_nopadd_width = i_dst_width;
1842 p_sys->i_nopadd_height = i_dst_height;
1843 p_sys->i_dst_x_offset = 0;
1844 p_sys->i_dst_y_offset = 0;
1846 /* Handle canvas and padding */
1847 if( p_sys->i_canvas_width <= 0 )
1849 /* No canvas width set, add explicit padding border */
1850 i_dst_width = p_sys->i_nopadd_width + ( p_sys->i_padd_left & ~1 ) +
1851 ( p_sys->i_padd_right & ~1 );
1852 p_sys->i_dst_x_offset = ( p_sys->i_padd_left & ~1 );
1856 /* Canvas set, check if we have to padd or crop */
1857 if( p_sys->i_canvas_width < p_sys->i_nopadd_width )
1859 /* need to crop more, but keep same scaling */
1860 int i_crop = 2 * (int)( ( p_sys->i_canvas_width & ~1 ) /
1861 f_scale_width / 2 + 0.5 );
1863 p_sys->i_src_x_offset += ( ( p_sys->i_crop_width - i_crop ) / 2 )
1865 p_sys->i_crop_width = i_crop;
1866 i_dst_width = p_sys->i_canvas_width & ~1;
1867 p_sys->i_nopadd_width = i_dst_width;
1869 else if( p_sys->i_canvas_width > p_sys->i_nopadd_width )
1872 i_dst_width = p_sys->i_canvas_width & ~1;
1873 p_sys->i_dst_x_offset = ( i_dst_width - p_sys->i_nopadd_width )/2;
1874 p_sys->i_dst_x_offset = p_sys->i_dst_x_offset & ~1;
1878 if( p_sys->i_canvas_height <= 0 )
1880 /* No canvas set, add padding border */
1881 i_dst_height = p_sys->i_nopadd_height + ( p_sys->i_padd_top & ~1 ) +
1882 ( p_sys->i_padd_bottom & ~1 );
1883 p_sys->i_dst_y_offset = ( p_sys->i_padd_top & ~1 );
1887 /* Canvas set, check if we have to padd or crop */
1888 if( p_sys->i_canvas_height < p_sys->i_nopadd_height )
1890 /* need to crop more, but keep same scaling */
1891 int i_crop = 2 * (int)( ( p_sys->i_canvas_height & ~1 ) /
1892 f_scale_height / 2 + 0.5 );
1894 p_sys->i_src_y_offset += ( ( p_sys->i_crop_height - i_crop ) / 2 )
1896 p_sys->i_crop_height = i_crop;
1897 i_dst_height = p_sys->i_canvas_height & ~1;
1898 p_sys->i_nopadd_height = i_dst_height;
1900 else if( p_sys->i_canvas_height > p_sys->i_nopadd_height )
1903 i_dst_height = p_sys->i_canvas_height & ~1;
1904 p_sys->i_dst_y_offset = ( i_dst_height - p_sys->i_nopadd_height )
1906 p_sys->i_dst_y_offset = p_sys->i_dst_y_offset & ~1;
1910 /* Change aspect ratio from scaled pixel to output frame */
1911 f_aspect = f_aspect * i_dst_width / i_dst_height;
1913 /* Store calculated values */
1914 id->p_encoder->fmt_out.video.i_width = i_dst_width;
1915 id->p_encoder->fmt_out.video.i_height = i_dst_height;
1917 id->p_encoder->fmt_in.video.i_width = i_dst_width;
1918 id->p_encoder->fmt_in.video.i_height = i_dst_height;
1920 msg_Dbg( p_stream, "source %ix%i, crop %ix%i, "
1921 "destination %ix%i, padding %ix%i",
1922 i_src_width, i_src_height,
1923 p_sys->i_crop_width, p_sys->i_crop_height,
1924 p_sys->i_nopadd_width, p_sys->i_nopadd_height,
1925 i_dst_width, i_dst_height
1928 /* Handle frame rate conversion */
1929 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1930 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1932 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1933 id->p_decoder->fmt_out.video.i_frame_rate_base )
1935 id->p_encoder->fmt_out.video.i_frame_rate =
1936 id->p_decoder->fmt_out.video.i_frame_rate;
1937 id->p_encoder->fmt_out.video.i_frame_rate_base =
1938 id->p_decoder->fmt_out.video.i_frame_rate_base;
1942 /* Pick a sensible default value */
1943 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1944 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1948 id->p_encoder->fmt_in.video.i_frame_rate =
1949 id->p_encoder->fmt_out.video.i_frame_rate;
1950 id->p_encoder->fmt_in.video.i_frame_rate_base =
1951 id->p_encoder->fmt_out.video.i_frame_rate_base;
1953 date_Init( &id->interpolated_pts,
1954 id->p_encoder->fmt_out.video.i_frame_rate,
1955 id->p_encoder->fmt_out.video.i_frame_rate_base );
1957 /* Check whether a particular aspect ratio was requested */
1958 if( !id->p_encoder->fmt_out.video.i_aspect )
1960 id->p_encoder->fmt_out.video.i_aspect = (int)( f_aspect * VOUT_ASPECT_FACTOR + 0.5 );
1962 id->p_encoder->fmt_in.video.i_aspect =
1963 id->p_encoder->fmt_out.video.i_aspect;
1965 msg_Dbg( p_stream, "encoder aspect is %i:%i", id->p_encoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );
1967 id->p_encoder->p_module =
1968 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1969 if( !id->p_encoder->p_module )
1971 msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_venc );
1972 return VLC_EGENERIC;
1975 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1977 /* Hack for mp2v/mp1v transcoding support */
1978 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1979 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1981 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1984 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1985 &id->p_encoder->fmt_out );
1988 msg_Err( p_stream, "cannot add this stream" );
1989 return VLC_EGENERIC;
1995 static void transcode_video_close( sout_stream_t *p_stream,
1996 sout_stream_id_t *id )
2000 if( p_stream->p_sys->i_threads >= 1 )
2002 vlc_mutex_lock( &p_stream->p_sys->lock_out );
2003 p_stream->p_sys->b_die = 1;
2004 vlc_cond_signal( &p_stream->p_sys->cond );
2005 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
2006 vlc_thread_join( p_stream->p_sys );
2007 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
2008 vlc_cond_destroy( &p_stream->p_sys->cond );
2012 if( id->p_decoder->p_module )
2013 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2015 if( id->p_decoder->p_owner )
2017 /* Clean-up pictures ring buffer */
2018 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2020 if( id->p_decoder->p_owner->pp_pics[i] )
2021 video_del_buffer( VLC_OBJECT(id->p_decoder),
2022 id->p_decoder->p_owner->pp_pics[i] );
2024 free( id->p_decoder->p_owner );
2028 if( id->p_encoder->p_module )
2029 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2032 for( i = 0; i < id->i_filter; i++ )
2034 vlc_object_detach( id->pp_filter[i] );
2035 if( id->pp_filter[i]->p_module )
2036 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
2038 /* Clean-up pictures ring buffer */
2039 for( j = 0; j < PICTURE_RING_SIZE; j++ )
2041 if( id->pp_filter[i]->p_owner->pp_pics[j] )
2042 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
2043 id->pp_filter[i]->p_owner->pp_pics[j] );
2045 free( id->pp_filter[i]->p_owner );
2046 vlc_object_destroy( id->pp_filter[i] );
2047 id->pp_filter[i] = NULL;
2050 for( i = 0; i < id->i_ufilter; i++ )
2052 vlc_object_detach( id->pp_ufilter[i] );
2053 if( id->pp_ufilter[i]->p_module )
2054 module_Unneed( id->pp_ufilter[i], id->pp_ufilter[i]->p_module );
2056 /* Clean-up pictures ring buffer */
2057 for( j = 0; j < PICTURE_RING_SIZE; j++ )
2059 if( id->pp_ufilter[i]->p_owner->pp_pics[j] )
2060 video_del_buffer( VLC_OBJECT(id->pp_ufilter[i]),
2061 id->pp_ufilter[i]->p_owner->pp_pics[j] );
2063 free( id->pp_ufilter[i]->p_owner );
2064 vlc_object_destroy( id->pp_ufilter[i] );
2065 id->pp_ufilter[i] = NULL;
2069 static int transcode_video_process( sout_stream_t *p_stream,
2070 sout_stream_id_t *id,
2071 block_t *in, block_t **out )
2073 sout_stream_sys_t *p_sys = p_stream->p_sys;
2074 int i_duplicate = 1, i;
2075 picture_t *p_pic, *p_pic2 = NULL;
2077 input_thread_t *p_input = NULL;
2079 if( p_stream->p_parent->p_parent && p_stream->p_parent->p_parent->
2080 i_object_type == VLC_OBJECT_INPUT )
2081 p_input = (input_thread_t *)p_stream->p_parent->p_parent;
2083 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
2085 subpicture_t *p_subpic = 0;
2087 stats_UpdateInteger( p_input, p_input->counters.p_decoded_video,
2090 if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
2092 mtime_t current_date = mdate();
2093 if( current_date + 50000 > p_pic->date )
2095 msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
2096 current_date + 50000 - p_pic->date );
2097 p_pic->pf_release( p_pic );
2102 if( p_sys->b_master_sync )
2104 mtime_t i_video_drift;
2105 mtime_t i_master_drift = p_sys->i_master_drift;
2108 i_pts = date_Get( &id->interpolated_pts ) + 1;
2109 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
2110 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2112 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2113 date_Set( &id->interpolated_pts, p_pic->date );
2114 i_pts = p_pic->date + 1;
2116 i_video_drift = p_pic->date - i_pts;
2119 /* Set the pts of the frame being encoded */
2120 p_pic->date = i_pts;
2122 if( i_video_drift < i_master_drift - 50000 )
2125 msg_Dbg( p_stream, "dropping frame (%i)",
2126 (int)(i_video_drift - i_master_drift) );
2128 p_pic->pf_release( p_pic );
2131 else if( i_video_drift > i_master_drift + 50000 )
2134 msg_Dbg( p_stream, "adding frame (%i)",
2135 (int)(i_video_drift - i_master_drift) );
2141 if( !id->p_encoder->p_module )
2143 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
2145 p_pic->pf_release( p_pic );
2146 transcode_video_close( p_stream, id );
2147 id->b_transcode = VLC_FALSE;
2148 return VLC_EGENERIC;
2152 if( p_stream->p_sys->b_deinterlace )
2154 id->pp_filter[id->i_filter] =
2155 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
2156 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
2158 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
2159 video_new_buffer_filter;
2160 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
2161 video_del_buffer_filter;
2163 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
2164 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
2165 id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
2166 id->pp_filter[id->i_filter]->p_module =
2167 module_Need( id->pp_filter[id->i_filter],
2168 "video filter2", p_sys->psz_deinterlace, 0 );
2169 if( id->pp_filter[id->i_filter]->p_module )
2171 id->pp_filter[id->i_filter]->p_owner =
2172 malloc( sizeof(filter_owner_sys_t) );
2173 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2174 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
2175 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
2181 msg_Dbg( p_stream, "no video filter found" );
2182 vlc_object_detach( id->pp_filter[id->i_filter] );
2183 vlc_object_destroy( id->pp_filter[id->i_filter] );
2187 /* Check if we need a filter for chroma conversion or resizing */
2188 if( id->p_decoder->fmt_out.video.i_chroma !=
2189 id->p_encoder->fmt_in.video.i_chroma ||
2191 (int)id->p_decoder->fmt_out.video.i_width != p_sys->i_crop_width ||
2192 p_sys->i_crop_width != p_sys->i_nopadd_width ||
2193 p_sys->i_nopadd_width != (int)id->p_encoder->fmt_out.video.i_width ||
2195 (int)id->p_decoder->fmt_out.video.i_height != p_sys->i_crop_height ||
2196 p_sys->i_crop_height != p_sys->i_nopadd_height ||
2197 p_sys->i_nopadd_height != (int)id->p_encoder->fmt_out.video.i_height)
2199 id->pp_filter[id->i_filter] =
2200 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
2201 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
2203 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
2204 video_new_buffer_filter;
2205 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
2206 video_del_buffer_filter;
2208 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
2209 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
2210 id->pp_filter[id->i_filter]->p_cfg = NULL;
2212 id->pp_filter[id->i_filter]->fmt_in.video.i_x_offset = p_sys->i_src_x_offset;
2213 id->pp_filter[id->i_filter]->fmt_in.video.i_y_offset = p_sys->i_src_y_offset;
2214 id->pp_filter[id->i_filter]->fmt_in.video.i_visible_width = p_sys->i_crop_width;
2215 id->pp_filter[id->i_filter]->fmt_in.video.i_visible_height = p_sys->i_crop_height;
2217 id->pp_filter[id->i_filter]->fmt_out.video.i_x_offset = p_sys->i_dst_x_offset;
2218 id->pp_filter[id->i_filter]->fmt_out.video.i_y_offset = p_sys->i_dst_y_offset;
2219 id->pp_filter[id->i_filter]->fmt_out.video.i_visible_width = p_sys->i_nopadd_width;
2220 id->pp_filter[id->i_filter]->fmt_out.video.i_visible_height = p_sys->i_nopadd_height;
2222 id->pp_filter[id->i_filter]->p_module =
2223 module_Need( id->pp_filter[id->i_filter],
2224 "crop padd", 0, 0 );
2225 if( id->pp_filter[id->i_filter]->p_module )
2227 id->pp_filter[id->i_filter]->p_owner =
2228 malloc( sizeof(filter_owner_sys_t) );
2229 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2230 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
2231 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
2237 msg_Dbg( p_stream, "no video filter found" );
2238 vlc_object_detach( id->pp_filter[id->i_filter] );
2239 vlc_object_destroy( id->pp_filter[id->i_filter] );
2241 p_pic->pf_release( p_pic );
2242 transcode_video_close( p_stream, id );
2243 id->b_transcode = VLC_FALSE;
2244 return VLC_EGENERIC;
2248 for( i = 0; (i < p_sys->i_vfilters) && (id->i_ufilter < TRANSCODE_FILTERS); i++ )
2250 id->pp_ufilter[id->i_ufilter] =
2251 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
2252 vlc_object_attach( id->pp_ufilter[id->i_ufilter], p_stream );
2254 id->pp_ufilter[id->i_ufilter]->pf_vout_buffer_new =
2255 video_new_buffer_filter;
2256 id->pp_ufilter[id->i_ufilter]->pf_vout_buffer_del =
2257 video_del_buffer_filter;
2259 id->pp_ufilter[id->i_ufilter]->fmt_in = id->p_encoder->fmt_in;
2260 id->pp_ufilter[id->i_ufilter]->fmt_out = id->p_encoder->fmt_in;
2261 id->pp_ufilter[id->i_ufilter]->p_cfg = p_sys->p_vfilters_cfg[i];
2262 id->pp_ufilter[id->i_ufilter]->p_module =
2263 module_Need( id->pp_ufilter[id->i_ufilter],
2264 "video filter2", p_sys->psz_vfilters[i], 0 );
2265 if( id->pp_ufilter[id->i_ufilter]->p_module )
2267 id->pp_ufilter[id->i_ufilter]->p_owner =
2268 malloc( sizeof(filter_owner_sys_t) );
2269 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2270 id->pp_ufilter[id->i_ufilter]->p_owner->pp_pics[i] = 0;
2271 id->pp_ufilter[id->i_ufilter]->p_owner->p_sys = p_sys;
2276 msg_Dbg( p_stream, "no video filter found" );
2277 vlc_object_detach( id->pp_ufilter[id->i_ufilter] );
2278 vlc_object_destroy( id->pp_ufilter[id->i_ufilter] );
2283 /* Run filter chain */
2284 for( i = 0; i < id->i_filter; i++ )
2286 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
2293 /* Check if we have a subpicture to overlay */
2296 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
2297 /* TODO: get another pic */
2300 /* Overlay subpicture */
2303 int i_scale_width, i_scale_height;
2304 video_format_t *p_fmt;
2306 i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
2307 id->p_decoder->fmt_out.video.i_width;
2308 i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
2309 id->p_decoder->fmt_out.video.i_height;
2311 if( p_pic->i_refcount && !id->i_filter )
2313 /* We can't modify the picture, we need to duplicate it */
2314 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
2317 vout_CopyPicture( p_stream, p_tmp, p_pic );
2318 p_pic->pf_release( p_pic );
2324 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
2326 p_fmt = &id->p_decoder->fmt_out.video;
2328 /* FIXME (shouldn't have to be done here) */
2329 p_fmt->i_sar_num = p_fmt->i_aspect *
2330 p_fmt->i_height / p_fmt->i_width;
2331 p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
2333 spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
2334 i_scale_width, i_scale_height );
2337 /* Run user specified filter chain */
2338 for( i = 0; i < id->i_ufilter; i++ )
2340 p_pic = id->pp_ufilter[i]->pf_video_filter(id->pp_ufilter[i], p_pic);
2343 if( p_sys->i_threads == 0 )
2346 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2347 block_ChainAppend( out, p_block );
2350 if( p_sys->b_master_sync )
2352 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
2353 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
2354 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2356 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2357 date_Set( &id->interpolated_pts, p_pic->date );
2358 i_pts = p_pic->date + 1;
2360 date_Increment( &id->interpolated_pts, 1 );
2363 if( p_sys->b_master_sync && i_duplicate > 1 )
2365 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
2366 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
2367 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2369 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2370 date_Set( &id->interpolated_pts, p_pic->date );
2371 i_pts = p_pic->date + 1;
2373 date_Increment( &id->interpolated_pts, 1 );
2375 if( p_sys->i_threads >= 1 )
2377 /* We can't modify the picture, we need to duplicate it */
2378 p_pic2 = video_new_buffer_decoder( id->p_decoder );
2379 if( p_pic2 != NULL )
2381 vout_CopyPicture( p_stream, p_pic2, p_pic );
2382 p_pic2->date = i_pts;
2388 p_pic->date = i_pts;
2389 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
2390 block_ChainAppend( out, p_block );
2394 if( p_sys->i_threads == 0 )
2396 p_pic->pf_release( p_pic );
2400 vlc_mutex_lock( &p_sys->lock_out );
2401 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
2402 p_sys->i_last_pic %= PICTURE_RING_SIZE;
2403 *out = p_sys->p_buffers;
2404 p_sys->p_buffers = NULL;
2405 if( p_pic2 != NULL )
2407 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic2;
2408 p_sys->i_last_pic %= PICTURE_RING_SIZE;
2410 vlc_cond_signal( &p_sys->cond );
2411 vlc_mutex_unlock( &p_sys->lock_out );
2418 static int EncoderThread( sout_stream_sys_t *p_sys )
2420 sout_stream_id_t *id = p_sys->id_video;
2423 while( !p_sys->b_die && !p_sys->b_error )
2427 vlc_mutex_lock( &p_sys->lock_out );
2428 while( p_sys->i_last_pic == p_sys->i_first_pic )
2430 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
2431 if( p_sys->b_die || p_sys->b_error ) break;
2433 if( p_sys->b_die || p_sys->b_error )
2435 vlc_mutex_unlock( &p_sys->lock_out );
2439 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2440 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2441 vlc_mutex_unlock( &p_sys->lock_out );
2443 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2444 vlc_mutex_lock( &p_sys->lock_out );
2445 block_ChainAppend( &p_sys->p_buffers, p_block );
2447 vlc_mutex_unlock( &p_sys->lock_out );
2448 p_pic->pf_release( p_pic );
2451 while( p_sys->i_last_pic != p_sys->i_first_pic )
2453 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2454 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2455 p_pic->pf_release( p_pic );
2457 block_ChainRelease( p_sys->p_buffers );
2462 struct picture_sys_t
2464 vlc_object_t *p_owner;
2467 static void video_release_buffer( picture_t *p_pic )
2469 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
2471 video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
2473 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
2476 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
2477 sout_stream_sys_t *p_sys )
2479 decoder_t *p_dec = (decoder_t *)p_this;
2483 /* Find an empty space in the picture ring buffer */
2484 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2486 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2488 pp_ring[i]->i_status = RESERVED_PICTURE;
2492 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2494 if( pp_ring[i] == 0 ) break;
2497 if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
2499 int i_first_pic = p_sys->i_first_pic;
2501 if( p_sys->i_first_pic != p_sys->i_last_pic )
2503 /* Encoder still has stuff to encode, wait to clear-up the list */
2504 while( p_sys->i_first_pic == i_first_pic )
2508 /* Find an empty space in the picture ring buffer */
2509 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2511 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2513 pp_ring[i]->i_status = RESERVED_PICTURE;
2517 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2519 if( pp_ring[i] == 0 ) break;
2523 if( i == PICTURE_RING_SIZE )
2525 msg_Err( p_this, "decoder/filter is leaking pictures, "
2526 "resetting its ring buffer" );
2528 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2530 pp_ring[i]->pf_release( pp_ring[i] );
2536 p_pic = malloc( sizeof(picture_t) );
2537 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2538 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
2539 p_dec->fmt_out.video.i_chroma,
2540 p_dec->fmt_out.video.i_width,
2541 p_dec->fmt_out.video.i_height,
2542 p_dec->fmt_out.video.i_aspect );
2544 if( !p_pic->i_planes )
2550 p_pic->pf_release = video_release_buffer;
2551 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2552 p_pic->p_sys->p_owner = p_this;
2553 p_pic->i_status = RESERVED_PICTURE;
2559 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
2561 return video_new_buffer( VLC_OBJECT(p_dec),
2562 p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2565 static picture_t *video_new_buffer_filter( filter_t *p_filter )
2567 return video_new_buffer( VLC_OBJECT(p_filter),
2568 p_filter->p_owner->pp_pics,
2569 p_filter->p_owner->p_sys );
2572 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2574 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2575 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
2576 if( p_pic ) free( p_pic );
2579 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
2581 p_pic->i_refcount = 0;
2582 p_pic->i_status = DESTROYED_PICTURE;
2585 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
2587 p_pic->i_refcount = 0;
2588 p_pic->i_status = DESTROYED_PICTURE;
2591 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2593 p_pic->i_refcount++;
2596 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2598 video_release_buffer( p_pic );
2604 static subpicture_t *spu_new_buffer( decoder_t * );
2605 static void spu_del_buffer( decoder_t *, subpicture_t * );
2607 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2609 sout_stream_sys_t *p_sys = p_stream->p_sys;
2615 /* Initialization of decoder structures */
2616 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2617 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2618 id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2619 /* id->p_decoder->p_cfg = p_sys->p_spu_cfg; */
2621 id->p_decoder->p_module =
2622 module_Need( id->p_decoder, "decoder", "$codec", 0 );
2624 if( !id->p_decoder->p_module )
2626 msg_Err( p_stream, "cannot find decoder" );
2627 return VLC_EGENERIC;
2630 if( !p_sys->b_soverlay )
2633 /* Initialization of encoder format structures */
2634 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
2635 id->p_decoder->fmt_in.i_codec );
2637 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2639 id->p_encoder->p_module =
2640 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2642 if( !id->p_encoder->p_module )
2644 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2645 msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_senc );
2646 return VLC_EGENERIC;
2652 p_sys->p_spu = spu_Create( p_stream );
2653 spu_Init( p_sys->p_spu );
2659 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2662 if( id->p_decoder->p_module )
2663 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2666 if( id->p_encoder->p_module )
2667 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2670 static int transcode_spu_process( sout_stream_t *p_stream,
2671 sout_stream_id_t *id,
2672 block_t *in, block_t **out )
2674 sout_stream_sys_t *p_sys = p_stream->p_sys;
2675 subpicture_t *p_subpic;
2678 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2679 if( !p_subpic ) return VLC_EGENERIC;
2681 if( p_sys->b_master_sync && p_sys->i_master_drift )
2683 p_subpic->i_start -= p_sys->i_master_drift;
2684 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2687 if( p_sys->b_soverlay )
2689 spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2695 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2696 spu_del_buffer( id->p_decoder, p_subpic );
2700 block_ChainAppend( out, p_block );
2705 return VLC_EGENERIC;
2708 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2710 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2711 return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2714 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2716 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2717 spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );
2723 static int transcode_osd_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2725 sout_stream_sys_t *p_sys = p_stream->p_sys;
2729 fmt.i_id = 0xbd1f; /* pid ?? */
2730 fmt.i_group = 3; /* pmt entry ?? */
2731 fmt.i_codec = VLC_FOURCC( 'Y', 'U', 'V', 'A' );
2732 fmt.psz_language = strdup( "osd" );
2734 id = malloc( sizeof( sout_stream_id_t ) );
2735 memset( id, 0, sizeof(sout_stream_id_t) );
2738 id->p_decoder = NULL;
2739 id->p_encoder = NULL;
2741 /* Create encoder object */
2742 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
2743 if( !id->p_encoder )
2745 msg_Err( p_stream, "out of memory" );
2748 vlc_object_attach( id->p_encoder, p_stream );
2749 id->p_encoder->p_module = NULL;
2751 /* Create fake destination format */
2752 es_format_Init( &id->p_encoder->fmt_out, fmt.i_cat, 0 );
2753 id->p_encoder->fmt_out.i_id = fmt.i_id;
2754 id->p_encoder->fmt_out.i_group = fmt.i_group;
2755 id->p_encoder->fmt_out.psz_language = strdup( fmt.psz_language );
2757 if( p_sys->i_osdcodec != 0 || p_sys->psz_osdenc )
2759 msg_Dbg( p_stream, "creating osdmenu transcoding from fcc=`%4.4s' "
2760 "to fcc=`%4.4s'", (char*)&fmt.i_codec,
2761 (char*)&p_sys->i_osdcodec );
2763 /* Complete destination format */
2764 id->p_encoder->fmt_out.i_codec = p_sys->i_osdcodec;
2767 /* Initialization of encoder format structures */
2768 es_format_Init( &id->p_encoder->fmt_in, fmt.i_cat, fmt.i_codec );
2769 id->p_encoder->fmt_in.psz_language = strdup( fmt.psz_language );
2771 id->p_encoder->p_cfg = p_sys->p_osd_cfg;
2773 id->p_encoder->p_module =
2774 module_Need( id->p_encoder, "encoder", p_sys->psz_osdenc, VLC_TRUE );
2776 if( !id->p_encoder->p_module )
2778 msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_osdenc );
2782 /* open output stream */
2783 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
2784 id->b_transcode = VLC_TRUE;
2786 if( !id->id ) goto error;
2790 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
2791 (char*)&fmt.i_codec );
2792 id->id = p_sys->p_out->pf_add( p_sys->p_out, &fmt );
2793 id->b_transcode = VLC_FALSE;
2795 if( !id->id ) goto error;
2799 p_sys->b_es_osd = VLC_TRUE;
2803 p_sys->p_spu = spu_Create( p_stream );
2804 if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
2805 msg_Err( p_sys, "spu initialisation failed" );
2808 if( fmt.psz_language )
2809 free( fmt.psz_language );
2814 msg_Err( p_stream, "starting osd encoding thread failed" );
2815 if( id->p_encoder->p_module )
2816 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2819 vlc_object_detach( id->p_encoder );
2820 vlc_object_destroy( id->p_encoder );
2822 if( fmt.psz_language ) free( fmt.psz_language );
2823 if( id ) free( id );
2824 p_sys->id_osd = NULL;
2825 p_sys->b_es_osd = VLC_FALSE;
2826 return VLC_EGENERIC;
2829 static void transcode_osd_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2831 sout_stream_sys_t *p_sys = p_stream->p_sys;
2834 if( p_sys->b_es_osd && id )
2836 if( id->p_encoder->p_module )
2837 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2839 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
2843 vlc_object_detach( id->p_encoder );
2844 vlc_object_destroy( id->p_encoder );
2847 p_sys->b_es_osd = VLC_FALSE;
2848 if( id ) free( id );
2851 static int transcode_osd_process( sout_stream_t *p_stream,
2852 sout_stream_id_t *id,
2853 block_t *in, block_t **out )
2855 sout_stream_sys_t *p_sys = p_stream->p_sys;
2856 subpicture_t *p_subpic = NULL;
2858 /* Check if we have a subpicture to send */
2859 if( p_sys->p_spu && in->i_dts > 0)
2861 p_subpic = spu_SortSubpictures( p_sys->p_spu, in->i_dts );
2865 msg_Warn( p_stream, "spu channel not initialized, doing it now" );
2868 p_sys->p_spu = spu_Create( p_stream );
2869 if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
2870 msg_Err( p_stream, "spu initialisation failed" );
2876 block_t *p_block = NULL;
2878 if( p_sys->b_master_sync && p_sys->i_master_drift )
2880 p_subpic->i_start -= p_sys->i_master_drift;
2881 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2884 p_block = p_sys->id_osd->p_encoder->pf_encode_sub( p_sys->id_osd->p_encoder, p_subpic );
2887 p_block->i_dts = p_block->i_pts = in->i_dts;
2888 block_ChainAppend( out, p_block );
2891 if( p_sys->p_out->pf_send( p_sys->p_out, p_sys->id_osd->id, *out ) == VLC_SUCCESS )
2892 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
2897 return VLC_EGENERIC;