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");
253 av_freep(
reinterpret_cast<void*
>(¶ms));
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 ret = avfilter_graph_parse_ptr(Graph, filters.toLocal8Bit(),
292 &inputs, &outputs,
nullptr);
295 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"avfilter_graph_parse_ptr failed for %1")
300 ret = avfilter_graph_config(Graph,
nullptr);
303 LOG(VB_GENERAL, LOG_ERR,
LOC +
304 QString(
"VAAPI deinterlacer config failed - '%1' unsupported?").arg(deinterlacer));
308 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Created deinterlacer '%1'")
314 avfilter_graph_free(&Graph);
317 avfilter_inout_free(&inputs);
318 avfilter_inout_free(&outputs);
324 VASurfaceID result = Current;
332 bool doublerate =
true;
340 deinterlacer = doublepref;
344 deinterlacer = singlepref;
355 auto* frames =
reinterpret_cast<AVBufferRef*
>(
Frame->m_priv[1]);
359 AVBufferRef* hwdeviceref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
363 auto* hwdevicecontext =
reinterpret_cast<AVHWDeviceContext*
>(hwdeviceref->data);
364 hwdevicecontext->free = [](AVHWDeviceContext* ) {
LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"VAAPI VPP device context finished"); };
366 auto *vaapidevicectx =
reinterpret_cast<AVVAAPIDeviceContext*
>(hwdevicecontext->hwctx);
369 if (av_hwdevice_ctx_init(hwdeviceref) < 0)
371 av_buffer_unref(&hwdeviceref);
376 AVBufferRef *newframes = av_hwframe_ctx_alloc(hwdeviceref);
380 av_buffer_unref(&hwdeviceref);
384 auto* dstframes =
reinterpret_cast<AVHWFramesContext*
>(newframes->data);
385 auto* srcframes =
reinterpret_cast<AVHWFramesContext*
>(frames->data);
389 static constexpr
int kVppPoolSize = 2;
390 dstframes->sw_format = srcframes->sw_format;
393 dstframes->initial_pool_size = kVppPoolSize;
394 dstframes->format = AV_PIX_FMT_VAAPI;
395 dstframes->free = [](AVHWFramesContext* ) {
LOG(VB_PLAYBACK, LOG_INFO,
LOC +
"VAAPI VPP frames context finished"); };
397 if (av_hwframe_ctx_init(newframes) < 0)
400 av_buffer_unref(&hwdeviceref);
401 av_buffer_unref(&newframes);
406 LOG(VB_PLAYBACK, LOG_INFO,
LOC + QString(
"New VAAPI frame pool with %1 %2x%3 surfaces")
408 av_buffer_unref(&hwdeviceref);
414 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Failed to create VAAPI deinterlacer %1 - disabling")
451 while ((result == Current) && retries--)
457 sinkframe->format = AV_PIX_FMT_VAAPI;
466 result =
m_lastFilteredFrame =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(sinkframe->data[3]));
471 if (ret != AVERROR(EAGAIN))
477 sourceframe->top_field_first =
478 static_cast<int>(
Frame->m_interlacedReverse ? !
Frame->m_topFieldFirst :
Frame->m_topFieldFirst);
479 sourceframe->interlaced_frame = 1;
480 sourceframe->data[3] =
Frame->m_buffer;
481 auto* buffer =
reinterpret_cast<AVBufferRef*
>(
Frame->m_priv[0]);
482 sourceframe->buf[0] = buffer ? av_buffer_ref(buffer) :
nullptr;
485 sourceframe->format = AV_PIX_FMT_VAAPI;
491 sourceframe->data[3] =
nullptr;
492 sourceframe->buf[0] =
nullptr;
501 result =
m_lastFilteredFrame =
static_cast<VASurfaceID
>(
reinterpret_cast<uintptr_t
>(sinkframe->data[3]));