encoder.c
来自「VLC媒体播放程序」· C语言 代码 · 共 773 行 · 第 1/2 页
C
773 行
vlc_cond_wait( &p_context->cond, &p_context->lock ); } p_context->b_work = 0; vlc_mutex_unlock( &p_context->lock ); if ( p_context->b_die || p_context->b_error ) break; if ( p_context->pf_func ) { p_context->i_ret = p_context->pf_func( p_context->p_context, p_context->arg ); } vlc_mutex_lock( &p_context->lock ); p_context->b_done = 1; vlc_cond_signal( &p_context->cond ); vlc_mutex_unlock( &p_context->lock ); } return 0;}static int FfmpegExecute( AVCodecContext *s, int (*pf_func)(AVCodecContext *c2, void *arg2), void **arg, int *ret, int count ){ struct thread_context_t ** pp_contexts = (struct thread_context_t **)s->thread_opaque; int i; /* Note, we can be certain that this is not called with the same * 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;}#endif/**************************************************************************** * 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 LIBAVCODEC_BUILD >= 4702 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( p_enc, &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, VLC_FALSE ) ) { msg_Err( p_enc, "cannot spawn encoder thread, expect to die soon" ); return NULL; } } p_sys->p_context->execute = FfmpegExecute; }#endif 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; } /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/ if( p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) || p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '1', 'v' ) || p_enc->fmt_out.i_codec == VLC_FOURCC( 'm', 'p', '2', 'v' ) ) { frame.pts = p_pict->date; if ( p_enc->b_hurry_up ) { mtime_t current_date = mdate();#if LIBAVCODEC_BUILD >= 4702 struct thread_context_t ** pp_contexts = (struct thread_context_t **)p_sys->p_context->thread_opaque;#endif if ( frame.pts && current_date + HURRY_UP_GUARD3 > frame.pts ) { p_sys->p_context->mb_decision = FF_MB_DECISION_SIMPLE; p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;#if LIBAVCODEC_BUILD >= 4702 if ( p_enc->i_threads >= 2 ) { int i; for ( i = 0; i < p_enc->i_threads; i++ ) { vlc_thread_set_priority( pp_contexts[i], VLC_THREAD_PRIORITY_VIDEO + 4 ); } }#endif msg_Dbg( p_enc, "hurry up mode 3" ); } else { p_sys->p_context->mb_decision = p_enc->i_hq; if ( frame.pts && current_date + HURRY_UP_GUARD2 > frame.pts ) { p_sys->p_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;#if LIBAVCODEC_BUILD >= 4702 if ( p_enc->i_threads >= 2 ) { int i; for ( i = 0; i < p_enc->i_threads; i++ ) { vlc_thread_set_priority( pp_contexts[i], VLC_THREAD_PRIORITY_VIDEO + 2 ); } }#endif msg_Dbg( p_enc, "hurry up mode 2" ); } else { if ( p_enc->b_trellis ) p_sys->p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;#if LIBAVCODEC_BUILD >= 4702 if ( p_enc->i_threads >= 2 ) { int i; for ( i = 0; i < p_enc->i_threads; i++ ) { vlc_thread_set_priority( pp_contexts[i], VLC_THREAD_PRIORITY_VIDEO ); } }#endif } }#if LIBAVCODEC_BUILD >= 4690 if ( frame.pts && current_date + HURRY_UP_GUARD1 > frame.pts ) { p_sys->p_context->noise_reduction = p_enc->i_noise_reduction + (HURRY_UP_GUARD1 + current_date - frame.pts) / 1500; } else { p_sys->p_context->noise_reduction = p_enc->i_noise_reduction; }#endif } } else { frame.pts = 0; } /* Let ffmpeg select the frame type */ frame.pict_type = 0; frame.repeat_pict = p_pict->i_nb_fields;#if LIBAVCODEC_BUILD >= 4685 frame.interlaced_frame = !p_pict->b_progressive; frame.top_field_first = p_pict->b_top_field_first;#endif i_out = avcodec_encode_video( p_sys->p_context, p_sys->p_buffer_out, AVCODEC_MAX_VIDEO_FRAME_SIZE, &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 ); if( 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; /* FIXME, 3-2 pulldown is not handled correctly */ p_block->i_length = I64C(1000000) * p_enc->fmt_in.video.i_frame_rate_base / p_enc->fmt_in.video.i_frame_rate; p_block->i_pts = p_sys->p_context->coded_frame->pts; if( !p_sys->p_context->delay || ( 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_length = I64C(1000000) * p_enc->fmt_in.video.i_frame_rate_base / p_enc->fmt_in.video.i_frame_rate; p_block->i_dts = p_block->i_pts = p_pict->date; } 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; char *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, 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE, 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 E_(CloseEncoder)( vlc_object_t *p_this ){ encoder_t *p_enc = (encoder_t *)p_this; encoder_sys_t *p_sys = p_enc->p_sys;#if LIBAVCODEC_BUILD >= 4702 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++ ) { pp_contexts[i]->b_die = 1; 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_destroy( pp_contexts[i] ); } free(pp_contexts); }#endif avcodec_close( p_sys->p_context ); free( p_sys->p_context ); if( p_sys->p_buffer ) free( p_sys->p_buffer ); if( p_sys->p_buffer_out ) free( p_sys->p_buffer_out ); free( p_sys );}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?