MythTV master
mythvaapiinterop.cpp
Go to the documentation of this file.
1#include "mythvaapiinterop.h"
2
3// MythTV
4#include "libmythbase/mythconfig.h"
7
8#if CONFIG_DRM_VIDEO
10#endif
11
12#include "mythvideoout.h"
13#include "mythplayerui.h"
15#include "fourcc.h"
16#if CONFIG_VAAPI_DRM
17#include "mythvaapidrminterop.h"
18#endif
19#if CONFIG_VAAPI_GLX
20#include "mythvaapiglxinterop.h"
21#endif
22
23extern "C" {
24#include "libavfilter/buffersrc.h"
25#include "libavfilter/buffersink.h"
26#include "libavutil/hwcontext_vaapi.h"
27}
28
29#define LOC QString("VAAPIInterop: ")
30
48{
49 if (!Context)
50 return;
51
52 OpenGLLocker locker(Context);
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");
56
57 // best first
59
60#if CONFIG_DRM_VIDEO
62 vaapitypes.emplace_back(DRM_DRMPRIME);
63#endif
64
65#if CONFIG_VAAPI_DRM && CONFIG_EGL
66 // zero copy
67 if (egl && MythVAAPIInteropDRM::IsSupported(Context))
68 vaapitypes.emplace_back(GL_VAAPIEGLDRM);
69#endif
70#if CONFIG_VAAPI_GLX
71# if CONFIG_VAAPI_X11
72 // 1x copy
73 if (!egl && !wayland && MythVAAPIInteropGLXPixmap::IsSupported(Context))
74 vaapitypes.emplace_back(GL_VAAPIGLXPIX);
75# endif
76 // 2x copy
77 if (!egl && !opengles && !wayland)
78 vaapitypes.emplace_back(GL_VAAPIGLXCOPY);
79#endif
80
81 if (!vaapitypes.empty())
82 Types[FMT_VAAPI] = vaapitypes;
83}
84
86{
87 if (!(Player && Context))
88 return nullptr;
89
90 const auto & types = Player->GetInteropTypes();
91 if (const auto & vaapi = types.find(FMT_VAAPI); vaapi != types.cend())
92 {
93 for ([[maybe_unused]] auto type : vaapi->second)
94 {
95#if CONFIG_VAAPI_DRM && CONFIG_EGL
96 if ((type == GL_VAAPIEGLDRM) || (type == DRM_DRMPRIME))
97 return new MythVAAPIInteropDRM(Player, Context, type);
98#endif
99#if CONFIG_VAAPI_GLX
100# if CONFIG_VAAPI_X11
101 if (type == GL_VAAPIGLXPIX)
102 return new MythVAAPIInteropGLXPixmap(Player, Context);
103# endif
104 if (type == GL_VAAPIGLXCOPY)
105 return new MythVAAPIInteropGLXCopy(Player, Context);
106#endif
107 }
108 }
109 return nullptr;
110}
111
120 : MythOpenGLInterop(Context, Type, Player)
121{
122}
123
125{
127 if (m_vaDisplay)
128 if (vaTerminate(m_vaDisplay) != VA_STATUS_SUCCESS)
129 LOG(VB_GENERAL, LOG_WARNING, LOC + "Error closing VAAPI display");
130}
131
133{
134 return m_vaDisplay;
135}
136
138{
139 return m_vaVendor;
140}
141
143{
144 if (!m_vaDisplay)
145 return;
146 int major = 0;
147 int minor = 0;
148 if (vaInitialize(m_vaDisplay, &major, &minor) != VA_STATUS_SUCCESS)
149 {
150 LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to initialise VAAPI display");
151 vaTerminate(m_vaDisplay);
152 m_vaDisplay = nullptr;
153 }
154 else
155 {
156 m_vaVendor = vaQueryVendorString(m_vaDisplay);
157 LOG(VB_GENERAL, LOG_INFO, LOC + QString("Created VAAPI %1.%2 display for %3 (%4)")
158 .arg(major).arg(minor).arg(TypeToString(m_type), m_vaVendor));
159 }
160}
161
163{
164 if (m_filterGraph)
165 LOG(VB_GENERAL, LOG_INFO, LOC + "Destroying VAAPI deinterlacer");
166 avfilter_graph_free(&m_filterGraph);
167 m_filterGraph = nullptr;
168 m_filterSink = nullptr;
169 m_filterSource = nullptr;
171 m_deinterlacer2x = false;
172 m_firstField = true;
175 av_buffer_unref(&m_vppFramesContext);
176}
177
179{
180 VASurfaceID result = 0;
181 if (!Frame)
182 return result;
183
184 if ((Frame->m_pixFmt != AV_PIX_FMT_VAAPI) || (Frame->m_type != FMT_VAAPI) ||
185 !Frame->m_buffer || !Frame->m_priv[1])
186 return result;
187
188 // Sanity check the context
189 if (m_openglContext != Context)
190 {
191 LOG(VB_GENERAL, LOG_ERR, LOC + "Mismatched OpenGL contexts!");
192 return result;
193 }
194
195 // Check size
196 QSize surfacesize(Frame->m_width, Frame->m_height);
197 if (m_textureSize != surfacesize)
198 {
199 if (!m_textureSize.isEmpty())
200 LOG(VB_GENERAL, LOG_WARNING, LOC + "Video texture size changed!");
201 m_textureSize = surfacesize;
202 }
203
204 // Retrieve surface
205 auto id = static_cast<VASurfaceID>(reinterpret_cast<uintptr_t>(Frame->m_buffer));
206 if (id)
207 result = id;
208 return result;
209}
210
211bool MythVAAPIInterop::SetupDeinterlacer(MythDeintType Deinterlacer, bool DoubleRate,
212 AVBufferRef *FramesContext,
213 int Width, int Height,
214 // Outputs
215 AVFilterGraph *&Graph,
216 AVFilterContext *&Source,
217 AVFilterContext *&Sink)
218{
219 if (!FramesContext)
220 {
221 LOG(VB_GENERAL, LOG_ERR, LOC + "No hardware frames context");
222 return false;
223 }
224
225 int ret = 0;
226 QString args;
227 QString deinterlacer = "bob";
228 if (DEINT_MEDIUM == Deinterlacer)
229 deinterlacer = "motion_adaptive";
230 else if (DEINT_HIGH == Deinterlacer)
231 deinterlacer = "motion_compensated";
232
233 // N.B. set auto to 0 otherwise we confuse playback if VAAPI does not deinterlace
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;
241
242 // Automatically clean up memory allocation at function exit
243 auto cleanup_fn = [&](int */*x*/) {
244 if (ret < 0) {
245 avfilter_graph_free(&Graph);
246 Graph = nullptr;
247 }
248 avfilter_inout_free(&inputs);
249 avfilter_inout_free(&outputs);
250 };
251 std::unique_ptr<int,decltype(cleanup_fn)> cleanup { &ret, cleanup_fn };
252
253 Graph = avfilter_graph_alloc();
254 if (!outputs || !inputs || !Graph)
255 {
256 ret = AVERROR(ENOMEM);
257 return false;
258 }
259
260 /* buffer video source: the decoded frames from the decoder will be inserted here. */
261 args = QString("video_size=%1x%2:pix_fmt=%3:time_base=1/1")
262 .arg(Width).arg(Height).arg(AV_PIX_FMT_VAAPI);
263
264 ret = avfilter_graph_create_filter(&Source, buffersrc, "in",
265 args.toLocal8Bit().constData(), nullptr, Graph);
266 if (ret < 0)
267 {
268 LOG(VB_GENERAL, LOG_ERR, LOC + "avfilter_graph_create_filter failed for buffer source");
269 return false;
270 }
271
272 params = av_buffersrc_parameters_alloc();
273 params->hw_frames_ctx = FramesContext;
274 ret = av_buffersrc_parameters_set(Source, params);
275
276 if (ret < 0)
277 {
278 LOG(VB_GENERAL, LOG_ERR, LOC + "av_buffersrc_parameters_set failed");
279 return false;
280 }
281 av_freep(reinterpret_cast<void*>(&params));
282
283 /* buffer video sink: to terminate the filter chain. */
284 ret = avfilter_graph_create_filter(&Sink, buffersink, "out",
285 nullptr, nullptr, Graph);
286 if (ret < 0)
287 {
288 LOG(VB_GENERAL, LOG_ERR, LOC + "avfilter_graph_create_filter failed for buffer sink");
289 return false;
290 }
291
292 /*
293 * Set the endpoints for the filter graph. The filter_graph will
294 * be linked to the graph described by filters_descr.
295 */
296
297 /*
298 * The buffer source output must be connected to the input pad of
299 * the first filter described by filters_descr; since the first
300 * filter input label is not specified, it is set to "in" by
301 * default.
302 */
303 outputs->name = av_strdup("in");
304 outputs->filter_ctx = Source;
305 outputs->pad_idx = 0;
306 outputs->next = nullptr;
307
308 /*
309 * The buffer sink input must be connected to the output pad of
310 * the last filter described by filters_descr; since the last
311 * filter output label is not specified, it is set to "out" by
312 * default.
313 */
314 inputs->name = av_strdup("out");
315 inputs->filter_ctx = Sink;
316 inputs->pad_idx = 0;
317 inputs->next = nullptr;
318
319 ret = avfilter_graph_parse_ptr(Graph, filters.toLocal8Bit(),
320 &inputs, &outputs, nullptr);
321 if (ret < 0)
322 {
323 LOG(VB_GENERAL, LOG_ERR, LOC + QString("avfilter_graph_parse_ptr failed for %1")
324 .arg(filters));
325 return false;
326 }
327
328 ret = avfilter_graph_config(Graph, nullptr);
329 if (ret < 0)
330 {
331 LOG(VB_GENERAL, LOG_ERR, LOC +
332 QString("VAAPI deinterlacer config failed - '%1' unsupported?").arg(deinterlacer));
333 return false;
334 }
335
336 LOG(VB_GENERAL, LOG_INFO, LOC + QString("Created deinterlacer '%1'")
337 .arg(MythVideoFrame::DeinterlacerName(Deinterlacer | DEINT_DRIVER, DoubleRate, FMT_VAAPI)));
338
339 return true;
340}
341
342VASurfaceID MythVAAPIInterop::Deinterlace(MythVideoFrame *Frame, VASurfaceID Current, FrameScanType Scan)
343{
344 VASurfaceID result = Current;
345 if (!Frame)
346 return result;
347
348 while (!m_filterError && is_interlaced(Scan))
349 {
350 // N.B. for DRM the use of a shader deint is checked before we get here
351 MythDeintType deinterlacer = DEINT_NONE;
352 bool doublerate = true;
353 // no CPU or GLSL deinterlacing so pick up these options as well
354 // N.B. Override deinterlacer_allowed to pick up any preference
355 MythDeintType doublepref = Frame->GetDoubleRateOption(DEINT_DRIVER | DEINT_SHADER | DEINT_CPU, DEINT_ALL);
356 MythDeintType singlepref = Frame->GetSingleRateOption(DEINT_DRIVER | DEINT_SHADER | DEINT_CPU, DEINT_ALL);
357
358 if (doublepref)
359 {
360 deinterlacer = doublepref;
361 }
362 else if (singlepref)
363 {
364 deinterlacer = singlepref;
365 doublerate = false;
366 }
367
368 if ((m_deinterlacer == deinterlacer) && (m_deinterlacer2x == doublerate))
369 break;
370
372
373 if (deinterlacer != DEINT_NONE)
374 {
375 auto* frames = reinterpret_cast<AVBufferRef*>(Frame->m_priv[1]);
376 if (!frames)
377 break;
378
379 AVBufferRef* hwdeviceref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
380 if (!hwdeviceref)
381 break;
382
383 auto* hwdevicecontext = reinterpret_cast<AVHWDeviceContext*>(hwdeviceref->data);
384 hwdevicecontext->free = [](AVHWDeviceContext* /*unused*/) { LOG(VB_PLAYBACK, LOG_INFO, LOC + "VAAPI VPP device context finished"); };
385
386 auto *vaapidevicectx = reinterpret_cast<AVVAAPIDeviceContext*>(hwdevicecontext->hwctx);
387 vaapidevicectx->display = m_vaDisplay; // re-use the existing display
388
389 if (av_hwdevice_ctx_init(hwdeviceref) < 0)
390 {
391 av_buffer_unref(&hwdeviceref);
392 m_filterError = true;
393 break;
394 }
395
396 AVBufferRef *newframes = av_hwframe_ctx_alloc(hwdeviceref);
397 if (!newframes)
398 {
399 m_filterError = true;
400 av_buffer_unref(&hwdeviceref);
401 break;
402 }
403
404 auto* dstframes = reinterpret_cast<AVHWFramesContext*>(newframes->data);
405 auto* srcframes = reinterpret_cast<AVHWFramesContext*>(frames->data);
406
407 m_filterWidth = srcframes->width;
408 m_filterHeight = srcframes->height;
409 static constexpr int kVppPoolSize = 2; // seems to be enough
410 dstframes->sw_format = srcframes->sw_format;
411 dstframes->width = m_filterWidth;
412 dstframes->height = m_filterHeight;
413 dstframes->initial_pool_size = kVppPoolSize;
414 dstframes->format = AV_PIX_FMT_VAAPI;
415 dstframes->free = [](AVHWFramesContext* /*unused*/) { LOG(VB_PLAYBACK, LOG_INFO, LOC + "VAAPI VPP frames context finished"); };
416
417 if (av_hwframe_ctx_init(newframes) < 0)
418 {
419 m_filterError = true;
420 av_buffer_unref(&hwdeviceref);
421 av_buffer_unref(&newframes);
422 break;
423 }
424
425 m_vppFramesContext = newframes;
426 LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("New VAAPI frame pool with %1 %2x%3 surfaces")
427 .arg(kVppPoolSize).arg(m_filterWidth).arg(m_filterHeight));
428 av_buffer_unref(&hwdeviceref);
429
430 if (!MythVAAPIInterop::SetupDeinterlacer(deinterlacer, doublerate, m_vppFramesContext,
433 {
434 LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to create VAAPI deinterlacer %1 - disabling")
435 .arg(MythVideoFrame::DeinterlacerName(deinterlacer | DEINT_DRIVER, doublerate, FMT_VAAPI)));
437 m_filterError = true;
438 }
439 else
440 {
441 m_deinterlacer = deinterlacer;
442 m_deinterlacer2x = doublerate;
444 break;
445 }
446 }
447 break;
448 }
449
450 if (!is_interlaced(Scan) && m_deinterlacer)
452
453 if (m_deinterlacer)
454 {
455 // deinterlacing the pause frame repeatedly is unnecessary and, due to the
456 // buffering in the VAAPI frames context, causes the image to 'jiggle' when using
457 // double rate deinterlacing. If we are confident this is a pause frame we have seen,
458 // return the last deinterlaced frame.
459 if (Frame->m_pauseFrame && m_lastFilteredFrame && (m_lastFilteredFrameCount == Frame->m_frameCounter))
460 return m_lastFilteredFrame;
461
462 Frame->m_deinterlaceInuse = m_deinterlacer | DEINT_DRIVER;
463 Frame->m_deinterlaceInuse2x = m_deinterlacer2x;
464
465 // 'pump' the filter with frames until it starts returning usefull output.
466 // This minimises discontinuities at start up (where we would otherwise
467 // show a couple of progressive frames first) and memory consumption for the DRM
468 // interop as we only cache OpenGL textures for the deinterlacer's frame
469 // pool.
470 int retries = 3;
471 while ((result == Current) && retries--)
472 {
473 while (true)
474 {
475 int ret = 0;
476 MythAVFrame sinkframe;
477 sinkframe->format = AV_PIX_FMT_VAAPI;
478
479 // only ask for a frame if we are expecting another
481 {
482 ret = av_buffersink_get_frame(m_filterSink, sinkframe);
483 if (ret >= 0)
484 {
485 // we have a filtered frame
486 result = m_lastFilteredFrame = static_cast<VASurfaceID>(reinterpret_cast<uintptr_t>(sinkframe->data[3]));
487 m_lastFilteredFrameCount = Frame->m_frameCounter;
488 m_firstField = true;
489 break;
490 }
491 if (ret != AVERROR(EAGAIN))
492 break;
493 }
494
495 // add another frame
496 MythAVFrame sourceframe;
497 sourceframe->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
498 if (Frame->m_interlacedReverse ^ Frame->m_topFieldFirst)
499 {
500 sourceframe->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
501 }
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;
506 sourceframe->width = m_filterWidth;
507 sourceframe->height = m_filterHeight;
508 sourceframe->format = AV_PIX_FMT_VAAPI;
509 // N.B. This is required after changes in FFmpeg. Using the vpp
510 // frames context doesn't seem correct - but using the 'master'
511 // context does not work
512 sourceframe->hw_frames_ctx = av_buffer_ref(m_vppFramesContext);
513 ret = av_buffersrc_add_frame(m_filterSource, sourceframe);
514 sourceframe->data[3] = nullptr;
515 sourceframe->buf[0] = nullptr;
516 if (ret < 0)
517 break;
518
519 // try again
520 ret = av_buffersink_get_frame(m_filterSink, sinkframe);
521 if (ret >= 0)
522 {
523 // we have a filtered frame
524 result = m_lastFilteredFrame = static_cast<VASurfaceID>(reinterpret_cast<uintptr_t>(sinkframe->data[3]));
525 m_lastFilteredFrameCount = Frame->m_frameCounter;
526 m_firstField = false;
527 break;
528 }
529 break;
530 }
531 }
532 }
533 return result;
534}
MythAVFrame little utility class that act as a safe way to allocate an AVFrame which can then be allo...
Definition: mythavframe.h:27
static bool DirectRenderingAvailable()
bool IsEGL(void)
Definition: mythegl.cpp:31
static QString TypeToString(InteropType Type)
std::vector< InteropType > InteropTypes
InteropType m_type
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
QString GetVendor(void)
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)
Definition: mythframe.cpp:462
#define minor(X)
Definition: compat.h:65
static const struct wl_interface * types[]
MythDeintType
Definition: mythframe.h:67
@ DEINT_HIGH
Definition: mythframe.h:71
@ DEINT_DRIVER
Definition: mythframe.h:74
@ DEINT_MEDIUM
Definition: mythframe.h:70
@ DEINT_NONE
Definition: mythframe.h:68
@ DEINT_SHADER
Definition: mythframe.h:73
@ DEINT_ALL
Definition: mythframe.h:75
@ DEINT_CPU
Definition: mythframe.h:72
@ FMT_VAAPI
Definition: mythframe.h:57
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
#define LOC
static QString Source(const QNetworkRequest &request)
Definition: netstream.cpp:139
static QString cleanup(const QString &str)
FrameScanType
Definition: videoouttypes.h:95
bool is_interlaced(FrameScanType Scan)