我试图对视频进行编码(此时使用h264编解码器,但其他编解码器也可以,如果更适合我的需要),这样解码所需的数据可以在一帧(包括第一帧)被编码后直接使用(所以,我只需要I和P帧,没有B帧)。C++,FFMPEG,H264,创建零延迟流
如何设置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 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,但我不明白他在做什么不同
我不熟悉h.264的内部结构,但它遇到与MPEG相同的问题,在音频开始之前需要几帧? (如果你不确定我在说什么,搜索无间隙MP3,这是一个常见问题,我怀疑这对许多有损编解码器来说是个问题。) – Brad 2012-04-15 15:32:21
我也是一个视频压缩的新手,所以我不' t知道这一点,但我只编码视频,没有音频 – Mat 2012-04-15 15:33:22
我假设你也只使用一个线程,就像邮件列表中的人一样。我在他的例子中看到的只是他手动设置了很多编码器参数,而我对ffmpeg的经验是,您设置/修复的次数越多,ffmepg在内部使用的启发法就越少,以查找过程的参数导致几帧延迟。尝试修复他修复的所有参数。 (例如mpCodecContext-> flags2 | = CODEC_FLAG2_FASTPSKIP;) – count0 2012-09-04 14:42:49