1 /*****************************************************************************
2 * transcode.c: transcoding stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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 #define MASTER_SYNC_MAX_DRIFT 100000
42 /*****************************************************************************
44 *****************************************************************************/
45 #define VENC_TEXT N_("Video encoder")
46 #define VENC_LONGTEXT N_( \
47 "Allows you to specify the video encoder to use and its associated " \
49 #define VCODEC_TEXT N_("Destination video codec")
50 #define VCODEC_LONGTEXT N_( \
51 "Allows you to specify the destination video codec used for the " \
53 #define VB_TEXT N_("Video bitrate")
54 #define VB_LONGTEXT N_( \
55 "Allows you to specify the video bitrate used for the streaming " \
57 #define SCALE_TEXT N_("Video scaling")
58 #define SCALE_LONGTEXT N_( \
59 "Allows you to scale the video before encoding." )
60 #define FPS_TEXT N_("Video frame-rate")
61 #define FPS_LONGTEXT N_( \
62 "Allows you to specify an output frame rate for the video." )
63 #define DEINTERLACE_TEXT N_("Deinterlace video")
64 #define DEINTERLACE_LONGTEXT N_( \
65 "Allows you to deinterlace the video before encoding." )
66 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
67 #define DEINTERLACE_MODULE_LONGTEXT N_( \
68 "Specifies the deinterlace module to use." )
69 #define WIDTH_TEXT N_("Video width")
70 #define WIDTH_LONGTEXT N_( \
71 "Allows you to specify the output video width." )
72 #define HEIGHT_TEXT N_("Video height")
73 #define HEIGHT_LONGTEXT N_( \
74 "Allows you to specify the output video height." )
75 #define VFILTER_TEXT N_("Video filter")
76 #define VFILTER_LONGTEXT N_( \
77 "Allows you to specify video filters used after the video " \
78 "transcoding and subpictures overlaying." )
80 #define CROPTOP_TEXT N_("Video crop top")
81 #define CROPTOP_LONGTEXT N_( \
82 "Allows you to specify the top coordinate for the video cropping." )
83 #define CROPLEFT_TEXT N_("Video crop left")
84 #define CROPLEFT_LONGTEXT N_( \
85 "Allows you to specify the left coordinate for the video cropping." )
86 #define CROPBOTTOM_TEXT N_("Video crop bottom")
87 #define CROPBOTTOM_LONGTEXT N_( \
88 "Allows you to specify the bottom coordinate for the video cropping." )
89 #define CROPRIGHT_TEXT N_("Video crop right")
90 #define CROPRIGHT_LONGTEXT N_( \
91 "Allows you to specify the right coordinate for the video cropping." )
93 #define AENC_TEXT N_("Audio encoder")
94 #define AENC_LONGTEXT N_( \
95 "Allows you to specify the audio encoder to use and its associated " \
97 #define ACODEC_TEXT N_("Destination audio codec")
98 #define ACODEC_LONGTEXT N_( \
99 "Allows you to specify the destination audio codec used for the " \
100 "streaming output." )
101 #define AB_TEXT N_("Audio bitrate")
102 #define AB_LONGTEXT N_( \
103 "Allows you to specify the audio bitrate used for the streaming " \
105 #define ARATE_TEXT N_("Audio sample rate")
106 #define ARATE_LONGTEXT N_( \
107 "Allows you to specify the audio sample rate used for the streaming " \
109 #define ACHANS_TEXT N_("Audio channels")
110 #define ACHANS_LONGTEXT N_( \
111 "Allows you to specify the number of audio channels used for the " \
112 "streaming output." )
114 #define SENC_TEXT N_("Subtitles encoder")
115 #define SENC_LONGTEXT N_( \
116 "Allows you to specify the subtitles encoder to use and its associated " \
118 #define SCODEC_TEXT N_("Destination subtitles codec")
119 #define SCODEC_LONGTEXT N_( \
120 "Allows you to specify the destination subtitles codec used for the " \
121 "streaming output." )
122 #define SFILTER_TEXT N_("Subpictures filter")
123 #define SFILTER_LONGTEXT N_( \
124 "Allows you to specify subpictures filters used during the video " \
125 "transcoding. The subpictures produced by the filters will be overlayed " \
126 "directly onto the video." )
128 #define THREADS_TEXT N_("Number of threads")
129 #define THREADS_LONGTEXT N_( \
130 "Allows you to specify the number of threads used for the transcoding." )
131 #define HP_TEXT N_("High priority")
132 #define HP_LONGTEXT N_( \
133 "Runs the optional encoder thread at the OUTPUT priority instead of " \
136 #define ASYNC_TEXT N_("Synchronise on audio track")
137 #define ASYNC_LONGTEXT N_( \
138 "This option will drop/duplicate video frames to synchronise the video " \
139 "track on the audio track." )
141 #define HURRYUP_TEXT N_( "Hurry up" )
142 #define HURRYUP_LONGTEXT N_( "Allows you to specify if the transcoder " \
143 "should drop frames if your CPU can't keep up with the encoding rate." )
145 static char *ppsz_deinterlace_type[] =
147 "deinterlace", "ffmpeg-deinterlace"
150 static int Open ( vlc_object_t * );
151 static void Close( vlc_object_t * );
153 #define SOUT_CFG_PREFIX "sout-transcode-"
156 set_shortname( _("Transcode"));
157 set_description( _("Transcode stream output") );
158 set_capability( "sout stream", 50 );
159 add_shortcut( "transcode" );
160 set_callbacks( Open, Close );
161 set_category( CAT_SOUT );
162 set_subcategory( SUBCAT_SOUT_STREAM );
163 set_section( N_("Video"), NULL );
164 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
165 VENC_LONGTEXT, VLC_FALSE );
166 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
167 VCODEC_LONGTEXT, VLC_FALSE );
168 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
169 VB_LONGTEXT, VLC_FALSE );
170 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
171 SCALE_LONGTEXT, VLC_FALSE );
172 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
173 FPS_LONGTEXT, VLC_FALSE );
174 add_bool( SOUT_CFG_PREFIX "hurry-up", VLC_TRUE, NULL, HURRYUP_TEXT,
175 HURRYUP_LONGTEXT, VLC_FALSE );
176 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
177 DEINTERLACE_LONGTEXT, VLC_FALSE );
178 add_string( SOUT_CFG_PREFIX "deinterlace-module", "deinterlace", NULL,
179 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
181 change_string_list( ppsz_deinterlace_type, 0, 0 );
182 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
183 WIDTH_LONGTEXT, VLC_TRUE );
184 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
185 HEIGHT_LONGTEXT, VLC_TRUE );
186 add_module_list_cat( SOUT_CFG_PREFIX "vfilter", SUBCAT_VIDEO_VFILTER,
188 VFILTER_TEXT, VFILTER_LONGTEXT, VLC_FALSE );
190 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
191 CROPTOP_LONGTEXT, VLC_TRUE );
192 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
193 CROPLEFT_LONGTEXT, VLC_TRUE );
194 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
195 CROPBOTTOM_LONGTEXT, VLC_TRUE );
196 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
197 CROPRIGHT_LONGTEXT, VLC_TRUE );
199 set_section( N_("Audio"), NULL );
200 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
201 AENC_LONGTEXT, VLC_FALSE );
202 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
203 ACODEC_LONGTEXT, VLC_FALSE );
204 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
205 AB_LONGTEXT, VLC_FALSE );
206 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
207 ACHANS_LONGTEXT, VLC_FALSE );
208 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
209 ARATE_LONGTEXT, VLC_TRUE );
210 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
211 ASYNC_LONGTEXT, VLC_FALSE );
213 set_section( N_("Overlays/Subtitles"), NULL );
214 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
215 SENC_LONGTEXT, VLC_FALSE );
216 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
217 SCODEC_LONGTEXT, VLC_FALSE );
218 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
219 SCODEC_LONGTEXT, VLC_FALSE );
220 add_module_list_cat( SOUT_CFG_PREFIX "sfilter", SUBCAT_VIDEO_SUBPIC,
222 SFILTER_TEXT, SFILTER_LONGTEXT, VLC_FALSE );
224 set_section( N_("Miscellaneous"), NULL );
225 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
226 THREADS_LONGTEXT, VLC_TRUE );
227 add_bool( SOUT_CFG_PREFIX "high-priority", 0, NULL, HP_TEXT, HP_LONGTEXT,
232 static const char *ppsz_sout_options[] = {
233 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
234 "scale", "fps", "width", "height", "vfilter", "deinterlace",
235 "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab",
236 "samplerate", "channels", "senc", "scodec", "soverlay", "sfilter",
237 "audio-sync", "high-priority", NULL
240 /*****************************************************************************
241 * Exported prototypes
242 *****************************************************************************/
243 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
244 static int Del ( sout_stream_t *, sout_stream_id_t * );
245 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
247 static int transcode_audio_new ( sout_stream_t *, sout_stream_id_t * );
248 static void transcode_audio_close ( sout_stream_t *, sout_stream_id_t * );
249 static int transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
250 block_t *, block_t ** );
252 static aout_buffer_t *audio_new_buffer( decoder_t *, int );
253 static void audio_del_buffer( decoder_t *, aout_buffer_t * );
255 static int transcode_video_new ( sout_stream_t *, sout_stream_id_t * );
256 static void transcode_video_close ( sout_stream_t *, sout_stream_id_t * );
257 static int transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
258 static int transcode_video_process( sout_stream_t *, sout_stream_id_t *,
259 block_t *, block_t ** );
261 static void video_del_buffer( vlc_object_t *, picture_t * );
262 static picture_t *video_new_buffer_decoder( decoder_t * );
263 static void video_del_buffer_decoder( decoder_t *, picture_t * );
264 static void video_link_picture_decoder( decoder_t *, picture_t * );
265 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
266 static picture_t *video_new_buffer_filter( filter_t * );
267 static void video_del_buffer_filter( filter_t *, picture_t * );
269 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
270 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
271 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
272 block_t *, block_t ** );
274 static int EncoderThread( struct sout_stream_sys_t * p_sys );
276 static int pi_channels_maps[6] =
279 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
280 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
281 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
282 | AOUT_CHAN_REARRIGHT,
283 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
284 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
287 #define PICTURE_RING_SIZE 64
288 #define SUBPICTURE_RING_SIZE 20
290 struct sout_stream_sys_t
294 sout_stream_t *p_out;
295 sout_stream_id_t *id_video;
297 vlc_mutex_t lock_out;
299 picture_t * pp_pics[PICTURE_RING_SIZE];
300 int i_first_pic, i_last_pic;
303 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
305 sout_cfg_t *p_audio_cfg;
311 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
313 sout_cfg_t *p_video_cfg;
319 vlc_bool_t b_deinterlace;
320 char *psz_deinterlace;
321 sout_cfg_t *p_deinterlace_cfg;
323 vlc_bool_t b_high_priority;
324 vlc_bool_t b_hurry_up;
325 char *psz_vfilters[10];
326 sout_cfg_t *p_vfilters_cfg[10];
335 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
337 vlc_bool_t b_soverlay;
338 sout_cfg_t *p_spu_cfg;
342 vlc_bool_t b_master_sync;
343 mtime_t i_master_drift;
346 struct decoder_owner_sys_t
348 picture_t *pp_pics[PICTURE_RING_SIZE];
349 sout_stream_sys_t *p_sys;
351 struct filter_owner_sys_t
353 picture_t *pp_pics[PICTURE_RING_SIZE];
354 sout_stream_sys_t *p_sys;
357 /*****************************************************************************
359 *****************************************************************************/
360 static int Open( vlc_object_t *p_this )
362 sout_stream_t *p_stream = (sout_stream_t*)p_this;
363 sout_stream_sys_t *p_sys;
366 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
368 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
371 msg_Err( p_stream, "cannot create chain" );
372 vlc_object_destroy( p_sys );
376 p_sys->i_master_drift = 0;
378 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
381 /* Audio transcoding parameters */
382 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
383 p_sys->psz_aenc = NULL;
384 p_sys->p_audio_cfg = NULL;
385 if( val.psz_string && *val.psz_string )
388 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
390 if( psz_next ) free( psz_next );
392 if( val.psz_string ) free( val.psz_string );
394 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
396 if( val.psz_string && *val.psz_string )
399 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
400 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
402 if( val.psz_string ) free( val.psz_string );
404 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
405 p_sys->i_abitrate = val.i_int;
406 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
408 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
409 p_sys->i_sample_rate = val.i_int;
411 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
412 p_sys->i_channels = val.i_int;
414 if( p_sys->i_acodec )
416 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
417 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
418 p_sys->i_channels, p_sys->i_abitrate / 1000 );
421 /* Video transcoding parameters */
422 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
423 p_sys->psz_venc = NULL;
424 p_sys->p_video_cfg = NULL;
425 if( val.psz_string && *val.psz_string )
428 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
430 if( psz_next ) free( psz_next );
432 if( val.psz_string ) free( val.psz_string );
434 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
436 if( val.psz_string && *val.psz_string )
439 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
440 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
442 if( val.psz_string ) free( val.psz_string );
444 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
445 p_sys->i_vbitrate = val.i_int;
446 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
448 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
449 p_sys->f_scale = val.f_float;
451 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
452 p_sys->f_fps = val.f_float;
454 var_Get( p_stream, SOUT_CFG_PREFIX "hurry-up", &val );
455 p_sys->b_hurry_up = val.b_bool;
457 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
458 p_sys->i_width = val.i_int;
460 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
461 p_sys->i_height = val.i_int;
463 var_Get( p_stream, SOUT_CFG_PREFIX "vfilter", &val );
464 p_sys->i_vfilters = 0;
465 if( val.psz_string && *val.psz_string )
467 char *psz_parser = val.psz_string;
469 while( psz_parser != NULL && *psz_parser != '\0' )
471 psz_parser = sout_CfgCreate(
472 &p_sys->psz_vfilters[p_sys->i_vfilters],
473 &p_sys->p_vfilters_cfg[p_sys->i_vfilters],
476 if( psz_parser != NULL && *psz_parser != '\0' ) psz_parser++;
479 if( val.psz_string ) free( val.psz_string );
480 p_sys->psz_vfilters[p_sys->i_vfilters] = NULL;
481 p_sys->p_vfilters_cfg[p_sys->i_vfilters] = NULL;
483 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
484 p_sys->b_deinterlace = val.b_bool;
486 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace-module", &val );
487 p_sys->psz_deinterlace = NULL;
488 p_sys->p_deinterlace_cfg = NULL;
489 if( val.psz_string && *val.psz_string )
492 psz_next = sout_CfgCreate( &p_sys->psz_deinterlace,
493 &p_sys->p_deinterlace_cfg,
495 if( psz_next ) free( psz_next );
497 if( val.psz_string ) free( val.psz_string );
499 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
500 p_sys->i_crop_top = val.i_int;
501 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
502 p_sys->i_crop_bottom = val.i_int;
503 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
504 p_sys->i_crop_left = val.i_int;
505 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
506 p_sys->i_crop_right = val.i_int;
508 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
509 p_sys->i_threads = val.i_int;
510 var_Get( p_stream, SOUT_CFG_PREFIX "high-priority", &val );
511 p_sys->b_high_priority = val.b_bool;
513 if( p_sys->i_vcodec )
515 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
516 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
517 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
520 /* Subpictures transcoding parameters */
522 p_sys->psz_senc = NULL;
523 p_sys->p_spu_cfg = NULL;
526 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
527 if( val.psz_string && *val.psz_string )
530 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
532 if( psz_next ) free( psz_next );
534 if( val.psz_string ) free( val.psz_string );
536 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
537 if( val.psz_string && *val.psz_string )
540 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
541 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
543 if( val.psz_string ) free( val.psz_string );
545 if( p_sys->i_scodec )
547 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
550 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
551 p_sys->b_soverlay = val.b_bool;
553 var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
554 if( val.psz_string && *val.psz_string )
556 p_sys->p_spu = spu_Create( p_stream );
557 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
558 var_Set( p_sys->p_spu, "sub-filter", val );
559 spu_Init( p_sys->p_spu );
561 if( val.psz_string ) free( val.psz_string );
563 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
564 p_sys->b_master_sync = val.b_bool;
565 if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
567 p_stream->pf_add = Add;
568 p_stream->pf_del = Del;
569 p_stream->pf_send = Send;
570 p_stream->p_sys = p_sys;
575 /*****************************************************************************
577 *****************************************************************************/
578 static void Close( vlc_object_t * p_this )
580 sout_stream_t *p_stream = (sout_stream_t*)p_this;
581 sout_stream_sys_t *p_sys = p_stream->p_sys;
583 sout_StreamDelete( p_sys->p_out );
585 while( p_sys->p_audio_cfg != NULL )
587 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
589 if( p_sys->p_audio_cfg->psz_name )
590 free( p_sys->p_audio_cfg->psz_name );
591 if( p_sys->p_audio_cfg->psz_value )
592 free( p_sys->p_audio_cfg->psz_value );
593 free( p_sys->p_audio_cfg );
595 p_sys->p_audio_cfg = p_next;
597 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
599 while( p_sys->p_video_cfg != NULL )
601 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
603 if( p_sys->p_video_cfg->psz_name )
604 free( p_sys->p_video_cfg->psz_name );
605 if( p_sys->p_video_cfg->psz_value )
606 free( p_sys->p_video_cfg->psz_value );
607 free( p_sys->p_video_cfg );
609 p_sys->p_video_cfg = p_next;
611 if( p_sys->psz_venc ) free( p_sys->psz_venc );
613 while( p_sys->p_deinterlace_cfg != NULL )
615 sout_cfg_t *p_next = p_sys->p_deinterlace_cfg->p_next;
617 if( p_sys->p_deinterlace_cfg->psz_name )
618 free( p_sys->p_deinterlace_cfg->psz_name );
619 if( p_sys->p_deinterlace_cfg->psz_value )
620 free( p_sys->p_deinterlace_cfg->psz_value );
621 free( p_sys->p_deinterlace_cfg );
623 p_sys->p_deinterlace_cfg = p_next;
625 if( p_sys->psz_deinterlace ) free( p_sys->psz_deinterlace );
627 while( p_sys->p_spu_cfg != NULL )
629 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
631 if( p_sys->p_spu_cfg->psz_name )
632 free( p_sys->p_spu_cfg->psz_name );
633 if( p_sys->p_spu_cfg->psz_value )
634 free( p_sys->p_spu_cfg->psz_value );
635 free( p_sys->p_spu_cfg );
637 p_sys->p_spu_cfg = p_next;
639 if( p_sys->psz_senc ) free( p_sys->psz_senc );
641 if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
643 vlc_object_destroy( p_sys );
646 struct sout_stream_id_t
648 vlc_fourcc_t b_transcode;
650 /* id of the out stream */
654 decoder_t *p_decoder;
657 filter_t *pp_filter[10];
659 filter_t *pp_vfilter[10];
663 encoder_t *p_encoder;
666 date_t interpolated_pts;
670 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
672 sout_stream_sys_t *p_sys = p_stream->p_sys;
673 sout_stream_id_t *id;
675 id = malloc( sizeof( sout_stream_id_t ) );
676 memset( id, 0, sizeof(sout_stream_id_t) );
679 id->p_decoder = NULL;
680 id->p_encoder = NULL;
682 /* Create decoder object */
683 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
686 msg_Err( p_stream, "out of memory" );
689 vlc_object_attach( id->p_decoder, p_stream );
690 id->p_decoder->p_module = NULL;
691 id->p_decoder->fmt_in = *p_fmt;
692 id->p_decoder->b_pace_control = VLC_TRUE;
694 /* Create encoder object */
695 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
698 msg_Err( p_stream, "out of memory" );
701 vlc_object_attach( id->p_encoder, p_stream );
702 id->p_encoder->p_module = NULL;
704 /* Create destination format */
705 es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
706 id->p_encoder->fmt_out.i_id = p_fmt->i_id;
707 id->p_encoder->fmt_out.i_group = p_fmt->i_group;
708 if( p_fmt->psz_language )
709 id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
711 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
714 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
715 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
717 /* Complete destination format */
718 id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
719 id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
720 p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
721 id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
722 id->p_encoder->fmt_out.audio.i_bitspersample =
723 p_fmt->audio.i_bitspersample;
724 id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
725 p_sys->i_channels : p_fmt->audio.i_channels;
726 /* Sanity check for audio channels */
727 id->p_encoder->fmt_out.audio.i_channels =
728 __MIN( id->p_encoder->fmt_out.audio.i_channels,
729 id->p_decoder->fmt_in.audio.i_channels );
730 id->p_encoder->fmt_out.audio.i_original_channels =
731 id->p_decoder->fmt_in.audio.i_physical_channels;
732 if( id->p_decoder->fmt_in.audio.i_channels ==
733 id->p_encoder->fmt_out.audio.i_channels )
735 id->p_encoder->fmt_out.audio.i_physical_channels =
736 id->p_decoder->fmt_in.audio.i_physical_channels;
740 id->p_encoder->fmt_out.audio.i_physical_channels =
741 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
744 /* Build decoder -> filter -> encoder chain */
745 if( transcode_audio_new( p_stream, id ) )
747 msg_Err( p_stream, "cannot create audio chain" );
751 /* Open output stream */
752 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
753 id->b_transcode = VLC_TRUE;
755 if( !id->id ) goto error;
757 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
759 else if( p_fmt->i_cat == VIDEO_ES &&
760 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
763 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
764 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
766 /* Complete destination format */
767 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
768 id->p_encoder->fmt_out.video.i_width = p_sys->i_width;
769 id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
770 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
772 /* Build decoder -> filter -> encoder chain */
773 if( transcode_video_new( p_stream, id ) )
775 msg_Err( p_stream, "cannot create video chain" );
779 /* Stream will be added later on because we don't know
780 * all the characteristics of the decoded stream yet */
781 id->b_transcode = VLC_TRUE;
783 if( p_sys->f_fps > 0 )
785 id->p_encoder->fmt_out.video.i_frame_rate =
786 (p_sys->f_fps * 1001) + 0.5;
787 id->p_encoder->fmt_out.video.i_frame_rate_base = 1001;
790 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
792 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
793 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
794 (char*)&p_sys->i_scodec );
796 /* Complete destination format */
797 id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
799 /* build decoder -> filter -> encoder */
800 if( transcode_spu_new( p_stream, id ) )
802 msg_Err( p_stream, "cannot create subtitles chain" );
806 /* open output stream */
807 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
808 id->b_transcode = VLC_TRUE;
810 if( !id->id ) goto error;
812 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
814 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
815 (char*)&p_fmt->i_codec );
817 id->b_transcode = VLC_TRUE;
819 /* Build decoder -> filter -> overlaying chain */
820 if( transcode_spu_new( p_stream, id ) )
822 msg_Err( p_stream, "cannot create subtitles chain" );
828 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
829 (char*)&p_fmt->i_codec );
830 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
831 id->b_transcode = VLC_FALSE;
833 if( !id->id ) goto error;
841 vlc_object_detach( id->p_decoder );
842 vlc_object_destroy( id->p_decoder );
847 vlc_object_detach( id->p_encoder );
848 vlc_object_destroy( id->p_encoder );
855 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
857 sout_stream_sys_t *p_sys = p_stream->p_sys;
859 if( id->b_transcode )
861 switch( id->p_decoder->fmt_in.i_cat )
864 transcode_audio_close( p_stream, id );
867 transcode_video_close( p_stream, id );
870 transcode_spu_close( p_stream, id );
875 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
879 vlc_object_detach( id->p_decoder );
880 vlc_object_destroy( id->p_decoder );
885 vlc_object_detach( id->p_encoder );
886 vlc_object_destroy( id->p_encoder );
894 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
897 sout_stream_sys_t *p_sys = p_stream->p_sys;
900 if( !id->b_transcode && id->id )
902 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
904 else if( !id->b_transcode )
906 block_Release( p_buffer );
910 switch( id->p_decoder->fmt_in.i_cat )
913 transcode_audio_process( p_stream, id, p_buffer, &p_out );
917 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
925 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
934 block_Release( p_buffer );
938 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
942 /****************************************************************************
943 * decoder reencoder part
944 ****************************************************************************/
945 int audio_BitsPerSample( vlc_fourcc_t i_format )
949 case VLC_FOURCC('u','8',' ',' '):
950 case VLC_FOURCC('s','8',' ',' '):
953 case VLC_FOURCC('u','1','6','l'):
954 case VLC_FOURCC('s','1','6','l'):
955 case VLC_FOURCC('u','1','6','b'):
956 case VLC_FOURCC('s','1','6','b'):
959 case VLC_FOURCC('u','2','4','l'):
960 case VLC_FOURCC('s','2','4','l'):
961 case VLC_FOURCC('u','2','4','b'):
962 case VLC_FOURCC('s','2','4','b'):
965 case VLC_FOURCC('u','3','2','l'):
966 case VLC_FOURCC('s','3','2','l'):
967 case VLC_FOURCC('u','3','2','b'):
968 case VLC_FOURCC('s','3','2','b'):
969 case VLC_FOURCC('f','l','3','2'):
970 case VLC_FOURCC('f','i','3','2'):
973 case VLC_FOURCC('f','l','6','4'):
980 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
981 sout_stream_id_t *id,
982 es_format_t *p_fmt_in,
983 es_format_t *p_fmt_out )
985 filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
987 vlc_object_attach( p_filter, p_stream );
988 p_filter->pf_audio_buffer_new = (block_t* (*) (filter_t*, int))__block_New;
990 p_filter->fmt_in = *p_fmt_in;
991 p_filter->fmt_out = *p_fmt_out;
993 p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
994 if( p_filter->p_module )
996 p_filter->fmt_out.audio.i_bitspersample =
997 audio_BitsPerSample( p_filter->fmt_out.i_codec );
998 *p_fmt_in = p_filter->fmt_out;
1002 vlc_object_detach( p_filter );
1003 vlc_object_destroy( p_filter );
1010 static int transcode_audio_new( sout_stream_t *p_stream,
1011 sout_stream_id_t *id )
1013 sout_stream_sys_t *p_sys = p_stream->p_sys;
1014 es_format_t fmt_last;
1021 /* Initialization of decoder structures */
1022 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1023 id->p_decoder->fmt_out.i_extra = 0;
1024 id->p_decoder->fmt_out.p_extra = 0;
1025 id->p_decoder->pf_decode_audio = 0;
1026 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
1027 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1028 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1030 id->p_decoder->p_module =
1031 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1033 if( !id->p_decoder->p_module )
1035 msg_Err( p_stream, "cannot find decoder" );
1036 return VLC_EGENERIC;
1038 id->p_decoder->fmt_out.audio.i_bitspersample =
1039 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1040 fmt_last = id->p_decoder->fmt_out;
1041 /* FIX decoders so we don't have to do this */
1042 fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1048 /* Initialization of encoder format structures */
1049 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1050 id->p_decoder->fmt_out.i_codec );
1051 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
1053 /* Initialization of encoder format structures */
1054 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1055 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1056 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
1057 id->p_encoder->fmt_in.audio.i_physical_channels =
1058 id->p_encoder->fmt_out.audio.i_physical_channels;
1059 id->p_encoder->fmt_in.audio.i_original_channels =
1060 id->p_encoder->fmt_out.audio.i_original_channels;
1061 id->p_encoder->fmt_in.audio.i_channels =
1062 id->p_encoder->fmt_out.audio.i_channels;
1063 id->p_encoder->fmt_in.audio.i_bitspersample =
1064 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1066 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1068 id->p_encoder->p_module =
1069 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
1070 if( !id->p_encoder->p_module )
1072 msg_Err( p_stream, "cannot find encoder" );
1073 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1074 id->p_decoder->p_module = 0;
1075 return VLC_EGENERIC;
1077 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1078 id->p_encoder->fmt_in.audio.i_bitspersample =
1079 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1081 /* Load conversion filters */
1082 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1083 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1085 /* We'll have to go through fl32 first */
1086 es_format_t fmt_out = id->p_encoder->fmt_in;
1087 fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1089 id->pp_filter[id->i_filter] =
1090 transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );
1092 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1097 if( fmt_last.audio.i_channels !=
1098 id->p_encoder->fmt_in.audio.i_channels ||
1099 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
1100 fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1102 id->pp_filter[id->i_filter] =
1103 transcode_audio_filter_new( p_stream, id, &fmt_last,
1104 &id->p_encoder->fmt_in );
1106 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1111 /* Final checks to see if conversions were successful */
1112 if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1114 msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1115 (char *)&fmt_last.i_codec,
1116 (char *)&id->p_encoder->fmt_in.i_codec );
1117 transcode_audio_close( p_stream, id );
1118 return VLC_EGENERIC;
1121 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1123 msg_Err( p_stream, "no audio filter found for mixing from"
1124 " %i to %i channels", fmt_last.audio.i_channels,
1125 id->p_encoder->fmt_in.audio.i_channels );
1127 /* FIXME : this might work, but only if the encoder is restarted */
1128 id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1129 id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1131 id->p_encoder->fmt_in.audio.i_physical_channels =
1132 id->p_encoder->fmt_in.audio.i_original_channels =
1133 fmt_last.audio.i_physical_channels;
1134 id->p_encoder->fmt_out.audio.i_physical_channels =
1135 id->p_encoder->fmt_out.audio.i_original_channels =
1136 fmt_last.audio.i_physical_channels;
1138 transcode_audio_close( p_stream, id );
1139 return VLC_EGENERIC;
1143 if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1145 msg_Err( p_stream, "no audio filter found for resampling from"
1146 " %iHz to %iHz", fmt_last.audio.i_rate,
1147 id->p_encoder->fmt_in.audio.i_rate );
1149 /* FIXME : this might work, but only if the encoder is restarted */
1150 id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1151 id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1153 transcode_audio_close( p_stream, id );
1154 return VLC_EGENERIC;
1158 /* FIXME: Hack for mp3 transcoding support */
1159 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1160 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1165 static void transcode_audio_close( sout_stream_t *p_stream,
1166 sout_stream_id_t *id )
1171 if( id->p_decoder->p_module )
1172 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1173 id->p_decoder->p_module = 0;
1176 if( id->p_encoder->p_module )
1177 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1178 id->p_encoder->p_module = 0;
1181 for( i = 0; i < id->i_filter; i++ )
1183 vlc_object_detach( id->pp_filter[i] );
1184 if( id->pp_filter[i]->p_module )
1185 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1186 vlc_object_destroy( id->pp_filter[i] );
1190 static int transcode_audio_process( sout_stream_t *p_stream,
1191 sout_stream_id_t *id,
1192 block_t *in, block_t **out )
1194 sout_stream_sys_t *p_sys = p_stream->p_sys;
1195 aout_buffer_t *p_audio_buf;
1196 block_t *p_block, *p_audio_block;
1200 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1203 if( p_sys->b_master_sync )
1205 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1206 if ( p_audio_buf->start_date - i_dts > MASTER_SYNC_MAX_DRIFT
1207 || p_audio_buf->start_date - i_dts < -MASTER_SYNC_MAX_DRIFT )
1209 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1210 date_Set( &id->interpolated_pts, p_audio_buf->start_date );
1211 i_dts = p_audio_buf->start_date + 1;
1213 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1214 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1215 p_audio_buf->start_date -= p_sys->i_master_drift;
1216 p_audio_buf->end_date -= p_sys->i_master_drift;
1219 p_audio_block = p_audio_buf->p_sys;
1220 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1221 p_audio_block->i_dts = p_audio_block->i_pts =
1222 p_audio_buf->start_date;
1223 p_audio_block->i_length = p_audio_buf->end_date -
1224 p_audio_buf->start_date;
1225 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1227 /* Run filter chain */
1228 for( i = 0; i < id->i_filter; i++ )
1231 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1235 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1236 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1237 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1238 p_audio_buf->start_date = p_audio_block->i_dts;
1239 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1241 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1242 block_ChainAppend( out, p_block );
1243 block_Release( p_audio_block );
1244 free( p_audio_buf );
1250 static void audio_release_buffer( aout_buffer_t *p_buffer )
1252 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1253 if( p_buffer ) free( p_buffer );
1256 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1258 aout_buffer_t *p_buffer;
1262 if( p_dec->fmt_out.audio.i_bitspersample )
1264 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1265 p_dec->fmt_out.audio.i_channels;
1267 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1268 p_dec->fmt_out.audio.i_frame_length )
1270 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1271 p_dec->fmt_out.audio.i_frame_length;
1275 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1278 p_buffer = malloc( sizeof(aout_buffer_t) );
1279 p_buffer->pf_release = audio_release_buffer;
1280 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1282 p_buffer->p_buffer = p_block->p_buffer;
1283 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1284 p_buffer->i_nb_samples = i_samples;
1285 p_block->i_samples = i_samples;
1290 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1292 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1293 if( p_buffer ) free( p_buffer );
1299 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1301 sout_stream_sys_t *p_sys = p_stream->p_sys;
1308 /* Initialization of decoder structures */
1309 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1310 id->p_decoder->fmt_out.i_extra = 0;
1311 id->p_decoder->fmt_out.p_extra = 0;
1312 id->p_decoder->pf_decode_video = 0;
1313 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1314 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1315 id->p_decoder->pf_picture_link = video_link_picture_decoder;
1316 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
1317 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1318 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1319 id->p_decoder->p_owner->pp_pics[i] = 0;
1320 id->p_decoder->p_owner->p_sys = p_sys;
1321 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1323 id->p_decoder->p_module =
1324 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1326 if( !id->p_decoder->p_module )
1328 msg_Err( p_stream, "cannot find decoder" );
1329 return VLC_EGENERIC;
1334 * Because some info about the decoded input will only be available
1335 * once the first frame is decoded, we actually only test the availability
1336 * of the encoder here.
1339 /* Initialization of encoder format structures */
1340 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1341 id->p_decoder->fmt_out.i_codec );
1342 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1344 /* The dimensions will be set properly later on.
1345 * Just put sensible values so we can test an encoder is available. */
1346 id->p_encoder->fmt_in.video.i_width =
1347 id->p_encoder->fmt_out.video.i_width ?
1348 id->p_encoder->fmt_out.video.i_width :
1349 id->p_decoder->fmt_in.video.i_width ?
1350 id->p_decoder->fmt_in.video.i_width : 16;
1351 id->p_encoder->fmt_in.video.i_height =
1352 id->p_encoder->fmt_out.video.i_height ?
1353 id->p_encoder->fmt_out.video.i_height :
1354 id->p_decoder->fmt_in.video.i_height ?
1355 id->p_decoder->fmt_in.video.i_height : 16;
1356 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1357 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1359 id->p_encoder->i_threads = p_sys->i_threads;
1360 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1362 id->p_encoder->p_module =
1363 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1364 if( !id->p_encoder->p_module )
1366 msg_Err( p_stream, "cannot find encoder" );
1367 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1368 id->p_decoder->p_module = 0;
1369 return VLC_EGENERIC;
1372 /* Close the encoder.
1373 * We'll open it only when we have the first frame. */
1374 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1375 id->p_encoder->p_module = NULL;
1377 if( p_sys->i_threads >= 1 )
1379 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
1380 VLC_THREAD_PRIORITY_VIDEO;
1381 p_sys->id_video = id;
1382 vlc_mutex_init( p_stream, &p_sys->lock_out );
1383 vlc_cond_init( p_stream, &p_sys->cond );
1384 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1385 p_sys->i_first_pic = 0;
1386 p_sys->i_last_pic = 0;
1387 p_sys->p_buffers = NULL;
1388 p_sys->b_die = p_sys->b_error = 0;
1389 if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
1392 msg_Err( p_stream, "cannot spawn encoder thread" );
1393 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1394 id->p_decoder->p_module = 0;
1395 return VLC_EGENERIC;
1402 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1403 sout_stream_id_t *id )
1405 sout_stream_sys_t *p_sys = p_stream->p_sys;
1407 /* Hack because of the copy packetizer which can fail to detect the
1408 * proper size (which forces us to wait until the 1st frame
1410 int i_width = id->p_decoder->fmt_out.video.i_width -
1411 p_sys->i_crop_left - p_sys->i_crop_right;
1412 int i_height = id->p_decoder->fmt_out.video.i_height -
1413 p_sys->i_crop_top - p_sys->i_crop_bottom;
1415 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1416 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1418 /* Apply the scaling */
1419 id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1420 id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1422 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1423 id->p_encoder->fmt_out.video.i_height <= 0 )
1425 id->p_encoder->fmt_out.video.i_height =
1426 id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1428 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1429 id->p_encoder->fmt_out.video.i_height > 0 )
1431 id->p_encoder->fmt_out.video.i_width =
1432 id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1435 /* Make sure the size is at least a multiple of 2 */
1436 id->p_encoder->fmt_out.video.i_width =
1437 (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
1438 id->p_encoder->fmt_out.video.i_height =
1439 (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
1441 id->p_encoder->fmt_in.video.i_width =
1442 id->p_encoder->fmt_out.video.i_width;
1443 id->p_encoder->fmt_in.video.i_height =
1444 id->p_encoder->fmt_out.video.i_height;
1446 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1447 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1449 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1450 id->p_decoder->fmt_out.video.i_frame_rate_base )
1452 id->p_encoder->fmt_out.video.i_frame_rate =
1453 id->p_decoder->fmt_out.video.i_frame_rate;
1454 id->p_encoder->fmt_out.video.i_frame_rate_base =
1455 id->p_decoder->fmt_out.video.i_frame_rate_base;
1459 /* Pick a sensible default value */
1460 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1461 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1465 id->p_encoder->fmt_in.video.i_frame_rate =
1466 id->p_encoder->fmt_out.video.i_frame_rate;
1467 id->p_encoder->fmt_in.video.i_frame_rate_base =
1468 id->p_encoder->fmt_out.video.i_frame_rate_base;
1470 date_Init( &id->interpolated_pts,
1471 id->p_encoder->fmt_out.video.i_frame_rate,
1472 id->p_encoder->fmt_out.video.i_frame_rate_base );
1474 /* Check whether a particular aspect ratio was requested */
1475 if( !id->p_encoder->fmt_out.video.i_aspect )
1477 id->p_encoder->fmt_out.video.i_aspect =
1478 id->p_decoder->fmt_out.video.i_aspect;
1480 id->p_encoder->fmt_in.video.i_aspect =
1481 id->p_encoder->fmt_out.video.i_aspect;
1483 id->p_encoder->p_module =
1484 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1485 if( !id->p_encoder->p_module )
1487 msg_Err( p_stream, "cannot find encoder" );
1488 return VLC_EGENERIC;
1491 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1493 /* Hack for mp2v/mp1v transcoding support */
1494 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1495 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1497 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1500 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1501 &id->p_encoder->fmt_out );
1504 msg_Err( p_stream, "cannot add this stream" );
1505 return VLC_EGENERIC;
1511 static void transcode_video_close( sout_stream_t *p_stream,
1512 sout_stream_id_t *id )
1516 if( p_stream->p_sys->i_threads >= 1 )
1518 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1519 p_stream->p_sys->b_die = 1;
1520 vlc_cond_signal( &p_stream->p_sys->cond );
1521 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1522 vlc_thread_join( p_stream->p_sys );
1523 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1524 vlc_cond_destroy( &p_stream->p_sys->cond );
1528 if( id->p_decoder->p_module )
1529 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1531 if( id->p_decoder->p_owner )
1533 /* Clean-up pictures ring buffer */
1534 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1536 if( id->p_decoder->p_owner->pp_pics[i] )
1537 video_del_buffer( VLC_OBJECT(id->p_decoder),
1538 id->p_decoder->p_owner->pp_pics[i] );
1540 free( id->p_decoder->p_owner );
1544 if( id->p_encoder->p_module )
1545 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1548 for( i = 0; i < id->i_filter; i++ )
1550 vlc_object_detach( id->pp_filter[i] );
1551 if( id->pp_filter[i]->p_module )
1552 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1554 /* Clean-up pictures ring buffer */
1555 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1557 if( id->pp_filter[i]->p_owner->pp_pics[j] )
1558 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1559 id->pp_filter[i]->p_owner->pp_pics[j] );
1561 free( id->pp_filter[i]->p_owner );
1563 vlc_object_destroy( id->pp_filter[i] );
1565 for( i = 0; i < id->i_vfilter; i++ )
1567 vlc_object_detach( id->pp_vfilter[i] );
1568 if( id->pp_vfilter[i]->p_module )
1569 module_Unneed( id->pp_vfilter[i], id->pp_vfilter[i]->p_module );
1571 /* Clean-up pictures ring buffer */
1572 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1574 if( id->pp_vfilter[i]->p_owner->pp_pics[j] )
1575 video_del_buffer( VLC_OBJECT(id->pp_vfilter[i]),
1576 id->pp_vfilter[i]->p_owner->pp_pics[j] );
1578 free( id->pp_vfilter[i]->p_owner );
1580 vlc_object_destroy( id->pp_vfilter[i] );
1584 static int transcode_video_process( sout_stream_t *p_stream,
1585 sout_stream_id_t *id,
1586 block_t *in, block_t **out )
1588 sout_stream_sys_t *p_sys = p_stream->p_sys;
1589 int i_duplicate = 1, i;
1593 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1595 subpicture_t *p_subpic = 0;
1597 if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
1599 mtime_t current_date = mdate();
1600 if( current_date + 50000 > p_pic->date )
1602 msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
1603 current_date + 50000 - p_pic->date );
1604 p_pic->pf_release( p_pic );
1609 if( p_sys->b_master_sync )
1611 mtime_t i_video_drift;
1612 mtime_t i_master_drift = p_sys->i_master_drift;
1615 i_pts = date_Get( &id->interpolated_pts ) + 1;
1616 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
1617 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
1619 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1620 date_Set( &id->interpolated_pts, p_pic->date );
1621 i_pts = p_pic->date + 1;
1623 i_video_drift = p_pic->date - i_pts;
1626 /* Set the pts of the frame being encoded */
1627 p_pic->date = i_pts;
1629 if( i_video_drift < i_master_drift - 50000 )
1632 msg_Dbg( p_stream, "dropping frame (%i)",
1633 (int)(i_video_drift - i_master_drift) );
1635 p_pic->pf_release( p_pic );
1638 else if( i_video_drift > i_master_drift + 50000 )
1641 msg_Dbg( p_stream, "adding frame (%i)",
1642 (int)(i_video_drift - i_master_drift) );
1648 if( !id->p_encoder->p_module )
1650 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1652 p_pic->pf_release( p_pic );
1653 transcode_video_close( p_stream, id );
1654 id->b_transcode = VLC_FALSE;
1655 return VLC_EGENERIC;
1659 if( p_stream->p_sys->b_deinterlace )
1661 id->pp_filter[id->i_filter] =
1662 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1663 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1665 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1666 video_new_buffer_filter;
1667 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1668 video_del_buffer_filter;
1670 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1671 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
1672 id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
1673 id->pp_filter[id->i_filter]->p_module =
1674 module_Need( id->pp_filter[id->i_filter],
1675 "video filter2", p_sys->psz_deinterlace, 0 );
1676 if( id->pp_filter[id->i_filter]->p_module )
1678 id->pp_filter[id->i_filter]->p_owner =
1679 malloc( sizeof(filter_owner_sys_t) );
1680 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1681 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1682 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1688 msg_Dbg( p_stream, "no video filter found" );
1689 vlc_object_detach( id->pp_filter[id->i_filter] );
1690 vlc_object_destroy( id->pp_filter[id->i_filter] );
1694 /* Check if we need a filter for chroma conversion or resizing */
1695 if( id->p_decoder->fmt_out.video.i_chroma !=
1696 id->p_encoder->fmt_in.video.i_chroma ||
1697 id->p_decoder->fmt_out.video.i_width !=
1698 id->p_encoder->fmt_out.video.i_width ||
1699 id->p_decoder->fmt_out.video.i_height !=
1700 id->p_encoder->fmt_out.video.i_height ||
1701 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1702 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1704 id->pp_filter[id->i_filter] =
1705 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1706 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1708 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1709 video_new_buffer_filter;
1710 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1711 video_del_buffer_filter;
1713 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1714 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1715 id->pp_filter[id->i_filter]->p_cfg = NULL;
1716 id->pp_filter[id->i_filter]->p_module =
1717 module_Need( id->pp_filter[id->i_filter],
1718 "video filter2", 0, 0 );
1719 if( id->pp_filter[id->i_filter]->p_module )
1721 id->pp_filter[id->i_filter]->p_owner =
1722 malloc( sizeof(filter_owner_sys_t) );
1723 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1724 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1725 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1731 msg_Dbg( p_stream, "no video filter found" );
1732 vlc_object_detach( id->pp_filter[id->i_filter] );
1733 vlc_object_destroy( id->pp_filter[id->i_filter] );
1735 p_pic->pf_release( p_pic );
1736 transcode_video_close( p_stream, id );
1737 id->b_transcode = VLC_FALSE;
1738 return VLC_EGENERIC;
1742 for( i = 0; i < p_sys->i_vfilters; i++ )
1744 id->pp_vfilter[id->i_vfilter] =
1745 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1746 vlc_object_attach( id->pp_vfilter[id->i_vfilter], p_stream );
1748 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_new =
1749 video_new_buffer_filter;
1750 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_del =
1751 video_del_buffer_filter;
1753 id->pp_vfilter[id->i_vfilter]->fmt_in = id->p_encoder->fmt_in;
1754 id->pp_vfilter[id->i_vfilter]->fmt_out = id->p_encoder->fmt_in;
1755 id->pp_vfilter[id->i_vfilter]->p_cfg = p_sys->p_vfilters_cfg[i];
1756 id->pp_vfilter[id->i_vfilter]->p_module =
1757 module_Need( id->pp_vfilter[id->i_vfilter],
1758 "video filter2", p_sys->psz_vfilters[i], 0 );
1759 if( id->pp_vfilter[id->i_vfilter]->p_module )
1761 id->pp_vfilter[id->i_vfilter]->p_owner =
1762 malloc( sizeof(filter_owner_sys_t) );
1763 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1764 id->pp_vfilter[id->i_vfilter]->p_owner->pp_pics[i] = 0;
1765 id->pp_vfilter[id->i_vfilter]->p_owner->p_sys = p_sys;
1771 msg_Dbg( p_stream, "no video filter found" );
1772 vlc_object_detach( id->pp_vfilter[id->i_vfilter] );
1773 vlc_object_destroy( id->pp_vfilter[id->i_vfilter] );
1778 /* Run filter chain */
1779 for( i = 0; i < id->i_filter; i++ )
1781 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1788 /* Check if we have a subpicture to overlay */
1791 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
1792 /* TODO: get another pic */
1795 /* Overlay subpicture */
1798 int i_scale_width, i_scale_height;
1799 video_format_t *p_fmt;
1801 i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
1802 id->p_decoder->fmt_out.video.i_width;
1803 i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
1804 id->p_decoder->fmt_out.video.i_height;
1806 if( p_pic->i_refcount && !id->i_filter )
1808 /* We can't modify the picture, we need to duplicate it */
1809 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1812 vout_CopyPicture( p_stream, p_tmp, p_pic );
1813 p_pic->pf_release( p_pic );
1819 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
1821 p_fmt = &id->p_decoder->fmt_out.video;
1823 /* FIXME (shouldn't have to be done here) */
1824 p_fmt->i_sar_num = p_fmt->i_aspect *
1825 p_fmt->i_height / p_fmt->i_width;
1826 p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
1828 spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
1829 i_scale_width, i_scale_height );
1832 /* Run vfilter chain */
1833 for( i = 0; i < id->i_vfilter; i++ )
1835 p_pic = id->pp_vfilter[i]->pf_video_filter(id->pp_vfilter[i], p_pic);
1838 if( p_sys->i_threads >= 1 )
1840 vlc_mutex_lock( &p_sys->lock_out );
1841 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1842 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1843 *out = p_sys->p_buffers;
1844 p_sys->p_buffers = NULL;
1845 vlc_cond_signal( &p_sys->cond );
1846 vlc_mutex_unlock( &p_sys->lock_out );
1851 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1852 block_ChainAppend( out, p_block );
1855 if( p_sys->b_master_sync )
1857 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1858 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
1859 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
1861 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1862 date_Set( &id->interpolated_pts, p_pic->date );
1863 i_pts = p_pic->date + 1;
1865 date_Increment( &id->interpolated_pts, 1 );
1868 if( p_sys->b_master_sync && i_duplicate > 1 )
1870 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1871 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
1872 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
1874 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1875 date_Set( &id->interpolated_pts, p_pic->date );
1876 i_pts = p_pic->date + 1;
1878 date_Increment( &id->interpolated_pts, 1 );
1880 if( p_sys->i_threads >= 1 )
1882 /* We can't modify the picture, we need to duplicate it */
1883 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1886 vout_CopyPicture( p_stream, p_tmp, p_pic );
1889 vlc_mutex_lock( &p_sys->lock_out );
1890 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1891 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1892 vlc_cond_signal( &p_sys->cond );
1893 vlc_mutex_unlock( &p_sys->lock_out );
1899 p_pic->date = i_pts;
1900 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1901 block_ChainAppend( out, p_block );
1905 if( p_sys->i_threads == 0 )
1906 p_pic->pf_release( p_pic );
1912 static int EncoderThread( sout_stream_sys_t *p_sys )
1914 sout_stream_id_t *id = p_sys->id_video;
1917 while( !p_sys->b_die && !p_sys->b_error )
1921 vlc_mutex_lock( &p_sys->lock_out );
1922 while( p_sys->i_last_pic == p_sys->i_first_pic )
1924 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1925 if( p_sys->b_die || p_sys->b_error ) break;
1927 if( p_sys->b_die || p_sys->b_error )
1929 vlc_mutex_unlock( &p_sys->lock_out );
1933 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1934 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1935 vlc_mutex_unlock( &p_sys->lock_out );
1937 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1938 vlc_mutex_lock( &p_sys->lock_out );
1939 block_ChainAppend( &p_sys->p_buffers, p_block );
1941 vlc_mutex_unlock( &p_sys->lock_out );
1943 p_pic->pf_release( p_pic );
1946 while( p_sys->i_last_pic != p_sys->i_first_pic )
1948 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1949 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1951 p_pic->pf_release( p_pic );
1954 block_ChainRelease( p_sys->p_buffers );
1959 struct picture_sys_t
1961 vlc_object_t *p_owner;
1964 static void video_release_buffer( picture_t *p_pic )
1966 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
1968 video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
1970 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
1973 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
1974 sout_stream_sys_t *p_sys )
1976 decoder_t *p_dec = (decoder_t *)p_this;
1980 /* Find an empty space in the picture ring buffer */
1981 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1983 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
1985 pp_ring[i]->i_status = RESERVED_PICTURE;
1989 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1991 if( pp_ring[i] == 0 ) break;
1994 if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
1996 int i_first_pic = p_sys->i_first_pic;
1998 if( p_sys->i_first_pic != p_sys->i_last_pic )
2000 /* Encoder still has stuff to encode, wait to clear-up the list */
2001 while( p_sys->i_first_pic == i_first_pic )
2005 /* Find an empty space in the picture ring buffer */
2006 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2008 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2010 pp_ring[i]->i_status = RESERVED_PICTURE;
2014 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2016 if( pp_ring[i] == 0 ) break;
2020 if( i == PICTURE_RING_SIZE )
2022 msg_Err( p_this, "decoder/filter is leaking pictures, "
2023 "resetting its ring buffer" );
2025 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2027 pp_ring[i]->pf_release( pp_ring[i] );
2033 p_pic = malloc( sizeof(picture_t) );
2034 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2035 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
2036 p_dec->fmt_out.video.i_chroma,
2037 p_dec->fmt_out.video.i_width,
2038 p_dec->fmt_out.video.i_height,
2039 p_dec->fmt_out.video.i_aspect );
2041 if( !p_pic->i_planes )
2047 p_pic->pf_release = video_release_buffer;
2048 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2049 p_pic->p_sys->p_owner = p_this;
2050 p_pic->i_status = RESERVED_PICTURE;
2057 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
2059 return video_new_buffer( VLC_OBJECT(p_dec),
2060 p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2063 static picture_t *video_new_buffer_filter( filter_t *p_filter )
2065 return video_new_buffer( VLC_OBJECT(p_filter),
2066 p_filter->p_owner->pp_pics,
2067 p_filter->p_owner->p_sys );
2070 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2072 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2073 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
2074 if( p_pic ) free( p_pic );
2077 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
2079 p_pic->i_refcount = 0;
2080 p_pic->i_status = DESTROYED_PICTURE;
2083 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
2085 p_pic->i_refcount = 0;
2086 p_pic->i_status = DESTROYED_PICTURE;
2089 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2091 p_pic->i_refcount++;
2094 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2096 video_release_buffer( p_pic );
2102 static subpicture_t *spu_new_buffer( decoder_t * );
2103 static void spu_del_buffer( decoder_t *, subpicture_t * );
2105 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2107 sout_stream_sys_t *p_sys = p_stream->p_sys;
2113 /* Initialization of decoder structures */
2114 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2115 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2116 id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2117 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
2119 id->p_decoder->p_module =
2120 module_Need( id->p_decoder, "decoder", "$codec", 0 );
2122 if( !id->p_decoder->p_module )
2124 msg_Err( p_stream, "cannot find decoder" );
2125 return VLC_EGENERIC;
2128 if( !p_sys->b_soverlay )
2134 /* Initialization of encoder format structures */
2135 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
2136 id->p_decoder->fmt_in.i_codec );
2138 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2140 id->p_encoder->p_module =
2141 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2143 if( !id->p_encoder->p_module )
2145 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2146 msg_Err( p_stream, "cannot find encoder" );
2147 return VLC_EGENERIC;
2153 p_sys->p_spu = spu_Create( p_stream );
2154 spu_Init( p_sys->p_spu );
2160 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2163 if( id->p_decoder->p_module )
2164 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2167 if( id->p_encoder->p_module )
2168 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2171 static int transcode_spu_process( sout_stream_t *p_stream,
2172 sout_stream_id_t *id,
2173 block_t *in, block_t **out )
2175 sout_stream_sys_t *p_sys = p_stream->p_sys;
2176 subpicture_t *p_subpic;
2179 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2180 if( !p_subpic ) return VLC_EGENERIC;
2182 if( p_sys->b_master_sync && p_sys->i_master_drift )
2184 p_subpic->i_start -= p_sys->i_master_drift;
2185 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2188 if( p_sys->b_soverlay )
2190 spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2196 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2197 spu_del_buffer( id->p_decoder, p_subpic );
2201 block_ChainAppend( out, p_block );
2206 return VLC_EGENERIC;
2209 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2211 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2212 return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2215 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2217 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2218 spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );