AVCodecContext,AVCodec结构体

AVCodecContext,AVCodec结构体,第1张

------------------------------------全系列文章目录------------------------------------ 相互关系

AVCodec
  • 该结构体用于描述编解码器信息
/* !!! 略有删减 */
typedef struct AVCodec {
    const char *name; 		//编解码器名称,编码器之间和解码器之间是唯一的,但是编码器和解码器可共享同一个名字
    const char *long_name;	//编解码器的描述性名称,更易于阅读
    enum AVMediaType type;  //指明编解码器类型,视频、音频或字幕
    enum AVCodecID id;		//编解码器id,如AV_CODEC_ID_MJPEG,AV_CODEC_ID_H264
    int capabilities;		
    uint8_t max_lowres;    //解码器支持的低分辨率最大值  
    const AVRational *supported_framerates; //编解码器支持的帧率组
    const enum AVPixelFormat *pix_fmts;     //编解码器支持的像素格式组
    const int *supported_samplerates;       //编解码器支持的采样率组
    const enum AVSampleFormat *sample_fmts; //编解码器支持的采样格式组
    const uint64_t *channel_layouts;        //编解码器支持的声道布局组
    const AVClass *priv_class;
    const AVProfile *profiles;              
    const char *wrapper_name;
    int caps_internal;
    int priv_data_size;
    const AVCodecDefault *defaults;
    const char *bsfs;
    const struct AVCodecHWConfigInternal *const *hw_configs;
    const uint32_t *codec_tags;
} AVCodec;
  • 每一个编解码器都对应一个该结构体,这些编解码器以一个链表的形式存储;因此可以通过调用 av_codec_next() 函数获取下一个编解码器指针,以此来遍历所有的编解码器。
AVCodecContext
  • 该结构体用于描述编解码器的上下文,包含了编解码器需要的参数信息。
/* !!! 略有删减 */
typedef struct AVCodecContext {
    const AVClass *av_class;
    int log_level_offset;
    enum AVMediaType 		codec_type; //编解码器种类
    const struct AVCodec  	*codec;		//编解码器AVCodec
    enum AVCodecID     		codec_id; 	//编解码器id,如AV_CODEC_ID_MJPEG,AV_CODEC_ID_H264
    unsigned int 			codec_tag;
    void 					*priv_data;
    struct AVCodecInternal 	*internal;
    void 					*opaque;	//用户的私人数据
    int64_t 				bit_rate;	//平均比特率,由用户设置
    int 					bit_rate_tolerance;
    int 					global_quality;		//全局编码质量
    int 					compression_level;	//编码压缩级别
#define FF_COMPRESSION_DEFAULT -1
    int flags;
    int flags2;
    uint8_t 	*extradata;			//一些编解码器需要的额外数据		
    int 		extradata_size;		//一些编解码器需要的额外数据大小
    AVRational 	time_base;			//时间基,可将pts等时间戳转换为实际时间(秒)
    int 		ticks_per_frame;	//以time_base为单位的帧率
    int delay;
    /* video only */
    int width, height;				//帧宽高
    int coded_width, coded_height;	//数据流宽高,因为存在解码帧在输出前被裁减或降低分辨率
    int gop_size;					//编码时一组图片的数量,即I帧间隔的数量
    enum AVPixelFormat pix_fmt;		//像素格式
    int max_b_frames;				//编码时,非B帧之间最大的B帧数量
    float b_quant_factor;
    float b_quant_offset;
    int has_b_frames;
    float i_quant_factor;
    float i_quant_offset;
    float lumi_masking;
    float temporal_cplx_masking;
    float spatial_cplx_masking;
    float p_masking;
    float dark_masking;
    int slice_count;
    int *slice_offset;
    AVRational sample_aspect_ratio;	//帧样本的宽高比
    int me_cmp;
    int me_sub_cmp;
    int mb_cmp;
    int ildct_cmp;
    int dia_size;
    int last_predictor_count;
    int me_pre_cmp;
    int pre_dia_size;
    int me_subpel_quality;
    int me_range;
    int slice_flags;
    int mb_decision;  
    uint16_t *intra_matrix;
    uint16_t *inter_matrix;
    int intra_dc_precision;
    int skip_top;
    int skip_bottom;
    int mb_lmin;
    int mb_lmax;
    int bidir_refine;
    int keyint_min;
    int refs;
    int mv0_threshold;
    enum AVColorPrimaries color_primaries;
    enum AVColorTransferCharacteristic color_trc;
    enum AVColorSpace colorspace;				//YUV色彩空间类型
    enum AVColorRange color_range;
    enum AVChromaLocation chroma_sample_location;
    int slices;
    enum AVFieldOrder field_order;

    /* audio only */
    int sample_rate; 					//采样率
    int channels;    					//通道数
    enum AVSampleFormat sample_fmt;  	//采样格式
    int frame_size;						//每个通道一帧音频帧的采样数
    int frame_number;					//帧计数器
    int block_align;
    int cutoff;							//编码时的音频截止带宽
    uint64_t channel_layout;			//声道布局
    uint64_t request_channel_layout;	//请求解码器使用该通道布局,0表示默认
    enum AVAudioServiceType audio_service_type;
    enum AVSampleFormat request_sample_fmt;	//请求解码器使用该采用格式
    int (*get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags);
    float qcompress;  
    float qblur;     
    int qmin;
    int qmax;
    int max_qdiff;
    int rc_buffer_size;
    int rc_override_count;
    RcOverride *rc_override;
    int64_t rc_max_rate;		//编解码时最大比特率
    int64_t rc_min_rate;		//编解码时小比特率
    float rc_max_available_vbv_use;
    float rc_min_vbv_overflow_use;
    int rc_initial_buffer_occupancy;
    int trellis;
    char *stats_out;
    char *stats_in;
    int workaround_bugs;
    int strict_std_compliance;
    int error_concealment;
    int debug;
    int err_recognition;      
    int64_t reordered_opaque;
    const struct AVHWAccel *hwaccel;
    void *hwaccel_context;
    uint64_t error[AV_NUM_DATA_POINTERS];
    int dct_algo;						//DCT算法
    int idct_algo;						//IDCT算法
    int bits_per_coded_sample;			//每个样本或像素的位数
    int bits_per_raw_sample;
    int lowres;							//低分辨率解码,1-> 1/2 size, 2->1/4 size
    int thread_count;
    int thread_type;
    int active_thread_type;
     int nsse_weight;
     int profile;
     int level;
#define FF_LEVEL_UNKNOWN -99
    enum AVDiscard skip_loop_filter;
    enum AVDiscard skip_idct;
    enum AVDiscard skip_frame;
    uint8_t *subtitle_header;
    int subtitle_header_size;
    int initial_padding;
    AVRational framerate;				//帧率
    enum AVPixelFormat sw_pix_fmt;
    AVRational pkt_timebase;			//pkt_dts/pts和AVPacket.dts/pts的时间基
    const AVCodecDescriptor *codec_descriptor;
    int64_t pts_correction_num_faulty_pts; 
    int64_t pts_correction_num_faulty_dts;
    int64_t pts_correction_last_pts;    
    int64_t pts_correction_last_dts;     
    char *sub_charenc;
    int sub_charenc_mode;
    int skip_alpha;
    int seek_preroll;
    uint16_t *chroma_intra_matrix;
    uint8_t *dump_separator;
    char *codec_whitelist;
    unsigned properties;
    AVPacketSideData *coded_side_data;
    int            nb_coded_side_data;
    AVBufferRef *hw_frames_ctx;
    int trailing_padding;
    int64_t max_pixels;					//每个图像最大接收的像素数
    AVBufferRef *hw_device_ctx;
    int hwaccel_flags;
    int apply_cropping;
    int extra_hw_frames;
    int discard_damaged_percentage;		
    int64_t max_samples;				//每个音频帧最大接收的采样数
    int export_side_data;
} AVCodecContext;
示例代码
  • 打印AVCodec信息

    void AVCodec_info(AVCodec *codec) 
    {
        printf("name: %s\n", codec->name);
        printf("long name: %s\n", codec->long_name);
    
        if (codec->type == AVMEDIA_TYPE_VIDEO) {
            printf("type: VIDEO, ");
        } else if (codec->type == AVMEDIA_TYPE_AUDIO) {
            printf("type: AUDIO, ");
        } else {
            printf("type: %d, ", codec->type);
        }
        printf("codec_id: %d\n", codec->id);
    
        printf("support framerates: ");
        const AVRational *p_fps = codec->supported_framerates;
        while (p_fps != NULL) {
            printf("[%d %d] ; ", p_fps->num, p_fps->den);
            p_fps ++;
        }
        printf("\n");
        printf("support pix_fmts: ");
        const enum AVPixelFormat *p_pixfmt = codec->pix_fmts;
        while (p_pixfmt != NULL && *p_pixfmt >= 0) {
            printf("[%d] ; ", *p_pixfmt);
            p_pixfmt ++;
        }
        printf("\n");
        printf("support samplerates: ");
        const int *p_sample_rate = codec->supported_samplerates;
        while (p_sample_rate != NULL && *p_sample_rate >= 0) {
            printf("[%d] ; ", *p_sample_rate);
            p_sample_rate ++;
        }
        printf("\n");
        printf("support sample_fmts: ");
        const enum AVSampleFormat *p_sample_fmt = codec->sample_fmts;
        while (p_sample_fmt != NULL && *p_sample_fmt >= 0) {
            printf("[%d] ; ", *p_sample_fmt);
            p_sample_fmt ++;
        }
        printf("\n");
        printf("support channel_layouts: ");
        const uint64_t *p_ch_lay = codec->channel_layouts;
        while (p_ch_lay != NULL && *p_ch_lay > 0) {
            printf("[%lu] ; ", *p_ch_lay);
            p_ch_lay ++;
        }
        printf("\n\n");
    }
    
  • 打印AVCodecContext信息

    void AVCodecContext_info(AVCodecContext *Ctx) {
        if (Ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
            printf("type: VIDEO, ");
        } else if (Ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
            printf("type: AUDIO, ");
        } else {
            printf("type: %d, ", Ctx->codec_type);
        }
    
        printf("codec_id: %d\n", Ctx->codec_id);
        printf("time_base: %d, %d\n", Ctx->time_base.num, Ctx->time_base.den);
        printf("width: %d, height: %d, PixelFormat: %d\n", Ctx->width, Ctx->height, Ctx->pix_fmt);
        printf("width : height = %d : %d\n", Ctx->sample_aspect_ratio.num, Ctx->sample_aspect_ratio.den);
        printf("channels: %d, channel layout: %lu\n", Ctx->channels, Ctx->channel_layout);
        printf("sample_rate: %d, sample_fmt: %d, frame_size: %d\n", Ctx->sample_rate, Ctx->sample_fmt, Ctx->frame_size);
        printf("frame rate: %d, %d\n\n", Ctx->framerate.num, Ctx->framerate.den);
    }
    
  • 输出测试视频的AVCodec和AVCodecContext信息

    int main(int argc, char **argv)
    {
        AVFormatContext   *fmt_ctx  = NULL;
        AVCodec           *vDc, *aDc;
        AVCodecContext    *vDcCtx, *aDcCtx;
    
        avformat_network_init();
        avformat_open_input(&fmt_ctx, "../../output/test.mp4", NULL, NULL);
        avformat_find_stream_info(fmt_ctx, NULL);
        
        AVStream *st1 = fmt_ctx->streams[0];
        AVStream *st2 = fmt_ctx->streams[1];
    
        vDcCtx = avcodec_alloc_context3(NULL);
        aDcCtx = avcodec_alloc_context3(NULL);
        avcodec_parameters_to_context(vDcCtx, st1->codecpar);
        avcodec_parameters_to_context(aDcCtx, st2->codecpar);
        vDc    = avcodec_find_decoder(vDcCtx->codec_id);
        aDc    = avcodec_find_decoder(aDcCtx->codec_id);
        avcodec_open2(vDcCtx, vDc, NULL);
        avcodec_open2(aDcCtx, aDc, NULL);
    
        AVCodecContext_info(vDcCtx);
        AVCodecContext_info(aDcCtx);
    
        AVCodec_info(vDc);
        AVCodec_info(aDc);
        return 0;
    }
    
  • 输出信息如下

    type: VIDEO, codec_id: 27
    time_base: 0, 2
    width: 1280, height: 720, PixelFormat: 0
    width : height = 1 : 1
    channels: 0, channel layout: 0
    sample_rate: 0, sample_fmt: -1, frame_size: 0
    frame rate: 0, 1
    
    type: AUDIO, codec_id: 86018
    time_base: 1, 44100
    width: 0, height: 0, PixelFormat: -1
    width : height = 0 : 1
    channels: 2, channel layout: 3
    sample_rate: 44100, sample_fmt: 8, frame_size: 1024
    frame rate: 0, 1
    
    name: h264
    long name: H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
    type: VIDEO, codec_id: 27
    support framerates: 
    support pix_fmts: 
    support samplerates: 
    support sample_fmts: 
    support channel_layouts: 
    
    name: aac
    long name: AAC (Advanced Audio Coding)
    type: AUDIO, codec_id: 86018
    support framerates: 
    support pix_fmts: 
    support samplerates: 
    support sample_fmts: [8] ; 
    support channel_layouts: [4] ; [3] ; [7] ; [263] ; [55] ; [63] ; [255] ; 
    

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/727448.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存