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