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 /*****************************************************************************
42 *****************************************************************************/
43 #define VENC_TEXT N_("Video encoder")
44 #define VENC_LONGTEXT N_( \
45 "Allows you to specify the video encoder to use and its associated " \
47 #define VCODEC_TEXT N_("Destination video codec")
48 #define VCODEC_LONGTEXT N_( \
49 "Allows you to specify the destination video codec used for the " \
51 #define VB_TEXT N_("Video bitrate")
52 #define VB_LONGTEXT N_( \
53 "Allows you to specify the video bitrate used for the streaming " \
55 #define SCALE_TEXT N_("Video scaling")
56 #define SCALE_LONGTEXT N_( \
57 "Allows you to scale the video before encoding." )
58 #define FPS_TEXT N_("Video frame-rate")
59 #define FPS_LONGTEXT N_( \
60 "Allows you to specify an output frame rate for the video." )
61 #define DEINTERLACE_TEXT N_("Deinterlace video")
62 #define DEINTERLACE_LONGTEXT N_( \
63 "Allows you to deinterlace the video before encoding." )
64 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
65 #define DEINTERLACE_MODULE_LONGTEXT N_( \
66 "Specifies the deinterlace module to use." )
67 #define WIDTH_TEXT N_("Video width")
68 #define WIDTH_LONGTEXT N_( \
69 "Allows you to specify the output video width." )
70 #define HEIGHT_TEXT N_("Video height")
71 #define HEIGHT_LONGTEXT N_( \
72 "Allows you to specify the output video height." )
73 #define VFILTER_TEXT N_("Video filter")
74 #define VFILTER_LONGTEXT N_( \
75 "Allows you to specify video filters used after the video " \
76 "transcoding and subpictures overlaying." )
78 #define CROPTOP_TEXT N_("Video crop top")
79 #define CROPTOP_LONGTEXT N_( \
80 "Allows you to specify the top coordinate for the video cropping." )
81 #define CROPLEFT_TEXT N_("Video crop left")
82 #define CROPLEFT_LONGTEXT N_( \
83 "Allows you to specify the left coordinate for the video cropping." )
84 #define CROPBOTTOM_TEXT N_("Video crop bottom")
85 #define CROPBOTTOM_LONGTEXT N_( \
86 "Allows you to specify the bottom coordinate for the video cropping." )
87 #define CROPRIGHT_TEXT N_("Video crop right")
88 #define CROPRIGHT_LONGTEXT N_( \
89 "Allows you to specify the right coordinate for the video cropping." )
91 #define AENC_TEXT N_("Audio encoder")
92 #define AENC_LONGTEXT N_( \
93 "Allows you to specify the audio encoder to use and its associated " \
95 #define ACODEC_TEXT N_("Destination audio codec")
96 #define ACODEC_LONGTEXT N_( \
97 "Allows you to specify the destination audio codec used for the " \
99 #define AB_TEXT N_("Audio bitrate")
100 #define AB_LONGTEXT N_( \
101 "Allows you to specify the audio bitrate used for the streaming " \
103 #define ARATE_TEXT N_("Audio sample rate")
104 #define ARATE_LONGTEXT N_( \
105 "Allows you to specify the audio sample rate used for the streaming " \
107 #define ACHANS_TEXT N_("Audio channels")
108 #define ACHANS_LONGTEXT N_( \
109 "Allows you to specify the number of audio channels used for the " \
110 "streaming output." )
112 #define SENC_TEXT N_("Subtitles encoder")
113 #define SENC_LONGTEXT N_( \
114 "Allows you to specify the subtitles encoder to use and its associated " \
116 #define SCODEC_TEXT N_("Destination subtitles codec")
117 #define SCODEC_LONGTEXT N_( \
118 "Allows you to specify the destination subtitles codec used for the " \
119 "streaming output." )
120 #define SFILTER_TEXT N_("Subpictures filter")
121 #define SFILTER_LONGTEXT N_( \
122 "Allows you to specify subpictures filters used during the video " \
123 "transcoding. The subpictures produced by the filters will be overlayed " \
124 "directly onto the video." )
126 #define THREADS_TEXT N_("Number of threads")
127 #define THREADS_LONGTEXT N_( \
128 "Allows you to specify the number of threads used for the transcoding." )
129 #define HP_TEXT N_("High priority")
130 #define HP_LONGTEXT N_( \
131 "Runs the optional encoder thread at the OUTPUT priority instead of " \
134 #define ASYNC_TEXT N_("Synchronise on audio track")
135 #define ASYNC_LONGTEXT N_( \
136 "This option will drop/duplicate video frames to synchronise the video " \
137 "track on the audio track." )
139 #define HURRYUP_TEXT N_( "Hurry up" )
140 #define HURRYUP_LONGTEXT N_( "Allows you to specify if the transcoder " \
141 "should drop frames if your CPU can't keep up with the encoding rate." )
143 static char *ppsz_deinterlace_type[] =
145 "deinterlace", "ffmpeg-deinterlace"
148 static int Open ( vlc_object_t * );
149 static void Close( vlc_object_t * );
151 #define SOUT_CFG_PREFIX "sout-transcode-"
154 set_shortname( _("Transcode"));
155 set_description( _("Transcode stream output") );
156 set_capability( "sout stream", 50 );
157 add_shortcut( "transcode" );
158 set_callbacks( Open, Close );
159 set_category( CAT_SOUT );
160 set_subcategory( SUBCAT_SOUT_STREAM );
161 set_section( N_("Video"), NULL );
162 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
163 VENC_LONGTEXT, VLC_FALSE );
164 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
165 VCODEC_LONGTEXT, VLC_FALSE );
166 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
167 VB_LONGTEXT, VLC_FALSE );
168 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
169 SCALE_LONGTEXT, VLC_FALSE );
170 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
171 FPS_LONGTEXT, VLC_FALSE );
172 add_bool( SOUT_CFG_PREFIX "hurry-up", VLC_TRUE, NULL, HURRYUP_TEXT,
173 HURRYUP_LONGTEXT, VLC_FALSE );
174 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
175 DEINTERLACE_LONGTEXT, VLC_FALSE );
176 add_string( SOUT_CFG_PREFIX "deinterlace-module", "deinterlace", NULL,
177 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
179 change_string_list( ppsz_deinterlace_type, 0, 0 );
180 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
181 WIDTH_LONGTEXT, VLC_TRUE );
182 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
183 HEIGHT_LONGTEXT, VLC_TRUE );
184 add_module_list_cat( SOUT_CFG_PREFIX "vfilter", SUBCAT_VIDEO_VFILTER,
186 VFILTER_TEXT, VFILTER_LONGTEXT, VLC_FALSE );
188 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
189 CROPTOP_LONGTEXT, VLC_TRUE );
190 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
191 CROPLEFT_LONGTEXT, VLC_TRUE );
192 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
193 CROPBOTTOM_LONGTEXT, VLC_TRUE );
194 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
195 CROPRIGHT_LONGTEXT, VLC_TRUE );
197 set_section( N_("Audio"), NULL );
198 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
199 AENC_LONGTEXT, VLC_FALSE );
200 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
201 ACODEC_LONGTEXT, VLC_FALSE );
202 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
203 AB_LONGTEXT, VLC_FALSE );
204 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
205 ACHANS_LONGTEXT, VLC_FALSE );
206 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
207 ARATE_LONGTEXT, VLC_TRUE );
208 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
209 ASYNC_LONGTEXT, VLC_FALSE );
211 set_section( N_("Overlays/Subtitles"), NULL );
212 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
213 SENC_LONGTEXT, VLC_FALSE );
214 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
215 SCODEC_LONGTEXT, VLC_FALSE );
216 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
217 SCODEC_LONGTEXT, VLC_FALSE );
218 add_module_list_cat( SOUT_CFG_PREFIX "sfilter", SUBCAT_VIDEO_SUBPIC,
220 SFILTER_TEXT, SFILTER_LONGTEXT, VLC_FALSE );
222 set_section( N_("Miscellaneous"), NULL );
223 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
224 THREADS_LONGTEXT, VLC_TRUE );
225 add_bool( SOUT_CFG_PREFIX "high-priority", 0, NULL, HP_TEXT, HP_LONGTEXT,
230 static const char *ppsz_sout_options[] = {
231 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
232 "scale", "fps", "width", "height", "vfilter", "deinterlace",
233 "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab",
234 "samplerate", "channels", "senc", "scodec", "soverlay", "sfilter",
235 "audio-sync", "high-priority", NULL
238 /*****************************************************************************
239 * Exported prototypes
240 *****************************************************************************/
241 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
242 static int Del ( sout_stream_t *, sout_stream_id_t * );
243 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
245 static int transcode_audio_new ( sout_stream_t *, sout_stream_id_t * );
246 static void transcode_audio_close ( sout_stream_t *, sout_stream_id_t * );
247 static int transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
248 block_t *, block_t ** );
250 static aout_buffer_t *audio_new_buffer( decoder_t *, int );
251 static void audio_del_buffer( decoder_t *, aout_buffer_t * );
253 static int transcode_video_new ( sout_stream_t *, sout_stream_id_t * );
254 static void transcode_video_close ( sout_stream_t *, sout_stream_id_t * );
255 static int transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
256 static int transcode_video_process( sout_stream_t *, sout_stream_id_t *,
257 block_t *, block_t ** );
259 static void video_del_buffer( vlc_object_t *, picture_t * );
260 static picture_t *video_new_buffer_decoder( decoder_t * );
261 static void video_del_buffer_decoder( decoder_t *, picture_t * );
262 static void video_link_picture_decoder( decoder_t *, picture_t * );
263 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
264 static picture_t *video_new_buffer_filter( filter_t * );
265 static void video_del_buffer_filter( filter_t *, picture_t * );
267 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
268 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
269 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
270 block_t *, block_t ** );
272 static int EncoderThread( struct sout_stream_sys_t * p_sys );
274 static int pi_channels_maps[6] =
277 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
278 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
279 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
280 | AOUT_CHAN_REARRIGHT,
281 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
282 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
285 #define PICTURE_RING_SIZE 64
286 #define SUBPICTURE_RING_SIZE 20
288 struct sout_stream_sys_t
292 sout_stream_t *p_out;
293 sout_stream_id_t *id_video;
295 vlc_mutex_t lock_out;
297 picture_t * pp_pics[PICTURE_RING_SIZE];
298 int i_first_pic, i_last_pic;
301 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
303 sout_cfg_t *p_audio_cfg;
309 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
311 sout_cfg_t *p_video_cfg;
317 vlc_bool_t b_deinterlace;
318 char *psz_deinterlace;
319 sout_cfg_t *p_deinterlace_cfg;
321 vlc_bool_t b_high_priority;
322 vlc_bool_t b_hurry_up;
323 char *psz_vfilters[10];
324 sout_cfg_t *p_vfilters_cfg[10];
333 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
335 vlc_bool_t b_soverlay;
336 sout_cfg_t *p_spu_cfg;
340 vlc_bool_t b_master_sync;
341 mtime_t i_master_drift;
344 struct decoder_owner_sys_t
346 picture_t *pp_pics[PICTURE_RING_SIZE];
347 sout_stream_sys_t *p_sys;
349 struct filter_owner_sys_t
351 picture_t *pp_pics[PICTURE_RING_SIZE];
352 sout_stream_sys_t *p_sys;
355 /*****************************************************************************
357 *****************************************************************************/
358 static int Open( vlc_object_t *p_this )
360 sout_stream_t *p_stream = (sout_stream_t*)p_this;
361 sout_stream_sys_t *p_sys;
364 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
366 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
369 msg_Err( p_stream, "cannot create chain" );
370 vlc_object_destroy( p_sys );
374 p_sys->i_master_drift = 0;
376 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
379 /* Audio transcoding parameters */
380 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
381 p_sys->psz_aenc = NULL;
382 p_sys->p_audio_cfg = NULL;
383 if( val.psz_string && *val.psz_string )
386 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
388 if( psz_next ) free( psz_next );
390 if( val.psz_string ) free( val.psz_string );
392 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
394 if( val.psz_string && *val.psz_string )
397 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
398 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
400 if( val.psz_string ) free( val.psz_string );
402 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
403 p_sys->i_abitrate = val.i_int;
404 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
406 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
407 p_sys->i_sample_rate = val.i_int;
409 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
410 p_sys->i_channels = val.i_int;
412 if( p_sys->i_acodec )
414 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
415 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
416 p_sys->i_channels, p_sys->i_abitrate / 1000 );
419 /* Video transcoding parameters */
420 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
421 p_sys->psz_venc = NULL;
422 p_sys->p_video_cfg = NULL;
423 if( val.psz_string && *val.psz_string )
426 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_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 "vcodec", &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_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
440 if( val.psz_string ) free( val.psz_string );
442 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
443 p_sys->i_vbitrate = val.i_int;
444 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
446 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
447 p_sys->f_scale = val.f_float;
449 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
450 p_sys->f_fps = val.f_float;
452 var_Get( p_stream, SOUT_CFG_PREFIX "hurry-up", &val );
453 p_sys->b_hurry_up = val.b_bool;
455 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
456 p_sys->i_width = val.i_int;
458 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
459 p_sys->i_height = val.i_int;
461 var_Get( p_stream, SOUT_CFG_PREFIX "vfilter", &val );
462 p_sys->i_vfilters = 0;
463 if( val.psz_string && *val.psz_string )
465 char *psz_parser = val.psz_string;
467 while( psz_parser != NULL && *psz_parser != '\0' )
469 psz_parser = sout_CfgCreate(
470 &p_sys->psz_vfilters[p_sys->i_vfilters],
471 &p_sys->p_vfilters_cfg[p_sys->i_vfilters],
474 if( psz_parser != NULL && *psz_parser != '\0' ) psz_parser++;
477 if( val.psz_string ) free( val.psz_string );
478 p_sys->psz_vfilters[p_sys->i_vfilters] = NULL;
479 p_sys->p_vfilters_cfg[p_sys->i_vfilters] = NULL;
481 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
482 p_sys->b_deinterlace = val.b_bool;
484 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace-module", &val );
485 p_sys->psz_deinterlace = NULL;
486 p_sys->p_deinterlace_cfg = NULL;
487 if( val.psz_string && *val.psz_string )
490 psz_next = sout_CfgCreate( &p_sys->psz_deinterlace,
491 &p_sys->p_deinterlace_cfg,
493 if( psz_next ) free( psz_next );
495 if( val.psz_string ) free( val.psz_string );
497 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
498 p_sys->i_crop_top = val.i_int;
499 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
500 p_sys->i_crop_bottom = val.i_int;
501 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
502 p_sys->i_crop_left = val.i_int;
503 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
504 p_sys->i_crop_right = val.i_int;
506 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
507 p_sys->i_threads = val.i_int;
508 var_Get( p_stream, SOUT_CFG_PREFIX "high-priority", &val );
509 p_sys->b_high_priority = val.b_bool;
511 if( p_sys->i_vcodec )
513 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
514 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
515 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
518 /* Subpictures transcoding parameters */
520 p_sys->psz_senc = NULL;
521 p_sys->p_spu_cfg = NULL;
524 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
525 if( val.psz_string && *val.psz_string )
528 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
530 if( psz_next ) free( psz_next );
532 if( val.psz_string ) free( val.psz_string );
534 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
535 if( val.psz_string && *val.psz_string )
538 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
539 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
541 if( val.psz_string ) free( val.psz_string );
543 if( p_sys->i_scodec )
545 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
548 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
549 p_sys->b_soverlay = val.b_bool;
551 var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
552 if( val.psz_string && *val.psz_string )
554 p_sys->p_spu = spu_Create( p_stream );
555 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
556 var_Set( p_sys->p_spu, "sub-filter", val );
557 spu_Init( p_sys->p_spu );
559 if( val.psz_string ) free( val.psz_string );
561 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
562 p_sys->b_master_sync = val.b_bool;
563 if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
565 p_stream->pf_add = Add;
566 p_stream->pf_del = Del;
567 p_stream->pf_send = Send;
568 p_stream->p_sys = p_sys;
573 /*****************************************************************************
575 *****************************************************************************/
576 static void Close( vlc_object_t * p_this )
578 sout_stream_t *p_stream = (sout_stream_t*)p_this;
579 sout_stream_sys_t *p_sys = p_stream->p_sys;
581 sout_StreamDelete( p_sys->p_out );
583 while( p_sys->p_audio_cfg != NULL )
585 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
587 if( p_sys->p_audio_cfg->psz_name )
588 free( p_sys->p_audio_cfg->psz_name );
589 if( p_sys->p_audio_cfg->psz_value )
590 free( p_sys->p_audio_cfg->psz_value );
591 free( p_sys->p_audio_cfg );
593 p_sys->p_audio_cfg = p_next;
595 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
597 while( p_sys->p_video_cfg != NULL )
599 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
601 if( p_sys->p_video_cfg->psz_name )
602 free( p_sys->p_video_cfg->psz_name );
603 if( p_sys->p_video_cfg->psz_value )
604 free( p_sys->p_video_cfg->psz_value );
605 free( p_sys->p_video_cfg );
607 p_sys->p_video_cfg = p_next;
609 if( p_sys->psz_venc ) free( p_sys->psz_venc );
611 while( p_sys->p_deinterlace_cfg != NULL )
613 sout_cfg_t *p_next = p_sys->p_deinterlace_cfg->p_next;
615 if( p_sys->p_deinterlace_cfg->psz_name )
616 free( p_sys->p_deinterlace_cfg->psz_name );
617 if( p_sys->p_deinterlace_cfg->psz_value )
618 free( p_sys->p_deinterlace_cfg->psz_value );
619 free( p_sys->p_deinterlace_cfg );
621 p_sys->p_deinterlace_cfg = p_next;
623 if( p_sys->psz_deinterlace ) free( p_sys->psz_deinterlace );
625 while( p_sys->p_spu_cfg != NULL )
627 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
629 if( p_sys->p_spu_cfg->psz_name )
630 free( p_sys->p_spu_cfg->psz_name );
631 if( p_sys->p_spu_cfg->psz_value )
632 free( p_sys->p_spu_cfg->psz_value );
633 free( p_sys->p_spu_cfg );
635 p_sys->p_spu_cfg = p_next;
637 if( p_sys->psz_senc ) free( p_sys->psz_senc );
639 if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
641 vlc_object_destroy( p_sys );
644 struct sout_stream_id_t
646 vlc_fourcc_t b_transcode;
648 /* id of the out stream */
652 decoder_t *p_decoder;
655 filter_t *pp_filter[10];
657 filter_t *pp_vfilter[10];
661 encoder_t *p_encoder;
664 date_t interpolated_pts;
668 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
670 sout_stream_sys_t *p_sys = p_stream->p_sys;
671 sout_stream_id_t *id;
673 id = malloc( sizeof( sout_stream_id_t ) );
674 memset( id, 0, sizeof(sout_stream_id_t) );
677 id->p_decoder = NULL;
678 id->p_encoder = NULL;
680 /* Create decoder object */
681 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
684 msg_Err( p_stream, "out of memory" );
687 vlc_object_attach( id->p_decoder, p_stream );
688 id->p_decoder->p_module = NULL;
689 id->p_decoder->fmt_in = *p_fmt;
690 id->p_decoder->b_pace_control = VLC_TRUE;
692 /* Create encoder object */
693 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
696 msg_Err( p_stream, "out of memory" );
699 vlc_object_attach( id->p_encoder, p_stream );
700 id->p_encoder->p_module = NULL;
702 /* Create destination format */
703 es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
704 id->p_encoder->fmt_out.i_id = p_fmt->i_id;
705 id->p_encoder->fmt_out.i_group = p_fmt->i_group;
706 if( p_fmt->psz_language )
707 id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
709 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
712 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
713 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
715 /* Complete destination format */
716 id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
717 id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
718 p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
719 id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
720 id->p_encoder->fmt_out.audio.i_bitspersample =
721 p_fmt->audio.i_bitspersample;
722 id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
723 p_sys->i_channels : p_fmt->audio.i_channels;
724 /* Sanity check for audio channels */
725 id->p_encoder->fmt_out.audio.i_channels =
726 __MIN( id->p_encoder->fmt_out.audio.i_channels,
727 id->p_decoder->fmt_in.audio.i_channels );
728 id->p_encoder->fmt_out.audio.i_original_channels =
729 id->p_decoder->fmt_in.audio.i_physical_channels;
730 if( id->p_decoder->fmt_in.audio.i_channels ==
731 id->p_encoder->fmt_out.audio.i_channels )
733 id->p_encoder->fmt_out.audio.i_physical_channels =
734 id->p_decoder->fmt_in.audio.i_physical_channels;
738 id->p_encoder->fmt_out.audio.i_physical_channels =
739 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
742 /* Build decoder -> filter -> encoder chain */
743 if( transcode_audio_new( p_stream, id ) )
745 msg_Err( p_stream, "cannot create audio chain" );
749 /* Open output stream */
750 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
751 id->b_transcode = VLC_TRUE;
753 if( !id->id ) goto error;
755 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
757 else if( p_fmt->i_cat == VIDEO_ES &&
758 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
761 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
762 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
764 /* Complete destination format */
765 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
766 id->p_encoder->fmt_out.video.i_width = p_sys->i_width;
767 id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
768 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
770 /* Build decoder -> filter -> encoder chain */
771 if( transcode_video_new( p_stream, id ) )
773 msg_Err( p_stream, "cannot create video chain" );
777 /* Stream will be added later on because we don't know
778 * all the characteristics of the decoded stream yet */
779 id->b_transcode = VLC_TRUE;
781 if( p_sys->f_fps > 0 )
783 id->p_encoder->fmt_out.video.i_frame_rate =
784 (p_sys->f_fps * 1001) + 0.5;
785 id->p_encoder->fmt_out.video.i_frame_rate_base = 1001;
788 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
790 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
791 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
792 (char*)&p_sys->i_scodec );
794 /* Complete destination format */
795 id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
797 /* build decoder -> filter -> encoder */
798 if( transcode_spu_new( p_stream, id ) )
800 msg_Err( p_stream, "cannot create subtitles chain" );
804 /* open output stream */
805 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
806 id->b_transcode = VLC_TRUE;
808 if( !id->id ) goto error;
810 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
812 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
813 (char*)&p_fmt->i_codec );
815 id->b_transcode = VLC_TRUE;
817 /* Build decoder -> filter -> overlaying chain */
818 if( transcode_spu_new( p_stream, id ) )
820 msg_Err( p_stream, "cannot create subtitles chain" );
826 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
827 (char*)&p_fmt->i_codec );
828 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
829 id->b_transcode = VLC_FALSE;
831 if( !id->id ) goto error;
839 vlc_object_detach( id->p_decoder );
840 vlc_object_destroy( id->p_decoder );
845 vlc_object_detach( id->p_encoder );
846 vlc_object_destroy( id->p_encoder );
853 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
855 sout_stream_sys_t *p_sys = p_stream->p_sys;
857 if( id->b_transcode )
859 switch( id->p_decoder->fmt_in.i_cat )
862 transcode_audio_close( p_stream, id );
865 transcode_video_close( p_stream, id );
868 transcode_spu_close( p_stream, id );
873 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
877 vlc_object_detach( id->p_decoder );
878 vlc_object_destroy( id->p_decoder );
883 vlc_object_detach( id->p_encoder );
884 vlc_object_destroy( id->p_encoder );
892 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
895 sout_stream_sys_t *p_sys = p_stream->p_sys;
898 if( !id->b_transcode && id->id )
900 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
902 else if( !id->b_transcode )
904 block_Release( p_buffer );
908 switch( id->p_decoder->fmt_in.i_cat )
911 transcode_audio_process( p_stream, id, p_buffer, &p_out );
915 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
923 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
932 block_Release( p_buffer );
936 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
940 /****************************************************************************
941 * decoder reencoder part
942 ****************************************************************************/
943 int audio_BitsPerSample( vlc_fourcc_t i_format )
947 case VLC_FOURCC('u','8',' ',' '):
948 case VLC_FOURCC('s','8',' ',' '):
951 case VLC_FOURCC('u','1','6','l'):
952 case VLC_FOURCC('s','1','6','l'):
953 case VLC_FOURCC('u','1','6','b'):
954 case VLC_FOURCC('s','1','6','b'):
957 case VLC_FOURCC('u','2','4','l'):
958 case VLC_FOURCC('s','2','4','l'):
959 case VLC_FOURCC('u','2','4','b'):
960 case VLC_FOURCC('s','2','4','b'):
963 case VLC_FOURCC('u','3','2','l'):
964 case VLC_FOURCC('s','3','2','l'):
965 case VLC_FOURCC('u','3','2','b'):
966 case VLC_FOURCC('s','3','2','b'):
967 case VLC_FOURCC('f','l','3','2'):
968 case VLC_FOURCC('f','i','3','2'):
971 case VLC_FOURCC('f','l','6','4'):
978 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
979 sout_stream_id_t *id,
980 es_format_t *p_fmt_in,
981 es_format_t *p_fmt_out )
983 filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
985 vlc_object_attach( p_filter, p_stream );
986 p_filter->pf_audio_buffer_new = (block_t* (*) (filter_t*, int))__block_New;
988 p_filter->fmt_in = *p_fmt_in;
989 p_filter->fmt_out = *p_fmt_out;
991 p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
992 if( p_filter->p_module )
994 p_filter->fmt_out.audio.i_bitspersample =
995 audio_BitsPerSample( p_filter->fmt_out.i_codec );
996 *p_fmt_in = p_filter->fmt_out;
1000 vlc_object_detach( p_filter );
1001 vlc_object_destroy( p_filter );
1008 static int transcode_audio_new( sout_stream_t *p_stream,
1009 sout_stream_id_t *id )
1011 sout_stream_sys_t *p_sys = p_stream->p_sys;
1012 es_format_t fmt_last;
1019 /* Initialization of decoder structures */
1020 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1021 id->p_decoder->fmt_out.i_extra = 0;
1022 id->p_decoder->fmt_out.p_extra = 0;
1023 id->p_decoder->pf_decode_audio = 0;
1024 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
1025 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1026 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1028 id->p_decoder->p_module =
1029 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1031 if( !id->p_decoder->p_module )
1033 msg_Err( p_stream, "cannot find decoder" );
1034 return VLC_EGENERIC;
1036 id->p_decoder->fmt_out.audio.i_bitspersample =
1037 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1038 fmt_last = id->p_decoder->fmt_out;
1039 /* FIX decoders so we don't have to do this */
1040 fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1046 /* Initialization of encoder format structures */
1047 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1048 id->p_decoder->fmt_out.i_codec );
1049 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
1051 /* Initialization of encoder format structures */
1052 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1053 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1054 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
1055 id->p_encoder->fmt_in.audio.i_physical_channels =
1056 id->p_encoder->fmt_out.audio.i_physical_channels;
1057 id->p_encoder->fmt_in.audio.i_original_channels =
1058 id->p_encoder->fmt_out.audio.i_original_channels;
1059 id->p_encoder->fmt_in.audio.i_channels =
1060 id->p_encoder->fmt_out.audio.i_channels;
1061 id->p_encoder->fmt_in.audio.i_bitspersample =
1062 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1064 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1066 id->p_encoder->p_module =
1067 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
1068 if( !id->p_encoder->p_module )
1070 msg_Err( p_stream, "cannot find encoder" );
1071 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1072 id->p_decoder->p_module = 0;
1073 return VLC_EGENERIC;
1075 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1076 id->p_encoder->fmt_in.audio.i_bitspersample =
1077 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1079 /* Load conversion filters */
1080 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1081 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1083 /* We'll have to go through fl32 first */
1084 es_format_t fmt_out = id->p_encoder->fmt_in;
1085 fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1087 id->pp_filter[id->i_filter] =
1088 transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );
1090 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1095 if( fmt_last.audio.i_channels !=
1096 id->p_encoder->fmt_in.audio.i_channels ||
1097 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
1098 fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1100 id->pp_filter[id->i_filter] =
1101 transcode_audio_filter_new( p_stream, id, &fmt_last,
1102 &id->p_encoder->fmt_in );
1104 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1109 /* Final checks to see if conversions were successful */
1110 if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1112 msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1113 (char *)&fmt_last.i_codec,
1114 (char *)&id->p_encoder->fmt_in.i_codec );
1115 transcode_audio_close( p_stream, id );
1116 return VLC_EGENERIC;
1119 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1121 msg_Err( p_stream, "no audio filter found for mixing from"
1122 " %i to %i channels", fmt_last.audio.i_channels,
1123 id->p_encoder->fmt_in.audio.i_channels );
1125 /* FIXME : this might work, but only if the encoder is restarted */
1126 id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1127 id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1129 id->p_encoder->fmt_in.audio.i_physical_channels =
1130 id->p_encoder->fmt_in.audio.i_original_channels =
1131 fmt_last.audio.i_physical_channels;
1132 id->p_encoder->fmt_out.audio.i_physical_channels =
1133 id->p_encoder->fmt_out.audio.i_original_channels =
1134 fmt_last.audio.i_physical_channels;
1136 transcode_audio_close( p_stream, id );
1137 return VLC_EGENERIC;
1141 if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1143 msg_Err( p_stream, "no audio filter found for resampling from"
1144 " %iHz to %iHz", fmt_last.audio.i_rate,
1145 id->p_encoder->fmt_in.audio.i_rate );
1147 /* FIXME : this might work, but only if the encoder is restarted */
1148 id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1149 id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1151 transcode_audio_close( p_stream, id );
1152 return VLC_EGENERIC;
1156 /* FIXME: Hack for mp3 transcoding support */
1157 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1158 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1163 static void transcode_audio_close( sout_stream_t *p_stream,
1164 sout_stream_id_t *id )
1169 if( id->p_decoder->p_module )
1170 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1171 id->p_decoder->p_module = 0;
1174 if( id->p_encoder->p_module )
1175 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1176 id->p_encoder->p_module = 0;
1179 for( i = 0; i < id->i_filter; i++ )
1181 vlc_object_detach( id->pp_filter[i] );
1182 if( id->pp_filter[i]->p_module )
1183 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1184 vlc_object_destroy( id->pp_filter[i] );
1188 static int transcode_audio_process( sout_stream_t *p_stream,
1189 sout_stream_id_t *id,
1190 block_t *in, block_t **out )
1192 sout_stream_sys_t *p_sys = p_stream->p_sys;
1193 aout_buffer_t *p_audio_buf;
1194 block_t *p_block, *p_audio_block;
1198 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1201 if( p_sys->b_master_sync )
1203 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1206 date_Set( &id->interpolated_pts, p_audio_buf->start_date );
1207 i_dts = p_audio_buf->start_date + 1;
1209 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1210 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1211 p_audio_buf->start_date -= p_sys->i_master_drift;
1212 p_audio_buf->end_date -= p_sys->i_master_drift;
1215 p_audio_block = p_audio_buf->p_sys;
1216 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1217 p_audio_block->i_dts = p_audio_block->i_pts =
1218 p_audio_buf->start_date;
1219 p_audio_block->i_length = p_audio_buf->end_date -
1220 p_audio_buf->start_date;
1221 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1223 /* Run filter chain */
1224 for( i = 0; i < id->i_filter; i++ )
1227 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1231 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1232 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1233 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1234 p_audio_buf->start_date = p_audio_block->i_dts;
1235 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1237 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1238 block_ChainAppend( out, p_block );
1239 block_Release( p_audio_block );
1240 free( p_audio_buf );
1246 static void audio_release_buffer( aout_buffer_t *p_buffer )
1248 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1249 if( p_buffer ) free( p_buffer );
1252 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1254 aout_buffer_t *p_buffer;
1258 if( p_dec->fmt_out.audio.i_bitspersample )
1260 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1261 p_dec->fmt_out.audio.i_channels;
1263 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1264 p_dec->fmt_out.audio.i_frame_length )
1266 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1267 p_dec->fmt_out.audio.i_frame_length;
1271 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1274 p_buffer = malloc( sizeof(aout_buffer_t) );
1275 p_buffer->pf_release = audio_release_buffer;
1276 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1278 p_buffer->p_buffer = p_block->p_buffer;
1279 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1280 p_buffer->i_nb_samples = i_samples;
1281 p_block->i_samples = i_samples;
1286 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1288 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1289 if( p_buffer ) free( p_buffer );
1295 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1297 sout_stream_sys_t *p_sys = p_stream->p_sys;
1304 /* Initialization of decoder structures */
1305 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1306 id->p_decoder->fmt_out.i_extra = 0;
1307 id->p_decoder->fmt_out.p_extra = 0;
1308 id->p_decoder->pf_decode_video = 0;
1309 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1310 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1311 id->p_decoder->pf_picture_link = video_link_picture_decoder;
1312 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
1313 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1314 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1315 id->p_decoder->p_owner->pp_pics[i] = 0;
1316 id->p_decoder->p_owner->p_sys = p_sys;
1317 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1319 id->p_decoder->p_module =
1320 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1322 if( !id->p_decoder->p_module )
1324 msg_Err( p_stream, "cannot find decoder" );
1325 return VLC_EGENERIC;
1330 * Because some info about the decoded input will only be available
1331 * once the first frame is decoded, we actually only test the availability
1332 * of the encoder here.
1335 /* Initialization of encoder format structures */
1336 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1337 id->p_decoder->fmt_out.i_codec );
1338 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1340 /* The dimensions will be set properly later on.
1341 * Just put sensible values so we can test an encoder is available. */
1342 id->p_encoder->fmt_in.video.i_width =
1343 id->p_encoder->fmt_out.video.i_width ?
1344 id->p_encoder->fmt_out.video.i_width :
1345 id->p_decoder->fmt_in.video.i_width ?
1346 id->p_decoder->fmt_in.video.i_width : 16;
1347 id->p_encoder->fmt_in.video.i_height =
1348 id->p_encoder->fmt_out.video.i_height ?
1349 id->p_encoder->fmt_out.video.i_height :
1350 id->p_decoder->fmt_in.video.i_height ?
1351 id->p_decoder->fmt_in.video.i_height : 16;
1352 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1353 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1355 id->p_encoder->i_threads = p_sys->i_threads;
1356 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1358 id->p_encoder->p_module =
1359 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1360 if( !id->p_encoder->p_module )
1362 msg_Err( p_stream, "cannot find encoder" );
1363 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1364 id->p_decoder->p_module = 0;
1365 return VLC_EGENERIC;
1368 /* Close the encoder.
1369 * We'll open it only when we have the first frame. */
1370 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1371 id->p_encoder->p_module = NULL;
1373 if( p_sys->i_threads >= 1 )
1375 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
1376 VLC_THREAD_PRIORITY_VIDEO;
1377 p_sys->id_video = id;
1378 vlc_mutex_init( p_stream, &p_sys->lock_out );
1379 vlc_cond_init( p_stream, &p_sys->cond );
1380 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1381 p_sys->i_first_pic = 0;
1382 p_sys->i_last_pic = 0;
1383 p_sys->p_buffers = NULL;
1384 p_sys->b_die = p_sys->b_error = 0;
1385 if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
1388 msg_Err( p_stream, "cannot spawn encoder thread" );
1389 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1390 id->p_decoder->p_module = 0;
1391 return VLC_EGENERIC;
1398 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1399 sout_stream_id_t *id )
1401 sout_stream_sys_t *p_sys = p_stream->p_sys;
1403 /* Hack because of the copy packetizer which can fail to detect the
1404 * proper size (which forces us to wait until the 1st frame
1406 int i_width = id->p_decoder->fmt_out.video.i_width -
1407 p_sys->i_crop_left - p_sys->i_crop_right;
1408 int i_height = id->p_decoder->fmt_out.video.i_height -
1409 p_sys->i_crop_top - p_sys->i_crop_bottom;
1411 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1412 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1414 /* Apply the scaling */
1415 id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1416 id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1418 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1419 id->p_encoder->fmt_out.video.i_height <= 0 )
1421 id->p_encoder->fmt_out.video.i_height =
1422 id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1424 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1425 id->p_encoder->fmt_out.video.i_height > 0 )
1427 id->p_encoder->fmt_out.video.i_width =
1428 id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1431 /* Make sure the size is at least a multiple of 2 */
1432 id->p_encoder->fmt_out.video.i_width =
1433 (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
1434 id->p_encoder->fmt_out.video.i_height =
1435 (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
1437 id->p_encoder->fmt_in.video.i_width =
1438 id->p_encoder->fmt_out.video.i_width;
1439 id->p_encoder->fmt_in.video.i_height =
1440 id->p_encoder->fmt_out.video.i_height;
1442 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1443 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1445 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1446 id->p_decoder->fmt_out.video.i_frame_rate_base )
1448 id->p_encoder->fmt_out.video.i_frame_rate =
1449 id->p_decoder->fmt_out.video.i_frame_rate;
1450 id->p_encoder->fmt_out.video.i_frame_rate_base =
1451 id->p_decoder->fmt_out.video.i_frame_rate_base;
1455 /* Pick a sensible default value */
1456 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1457 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1461 id->p_encoder->fmt_in.video.i_frame_rate =
1462 id->p_encoder->fmt_out.video.i_frame_rate;
1463 id->p_encoder->fmt_in.video.i_frame_rate_base =
1464 id->p_encoder->fmt_out.video.i_frame_rate_base;
1466 date_Init( &id->interpolated_pts,
1467 id->p_encoder->fmt_out.video.i_frame_rate,
1468 id->p_encoder->fmt_out.video.i_frame_rate_base );
1470 /* Check whether a particular aspect ratio was requested */
1471 if( !id->p_encoder->fmt_out.video.i_aspect )
1473 id->p_encoder->fmt_out.video.i_aspect =
1474 id->p_decoder->fmt_out.video.i_aspect;
1476 id->p_encoder->fmt_in.video.i_aspect =
1477 id->p_encoder->fmt_out.video.i_aspect;
1479 id->p_encoder->p_module =
1480 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1481 if( !id->p_encoder->p_module )
1483 msg_Err( p_stream, "cannot find encoder" );
1484 return VLC_EGENERIC;
1487 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1489 /* Hack for mp2v/mp1v transcoding support */
1490 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1491 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1493 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1496 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1497 &id->p_encoder->fmt_out );
1500 msg_Err( p_stream, "cannot add this stream" );
1501 return VLC_EGENERIC;
1507 static void transcode_video_close( sout_stream_t *p_stream,
1508 sout_stream_id_t *id )
1512 if( p_stream->p_sys->i_threads >= 1 )
1514 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1515 p_stream->p_sys->b_die = 1;
1516 vlc_cond_signal( &p_stream->p_sys->cond );
1517 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1518 vlc_thread_join( p_stream->p_sys );
1519 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1520 vlc_cond_destroy( &p_stream->p_sys->cond );
1524 if( id->p_decoder->p_module )
1525 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1527 if( id->p_decoder->p_owner )
1529 /* Clean-up pictures ring buffer */
1530 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1532 if( id->p_decoder->p_owner->pp_pics[i] )
1533 video_del_buffer( VLC_OBJECT(id->p_decoder),
1534 id->p_decoder->p_owner->pp_pics[i] );
1536 free( id->p_decoder->p_owner );
1540 if( id->p_encoder->p_module )
1541 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1544 for( i = 0; i < id->i_filter; i++ )
1546 vlc_object_detach( id->pp_filter[i] );
1547 if( id->pp_filter[i]->p_module )
1548 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1550 /* Clean-up pictures ring buffer */
1551 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1553 if( id->pp_filter[i]->p_owner->pp_pics[j] )
1554 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1555 id->pp_filter[i]->p_owner->pp_pics[j] );
1557 free( id->pp_filter[i]->p_owner );
1559 vlc_object_destroy( id->pp_filter[i] );
1561 for( i = 0; i < id->i_vfilter; i++ )
1563 vlc_object_detach( id->pp_vfilter[i] );
1564 if( id->pp_vfilter[i]->p_module )
1565 module_Unneed( id->pp_vfilter[i], id->pp_vfilter[i]->p_module );
1567 /* Clean-up pictures ring buffer */
1568 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1570 if( id->pp_vfilter[i]->p_owner->pp_pics[j] )
1571 video_del_buffer( VLC_OBJECT(id->pp_vfilter[i]),
1572 id->pp_vfilter[i]->p_owner->pp_pics[j] );
1574 free( id->pp_vfilter[i]->p_owner );
1576 vlc_object_destroy( id->pp_vfilter[i] );
1580 static int transcode_video_process( sout_stream_t *p_stream,
1581 sout_stream_id_t *id,
1582 block_t *in, block_t **out )
1584 sout_stream_sys_t *p_sys = p_stream->p_sys;
1585 int i_duplicate = 1, i;
1589 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1591 subpicture_t *p_subpic = 0;
1593 if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
1595 mtime_t current_date = mdate();
1596 if( current_date + 50000 > p_pic->date )
1598 msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
1599 current_date + 50000 - p_pic->date );
1600 p_pic->pf_release( p_pic );
1605 if( p_sys->b_master_sync )
1607 mtime_t i_video_drift;
1608 mtime_t i_master_drift = p_sys->i_master_drift;
1611 i_pts = date_Get( &id->interpolated_pts ) + 1;
1614 date_Set( &id->interpolated_pts, p_pic->date );
1615 i_pts = p_pic->date + 1;
1617 i_video_drift = p_pic->date - i_pts;
1620 /* Set the pts of the frame being encoded */
1621 p_pic->date = i_pts;
1623 if( i_video_drift < i_master_drift - 50000 )
1626 msg_Dbg( p_stream, "dropping frame (%i)",
1627 (int)(i_video_drift - i_master_drift) );
1629 p_pic->pf_release( p_pic );
1632 else if( i_video_drift > i_master_drift + 50000 )
1635 msg_Dbg( p_stream, "adding frame (%i)",
1636 (int)(i_video_drift - i_master_drift) );
1642 if( !id->p_encoder->p_module )
1644 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1646 p_pic->pf_release( p_pic );
1647 transcode_video_close( p_stream, id );
1648 id->b_transcode = VLC_FALSE;
1649 return VLC_EGENERIC;
1653 if( p_stream->p_sys->b_deinterlace )
1655 id->pp_filter[id->i_filter] =
1656 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1657 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1659 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1660 video_new_buffer_filter;
1661 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1662 video_del_buffer_filter;
1664 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1665 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
1666 id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
1667 id->pp_filter[id->i_filter]->p_module =
1668 module_Need( id->pp_filter[id->i_filter],
1669 "video filter2", p_sys->psz_deinterlace, 0 );
1670 if( id->pp_filter[id->i_filter]->p_module )
1672 id->pp_filter[id->i_filter]->p_owner =
1673 malloc( sizeof(filter_owner_sys_t) );
1674 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1675 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1676 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1682 msg_Dbg( p_stream, "no video filter found" );
1683 vlc_object_detach( id->pp_filter[id->i_filter] );
1684 vlc_object_destroy( id->pp_filter[id->i_filter] );
1688 /* Check if we need a filter for chroma conversion or resizing */
1689 if( id->p_decoder->fmt_out.video.i_chroma !=
1690 id->p_encoder->fmt_in.video.i_chroma ||
1691 id->p_decoder->fmt_out.video.i_width !=
1692 id->p_encoder->fmt_out.video.i_width ||
1693 id->p_decoder->fmt_out.video.i_height !=
1694 id->p_encoder->fmt_out.video.i_height ||
1695 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1696 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1698 id->pp_filter[id->i_filter] =
1699 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1700 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1702 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1703 video_new_buffer_filter;
1704 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1705 video_del_buffer_filter;
1707 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1708 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1709 id->pp_filter[id->i_filter]->p_cfg = NULL;
1710 id->pp_filter[id->i_filter]->p_module =
1711 module_Need( id->pp_filter[id->i_filter],
1712 "video filter2", 0, 0 );
1713 if( id->pp_filter[id->i_filter]->p_module )
1715 id->pp_filter[id->i_filter]->p_owner =
1716 malloc( sizeof(filter_owner_sys_t) );
1717 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1718 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1719 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1725 msg_Dbg( p_stream, "no video filter found" );
1726 vlc_object_detach( id->pp_filter[id->i_filter] );
1727 vlc_object_destroy( id->pp_filter[id->i_filter] );
1729 p_pic->pf_release( p_pic );
1730 transcode_video_close( p_stream, id );
1731 id->b_transcode = VLC_FALSE;
1732 return VLC_EGENERIC;
1736 for( i = 0; i < p_sys->i_vfilters; i++ )
1738 id->pp_vfilter[id->i_vfilter] =
1739 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1740 vlc_object_attach( id->pp_vfilter[id->i_vfilter], p_stream );
1742 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_new =
1743 video_new_buffer_filter;
1744 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_del =
1745 video_del_buffer_filter;
1747 id->pp_vfilter[id->i_vfilter]->fmt_in = id->p_encoder->fmt_in;
1748 id->pp_vfilter[id->i_vfilter]->fmt_out = id->p_encoder->fmt_in;
1749 id->pp_vfilter[id->i_vfilter]->p_cfg = p_sys->p_vfilters_cfg[i];
1750 id->pp_vfilter[id->i_vfilter]->p_module =
1751 module_Need( id->pp_vfilter[id->i_vfilter],
1752 "video filter2", p_sys->psz_vfilters[i], 0 );
1753 if( id->pp_vfilter[id->i_vfilter]->p_module )
1755 id->pp_vfilter[id->i_vfilter]->p_owner =
1756 malloc( sizeof(filter_owner_sys_t) );
1757 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1758 id->pp_vfilter[id->i_vfilter]->p_owner->pp_pics[i] = 0;
1759 id->pp_vfilter[id->i_vfilter]->p_owner->p_sys = p_sys;
1765 msg_Dbg( p_stream, "no video filter found" );
1766 vlc_object_detach( id->pp_vfilter[id->i_vfilter] );
1767 vlc_object_destroy( id->pp_vfilter[id->i_vfilter] );
1772 /* Run filter chain */
1773 for( i = 0; i < id->i_filter; i++ )
1775 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1782 /* Check if we have a subpicture to overlay */
1785 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
1786 /* TODO: get another pic */
1789 /* Overlay subpicture */
1792 int i_scale_width, i_scale_height;
1793 video_format_t *p_fmt;
1795 i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
1796 id->p_decoder->fmt_out.video.i_width;
1797 i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
1798 id->p_decoder->fmt_out.video.i_height;
1800 if( p_pic->i_refcount && !id->i_filter )
1802 /* We can't modify the picture, we need to duplicate it */
1803 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1806 vout_CopyPicture( p_stream, p_tmp, p_pic );
1807 p_pic->pf_release( p_pic );
1813 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
1815 p_fmt = &id->p_decoder->fmt_out.video;
1817 /* FIXME (shouldn't have to be done here) */
1818 p_fmt->i_sar_num = p_fmt->i_aspect *
1819 p_fmt->i_height / p_fmt->i_width;
1820 p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
1822 spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
1823 i_scale_width, i_scale_height );
1826 /* Run vfilter chain */
1827 for( i = 0; i < id->i_vfilter; i++ )
1829 p_pic = id->pp_vfilter[i]->pf_video_filter(id->pp_vfilter[i], p_pic);
1832 if( p_sys->i_threads >= 1 )
1834 vlc_mutex_lock( &p_sys->lock_out );
1835 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1836 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1837 *out = p_sys->p_buffers;
1838 p_sys->p_buffers = NULL;
1839 vlc_cond_signal( &p_sys->cond );
1840 vlc_mutex_unlock( &p_sys->lock_out );
1845 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1846 block_ChainAppend( out, p_block );
1849 if( p_sys->b_master_sync )
1851 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1854 date_Set( &id->interpolated_pts, p_pic->date );
1855 i_pts = p_pic->date + 1;
1857 date_Increment( &id->interpolated_pts, 1 );
1860 if( p_sys->b_master_sync && i_duplicate > 1 )
1862 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1865 date_Set( &id->interpolated_pts, p_pic->date );
1866 i_pts = p_pic->date + 1;
1868 date_Increment( &id->interpolated_pts, 1 );
1870 if( p_sys->i_threads >= 1 )
1872 /* We can't modify the picture, we need to duplicate it */
1873 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1876 vout_CopyPicture( p_stream, p_tmp, p_pic );
1879 vlc_mutex_lock( &p_sys->lock_out );
1880 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1881 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1882 vlc_cond_signal( &p_sys->cond );
1883 vlc_mutex_unlock( &p_sys->lock_out );
1889 p_pic->date = i_pts;
1890 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1891 block_ChainAppend( out, p_block );
1895 if( p_sys->i_threads == 0 )
1896 p_pic->pf_release( p_pic );
1902 static int EncoderThread( sout_stream_sys_t *p_sys )
1904 sout_stream_id_t *id = p_sys->id_video;
1907 while( !p_sys->b_die && !p_sys->b_error )
1911 vlc_mutex_lock( &p_sys->lock_out );
1912 while( p_sys->i_last_pic == p_sys->i_first_pic )
1914 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1915 if( p_sys->b_die || p_sys->b_error ) break;
1917 if( p_sys->b_die || p_sys->b_error )
1919 vlc_mutex_unlock( &p_sys->lock_out );
1923 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1924 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1925 vlc_mutex_unlock( &p_sys->lock_out );
1927 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1928 vlc_mutex_lock( &p_sys->lock_out );
1929 block_ChainAppend( &p_sys->p_buffers, p_block );
1931 vlc_mutex_unlock( &p_sys->lock_out );
1933 p_pic->pf_release( p_pic );
1936 while( p_sys->i_last_pic != p_sys->i_first_pic )
1938 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1939 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1941 p_pic->pf_release( p_pic );
1944 block_ChainRelease( p_sys->p_buffers );
1949 struct picture_sys_t
1951 vlc_object_t *p_owner;
1954 static void video_release_buffer( picture_t *p_pic )
1956 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
1958 video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
1960 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
1963 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
1964 sout_stream_sys_t *p_sys )
1966 decoder_t *p_dec = (decoder_t *)p_this;
1970 /* Find an empty space in the picture ring buffer */
1971 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1973 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
1975 pp_ring[i]->i_status = RESERVED_PICTURE;
1979 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1981 if( pp_ring[i] == 0 ) break;
1984 if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
1986 int i_first_pic = p_sys->i_first_pic;
1988 if( p_sys->i_first_pic != p_sys->i_last_pic )
1990 /* Encoder still has stuff to encode, wait to clear-up the list */
1991 while( p_sys->i_first_pic == i_first_pic )
1995 /* Find an empty space in the picture ring buffer */
1996 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1998 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
2000 pp_ring[i]->i_status = RESERVED_PICTURE;
2004 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2006 if( pp_ring[i] == 0 ) break;
2010 if( i == PICTURE_RING_SIZE )
2012 msg_Err( p_this, "decoder/filter is leaking pictures, "
2013 "resetting its ring buffer" );
2015 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2017 pp_ring[i]->pf_release( pp_ring[i] );
2023 p_pic = malloc( sizeof(picture_t) );
2024 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2025 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
2026 p_dec->fmt_out.video.i_chroma,
2027 p_dec->fmt_out.video.i_width,
2028 p_dec->fmt_out.video.i_height,
2029 p_dec->fmt_out.video.i_aspect );
2031 if( !p_pic->i_planes )
2037 p_pic->pf_release = video_release_buffer;
2038 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2039 p_pic->p_sys->p_owner = p_this;
2040 p_pic->i_status = RESERVED_PICTURE;
2047 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
2049 return video_new_buffer( VLC_OBJECT(p_dec),
2050 p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2053 static picture_t *video_new_buffer_filter( filter_t *p_filter )
2055 return video_new_buffer( VLC_OBJECT(p_filter),
2056 p_filter->p_owner->pp_pics,
2057 p_filter->p_owner->p_sys );
2060 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2062 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2063 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
2064 if( p_pic ) free( p_pic );
2067 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
2069 p_pic->i_refcount = 0;
2070 p_pic->i_status = DESTROYED_PICTURE;
2073 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
2075 p_pic->i_refcount = 0;
2076 p_pic->i_status = DESTROYED_PICTURE;
2079 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2081 p_pic->i_refcount++;
2084 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2086 video_release_buffer( p_pic );
2092 static subpicture_t *spu_new_buffer( decoder_t * );
2093 static void spu_del_buffer( decoder_t *, subpicture_t * );
2095 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2097 sout_stream_sys_t *p_sys = p_stream->p_sys;
2103 /* Initialization of decoder structures */
2104 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2105 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2106 id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2107 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
2109 id->p_decoder->p_module =
2110 module_Need( id->p_decoder, "decoder", "$codec", 0 );
2112 if( !id->p_decoder->p_module )
2114 msg_Err( p_stream, "cannot find decoder" );
2115 return VLC_EGENERIC;
2118 if( !p_sys->b_soverlay )
2124 /* Initialization of encoder format structures */
2125 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
2126 id->p_decoder->fmt_in.i_codec );
2128 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2130 id->p_encoder->p_module =
2131 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2133 if( !id->p_encoder->p_module )
2135 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2136 msg_Err( p_stream, "cannot find encoder" );
2137 return VLC_EGENERIC;
2143 p_sys->p_spu = spu_Create( p_stream );
2144 spu_Init( p_sys->p_spu );
2150 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2153 if( id->p_decoder->p_module )
2154 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2157 if( id->p_encoder->p_module )
2158 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2161 static int transcode_spu_process( sout_stream_t *p_stream,
2162 sout_stream_id_t *id,
2163 block_t *in, block_t **out )
2165 sout_stream_sys_t *p_sys = p_stream->p_sys;
2166 subpicture_t *p_subpic;
2169 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2170 if( !p_subpic ) return VLC_EGENERIC;
2172 if( p_sys->b_master_sync && p_sys->i_master_drift )
2174 p_subpic->i_start -= p_sys->i_master_drift;
2175 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2178 if( p_sys->b_soverlay )
2180 spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2186 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2187 spu_del_buffer( id->p_decoder, p_subpic );
2191 block_ChainAppend( out, p_block );
2196 return VLC_EGENERIC;
2199 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2201 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2202 return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2205 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2207 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2208 spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );