为什么编解码器x264 / x265会忽略输入帧的pts和dts?

问题描述 投票:0回答:1

我正在尝试使用libx265(先前尝试过的libx264)对来自网络摄像头的图像进行编码... 由于进入矩阵的光量不同,网络摄像头无法使用稳定的FPS进行拍摄,因此会出现不同的延迟。因此,我计算输入帧的fps和dts,并为x265_image对象的相应参数设置这些值,并使用1000和fpsNum为1(对于毫秒时基)初始化编码器fpsDenom。 问题是编码器忽略输入图像的pts和dts并以1000 fps编码!与timebase相同的技巧使用libvpx产生流畅的记录。为什么它不适用于x264 / x265编解码器?

这是参数初始化:

...
    error = (x265_param_default_preset(param, "fast", "zerolatency") != 0);
    if(!error){
        param->sourceWidth = width;
        param->sourceHeight = height;
        param->frameNumThreads = 1;
        param->fpsNum = 1000;
        param->fpsDenom =  1;
        // Intra refres:
        param->keyframeMax = 15;
        param->intraRefine = 1;
        // Rate control:
        param->rc.rateControlMode = X265_RC_CQP;
        param->rc.rfConstant = 12;
        param->rc.rfConstantMax = 48;
        // For streaming:
        param->bRepeatHeaders = 1;
        param->bAnnexB = 1;
        encoder = x265_encoder_open(param);
        ...
    }
...

这是帧添加功能:

bool hevc::Push(unsigned char *data){
    if(!error){
        std::lock_guard<std::mutex> lock(m_framestack);
        if( timer > 0){
            framestack.back()->dts = clock() - timer;
            timer+= framestack.back()->dts;
        }
        else{timer = clock();}
        x265_picture *picture = x265_picture_alloc();
        if( picture){
            x265_picture_init(param, picture);
            picture->height = param->sourceHeight;
            picture->stride[0] = param->sourceWidth;
            picture->stride[1] = picture->stride[2] = picture->stride[0] / 2;
            picture->planes[0] = new char[  luma_size];
            picture->planes[1] = new char[chroma_size];
            picture->planes[2] = new char[chroma_size];

            colorspaces::BGRtoI420(param->sourceWidth, param->sourceHeight, data, (byte*)picture->planes[0], (byte*)picture->planes[1], (byte*)picture->planes[2]);
            picture->pts = picture->dts = 0;
            framestack.emplace_back(picture);
        }
        else{error = true;}
    }
    return !error;
}

x265_encoder_encode调用之后,全局PTS正在增加:pts+= pic_in->dts;并且在编码器方面设置为来自framestack队列的新图像。

x265 / x264编解码器能否以可变fps编码?如果是的话如何配置?

c++ frame-rate video-encoding libx264 libx265
1个回答
1
投票

我不知道x265但是在x264中编码可变帧率(VFR)视频你应该启用x264_param_t.b_vfr_input选项,这个选项被你的零容量调整禁用(VFR编码需要1帧延迟)。此外,至少在x264时基中,i_timebase_num / i_timebase_den和i_fps_num / i_fps_den应该是平均fps(如果你不知道fps,则保持默认值为25/1),否则你将破坏ratecontrol。

© www.soinside.com 2019 - 2024. All rights reserved.