15 #include "libavfilter/buffersrc.h"
16 #include "libavfilter/buffersink.h"
17 #include "libavutil/hwcontext_vaapi.h"
20 #define LOC QString("VAAPIInterop: ")
44 bool egl = Context->
IsEGL();
45 bool opengles = Context->isOpenGLES();
46 bool wayland = qgetenv(
"XDG_SESSION_TYPE").contains(
"wayland");
51 #ifdef USING_DRM_VIDEO
65 if (!egl && !opengles && !wayland)
68 if (!vaapitypes.empty())
80 for (
auto type : vaapi->second)
112 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Error closing VAAPI display");
133 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to initialise VAAPI display");
140 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Created VAAPI %1.%2 display for %3 (%4)")
148 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Destroying VAAPI deinterlacer");
163 VASurfaceID result = 0;
174 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Mismatched OpenGL contexts!");
179 QSize surfacesize(
Frame->m_width,
Frame->m_height);
183 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Video texture size changed!");
188 auto id =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(
Frame->m_buffer));
195 AVBufferRef *FramesContext,
196 int Width,
int Height,
198 AVFilterGraph *&Graph,
200 AVFilterContext *&Sink)
204 LOG(VB_GENERAL, LOG_ERR,
LOC +
"No hardware frames context");
210 QString deinterlacer =
"bob";
212 deinterlacer =
"motion_adaptive";
214 deinterlacer =
"motion_compensated";
217 QString filters = QString(
"deinterlace_vaapi=mode=%1:rate=%2:auto=0")
218 .arg(deinterlacer, DoubleRate ?
"field" :
"frame");
219 const AVFilter *buffersrc = avfilter_get_by_name(
"buffer");
220 const AVFilter *buffersink = avfilter_get_by_name(
"buffersink");
221 AVFilterInOut *outputs = avfilter_inout_alloc();
222 AVFilterInOut *inputs = avfilter_inout_alloc();
223 AVBufferSrcParameters* params =
nullptr;
225 Graph = avfilter_graph_alloc();
226 if (!outputs || !inputs || !Graph)
228 ret = AVERROR(ENOMEM);
233 args = QString(
"video_size=%1x%2:pix_fmt=%3:time_base=1/1")
234 .arg(Width).arg(Height).arg(AV_PIX_FMT_VAAPI);
236 ret = avfilter_graph_create_filter(&
Source, buffersrc,
"in",
237 args.toLocal8Bit().constData(),
nullptr, Graph);
240 LOG(VB_GENERAL, LOG_ERR,
LOC +
"avfilter_graph_create_filter failed for buffer source");
244 params = av_buffersrc_parameters_alloc();
245 params->hw_frames_ctx = FramesContext;
246 ret = av_buffersrc_parameters_set(
Source, params);
250 LOG(VB_GENERAL, LOG_ERR,
LOC +
"av_buffersrc_parameters_set failed");
256 ret = avfilter_graph_create_filter(&Sink, buffersink,
"out",
257 nullptr,
nullptr, Graph);
260 LOG(VB_GENERAL, LOG_ERR,
LOC +
"avfilter_graph_create_filter failed for buffer sink");
275 outputs->name = av_strdup(
"in");
276 outputs->filter_ctx =
Source;
277 outputs->pad_idx = 0;
278 outputs->next =
nullptr;
286 inputs->name = av_strdup(
"out");
287 inputs->filter_ctx = Sink;
289 inputs->next =
nullptr;
291 if ((ret = avfilter_graph_parse_ptr(Graph, filters.toLocal8Bit(),
292 &inputs, &outputs,
nullptr)) < 0)
294 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"avfilter_graph_parse_ptr failed for %1")
299 if ((ret = avfilter_graph_config(Graph,
nullptr)) < 0)
301 LOG(VB_GENERAL, LOG_ERR,
LOC +
302 QString(
"VAAPI deinterlacer config failed - '%1' unsupported?").arg(deinterlacer));
306 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Created deinterlacer '%1'")
312 avfilter_graph_free(&Graph);
315 avfilter_inout_free(&inputs);
316 avfilter_inout_free(&outputs);
322 VASurfaceID result = Current;
330 bool doublerate =
true;
338 deinterlacer = doublepref;
342 deinterlacer = singlepref;
353 auto* frames =
reinterpret_cast<AVBufferRef*
>(
Frame->m_priv[1]);
357 AVBufferRef* hwdeviceref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
361 auto* hwdevicecontext =
reinterpret_cast<AVHWDeviceContext*
>(hwdeviceref->data);
362 hwdevicecontext->free = [](AVHWDeviceContext* ) {
LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"VAAPI VPP device context finished"); };
364 auto *vaapidevicectx =
reinterpret_cast<AVVAAPIDeviceContext*
>(hwdevicecontext->hwctx);
367 if (av_hwdevice_ctx_init(hwdeviceref) < 0)
369 av_buffer_unref(&hwdeviceref);
374 AVBufferRef *newframes = av_hwframe_ctx_alloc(hwdeviceref);
378 av_buffer_unref(&hwdeviceref);
382 auto* dstframes =
reinterpret_cast<AVHWFramesContext*
>(newframes->data);
383 auto* srcframes =
reinterpret_cast<AVHWFramesContext*
>(frames->data);
387 static constexpr
int kVppPoolSize = 2;
388 dstframes->sw_format = srcframes->sw_format;
391 dstframes->initial_pool_size = kVppPoolSize;
392 dstframes->format = AV_PIX_FMT_VAAPI;
393 dstframes->free = [](AVHWFramesContext* ) {
LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"VAAPI VPP frames context finished"); };
395 if (av_hwframe_ctx_init(newframes) < 0)
398 av_buffer_unref(&hwdeviceref);
399 av_buffer_unref(&newframes);
404 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"New VAAPI frame pool with %1 %2x%3 surfaces")
406 av_buffer_unref(&hwdeviceref);
412 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Failed to create VAAPI deinterlacer %1 - disabling")
449 while ((result == Current) && retries--)
455 sinkframe->format = AV_PIX_FMT_VAAPI;
464 result =
m_lastFilteredFrame =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(sinkframe->data[3]));
469 if (ret != AVERROR(EAGAIN))
475 sourceframe->top_field_first =
476 static_cast<int>(
Frame->m_interlacedReverse ? !
Frame->m_topFieldFirst :
Frame->m_topFieldFirst);
477 sourceframe->interlaced_frame = 1;
478 sourceframe->data[3] =
Frame->m_buffer;
479 auto* buffer =
reinterpret_cast<AVBufferRef*
>(
Frame->m_priv[0]);
480 sourceframe->buf[0] = buffer ? av_buffer_ref(buffer) :
nullptr;
483 sourceframe->format = AV_PIX_FMT_VAAPI;
489 sourceframe->data[3] =
nullptr;
490 sourceframe->buf[0] =
nullptr;
499 result =
m_lastFilteredFrame =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(sinkframe->data[3]));