1 /*****************************************************************************
2 * transcode.c: transcoding stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
35 #include <vlc/decoder.h>
36 #include "vlc_filter.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 #define VENC_TEXT N_("Video encoder")
43 #define VENC_LONGTEXT N_( \
44 "Allows you to specify the video encoder to use and its associated " \
46 #define VCODEC_TEXT N_("Destination video codec")
47 #define VCODEC_LONGTEXT N_( \
48 "Allows you to specify the destination video codec used for the " \
50 #define VB_TEXT N_("Video bitrate")
51 #define VB_LONGTEXT N_( \
52 "Allows you to specify the video bitrate used for the streaming " \
54 #define SCALE_TEXT N_("Video scaling")
55 #define SCALE_LONGTEXT N_( \
56 "Allows you to scale the video before encoding." )
57 #define FPS_TEXT N_("Video frame-rate")
58 #define FPS_LONGTEXT N_( \
59 "Allows you to specify an output frame rate for the video." )
60 #define DEINTERLACE_TEXT N_("Deinterlace video")
61 #define DEINTERLACE_LONGTEXT N_( \
62 "Allows you to deinterlace the video before encoding." )
63 #define WIDTH_TEXT N_("Video width")
64 #define WIDTH_LONGTEXT N_( \
65 "Allows you to specify the output video width." )
66 #define HEIGHT_TEXT N_("Video height")
67 #define HEIGHT_LONGTEXT N_( \
68 "Allows you to specify the output video height." )
70 #define CROPTOP_TEXT N_("Video crop top")
71 #define CROPTOP_LONGTEXT N_( \
72 "Allows you to specify the top coordinate for the video cropping." )
73 #define CROPLEFT_TEXT N_("Video crop left")
74 #define CROPLEFT_LONGTEXT N_( \
75 "Allows you to specify the left coordinate for the video cropping." )
76 #define CROPBOTTOM_TEXT N_("Video crop bottom")
77 #define CROPBOTTOM_LONGTEXT N_( \
78 "Allows you to specify the bottom coordinate for the video cropping." )
79 #define CROPRIGHT_TEXT N_("Video crop right")
80 #define CROPRIGHT_LONGTEXT N_( \
81 "Allows you to specify the right coordinate for the video cropping." )
83 #define AENC_TEXT N_("Audio encoder")
84 #define AENC_LONGTEXT N_( \
85 "Allows you to specify the audio encoder to use and its associated " \
87 #define ACODEC_TEXT N_("Destination audio codec")
88 #define ACODEC_LONGTEXT N_( \
89 "Allows you to specify the destination audio codec used for the " \
91 #define AB_TEXT N_("Audio bitrate")
92 #define AB_LONGTEXT N_( \
93 "Allows you to specify the audio bitrate used for the streaming " \
95 #define ARATE_TEXT N_("Audio sample rate")
96 #define ARATE_LONGTEXT N_( \
97 "Allows you to specify the audio sample rate used for the streaming " \
99 #define ACHANS_TEXT N_("Audio channels")
100 #define ACHANS_LONGTEXT N_( \
101 "Allows you to specify the number of audio channels used for the " \
102 "streaming output." )
104 #define SENC_TEXT N_("Subtitles encoder")
105 #define SENC_LONGTEXT N_( \
106 "Allows you to specify the subtitles encoder to use and its associated " \
108 #define SCODEC_TEXT N_("Destination subtitles codec")
109 #define SCODEC_LONGTEXT N_( \
110 "Allows you to specify the destination subtitles codec used for the " \
111 "streaming output." )
112 #define SFILTER_TEXT N_("Subpictures filter")
113 #define SFILTER_LONGTEXT N_( \
114 "Allows you to specify subpictures filters used during the video " \
115 "transcoding. The subpictures produced by the filters will be overlayed " \
116 "directly onto the video." )
118 #define THREADS_TEXT N_("Number of threads")
119 #define THREADS_LONGTEXT N_( \
120 "Allows you to specify the number of threads used for the transcoding." )
122 #define ASYNC_TEXT N_("Synchronise on audio track")
123 #define ASYNC_LONGTEXT N_( \
124 "This option will drop/duplicate video frames to synchronise the video " \
125 "track on the audio track." )
127 static int Open ( vlc_object_t * );
128 static void Close( vlc_object_t * );
130 #define SOUT_CFG_PREFIX "sout-transcode-"
133 set_shortname( _("Transcode"));
134 set_description( _("Transcode stream output") );
135 set_capability( "sout stream", 50 );
136 add_shortcut( "transcode" );
137 set_callbacks( Open, Close );
138 set_category( CAT_SOUT );
139 set_subcategory( SUBCAT_SOUT_STREAM );
140 set_section( N_("Video"), NULL );
141 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
142 VENC_LONGTEXT, VLC_FALSE );
143 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
144 VCODEC_LONGTEXT, VLC_FALSE );
145 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
146 VB_LONGTEXT, VLC_FALSE );
147 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
148 SCALE_LONGTEXT, VLC_FALSE );
149 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
150 FPS_LONGTEXT, VLC_FALSE );
151 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
152 DEINTERLACE_LONGTEXT, VLC_FALSE );
153 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
154 WIDTH_LONGTEXT, VLC_TRUE );
155 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
156 HEIGHT_LONGTEXT, VLC_TRUE );
158 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
159 CROPTOP_LONGTEXT, VLC_TRUE );
160 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
161 CROPLEFT_LONGTEXT, VLC_TRUE );
162 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
163 CROPBOTTOM_LONGTEXT, VLC_TRUE );
164 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
165 CROPRIGHT_LONGTEXT, VLC_TRUE );
167 set_section( N_("Audio"), NULL );
168 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
169 AENC_LONGTEXT, VLC_FALSE );
170 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
171 ACODEC_LONGTEXT, VLC_FALSE );
172 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
173 AB_LONGTEXT, VLC_FALSE );
174 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
175 ACHANS_LONGTEXT, VLC_FALSE );
176 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
177 ARATE_LONGTEXT, VLC_TRUE );
178 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
179 ASYNC_LONGTEXT, VLC_FALSE );
181 set_section( N_("Overlays/Subtitles"), NULL );
182 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
183 SENC_LONGTEXT, VLC_FALSE );
184 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
185 SCODEC_LONGTEXT, VLC_FALSE );
186 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
187 SCODEC_LONGTEXT, VLC_FALSE );
188 add_module_list_cat( SOUT_CFG_PREFIX "sfilter", SUBCAT_VIDEO_SUBPIC,
190 SFILTER_TEXT, SFILTER_LONGTEXT, VLC_FALSE );
192 set_section( N_("Miscellaneous"), NULL );
193 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
194 THREADS_LONGTEXT, VLC_TRUE );
198 static const char *ppsz_sout_options[] = {
199 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
200 "scale", "fps", "width", "height", "deinterlace", "threads",
201 "aenc", "acodec", "ab", "samplerate", "channels",
202 "senc", "scodec", "soverlay", "sfilter",
206 /*****************************************************************************
207 * Exported prototypes
208 *****************************************************************************/
209 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
210 static int Del ( sout_stream_t *, sout_stream_id_t * );
211 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
213 static int transcode_audio_new ( sout_stream_t *, sout_stream_id_t * );
214 static void transcode_audio_close ( sout_stream_t *, sout_stream_id_t * );
215 static int transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
216 block_t *, block_t ** );
218 static aout_buffer_t *audio_new_buffer( decoder_t *, int );
219 static void audio_del_buffer( decoder_t *, aout_buffer_t * );
221 static int transcode_video_new ( sout_stream_t *, sout_stream_id_t * );
222 static void transcode_video_close ( sout_stream_t *, sout_stream_id_t * );
223 static int transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
224 static int transcode_video_process( sout_stream_t *, sout_stream_id_t *,
225 block_t *, block_t ** );
227 static void video_del_buffer( vlc_object_t *, picture_t * );
228 static picture_t *video_new_buffer_decoder( decoder_t * );
229 static void video_del_buffer_decoder( decoder_t *, picture_t * );
230 static void video_link_picture_decoder( decoder_t *, picture_t * );
231 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
232 static picture_t *video_new_buffer_filter( filter_t * );
233 static void video_del_buffer_filter( filter_t *, picture_t * );
235 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
236 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
237 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
238 block_t *, block_t ** );
240 static int EncoderThread( struct sout_stream_sys_t * p_sys );
242 static int pi_channels_maps[6] =
245 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
246 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
247 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
248 | AOUT_CHAN_REARRIGHT,
249 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
250 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
253 #define PICTURE_RING_SIZE 64
254 #define SUBPICTURE_RING_SIZE 20
256 struct sout_stream_sys_t
260 sout_stream_t *p_out;
261 sout_stream_id_t *id_video;
263 vlc_mutex_t lock_out;
265 picture_t * pp_pics[PICTURE_RING_SIZE];
266 int i_first_pic, i_last_pic;
269 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
271 sout_cfg_t *p_audio_cfg;
277 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
279 sout_cfg_t *p_video_cfg;
285 vlc_bool_t b_deinterlace;
294 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
296 vlc_bool_t b_soverlay;
297 sout_cfg_t *p_spu_cfg;
301 vlc_bool_t b_master_sync;
302 mtime_t i_master_drift;
305 struct decoder_owner_sys_t
307 picture_t *pp_pics[PICTURE_RING_SIZE];
309 struct filter_owner_sys_t
311 picture_t *pp_pics[PICTURE_RING_SIZE];
314 /*****************************************************************************
316 *****************************************************************************/
317 static int Open( vlc_object_t *p_this )
319 sout_stream_t *p_stream = (sout_stream_t*)p_this;
320 sout_stream_sys_t *p_sys;
323 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
325 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
328 msg_Err( p_stream, "cannot create chain" );
329 vlc_object_destroy( p_sys );
333 p_sys->i_master_drift = 0;
335 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
338 /* Audio transcoding parameters */
339 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
340 p_sys->psz_aenc = NULL;
341 p_sys->p_audio_cfg = NULL;
342 if( val.psz_string && *val.psz_string )
345 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
347 if( psz_next ) free( psz_next );
349 if( val.psz_string ) free( val.psz_string );
351 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
353 if( val.psz_string && *val.psz_string )
356 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
357 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
359 if( val.psz_string ) free( val.psz_string );
361 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
362 p_sys->i_abitrate = val.i_int;
363 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
365 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
366 p_sys->i_sample_rate = val.i_int;
368 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
369 p_sys->i_channels = val.i_int;
371 if( p_sys->i_acodec )
373 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
374 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
375 p_sys->i_channels, p_sys->i_abitrate / 1000 );
378 /* Video transcoding parameters */
379 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
380 p_sys->psz_venc = NULL;
381 p_sys->p_video_cfg = NULL;
382 if( val.psz_string && *val.psz_string )
385 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
387 if( psz_next ) free( psz_next );
389 if( val.psz_string ) free( val.psz_string );
391 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
393 if( val.psz_string && *val.psz_string )
396 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
397 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
399 if( val.psz_string ) free( val.psz_string );
401 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
402 p_sys->i_vbitrate = val.i_int;
403 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
405 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
406 p_sys->f_scale = val.f_float;
408 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
409 p_sys->f_fps = val.f_float;
411 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
412 p_sys->i_width = val.i_int;
414 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
415 p_sys->i_height = val.i_int;
417 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
418 p_sys->b_deinterlace = val.b_bool;
420 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
421 p_sys->i_crop_top = val.i_int;
422 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
423 p_sys->i_crop_bottom = val.i_int;
424 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
425 p_sys->i_crop_left = val.i_int;
426 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
427 p_sys->i_crop_right = val.i_int;
429 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
430 p_sys->i_threads = val.i_int;
432 if( p_sys->i_vcodec )
434 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
435 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
436 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
439 /* Subpictures transcoding parameters */
441 p_sys->psz_senc = NULL;
442 p_sys->p_spu_cfg = NULL;
445 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
446 if( val.psz_string && *val.psz_string )
449 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
451 if( psz_next ) free( psz_next );
453 if( val.psz_string ) free( val.psz_string );
455 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
456 if( val.psz_string && *val.psz_string )
459 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
460 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
462 if( val.psz_string ) free( val.psz_string );
464 if( p_sys->i_scodec )
466 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
469 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
470 p_sys->b_soverlay = val.b_bool;
472 var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
473 if( val.psz_string && *val.psz_string )
475 p_sys->p_spu = spu_Create( p_stream );
476 var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
477 var_Set( p_sys->p_spu, "sub-filter", val );
478 spu_Init( p_sys->p_spu );
480 if( val.psz_string ) free( val.psz_string );
482 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
483 p_sys->b_master_sync = val.b_bool;
484 if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
486 p_stream->pf_add = Add;
487 p_stream->pf_del = Del;
488 p_stream->pf_send = Send;
489 p_stream->p_sys = p_sys;
494 /*****************************************************************************
496 *****************************************************************************/
497 static void Close( vlc_object_t * p_this )
499 sout_stream_t *p_stream = (sout_stream_t*)p_this;
500 sout_stream_sys_t *p_sys = p_stream->p_sys;
502 sout_StreamDelete( p_sys->p_out );
504 while( p_sys->p_audio_cfg != NULL )
506 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
508 if( p_sys->p_audio_cfg->psz_name )
509 free( p_sys->p_audio_cfg->psz_name );
510 if( p_sys->p_audio_cfg->psz_value )
511 free( p_sys->p_audio_cfg->psz_value );
512 free( p_sys->p_audio_cfg );
514 p_sys->p_audio_cfg = p_next;
516 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
518 while( p_sys->p_video_cfg != NULL )
520 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
522 if( p_sys->p_video_cfg->psz_name )
523 free( p_sys->p_video_cfg->psz_name );
524 if( p_sys->p_video_cfg->psz_value )
525 free( p_sys->p_video_cfg->psz_value );
526 free( p_sys->p_video_cfg );
528 p_sys->p_video_cfg = p_next;
530 if( p_sys->psz_venc ) free( p_sys->psz_venc );
532 while( p_sys->p_spu_cfg != NULL )
534 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
536 if( p_sys->p_spu_cfg->psz_name )
537 free( p_sys->p_spu_cfg->psz_name );
538 if( p_sys->p_spu_cfg->psz_value )
539 free( p_sys->p_spu_cfg->psz_value );
540 free( p_sys->p_spu_cfg );
542 p_sys->p_spu_cfg = p_next;
544 if( p_sys->psz_senc ) free( p_sys->psz_senc );
546 if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
548 vlc_object_destroy( p_sys );
551 struct sout_stream_id_t
553 vlc_fourcc_t b_transcode;
555 /* id of the out stream */
559 decoder_t *p_decoder;
562 filter_t *pp_filter[10];
566 encoder_t *p_encoder;
569 date_t interpolated_pts;
573 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
575 sout_stream_sys_t *p_sys = p_stream->p_sys;
576 sout_stream_id_t *id;
578 id = malloc( sizeof( sout_stream_id_t ) );
579 memset( id, 0, sizeof(sout_stream_id_t) );
582 id->p_decoder = NULL;
583 id->p_encoder = NULL;
585 /* Create decoder object */
586 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
589 msg_Err( p_stream, "out of memory" );
592 vlc_object_attach( id->p_decoder, p_stream );
593 id->p_decoder->p_module = NULL;
594 id->p_decoder->fmt_in = *p_fmt;
595 id->p_decoder->fmt_out = *p_fmt;
596 id->p_decoder->fmt_out.i_extra = 0;
597 id->p_decoder->fmt_out.p_extra = 0;
598 id->p_decoder->b_pace_control = VLC_TRUE;
600 /* Create encoder object */
601 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
604 msg_Err( p_stream, "out of memory" );
607 vlc_object_attach( id->p_encoder, p_stream );
608 id->p_encoder->p_module = NULL;
610 /* Create destination format */
611 es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
612 id->p_encoder->fmt_out.i_id = p_fmt->i_id;
613 id->p_encoder->fmt_out.i_group = p_fmt->i_group;
614 if( p_fmt->psz_language )
615 id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
617 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
620 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
621 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
623 /* Complete destination format */
624 id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
625 id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
626 p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
627 id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
628 p_sys->i_channels : p_fmt->audio.i_channels;
629 id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
630 id->p_encoder->fmt_out.audio.i_bitspersample =
631 p_fmt->audio.i_bitspersample;
633 /* Build decoder -> filter -> encoder chain */
634 if( transcode_audio_new( p_stream, id ) )
636 msg_Err( p_stream, "cannot create audio chain" );
640 /* Open output stream */
641 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
642 id->b_transcode = VLC_TRUE;
644 if( !id->id ) goto error;
646 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
648 else if( p_fmt->i_cat == VIDEO_ES &&
649 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
652 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
653 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
655 /* Complete destination format */
656 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
657 id->p_encoder->fmt_out.video.i_width = p_sys->i_width;
658 id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
659 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
661 /* Build decoder -> filter -> encoder chain */
662 if( transcode_video_new( p_stream, id ) )
664 msg_Err( p_stream, "cannot create video chain" );
668 /* Stream will be added later on because we don't know
669 * all the characteristics of the decoded stream yet */
670 id->b_transcode = VLC_TRUE;
672 if( p_sys->f_fps > 0 )
674 id->p_encoder->fmt_out.video.i_frame_rate = p_sys->f_fps * 1000;
675 id->p_encoder->fmt_out.video.i_frame_rate_base = 1000;
678 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
680 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
681 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
682 (char*)&p_sys->i_scodec );
684 /* Complete destination format */
685 id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
687 /* build decoder -> filter -> encoder */
688 if( transcode_spu_new( p_stream, id ) )
690 msg_Err( p_stream, "cannot create subtitles chain" );
694 /* open output stream */
695 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
696 id->b_transcode = VLC_TRUE;
698 if( !id->id ) goto error;
700 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
702 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
703 (char*)&p_fmt->i_codec );
705 id->b_transcode = VLC_TRUE;
707 /* Build decoder -> filter -> overlaying chain */
708 if( transcode_spu_new( p_stream, id ) )
710 msg_Err( p_stream, "cannot create subtitles chain" );
716 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
717 (char*)&p_fmt->i_codec );
718 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
719 id->b_transcode = VLC_FALSE;
721 if( !id->id ) goto error;
729 vlc_object_detach( id->p_decoder );
730 vlc_object_destroy( id->p_decoder );
735 vlc_object_detach( id->p_encoder );
736 vlc_object_destroy( id->p_encoder );
743 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
745 sout_stream_sys_t *p_sys = p_stream->p_sys;
747 if( id->b_transcode )
749 switch( id->p_decoder->fmt_in.i_cat )
752 transcode_audio_close( p_stream, id );
755 transcode_video_close( p_stream, id );
758 transcode_spu_close( p_stream, id );
763 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
767 vlc_object_detach( id->p_decoder );
768 vlc_object_destroy( id->p_decoder );
773 vlc_object_detach( id->p_encoder );
774 vlc_object_destroy( id->p_encoder );
782 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
785 sout_stream_sys_t *p_sys = p_stream->p_sys;
788 if( !id->b_transcode && id->id )
790 if( p_sys->b_master_sync && p_sys->i_master_drift )
792 if( p_buffer->i_dts > 0 )
794 p_buffer->i_dts -= p_sys->i_master_drift;
795 if( p_buffer->i_dts < 0 )
797 block_Release( p_buffer );
801 if( p_buffer->i_pts > 0 )
803 p_buffer->i_pts -= p_sys->i_master_drift;
804 if( p_buffer->i_pts < 0 )
806 block_Release( p_buffer );
812 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
814 else if( !id->b_transcode )
816 block_Release( p_buffer );
820 switch( id->p_decoder->fmt_in.i_cat )
823 transcode_audio_process( p_stream, id, p_buffer, &p_out );
827 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
835 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
843 block_Release( p_buffer );
847 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
851 /****************************************************************************
852 * decoder reencoder part
853 ****************************************************************************/
854 int audio_BitsPerSample( vlc_fourcc_t i_format )
858 case VLC_FOURCC('u','8',' ',' '):
859 case VLC_FOURCC('s','8',' ',' '):
862 case VLC_FOURCC('u','1','6','l'):
863 case VLC_FOURCC('s','1','6','l'):
864 case VLC_FOURCC('u','1','6','b'):
865 case VLC_FOURCC('s','1','6','b'):
868 case VLC_FOURCC('u','2','4','l'):
869 case VLC_FOURCC('s','2','4','l'):
870 case VLC_FOURCC('u','2','4','b'):
871 case VLC_FOURCC('s','2','4','b'):
874 case VLC_FOURCC('u','3','2','l'):
875 case VLC_FOURCC('s','3','2','l'):
876 case VLC_FOURCC('u','3','2','b'):
877 case VLC_FOURCC('s','3','2','b'):
878 case VLC_FOURCC('f','l','3','2'):
879 case VLC_FOURCC('f','i','3','2'):
882 case VLC_FOURCC('f','l','6','4'):
889 static int transcode_audio_new( sout_stream_t *p_stream,
890 sout_stream_id_t *id )
892 sout_stream_sys_t *p_sys = p_stream->p_sys;
893 es_format_t fmt_last;
899 /* Initialization of decoder structures */
900 id->p_decoder->pf_decode_audio = 0;
901 id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
902 id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
903 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
905 id->p_decoder->p_module =
906 module_Need( id->p_decoder, "decoder", "$codec", 0 );
908 if( !id->p_decoder->p_module )
910 msg_Err( p_stream, "cannot find decoder" );
913 id->p_decoder->fmt_out.audio.i_bitspersample =
914 audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
915 fmt_last = id->p_decoder->fmt_out;
916 /* FIX decoders so we don't have to do this */
917 fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
923 /* Initialization of encoder format structures */
924 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
925 id->p_decoder->fmt_out.i_codec );
926 id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
928 /* Sanity check for audio channels */
929 id->p_encoder->fmt_out.audio.i_channels =
930 __MIN( id->p_encoder->fmt_out.audio.i_channels,
931 id->p_decoder->fmt_out.audio.i_channels );
932 if( id->p_decoder->fmt_out.audio.i_channels ==
933 id->p_encoder->fmt_out.audio.i_channels )
934 id->p_encoder->fmt_out.audio.i_physical_channels =
935 id->p_encoder->fmt_out.audio.i_original_channels =
936 id->p_decoder->fmt_out.audio.i_physical_channels;
938 id->p_encoder->fmt_out.audio.i_physical_channels =
939 id->p_encoder->fmt_out.audio.i_original_channels =
940 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
942 /* Initialization of encoder format structures */
943 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
944 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
945 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
946 id->p_encoder->fmt_in.audio.i_physical_channels =
947 id->p_encoder->fmt_in.audio.i_original_channels =
948 id->p_encoder->fmt_out.audio.i_physical_channels;
949 id->p_encoder->fmt_in.audio.i_channels =
950 id->p_encoder->fmt_out.audio.i_channels;
951 id->p_encoder->fmt_in.audio.i_bitspersample =
952 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
954 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
956 id->p_encoder->p_module =
957 module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
958 if( !id->p_encoder->p_module )
960 msg_Err( p_stream, "cannot find encoder" );
961 module_Unneed( id->p_decoder, id->p_decoder->p_module );
962 id->p_decoder->p_module = 0;
965 id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
966 id->p_encoder->fmt_in.audio.i_bitspersample =
967 audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
969 /* Check if we need a filter for the audio format conversion */
970 if( id->p_decoder->fmt_out.i_codec !=
971 id->p_encoder->fmt_in.i_codec )
974 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
975 vlc_object_attach( id->pp_filter[0], p_stream );
977 id->pp_filter[0]->pf_audio_buffer_new = __block_New;
979 id->pp_filter[0]->fmt_in = fmt_last;
980 id->pp_filter[0]->fmt_out = id->p_encoder->fmt_in;
981 id->pp_filter[0]->p_module =
982 module_Need( id->pp_filter[0], "audio filter2", 0, 0 );
983 if( id->pp_filter[0]->p_module ) id->i_filter++;
986 msg_Dbg( p_stream, "no audio filter found (%4.4s->%4.4s)",
987 (char *)&id->pp_filter[0]->fmt_in,
988 (char *)&id->pp_filter[0]->fmt_out );
989 vlc_object_detach( id->pp_filter[0] );
990 vlc_object_destroy( id->pp_filter[0] );
991 module_Unneed( id->p_decoder, id->p_decoder->p_module );
992 id->p_decoder->p_module = 0;
993 module_Unneed( id->p_encoder, id->p_encoder->p_module );
994 id->p_encoder->p_module = 0;
998 id->pp_filter[0]->fmt_out.audio.i_bitspersample =
999 audio_BitsPerSample( id->pp_filter[0]->fmt_out.i_codec );
1000 fmt_last = id->pp_filter[0]->fmt_out;
1002 /* Try a 2 stage conversion */
1003 if( id->pp_filter[0]->fmt_out.i_codec !=
1004 id->p_encoder->fmt_in.i_codec )
1007 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1008 vlc_object_attach( id->pp_filter[1], p_stream );
1010 id->pp_filter[1]->pf_audio_buffer_new = __block_New;
1012 id->pp_filter[1]->fmt_in = id->pp_filter[0]->fmt_out;
1013 id->pp_filter[1]->fmt_out = id->p_encoder->fmt_in;
1014 id->pp_filter[1]->p_module =
1015 module_Need( id->pp_filter[1], "audio filter2", 0, 0 );
1016 if( !id->pp_filter[1]->p_module ||
1017 id->pp_filter[1]->fmt_out.i_codec !=
1018 id->p_encoder->fmt_in.i_codec )
1020 msg_Dbg( p_stream, "no audio filter found (%4.4s->%4.4s)",
1021 (char *)&id->pp_filter[1]->fmt_in,
1022 (char *)&id->pp_filter[1]->fmt_out );
1023 module_Unneed( id->pp_filter[0], id->pp_filter[0]->p_module );
1024 vlc_object_detach( id->pp_filter[0] );
1025 vlc_object_destroy( id->pp_filter[0] );
1026 if( id->pp_filter[1]->p_module )
1027 module_Unneed( id->pp_filter[0], id->pp_filter[0]->p_module );
1028 vlc_object_detach( id->pp_filter[1] );
1029 vlc_object_destroy( id->pp_filter[1] );
1030 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1031 id->p_decoder->p_module = 0;
1032 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1033 id->p_encoder->p_module = 0;
1034 return VLC_EGENERIC;
1036 else id->i_filter++;
1038 fmt_last = id->pp_filter[1]->fmt_out;
1042 /* Check if we need a filter for channel downmixing */
1043 if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1045 id->pp_filter[id->i_filter] =
1046 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1047 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1049 id->pp_filter[id->i_filter]->pf_audio_buffer_new = __block_New;
1051 id->pp_filter[id->i_filter]->fmt_in = fmt_last;
1052 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1053 id->pp_filter[id->i_filter]->fmt_out.audio.i_rate =
1054 fmt_last.audio.i_rate;
1056 id->pp_filter[id->i_filter]->p_module =
1057 module_Need( id->pp_filter[id->i_filter], "audio filter2", 0, 0 );
1058 if( id->pp_filter[id->i_filter]->p_module )
1060 id->pp_filter[id->i_filter]->fmt_out.audio.i_bitspersample =
1061 audio_BitsPerSample( id->pp_filter[id->i_filter]->fmt_out.i_codec );
1062 fmt_last = id->pp_filter[id->i_filter]->fmt_out;
1068 msg_Dbg( p_stream, "no audio filter found for mixing from"
1069 " %i to %i channels",
1070 id->pp_filter[id->i_filter]->fmt_in.audio.i_channels,
1071 id->pp_filter[id->i_filter]->fmt_out.audio.i_channels );
1072 vlc_object_detach( id->pp_filter[id->i_filter] );
1073 vlc_object_destroy( id->pp_filter[id->i_filter] );
1074 id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
1075 id->p_encoder->fmt_out.audio.i_channels =fmt_last.audio.i_channels;
1077 id->p_encoder->fmt_in.audio.i_physical_channels =
1078 id->p_encoder->fmt_in.audio.i_original_channels =
1079 fmt_last.audio.i_physical_channels;
1080 id->p_encoder->fmt_out.audio.i_physical_channels =
1081 id->p_encoder->fmt_out.audio.i_original_channels =
1082 fmt_last.audio.i_physical_channels;
1086 /* Check if we need a filter for the sampling rate conversion */
1087 if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
1089 id->pp_filter[id->i_filter] =
1090 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1091 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1093 id->pp_filter[id->i_filter]->pf_audio_buffer_new = __block_New;
1095 id->pp_filter[id->i_filter]->fmt_in = fmt_last;
1096 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1097 id->pp_filter[id->i_filter]->p_module =
1098 module_Need( id->pp_filter[id->i_filter], "audio filter2", 0, 0 );
1099 if( id->pp_filter[id->i_filter]->p_module )
1101 id->pp_filter[id->i_filter]->fmt_out.audio.i_bitspersample =
1102 audio_BitsPerSample( id->pp_filter[id->i_filter]->fmt_out.i_codec );
1103 fmt_last = id->pp_filter[id->i_filter]->fmt_out;
1109 msg_Dbg( p_stream, "no audio filter found resampling from"
1111 id->pp_filter[id->i_filter]->fmt_in.audio.i_rate,
1112 id->pp_filter[id->i_filter]->fmt_out.audio.i_rate );
1113 vlc_object_detach( id->pp_filter[id->i_filter] );
1114 vlc_object_destroy( id->pp_filter[id->i_filter] );
1115 id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
1116 id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1120 /* FIXME: Hack for mp3 transcoding support */
1121 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1122 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
1127 static void transcode_audio_close( sout_stream_t *p_stream,
1128 sout_stream_id_t *id )
1133 if( id->p_decoder->p_module )
1134 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1137 if( id->p_encoder->p_module )
1138 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1141 for( i = 0; i < id->i_filter; i++ )
1143 vlc_object_detach( id->pp_filter[i] );
1144 if( id->pp_filter[i]->p_module )
1145 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1146 vlc_object_destroy( id->pp_filter[i] );
1150 static int transcode_audio_process( sout_stream_t *p_stream,
1151 sout_stream_id_t *id,
1152 block_t *in, block_t **out )
1154 sout_stream_sys_t *p_sys = p_stream->p_sys;
1155 aout_buffer_t *p_audio_buf;
1156 block_t *p_block, *p_audio_block;
1160 while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
1163 if( p_sys->b_master_sync )
1165 mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1166 p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
1167 date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
1168 p_audio_buf->start_date -= p_sys->i_master_drift;
1169 p_audio_buf->end_date -= p_sys->i_master_drift;
1172 p_audio_block = p_audio_buf->p_sys;
1173 p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
1174 p_audio_block->i_dts = p_audio_block->i_pts =
1175 p_audio_buf->start_date;
1176 p_audio_block->i_length = p_audio_buf->end_date -
1177 p_audio_buf->start_date;
1178 p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1180 /* Run filter chain */
1181 for( i = 0; i < id->i_filter; i++ )
1184 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
1188 p_audio_buf->p_buffer = p_audio_block->p_buffer;
1189 p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
1190 p_audio_buf->i_nb_samples = p_audio_block->i_samples;
1191 p_audio_buf->start_date = p_audio_block->i_dts;
1192 p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1194 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
1195 block_ChainAppend( out, p_block );
1196 block_Release( p_audio_block );
1197 free( p_audio_buf );
1203 static void audio_release_buffer( aout_buffer_t *p_buffer )
1205 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1206 if( p_buffer ) free( p_buffer );
1209 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
1211 aout_buffer_t *p_buffer;
1215 if( p_dec->fmt_out.audio.i_bitspersample )
1217 i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1218 p_dec->fmt_out.audio.i_channels;
1220 else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
1221 p_dec->fmt_out.audio.i_frame_length )
1223 i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
1224 p_dec->fmt_out.audio.i_frame_length;
1228 i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
1231 p_buffer = malloc( sizeof(aout_buffer_t) );
1232 p_buffer->pf_release = audio_release_buffer;
1233 p_buffer->p_sys = p_block = block_New( p_dec, i_size );
1235 p_buffer->p_buffer = p_block->p_buffer;
1236 p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
1237 p_buffer->i_nb_samples = i_samples;
1238 p_block->i_samples = i_samples;
1243 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1245 if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
1246 if( p_buffer ) free( p_buffer );
1252 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1254 sout_stream_sys_t *p_sys = p_stream->p_sys;
1261 /* Initialization of decoder structures */
1262 id->p_decoder->pf_decode_video = 0;
1263 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
1264 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
1265 id->p_decoder->pf_picture_link = video_link_picture_decoder;
1266 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
1267 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
1268 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1269 id->p_decoder->p_owner->pp_pics[i] = 0;
1270 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1272 id->p_decoder->p_module =
1273 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1275 if( !id->p_decoder->p_module )
1277 msg_Err( p_stream, "cannot find decoder" );
1278 return VLC_EGENERIC;
1283 * Because some info about the decoded input will only be available
1284 * once the first frame is decoded, we actually only test the availability
1285 * of the encoder here.
1288 /* Initialization of encoder format structures */
1289 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1290 id->p_decoder->fmt_out.i_codec );
1291 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1293 /* The dimensions will be set properly later on.
1294 * Just put sensible values so we can test an encoder is available. */
1295 id->p_encoder->fmt_in.video.i_width =
1296 id->p_encoder->fmt_out.video.i_width ?
1297 id->p_encoder->fmt_out.video.i_width :
1298 id->p_decoder->fmt_in.video.i_width ?
1299 id->p_decoder->fmt_in.video.i_width : 16;
1300 id->p_encoder->fmt_in.video.i_height =
1301 id->p_encoder->fmt_out.video.i_height ?
1302 id->p_encoder->fmt_out.video.i_height :
1303 id->p_decoder->fmt_in.video.i_height ?
1304 id->p_decoder->fmt_in.video.i_height : 16;
1305 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1306 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1308 id->p_encoder->i_threads = p_sys->i_threads;
1309 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1311 id->p_encoder->p_module =
1312 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1313 if( !id->p_encoder->p_module )
1315 msg_Err( p_stream, "cannot find encoder" );
1316 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1317 id->p_decoder->p_module = 0;
1318 return VLC_EGENERIC;
1321 /* Close the encoder.
1322 * We'll open it only when we have the first frame. */
1323 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1324 id->p_encoder->p_module = NULL;
1326 if( p_sys->i_threads >= 1 )
1328 p_sys->id_video = id;
1329 vlc_mutex_init( p_stream, &p_sys->lock_out );
1330 vlc_cond_init( p_stream, &p_sys->cond );
1331 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1332 p_sys->i_first_pic = 0;
1333 p_sys->i_last_pic = 0;
1334 p_sys->p_buffers = NULL;
1335 p_sys->b_die = p_sys->b_error = 0;
1336 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1337 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1339 msg_Err( p_stream, "cannot spawn encoder thread" );
1340 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1341 id->p_decoder->p_module = 0;
1342 return VLC_EGENERIC;
1346 date_Set( &id->interpolated_pts, 0 );
1351 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1352 sout_stream_id_t *id )
1354 sout_stream_sys_t *p_sys = p_stream->p_sys;
1356 /* Hack because of the copy packetizer which can fail to detect the
1357 * proper size (which forces us to wait until the 1st frame
1359 int i_width = id->p_decoder->fmt_out.video.i_width -
1360 p_sys->i_crop_left - p_sys->i_crop_right;
1361 int i_height = id->p_decoder->fmt_out.video.i_height -
1362 p_sys->i_crop_top - p_sys->i_crop_bottom;
1364 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1365 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1367 /* Apply the scaling */
1368 id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1369 id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1371 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1372 id->p_encoder->fmt_out.video.i_height <= 0 )
1374 id->p_encoder->fmt_out.video.i_height =
1375 id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1377 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1378 id->p_encoder->fmt_out.video.i_height > 0 )
1380 id->p_encoder->fmt_out.video.i_width =
1381 id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1384 /* Make sure the size is at least a multiple of 2 */
1385 id->p_encoder->fmt_out.video.i_width =
1386 (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
1387 id->p_encoder->fmt_out.video.i_height =
1388 (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
1390 id->p_encoder->fmt_in.video.i_width =
1391 id->p_encoder->fmt_out.video.i_width;
1392 id->p_encoder->fmt_in.video.i_height =
1393 id->p_encoder->fmt_out.video.i_height;
1395 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1396 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1398 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1399 id->p_decoder->fmt_out.video.i_frame_rate_base )
1401 id->p_encoder->fmt_out.video.i_frame_rate =
1402 id->p_decoder->fmt_out.video.i_frame_rate;
1403 id->p_encoder->fmt_out.video.i_frame_rate_base =
1404 id->p_decoder->fmt_out.video.i_frame_rate_base;
1408 /* Pick a sensible default value */
1409 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1410 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1414 id->p_encoder->fmt_in.video.i_frame_rate =
1415 id->p_encoder->fmt_out.video.i_frame_rate;
1416 id->p_encoder->fmt_in.video.i_frame_rate_base =
1417 id->p_encoder->fmt_out.video.i_frame_rate_base;
1419 date_Init( &id->interpolated_pts,
1420 id->p_encoder->fmt_out.video.i_frame_rate,
1421 id->p_encoder->fmt_out.video.i_frame_rate_base );
1423 /* Check whether a particular aspect ratio was requested */
1424 if( !id->p_encoder->fmt_out.video.i_aspect )
1426 id->p_encoder->fmt_out.video.i_aspect =
1427 id->p_decoder->fmt_out.video.i_aspect;
1429 id->p_encoder->fmt_in.video.i_aspect =
1430 id->p_encoder->fmt_out.video.i_aspect;
1432 id->p_encoder->p_module =
1433 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1434 if( !id->p_encoder->p_module )
1436 msg_Err( p_stream, "cannot find encoder" );
1437 return VLC_EGENERIC;
1440 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1442 /* Hack for mp2v/mp1v transcoding support */
1443 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1444 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1446 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1449 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1450 &id->p_encoder->fmt_out );
1453 msg_Err( p_stream, "cannot add this stream" );
1454 return VLC_EGENERIC;
1460 static void transcode_video_close( sout_stream_t *p_stream,
1461 sout_stream_id_t *id )
1465 if( p_stream->p_sys->i_threads >= 1 )
1467 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1468 p_stream->p_sys->b_die = 1;
1469 vlc_cond_signal( &p_stream->p_sys->cond );
1470 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1471 vlc_thread_join( p_stream->p_sys );
1472 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1473 vlc_cond_destroy( &p_stream->p_sys->cond );
1477 if( id->p_decoder->p_module )
1478 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1480 if( id->p_decoder->p_owner )
1482 /* Clean-up pictures ring buffer */
1483 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1485 if( id->p_decoder->p_owner->pp_pics[i] )
1486 video_del_buffer( VLC_OBJECT(id->p_decoder),
1487 id->p_decoder->p_owner->pp_pics[i] );
1489 free( id->p_decoder->p_owner );
1493 if( id->p_encoder->p_module )
1494 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1497 for( i = 0; i < id->i_filter; i++ )
1499 vlc_object_detach( id->pp_filter[i] );
1500 if( id->pp_filter[i]->p_module )
1501 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1503 /* Clean-up pictures ring buffer */
1504 for( j = 0; j < PICTURE_RING_SIZE; j++ )
1506 if( id->pp_filter[i]->p_owner->pp_pics[j] )
1507 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
1508 id->pp_filter[i]->p_owner->pp_pics[j] );
1510 free( id->pp_filter[i]->p_owner );
1512 vlc_object_destroy( id->pp_filter[i] );
1516 static int transcode_video_process( sout_stream_t *p_stream,
1517 sout_stream_id_t *id,
1518 block_t *in, block_t **out )
1520 sout_stream_sys_t *p_sys = p_stream->p_sys;
1521 int i_duplicate = 1, i;
1525 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1527 subpicture_t *p_subpic = 0;
1529 if( p_sys->b_master_sync )
1531 mtime_t i_video_drift;
1532 mtime_t i_master_drift = p_sys->i_master_drift;
1535 if( !i_master_drift )
1537 /* No audio track ? */
1538 p_sys->i_master_drift = i_master_drift = p_pic->date;
1541 i_pts = date_Get( &id->interpolated_pts ) + 1;
1542 i_video_drift = p_pic->date - i_pts;
1545 /* Set the pts of the frame being encoded */
1546 p_pic->date = i_pts;
1548 if( i_video_drift < i_master_drift - 50000 )
1551 msg_Dbg( p_stream, "dropping frame (%i)",
1552 (int)(i_video_drift - i_master_drift) );
1554 p_pic->pf_release( p_pic );
1555 return VLC_EGENERIC;
1557 else if( i_video_drift > i_master_drift + 50000 )
1560 msg_Dbg( p_stream, "adding frame (%i)",
1561 (int)(i_video_drift - i_master_drift) );
1567 if( !id->p_encoder->p_module )
1569 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1571 p_pic->pf_release( p_pic );
1572 transcode_video_close( p_stream, id );
1573 id->b_transcode = VLC_FALSE;
1574 return VLC_EGENERIC;
1578 if( p_stream->p_sys->b_deinterlace )
1580 id->pp_filter[id->i_filter] =
1581 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1582 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1584 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1585 video_new_buffer_filter;
1586 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1587 video_del_buffer_filter;
1589 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1590 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
1591 id->pp_filter[id->i_filter]->p_module =
1592 module_Need( id->pp_filter[id->i_filter],
1593 "video filter2", "deinterlace", 0 );
1594 if( id->pp_filter[id->i_filter]->p_module )
1596 id->pp_filter[id->i_filter]->p_owner =
1597 malloc( sizeof(filter_owner_sys_t) );
1598 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1599 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1605 msg_Dbg( p_stream, "no video filter found" );
1606 vlc_object_detach( id->pp_filter[id->i_filter] );
1607 vlc_object_destroy( id->pp_filter[id->i_filter] );
1611 /* Check if we need a filter for chroma conversion or resizing */
1612 if( id->p_decoder->fmt_out.video.i_chroma !=
1613 id->p_encoder->fmt_in.video.i_chroma ||
1614 id->p_decoder->fmt_out.video.i_width !=
1615 id->p_encoder->fmt_out.video.i_width ||
1616 id->p_decoder->fmt_out.video.i_height !=
1617 id->p_encoder->fmt_out.video.i_height ||
1618 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1619 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1621 id->pp_filter[id->i_filter] =
1622 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1623 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
1625 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
1626 video_new_buffer_filter;
1627 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
1628 video_del_buffer_filter;
1630 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
1631 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
1632 id->pp_filter[id->i_filter]->p_module =
1633 module_Need( id->pp_filter[id->i_filter],
1634 "video filter2", 0, 0 );
1635 if( id->pp_filter[id->i_filter]->p_module )
1637 id->pp_filter[id->i_filter]->p_owner =
1638 malloc( sizeof(filter_owner_sys_t) );
1639 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1640 id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
1646 msg_Dbg( p_stream, "no video filter found" );
1647 vlc_object_detach( id->pp_filter[id->i_filter] );
1648 vlc_object_destroy( id->pp_filter[id->i_filter] );
1650 p_pic->pf_release( p_pic );
1651 transcode_video_close( p_stream, id );
1652 id->b_transcode = VLC_FALSE;
1653 return VLC_EGENERIC;
1658 /* Run filter chain */
1659 for( i = 0; i < id->i_filter; i++ )
1661 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1668 /* Check if we have a subpicture to overlay */
1671 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
1672 /* TODO: get another pic */
1675 /* Overlay subpicture */
1678 int i_scale_width, i_scale_height;
1679 video_format_t *p_fmt;
1681 i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
1682 id->p_decoder->fmt_out.video.i_width;
1683 i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
1684 id->p_decoder->fmt_out.video.i_height;
1686 if( p_pic->i_refcount && !id->i_filter )
1688 /* We can't modify the picture, we need to duplicate it */
1689 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
1692 vout_CopyPicture( p_stream, p_tmp, p_pic );
1693 p_pic->pf_release( p_pic );
1699 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
1701 p_fmt = &id->p_decoder->fmt_out.video;
1703 /* FIXME (shouldn't have to be done here) */
1704 p_fmt->i_sar_num = p_fmt->i_aspect *
1705 p_fmt->i_height / p_fmt->i_width;
1706 p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
1708 spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
1709 i_scale_width, i_scale_height );
1712 if( p_sys->i_threads >= 1 )
1714 vlc_mutex_lock( &p_sys->lock_out );
1715 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1716 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1717 *out = p_sys->p_buffers;
1718 p_sys->p_buffers = NULL;
1719 vlc_cond_signal( &p_sys->cond );
1720 vlc_mutex_unlock( &p_sys->lock_out );
1725 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1726 block_ChainAppend( out, p_block );
1728 if( p_sys->b_master_sync )
1729 date_Increment( &id->interpolated_pts, 1 );
1731 if( p_sys->b_master_sync && i_duplicate > 1 )
1733 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1734 date_Increment( &id->interpolated_pts, 1 );
1735 p_pic->date = i_pts;
1736 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1737 block_ChainAppend( out, p_block );
1740 p_pic->pf_release( p_pic );
1747 static int EncoderThread( sout_stream_sys_t *p_sys )
1749 sout_stream_id_t *id = p_sys->id_video;
1752 while( !p_sys->b_die && !p_sys->b_error )
1756 vlc_mutex_lock( &p_sys->lock_out );
1757 while( p_sys->i_last_pic == p_sys->i_first_pic )
1759 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1760 if( p_sys->b_die || p_sys->b_error ) break;
1762 if( p_sys->b_die || p_sys->b_error )
1764 vlc_mutex_unlock( &p_sys->lock_out );
1768 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1769 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1770 vlc_mutex_unlock( &p_sys->lock_out );
1772 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1773 vlc_mutex_lock( &p_sys->lock_out );
1774 block_ChainAppend( &p_sys->p_buffers, p_block );
1776 if( p_sys->b_master_sync )
1777 date_Increment( &id->interpolated_pts, 1 );
1780 if( p_sys->b_master_sync && i_duplicate > 1 )
1782 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1783 date_Increment( &id->interpolated_pts, 1 );
1784 p_pic->date = i_pts;
1785 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1786 block_ChainAppend( &p_sys->p_buffers, p_block );
1789 vlc_mutex_unlock( &p_sys->lock_out );
1791 p_pic->pf_release( p_pic );
1794 while( p_sys->i_last_pic != p_sys->i_first_pic )
1796 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1797 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1799 p_pic->pf_release( p_pic );
1802 block_ChainRelease( p_sys->p_buffers );
1807 struct picture_sys_t
1809 vlc_object_t *p_owner;
1812 static void video_release_buffer( picture_t *p_pic )
1814 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
1816 video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
1818 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
1821 static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring )
1823 decoder_t *p_dec = (decoder_t *)p_this;
1827 /* Find an empty space in the picture ring buffer */
1828 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1830 if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
1832 pp_ring[i]->i_status = RESERVED_PICTURE;
1836 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1838 if( pp_ring[i] == 0 ) break;
1841 if( i == PICTURE_RING_SIZE )
1843 msg_Err( p_this, "decoder/filter is leaking pictures, "
1844 "resetting its ring buffer" );
1846 for( i = 0; i < PICTURE_RING_SIZE; i++ )
1848 pp_ring[i]->pf_release( pp_ring[i] );
1854 p_pic = malloc( sizeof(picture_t) );
1855 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1856 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
1857 p_dec->fmt_out.video.i_chroma,
1858 p_dec->fmt_out.video.i_width,
1859 p_dec->fmt_out.video.i_height,
1860 p_dec->fmt_out.video.i_aspect );
1862 if( !p_pic->i_planes )
1868 p_pic->pf_release = video_release_buffer;
1869 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
1870 p_pic->p_sys->p_owner = p_this;
1871 p_pic->i_status = RESERVED_PICTURE;
1878 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
1880 return video_new_buffer( VLC_OBJECT(p_dec),
1881 p_dec->p_owner->pp_pics );
1884 static picture_t *video_new_buffer_filter( filter_t *p_filter )
1886 return video_new_buffer( VLC_OBJECT(p_filter),
1887 p_filter->p_owner->pp_pics );
1890 static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
1892 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
1893 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
1894 if( p_pic ) free( p_pic );
1897 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
1899 p_pic->i_refcount = 0;
1900 p_pic->i_status = DESTROYED_PICTURE;
1903 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
1905 p_pic->i_refcount = 0;
1906 p_pic->i_status = DESTROYED_PICTURE;
1909 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
1911 p_pic->i_refcount++;
1914 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
1916 video_release_buffer( p_pic );
1922 static subpicture_t *spu_new_buffer( decoder_t * );
1923 static void spu_del_buffer( decoder_t *, subpicture_t * );
1925 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1927 sout_stream_sys_t *p_sys = p_stream->p_sys;
1933 /* Initialization of decoder structures */
1934 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
1935 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
1936 id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
1937 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
1939 id->p_decoder->p_module =
1940 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1942 if( !id->p_decoder->p_module )
1944 msg_Err( p_stream, "cannot find decoder" );
1945 return VLC_EGENERIC;
1948 if( !p_sys->b_soverlay )
1954 /* Initialization of encoder format structures */
1955 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1956 id->p_decoder->fmt_in.i_codec );
1958 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
1960 id->p_encoder->p_module =
1961 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
1963 if( !id->p_encoder->p_module )
1965 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1966 msg_Err( p_stream, "cannot find encoder" );
1967 return VLC_EGENERIC;
1973 p_sys->p_spu = spu_Create( p_stream );
1974 spu_Init( p_sys->p_spu );
1980 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
1983 if( id->p_decoder->p_module )
1984 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1987 if( id->p_encoder->p_module )
1988 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1991 static int transcode_spu_process( sout_stream_t *p_stream,
1992 sout_stream_id_t *id,
1993 block_t *in, block_t **out )
1995 sout_stream_sys_t *p_sys = p_stream->p_sys;
1996 subpicture_t *p_subpic;
1999 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2000 if( !p_subpic ) return VLC_EGENERIC;
2002 if( p_sys->b_master_sync && p_sys->i_master_drift )
2004 p_subpic->i_start -= p_sys->i_master_drift;
2005 if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2008 if( p_sys->b_soverlay )
2010 spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
2016 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2017 spu_del_buffer( id->p_decoder, p_subpic );
2021 block_ChainAppend( out, p_block );
2026 return VLC_EGENERIC;
2029 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2031 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2032 return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2035 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2037 sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
2038 spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );