<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body dir="auto">The change is gradual, not instant. It will change over time. At least it did in my testing of directly modifying the x264 code. Set it to crf 45 and then change to 12. It will do something for sure.<div><br></div><div style="font-size:100%;color:#000000" dir="auto"><!-- originalMessage --><div>-------- Original message --------</div><div>From: 田浩杨 <734512183@qq.com> </div><div>Date: 16/12/2019  10:46  (GMT+01:00) </div><div>To: "This list is about using libavcodec, libavformat, libavutil,libavdevice and libavfilter." <libav-user@ffmpeg.org> </div><div>Subject: [Libav-user] 回复:  videorate realtime-adjusting </div><div><br></div></div><div>Excuse me, I have called the API-'reconfig_encoder' to controll the bitrate of X264-encoder in real time, but I found it didn't work by network monitor. I changed the  AVCodecContext->bit_rate \ AVCodecContext->rc_max_rate \  AVCodecContext->rc_buffer_size. And I checked the return value of 'x264_encoder_reconfig' which equals to zero. So whats the problem? </div><div><div><br></div><div style="font-size: 12px;font-family: Arial Narrow;padding:2px 0 2px 0;">------------------ 原始邮件 ------------------</div><div style="font-size: 12px;background:#efefef;padding:8px;"><div><b>发件人:</b> "Strahinja Radman"<dr.strashni@gmail.com>;</div><div><b>发送时间:</b> 2019年12月2日(星期一) 下午5:03</div><div><b>收件人:</b> "This list is about using libavcodec, libavformat, libavutil,libavdevice and libavfilter."<libav-user@ffmpeg.org>;<wbr></div><div></div><div><b>主题:</b> Re: [Libav-user] videorate realtime-adjusting</div></div><div><br></div><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Dec 2, 2019 at 9:29 AM 田浩杨 <<a href="mailto:734512183@qq.com">734512183@qq.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div>I have a question. How can I modify the video-encode bitrate in real time during coding after the video encoder has been turned on? Here's what I did (take x264-encoder as an example) :</div><div><br></div><div>step1.</div><div>Modified the definition of structural AVCoder to add a function pointer.</div><div><span style="white-space:pre-wrap"> </span>int (*valid_context)(AVCodecContext*);</div><div><br></div><div>step2.</div><div>Added function definition to enable the codec rate of x264.</div><div>static int X264_config(AVCodecContext* avctx){</div><div>    X264Context* x4 = avctx->priv_data;</div><div>    if(avctx->bit_rate){</div><div>        x4->params.rc.i_bitrate = avctx->bit_rate / 1000;</div><div>    }</div><div>    avctx->bit_rate = x4->params.rc.i_bitrate*1000;</div><div>    return 0;</div><div>}</div><div><br></div><div>step3.</div><div>build AVCodec.</div><div>AVCodec ff_libx264_encoder = {</div><div>    .name             = "libx264",</div><div>    .long_name        = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),</div><div>    .type             = AVMEDIA_TYPE_VIDEO,</div><div>    .id               = AV_CODEC_ID_H264,</div><div>    .priv_data_size   = sizeof(X264Context),</div><div>    .init             = X264_init,</div><div>    //</div><div>    .valid_context    = X264_config,</div><div>    //</div><div>    .encode2          = X264_frame,</div><div>    .close            = X264_close,</div><div>    .capabilities     = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,</div><div>    .priv_class       = &x264_class,</div><div>    .defaults         = x264_defaults,</div><div>    .init_static_data = X264_init_static,</div><div>    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |</div><div>                        FF_CODEC_CAP_INIT_CLEANUP,</div><div>    .wrapper_name     = "libx264",</div><div>};</div><div><br></div><div>step4.</div><div>FFmpeg interface layer.</div><div>int attribute_align_arg avcodec_valid_codec_Context(AVCodecContext* avctx){</div><div>    int ret = 0;</div><div>    ret = avctx->codec->valid_context(avctx);</div><div>    if(ret){</div><div>        av_log(avctx, AV_LOG_ERROR, "valid context error, ERROR CODE:%d\n", </div><div><span style="white-space:pre-wrap">             </span>ret);</div><div>    }</div><div>    return ret;</div><div>}</div><div><br></div><div>step5.</div><div>Whenever the application layer needs to modify the bit rate, then App should modify the AVCodecContext-> bit_rate, and then call the above interface to make the Context take effect in the encoder.</div><div><br></div><div>Because of the limited understanding of ffmpeg. So if there is a better way, also hope to give instruction.</div></div>_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
<a href="https://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">https://ffmpeg.org/mailman/listinfo/libav-user</a><br>
<br>
To unsubscribe, visit link above, or email<br>
<a href="mailto:libav-user-request@ffmpeg.org" target="_blank">libav-user-request@ffmpeg.org</a> with subject "unsubscribe".</blockquote></div><br clear="all"><div><br></div><div>Well, according to the libx264.c wrapper and x264's encoder.c code, one can simply change the value</div><div>of bitrate, bufsize, maxrate or crf in AVCodecContext, and reconfig_encoder will invoke x264's internal</div><div>API that will reconfigure the encoder with one or more of the previously mentioned values. That should</div><div>be it. The change will be triggered on the next sent frame. Checkout the source of libx264.c line 186.</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><br>Regards<br>Strahinja Radman</div></div></div></body></html>