4#include "libmythbase/mythconfig.h"
24#include "libavfilter/buffersrc.h"
25#include "libavfilter/buffersink.h"
26#include "libavutil/hwcontext_vaapi.h"
29#define LOC QString("VAAPIInterop: ")
53 [[maybe_unused]]
bool egl = Context->
IsEGL();
54 [[maybe_unused]]
bool opengles = Context->isOpenGLES();
55 [[maybe_unused]]
bool wayland = qgetenv(
"XDG_SESSION_TYPE").contains(
"wayland");
65#if CONFIG_VAAPI_DRM && CONFIG_EGL
73 if (!egl && !wayland && MythVAAPIInteropGLXPixmap::IsSupported(Context))
77 if (!egl && !opengles && !wayland)
81 if (!vaapitypes.empty())
93 for ([[maybe_unused]]
auto type : vaapi->second)
95#if CONFIG_VAAPI_DRM && CONFIG_EGL
102 return new MythVAAPIInteropGLXPixmap(
Player, Context);
129 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Error closing VAAPI display");
150 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to initialise VAAPI display");
157 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Created VAAPI %1.%2 display for %3 (%4)")
165 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Destroying VAAPI deinterlacer");
180 VASurfaceID result = 0;
191 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Mismatched OpenGL contexts!");
196 QSize surfacesize(
Frame->m_width,
Frame->m_height);
200 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Video texture size changed!");
205 auto id =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(
Frame->m_buffer));
212 AVBufferRef *FramesContext,
213 int Width,
int Height,
215 AVFilterGraph *&Graph,
217 AVFilterContext *&Sink)
221 LOG(VB_GENERAL, LOG_ERR,
LOC +
"No hardware frames context");
226 QString deinterlacer =
"bob";
228 deinterlacer =
"motion_adaptive";
230 deinterlacer =
"default";
233 QString filters = QString(
"deinterlace_vaapi=mode=%1:rate=%2:auto=0")
234 .arg(deinterlacer, DoubleRate ?
"field" :
"frame");
235 const AVFilter *buffersrc = avfilter_get_by_name(
"buffer");
236 const AVFilter *buffersink = avfilter_get_by_name(
"buffersink");
237 AVFilterInOut *outputs = avfilter_inout_alloc();
238 AVFilterInOut *inputs = avfilter_inout_alloc();
241 auto cleanup_fn = [&](
int *) {
245 avfilter_graph_free(&Graph);
248 avfilter_inout_free(&inputs);
249 avfilter_inout_free(&outputs);
251 std::unique_ptr<int,
decltype(cleanup_fn)>
cleanup { &ret, cleanup_fn };
253 Graph = avfilter_graph_alloc();
254 if (!outputs || !inputs || !Graph)
256 ret = AVERROR(ENOMEM);
261 Source = avfilter_graph_alloc_filter(Graph, buffersrc,
"in");
264 ret = AVERROR(ENOMEM);
265 LOG(VB_GENERAL, LOG_ERR,
"avfilter_graph_alloc_filter() failed to allocate memory.");
269 AVBufferSrcParameters* params = av_buffersrc_parameters_alloc();
270 if (params ==
nullptr)
272 ret = AVERROR(ENOMEM);
273 LOG(VB_GENERAL, LOG_ERR,
"av_buffersrc_parameters_alloc() failed to allocate memory.");
276 params->format = AV_PIX_FMT_VAAPI;
277 params->time_base = {.num=1, .den=1};
278 params->width = Width;
279 params->height = Height;
280 params->hw_frames_ctx = FramesContext;
281 ret = av_buffersrc_parameters_set(
Source, params);
282 av_freep(
reinterpret_cast<void*
>(¶ms));
285 LOG(VB_GENERAL, LOG_ERR,
LOC +
"av_buffersrc_parameters_set failed");
289 ret = avfilter_init_str(
Source,
nullptr);
292 LOG(VB_GENERAL, LOG_ERR,
LOC +
"avfilter_init_str() failed for buffer source");
297 ret = avfilter_graph_create_filter(&Sink, buffersink,
"out",
298 nullptr,
nullptr, Graph);
301 LOG(VB_GENERAL, LOG_ERR,
LOC +
"avfilter_graph_create_filter failed for buffer sink");
316 outputs->name = av_strdup(
"in");
317 outputs->filter_ctx =
Source;
318 outputs->pad_idx = 0;
319 outputs->next =
nullptr;
327 inputs->name = av_strdup(
"out");
328 inputs->filter_ctx = Sink;
330 inputs->next =
nullptr;
332 ret = avfilter_graph_parse_ptr(Graph, filters.toLocal8Bit().constData(),
333 &inputs, &outputs,
nullptr);
336 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"avfilter_graph_parse_ptr failed for %1")
341 ret = avfilter_graph_config(Graph,
nullptr);
344 LOG(VB_GENERAL, LOG_ERR,
LOC +
345 QString(
"VAAPI deinterlacer config failed - '%1' unsupported?").arg(deinterlacer));
349 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Created deinterlacer '%1'")
357 VASurfaceID result = Current;
365 bool doublerate =
true;
373 deinterlacer = doublepref;
377 deinterlacer = singlepref;
388 auto* frames =
reinterpret_cast<AVBufferRef*
>(
Frame->m_priv[1]);
392 AVBufferRef* hwdeviceref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
396 auto* hwdevicecontext =
reinterpret_cast<AVHWDeviceContext*
>(hwdeviceref->data);
397 hwdevicecontext->free = [](AVHWDeviceContext* ) {
LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"VAAPI VPP device context finished"); };
399 auto *vaapidevicectx =
reinterpret_cast<AVVAAPIDeviceContext*
>(hwdevicecontext->hwctx);
402 if (av_hwdevice_ctx_init(hwdeviceref) < 0)
404 av_buffer_unref(&hwdeviceref);
409 AVBufferRef *newframes = av_hwframe_ctx_alloc(hwdeviceref);
413 av_buffer_unref(&hwdeviceref);
417 auto* dstframes =
reinterpret_cast<AVHWFramesContext*
>(newframes->data);
418 auto* srcframes =
reinterpret_cast<AVHWFramesContext*
>(frames->data);
422 static constexpr int kVppPoolSize = 2;
423 dstframes->sw_format = srcframes->sw_format;
426 dstframes->initial_pool_size = kVppPoolSize;
427 dstframes->format = AV_PIX_FMT_VAAPI;
428 dstframes->free = [](AVHWFramesContext* ) {
LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"VAAPI VPP frames context finished"); };
430 if (av_hwframe_ctx_init(newframes) < 0)
433 av_buffer_unref(&hwdeviceref);
434 av_buffer_unref(&newframes);
439 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"New VAAPI frame pool with %1 %2x%3 surfaces")
441 av_buffer_unref(&hwdeviceref);
447 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Failed to create VAAPI deinterlacer %1 - disabling")
484 while ((result == Current) && retries--)
490 sinkframe->format = AV_PIX_FMT_VAAPI;
499 result =
m_lastFilteredFrame =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(sinkframe->data[3]));
504 if (ret != AVERROR(EAGAIN))
510 sourceframe->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
511 if (
Frame->m_interlacedReverse ^
Frame->m_topFieldFirst)
513 sourceframe->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
515 sourceframe->flags |= AV_FRAME_FLAG_INTERLACED;
516 sourceframe->data[3] =
Frame->m_buffer;
517 auto* buffer =
reinterpret_cast<AVBufferRef*
>(
Frame->m_priv[0]);
518 sourceframe->buf[0] = buffer ? av_buffer_ref(buffer) :
nullptr;
521 sourceframe->format = AV_PIX_FMT_VAAPI;
527 sourceframe->data[3] =
nullptr;
528 sourceframe->buf[0] =
nullptr;
537 result =
m_lastFilteredFrame =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(sinkframe->data[3]));
MythAVFrame little utility class that act as a safe way to allocate an AVFrame which can then be allo...
static bool DirectRenderingAvailable()
static QString TypeToString(InteropType Type)
std::vector< InteropType > InteropTypes
std::map< VideoFrameType, InteropTypes > InteropMap
MythRenderOpenGL * m_openglContext
static bool IsSupported(MythRenderOpenGL *Context)
virtual void DestroyDeinterlacer(void)
AVBufferRef * m_vppFramesContext
static bool SetupDeinterlacer(MythDeintType Deinterlacer, bool DoubleRate, AVBufferRef *FramesContext, int Width, int Height, AVFilterGraph *&Graph, AVFilterContext *&Source, AVFilterContext *&Sink)
void InitaliseDisplay(void)
~MythVAAPIInterop() override
VASurfaceID m_lastFilteredFrame
virtual void PostInitDeinterlacer(void)
static void GetVAAPITypes(MythRenderOpenGL *Context, MythInteropGPU::InteropMap &Types)
Return a list of interops that are supported by the current render device.
MythDeintType m_deinterlacer
AVFilterGraph * m_filterGraph
static MythVAAPIInterop * CreateVAAPI(MythPlayerUI *Player, MythRenderOpenGL *Context)
AVFilterContext * m_filterSource
VASurfaceID VerifySurface(MythRenderOpenGL *Context, MythVideoFrame *Frame)
uint64_t m_lastFilteredFrameCount
VASurfaceID Deinterlace(MythVideoFrame *Frame, VASurfaceID Current, FrameScanType Scan)
AVFilterContext * m_filterSink
MythVAAPIInterop(MythPlayerUI *Player, MythRenderOpenGL *Context, InteropType Type)
VADisplay GetDisplay(void)
static QString DeinterlacerName(MythDeintType Deint, bool DoubleRate, VideoFrameType Format=FMT_NONE)
static const struct wl_interface * types[]
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
static QString Source(const QNetworkRequest &request)
static QString cleanup(const QString &str)
bool is_interlaced(FrameScanType Scan)