【发布时间】:2012-04-26 15:50:13
【问题描述】:
我正在尝试对视频进行编码(目前使用 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 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。这是一个常见问题,我怀疑这是许多有损编解码器的问题。)
-
我也是视频压缩的新手,所以我不知道,但我只编码视频,没有音频
-
我假设您也只使用了一个线程,就像邮件列表中的那个人一样。我在他的示例中看到的唯一一件事是他手动设置了许多编码器参数,而我对 ffmpeg 的经验是,您设置/修复的越多,ffmepg 在内部使用的启发式方法就越少,以找出您的过程参数,这通常导致几帧延迟。尝试修复他修复的所有参数。 (例如 mpCodecContext->flags2 |= CODEC_FLAG2_FASTPSKIP;)
标签: c++ ffmpeg video-streaming