加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c,FFMPEG,H264,创建零延迟流

发布时间:2020-12-16 07:30:02 所属栏目:百科 来源:网络整理
导读:我正在尝试编码视频(目前使用h264编解码器,但如果更适合我的需要,其他编解码器也会很好),这样解码所需的数据可以在帧(包括第一帧)编码后直接使用(所以,我只想要I帧和P帧,没有B帧). 如何设置AVCodecContext以获取此类流?到目前为止,我的测试结果仍然总是导致
我正在尝试编码视频(目前使用h264编解码器,但如果更适合我的需要,其他编解码器也会很好),这样解码所需的数据可以在帧(包括第一帧)编码后直接使用(所以,我只想要I帧和P帧,没有B帧).

如何设置AVCodecContext以获取此类流?到目前为止,我的测试结果仍然总是导致avcodec_encode_video()在第一帧返回0.

//编辑:这是我目前AVCodecContext的设置代码:

static AVStream* add_video_stream(AVFormatContext *oc,enum CodecID codec_id,int w,int h,int fps)
{
    AVCodecContext *c;
    AVStream *st;
    AVCodec *codec;

    /* find the video encoder */
    codec = avcodec_find_encoder(codec_id);
    if (!codec) {
        fprintf(stderr,"codec not foundn");
        exit(1);
    }

    st = avformat_new_stream(oc,codec);
    if (!st) {
        fprintf(stderr,"Could not alloc streamn");
        exit(1);
    }

    c = st->codec;

    /* Put sample parameters. */
    c->bit_rate = 400000;
    /* Resolution must be a multiple of two. */
    c->width    = w;
    c->height   = h;
    /* timebase: This is the fundamental unit of time (in seconds) in terms
     * of which frame timestamps are represented. For fixed-fps content,* timebase should be 1/framerate and timestamp increments should be
     * identical to 1. */
    c->time_base.den = fps;
    c->time_base.num = 1;
    c->gop_size      = 12; /* emit one intra frame every twelve frames at most */

    c->codec = codec;
    c->codec_type = AVMEDIA_TYPE_VIDEO;
    c->coder_type = FF_CODER_TYPE_VLC;
    c->me_method = 7; //motion estimation algorithm
    c->me_subpel_quality = 4;
    c->delay = 0; 
    c->max_b_frames = 0; 
    c->thread_count = 1; // more than one threads seem to increase delay
    c->refs = 3;

    c->pix_fmt       = PIX_FMT_YUV420P;

    /* Some formats want stream headers to be separate. */
    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;

    return st;
}

但是使用这个avcodec_encode_video()将在返回任何字节之前缓冲13帧(之后,它将在每帧上返回字节).如果我将gop_size设置为0,那么avcodec_encode_video()将仅在第二帧传递给它后返回字节.我需要零延迟.

这个人显然是成功的(即使有更大的gop):http://mailman.videolan.org/pipermail/x264-devel/2009-May/005880.html,但我看不出他在做什么不同

解决方法

对我来说,这段代码做了诀窍:

av_opt_set(context->priv_data,"tune","zerolatency",0);

(在打开上下文之前调用此函数.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读