diff --git a/AVQt/filter/EncoderVAAPI.cpp b/AVQt/filter/EncoderVAAPI.cpp index 248f91fe24f36602d90825f29e9fe5ea3b6d2f09..28acfa57e9f39fbeeaee35dedb03edb69bc45e8c 100644 --- a/AVQt/filter/EncoderVAAPI.cpp +++ b/AVQt/filter/EncoderVAAPI.cpp @@ -235,6 +235,7 @@ namespace AVQt { while (d->m_running.load()) { if (!d->m_paused.load() && d->m_inputQueue.size() > 5) { bool shouldBe = true; + // Encoder init is only possible with frame parameters if (d->m_firstFrame.compare_exchange_strong(shouldBe, false)) { auto frame = d->m_inputQueue.first().first; @@ -244,27 +245,6 @@ namespace AVQt { } d->m_pCodecCtx->width = frame->width; d->m_pCodecCtx->height = frame->height; -// if (frame->hw_frames_ctx) { -// qDebug("Creating derived HW context..."); -// d->m_pDeviceCtx = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI); -// ret = av_hwdevice_ctx_create(&d->m_pDeviceCtx, AV_HWDEVICE_TYPE_VAAPI, "/dev/dri/renderD128", nullptr, 0); -// if (ret < 0) { -// qFatal("%i: Unable to create derived AVHWDeviceContext: %s", ret, av_make_error_string(strBuf, strBufSize, ret)); -// } else if (!d->m_pDeviceCtx) { -// qFatal("Unable to create derived AVHWDeviceContext"); -// } -// -// d->m_pCodecCtx->sw_pix_fmt = reinterpret_cast<AVHWFramesContext*>(frame->hw_frames_ctx->data)->sw_format; -// ret = av_hwframe_ctx_create_derived(&d->m_pFramesCtx, d->m_pCodecCtx->sw_pix_fmt, d->m_pDeviceCtx, frame->hw_frames_ctx, 0); -// if (ret < 0) { -// qFatal("%i: Unable to create derived AVHWFramesContext: %s", ret, av_make_error_string(strBuf, strBufSize, ret)); -// } else if (!d->m_pDeviceCtx) { -// qFatal("Unable to create derived AVHWFramesContext"); -// } -// d->m_pCodecCtx->pix_fmt = reinterpret_cast<AVHWFramesContext*>(d->m_pFramesCtx->data)->format; -// d->m_pCodecCtx->hw_device_ctx = av_buffer_ref(d->m_pDeviceCtx); -// d->m_pCodecCtx->hw_frames_ctx = av_buffer_ref(d->m_pFramesCtx); -// } else { qDebug("Creating new HW context..."); ret = av_hwdevice_ctx_create(&d->m_pDeviceCtx, AV_HWDEVICE_TYPE_VAAPI, "/dev/dri/renderD128", nullptr, 0); if (ret < 0) { @@ -296,24 +276,24 @@ namespace AVQt { d->m_pCodecCtx->hw_device_ctx = av_buffer_ref(d->m_pDeviceCtx); d->m_pCodecCtx->hw_frames_ctx = av_buffer_ref(d->m_pFramesCtx); - d->m_pCodecCtx->bit_rate = 50000000; - d->m_pCodecCtx->rc_min_rate = 30000000; - d->m_pCodecCtx->rc_max_rate = 50000000; - d->m_pCodecCtx->rc_buffer_size = 100000000; + d->m_pCodecCtx->bit_rate = 5000000; + d->m_pCodecCtx->rc_min_rate = 4500000; + d->m_pCodecCtx->rc_max_rate = 6000000; + d->m_pCodecCtx->rc_buffer_size = 10000000; d->m_pCodecCtx->gop_size = 20; d->m_pCodecCtx->color_primaries = AVCOL_PRI_BT2020; d->m_pCodecCtx->color_trc = AVCOL_TRC_SMPTE2084; d->m_pCodecCtx->colorspace = AVCOL_SPC_BT2020_NCL; -// } - -// d->m_pCodecCtx->pix_fmt = AV_PIX_FMT_VAAPI; - d->m_pCodecCtx->time_base = av_make_q(1, 1000000); // Timestamps from frame sources are always microseconds + // Timestamps from frame sources are always microseconds, trying to use this timebase for the encoder too + d->m_pCodecCtx->time_base = av_make_q(1, 1000000); ret = avcodec_open2(d->m_pCodecCtx, d->m_pCodec, nullptr); if (ret < 0) { qFatal("%i: Unable to open VAAPI encoder: %s", ret, av_make_error_string(strBuf, strBufSize, ret)); } qDebug("[AVQt::EncoderVAAPI] Encoder timebase: %d/%d", d->m_pCodecCtx->time_base.num, d->m_pCodecCtx->time_base.den); + + // Preallocating frame on gpu d->m_pHWFrame = av_frame_alloc(); ret = av_hwframe_get_buffer(d->m_pFramesCtx, d->m_pHWFrame, 0); if (ret != 0) {