1 /*****************************************************************************
2 * transcode.c: transcoding stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc/input.h>
36 #include <vlc/decoder.h>
37 #include "vlc_filter.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 #define VENC_TEXT N_("Video encoder")
44 #define VENC_LONGTEXT N_( \
45 "Allows you to specify the video encoder to use and its associated " \
47 #define VCODEC_TEXT N_("Destination video codec")
48 #define VCODEC_LONGTEXT N_( \
49 "Allows you to specify the destination video codec used for the " \
51 #define VB_TEXT N_("Video bitrate")
52 #define VB_LONGTEXT N_( \
53 "Allows you to specify the video bitrate used for the streaming " \
55 #define SCALE_TEXT N_("Video scaling")
56 #define SCALE_LONGTEXT N_( \
57 "Allows you to scale the video before encoding." )
58 #define FPS_TEXT N_("Video frame-rate")
59 #define FPS_LONGTEXT N_( \
60 "Allows you to specify an output frame rate for the video." )
61 #define DEINTERLACE_TEXT N_("Deinterlace video")
62 #define DEINTERLACE_LONGTEXT N_( \
63 "Allows you to deinterlace the video before encoding." )
64 #define WIDTH_TEXT N_("Video width")
65 #define WIDTH_LONGTEXT N_( \
66 "Allows you to specify the output video width." )
67 #define HEIGHT_TEXT N_("Video height")
68 #define HEIGHT_LONGTEXT N_( \
69 "Allows you to specify the output video height." )
71 #define CROPTOP_TEXT N_("Video crop top")
72 #define CROPTOP_LONGTEXT N_( \
73 "Allows you to specify the top coordinate for the video cropping." )
74 #define CROPLEFT_TEXT N_("Video crop left")
75 #define CROPLEFT_LONGTEXT N_( \
76 "Allows you to specify the left coordinate for the video cropping." )
77 #define CROPBOTTOM_TEXT N_("Video crop bottom")
78 #define CROPBOTTOM_LONGTEXT N_( \
79 "Allows you to specify the bottom coordinate for the video cropping." )
80 #define CROPRIGHT_TEXT N_("Video crop right")
81 #define CROPRIGHT_LONGTEXT N_( \
82 "Allows you to specify the right coordinate for the video cropping." )
84 #define AENC_TEXT N_("Audio encoder")
85 #define AENC_LONGTEXT N_( \
86 "Allows you to specify the audio encoder to use and its associated " \
88 #define ACODEC_TEXT N_("Destination audio codec")
89 #define ACODEC_LONGTEXT N_( \
90 "Allows you to specify the destination audio codec used for the " \
92 #define AB_TEXT N_("Audio bitrate")
93 #define AB_LONGTEXT N_( \
94 "Allows you to specify the audio bitrate used for the streaming " \
96 #define ARATE_TEXT N_("Audio sample rate")
97 #define ARATE_LONGTEXT N_( \
98 "Allows you to specify the audio sample rate used for the streaming " \
100 #define ACHANS_TEXT N_("Audio channels")
101 #define ACHANS_LONGTEXT N_( \
102 "Allows you to specify the number of audio channels used for the " \
103 "streaming output." )
105 #define SENC_TEXT N_("Subtitles encoder")
106 #define SENC_LONGTEXT N_( \
107 "Allows you to specify the subtitles encoder to use and its associated " \
109 #define SCODEC_TEXT N_("Destination subtitles codec")
110 #define SCODEC_LONGTEXT N_( \
111 "Allows you to specify the destination subtitles codec used for the " \
112 "streaming output." )
113 #define SFILTER_TEXT N_("Subpictures filter")
114 #define SFILTER_LONGTEXT N_( \
115 "Allows you to specify subpictures filters used during the video " \
116 "transcoding. The subpictures produced by the filters will be overlayed " \
117 "directly onto the video." )
119 #define THREADS_TEXT N_("Number of threads")
120 #define THREADS_LONGTEXT N_( \
121 "Allows you to specify the number of threads used for the transcoding." )
123 #define ASYNC_TEXT N_("Synchronise on audio track")
124 #define ASYNC_LONGTEXT N_( \
125 "This option will drop/duplicate video frames to synchronise the video " \
126 "track on the audio track." )
128 static int Open ( vlc_object_t * );
129 static void Close( vlc_object_t * );
131 #define SOUT_CFG_PREFIX "sout-transcode-"
134 set_shortname( _("Transcode"));
135 set_description( _("Transcode stream output") );
136 set_capability( "sout stream", 50 );
137 add_shortcut( "transcode" );
138 set_callbacks( Open, Close );
139 set_category( CAT_SOUT );
140 set_subcategory( SUBCAT_SOUT_STREAM );
141 set_section( N_("Video"), NULL );
142 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
143 VENC_LONGTEXT, VLC_FALSE );
144 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
145 VCODEC_LONGTEXT, VLC_FALSE );
146 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
147 VB_LONGTEXT, VLC_FALSE );
148 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
149 SCALE_LONGTEXT, VLC_FALSE );
150 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
151 FPS_LONGTEXT, VLC_FALSE );
152 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
153 DEINTERLACE_LONGTEXT, VLC_FALSE );
154 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
155 WIDTH_LONGTEXT, VLC_TRUE );
156 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
157 HEIGHT_LONGTEXT, VLC_TRUE );
159 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
160 CROPTOP_LONGTEXT, VLC_TRUE );
161 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
162 CROPLEFT_LONGTEXT, VLC_TRUE );
163 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
164 CROPBOTTOM_LONGTEXT, VLC_TRUE );
165 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
166 CROPRIGHT_LONGTEXT, VLC_TRUE );
168 set_section( N_("Audio"), NULL );
169 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
170 AENC_LONGTEXT, VLC_FALSE );
171 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
172 ACODEC_LONGTEXT, VLC_FALSE );
173 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
174 AB_LONGTEXT, VLC_FALSE );
175 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
176 ACHANS_LONGTEXT, VLC_FALSE );
177 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
178 ARATE_LONGTEXT, VLC_TRUE );
179 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
180 ASYNC_LONGTEXT, VLC_FALSE );
182 set_section( N_("Overlays/Subtitles"), NULL );
183 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
184 SENC_LONGTEXT, VLC_FALSE );
185 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
186 SCODEC_LONGTEXT, VLC_FALSE );
187 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
188 SCODEC_LONGTEXT, VLC_FALSE );
189 add_module_list_cat( SOUT_CFG_PREFIX "sfilter", SUBCAT_VIDEO_SUBPIC,
191 SFILTER_TEXT, SFILTER_LONGTEXT, VLC_FALSE );
193 set_section( N_("Miscellaneous"), NULL );
194 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
195 THREADS_LONGTEXT, VLC_TRUE );
199 static const char *ppsz_sout_options[] = {
200 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
201 "scale", "fps", "width", "height", "deinterlace", "threads",
202 "aenc", "acodec", "ab", "samplerate", "channels",
203 "senc", "scodec", "soverlay", "sfilter",
207 /*****************************************************************************
208 * Exported prototypes
209 *****************************************************************************/
210 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
211 static int Del ( sout_stream_t *, sout_stream_id_t * );
212 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
214 static int transcode_audio_new ( sout_stream_t *, sout_stream_id_t * );
215 static void transcode_audio_close ( sout_stream_t *, sout_stream_id_t * );
216 static int transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
217 block_t *, block_t ** );
219 static aout_buffer_t *audio_new_buffer( decoder_t *, int );
220 static void audio_del_buffer( decoder_t *, aout_buffer_t * );
222 static int transcode_video_new ( sout_stream_t *, sout_stream_id_t * );
223 static void transcode_video_close ( sout_stream_t *, sout_stream_id_t * );
224 static int transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
225 static int transcode_video_process( sout_stream_t *, sout_stream_id_t *,
226 block_t *, block_t ** );
228 static void video_del_buffer( vlc_object_t *, picture_t * );
229 static picture_t *video_new_buffer_decoder( decoder_t * );
230 static void video_del_buffer_decoder( decoder_t *, picture_t * );
231 static void video_link_picture_decoder( decoder_t *, picture_t * );
232 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
233 static picture_t *video_new_buffer_filter( filter_t * );
234 static void video_del_buffer_filter( filter_t *, picture_t * );
236 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
237 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
238 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
239 block_t *, block_t ** );
241 static int EncoderThread( struct sout_stream_sys_t * p_sys );
243 static int pi_channels_maps[6] =
246 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
247 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
248 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
249 | AOUT_CHAN_REARRIGHT,
250 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
251 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
254 #define PICTURE_RING_SIZE 64
255 #define SUBPICTURE_RING_SIZE 20
257 struct sout_stream_sys_t
261 sout_stream_t *p_out;
262 sout_stream_id_t *id_video;
264 vlc_mutex_t lock_out;
266 picture_t * pp_pics[PICTURE_RING_SIZE];
267 int i_first_pic, i_last_pic;
270 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
272 sout_cfg_t *p_audio_cfg;
278 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
280 sout_cfg_t *p_video_cfg;
286 vlc_bool_t b_deinterlace;
295 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
297 vlc_bool_t b_soverlay;
298 sout_cfg_t *p_spu_cfg;
302 vlc_bool_t b_master_sync;
303 mtime_t i_master_drift;
306 struct decoder_owner_sys_t
308 picture_t *pp_pics[PICTURE_RING_SIZE];
310 struct filter_owner_sys_t
312 picture_t *pp_pics[PICTURE_RING_SIZE];
315 /*****************************************************************************
317 *****************************************************************************/
318 static int Open( vlc_object_t *p_this )
320 sout_stream_t *p_stream = (sout_stream_t*)p_this;
321 sout_stream_sys_t *p_sys;
324 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
326 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
329 msg_Err( p_stream, "cannot create chain" );
330 vlc_object_destroy( p_sys );
334 p_sys->i_master_drift = 0;
336 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
339 /* Audio transcoding parameters */
340 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
341 p_sys->psz_aenc = NULL;
342 p_sys->p_audio_cfg = NULL;
343 if( val.psz_string && *val.psz_string )
346 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
348 if( psz_next ) free( psz_next );
350 if( val.psz_string ) free( val.psz_string );
352 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
354 if( val.psz_string && *val.psz_string )
357 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
358 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
360 if( val.psz_string ) free( val.psz_string );
362 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
363 p_sys->i_abitrate = val.i_int;
364 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
366 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
367 p_sys->i_sample_rate = val.i_int;
369 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
370 p_sys->i_channels = val.i_int;
372 if( p_sys->i_acodec )
374 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
375 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
376 p_sys->i_channels, p_sys->i_abitrate / 1000 );
379 /* Video transcoding parameters */
380 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
381 p_sys->psz_venc = NULL;
382 p_sys->p_video_cfg = NULL;
383 if( val.psz_string && *val.psz_string )
386 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
388 if( psz_next ) free( psz_next );
390 if( val.psz_string ) free( val.psz_string );
392 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
394 if( val.psz_string && *val.psz_string )
397 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
398 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
400 if( val.psz_string ) free( val.psz_string );
402 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
403 p_sys->i_vbitrate = val.i_int;
404 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
406 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
407 p_sys->f_scale = val.f_float;
409 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
410 p_sys->f_fps = val.f_float;
412 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
413 p_sys->i_width = val.i_int;
415 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
416 p_sys->i_height = val.i_int;
418 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
419 p_sys->b_deinterlace = val.b_bool;
421 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
422 p_sys->i_crop_top = val.i_int;
423 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
424 p_sys->i_crop_bottom = val.i_int;
425 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
426 p_sys->i_crop_left = val.i_int;
427 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
428 p_sys->i_crop_right = val.i_int;
430 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
431 p_sys->i_threads = val.i_int;
433 if( p_sys->i_vcodec )
435 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
436 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
437 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
440 /* Subpictures transcoding parameters */
442 p_sys->psz_senc = NULL;
443 p_sys->p_spu_cfg = NULL;
446 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
447 if( val.psz_string && *val.psz_string )
450 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
452 if( psz_next ) free( psz_next );
454 if( val.psz_string ) free( val.psz_string );
456 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
457 if( val.psz_string && *val.psz_string )
460 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
461 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
463 if( val.psz_string ) free( val.psz_string );
465 if( p_sys->i_scodec )
467 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
470 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
471 p_sys->b_soverlay = val.b_bool;
473 var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
474 if( val.psz_string && *val.psz_string )
476 p_sys->p_spu = spu_Create( p_stream );
477 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
478 var_Set( p_sys->p_spu, "sub-filter", val );
479 spu_Init( p_sys->p_spu );
481 if( val.psz_string ) free( val.psz_string );
483 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
484 p_sys->b_master_sync = val.b_bool;
485 if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
487 p_stream->pf_add = Add;
488 p_stream->pf_del = Del;
489 p_stream->pf_send = Send;
490 p_stream->p_sys = p_sys;
495 /*****************************************************************************
497 *****************************************************************************/
498 static void Close( vlc_object_t * p_this )
500 sout_stream_t *p_stream = (sout_stream_t*)p_this;
501 sout_stream_sys_t *p_sys = p_stream->p_sys;
503 sout_StreamDelete( p_sys->p_out );
505 while( p_sys->p_audio_cfg != NULL )
507 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
509 if( p_sys->p_audio_cfg->psz_name )
510 free( p_sys->p_audio_cfg->psz_name );
511 if( p_sys->p_audio_cfg->psz_value )
512 free( p_sys->p_audio_cfg->psz_value );
513 free( p_sys->p_audio_cfg );
515 p_sys->p_audio_cfg = p_next;
517 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
519 while( p_sys->p_video_cfg != NULL )
521 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
523 if( p_sys->p_video_cfg->psz_name )
524 free( p_sys->p_video_cfg->psz_name );
525 if( p_sys->p_video_cfg->psz_value )
526 free( p_sys->p_video_cfg->psz_value );
527 free( p_sys->p_video_cfg );
529 p_sys->p_video_cfg = p_next;
531 if( p_sys->psz_venc ) free( p_sys->psz_venc );
533 while( p_sys->p_spu_cfg != NULL )
535 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
537 if( p_sys->p_spu_cfg->psz_name )
538 free( p_sys->p_spu_cfg->psz_name );
539 if( p_sys->p_spu_cfg->psz_value )
540 free( p_sys->p_spu_cfg->psz_value );
541 free( p_sys->p_spu_cfg );
543 p_sys->p_spu_cfg = p_next;
545 if( p_sys->psz_senc ) free( p_sys->psz_senc );
547 if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
549 vlc_object_destroy( p_sys );
552 struct sout_stream_id_t
554 vlc_fourcc_t b_transcode;
556 /* id of the out stream */
560 decoder_t *p_decoder;
563 filter_t *pp_filter[10];
567 encoder_t *p_encoder;
570 date_t interpolated_pts;
574 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
576 sout_stream_sys_t *p_sys = p_stream->p_sys;
577 sout_stream_id_t *id;
579 id = malloc( sizeof( sout_stream_id_t ) );
580 memset( id, 0, sizeof(sout_stream_id_t) );
583 id->p_decoder = NULL;
584 id->p_encoder = NULL;
586 /* Create decoder object */
587 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
590 msg_Err( p_stream, "out of memory" );
593 vlc_object_attach( id->p_decoder, p_stream );
594 id->p_decoder->p_module = NULL;
595 id->p_decoder->fmt_in = *p_fmt;
596 id->p_decoder->fmt_out = *p_fmt;
597 id->p_decoder->fmt_out.i_extra = 0;
598 id->p_decoder->fmt_out.p_extra = 0;
599 id->p_decoder->b_pace_control = VLC_TRUE;
601 /* Create encoder object */
602 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
605 msg_Err( p_stream, "out of memory" );
608 vlc_object_attach( id->p_encoder, p_stream );
609 id->p_encoder->p_module = NULL;
611 /* Create destination format */
612 es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
613 id->p_encoder->fmt_out.i_id = p_fmt->i_id;
614 id->p_encoder->fmt_out.i_group = p_fmt->i_group;
615 if( p_fmt->psz_language )
616 id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
618 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
621 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
622 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
624 /* Complete destination format */
625 id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
626 id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
627 p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
628 id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
629 p_sys->i_channels : p_fmt->audio.i_channels;
630 id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
631 id->p_encoder->fmt_out.audio.i_bitspersample =
632 p_fmt->audio.i_bitspersample;
634 /* Build decoder -> filter -> encoder chain */
635 if( transcode_audio_new( p_stream, id ) )
637 msg_Err( p_stream, "cannot create audio chain" );
641 /* Open output stream */
642 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
643 id->b_transcode = VLC_TRUE;
645 if( !id->id ) goto error;
647 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
649 else if( p_fmt->i_cat == VIDEO_ES &&
650 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
653 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
654 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
656 /* Complete destination format */
657 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
658 id->p_encoder->fmt_out.video.i_width = p_sys->i_width;
659 id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
660 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
662 /* Build decoder -> filter -> encoder chain */
663 if( transcode_video_new( p_stream, id ) )
665 msg_Err( p_stream, "cannot create video chain" );
669 /* Stream will be added later on because we don't know
670 * all the characteristics of the decoded stream yet */
671 id->b_transcode = VLC_TRUE;
673 if( p_sys->f_fps > 0 )
675 id->p_encoder->fmt_out.video.i_frame_rate =
676 nearbyint(p_sys->f_fps * 1001);
677 id->p_encoder->fmt_out.video.i_frame_rate_base = 1001;
680 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
682 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
683 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
684 (char*)&p_sys->i_scodec );
686 /* Complete destination format */
687 id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
689 /* build decoder -> filter -> encoder */
690 if( transcode_spu_new( p_stream, id ) )
692 msg_Err( p_stream, "cannot create subtitles chain" );
696 /* open output stream */
697 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
698 id->b_transcode = VLC_TRUE;
700 if( !id->id ) goto error;
702 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
704 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
705 (char*)&p_fmt->i_codec );
707 id->b_transcode = VLC_TRUE;
709 /* Build decoder -> filter -> overlaying chain */
710 if( transcode_spu_new( p_stream, id ) )
712 msg_Err( p_stream, "cannot create subtitles chain" );
718 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
719 (char*)&p_fmt->i_codec );
720 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
721 id->b_transcode = VLC_FALSE;
723 if( !id->id ) goto error;
731 vlc_object_detach( id->p_decoder );
732 vlc_object_destroy( id->p_decoder );
737 vlc_object_detach( id->p_encoder );
738 vlc_object_destroy( id->p_encoder );
745 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
747 sout_stream_sys_t *p_sys = p_stream->p_sys;
749 if( id->b_transcode )
751 switch( id->p_decoder->fmt_in.i_cat )
754 transcode_audio_close( p_stream, id );
757 transcode_video_close( p_stream, id );
760 transcode_spu_close( p_stream, id );
765 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
769 vlc_object_detach( id->p_decoder );
770 vlc_object_destroy( id->p_decoder );
775 vlc_object_detach( id->p_encoder );
776 vlc_object_destroy( id->p_encoder );
784 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
787 sout_stream_sys_t *p_sys = p_stream->p_sys;
790 if( !id->b_transcode && id->id )
792 if( p_sys->b_master_sync && p_sys->i_master_drift )
794 if( p_buffer->i_dts > 0 )
796 p_buffer->i_dts -= p_sys->i_master_drift;
797 if( p_buffer->i_dts < 0 )
799 block_Release( p_buffer );
803 if( p_buffer->i_pts > 0 )
805 p_buffer->i_pts -= p_sys->i_master_drift;
806 if( p_buffer->i_pts < 0 )
808 block_Release( p_buffer );
814 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
816 else if( !id->b_transcode )
818 block_Release( p_buffer );
822 switch( id->p_decoder->fmt_in.i_cat )
825 transcode_audio_process( p_stream, id, p_buffer, &p_out );
829 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
837 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
845 block_Release( p_buffer );
849 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
853 /****************************************************************************
854 * decoder reencoder part
855 ****************************************************************************/
856 int audio_BitsPerSample( vlc_fourcc_t i_format )
860 case VLC_FOURCC('u','8',' ',' '):
861 case VLC_FOURCC('s','8',' ',' '):
864 case VLC_FOURCC('u','1','6','l'):
865 case VLC_FOURCC('s','1','6','l'):
866 case VLC_FOURCC('u','1','6','b'):
867 case VLC_FOURCC('s','1','6','b'):
870 case VLC_FOURCC('u','2','4','l'):
871 case VLC_FOURCC('s','2','4','l'):
872 case VLC_FOURCC('u','2','4','b'):
873 case VLC_FOURCC('s','2','4','b'):
876 case VLC_FOURCC('u','3','2','l'):
877 case VLC_FOURCC('s','3','2','l'):
878 case VLC_FOURCC('u','3','2','b'):
879 case VLC_FOURCC('s','3','2','b'):
880 case VLC_FOURCC('f','l','3','2'):
881 case VLC_FOURCC('f','i','3','2'):
884 case VLC_FOURCC('f','l','6','4'):
891 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
892 sout_stream_id_t *id,
893 es_format_t *p_fmt_in,
894 es_format_t *p_fmt_out )
896 filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
898 vlc_object_attach( p_filter, p_stream );
899 p_filter->pf_audio_buffer_new = __block_New;
901 p_filter->fmt_in = *p_fmt_in;
902 p_filter->fmt_out = *p_fmt_out;
904 p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
905 if( p_filter->p_module )
907 p_filter->fmt_out.audio.i_bitspersample =
908 audio_BitsPerSample( p_filter->fmt_out.i_codec );
909 *p_fmt_in = p_filter->fmt_out;
913 vlc_object_detach( p_filter );
914 vlc_object_destroy( p_filter );
921 static int transcode_audio_new( sout_stream_t *p_stream,
922 sout_stream_id_t *id )
924 sout_stream_sys_t *p_sys = p_stream->p_sys;
925 es_format_t fmt_last;
932 /* Initialization of decoder structures */
933 id->p_decoder->pf_decode_audio = 0;
934 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
935 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
936 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
938 id->p_decoder->p_module =
939 module_Need( id->p_decoder, "decoder", "$codec", 0 );
941 if( !id->p_decoder->p_module )
943 msg_Err( p_stream, "cannot find decoder" );
946 id->p_decoder->fmt_out.audio.i_bitspersample =
947 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
948 fmt_last = id->p_decoder->fmt_out;
949 /* FIX decoders so we don't have to do this */
950 fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
956 /* Initialization of encoder format structures */
957 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
958 id->p_decoder->fmt_out.i_codec );
959 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
961 /* Sanity check for audio channels */
962 id->p_encoder->fmt_out.audio.i_channels =
963 __MIN( id->p_encoder->fmt_out.audio.i_channels,
964 id->p_decoder->fmt_out.audio.i_channels );
965 if( id->p_decoder->fmt_out.audio.i_channels ==
966 id->p_encoder->fmt_out.audio.i_channels )
967 id->p_encoder->fmt_out.audio.i_physical_channels =
968 id->p_encoder->fmt_out.audio.i_original_channels =
969 id->p_decoder->fmt_out.audio.i_physical_channels;
971 id->p_encoder->fmt_out.audio.i_physical_channels =
972 id->p_encoder->fmt_out.audio.i_original_channels =
973 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
975 /* Initialization of encoder format structures */
976 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
977 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
978 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
979 id->p_encoder->fmt_in.audio.i_physical_channels =
980 id->p_encoder->fmt_in.audio.i_original_channels =
981 id->p_encoder->fmt_out.audio.i_physical_channels;
982 id->p_encoder->fmt_in.audio.i_channels =
983 id->p_encoder->fmt_out.audio.i_channels;
984 id->p_encoder->fmt_in.audio.i_bitspersample =
985 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
987 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
989 id->p_encoder->p_module =
990 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
991 if( !id->p_encoder->p_module )
993 msg_Err( p_stream, "cannot find encoder" );
994 module_Unneed( id->p_decoder, id->p_decoder->p_module );
995 id->p_decoder->p_module = 0;
998 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
999 id->p_encoder->fmt_in.audio.i_bitspersample =
1000 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1002 /* Load conversion filters */
1003 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1004 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1006 /* We'll have to go through fl32 first */
1007 es_format_t fmt_out = id->p_encoder->fmt_in;
1008 fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1010 id->pp_filter[id->i_filter] =
1011 transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );
1013 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1018 if( fmt_last.audio.i_channels !=
1019 id->p_encoder->fmt_in.audio.i_channels ||
1020 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
1021 fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1023 id->pp_filter[id->i_filter] =
1024 transcode_audio_filter_new( p_stream, id, &fmt_last,
1025 &id->p_encoder->fmt_in );
1027 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1032 /* Final checks to see if conversions were successful */
1033 if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1035 msg_Dbg( p_stream, "no audio filter found (%4.4s->%4.4s)",
1036 (char *)&fmt_last.i_codec,
1037 (char *)&id->p_encoder->fmt_in.i_codec );
1038 transcode_audio_close( p_stream, id );
1039 return VLC_EGENERIC;
1042 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1044 msg_Dbg( p_stream, "no audio filter found for mixing from"
1045 " %i to %i channels", fmt_last.audio.i_channels,
1046 id->p_encoder->fmt_in.audio.i_channels );
1048 id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1049 id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1051 id->p_encoder->fmt_in.audio.i_physical_channels =
1052 id->p_encoder->fmt_in.audio.i_original_channels =
1053 fmt_last.audio.i_physical_channels;
1054 id->p_encoder->fmt_out.audio.i_physical_channels =
1055 id->p_encoder->fmt_out.audio.i_original_channels =
1056 fmt_last.audio.i_physical_channels;
1059 if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1061 msg_Dbg( p_stream, "no audio filter found for resampling from"
1062 " %iHz to %iHz", fmt_last.audio.i_rate,
1063 id->p_encoder->fmt_in.audio.i_rate );
1064 id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1065 id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1068 /* FIXME: Hack for mp3 transcoding support */
1069 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1070 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1075 static void transcode_audio_close( sout_stream_t *p_stream,
1076 sout_stream_id_t *id )
1081 if( id->p_decoder->p_module )
1082 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1083 id->p_decoder->p_module = 0;
1086 if( id->p_encoder->p_module )
1087 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1088 id->p_encoder->p_module = 0;
1091 for( i = 0; i < id->i_filter; i++ )
1093 vlc_object_detach( id->pp_filter[i] );
1094 if( id->pp_filter[i]->p_module )
1095 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1096 vlc_object_destroy( id->pp_filter[i] );
1100 static int transcode_audio_process( sout_stream_t *p_stream,
1101 sout_stream_id_t *id,
1102 block_t *in, block_t **out )
1104 sout_stream_sys_t *p_sys = p_stream->p_sys;
1105 aout_buffer_t *p_audio_buf;
1106 block_t *p_block, *p_audio_block;
1110 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1113 if( p_sys->b_master_sync )
1115 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1116 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1117 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1118 p_audio_buf->start_date -= p_sys->i_master_drift;
1119 p_audio_buf->end_date -= p_sys->i_master_drift;
1122 p_audio_block = p_audio_buf->p_sys;
1123 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1124 p_audio_block->i_dts = p_audio_block->i_pts =
1125 p_audio_buf->start_date;
1126 p_audio_block->i_length = p_audio_buf->end_date -
1127 p_audio_buf->start_date;
1128 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1130 /* Run filter chain */
1131 for( i = 0; i < id->i_filter; i++ )
1134 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1138 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1139 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1140 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1141 p_audio_buf->start_date = p_audio_block->i_dts;
1142 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1144 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1145 block_ChainAppend( out, p_block );
1146 block_Release( p_audio_block );
1147 free( p_audio_buf );
1153 static void audio_release_buffer( aout_buffer_t *p_buffer )
1155 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1156 if( p_buffer ) free( p_buffer );
1159 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1161 aout_buffer_t *p_buffer;
1165 if( p_dec->fmt_out.audio.i_bitspersample )
1167 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1168 p_dec->fmt_out.audio.i_channels;
1170 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1171 p_dec->fmt_out.audio.i_frame_length )
1173 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1174 p_dec->fmt_out.audio.i_frame_length;
1178 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1181 p_buffer = malloc( sizeof(aout_buffer_t) );
1182 p_buffer->pf_release = audio_release_buffer;
1183 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1185 p_buffer->p_buffer = p_block->p_buffer;
1186 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1187 p_buffer->i_nb_samples = i_samples;
1188 p_block->i_samples = i_samples;
1193 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1195 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1196 if( p_buffer ) free( p_buffer );
1202 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1204 sout_stream_sys_t *p_sys = p_stream->p_sys;
1211 /* Initialization of decoder structures */
1212 id->p_decoder->pf_decode_video = 0;
1213 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1214 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1215 id->p_decoder->pf_picture_link = video_link_picture_decoder;
1216 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
1217 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1218 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1219 id->p_decoder->p_owner->pp_pics[i] = 0;
1220 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1222 id->p_decoder->p_module =
1223 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1225 if( !id->p_decoder->p_module )
1227 msg_Err( p_stream, "cannot find decoder" );
1228 return VLC_EGENERIC;
1233 * Because some info about the decoded input will only be available
1234 * once the first frame is decoded, we actually only test the availability
1235 * of the encoder here.
1238 /* Initialization of encoder format structures */
1239 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1240 id->p_decoder->fmt_out.i_codec );
1241 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1243 /* The dimensions will be set properly later on.
1244 * Just put sensible values so we can test an encoder is available. */
1245 id->p_encoder->fmt_in.video.i_width =
1246 id->p_encoder->fmt_out.video.i_width ?
1247 id->p_encoder->fmt_out.video.i_width :
1248 id->p_decoder->fmt_in.video.i_width ?
1249 id->p_decoder->fmt_in.video.i_width : 16;
1250 id->p_encoder->fmt_in.video.i_height =
1251 id->p_encoder->fmt_out.video.i_height ?
1252 id->p_encoder->fmt_out.video.i_height :
1253 id->p_decoder->fmt_in.video.i_height ?
1254 id->p_decoder->fmt_in.video.i_height : 16;
1255 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1256 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1258 id->p_encoder->i_threads = p_sys->i_threads;
1259 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1261 id->p_encoder->p_module =
1262 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1263 if( !id->p_encoder->p_module )
1265 msg_Err( p_stream, "cannot find encoder" );
1266 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1267 id->p_decoder->p_module = 0;
1268 return VLC_EGENERIC;
1271 /* Close the encoder.
1272 * We'll open it only when we have the first frame. */
1273 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1274 id->p_encoder->p_module = NULL;
1276 if( p_sys->i_threads >= 1 )
1278 p_sys->id_video = id;
1279 vlc_mutex_init( p_stream, &p_sys->lock_out );
1280 vlc_cond_init( p_stream, &p_sys->cond );
1281 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1282 p_sys->i_first_pic = 0;
1283 p_sys->i_last_pic = 0;
1284 p_sys->p_buffers = NULL;
1285 p_sys->b_die = p_sys->b_error = 0;
1286 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1287 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1289 msg_Err( p_stream, "cannot spawn encoder thread" );
1290 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1291 id->p_decoder->p_module = 0;
1292 return VLC_EGENERIC;
1296 date_Set( &id->interpolated_pts, 0 );
1301 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1302 sout_stream_id_t *id )
1304 sout_stream_sys_t *p_sys = p_stream->p_sys;
1306 /* Hack because of the copy packetizer which can fail to detect the
1307 * proper size (which forces us to wait until the 1st frame
1309 int i_width = id->p_decoder->fmt_out.video.i_width -
1310 p_sys->i_crop_left - p_sys->i_crop_right;
1311 int i_height = id->p_decoder->fmt_out.video.i_height -
1312 p_sys->i_crop_top - p_sys->i_crop_bottom;
1314 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1315 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1317 /* Apply the scaling */
1318 id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1319 id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1321 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1322 id->p_encoder->fmt_out.video.i_height <= 0 )
1324 id->p_encoder->fmt_out.video.i_height =
1325 id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1327 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1328 id->p_encoder->fmt_out.video.i_height > 0 )
1330 id->p_encoder->fmt_out.video.i_width =
1331 id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1334 /* Make sure the size is at least a multiple of 2 */
1335 id->p_encoder->fmt_out.video.i_width =
1336 (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
1337 id->p_encoder->fmt_out.video.i_height =
1338 (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
1340 id->p_encoder->fmt_in.video.i_width =
1341 id->p_encoder->fmt_out.video.i_width;
1342 id->p_encoder->fmt_in.video.i_height =
1343 id->p_encoder->fmt_out.video.i_height;
1345 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1346 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1348 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1349 id->p_decoder->fmt_out.video.i_frame_rate_base )
1351 id->p_encoder->fmt_out.video.i_frame_rate =
1352 id->p_decoder->fmt_out.video.i_frame_rate;
1353 id->p_encoder->fmt_out.video.i_frame_rate_base =
1354 id->p_decoder->fmt_out.video.i_frame_rate_base;
1358 /* Pick a sensible default value */
1359 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1360 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1364 id->p_encoder->fmt_in.video.i_frame_rate =
1365 id->p_encoder->fmt_out.video.i_frame_rate;
1366 id->p_encoder->fmt_in.video.i_frame_rate_base =
1367 id->p_encoder->fmt_out.video.i_frame_rate_base;
1369 date_Init( &id->interpolated_pts,
1370 id->p_encoder->fmt_out.video.i_frame_rate,
1371 id->p_encoder->fmt_out.video.i_frame_rate_base );
1373 /* Check whether a particular aspect ratio was requested */
1374 if( !id->p_encoder->fmt_out.video.i_aspect )
1376 id->p_encoder->fmt_out.video.i_aspect =
1377 id->p_decoder->fmt_out.video.i_aspect;
1379 id->p_encoder->fmt_in.video.i_aspect =
1380 id->p_encoder->fmt_out.video.i_aspect;
1382 id->p_encoder->p_module =
1383 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1384 if( !id->p_encoder->p_module )
1386 msg_Err( p_stream, "cannot find encoder" );
1387 return VLC_EGENERIC;
1390 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1392 /* Hack for mp2v/mp1v transcoding support */
1393 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1394 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1396 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1399 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1400 &id->p_encoder->fmt_out );
1403 msg_Err( p_stream, "cannot add this stream" );
1404 return VLC_EGENERIC;
1410 static void transcode_video_close( sout_stream_t *p_stream,
1411 sout_stream_id_t *id )
1415 if( p_stream->p_sys->i_threads >= 1 )
1417 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1418 p_stream->p_sys->b_die = 1;
1419 vlc_cond_signal( &p_stream->p_sys->cond );
1420 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1421 vlc_thread_join( p_stream->p_sys );
1422 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1423 vlc_cond_destroy( &p_stream->p_sys->cond );
1427 if( id->p_decoder->p_module )
1428 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1430 if( id->p_decoder->p_owner )
1432 /* Clean-up pictures ring buffer */
1433 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1435 if( id->p_decoder->p_owner->pp_pics[i] )
1436 video_del_buffer( VLC_OBJECT(id->p_decoder),
1437 id->p_decoder->p_owner->pp_pics[i] );
1439 free( id->p_decoder->p_owner );
1443 if( id->p_encoder->p_module )
1444 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1447 for( i = 0; i < id->i_filter; i++ )
1449 vlc_object_detach( id->pp_filter[i] );
1450 if( id->pp_filter[i]->p_module )
1451 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1453 /* Clean-up pictures ring buffer */
1454 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1456 if( id->pp_filter[i]->p_owner->pp_pics[j] )
1457 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1458 id->pp_filter[i]->p_owner->pp_pics[j] );
1460 free( id->pp_filter[i]->p_owner );
1462 vlc_object_destroy( id->pp_filter[i] );
1466 static int transcode_video_process( sout_stream_t *p_stream,
1467 sout_stream_id_t *id,
1468 block_t *in, block_t **out )
1470 sout_stream_sys_t *p_sys = p_stream->p_sys;
1471 int i_duplicate = 1, i;
1475 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1477 subpicture_t *p_subpic = 0;
1479 if( p_sys->b_master_sync )
1481 mtime_t i_video_drift;
1482 mtime_t i_master_drift = p_sys->i_master_drift;
1485 if( !i_master_drift )
1487 /* No audio track ? */
1488 p_sys->i_master_drift = i_master_drift = p_pic->date;
1491 i_pts = date_Get( &id->interpolated_pts ) + 1;
1492 i_video_drift = p_pic->date - i_pts;
1495 /* Set the pts of the frame being encoded */
1496 p_pic->date = i_pts;
1498 if( i_video_drift < i_master_drift - 50000 )
1501 msg_Dbg( p_stream, "dropping frame (%i)",
1502 (int)(i_video_drift - i_master_drift) );
1504 p_pic->pf_release( p_pic );
1505 return VLC_EGENERIC;
1507 else if( i_video_drift > i_master_drift + 50000 )
1510 msg_Dbg( p_stream, "adding frame (%i)",
1511 (int)(i_video_drift - i_master_drift) );
1517 if( !id->p_encoder->p_module )
1519 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1521 p_pic->pf_release( p_pic );
1522 transcode_video_close( p_stream, id );
1523 id->b_transcode = VLC_FALSE;
1524 return VLC_EGENERIC;
1528 if( p_stream->p_sys->b_deinterlace )
1530 id->pp_filter[id->i_filter] =
1531 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1532 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1534 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1535 video_new_buffer_filter;
1536 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1537 video_del_buffer_filter;
1539 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1540 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
1541 id->pp_filter[id->i_filter]->p_module =
1542 module_Need( id->pp_filter[id->i_filter],
1543 "video filter2", "deinterlace", 0 );
1544 if( id->pp_filter[id->i_filter]->p_module )
1546 id->pp_filter[id->i_filter]->p_owner =
1547 malloc( sizeof(filter_owner_sys_t) );
1548 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1549 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1555 msg_Dbg( p_stream, "no video filter found" );
1556 vlc_object_detach( id->pp_filter[id->i_filter] );
1557 vlc_object_destroy( id->pp_filter[id->i_filter] );
1561 /* Check if we need a filter for chroma conversion or resizing */
1562 if( id->p_decoder->fmt_out.video.i_chroma !=
1563 id->p_encoder->fmt_in.video.i_chroma ||
1564 id->p_decoder->fmt_out.video.i_width !=
1565 id->p_encoder->fmt_out.video.i_width ||
1566 id->p_decoder->fmt_out.video.i_height !=
1567 id->p_encoder->fmt_out.video.i_height ||
1568 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1569 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1571 id->pp_filter[id->i_filter] =
1572 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1573 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1575 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1576 video_new_buffer_filter;
1577 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1578 video_del_buffer_filter;
1580 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1581 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1582 id->pp_filter[id->i_filter]->p_module =
1583 module_Need( id->pp_filter[id->i_filter],
1584 "video filter2", 0, 0 );
1585 if( id->pp_filter[id->i_filter]->p_module )
1587 id->pp_filter[id->i_filter]->p_owner =
1588 malloc( sizeof(filter_owner_sys_t) );
1589 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1590 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1596 msg_Dbg( p_stream, "no video filter found" );
1597 vlc_object_detach( id->pp_filter[id->i_filter] );
1598 vlc_object_destroy( id->pp_filter[id->i_filter] );
1600 p_pic->pf_release( p_pic );
1601 transcode_video_close( p_stream, id );
1602 id->b_transcode = VLC_FALSE;
1603 return VLC_EGENERIC;
1608 /* Run filter chain */
1609 for( i = 0; i < id->i_filter; i++ )
1611 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1618 /* Check if we have a subpicture to overlay */
1621 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
1622 /* TODO: get another pic */
1625 /* Overlay subpicture */
1628 int i_scale_width, i_scale_height;
1629 video_format_t *p_fmt;
1631 i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
1632 id->p_decoder->fmt_out.video.i_width;
1633 i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
1634 id->p_decoder->fmt_out.video.i_height;
1636 if( p_pic->i_refcount && !id->i_filter )
1638 /* We can't modify the picture, we need to duplicate it */
1639 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1642 vout_CopyPicture( p_stream, p_tmp, p_pic );
1643 p_pic->pf_release( p_pic );
1649 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
1651 p_fmt = &id->p_decoder->fmt_out.video;
1653 /* FIXME (shouldn't have to be done here) */
1654 p_fmt->i_sar_num = p_fmt->i_aspect *
1655 p_fmt->i_height / p_fmt->i_width;
1656 p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
1658 spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
1659 i_scale_width, i_scale_height );
1662 if( p_sys->i_threads >= 1 )
1664 vlc_mutex_lock( &p_sys->lock_out );
1665 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1666 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1667 *out = p_sys->p_buffers;
1668 p_sys->p_buffers = NULL;
1669 vlc_cond_signal( &p_sys->cond );
1670 vlc_mutex_unlock( &p_sys->lock_out );
1675 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1676 block_ChainAppend( out, p_block );
1678 if( p_sys->b_master_sync )
1679 date_Increment( &id->interpolated_pts, 1 );
1681 if( p_sys->b_master_sync && i_duplicate > 1 )
1683 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1684 date_Increment( &id->interpolated_pts, 1 );
1685 p_pic->date = i_pts;
1686 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1687 block_ChainAppend( out, p_block );
1690 p_pic->pf_release( p_pic );
1697 static int EncoderThread( sout_stream_sys_t *p_sys )
1699 sout_stream_id_t *id = p_sys->id_video;
1702 while( !p_sys->b_die && !p_sys->b_error )
1706 vlc_mutex_lock( &p_sys->lock_out );
1707 while( p_sys->i_last_pic == p_sys->i_first_pic )
1709 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1710 if( p_sys->b_die || p_sys->b_error ) break;
1712 if( p_sys->b_die || p_sys->b_error )
1714 vlc_mutex_unlock( &p_sys->lock_out );
1718 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1719 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1720 vlc_mutex_unlock( &p_sys->lock_out );
1722 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1723 vlc_mutex_lock( &p_sys->lock_out );
1724 block_ChainAppend( &p_sys->p_buffers, p_block );
1726 if( p_sys->b_master_sync )
1727 date_Increment( &id->interpolated_pts, 1 );
1730 if( p_sys->b_master_sync && i_duplicate > 1 )
1732 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1733 date_Increment( &id->interpolated_pts, 1 );
1734 p_pic->date = i_pts;
1735 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1736 block_ChainAppend( &p_sys->p_buffers, p_block );
1739 vlc_mutex_unlock( &p_sys->lock_out );
1741 p_pic->pf_release( p_pic );
1744 while( p_sys->i_last_pic != p_sys->i_first_pic )
1746 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1747 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1749 p_pic->pf_release( p_pic );
1752 block_ChainRelease( p_sys->p_buffers );
1757 struct picture_sys_t
1759 vlc_object_t *p_owner;
1762 static void video_release_buffer( picture_t *p_pic )
1764 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
1766 video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
1768 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
1771 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring )
1773 decoder_t *p_dec = (decoder_t *)p_this;
1777 /* Find an empty space in the picture ring buffer */
1778 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1780 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
1782 pp_ring[i]->i_status = RESERVED_PICTURE;
1786 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1788 if( pp_ring[i] == 0 ) break;
1791 if( i == PICTURE_RING_SIZE )
1793 msg_Err( p_this, "decoder/filter is leaking pictures, "
1794 "resetting its ring buffer" );
1796 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1798 pp_ring[i]->pf_release( pp_ring[i] );
1804 p_pic = malloc( sizeof(picture_t) );
1805 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1806 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
1807 p_dec->fmt_out.video.i_chroma,
1808 p_dec->fmt_out.video.i_width,
1809 p_dec->fmt_out.video.i_height,
1810 p_dec->fmt_out.video.i_aspect );
1812 if( !p_pic->i_planes )
1818 p_pic->pf_release = video_release_buffer;
1819 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
1820 p_pic->p_sys->p_owner = p_this;
1821 p_pic->i_status = RESERVED_PICTURE;
1828 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
1830 return video_new_buffer( VLC_OBJECT(p_dec),
1831 p_dec->p_owner->pp_pics );
1834 static picture_t *video_new_buffer_filter( filter_t *p_filter )
1836 return video_new_buffer( VLC_OBJECT(p_filter),
1837 p_filter->p_owner->pp_pics );
1840 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
1842 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1843 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
1844 if( p_pic ) free( p_pic );
1847 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
1849 p_pic->i_refcount = 0;
1850 p_pic->i_status = DESTROYED_PICTURE;
1853 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
1855 p_pic->i_refcount = 0;
1856 p_pic->i_status = DESTROYED_PICTURE;
1859 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
1861 p_pic->i_refcount++;
1864 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
1866 video_release_buffer( p_pic );
1872 static subpicture_t *spu_new_buffer( decoder_t * );
1873 static void spu_del_buffer( decoder_t *, subpicture_t * );
1875 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1877 sout_stream_sys_t *p_sys = p_stream->p_sys;
1883 /* Initialization of decoder structures */
1884 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
1885 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
1886 id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
1887 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
1889 id->p_decoder->p_module =
1890 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1892 if( !id->p_decoder->p_module )
1894 msg_Err( p_stream, "cannot find decoder" );
1895 return VLC_EGENERIC;
1898 if( !p_sys->b_soverlay )
1904 /* Initialization of encoder format structures */
1905 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1906 id->p_decoder->fmt_in.i_codec );
1908 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
1910 id->p_encoder->p_module =
1911 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
1913 if( !id->p_encoder->p_module )
1915 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1916 msg_Err( p_stream, "cannot find encoder" );
1917 return VLC_EGENERIC;
1923 p_sys->p_spu = spu_Create( p_stream );
1924 spu_Init( p_sys->p_spu );
1930 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
1933 if( id->p_decoder->p_module )
1934 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1937 if( id->p_encoder->p_module )
1938 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1941 static int transcode_spu_process( sout_stream_t *p_stream,
1942 sout_stream_id_t *id,
1943 block_t *in, block_t **out )
1945 sout_stream_sys_t *p_sys = p_stream->p_sys;
1946 subpicture_t *p_subpic;
1949 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
1950 if( !p_subpic ) return VLC_EGENERIC;
1952 if( p_sys->b_master_sync && p_sys->i_master_drift )
1954 p_subpic->i_start -= p_sys->i_master_drift;
1955 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
1958 if( p_sys->b_soverlay )
1960 spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
1966 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
1967 spu_del_buffer( id->p_decoder, p_subpic );
1971 block_ChainAppend( out, p_block );
1976 return VLC_EGENERIC;
1979 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1981 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
1982 return spu_CreateSubpicture( p_stream->p_sys->p_spu );
1985 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1987 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
1988 spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );