[Libav-user] 回复: videorate realtime-adjusting

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[Libav-user] 回复: videorate realtime-adjusting

田浩杨
Very grateful to that.


------------------ 原始邮件 ------------------
发件人: "Strahinja Radman"<[hidden email]>;
发送时间: 2019年12月2日(星期一) 下午5:03
收件人: "This list is about using libavcodec, libavformat, libavutil,libavdevice and libavfilter."<[hidden email]>;
主题: Re: [Libav-user] videorate realtime-adjusting



On Mon, Dec 2, 2019 at 9:29 AM 田浩杨 <[hidden email]> wrote:
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) :

step1.
Modified the definition of structural AVCoder to add a function pointer.
int (*valid_context)(AVCodecContext*);

step2.
Added function definition to enable the codec rate of x264.
static int X264_config(AVCodecContext* avctx){
    X264Context* x4 = avctx->priv_data;
    if(avctx->bit_rate){
        x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
    }
    avctx->bit_rate = x4->params.rc.i_bitrate*1000;
    return 0;
}

step3.
build AVCodec.
AVCodec ff_libx264_encoder = {
    .name             = "libx264",
    .long_name        = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
    .type             = AVMEDIA_TYPE_VIDEO,
    .id               = AV_CODEC_ID_H264,
    .priv_data_size   = sizeof(X264Context),
    .init             = X264_init,
    //
    .valid_context    = X264_config,
    //
    .encode2          = X264_frame,
    .close            = X264_close,
    .capabilities     = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
    .priv_class       = &x264_class,
    .defaults         = x264_defaults,
    .init_static_data = X264_init_static,
    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
                        FF_CODEC_CAP_INIT_CLEANUP,
    .wrapper_name     = "libx264",
};

step4.
FFmpeg interface layer.
int attribute_align_arg avcodec_valid_codec_Context(AVCodecContext* avctx){
    int ret = 0;
    ret = avctx->codec->valid_context(avctx);
    if(ret){
        av_log(avctx, AV_LOG_ERROR, "valid context error, ERROR CODE:%d\n", 
ret);
    }
    return ret;
}

step5.
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.

Because of the limited understanding of ffmpeg. So if there is a better way, also hope to give instruction.
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


Well, according to the libx264.c wrapper and x264's encoder.c code, one can simply change the value
of bitrate, bufsize, maxrate or crf in AVCodecContext, and reconfig_encoder will invoke x264's internal
API that will reconfigure the encoder with one or more of the previously mentioned values. That should
be it. The change will be triggered on the next sent frame. Checkout the source of libx264.c line 186.

--

Regards
Strahinja Radman

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".