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

时间:2018-02-28 22:42:03

标签: c++ frame-rate video-encoding libx264 libx265

我尝试使用libx265(先前尝试过的libx264)对网络摄像头进行编码... ...由于进入矩阵的光量不同,网络摄像头无法使用稳定的FPS进行拍摄结果,不同的延迟。因此,我计算传入帧的fps和dts,并为x265_image对象的相应参数设置这些值,并使用1000和fpsNum初始化编码器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;
}

全球PTS在x265_encoder_encode电话后正在增加: pts+= pic_in->dts;并设置为编码器时来自framestack队列的新图像。

x265 / x264编解码器能否以变量fps进行编码?如果是,如何配置?

1 个答案:

答案 0 :(得分:1)

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