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"
40 #ifdef HAVE_FFMPEG_AVCODEC_H
41 # include <ffmpeg/avcodec.h>
46 #if LIBAVCODEC_BUILD < 4704
47 # define AV_NOPTS_VALUE 0
50 /*****************************************************************************
52 *****************************************************************************/
53 #define VENC_TEXT N_("Video encoder")
54 #define VENC_LONGTEXT N_( \
55 "Allows you to specify the video encoder to use and its associated " \
57 #define VCODEC_TEXT N_("Destination video codec")
58 #define VCODEC_LONGTEXT N_( \
59 "Allows you to specify the destination video codec used for the " \
61 #define VB_TEXT N_("Video bitrate")
62 #define VB_LONGTEXT N_( \
63 "Allows you to specify the video bitrate used for the streaming " \
65 #define SCALE_TEXT N_("Video scaling")
66 #define SCALE_LONGTEXT N_( \
67 "Allows you to scale the video before encoding." )
68 #define FPS_TEXT N_("Video frame-rate")
69 #define FPS_LONGTEXT N_( \
70 "Allows you to specify an output frame rate for the video." )
71 #define DEINTERLACE_TEXT N_("Deinterlace video")
72 #define DEINTERLACE_LONGTEXT N_( \
73 "Allows you to deinterlace the video before encoding." )
74 #define WIDTH_TEXT N_("Video width")
75 #define WIDTH_LONGTEXT N_( \
76 "Allows you to specify the output video width." )
77 #define HEIGHT_TEXT N_("Video height")
78 #define HEIGHT_LONGTEXT N_( \
79 "Allows you to specify the output video height." )
81 #define CROPTOP_TEXT N_("Video crop top")
82 #define CROPTOP_LONGTEXT N_( \
83 "Allows you to specify the top coordinate for the video cropping." )
84 #define CROPLEFT_TEXT N_("Video crop left")
85 #define CROPLEFT_LONGTEXT N_( \
86 "Allows you to specify the left coordinate for the video cropping." )
87 #define CROPBOTTOM_TEXT N_("Video crop bottom")
88 #define CROPBOTTOM_LONGTEXT N_( \
89 "Allows you to specify the bottom coordinate for the video cropping." )
90 #define CROPRIGHT_TEXT N_("Video crop right")
91 #define CROPRIGHT_LONGTEXT N_( \
92 "Allows you to specify the right coordinate for the video cropping." )
94 #define AENC_TEXT N_("Audio encoder")
95 #define AENC_LONGTEXT N_( \
96 "Allows you to specify the audio encoder to use and its associated " \
98 #define ACODEC_TEXT N_("Destination audio codec")
99 #define ACODEC_LONGTEXT N_( \
100 "Allows you to specify the destination audio codec used for the " \
101 "streaming output." )
102 #define AB_TEXT N_("Audio bitrate")
103 #define AB_LONGTEXT N_( \
104 "Allows you to specify the audio bitrate used for the streaming " \
106 #define ARATE_TEXT N_("Audio sample rate")
107 #define ARATE_LONGTEXT N_( \
108 "Allows you to specify the audio sample rate used for the streaming " \
110 #define ACHANS_TEXT N_("Audio channels")
111 #define ACHANS_LONGTEXT N_( \
112 "Allows you to specify the number of audio channels used for the " \
113 "streaming output." )
115 #define SENC_TEXT N_("Subtitles encoder")
116 #define SENC_LONGTEXT N_( \
117 "Allows you to specify the subtitles encoder to use and its associated " \
119 #define SCODEC_TEXT N_("Destination subtitles codec")
120 #define SCODEC_LONGTEXT N_( \
121 "Allows you to specify the destination subtitles codec used for the " \
122 "streaming output." )
124 #define THREADS_TEXT N_("Number of threads")
125 #define THREADS_LONGTEXT N_( \
126 "Allows you to specify the number of threads used for the transcoding." )
128 #define ASYNC_TEXT N_("Synchronise on audio track")
129 #define ASYNC_LONGTEXT N_( \
130 "This option will drop/duplicate video frames to synchronise the video " \
131 "track on the audio track." )
133 static int Open ( vlc_object_t * );
134 static void Close( vlc_object_t * );
136 #define SOUT_CFG_PREFIX "sout-transcode-"
139 #if defined(MODULE_NAME_is_stream_out_transcodealtivec) \
140 || (defined(CAN_COMPILE_ALTIVEC) && !defined(NO_ALTIVEC_IN_FFMPEG))
141 set_description( _("AltiVec transcode stream output") );
142 add_requirement( ALTIVEC );
143 set_capability( "sout stream", 51 );
145 set_description( _("Transcode stream output") );
146 set_capability( "sout stream", 50 );
148 add_shortcut( "transcode" );
149 set_callbacks( Open, Close );
151 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
152 VENC_LONGTEXT, VLC_FALSE );
153 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
154 VCODEC_LONGTEXT, VLC_FALSE );
155 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
156 VB_LONGTEXT, VLC_FALSE );
157 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
158 SCALE_LONGTEXT, VLC_FALSE );
159 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
160 FPS_LONGTEXT, VLC_FALSE );
161 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
162 DEINTERLACE_LONGTEXT, VLC_FALSE );
163 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
164 WIDTH_LONGTEXT, VLC_TRUE );
165 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
166 HEIGHT_LONGTEXT, VLC_TRUE );
168 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
169 CROPTOP_LONGTEXT, VLC_TRUE );
170 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
171 CROPLEFT_LONGTEXT, VLC_TRUE );
172 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
173 CROPBOTTOM_LONGTEXT, VLC_TRUE );
174 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
175 CROPRIGHT_LONGTEXT, VLC_TRUE );
177 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
178 AENC_LONGTEXT, VLC_FALSE );
179 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
180 ACODEC_LONGTEXT, VLC_FALSE );
181 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
182 AB_LONGTEXT, VLC_FALSE );
183 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
184 ACHANS_LONGTEXT, VLC_FALSE );
185 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
186 ARATE_LONGTEXT, VLC_TRUE );
188 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
189 SENC_LONGTEXT, VLC_FALSE );
190 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
191 SCODEC_LONGTEXT, VLC_FALSE );
192 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
193 SCODEC_LONGTEXT, VLC_FALSE );
195 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
196 THREADS_LONGTEXT, VLC_TRUE );
198 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
199 ASYNC_LONGTEXT, VLC_FALSE );
202 static const char *ppsz_sout_options[] = {
203 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
204 "scale", "fps", "width", "height", "deinterlace", "threads",
205 "aenc", "acodec", "ab", "samplerate", "channels",
206 "senc", "scodec", "soverlay",
210 /*****************************************************************************
211 * Exported prototypes
212 *****************************************************************************/
213 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
214 static int Del ( sout_stream_t *, sout_stream_id_t * );
215 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
217 static int transcode_audio_new ( sout_stream_t *, sout_stream_id_t * );
218 static void transcode_audio_close ( sout_stream_t *, sout_stream_id_t * );
219 static int transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
220 block_t *, block_t ** );
222 static int transcode_video_new ( sout_stream_t *, sout_stream_id_t * );
223 static void transcode_video_close ( sout_stream_t *, sout_stream_id_t * );
224 static int transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
225 static int transcode_video_process( sout_stream_t *, sout_stream_id_t *,
226 block_t *, block_t ** );
228 static picture_t *video_new_buffer( decoder_t * );
229 static void video_del_buffer( decoder_t *, picture_t * );
230 static void video_link_picture( decoder_t *, picture_t * );
231 static void video_unlink_picture( decoder_t *, picture_t * );
233 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
234 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
235 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
236 block_t *, block_t ** );
237 static subpicture_t *transcode_spu_get( sout_stream_t *, sout_stream_id_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;
298 subpicture_t *pp_subpics[SUBPICTURE_RING_SIZE];
301 filter_t *p_filter_blend;
304 vlc_bool_t b_audio_sync;
305 mtime_t i_master_drift;
308 /*****************************************************************************
310 *****************************************************************************/
311 static int Open( vlc_object_t *p_this )
313 sout_stream_t *p_stream = (sout_stream_t*)p_this;
314 sout_stream_sys_t *p_sys;
318 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
320 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
323 msg_Err( p_stream, "cannot create chain" );
328 p_sys->i_master_drift = 0;
330 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
333 /* Audio transcoding parameters */
334 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
335 p_sys->psz_aenc = NULL;
336 p_sys->p_audio_cfg = NULL;
337 if( val.psz_string && *val.psz_string )
340 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
342 if( psz_next ) free( psz_next );
344 if( val.psz_string ) free( val.psz_string );
346 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
348 if( val.psz_string && *val.psz_string )
351 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
352 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
354 if( val.psz_string ) free( val.psz_string );
356 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
357 p_sys->i_abitrate = val.i_int;
358 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
360 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
361 p_sys->i_sample_rate = val.i_int;
363 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
364 p_sys->i_channels = val.i_int;
366 if( p_sys->i_acodec )
368 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
369 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
370 p_sys->i_channels, p_sys->i_abitrate / 1000 );
373 /* Video transcoding parameters */
374 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
375 p_sys->psz_venc = NULL;
376 p_sys->p_video_cfg = NULL;
377 if( val.psz_string && *val.psz_string )
380 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
382 if( psz_next ) free( psz_next );
384 if( val.psz_string ) free( val.psz_string );
386 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
388 if( val.psz_string && *val.psz_string )
391 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
392 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
394 if( val.psz_string ) free( val.psz_string );
396 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
397 p_sys->i_vbitrate = val.i_int;
398 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
400 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
401 p_sys->f_scale = val.f_float;
403 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
404 p_sys->f_fps = val.f_float;
406 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
407 p_sys->i_width = val.i_int;
409 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
410 p_sys->i_height = val.i_int;
412 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
413 p_sys->b_deinterlace = val.b_bool;
415 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
416 p_sys->i_crop_top = val.i_int;
417 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
418 p_sys->i_crop_bottom = val.i_int;
419 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
420 p_sys->i_crop_left = val.i_int;
421 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
422 p_sys->i_crop_right = val.i_int;
424 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
425 p_sys->i_threads = val.i_int;
427 if( p_sys->i_vcodec )
429 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
430 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
431 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
434 /* Subpictures transcoding parameters */
435 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
436 p_sys->psz_senc = NULL;
437 p_sys->p_spu_cfg = NULL;
438 if( val.psz_string && *val.psz_string )
441 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
443 if( psz_next ) free( psz_next );
445 if( val.psz_string ) free( val.psz_string );
447 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
449 if( val.psz_string && *val.psz_string )
452 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
453 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
455 if( val.psz_string ) free( val.psz_string );
457 if( p_sys->i_scodec )
459 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_acodec );
462 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
463 p_sys->b_soverlay = val.b_bool;
464 p_sys->p_filter_blend = 0;
466 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
468 p_sys->pp_subpics[i] = 0;
471 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
472 p_sys->b_audio_sync = val.b_bool;
473 if( p_sys->f_fps > 0 ) p_sys->b_audio_sync = VLC_TRUE;
475 p_stream->pf_add = Add;
476 p_stream->pf_del = Del;
477 p_stream->pf_send = Send;
478 p_stream->p_sys = p_sys;
481 avcodec_register_all();
486 /*****************************************************************************
488 *****************************************************************************/
489 static void Close( vlc_object_t * p_this )
491 sout_stream_t *p_stream = (sout_stream_t*)p_this;
492 sout_stream_sys_t *p_sys = p_stream->p_sys;
494 sout_StreamDelete( p_sys->p_out );
496 while( p_sys->p_audio_cfg != NULL )
498 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
500 if( p_sys->p_audio_cfg->psz_name )
501 free( p_sys->p_audio_cfg->psz_name );
502 if( p_sys->p_audio_cfg->psz_value )
503 free( p_sys->p_audio_cfg->psz_value );
504 free( p_sys->p_audio_cfg );
506 p_sys->p_audio_cfg = p_next;
508 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
510 while( p_sys->p_video_cfg != NULL )
512 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
514 if( p_sys->p_video_cfg->psz_name )
515 free( p_sys->p_video_cfg->psz_name );
516 if( p_sys->p_video_cfg->psz_value )
517 free( p_sys->p_video_cfg->psz_value );
518 free( p_sys->p_video_cfg );
520 p_sys->p_video_cfg = p_next;
522 if( p_sys->psz_venc ) free( p_sys->psz_venc );
524 while( p_sys->p_spu_cfg != NULL )
526 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
528 if( p_sys->p_spu_cfg->psz_name )
529 free( p_sys->p_spu_cfg->psz_name );
530 if( p_sys->p_spu_cfg->psz_value )
531 free( p_sys->p_spu_cfg->psz_value );
532 free( p_sys->p_spu_cfg );
534 p_sys->p_spu_cfg = p_next;
536 if( p_sys->psz_senc ) free( p_sys->psz_senc );
538 if( p_sys->p_filter_blend )
540 if( p_sys->p_filter_blend->p_module )
541 module_Unneed( p_sys->p_filter_blend,
542 p_sys->p_filter_blend->p_module );
544 vlc_object_detach( p_sys->p_filter_blend );
545 vlc_object_destroy( p_sys->p_filter_blend );
548 vlc_object_destroy( p_sys );
551 struct sout_stream_id_t
553 vlc_fourcc_t b_transcode;
555 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
557 /* id of the out stream */
561 decoder_t *p_decoder;
564 filter_t *pp_filter[10];
568 encoder_t *p_encoder;
572 AVCodecContext *ff_dec_c;
582 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
583 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
584 AVFrame *p_ff_pic_tmp2; /* to do resample */
585 AVFrame *p_ff_pic_tmp3; /* to do subpicture overlay */
587 ImgReSampleContext *p_vresample;
590 date_t interpolated_pts;
591 mtime_t i_initial_pts;
595 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
597 sout_stream_sys_t *p_sys = p_stream->p_sys;
598 sout_stream_id_t *id;
600 id = malloc( sizeof( sout_stream_id_t ) );
601 memset( id, 0, sizeof(sout_stream_id_t) );
604 id->p_decoder = NULL;
605 id->p_encoder = NULL;
607 /* Create decoder object */
608 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
611 msg_Err( p_stream, "out of memory" );
614 vlc_object_attach( id->p_decoder, p_stream );
615 id->p_decoder->p_module = NULL;
616 id->p_decoder->fmt_in = *p_fmt;
617 id->p_decoder->fmt_out = *p_fmt;
618 id->p_decoder->fmt_out.i_extra = 0;
619 id->p_decoder->fmt_out.p_extra = 0;
620 id->p_decoder->b_pace_control = VLC_TRUE;
622 /* Create encoder object */
623 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
626 msg_Err( p_stream, "out of memory" );
629 vlc_object_attach( id->p_encoder, p_stream );
630 id->p_encoder->p_module = NULL;
632 /* Create destination format */
633 es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
634 id->p_encoder->fmt_out.i_id = p_fmt->i_id;
635 id->p_encoder->fmt_out.i_group = p_fmt->i_group;
636 if( p_fmt->psz_language )
637 id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
639 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
642 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
643 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
645 /* Complete destination format */
646 id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
647 id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
648 p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
649 id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
650 p_sys->i_channels : p_fmt->audio.i_channels;
651 id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
652 id->p_encoder->fmt_out.audio.i_bitspersample =
653 p_fmt->audio.i_bitspersample;
655 /* Build decoder -> filter -> encoder chain */
656 if( transcode_audio_new( p_stream, id ) )
658 msg_Err( p_stream, "cannot create audio chain" );
662 /* Open output stream */
663 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
664 id->b_transcode = VLC_TRUE;
666 if( !id->id ) goto error;
668 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
670 else if( p_fmt->i_cat == VIDEO_ES &&
671 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
674 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
675 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
677 /* Complete destination format */
678 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
679 id->p_encoder->fmt_out.video.i_width = p_sys->i_width;
680 id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
681 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
683 /* Build decoder -> filter -> encoder chain */
684 if( transcode_video_new( p_stream, id ) )
686 msg_Err( p_stream, "cannot create video chain" );
690 /* Stream will be added later on because we don't know
691 * all the characteristics of the decoded stream yet */
692 id->b_transcode = VLC_TRUE;
694 if( p_sys->f_fps > 0 )
696 id->p_encoder->fmt_out.video.i_frame_rate = p_sys->f_fps * 1000;
697 id->p_encoder->fmt_out.video.i_frame_rate_base = 1000;
700 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
702 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
703 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
704 (char*)&p_sys->i_scodec );
706 /* Complete destination format */
707 id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
709 /* build decoder -> filter -> encoder */
710 if( transcode_spu_new( p_stream, id ) )
712 msg_Err( p_stream, "cannot create subtitles chain" );
716 /* open output stream */
717 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
718 id->b_transcode = VLC_TRUE;
720 if( !id->id ) goto error;
722 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
724 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
725 (char*)&p_fmt->i_codec );
727 id->b_transcode = VLC_TRUE;
729 /* Build decoder -> filter -> overlaying chain */
730 if( transcode_spu_new( p_stream, id ) )
732 msg_Err( p_stream, "cannot create subtitles chain" );
738 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
739 (char*)&p_fmt->i_codec );
740 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
741 id->b_transcode = VLC_FALSE;
743 if( !id->id ) goto error;
751 vlc_object_detach( id->p_decoder );
752 vlc_object_destroy( id->p_decoder );
757 vlc_object_detach( id->p_encoder );
758 vlc_object_destroy( id->p_encoder );
765 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
767 sout_stream_sys_t *p_sys = p_stream->p_sys;
769 if( id->b_transcode )
771 switch( id->p_decoder->fmt_in.i_cat )
774 transcode_audio_close( p_stream, id );
777 transcode_video_close( p_stream, id );
780 transcode_spu_close( p_stream, id );
785 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
789 vlc_object_detach( id->p_decoder );
790 vlc_object_destroy( id->p_decoder );
795 vlc_object_detach( id->p_encoder );
796 vlc_object_destroy( id->p_encoder );
804 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
807 sout_stream_sys_t *p_sys = p_stream->p_sys;
810 if( !id->b_transcode && id->id )
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 );
824 block_Release( p_buffer );
828 if( transcode_video_process( p_stream, id, p_buffer, &p_out )
836 if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
844 block_Release( p_buffer );
848 if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
852 /****************************************************************************
853 * ffmpeg decoder reencoder part
854 ****************************************************************************/
860 } fourcc_to_ff_code[] =
863 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
864 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
865 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
866 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
867 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
868 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
869 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
870 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
871 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
873 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
876 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
880 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
882 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
884 return fourcc_to_ff_code[i].i_ff_codec;
891 static int transcode_audio_new( sout_stream_t *p_stream,
892 sout_stream_id_t *id )
896 if( id->p_decoder->fmt_in.i_codec == VLC_FOURCC('s','1','6','l') ||
897 id->p_decoder->fmt_in.i_codec == VLC_FOURCC('s','1','6','b') ||
898 id->p_decoder->fmt_in.i_codec == VLC_FOURCC('s','8',' ',' ') ||
899 id->p_decoder->fmt_in.i_codec == VLC_FOURCC('u','8',' ',' ') )
903 id->ff_dec_c = avcodec_alloc_context();
904 id->ff_dec_c->sample_rate = id->p_decoder->fmt_in.audio.i_rate;
905 id->ff_dec_c->channels = id->p_decoder->fmt_in.audio.i_channels;
906 id->ff_dec_c->block_align = id->p_decoder->fmt_in.audio.i_blockalign;
907 id->ff_dec_c->bit_rate = id->p_decoder->fmt_in.i_bitrate;
912 i_ff_codec = get_ff_codec( id->p_decoder->fmt_in.i_codec );
913 if( i_ff_codec == 0 )
915 msg_Err( p_stream, "cannot find decoder id" );
919 id->ff_dec = avcodec_find_decoder( i_ff_codec );
922 msg_Err( p_stream, "cannot find decoder (avcodec)" );
926 id->ff_dec_c = avcodec_alloc_context();
927 id->ff_dec_c->sample_rate = id->p_decoder->fmt_in.audio.i_rate;
928 id->ff_dec_c->channels = id->p_decoder->fmt_in.audio.i_channels;
929 id->ff_dec_c->block_align = id->p_decoder->fmt_in.audio.i_blockalign;
930 id->ff_dec_c->bit_rate = id->p_decoder->fmt_in.i_bitrate;
932 id->ff_dec_c->extradata_size = id->p_decoder->fmt_in.i_extra;
933 id->ff_dec_c->extradata = id->p_decoder->fmt_in.p_extra;
934 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
936 msg_Err( p_stream, "cannot open decoder" );
937 av_free( id->ff_dec_c );
942 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
943 id->i_buffer_pos = 0;
944 id->p_buffer = malloc( id->i_buffer );
946 /* Sanity check for audio channels */
947 id->p_encoder->fmt_out.audio.i_channels =
948 __MIN( id->p_encoder->fmt_out.audio.i_channels,
949 id->p_decoder->fmt_in.audio.i_channels );
951 /* Initialization of encoder format structures */
952 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
953 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
954 id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
955 id->p_encoder->fmt_in.audio.i_physical_channels =
956 id->p_encoder->fmt_in.audio.i_original_channels =
957 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
958 id->p_encoder->fmt_in.audio.i_channels =
959 id->p_encoder->fmt_out.audio.i_channels;
960 id->p_encoder->fmt_in.audio.i_bitspersample = 16;
962 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
964 id->p_encoder->p_module =
965 module_Need( id->p_encoder, "encoder",
966 p_stream->p_sys->psz_aenc, VLC_TRUE );
967 if( !id->p_encoder->p_module )
969 vlc_object_detach( id->p_encoder );
970 vlc_object_destroy( id->p_encoder );
971 msg_Err( p_stream, "cannot open encoder" );
972 av_free( id->ff_dec_c );
976 /* FIXME: Hack for mp3 transcoding support */
977 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
978 id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
983 static void transcode_audio_close( sout_stream_t *p_stream,
984 sout_stream_id_t *id )
986 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
987 av_free( id->ff_dec_c );
989 module_Unneed( id->p_encoder, id->p_encoder->p_module );
991 vlc_object_detach( id->p_encoder );
992 vlc_object_destroy( id->p_encoder );
994 free( id->p_buffer );
997 static int transcode_audio_process( sout_stream_t *p_stream,
998 sout_stream_id_t *id,
999 block_t *in, block_t **out )
1001 sout_stream_sys_t *p_sys = p_stream->p_sys;
1002 aout_buffer_t aout_buf;
1004 int i_buffer = in->i_buffer;
1005 char *p_buffer = in->p_buffer;
1006 id->i_dts = in->i_dts;
1011 id->i_buffer_pos = 0;
1013 /* decode as much data as possible */
1018 i_used = avcodec_decode_audio( id->ff_dec_c,
1019 (int16_t*)id->p_buffer, &id->i_buffer_pos,
1020 p_buffer, i_buffer );
1023 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
1028 msg_Warn( p_stream, "error audio decoding");
1035 if ( id->i_buffer_pos < 0 )
1037 msg_Warn( p_stream, "weird error audio decoding");
1043 int16_t *sout = (int16_t*)id->p_buffer;
1044 vlc_fourcc_t i_codec = id->p_decoder->fmt_in.i_codec;
1046 if( i_codec == VLC_FOURCC('s','8',' ',' ') ||
1047 i_codec == VLC_FOURCC('u','8',' ',' ') )
1049 int8_t *sin = (int8_t*)p_buffer;
1050 int i_used = __MIN( id->i_buffer/2, i_buffer );
1051 int i_samples = i_used;
1053 if( i_codec == VLC_FOURCC('s','8',' ',' ') )
1054 while( i_samples > 0 )
1056 *sout++ = ( *sin++ ) << 8;
1060 while( i_samples > 0 )
1062 *sout++ = ( *sin++ - 128 ) << 8;
1068 id->i_buffer_pos = i_used * 2;
1070 else if( i_codec == VLC_FOURCC('s','1','6','l') ||
1071 i_codec == VLC_FOURCC('s','1','6','b') )
1073 int16_t *sin = (int16_t*)p_buffer;
1074 int i_used = __MIN( id->i_buffer, i_buffer );
1075 int i_samples = i_used / 2;
1078 memcpy( sout, sin, i_used );
1080 #ifdef WORDS_BIGENDIAN
1081 if( i_codec == VLC_FOURCC('s','1','6','l') )
1083 if( i_codec == VLC_FOURCC('s','1','6','b') )
1086 uint8_t *dat = (uint8_t*)sout;
1088 while( i_samples > 0 )
1103 id->i_buffer_pos = i_used;
1107 if( id->i_buffer_pos == 0 ) continue;
1109 aout_buf.p_buffer = id->p_buffer;
1110 aout_buf.i_nb_bytes = id->i_buffer_pos;
1111 aout_buf.i_nb_samples = id->i_buffer_pos / 2 /
1112 id->p_decoder->fmt_in.audio.i_channels;
1113 aout_buf.start_date = id->i_dts;
1114 aout_buf.end_date = id->i_dts;
1116 if( p_sys->b_audio_sync )
1118 aout_buf.start_date = date_Get( &id->interpolated_pts ) + 1;
1119 p_sys->i_master_drift = id->i_dts - aout_buf.start_date;
1120 date_Increment( &id->interpolated_pts, aout_buf.i_nb_samples );
1123 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
1124 id->p_decoder->fmt_in.audio.i_channels /
1125 id->p_decoder->fmt_in.audio.i_rate );
1127 if( id->p_encoder->fmt_in.audio.i_channels == 1 &&
1128 id->p_decoder->fmt_in.audio.i_channels > 1 )
1130 int16_t *p_sample = (int16_t *)aout_buf.p_buffer;
1131 int i_src_c = id->p_decoder->fmt_in.audio.i_channels;
1134 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1138 for( j = 1; j < i_src_c; j++ )
1140 c += p_sample[i_src_c * i + j];
1142 p_sample[i] = c / (i_src_c-1);
1144 aout_buf.i_nb_bytes = i * 2;
1146 else if( id->p_encoder->fmt_in.audio.i_channels == 2 &&
1147 id->p_decoder->fmt_in.audio.i_channels > 2 )
1149 int i_src_c = id->p_decoder->fmt_in.audio.i_channels;
1152 static const float mixf_l[4][6] =/* [i_src_c - 3][channel index] */
1154 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1155 { 0.00, 0.50, 0.50, 0.00, 0.00, 0.00 }, /* 4 channels */
1156 { 0.00, 0.50, 0.00, 0.50, 0.00, 0.00 }, /* 5 channels */
1157 { 0.00, 0.34, 0.33, 0.00, 0.33, 0.00 }, /* 6 channels */
1159 static const float mixf_r[4][6] =/* [i_src_c - 3][channel index] */
1161 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1162 { 0.00, 0.00, 0.50, 0.50, 0.00, 0.00 }, /* 4 channels */
1163 { 0.00, 0.00, 0.50, 0.00, 0.50, 0.00 }, /* 5 channels */
1164 { 0.00, 0.00, 0.33, 0.34, 0.00, 0.33 }, /* 6 channels */
1168 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1170 int16_t *p_src = (int16_t *)aout_buf.p_buffer + i_src_c * i;
1171 int16_t *p_dst = (int16_t *)aout_buf.p_buffer + 2 * i;
1174 float l = 0.0, r = 0.0;
1175 for( j = 0; j < i_src_c; j++ )
1177 l += mixf_l[i_src_c-3][j] * p_src[j];
1178 r += mixf_r[i_src_c-3][j] * p_src[j];
1181 p_dst[0] = (int)( l + 0.5 );
1182 p_dst[1] = (int)( r + 0.5 );
1184 aout_buf.i_nb_bytes = i * 2 * 2;
1186 else if( id->p_decoder->fmt_in.audio.i_channels !=
1187 id->p_encoder->fmt_in.audio.i_channels )
1192 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
1193 static const int translation[7][6] =
1194 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
1195 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
1196 { 0, 1, 0, 0, 0, 0 }, /* 2 */
1197 { 1, 2, 0, 0, 0, 0 }, /* 3 */
1198 { 1, 3, 2, 0, 0, 0 }, /* 4 */
1199 { 1, 3, 4, 2, 0, 0 }, /* 5 */
1200 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
1202 /* dumb downmixing */
1203 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1205 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
1206 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
1208 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
1209 p_buffer[i*id->p_decoder->fmt_in.audio.i_channels +
1210 translation[id->p_decoder->fmt_in.audio.i_channels][j]];
1213 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1216 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1217 block_ChainAppend( out, p_block );
1227 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1229 sout_stream_sys_t *p_sys = p_stream->p_sys;
1235 /* Initialization of decoder structures */
1236 id->p_decoder->pf_decode_video = 0;
1237 id->p_decoder->pf_vout_buffer_new = video_new_buffer;
1238 id->p_decoder->pf_vout_buffer_del = video_del_buffer;
1239 id->p_decoder->pf_picture_link = video_link_picture;
1240 id->p_decoder->pf_picture_unlink = video_unlink_picture;
1241 //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1243 id->p_decoder->p_module =
1244 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1246 if( !id->p_decoder->p_module )
1248 msg_Err( p_stream, "cannot find decoder" );
1249 return VLC_EGENERIC;
1254 * Because some info about the decoded input will only be available
1255 * once the first frame is decoded, we actually only test the availability
1256 * of the encoder here.
1259 /* Initialization of encoder format structures */
1260 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1261 id->p_decoder->fmt_out.i_codec );
1262 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1264 /* The dimensions will be set properly later on.
1265 * Just put sensible values so we can test an encoder is available. */
1266 id->p_encoder->fmt_in.video.i_width =
1267 id->p_encoder->fmt_out.video.i_width ?
1268 id->p_encoder->fmt_out.video.i_width :
1269 id->p_decoder->fmt_in.video.i_width ?
1270 id->p_decoder->fmt_in.video.i_width : 16;
1271 id->p_encoder->fmt_in.video.i_height =
1272 id->p_encoder->fmt_out.video.i_height ?
1273 id->p_encoder->fmt_out.video.i_height :
1274 id->p_decoder->fmt_in.video.i_height ?
1275 id->p_decoder->fmt_in.video.i_height : 16;
1276 id->p_encoder->fmt_in.video.i_frame_rate = 25;
1277 id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1279 id->p_encoder->i_threads = p_sys->i_threads;
1280 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1282 id->p_encoder->p_module =
1283 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1284 if( !id->p_encoder->p_module )
1286 msg_Err( p_stream, "cannot find encoder" );
1287 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1288 id->p_decoder->p_module = 0;
1289 return VLC_EGENERIC;
1292 /* Close the encoder.
1293 * We'll open it only when we have the first frame. */
1294 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1295 id->p_encoder->p_module = NULL;
1297 if( p_sys->i_threads >= 1 )
1299 p_sys->id_video = id;
1300 vlc_mutex_init( p_stream, &p_sys->lock_out );
1301 vlc_cond_init( p_stream, &p_sys->cond );
1302 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1303 p_sys->i_first_pic = 0;
1304 p_sys->i_last_pic = 0;
1305 p_sys->p_buffers = NULL;
1306 p_sys->b_die = p_sys->b_error = 0;
1307 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1308 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1310 msg_Err( p_stream, "cannot spawn encoder thread" );
1311 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1312 id->p_decoder->p_module = 0;
1313 return VLC_EGENERIC;
1320 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1321 sout_stream_id_t *id )
1323 sout_stream_sys_t *p_sys = p_stream->p_sys;
1325 /* Hack because of the copy packetizer which can fail to detect the
1326 * proper size (which forces us to wait until the 1st frame
1328 int i_width = id->p_decoder->fmt_out.video.i_width -
1329 p_sys->i_crop_left - p_sys->i_crop_right;
1330 int i_height = id->p_decoder->fmt_out.video.i_height -
1331 p_sys->i_crop_top - p_sys->i_crop_bottom;
1333 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1334 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1336 /* Apply the scaling */
1337 id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1338 id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1340 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1341 id->p_encoder->fmt_out.video.i_height <= 0 )
1343 id->p_encoder->fmt_out.video.i_height =
1344 id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1346 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1347 id->p_encoder->fmt_out.video.i_height > 0 )
1349 id->p_encoder->fmt_out.video.i_width =
1350 id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1353 id->p_encoder->fmt_in.video.i_width =
1354 id->p_encoder->fmt_out.video.i_width;
1355 id->p_encoder->fmt_in.video.i_height =
1356 id->p_encoder->fmt_out.video.i_height;
1358 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1359 !id->p_encoder->fmt_out.video.i_frame_rate_base )
1361 if( id->p_decoder->fmt_out.video.i_frame_rate &&
1362 id->p_decoder->fmt_out.video.i_frame_rate_base )
1364 id->p_encoder->fmt_out.video.i_frame_rate =
1365 id->p_decoder->fmt_out.video.i_frame_rate;
1366 id->p_encoder->fmt_out.video.i_frame_rate_base =
1367 id->p_decoder->fmt_out.video.i_frame_rate_base;
1371 /* Pick a sensible default value */
1372 id->p_encoder->fmt_out.video.i_frame_rate = 25;
1373 id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1377 id->p_encoder->fmt_in.video.i_frame_rate =
1378 id->p_encoder->fmt_out.video.i_frame_rate;
1379 id->p_encoder->fmt_in.video.i_frame_rate_base =
1380 id->p_encoder->fmt_out.video.i_frame_rate_base;
1382 date_Init( &id->interpolated_pts,
1383 id->p_encoder->fmt_out.video.i_frame_rate,
1384 id->p_encoder->fmt_out.video.i_frame_rate_base );
1386 /* Check whether a particular aspect ratio was requested */
1387 if( !id->p_encoder->fmt_out.video.i_aspect )
1389 id->p_encoder->fmt_out.video.i_aspect =
1390 id->p_decoder->fmt_out.video.i_aspect;
1392 id->p_encoder->fmt_in.video.i_aspect =
1393 id->p_encoder->fmt_out.video.i_aspect;
1395 id->p_encoder->p_module =
1396 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1397 if( !id->p_encoder->p_module )
1399 msg_Err( p_stream, "cannot find encoder" );
1400 return VLC_EGENERIC;
1403 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1405 /* Hack for mp2v/mp1v transcoding support */
1406 if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1407 id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1409 id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1412 id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1413 &id->p_encoder->fmt_out );
1416 msg_Err( p_stream, "cannot add this stream" );
1417 return VLC_EGENERIC;
1423 static void transcode_video_close( sout_stream_t *p_stream,
1424 sout_stream_id_t *id )
1428 if( p_stream->p_sys->i_threads >= 1 )
1430 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1431 p_stream->p_sys->b_die = 1;
1432 vlc_cond_signal( &p_stream->p_sys->cond );
1433 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1434 vlc_thread_join( p_stream->p_sys );
1435 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1436 vlc_cond_destroy( &p_stream->p_sys->cond );
1440 if( id->p_decoder->p_module )
1441 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1444 if( id->p_encoder->p_module )
1445 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1448 for( i = 0; i < id->i_filter; i++ )
1450 vlc_object_detach( id->pp_filter[i] );
1451 if( id->pp_filter[i]->p_module )
1452 module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1453 vlc_object_destroy( id->pp_filter[i] );
1457 static int transcode_video_process( sout_stream_t *p_stream,
1458 sout_stream_id_t *id,
1459 block_t *in, block_t **out )
1461 sout_stream_sys_t *p_sys = p_stream->p_sys;
1462 int i_duplicate = 1, i;
1466 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1468 subpicture_t *p_subpic = 0;
1470 if( p_sys->b_audio_sync )
1472 mtime_t i_video_drift;
1473 mtime_t i_master_drift = p_sys->i_master_drift;
1476 if( !id->i_initial_pts ) id->i_initial_pts = p_pic->date;
1478 if( !i_master_drift )
1480 /* No audio track ? */
1481 i_master_drift = id->i_initial_pts;
1484 i_pts = date_Get( &id->interpolated_pts ) + 1;
1485 i_video_drift = p_pic->date - i_pts;
1488 /* Set the pts of the frame being encoded */
1489 p_pic->date = i_pts;
1491 if( i_video_drift < i_master_drift - 50000 )
1493 msg_Dbg( p_stream, "dropping frame (%i)",
1494 (int)(i_video_drift - i_master_drift) );
1495 return VLC_EGENERIC;
1497 else if( i_video_drift > i_master_drift + 50000 )
1499 msg_Dbg( p_stream, "adding frame (%i)",
1500 (int)(i_video_drift - i_master_drift) );
1504 date_Increment( &id->interpolated_pts, 1 );
1507 if( !id->p_encoder->p_module )
1509 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1511 transcode_video_close( p_stream, id );
1512 id->b_transcode = VLC_FALSE;
1515 /* Check if we need a filter for chroma conversion or resizing */
1516 if( id->p_decoder->fmt_out.video.i_chroma !=
1517 id->p_encoder->fmt_in.video.i_chroma ||
1518 id->p_decoder->fmt_out.video.i_width !=
1519 id->p_encoder->fmt_out.video.i_width ||
1520 id->p_decoder->fmt_out.video.i_height !=
1521 id->p_encoder->fmt_out.video.i_height ||
1522 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1523 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1526 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1527 vlc_object_attach( id->pp_filter[0], p_stream );
1529 id->pp_filter[0]->pf_vout_buffer_new = video_new_buffer;
1530 id->pp_filter[0]->pf_vout_buffer_del = video_del_buffer;
1532 id->pp_filter[0]->fmt_in = id->p_decoder->fmt_out;
1533 id->pp_filter[0]->fmt_out = id->p_encoder->fmt_in;
1534 id->pp_filter[0]->p_module =
1535 module_Need( id->pp_filter[0], "video filter2", 0, 0 );
1536 if( id->pp_filter[0]->p_module ) id->i_filter++;
1539 msg_Dbg( p_stream, "no video filter found" );
1540 vlc_object_detach( id->pp_filter[0] );
1541 vlc_object_destroy( id->pp_filter[0] );
1547 if( p_stream->p_sys->b_deinterlace )
1551 /* Run filter chain */
1552 for( i = 0; i < id->i_filter; i++ )
1554 p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1561 /* Check if we have a subpicture to overlay */
1562 if( p_sys->p_filter_blend )
1564 p_subpic = transcode_spu_get( p_stream, id, p_pic->date );
1565 /* TODO: get another pic */
1568 /* Overlay subpicture */
1571 int i_width, i_height;
1573 p_sys->p_filter_blend->fmt_out = id->p_encoder->fmt_in;
1574 p_sys->p_filter_blend->fmt_out.video.i_visible_width =
1575 p_sys->p_filter_blend->fmt_out.video.i_width;
1576 p_sys->p_filter_blend->fmt_out.video.i_visible_height =
1577 p_sys->p_filter_blend->fmt_out.video.i_height;
1578 p_sys->p_filter_blend->fmt_out.video.i_chroma =
1579 VLC_FOURCC('I','4','2','0');
1581 i_width = id->p_encoder->fmt_in.video.i_width;
1582 i_height = id->p_encoder->fmt_in.video.i_height;
1584 while( p_subpic != NULL )
1586 subpicture_region_t *p_region = p_subpic->p_region;
1588 while( p_region && p_sys->p_filter_blend &&
1589 p_sys->p_filter_blend->pf_video_blend )
1591 int i_x_offset = p_region->i_x + p_subpic->i_x;
1592 int i_y_offset = p_region->i_y + p_subpic->i_y;
1594 if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
1596 i_y_offset = i_height - p_region->fmt.i_height -
1599 else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
1601 i_y_offset = i_height / 2 - p_region->fmt.i_height / 2;
1604 if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
1606 i_x_offset = i_width - p_region->fmt.i_width -
1609 else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
1611 i_x_offset = i_width / 2 - p_region->fmt.i_width / 2;
1614 if( p_subpic->b_absolute )
1616 i_x_offset = p_region->i_x + p_subpic->i_x;
1617 i_y_offset = p_region->i_y + p_subpic->i_y;
1620 p_sys->p_filter_blend->fmt_in.video = p_region->fmt;
1622 p_sys->p_filter_blend->pf_video_blend(
1623 p_sys->p_filter_blend, p_pic, p_pic,
1624 &p_region->picture, i_x_offset, i_y_offset );
1626 p_region = p_region->p_next;
1629 p_subpic = p_subpic->p_next;
1633 if( p_sys->i_threads >= 1 )
1635 vlc_mutex_lock( &p_sys->lock_out );
1636 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1637 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1638 *out = p_sys->p_buffers;
1639 p_sys->p_buffers = NULL;
1640 vlc_cond_signal( &p_sys->cond );
1641 vlc_mutex_unlock( &p_sys->lock_out );
1646 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1647 block_ChainAppend( out, p_block );
1649 if( p_sys->b_audio_sync && i_duplicate > 1 )
1651 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1652 date_Increment( &id->interpolated_pts, 1 );
1653 p_pic->date = i_pts;
1654 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1655 block_ChainAppend( out, p_block );
1658 p_pic->pf_release( p_pic );
1665 static int EncoderThread( sout_stream_sys_t *p_sys )
1667 sout_stream_id_t *id = p_sys->id_video;
1671 while( !p_sys->b_die && !p_sys->b_error )
1675 vlc_mutex_lock( &p_sys->lock_out );
1676 while( p_sys->i_last_pic == p_sys->i_first_pic )
1678 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1679 if( p_sys->b_die || p_sys->b_error ) break;
1681 if( p_sys->b_die || p_sys->b_error )
1683 vlc_mutex_unlock( &p_sys->lock_out );
1687 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1688 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1689 vlc_mutex_unlock( &p_sys->lock_out );
1691 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1692 vlc_mutex_lock( &p_sys->lock_out );
1693 block_ChainAppend( &p_sys->p_buffers, p_block );
1694 vlc_mutex_unlock( &p_sys->lock_out );
1696 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1698 free( p_pic->p[i_plane].p_pixels );
1703 while( p_sys->i_last_pic != p_sys->i_first_pic )
1705 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1706 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1708 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1710 free( p_pic->p[i_plane].p_pixels );
1715 block_ChainRelease( p_sys->p_buffers );
1720 struct picture_sys_t
1725 static void video_release_buffer( picture_t *p_pic )
1727 if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
1729 video_del_buffer( p_pic->p_sys->p_dec, p_pic );
1731 else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
1734 static picture_t *video_new_buffer( decoder_t *p_dec )
1736 picture_t *p_pic = malloc( sizeof(picture_t) );
1738 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1739 vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
1740 p_dec->fmt_out.video.i_chroma,
1741 p_dec->fmt_out.video.i_width,
1742 p_dec->fmt_out.video.i_height,
1743 p_dec->fmt_out.video.i_aspect );
1745 if( !p_pic->i_planes )
1751 p_pic->pf_release = video_release_buffer;
1752 p_pic->p_sys = malloc( sizeof(picture_sys_t) );
1753 p_pic->p_sys->p_dec = p_dec;
1758 static void video_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1760 if( p_pic && p_pic->p_data ) free( p_pic->p_data );
1761 if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
1762 if( p_pic ) free( p_pic );
1765 static void video_link_picture( decoder_t *p_dec, picture_t *p_pic )
1767 p_pic->i_refcount++;
1770 static void video_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1772 video_release_buffer( p_pic );
1778 static subpicture_t *spu_new_buffer( decoder_t * );
1779 static void spu_del_buffer( decoder_t *, subpicture_t * );
1781 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1783 sout_stream_sys_t *p_sys = p_stream->p_sys;
1789 /* Initialization of decoder structures */
1790 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
1791 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
1792 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
1794 id->p_decoder->p_module =
1795 module_Need( id->p_decoder, "decoder", "$codec", 0 );
1797 if( !id->p_decoder->p_module )
1799 msg_Err( p_stream, "cannot find decoder" );
1800 return VLC_EGENERIC;
1803 if( !p_sys->b_soverlay )
1809 /* Initialization of encoder format structures */
1810 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1811 id->p_decoder->fmt_in.i_codec );
1813 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
1815 id->p_encoder->p_module =
1816 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
1818 if( !id->p_encoder->p_module )
1820 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1821 msg_Err( p_stream, "cannot find encoder" );
1822 return VLC_EGENERIC;
1827 p_sys->p_filter_blend =
1828 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1829 vlc_object_attach( p_sys->p_filter_blend, p_stream );
1830 p_sys->p_filter_blend->fmt_out.video.i_chroma =
1831 VLC_FOURCC('I','4','2','0');
1832 p_sys->p_filter_blend->fmt_in.video.i_chroma =
1833 VLC_FOURCC('Y','U','V','A');
1834 p_sys->p_filter_blend->p_module =
1835 module_Need( p_sys->p_filter_blend, "video blending", 0, 0 );
1841 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
1843 sout_stream_sys_t *p_sys = p_stream->p_sys;
1847 if( id->p_decoder->p_module )
1848 module_Unneed( id->p_decoder, id->p_decoder->p_module );
1851 if( id->p_encoder->p_module )
1852 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1854 /* Free subpictures */
1855 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
1857 if( !p_sys->pp_subpics[i] ) continue;
1859 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
1860 p_sys->pp_subpics[i] = NULL;
1864 static int transcode_spu_process( sout_stream_t *p_stream,
1865 sout_stream_id_t *id,
1866 block_t *in, block_t **out )
1868 sout_stream_sys_t *p_sys = p_stream->p_sys;
1869 subpicture_t *p_subpic;
1872 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
1873 if( p_subpic && p_sys->b_soverlay )
1877 /* Find a free slot in our supictures ring buffer */
1878 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
1880 if( !p_sys->pp_subpics[i] )
1882 p_sys->pp_subpics[i] = p_subpic;
1886 if( i == SUBPICTURE_RING_SIZE )
1888 spu_del_buffer( id->p_decoder, p_subpic );
1892 if( p_subpic && !p_sys->b_soverlay )
1896 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
1897 spu_del_buffer( id->p_decoder, p_subpic );
1901 block_ChainAppend( out, p_block );
1906 return VLC_EGENERIC;
1909 static subpicture_t *transcode_spu_get( sout_stream_t *p_stream,
1910 sout_stream_id_t *id,
1911 mtime_t display_date )
1913 sout_stream_sys_t *p_sys = p_stream->p_sys;
1914 subpicture_t *p_subpic = 0;
1915 subpicture_t *p_ephemer = 0;
1916 subpicture_t **pp_subpic = &p_subpic;
1919 /* Find current subpictures and remove old ones */
1920 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
1922 if( !p_sys->pp_subpics[i] ) continue;
1924 if( !p_sys->pp_subpics[i]->b_ephemer &&
1925 p_sys->pp_subpics[i]->i_stop < display_date )
1927 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
1928 p_sys->pp_subpics[i] = NULL;
1932 if( p_sys->pp_subpics[i]->i_start > display_date ) continue;
1934 if( p_sys->pp_subpics[i]->b_ephemer && !p_ephemer )
1936 p_ephemer = p_sys->pp_subpics[i];
1938 else if( p_sys->pp_subpics[i]->b_ephemer )
1940 if( p_ephemer->i_start < p_sys->pp_subpics[i]->i_start )
1944 *p_ephemer = *p_sys->pp_subpics[i];
1945 *p_sys->pp_subpics[i] = tmp;
1948 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
1949 p_sys->pp_subpics[i] = NULL;
1953 /* Add subpicture to the list */
1954 *pp_subpic = p_sys->pp_subpics[i];
1955 pp_subpic = &p_sys->pp_subpics[i]->p_next;
1961 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1963 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1964 memset( p_subpic, 0, sizeof(subpicture_t) );
1965 p_subpic->b_absolute = VLC_TRUE;
1967 p_subpic->pf_create_region = __spu_CreateRegion;
1968 p_subpic->pf_destroy_region = __spu_DestroyRegion;
1973 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1975 while( p_subpic->p_region )
1977 subpicture_region_t *p_region = p_subpic->p_region;
1978 p_subpic->p_region = p_region->p_next;
1979 p_subpic->pf_destroy_region( VLC_OBJECT(p_dec), p_region );