<div dir="ltr">Hi.<div>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.</div><div><br></div><div>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</div><div><br></div><div>        [libx264 @ 0x7f45f00013c0] non-strictly-monotonic PTS<br></div><div><br></div><div>I set up my AVCodecContext with the following options:</div><div><br></div><div>    AVCodecContext * pCodecCtx = pVideoStream->codec;<br><br>    if (pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)<br>        pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;<br><br>    pCodecCtx->pix_fmt = outputPixelFormat; // AV_PIX_FMT_YUV420P<br>    pCodecCtx->bit_rate = 5000 * 1000;<br>    pCodecCtx->width = outputWidth;<br>    pCodecCtx->height = outputHeight;<br>    pCodecCtx->time_base = timeBase; // {1, 250}<br>    pCodecCtx->thread_count = 16;<br><br>    // Set Option<br>    AVDictionary * param = 0;<br><br>    fprintf(stderr, "Setting preset... \n");<br>    av_dict_set(&param, "preset", "ultrafast", 0);<br>    av_dict_set(&param, "crf", "23", 0);</div><div><br></div><div>    // av_dict_set(&param, "tune", "zerolatency", 0);</div><div><br></div><div>These are the stats that appear when I open the encoder :</div><div><br></div><div>[libx264 @ 0x7f45f00013c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX AVX2 FMA3 LZCNT BMI2<br>[libx264 @ 0x7f45f00013c0] profile High, level 5.2<br>[libx264 @ 0x7f45f00013c0] 264 - core 142 r2495 6a301b6 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - <a href="http://www.videolan.org/x264.html">http://www.videolan.org/x264.html</a> - 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</div><div><br></div><div>And these are the stats for a sample, 15-seconds-long recorded video:</div><div><br></div><div>[libx264 @ 0x7fe9440013c0] frame I:2     Avg QP:10.92  size:  7930<br>[libx264 @ 0x7fe9440013c0] frame P:295   Avg QP:13.07  size:  5101<br>[libx264 @ 0x7fe9440013c0] frame B:83    Avg QP:16.71  size:  1864<br>[libx264 @ 0x7fe9440013c0] consecutive B-frames: 62.5% 24.1%  0.8% 12.6%<br>[libx264 @ 0x7fe9440013c0] mb I  I16..4: 68.7%  1.1% 30.2%<br>[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%<br>[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%<br>[libx264 @ 0x7fe9440013c0] 8x8 transform intra:6.4% inter:2.1%<br>[libx264 @ 0x7fe9440013c0] coded y,uvDC,uvAC intra: 11.0% 24.5% 20.7% inter: 13.7% 25.5% 25.3%<br>[libx264 @ 0x7fe9440013c0] i16 v,h,dc,p: 75% 19%  7%  0%<br>[libx264 @ 0x7fe9440013c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu:  4%  2% 91%  0%  0%  2%  0%  0%  0%<br>[libx264 @ 0x7fe9440013c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 12% 46%  3%  4%  4%  7%  3%  2%<br>[libx264 @ 0x7fe9440013c0] i8c dc,h,v,p: 67% 15% 16%  2%<br>[libx264 @ 0x7fe9440013c0] Weighted P-Frames: Y:0.0% UV:0.0%<br>[libx264 @ 0x7fe9440013c0] ref P L0: 84.5% 15.5%<br>[libx264 @ 0x7fe9440013c0] ref B L0: 88.2% 11.8%<br>[libx264 @ 0x7fe9440013c0] ref B L1: 99.6%  0.4%<br>[libx264 @ 0x7fe9440013c0] kb/s:16.06<br></div><div><br></div><div>If I uncomment the last line, thus applying the "zerolatency" option, the recorded video looks way better. Inspecting the stats for the second video:<br></div><div><br></div><div>[libx264 @ 0x7fb4fc0013c0] frame I:2     Avg QP:13.53  size:  5804<br>[libx264 @ 0x7fb4fc0013c0] frame P:378   Avg QP:28.15  size:  1306<br>[libx264 @ 0x7fb4fc0013c0] mb I  I16..4: 65.9%  4.0% 30.1%<br>[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%<br>[libx264 @ 0x7fb4fc0013c0] 8x8 transform intra:66.0% inter:18.1%<br>[libx264 @ 0x7fb4fc0013c0] coded y,uvDC,uvAC intra: 6.5% 15.6% 11.9% inter: 8.5% 16.2% 12.0%<br>[libx264 @ 0x7fb4fc0013c0] i16 v,h,dc,p: 54% 15% 30%  0%<br>[libx264 @ 0x7fb4fc0013c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu:  2%  4% 93%  0%  0%  0%  0%  0%  0%<br>[libx264 @ 0x7fb4fc0013c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 15%  9% 59%  3%  3%  2%  5%  2%  2%<br>[libx264 @ 0x7fb4fc0013c0] i8c dc,h,v,p: 72% 16% 12%  1%<br>[libx264 @ 0x7fb4fc0013c0] Weighted P-Frames: Y:0.0% UV:0.0%<br>[libx264 @ 0x7fb4fc0013c0] ref P L0: 85.1% 14.9%<br>[libx264 @ 0x7fb4fc0013c0] kb/s:2659.88<br></div><div><br></div><div>I can see that there are no B-frames. </div><div><br></div><div>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.</div><div><br></div><div>Thanks.</div><div>Regards,</div><div><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div style="font-size:small">José Tomás Tocino</div><div style="font-size:small"><a href="http://josetomastocino.com" target="_blank">http://josetomastocino.com</a></div><div style="font-size:small"><a href="http://cadizenmoto.com" target="_blank">http://cadizenmoto.com</a></div></div></div></div></div></div>