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 set_description( _("Transcode stream output") );
140 set_capability( "sout stream", 50 );
141 add_shortcut( "transcode" );
142 set_callbacks( Open, Close );
144 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
145 VENC_LONGTEXT, VLC_FALSE );
146 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
147 VCODEC_LONGTEXT, VLC_FALSE );
148 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
149 VB_LONGTEXT, VLC_FALSE );
150 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
151 SCALE_LONGTEXT, VLC_FALSE );
152 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
153 FPS_LONGTEXT, VLC_FALSE );
154 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
155 DEINTERLACE_LONGTEXT, VLC_FALSE );
156 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
157 WIDTH_LONGTEXT, VLC_TRUE );
158 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
159 HEIGHT_LONGTEXT, VLC_TRUE );
161 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
162 CROPTOP_LONGTEXT, VLC_TRUE );
163 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
164 CROPLEFT_LONGTEXT, VLC_TRUE );
165 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
166 CROPBOTTOM_LONGTEXT, VLC_TRUE );
167 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
168 CROPRIGHT_LONGTEXT, VLC_TRUE );
170 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
171 AENC_LONGTEXT, VLC_FALSE );
172 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
173 ACODEC_LONGTEXT, VLC_FALSE );
174 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
175 AB_LONGTEXT, VLC_FALSE );
176 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
177 ACHANS_LONGTEXT, VLC_FALSE );
178 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
179 ARATE_LONGTEXT, VLC_TRUE );
181 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
182 SENC_LONGTEXT, VLC_FALSE );
183 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
184 SCODEC_LONGTEXT, VLC_FALSE );
185 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
186 SCODEC_LONGTEXT, VLC_FALSE );
188 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
189 THREADS_LONGTEXT, VLC_TRUE );
191 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
192 ASYNC_LONGTEXT, VLC_FALSE );
195 static const char *ppsz_sout_options[] = {
196 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
197 "scale", "fps", "width", "height", "deinterlace", "threads",
198 "aenc", "acodec", "ab", "samplerate", "channels",
199 "senc", "scodec", "soverlay",
203 /*****************************************************************************
204 * Exported prototypes
205 *****************************************************************************/
206 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
207 static int Del ( sout_stream_t *, sout_stream_id_t * );
208 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
210 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
211 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
212 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
214 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
215 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
216 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
218 static int transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
220 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
221 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
222 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
223 block_t *, block_t ** );
224 static subpicture_t *transcode_spu_get( sout_stream_t *, sout_stream_id_t *,
227 static int EncoderThread( struct sout_stream_sys_t * p_sys );
229 static int pi_channels_maps[6] =
232 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
233 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
234 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
235 | AOUT_CHAN_REARRIGHT,
236 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
237 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
240 #define PICTURE_RING_SIZE 64
241 #define SUBPICTURE_RING_SIZE 20
243 struct sout_stream_sys_t
247 sout_stream_t * p_out;
248 sout_stream_id_t * id_video;
250 vlc_mutex_t lock_out;
252 picture_t * pp_pics[PICTURE_RING_SIZE];
253 int i_first_pic, i_last_pic;
256 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
258 sout_cfg_t *p_audio_cfg;
264 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
266 sout_cfg_t *p_video_cfg;
272 vlc_bool_t b_deinterlace;
282 vlc_bool_t b_input_has_b_frames;
284 mtime_t i_output_pts;
287 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
289 vlc_bool_t b_soverlay;
290 sout_cfg_t *p_spu_cfg;
291 subpicture_t *pp_subpics[SUBPICTURE_RING_SIZE];
294 filter_t *p_filter_blend;
297 vlc_bool_t b_audio_sync;
298 mtime_t i_master_drift;
301 /*****************************************************************************
303 *****************************************************************************/
304 static int Open( vlc_object_t *p_this )
306 sout_stream_t *p_stream = (sout_stream_t*)p_this;
307 sout_stream_sys_t *p_sys;
311 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
313 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
316 msg_Err( p_stream, "cannot create chain" );
321 p_sys->b_input_has_b_frames = VLC_FALSE;
322 p_sys->i_output_pts = 0;
323 p_sys->i_master_drift = 0;
325 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
328 /* Audio transcoding parameters */
329 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
330 p_sys->psz_aenc = NULL;
331 p_sys->p_audio_cfg = NULL;
332 if( val.psz_string && *val.psz_string )
335 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
337 if( psz_next ) free( psz_next );
339 if( val.psz_string ) free( val.psz_string );
341 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
343 if( val.psz_string && *val.psz_string )
346 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
347 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
349 if( val.psz_string ) free( val.psz_string );
351 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
352 p_sys->i_abitrate = val.i_int;
353 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
355 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
356 p_sys->i_sample_rate = val.i_int;
358 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
359 p_sys->i_channels = val.i_int;
361 if( p_sys->i_acodec )
363 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
364 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
365 p_sys->i_channels, p_sys->i_abitrate / 1000 );
368 /* Video transcoding parameters */
369 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
370 p_sys->psz_venc = NULL;
371 p_sys->p_video_cfg = NULL;
372 if( val.psz_string && *val.psz_string )
375 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
377 if( psz_next ) free( psz_next );
379 if( val.psz_string ) free( val.psz_string );
381 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
383 if( val.psz_string && *val.psz_string )
386 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
387 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
389 if( val.psz_string ) free( val.psz_string );
391 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
392 p_sys->i_vbitrate = val.i_int;
393 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
395 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
396 p_sys->f_scale = val.f_float;
398 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
399 p_sys->f_fps = val.f_float;
401 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
402 p_sys->i_width = val.i_int;
404 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
405 p_sys->i_height = val.i_int;
407 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
408 p_sys->b_deinterlace = val.b_bool;
410 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
411 p_sys->i_crop_top = val.i_int;
412 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
413 p_sys->i_crop_bottom = val.i_int;
414 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
415 p_sys->i_crop_left = val.i_int;
416 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
417 p_sys->i_crop_right = val.i_int;
419 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
420 p_sys->i_threads = val.i_int;
422 if( p_sys->i_vcodec )
424 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
425 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
426 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
429 /* Subpictures transcoding parameters */
430 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
431 p_sys->psz_senc = NULL;
432 p_sys->p_spu_cfg = NULL;
433 if( val.psz_string && *val.psz_string )
436 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
438 if( psz_next ) free( psz_next );
440 if( val.psz_string ) free( val.psz_string );
442 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
444 if( val.psz_string && *val.psz_string )
447 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
448 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
450 if( val.psz_string ) free( val.psz_string );
452 if( p_sys->i_scodec )
454 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_acodec );
457 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
458 p_sys->b_soverlay = val.b_bool;
459 p_sys->p_filter_blend = 0;
461 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
463 p_sys->pp_subpics[i] = 0;
466 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
467 p_sys->b_audio_sync = val.b_bool;
468 if( p_sys->f_fps > 0 ) p_sys->b_audio_sync = VLC_TRUE;
470 p_stream->pf_add = Add;
471 p_stream->pf_del = Del;
472 p_stream->pf_send = Send;
473 p_stream->p_sys = p_sys;
476 avcodec_register_all();
481 /*****************************************************************************
483 *****************************************************************************/
484 static void Close( vlc_object_t * p_this )
486 sout_stream_t *p_stream = (sout_stream_t*)p_this;
487 sout_stream_sys_t *p_sys = p_stream->p_sys;
489 sout_StreamDelete( p_sys->p_out );
491 while( p_sys->p_audio_cfg != NULL )
493 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
495 if( p_sys->p_audio_cfg->psz_name )
496 free( p_sys->p_audio_cfg->psz_name );
497 if( p_sys->p_audio_cfg->psz_value )
498 free( p_sys->p_audio_cfg->psz_value );
499 free( p_sys->p_audio_cfg );
501 p_sys->p_audio_cfg = p_next;
503 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
505 while( p_sys->p_video_cfg != NULL )
507 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
509 if( p_sys->p_video_cfg->psz_name )
510 free( p_sys->p_video_cfg->psz_name );
511 if( p_sys->p_video_cfg->psz_value )
512 free( p_sys->p_video_cfg->psz_value );
513 free( p_sys->p_video_cfg );
515 p_sys->p_video_cfg = p_next;
517 if( p_sys->psz_venc ) free( p_sys->psz_venc );
519 while( p_sys->p_spu_cfg != NULL )
521 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
523 if( p_sys->p_spu_cfg->psz_name )
524 free( p_sys->p_spu_cfg->psz_name );
525 if( p_sys->p_spu_cfg->psz_value )
526 free( p_sys->p_spu_cfg->psz_value );
527 free( p_sys->p_spu_cfg );
529 p_sys->p_spu_cfg = p_next;
531 if( p_sys->psz_senc ) free( p_sys->psz_senc );
533 if( p_sys->p_filter_blend )
535 if( p_sys->p_filter_blend->p_module )
536 module_Unneed( p_sys->p_filter_blend,
537 p_sys->p_filter_blend->p_module );
539 vlc_object_detach( p_sys->p_filter_blend );
540 vlc_object_destroy( p_sys->p_filter_blend );
543 vlc_object_destroy( p_sys );
546 struct sout_stream_id_t
548 vlc_fourcc_t b_transcode;
549 es_format_t f_src; /* only if transcoding */
550 es_format_t f_dst; /* " " " */
551 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
553 /* id of the out stream */
557 decoder_t *p_decoder;
560 encoder_t *p_encoder;
561 vlc_fourcc_t b_enc_inited;
565 AVCodecContext *ff_dec_c;
575 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
576 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
577 AVFrame *p_ff_pic_tmp2; /* to do resample */
578 AVFrame *p_ff_pic_tmp3; /* to do subpicture overlay */
580 ImgReSampleContext *p_vresample;
583 date_t interpolated_pts;
584 mtime_t i_initial_pts;
588 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
590 sout_stream_sys_t *p_sys = p_stream->p_sys;
591 sout_stream_id_t *id;
593 id = malloc( sizeof( sout_stream_id_t ) );
594 memset( id, 0, sizeof(sout_stream_id_t) );
597 id->p_decoder = NULL;
598 id->p_encoder = NULL;
600 /* Get source format */
603 /* Create destination format */
604 es_format_Init( &id->f_dst, p_fmt->i_cat, 0 );
605 id->f_dst.i_id = id->f_src.i_id;
606 id->f_dst.i_group = id->f_src.i_group;
607 if( id->f_src.psz_language )
608 id->f_dst.psz_language = strdup( id->f_src.psz_language );
610 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
613 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
614 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
616 /* Complete destination format */
617 id->f_dst.i_codec = p_sys->i_acodec;
618 id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ?
619 p_sys->i_sample_rate : id->f_src.audio.i_rate;
620 id->f_dst.audio.i_channels = p_sys->i_channels > 0 ?
621 p_sys->i_channels : id->f_src.audio.i_channels;
622 id->f_dst.i_bitrate = p_sys->i_abitrate;
623 id->f_dst.audio.i_bitspersample = id->f_src.audio.i_bitspersample;
625 /* build decoder -> filter -> encoder */
626 if( transcode_audio_ffmpeg_new( p_stream, id ) )
628 msg_Err( p_stream, "cannot create audio chain" );
633 /* open output stream */
634 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
635 id->b_transcode = VLC_TRUE;
643 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
645 else if( p_fmt->i_cat == VIDEO_ES &&
646 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
649 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
650 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
652 /* Complete destination format */
653 id->f_dst.i_codec = p_sys->i_vcodec;
654 id->f_dst.video.i_width = p_sys->i_width;
655 id->f_dst.video.i_height = p_sys->i_height;
656 id->f_dst.i_bitrate = p_sys->i_vbitrate;
658 /* build decoder -> filter -> encoder */
659 if( transcode_video_ffmpeg_new( p_stream, id ) )
661 msg_Err( p_stream, "cannot create video chain" );
666 /* open output stream */
667 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
669 id->b_transcode = VLC_TRUE;
671 if( id->f_dst.video.i_frame_rate && id->f_dst.video.i_frame_rate_base )
673 date_Init( &id->interpolated_pts, id->f_dst.video.i_frame_rate,
674 id->f_dst.video.i_frame_rate_base );
676 else if( p_sys->b_audio_sync )
678 msg_Warn( p_stream, "no video frame rate available, disabling "
680 p_sys->b_audio_sync = VLC_FALSE;
683 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
685 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
686 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
687 (char*)&p_sys->i_scodec );
689 /* Complete destination format */
690 id->f_dst.i_codec = p_sys->i_scodec;
692 /* build decoder -> filter -> encoder */
693 if( transcode_spu_new( p_stream, id ) )
695 msg_Err( p_stream, "cannot create subtitles chain" );
700 /* open output stream */
701 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
702 id->b_transcode = VLC_TRUE;
710 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
712 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
713 (char*)&p_fmt->i_codec );
715 id->b_transcode = VLC_TRUE;
717 /* build decoder -> filter -> encoder */
718 if( transcode_spu_new( p_stream, id ) )
720 msg_Err( p_stream, "cannot create subtitles chain" );
727 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
728 (char*)&p_fmt->i_codec );
729 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
730 id->b_transcode = VLC_FALSE;
742 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
744 sout_stream_sys_t *p_sys = p_stream->p_sys;
746 if( id->b_transcode )
748 if( id->f_src.i_cat == AUDIO_ES )
750 transcode_audio_ffmpeg_close( p_stream, id );
752 else if( id->f_src.i_cat == VIDEO_ES )
754 transcode_video_ffmpeg_close( p_stream, id );
756 else if( id->f_src.i_cat == SPU_ES )
758 transcode_spu_close( p_stream, id );
762 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
768 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
771 sout_stream_sys_t *p_sys = p_stream->p_sys;
773 if( id->b_transcode )
775 block_t *p_buffer_out;
777 /* Be sure to have padding */
778 p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
779 if( p_buffer == NULL )
783 p_buffer->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
784 memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
786 if( id->f_src.i_cat == AUDIO_ES )
788 transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
790 block_Release( p_buffer );
792 else if( id->f_src.i_cat == VIDEO_ES )
794 if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
795 &p_buffer_out ) != VLC_SUCCESS )
797 block_Release( p_buffer );
800 block_Release( p_buffer );
802 else if( id->f_src.i_cat == SPU_ES )
804 if( transcode_spu_process( p_stream, id, p_buffer,
805 &p_buffer_out ) != VLC_SUCCESS )
812 block_Release( p_buffer );
817 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
821 else if( id->id != NULL )
823 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
827 block_Release( p_buffer );
832 /****************************************************************************
833 * ffmpeg decoder reencoder part
834 ****************************************************************************/
839 } fourcc_to_ff_code[] =
842 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
843 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
844 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
845 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
846 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
847 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
848 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
849 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
850 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
853 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
854 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
855 #if LIBAVCODEC_BUILD >= 4676
856 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
858 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
859 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
860 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
861 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
862 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
863 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
864 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
865 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
866 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
867 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
868 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
869 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
870 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
871 #if LIBAVCODEC_BUILD >= 4666
872 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
873 { VLC_FOURCC( 'h', '2', '6', '4' ), CODEC_ID_H264 },
875 #if LIBAVCODEC_BUILD >= 4719
876 { VLC_FOURCC( 'S', 'N', 'O', 'W' ), CODEC_ID_SNOW },
879 /* raw video code, only used for 'encoding' */
880 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
881 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
882 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
883 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
884 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
885 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
886 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
887 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
888 { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
889 { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
891 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
894 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
898 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
900 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
902 return fourcc_to_ff_code[i].i_ff_codec;
909 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
913 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
914 case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
915 case VLC_FOURCC( 'I', '4', '2', '0' ):
916 return PIX_FMT_YUV420P;
917 case VLC_FOURCC( 'I', '4', '2', '2' ):
918 return PIX_FMT_YUV422P;
919 case VLC_FOURCC( 'I', '4', '4', '4' ):
920 return PIX_FMT_YUV444P;
921 case VLC_FOURCC( 'R', 'V', '1', '5' ):
922 return PIX_FMT_RGB555;
923 case VLC_FOURCC( 'R', 'V', '1', '6' ):
924 return PIX_FMT_RGB565;
925 case VLC_FOURCC( 'R', 'V', '2', '4' ):
926 return PIX_FMT_BGR24;
927 case VLC_FOURCC( 'R', 'V', '3', '2' ):
928 return PIX_FMT_RGBA32;
929 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
930 return PIX_FMT_GRAY8;
931 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
932 return PIX_FMT_YUV422;
938 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
942 case PIX_FMT_YUV420P:
943 return VLC_FOURCC('I','4','2','0');
944 case PIX_FMT_YUV422P:
945 return VLC_FOURCC('I','4','2','2');
946 case PIX_FMT_YUV444P:
947 return VLC_FOURCC('I','4','4','4');
950 return VLC_FOURCC('Y','U','Y','2');
953 return VLC_FOURCC('R','V','1','5');
955 return VLC_FOURCC('R','V','1','6');
957 return VLC_FOURCC('R','V','2','4');
959 return VLC_FOURCC('R','V','3','2');
961 return VLC_FOURCC('G','R','E','Y');
963 case PIX_FMT_YUV410P:
964 case PIX_FMT_YUV411P:
971 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
972 sout_stream_id_t *id )
976 if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
977 id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
978 id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
979 id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
983 id->ff_dec_c = avcodec_alloc_context();
984 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
985 id->ff_dec_c->channels = id->f_src.audio.i_channels;
986 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
987 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
992 i_ff_codec = get_ff_codec( id->f_src.i_codec );
993 if( i_ff_codec == 0 )
995 msg_Err( p_stream, "cannot find decoder id" );
999 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1002 msg_Err( p_stream, "cannot find decoder (avcodec)" );
1003 return VLC_EGENERIC;
1006 id->ff_dec_c = avcodec_alloc_context();
1007 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
1008 id->ff_dec_c->channels = id->f_src.audio.i_channels;
1009 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
1010 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
1012 id->ff_dec_c->extradata_size = id->f_src.i_extra;
1013 id->ff_dec_c->extradata = id->f_src.p_extra;
1014 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
1016 msg_Err( p_stream, "cannot open decoder" );
1017 av_free( id->ff_dec_c );
1018 return VLC_EGENERIC;
1022 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1023 id->i_buffer_pos = 0;
1024 id->p_buffer = malloc( id->i_buffer );
1026 /* Sanity check for audio channels */
1027 id->f_dst.audio.i_channels =
1028 __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
1031 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1033 /* Initialization of encoder format structures */
1034 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1035 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1036 id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
1037 id->p_encoder->fmt_in.audio.i_physical_channels =
1038 id->p_encoder->fmt_in.audio.i_original_channels =
1039 pi_channels_maps[id->f_dst.audio.i_channels];
1040 id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
1041 id->p_encoder->fmt_in.audio.i_bitspersample = 16;
1043 id->p_encoder->fmt_out = id->f_dst;
1045 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1047 /* Attach object to parent so object variables inheritance works */
1048 vlc_object_attach( id->p_encoder, p_stream );
1050 id->p_encoder->p_module =
1051 module_Need( id->p_encoder, "encoder",
1052 p_stream->p_sys->psz_aenc, VLC_TRUE );
1053 if( !id->p_encoder->p_module )
1055 vlc_object_detach( id->p_encoder );
1056 vlc_object_destroy( id->p_encoder );
1057 msg_Err( p_stream, "cannot open encoder" );
1058 av_free( id->ff_dec_c );
1059 return VLC_EGENERIC;
1062 id->b_enc_inited = VLC_FALSE;
1064 id->f_dst = id->p_encoder->fmt_out;
1066 /* Hack for mp3 transcoding support */
1067 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1069 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
1075 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
1076 sout_stream_id_t *id )
1078 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1079 av_free( id->ff_dec_c );
1081 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1083 vlc_object_detach( id->p_encoder );
1084 vlc_object_destroy( id->p_encoder );
1086 free( id->p_buffer );
1089 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
1090 sout_stream_id_t *id,
1094 sout_stream_sys_t *p_sys = p_stream->p_sys;
1095 aout_buffer_t aout_buf;
1097 int i_buffer = in->i_buffer;
1098 char *p_buffer = in->p_buffer;
1099 id->i_dts = in->i_dts;
1104 id->i_buffer_pos = 0;
1106 /* decode as much data as possible */
1111 i_used = avcodec_decode_audio( id->ff_dec_c,
1112 (int16_t*)id->p_buffer, &id->i_buffer_pos,
1113 p_buffer, i_buffer );
1116 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
1121 msg_Warn( p_stream, "error audio decoding");
1128 if ( id->i_buffer_pos < 0 )
1130 msg_Warn( p_stream, "weird error audio decoding");
1136 int16_t *sout = (int16_t*)id->p_buffer;
1138 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
1139 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
1141 int8_t *sin = (int8_t*)p_buffer;
1142 int i_used = __MIN( id->i_buffer/2, i_buffer );
1143 int i_samples = i_used;
1145 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
1146 while( i_samples > 0 )
1148 *sout++ = ( *sin++ ) << 8;
1152 while( i_samples > 0 )
1154 *sout++ = ( *sin++ - 128 ) << 8;
1160 id->i_buffer_pos = i_used * 2;
1162 else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
1163 id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
1165 int16_t *sin = (int16_t*)p_buffer;
1166 int i_used = __MIN( id->i_buffer, i_buffer );
1167 int i_samples = i_used / 2;
1170 memcpy( sout, sin, i_used );
1172 #ifdef WORDS_BIGENDIAN
1173 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
1175 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
1178 uint8_t *dat = (uint8_t*)sout;
1180 while( i_samples > 0 )
1195 id->i_buffer_pos = i_used;
1199 if( id->i_buffer_pos == 0 ) continue;
1201 /* Encode as much data as possible */
1202 if( !id->b_enc_inited && id->p_encoder->pf_header )
1204 block_t *p_block_tmp;
1206 p_block = id->p_encoder->pf_header( id->p_encoder );
1207 p_block_tmp = p_block;
1208 while( p_block_tmp )
1210 p_block_tmp->i_dts = p_block_tmp->i_pts = in->i_dts;
1211 p_block_tmp = p_block_tmp->p_next;
1213 block_ChainAppend( out, p_block );
1215 id->b_enc_inited = VLC_TRUE;
1218 aout_buf.p_buffer = id->p_buffer;
1219 aout_buf.i_nb_bytes = id->i_buffer_pos;
1220 aout_buf.i_nb_samples = id->i_buffer_pos/2/id->f_src.audio.i_channels;
1221 aout_buf.start_date = id->i_dts;
1222 aout_buf.end_date = id->i_dts;
1224 if( p_sys->b_audio_sync )
1226 aout_buf.start_date = date_Get( &id->interpolated_pts ) + 1;
1227 p_sys->i_master_drift = id->i_dts - aout_buf.start_date;
1228 date_Increment( &id->interpolated_pts, aout_buf.i_nb_samples );
1231 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
1232 id->f_src.audio.i_channels / id->f_src.audio.i_rate );
1234 if( id->p_encoder->fmt_in.audio.i_channels == 1 &&
1235 id->f_src.audio.i_channels > 1 )
1237 int16_t *p_sample = (int16_t *)aout_buf.p_buffer;
1238 int i_src_c = id->f_src.audio.i_channels;
1241 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1245 for( j = 1; j < i_src_c; j++ )
1247 c += p_sample[i_src_c * i + j];
1249 p_sample[i] = c / (i_src_c-1);
1251 aout_buf.i_nb_bytes = i * 2;
1253 else if( id->p_encoder->fmt_in.audio.i_channels == 2 &&
1254 id->f_src.audio.i_channels > 2 )
1256 int i_src_c = id->f_src.audio.i_channels;
1259 static const float mixf_l[4][6] = /* [i_src_c - 3][channel index] */
1261 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1262 { 0.00, 0.50, 0.50, 0.00, 0.00, 0.00 }, /* 4 channels */
1263 { 0.00, 0.50, 0.00, 0.50, 0.00, 0.00 }, /* 5 channels */
1264 { 0.00, 0.34, 0.33, 0.00, 0.33, 0.00 }, /* 6 channels */
1266 static const float mixf_r[4][6] = /* [i_src_c - 3][channel index] */
1268 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1269 { 0.00, 0.00, 0.50, 0.50, 0.00, 0.00 }, /* 4 channels */
1270 { 0.00, 0.00, 0.50, 0.00, 0.50, 0.00 }, /* 5 channels */
1271 { 0.00, 0.00, 0.33, 0.34, 0.00, 0.33 }, /* 6 channels */
1275 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1277 int16_t *p_src = (int16_t *)aout_buf.p_buffer + i_src_c * i;
1278 int16_t *p_dst = (int16_t *)aout_buf.p_buffer + 2 * i;
1281 float l = 0.0, r = 0.0;
1282 for( j = 0; j < i_src_c; j++ )
1284 l += mixf_l[i_src_c-3][j] * p_src[j];
1285 r += mixf_r[i_src_c-3][j] * p_src[j];
1288 p_dst[0] = (int)( l + 0.5 );
1289 p_dst[1] = (int)( r + 0.5 );
1291 aout_buf.i_nb_bytes = i * 2 * 2;
1293 else if( id->f_src.audio.i_channels !=
1294 id->p_encoder->fmt_in.audio.i_channels )
1299 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
1300 static const int translation[7][6] =
1301 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
1302 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
1303 { 0, 1, 0, 0, 0, 0 }, /* 2 */
1304 { 1, 2, 0, 0, 0, 0 }, /* 3 */
1305 { 1, 3, 2, 0, 0, 0 }, /* 4 */
1306 { 1, 3, 4, 2, 0, 0 }, /* 5 */
1307 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
1309 /* dumb downmixing */
1310 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1312 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
1313 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
1315 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
1316 p_buffer[i*id->f_src.audio.i_channels+
1317 translation[id->f_src.audio.i_channels][j]];
1320 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1323 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1324 block_ChainAppend( out, p_block );
1334 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1335 sout_stream_id_t *id )
1337 sout_stream_sys_t *p_sys = p_stream->p_sys;
1341 if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1342 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1343 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1344 id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1345 id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1346 id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1347 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1348 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1349 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1350 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1351 id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1354 id->ff_dec_c = avcodec_alloc_context();
1355 id->ff_dec_c->width = id->f_src.video.i_width;
1356 id->ff_dec_c->height = id->f_src.video.i_height;
1357 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_codec );
1359 #if LIBAVCODEC_BUILD >= 4687
1360 if( id->ff_dec_c->width )
1361 id->ff_dec_c->sample_aspect_ratio =
1362 av_d2q( id->f_src.video.i_aspect / (double)VOUT_ASPECT_FACTOR *
1363 id->ff_dec_c->height / id->ff_dec_c->width, 255 );
1365 id->ff_dec_c->aspect_ratio =
1366 id->f_src.video.i_aspect / (float)VOUT_ASPECT_FACTOR;
1372 i_ff_codec = get_ff_codec( id->f_src.i_codec );
1373 if( i_ff_codec == 0 )
1375 msg_Err( p_stream, "cannot find decoder" );
1376 return VLC_EGENERIC;
1379 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1382 msg_Err( p_stream, "cannot find decoder" );
1383 return VLC_EGENERIC;
1386 id->ff_dec_c = avcodec_alloc_context();
1387 id->ff_dec_c->width = id->f_src.video.i_width;
1388 id->ff_dec_c->height = id->f_src.video.i_height;
1389 id->ff_dec_c->bits_per_sample=id->f_src.video.i_bits_per_pixel;
1390 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1392 if( id->f_src.i_extra > 0 )
1394 if( i_ff_codec == CODEC_ID_SVQ3 )
1396 int i_size = id->f_src.i_extra;
1399 id->ff_dec_c->extradata_size = i_size + 12;
1400 p = id->ff_dec_c->extradata = malloc( i_size + 12 );
1402 memcpy( &p[0], "SVQ3", 4 );
1403 memset( &p[4], 0, 8 );
1404 memcpy( &p[12], id->f_src.p_extra, i_size );
1406 /* Now remove all atoms before the SMI one */
1407 if( id->ff_dec_c->extradata_size > 0x5a && strncmp( &p[0x56], "SMI ", 4 ) )
1409 uint8_t *psz = &p[0x52];
1411 while( psz < &p[id->ff_dec_c->extradata_size - 8] )
1413 int i_size = GetDWBE( psz );
1416 /* FIXME handle 1 as long size */
1419 if( !strncmp( &psz[4], "SMI ", 4 ) )
1421 memmove( &p[0x52], psz, &p[id->ff_dec_c->extradata_size] - psz );
1430 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1431 id->ff_dec_c->extradata = malloc( id->f_src.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
1433 memcpy( id->ff_dec_c->extradata, id->f_src.p_extra, id->f_src.i_extra );
1434 memset( (uint8_t*)id->ff_dec_c->extradata + id->f_src.i_extra, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1437 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1438 id->ff_dec_c->error_resilience= -1;
1439 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1440 id->ff_dec_c->opaque = p_sys;
1442 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1444 msg_Err( p_stream, "cannot open decoder" );
1445 av_free( id->ff_dec_c );
1446 return VLC_EGENERIC;
1451 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1453 /* Initialization of encoder format structures */
1454 es_format_Init( &id->p_encoder->fmt_in,
1455 id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1457 /* The dimensions will be set properly later on.
1458 * Just put sensible values so we can test if there is an encoder. */
1459 id->p_encoder->fmt_in.video.i_width =
1460 id->f_src.video.i_width ? id->f_src.video.i_width : 16;
1461 id->p_encoder->fmt_in.video.i_height =
1462 id->f_src.video.i_height ? id->f_src.video.i_height : 16;
1464 id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1465 id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1468 id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1469 #if LIBAVCODEC_BUILD >= 4662
1470 id->p_encoder->fmt_in.video.i_frame_rate_base =
1471 id->ff_dec_c->frame_rate_base;
1474 #if LIBAVCODEC_BUILD >= 4687
1475 if( id->ff_dec_c->height )
1476 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1477 ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1478 id->ff_dec_c->width / id->ff_dec_c->height );
1480 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1481 id->ff_dec_c->aspect_ratio;
1485 /* Override with user settings */
1486 if( p_sys->f_fps > 0 )
1488 id->p_encoder->fmt_in.video.i_frame_rate = p_sys->f_fps * 1000;
1489 id->p_encoder->fmt_in.video.i_frame_rate_base = 1000;
1492 id->f_dst.video.i_frame_rate = id->p_encoder->fmt_in.video.i_frame_rate;
1493 id->f_dst.video.i_frame_rate_base =
1494 id->p_encoder->fmt_in.video.i_frame_rate_base;
1496 /* Check whether a particular aspect ratio was requested */
1497 if( id->f_src.video.i_aspect )
1499 id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1500 id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1503 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1504 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1505 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1507 id->p_encoder->i_threads = p_sys->i_threads;
1509 id->p_ff_pic = avcodec_alloc_frame();
1510 id->p_ff_pic_tmp0 = NULL;
1511 id->p_ff_pic_tmp1 = NULL;
1512 id->p_ff_pic_tmp2 = NULL;
1513 id->p_ff_pic_tmp3 = NULL;
1514 id->p_vresample = NULL;
1516 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1518 /* Attach object to parent so object variables inheritance works */
1519 vlc_object_attach( id->p_encoder, p_stream );
1521 id->p_encoder->p_module =
1522 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1524 if( !id->p_encoder->p_module )
1526 vlc_object_detach( id->p_encoder );
1527 vlc_object_destroy( id->p_encoder );
1528 av_free( id->ff_dec_c );
1529 msg_Err( p_stream, "cannot find encoder" );
1530 return VLC_EGENERIC;
1533 /* Close the encoder.
1534 * We'll open it only when we have the first frame */
1535 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1536 id->p_encoder->p_module = NULL;
1538 id->b_enc_inited = VLC_FALSE;
1540 if ( p_sys->i_threads >= 1 )
1542 p_sys->id_video = id;
1543 vlc_mutex_init( p_stream, &p_sys->lock_out );
1544 vlc_cond_init( p_stream, &p_sys->cond );
1545 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1546 p_sys->i_first_pic = 0;
1547 p_sys->i_last_pic = 0;
1548 p_sys->p_buffers = NULL;
1549 p_sys->b_die = p_sys->b_error = 0;
1550 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1551 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1553 vlc_object_detach( id->p_encoder );
1554 vlc_object_destroy( id->p_encoder );
1555 av_free( id->ff_dec_c );
1556 msg_Err( p_stream, "cannot spawn encoder thread" );
1557 return VLC_EGENERIC;
1564 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1565 sout_stream_id_t *id )
1567 if ( p_stream->p_sys->i_threads >= 1 )
1569 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1570 p_stream->p_sys->b_die = 1;
1571 vlc_cond_signal( &p_stream->p_sys->cond );
1572 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1573 vlc_thread_join( p_stream->p_sys );
1574 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1575 vlc_cond_destroy( &p_stream->p_sys->cond );
1579 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1580 av_free( id->ff_dec_c );
1583 if( id->p_encoder->p_module )
1584 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1585 vlc_object_detach( id->p_encoder );
1586 vlc_object_destroy( id->p_encoder );
1591 free( id->p_ff_pic );
1594 if( id->p_ff_pic_tmp0 )
1596 free( id->p_ff_pic_tmp0->data[0] );
1597 free( id->p_ff_pic_tmp0 );
1599 if( id->p_ff_pic_tmp1 )
1601 free( id->p_ff_pic_tmp1->data[0] );
1602 free( id->p_ff_pic_tmp1 );
1604 if( id->p_ff_pic_tmp2 )
1606 free( id->p_ff_pic_tmp2->data[0] );
1607 free( id->p_ff_pic_tmp2 );
1609 if( id->p_ff_pic_tmp3 )
1611 free( id->p_ff_pic_tmp3->data[0] );
1612 free( id->p_ff_pic_tmp3 );
1614 if( id->p_vresample )
1616 img_resample_close( id->p_vresample );
1620 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1621 sout_stream_id_t *id, block_t *in, block_t **out )
1623 sout_stream_sys_t *p_sys = p_stream->p_sys;
1624 int i_used, b_gotpicture, i_duplicate = 1;
1633 i_data = in->i_buffer;
1634 p_data = in->p_buffer;
1641 subpicture_t *p_subpic = 0;
1644 frame = id->p_ff_pic;
1645 p_sys->i_input_pts = in->i_pts;
1646 p_sys->i_input_dts = in->i_dts;
1649 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1656 avpicture_fill( (AVPicture*)frame, p_data,
1657 id->ff_dec_c->pix_fmt,
1658 id->ff_dec_c->width, id->ff_dec_c->height );
1663 frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts :
1666 frame->pict_type = FF_I_TYPE;
1671 msg_Warn( p_stream, "error");
1672 return VLC_EGENERIC;
1682 /* Get the pts of the decoded frame if any, otherwise keep the
1683 * interpolated one */
1684 if( frame->pts != AV_NOPTS_VALUE )
1686 p_sys->i_output_pts = frame->pts;
1688 i_pts = p_sys->i_output_pts;
1690 /* Sanity check (seems to be needed for some streams ) */
1691 if( frame->pict_type == FF_B_TYPE )
1693 p_sys->b_input_has_b_frames = VLC_TRUE;
1696 if( p_sys->b_audio_sync )
1698 mtime_t i_video_drift;
1699 mtime_t i_master_drift = p_sys->i_master_drift;
1701 if( !id->i_initial_pts ) id->i_initial_pts = p_sys->i_output_pts;
1703 if( !i_master_drift )
1705 /* No audio track ? */
1706 i_master_drift = id->i_initial_pts;
1709 i_pts = date_Get( &id->interpolated_pts ) + 1;
1710 i_video_drift = p_sys->i_output_pts - i_pts;
1713 if( i_video_drift < i_master_drift - 50000 )
1715 msg_Dbg( p_stream, "dropping frame (%i)",
1716 (int)(i_video_drift - i_master_drift) );
1717 return VLC_EGENERIC;
1719 else if( i_video_drift > i_master_drift + 50000 )
1721 msg_Dbg( p_stream, "adding frame (%i)",
1722 (int)(i_video_drift - i_master_drift) );
1726 date_Increment( &id->interpolated_pts, 1 );
1729 if( !id->b_enc_inited )
1731 /* Hack because of the copy packetizer which can fail to detect the
1732 * proper size (which forces us to wait until the 1st frame
1734 int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1735 p_sys->i_crop_right;
1736 int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1737 p_sys->i_crop_bottom;
1739 if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1742 /* Apply the scaling */
1743 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1744 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1746 else if( id->f_dst.video.i_width > 0 &&
1747 id->f_dst.video.i_height <= 0 )
1749 id->f_dst.video.i_height =
1750 id->f_dst.video.i_width / (double)i_width * i_height;
1752 else if( id->f_dst.video.i_width <= 0 &&
1753 id->f_dst.video.i_height > 0 )
1755 id->f_dst.video.i_width =
1756 id->f_dst.video.i_height / (double)i_height * i_width;
1759 id->p_encoder->fmt_in.video.i_width = id->f_dst.video.i_width;
1760 id->p_encoder->fmt_in.video.i_height = id->f_dst.video.i_height;
1761 id->p_encoder->fmt_out = id->f_dst;
1763 id->p_encoder->p_module =
1764 module_Need( id->p_encoder, "encoder",
1765 p_sys->psz_venc, VLC_TRUE );
1766 if( !id->p_encoder->p_module )
1768 vlc_object_destroy( id->p_encoder );
1769 msg_Err( p_stream, "cannot find encoder" );
1770 id->b_transcode = VLC_FALSE;
1771 return VLC_EGENERIC;
1774 id->f_dst = id->p_encoder->fmt_out;
1776 /* Hack for mp2v/mp1v transcoding support */
1777 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1778 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1780 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1784 p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1787 msg_Err( p_stream, "cannot add this stream" );
1788 transcode_video_ffmpeg_close( p_stream, id );
1789 id->b_transcode = VLC_FALSE;
1790 return VLC_EGENERIC;
1793 if( id->p_encoder->pf_header )
1795 block_t *p_block_tmp;
1797 p_block = id->p_encoder->pf_header( id->p_encoder );
1798 p_block_tmp = p_block;
1799 while( p_block_tmp )
1801 p_block_tmp->i_dts = p_block_tmp->i_pts = in->i_dts;
1802 p_block_tmp = p_block_tmp->p_next;
1804 block_ChainAppend( out, p_block );
1807 id->i_inter_pixfmt =
1808 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1810 id->b_enc_inited = VLC_TRUE;
1814 if( p_stream->p_sys->b_deinterlace )
1816 if( id->p_ff_pic_tmp0 == NULL )
1820 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1821 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1822 id->ff_dec_c->width,
1823 id->ff_dec_c->height );
1825 buf = malloc( i_size );
1827 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1829 id->ff_dec_c->width, id->ff_dec_c->height );
1832 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0,
1833 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1834 id->ff_dec_c->width, id->ff_dec_c->height );
1836 #if LIBAVCODEC_BUILD >= 4685
1837 id->p_ff_pic_tmp0->interlaced_frame = 0;
1839 id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1840 frame = id->p_ff_pic_tmp0;
1843 /* convert pix format */
1844 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1846 if( id->p_ff_pic_tmp1 == NULL )
1850 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1851 i_size = avpicture_get_size( id->i_inter_pixfmt,
1852 id->ff_dec_c->width,
1853 id->ff_dec_c->height );
1855 buf = malloc( i_size );
1857 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1859 id->ff_dec_c->width, id->ff_dec_c->height );
1862 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1863 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1864 id->ff_dec_c->width, id->ff_dec_c->height );
1866 id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1867 #if LIBAVCODEC_BUILD >= 4685
1868 id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1869 id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1871 frame = id->p_ff_pic_tmp1;
1874 /* convert size and crop */
1875 if( id->ff_dec_c->width != id->f_dst.video.i_width ||
1876 id->ff_dec_c->height != id->f_dst.video.i_height ||
1877 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1878 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1880 if( id->p_ff_pic_tmp2 == NULL )
1884 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1885 i_size = avpicture_get_size( id->i_inter_pixfmt,
1886 id->f_dst.video.i_width,
1887 id->f_dst.video.i_height );
1889 buf = malloc( i_size );
1891 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1893 id->f_dst.video.i_width,
1894 id->f_dst.video.i_height );
1897 img_resample_full_init( id->f_dst.video.i_width,
1898 id->f_dst.video.i_height,
1899 id->ff_dec_c->width,
1900 id->ff_dec_c->height,
1901 p_stream->p_sys->i_crop_top,
1902 p_stream->p_sys->i_crop_bottom,
1903 p_stream->p_sys->i_crop_left,
1904 p_stream->p_sys->i_crop_right
1905 #if LIBAVCODEC_BUILD >= 4708
1912 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1913 (AVPicture*)frame );
1915 id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1916 #if LIBAVCODEC_BUILD >= 4685
1917 id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1918 id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1920 frame = id->p_ff_pic_tmp2;
1924 p_pic = malloc(sizeof(picture_t));
1925 vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1926 id->p_encoder->fmt_in.i_codec,
1927 id->f_dst.video.i_width, id->f_dst.video.i_height,
1928 id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1929 id->f_dst.video.i_height );
1931 /* Check if we have a subpicture to overlay */
1932 if( p_sys->p_filter_blend )
1934 p_subpic = transcode_spu_get( p_stream, id, p_sys->i_output_pts );
1936 if( p_subpic && frame != id->p_ff_pic_tmp0 &&
1937 frame != id->p_ff_pic_tmp1 && frame != id->p_ff_pic_tmp2 )
1939 if( id->p_ff_pic_tmp3 == NULL )
1941 uint8_t *buf = malloc( frame->linesize[0] *
1942 p_pic->p[0].i_lines * 3 );
1943 id->p_ff_pic_tmp3 = avcodec_alloc_frame();
1944 *id->p_ff_pic_tmp3 = *frame;
1945 id->p_ff_pic_tmp3->data[0] = buf;
1946 id->p_ff_pic_tmp3->data[1] = id->p_ff_pic_tmp3->data[0] +
1947 frame->linesize[0] * p_pic->p[0].i_lines;
1948 id->p_ff_pic_tmp3->data[2] = id->p_ff_pic_tmp3->data[1] +
1949 frame->linesize[1] * p_pic->p[1].i_lines;
1952 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1954 p_stream->p_vlc->pf_memcpy(
1955 id->p_ff_pic_tmp3->data[i_plane],
1956 frame->data[i_plane],
1957 p_pic->p[i_plane].i_lines * frame->linesize[i_plane] );
1960 id->p_ff_pic_tmp3->repeat_pict = frame->repeat_pict;
1961 #if LIBAVCODEC_BUILD >= 4685
1962 id->p_ff_pic_tmp3->interlaced_frame = frame->interlaced_frame;
1963 id->p_ff_pic_tmp3->top_field_first = frame->top_field_first;
1965 frame = id->p_ff_pic_tmp3;
1969 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1971 p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1972 if ( p_sys->i_threads >= 1 )
1974 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines *
1975 p_pic->p[i_plane].i_pitch);
1976 p_stream->p_vlc->pf_memcpy( p_pic->p[i_plane].p_pixels,
1977 frame->data[i_plane], p_pic->p[i_plane].i_lines *
1978 p_pic->p[i_plane].i_pitch );
1982 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1986 /* Set the pts of the frame being encoded */
1987 p_pic->date = i_pts;
1989 p_pic->i_nb_fields = frame->repeat_pict;
1990 #if LIBAVCODEC_BUILD >= 4685
1991 p_pic->b_progressive = !frame->interlaced_frame;
1992 p_pic->b_top_field_first = frame->top_field_first;
1995 /* Interpolate the next PTS
1996 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1997 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1999 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
2000 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
2003 /* Overlay subpicture */
2006 int i_width, i_height;
2008 p_sys->p_filter_blend->fmt_out = id->p_encoder->fmt_in;
2009 p_sys->p_filter_blend->fmt_out.video.i_visible_width =
2010 p_sys->p_filter_blend->fmt_out.video.i_width;
2011 p_sys->p_filter_blend->fmt_out.video.i_visible_height =
2012 p_sys->p_filter_blend->fmt_out.video.i_height;
2013 p_sys->p_filter_blend->fmt_out.video.i_chroma =
2014 VLC_FOURCC('I','4','2','0');
2016 i_width = id->p_encoder->fmt_in.video.i_width;
2017 i_height = id->p_encoder->fmt_in.video.i_height;
2019 while( p_subpic != NULL )
2021 subpicture_region_t *p_region = p_subpic->p_region;
2023 while( p_region && p_sys->p_filter_blend &&
2024 p_sys->p_filter_blend->pf_video_blend )
2026 int i_x_offset = p_region->i_x + p_subpic->i_x;
2027 int i_y_offset = p_region->i_y + p_subpic->i_y;
2029 if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
2031 i_y_offset = i_height - p_region->fmt.i_height -
2034 else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
2036 i_y_offset = i_height / 2 - p_region->fmt.i_height / 2;
2039 if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
2041 i_x_offset = i_width - p_region->fmt.i_width -
2044 else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
2046 i_x_offset = i_width / 2 - p_region->fmt.i_width / 2;
2049 if( p_subpic->b_absolute )
2051 i_x_offset = p_region->i_x + p_subpic->i_x;
2052 i_y_offset = p_region->i_y + p_subpic->i_y;
2055 p_sys->p_filter_blend->fmt_in.video = p_region->fmt;
2057 p_sys->p_filter_blend->pf_video_blend(
2058 p_sys->p_filter_blend, p_pic, p_pic,
2059 &p_region->picture, i_x_offset, i_y_offset );
2061 p_region = p_region->p_next;
2064 p_subpic = p_subpic->p_next;
2068 if ( p_sys->i_threads >= 1 )
2070 vlc_mutex_lock( &p_sys->lock_out );
2071 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
2072 p_sys->i_last_pic %= PICTURE_RING_SIZE;
2073 *out = p_sys->p_buffers;
2074 p_sys->p_buffers = NULL;
2075 vlc_cond_signal( &p_sys->cond );
2076 vlc_mutex_unlock( &p_sys->lock_out );
2081 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2082 block_ChainAppend( out, p_block );
2084 if( p_sys->b_audio_sync && i_duplicate > 1 )
2086 i_pts = date_Get( &id->interpolated_pts ) + 1;
2087 date_Increment( &id->interpolated_pts, 1 );
2088 p_pic->date = i_pts;
2089 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2090 block_ChainAppend( out, p_block );
2105 static int EncoderThread( sout_stream_sys_t * p_sys )
2107 sout_stream_id_t * id = p_sys->id_video;
2111 while ( !p_sys->b_die && !p_sys->b_error )
2115 vlc_mutex_lock( &p_sys->lock_out );
2116 while ( p_sys->i_last_pic == p_sys->i_first_pic )
2118 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
2119 if ( p_sys->b_die || p_sys->b_error )
2122 if ( p_sys->b_die || p_sys->b_error )
2124 vlc_mutex_unlock( &p_sys->lock_out );
2128 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2129 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2130 vlc_mutex_unlock( &p_sys->lock_out );
2132 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2133 vlc_mutex_lock( &p_sys->lock_out );
2134 block_ChainAppend( &p_sys->p_buffers, p_block );
2135 vlc_mutex_unlock( &p_sys->lock_out );
2137 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
2139 free( p_pic->p[i_plane].p_pixels );
2144 while ( p_sys->i_last_pic != p_sys->i_first_pic )
2146 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2147 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2149 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
2151 free( p_pic->p[i_plane].p_pixels );
2156 block_ChainRelease( p_sys->p_buffers );
2161 /*****************************************************************************
2162 * transcode_video_ffmpeg_getframebuf:
2164 * Callback used by ffmpeg to get a frame buffer.
2165 * We use it to get the right PTS for each decoded picture.
2166 *****************************************************************************/
2167 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
2170 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
2173 if( p_sys->i_input_pts )
2175 p_frame->pts = p_sys->i_input_pts;
2177 else if( p_sys->i_input_dts )
2179 /* Some demuxers/packetizers only set the dts so let's try to find a
2180 * useful timestamp from this */
2181 if( !p_context->has_b_frames || !p_sys->b_input_has_b_frames ||
2182 !p_frame->reference || !p_sys->i_output_pts )
2184 p_frame->pts = p_sys->i_input_dts +
2185 /* Hack: ffmpeg encoding doesn't like frames with identical pts */
2186 (p_sys->i_output_pts ? 0 : 50000);
2188 else p_frame->pts = AV_NOPTS_VALUE;
2190 else p_frame->pts = AV_NOPTS_VALUE;
2192 if( p_sys->i_output_pts ) /* make sure 1st frame has a pts > 0 */
2194 p_sys->i_input_pts = 0;
2195 p_sys->i_input_dts = 0;
2198 return avcodec_default_get_buffer( p_context, p_frame );
2204 static subpicture_t *spu_new_buffer( decoder_t * );
2205 static void spu_del_buffer( decoder_t *, subpicture_t * );
2207 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2209 sout_stream_sys_t *p_sys = p_stream->p_sys;
2212 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
2214 /* Initialization of decoder format structures */
2215 id->p_decoder->fmt_in = id->f_src;
2216 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2217 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2218 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
2220 /* Attach object to parent so object variables inheritance works */
2221 vlc_object_attach( id->p_decoder, p_stream );
2223 id->p_decoder->p_module =
2224 module_Need( id->p_decoder, "decoder", "$codec", VLC_TRUE );
2226 if( !id->p_decoder->p_module )
2228 vlc_object_detach( id->p_decoder );
2229 vlc_object_destroy( id->p_decoder );
2230 msg_Err( p_stream, "cannot find decoder" );
2231 return VLC_EGENERIC;
2234 if( !p_sys->b_soverlay )
2237 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
2239 /* Initialization of encoder format structures */
2240 es_format_Init( &id->p_encoder->fmt_in,
2241 id->f_src.i_cat, id->f_src.i_codec );
2243 id->p_encoder->fmt_out = id->f_dst;
2245 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2247 /* Attach object to parent so object variables inheritance works */
2248 vlc_object_attach( id->p_encoder, p_stream );
2250 id->p_encoder->p_module =
2251 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2253 if( !id->p_encoder->p_module )
2255 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2256 vlc_object_detach( id->p_decoder );
2257 vlc_object_destroy( id->p_decoder );
2259 vlc_object_detach( id->p_encoder );
2260 vlc_object_destroy( id->p_encoder );
2261 msg_Err( p_stream, "cannot find encoder" );
2262 return VLC_EGENERIC;
2265 id->f_dst = id->p_encoder->fmt_out;
2269 p_sys->p_filter_blend =
2270 vlc_object_create( p_stream, sizeof(filter_t) );
2271 vlc_object_attach( p_sys->p_filter_blend, p_stream );
2272 p_sys->p_filter_blend->fmt_out.video.i_chroma =
2273 VLC_FOURCC('I','4','2','0');
2274 p_sys->p_filter_blend->fmt_in.video.i_chroma =
2275 VLC_FOURCC('Y','U','V','A');
2276 p_sys->p_filter_blend->p_module =
2277 module_Need( p_sys->p_filter_blend, "video blending", 0, 0 );
2283 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2285 sout_stream_sys_t *p_sys = p_stream->p_sys;
2289 if( id->p_decoder->p_module )
2290 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2291 vlc_object_detach( id->p_decoder );
2292 vlc_object_destroy( id->p_decoder );
2297 if( id->p_encoder->p_module )
2298 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2299 vlc_object_detach( id->p_encoder );
2300 vlc_object_destroy( id->p_encoder );
2303 /* Free subpictures */
2304 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2306 if( !p_sys->pp_subpics[i] ) continue;
2308 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2309 p_sys->pp_subpics[i] = NULL;
2313 static int transcode_spu_process( sout_stream_t *p_stream,
2314 sout_stream_id_t *id,
2315 block_t *in, block_t **out )
2317 sout_stream_sys_t *p_sys = p_stream->p_sys;
2318 subpicture_t *p_subpic;
2321 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2322 if( p_subpic && p_sys->b_soverlay )
2326 /* Find a free slot in our supictures ring buffer */
2327 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2329 if( !p_sys->pp_subpics[i] )
2331 p_sys->pp_subpics[i] = p_subpic;
2335 if( i == SUBPICTURE_RING_SIZE )
2337 spu_del_buffer( id->p_decoder, p_subpic );
2341 if( p_subpic && !p_sys->b_soverlay )
2345 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2346 spu_del_buffer( id->p_decoder, p_subpic );
2350 block_ChainAppend( out, p_block );
2355 return VLC_EGENERIC;
2358 static subpicture_t *transcode_spu_get( sout_stream_t *p_stream,
2359 sout_stream_id_t *id,
2360 mtime_t display_date )
2362 sout_stream_sys_t *p_sys = p_stream->p_sys;
2363 subpicture_t *p_subpic = 0;
2364 subpicture_t *p_ephemer = 0;
2365 subpicture_t **pp_subpic = &p_subpic;
2368 /* Find current subpictures and remove old ones */
2369 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2371 if( !p_sys->pp_subpics[i] ) continue;
2373 if( !p_sys->pp_subpics[i]->b_ephemer &&
2374 p_sys->pp_subpics[i]->i_stop < display_date )
2376 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2377 p_sys->pp_subpics[i] = NULL;
2381 if( p_sys->pp_subpics[i]->i_start > display_date ) continue;
2383 if( p_sys->pp_subpics[i]->b_ephemer && !p_ephemer )
2385 p_ephemer = p_sys->pp_subpics[i];
2387 else if( p_sys->pp_subpics[i]->b_ephemer )
2389 if( p_ephemer->i_start < p_sys->pp_subpics[i]->i_start )
2393 *p_ephemer = *p_sys->pp_subpics[i];
2394 *p_sys->pp_subpics[i] = tmp;
2397 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2398 p_sys->pp_subpics[i] = NULL;
2402 /* Add subpicture to the list */
2403 *pp_subpic = p_sys->pp_subpics[i];
2404 pp_subpic = &p_sys->pp_subpics[i]->p_next;
2410 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2412 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
2413 memset( p_subpic, 0, sizeof(subpicture_t) );
2414 p_subpic->b_absolute = VLC_TRUE;
2416 p_subpic->pf_create_region = __spu_CreateRegion;
2417 p_subpic->pf_destroy_region = __spu_DestroyRegion;
2422 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2424 while( p_subpic->p_region )
2426 subpicture_region_t *p_region = p_subpic->p_region;
2427 p_subpic->p_region = p_region->p_next;
2428 p_subpic->pf_destroy_region( VLC_OBJECT(p_dec), p_region );