📄 ffmpeg.c
字号:
/***************************************************************************** * ffmpeg.c: video decoder using ffmpeg library ***************************************************************************** * Copyright (C) 1999-2001 the VideoLAN team * $Id: ffmpeg.c 19657 2007-04-03 21:23:26Z jb $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * Gildas Bazin <gbazin@videolan.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#include <vlc/vlc.h>#include <vlc/decoder.h>/* ffmpeg header */#define HAVE_MMX 1#ifdef HAVE_FFMPEG_AVCODEC_H# include <ffmpeg/avcodec.h>#else# include <avcodec.h>#endif#if LIBAVCODEC_BUILD < 4680# error You must have a libavcodec >= 4680 (get CVS)#endif#include "ffmpeg.h"#ifdef LIBAVCODEC_PP# ifdef HAVE_POSTPROC_POSTPROCESS_H# include <postproc/postprocess.h># else# include <libpostproc/postprocess.h># endif#endif/***************************************************************************** * decoder_sys_t: decoder descriptor *****************************************************************************/struct decoder_sys_t{ /* Common part between video and audio decoder */ int i_cat; int i_codec_id; char *psz_namecodec; AVCodecContext *p_context; AVCodec *p_codec;};/**************************************************************************** * Local prototypes ****************************************************************************/static int OpenDecoder( vlc_object_t * );static void CloseDecoder( vlc_object_t * );static int nloopf_list[] = { 0, 1, 2, 3, 4 };static char *nloopf_list_text[] = { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };static char *enc_hq_list[] = { "rd", "bits", "simple" };static char *enc_hq_list_text[] = { N_("rd"), N_("bits"), N_("simple") };/***************************************************************************** * Module descriptor *****************************************************************************/vlc_module_begin(); set_shortname( "FFmpeg"); set_category( CAT_INPUT ); set_subcategory( SUBCAT_INPUT_SCODEC ); /* decoder main module */#if defined(MODULE_NAME_is_ffmpegaltivec) \ || (defined(CAN_COMPILE_ALTIVEC) && !defined(NO_ALTIVEC_IN_FFMPEG)) set_description( _("AltiVec FFmpeg audio/video decoder/encoder ((MS)MPEG4,SVQ1,H263,WMV,WMA)") ); /*add_requirement( ALTIVEC );*/ set_capability( "decoder", 71 );#else set_description( _("FFmpeg audio/video decoder/encoder ((MS)MPEG4,SVQ1,H263,WMV,WMA)") ); set_capability( "decoder", 70 );#endif set_section( N_("Decoding") , NULL ); set_callbacks( OpenDecoder, CloseDecoder ); add_bool( "ffmpeg-dr", 1, NULL, DR_TEXT, DR_TEXT, VLC_TRUE ); add_integer ( "ffmpeg-error-resilience", 1, NULL, ERROR_TEXT, ERROR_LONGTEXT, VLC_TRUE ); add_integer ( "ffmpeg-workaround-bugs", 1, NULL, BUGS_TEXT, BUGS_LONGTEXT, VLC_FALSE ); add_bool( "ffmpeg-hurry-up", 0, NULL, HURRYUP_TEXT, HURRYUP_LONGTEXT, VLC_FALSE ); add_integer ( "ffmpeg-vismv", 0, NULL, VISMV_TEXT, VISMV_LONGTEXT, VLC_TRUE ); add_integer ( "ffmpeg-lowres", 0, NULL, LOWRES_TEXT, LOWRES_LONGTEXT, VLC_TRUE ); change_integer_range( 0, 2 ); add_integer ( "ffmpeg-skiploopfilter", 0, NULL, SKIPLOOPF_TEXT, SKIPLOOPF_LONGTEXT, VLC_TRUE ); change_integer_list( nloopf_list, nloopf_list_text, 0 );#ifdef LIBAVCODEC_PP add_integer( "ffmpeg-pp-q", 0, NULL, PP_Q_TEXT, PP_Q_LONGTEXT, VLC_FALSE ); add_string( "ffmpeg-pp-name", "default", NULL, LIBAVCODEC_PP_TEXT, LIBAVCODEC_PP_LONGTEXT, VLC_TRUE );#endif add_integer( "ffmpeg-debug", 0, NULL, DEBUG_TEXT, DEBUG_LONGTEXT, VLC_TRUE ); /* chroma conversion submodule */ add_submodule(); set_capability( "chroma", 50 ); set_callbacks( E_(OpenChroma), E_(CloseChroma) ); set_description( _("FFmpeg chroma conversion") ); /* encoder submodule */ add_submodule(); set_section( N_("Encoding") , NULL ); set_description( _("FFmpeg audio/video encoder") ); set_capability( "encoder", 100 ); set_callbacks( E_(OpenEncoder), E_(CloseEncoder) ); add_string( ENC_CFG_PREFIX "hq", "simple", NULL, ENC_HQ_TEXT, ENC_HQ_LONGTEXT, VLC_FALSE ); change_string_list( enc_hq_list, enc_hq_list_text, 0 ); add_integer( ENC_CFG_PREFIX "keyint", 0, NULL, ENC_KEYINT_TEXT, ENC_KEYINT_LONGTEXT, VLC_FALSE ); add_integer( ENC_CFG_PREFIX "bframes", 0, NULL, ENC_BFRAMES_TEXT, ENC_BFRAMES_LONGTEXT, VLC_FALSE ); add_bool( ENC_CFG_PREFIX "hurry-up", 0, NULL, ENC_HURRYUP_TEXT, ENC_HURRYUP_LONGTEXT, VLC_FALSE ); add_bool( ENC_CFG_PREFIX "interlace", 0, NULL, ENC_INTERLACE_TEXT, ENC_INTERLACE_LONGTEXT, VLC_TRUE ); add_bool( ENC_CFG_PREFIX "interlace-me", 1, NULL, ENC_INTERLACE_ME_TEXT, ENC_INTERLACE_ME_LONGTEXT, VLC_TRUE ); add_integer( ENC_CFG_PREFIX "vt", 0, NULL, ENC_VT_TEXT, ENC_VT_LONGTEXT, VLC_TRUE ); add_bool( ENC_CFG_PREFIX "pre-me", 0, NULL, ENC_PRE_ME_TEXT, ENC_PRE_ME_LONGTEXT, VLC_TRUE ); add_bool( ENC_CFG_PREFIX "strict-rc", 0, NULL, ENC_RC_STRICT_TEXT, ENC_RC_STRICT_LONGTEXT, VLC_TRUE ); add_integer( ENC_CFG_PREFIX "rc-buffer-size", 224*1024*8, NULL, ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, VLC_TRUE ); add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0, NULL, ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, VLC_TRUE ); add_float( ENC_CFG_PREFIX "i-quant-factor", 0, NULL, ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, VLC_TRUE ); add_integer( ENC_CFG_PREFIX "noise-reduction", 0, NULL, ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, VLC_TRUE ); add_bool( ENC_CFG_PREFIX "mpeg4-matrix", 0, NULL, ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, VLC_TRUE ); add_integer( ENC_CFG_PREFIX "qmin", 0, NULL, ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, VLC_TRUE ); add_integer( ENC_CFG_PREFIX "qmax", 0, NULL, ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, VLC_TRUE ); add_bool( ENC_CFG_PREFIX "trellis", 0, NULL, ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, VLC_TRUE ); add_float( ENC_CFG_PREFIX "qscale", 0, NULL, ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, VLC_TRUE ); add_integer( ENC_CFG_PREFIX "strict", 0, NULL, ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, VLC_TRUE ); add_float( ENC_CFG_PREFIX "lumi-masking", 0.0, NULL, ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, VLC_TRUE ); add_float( ENC_CFG_PREFIX "dark-masking", 0.0, NULL, ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, VLC_TRUE ); add_float( ENC_CFG_PREFIX "p-masking", 0.0, NULL, ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, VLC_TRUE ); add_float( ENC_CFG_PREFIX "border-masking", 0.0, NULL, ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, VLC_TRUE ); add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0, NULL, ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, VLC_TRUE ); add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0, NULL, ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, VLC_TRUE ); /* demux submodule */ add_submodule(); set_description( _("FFmpeg demuxer" ) ); set_capability( "demux2", 2 ); set_callbacks( E_(OpenDemux), E_(CloseDemux) ); /* mux submodule */ add_submodule(); set_description( _("FFmpeg muxer" ) ); set_capability( "sout mux", 2 ); set_callbacks( E_(OpenMux), E_(CloseMux) ); /* video filter submodule */ add_submodule(); set_capability( "video filter2", 50 ); set_callbacks( E_(OpenFilter), E_(CloseFilter) ); set_description( _("FFmpeg video filter") ); /* crop/padd submodule */ add_submodule(); set_capability( "crop padd", 10 ); set_callbacks( E_(OpenCropPadd), E_(CloseFilter) ); set_description( _("FFmpeg crop padd filter") ); /* video filter submodule */ add_submodule(); set_capability( "video filter2", 0 ); set_callbacks( E_(OpenDeinterlace), E_(CloseDeinterlace) ); set_description( _("FFmpeg deinterlace video filter") ); add_shortcut( "ffmpeg-deinterlace" ); var_Create( p_module->p_libvlc, "avcodec", VLC_VAR_MUTEX );vlc_module_end();/***************************************************************************** * OpenDecoder: probe the decoder and return score *****************************************************************************/static int OpenDecoder( vlc_object_t *p_this ){ decoder_t *p_dec = (decoder_t*) p_this; int i_cat, i_codec_id, i_result; char *psz_namecodec; AVCodecContext *p_context = NULL; AVCodec *p_codec = NULL; /* *** determine codec type *** */ if( !E_(GetFfmpegCodec)( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id, &psz_namecodec ) ) { return VLC_EGENERIC; } /* Bail out if buggy decoder */ if( i_codec_id == CODEC_ID_AAC ) { msg_Dbg( p_dec, "refusing to use ffmpeg's (%s) decoder which is buggy", psz_namecodec ); return VLC_EGENERIC; } /* Initialization must be done before avcodec_find_decoder() */ E_(InitLibavcodec)(p_this); /* *** ask ffmpeg for a decoder *** */ p_codec = avcodec_find_decoder( i_codec_id ); if( !p_codec ) { msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec ); return VLC_EGENERIC; } /* *** get a p_context *** */ p_context = avcodec_alloc_context(); if( !p_context ) return VLC_ENOMEM; p_context->debug = config_GetInt( p_dec, "ffmpeg-debug" ); p_context->opaque = (void *)p_this; /* Set CPU capabilities */ p_context->dsp_mask = 0; if( !(p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) ) { p_context->dsp_mask |= FF_MM_MMX; } if( !(p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) ) { p_context->dsp_mask |= FF_MM_MMXEXT; } if( !(p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) ) { p_context->dsp_mask |= FF_MM_3DNOW; } if( !(p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) ) { p_context->dsp_mask |= FF_MM_SSE; } if( !(p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2) ) { p_context->dsp_mask |= FF_MM_SSE2; } switch( i_cat ) { case VIDEO_ES: p_dec->b_need_packetized = VLC_TRUE; p_dec->pf_decode_video = E_(DecodeVideo); i_result = E_( InitVideoDec )( p_dec, p_context, p_codec, i_codec_id, psz_namecodec ); break; case AUDIO_ES: p_dec->pf_decode_audio = E_(DecodeAudio); i_result = E_( InitAudioDec )( p_dec, p_context, p_codec, i_codec_id, psz_namecodec ); break; default: i_result = VLC_EGENERIC; } if( i_result == VLC_SUCCESS ) p_dec->p_sys->i_cat = i_cat; return i_result;}/***************************************************************************** * CloseDecoder: decoder destruction *****************************************************************************/static void CloseDecoder( vlc_object_t *p_this ){ decoder_t *p_dec = (decoder_t *)p_this; decoder_sys_t *p_sys = p_dec->p_sys; vlc_value_t lockval; var_Get( p_dec->p_libvlc, "avcodec", &lockval ); switch( p_sys->i_cat ) { case AUDIO_ES: E_( EndAudioDec )( p_dec ); break; case VIDEO_ES: E_( EndVideoDec )( p_dec ); break; } if( p_sys->p_context ) { if( p_sys->p_context->extradata ) free( p_sys->p_context->extradata ); p_sys->p_context->extradata = NULL; vlc_mutex_lock( lockval.p_address ); avcodec_close( p_sys->p_context ); vlc_mutex_unlock( lockval.p_address ); msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec ); av_free( p_sys->p_context ); } free( p_sys );}/***************************************************************************** * local Functions *****************************************************************************/static void LibavcodecCallback( void *p_opaque, int i_level, const char *psz_format, va_list va ){ int i_vlc_level; AVCodecContext *p_avctx = (AVCodecContext *)p_opaque; AVClass *p_avc; vlc_object_t *p_this; char *psz_new_format; const char *psz_item_name; p_avc = p_avctx ? p_avctx->av_class : 0;#define cln p_avc->class_name /* Make sure we can get p_this back */ if( !p_avctx || !p_avc || !cln || cln[0]!='A' || cln[1]!='V' || cln[2]!='C' || cln[3]!='o' || cln[4]!='d' || cln[5]!='e' || cln[6]!='c' ) { if( i_level == AV_LOG_ERROR ) vfprintf( stderr, psz_format, va ); return; }#undef cln p_this = (vlc_object_t *)p_avctx->opaque; switch( i_level ) { case AV_LOG_QUIET: i_vlc_level = VLC_MSG_ERR; break; case AV_LOG_ERROR: i_vlc_level = VLC_MSG_WARN; break; case AV_LOG_INFO: i_vlc_level = VLC_MSG_DBG; break; case AV_LOG_DEBUG: /* Print debug messages if they were requested */ if( p_avctx->debug ) vfprintf( stderr, psz_format, va ); return; default: return; } psz_item_name = p_avc->item_name(p_opaque); psz_new_format = malloc( strlen(psz_format) + strlen(psz_item_name) + 18 + 5 ); snprintf( psz_new_format, strlen(psz_format) + strlen(psz_item_name)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -