📄 encoder.c
字号:
* AVCodecContext by different threads at the same time */ for ( i = 0; i < count; i++ ) { vlc_mutex_lock( &pp_contexts[i]->lock ); pp_contexts[i]->arg = arg[i]; pp_contexts[i]->pf_func = pf_func; pp_contexts[i]->i_ret = 12345; pp_contexts[i]->b_work = 1; vlc_cond_signal( &pp_contexts[i]->cond ); vlc_mutex_unlock( &pp_contexts[i]->lock ); } for ( i = 0; i < count; i++ ) { vlc_mutex_lock( &pp_contexts[i]->lock ); while ( !pp_contexts[i]->b_done ) { vlc_cond_wait( &pp_contexts[i]->cond, &pp_contexts[i]->lock ); } pp_contexts[i]->b_done = 0; pp_contexts[i]->pf_func = NULL; vlc_mutex_unlock( &pp_contexts[i]->lock ); if ( ret ) { ret[i] = pp_contexts[i]->i_ret; } } return 0;}/**************************************************************************** * EncodeVideo: the whole thing ****************************************************************************/static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict ){ encoder_sys_t *p_sys = p_enc->p_sys; AVFrame frame; int i_out, i_plane; if ( !p_sys->b_inited && p_enc->i_threads >= 1 ) { struct thread_context_t ** pp_contexts; int i; p_sys->b_inited = 1; pp_contexts = malloc( sizeof(struct thread_context_t *) * p_enc->i_threads ); p_sys->p_context->thread_opaque = (void *)pp_contexts; for ( i = 0; i < p_enc->i_threads; i++ ) { pp_contexts[i] = vlc_object_create( p_enc, sizeof(struct thread_context_t) ); pp_contexts[i]->p_context = p_sys->p_context; vlc_mutex_init( &pp_contexts[i]->lock ); vlc_cond_init( p_enc, &pp_contexts[i]->cond ); pp_contexts[i]->b_work = 0; pp_contexts[i]->b_done = 0; if ( vlc_thread_create( pp_contexts[i], "encoder", FfmpegThread, VLC_THREAD_PRIORITY_VIDEO, false ) ) { msg_Err( p_enc, "cannot spawn encoder thread, expect to die soon" ); return NULL; } } p_sys->p_context->execute = FfmpegExecute; } memset( &frame, 0, sizeof( AVFrame ) ); for( i_plane = 0; i_plane < p_pict->i_planes; i_plane++ ) { frame.data[i_plane] = p_pict->p[i_plane].p_pixels; frame.linesize[i_plane] = p_pict->p[i_plane].i_pitch; } /* Let ffmpeg select the frame type */ frame.pict_type = 0; frame.repeat_pict = p_pict->i_nb_fields - 2; frame.interlaced_frame = !p_pict->b_progressive; frame.top_field_first = !!p_pict->b_top_field_first; /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/ if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'm', 'p', '4', 'v' ) ) { frame.pts = p_pict->date ? p_pict->date : (int64_t)AV_NOPTS_VALUE; if ( p_sys->b_hurry_up && frame.pts != (int64_t)AV_NOPTS_VALUE ) { mtime_t current_date = mdate(); if ( current_date + HURRY_UP_GUARD3 > frame.pts ) { p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE;#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0) p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;#else p_sys->p_context->trellis = 0;#endif msg_Dbg( p_enc, "hurry up mode 3" ); } else { p_sys->p_context->mb_decision = p_sys->i_hq; if ( current_date + HURRY_UP_GUARD2 > frame.pts ) {#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0) p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;#else p_sys->p_context->trellis = 0;#endif p_sys->p_context->noise_reduction = p_sys->i_noise_reduction + (HURRY_UP_GUARD2 + current_date - frame.pts) / 500; msg_Dbg( p_enc, "hurry up mode 2" ); } else {#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0) if ( p_sys->b_trellis ) p_sys->p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;#else p_sys->p_context->trellis = p_sys->b_trellis;#endif p_sys->p_context->noise_reduction = p_sys->i_noise_reduction; } } if ( current_date + HURRY_UP_GUARD1 > frame.pts ) { frame.pict_type = FF_P_TYPE; /* msg_Dbg( p_enc, "hurry up mode 1 %lld", current_date + HURRY_UP_GUARD1 - frame.pts ); */ } } } else { frame.pts = (int64_t)AV_NOPTS_VALUE; } if ( frame.pts != (int64_t)AV_NOPTS_VALUE && frame.pts != 0 ) { if ( p_sys->i_last_pts == frame.pts ) { msg_Warn( p_enc, "almost fed libavcodec with two frames with the " "same PTS (%"PRId64 ")", frame.pts ); return NULL; } else if ( p_sys->i_last_pts > frame.pts ) { msg_Warn( p_enc, "almost fed libavcodec with a frame in the " "past (current: %"PRId64 ", last: %"PRId64")", frame.pts, p_sys->i_last_pts ); return NULL; } else { p_sys->i_last_pts = frame.pts; } } frame.quality = p_sys->i_quality; /* Ugly work-around for stupid libavcodec behaviour */ p_sys->i_framenum++; p_sys->pi_delay_pts[p_sys->i_framenum % MAX_FRAME_DELAY] = frame.pts; frame.pts = p_sys->i_framenum * AV_TIME_BASE * p_enc->fmt_in.video.i_frame_rate_base; frame.pts += p_enc->fmt_in.video.i_frame_rate - 1; frame.pts /= p_enc->fmt_in.video.i_frame_rate; /* End work-around */ i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out, p_sys->i_buffer_out, &frame ); if( i_out > 0 ) { block_t *p_block = block_New( p_enc, i_out ); memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out ); /* FIXME, 3-2 pulldown is not handled correctly */ p_block->i_length = INT64_C(1000000) * p_enc->fmt_in.video.i_frame_rate_base / p_enc->fmt_in.video.i_frame_rate; if( !p_sys->p_context->max_b_frames || !p_sys->p_context->delay ) { /* No delay -> output pts == input pts */ p_block->i_pts = p_block->i_dts = p_pict->date; } else if( p_sys->p_context->coded_frame->pts != (int64_t)AV_NOPTS_VALUE && p_sys->p_context->coded_frame->pts != 0 && p_sys->i_buggy_pts_detect != p_sys->p_context->coded_frame->pts ) { p_sys->i_buggy_pts_detect = p_sys->p_context->coded_frame->pts; p_block->i_pts = p_sys->p_context->coded_frame->pts; /* Ugly work-around for stupid libavcodec behaviour */ { int64_t i_framenum = p_block->i_pts * p_enc->fmt_in.video.i_frame_rate / p_enc->fmt_in.video.i_frame_rate_base / AV_TIME_BASE; p_block->i_pts = p_sys->pi_delay_pts[i_framenum % MAX_FRAME_DELAY]; } /* End work-around */ if( p_sys->p_context->coded_frame->pict_type != FF_I_TYPE && p_sys->p_context->coded_frame->pict_type != FF_P_TYPE ) { p_block->i_dts = p_block->i_pts; } else { if( p_sys->i_last_ref_pts ) { p_block->i_dts = p_sys->i_last_ref_pts; } else { /* Let's put something sensible */ p_block->i_dts = p_block->i_pts; } p_sys->i_last_ref_pts = p_block->i_pts; } } else { /* Buggy libavcodec which doesn't update coded_frame->pts * correctly */ p_block->i_dts = p_block->i_pts = p_pict->date; } switch ( p_sys->p_context->coded_frame->pict_type ) { case FF_I_TYPE: p_block->i_flags |= BLOCK_FLAG_TYPE_I; break; case FF_P_TYPE: p_block->i_flags |= BLOCK_FLAG_TYPE_P; break; case FF_B_TYPE: p_block->i_flags |= BLOCK_FLAG_TYPE_B; break; } return p_block; } return NULL;}/**************************************************************************** * EncodeAudio: the whole thing ****************************************************************************/static block_t *EncodeAudio( encoder_t *p_enc, aout_buffer_t *p_aout_buf ){ encoder_sys_t *p_sys = p_enc->p_sys; block_t *p_block, *p_chain = NULL; uint8_t *p_buffer = p_aout_buf->p_buffer; int i_samples = p_aout_buf->i_nb_samples; int i_samples_delay = p_sys->i_samples_delay; p_sys->i_pts = p_aout_buf->start_date - (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay / (mtime_t)p_enc->fmt_in.audio.i_rate; p_sys->i_samples_delay += i_samples; while( p_sys->i_samples_delay >= p_sys->p_context->frame_size ) { int16_t *p_samples; int i_out; if( i_samples_delay ) { /* Take care of the left-over from last time */ int i_delay_size = i_samples_delay * 2 * p_sys->p_context->channels; int i_size = p_sys->i_frame_size - i_delay_size; p_samples = (int16_t *)p_sys->p_buffer; memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size ); p_buffer -= i_delay_size; i_samples += i_samples_delay; i_samples_delay = 0; } else { p_samples = (int16_t *)p_buffer; } i_out = avcodec_encode_audio( p_sys->p_context, p_sys->p_buffer_out, p_sys->i_buffer_out, p_samples );#if 0 msg_Warn( p_enc, "avcodec_encode_audio: %d", i_out );#endif if( i_out < 0 ) break; p_buffer += p_sys->i_frame_size; p_sys->i_samples_delay -= p_sys->p_context->frame_size; i_samples -= p_sys->p_context->frame_size; if( i_out == 0 ) continue; p_block = block_New( p_enc, i_out ); memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out ); p_block->i_length = (mtime_t)1000000 * (mtime_t)p_sys->p_context->frame_size / (mtime_t)p_sys->p_context->sample_rate; p_block->i_dts = p_block->i_pts = p_sys->i_pts; /* Update pts */ p_sys->i_pts += p_block->i_length; block_ChainAppend( &p_chain, p_block ); } /* Backup the remaining raw samples */ if( i_samples ) { memcpy( p_sys->p_buffer + i_samples_delay * 2 * p_sys->p_context->channels, p_buffer, i_samples * 2 * p_sys->p_context->channels ); } return p_chain;}/***************************************************************************** * CloseEncoder: ffmpeg encoder destruction *****************************************************************************/void CloseEncoder( vlc_object_t *p_this ){ encoder_t *p_enc = (encoder_t *)p_this; encoder_sys_t *p_sys = p_enc->p_sys; if ( p_sys->b_inited && p_enc->i_threads >= 1 ) { int i; struct thread_context_t ** pp_contexts = (struct thread_context_t **)p_sys->p_context->thread_opaque; for ( i = 0; i < p_enc->i_threads; i++ ) { vlc_object_kill( pp_contexts[i] ); vlc_cond_signal( &pp_contexts[i]->cond ); vlc_thread_join( pp_contexts[i] ); vlc_mutex_destroy( &pp_contexts[i]->lock ); vlc_cond_destroy( &pp_contexts[i]->cond ); vlc_object_release( pp_contexts[i] ); } free( pp_contexts ); } vlc_mutex_t *lock = var_AcquireMutex( "avcodec" ); avcodec_close( p_sys->p_context ); vlc_mutex_unlock( lock ); av_free( p_sys->p_context ); free( p_sys->p_buffer ); free( p_sys->p_buffer_out ); free( p_sys );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -