用AVCodecParameters代替AVCodecContext

用AVCodecParameters代替AVCodecContext

高版本的FFmpeg 如下代码会报 “warning: ‘AVStream::codec’ is deprecated” “Using AVStream.codec … deprecated, use AVStream.codecpar instead”,说明 AVStream.codec这个已经被遗弃,可以使用 AVStream.codecpar 代替就行,网上查到 使用avcodec_parameters_from_context()可以将AVCodecContext 结构体成员值复制到 AVCodecParameters

static AVStream * add_video_stream(AVFormatContext *oc, int codec_id, int w, int h, int fps)   
{   
    AVCodecContext *c = 0;   
    AVStream *st = 0;   

	st = avformat_new_stream(oc, 0);
    if (!st) 
	{   
        log_print(LOG_ERR, "Could not alloc stream\n");   
        return NULL;   
    }   
   
    c = st->codec;   
    c->codec_id = (AVCodecID)codec_id;   
    c->codec_type = AVMEDIA_TYPE_VIDEO;
   
    /* put sample parameters */   
    c->bit_rate = (int) (w * h * fps * 8);  

	c->qblur = 0.5f;

    /* resolution must be a multiple of two */   
    c->width = w;   
    c->height = h; 
    
	c->time_base.num = 1;    
    c->time_base.den = fps;

    c->gop_size = 60; /* emit one intra frame every twelve frames at most */   
    c->pix_fmt = AV_PIX_FMT_YUV420P;     

    if (oc->oformat->flags & AVFMT_GLOBALHEADER) 
    {
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;		
    }

    if (0 == c->sample_aspect_ratio.num)
    {
        c->sample_aspect_ratio.num = c->sample_aspect_ratio.den;
    }
    
 	st->sample_aspect_ratio.num = c->sample_aspect_ratio.num;
    st->sample_aspect_ratio.den = c->sample_aspect_ratio.den;
    
    return st;   
}   

修改后的代码如下所示:

//[用AVCodecParameters代替AVCodecContext](https://blog.csdn.net/luotuo44/article/details/54981809)
static AVStream * add_video_stream(AVFormatContext *oc, int codec_id, int w, int h, int fps)   
{   
    AVCodecContext codec_ctx;
    AVCodecParameters *cp = 0;
    AVStream *st = 0;   

	st = avformat_new_stream(oc, 0);
    if (!st) 
	{   
        log_print(LOG_ERR, "Could not alloc stream\n");   
        return NULL;   
    }   
   
//    c = st->codec;
    cp = st->codecpar;
    codec_ctx.codec_id = (AVCodecID)codec_id;
    codec_ctx.codec_type = AVMEDIA_TYPE_VIDEO;
   
    /* put sample parameters */   
    codec_ctx.bit_rate = (int) (w * h * fps * 8);

    codec_ctx.qblur = 0.5f;

    /* resolution must be a multiple of two */   
    codec_ctx.width = w;
    codec_ctx.height = h;
    
    codec_ctx.time_base.num = 1;
    codec_ctx.time_base.den = fps;
    st->time_base.num = 1;
    st->time_base.den = fps;

    codec_ctx.gop_size = 60; /* emit one intra frame every twelve frames at most */
    codec_ctx.pix_fmt = AV_PIX_FMT_YUV420P;

    if (oc->oformat->flags & AVFMT_GLOBALHEADER) 
    {
        codec_ctx.flags |= CODEC_FLAG_GLOBAL_HEADER;
    }

    if (0 == codec_ctx.sample_aspect_ratio.num)
    {
        codec_ctx.sample_aspect_ratio.num = codec_ctx.sample_aspect_ratio.den;
    }
    
    st->sample_aspect_ratio.num = codec_ctx.sample_aspect_ratio.num;
    st->sample_aspect_ratio.den = codec_ctx.sample_aspect_ratio.den;

    avcodec_parameters_from_context(cp, &codec_ctx);
    
    return st;   
}

你可能感兴趣的:(嵌入式,FFmpeg)