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 "Allows you to specify the video encoder to use and its associated " \
50 #define VCODEC_TEXT N_("Destination video codec")
51 #define VCODEC_LONGTEXT N_( \
52 "Allows you to specify the destination video codec used for the " \
54 #define VB_TEXT N_("Video bitrate")
55 #define VB_LONGTEXT N_( \
56 "Allows you to specify the video bitrate used for the streaming " \
58 #define SCALE_TEXT N_("Video scaling")
59 #define SCALE_LONGTEXT N_( \
60 "Allows you to scale the video before encoding." )
61 #define FPS_TEXT N_("Video frame-rate")
62 #define FPS_LONGTEXT N_( \
63 "Allows you to specify an output frame rate for the video." )
64 #define DEINTERLACE_TEXT N_("Deinterlace video")
65 #define DEINTERLACE_LONGTEXT N_( \
66 "Allows you to deinterlace the video before encoding." )
67 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
68 #define DEINTERLACE_MODULE_LONGTEXT N_( \
69 "Specifies the deinterlace module to use." )
70 #define WIDTH_TEXT N_("Video width")
71 #define WIDTH_LONGTEXT N_( \
72 "Allows you to specify the output video width." )
73 #define HEIGHT_TEXT N_("Video height")
74 #define HEIGHT_LONGTEXT N_( \
75 "Allows you to specify the output video height." )
76 #define MAXWIDTH_TEXT N_("Maximum video width")
77 #define MAXWIDTH_LONGTEXT N_( \
78 "Allows you to specify a maximum output video width." )
79 #define MAXHEIGHT_TEXT N_("Maximum video height")
80 #define MAXHEIGHT_LONGTEXT N_( \
81 "Allows you to specify a maximum output video height." )
82 #define VFILTER_TEXT N_("Video filter")
83 #define VFILTER_LONGTEXT N_( \
84 "Allows you to specify video filters used after the video " \
85 "transcoding and subpictures overlaying." )
87 #define CROPTOP_TEXT N_("Video crop top")
88 #define CROPTOP_LONGTEXT N_( \
89 "Allows you to specify the top coordinate for the video cropping." )
90 #define CROPLEFT_TEXT N_("Video crop left")
91 #define CROPLEFT_LONGTEXT N_( \
92 "Allows you to specify the left coordinate for the video cropping." )
93 #define CROPBOTTOM_TEXT N_("Video crop bottom")
94 #define CROPBOTTOM_LONGTEXT N_( \
95 "Allows you to specify the bottom coordinate for the video cropping." )
96 #define CROPRIGHT_TEXT N_("Video crop right")
97 #define CROPRIGHT_LONGTEXT N_( \
98 "Allows you to specify the right coordinate for the video cropping." )
100 #define PADDTOP_TEXT N_("Video padd top")
101 #define PADDTOP_LONGTEXT N_( \
102 "Allows you to specify padding of black lines at the top." )
103 #define PADDLEFT_TEXT N_("Video padd left")
104 #define PADDLEFT_LONGTEXT N_( \
105 "Allows you to specify padding of black lines on the left." )
106 #define PADDBOTTOM_TEXT N_("Video padd bottom")
107 #define PADDBOTTOM_LONGTEXT N_( \
108 "Allows you to specify padding of black lines at the top." )
109 #define PADDRIGHT_TEXT N_("Video padd right")
110 #define PADDRIGHT_LONGTEXT N_( \
111 "Allows you to specify padding of black lines on the right." )
113 #define CANVAS_WIDTH_TEXT N_("Video canvas width")
114 #define CANVAS_WIDTH_LONGTEXT N_( \
115 "Allows to padd or crop the frame to a specified width" )
116 #define CANVAS_HEIGHT_TEXT N_("Video canvas height")
117 #define CANVAS_HEIGHT_LONGTEXT N_( \
118 "Allows to padd or crop the frame to a specified height" )
119 #define CANVAS_ASPECT_TEXT N_("Video canvas aspect ratio")
120 #define CANVAS_ASPECT_LONGTEXT N_( \
121 "Set aspect (like 4:3) of video canvas and letterbox accordingly" )
123 #define AENC_TEXT N_("Audio encoder")
124 #define AENC_LONGTEXT N_( \
125 "Allows you to specify the audio encoder to use and its associated " \
127 #define ACODEC_TEXT N_("Destination audio codec")
128 #define ACODEC_LONGTEXT N_( \
129 "Allows you to specify the destination audio codec used for the " \
130 "streaming output." )
131 #define AB_TEXT N_("Audio bitrate")
132 #define AB_LONGTEXT N_( \
133 "Allows you to specify the audio bitrate used for the streaming " \
135 #define ARATE_TEXT N_("Audio sample rate")
136 #define ARATE_LONGTEXT N_( \
137 "Allows you to specify the audio sample rate used for the streaming " \
139 #define ACHANS_TEXT N_("Audio channels")
140 #define ACHANS_LONGTEXT N_( \
141 "Allows you to specify the number of audio channels used for the " \
142 "streaming output." )
144 #define SENC_TEXT N_("Subtitles encoder")
145 #define SENC_LONGTEXT N_( \
146 "Allows you to specify the subtitles encoder to use and its associated " \
148 #define SCODEC_TEXT N_("Destination subtitles codec")
149 #define SCODEC_LONGTEXT N_( \
150 "Allows you to specify the destination subtitles codec used for the " \
151 "streaming output." )
152 #define SFILTER_TEXT N_("Subpictures filter")
153 #define SFILTER_LONGTEXT N_( \
154 "Allows you to specify subpictures filters used during the video " \
155 "transcoding. The subpictures produced by the filters will be overlayed " \
156 "directly onto the video." )
158 #define OSD_TEXT N_("OSD menu")
159 #define OSD_LONGTEXT N_(\
160 "Enable streaming of the On Screen Display. It uses the osdmenu subfilter." )
162 #define THREADS_TEXT N_("Number of threads")
163 #define THREADS_LONGTEXT N_( \
164 "Allows you to specify the 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_( "Allows you to specify if the transcoder " \
177 "should drop frames if your CPU 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_VFILTER,
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 );
267 set_section( N_("Overlays/Subtitles"), NULL );
268 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
269 SENC_LONGTEXT, VLC_FALSE );
270 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
271 SCODEC_LONGTEXT, VLC_FALSE );
272 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
273 SCODEC_LONGTEXT, VLC_FALSE );
274 add_module_list_cat( SOUT_CFG_PREFIX "sfilter", SUBCAT_VIDEO_SUBPIC,
276 SFILTER_TEXT, SFILTER_LONGTEXT, VLC_FALSE );
278 set_section( N_("On Screen Display"), NULL );
279 add_bool( SOUT_CFG_PREFIX "osd", 0, NULL, OSD_TEXT,
280 OSD_LONGTEXT, VLC_FALSE );
282 set_section( N_("Miscellaneous"), NULL );
283 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
284 THREADS_LONGTEXT, VLC_TRUE );
285 add_bool( SOUT_CFG_PREFIX "high-priority", 0, NULL, HP_TEXT, HP_LONGTEXT,
290 static const char *ppsz_sout_options[] = {
291 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
292 "paddtop", "paddbottom", "paddleft", "paddright",
293 "canvas-width", "canvas-height", "canvas-aspect",
294 "scale", "fps", "width", "height", "vfilter", "deinterlace",
295 "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab",
296 "samplerate", "channels", "senc", "scodec", "soverlay", "sfilter",
297 "osd", "audio-sync", "high-priority", "maxwidth", "maxheight", NULL
300 /*****************************************************************************
301 * Exported prototypes
302 *****************************************************************************/
303 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
304 static int Del ( sout_stream_t *, sout_stream_id_t * );
305 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
307 static int transcode_audio_new ( sout_stream_t *, sout_stream_id_t * );
308 static void transcode_audio_close ( sout_stream_t *, sout_stream_id_t * );
309 static int transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
310 block_t *, block_t ** );
312 static aout_buffer_t *audio_new_buffer( decoder_t *, int );
313 static void audio_del_buffer( decoder_t *, aout_buffer_t * );
315 static int transcode_video_new ( sout_stream_t *, sout_stream_id_t * );
316 static void transcode_video_close ( sout_stream_t *, sout_stream_id_t * );
317 static int transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
318 static int transcode_video_process( sout_stream_t *, sout_stream_id_t *,
319 block_t *, block_t ** );
321 static void video_del_buffer( vlc_object_t *, picture_t * );
322 static picture_t *video_new_buffer_decoder( decoder_t * );
323 static void video_del_buffer_decoder( decoder_t *, picture_t * );
324 static void video_link_picture_decoder( decoder_t *, picture_t * );
325 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
326 static picture_t *video_new_buffer_filter( filter_t * );
327 static void video_del_buffer_filter( filter_t *, picture_t * );
329 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
330 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
331 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
332 block_t *, block_t ** );
334 static int transcode_osd_new ( sout_stream_t *, sout_stream_id_t * );
335 static void transcode_osd_close ( sout_stream_t *, sout_stream_id_t * );
336 static int transcode_osd_process( sout_stream_t *, sout_stream_id_t *,
337 block_t *, block_t ** );
339 static int EncoderThread( struct sout_stream_sys_t * p_sys );
341 static int pi_channels_maps[6] =
344 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
345 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
346 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
347 | AOUT_CHAN_REARRIGHT,
348 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
349 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
352 #define PICTURE_RING_SIZE 64
353 #define SUBPICTURE_RING_SIZE 20
355 struct sout_stream_sys_t
359 sout_stream_t *p_out;
360 sout_stream_id_t *id_video;
362 vlc_mutex_t lock_out;
364 picture_t * pp_pics[PICTURE_RING_SIZE];
365 int i_first_pic, i_last_pic;
368 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
370 sout_cfg_t *p_audio_cfg;
376 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
378 sout_cfg_t *p_video_cfg;
382 unsigned int i_width, i_maxwidth;
383 unsigned int i_height, i_maxheight;
384 vlc_bool_t b_deinterlace;
385 char *psz_deinterlace;
386 sout_cfg_t *p_deinterlace_cfg;
388 vlc_bool_t b_high_priority;
389 vlc_bool_t b_hurry_up;
390 char *psz_vfilters[10];
391 sout_cfg_t *p_vfilters_cfg[10];
408 /* Video, calculated */
420 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
422 vlc_bool_t b_soverlay;
423 sout_cfg_t *p_spu_cfg;
427 sout_stream_id_t *id_osd; /* extension for streaming OSD menus */
428 vlc_fourcc_t i_osdcodec; /* codec osd menu (0 if not transcode) */
430 sout_cfg_t *p_osd_cfg;
431 vlc_bool_t b_es_osd; /* VLC_TRUE when osd es is registered */
432 vlc_bool_t b_sout_osd;
435 vlc_bool_t b_master_sync;
436 mtime_t i_master_drift;
439 struct decoder_owner_sys_t
441 picture_t *pp_pics[PICTURE_RING_SIZE];
442 sout_stream_sys_t *p_sys;
444 struct filter_owner_sys_t
446 picture_t *pp_pics[PICTURE_RING_SIZE];
447 sout_stream_sys_t *p_sys;
450 /*****************************************************************************
452 *****************************************************************************/
453 static int Open( vlc_object_t *p_this )
455 sout_stream_t *p_stream = (sout_stream_t*)p_this;
456 sout_stream_sys_t *p_sys;
459 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
461 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
464 msg_Err( p_stream, "cannot create chain" );
465 vlc_object_destroy( p_sys );
469 p_sys->i_master_drift = 0;
471 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
474 /* Audio transcoding parameters */
475 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
476 p_sys->psz_aenc = NULL;
477 p_sys->p_audio_cfg = NULL;
478 if( val.psz_string && *val.psz_string )
481 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
483 if( psz_next ) free( psz_next );
485 if( val.psz_string ) free( val.psz_string );
487 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
489 if( val.psz_string && *val.psz_string )
492 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
493 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
495 if( val.psz_string ) free( val.psz_string );
497 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
498 p_sys->i_abitrate = val.i_int;
499 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
501 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
502 p_sys->i_sample_rate = val.i_int;
504 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
505 p_sys->i_channels = val.i_int;
507 if( p_sys->i_acodec )
509 if( (strncmp( (char *)&p_sys->i_acodec, "mp3", 3) == 0) &&
510 (p_sys->i_channels > 2) )
512 msg_Warn( p_stream, "%d channels invalid for mp3, forcing to 2",
514 p_sys->i_channels = 2;
516 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
517 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
518 p_sys->i_channels, p_sys->i_abitrate / 1000 );
521 /* Video transcoding parameters */
522 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
523 p_sys->psz_venc = NULL;
524 p_sys->p_video_cfg = NULL;
525 if( val.psz_string && *val.psz_string )
528 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
530 if( psz_next ) free( psz_next );
532 if( val.psz_string ) free( val.psz_string );
534 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
536 if( val.psz_string && *val.psz_string )
539 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
540 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
542 if( val.psz_string ) free( val.psz_string );
544 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
545 p_sys->i_vbitrate = val.i_int;
546 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
548 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
549 p_sys->f_scale = val.f_float;
551 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
552 p_sys->f_fps = val.f_float;
554 var_Get( p_stream, SOUT_CFG_PREFIX "hurry-up", &val );
555 p_sys->b_hurry_up = val.b_bool;
557 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
558 p_sys->i_width = val.i_int;
560 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
561 p_sys->i_height = val.i_int;
563 var_Get( p_stream, SOUT_CFG_PREFIX "maxwidth", &val );
564 p_sys->i_maxwidth = val.i_int;
566 var_Get( p_stream, SOUT_CFG_PREFIX "maxheight", &val );
567 p_sys->i_maxheight = val.i_int;
569 var_Get( p_stream, SOUT_CFG_PREFIX "vfilter", &val );
570 p_sys->i_vfilters = 0;
571 if( val.psz_string && *val.psz_string )
573 char *psz_parser = val.psz_string;
575 while( psz_parser != NULL && *psz_parser != '\0' )
577 psz_parser = sout_CfgCreate(
578 &p_sys->psz_vfilters[p_sys->i_vfilters],
579 &p_sys->p_vfilters_cfg[p_sys->i_vfilters],
582 if( psz_parser != NULL && *psz_parser != '\0' ) psz_parser++;
585 if( val.psz_string ) free( val.psz_string );
586 p_sys->psz_vfilters[p_sys->i_vfilters] = NULL;
587 p_sys->p_vfilters_cfg[p_sys->i_vfilters] = NULL;
589 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
590 p_sys->b_deinterlace = val.b_bool;
592 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace-module", &val );
593 p_sys->psz_deinterlace = NULL;
594 p_sys->p_deinterlace_cfg = NULL;
595 if( val.psz_string && *val.psz_string )
598 psz_next = sout_CfgCreate( &p_sys->psz_deinterlace,
599 &p_sys->p_deinterlace_cfg,
601 if( psz_next ) free( psz_next );
603 if( val.psz_string ) free( val.psz_string );
605 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
606 p_sys->i_crop_top = val.i_int;
607 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
608 p_sys->i_crop_bottom = val.i_int;
609 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
610 p_sys->i_crop_left = val.i_int;
611 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
612 p_sys->i_crop_right = val.i_int;
614 var_Get( p_stream, SOUT_CFG_PREFIX "paddtop", &val );
615 p_sys->i_padd_top = val.i_int;
616 var_Get( p_stream, SOUT_CFG_PREFIX "paddbottom", &val );
617 p_sys->i_padd_bottom = val.i_int;
618 var_Get( p_stream, SOUT_CFG_PREFIX "paddleft", &val );
619 p_sys->i_padd_left = val.i_int;
620 var_Get( p_stream, SOUT_CFG_PREFIX "paddright", &val );
621 p_sys->i_padd_right = val.i_int;
623 var_Get( p_stream, SOUT_CFG_PREFIX "canvas-width", &val );
624 p_sys->i_canvas_width = val.i_int;
625 var_Get( p_stream, SOUT_CFG_PREFIX "canvas-height", &val );
626 p_sys->i_canvas_height = val.i_int;
628 var_Get( p_stream, SOUT_CFG_PREFIX "canvas-aspect", &val );
629 if ( val.psz_string )
631 char *psz_parser = strchr( val.psz_string, ':' );
635 *psz_parser++ = '\0';
636 p_sys->i_canvas_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
637 / atoi( psz_parser );
641 msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
642 p_sys->i_canvas_aspect = 0;
645 free( val.psz_string );
649 p_sys->i_canvas_aspect = 0;
652 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
653 p_sys->i_threads = val.i_int;
654 var_Get( p_stream, SOUT_CFG_PREFIX "high-priority", &val );
655 p_sys->b_high_priority = val.b_bool;
657 if( p_sys->i_vcodec )
659 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
660 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
661 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
664 /* Subpictures transcoding parameters */
666 p_sys->psz_senc = NULL;
667 p_sys->p_spu_cfg = NULL;
670 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
671 if( val.psz_string && *val.psz_string )
674 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
676 if( psz_next ) free( psz_next );
678 if( val.psz_string ) free( val.psz_string );
680 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
681 if( val.psz_string && *val.psz_string )
684 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
685 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
687 if( val.psz_string ) free( val.psz_string );
689 if( p_sys->i_scodec )
691 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
694 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
695 p_sys->b_soverlay = val.b_bool;
697 var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
698 if( val.psz_string && *val.psz_string )
700 p_sys->p_spu = spu_Create( p_stream );
701 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
702 var_Set( p_sys->p_spu, "sub-filter", val );
703 spu_Init( p_sys->p_spu );
705 if( val.psz_string ) free( val.psz_string );
707 /* OSD menu transcoding parameters */
708 p_sys->psz_osdenc = NULL;
709 p_sys->p_osd_cfg = NULL;
710 p_sys->i_osdcodec = 0;
711 p_sys->b_es_osd = VLC_FALSE;
713 var_Get( p_stream, SOUT_CFG_PREFIX "osd", &val );
714 p_sys->b_sout_osd = val.b_bool;
715 if( p_sys->b_sout_osd )
720 psz_next = sout_CfgCreate( &p_sys->psz_osdenc,
721 &p_sys->p_osd_cfg, strdup( "dvbsub") );
722 if( psz_next ) free( psz_next );
724 p_sys->i_osdcodec = VLC_FOURCC('Y','U','V','P' );
726 msg_Dbg( p_stream, "codec osd=%4.4s", (char *)&p_sys->i_osdcodec );
730 osd_val.psz_string = strdup("osdmenu");
731 p_sys->p_spu = spu_Create( p_stream );
732 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
733 var_Set( p_sys->p_spu, "sub-filter", osd_val );
734 spu_Init( p_sys->p_spu );
735 if( osd_val.psz_string ) free( osd_val.psz_string );
739 osd_val.psz_string = strdup("osdmenu");
740 var_Set( p_sys->p_spu, "sub-filter", osd_val );
741 if( osd_val.psz_string ) free( osd_val.psz_string );
746 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
747 p_sys->b_master_sync = val.b_bool;
748 if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
750 p_stream->pf_add = Add;
751 p_stream->pf_del = Del;
752 p_stream->pf_send = Send;
753 p_stream->p_sys = p_sys;
758 /*****************************************************************************
760 *****************************************************************************/
761 static void Close( vlc_object_t * p_this )
763 sout_stream_t *p_stream = (sout_stream_t*)p_this;
764 sout_stream_sys_t *p_sys = p_stream->p_sys;
766 sout_StreamDelete( p_sys->p_out );
768 while( p_sys->p_audio_cfg != NULL )
770 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
772 if( p_sys->p_audio_cfg->psz_name )
773 free( p_sys->p_audio_cfg->psz_name );
774 if( p_sys->p_audio_cfg->psz_value )
775 free( p_sys->p_audio_cfg->psz_value );
776 free( p_sys->p_audio_cfg );
778 p_sys->p_audio_cfg = p_next;
780 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
782 while( p_sys->p_video_cfg != NULL )
784 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
786 if( p_sys->p_video_cfg->psz_name )
787 free( p_sys->p_video_cfg->psz_name );
788 if( p_sys->p_video_cfg->psz_value )
789 free( p_sys->p_video_cfg->psz_value );
790 free( p_sys->p_video_cfg );
792 p_sys->p_video_cfg = p_next;
794 if( p_sys->psz_venc ) free( p_sys->psz_venc );
796 while( p_sys->p_deinterlace_cfg != NULL )
798 sout_cfg_t *p_next = p_sys->p_deinterlace_cfg->p_next;
800 if( p_sys->p_deinterlace_cfg->psz_name )
801 free( p_sys->p_deinterlace_cfg->psz_name );
802 if( p_sys->p_deinterlace_cfg->psz_value )
803 free( p_sys->p_deinterlace_cfg->psz_value );
804 free( p_sys->p_deinterlace_cfg );
806 p_sys->p_deinterlace_cfg = p_next;
808 if( p_sys->psz_deinterlace ) free( p_sys->psz_deinterlace );
810 while( p_sys->p_spu_cfg != NULL )
812 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
814 if( p_sys->p_spu_cfg->psz_name )
815 free( p_sys->p_spu_cfg->psz_name );
816 if( p_sys->p_spu_cfg->psz_value )
817 free( p_sys->p_spu_cfg->psz_value );
818 free( p_sys->p_spu_cfg );
820 p_sys->p_spu_cfg = p_next;
822 if( p_sys->psz_senc ) free( p_sys->psz_senc );
824 if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
826 while( p_sys->p_osd_cfg != NULL )
828 sout_cfg_t *p_next = p_sys->p_osd_cfg->p_next;
830 if( p_sys->p_osd_cfg->psz_name )
831 free( p_sys->p_osd_cfg->psz_name );
832 if( p_sys->p_osd_cfg->psz_value )
833 free( p_sys->p_osd_cfg->psz_value );
834 free( p_sys->p_osd_cfg );
836 p_sys->p_osd_cfg = p_next;
838 if( p_sys->psz_osdenc ) free( p_sys->psz_osdenc );
840 vlc_object_destroy( p_sys );
843 struct sout_stream_id_t
845 vlc_fourcc_t b_transcode;
847 /* id of the out stream */
851 decoder_t *p_decoder;
854 filter_t *pp_filter[10];
856 filter_t *pp_vfilter[10];
860 encoder_t *p_encoder;
863 date_t interpolated_pts;
866 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
868 sout_stream_sys_t *p_sys = p_stream->p_sys;
869 sout_stream_id_t *id;
871 id = malloc( sizeof( sout_stream_id_t ) );
872 memset( id, 0, sizeof(sout_stream_id_t) );
875 id->p_decoder = NULL;
876 id->p_encoder = NULL;
878 /* Create decoder object */
879 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
882 msg_Err( p_stream, "out of memory" );
885 vlc_object_attach( id->p_decoder, p_stream );
886 id->p_decoder->p_module = NULL;
887 id->p_decoder->fmt_in = *p_fmt;
888 id->p_decoder->b_pace_control = VLC_TRUE;
890 /* Create encoder object */
891 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
894 msg_Err( p_stream, "out of memory" );
897 vlc_object_attach( id->p_encoder, p_stream );
898 id->p_encoder->p_module = NULL;
900 /* Create destination format */
901 es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
902 id->p_encoder->fmt_out.i_id = p_fmt->i_id;
903 id->p_encoder->fmt_out.i_group = p_fmt->i_group;
904 if( p_fmt->psz_language )
905 id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
907 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
910 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
911 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
913 /* Complete destination format */
914 id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
915 id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
916 p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
917 id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
918 id->p_encoder->fmt_out.audio.i_bitspersample =
919 p_fmt->audio.i_bitspersample;
920 id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
921 p_sys->i_channels : p_fmt->audio.i_channels;
922 /* Sanity check for audio channels */
923 id->p_encoder->fmt_out.audio.i_channels =
924 __MIN( id->p_encoder->fmt_out.audio.i_channels,
925 id->p_decoder->fmt_in.audio.i_channels );
926 id->p_encoder->fmt_out.audio.i_original_channels =
927 id->p_decoder->fmt_in.audio.i_physical_channels;
928 if( id->p_decoder->fmt_in.audio.i_channels ==
929 id->p_encoder->fmt_out.audio.i_channels )
931 id->p_encoder->fmt_out.audio.i_physical_channels =
932 id->p_decoder->fmt_in.audio.i_physical_channels;
936 id->p_encoder->fmt_out.audio.i_physical_channels =
937 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
940 /* Build decoder -> filter -> encoder chain */
941 if( transcode_audio_new( p_stream, id ) )
943 msg_Err( p_stream, "cannot create audio chain" );
947 /* Open output stream */
948 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
949 id->b_transcode = VLC_TRUE;
953 transcode_audio_close( p_stream, id );
957 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
959 else if( p_fmt->i_cat == VIDEO_ES &&
960 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
963 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
964 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
966 /* Complete destination format */
967 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
968 id->p_encoder->fmt_out.video.i_width = p_sys->i_width & ~1;
969 id->p_encoder->fmt_out.video.i_height = p_sys->i_height & ~1;
970 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
972 /* Build decoder -> filter -> encoder chain */
973 if( transcode_video_new( p_stream, id ) )
975 msg_Err( p_stream, "cannot create video chain" );
979 /* Stream will be added later on because we don't know
980 * all the characteristics of the decoded stream yet */
981 id->b_transcode = VLC_TRUE;
983 if( p_sys->f_fps > 0 )
985 id->p_encoder->fmt_out.video.i_frame_rate =
986 (p_sys->f_fps * 1001) + 0.5;
987 id->p_encoder->fmt_out.video.i_frame_rate_base = 1001;
990 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
992 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
993 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
994 (char*)&p_sys->i_scodec );
996 /* Complete destination format */
997 id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
999 /* build decoder -> filter -> encoder */
1000 if( transcode_spu_new( p_stream, id ) )
1002 msg_Err( p_stream, "cannot create subtitles chain" );
1006 /* open output stream */
1007 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
1008 id->b_transcode = VLC_TRUE;
1012 transcode_spu_close( p_stream, id );
1016 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
1018 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
1019 (char*)&p_fmt->i_codec );
1021 id->b_transcode = VLC_TRUE;
1023 /* Build decoder -> filter -> overlaying chain */
1024 if( transcode_spu_new( p_stream, id ) )
1026 msg_Err( p_stream, "cannot create subtitles chain" );
1032 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
1033 (char*)&p_fmt->i_codec );
1034 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
1035 id->b_transcode = VLC_FALSE;
1037 if( !id->id ) goto error;
1040 if( p_sys->b_sout_osd )
1042 /* Create a fake OSD menu elementary stream */
1043 if( !p_sys->b_es_osd && (p_sys->i_osdcodec != 0 || p_sys->psz_osdenc) )
1045 if( transcode_osd_new( p_stream, p_sys->id_osd ) )
1047 msg_Err( p_stream, "cannot create osd chain" );
1050 p_sys->b_es_osd = VLC_TRUE;
1058 vlc_object_detach( id->p_decoder );
1059 vlc_object_destroy( id->p_decoder );
1064 vlc_object_detach( id->p_encoder );
1065 es_format_Clean( &id->p_encoder->fmt_out );
1066 vlc_object_destroy( id->p_encoder );
1073 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1075 sout_stream_sys_t *p_sys = p_stream->p_sys;
1077 if( p_sys->b_es_osd )
1078 transcode_osd_close( p_stream, p_sys->id_osd );
1080 if( id->b_transcode )
1082 switch( id->p_decoder->fmt_in.i_cat )
1085 transcode_audio_close( p_stream, id );
1088 transcode_video_close( p_stream, id );
1091 transcode_spu_close( p_stream, id );
1096 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
1100 vlc_object_detach( id->p_decoder );
1101 vlc_object_destroy( id->p_decoder );
1106 vlc_object_detach( id->p_encoder );
1107 es_format_Clean( &id->p_encoder->fmt_out );
1108 vlc_object_destroy( id->p_encoder );
1116 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1119 sout_stream_sys_t *p_sys = p_stream->p_sys;
1120 block_t *p_out = NULL;
1122 if( !id->b_transcode && id->id )
1124 /* Transcode OSD menu pictures. */
1125 if( p_sys->b_es_osd )
1127 transcode_osd_process( p_stream, id, p_buffer, &p_out );
1129 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
1131 else if( !id->b_transcode )
1133 block_Release( p_buffer );
1134 return VLC_EGENERIC;
1137 switch( id->p_decoder->fmt_in.i_cat )
1140 transcode_audio_process( p_stream, id, p_buffer, &p_out );
1144 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
1147 return VLC_EGENERIC;
1152 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
1155 return VLC_EGENERIC;
1161 block_Release( p_buffer );
1165 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
1169 /****************************************************************************
1170 * decoder reencoder part
1171 ****************************************************************************/
1172 int audio_BitsPerSample( vlc_fourcc_t i_format )
1176 case VLC_FOURCC('u','8',' ',' '):
1177 case VLC_FOURCC('s','8',' ',' '):
1180 case VLC_FOURCC('u','1','6','l'):
1181 case VLC_FOURCC('s','1','6','l'):
1182 case VLC_FOURCC('u','1','6','b'):
1183 case VLC_FOURCC('s','1','6','b'):
1186 case VLC_FOURCC('u','2','4','l'):
1187 case VLC_FOURCC('s','2','4','l'):
1188 case VLC_FOURCC('u','2','4','b'):
1189 case VLC_FOURCC('s','2','4','b'):
1192 case VLC_FOURCC('u','3','2','l'):
1193 case VLC_FOURCC('s','3','2','l'):
1194 case VLC_FOURCC('u','3','2','b'):
1195 case VLC_FOURCC('s','3','2','b'):
1196 case VLC_FOURCC('f','l','3','2'):
1197 case VLC_FOURCC('f','i','3','2'):
1200 case VLC_FOURCC('f','l','6','4'):
1207 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
1208 sout_stream_id_t *id,
1209 es_format_t *p_fmt_in,
1210 es_format_t *p_fmt_out )
1212 filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1214 vlc_object_attach( p_filter, p_stream );
1215 p_filter->pf_audio_buffer_new = (block_t* (*) (filter_t*, int))__block_New;
1217 p_filter->fmt_in = *p_fmt_in;
1218 p_filter->fmt_out = *p_fmt_out;
1220 p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
1221 if( p_filter->p_module )
1223 p_filter->fmt_out.audio.i_bitspersample =
1224 audio_BitsPerSample( p_filter->fmt_out.i_codec );
1225 *p_fmt_in = p_filter->fmt_out;
1229 vlc_object_detach( p_filter );
1230 vlc_object_destroy( p_filter );
1237 static int transcode_audio_new( sout_stream_t *p_stream,
1238 sout_stream_id_t *id )
1240 sout_stream_sys_t *p_sys = p_stream->p_sys;
1241 es_format_t fmt_last;
1248 /* Initialization of decoder structures */
1249 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1250 id->p_decoder->fmt_out.i_extra = 0;
1251 id->p_decoder->fmt_out.p_extra = 0;
1252 id->p_decoder->pf_decode_audio = 0;
1253 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
1254 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1255 /* id->p_decoder->p_cfg = p_sys->p_audio_cfg; */
1257 id->p_decoder->p_module =
1258 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1260 if( !id->p_decoder->p_module )
1262 msg_Err( p_stream, "cannot find decoder" );
1263 return VLC_EGENERIC;
1265 id->p_decoder->fmt_out.audio.i_bitspersample =
1266 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1267 fmt_last = id->p_decoder->fmt_out;
1268 /* FIX decoders so we don't have to do this */
1269 fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1275 /* Initialization of encoder format structures */
1276 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1277 id->p_decoder->fmt_out.i_codec );
1278 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
1280 /* Initialization of encoder format structures */
1281 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1282 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1283 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
1284 id->p_encoder->fmt_in.audio.i_physical_channels =
1285 id->p_encoder->fmt_out.audio.i_physical_channels;
1286 id->p_encoder->fmt_in.audio.i_original_channels =
1287 id->p_encoder->fmt_out.audio.i_original_channels;
1288 id->p_encoder->fmt_in.audio.i_channels =
1289 id->p_encoder->fmt_out.audio.i_channels;
1290 id->p_encoder->fmt_in.audio.i_bitspersample =
1291 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1293 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1295 id->p_encoder->p_module =
1296 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
1297 if( !id->p_encoder->p_module )
1299 msg_Err( p_stream, "cannot find encoder" );
1300 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1301 id->p_decoder->p_module = 0;
1302 return VLC_EGENERIC;
1304 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1305 id->p_encoder->fmt_in.audio.i_bitspersample =
1306 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1308 /* Load conversion filters */
1309 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1310 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1312 /* We'll have to go through fl32 first */
1313 es_format_t fmt_out = id->p_encoder->fmt_in;
1314 fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1316 id->pp_filter[id->i_filter] =
1317 transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );
1319 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1324 if( fmt_last.audio.i_channels !=
1325 id->p_encoder->fmt_in.audio.i_channels ||
1326 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
1327 fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1329 id->pp_filter[id->i_filter] =
1330 transcode_audio_filter_new( p_stream, id, &fmt_last,
1331 &id->p_encoder->fmt_in );
1333 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1338 /* Final checks to see if conversions were successful */
1339 if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1341 msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1342 (char *)&fmt_last.i_codec,
1343 (char *)&id->p_encoder->fmt_in.i_codec );
1344 transcode_audio_close( p_stream, id );
1345 return VLC_EGENERIC;
1348 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1350 msg_Err( p_stream, "no audio filter found for mixing from"
1351 " %i to %i channels", fmt_last.audio.i_channels,
1352 id->p_encoder->fmt_in.audio.i_channels );
1354 /* FIXME : this might work, but only if the encoder is restarted */
1355 id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1356 id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1358 id->p_encoder->fmt_in.audio.i_physical_channels =
1359 id->p_encoder->fmt_in.audio.i_original_channels =
1360 fmt_last.audio.i_physical_channels;
1361 id->p_encoder->fmt_out.audio.i_physical_channels =
1362 id->p_encoder->fmt_out.audio.i_original_channels =
1363 fmt_last.audio.i_physical_channels;
1365 transcode_audio_close( p_stream, id );
1366 return VLC_EGENERIC;
1370 if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1372 msg_Err( p_stream, "no audio filter found for resampling from"
1373 " %iHz to %iHz", fmt_last.audio.i_rate,
1374 id->p_encoder->fmt_in.audio.i_rate );
1376 /* FIXME : this might work, but only if the encoder is restarted */
1377 id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1378 id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1380 transcode_audio_close( p_stream, id );
1381 return VLC_EGENERIC;
1385 /* FIXME: Hack for mp3 transcoding support */
1386 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1387 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1392 static void transcode_audio_close( sout_stream_t *p_stream,
1393 sout_stream_id_t *id )
1398 if( id->p_decoder->p_module )
1399 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1400 id->p_decoder->p_module = 0;
1403 if( id->p_encoder->p_module )
1404 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1405 id->p_encoder->p_module = 0;
1408 for( i = 0; i < id->i_filter; i++ )
1410 vlc_object_detach( id->pp_filter[i] );
1411 if( id->pp_filter[i]->p_module )
1412 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1413 vlc_object_destroy( id->pp_filter[i] );
1417 static int transcode_audio_process( sout_stream_t *p_stream,
1418 sout_stream_id_t *id,
1419 block_t *in, block_t **out )
1421 sout_stream_sys_t *p_sys = p_stream->p_sys;
1422 aout_buffer_t *p_audio_buf;
1423 block_t *p_block, *p_audio_block;
1427 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1430 stats_UpdateInteger( p_stream->p_parent->p_parent, STATS_DECODED_AUDIO,
1432 if( p_sys->b_master_sync )
1434 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1435 if ( p_audio_buf->start_date - i_dts > MASTER_SYNC_MAX_DRIFT
1436 || p_audio_buf->start_date - i_dts < -MASTER_SYNC_MAX_DRIFT )
1438 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1439 date_Set( &id->interpolated_pts, p_audio_buf->start_date );
1440 i_dts = p_audio_buf->start_date + 1;
1442 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1443 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1444 p_audio_buf->start_date -= p_sys->i_master_drift;
1445 p_audio_buf->end_date -= p_sys->i_master_drift;
1448 p_audio_block = p_audio_buf->p_sys;
1449 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1450 p_audio_block->i_dts = p_audio_block->i_pts =
1451 p_audio_buf->start_date;
1452 p_audio_block->i_length = p_audio_buf->end_date -
1453 p_audio_buf->start_date;
1454 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1456 /* Run filter chain */
1457 for( i = 0; i < id->i_filter; i++ )
1460 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1464 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1465 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1466 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1467 p_audio_buf->start_date = p_audio_block->i_dts;
1468 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1470 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1471 block_ChainAppend( out, p_block );
1472 block_Release( p_audio_block );
1473 free( p_audio_buf );
1479 static void audio_release_buffer( aout_buffer_t *p_buffer )
1481 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1482 if( p_buffer ) free( p_buffer );
1485 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1487 aout_buffer_t *p_buffer;
1491 if( p_dec->fmt_out.audio.i_bitspersample )
1493 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1494 p_dec->fmt_out.audio.i_channels;
1496 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1497 p_dec->fmt_out.audio.i_frame_length )
1499 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1500 p_dec->fmt_out.audio.i_frame_length;
1504 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1507 p_buffer = malloc( sizeof(aout_buffer_t) );
1508 p_buffer->pf_release = audio_release_buffer;
1509 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1511 p_buffer->p_buffer = p_block->p_buffer;
1512 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1513 p_buffer->i_nb_samples = i_samples;
1514 p_block->i_samples = i_samples;
1519 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1521 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1522 if( p_buffer ) free( p_buffer );
1528 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1530 sout_stream_sys_t *p_sys = p_stream->p_sys;
1537 /* Initialization of decoder structures */
1538 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1539 id->p_decoder->fmt_out.i_extra = 0;
1540 id->p_decoder->fmt_out.p_extra = 0;
1541 id->p_decoder->pf_decode_video = 0;
1542 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1543 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1544 id->p_decoder->pf_picture_link = video_link_picture_decoder;
1545 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
1546 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1547 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1548 id->p_decoder->p_owner->pp_pics[i] = 0;
1549 id->p_decoder->p_owner->p_sys = p_sys;
1550 /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
1552 id->p_decoder->p_module =
1553 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1555 if( !id->p_decoder->p_module )
1557 msg_Err( p_stream, "cannot find decoder" );
1558 return VLC_EGENERIC;
1563 * Because some info about the decoded input will only be available
1564 * once the first frame is decoded, we actually only test the availability
1565 * of the encoder here.
1568 /* Initialization of encoder format structures */
1569 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1570 id->p_decoder->fmt_out.i_codec );
1571 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1573 /* The dimensions will be set properly later on.
1574 * Just put sensible values so we can test an encoder is available. */
1575 id->p_encoder->fmt_in.video.i_width =
1576 id->p_encoder->fmt_out.video.i_width ?
1577 id->p_encoder->fmt_out.video.i_width :
1578 id->p_decoder->fmt_in.video.i_width ?
1579 id->p_decoder->fmt_in.video.i_width : 16;
1580 id->p_encoder->fmt_in.video.i_height =
1581 id->p_encoder->fmt_out.video.i_height ?
1582 id->p_encoder->fmt_out.video.i_height :
1583 id->p_decoder->fmt_in.video.i_height ?
1584 id->p_decoder->fmt_in.video.i_height : 16;
1585 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1586 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1588 id->p_encoder->i_threads = p_sys->i_threads;
1589 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1591 id->p_encoder->p_module =
1592 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1593 if( !id->p_encoder->p_module )
1595 msg_Err( p_stream, "cannot find encoder" );
1596 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1597 id->p_decoder->p_module = 0;
1598 return VLC_EGENERIC;
1601 /* Close the encoder.
1602 * We'll open it only when we have the first frame. */
1603 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1604 if( id->p_encoder->fmt_out.p_extra )
1605 free( id->p_encoder->fmt_out.p_extra );
1606 id->p_encoder->p_module = NULL;
1608 if( p_sys->i_threads >= 1 )
1610 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
1611 VLC_THREAD_PRIORITY_VIDEO;
1612 p_sys->id_video = id;
1613 vlc_mutex_init( p_stream, &p_sys->lock_out );
1614 vlc_cond_init( p_stream, &p_sys->cond );
1615 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1616 p_sys->i_first_pic = 0;
1617 p_sys->i_last_pic = 0;
1618 p_sys->p_buffers = NULL;
1619 p_sys->b_die = p_sys->b_error = 0;
1620 if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
1623 msg_Err( p_stream, "cannot spawn encoder thread" );
1624 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1625 id->p_decoder->p_module = 0;
1626 return VLC_EGENERIC;
1633 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1634 sout_stream_id_t *id )
1636 sout_stream_sys_t *p_sys = p_stream->p_sys;
1638 /* Calculate scaling, padding, cropping etc. */
1640 /* width/height of source */
1641 int i_src_width = id->p_decoder->fmt_out.video.i_width;
1642 int i_src_height = id->p_decoder->fmt_out.video.i_height;
1644 /* with/height scaling */
1645 float f_scale_width = 1;
1646 float f_scale_height = 1;
1648 /* width/height of output stream */
1653 float f_aspect = (float)id->p_decoder->fmt_out.video.i_aspect /
1656 msg_Dbg( p_stream, "decoder aspect is %i:%i",
1657 id->p_decoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );
1659 /* Change f_aspect from source frame to source pixel */
1660 f_aspect = f_aspect * i_src_height / i_src_width;
1661 msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );
1663 /* width/height after cropping */
1664 p_sys->i_src_x_offset = p_sys->i_crop_left & ~1;
1665 p_sys->i_src_y_offset = p_sys->i_crop_top & ~1;
1666 p_sys->i_crop_width = i_src_width - ( p_sys->i_crop_left & ~1 ) -
1667 ( p_sys->i_crop_right & ~1 );
1668 p_sys->i_crop_height = i_src_height - ( p_sys->i_crop_top & ~1 ) -
1669 ( p_sys->i_crop_bottom & ~1 );
1671 /* Calculate scaling factor for specified parameters */
1672 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1673 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1675 /* Global scaling. Make sure width will remain a factor of 16 */
1678 int i_new_width = i_src_width * p_sys->f_scale;
1680 if( i_new_width % 16 <= 7 && i_new_width >= 16 )
1681 i_new_width -= i_new_width % 16;
1683 i_new_width += 16 - i_new_width % 16;
1685 f_real_scale = (float)( i_new_width ) / (float) i_src_width;
1687 i_new_height = __MAX( 16, i_src_height * (float)f_real_scale );
1689 f_scale_width = f_real_scale;
1690 f_scale_height = (float) i_new_height / (float) i_src_height;
1692 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1693 id->p_encoder->fmt_out.video.i_height <= 0 )
1695 /* Only width specified */
1696 f_scale_width = (float)id->p_encoder->fmt_out.video.i_width /
1697 p_sys->i_crop_width;
1698 f_scale_height = f_scale_width;
1700 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1701 id->p_encoder->fmt_out.video.i_height > 0 )
1703 /* Only height specified */
1704 f_scale_height = (float)id->p_encoder->fmt_out.video.i_height /
1705 p_sys->i_crop_height;
1706 f_scale_width = f_scale_height;
1708 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1709 id->p_encoder->fmt_out.video.i_height > 0 )
1711 /* Width and height specified */
1712 f_scale_width = (float)id->p_encoder->fmt_out.video.i_width
1713 / p_sys->i_crop_width;
1714 f_scale_height = (float)id->p_encoder->fmt_out.video.i_height
1715 / p_sys->i_crop_height;
1718 /* check maxwidth and maxheight */
1719 /* note: maxwidth and maxheight currently does not handle
1720 * canvas and padding, just scaling and cropping. */
1722 if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
1723 p_sys->i_crop_width )
1725 f_scale_width = (float)p_sys->i_maxwidth / p_sys->i_crop_width;
1727 if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
1728 p_sys->i_crop_height )
1730 f_scale_height = (float)p_sys->i_maxheight / p_sys->i_crop_height;
1733 /* Change aspect ratio from source pixel to scaled pixel */
1734 f_aspect = f_aspect * f_scale_height / f_scale_width;
1735 msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );
1737 /* Correct scaling for target aspect ratio */
1738 /* Shrink video if necessary */
1739 if ( p_sys->i_canvas_aspect > 0 )
1741 float f_target_aspect = (float)p_sys->i_canvas_aspect /
1744 if( p_sys->i_canvas_width > 0 && p_sys->i_canvas_height > 0)
1746 /* Calculate pixel aspect of canvas */
1747 f_target_aspect = f_target_aspect / p_sys->i_canvas_width *
1748 p_sys->i_canvas_height;
1750 if( f_target_aspect > f_aspect )
1752 /* Reduce width scale to increase aspect */
1753 f_scale_width = f_scale_width * f_aspect / f_target_aspect;
1757 /* Reduce height scale to decrease aspect */
1758 f_scale_height = f_scale_height * f_target_aspect / f_aspect;
1760 f_aspect = f_target_aspect;
1761 msg_Dbg( p_stream, "Canvas scaled pixel aspect is %f:1", f_aspect );
1764 /* f_scale_width and f_scale_height are now final */
1766 /* Calculate width, height from scaling */
1767 /* Make sure its multiple of 2 */
1769 i_dst_width = 2 * (int)( p_sys->i_crop_width * f_scale_width / 2 + 0.5 );
1771 (int)( p_sys->i_crop_height * f_scale_height / 2 + 0.5 );
1773 p_sys->i_nopadd_width = i_dst_width;
1774 p_sys->i_nopadd_height = i_dst_height;
1775 p_sys->i_dst_x_offset = 0;
1776 p_sys->i_dst_y_offset = 0;
1778 /* Handle canvas and padding */
1780 if( p_sys->i_canvas_width <= 0 )
1782 /* No canvas width set, add explicit padding border */
1783 i_dst_width = p_sys->i_nopadd_width + ( p_sys->i_padd_left & ~1 ) +
1784 ( p_sys->i_padd_right & ~1 );
1785 p_sys->i_dst_x_offset = ( p_sys->i_padd_left & ~1 );
1789 /* Canvas set, check if we have to padd or crop */
1791 if( p_sys->i_canvas_width < p_sys->i_nopadd_width )
1793 /* need to crop more, but keep same scaling */
1794 int i_crop = 2 * (int)( ( p_sys->i_canvas_width & ~1 ) /
1795 f_scale_width / 2 + 0.5 );
1797 p_sys->i_src_x_offset += ( ( p_sys->i_crop_width - i_crop ) / 2 )
1799 p_sys->i_crop_width = i_crop;
1800 i_dst_width = p_sys->i_canvas_width & ~1;
1801 p_sys->i_nopadd_width = i_dst_width;
1803 else if( p_sys->i_canvas_width > p_sys->i_nopadd_width )
1806 i_dst_width = p_sys->i_canvas_width & ~1;
1807 p_sys->i_dst_x_offset = ( i_dst_width - p_sys->i_nopadd_width )/2;
1808 p_sys->i_dst_x_offset = p_sys->i_dst_x_offset & ~1;
1812 if( p_sys->i_canvas_height <= 0 )
1814 /* No canvas set, add padding border */
1815 i_dst_height = p_sys->i_nopadd_height + ( p_sys->i_padd_top & ~1 ) +
1816 ( p_sys->i_padd_bottom & ~1 );
1817 p_sys->i_dst_y_offset = ( p_sys->i_padd_top & ~1 );
1821 /* Canvas set, check if we have to padd or crop */
1823 if( p_sys->i_canvas_height < p_sys->i_nopadd_height )
1825 /* need to crop more, but keep same scaling */
1826 int i_crop = 2 * (int)( ( p_sys->i_canvas_height & ~1 ) /
1827 f_scale_height / 2 + 0.5 );
1829 p_sys->i_src_y_offset += ( ( p_sys->i_crop_height - i_crop ) / 2 )
1831 p_sys->i_crop_height = i_crop;
1832 i_dst_height = p_sys->i_canvas_height & ~1;
1833 p_sys->i_nopadd_height = i_dst_height;
1835 else if( p_sys->i_canvas_height > p_sys->i_nopadd_height )
1838 i_dst_height = p_sys->i_canvas_height & ~1;
1839 p_sys->i_dst_y_offset = ( i_dst_height - p_sys->i_nopadd_height )
1841 p_sys->i_dst_y_offset = p_sys->i_dst_y_offset & ~1;
1846 /* Change aspect ratio from scaled pixel to output frame */
1848 f_aspect = f_aspect * i_dst_width / i_dst_height;
1850 /* Store calculated values */
1851 id->p_encoder->fmt_out.video.i_width = i_dst_width;
1852 id->p_encoder->fmt_out.video.i_height = i_dst_height;
1854 id->p_encoder->fmt_in.video.i_width = i_dst_width;
1855 id->p_encoder->fmt_in.video.i_height = i_dst_height;
1857 msg_Dbg( p_stream, "Source %ix%i, crop %ix%i, "
1858 "destination %ix%i, padding %ix%i",
1859 i_src_width, i_src_height,
1860 p_sys->i_crop_width, p_sys->i_crop_height,
1861 p_sys->i_nopadd_width, p_sys->i_nopadd_height,
1862 i_dst_width, i_dst_height
1865 /* Handle frame rate conversion */
1866 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1867 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1869 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1870 id->p_decoder->fmt_out.video.i_frame_rate_base )
1872 id->p_encoder->fmt_out.video.i_frame_rate =
1873 id->p_decoder->fmt_out.video.i_frame_rate;
1874 id->p_encoder->fmt_out.video.i_frame_rate_base =
1875 id->p_decoder->fmt_out.video.i_frame_rate_base;
1879 /* Pick a sensible default value */
1880 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1881 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1885 id->p_encoder->fmt_in.video.i_frame_rate =
1886 id->p_encoder->fmt_out.video.i_frame_rate;
1887 id->p_encoder->fmt_in.video.i_frame_rate_base =
1888 id->p_encoder->fmt_out.video.i_frame_rate_base;
1890 date_Init( &id->interpolated_pts,
1891 id->p_encoder->fmt_out.video.i_frame_rate,
1892 id->p_encoder->fmt_out.video.i_frame_rate_base );
1894 /* Check whether a particular aspect ratio was requested */
1895 if( !id->p_encoder->fmt_out.video.i_aspect )
1897 id->p_encoder->fmt_out.video.i_aspect = (int)( f_aspect * VOUT_ASPECT_FACTOR + 0.5 );
1899 id->p_encoder->fmt_in.video.i_aspect =
1900 id->p_encoder->fmt_out.video.i_aspect;
1902 msg_Dbg( p_stream, "encoder aspect is %i:%i", id->p_encoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );
1904 id->p_encoder->p_module =
1905 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1906 if( !id->p_encoder->p_module )
1908 msg_Err( p_stream, "cannot find encoder" );
1909 return VLC_EGENERIC;
1912 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1914 /* Hack for mp2v/mp1v transcoding support */
1915 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1916 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1918 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1921 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1922 &id->p_encoder->fmt_out );
1925 msg_Err( p_stream, "cannot add this stream" );
1926 return VLC_EGENERIC;
1932 static void transcode_video_close( sout_stream_t *p_stream,
1933 sout_stream_id_t *id )
1937 if( p_stream->p_sys->i_threads >= 1 )
1939 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1940 p_stream->p_sys->b_die = 1;
1941 vlc_cond_signal( &p_stream->p_sys->cond );
1942 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1943 vlc_thread_join( p_stream->p_sys );
1944 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1945 vlc_cond_destroy( &p_stream->p_sys->cond );
1949 if( id->p_decoder->p_module )
1950 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1952 if( id->p_decoder->p_owner )
1954 /* Clean-up pictures ring buffer */
1955 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1957 if( id->p_decoder->p_owner->pp_pics[i] )
1958 video_del_buffer( VLC_OBJECT(id->p_decoder),
1959 id->p_decoder->p_owner->pp_pics[i] );
1961 free( id->p_decoder->p_owner );
1965 if( id->p_encoder->p_module )
1966 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1969 for( i = 0; i < id->i_filter; i++ )
1971 vlc_object_detach( id->pp_filter[i] );
1972 if( id->pp_filter[i]->p_module )
1973 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1975 /* Clean-up pictures ring buffer */
1976 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1978 if( id->pp_filter[i]->p_owner->pp_pics[j] )
1979 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1980 id->pp_filter[i]->p_owner->pp_pics[j] );
1982 free( id->pp_filter[i]->p_owner );
1984 vlc_object_destroy( id->pp_filter[i] );
1986 for( i = 0; i < id->i_vfilter; i++ )
1988 vlc_object_detach( id->pp_vfilter[i] );
1989 if( id->pp_vfilter[i]->p_module )
1990 module_Unneed( id->pp_vfilter[i], id->pp_vfilter[i]->p_module );
1992 /* Clean-up pictures ring buffer */
1993 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1995 if( id->pp_vfilter[i]->p_owner->pp_pics[j] )
1996 video_del_buffer( VLC_OBJECT(id->pp_vfilter[i]),
1997 id->pp_vfilter[i]->p_owner->pp_pics[j] );
1999 free( id->pp_vfilter[i]->p_owner );
2001 vlc_object_destroy( id->pp_vfilter[i] );
2005 static int transcode_video_process( sout_stream_t *p_stream,
2006 sout_stream_id_t *id,
2007 block_t *in, block_t **out )
2009 sout_stream_sys_t *p_sys = p_stream->p_sys;
2010 int i_duplicate = 1, i;
2011 picture_t *p_pic, *p_pic2 = NULL;
2014 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
2016 subpicture_t *p_subpic = 0;
2017 stats_UpdateInteger( p_stream->p_parent->p_parent, STATS_DECODED_VIDEO,
2020 if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
2022 mtime_t current_date = mdate();
2023 if( current_date + 50000 > p_pic->date )
2025 msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
2026 current_date + 50000 - p_pic->date );
2027 p_pic->pf_release( p_pic );
2032 if( p_sys->b_master_sync )
2034 mtime_t i_video_drift;
2035 mtime_t i_master_drift = p_sys->i_master_drift;
2038 i_pts = date_Get( &id->interpolated_pts ) + 1;
2039 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
2040 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2042 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2043 date_Set( &id->interpolated_pts, p_pic->date );
2044 i_pts = p_pic->date + 1;
2046 i_video_drift = p_pic->date - i_pts;
2049 /* Set the pts of the frame being encoded */
2050 p_pic->date = i_pts;
2052 if( i_video_drift < i_master_drift - 50000 )
2055 msg_Dbg( p_stream, "dropping frame (%i)",
2056 (int)(i_video_drift - i_master_drift) );
2058 p_pic->pf_release( p_pic );
2061 else if( i_video_drift > i_master_drift + 50000 )
2064 msg_Dbg( p_stream, "adding frame (%i)",
2065 (int)(i_video_drift - i_master_drift) );
2071 if( !id->p_encoder->p_module )
2073 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
2075 p_pic->pf_release( p_pic );
2076 transcode_video_close( p_stream, id );
2077 id->b_transcode = VLC_FALSE;
2078 return VLC_EGENERIC;
2082 if( p_stream->p_sys->b_deinterlace )
2084 id->pp_filter[id->i_filter] =
2085 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
2086 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
2088 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
2089 video_new_buffer_filter;
2090 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
2091 video_del_buffer_filter;
2093 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
2094 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
2095 id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
2096 id->pp_filter[id->i_filter]->p_module =
2097 module_Need( id->pp_filter[id->i_filter],
2098 "video filter2", p_sys->psz_deinterlace, 0 );
2099 if( id->pp_filter[id->i_filter]->p_module )
2101 id->pp_filter[id->i_filter]->p_owner =
2102 malloc( sizeof(filter_owner_sys_t) );
2103 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2104 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
2105 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
2111 msg_Dbg( p_stream, "no video filter found" );
2112 vlc_object_detach( id->pp_filter[id->i_filter] );
2113 vlc_object_destroy( id->pp_filter[id->i_filter] );
2117 /* Check if we need a filter for chroma conversion or resizing */
2118 if( id->p_decoder->fmt_out.video.i_chroma !=
2119 id->p_encoder->fmt_in.video.i_chroma ||
2121 (int)id->p_decoder->fmt_out.video.i_width != p_sys->i_crop_width ||
2122 p_sys->i_crop_width != p_sys->i_nopadd_width ||
2123 p_sys->i_nopadd_width != (int)id->p_encoder->fmt_out.video.i_width ||
2125 (int)id->p_decoder->fmt_out.video.i_height != p_sys->i_crop_height ||
2126 p_sys->i_crop_height != p_sys->i_nopadd_height ||
2127 p_sys->i_nopadd_height != (int)id->p_encoder->fmt_out.video.i_height)
2129 id->pp_filter[id->i_filter] =
2130 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
2131 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
2133 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
2134 video_new_buffer_filter;
2135 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
2136 video_del_buffer_filter;
2138 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
2139 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
2140 id->pp_filter[id->i_filter]->p_cfg = NULL;
2143 id->pp_filter[id->i_filter]->fmt_in.video.i_x_offset = p_sys->i_src_x_offset;
2144 id->pp_filter[id->i_filter]->fmt_in.video.i_y_offset = p_sys->i_src_y_offset;
2145 id->pp_filter[id->i_filter]->fmt_in.video.i_visible_width = p_sys->i_crop_width;
2146 id->pp_filter[id->i_filter]->fmt_in.video.i_visible_height = p_sys->i_crop_height;
2148 id->pp_filter[id->i_filter]->fmt_out.video.i_x_offset = p_sys->i_dst_x_offset;
2149 id->pp_filter[id->i_filter]->fmt_out.video.i_y_offset = p_sys->i_dst_y_offset;
2150 id->pp_filter[id->i_filter]->fmt_out.video.i_visible_width = p_sys->i_nopadd_width;
2151 id->pp_filter[id->i_filter]->fmt_out.video.i_visible_height = p_sys->i_nopadd_height;
2153 id->pp_filter[id->i_filter]->p_module =
2154 module_Need( id->pp_filter[id->i_filter],
2155 "crop padd", 0, 0 );
2156 if( id->pp_filter[id->i_filter]->p_module )
2158 id->pp_filter[id->i_filter]->p_owner =
2159 malloc( sizeof(filter_owner_sys_t) );
2160 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2161 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
2162 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
2168 msg_Dbg( p_stream, "no video filter found" );
2169 vlc_object_detach( id->pp_filter[id->i_filter] );
2170 vlc_object_destroy( id->pp_filter[id->i_filter] );
2172 p_pic->pf_release( p_pic );
2173 transcode_video_close( p_stream, id );
2174 id->b_transcode = VLC_FALSE;
2175 return VLC_EGENERIC;
2179 for( i = 0; i < p_sys->i_vfilters; i++ )
2181 id->pp_vfilter[id->i_vfilter] =
2182 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
2183 vlc_object_attach( id->pp_vfilter[id->i_vfilter], p_stream );
2185 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_new =
2186 video_new_buffer_filter;
2187 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_del =
2188 video_del_buffer_filter;
2190 id->pp_vfilter[id->i_vfilter]->fmt_in = id->p_encoder->fmt_in;
2191 id->pp_vfilter[id->i_vfilter]->fmt_out = id->p_encoder->fmt_in;
2192 id->pp_vfilter[id->i_vfilter]->p_cfg = p_sys->p_vfilters_cfg[i];
2193 id->pp_vfilter[id->i_vfilter]->p_module =
2194 module_Need( id->pp_vfilter[id->i_vfilter],
2195 "video filter2", p_sys->psz_vfilters[i], 0 );
2196 if( id->pp_vfilter[id->i_vfilter]->p_module )
2198 id->pp_vfilter[id->i_vfilter]->p_owner =
2199 malloc( sizeof(filter_owner_sys_t) );
2200 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2201 id->pp_vfilter[id->i_vfilter]->p_owner->pp_pics[i] = 0;
2202 id->pp_vfilter[id->i_vfilter]->p_owner->p_sys = p_sys;
2208 msg_Dbg( p_stream, "no video filter found" );
2209 vlc_object_detach( id->pp_vfilter[id->i_vfilter] );
2210 vlc_object_destroy( id->pp_vfilter[id->i_vfilter] );
2215 /* Run filter chain */
2216 for( i = 0; i < id->i_filter; i++ )
2218 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
2225 /* Check if we have a subpicture to overlay */
2228 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
2229 /* TODO: get another pic */
2232 /* Overlay subpicture */
2235 int i_scale_width, i_scale_height;
2236 video_format_t *p_fmt;
2238 i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
2239 id->p_decoder->fmt_out.video.i_width;
2240 i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
2241 id->p_decoder->fmt_out.video.i_height;
2243 if( p_pic->i_refcount && !id->i_filter )
2245 /* We can't modify the picture, we need to duplicate it */
2246 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
2249 vout_CopyPicture( p_stream, p_tmp, p_pic );
2250 p_pic->pf_release( p_pic );
2256 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
2258 p_fmt = &id->p_decoder->fmt_out.video;
2260 /* FIXME (shouldn't have to be done here) */
2261 p_fmt->i_sar_num = p_fmt->i_aspect *
2262 p_fmt->i_height / p_fmt->i_width;
2263 p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
2265 spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
2266 i_scale_width, i_scale_height );
2269 /* Run vfilter chain */
2270 for( i = 0; i < id->i_vfilter; i++ )
2272 p_pic = id->pp_vfilter[i]->pf_video_filter(id->pp_vfilter[i], p_pic);
2275 if( p_sys->i_threads == 0 )
2278 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2279 block_ChainAppend( out, p_block );
2282 if( p_sys->b_master_sync )
2284 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
2285 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
2286 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2288 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2289 date_Set( &id->interpolated_pts, p_pic->date );
2290 i_pts = p_pic->date + 1;
2292 date_Increment( &id->interpolated_pts, 1 );
2295 if( p_sys->b_master_sync && i_duplicate > 1 )
2297 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
2298 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
2299 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2301 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2302 date_Set( &id->interpolated_pts, p_pic->date );
2303 i_pts = p_pic->date + 1;
2305 date_Increment( &id->interpolated_pts, 1 );
2307 if( p_sys->i_threads >= 1 )
2309 /* We can't modify the picture, we need to duplicate it */
2310 p_pic2 = video_new_buffer_decoder( id->p_decoder );
2311 if( p_pic2 != NULL )
2313 vout_CopyPicture( p_stream, p_pic2, p_pic );
2314 p_pic2->date = i_pts;
2320 p_pic->date = i_pts;
2321 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
2322 block_ChainAppend( out, p_block );
2326 if( p_sys->i_threads == 0 )
2328 p_pic->pf_release( p_pic );
2332 vlc_mutex_lock( &p_sys->lock_out );
2333 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
2334 p_sys->i_last_pic %= PICTURE_RING_SIZE;
2335 *out = p_sys->p_buffers;
2336 p_sys->p_buffers = NULL;
2337 if( p_pic2 != NULL )
2339 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic2;
2340 p_sys->i_last_pic %= PICTURE_RING_SIZE;
2342 vlc_cond_signal( &p_sys->cond );
2343 vlc_mutex_unlock( &p_sys->lock_out );
2350 static int EncoderThread( sout_stream_sys_t *p_sys )
2352 sout_stream_id_t *id = p_sys->id_video;
2355 while( !p_sys->b_die && !p_sys->b_error )
2359 vlc_mutex_lock( &p_sys->lock_out );
2360 while( p_sys->i_last_pic == p_sys->i_first_pic )
2362 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
2363 if( p_sys->b_die || p_sys->b_error ) break;
2365 if( p_sys->b_die || p_sys->b_error )
2367 vlc_mutex_unlock( &p_sys->lock_out );
2371 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2372 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2373 vlc_mutex_unlock( &p_sys->lock_out );
2375 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2376 vlc_mutex_lock( &p_sys->lock_out );
2377 block_ChainAppend( &p_sys->p_buffers, p_block );
2379 vlc_mutex_unlock( &p_sys->lock_out );
2381 p_pic->pf_release( p_pic );
2384 while( p_sys->i_last_pic != p_sys->i_first_pic )
2386 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2387 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2389 p_pic->pf_release( p_pic );
2392 block_ChainRelease( p_sys->p_buffers );
2397 struct picture_sys_t
2399 vlc_object_t *p_owner;
2402 static void video_release_buffer( picture_t *p_pic )
2404 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
2406 video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
2408 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
2411 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
2412 sout_stream_sys_t *p_sys )
2414 decoder_t *p_dec = (decoder_t *)p_this;
2418 /* Find an empty space in the picture ring buffer */
2419 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2421 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2423 pp_ring[i]->i_status = RESERVED_PICTURE;
2427 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2429 if( pp_ring[i] == 0 ) break;
2432 if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
2434 int i_first_pic = p_sys->i_first_pic;
2436 if( p_sys->i_first_pic != p_sys->i_last_pic )
2438 /* Encoder still has stuff to encode, wait to clear-up the list */
2439 while( p_sys->i_first_pic == i_first_pic )
2443 /* Find an empty space in the picture ring buffer */
2444 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2446 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2448 pp_ring[i]->i_status = RESERVED_PICTURE;
2452 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2454 if( pp_ring[i] == 0 ) break;
2458 if( i == PICTURE_RING_SIZE )
2460 msg_Err( p_this, "decoder/filter is leaking pictures, "
2461 "resetting its ring buffer" );
2463 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2465 pp_ring[i]->pf_release( pp_ring[i] );
2471 p_pic = malloc( sizeof(picture_t) );
2472 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2473 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
2474 p_dec->fmt_out.video.i_chroma,
2475 p_dec->fmt_out.video.i_width,
2476 p_dec->fmt_out.video.i_height,
2477 p_dec->fmt_out.video.i_aspect );
2479 if( !p_pic->i_planes )
2485 p_pic->pf_release = video_release_buffer;
2486 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2487 p_pic->p_sys->p_owner = p_this;
2488 p_pic->i_status = RESERVED_PICTURE;
2495 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
2497 return video_new_buffer( VLC_OBJECT(p_dec),
2498 p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2501 static picture_t *video_new_buffer_filter( filter_t *p_filter )
2503 return video_new_buffer( VLC_OBJECT(p_filter),
2504 p_filter->p_owner->pp_pics,
2505 p_filter->p_owner->p_sys );
2508 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2510 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2511 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
2512 if( p_pic ) free( p_pic );
2515 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
2517 p_pic->i_refcount = 0;
2518 p_pic->i_status = DESTROYED_PICTURE;
2521 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
2523 p_pic->i_refcount = 0;
2524 p_pic->i_status = DESTROYED_PICTURE;
2527 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2529 p_pic->i_refcount++;
2532 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2534 video_release_buffer( p_pic );
2540 static subpicture_t *spu_new_buffer( decoder_t * );
2541 static void spu_del_buffer( decoder_t *, subpicture_t * );
2543 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2545 sout_stream_sys_t *p_sys = p_stream->p_sys;
2551 /* Initialization of decoder structures */
2552 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2553 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2554 id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2555 /* id->p_decoder->p_cfg = p_sys->p_spu_cfg; */
2557 id->p_decoder->p_module =
2558 module_Need( id->p_decoder, "decoder", "$codec", 0 );
2560 if( !id->p_decoder->p_module )
2562 msg_Err( p_stream, "cannot find decoder" );
2563 return VLC_EGENERIC;
2566 if( !p_sys->b_soverlay )
2572 /* Initialization of encoder format structures */
2573 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
2574 id->p_decoder->fmt_in.i_codec );
2576 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2578 id->p_encoder->p_module =
2579 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2581 if( !id->p_encoder->p_module )
2583 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2584 msg_Err( p_stream, "cannot find encoder" );
2585 return VLC_EGENERIC;
2591 p_sys->p_spu = spu_Create( p_stream );
2592 spu_Init( p_sys->p_spu );
2598 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2601 if( id->p_decoder->p_module )
2602 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2605 if( id->p_encoder->p_module )
2606 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2609 static int transcode_spu_process( sout_stream_t *p_stream,
2610 sout_stream_id_t *id,
2611 block_t *in, block_t **out )
2613 sout_stream_sys_t *p_sys = p_stream->p_sys;
2614 subpicture_t *p_subpic;
2617 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2618 if( !p_subpic ) return VLC_EGENERIC;
2620 if( p_sys->b_master_sync && p_sys->i_master_drift )
2622 p_subpic->i_start -= p_sys->i_master_drift;
2623 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2626 if( p_sys->b_soverlay )
2628 spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2634 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2635 spu_del_buffer( id->p_decoder, p_subpic );
2639 block_ChainAppend( out, p_block );
2644 return VLC_EGENERIC;
2647 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2649 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2650 return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2653 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2655 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2656 spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );
2662 static int transcode_osd_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2664 sout_stream_sys_t *p_sys = p_stream->p_sys;
2668 fmt.i_id = 0xbd1f; /* pid ?? */
2669 fmt.i_group = 3; /* pmt entry ?? */
2670 fmt.i_codec = VLC_FOURCC( 'Y', 'U', 'V', 'A' );
2671 fmt.psz_language = strdup( "osd" );
2673 id = malloc( sizeof( sout_stream_id_t ) );
2674 memset( id, 0, sizeof(sout_stream_id_t) );
2677 id->p_decoder = NULL;
2678 id->p_encoder = NULL;
2680 /* Create encoder object */
2681 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
2682 if( !id->p_encoder )
2684 msg_Err( p_stream, "out of memory" );
2687 vlc_object_attach( id->p_encoder, p_stream );
2688 id->p_encoder->p_module = NULL;
2690 /* Create fake destination format */
2691 es_format_Init( &id->p_encoder->fmt_out, fmt.i_cat, 0 );
2692 id->p_encoder->fmt_out.i_id = fmt.i_id;
2693 id->p_encoder->fmt_out.i_group = fmt.i_group;
2694 id->p_encoder->fmt_out.psz_language = strdup( fmt.psz_language );
2696 if( p_sys->i_osdcodec != 0 || p_sys->psz_osdenc )
2698 msg_Dbg( p_stream, "creating osdmenu transcoding from fcc=`%4.4s' "
2699 "to fcc=`%4.4s'", (char*)&fmt.i_codec,
2700 (char*)&p_sys->i_osdcodec );
2702 /* Complete destination format */
2703 id->p_encoder->fmt_out.i_codec = p_sys->i_osdcodec;
2709 /* Initialization of encoder format structures */
2710 es_format_Init( &id->p_encoder->fmt_in, fmt.i_cat, fmt.i_codec );
2711 id->p_encoder->fmt_in.psz_language = strdup( fmt.psz_language );
2713 id->p_encoder->p_cfg = p_sys->p_osd_cfg;
2715 id->p_encoder->p_module =
2716 module_Need( id->p_encoder, "encoder", p_sys->psz_osdenc, VLC_TRUE );
2718 if( !id->p_encoder->p_module )
2720 msg_Err( p_stream, "cannot find encoder" );
2724 /* open output stream */
2725 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
2726 id->b_transcode = VLC_TRUE;
2728 if( !id->id ) goto error;
2732 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
2733 (char*)&fmt.i_codec );
2734 id->id = p_sys->p_out->pf_add( p_sys->p_out, &fmt );
2735 id->b_transcode = VLC_FALSE;
2737 if( !id->id ) goto error;
2741 p_sys->b_es_osd = VLC_TRUE;
2745 p_sys->p_spu = spu_Create( p_stream );
2746 if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
2747 msg_Err( p_sys, "spu initialisation failed" );
2750 if( fmt.psz_language )
2751 free( fmt.psz_language );
2756 msg_Err( p_stream, "starting osd encoding thread failed" );
2757 if( id->p_encoder->p_module )
2758 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2761 vlc_object_detach( id->p_encoder );
2762 vlc_object_destroy( id->p_encoder );
2764 if( fmt.psz_language ) free( fmt.psz_language );
2765 if( id ) free( id );
2766 p_sys->id_osd = NULL;
2767 p_sys->b_es_osd = VLC_FALSE;
2768 return VLC_EGENERIC;
2771 static void transcode_osd_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2773 sout_stream_sys_t *p_sys = p_stream->p_sys;
2776 if( p_sys->b_es_osd && id )
2778 if( id->p_encoder->p_module )
2779 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2781 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
2785 vlc_object_detach( id->p_encoder );
2786 vlc_object_destroy( id->p_encoder );
2789 p_sys->b_es_osd = VLC_FALSE;
2790 if( id ) free( id );
2793 static int transcode_osd_process( sout_stream_t *p_stream,
2794 sout_stream_id_t *id,
2795 block_t *in, block_t **out )
2797 sout_stream_sys_t *p_sys = p_stream->p_sys;
2798 subpicture_t *p_subpic = NULL;
2800 /* Check if we have a subpicture to send */
2801 if( p_sys->p_spu && in->i_dts > 0)
2803 p_subpic = spu_SortSubpictures( p_sys->p_spu, in->i_dts );
2807 msg_Warn( p_stream, "spu channel not initialized, doing it now" );
2810 p_sys->p_spu = spu_Create( p_stream );
2811 if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
2812 msg_Err( p_stream, "spu initialisation failed" );
2818 block_t *p_block = NULL;
2820 if( p_sys->b_master_sync && p_sys->i_master_drift )
2822 p_subpic->i_start -= p_sys->i_master_drift;
2823 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2826 p_block = p_sys->id_osd->p_encoder->pf_encode_sub( p_sys->id_osd->p_encoder, p_subpic );
2829 p_block->i_dts = p_block->i_pts = in->i_dts;
2830 block_ChainAppend( out, p_block );
2833 if( p_sys->p_out->pf_send( p_sys->p_out, p_sys->id_osd->id, *out ) == VLC_SUCCESS )
2834 spu_DestroySubpicture( p_sys->p_spu, p_subpic );
2839 return VLC_EGENERIC;