[英]c++, FFMPEG, H264, creating zero-delay stream
我正在尝试对视频进行编码(目前使用h264编解码器,但如果更适合我的需求,其他编解码器也可以),以便在编码一帧(包括第一帧)后直接获得解码所需的数据(所以,我只需要I和P帧,不希望B帧)。
我需要如何设置AVCodecContext以获得这样的流? 到目前为止,我对这些值的测试仍然总是导致avcodec_encode_video()在第一帧返回0。
// edit:这是当前我的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 found\n");
exit(1);
}
st = avformat_new_stream(oc, codec);
if (!st) {
fprintf(stderr, "Could not alloc stream\n");
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);
(在打开上下文之前,请调用此函数。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.