21#include <freerdp/config.h>
23#include <winpr/wlog.h>
24#include <freerdp/log.h>
25#include <freerdp/codec/h264.h>
26#include <libavcodec/avcodec.h>
27#include <libavutil/opt.h>
31#ifdef WITH_VIDEOTOOLBOX
32#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 9, 0)
33#include <libavutil/hwcontext.h>
35#pragma warning You have asked for VideoToolbox decoding, \
36 but your version of libavutil is too old !Disabling.
37#undef WITH_VIDEOTOOLBOX
42#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 9, 0)
43#include <libavutil/hwcontext.h>
45#pragma warning You have asked for VA - API decoding, \
46 but your version of libavutil is too old !Disabling.
52#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 59, 100)
53#define AV_CODEC_ID_H264 CODEC_ID_H264
56#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 34, 2)
57#define AV_CODEC_FLAG_LOOP_FILTER CODEC_FLAG_LOOP_FILTER
58#define AV_CODEC_CAP_TRUNCATED CODEC_CAP_TRUNCATED
59#define AV_CODEC_FLAG_TRUNCATED CODEC_FLAG_TRUNCATED
62#if LIBAVUTIL_VERSION_MAJOR < 52
63#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
68#if !defined(av_err2str)
69static inline char* error_string(
char* errbuf,
size_t errbuf_size,
int errnum)
71 av_strerror(errnum, errbuf, errbuf_size);
75#define av_err2str(errnum) error_string((char[64])WINPR_C_ARRAY_INIT, 64, errnum)
78#if defined(WITH_VAAPI) || defined(WITH_VAAPI_H264_ENCODING)
79static const char* get_vaapi_device(
void)
81 static char device[MAX_PATH] = WINPR_C_ARRAY_INIT;
82 static bool initialized =
false;
87 const char* env = getenv(
"FREERDP_VAAPI_DEVICE");
89 (void)_snprintf(device,
sizeof(device),
"%s", env);
91 (
void)_snprintf(device,
sizeof(device),
"/dev/dri/renderD128");
99 const AVCodec* codecDecoder;
100 AVCodecContext* codecDecoderContext;
101 const AVCodec* codecEncoder;
102 AVCodecContext* codecEncoderContext;
103 AVCodecParserContext* codecParser;
105#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
106 AVPacket bufferpacket;
109#if defined(WITH_VAAPI) || defined(WITH_VAAPI_H264_ENCODING) || defined(WITH_VIDEOTOOLBOX)
111 AVFrame* hwVideoFrame;
112 enum AVPixelFormat hw_pix_fmt;
113#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 80, 100) || defined(WITH_VIDEOTOOLBOX)
114 AVBufferRef* hw_frames_ctx;
118} H264_CONTEXT_LIBAVCODEC;
120static void libavcodec_destroy_encoder_context(H264_CONTEXT* WINPR_RESTRICT h264)
122 H264_CONTEXT_LIBAVCODEC* sys =
nullptr;
124 if (!h264 || !h264->subsystem)
127 sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
129 if (sys->codecEncoderContext)
131#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 69, 100)
132 avcodec_free_context(&sys->codecEncoderContext);
134 avcodec_close(sys->codecEncoderContext);
135 av_free(sys->codecEncoderContext);
139 sys->codecEncoderContext =
nullptr;
142#ifdef WITH_VAAPI_H264_ENCODING
143static int set_hw_frames_ctx(H264_CONTEXT* WINPR_RESTRICT h264)
145 H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
146 AVBufferRef* hw_frames_ref =
nullptr;
147 AVHWFramesContext* frames_ctx =
nullptr;
150 if (!(hw_frames_ref = av_hwframe_ctx_alloc(sys->hwctx)))
152 WLog_Print(h264->log, WLOG_ERROR,
"Failed to create VAAPI frame context");
155 frames_ctx = (AVHWFramesContext*)(hw_frames_ref->data);
156 frames_ctx->format = AV_PIX_FMT_VAAPI;
157 frames_ctx->sw_format = AV_PIX_FMT_NV12;
158 frames_ctx->width = sys->codecEncoderContext->width;
159 frames_ctx->height = sys->codecEncoderContext->height;
160 frames_ctx->initial_pool_size = 20;
161 if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0)
163 WLog_Print(h264->log, WLOG_ERROR,
164 "Failed to initialize VAAPI frame context."
167 av_buffer_unref(&hw_frames_ref);
170 sys->codecEncoderContext->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
171 if (!sys->codecEncoderContext->hw_frames_ctx)
172 err = AVERROR(ENOMEM);
174 av_buffer_unref(&hw_frames_ref);
179static BOOL libavcodec_create_encoder_context(H264_CONTEXT* WINPR_RESTRICT h264)
181 BOOL recreate = FALSE;
182 H264_CONTEXT_LIBAVCODEC* sys =
nullptr;
184 if (!h264 || !h264->subsystem)
187 if ((h264->width > INT_MAX) || (h264->height > INT_MAX))
190 sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
191 if (!sys || !sys->codecEncoder)
194 recreate = !sys->codecEncoderContext;
196 if (sys->codecEncoderContext)
198 if ((sys->codecEncoderContext->width != (
int)h264->width) ||
199 (sys->codecEncoderContext->height != (
int)h264->height))
206 libavcodec_destroy_encoder_context(h264);
208 sys->codecEncoderContext = avcodec_alloc_context3(sys->codecEncoder);
210 if (!sys->codecEncoderContext)
213 switch (h264->RateControlMode)
215 case H264_RATECONTROL_VBR:
216 sys->codecEncoderContext->bit_rate = h264->BitRate;
219 case H264_RATECONTROL_CQP:
220 if (av_opt_set_int(sys->codecEncoderContext,
"qp", h264->QP, AV_OPT_SEARCH_CHILDREN) <
223 WLog_Print(h264->log, WLOG_ERROR,
"av_opt_set_int failed");
231 sys->codecEncoderContext->width = (int)MIN(INT32_MAX, h264->width);
232 sys->codecEncoderContext->height = (int)MIN(INT32_MAX, h264->height);
233 sys->codecEncoderContext->delay = 0;
234#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 13, 100)
235 sys->codecEncoderContext->framerate =
236 (AVRational){ WINPR_ASSERTING_INT_CAST(
int, h264->FrameRate), 1 };
238 sys->codecEncoderContext->time_base =
239 (AVRational){ 1, WINPR_ASSERTING_INT_CAST(
int, h264->FrameRate) };
240 av_opt_set(sys->codecEncoderContext,
"tune",
"zerolatency", AV_OPT_SEARCH_CHILDREN);
242 sys->codecEncoderContext->flags |= AV_CODEC_FLAG_LOOP_FILTER;
244#ifdef WITH_VAAPI_H264_ENCODING
247 av_opt_set(sys->codecEncoderContext,
"preset",
"veryslow", AV_OPT_SEARCH_CHILDREN);
249 sys->codecEncoderContext->pix_fmt = AV_PIX_FMT_VAAPI;
251 if (set_hw_frames_ctx(h264) < 0)
257 av_opt_set(sys->codecEncoderContext,
"preset",
"medium", AV_OPT_SEARCH_CHILDREN);
258 sys->codecEncoderContext->pix_fmt = AV_PIX_FMT_YUV420P;
261 if (avcodec_open2(sys->codecEncoderContext, sys->codecEncoder,
nullptr) < 0)
266 libavcodec_destroy_encoder_context(h264);
270static int libavcodec_decompress(H264_CONTEXT* WINPR_RESTRICT h264,
271 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize)
281 AVPacket* packet =
nullptr;
284 WINPR_ASSERT(pSrcData || (SrcSize == 0));
286 H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
287 BYTE** pYUVData = h264->pYUVData;
288 UINT32* iStride = h264->iStride;
292#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
293 packet = &sys->bufferpacket;
294 WINPR_ASSERT(packet);
295 av_init_packet(packet);
297 packet = av_packet_alloc();
301 WLog_Print(h264->log, WLOG_ERROR,
"Failed to allocate AVPacket");
306 packet->data = cnv.pv;
307 packet->size = (int)MIN(SrcSize, INT32_MAX);
309 WINPR_ASSERT(sys->codecDecoderContext);
311#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
312 status = avcodec_send_packet(sys->codecDecoderContext, packet);
316 WLog_Print(h264->log, WLOG_ERROR,
"Failed to decode video frame (status=%d)", status);
320 sys->videoFrame->format = AV_PIX_FMT_YUV420P;
322#if defined(WITH_VAAPI) || defined(WITH_VIDEOTOOLBOX)
323 status = avcodec_receive_frame(sys->codecDecoderContext,
324 sys->hwctx ? sys->hwVideoFrame : sys->videoFrame);
326 status = avcodec_receive_frame(sys->codecDecoderContext, sys->videoFrame);
328 if (status == AVERROR(EAGAIN))
337#if defined(WITH_VAAPI) || defined(WITH_VIDEOTOOLBOX)
339 avcodec_decode_video2(sys->codecDecoderContext,
340 sys->hwctx ? sys->hwVideoFrame : sys->videoFrame, &gotFrame, packet);
342 status = avcodec_decode_video2(sys->codecDecoderContext, sys->videoFrame, &gotFrame, packet);
347 WLog_Print(h264->log, WLOG_ERROR,
"Failed to decode video frame (status=%d)", status);
351#if defined(WITH_VAAPI) || defined(WITH_VIDEOTOOLBOX)
355 if (sys->hwVideoFrame->format == sys->hw_pix_fmt)
357 sys->videoFrame->width = sys->hwVideoFrame->width;
358 sys->videoFrame->height = sys->hwVideoFrame->height;
359 status = av_hwframe_transfer_data(sys->videoFrame, sys->hwVideoFrame, 0);
363 status = av_frame_copy(sys->videoFrame, sys->hwVideoFrame);
365 gotFrame = (status == 0);
369 WLog_Print(h264->log, WLOG_ERROR,
"Failed to transfer video frame (status=%d) (%s)",
370 status, av_err2str(status));
379 WINPR_ASSERT(sys->videoFrame);
381 pYUVData[0] = sys->videoFrame->data[0];
382 pYUVData[1] = sys->videoFrame->data[1];
383 pYUVData[2] = sys->videoFrame->data[2];
384 iStride[0] = (UINT32)MAX(0, sys->videoFrame->linesize[0]);
385 iStride[1] = (UINT32)MAX(0, sys->videoFrame->linesize[1]);
386 iStride[2] = (UINT32)MAX(0, sys->videoFrame->linesize[2]);
388 h264->YUVWidth = WINPR_ASSERTING_INT_CAST(UINT32, sys->videoFrame->width);
389 h264->YUVHeight = WINPR_ASSERTING_INT_CAST(UINT32, sys->videoFrame->height);
396#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
397 av_packet_unref(packet);
399 av_packet_free(&packet);
405static int libavcodec_compress(H264_CONTEXT* WINPR_RESTRICT h264,
406 const BYTE** WINPR_RESTRICT pSrcYuv,
407 const UINT32* WINPR_RESTRICT pStride,
408 BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize)
421 H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
424 if (!libavcodec_create_encoder_context(h264))
427#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
428 sys->packet = &sys->bufferpacket;
429 av_packet_unref(sys->packet);
430 av_init_packet(sys->packet);
432 av_packet_free(&sys->packet);
433 sys->packet = av_packet_alloc();
437 WLog_Print(h264->log, WLOG_ERROR,
"Failed to allocate AVPacket");
441 WINPR_ASSERT(sys->packet);
442 sys->packet->data =
nullptr;
443 sys->packet->size = 0;
445 WINPR_ASSERT(sys->videoFrame);
446 WINPR_ASSERT(sys->codecEncoderContext);
447 sys->videoFrame->format = AV_PIX_FMT_YUV420P;
448 sys->videoFrame->width = sys->codecEncoderContext->width;
449 sys->videoFrame->height = sys->codecEncoderContext->height;
450#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(52, 48, 100)
451 sys->videoFrame->colorspace = AVCOL_SPC_BT709;
453#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(52, 92, 100)
454 sys->videoFrame->chroma_location = AVCHROMA_LOC_LEFT;
456 cnv.cpv = pSrcYuv[0];
457 sys->videoFrame->data[0] = cnv.pv;
459 cnv.cpv = pSrcYuv[1];
460 sys->videoFrame->data[1] = cnv.pv;
462 cnv.cpv = pSrcYuv[2];
463 sys->videoFrame->data[2] = cnv.pv;
465 sys->videoFrame->linesize[0] = (int)pStride[0];
466 sys->videoFrame->linesize[1] = (int)pStride[1];
467 sys->videoFrame->linesize[2] = (int)pStride[2];
468 sys->videoFrame->pts++;
470#ifdef WITH_VAAPI_H264_ENCODING
473 av_frame_unref(sys->hwVideoFrame);
474 if ((status = av_hwframe_get_buffer(sys->codecEncoderContext->hw_frames_ctx,
475 sys->hwVideoFrame, 0)) < 0 ||
476 !sys->hwVideoFrame->hw_frames_ctx)
478 WLog_Print(h264->log, WLOG_ERROR,
"av_hwframe_get_buffer failed (%s [%d])",
479 av_err2str(status), status);
482 sys->videoFrame->format = AV_PIX_FMT_NV12;
483 if ((status = av_hwframe_transfer_data(sys->hwVideoFrame, sys->videoFrame, 0)) < 0)
485 WLog_Print(h264->log, WLOG_ERROR,
"av_hwframe_transfer_data failed (%s [%d])",
486 av_err2str(status), status);
493#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
494#ifdef WITH_VAAPI_H264_ENCODING
495 status = avcodec_send_frame(sys->codecEncoderContext,
496 sys->hwctx ? sys->hwVideoFrame : sys->videoFrame);
498 status = avcodec_send_frame(sys->codecEncoderContext, sys->videoFrame);
503 WLog_Print(h264->log, WLOG_ERROR,
"Failed to encode video frame (%s [%d])",
504 av_err2str(status), status);
508 status = avcodec_receive_packet(sys->codecEncoderContext, sys->packet);
512 WLog_Print(h264->log, WLOG_ERROR,
"Failed to encode video frame (%s [%d])",
513 av_err2str(status), status);
517 gotFrame = (status == 0);
518#elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 59, 100)
522 status = avcodec_encode_video2(sys->codecEncoderContext, sys->packet, sys->videoFrame,
524 }
while ((status >= 0) && (gotFrame == 0));
528 avpicture_get_size(sys->codecDecoderContext->pix_fmt, sys->codecDecoderContext->width,
529 sys->codecDecoderContext->height);
530 sys->packet->data = av_malloc(sys->packet->size);
532 if (!sys->packet->data)
536 status = avcodec_encode_video(sys->codecDecoderContext, sys->packet->data,
537 sys->packet->size, sys->videoFrame);
544 WLog_Print(h264->log, WLOG_ERROR,
"Failed to encode video frame (%s [%d])",
545 av_err2str(status), status);
549 WINPR_ASSERT(sys->packet);
550 *ppDstData = sys->packet->data;
551 *pDstSize = (UINT32)MAX(0, sys->packet->size);
555 WLog_Print(h264->log, WLOG_ERROR,
"Did not get frame! (%s [%d])", av_err2str(status),
565static void libavcodec_uninit(H264_CONTEXT* h264)
569 H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
576#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
577 av_packet_unref(sys->packet);
579 av_packet_free(&sys->packet);
585#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
586 av_frame_free(&sys->videoFrame);
588 av_free(sys->videoFrame);
592#if defined(WITH_VAAPI) || defined(WITH_VAAPI_H264_ENCODING) || defined(WITH_VIDEOTOOLBOX)
593 if (sys->hwVideoFrame)
595#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
596 av_frame_free(&sys->hwVideoFrame);
598 av_free(sys->hwVideoFrame);
603 av_buffer_unref(&sys->hwctx);
605#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 80, 100) || defined(WITH_VIDEOTOOLBOX)
607 if (sys->hw_frames_ctx)
608 av_buffer_unref(&sys->hw_frames_ctx);
614 if (sys->codecParser)
615 av_parser_close(sys->codecParser);
617 if (sys->codecDecoderContext)
619#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 69, 100)
620 avcodec_free_context(&sys->codecDecoderContext);
622 avcodec_close(sys->codecDecoderContext);
623 av_free(sys->codecDecoderContext);
627 libavcodec_destroy_encoder_context(h264);
629 h264->pSystemData =
nullptr;
632#if defined(WITH_VAAPI) || defined(WITH_VIDEOTOOLBOX)
633static enum AVPixelFormat libavcodec_get_format(
struct AVCodecContext* ctx,
634 const enum AVPixelFormat* fmts)
638 H264_CONTEXT* h264 = (H264_CONTEXT*)ctx->opaque;
641 H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
644 for (
const enum AVPixelFormat* p = fmts; *p != AV_PIX_FMT_NONE; p++)
646 if (*p == sys->hw_pix_fmt)
648#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 80, 100) || defined(WITH_VIDEOTOOLBOX)
649 if (sys->hw_frames_ctx)
650 av_buffer_unref(&sys->hw_frames_ctx);
652 sys->hw_frames_ctx = av_hwframe_ctx_alloc(sys->hwctx);
654 if (!sys->hw_frames_ctx)
656 return AV_PIX_FMT_NONE;
659 sys->codecDecoderContext->pix_fmt = *p;
660 AVHWFramesContext* frames = (AVHWFramesContext*)sys->hw_frames_ctx->data;
662 frames->height = sys->codecDecoderContext->coded_height;
663 frames->width = sys->codecDecoderContext->coded_width;
664#ifdef WITH_VIDEOTOOLBOX
665 frames->sw_format = AV_PIX_FMT_YUV420P;
668 (sys->codecDecoderContext->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ? AV_PIX_FMT_P010
671 frames->initial_pool_size = 20;
673 if (sys->codecDecoderContext->active_thread_type & FF_THREAD_FRAME)
674 frames->initial_pool_size += sys->codecDecoderContext->thread_count;
676 int err = av_hwframe_ctx_init(sys->hw_frames_ctx);
680 WLog_Print(h264->log, WLOG_ERROR,
"Could not init hwframes context: %s",
682 return AV_PIX_FMT_NONE;
685 sys->codecDecoderContext->hw_frames_ctx = av_buffer_ref(sys->hw_frames_ctx);
691 return AV_PIX_FMT_NONE;
695static BOOL libavcodec_init(H264_CONTEXT* h264)
698 H264_CONTEXT_LIBAVCODEC* sys =
699 (H264_CONTEXT_LIBAVCODEC*)calloc(1,
sizeof(H264_CONTEXT_LIBAVCODEC));
706 h264->pSystemData = (
void*)sys;
708#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
709 avcodec_register_all();
712 if (!h264->Compressor)
714 sys->codecDecoder = avcodec_find_decoder(AV_CODEC_ID_H264);
716 if (!sys->codecDecoder)
718 WLog_Print(h264->log, WLOG_ERROR,
"Failed to find libav H.264 codec");
722 sys->codecDecoderContext = avcodec_alloc_context3(sys->codecDecoder);
724 if (!sys->codecDecoderContext)
726 WLog_Print(h264->log, WLOG_ERROR,
"Failed to allocate libav codec context");
730#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
731 if (sys->codecDecoder->capabilities & AV_CODEC_CAP_TRUNCATED)
733 sys->codecDecoderContext->flags |= AV_CODEC_FLAG_TRUNCATED;
741 int ret = av_hwdevice_ctx_create(&sys->hwctx, AV_HWDEVICE_TYPE_VAAPI,
742 get_vaapi_device(),
nullptr, 0);
746 WLog_Print(h264->log, WLOG_ERROR,
747 "Could not initialize hardware decoder, falling back to software: %s",
749 sys->hwctx =
nullptr;
750 goto fail_hwdevice_create;
753 WLog_Print(h264->log, WLOG_INFO,
"Using VAAPI for accelerated H264 decoding");
755 sys->codecDecoderContext->get_format = libavcodec_get_format;
756 sys->hw_pix_fmt = AV_PIX_FMT_VAAPI;
757#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 80, 100)
758 sys->codecDecoderContext->hw_device_ctx = av_buffer_ref(sys->hwctx);
760 sys->codecDecoderContext->opaque = (
void*)h264;
761 fail_hwdevice_create:
764#ifdef WITH_VIDEOTOOLBOX
768 int ret = av_hwdevice_ctx_create(&sys->hwctx, AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
nullptr,
774 h264->log, WLOG_ERROR,
775 "Could not initialize VideoToolbox decoder, falling back to software: %s",
777 sys->hwctx =
nullptr;
781 WLog_Print(h264->log, WLOG_INFO,
"Using VideoToolbox for accelerated H264 decoding");
783 sys->codecDecoderContext->get_format = libavcodec_get_format;
784 sys->hw_pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX;
785 sys->codecDecoderContext->hw_device_ctx = av_buffer_ref(sys->hwctx);
786 sys->codecDecoderContext->opaque = (
void*)h264;
790 if (avcodec_open2(sys->codecDecoderContext, sys->codecDecoder,
nullptr) < 0)
792 WLog_Print(h264->log, WLOG_ERROR,
"Failed to open libav codec");
796 sys->codecParser = av_parser_init(AV_CODEC_ID_H264);
798 if (!sys->codecParser)
800 WLog_Print(h264->log, WLOG_ERROR,
"Failed to initialize libav parser");
806#ifdef WITH_VAAPI_H264_ENCODING
809 sys->codecEncoder = avcodec_find_encoder_by_name(
"h264_vaapi");
810 if (!sys->codecEncoder)
812 WLog_Print(h264->log, WLOG_ERROR,
"H264 VAAPI encoder not found");
814 else if (av_hwdevice_ctx_create(&sys->hwctx, AV_HWDEVICE_TYPE_VAAPI, get_vaapi_device(),
817 WLog_Print(h264->log, WLOG_ERROR,
"av_hwdevice_ctx_create failed");
818 sys->codecEncoder =
nullptr;
819 sys->hwctx =
nullptr;
823 WLog_Print(h264->log, WLOG_INFO,
"Using VAAPI for accelerated H264 encoding");
827 if (!sys->codecEncoder)
829 sys->codecEncoder = avcodec_find_encoder(AV_CODEC_ID_H264);
830 h264->hwAccel = FALSE;
833 if (!sys->codecEncoder)
835 WLog_Print(h264->log, WLOG_ERROR,
"Failed to initialize H264 encoder");
840#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
841 sys->videoFrame = av_frame_alloc();
842#if defined(WITH_VAAPI) || defined(WITH_VAAPI_H264_ENCODING) || defined(WITH_VIDEOTOOLBOX)
843 sys->hwVideoFrame = av_frame_alloc();
846 sys->videoFrame = avcodec_alloc_frame();
849 if (!sys->videoFrame)
851 WLog_Print(h264->log, WLOG_ERROR,
"Failed to allocate libav frame");
855#if defined(WITH_VAAPI) || defined(WITH_VAAPI_H264_ENCODING) || defined(WITH_VIDEOTOOLBOX)
856 if (!sys->hwVideoFrame)
858 WLog_Print(h264->log, WLOG_ERROR,
"Failed to allocate libav hw frame");
863 sys->videoFrame->pts = 0;
866 libavcodec_uninit(h264);
870const H264_CONTEXT_SUBSYSTEM g_Subsystem_libavcodec = {
"libavcodec", libavcodec_init,
871 libavcodec_uninit, libavcodec_decompress,
872 libavcodec_compress };