1 /*****************************************************************************
2 * transcode.c: transcoding stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc/input.h>
36 #include <vlc/decoder.h>
37 #include "vlc_filter.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 #define VENC_TEXT N_("Video encoder")
44 #define VENC_LONGTEXT N_( \
45 "Allows you to specify the video encoder to use and its associated " \
47 #define VCODEC_TEXT N_("Destination video codec")
48 #define VCODEC_LONGTEXT N_( \
49 "Allows you to specify the destination video codec used for the " \
51 #define VB_TEXT N_("Video bitrate")
52 #define VB_LONGTEXT N_( \
53 "Allows you to specify the video bitrate used for the streaming " \
55 #define SCALE_TEXT N_("Video scaling")
56 #define SCALE_LONGTEXT N_( \
57 "Allows you to scale the video before encoding." )
58 #define FPS_TEXT N_("Video frame-rate")
59 #define FPS_LONGTEXT N_( \
60 "Allows you to specify an output frame rate for the video." )
61 #define DEINTERLACE_TEXT N_("Deinterlace video")
62 #define DEINTERLACE_LONGTEXT N_( \
63 "Allows you to deinterlace the video before encoding." )
64 #define 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 ) !=
953 block_Release( p_buffer );
957 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
961 /****************************************************************************
962 * decoder reencoder part
963 ****************************************************************************/
964 int audio_BitsPerSample( vlc_fourcc_t i_format )
968 case VLC_FOURCC('u','8',' ',' '):
969 case VLC_FOURCC('s','8',' ',' '):
972 case VLC_FOURCC('u','1','6','l'):
973 case VLC_FOURCC('s','1','6','l'):
974 case VLC_FOURCC('u','1','6','b'):
975 case VLC_FOURCC('s','1','6','b'):
978 case VLC_FOURCC('u','2','4','l'):
979 case VLC_FOURCC('s','2','4','l'):
980 case VLC_FOURCC('u','2','4','b'):
981 case VLC_FOURCC('s','2','4','b'):
984 case VLC_FOURCC('u','3','2','l'):
985 case VLC_FOURCC('s','3','2','l'):
986 case VLC_FOURCC('u','3','2','b'):
987 case VLC_FOURCC('s','3','2','b'):
988 case VLC_FOURCC('f','l','3','2'):
989 case VLC_FOURCC('f','i','3','2'):
992 case VLC_FOURCC('f','l','6','4'):
999 static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
1000 sout_stream_id_t *id,
1001 es_format_t *p_fmt_in,
1002 es_format_t *p_fmt_out )
1004 filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1006 vlc_object_attach( p_filter, p_stream );
1007 p_filter->pf_audio_buffer_new = __block_New;
1009 p_filter->fmt_in = *p_fmt_in;
1010 p_filter->fmt_out = *p_fmt_out;
1012 p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
1013 if( p_filter->p_module )
1015 p_filter->fmt_out.audio.i_bitspersample =
1016 audio_BitsPerSample( p_filter->fmt_out.i_codec );
1017 *p_fmt_in = p_filter->fmt_out;
1021 vlc_object_detach( p_filter );
1022 vlc_object_destroy( p_filter );
1029 static int transcode_audio_new( sout_stream_t *p_stream,
1030 sout_stream_id_t *id )
1032 sout_stream_sys_t *p_sys = p_stream->p_sys;
1033 es_format_t fmt_last;
1040 /* Initialization of decoder structures */
1041 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1042 id->p_decoder->fmt_out.i_extra = 0;
1043 id->p_decoder->fmt_out.p_extra = 0;
1044 id->p_decoder->pf_decode_audio = 0;
1045 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
1046 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1047 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1049 id->p_decoder->p_module =
1050 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1052 if( !id->p_decoder->p_module )
1054 msg_Err( p_stream, "cannot find decoder" );
1055 return VLC_EGENERIC;
1057 id->p_decoder->fmt_out.audio.i_bitspersample =
1058 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1059 fmt_last = id->p_decoder->fmt_out;
1060 /* FIX decoders so we don't have to do this */
1061 fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1067 /* Initialization of encoder format structures */
1068 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1069 id->p_decoder->fmt_out.i_codec );
1070 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
1072 /* Initialization of encoder format structures */
1073 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1074 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1075 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
1076 id->p_encoder->fmt_in.audio.i_physical_channels =
1077 id->p_encoder->fmt_out.audio.i_physical_channels;
1078 id->p_encoder->fmt_in.audio.i_original_channels =
1079 id->p_encoder->fmt_out.audio.i_original_channels;
1080 id->p_encoder->fmt_in.audio.i_channels =
1081 id->p_encoder->fmt_out.audio.i_channels;
1082 id->p_encoder->fmt_in.audio.i_bitspersample =
1083 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1085 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1087 id->p_encoder->p_module =
1088 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
1089 if( !id->p_encoder->p_module )
1091 msg_Err( p_stream, "cannot find encoder" );
1092 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1093 id->p_decoder->p_module = 0;
1094 return VLC_EGENERIC;
1096 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1097 id->p_encoder->fmt_in.audio.i_bitspersample =
1098 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1100 /* Load conversion filters */
1101 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
1102 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1104 /* We'll have to go through fl32 first */
1105 es_format_t fmt_out = id->p_encoder->fmt_in;
1106 fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
1108 id->pp_filter[id->i_filter] =
1109 transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );
1111 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1116 if( fmt_last.audio.i_channels !=
1117 id->p_encoder->fmt_in.audio.i_channels ||
1118 fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
1119 fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1121 id->pp_filter[id->i_filter] =
1122 transcode_audio_filter_new( p_stream, id, &fmt_last,
1123 &id->p_encoder->fmt_in );
1125 if( id->pp_filter[id->i_filter] ) id->i_filter++;
1130 /* Final checks to see if conversions were successful */
1131 if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1133 msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1134 (char *)&fmt_last.i_codec,
1135 (char *)&id->p_encoder->fmt_in.i_codec );
1136 transcode_audio_close( p_stream, id );
1137 return VLC_EGENERIC;
1140 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1142 msg_Err( p_stream, "no audio filter found for mixing from"
1143 " %i to %i channels", fmt_last.audio.i_channels,
1144 id->p_encoder->fmt_in.audio.i_channels );
1146 /* FIXME : this might work, but only if the encoder is restarted */
1147 id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1148 id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1150 id->p_encoder->fmt_in.audio.i_physical_channels =
1151 id->p_encoder->fmt_in.audio.i_original_channels =
1152 fmt_last.audio.i_physical_channels;
1153 id->p_encoder->fmt_out.audio.i_physical_channels =
1154 id->p_encoder->fmt_out.audio.i_original_channels =
1155 fmt_last.audio.i_physical_channels;
1157 transcode_audio_close( p_stream, id );
1158 return VLC_EGENERIC;
1162 if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1164 msg_Err( p_stream, "no audio filter found for resampling from"
1165 " %iHz to %iHz", fmt_last.audio.i_rate,
1166 id->p_encoder->fmt_in.audio.i_rate );
1168 /* FIXME : this might work, but only if the encoder is restarted */
1169 id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1170 id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1172 transcode_audio_close( p_stream, id );
1173 return VLC_EGENERIC;
1177 /* FIXME: Hack for mp3 transcoding support */
1178 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1179 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1184 static void transcode_audio_close( sout_stream_t *p_stream,
1185 sout_stream_id_t *id )
1190 if( id->p_decoder->p_module )
1191 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1192 id->p_decoder->p_module = 0;
1195 if( id->p_encoder->p_module )
1196 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1197 id->p_encoder->p_module = 0;
1200 for( i = 0; i < id->i_filter; i++ )
1202 vlc_object_detach( id->pp_filter[i] );
1203 if( id->pp_filter[i]->p_module )
1204 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1205 vlc_object_destroy( id->pp_filter[i] );
1209 static int transcode_audio_process( sout_stream_t *p_stream,
1210 sout_stream_id_t *id,
1211 block_t *in, block_t **out )
1213 sout_stream_sys_t *p_sys = p_stream->p_sys;
1214 aout_buffer_t *p_audio_buf;
1215 block_t *p_block, *p_audio_block;
1219 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1222 if( p_sys->b_master_sync )
1224 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1225 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1226 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1227 p_audio_buf->start_date -= p_sys->i_master_drift;
1228 p_audio_buf->end_date -= p_sys->i_master_drift;
1231 p_audio_block = p_audio_buf->p_sys;
1232 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1233 p_audio_block->i_dts = p_audio_block->i_pts =
1234 p_audio_buf->start_date;
1235 p_audio_block->i_length = p_audio_buf->end_date -
1236 p_audio_buf->start_date;
1237 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1239 /* Run filter chain */
1240 for( i = 0; i < id->i_filter; i++ )
1243 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1247 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1248 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1249 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1250 p_audio_buf->start_date = p_audio_block->i_dts;
1251 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1253 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1254 block_ChainAppend( out, p_block );
1255 block_Release( p_audio_block );
1256 free( p_audio_buf );
1262 static void audio_release_buffer( aout_buffer_t *p_buffer )
1264 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1265 if( p_buffer ) free( p_buffer );
1268 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1270 aout_buffer_t *p_buffer;
1274 if( p_dec->fmt_out.audio.i_bitspersample )
1276 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1277 p_dec->fmt_out.audio.i_channels;
1279 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1280 p_dec->fmt_out.audio.i_frame_length )
1282 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1283 p_dec->fmt_out.audio.i_frame_length;
1287 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1290 p_buffer = malloc( sizeof(aout_buffer_t) );
1291 p_buffer->pf_release = audio_release_buffer;
1292 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1294 p_buffer->p_buffer = p_block->p_buffer;
1295 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1296 p_buffer->i_nb_samples = i_samples;
1297 p_block->i_samples = i_samples;
1302 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1304 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1305 if( p_buffer ) free( p_buffer );
1311 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1313 sout_stream_sys_t *p_sys = p_stream->p_sys;
1320 /* Initialization of decoder structures */
1321 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
1322 id->p_decoder->fmt_out.i_extra = 0;
1323 id->p_decoder->fmt_out.p_extra = 0;
1324 id->p_decoder->pf_decode_video = 0;
1325 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1326 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1327 id->p_decoder->pf_picture_link = video_link_picture_decoder;
1328 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
1329 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1330 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1331 id->p_decoder->p_owner->pp_pics[i] = 0;
1332 id->p_decoder->p_owner->p_sys = p_sys;
1333 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1335 id->p_decoder->p_module =
1336 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1338 if( !id->p_decoder->p_module )
1340 msg_Err( p_stream, "cannot find decoder" );
1341 return VLC_EGENERIC;
1346 * Because some info about the decoded input will only be available
1347 * once the first frame is decoded, we actually only test the availability
1348 * of the encoder here.
1351 /* Initialization of encoder format structures */
1352 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1353 id->p_decoder->fmt_out.i_codec );
1354 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1356 /* The dimensions will be set properly later on.
1357 * Just put sensible values so we can test an encoder is available. */
1358 id->p_encoder->fmt_in.video.i_width =
1359 id->p_encoder->fmt_out.video.i_width ?
1360 id->p_encoder->fmt_out.video.i_width :
1361 id->p_decoder->fmt_in.video.i_width ?
1362 id->p_decoder->fmt_in.video.i_width : 16;
1363 id->p_encoder->fmt_in.video.i_height =
1364 id->p_encoder->fmt_out.video.i_height ?
1365 id->p_encoder->fmt_out.video.i_height :
1366 id->p_decoder->fmt_in.video.i_height ?
1367 id->p_decoder->fmt_in.video.i_height : 16;
1368 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1369 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1371 id->p_encoder->i_threads = p_sys->i_threads;
1372 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1374 id->p_encoder->p_module =
1375 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1376 if( !id->p_encoder->p_module )
1378 msg_Err( p_stream, "cannot find encoder" );
1379 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1380 id->p_decoder->p_module = 0;
1381 return VLC_EGENERIC;
1384 /* Close the encoder.
1385 * We'll open it only when we have the first frame. */
1386 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1387 id->p_encoder->p_module = NULL;
1389 if( p_sys->i_threads >= 1 )
1391 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
1392 VLC_THREAD_PRIORITY_VIDEO;
1393 p_sys->id_video = id;
1394 vlc_mutex_init( p_stream, &p_sys->lock_out );
1395 vlc_cond_init( p_stream, &p_sys->cond );
1396 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1397 p_sys->i_first_pic = 0;
1398 p_sys->i_last_pic = 0;
1399 p_sys->p_buffers = NULL;
1400 p_sys->b_die = p_sys->b_error = 0;
1401 if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
1404 msg_Err( p_stream, "cannot spawn encoder thread" );
1405 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1406 id->p_decoder->p_module = 0;
1407 return VLC_EGENERIC;
1411 date_Set( &id->interpolated_pts, 0 );
1416 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1417 sout_stream_id_t *id )
1419 sout_stream_sys_t *p_sys = p_stream->p_sys;
1421 /* Hack because of the copy packetizer which can fail to detect the
1422 * proper size (which forces us to wait until the 1st frame
1424 int i_width = id->p_decoder->fmt_out.video.i_width -
1425 p_sys->i_crop_left - p_sys->i_crop_right;
1426 int i_height = id->p_decoder->fmt_out.video.i_height -
1427 p_sys->i_crop_top - p_sys->i_crop_bottom;
1429 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1430 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1432 /* Apply the scaling */
1433 id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1434 id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1436 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1437 id->p_encoder->fmt_out.video.i_height <= 0 )
1439 id->p_encoder->fmt_out.video.i_height =
1440 id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1442 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1443 id->p_encoder->fmt_out.video.i_height > 0 )
1445 id->p_encoder->fmt_out.video.i_width =
1446 id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1449 /* Make sure the size is at least a multiple of 2 */
1450 id->p_encoder->fmt_out.video.i_width =
1451 (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
1452 id->p_encoder->fmt_out.video.i_height =
1453 (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
1455 id->p_encoder->fmt_in.video.i_width =
1456 id->p_encoder->fmt_out.video.i_width;
1457 id->p_encoder->fmt_in.video.i_height =
1458 id->p_encoder->fmt_out.video.i_height;
1460 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1461 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1463 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1464 id->p_decoder->fmt_out.video.i_frame_rate_base )
1466 id->p_encoder->fmt_out.video.i_frame_rate =
1467 id->p_decoder->fmt_out.video.i_frame_rate;
1468 id->p_encoder->fmt_out.video.i_frame_rate_base =
1469 id->p_decoder->fmt_out.video.i_frame_rate_base;
1473 /* Pick a sensible default value */
1474 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1475 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1479 id->p_encoder->fmt_in.video.i_frame_rate =
1480 id->p_encoder->fmt_out.video.i_frame_rate;
1481 id->p_encoder->fmt_in.video.i_frame_rate_base =
1482 id->p_encoder->fmt_out.video.i_frame_rate_base;
1484 date_Init( &id->interpolated_pts,
1485 id->p_encoder->fmt_out.video.i_frame_rate,
1486 id->p_encoder->fmt_out.video.i_frame_rate_base );
1488 /* Check whether a particular aspect ratio was requested */
1489 if( !id->p_encoder->fmt_out.video.i_aspect )
1491 id->p_encoder->fmt_out.video.i_aspect =
1492 id->p_decoder->fmt_out.video.i_aspect;
1494 id->p_encoder->fmt_in.video.i_aspect =
1495 id->p_encoder->fmt_out.video.i_aspect;
1497 id->p_encoder->p_module =
1498 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1499 if( !id->p_encoder->p_module )
1501 msg_Err( p_stream, "cannot find encoder" );
1502 return VLC_EGENERIC;
1505 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1507 /* Hack for mp2v/mp1v transcoding support */
1508 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1509 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1511 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1514 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1515 &id->p_encoder->fmt_out );
1518 msg_Err( p_stream, "cannot add this stream" );
1519 return VLC_EGENERIC;
1525 static void transcode_video_close( sout_stream_t *p_stream,
1526 sout_stream_id_t *id )
1530 if( p_stream->p_sys->i_threads >= 1 )
1532 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1533 p_stream->p_sys->b_die = 1;
1534 vlc_cond_signal( &p_stream->p_sys->cond );
1535 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1536 vlc_thread_join( p_stream->p_sys );
1537 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1538 vlc_cond_destroy( &p_stream->p_sys->cond );
1542 if( id->p_decoder->p_module )
1543 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1545 if( id->p_decoder->p_owner )
1547 /* Clean-up pictures ring buffer */
1548 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1550 if( id->p_decoder->p_owner->pp_pics[i] )
1551 video_del_buffer( VLC_OBJECT(id->p_decoder),
1552 id->p_decoder->p_owner->pp_pics[i] );
1554 free( id->p_decoder->p_owner );
1558 if( id->p_encoder->p_module )
1559 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1562 for( i = 0; i < id->i_filter; i++ )
1564 vlc_object_detach( id->pp_filter[i] );
1565 if( id->pp_filter[i]->p_module )
1566 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1568 /* Clean-up pictures ring buffer */
1569 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1571 if( id->pp_filter[i]->p_owner->pp_pics[j] )
1572 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1573 id->pp_filter[i]->p_owner->pp_pics[j] );
1575 free( id->pp_filter[i]->p_owner );
1577 vlc_object_destroy( id->pp_filter[i] );
1579 for( i = 0; i < id->i_vfilter; i++ )
1581 vlc_object_detach( id->pp_vfilter[i] );
1582 if( id->pp_vfilter[i]->p_module )
1583 module_Unneed( id->pp_vfilter[i], id->pp_vfilter[i]->p_module );
1585 /* Clean-up pictures ring buffer */
1586 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1588 if( id->pp_vfilter[i]->p_owner->pp_pics[j] )
1589 video_del_buffer( VLC_OBJECT(id->pp_vfilter[i]),
1590 id->pp_vfilter[i]->p_owner->pp_pics[j] );
1592 free( id->pp_vfilter[i]->p_owner );
1594 vlc_object_destroy( id->pp_vfilter[i] );
1598 static int transcode_video_process( sout_stream_t *p_stream,
1599 sout_stream_id_t *id,
1600 block_t *in, block_t **out )
1602 sout_stream_sys_t *p_sys = p_stream->p_sys;
1603 int i_duplicate = 1, i;
1607 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1609 subpicture_t *p_subpic = 0;
1611 if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
1613 mtime_t current_date = mdate();
1614 if( current_date + 50000 > p_pic->date )
1616 msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
1617 current_date + 50000 - p_pic->date );
1618 p_pic->pf_release( p_pic );
1623 if( p_sys->b_master_sync )
1625 mtime_t i_video_drift;
1626 mtime_t i_master_drift = p_sys->i_master_drift;
1629 if( !i_master_drift )
1631 /* No audio track ? */
1632 p_sys->i_master_drift = i_master_drift = p_pic->date;
1635 i_pts = date_Get( &id->interpolated_pts ) + 1;
1636 i_video_drift = p_pic->date - i_pts;
1639 /* Set the pts of the frame being encoded */
1640 p_pic->date = i_pts;
1642 if( i_video_drift < i_master_drift - 50000 )
1645 msg_Dbg( p_stream, "dropping frame (%i)",
1646 (int)(i_video_drift - i_master_drift) );
1648 p_pic->pf_release( p_pic );
1651 else if( i_video_drift > i_master_drift + 50000 )
1654 msg_Dbg( p_stream, "adding frame (%i)",
1655 (int)(i_video_drift - i_master_drift) );
1661 if( !id->p_encoder->p_module )
1663 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1665 p_pic->pf_release( p_pic );
1666 transcode_video_close( p_stream, id );
1667 id->b_transcode = VLC_FALSE;
1668 return VLC_EGENERIC;
1672 if( p_stream->p_sys->b_deinterlace )
1674 id->pp_filter[id->i_filter] =
1675 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1676 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1678 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1679 video_new_buffer_filter;
1680 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1681 video_del_buffer_filter;
1683 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1684 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
1685 id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
1686 id->pp_filter[id->i_filter]->p_module =
1687 module_Need( id->pp_filter[id->i_filter],
1688 "video filter2", p_sys->psz_deinterlace, 0 );
1689 if( id->pp_filter[id->i_filter]->p_module )
1691 id->pp_filter[id->i_filter]->p_owner =
1692 malloc( sizeof(filter_owner_sys_t) );
1693 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1694 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1695 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1701 msg_Dbg( p_stream, "no video filter found" );
1702 vlc_object_detach( id->pp_filter[id->i_filter] );
1703 vlc_object_destroy( id->pp_filter[id->i_filter] );
1707 /* Check if we need a filter for chroma conversion or resizing */
1708 if( id->p_decoder->fmt_out.video.i_chroma !=
1709 id->p_encoder->fmt_in.video.i_chroma ||
1710 id->p_decoder->fmt_out.video.i_width !=
1711 id->p_encoder->fmt_out.video.i_width ||
1712 id->p_decoder->fmt_out.video.i_height !=
1713 id->p_encoder->fmt_out.video.i_height ||
1714 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1715 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1717 id->pp_filter[id->i_filter] =
1718 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1719 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1721 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1722 video_new_buffer_filter;
1723 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1724 video_del_buffer_filter;
1726 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1727 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1728 id->pp_filter[id->i_filter]->p_cfg = NULL;
1729 id->pp_filter[id->i_filter]->p_module =
1730 module_Need( id->pp_filter[id->i_filter],
1731 "video filter2", 0, 0 );
1732 if( id->pp_filter[id->i_filter]->p_module )
1734 id->pp_filter[id->i_filter]->p_owner =
1735 malloc( sizeof(filter_owner_sys_t) );
1736 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1737 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1738 id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
1744 msg_Dbg( p_stream, "no video filter found" );
1745 vlc_object_detach( id->pp_filter[id->i_filter] );
1746 vlc_object_destroy( id->pp_filter[id->i_filter] );
1748 p_pic->pf_release( p_pic );
1749 transcode_video_close( p_stream, id );
1750 id->b_transcode = VLC_FALSE;
1751 return VLC_EGENERIC;
1755 for( i = 0; i < p_sys->i_vfilters; i++ )
1757 id->pp_vfilter[id->i_vfilter] =
1758 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1759 vlc_object_attach( id->pp_vfilter[id->i_vfilter], p_stream );
1761 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_new =
1762 video_new_buffer_filter;
1763 id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_del =
1764 video_del_buffer_filter;
1766 id->pp_vfilter[id->i_vfilter]->fmt_in = id->p_encoder->fmt_in;
1767 id->pp_vfilter[id->i_vfilter]->fmt_out = id->p_encoder->fmt_in;
1768 id->pp_vfilter[id->i_vfilter]->p_cfg = p_sys->p_vfilters_cfg[i];
1769 id->pp_vfilter[id->i_vfilter]->p_module =
1770 module_Need( id->pp_vfilter[id->i_vfilter],
1771 "video filter2", p_sys->psz_vfilters[i], 0 );
1772 if( id->pp_vfilter[id->i_vfilter]->p_module )
1774 id->pp_vfilter[id->i_vfilter]->p_owner =
1775 malloc( sizeof(filter_owner_sys_t) );
1776 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1777 id->pp_vfilter[id->i_vfilter]->p_owner->pp_pics[i] = 0;
1778 id->pp_vfilter[id->i_vfilter]->p_owner->p_sys = p_sys;
1784 msg_Dbg( p_stream, "no video filter found" );
1785 vlc_object_detach( id->pp_vfilter[id->i_vfilter] );
1786 vlc_object_destroy( id->pp_vfilter[id->i_vfilter] );
1791 /* Run filter chain */
1792 for( i = 0; i < id->i_filter; i++ )
1794 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1801 /* Check if we have a subpicture to overlay */
1804 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
1805 /* TODO: get another pic */
1808 /* Overlay subpicture */
1811 int i_scale_width, i_scale_height;
1812 video_format_t *p_fmt;
1814 i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
1815 id->p_decoder->fmt_out.video.i_width;
1816 i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
1817 id->p_decoder->fmt_out.video.i_height;
1819 if( p_pic->i_refcount && !id->i_filter )
1821 /* We can't modify the picture, we need to duplicate it */
1822 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1825 vout_CopyPicture( p_stream, p_tmp, p_pic );
1826 p_pic->pf_release( p_pic );
1832 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
1834 p_fmt = &id->p_decoder->fmt_out.video;
1836 /* FIXME (shouldn't have to be done here) */
1837 p_fmt->i_sar_num = p_fmt->i_aspect *
1838 p_fmt->i_height / p_fmt->i_width;
1839 p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
1841 spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
1842 i_scale_width, i_scale_height );
1845 /* Run vfilter chain */
1846 for( i = 0; i < id->i_vfilter; i++ )
1848 p_pic = id->pp_vfilter[i]->pf_video_filter(id->pp_vfilter[i], p_pic);
1851 if( p_sys->i_threads >= 1 )
1853 vlc_mutex_lock( &p_sys->lock_out );
1854 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1855 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1856 *out = p_sys->p_buffers;
1857 p_sys->p_buffers = NULL;
1858 vlc_cond_signal( &p_sys->cond );
1859 vlc_mutex_unlock( &p_sys->lock_out );
1864 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1865 block_ChainAppend( out, p_block );
1867 if( p_sys->b_master_sync )
1868 date_Increment( &id->interpolated_pts, 1 );
1870 if( p_sys->b_master_sync && i_duplicate > 1 )
1872 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1873 date_Increment( &id->interpolated_pts, 1 );
1874 p_pic->date = i_pts;
1875 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1876 block_ChainAppend( out, p_block );
1879 p_pic->pf_release( p_pic );
1886 static int EncoderThread( sout_stream_sys_t *p_sys )
1888 sout_stream_id_t *id = p_sys->id_video;
1891 while( !p_sys->b_die && !p_sys->b_error )
1895 vlc_mutex_lock( &p_sys->lock_out );
1896 while( p_sys->i_last_pic == p_sys->i_first_pic )
1898 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1899 if( p_sys->b_die || p_sys->b_error ) break;
1901 if( p_sys->b_die || p_sys->b_error )
1903 vlc_mutex_unlock( &p_sys->lock_out );
1907 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1908 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1909 vlc_mutex_unlock( &p_sys->lock_out );
1911 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1912 vlc_mutex_lock( &p_sys->lock_out );
1913 block_ChainAppend( &p_sys->p_buffers, p_block );
1915 if( p_sys->b_master_sync )
1916 date_Increment( &id->interpolated_pts, 1 );
1919 if( p_sys->b_master_sync && i_duplicate > 1 )
1921 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1922 date_Increment( &id->interpolated_pts, 1 );
1923 p_pic->date = i_pts;
1924 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1925 block_ChainAppend( &p_sys->p_buffers, p_block );
1928 vlc_mutex_unlock( &p_sys->lock_out );
1930 p_pic->pf_release( p_pic );
1933 while( p_sys->i_last_pic != p_sys->i_first_pic )
1935 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1936 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1938 p_pic->pf_release( p_pic );
1941 block_ChainRelease( p_sys->p_buffers );
1946 struct picture_sys_t
1948 vlc_object_t *p_owner;
1951 static void video_release_buffer( picture_t *p_pic )
1953 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
1955 video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
1957 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
1960 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
1961 sout_stream_sys_t *p_sys )
1963 decoder_t *p_dec = (decoder_t *)p_this;
1967 /* Find an empty space in the picture ring buffer */
1968 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1970 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
1972 pp_ring[i]->i_status = RESERVED_PICTURE;
1976 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1978 if( pp_ring[i] == 0 ) break;
1981 if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
1983 int i_first_pic = p_sys->i_first_pic;
1985 if( p_sys->i_first_pic != p_sys->i_last_pic )
1987 /* Encoder still has stuff to encode, wait to clear-up the list */
1988 while( p_sys->i_first_pic == i_first_pic )
1992 /* Find an empty space in the picture ring buffer */
1993 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1995 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
1997 pp_ring[i]->i_status = RESERVED_PICTURE;
2001 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2003 if( pp_ring[i] == 0 ) break;
2007 if( i == PICTURE_RING_SIZE )
2009 msg_Err( p_this, "decoder/filter is leaking pictures, "
2010 "resetting its ring buffer" );
2012 for( i = 0; i < PICTURE_RING_SIZE; i++ )
2014 pp_ring[i]->pf_release( pp_ring[i] );
2020 p_pic = malloc( sizeof(picture_t) );
2021 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2022 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
2023 p_dec->fmt_out.video.i_chroma,
2024 p_dec->fmt_out.video.i_width,
2025 p_dec->fmt_out.video.i_height,
2026 p_dec->fmt_out.video.i_aspect );
2028 if( !p_pic->i_planes )
2034 p_pic->pf_release = video_release_buffer;
2035 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2036 p_pic->p_sys->p_owner = p_this;
2037 p_pic->i_status = RESERVED_PICTURE;
2044 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
2046 return video_new_buffer( VLC_OBJECT(p_dec),
2047 p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2050 static picture_t *video_new_buffer_filter( filter_t *p_filter )
2052 return video_new_buffer( VLC_OBJECT(p_filter),
2053 p_filter->p_owner->pp_pics,
2054 p_filter->p_owner->p_sys );
2057 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2059 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2060 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
2061 if( p_pic ) free( p_pic );
2064 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
2066 p_pic->i_refcount = 0;
2067 p_pic->i_status = DESTROYED_PICTURE;
2070 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
2072 p_pic->i_refcount = 0;
2073 p_pic->i_status = DESTROYED_PICTURE;
2076 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2078 p_pic->i_refcount++;
2081 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2083 video_release_buffer( p_pic );
2089 static subpicture_t *spu_new_buffer( decoder_t * );
2090 static void spu_del_buffer( decoder_t *, subpicture_t * );
2092 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2094 sout_stream_sys_t *p_sys = p_stream->p_sys;
2100 /* Initialization of decoder structures */
2101 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2102 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2103 id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2104 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
2106 id->p_decoder->p_module =
2107 module_Need( id->p_decoder, "decoder", "$codec", 0 );
2109 if( !id->p_decoder->p_module )
2111 msg_Err( p_stream, "cannot find decoder" );
2112 return VLC_EGENERIC;
2115 if( !p_sys->b_soverlay )
2121 /* Initialization of encoder format structures */
2122 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
2123 id->p_decoder->fmt_in.i_codec );
2125 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2127 id->p_encoder->p_module =
2128 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2130 if( !id->p_encoder->p_module )
2132 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2133 msg_Err( p_stream, "cannot find encoder" );
2134 return VLC_EGENERIC;
2140 p_sys->p_spu = spu_Create( p_stream );
2141 spu_Init( p_sys->p_spu );
2147 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2150 if( id->p_decoder->p_module )
2151 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2154 if( id->p_encoder->p_module )
2155 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2158 static int transcode_spu_process( sout_stream_t *p_stream,
2159 sout_stream_id_t *id,
2160 block_t *in, block_t **out )
2162 sout_stream_sys_t *p_sys = p_stream->p_sys;
2163 subpicture_t *p_subpic;
2166 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2167 if( !p_subpic ) return VLC_EGENERIC;
2169 if( p_sys->b_master_sync && p_sys->i_master_drift )
2171 p_subpic->i_start -= p_sys->i_master_drift;
2172 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2175 if( p_sys->b_soverlay )
2177 spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2183 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2184 spu_del_buffer( id->p_decoder, p_subpic );
2188 block_ChainAppend( out, p_block );
2193 return VLC_EGENERIC;
2196 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2198 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2199 return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2202 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2204 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2205 spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );