[Libav-user] Trouble optimizing x264

Adam Fenn adamfenn28 at gmail.com
Sun Oct 23 03:01:04 EEST 2016

I’m trying to use the following code to configure the library to live stream via RTMP.  I’ve found a couple problems that I can’t seem to work out.

1. The bit rate is not well controlled at all. I may end up streaming at a rate twice my _encoder_preset.bitRate.  How can I control it better?  I’ve tried using:


They have not worked at all, as far as I can tell. I even set min and max to 0 to test prove they have no impact.

2.  With some video sources I can see when my keyframes kick in.  It’s especially when I have a live camera on a zoomed out image of something almost static.  Every 2 seconds I can see the less detailed areas of the image shift their color slightly.  How can I optimize so that they keyframes are not noticeable at all?

3. When I stream a static image for some number of minutes, then switch to a different image, my CPU and packet rate go way up (3X) for some period for a few minutes.  The longer I stream the static image before I cut to a different scene, the worse the impact.  This issue does not happen unless the initial scene is almost 100% static for a while.  I think this problem is something to do with my settings below, but I can’t be sure.  What could this problem be?

  AVCodecContext *cc = _stream_video->codec;
    cc->codec_type = AVMEDIA_TYPE_VIDEO;
    cc->codec_id = AV_CODEC_ID_H264;
    cc->width = (_encoder_preset.width + 1 ) / 2 * 2 ; // using 1280
    cc->height = _encoder_preset.height; //using 720
    cc->bit_rate = _encoder_preset.bitRate * 1024; // using 3300 for bitRate
    cc->bit_rate_tolerance = cc->bit_rate * .1; // trying to restrict variance to 10%
    cc->time_base.num = 1;
    cc->time_base.den = _encoder_preset.framesPerSecond; //using 30fps
    cc->pix_fmt = AV_PIX_FMT_YUV420P;
    cc->coder_type = 1;
    cc->flags |= CODEC_FLAG_LOOP_FILTER;
    cc->flags |= CODEC_FLAG_CLOSED_GOP;
    cc->me_cmp |= 1;
    cc->me_method = ME_HEX;
    cc->me_subpel_quality = 4;
    cc->me_range = 16;
    cc->gop_size = _encoder_preset.keyFrameInSecs * _encoder_preset.framesPerSecond; //using 60 frames
    cc->keyint_min = 9;
    cc->scenechange_threshold = 40;
    cc->i_quant_factor = 0.71;
    cc->b_frame_strategy = 1;
    cc->qcompress = 0.6;
    cc->qmin = 10;
    cc->qmax = 51;
    cc->max_qdiff = 4;
    cc->max_b_frames = 3;
    cc->refs = 6; 
    cc->trellis = 1;
    cc->profile = FF_PROFILE_H264_HIGH;
    av_opt_set(cc->priv_data, "profile","high",0);
    av_opt_set(cc->priv_data, "preset", "fast", 0);

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20161022/faaa50a5/attachment.html>

More information about the Libav-user mailing list