[Libav-user] x264 encoding only works properly with zerolatency option

José Tomás Tocino García josetomas.tocino at gmail.com
Mon May 24 14:08:10 EEST 2021


Hi.
I'm building an application that captures the screen and creates a video
with the saved frames. I'm encoding with x264 on an mp4 file.

The problem I'm facing is that if I don't use the "zerolatency" option, the
video file plays back with a lot of artifacts and green blocks. Also, when
the encoding starts, the avcodec_encode_video2 produces a lot of empty
results (got_picture == 0) with a message saying

        [libx264 @ 0x7f45f00013c0] non-strictly-monotonic PTS

I set up my AVCodecContext with the following options:

    AVCodecContext * pCodecCtx = pVideoStream->codec;

    if (pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)
        pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;

    pCodecCtx->pix_fmt = outputPixelFormat; // AV_PIX_FMT_YUV420P
    pCodecCtx->bit_rate = 5000 * 1000;
    pCodecCtx->width = outputWidth;
    pCodecCtx->height = outputHeight;
    pCodecCtx->time_base = timeBase; // {1, 250}
    pCodecCtx->thread_count = 16;

    // Set Option
    AVDictionary * param = 0;

    fprintf(stderr, "Setting preset... \n");
    av_dict_set(&param, "preset", "ultrafast", 0);
    av_dict_set(&param, "crf", "23", 0);

    // av_dict_set(&param, "tune", "zerolatency", 0);

These are the stats that appear when I open the encoder :

[libx264 @ 0x7f45f00013c0] using cpu capabilities: MMX2 SSE2Fast SSSE3
SSE4.2 AVX AVX2 FMA3 LZCNT BMI2
[libx264 @ 0x7f45f00013c0] profile High, level 5.2
[libx264 @ 0x7f45f00013c0] 264 - core 142 r2495 6a301b6 - H.264/MPEG-4 AVC
codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options:
cabac=1 ref=2 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=6 psy=1
psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1
cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=16
lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0
bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1
b_bias=0 direct=1 weightb=1 open_gop=0 weightp=1 keyint=250 keyint_min=25
scenecut=40 intra_refresh=0 rc_lookahead=30 rc=crf mbtree=1 crf=23.0
qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.41 aq=1:1.00

And these are the stats for a sample, 15-seconds-long recorded video:

[libx264 @ 0x7fe9440013c0] frame I:2     Avg QP:10.92  size:  7930
[libx264 @ 0x7fe9440013c0] frame P:295   Avg QP:13.07  size:  5101
[libx264 @ 0x7fe9440013c0] frame B:83    Avg QP:16.71  size:  1864
[libx264 @ 0x7fe9440013c0] consecutive B-frames: 62.5% 24.1%  0.8% 12.6%
[libx264 @ 0x7fe9440013c0] mb I  I16..4: 68.7%  1.1% 30.2%
[libx264 @ 0x7fe9440013c0] mb P  I16..4:  1.8%  0.2%  0.3%  P16..4:  6.9%
10.4% 13.6%  0.0%  0.0%    skip:66.8%
[libx264 @ 0x7fe9440013c0] mb B  I16..4:  0.3%  0.0%  0.0%  B16..8:  3.8%
 6.5%  6.7%  direct: 1.2%  skip:81.5%  L0:38.6% L1:39.7% BI:21.7%
[libx264 @ 0x7fe9440013c0] 8x8 transform intra:6.4% inter:2.1%
[libx264 @ 0x7fe9440013c0] coded y,uvDC,uvAC intra: 11.0% 24.5% 20.7%
inter: 13.7% 25.5% 25.3%
[libx264 @ 0x7fe9440013c0] i16 v,h,dc,p: 75% 19%  7%  0%
[libx264 @ 0x7fe9440013c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu:  4%  2% 91%  0%
 0%  2%  0%  0%  0%
[libx264 @ 0x7fe9440013c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 12% 46%  3%
 4%  4%  7%  3%  2%
[libx264 @ 0x7fe9440013c0] i8c dc,h,v,p: 67% 15% 16%  2%
[libx264 @ 0x7fe9440013c0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x7fe9440013c0] ref P L0: 84.5% 15.5%
[libx264 @ 0x7fe9440013c0] ref B L0: 88.2% 11.8%
[libx264 @ 0x7fe9440013c0] ref B L1: 99.6%  0.4%
[libx264 @ 0x7fe9440013c0] kb/s:16.06

If I uncomment the last line, thus applying the "zerolatency" option, the
recorded video looks way better. Inspecting the stats for the second video:

[libx264 @ 0x7fb4fc0013c0] frame I:2     Avg QP:13.53  size:  5804
[libx264 @ 0x7fb4fc0013c0] frame P:378   Avg QP:28.15  size:  1306
[libx264 @ 0x7fb4fc0013c0] mb I  I16..4: 65.9%  4.0% 30.1%
[libx264 @ 0x7fb4fc0013c0] mb P  I16..4:  0.5%  2.4%  0.3%  P16..4: 19.3%
11.2%  7.1%  0.0%  0.0%    skip:59.2%
[libx264 @ 0x7fb4fc0013c0] 8x8 transform intra:66.0% inter:18.1%
[libx264 @ 0x7fb4fc0013c0] coded y,uvDC,uvAC intra: 6.5% 15.6% 11.9% inter:
8.5% 16.2% 12.0%
[libx264 @ 0x7fb4fc0013c0] i16 v,h,dc,p: 54% 15% 30%  0%
[libx264 @ 0x7fb4fc0013c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu:  2%  4% 93%  0%
 0%  0%  0%  0%  0%
[libx264 @ 0x7fb4fc0013c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 15%  9% 59%  3%
 3%  2%  5%  2%  2%
[libx264 @ 0x7fb4fc0013c0] i8c dc,h,v,p: 72% 16% 12%  1%
[libx264 @ 0x7fb4fc0013c0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x7fb4fc0013c0] ref P L0: 85.1% 14.9%
[libx264 @ 0x7fb4fc0013c0] kb/s:2659.88

I can see that there are no B-frames.

I'm not sure what else I can do other than leaving the zerolatency option
turned on, but it slows down the encoding quite significantly.

Thanks.
Regards,

-- 
José Tomás Tocino
http://josetomastocino.com
http://cadizenmoto.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20210524/5847830b/attachment.htm>


More information about the Libav-user mailing list