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 if( p_sys->b_master_sync && p_sys->i_master_drift )
902 if( p_buffer->i_dts > 0 )
904 p_buffer->i_dts -= p_sys->i_master_drift;
905 if( p_buffer->i_dts < 0 )
907 block_Release( p_buffer );
911 if( p_buffer->i_pts > 0 )
913 p_buffer->i_pts -= p_sys->i_master_drift;
914 if( p_buffer->i_pts < 0 )
916 block_Release( p_buffer );
922 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
924 else if( !id->b_transcode )
926 block_Release( p_buffer );
930 switch( id->p_decoder->fmt_in.i_cat )
933 transcode_audio_process( p_stream, id, p_buffer, &p_out );
937 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
945 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
954 block_Release( p_buffer );
958 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
962 /****************************************************************************
963 * decoder reencoder part
964 ****************************************************************************/
965 int audio_BitsPerSample( vlc_fourcc_t i_format )
969 case VLC_FOURCC('u','8',' ',' '):
970 case VLC_FOURCC('s','8',' ',' '):
973 case VLC_FOURCC('u','1','6','l'):
974 case VLC_FOURCC('s','1','6','l'):
975 case VLC_FOURCC('u','1','6','b'):
976 case VLC_FOURCC('s','1','6','b'):
979 case VLC_FOURCC('u','2','4','l'):
980 case VLC_FOURCC('s','2','4','l'):
981 case VLC_FOURCC('u','2','4','b'):
982 case VLC_FOURCC('s','2','4','b'):
985 case VLC_FOURCC('u','3','2','l'):
986 case VLC_FOURCC('s','3','2','l'):
987 case VLC_FOURCC('u','3','2','b'):
988 case VLC_FOURCC('s','3','2','b'):
989 case VLC_FOURCC('f','l','3','2'):
990 case VLC_FOURCC('f','i','3','2'):
993 case VLC_FOURCC('f','l','6','4'):
1000 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
1001 sout_stream_id_t *id,
1002 es_format_t *p_fmt_in,
1003 es_format_t *p_fmt_out )
1005 filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1007 vlc_object_attach( p_filter, p_stream );
1008 p_filter->pf_audio_buffer_new = (block_t* (*) (filter_t*, int))__block_New;
1010 p_filter->fmt_in = *p_fmt_in;
1011 p_filter->fmt_out = *p_fmt_out;
1013 p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
1014 if( p_filter->p_module )
1016 p_filter->fmt_out.audio.i_bitspersample =
1017 audio_BitsPerSample( p_filter->fmt_out.i_codec );
1018 *p_fmt_in = p_filter->fmt_out;
1022 vlc_object_detach( p_filter );
1023 vlc_object_destroy( p_filter );
1030 static int transcode_audio_new( sout_stream_t *p_stream,
1031 sout_stream_id_t *id )
1033 sout_stream_sys_t *p_sys = p_stream->p_sys;
1034 es_format_t fmt_last;
1041 /* Initialization of decoder structures */
1042 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1043 id->p_decoder->fmt_out.i_extra = 0;
1044 id->p_decoder->fmt_out.p_extra = 0;
1045 id->p_decoder->pf_decode_audio = 0;
1046 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
1047 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1048 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1050 id->p_decoder->p_module =
1051 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1053 if( !id->p_decoder->p_module )
1055 msg_Err( p_stream, "cannot find decoder" );
1056 return VLC_EGENERIC;
1058 id->p_decoder->fmt_out.audio.i_bitspersample =
1059 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1060 fmt_last = id->p_decoder->fmt_out;
1061 /* FIX decoders so we don't have to do this */
1062 fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1068 /* Initialization of encoder format structures */
1069 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1070 id->p_decoder->fmt_out.i_codec );
1071 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
1073 /* Initialization of encoder format structures */
1074 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1075 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1076 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
1077 id->p_encoder->fmt_in.audio.i_physical_channels =
1078 id->p_encoder->fmt_out.audio.i_physical_channels;
1079 id->p_encoder->fmt_in.audio.i_original_channels =
1080 id->p_encoder->fmt_out.audio.i_original_channels;
1081 id->p_encoder->fmt_in.audio.i_channels =
1082 id->p_encoder->fmt_out.audio.i_channels;
1083 id->p_encoder->fmt_in.audio.i_bitspersample =
1084 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1086 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1088 id->p_encoder->p_module =
1089 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
1090 if( !id->p_encoder->p_module )
1092 msg_Err( p_stream, "cannot find encoder" );
1093 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1094 id->p_decoder->p_module = 0;
1095 return VLC_EGENERIC;
1097 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1098 id->p_encoder->fmt_in.audio.i_bitspersample =
1099 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1101 /* Load conversion filters */
1102 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1103 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1105 /* We'll have to go through fl32 first */
1106 es_format_t fmt_out = id->p_encoder->fmt_in;
1107 fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1109 id->pp_filter[id->i_filter] =
1110 transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );
1112 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1117 if( fmt_last.audio.i_channels !=
1118 id->p_encoder->fmt_in.audio.i_channels ||
1119 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
1120 fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1122 id->pp_filter[id->i_filter] =
1123 transcode_audio_filter_new( p_stream, id, &fmt_last,
1124 &id->p_encoder->fmt_in );
1126 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1131 /* Final checks to see if conversions were successful */
1132 if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1134 msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1135 (char *)&fmt_last.i_codec,
1136 (char *)&id->p_encoder->fmt_in.i_codec );
1137 transcode_audio_close( p_stream, id );
1138 return VLC_EGENERIC;
1141 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1143 msg_Err( p_stream, "no audio filter found for mixing from"
1144 " %i to %i channels", fmt_last.audio.i_channels,
1145 id->p_encoder->fmt_in.audio.i_channels );
1147 /* FIXME : this might work, but only if the encoder is restarted */
1148 id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1149 id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1151 id->p_encoder->fmt_in.audio.i_physical_channels =
1152 id->p_encoder->fmt_in.audio.i_original_channels =
1153 fmt_last.audio.i_physical_channels;
1154 id->p_encoder->fmt_out.audio.i_physical_channels =
1155 id->p_encoder->fmt_out.audio.i_original_channels =
1156 fmt_last.audio.i_physical_channels;
1158 transcode_audio_close( p_stream, id );
1159 return VLC_EGENERIC;
1163 if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1165 msg_Err( p_stream, "no audio filter found for resampling from"
1166 " %iHz to %iHz", fmt_last.audio.i_rate,
1167 id->p_encoder->fmt_in.audio.i_rate );
1169 /* FIXME : this might work, but only if the encoder is restarted */
1170 id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1171 id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1173 transcode_audio_close( p_stream, id );
1174 return VLC_EGENERIC;
1178 /* FIXME: Hack for mp3 transcoding support */
1179 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1180 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1185 static void transcode_audio_close( sout_stream_t *p_stream,
1186 sout_stream_id_t *id )
1191 if( id->p_decoder->p_module )
1192 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1193 id->p_decoder->p_module = 0;
1196 if( id->p_encoder->p_module )
1197 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1198 id->p_encoder->p_module = 0;
1201 for( i = 0; i < id->i_filter; i++ )
1203 vlc_object_detach( id->pp_filter[i] );
1204 if( id->pp_filter[i]->p_module )
1205 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1206 vlc_object_destroy( id->pp_filter[i] );
1210 static int transcode_audio_process( sout_stream_t *p_stream,
1211 sout_stream_id_t *id,
1212 block_t *in, block_t **out )
1214 sout_stream_sys_t *p_sys = p_stream->p_sys;
1215 aout_buffer_t *p_audio_buf;
1216 block_t *p_block, *p_audio_block;
1220 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1223 if( p_sys->b_master_sync )
1225 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1226 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1227 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1228 p_audio_buf->start_date -= p_sys->i_master_drift;
1229 p_audio_buf->end_date -= p_sys->i_master_drift;
1232 p_audio_block = p_audio_buf->p_sys;
1233 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1234 p_audio_block->i_dts = p_audio_block->i_pts =
1235 p_audio_buf->start_date;
1236 p_audio_block->i_length = p_audio_buf->end_date -
1237 p_audio_buf->start_date;
1238 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1240 /* Run filter chain */
1241 for( i = 0; i < id->i_filter; i++ )
1244 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1248 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1249 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1250 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1251 p_audio_buf->start_date = p_audio_block->i_dts;
1252 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1254 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1255 block_ChainAppend( out, p_block );
1256 block_Release( p_audio_block );
1257 free( p_audio_buf );
1263 static void audio_release_buffer( aout_buffer_t *p_buffer )
1265 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1266 if( p_buffer ) free( p_buffer );
1269 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1271 aout_buffer_t *p_buffer;
1275 if( p_dec->fmt_out.audio.i_bitspersample )
1277 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1278 p_dec->fmt_out.audio.i_channels;
1280 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1281 p_dec->fmt_out.audio.i_frame_length )
1283 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1284 p_dec->fmt_out.audio.i_frame_length;
1288 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1291 p_buffer = malloc( sizeof(aout_buffer_t) );
1292 p_buffer->pf_release = audio_release_buffer;
1293 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1295 p_buffer->p_buffer = p_block->p_buffer;
1296 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1297 p_buffer->i_nb_samples = i_samples;
1298 p_block->i_samples = i_samples;
1303 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1305 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1306 if( p_buffer ) free( p_buffer );
1312 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1314 sout_stream_sys_t *p_sys = p_stream->p_sys;
1321 /* Initialization of decoder structures */
1322 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1323 id->p_decoder->fmt_out.i_extra = 0;
1324 id->p_decoder->fmt_out.p_extra = 0;
1325 id->p_decoder->pf_decode_video = 0;
1326 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1327 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1328 id->p_decoder->pf_picture_link = video_link_picture_decoder;
1329 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
1330 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1331 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1332 id->p_decoder->p_owner->pp_pics[i] = 0;
1333 id->p_decoder->p_owner->p_sys = p_sys;
1334 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1336 id->p_decoder->p_module =
1337 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1339 if( !id->p_decoder->p_module )
1341 msg_Err( p_stream, "cannot find decoder" );
1342 return VLC_EGENERIC;
1347 * Because some info about the decoded input will only be available
1348 * once the first frame is decoded, we actually only test the availability
1349 * of the encoder here.
1352 /* Initialization of encoder format structures */
1353 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1354 id->p_decoder->fmt_out.i_codec );
1355 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1357 /* The dimensions will be set properly later on.
1358 * Just put sensible values so we can test an encoder is available. */
1359 id->p_encoder->fmt_in.video.i_width =
1360 id->p_encoder->fmt_out.video.i_width ?
1361 id->p_encoder->fmt_out.video.i_width :
1362 id->p_decoder->fmt_in.video.i_width ?
1363 id->p_decoder->fmt_in.video.i_width : 16;
1364 id->p_encoder->fmt_in.video.i_height =
1365 id->p_encoder->fmt_out.video.i_height ?
1366 id->p_encoder->fmt_out.video.i_height :
1367 id->p_decoder->fmt_in.video.i_height ?
1368 id->p_decoder->fmt_in.video.i_height : 16;
1369 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1370 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1372 id->p_encoder->i_threads = p_sys->i_threads;
1373 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1375 id->p_encoder->p_module =
1376 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1377 if( !id->p_encoder->p_module )
1379 msg_Err( p_stream, "cannot find encoder" );
1380 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1381 id->p_decoder->p_module = 0;
1382 return VLC_EGENERIC;
1385 /* Close the encoder.
1386 * We'll open it only when we have the first frame. */
1387 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1388 id->p_encoder->p_module = NULL;
1390 if( p_sys->i_threads >= 1 )
1392 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
1393 VLC_THREAD_PRIORITY_VIDEO;
1394 p_sys->id_video = id;
1395 vlc_mutex_init( p_stream, &p_sys->lock_out );
1396 vlc_cond_init( p_stream, &p_sys->cond );
1397 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1398 p_sys->i_first_pic = 0;
1399 p_sys->i_last_pic = 0;
1400 p_sys->p_buffers = NULL;
1401 p_sys->b_die = p_sys->b_error = 0;
1402 if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
1405 msg_Err( p_stream, "cannot spawn encoder thread" );
1406 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1407 id->p_decoder->p_module = 0;
1408 return VLC_EGENERIC;
1412 date_Set( &id->interpolated_pts, 0 );
1417 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1418 sout_stream_id_t *id )
1420 sout_stream_sys_t *p_sys = p_stream->p_sys;
1422 /* Hack because of the copy packetizer which can fail to detect the
1423 * proper size (which forces us to wait until the 1st frame
1425 int i_width = id->p_decoder->fmt_out.video.i_width -
1426 p_sys->i_crop_left - p_sys->i_crop_right;
1427 int i_height = id->p_decoder->fmt_out.video.i_height -
1428 p_sys->i_crop_top - p_sys->i_crop_bottom;
1430 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1431 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1433 /* Apply the scaling */
1434 id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1435 id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1437 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1438 id->p_encoder->fmt_out.video.i_height <= 0 )
1440 id->p_encoder->fmt_out.video.i_height =
1441 id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1443 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1444 id->p_encoder->fmt_out.video.i_height > 0 )
1446 id->p_encoder->fmt_out.video.i_width =
1447 id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1450 /* Make sure the size is at least a multiple of 2 */
1451 id->p_encoder->fmt_out.video.i_width =
1452 (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
1453 id->p_encoder->fmt_out.video.i_height =
1454 (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
1456 id->p_encoder->fmt_in.video.i_width =
1457 id->p_encoder->fmt_out.video.i_width;
1458 id->p_encoder->fmt_in.video.i_height =
1459 id->p_encoder->fmt_out.video.i_height;
1461 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1462 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1464 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1465 id->p_decoder->fmt_out.video.i_frame_rate_base )
1467 id->p_encoder->fmt_out.video.i_frame_rate =
1468 id->p_decoder->fmt_out.video.i_frame_rate;
1469 id->p_encoder->fmt_out.video.i_frame_rate_base =
1470 id->p_decoder->fmt_out.video.i_frame_rate_base;
1474 /* Pick a sensible default value */
1475 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1476 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1480 id->p_encoder->fmt_in.video.i_frame_rate =
1481 id->p_encoder->fmt_out.video.i_frame_rate;
1482 id->p_encoder->fmt_in.video.i_frame_rate_base =
1483 id->p_encoder->fmt_out.video.i_frame_rate_base;
1485 date_Init( &id->interpolated_pts,
1486 id->p_encoder->fmt_out.video.i_frame_rate,
1487 id->p_encoder->fmt_out.video.i_frame_rate_base );
1489 /* Check whether a particular aspect ratio was requested */
1490 if( !id->p_encoder->fmt_out.video.i_aspect )
1492 id->p_encoder->fmt_out.video.i_aspect =
1493 id->p_decoder->fmt_out.video.i_aspect;
1495 id->p_encoder->fmt_in.video.i_aspect =
1496 id->p_encoder->fmt_out.video.i_aspect;
1498 id->p_encoder->p_module =
1499 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1500 if( !id->p_encoder->p_module )
1502 msg_Err( p_stream, "cannot find encoder" );
1503 return VLC_EGENERIC;
1506 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1508 /* Hack for mp2v/mp1v transcoding support */
1509 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1510 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1512 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1515 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1516 &id->p_encoder->fmt_out );
1519 msg_Err( p_stream, "cannot add this stream" );
1520 return VLC_EGENERIC;
1526 static void transcode_video_close( sout_stream_t *p_stream,
1527 sout_stream_id_t *id )
1531 if( p_stream->p_sys->i_threads >= 1 )
1533 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1534 p_stream->p_sys->b_die = 1;
1535 vlc_cond_signal( &p_stream->p_sys->cond );
1536 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1537 vlc_thread_join( p_stream->p_sys );
1538 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1539 vlc_cond_destroy( &p_stream->p_sys->cond );
1543 if( id->p_decoder->p_module )
1544 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1546 if( id->p_decoder->p_owner )
1548 /* Clean-up pictures ring buffer */
1549 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1551 if( id->p_decoder->p_owner->pp_pics[i] )
1552 video_del_buffer( VLC_OBJECT(id->p_decoder),
1553 id->p_decoder->p_owner->pp_pics[i] );
1555 free( id->p_decoder->p_owner );
1559 if( id->p_encoder->p_module )
1560 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1563 for( i = 0; i < id->i_filter; i++ )
1565 vlc_object_detach( id->pp_filter[i] );
1566 if( id->pp_filter[i]->p_module )
1567 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1569 /* Clean-up pictures ring buffer */
1570 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1572 if( id->pp_filter[i]->p_owner->pp_pics[j] )
1573 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1574 id->pp_filter[i]->p_owner->pp_pics[j] );
1576 free( id->pp_filter[i]->p_owner );
1578 vlc_object_destroy( id->pp_filter[i] );
1580 for( i = 0; i < id->i_vfilter; i++ )
1582 vlc_object_detach( id->pp_vfilter[i] );
1583 if( id->pp_vfilter[i]->p_module )
1584 module_Unneed( id->pp_vfilter[i], id->pp_vfilter[i]->p_module );
1586 /* Clean-up pictures ring buffer */
1587 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1589 if( id->pp_vfilter[i]->p_owner->pp_pics[j] )
1590 video_del_buffer( VLC_OBJECT(id->pp_vfilter[i]),
1591 id->pp_vfilter[i]->p_owner->pp_pics[j] );
1593 free( id->pp_vfilter[i]->p_owner );
1595 vlc_object_destroy( id->pp_vfilter[i] );
1599 static int transcode_video_process( sout_stream_t *p_stream,
1600 sout_stream_id_t *id,
1601 block_t *in, block_t **out )
1603 sout_stream_sys_t *p_sys = p_stream->p_sys;
1604 int i_duplicate = 1, i;
1608 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1610 subpicture_t *p_subpic = 0;
1612 if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
1614 mtime_t current_date = mdate();
1615 if( current_date + 50000 > p_pic->date )
1617 msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
1618 current_date + 50000 - p_pic->date );
1619 p_pic->pf_release( p_pic );
1624 if( p_sys->b_master_sync )
1626 mtime_t i_video_drift;
1627 mtime_t i_master_drift = p_sys->i_master_drift;
1630 if( !i_master_drift )
1632 /* No audio track ? */
1633 p_sys->i_master_drift = i_master_drift = p_pic->date;
1636 i_pts = date_Get( &id->interpolated_pts ) + 1;
1637 i_video_drift = p_pic->date - i_pts;
1640 /* Set the pts of the frame being encoded */
1641 p_pic->date = i_pts;
1643 if( i_video_drift < i_master_drift - 50000 )
1646 msg_Dbg( p_stream, "dropping frame (%i)",
1647 (int)(i_video_drift - i_master_drift) );
1649 p_pic->pf_release( p_pic );
1652 else if( i_video_drift > i_master_drift + 50000 )
1655 msg_Dbg( p_stream, "adding frame (%i)",
1656 (int)(i_video_drift - i_master_drift) );
1662 if( !id->p_encoder->p_module )
1664 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1666 p_pic->pf_release( p_pic );
1667 transcode_video_close( p_stream, id );
1668 id->b_transcode = VLC_FALSE;
1669 return VLC_EGENERIC;
1673 if( p_stream->p_sys->b_deinterlace )
1675 id->pp_filter[id->i_filter] =
1676 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1677 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1679 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1680 video_new_buffer_filter;
1681 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1682 video_del_buffer_filter;
1684 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1685 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
1686 id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
1687 id->pp_filter[id->i_filter]->p_module =
1688 module_Need( id->pp_filter[id->i_filter],
1689 "video filter2", p_sys->psz_deinterlace, 0 );
1690 if( id->pp_filter[id->i_filter]->p_module )
1692 id->pp_filter[id->i_filter]->p_owner =
1693 malloc( sizeof(filter_owner_sys_t) );
1694 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1695 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1696 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1702 msg_Dbg( p_stream, "no video filter found" );
1703 vlc_object_detach( id->pp_filter[id->i_filter] );
1704 vlc_object_destroy( id->pp_filter[id->i_filter] );
1708 /* Check if we need a filter for chroma conversion or resizing */
1709 if( id->p_decoder->fmt_out.video.i_chroma !=
1710 id->p_encoder->fmt_in.video.i_chroma ||
1711 id->p_decoder->fmt_out.video.i_width !=
1712 id->p_encoder->fmt_out.video.i_width ||
1713 id->p_decoder->fmt_out.video.i_height !=
1714 id->p_encoder->fmt_out.video.i_height ||
1715 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1716 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1718 id->pp_filter[id->i_filter] =
1719 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1720 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1722 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1723 video_new_buffer_filter;
1724 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1725 video_del_buffer_filter;
1727 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1728 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1729 id->pp_filter[id->i_filter]->p_cfg = NULL;
1730 id->pp_filter[id->i_filter]->p_module =
1731 module_Need( id->pp_filter[id->i_filter],
1732 "video filter2", 0, 0 );
1733 if( id->pp_filter[id->i_filter]->p_module )
1735 id->pp_filter[id->i_filter]->p_owner =
1736 malloc( sizeof(filter_owner_sys_t) );
1737 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1738 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1739 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1745 msg_Dbg( p_stream, "no video filter found" );
1746 vlc_object_detach( id->pp_filter[id->i_filter] );
1747 vlc_object_destroy( id->pp_filter[id->i_filter] );
1749 p_pic->pf_release( p_pic );
1750 transcode_video_close( p_stream, id );
1751 id->b_transcode = VLC_FALSE;
1752 return VLC_EGENERIC;
1756 for( i = 0; i < p_sys->i_vfilters; i++ )
1758 id->pp_vfilter[id->i_vfilter] =
1759 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1760 vlc_object_attach( id->pp_vfilter[id->i_vfilter], p_stream );
1762 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_new =
1763 video_new_buffer_filter;
1764 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_del =
1765 video_del_buffer_filter;
1767 id->pp_vfilter[id->i_vfilter]->fmt_in = id->p_encoder->fmt_in;
1768 id->pp_vfilter[id->i_vfilter]->fmt_out = id->p_encoder->fmt_in;
1769 id->pp_vfilter[id->i_vfilter]->p_cfg = p_sys->p_vfilters_cfg[i];
1770 id->pp_vfilter[id->i_vfilter]->p_module =
1771 module_Need( id->pp_vfilter[id->i_vfilter],
1772 "video filter2", p_sys->psz_vfilters[i], 0 );
1773 if( id->pp_vfilter[id->i_vfilter]->p_module )
1775 id->pp_vfilter[id->i_vfilter]->p_owner =
1776 malloc( sizeof(filter_owner_sys_t) );
1777 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1778 id->pp_vfilter[id->i_vfilter]->p_owner->pp_pics[i] = 0;
1779 id->pp_vfilter[id->i_vfilter]->p_owner->p_sys = p_sys;
1785 msg_Dbg( p_stream, "no video filter found" );
1786 vlc_object_detach( id->pp_vfilter[id->i_vfilter] );
1787 vlc_object_destroy( id->pp_vfilter[id->i_vfilter] );
1792 /* Run filter chain */
1793 for( i = 0; i < id->i_filter; i++ )
1795 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1802 /* Check if we have a subpicture to overlay */
1805 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
1806 /* TODO: get another pic */
1809 /* Overlay subpicture */
1812 int i_scale_width, i_scale_height;
1813 video_format_t *p_fmt;
1815 i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
1816 id->p_decoder->fmt_out.video.i_width;
1817 i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
1818 id->p_decoder->fmt_out.video.i_height;
1820 if( p_pic->i_refcount && !id->i_filter )
1822 /* We can't modify the picture, we need to duplicate it */
1823 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1826 vout_CopyPicture( p_stream, p_tmp, p_pic );
1827 p_pic->pf_release( p_pic );
1833 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
1835 p_fmt = &id->p_decoder->fmt_out.video;
1837 /* FIXME (shouldn't have to be done here) */
1838 p_fmt->i_sar_num = p_fmt->i_aspect *
1839 p_fmt->i_height / p_fmt->i_width;
1840 p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
1842 spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
1843 i_scale_width, i_scale_height );
1846 /* Run vfilter chain */
1847 for( i = 0; i < id->i_vfilter; i++ )
1849 p_pic = id->pp_vfilter[i]->pf_video_filter(id->pp_vfilter[i], p_pic);
1852 if( p_sys->i_threads >= 1 )
1854 vlc_mutex_lock( &p_sys->lock_out );
1855 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1856 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1857 *out = p_sys->p_buffers;
1858 p_sys->p_buffers = NULL;
1859 vlc_cond_signal( &p_sys->cond );
1860 vlc_mutex_unlock( &p_sys->lock_out );
1865 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1866 block_ChainAppend( out, p_block );
1868 if( p_sys->b_master_sync )
1869 date_Increment( &id->interpolated_pts, 1 );
1871 if( p_sys->b_master_sync && i_duplicate > 1 )
1873 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1874 date_Increment( &id->interpolated_pts, 1 );
1875 p_pic->date = i_pts;
1876 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1877 block_ChainAppend( out, p_block );
1880 p_pic->pf_release( p_pic );
1887 static int EncoderThread( sout_stream_sys_t *p_sys )
1889 sout_stream_id_t *id = p_sys->id_video;
1892 while( !p_sys->b_die && !p_sys->b_error )
1896 vlc_mutex_lock( &p_sys->lock_out );
1897 while( p_sys->i_last_pic == p_sys->i_first_pic )
1899 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1900 if( p_sys->b_die || p_sys->b_error ) break;
1902 if( p_sys->b_die || p_sys->b_error )
1904 vlc_mutex_unlock( &p_sys->lock_out );
1908 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1909 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1910 vlc_mutex_unlock( &p_sys->lock_out );
1912 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1913 vlc_mutex_lock( &p_sys->lock_out );
1914 block_ChainAppend( &p_sys->p_buffers, p_block );
1916 if( p_sys->b_master_sync )
1917 date_Increment( &id->interpolated_pts, 1 );
1920 if( p_sys->b_master_sync && i_duplicate > 1 )
1922 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1923 date_Increment( &id->interpolated_pts, 1 );
1924 p_pic->date = i_pts;
1925 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1926 block_ChainAppend( &p_sys->p_buffers, p_block );
1929 vlc_mutex_unlock( &p_sys->lock_out );
1931 p_pic->pf_release( p_pic );
1934 while( p_sys->i_last_pic != p_sys->i_first_pic )
1936 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1937 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1939 p_pic->pf_release( p_pic );
1942 block_ChainRelease( p_sys->p_buffers );
1947 struct picture_sys_t
1949 vlc_object_t *p_owner;
1952 static void video_release_buffer( picture_t *p_pic )
1954 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
1956 video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
1958 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
1961 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
1962 sout_stream_sys_t *p_sys )
1964 decoder_t *p_dec = (decoder_t *)p_this;
1968 /* Find an empty space in the picture ring buffer */
1969 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1971 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
1973 pp_ring[i]->i_status = RESERVED_PICTURE;
1977 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1979 if( pp_ring[i] == 0 ) break;
1982 if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
1984 int i_first_pic = p_sys->i_first_pic;
1986 if( p_sys->i_first_pic != p_sys->i_last_pic )
1988 /* Encoder still has stuff to encode, wait to clear-up the list */
1989 while( p_sys->i_first_pic == i_first_pic )
1993 /* Find an empty space in the picture ring buffer */
1994 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1996 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
1998 pp_ring[i]->i_status = RESERVED_PICTURE;
2002 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2004 if( pp_ring[i] == 0 ) break;
2008 if( i == PICTURE_RING_SIZE )
2010 msg_Err( p_this, "decoder/filter is leaking pictures, "
2011 "resetting its ring buffer" );
2013 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2015 pp_ring[i]->pf_release( pp_ring[i] );
2021 p_pic = malloc( sizeof(picture_t) );
2022 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2023 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
2024 p_dec->fmt_out.video.i_chroma,
2025 p_dec->fmt_out.video.i_width,
2026 p_dec->fmt_out.video.i_height,
2027 p_dec->fmt_out.video.i_aspect );
2029 if( !p_pic->i_planes )
2035 p_pic->pf_release = video_release_buffer;
2036 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2037 p_pic->p_sys->p_owner = p_this;
2038 p_pic->i_status = RESERVED_PICTURE;
2045 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
2047 return video_new_buffer( VLC_OBJECT(p_dec),
2048 p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2051 static picture_t *video_new_buffer_filter( filter_t *p_filter )
2053 return video_new_buffer( VLC_OBJECT(p_filter),
2054 p_filter->p_owner->pp_pics,
2055 p_filter->p_owner->p_sys );
2058 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2060 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2061 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
2062 if( p_pic ) free( p_pic );
2065 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
2067 p_pic->i_refcount = 0;
2068 p_pic->i_status = DESTROYED_PICTURE;
2071 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
2073 p_pic->i_refcount = 0;
2074 p_pic->i_status = DESTROYED_PICTURE;
2077 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2079 p_pic->i_refcount++;
2082 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2084 video_release_buffer( p_pic );
2090 static subpicture_t *spu_new_buffer( decoder_t * );
2091 static void spu_del_buffer( decoder_t *, subpicture_t * );
2093 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2095 sout_stream_sys_t *p_sys = p_stream->p_sys;
2101 /* Initialization of decoder structures */
2102 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2103 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2104 id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2105 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
2107 id->p_decoder->p_module =
2108 module_Need( id->p_decoder, "decoder", "$codec", 0 );
2110 if( !id->p_decoder->p_module )
2112 msg_Err( p_stream, "cannot find decoder" );
2113 return VLC_EGENERIC;
2116 if( !p_sys->b_soverlay )
2122 /* Initialization of encoder format structures */
2123 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
2124 id->p_decoder->fmt_in.i_codec );
2126 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2128 id->p_encoder->p_module =
2129 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2131 if( !id->p_encoder->p_module )
2133 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2134 msg_Err( p_stream, "cannot find encoder" );
2135 return VLC_EGENERIC;
2141 p_sys->p_spu = spu_Create( p_stream );
2142 spu_Init( p_sys->p_spu );
2148 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2151 if( id->p_decoder->p_module )
2152 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2155 if( id->p_encoder->p_module )
2156 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2159 static int transcode_spu_process( sout_stream_t *p_stream,
2160 sout_stream_id_t *id,
2161 block_t *in, block_t **out )
2163 sout_stream_sys_t *p_sys = p_stream->p_sys;
2164 subpicture_t *p_subpic;
2167 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2168 if( !p_subpic ) return VLC_EGENERIC;
2170 if( p_sys->b_master_sync && p_sys->i_master_drift )
2172 p_subpic->i_start -= p_sys->i_master_drift;
2173 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2176 if( p_sys->b_soverlay )
2178 spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2184 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2185 spu_del_buffer( id->p_decoder, p_subpic );
2189 block_ChainAppend( out, p_block );
2194 return VLC_EGENERIC;
2197 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2199 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2200 return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2203 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2205 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2206 spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );