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 *****************************************************************************/
32 #include <vlc/input.h>
35 #include <vlc/decoder.h>
36 #include "vlc_filter.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 #define VENC_TEXT N_("Video encoder")
43 #define VENC_LONGTEXT N_( \
44 "Allows you to specify the video encoder to use and its associated " \
46 #define VCODEC_TEXT N_("Destination video codec")
47 #define VCODEC_LONGTEXT N_( \
48 "Allows you to specify the destination video codec used for the " \
50 #define VB_TEXT N_("Video bitrate")
51 #define VB_LONGTEXT N_( \
52 "Allows you to specify the video bitrate used for the streaming " \
54 #define SCALE_TEXT N_("Video scaling")
55 #define SCALE_LONGTEXT N_( \
56 "Allows you to scale the video before encoding." )
57 #define FPS_TEXT N_("Video frame-rate")
58 #define FPS_LONGTEXT N_( \
59 "Allows you to specify an output frame rate for the video." )
60 #define DEINTERLACE_TEXT N_("Deinterlace video")
61 #define DEINTERLACE_LONGTEXT N_( \
62 "Allows you to deinterlace the video before encoding." )
63 #define WIDTH_TEXT N_("Video width")
64 #define WIDTH_LONGTEXT N_( \
65 "Allows you to specify the output video width." )
66 #define HEIGHT_TEXT N_("Video height")
67 #define HEIGHT_LONGTEXT N_( \
68 "Allows you to specify the output video height." )
70 #define CROPTOP_TEXT N_("Video crop top")
71 #define CROPTOP_LONGTEXT N_( \
72 "Allows you to specify the top coordinate for the video cropping." )
73 #define CROPLEFT_TEXT N_("Video crop left")
74 #define CROPLEFT_LONGTEXT N_( \
75 "Allows you to specify the left coordinate for the video cropping." )
76 #define CROPBOTTOM_TEXT N_("Video crop bottom")
77 #define CROPBOTTOM_LONGTEXT N_( \
78 "Allows you to specify the bottom coordinate for the video cropping." )
79 #define CROPRIGHT_TEXT N_("Video crop right")
80 #define CROPRIGHT_LONGTEXT N_( \
81 "Allows you to specify the right coordinate for the video cropping." )
83 #define AENC_TEXT N_("Audio encoder")
84 #define AENC_LONGTEXT N_( \
85 "Allows you to specify the audio encoder to use and its associated " \
87 #define ACODEC_TEXT N_("Destination audio codec")
88 #define ACODEC_LONGTEXT N_( \
89 "Allows you to specify the destination audio codec used for the " \
91 #define AB_TEXT N_("Audio bitrate")
92 #define AB_LONGTEXT N_( \
93 "Allows you to specify the audio bitrate used for the streaming " \
95 #define ARATE_TEXT N_("Audio sample rate")
96 #define ARATE_LONGTEXT N_( \
97 "Allows you to specify the audio sample rate used for the streaming " \
99 #define ACHANS_TEXT N_("Audio channels")
100 #define ACHANS_LONGTEXT N_( \
101 "Allows you to specify the number of audio channels used for the " \
102 "streaming output." )
104 #define SENC_TEXT N_("Subtitles encoder")
105 #define SENC_LONGTEXT N_( \
106 "Allows you to specify the subtitles encoder to use and its associated " \
108 #define SCODEC_TEXT N_("Destination subtitles codec")
109 #define SCODEC_LONGTEXT N_( \
110 "Allows you to specify the destination subtitles codec used for the " \
111 "streaming output." )
113 #define THREADS_TEXT N_("Number of threads")
114 #define THREADS_LONGTEXT N_( \
115 "Allows you to specify the number of threads used for the transcoding." )
117 #define ASYNC_TEXT N_("Synchronise on audio track")
118 #define ASYNC_LONGTEXT N_( \
119 "This option will drop/duplicate video frames to synchronise the video " \
120 "track on the audio track." )
122 static int Open ( vlc_object_t * );
123 static void Close( vlc_object_t * );
125 #define SOUT_CFG_PREFIX "sout-transcode-"
128 set_description( _("Transcode stream output") );
129 set_capability( "sout stream", 50 );
130 add_shortcut( "transcode" );
131 set_callbacks( Open, Close );
133 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
134 VENC_LONGTEXT, VLC_FALSE );
135 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
136 VCODEC_LONGTEXT, VLC_FALSE );
137 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
138 VB_LONGTEXT, VLC_FALSE );
139 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
140 SCALE_LONGTEXT, VLC_FALSE );
141 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
142 FPS_LONGTEXT, VLC_FALSE );
143 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
144 DEINTERLACE_LONGTEXT, VLC_FALSE );
145 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
146 WIDTH_LONGTEXT, VLC_TRUE );
147 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
148 HEIGHT_LONGTEXT, VLC_TRUE );
150 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
151 CROPTOP_LONGTEXT, VLC_TRUE );
152 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
153 CROPLEFT_LONGTEXT, VLC_TRUE );
154 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
155 CROPBOTTOM_LONGTEXT, VLC_TRUE );
156 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
157 CROPRIGHT_LONGTEXT, VLC_TRUE );
159 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
160 AENC_LONGTEXT, VLC_FALSE );
161 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
162 ACODEC_LONGTEXT, VLC_FALSE );
163 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
164 AB_LONGTEXT, VLC_FALSE );
165 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
166 ACHANS_LONGTEXT, VLC_FALSE );
167 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
168 ARATE_LONGTEXT, VLC_TRUE );
170 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
171 SENC_LONGTEXT, VLC_FALSE );
172 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
173 SCODEC_LONGTEXT, VLC_FALSE );
174 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
175 SCODEC_LONGTEXT, VLC_FALSE );
177 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
178 THREADS_LONGTEXT, VLC_TRUE );
180 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
181 ASYNC_LONGTEXT, VLC_FALSE );
184 static const char *ppsz_sout_options[] = {
185 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
186 "scale", "fps", "width", "height", "deinterlace", "threads",
187 "aenc", "acodec", "ab", "samplerate", "channels",
188 "senc", "scodec", "soverlay",
192 /*****************************************************************************
193 * Exported prototypes
194 *****************************************************************************/
195 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
196 static int Del ( sout_stream_t *, sout_stream_id_t * );
197 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
199 static int transcode_audio_new ( sout_stream_t *, sout_stream_id_t * );
200 static void transcode_audio_close ( sout_stream_t *, sout_stream_id_t * );
201 static int transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
202 block_t *, block_t ** );
204 static aout_buffer_t *audio_new_buffer( decoder_t *, int );
205 static void audio_del_buffer( decoder_t *, aout_buffer_t * );
207 static int transcode_video_new ( sout_stream_t *, sout_stream_id_t * );
208 static void transcode_video_close ( sout_stream_t *, sout_stream_id_t * );
209 static int transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
210 static int transcode_video_process( sout_stream_t *, sout_stream_id_t *,
211 block_t *, block_t ** );
213 static picture_t *video_new_buffer( decoder_t * );
214 static void video_del_buffer( decoder_t *, picture_t * );
215 static void video_link_picture( decoder_t *, picture_t * );
216 static void video_unlink_picture( decoder_t *, picture_t * );
218 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
219 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
220 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
221 block_t *, block_t ** );
222 static subpicture_t *transcode_spu_get( sout_stream_t *, sout_stream_id_t *,
225 static int EncoderThread( struct sout_stream_sys_t * p_sys );
227 static int pi_channels_maps[6] =
230 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
231 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
232 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
233 | AOUT_CHAN_REARRIGHT,
234 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
235 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
238 #define PICTURE_RING_SIZE 64
239 #define SUBPICTURE_RING_SIZE 20
241 struct sout_stream_sys_t
245 sout_stream_t *p_out;
246 sout_stream_id_t *id_video;
248 vlc_mutex_t lock_out;
250 picture_t * pp_pics[PICTURE_RING_SIZE];
251 int i_first_pic, i_last_pic;
254 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
256 sout_cfg_t *p_audio_cfg;
262 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
264 sout_cfg_t *p_video_cfg;
270 vlc_bool_t b_deinterlace;
279 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
281 vlc_bool_t b_soverlay;
282 sout_cfg_t *p_spu_cfg;
283 subpicture_t *pp_subpics[SUBPICTURE_RING_SIZE];
286 filter_t *p_filter_blend;
289 vlc_bool_t b_audio_sync;
290 mtime_t i_master_drift;
293 /*****************************************************************************
295 *****************************************************************************/
296 static int Open( vlc_object_t *p_this )
298 sout_stream_t *p_stream = (sout_stream_t*)p_this;
299 sout_stream_sys_t *p_sys;
303 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
305 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
308 msg_Err( p_stream, "cannot create chain" );
313 p_sys->i_master_drift = 0;
315 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
318 /* Audio transcoding parameters */
319 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
320 p_sys->psz_aenc = NULL;
321 p_sys->p_audio_cfg = NULL;
322 if( val.psz_string && *val.psz_string )
325 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
327 if( psz_next ) free( psz_next );
329 if( val.psz_string ) free( val.psz_string );
331 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
333 if( val.psz_string && *val.psz_string )
336 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
337 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
339 if( val.psz_string ) free( val.psz_string );
341 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
342 p_sys->i_abitrate = val.i_int;
343 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
345 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
346 p_sys->i_sample_rate = val.i_int;
348 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
349 p_sys->i_channels = val.i_int;
351 if( p_sys->i_acodec )
353 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
354 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
355 p_sys->i_channels, p_sys->i_abitrate / 1000 );
358 /* Video transcoding parameters */
359 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
360 p_sys->psz_venc = NULL;
361 p_sys->p_video_cfg = NULL;
362 if( val.psz_string && *val.psz_string )
365 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
367 if( psz_next ) free( psz_next );
369 if( val.psz_string ) free( val.psz_string );
371 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
373 if( val.psz_string && *val.psz_string )
376 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
377 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
379 if( val.psz_string ) free( val.psz_string );
381 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
382 p_sys->i_vbitrate = val.i_int;
383 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
385 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
386 p_sys->f_scale = val.f_float;
388 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
389 p_sys->f_fps = val.f_float;
391 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
392 p_sys->i_width = val.i_int;
394 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
395 p_sys->i_height = val.i_int;
397 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
398 p_sys->b_deinterlace = val.b_bool;
400 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
401 p_sys->i_crop_top = val.i_int;
402 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
403 p_sys->i_crop_bottom = val.i_int;
404 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
405 p_sys->i_crop_left = val.i_int;
406 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
407 p_sys->i_crop_right = val.i_int;
409 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
410 p_sys->i_threads = val.i_int;
412 if( p_sys->i_vcodec )
414 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
415 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
416 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
419 /* Subpictures transcoding parameters */
420 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
421 p_sys->psz_senc = NULL;
422 p_sys->p_spu_cfg = NULL;
423 if( val.psz_string && *val.psz_string )
426 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
428 if( psz_next ) free( psz_next );
430 if( val.psz_string ) free( val.psz_string );
432 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
434 if( val.psz_string && *val.psz_string )
437 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
438 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
440 if( val.psz_string ) free( val.psz_string );
442 if( p_sys->i_scodec )
444 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_acodec );
447 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
448 p_sys->b_soverlay = val.b_bool;
449 p_sys->p_filter_blend = 0;
451 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
453 p_sys->pp_subpics[i] = 0;
456 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
457 p_sys->b_audio_sync = val.b_bool;
458 if( p_sys->f_fps > 0 ) p_sys->b_audio_sync = VLC_TRUE;
460 p_stream->pf_add = Add;
461 p_stream->pf_del = Del;
462 p_stream->pf_send = Send;
463 p_stream->p_sys = p_sys;
468 /*****************************************************************************
470 *****************************************************************************/
471 static void Close( vlc_object_t * p_this )
473 sout_stream_t *p_stream = (sout_stream_t*)p_this;
474 sout_stream_sys_t *p_sys = p_stream->p_sys;
476 sout_StreamDelete( p_sys->p_out );
478 while( p_sys->p_audio_cfg != NULL )
480 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
482 if( p_sys->p_audio_cfg->psz_name )
483 free( p_sys->p_audio_cfg->psz_name );
484 if( p_sys->p_audio_cfg->psz_value )
485 free( p_sys->p_audio_cfg->psz_value );
486 free( p_sys->p_audio_cfg );
488 p_sys->p_audio_cfg = p_next;
490 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
492 while( p_sys->p_video_cfg != NULL )
494 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
496 if( p_sys->p_video_cfg->psz_name )
497 free( p_sys->p_video_cfg->psz_name );
498 if( p_sys->p_video_cfg->psz_value )
499 free( p_sys->p_video_cfg->psz_value );
500 free( p_sys->p_video_cfg );
502 p_sys->p_video_cfg = p_next;
504 if( p_sys->psz_venc ) free( p_sys->psz_venc );
506 while( p_sys->p_spu_cfg != NULL )
508 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
510 if( p_sys->p_spu_cfg->psz_name )
511 free( p_sys->p_spu_cfg->psz_name );
512 if( p_sys->p_spu_cfg->psz_value )
513 free( p_sys->p_spu_cfg->psz_value );
514 free( p_sys->p_spu_cfg );
516 p_sys->p_spu_cfg = p_next;
518 if( p_sys->psz_senc ) free( p_sys->psz_senc );
520 if( p_sys->p_filter_blend )
522 if( p_sys->p_filter_blend->p_module )
523 module_Unneed( p_sys->p_filter_blend,
524 p_sys->p_filter_blend->p_module );
526 vlc_object_detach( p_sys->p_filter_blend );
527 vlc_object_destroy( p_sys->p_filter_blend );
530 vlc_object_destroy( p_sys );
533 struct sout_stream_id_t
535 vlc_fourcc_t b_transcode;
537 /* id of the out stream */
541 decoder_t *p_decoder;
544 filter_t *pp_filter[10];
548 encoder_t *p_encoder;
551 date_t interpolated_pts;
552 mtime_t i_initial_pts;
556 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
558 sout_stream_sys_t *p_sys = p_stream->p_sys;
559 sout_stream_id_t *id;
561 id = malloc( sizeof( sout_stream_id_t ) );
562 memset( id, 0, sizeof(sout_stream_id_t) );
565 id->p_decoder = NULL;
566 id->p_encoder = NULL;
568 /* Create decoder object */
569 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
572 msg_Err( p_stream, "out of memory" );
575 vlc_object_attach( id->p_decoder, p_stream );
576 id->p_decoder->p_module = NULL;
577 id->p_decoder->fmt_in = *p_fmt;
578 id->p_decoder->fmt_out = *p_fmt;
579 id->p_decoder->fmt_out.i_extra = 0;
580 id->p_decoder->fmt_out.p_extra = 0;
581 id->p_decoder->b_pace_control = VLC_TRUE;
583 /* Create encoder object */
584 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
587 msg_Err( p_stream, "out of memory" );
590 vlc_object_attach( id->p_encoder, p_stream );
591 id->p_encoder->p_module = NULL;
593 /* Create destination format */
594 es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
595 id->p_encoder->fmt_out.i_id = p_fmt->i_id;
596 id->p_encoder->fmt_out.i_group = p_fmt->i_group;
597 if( p_fmt->psz_language )
598 id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
600 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
603 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
604 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
606 /* Complete destination format */
607 id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
608 id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
609 p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
610 id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
611 p_sys->i_channels : p_fmt->audio.i_channels;
612 id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
613 id->p_encoder->fmt_out.audio.i_bitspersample =
614 p_fmt->audio.i_bitspersample;
616 /* Build decoder -> filter -> encoder chain */
617 if( transcode_audio_new( p_stream, id ) )
619 msg_Err( p_stream, "cannot create audio chain" );
623 /* Open output stream */
624 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
625 id->b_transcode = VLC_TRUE;
627 if( !id->id ) goto error;
629 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
631 else if( p_fmt->i_cat == VIDEO_ES &&
632 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
635 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
636 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
638 /* Complete destination format */
639 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
640 id->p_encoder->fmt_out.video.i_width = p_sys->i_width;
641 id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
642 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
644 /* Build decoder -> filter -> encoder chain */
645 if( transcode_video_new( p_stream, id ) )
647 msg_Err( p_stream, "cannot create video chain" );
651 /* Stream will be added later on because we don't know
652 * all the characteristics of the decoded stream yet */
653 id->b_transcode = VLC_TRUE;
655 if( p_sys->f_fps > 0 )
657 id->p_encoder->fmt_out.video.i_frame_rate = p_sys->f_fps * 1000;
658 id->p_encoder->fmt_out.video.i_frame_rate_base = 1000;
661 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
663 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
664 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
665 (char*)&p_sys->i_scodec );
667 /* Complete destination format */
668 id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
670 /* build decoder -> filter -> encoder */
671 if( transcode_spu_new( p_stream, id ) )
673 msg_Err( p_stream, "cannot create subtitles chain" );
677 /* open output stream */
678 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
679 id->b_transcode = VLC_TRUE;
681 if( !id->id ) goto error;
683 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
685 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
686 (char*)&p_fmt->i_codec );
688 id->b_transcode = VLC_TRUE;
690 /* Build decoder -> filter -> overlaying chain */
691 if( transcode_spu_new( p_stream, id ) )
693 msg_Err( p_stream, "cannot create subtitles chain" );
699 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
700 (char*)&p_fmt->i_codec );
701 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
702 id->b_transcode = VLC_FALSE;
704 if( !id->id ) goto error;
712 vlc_object_detach( id->p_decoder );
713 vlc_object_destroy( id->p_decoder );
718 vlc_object_detach( id->p_encoder );
719 vlc_object_destroy( id->p_encoder );
726 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
728 sout_stream_sys_t *p_sys = p_stream->p_sys;
730 if( id->b_transcode )
732 switch( id->p_decoder->fmt_in.i_cat )
735 transcode_audio_close( p_stream, id );
738 transcode_video_close( p_stream, id );
741 transcode_spu_close( p_stream, id );
746 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
750 vlc_object_detach( id->p_decoder );
751 vlc_object_destroy( id->p_decoder );
756 vlc_object_detach( id->p_encoder );
757 vlc_object_destroy( id->p_encoder );
765 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
768 sout_stream_sys_t *p_sys = p_stream->p_sys;
771 if( !id->b_transcode && id->id )
773 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
775 else if( !id->b_transcode )
777 block_Release( p_buffer );
781 switch( id->p_decoder->fmt_in.i_cat )
784 transcode_audio_process( p_stream, id, p_buffer, &p_out );
788 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
796 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
804 block_Release( p_buffer );
808 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
812 /****************************************************************************
813 * decoder reencoder part
814 ****************************************************************************/
815 int audio_BitsPerSample( vlc_fourcc_t i_format )
819 case VLC_FOURCC('u','8',' ',' '):
820 case VLC_FOURCC('s','8',' ',' '):
823 case VLC_FOURCC('u','1','6','l'):
824 case VLC_FOURCC('s','1','6','l'):
825 case VLC_FOURCC('u','1','6','b'):
826 case VLC_FOURCC('s','1','6','b'):
829 case VLC_FOURCC('f','l','3','2'):
830 case VLC_FOURCC('f','i','3','2'):
837 static int transcode_audio_new( sout_stream_t *p_stream,
838 sout_stream_id_t *id )
840 sout_stream_sys_t *p_sys = p_stream->p_sys;
846 /* Initialization of decoder structures */
847 id->p_decoder->pf_decode_audio = 0;
848 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
849 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
850 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
852 id->p_decoder->p_module =
853 module_Need( id->p_decoder, "decoder", "$codec", 0 );
855 if( !id->p_decoder->p_module )
857 msg_Err( p_stream, "cannot find decoder" );
860 id->p_decoder->fmt_out.audio.i_bitspersample =
861 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
867 /* Initialization of encoder format structures */
868 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
869 id->p_decoder->fmt_out.i_codec );
870 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
872 /* Sanity check for audio channels */
873 id->p_encoder->fmt_out.audio.i_channels =
874 __MIN( id->p_encoder->fmt_out.audio.i_channels,
875 id->p_decoder->fmt_in.audio.i_channels );
876 id->p_encoder->fmt_out.audio.i_physical_channels =
877 id->p_encoder->fmt_out.audio.i_original_channels =
878 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
880 /* Initialization of encoder format structures */
881 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
882 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
883 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
884 id->p_encoder->fmt_in.audio.i_physical_channels =
885 id->p_encoder->fmt_in.audio.i_original_channels =
886 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
887 id->p_encoder->fmt_in.audio.i_channels =
888 id->p_encoder->fmt_out.audio.i_channels;
889 id->p_encoder->fmt_in.audio.i_bitspersample = 16;
891 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
893 id->p_encoder->p_module =
894 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
895 if( !id->p_encoder->p_module )
897 msg_Err( p_stream, "cannot find encoder" );
898 module_Unneed( id->p_decoder, id->p_decoder->p_module );
899 id->p_decoder->p_module = 0;
902 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
904 /* Check if we need a filter for chroma conversion or resizing */
905 if( id->p_decoder->fmt_out.i_codec !=
906 id->p_encoder->fmt_in.i_codec )
909 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
910 vlc_object_attach( id->pp_filter[0], p_stream );
912 id->pp_filter[0]->pf_audio_buffer_new = __block_New;
914 id->pp_filter[0]->fmt_in = id->p_decoder->fmt_out;
915 id->pp_filter[0]->fmt_out = id->p_encoder->fmt_in;
916 id->pp_filter[0]->p_module =
917 module_Need( id->pp_filter[0], "audio filter2", 0, 0 );
918 if( id->pp_filter[0]->p_module ) id->i_filter++;
921 msg_Dbg( p_stream, "no audio filter found" );
922 vlc_object_detach( id->pp_filter[0] );
923 vlc_object_destroy( id->pp_filter[0] );
924 module_Unneed( id->p_decoder, id->p_decoder->p_module );
925 id->p_decoder->p_module = 0;
926 module_Unneed( id->p_encoder, id->p_encoder->p_module );
927 id->p_encoder->p_module = 0;
931 /* Try a 2 stage conversion */
932 if( id->pp_filter[0]->fmt_out.i_codec !=
933 id->p_encoder->fmt_in.i_codec )
936 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
937 vlc_object_attach( id->pp_filter[1], p_stream );
939 id->pp_filter[1]->pf_audio_buffer_new = __block_New;
941 id->pp_filter[1]->fmt_in = id->pp_filter[0]->fmt_out;
942 id->pp_filter[1]->fmt_out = id->p_encoder->fmt_in;
943 id->pp_filter[1]->p_module =
944 module_Need( id->pp_filter[1], "audio filter2", 0, 0 );
945 if( !id->pp_filter[1]->p_module ||
946 id->pp_filter[1]->fmt_out.i_codec !=
947 id->p_encoder->fmt_in.i_codec )
949 msg_Dbg( p_stream, "no audio filter found" );
950 module_Unneed( id->pp_filter[0], id->pp_filter[0]->p_module );
951 vlc_object_detach( id->pp_filter[0] );
952 vlc_object_destroy( id->pp_filter[0] );
953 if( id->pp_filter[1]->p_module )
954 module_Unneed( id->pp_filter[0], id->pp_filter[0]->p_module );
955 vlc_object_detach( id->pp_filter[1] );
956 vlc_object_destroy( id->pp_filter[1] );
957 module_Unneed( id->p_decoder, id->p_decoder->p_module );
958 id->p_decoder->p_module = 0;
959 module_Unneed( id->p_encoder, id->p_encoder->p_module );
960 id->p_encoder->p_module = 0;
967 /* FIXME: Hack for mp3 transcoding support */
968 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
969 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
974 static void transcode_audio_close( sout_stream_t *p_stream,
975 sout_stream_id_t *id )
980 if( id->p_decoder->p_module )
981 module_Unneed( id->p_decoder, id->p_decoder->p_module );
984 if( id->p_encoder->p_module )
985 module_Unneed( id->p_encoder, id->p_encoder->p_module );
988 for( i = 0; i < id->i_filter; i++ )
990 vlc_object_detach( id->pp_filter[i] );
991 if( id->pp_filter[i]->p_module )
992 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
993 vlc_object_destroy( id->pp_filter[i] );
997 static int transcode_audio_process( sout_stream_t *p_stream,
998 sout_stream_id_t *id,
999 block_t *in, block_t **out )
1001 sout_stream_sys_t *p_sys = p_stream->p_sys;
1002 aout_buffer_t *p_audio_buf;
1003 block_t *p_block, *p_audio_block;
1007 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1010 if( p_sys->b_audio_sync )
1012 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1013 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1014 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1015 p_audio_buf->start_date -= p_sys->i_master_drift;
1016 p_audio_buf->end_date -= p_sys->i_master_drift;
1019 p_audio_block = p_audio_buf->p_sys;
1020 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1021 p_audio_block->i_dts = p_audio_block->i_pts =
1022 p_audio_buf->start_date;
1023 p_audio_block->i_length = p_audio_buf->end_date -
1024 p_audio_buf->start_date;
1025 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1027 /* Run filter chain */
1028 for( i = 0; i < id->i_filter; i++ )
1031 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1035 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1036 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1037 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1038 p_audio_buf->start_date = p_audio_block->i_dts;
1039 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1041 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1042 block_ChainAppend( out, p_block );
1043 block_Release( p_audio_block );
1044 free( p_audio_buf );
1050 static void audio_release_buffer( aout_buffer_t *p_buffer )
1052 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1053 if( p_buffer ) free( p_buffer );
1056 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1058 aout_buffer_t *p_buffer;
1062 if( p_dec->fmt_out.audio.i_bitspersample )
1064 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample *
1065 p_dec->fmt_out.audio.i_channels;
1067 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1068 p_dec->fmt_out.audio.i_frame_length )
1070 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1071 p_dec->fmt_out.audio.i_frame_length;
1075 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1078 p_buffer = malloc( sizeof(aout_buffer_t) );
1079 p_buffer->pf_release = audio_release_buffer;
1080 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1082 p_buffer->p_buffer = p_block->p_buffer;
1083 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1084 p_buffer->i_nb_samples = i_samples;
1085 p_block->i_samples = i_samples;
1090 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1092 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1093 if( p_buffer ) free( p_buffer );
1099 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1101 sout_stream_sys_t *p_sys = p_stream->p_sys;
1107 /* Initialization of decoder structures */
1108 id->p_decoder->pf_decode_video = 0;
1109 id->p_decoder->pf_vout_buffer_new = video_new_buffer;
1110 id->p_decoder->pf_vout_buffer_del = video_del_buffer;
1111 id->p_decoder->pf_picture_link = video_link_picture;
1112 id->p_decoder->pf_picture_unlink = video_unlink_picture;
1113 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1115 id->p_decoder->p_module =
1116 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1118 if( !id->p_decoder->p_module )
1120 msg_Err( p_stream, "cannot find decoder" );
1121 return VLC_EGENERIC;
1126 * Because some info about the decoded input will only be available
1127 * once the first frame is decoded, we actually only test the availability
1128 * of the encoder here.
1131 /* Initialization of encoder format structures */
1132 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1133 id->p_decoder->fmt_out.i_codec );
1134 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1136 /* The dimensions will be set properly later on.
1137 * Just put sensible values so we can test an encoder is available. */
1138 id->p_encoder->fmt_in.video.i_width =
1139 id->p_encoder->fmt_out.video.i_width ?
1140 id->p_encoder->fmt_out.video.i_width :
1141 id->p_decoder->fmt_in.video.i_width ?
1142 id->p_decoder->fmt_in.video.i_width : 16;
1143 id->p_encoder->fmt_in.video.i_height =
1144 id->p_encoder->fmt_out.video.i_height ?
1145 id->p_encoder->fmt_out.video.i_height :
1146 id->p_decoder->fmt_in.video.i_height ?
1147 id->p_decoder->fmt_in.video.i_height : 16;
1148 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1149 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1151 id->p_encoder->i_threads = p_sys->i_threads;
1152 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1154 id->p_encoder->p_module =
1155 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1156 if( !id->p_encoder->p_module )
1158 msg_Err( p_stream, "cannot find encoder" );
1159 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1160 id->p_decoder->p_module = 0;
1161 return VLC_EGENERIC;
1164 /* Close the encoder.
1165 * We'll open it only when we have the first frame. */
1166 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1167 id->p_encoder->p_module = NULL;
1169 if( p_sys->i_threads >= 1 )
1171 p_sys->id_video = id;
1172 vlc_mutex_init( p_stream, &p_sys->lock_out );
1173 vlc_cond_init( p_stream, &p_sys->cond );
1174 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1175 p_sys->i_first_pic = 0;
1176 p_sys->i_last_pic = 0;
1177 p_sys->p_buffers = NULL;
1178 p_sys->b_die = p_sys->b_error = 0;
1179 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1180 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1182 msg_Err( p_stream, "cannot spawn encoder thread" );
1183 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1184 id->p_decoder->p_module = 0;
1185 return VLC_EGENERIC;
1192 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1193 sout_stream_id_t *id )
1195 sout_stream_sys_t *p_sys = p_stream->p_sys;
1197 /* Hack because of the copy packetizer which can fail to detect the
1198 * proper size (which forces us to wait until the 1st frame
1200 int i_width = id->p_decoder->fmt_out.video.i_width -
1201 p_sys->i_crop_left - p_sys->i_crop_right;
1202 int i_height = id->p_decoder->fmt_out.video.i_height -
1203 p_sys->i_crop_top - p_sys->i_crop_bottom;
1205 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1206 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1208 /* Apply the scaling */
1209 id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1210 id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1212 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1213 id->p_encoder->fmt_out.video.i_height <= 0 )
1215 id->p_encoder->fmt_out.video.i_height =
1216 id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1218 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1219 id->p_encoder->fmt_out.video.i_height > 0 )
1221 id->p_encoder->fmt_out.video.i_width =
1222 id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1225 /* Make sure the size is at least a multiple of 2 */
1226 id->p_encoder->fmt_out.video.i_width =
1227 (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
1228 id->p_encoder->fmt_out.video.i_height =
1229 (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
1231 id->p_encoder->fmt_in.video.i_width =
1232 id->p_encoder->fmt_out.video.i_width;
1233 id->p_encoder->fmt_in.video.i_height =
1234 id->p_encoder->fmt_out.video.i_height;
1236 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1237 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1239 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1240 id->p_decoder->fmt_out.video.i_frame_rate_base )
1242 id->p_encoder->fmt_out.video.i_frame_rate =
1243 id->p_decoder->fmt_out.video.i_frame_rate;
1244 id->p_encoder->fmt_out.video.i_frame_rate_base =
1245 id->p_decoder->fmt_out.video.i_frame_rate_base;
1249 /* Pick a sensible default value */
1250 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1251 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1255 id->p_encoder->fmt_in.video.i_frame_rate =
1256 id->p_encoder->fmt_out.video.i_frame_rate;
1257 id->p_encoder->fmt_in.video.i_frame_rate_base =
1258 id->p_encoder->fmt_out.video.i_frame_rate_base;
1260 date_Init( &id->interpolated_pts,
1261 id->p_encoder->fmt_out.video.i_frame_rate,
1262 id->p_encoder->fmt_out.video.i_frame_rate_base );
1264 /* Check whether a particular aspect ratio was requested */
1265 if( !id->p_encoder->fmt_out.video.i_aspect )
1267 id->p_encoder->fmt_out.video.i_aspect =
1268 id->p_decoder->fmt_out.video.i_aspect;
1270 id->p_encoder->fmt_in.video.i_aspect =
1271 id->p_encoder->fmt_out.video.i_aspect;
1273 id->p_encoder->p_module =
1274 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1275 if( !id->p_encoder->p_module )
1277 msg_Err( p_stream, "cannot find encoder" );
1278 return VLC_EGENERIC;
1281 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1283 /* Hack for mp2v/mp1v transcoding support */
1284 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1285 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1287 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1290 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1291 &id->p_encoder->fmt_out );
1294 msg_Err( p_stream, "cannot add this stream" );
1295 return VLC_EGENERIC;
1301 static void transcode_video_close( sout_stream_t *p_stream,
1302 sout_stream_id_t *id )
1306 if( p_stream->p_sys->i_threads >= 1 )
1308 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1309 p_stream->p_sys->b_die = 1;
1310 vlc_cond_signal( &p_stream->p_sys->cond );
1311 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1312 vlc_thread_join( p_stream->p_sys );
1313 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1314 vlc_cond_destroy( &p_stream->p_sys->cond );
1318 if( id->p_decoder->p_module )
1319 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1322 if( id->p_encoder->p_module )
1323 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1326 for( i = 0; i < id->i_filter; i++ )
1328 vlc_object_detach( id->pp_filter[i] );
1329 if( id->pp_filter[i]->p_module )
1330 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1331 vlc_object_destroy( id->pp_filter[i] );
1335 static int transcode_video_process( sout_stream_t *p_stream,
1336 sout_stream_id_t *id,
1337 block_t *in, block_t **out )
1339 sout_stream_sys_t *p_sys = p_stream->p_sys;
1340 int i_duplicate = 1, i;
1344 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1346 subpicture_t *p_subpic = 0;
1348 if( p_sys->b_audio_sync )
1350 mtime_t i_video_drift;
1351 mtime_t i_master_drift = p_sys->i_master_drift;
1354 if( !id->i_initial_pts ) id->i_initial_pts = p_pic->date;
1356 if( !i_master_drift )
1358 /* No audio track ? */
1359 i_master_drift = id->i_initial_pts;
1362 i_pts = date_Get( &id->interpolated_pts ) + 1;
1363 i_video_drift = p_pic->date - i_pts;
1366 /* Set the pts of the frame being encoded */
1367 p_pic->date = i_pts;
1369 if( i_video_drift < i_master_drift - 50000 )
1371 msg_Dbg( p_stream, "dropping frame (%i)",
1372 (int)(i_video_drift - i_master_drift) );
1373 return VLC_EGENERIC;
1375 else if( i_video_drift > i_master_drift + 50000 )
1377 msg_Dbg( p_stream, "adding frame (%i)",
1378 (int)(i_video_drift - i_master_drift) );
1382 date_Increment( &id->interpolated_pts, 1 );
1385 if( !id->p_encoder->p_module )
1387 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1389 transcode_video_close( p_stream, id );
1390 id->b_transcode = VLC_FALSE;
1394 if( p_stream->p_sys->b_deinterlace )
1396 id->pp_filter[id->i_filter] =
1397 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1398 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1400 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1402 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1405 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1406 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
1407 id->pp_filter[id->i_filter]->p_module =
1408 module_Need( id->pp_filter[id->i_filter],
1409 "video filter2", "deinterlace", 0 );
1410 if( id->pp_filter[id->i_filter]->p_module ) id->i_filter++;
1413 msg_Dbg( p_stream, "no video filter found" );
1414 vlc_object_detach( id->pp_filter[id->i_filter] );
1415 vlc_object_destroy( id->pp_filter[id->i_filter] );
1419 /* Check if we need a filter for chroma conversion or resizing */
1420 if( id->p_decoder->fmt_out.video.i_chroma !=
1421 id->p_encoder->fmt_in.video.i_chroma ||
1422 id->p_decoder->fmt_out.video.i_width !=
1423 id->p_encoder->fmt_out.video.i_width ||
1424 id->p_decoder->fmt_out.video.i_height !=
1425 id->p_encoder->fmt_out.video.i_height ||
1426 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1427 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1429 id->pp_filter[id->i_filter] =
1430 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1431 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1433 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1435 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1438 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1439 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1440 id->pp_filter[id->i_filter]->p_module =
1441 module_Need( id->pp_filter[id->i_filter],
1442 "video filter2", 0, 0 );
1443 if( id->pp_filter[id->i_filter]->p_module ) id->i_filter++;
1446 msg_Dbg( p_stream, "no video filter found" );
1447 vlc_object_detach( id->pp_filter[id->i_filter] );
1448 vlc_object_destroy( id->pp_filter[id->i_filter] );
1453 /* Run filter chain */
1454 for( i = 0; i < id->i_filter; i++ )
1456 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1463 /* Check if we have a subpicture to overlay */
1464 if( p_sys->p_filter_blend )
1466 p_subpic = transcode_spu_get( p_stream, id, p_pic->date );
1467 /* TODO: get another pic */
1470 /* Overlay subpicture */
1473 int i_width, i_height;
1475 p_sys->p_filter_blend->fmt_out = id->p_encoder->fmt_in;
1476 p_sys->p_filter_blend->fmt_out.video.i_visible_width =
1477 p_sys->p_filter_blend->fmt_out.video.i_width;
1478 p_sys->p_filter_blend->fmt_out.video.i_visible_height =
1479 p_sys->p_filter_blend->fmt_out.video.i_height;
1480 p_sys->p_filter_blend->fmt_out.video.i_chroma =
1481 VLC_FOURCC('I','4','2','0');
1483 i_width = id->p_encoder->fmt_in.video.i_width;
1484 i_height = id->p_encoder->fmt_in.video.i_height;
1486 while( p_subpic != NULL )
1488 subpicture_region_t *p_region = p_subpic->p_region;
1490 while( p_region && p_sys->p_filter_blend &&
1491 p_sys->p_filter_blend->pf_video_blend )
1493 int i_x_offset = p_region->i_x + p_subpic->i_x;
1494 int i_y_offset = p_region->i_y + p_subpic->i_y;
1496 if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
1498 i_y_offset = i_height - p_region->fmt.i_height -
1501 else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
1503 i_y_offset = i_height / 2 - p_region->fmt.i_height / 2;
1506 if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
1508 i_x_offset = i_width - p_region->fmt.i_width -
1511 else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
1513 i_x_offset = i_width / 2 - p_region->fmt.i_width / 2;
1516 if( p_subpic->b_absolute )
1518 i_x_offset = p_region->i_x + p_subpic->i_x;
1519 i_y_offset = p_region->i_y + p_subpic->i_y;
1522 p_sys->p_filter_blend->fmt_in.video = p_region->fmt;
1524 p_sys->p_filter_blend->pf_video_blend(
1525 p_sys->p_filter_blend, p_pic, p_pic,
1526 &p_region->picture, i_x_offset, i_y_offset );
1528 p_region = p_region->p_next;
1531 p_subpic = p_subpic->p_next;
1535 if( p_sys->i_threads >= 1 )
1537 vlc_mutex_lock( &p_sys->lock_out );
1538 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1539 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1540 *out = p_sys->p_buffers;
1541 p_sys->p_buffers = NULL;
1542 vlc_cond_signal( &p_sys->cond );
1543 vlc_mutex_unlock( &p_sys->lock_out );
1548 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1549 block_ChainAppend( out, p_block );
1551 if( p_sys->b_audio_sync && i_duplicate > 1 )
1553 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1554 date_Increment( &id->interpolated_pts, 1 );
1555 p_pic->date = i_pts;
1556 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1557 block_ChainAppend( out, p_block );
1560 p_pic->pf_release( p_pic );
1567 static int EncoderThread( sout_stream_sys_t *p_sys )
1569 sout_stream_id_t *id = p_sys->id_video;
1573 while( !p_sys->b_die && !p_sys->b_error )
1577 vlc_mutex_lock( &p_sys->lock_out );
1578 while( p_sys->i_last_pic == p_sys->i_first_pic )
1580 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1581 if( p_sys->b_die || p_sys->b_error ) break;
1583 if( p_sys->b_die || p_sys->b_error )
1585 vlc_mutex_unlock( &p_sys->lock_out );
1589 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1590 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1591 vlc_mutex_unlock( &p_sys->lock_out );
1593 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1594 vlc_mutex_lock( &p_sys->lock_out );
1595 block_ChainAppend( &p_sys->p_buffers, p_block );
1596 vlc_mutex_unlock( &p_sys->lock_out );
1598 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1600 free( p_pic->p[i_plane].p_pixels );
1605 while( p_sys->i_last_pic != p_sys->i_first_pic )
1607 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1608 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1610 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1612 free( p_pic->p[i_plane].p_pixels );
1617 block_ChainRelease( p_sys->p_buffers );
1622 struct picture_sys_t
1627 static void video_release_buffer( picture_t *p_pic )
1629 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
1631 video_del_buffer( p_pic->p_sys->p_dec, p_pic );
1633 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
1636 static picture_t *video_new_buffer( decoder_t *p_dec )
1638 picture_t *p_pic = malloc( sizeof(picture_t) );
1640 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1641 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
1642 p_dec->fmt_out.video.i_chroma,
1643 p_dec->fmt_out.video.i_width,
1644 p_dec->fmt_out.video.i_height,
1645 p_dec->fmt_out.video.i_aspect );
1647 if( !p_pic->i_planes )
1653 p_pic->pf_release = video_release_buffer;
1654 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
1655 p_pic->p_sys->p_dec = p_dec;
1660 static void video_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1662 if( p_pic && p_pic->p_data ) free( p_pic->p_data );
1663 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
1664 if( p_pic ) free( p_pic );
1667 static void video_link_picture( decoder_t *p_dec, picture_t *p_pic )
1669 p_pic->i_refcount++;
1672 static void video_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1674 video_release_buffer( p_pic );
1680 static subpicture_t *spu_new_buffer( decoder_t * );
1681 static void spu_del_buffer( decoder_t *, subpicture_t * );
1683 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1685 sout_stream_sys_t *p_sys = p_stream->p_sys;
1691 /* Initialization of decoder structures */
1692 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
1693 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
1694 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
1696 id->p_decoder->p_module =
1697 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1699 if( !id->p_decoder->p_module )
1701 msg_Err( p_stream, "cannot find decoder" );
1702 return VLC_EGENERIC;
1705 if( !p_sys->b_soverlay )
1711 /* Initialization of encoder format structures */
1712 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1713 id->p_decoder->fmt_in.i_codec );
1715 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
1717 id->p_encoder->p_module =
1718 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
1720 if( !id->p_encoder->p_module )
1722 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1723 msg_Err( p_stream, "cannot find encoder" );
1724 return VLC_EGENERIC;
1729 p_sys->p_filter_blend =
1730 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1731 vlc_object_attach( p_sys->p_filter_blend, p_stream );
1732 p_sys->p_filter_blend->fmt_out.video.i_chroma =
1733 VLC_FOURCC('I','4','2','0');
1734 p_sys->p_filter_blend->fmt_in.video.i_chroma =
1735 VLC_FOURCC('Y','U','V','A');
1736 p_sys->p_filter_blend->p_module =
1737 module_Need( p_sys->p_filter_blend, "video blending", 0, 0 );
1743 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
1745 sout_stream_sys_t *p_sys = p_stream->p_sys;
1749 if( id->p_decoder->p_module )
1750 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1753 if( id->p_encoder->p_module )
1754 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1756 /* Free subpictures */
1757 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
1759 if( !p_sys->pp_subpics[i] ) continue;
1761 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
1762 p_sys->pp_subpics[i] = NULL;
1766 static int transcode_spu_process( sout_stream_t *p_stream,
1767 sout_stream_id_t *id,
1768 block_t *in, block_t **out )
1770 sout_stream_sys_t *p_sys = p_stream->p_sys;
1771 subpicture_t *p_subpic;
1774 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
1775 if( p_subpic && p_sys->b_soverlay )
1779 /* Find a free slot in our supictures ring buffer */
1780 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
1782 if( !p_sys->pp_subpics[i] )
1784 p_sys->pp_subpics[i] = p_subpic;
1788 if( i == SUBPICTURE_RING_SIZE )
1790 spu_del_buffer( id->p_decoder, p_subpic );
1794 if( p_subpic && !p_sys->b_soverlay )
1798 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
1799 spu_del_buffer( id->p_decoder, p_subpic );
1803 block_ChainAppend( out, p_block );
1808 return VLC_EGENERIC;
1811 static subpicture_t *transcode_spu_get( sout_stream_t *p_stream,
1812 sout_stream_id_t *id,
1813 mtime_t display_date )
1815 sout_stream_sys_t *p_sys = p_stream->p_sys;
1816 subpicture_t *p_subpic = 0;
1817 subpicture_t *p_ephemer = 0;
1818 subpicture_t **pp_subpic = &p_subpic;
1821 /* Find current subpictures and remove old ones */
1822 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
1824 if( !p_sys->pp_subpics[i] ) continue;
1826 if( !p_sys->pp_subpics[i]->b_ephemer &&
1827 p_sys->pp_subpics[i]->i_stop < display_date )
1829 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
1830 p_sys->pp_subpics[i] = NULL;
1834 if( p_sys->pp_subpics[i]->i_start > display_date ) continue;
1836 if( p_sys->pp_subpics[i]->b_ephemer && !p_ephemer )
1838 p_ephemer = p_sys->pp_subpics[i];
1840 else if( p_sys->pp_subpics[i]->b_ephemer )
1842 if( p_ephemer->i_start < p_sys->pp_subpics[i]->i_start )
1846 *p_ephemer = *p_sys->pp_subpics[i];
1847 *p_sys->pp_subpics[i] = tmp;
1850 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
1851 p_sys->pp_subpics[i] = NULL;
1855 /* Add subpicture to the list */
1856 *pp_subpic = p_sys->pp_subpics[i];
1857 pp_subpic = &p_sys->pp_subpics[i]->p_next;
1863 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1865 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1866 memset( p_subpic, 0, sizeof(subpicture_t) );
1867 p_subpic->b_absolute = VLC_TRUE;
1869 p_subpic->pf_create_region = __spu_CreateRegion;
1870 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1875 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1877 while( p_subpic->p_region )
1879 subpicture_region_t *p_region = p_subpic->p_region;
1880 p_subpic->p_region = p_region->p_next;
1881 p_subpic->pf_destroy_region( VLC_OBJECT(p_dec), p_region );