MythTV master
mythvaapicontext.cpp
Go to the documentation of this file.
1// Qt
2#include <QCoreApplication>
3#include <QWaitCondition>
4
5// Mythtv
10
12#include "mythplayerui.h"
13#include "mythvaapicontext.h"
15#include "videobuffers.h"
16
17extern "C" {
18#include "libavutil/hwcontext_vaapi.h"
19#include "libavutil/pixdesc.h"
20#include "libavfilter/buffersink.h"
21}
22
23#define LOC QString("VAAPIDec: ")
24
34 : MythCodecContext(Parent, CodecID)
35{
36}
37
39{
41}
42
43VAProfile MythVAAPIContext::VAAPIProfileForCodec(const AVCodecContext* Codec)
44{
45 if (!Codec)
46 return VAProfileNone;
47
48 switch (Codec->codec_id)
49 {
50 case AV_CODEC_ID_MPEG2VIDEO:
51 switch (Codec->profile)
52 {
53 case FF_PROFILE_MPEG2_SIMPLE: return VAProfileMPEG2Simple;
54 case FF_PROFILE_MPEG2_MAIN: return VAProfileMPEG2Main;
55 default: break;
56 }
57 break;
58 case AV_CODEC_ID_H263: return VAProfileH263Baseline;
59 case AV_CODEC_ID_MPEG4:
60 switch (Codec->profile)
61 {
62 case FF_PROFILE_MPEG4_SIMPLE: return VAProfileMPEG4Simple;
63 case FF_PROFILE_MPEG4_ADVANCED_SIMPLE: return VAProfileMPEG4AdvancedSimple;
64 case FF_PROFILE_MPEG4_MAIN: return VAProfileMPEG4Main;
65 default: break;
66 }
67 break;
68 case AV_CODEC_ID_H264:
69 switch (Codec->profile)
70 {
71 case FF_PROFILE_H264_CONSTRAINED_BASELINE: return VAProfileH264ConstrainedBaseline;
72 case FF_PROFILE_H264_MAIN: return VAProfileH264Main;
73 case FF_PROFILE_H264_HIGH: return VAProfileH264High;
74 default: break;
75 }
76 break;
77 case AV_CODEC_ID_HEVC:
78 switch (Codec->profile)
79 {
80 case FF_PROFILE_HEVC_MAIN: return VAProfileHEVCMain;
81 case FF_PROFILE_HEVC_MAIN_10: return VAProfileHEVCMain10;
82 default: break;
83 }
84 break;
85 case AV_CODEC_ID_MJPEG: return VAProfileJPEGBaseline;
86 case AV_CODEC_ID_WMV3:
87 case AV_CODEC_ID_VC1:
88 switch (Codec->profile)
89 {
90 case FF_PROFILE_VC1_SIMPLE: return VAProfileVC1Simple;
91 case FF_PROFILE_VC1_MAIN: return VAProfileVC1Main;
92 case FF_PROFILE_VC1_ADVANCED:
93 case FF_PROFILE_VC1_COMPLEX: return VAProfileVC1Advanced;
94 default: break;
95 }
96 break;
97 case AV_CODEC_ID_VP8: return VAProfileVP8Version0_3;
98 case AV_CODEC_ID_VP9:
99 switch (Codec->profile)
100 {
101 case FF_PROFILE_VP9_0: return VAProfileVP9Profile0;
102 case FF_PROFILE_VP9_2: return VAProfileVP9Profile2;
103 default: break;
104 }
105 break;
106 default: break;
107 }
108
109 return VAProfileNone;
110}
111
112inline AVPixelFormat MythVAAPIContext::FramesFormat(AVPixelFormat Format)
113{
114 switch (Format)
115 {
116 case AV_PIX_FMT_YUV420P10: return AV_PIX_FMT_P010;
117 case AV_PIX_FMT_YUV420P12:
118 case AV_PIX_FMT_YUV420P14:
119 case AV_PIX_FMT_YUV420P16: return AV_PIX_FMT_P016;
120 default: return AV_PIX_FMT_NV12;
121 }
122}
123
127 const AVCodec** /*Codec*/,
128 const QString& Decoder,
129 uint StreamType)
130{
131 bool decodeonly = Decoder == "vaapi-dec";
132 auto success = static_cast<MythCodecID>((decodeonly ? kCodec_MPEG1_VAAPI_DEC : kCodec_MPEG1_VAAPI) + (StreamType - 1));
133 auto failure = static_cast<MythCodecID>(kCodec_MPEG1 + (StreamType - 1));
134 auto vendor = HaveVAAPI();
135 if (!Decoder.startsWith("vaapi") || vendor.isEmpty() || qEnvironmentVariableIsSet("NO_VAAPI"))
136 return failure;
137
138 const auto * codec = avcodec_get_name((*Context)->codec_id);
139 const auto * profile = avcodec_profile_name((*Context)->codec_id, (*Context)->profile);
140 const auto * pixfmt = av_get_pix_fmt_name((*Context)->pix_fmt);
141
142 // Simple check for known profile
143 auto desired = VAAPIProfileForCodec(*Context);
144 if (desired == VAProfileNone)
145 {
146 LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("VAAPI does not support decoding '%1 %2 %3'")
147 .arg(codec, profile, pixfmt));
148 return failure;
149 }
150
151 // Check for ironlake decode only - which won't work due to FFmpeg frame format
152 // constraints. May apply to other platforms.
153 if (decodeonly)
154 {
155 if (vendor.contains("ironlake", Qt::CaseInsensitive))
156 {
157 LOG(VB_GENERAL, LOG_WARNING, LOC + "Disallowing VAAPI decode only for Ironlake");
158 return failure;
159 }
160 }
161 else
162 {
163 if (!FrameTypeIsSupported(*Context, FMT_VAAPI))
164 return failure;
165 }
166
167 // Check profile support
168 bool ok = false;
169 const auto & profiles = MythVAAPIContext::GetProfiles();
170 auto mythprofile = MythCodecContext::FFmpegToMythProfile((*Context)->codec_id, (*Context)->profile);
171 auto haveprofile = [&](MythCodecContext::CodecProfile Profile, QSize Size)
172 {
173 return std::any_of(profiles.cbegin(), profiles.cend(),
174 [&Profile,Size](auto vaprofile)
175 { return vaprofile.first == Profile &&
176 vaprofile.second.first.width() <= Size.width() &&
177 vaprofile.second.first.height() <= Size.height() &&
178 vaprofile.second.second.width() >= Size.width() &&
179 vaprofile.second.second.height() >= Size.height(); } );
180 };
181
182 ok = haveprofile(mythprofile, QSize((*Context)->width, (*Context)->height));
183 // use JPEG support as a proxy for MJPEG (full range YUV)
184 if (ok && (AV_PIX_FMT_YUVJ420P == (*Context)->pix_fmt || AV_PIX_FMT_YUVJ422P == (*Context)->pix_fmt ||
185 AV_PIX_FMT_YUVJ444P == (*Context)->pix_fmt))
186 {
187 ok = haveprofile(MythCodecContext::MJPEG, QSize());
188 }
189
190 auto desc = QString("'%1 %2 %3 %4x%5'").arg(codec, profile, pixfmt)
191 .arg((*Context)->width).arg((*Context)->height);
192
193 if (ok)
194 {
195 LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("VAAPI supports decoding %1").arg(desc));
196 (*Context)->pix_fmt = AV_PIX_FMT_VAAPI;
197 return success;
198 }
199
200 LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("VAAPI does NOT support %1").arg(desc));
201 return failure;
202}
203
204AVPixelFormat MythVAAPIContext::GetFormat(AVCodecContext* Context, const AVPixelFormat* PixFmt)
205{
206 while (*PixFmt != AV_PIX_FMT_NONE)
207 {
208 if (*PixFmt == AV_PIX_FMT_VAAPI)
209 if (MythCodecContext::InitialiseDecoder(Context, MythVAAPIContext::InitialiseContext, "VAAPI context creation") >= 0)
210 return AV_PIX_FMT_VAAPI;
211 PixFmt++;
212 }
213 return AV_PIX_FMT_NONE;
214}
215
216AVPixelFormat MythVAAPIContext::GetFormat2(AVCodecContext* Context, const AVPixelFormat* PixFmt)
217{
218 while (*PixFmt != AV_PIX_FMT_NONE)
219 {
220 if (*PixFmt == AV_PIX_FMT_VAAPI)
221 if (InitialiseContext2(Context) >= 0)
222 return AV_PIX_FMT_VAAPI;
223 PixFmt++;
224 }
225 return AV_PIX_FMT_NONE;
226}
227
230int MythVAAPIContext::InitialiseContext(AVCodecContext* Context)
231{
232 if (!Context || !gCoreContext->IsUIThread())
233 return -1;
234
235 // The interop must have a reference to the ui player so it can be deleted
236 // from the main thread.
237 MythVAAPIInterop* interop = nullptr;
238 if (auto * player = GetPlayerUI(Context); player != nullptr)
239 if (auto * render = dynamic_cast<MythRenderOpenGL*>(player->GetRender()); render != nullptr)
240 interop = MythVAAPIInterop::CreateVAAPI(player, render);
241
242 if (!interop || !interop->GetDisplay())
243 {
244 if (interop)
245 interop->DecrRef();
246 return -1;
247 }
248
249 // Create hardware device context
250 auto * hwdeviceref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
251 if (!hwdeviceref || !hwdeviceref->data)
252 {
253 LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to create VAAPI hardware device context");
254 return -1;
255 }
256
257 AVVAAPIDeviceContext * vaapidevicectx = nullptr;
258 if (auto * hwdevicecontext = reinterpret_cast<AVHWDeviceContext*>(hwdeviceref->data); hwdevicecontext != nullptr)
259 if (vaapidevicectx = reinterpret_cast<AVVAAPIDeviceContext*>(hwdevicecontext->hwctx); !vaapidevicectx)
260 return -1;
261
262 // Set the display
263 vaapidevicectx->display = interop->GetDisplay();
264
265 // Initialise hardware device context
266 int res = av_hwdevice_ctx_init(hwdeviceref);
267 if (res < 0)
268 {
269 LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to initialise VAAPI hardware context");
270 av_buffer_unref(&hwdeviceref);
271 interop->DecrRef();
272 return res;
273 }
274
275 // Allocate the hardware frames context for FFmpeg
276 Context->hw_frames_ctx = av_hwframe_ctx_alloc(hwdeviceref);
277 if (!Context->hw_frames_ctx)
278 {
279 LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to create VAAPI hardware frames context");
280 av_buffer_unref(&hwdeviceref);
281 interop->DecrRef();
282 return -1;
283 }
284
285 // Setup the frames context
286 auto * hw_frames_ctx = reinterpret_cast<AVHWFramesContext*>(Context->hw_frames_ctx->data);
287 auto * vaapi_frames_ctx = reinterpret_cast<AVVAAPIFramesContext*>(hw_frames_ctx->hwctx);
288
289 // Workarounds for specific drivers, surface formats and codecs
290 // NV12 seems to work best across GPUs and codecs with the exception of
291 // MPEG2 on Ironlake where it seems to return I420 labelled as NV12. I420 is
292 // buggy on Sandybridge (stride?) and produces a mixture of I420/NV12 frames
293 // for H.264 on Ironlake.
294 // This may need extending for AMD etc
295
296 auto vendor = interop->GetVendor();
297 // Intel NUC
298 if (vendor.contains("iHD", Qt::CaseInsensitive) && vendor.contains("Intel", Qt::CaseInsensitive))
299 {
300 vaapi_frames_ctx->attributes = nullptr;
301 vaapi_frames_ctx->nb_attributes = 0;
302 }
303 // i965 series
304 else
305 {
306 int format = VA_FOURCC_NV12;
307 if (vendor.contains("ironlake", Qt::CaseInsensitive))
308 if (CODEC_IS_MPEG(Context->codec_id))
309 format = VA_FOURCC_I420;
310
311 if (format != VA_FOURCC_NV12)
312 {
313 auto vaapiid = static_cast<MythCodecID>(kCodec_MPEG1_VAAPI + (mpeg_version(Context->codec_id) - 1));
314 LOG(VB_GENERAL, LOG_INFO, LOC + QString("Forcing surface format for %1 and %2 with driver '%3'")
315 .arg(toString(vaapiid), MythOpenGLInterop::TypeToString(interop->GetType()), vendor));
316 }
317
318 std::array<VASurfaceAttrib,3> prefs {{
319 { VASurfaceAttribPixelFormat, VA_SURFACE_ATTRIB_SETTABLE, { VAGenericValueTypeInteger, { format } } },
320 { VASurfaceAttribUsageHint, VA_SURFACE_ATTRIB_SETTABLE, { VAGenericValueTypeInteger, { VA_SURFACE_ATTRIB_USAGE_HINT_DISPLAY } } },
321 { VASurfaceAttribMemoryType, VA_SURFACE_ATTRIB_SETTABLE, { VAGenericValueTypeInteger, { VA_SURFACE_ATTRIB_MEM_TYPE_VA} } } }};
322 vaapi_frames_ctx->attributes = prefs.data();
323 vaapi_frames_ctx->nb_attributes = 3;
324 }
325
326 hw_frames_ctx->sw_format = FramesFormat(Context->sw_pix_fmt);
327 int referenceframes = AvFormatDecoder::GetMaxReferenceFrames(Context);
328 hw_frames_ctx->initial_pool_size = static_cast<int>(VideoBuffers::GetNumBuffers(FMT_VAAPI, referenceframes, true));
329 hw_frames_ctx->format = AV_PIX_FMT_VAAPI;
330 hw_frames_ctx->width = Context->coded_width;
331 hw_frames_ctx->height = Context->coded_height;
332 // The frames context now holds the reference to MythVAAPIInterop
333 hw_frames_ctx->user_opaque = interop;
334 // Set the callback to ensure it is released
335 hw_frames_ctx->free = &MythCodecContext::FramesContextFinished;
336
337 // Initialise hardwar frames context
338 res = av_hwframe_ctx_init(Context->hw_frames_ctx);
339 if (res < 0)
340 {
341 LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to initialise VAAPI frames context");
342 av_buffer_unref(&hwdeviceref);
343 av_buffer_unref(&(Context->hw_frames_ctx));
344 return res;
345 }
346
347 LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("VAAPI FFmpeg buffer pool created with %1 %2x%3 surfaces (%4 references)")
348 .arg(vaapi_frames_ctx->nb_surfaces).arg(Context->coded_width).arg(Context->coded_height)
349 .arg(referenceframes));
350 av_buffer_unref(&hwdeviceref);
351
353
354 return 0;
355}
356
364int MythVAAPIContext::InitialiseContext2(AVCodecContext* Context)
365{
366 if (!Context)
367 return -1;
368
369 auto * device = MythCodecContext::CreateDevice(AV_HWDEVICE_TYPE_VAAPI, nullptr,
370 gCoreContext->GetSetting("VAAPIDevice"));
371 Context->hw_frames_ctx = av_hwframe_ctx_alloc(device);
372 if (!Context->hw_frames_ctx)
373 {
374 LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to create VAAPI hardware frames context");
375 av_buffer_unref(&device);
376 return -1;
377 }
378
379 int referenceframes = AvFormatDecoder::GetMaxReferenceFrames(Context);
380 auto * hw_frames_ctx = reinterpret_cast<AVHWFramesContext*>(Context->hw_frames_ctx->data);
381 auto * vaapi_frames_ctx = reinterpret_cast<AVVAAPIFramesContext*>(hw_frames_ctx->hwctx);
382 hw_frames_ctx->sw_format = FramesFormat(Context->sw_pix_fmt);
383 hw_frames_ctx->format = AV_PIX_FMT_VAAPI;
384 hw_frames_ctx->width = Context->coded_width;
385 hw_frames_ctx->height = Context->coded_height;
386 hw_frames_ctx->initial_pool_size = static_cast<int>(VideoBuffers::GetNumBuffers(FMT_VAAPI, referenceframes));
387 hw_frames_ctx->free = &MythCodecContext::FramesContextFinished;
388 if (av_hwframe_ctx_init(Context->hw_frames_ctx) < 0)
389 {
390 LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to initialise VAAPI frames context");
391 av_buffer_unref(&device);
392 av_buffer_unref(&(Context->hw_frames_ctx));
393 return -1;
394 }
395
396 LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("VAAPI FFmpeg buffer pool created with %1 %2x%3 surfaces (%4 references)")
397 .arg(vaapi_frames_ctx->nb_surfaces).arg(Context->coded_width).arg(Context->coded_height)
398 .arg(referenceframes));
399 av_buffer_unref(&device);
400
402
403 return 0;
404}
405
413QString MythVAAPIContext::HaveVAAPI(bool ReCheck /*= false*/)
414{
415 static QString s_vendor;
416 static bool s_checked = false;
417 if (s_checked && !ReCheck)
418 return s_vendor;
419 s_checked = true;
420
421 auto * context = MythCodecContext::CreateDevice(AV_HWDEVICE_TYPE_VAAPI, nullptr,
422 gCoreContext->GetSetting("VAAPIDevice"));
423 if (context)
424 {
425 auto * hwdevice = reinterpret_cast<AVHWDeviceContext*>(context->data);
426 auto * hwctx = reinterpret_cast<AVVAAPIDeviceContext*>(hwdevice->hwctx);
427 s_vendor = QString(vaQueryVendorString(hwctx->display));
428 if (s_vendor.contains("vdpau", Qt::CaseInsensitive))
429 {
430 s_vendor = QString();
431 LOG(VB_GENERAL, LOG_INFO, LOC + "VAAPI is using a VDPAU backend - ignoring VAAPI");
432 }
433 else if (s_vendor.isEmpty())
434 {
435 LOG(VB_GENERAL, LOG_INFO, LOC + "Unknown VAAPI vendor - ignoring VAAPI");
436 }
437 else
438 {
439 LOG(VB_GENERAL, LOG_INFO, LOC + "Supported/available VAAPI decoders:");
440 const auto & profiles = MythVAAPIContext::GetProfiles();
441 for (const auto & profile : std::as_const(profiles))
442 {
443 if (profile.first != MythCodecContext::MJPEG)
444 {
445 LOG(VB_GENERAL, LOG_INFO, LOC +
447 }
448 }
449 }
450 av_buffer_unref(&context);
451 }
452 else
453 {
454 LOG(VB_GENERAL, LOG_INFO, LOC + "VAAPI functionality checked failed");
455 }
456
457 return s_vendor;
458}
459
461{
462 static QRecursiveMutex lock;
463 static bool s_initialised = false;
464 static VAAPIProfiles s_profiles;
465
466 QMutexLocker locker(&lock);
467 if (s_initialised)
468 return s_profiles;
469 s_initialised = true;
470
471 auto VAToMythProfile = [](VAProfile Profile)
472 {
473 switch (Profile)
474 {
475 case VAProfileMPEG2Simple: return MythCodecContext::MPEG2Simple;
476 case VAProfileMPEG2Main: return MythCodecContext::MPEG2Main;
477 case VAProfileMPEG4Simple: return MythCodecContext::MPEG4Simple;
478 case VAProfileMPEG4AdvancedSimple: return MythCodecContext::MPEG4AdvancedSimple;
479 case VAProfileMPEG4Main: return MythCodecContext::MPEG4Main;
480 case VAProfileH263Baseline: return MythCodecContext::H263;
481 case VAProfileH264ConstrainedBaseline: return MythCodecContext::H264ConstrainedBaseline;
482 case VAProfileH264Main: return MythCodecContext::H264Main;
483 case VAProfileH264High: return MythCodecContext::H264High;
484 case VAProfileVC1Simple: return MythCodecContext::VC1Simple;
485 case VAProfileVC1Main: return MythCodecContext::VC1Main;
486 case VAProfileVC1Advanced: return MythCodecContext::VC1Advanced;
487 case VAProfileVP8Version0_3: return MythCodecContext::VP8;
488 case VAProfileVP9Profile0: return MythCodecContext::VP9_0;
489 case VAProfileVP9Profile2: return MythCodecContext::VP9_2;
490 case VAProfileHEVCMain: return MythCodecContext::HEVCMain;
491 case VAProfileHEVCMain10: return MythCodecContext::HEVCMain10;
492 case VAProfileJPEGBaseline: return MythCodecContext::MJPEG;
493 default: break;
494 }
496 };
497
498 auto * hwdevicectx = MythCodecContext::CreateDevice(AV_HWDEVICE_TYPE_VAAPI, nullptr,
499 gCoreContext->GetSetting("VAAPIDevice"));
500 if(!hwdevicectx)
501 return s_profiles;
502
503 auto * device = reinterpret_cast<AVHWDeviceContext*>(hwdevicectx->data);
504 auto * hwctx = reinterpret_cast<AVVAAPIDeviceContext*>(device->hwctx);
505
506 int profilecount = vaMaxNumProfiles(hwctx->display);
507 auto * profilelist = static_cast<VAProfile*>(av_malloc_array(static_cast<size_t>(profilecount), sizeof(VAProfile)));
508 if (vaQueryConfigProfiles(hwctx->display, profilelist, &profilecount) == VA_STATUS_SUCCESS)
509 {
510 for (auto i = 0; i < profilecount; ++i)
511 {
512 VAProfile profile = profilelist[i];
513 if (profile == VAProfileNone || profile == VAProfileH264StereoHigh || profile == VAProfileH264MultiviewHigh)
514 continue;
515 int count = 0;
516 int entrysize = vaMaxNumEntrypoints(hwctx->display);
517 auto * entrylist = static_cast<VAEntrypoint*>(av_malloc_array(static_cast<size_t>(entrysize), sizeof(VAEntrypoint)));
518 if (vaQueryConfigEntrypoints(hwctx->display, profile, entrylist, &count) == VA_STATUS_SUCCESS)
519 {
520 for (int j = 0; j < count; ++j)
521 {
522 if (entrylist[j] != VAEntrypointVLD)
523 continue;
524
525 QSize minsize;
526 QSize maxsize;
527 VAConfigID config = 0;
528 if (vaCreateConfig(hwctx->display, profile, VAEntrypointVLD, nullptr, 0, &config) != VA_STATUS_SUCCESS)
529 continue;
530
531 uint attrcount = 0;
532 if (vaQuerySurfaceAttributes(hwctx->display, config, nullptr, &attrcount) == VA_STATUS_SUCCESS)
533 {
534 auto * attrlist = static_cast<VASurfaceAttrib*>(av_malloc(attrcount * sizeof(VASurfaceAttrib)));
535 if (vaQuerySurfaceAttributes(hwctx->display, config, attrlist, &attrcount) == VA_STATUS_SUCCESS)
536 {
537 for (uint k = 0; k < attrcount; ++k)
538 {
539 if (attrlist[k].type == VASurfaceAttribMaxWidth)
540 maxsize.setWidth(attrlist[k].value.value.i);
541 if (attrlist[k].type == VASurfaceAttribMaxHeight)
542 maxsize.setHeight(attrlist[k].value.value.i);
543 if (attrlist[k].type == VASurfaceAttribMinWidth)
544 minsize.setWidth(attrlist[k].value.value.i);
545 if (attrlist[k].type == VASurfaceAttribMinHeight)
546 minsize.setHeight(attrlist[k].value.value.i);
547 }
548 }
549 av_freep(reinterpret_cast<void*>(&attrlist));
550 }
551 vaDestroyConfig(hwctx->display, config);
552 s_profiles.append(VAAPIProfile(VAToMythProfile(profile), QPair<QSize,QSize>(minsize, maxsize)));
553 }
554 }
555 av_freep(reinterpret_cast<void*>(&entrylist));
556 }
557 }
558 av_freep(reinterpret_cast<void*>(&profilelist));
559 av_buffer_unref(&hwdevicectx);
560 return s_profiles;
561}
562
563void MythVAAPIContext::GetDecoderList(QStringList& Decoders)
564{
565 const auto & profiles = MythVAAPIContext::GetProfiles();
566 if (profiles.isEmpty())
567 return;
568 Decoders.append("VAAPI:");
569 for (const auto & profile : std::as_const(profiles))
570 if (profile.first != MythCodecContext::MJPEG)
571 Decoders.append(MythCodecContext::GetProfileDescription(profile.first, profile.second.second));
572}
573
574void MythVAAPIContext::InitVideoCodec(AVCodecContext* Context, bool SelectedStream, bool& DirectRendering)
575{
577 {
578 Context->get_buffer2 = MythCodecContext::GetBuffer;
579 Context->get_format = MythVAAPIContext::GetFormat;
580 Context->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
581 return;
582 }
584 {
585 Context->get_format = MythVAAPIContext::GetFormat2;
586 Context->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
587 DirectRendering = false;
588 return;
589 }
590
591 MythCodecContext::InitVideoCodec(Context, SelectedStream, DirectRendering);
592}
593
594bool MythVAAPIContext::RetrieveFrame(AVCodecContext* /*unused*/, MythVideoFrame* Frame, AVFrame* AvFrame)
595{
596 if (AvFrame->format != AV_PIX_FMT_VAAPI)
597 return false;
599 return RetrieveHWFrame(Frame, AvFrame);
600 return false;
601}
602
612{
613 int ret = 0;
614
615 while (true)
616 {
617 if (m_filterGraph && ((m_filterWidth != Context->coded_width) || (m_filterHeight != Context->coded_height)))
618 {
619 LOG(VB_GENERAL, LOG_WARNING, LOC + "Input changed - deleting filter");
621 }
622
623 if (m_filterGraph)
624 {
625 ret = av_buffersink_get_frame(m_filterSink, Frame);
626 if (ret >= 0)
627 {
629 {
630 Frame->pts = m_filterPriorPTS[1] + ((m_filterPriorPTS[1] - m_filterPriorPTS[0]) / 2);
631 av_frame_remove_side_data(Frame, AV_FRAME_DATA_A53_CC);
632 }
633 else
634 {
635 Frame->pts = m_filterPriorPTS[1];
637 }
638 }
639 if (ret != AVERROR(EAGAIN))
640 break;
641 }
642
643 // EAGAIN or no filter graph
644 ret = avcodec_receive_frame(Context, Frame);
645 if (ret == 0)
646 {
647 // preserve interlaced flags
648 m_lastInterlaced = (Frame->flags & AV_FRAME_FLAG_INTERLACED) != 0;
649 m_lastTopFieldFirst = (Frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) != 0;
650 }
651
652 if (ret < 0)
653 break;
654
655 // some streams with missing timestamps break frame timing when doublerate
656 // so replace with dts if available
657 int64_t pts = Frame->pts;
658 if (pts == AV_NOPTS_VALUE && Frame->pkt_dts != AV_NOPTS_VALUE && m_deinterlacer2x)
659 pts = Frame->pkt_dts;
662
663 if (!m_filterGraph)
664 break;
665
666 ret = av_buffersrc_add_frame(m_filterSource, Frame);
667 if (ret < 0)
668 break;
669 }
670
671 return ret;
672}
673
675{
676 if (!Frame || !codec_is_vaapi_dec(m_codecID) || !Context->hw_frames_ctx)
677 return;
678
679 // if VAAPI driver deints are errored or not available (older boards), then
680 // allow CPU/GLSL
681 if (m_filterError)
682 {
683 Frame->m_deinterlaceAllowed = Frame->m_deinterlaceAllowed & ~DEINT_DRIVER;
684 return;
685 }
688 {
689 // enabling VPP deinterlacing with these codecs breaks decoding for some reason.
690 // HEVC interlacing is not currently detected by FFmpeg and I can't find
691 // any interlaced VP8/9 material. Shaders and/or CPU deints will be available
692 // as appropriate
693 m_filterError = true;
694 LOG(VB_GENERAL, LOG_INFO, LOC + QString("Disabling VAAPI VPP deinterlacer for %1")
695 .arg(toString(m_codecID)));
696 return;
697 }
698
699 // if this frame has already been deinterlaced, then flag the deinterlacer and
700 // that the frame has already been deinterlaced.
701 // the FFmpeg vaapi deint filter will mark frames as progressive, so restore the
702 // interlaced flags to ensure auto deinterlacing continues to work
703 if (m_deinterlacer)
704 {
705 Frame->m_interlaced = m_lastInterlaced;
706 Frame->m_topFieldFirst = m_lastTopFieldFirst;
707 Frame->m_deinterlaceInuse = m_deinterlacer | DEINT_DRIVER;
708 Frame->m_deinterlaceInuse2x = m_deinterlacer2x;
709 Frame->m_alreadyDeinterlaced = true;
710 }
711
712 // N.B. this picks up the scan tracking in MythPlayer. So we can
713 // auto enable deinterlacing etc and override Progressive/Interlaced - but
714 // no reversed interlaced.
715 MythDeintType vaapideint = DEINT_NONE;
716 MythDeintType singlepref = Frame->GetSingleRateOption(DEINT_DRIVER);
717 MythDeintType doublepref = Frame->GetDoubleRateOption(DEINT_DRIVER);
718 bool doublerate = true;
719 bool other = false;
720
721 // For decode only, a CPU or shader deint may also be used/preferred
722 if (doublepref)
723 vaapideint = doublepref;
724 else if (Frame->GetDoubleRateOption(DEINT_CPU | DEINT_SHADER))
725 other = true;
726
727 if (!vaapideint && !other && singlepref)
728 {
729 doublerate = false;
730 vaapideint = singlepref;
731 }
732
733 // nothing to see
734 if (vaapideint == DEINT_NONE)
735 {
736 if (m_deinterlacer)
738 return;
739 }
740
741 // already setup
742 if ((m_deinterlacer == vaapideint) && (m_deinterlacer2x == doublerate))
743 return;
744
745 // Start from scratch
747 m_framesCtx = av_buffer_ref(Context->hw_frames_ctx);
748 if (!MythVAAPIInterop::SetupDeinterlacer(vaapideint, doublerate, Context->hw_frames_ctx,
749 Context->coded_width, Context->coded_height,
751 {
752 LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to create deinterlacer %1 - disabling")
753 .arg(MythVideoFrame::DeinterlacerName(vaapideint | DEINT_DRIVER, doublerate, FMT_VAAPI)));
755 m_filterError = true;
756 }
757 else
758 {
759 m_deinterlacer = vaapideint;
760 m_deinterlacer2x = doublerate;
761 m_filterWidth = Context->coded_width;
762 m_filterHeight = Context->coded_height;
763 }
764}
765
766bool MythVAAPIContext::IsDeinterlacing(bool& DoubleRate, bool StreamChange)
767{
768 // the VAAPI deinterlacer can be turned on and off, so on stream changes
769 // return false to ensure auto deint works for the new format (the deinterlacer
770 // refers to the current format)
771 if (m_deinterlacer && !StreamChange)
772 {
773 DoubleRate = m_deinterlacer2x;
774 return true;
775 }
776 DoubleRate = false;
777 return false;
778}
779
781{
782 // HEVC appears to be OK
784}
785
787{
788 // Only MPEG2 tested so far
790}
791
793{
794 if (m_filterGraph)
795 LOG(VB_GENERAL, LOG_INFO, LOC + "Destroying VAAPI deinterlacer");
796 avfilter_graph_free(&m_filterGraph);
797 m_filterGraph = nullptr;
798 m_filterSink = nullptr;
799 m_filterSource = nullptr;
800 m_filterPTSUsed = 0;
801 m_filterPriorPTS.fill(0);
802 m_filterWidth = 0;
803 m_filterHeight = 0;
804 if (m_framesCtx)
805 av_buffer_unref(&m_framesCtx);
807 m_deinterlacer2x = false;
808}
AVFrame AVFrame
static int GetMaxReferenceFrames(AVCodecContext *Context)
static int InitialiseDecoder(AVCodecContext *Context, CreateHWDecoder Callback, const QString &Debug)
Initialise a hardware decoder that is expected to use AVHWFramesContext.
static bool FrameTypeIsSupported(AVCodecContext *Context, VideoFrameType Format)
static int GetBuffer(struct AVCodecContext *Context, AVFrame *Frame, int Flags)
A generic hardware buffer initialisation method when using AVHWFramesContext.
virtual bool RetrieveHWFrame(MythVideoFrame *Frame, AVFrame *AvFrame)
static AVBufferRef * CreateDevice(AVHWDeviceType Type, MythInteropGPU *Interop, const QString &Device=QString())
virtual void InitVideoCodec(AVCodecContext *Context, bool SelectedStream, bool &DirectRendering)
static void FramesContextFinished(AVHWFramesContext *Context)
static void NewHardwareFramesContext(void)
Track the number of concurrent frames contexts.
static QString GetProfileDescription(CodecProfile Profile, QSize Size, VideoFrameType Format=FMT_NONE, uint ColorDepth=0)
static MythPlayerUI * GetPlayerUI(AVCodecContext *Context)
MythCodecID m_codecID
static CodecProfile FFmpegToMythProfile(AVCodecID CodecID, int Profile)
QString GetSetting(const QString &key, const QString &defaultval="")
InteropType GetType()
static QString TypeToString(InteropType Type)
static int InitialiseContext2(AVCodecContext *Context)
Create a VAAPI hardware context without OpenGL interop.
~MythVAAPIContext() override
static AVPixelFormat FramesFormat(AVPixelFormat Format)
static QString HaveVAAPI(bool ReCheck=false)
Check whether VAAPI is available and not emulated via VDPAU.
std::array< int64_t, 2 > m_filterPriorPTS
bool IsDeinterlacing(bool &DoubleRate, bool StreamChange=false) override
static int InitialiseContext(AVCodecContext *Context)
Create a VAAPI hardware context with appropriate OpenGL interop.
static VAProfile VAAPIProfileForCodec(const AVCodecContext *Codec)
MythDeintType m_deinterlacer
static const VAAPIProfiles & GetProfiles()
static enum AVPixelFormat GetFormat(AVCodecContext *Context, const AVPixelFormat *PixFmt)
AVFilterContext * m_filterSource
void PostProcessFrame(AVCodecContext *Context, MythVideoFrame *Frame) override
AVFilterGraph * m_filterGraph
static MythCodecID GetSupportedCodec(AVCodecContext **Context, const AVCodec **Codec, const QString &Decoder, uint StreamType)
Confirm whether VAAPI support is available given Decoder and Context.
MythVAAPIContext(DecoderBase *Parent, MythCodecID CodecID)
static enum AVPixelFormat GetFormat2(AVCodecContext *Context, const AVPixelFormat *PixFmt)
bool RetrieveFrame(AVCodecContext *Context, MythVideoFrame *Frame, AVFrame *AvFrame) override
bool DecoderWillResetOnFlush() override
int FilteredReceiveFrame(AVCodecContext *Context, AVFrame *Frame) override
Retrieve decoded frame and optionally deinterlace.
bool DecoderWillResetOnAspect() override
AVFilterContext * m_filterSink
AVBufferRef * m_framesCtx
static void GetDecoderList(QStringList &Decoders)
void InitVideoCodec(AVCodecContext *Context, bool SelectedStream, bool &DirectRendering) override
static bool SetupDeinterlacer(MythDeintType Deinterlacer, bool DoubleRate, AVBufferRef *FramesContext, int Width, int Height, AVFilterGraph *&Graph, AVFilterContext *&Source, AVFilterContext *&Sink)
QString GetVendor(void)
static MythVAAPIInterop * CreateVAAPI(MythPlayerUI *Player, MythRenderOpenGL *Context)
VADisplay GetDisplay(void)
static QString DeinterlacerName(MythDeintType Deint, bool DoubleRate, VideoFrameType Format=FMT_NONE)
Definition: mythframe.cpp:462
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
static uint GetNumBuffers(int PixelFormat, int MaxReferenceFrames=16, bool Decoder=false)
unsigned int uint
Definition: compat.h:68
uint mpeg_version(AVCodecID codec_id)
MythCodecID
Definition: mythcodecid.h:14
@ kCodec_VP8_VAAPI_DEC
Definition: mythcodecid.h:95
@ kCodec_H264_VAAPI
Definition: mythcodecid.h:76
@ kCodec_MPEG1_VAAPI_DEC
Definition: mythcodecid.h:88
@ kCodec_MPEG1
Definition: mythcodecid.h:24
@ kCodec_MPEG2_VAAPI
Definition: mythcodecid.h:73
@ kCodec_MPEG1_VAAPI
Definition: mythcodecid.h:72
@ kCodec_HEVC_VAAPI_DEC
Definition: mythcodecid.h:97
@ kCodec_VP9_VAAPI_DEC
Definition: mythcodecid.h:96
@ kCodec_MPEG2_VAAPI_DEC
Definition: mythcodecid.h:89
static bool CODEC_IS_MPEG(AVCodecID id)
Definition: mythcodecid.h:386
static bool codec_is_vaapi(MythCodecID id)
Definition: mythcodecid.h:321
static bool codec_is_vaapi_dec(MythCodecID id)
Definition: mythcodecid.h:324
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythDeintType
Definition: mythframe.h:67
@ DEINT_DRIVER
Definition: mythframe.h:74
@ DEINT_NONE
Definition: mythframe.h:68
@ DEINT_SHADER
Definition: mythframe.h:73
@ DEINT_CPU
Definition: mythframe.h:72
@ FMT_VAAPI
Definition: mythframe.h:57
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
#define LOC
QVector< VAAPIProfile > VAAPIProfiles
QPair< MythCodecContext::CodecProfile, QPair< QSize, QSize > > VAAPIProfile
#define VA_FOURCC_I420
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:93
std::chrono::duration< CHRONO_TYPE, std::ratio< 1, 90000 > > pts
Definition: mythchrono.h:55