📄 transcode.c
字号:
/* id->p_decoder->p_cfg = p_sys->p_video_cfg; */ id->p_decoder->p_module = module_Need( id->p_decoder, "decoder", "$codec", 0 ); if( !id->p_decoder->p_module ) { msg_Err( p_stream, "cannot find video decoder" ); free( id->p_decoder->p_owner ); return VLC_EGENERIC; } /* * Open encoder. * Because some info about the decoded input will only be available * once the first frame is decoded, we actually only test the availability * of the encoder here. */ /* Initialization of encoder format structures */ es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat, id->p_decoder->fmt_out.i_codec ); id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec; /* The dimensions will be set properly later on. * Just put sensible values so we can test an encoder is available. */ id->p_encoder->fmt_in.video.i_width = id->p_encoder->fmt_out.video.i_width ?: id->p_decoder->fmt_in.video.i_width ?: 16; id->p_encoder->fmt_in.video.i_height = id->p_encoder->fmt_out.video.i_height ?: id->p_decoder->fmt_in.video.i_height ?: 16; id->p_encoder->fmt_in.video.i_frame_rate = ENC_FRAMERATE; id->p_encoder->fmt_in.video.i_frame_rate_base = ENC_FRAMERATE_BASE; id->p_encoder->i_threads = p_sys->i_threads; id->p_encoder->p_cfg = p_sys->p_video_cfg; id->p_encoder->p_module = module_Need( id->p_encoder, "encoder", p_sys->psz_venc, true ); if( !id->p_encoder->p_module ) { msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)", p_sys->psz_venc ? p_sys->psz_venc : "any", (char *)&p_sys->i_vcodec ); module_Unneed( id->p_decoder, id->p_decoder->p_module ); id->p_decoder->p_module = 0; free( id->p_decoder->p_owner ); return VLC_EGENERIC; } /* Close the encoder. * We'll open it only when we have the first frame. */ module_Unneed( id->p_encoder, id->p_encoder->p_module ); if( id->p_encoder->fmt_out.p_extra ) { free( id->p_encoder->fmt_out.p_extra ); id->p_encoder->fmt_out.p_extra = NULL; id->p_encoder->fmt_out.i_extra = 0; } id->p_encoder->p_module = NULL; if( p_sys->i_threads >= 1 ) { int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT : VLC_THREAD_PRIORITY_VIDEO; p_sys->id_video = id; vlc_mutex_init( &p_sys->lock_out ); vlc_cond_init( p_stream, &p_sys->cond ); memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) ); p_sys->i_first_pic = 0; p_sys->i_last_pic = 0; p_sys->p_buffers = NULL; p_sys->b_die = p_sys->b_error = 0; if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority, false ) ) { msg_Err( p_stream, "cannot spawn encoder thread" ); module_Unneed( id->p_decoder, id->p_decoder->p_module ); id->p_decoder->p_module = 0; free( id->p_decoder->p_owner ); return VLC_EGENERIC; } } return VLC_SUCCESS;}static void transcode_video_encoder_init( sout_stream_t *p_stream, sout_stream_id_t *id ){ sout_stream_sys_t *p_sys = p_stream->p_sys; /* Calculate scaling * width/height of source */ int i_src_width = id->p_decoder->fmt_out.video.i_width; int i_src_height = id->p_decoder->fmt_out.video.i_height; /* with/height scaling */ float f_scale_width = 1; float f_scale_height = 1; /* width/height of output stream */ int i_dst_width; int i_dst_height; /* aspect ratio */ float f_aspect = (float)id->p_decoder->fmt_out.video.i_aspect / VOUT_ASPECT_FACTOR; msg_Dbg( p_stream, "decoder aspect is %i:%i", id->p_decoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR ); /* Change f_aspect from source frame to source pixel */ f_aspect = f_aspect * i_src_height / i_src_width; msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect ); /* Calculate scaling factor for specified parameters */ if( id->p_encoder->fmt_out.video.i_width <= 0 && id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale ) { /* Global scaling. Make sure width will remain a factor of 16 */ float f_real_scale; int i_new_height; int i_new_width = i_src_width * p_sys->f_scale; if( i_new_width % 16 <= 7 && i_new_width >= 16 ) i_new_width -= i_new_width % 16; else i_new_width += 16 - i_new_width % 16; f_real_scale = (float)( i_new_width ) / (float) i_src_width; i_new_height = __MAX( 16, i_src_height * (float)f_real_scale ); f_scale_width = f_real_scale; f_scale_height = (float) i_new_height / (float) i_src_height; } else if( id->p_encoder->fmt_out.video.i_width > 0 && id->p_encoder->fmt_out.video.i_height <= 0 ) { /* Only width specified */ f_scale_width = (float)id->p_encoder->fmt_out.video.i_width/i_src_width; f_scale_height = f_scale_width; } else if( id->p_encoder->fmt_out.video.i_width <= 0 && id->p_encoder->fmt_out.video.i_height > 0 ) { /* Only height specified */ f_scale_height = (float)id->p_encoder->fmt_out.video.i_height/i_src_height; f_scale_width = f_scale_height; } else if( id->p_encoder->fmt_out.video.i_width > 0 && id->p_encoder->fmt_out.video.i_height > 0 ) { /* Width and height specified */ f_scale_width = (float)id->p_encoder->fmt_out.video.i_width/i_src_width; f_scale_height = (float)id->p_encoder->fmt_out.video.i_height/i_src_height; } /* check maxwidth and maxheight */ if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth / i_src_width ) { f_scale_width = (float)p_sys->i_maxwidth / i_src_width; } if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight / i_src_height ) { f_scale_height = (float)p_sys->i_maxheight / i_src_height; } /* Change aspect ratio from source pixel to scaled pixel */ f_aspect = f_aspect * f_scale_height / f_scale_width; msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect ); /* f_scale_width and f_scale_height are now final */ /* Calculate width, height from scaling * Make sure its multiple of 2 */ i_dst_width = 2 * (int)(f_scale_width*i_src_width/2+0.5); i_dst_height = 2 * (int)(f_scale_height*i_src_height/2+0.5); /* Change aspect ratio from scaled pixel to output frame */ f_aspect = f_aspect * i_dst_width / i_dst_height; /* Store calculated values */ id->p_encoder->fmt_out.video.i_width = id->p_encoder->fmt_out.video.i_visible_width = i_dst_width; id->p_encoder->fmt_out.video.i_height = id->p_encoder->fmt_out.video.i_visible_height = i_dst_height; id->p_encoder->fmt_in.video.i_width = id->p_encoder->fmt_in.video.i_visible_width = i_dst_width; id->p_encoder->fmt_in.video.i_height = id->p_encoder->fmt_in.video.i_visible_height = i_dst_height; msg_Dbg( p_stream, "source %ix%i, destination %ix%i", i_src_width, i_src_height, i_dst_width, i_dst_height ); /* Handle frame rate conversion */ if( !id->p_encoder->fmt_out.video.i_frame_rate || !id->p_encoder->fmt_out.video.i_frame_rate_base ) { if( id->p_decoder->fmt_out.video.i_frame_rate && id->p_decoder->fmt_out.video.i_frame_rate_base ) { id->p_encoder->fmt_out.video.i_frame_rate = id->p_decoder->fmt_out.video.i_frame_rate; id->p_encoder->fmt_out.video.i_frame_rate_base = id->p_decoder->fmt_out.video.i_frame_rate_base; } else { /* Pick a sensible default value */ id->p_encoder->fmt_out.video.i_frame_rate = ENC_FRAMERATE; id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE; } } id->p_encoder->fmt_in.video.i_frame_rate = id->p_encoder->fmt_out.video.i_frame_rate; id->p_encoder->fmt_in.video.i_frame_rate_base = id->p_encoder->fmt_out.video.i_frame_rate_base; date_Init( &id->interpolated_pts, id->p_encoder->fmt_out.video.i_frame_rate, id->p_encoder->fmt_out.video.i_frame_rate_base ); /* Check whether a particular aspect ratio was requested */ if( !id->p_encoder->fmt_out.video.i_aspect ) { id->p_encoder->fmt_out.video.i_aspect = (int)( f_aspect * VOUT_ASPECT_FACTOR + 0.5 ); } id->p_encoder->fmt_in.video.i_aspect = id->p_encoder->fmt_out.video.i_aspect; msg_Dbg( p_stream, "encoder aspect is %i:%i", id->p_encoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR ); id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;}static int transcode_video_encoder_open( sout_stream_t *p_stream, sout_stream_id_t *id ){ sout_stream_sys_t *p_sys = p_stream->p_sys; msg_Dbg( p_stream, "destination (after video filters) %ix%i", id->p_encoder->fmt_in.video.i_width, id->p_encoder->fmt_in.video.i_height ); id->p_encoder->p_module = module_Need( id->p_encoder, "encoder", p_sys->psz_venc, true ); if( !id->p_encoder->p_module ) { msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)", p_sys->psz_venc ? p_sys->psz_venc : "any", (char *)&p_sys->i_vcodec ); return VLC_EGENERIC; } id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec; /* Hack for mp2v/mp1v transcoding support */ if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') || id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') ) { id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v'); } id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out, &id->p_encoder->fmt_out ); if( !id->id ) { msg_Err( p_stream, "cannot add this stream" ); return VLC_EGENERIC; } return VLC_SUCCESS;}static void transcode_video_close( sout_stream_t *p_stream, sout_stream_id_t *id ){ int i; if( p_stream->p_sys->i_threads >= 1 ) { vlc_mutex_lock( &p_stream->p_sys->lock_out ); vlc_object_kill( p_stream->p_sys ); vlc_cond_signal( &p_stream->p_sys->cond ); vlc_mutex_unlock( &p_stream->p_sys->lock_out ); vlc_thread_join( p_stream->p_sys ); vlc_mutex_destroy( &p_stream->p_sys->lock_out ); vlc_cond_destroy( &p_stream->p_sys->cond ); } video_timer_close( id->p_encoder ); /* Close decoder */ if( id->p_decoder->p_module ) module_Unneed( id->p_decoder, id->p_decoder->p_module ); if( id->p_decoder->p_owner ) { /* Clean-up pictures ring buffer */ for( i = 0; i < PICTURE_RING_SIZE; i++ ) { if( id->p_decoder->p_owner->pp_pics[i] ) video_del_buffer( VLC_OBJECT(id->p_decoder), id->p_decoder->p_owner->pp_pics[i] ); } free( id->p_decoder->p_owner ); } /* Close encoder */ if( id->p_encoder->p_module ) module_Unneed( id->p_encoder, id->p_encoder->p_module ); /* Close filters */ if( id->p_f_chain ) filter_chain_Delete( id->p_f_chain ); if( id->p_uf_chain ) filter_chain_Delete( id->p_uf_chain );}static int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id, block_t *in, block_t **out ){ sout_stream_sys_t *p_sys = p_stream->p_sys; int i_duplicate = 1; picture_t *p_pic, *p_pic2 = NULL; *out = NULL; while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) ) { subpicture_t *p_subpic = NULL; sout_UpdateStatistic( p_stream->p_sout, SOUT_STATISTIC_DECODED_VIDEO, 1 ); if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up ) { mtime_t current_date = mdate(); if( current_date + 50000 > p_pic->date ) { msg_Dbg( p_stream, "late picture skipped (%"PRId64")", current_date + 50000 - p_pic->date ); p_pic->pf_release( p_pic ); continue; } } if( p_sys->b_master_sync ) { mtime_t i_video_drift; mtime_t i_master_drift = p_sys->i_master_drift; mtime_t i_pts; i_pts = date_Get( &id->interpolated_pts ) + 1; if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT ) { msg_Dbg( p_stream, "drift is too high, resetting master sync" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -