2#include "libmythbase/mythconfig.h"
19#include "libavfilter/buffersrc.h"
20#include "libavfilter/buffersink.h"
21#include "libavutil/hwcontext_vaapi.h"
24#define LOC QString("VAAPIInterop: ")
48 bool egl = Context->
IsEGL();
49 bool opengles = Context->isOpenGLES();
50 bool wayland = qgetenv(
"XDG_SESSION_TYPE").contains(
"wayland");
69 if (!egl && !opengles && !wayland)
72 if (!vaapitypes.empty())
84 for (
auto type : vaapi->second)
116 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Error closing VAAPI display");
137 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to initialise VAAPI display");
144 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Created VAAPI %1.%2 display for %3 (%4)")
152 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Destroying VAAPI deinterlacer");
167 VASurfaceID result = 0;
178 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Mismatched OpenGL contexts!");
183 QSize surfacesize(
Frame->m_width,
Frame->m_height);
187 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Video texture size changed!");
192 auto id =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(
Frame->m_buffer));
199 AVBufferRef *FramesContext,
200 int Width,
int Height,
202 AVFilterGraph *&Graph,
204 AVFilterContext *&Sink)
208 LOG(VB_GENERAL, LOG_ERR,
LOC +
"No hardware frames context");
214 QString deinterlacer =
"bob";
216 deinterlacer =
"motion_adaptive";
218 deinterlacer =
"motion_compensated";
221 QString filters = QString(
"deinterlace_vaapi=mode=%1:rate=%2:auto=0")
222 .arg(deinterlacer, DoubleRate ?
"field" :
"frame");
223 const AVFilter *buffersrc = avfilter_get_by_name(
"buffer");
224 const AVFilter *buffersink = avfilter_get_by_name(
"buffersink");
225 AVFilterInOut *outputs = avfilter_inout_alloc();
226 AVFilterInOut *inputs = avfilter_inout_alloc();
227 AVBufferSrcParameters* params =
nullptr;
230 auto cleanup_fn = [&](
int *) {
232 avfilter_graph_free(&Graph);
235 avfilter_inout_free(&inputs);
236 avfilter_inout_free(&outputs);
238 std::unique_ptr<int,
decltype(cleanup_fn)>
cleanup { &ret, cleanup_fn };
240 Graph = avfilter_graph_alloc();
241 if (!outputs || !inputs || !Graph)
243 ret = AVERROR(ENOMEM);
248 args = QString(
"video_size=%1x%2:pix_fmt=%3:time_base=1/1")
249 .arg(Width).arg(Height).arg(AV_PIX_FMT_VAAPI);
251 ret = avfilter_graph_create_filter(&
Source, buffersrc,
"in",
252 args.toLocal8Bit().constData(),
nullptr, Graph);
255 LOG(VB_GENERAL, LOG_ERR,
LOC +
"avfilter_graph_create_filter failed for buffer source");
259 params = av_buffersrc_parameters_alloc();
260 params->hw_frames_ctx = FramesContext;
261 ret = av_buffersrc_parameters_set(
Source, params);
265 LOG(VB_GENERAL, LOG_ERR,
LOC +
"av_buffersrc_parameters_set failed");
268 av_freep(
reinterpret_cast<void*
>(¶ms));
271 ret = avfilter_graph_create_filter(&Sink, buffersink,
"out",
272 nullptr,
nullptr, Graph);
275 LOG(VB_GENERAL, LOG_ERR,
LOC +
"avfilter_graph_create_filter failed for buffer sink");
290 outputs->name = av_strdup(
"in");
291 outputs->filter_ctx =
Source;
292 outputs->pad_idx = 0;
293 outputs->next =
nullptr;
301 inputs->name = av_strdup(
"out");
302 inputs->filter_ctx = Sink;
304 inputs->next =
nullptr;
306 ret = avfilter_graph_parse_ptr(Graph, filters.toLocal8Bit(),
307 &inputs, &outputs,
nullptr);
310 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"avfilter_graph_parse_ptr failed for %1")
315 ret = avfilter_graph_config(Graph,
nullptr);
318 LOG(VB_GENERAL, LOG_ERR,
LOC +
319 QString(
"VAAPI deinterlacer config failed - '%1' unsupported?").arg(deinterlacer));
323 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Created deinterlacer '%1'")
331 VASurfaceID result = Current;
339 bool doublerate =
true;
347 deinterlacer = doublepref;
351 deinterlacer = singlepref;
362 auto* frames =
reinterpret_cast<AVBufferRef*
>(
Frame->m_priv[1]);
366 AVBufferRef* hwdeviceref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
370 auto* hwdevicecontext =
reinterpret_cast<AVHWDeviceContext*
>(hwdeviceref->data);
371 hwdevicecontext->free = [](AVHWDeviceContext* ) {
LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"VAAPI VPP device context finished"); };
373 auto *vaapidevicectx =
reinterpret_cast<AVVAAPIDeviceContext*
>(hwdevicecontext->hwctx);
376 if (av_hwdevice_ctx_init(hwdeviceref) < 0)
378 av_buffer_unref(&hwdeviceref);
383 AVBufferRef *newframes = av_hwframe_ctx_alloc(hwdeviceref);
387 av_buffer_unref(&hwdeviceref);
391 auto* dstframes =
reinterpret_cast<AVHWFramesContext*
>(newframes->data);
392 auto* srcframes =
reinterpret_cast<AVHWFramesContext*
>(frames->data);
396 static constexpr int kVppPoolSize = 2;
397 dstframes->sw_format = srcframes->sw_format;
400 dstframes->initial_pool_size = kVppPoolSize;
401 dstframes->format = AV_PIX_FMT_VAAPI;
402 dstframes->free = [](AVHWFramesContext* ) {
LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"VAAPI VPP frames context finished"); };
404 if (av_hwframe_ctx_init(newframes) < 0)
407 av_buffer_unref(&hwdeviceref);
408 av_buffer_unref(&newframes);
413 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"New VAAPI frame pool with %1 %2x%3 surfaces")
415 av_buffer_unref(&hwdeviceref);
421 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Failed to create VAAPI deinterlacer %1 - disabling")
458 while ((result == Current) && retries--)
464 sinkframe->format = AV_PIX_FMT_VAAPI;
473 result =
m_lastFilteredFrame =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(sinkframe->data[3]));
478 if (ret != AVERROR(EAGAIN))
484 sourceframe->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
485 if (
Frame->m_interlacedReverse ^
Frame->m_topFieldFirst)
487 sourceframe->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
489 sourceframe->flags |= AV_FRAME_FLAG_INTERLACED;
490 sourceframe->data[3] =
Frame->m_buffer;
491 auto* buffer =
reinterpret_cast<AVBufferRef*
>(
Frame->m_priv[0]);
492 sourceframe->buf[0] = buffer ? av_buffer_ref(buffer) :
nullptr;
495 sourceframe->format = AV_PIX_FMT_VAAPI;
501 sourceframe->data[3] =
nullptr;
502 sourceframe->buf[0] =
nullptr;
511 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)
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)