Skip to content

Commit 2138ccc

Browse files
committed
Support enabling VAAPI at runtime
1 parent e3d5ef8 commit 2138ccc

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed

include/freerdp/codec/h264.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ extern "C"
4646
H264_CONTEXT_OPTION_RATECONTROL,
4747
H264_CONTEXT_OPTION_BITRATE,
4848
H264_CONTEXT_OPTION_FRAMERATE,
49-
H264_CONTEXT_OPTION_QP
49+
H264_CONTEXT_OPTION_QP,
50+
H264_CONTEXT_OPTION_HW_ACCELERATED
5051
} H264_CONTEXT_OPTION;
5152

5253
FREERDP_API void free_h264_metablock(RDPGFX_H264_METABLOCK* meta);

libfreerdp/codec/h264.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,9 @@ BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UIN
749749
case H264_CONTEXT_OPTION_QP:
750750
h264->QP = value;
751751
return TRUE;
752+
case H264_CONTEXT_OPTION_HW_ACCELERATED:
753+
h264->HardwareAccelerated = value;
754+
return TRUE;
752755
default:
753756
WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]",
754757
option);
@@ -769,6 +772,8 @@ UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option)
769772
return h264->RateControlMode;
770773
case H264_CONTEXT_OPTION_QP:
771774
return h264->QP;
775+
case H264_CONTEXT_OPTION_HW_ACCELERATED:
776+
return h264->HardwareAccelerated;
772777
default:
773778
WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]",
774779
option);

libfreerdp/codec/h264.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ extern "C"
6060
UINT32 FrameRate;
6161
UINT32 QP;
6262
UINT32 NumberOfThreads;
63+
UINT32 HardwareAccelerated; /* 0 - non accelerated, != 0 - accelerated */
6364

6465
UINT32 iStride[3];
6566
BYTE* pOldYUVData[3];

libfreerdp/codec/h264_ffmpeg.c

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ static int libavcodec_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT3
225225
packet->size = (int)MIN(SrcSize, INT32_MAX);
226226

227227
WINPR_ASSERT(sys->codecDecoderContext);
228+
228229
/* avcodec_decode_video2 is deprecated with libavcodec 57.48.101 */
229230
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
230231
status = avcodec_send_packet(sys->codecDecoderContext, packet);
@@ -264,7 +265,6 @@ static int libavcodec_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT3
264265
}
265266

266267
#ifdef WITH_VAAPI
267-
268268
if (sys->hwctx)
269269
{
270270
if (sys->hwVideoFrame->format == sys->hw_pix_fmt)
@@ -287,7 +287,6 @@ static int libavcodec_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT3
287287
av_err2str(status));
288288
goto fail;
289289
}
290-
291290
#endif
292291
#if 0
293292
WLog_Print(h264->log, WLOG_INFO,
@@ -482,7 +481,6 @@ static void libavcodec_uninit(H264_CONTEXT* h264)
482481
}
483482

484483
#ifdef WITH_VAAPI
485-
486484
if (sys->hwVideoFrame)
487485
{
488486
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
@@ -496,7 +494,6 @@ static void libavcodec_uninit(H264_CONTEXT* h264)
496494
av_buffer_unref(&sys->hwctx);
497495

498496
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 80, 100)
499-
500497
if (sys->hw_frames_ctx)
501498
av_buffer_unref(&sys->hw_frames_ctx);
502499

@@ -621,29 +618,32 @@ static BOOL libavcodec_init(H264_CONTEXT* h264)
621618
#endif
622619

623620
#ifdef WITH_VAAPI
624-
625-
if (!sys->hwctx)
621+
if (h264->HardwareAccelerated != 0)
626622
{
627-
int ret =
628-
av_hwdevice_ctx_create(&sys->hwctx, AV_HWDEVICE_TYPE_VAAPI, VAAPI_DEVICE, NULL, 0);
629-
630-
if (ret < 0)
623+
if (!sys->hwctx)
631624
{
632-
WLog_Print(h264->log, WLOG_ERROR,
633-
"Could not initialize hardware decoder, falling back to software: %s",
634-
av_err2str(ret));
635-
sys->hwctx = NULL;
636-
goto fail_hwdevice_create;
625+
int ret =
626+
av_hwdevice_ctx_create(&sys->hwctx, AV_HWDEVICE_TYPE_VAAPI, VAAPI_DEVICE, NULL, 0);
627+
628+
if (ret < 0)
629+
{
630+
WLog_Print(h264->log, WLOG_ERROR,
631+
"Could not initialize hardware decoder, falling back to software: %s",
632+
av_err2str(ret));
633+
sys->hwctx = NULL;
634+
goto fail_hwdevice_create;
635+
}
637636
}
638-
}
639-
WLog_Print(h264->log, WLOG_INFO, "Using VAAPI for accelerated H264 decoding");
637+
WLog_Print(h264->log, WLOG_INFO, "Using VAAPI for accelerated H264 decoding");
640638

641-
sys->codecDecoderContext->get_format = libavcodec_get_format;
642-
sys->hw_pix_fmt = AV_PIX_FMT_VAAPI;
639+
sys->codecDecoderContext->get_format = libavcodec_get_format;
640+
sys->hw_pix_fmt = AV_PIX_FMT_VAAPI;
643641
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 80, 100)
644-
sys->codecDecoderContext->hw_device_ctx = av_buffer_ref(sys->hwctx);
642+
sys->codecDecoderContext->hw_device_ctx = av_buffer_ref(sys->hwctx);
645643
#endif
646-
sys->codecDecoderContext->opaque = (void*)h264;
644+
sys->codecDecoderContext->opaque = (void*)h264;
645+
}
646+
647647
fail_hwdevice_create:
648648
#endif
649649

@@ -665,7 +665,10 @@ static BOOL libavcodec_init(H264_CONTEXT* h264)
665665
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
666666
sys->videoFrame = av_frame_alloc();
667667
#ifdef WITH_VAAPI
668-
sys->hwVideoFrame = av_frame_alloc();
668+
if (h264->HardwareAccelerated != 0)
669+
{
670+
sys->hwVideoFrame = av_frame_alloc();
671+
}
669672
#endif
670673
#else
671674
sys->videoFrame = avcodec_alloc_frame();
@@ -678,11 +681,13 @@ static BOOL libavcodec_init(H264_CONTEXT* h264)
678681
}
679682

680683
#ifdef WITH_VAAPI
681-
682-
if (!sys->hwVideoFrame)
684+
if (h264->HardwareAccelerated != 0)
683685
{
684-
WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate libav hw frame");
685-
goto EXCEPTION;
686+
if (!sys->hwVideoFrame)
687+
{
688+
WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate libav hw frame");
689+
goto EXCEPTION;
690+
}
686691
}
687692

688693
#endif

server/shadow/shadow_encoder.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ static int shadow_encoder_init_h264(rdpShadowEncoder* encoder)
256256
if (!h264_context_set_option(encoder->h264, H264_CONTEXT_OPTION_QP, encoder->server->h264QP))
257257
goto fail;
258258

259+
UINT32 hardware_accelerated = freerdp_settings_get_bool(encoder->server->settings, FreeRDP_SoftwareGdi) ? 0 : 1;
260+
261+
if (!h264_context_set_option(encoder->h264, H264_CONTEXT_OPTION_HW_ACCELERATED, hardware_accelerated))
262+
goto fail;
263+
259264
encoder->codecs |= FREERDP_CODEC_AVC420 | FREERDP_CODEC_AVC444;
260265
return 1;
261266
fail:

0 commit comments

Comments
 (0)