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");
227 QString deinterlacer =
"bob";
229 deinterlacer =
"motion_adaptive";
231 deinterlacer =
"motion_compensated";
234 QString filters = QString(
"deinterlace_vaapi=mode=%1:rate=%2:auto=0")
235 .arg(deinterlacer, DoubleRate ?
"field" :
"frame");
236 const AVFilter *buffersrc = avfilter_get_by_name(
"buffer");
237 const AVFilter *buffersink = avfilter_get_by_name(
"buffersink");
238 AVFilterInOut *outputs = avfilter_inout_alloc();
239 AVFilterInOut *inputs = avfilter_inout_alloc();
240 AVBufferSrcParameters* params =
nullptr;
243 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 args = QString(
"video_size=%1x%2:pix_fmt=%3:time_base=1/1")
262 .arg(Width).arg(Height).arg(AV_PIX_FMT_VAAPI);
264 ret = avfilter_graph_create_filter(&
Source, buffersrc,
"in",
265 args.toLocal8Bit().constData(),
nullptr, Graph);
268 LOG(VB_GENERAL, LOG_ERR,
LOC +
"avfilter_graph_create_filter failed for buffer source");
272 params = av_buffersrc_parameters_alloc();
273 params->hw_frames_ctx = FramesContext;
274 ret = av_buffersrc_parameters_set(
Source, params);
278 LOG(VB_GENERAL, LOG_ERR,
LOC +
"av_buffersrc_parameters_set failed");
281 av_freep(
reinterpret_cast<void*
>(¶ms));
284 ret = avfilter_graph_create_filter(&Sink, buffersink,
"out",
285 nullptr,
nullptr, Graph);
288 LOG(VB_GENERAL, LOG_ERR,
LOC +
"avfilter_graph_create_filter failed for buffer sink");
303 outputs->name = av_strdup(
"in");
304 outputs->filter_ctx =
Source;
305 outputs->pad_idx = 0;
306 outputs->next =
nullptr;
314 inputs->name = av_strdup(
"out");
315 inputs->filter_ctx = Sink;
317 inputs->next =
nullptr;
319 ret = avfilter_graph_parse_ptr(Graph, filters.toLocal8Bit(),
320 &inputs, &outputs,
nullptr);
323 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"avfilter_graph_parse_ptr failed for %1")
328 ret = avfilter_graph_config(Graph,
nullptr);
331 LOG(VB_GENERAL, LOG_ERR,
LOC +
332 QString(
"VAAPI deinterlacer config failed - '%1' unsupported?").arg(deinterlacer));
336 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Created deinterlacer '%1'")
344 VASurfaceID result = Current;
352 bool doublerate =
true;
360 deinterlacer = doublepref;
364 deinterlacer = singlepref;
375 auto* frames =
reinterpret_cast<AVBufferRef*
>(
Frame->m_priv[1]);
379 AVBufferRef* hwdeviceref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
383 auto* hwdevicecontext =
reinterpret_cast<AVHWDeviceContext*
>(hwdeviceref->data);
384 hwdevicecontext->free = [](AVHWDeviceContext* ) {
LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"VAAPI VPP device context finished"); };
386 auto *vaapidevicectx =
reinterpret_cast<AVVAAPIDeviceContext*
>(hwdevicecontext->hwctx);
389 if (av_hwdevice_ctx_init(hwdeviceref) < 0)
391 av_buffer_unref(&hwdeviceref);
396 AVBufferRef *newframes = av_hwframe_ctx_alloc(hwdeviceref);
400 av_buffer_unref(&hwdeviceref);
404 auto* dstframes =
reinterpret_cast<AVHWFramesContext*
>(newframes->data);
405 auto* srcframes =
reinterpret_cast<AVHWFramesContext*
>(frames->data);
409 static constexpr int kVppPoolSize = 2;
410 dstframes->sw_format = srcframes->sw_format;
413 dstframes->initial_pool_size = kVppPoolSize;
414 dstframes->format = AV_PIX_FMT_VAAPI;
415 dstframes->free = [](AVHWFramesContext* ) {
LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"VAAPI VPP frames context finished"); };
417 if (av_hwframe_ctx_init(newframes) < 0)
420 av_buffer_unref(&hwdeviceref);
421 av_buffer_unref(&newframes);
426 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"New VAAPI frame pool with %1 %2x%3 surfaces")
428 av_buffer_unref(&hwdeviceref);
434 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Failed to create VAAPI deinterlacer %1 - disabling")
471 while ((result == Current) && retries--)
477 sinkframe->format = AV_PIX_FMT_VAAPI;
486 result =
m_lastFilteredFrame =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(sinkframe->data[3]));
491 if (ret != AVERROR(EAGAIN))
497 sourceframe->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
498 if (
Frame->m_interlacedReverse ^
Frame->m_topFieldFirst)
500 sourceframe->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
502 sourceframe->flags |= AV_FRAME_FLAG_INTERLACED;
503 sourceframe->data[3] =
Frame->m_buffer;
504 auto* buffer =
reinterpret_cast<AVBufferRef*
>(
Frame->m_priv[0]);
505 sourceframe->buf[0] = buffer ? av_buffer_ref(buffer) :
nullptr;
508 sourceframe->format = AV_PIX_FMT_VAAPI;
514 sourceframe->data[3] =
nullptr;
515 sourceframe->buf[0] =
nullptr;
524 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)