MythTV  master
mythnvdeccontext.cpp
Go to the documentation of this file.
1 // MythTV
2 #include "mythlogging.h"
3 #include "mythmainwindow.h"
4 #include "avformatdecoder.h"
5 #include "mythnvdecinterop.h"
6 #include "mythvideoout.h"
7 #include "mythplayerui.h"
8 #include "mythnvdeccontext.h"
9 
10 extern "C" {
11 #include "libavutil/opt.h"
12 }
13 
14 #define LOC QString("NVDEC: ")
15 
16 
18  : MythCodecContext(Parent, CodecID)
19 {
20 }
21 
23 {
24  av_buffer_unref(&m_framesContext);
25 }
26 
33  AVCodec **Codec,
34  const QString &Decoder,
35  AVStream *Stream,
36  uint StreamType)
37 {
38  bool decodeonly = Decoder == "nvdec-dec";
39  auto success = static_cast<MythCodecID>((decodeonly ? kCodec_MPEG1_NVDEC_DEC : kCodec_MPEG1_NVDEC) + (StreamType - 1));
40  auto failure = static_cast<MythCodecID>(kCodec_MPEG1 + (StreamType - 1));
41 
42  // no brainers
43  if (!Decoder.startsWith("nvdec") || qEnvironmentVariableIsSet("NO_NVDEC") || !HaveNVDEC() || IsUnsupportedProfile(*Context))
44  return failure;
45 
46  if (!decodeonly)
47  if (!FrameTypeIsSupported(*Context, FMT_NVDEC))
48  return failure;
49 
50  QString codecstr = ff_codec_id_string((*Context)->codec_id);
51  QString profile = avcodec_profile_name((*Context)->codec_id, (*Context)->profile);
52  QString pixfmt = av_get_pix_fmt_name((*Context)->pix_fmt);
53 
54  cudaVideoCodec cudacodec = cudaVideoCodec_NumCodecs;
55  switch ((*Context)->codec_id)
56  {
57  case AV_CODEC_ID_MPEG1VIDEO: cudacodec = cudaVideoCodec_MPEG1; break;
58  case AV_CODEC_ID_MPEG2VIDEO: cudacodec = cudaVideoCodec_MPEG2; break;
59  case AV_CODEC_ID_MPEG4: cudacodec = cudaVideoCodec_MPEG4; break;
60  case AV_CODEC_ID_VC1: cudacodec = cudaVideoCodec_VC1; break;
61  case AV_CODEC_ID_H264: cudacodec = cudaVideoCodec_H264; break;
62  case AV_CODEC_ID_HEVC: cudacodec = cudaVideoCodec_HEVC; break;
63  case AV_CODEC_ID_VP8: cudacodec = cudaVideoCodec_VP8; break;
64  case AV_CODEC_ID_VP9: cudacodec = cudaVideoCodec_VP9; break;
65  default: break;
66  }
67 
68  cudaVideoChromaFormat cudaformat = cudaVideoChromaFormat_Monochrome;
70  uint depth = static_cast<uint>(MythVideoFrame::ColorDepth(type) - 8);
71  QString desc = QString("'%1 %2 %3 Depth:%4 %5x%6'")
72  .arg(codecstr).arg(profile).arg(pixfmt).arg(depth + 8)
73  .arg((*Context)->width).arg((*Context)->height);
74 
75  // N.B. on stream changes format is set to CUDA/NVDEC. This may break if the new
76  // stream has an unsupported chroma but the decoder should fail gracefully - just later.
78  cudaformat = cudaVideoChromaFormat_420;
80  cudaformat = cudaVideoChromaFormat_422;
82  cudaformat = cudaVideoChromaFormat_444;
83 
84  if ((cudacodec == cudaVideoCodec_NumCodecs) || (cudaformat == cudaVideoChromaFormat_Monochrome))
85  {
86  LOG(VB_PLAYBACK, LOG_DEBUG, LOC + "Unknown codec or format");
87  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("NVDEC does NOT support %1").arg(desc));
88  return failure;
89  }
90 
91  // iterate over known decoder capabilities
92  const auto & profiles = MythNVDECContext::GetProfiles();
93  auto capcheck = [&](const MythNVDECCaps& Cap)
94  { return Cap.Supports(cudacodec, cudaformat, depth, (*Context)->width, (*Context)->height); };
95  if (!std::any_of(profiles.cbegin(), profiles.cend(), capcheck))
96  {
97  LOG(VB_PLAYBACK, LOG_DEBUG, LOC + "No matching profile support");
98  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("NVDEC does NOT support %1").arg(desc));
99  return failure;
100  }
101 
102  auto *decoder = dynamic_cast<AvFormatDecoder*>(reinterpret_cast<DecoderBase*>((*Context)->opaque));
103  // and finally try and retrieve the actual FFmpeg decoder
104  QString name = QString((*Codec)->name) + "_cuvid";
105  if (name == "mpeg2video_cuvid")
106  name = "mpeg2_cuvid";
107  for (int i = 0; ; i++)
108  {
109  const AVCodecHWConfig *config = avcodec_get_hw_config(*Codec, i);
110  if (!config)
111  break;
112 
113  if ((config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) &&
114  (config->device_type == AV_HWDEVICE_TYPE_CUDA))
115  {
116  AVCodec *codec = avcodec_find_decoder_by_name(name.toLocal8Bit());
117  if (codec)
118  {
119  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("NVDEC supports decoding %1").arg(desc));
120  *Codec = codec;
121  decoder->CodecMap()->FreeCodecContext(Stream);
122  *Context = decoder->CodecMap()->GetCodecContext(Stream, *Codec);
123  return success;
124  }
125  break;
126  }
127  }
128 
129  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to find decoder '%1'").arg(name));
130  return failure;
131 }
132 
133 int MythNVDECContext::InitialiseDecoder(AVCodecContext *Context)
134 {
135  if (!gCoreContext->IsUIThread() || !Context)
136  return -1;
137 
138  // We need a player to release the interop. As we are using direct rendering
139  // it must be a MythPlayerUI instance
140  auto * player = GetPlayerUI(Context);
141  if (!player)
142  return -1;
143 
144  // Retrieve OpenGL render context
145  auto * render = dynamic_cast<MythRenderOpenGL*>(player->GetRender());
146  if (!render)
147  return -1;
148  OpenGLLocker locker(render);
149 
150  // Create interop (and CUDA context)
151  auto * interop = MythNVDECInterop::CreateNVDEC(player, render);
152  if (!interop)
153  return -1;
154  if (!interop->IsValid())
155  {
156  interop->DecrRef();
157  return -1;
158  }
159 
160  // Allocate the device context
161  AVBufferRef* hwdeviceref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA);
162  if (!hwdeviceref)
163  {
164  interop->DecrRef();
165  return -1;
166  }
167 
168  // Set release method, interop and CUDA context
169  auto* hwdevicecontext = reinterpret_cast<AVHWDeviceContext*>(hwdeviceref->data);
170  hwdevicecontext->free = &MythCodecContext::DeviceContextFinished;
171  hwdevicecontext->user_opaque = interop;
172  auto *devicehwctx = reinterpret_cast<AVCUDADeviceContext*>(hwdevicecontext->hwctx);
173  devicehwctx->cuda_ctx = interop->GetCUDAContext();
174  devicehwctx->stream = nullptr;
175 
176  if (av_hwdevice_ctx_init(hwdeviceref) < 0)
177  {
178  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to init CUDA hw device");
179  av_buffer_unref(&hwdeviceref);
180  return -1;
181  }
182 
183  Context->hw_device_ctx = hwdeviceref;
184  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Created CUDA device context");
185  return 0;
186 }
187 
188 void MythNVDECContext::InitVideoCodec(AVCodecContext *Context, bool SelectedStream, bool &DirectRendering)
189 {
191  {
192  Context->get_format = MythNVDECContext::GetFormat;
193  DirectRendering = false;
194  return;
195  }
196 
197  MythCodecContext::InitVideoCodec(Context, SelectedStream, DirectRendering);
198 }
199 
200 int MythNVDECContext::HwDecoderInit(AVCodecContext *Context)
201 {
203  {
205  "Create NVDEC decoder");
206  }
208  {
209  AVBufferRef *context = MythCodecContext::CreateDevice(AV_HWDEVICE_TYPE_CUDA, nullptr,
210  gCoreContext->GetSetting("NVDECDevice"));
211  if (context)
212  {
213  Context->hw_device_ctx = context;
214  return 0;
215  }
216  }
217  return -1;
218 }
219 
226 void MythNVDECContext::SetDeinterlacing(AVCodecContext *Context,
227  MythVideoProfile *Profile, bool DoubleRate)
228 {
229  if (!Context)
230  return;
231 
232  // Don't enable for anything that cannot be interlaced
233  // We could use frame rate here but initial frame rate detection is not always accurate
234  // and we lose little by enabling deinterlacing. NVDEC will not deinterlace a
235  // progressive stream and any CUDA capable video card has memory to spare
236  // (assuming it even sets up deinterlacing for a progressive stream)
237  if (Context->height == 720) // 720P
238  return;
239 
240  MythDeintType deinterlacer = DEINT_NONE;
241  MythDeintType singlepref = DEINT_NONE;
242  MythDeintType doublepref = DEINT_NONE;
243  if (Profile)
244  {
246  if (DoubleRate)
248  }
249 
250  // Deinterlacers are not filtered (as we have no frame) - so mask appropriately
251  MythDeintType singledeint = singlepref & (DEINT_BASIC | DEINT_MEDIUM | DEINT_HIGH);
252  MythDeintType doubledeint = doublepref & (DEINT_BASIC | DEINT_MEDIUM | DEINT_HIGH);
253 
254  // With decode only/copy back, we can deinterlace here or the frames can use
255  // either CPU or shader deints. So only accept a driver deinterlacer preference.
256 
257  // When direct rendering, there is no CPU option but shaders can be used.
258  // Assume driver deints are higher quality so accept a driver preference or CPU
259  // if shader is not preferred
260  bool other = false;
262  {
263  if (DoubleRate)
264  {
265  if (doubledeint)
266  {
267  if (doublepref & (DEINT_DRIVER))
268  deinterlacer = doubledeint;
269  else if (doublepref & (DEINT_CPU | DEINT_SHADER))
270  other = true;
271  }
272  else
273  {
274  DoubleRate = false;
275  }
276  }
277 
278  if (!deinterlacer && !other && (singlepref & DEINT_DRIVER))
279  deinterlacer = singledeint;
280  }
281  else if (codec_is_nvdec(m_codecID))
282  {
283  if (DoubleRate)
284  {
285  if (doubledeint)
286  {
287  if (doublepref & (DEINT_DRIVER | DEINT_CPU))
288  deinterlacer = doubledeint;
289  else if (doublepref & DEINT_SHADER)
290  other = true;
291  }
292  else
293  {
294  DoubleRate = false;
295  }
296  }
297 
298  if (!deinterlacer && !other && singledeint)
299  {
300  if (singlepref & (DEINT_DRIVER | DEINT_CPU))
301  deinterlacer = singledeint;
302  else if (singlepref & DEINT_SHADER)
303  { }
304  }
305  }
306 
307  if (deinterlacer == DEINT_NONE)
308  return;
309 
310  QString mode = "adaptive";
311  if (DEINT_BASIC == deinterlacer)
312  mode = "bob";
313  int result = av_opt_set(Context->priv_data, "deint", mode.toLocal8Bit(), 0);
314  if (result == 0)
315  {
316  if (av_opt_set_int(Context->priv_data, "drop_second_field", static_cast<int>(!DoubleRate), 0) == 0)
317  {
318  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Setup decoder deinterlacer '%1'")
319  .arg(MythVideoFrame::DeinterlacerName(deinterlacer | DEINT_DRIVER, DoubleRate, FMT_NVDEC)));
320  m_deinterlacer = deinterlacer;
321  m_deinterlacer2x = DoubleRate;
322  }
323  }
324 }
325 
326 void MythNVDECContext::PostProcessFrame(AVCodecContext* /*Context*/, MythVideoFrame *Frame)
327 {
328  // Remove interlacing flags and set deinterlacer if necessary
329  if (Frame && m_deinterlacer)
330  {
331  Frame->m_interlaced = 0;
332  Frame->m_interlacedReverse = false;
333  Frame->m_topFieldFirst = false;
334  Frame->m_deinterlaceInuse = m_deinterlacer | DEINT_DRIVER;
335  Frame->m_deinterlaceInuse2x = m_deinterlacer2x;
336  Frame->m_alreadyDeinterlaced = true;
337  }
338 }
339 
341 {
342  return codec_is_nvdec(m_codecID);
343 }
344 
345 bool MythNVDECContext::IsDeinterlacing(bool &DoubleRate, bool /*unused*/)
346 {
347  if (m_deinterlacer != DEINT_NONE)
348  {
349  DoubleRate = m_deinterlacer2x;
350  return true;
351  }
352  DoubleRate = false;
353  return false;
354 }
355 
356 enum AVPixelFormat MythNVDECContext::GetFormat(AVCodecContext* Context, const AVPixelFormat *PixFmt)
357 {
358  while (*PixFmt != AV_PIX_FMT_NONE)
359  {
360  if (*PixFmt == AV_PIX_FMT_CUDA)
361  {
362  auto * decoder = reinterpret_cast<AvFormatDecoder*>(Context->opaque);
363  if (decoder)
364  {
365  auto * me = dynamic_cast<MythNVDECContext*>(decoder->GetMythCodecContext());
366  if (me)
367  me->InitFramesContext(Context);
368  }
369  return AV_PIX_FMT_CUDA;
370  }
371  PixFmt++;
372  }
373  return AV_PIX_FMT_NONE;
374 }
375 
376 bool MythNVDECContext::RetrieveFrame(AVCodecContext *Context, MythVideoFrame *Frame, AVFrame *AvFrame)
377 {
378  if (AvFrame->format != AV_PIX_FMT_CUDA)
379  return false;
381  return RetrieveHWFrame(Frame, AvFrame);
383  return GetBuffer(Context, Frame, AvFrame, 0);
384  return false;
385 }
386 
392 bool MythNVDECContext::GetBuffer(struct AVCodecContext *Context, MythVideoFrame *Frame,
393  AVFrame *AvFrame, int /*Flags*/)
394 {
395  if ((AvFrame->format != AV_PIX_FMT_CUDA) || !AvFrame->data[0])
396  {
397  LOG(VB_GENERAL, LOG_ERR, LOC + "Not a valid CUDA hw frame");
398  return false;
399  }
400 
401  if (!Context || !Frame)
402  return false;
403 
404  auto *decoder = static_cast<AvFormatDecoder*>(Context->opaque);
405 
406  for (int i = 0; i < 3; i++)
407  {
408  Frame->m_pitches[i] = AvFrame->linesize[i];
409  Frame->m_offsets[i] = AvFrame->data[i] ? (static_cast<int>(AvFrame->data[i] - AvFrame->data[0])) : 0;
410  Frame->m_priv[i] = nullptr;
411  }
412 
413  Frame->m_width = AvFrame->width;
414  Frame->m_height = AvFrame->height;
415  Frame->m_pixFmt = Context->pix_fmt;
416  Frame->m_directRendering = true;
417 
418  AvFrame->opaque = Frame;
419  AvFrame->reordered_opaque = Context->reordered_opaque;
420 
421  // set the pixel format - normally NV12 but P010 for 10bit etc. Set here rather than guessing later.
422  if (AvFrame->hw_frames_ctx)
423  {
424  auto *context = reinterpret_cast<AVHWFramesContext*>(AvFrame->hw_frames_ctx->data);
425  if (context)
426  Frame->m_swPixFmt = context->sw_format;
427  }
428 
429  // NVDEC 'fixes' 10/12/16bit colour values
430  Frame->m_colorshifted = true;
431 
432  // Frame->data[0] holds CUdeviceptr for the frame data - offsets calculated above
433  Frame->m_buffer = AvFrame->data[0];
434 
435  // Retain the buffer so it is not released before we display it
436  Frame->m_priv[0] = reinterpret_cast<unsigned char*>(av_buffer_ref(AvFrame->buf[0]));
437 
438  // We need the CUDA device context in the interop class and it also holds the reference
439  // to the interop class itself
440  Frame->m_priv[1] = reinterpret_cast<unsigned char*>(av_buffer_ref(Context->hw_device_ctx));
441 
442  // Set the release method
443  AvFrame->buf[1] = av_buffer_create(reinterpret_cast<uint8_t*>(Frame), 0,
444  MythCodecContext::ReleaseBuffer, decoder, 0);
445  return true;
446 }
447 
448 MythNVDECContext::MythNVDECCaps::MythNVDECCaps(cudaVideoCodec Codec, uint Depth, cudaVideoChromaFormat Format,
449  QSize Minimum, QSize Maximum, uint MacroBlocks)
450  : m_codec(Codec),
451  m_depth(Depth),
452  m_format(Format),
453  m_minimum(Minimum),
454  m_maximum(Maximum),
455  m_macroBlocks(MacroBlocks)
456 {
457  auto ToMythProfile = [](cudaVideoCodec CudaCodec)
458  {
459  switch (CudaCodec)
460  {
461  case cudaVideoCodec_MPEG1: return MythCodecContext::MPEG1;
462  case cudaVideoCodec_MPEG2: return MythCodecContext::MPEG2;
463  case cudaVideoCodec_MPEG4: return MythCodecContext::MPEG4;
464  case cudaVideoCodec_VC1: return MythCodecContext::VC1;
465  case cudaVideoCodec_H264: return MythCodecContext::H264;
466  case cudaVideoCodec_HEVC: return MythCodecContext::HEVC;
467  case cudaVideoCodec_VP8: return MythCodecContext::VP8;
468  case cudaVideoCodec_VP9: return MythCodecContext::VP9;
469  default: break;
470  }
472  };
473 
474  auto ToMythFormat = [](cudaVideoChromaFormat CudaFormat)
475  {
476  switch (CudaFormat)
477  {
478  case cudaVideoChromaFormat_420: return FMT_YV12;
479  case cudaVideoChromaFormat_422: return FMT_YUV422P;
480  case cudaVideoChromaFormat_444: return FMT_YUV444P;
481  default: break;
482  }
483  return FMT_NONE;
484  };
485  m_profile = ToMythProfile(m_codec);
486  m_type = ToMythFormat(m_format);
487 }
488 
489 bool MythNVDECContext::MythNVDECCaps::Supports(cudaVideoCodec Codec, cudaVideoChromaFormat Format,
490  uint Depth, int Width, int Height) const
491 {
492  uint mblocks = static_cast<uint>((Width * Height) / 256);
493 
494  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
495  QString("Trying to match: Codec %1 Format %2 Depth %3 Width %4 Height %5 MBs %6")
496  .arg(Codec).arg(Format).arg(Depth).arg(Width).arg(Height).arg(mblocks));
497  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
498  QString("to this profile: Codec %1 Format %2 Depth %3 Width %4<->%5 Height %6<->%7 MBs %8")
499  .arg(m_codec).arg(m_format).arg(m_depth)
500  .arg(m_minimum.width()).arg(m_maximum.width())
501  .arg(m_minimum.height()).arg(m_maximum.height()).arg(m_macroBlocks));
502 
503  bool result = (Codec == m_codec) && (Format == m_format) && (Depth == m_depth) &&
504  (m_maximum.width() >= Width) && (m_maximum.height() >= Height) &&
505  (m_minimum.width() <= Width) && (m_minimum.height() <= Height) &&
506  (m_macroBlocks >= mblocks);
507 
508  LOG(VB_PLAYBACK, LOG_DEBUG, LOC + QString("%1 Match").arg(result ? "" : "NO"));
509  return result;
510 }
511 
512 bool MythNVDECContext::HaveNVDEC(bool Reinit /*=false*/)
513 {
514  static QMutex lock(QMutex::Recursive);
515  QMutexLocker locker(&lock);
516  static bool s_checked = false;
517  static bool s_available = false;
518  if (!s_checked || Reinit)
519  {
520  if (gCoreContext->IsUIThread())
521  {
522  const std::vector<MythNVDECCaps>& profiles = MythNVDECContext::GetProfiles();
523  if (profiles.empty())
524  {
525  LOG(VB_GENERAL, LOG_INFO, LOC + "No NVDEC decoders found");
526  }
527  else
528  {
529  s_available = true;
530  LOG(VB_GENERAL, LOG_INFO, LOC + "Supported/available NVDEC decoders:");
531  for (auto profile : profiles)
532  {
533  QString desc = MythCodecContext::GetProfileDescription(profile.m_profile,profile.m_maximum,
534  profile.m_type, profile.m_depth + 8);
535  LOG(VB_GENERAL, LOG_INFO, LOC + desc + QString(" MBs: %1").arg(profile.m_macroBlocks));
536  }
537  }
538  }
539  else
540  {
541  LOG(VB_GENERAL, LOG_WARNING, LOC + "HaveNVDEC must be initialised from the main thread");
542  }
543  }
544  s_checked = true;
545  return s_available;
546 }
547 
548 
549 void MythNVDECContext::GetDecoderList(QStringList &Decoders)
550 {
551  const std::vector<MythNVDECCaps>& profiles = MythNVDECContext::GetProfiles();
552  if (profiles.empty())
553  return;
554  Decoders.append("NVDEC:");
555  for (auto profile : profiles)
556  {
557  if (!(profile.m_depth % 2)) // Ignore 9/11bit etc
558  Decoders.append(MythCodecContext::GetProfileDescription(profile.m_profile, profile.m_maximum,
559  profile.m_type, profile.m_depth + 8));
560  }
561 }
562 
563 const std::vector<MythNVDECContext::MythNVDECCaps> &MythNVDECContext::GetProfiles(void)
564 {
565  static QMutex lock(QMutex::Recursive);
566  static bool s_initialised = false;
567  static std::vector<MythNVDECContext::MythNVDECCaps> s_profiles;
568 
569  QMutexLocker locker(&lock);
570  if (s_initialised)
571  return s_profiles;
572  s_initialised = true;
573 
575  CUcontext context = nullptr;
576  CudaFunctions *cuda = nullptr;
577  if (MythNVDECInterop::CreateCUDAContext(opengl, cuda, context))
578  {
579  OpenGLLocker gllocker(opengl);
580  CuvidFunctions *cuvid = nullptr;
581  CUcontext dummy = nullptr;
582  cuda->cuCtxPushCurrent(context);
583 
584  if (cuvid_load_functions(&cuvid, nullptr) == 0)
585  {
586  // basic check passed
587  if (!cuvid->cuvidGetDecoderCaps)
588  LOG(VB_GENERAL, LOG_WARNING, LOC + "Old driver - cannot check decoder capabilities");
589 
590  // now iterate over codecs, depths and formats to check support
591  for (int codec = cudaVideoCodec_MPEG1; codec < cudaVideoCodec_NumCodecs; ++codec)
592  {
593  auto cudacodec = static_cast<cudaVideoCodec>(codec);
594  if (cudacodec == cudaVideoCodec_JPEG)
595  continue;
596  for (int format = cudaVideoChromaFormat_420; format < cudaVideoChromaFormat_444; ++format)
597  {
598  auto cudaformat = static_cast<cudaVideoChromaFormat>(format);
599  for (uint depth = 0; depth < 9; ++depth)
600  {
601  CUVIDDECODECAPS caps;
602  caps.eCodecType = cudacodec;
603  caps.eChromaFormat = cudaformat;
604  caps.nBitDepthMinus8 = depth;
605  // N.B. cuvidGetDecoderCaps was not available on older drivers
606  if (cuvid->cuvidGetDecoderCaps && (cuvid->cuvidGetDecoderCaps(&caps) == CUDA_SUCCESS) &&
607  caps.bIsSupported)
608  {
609  s_profiles.emplace_back(
610  MythNVDECCaps(cudacodec, depth, cudaformat,
611  QSize(caps.nMinWidth, caps.nMinHeight),
612  QSize(static_cast<int>(caps.nMaxWidth), static_cast<int>(caps.nMaxHeight)),
613  caps.nMaxMBCount));
614  }
615  else if (!cuvid->cuvidGetDecoderCaps)
616  {
617  // dummy - just support everything:)
618  s_profiles.emplace_back(MythNVDECCaps(cudacodec, depth, cudaformat,
619  QSize(32, 32), QSize(8192, 8192),
620  (8192 * 8192) / 256));
621  }
622  }
623  }
624  }
625  cuvid_free_functions(&cuvid);
626  }
627  cuda->cuCtxPopCurrent(&dummy);
628  }
629  MythNVDECInterop::CleanupContext(opengl, cuda, context);
630 
631  return s_profiles;
632 }
633 
634 void MythNVDECContext::InitFramesContext(AVCodecContext *Context)
635 {
636  if (!Context)
637  return;
638 
639  if (m_framesContext)
640  {
641  auto *frames = reinterpret_cast<AVHWFramesContext*>(m_framesContext->data);
642  if ((frames->sw_format == Context->sw_pix_fmt) && (frames->width == Context->coded_width) &&
643  (frames->height == Context->coded_height))
644  {
645  Context->hw_frames_ctx = av_buffer_ref(m_framesContext);
646  return;
647  }
648  }
649 
650  // If this is a 'spontaneous' callback from FFmpeg (i.e. not on a stream change)
651  // then we must release any direct render buffers.
653  m_parent->GetPlayer()->DiscardVideoFrames(true, true);
654 
655  av_buffer_unref(&m_framesContext);
656 
657  AVBufferRef* framesref = av_hwframe_ctx_alloc(Context->hw_device_ctx);
658  auto *frames = reinterpret_cast<AVHWFramesContext*>(framesref->data);
660  frames->user_opaque = nullptr;
661  frames->sw_format = Context->sw_pix_fmt;
662  frames->format = AV_PIX_FMT_CUDA;
663  frames->width = Context->coded_width;
664  frames->height = Context->coded_height;
665  if (av_hwframe_ctx_init(framesref) < 0)
666  {
667  av_buffer_unref(&framesref);
668  }
669  else
670  {
671  Context->hw_frames_ctx = framesref;
672  m_framesContext = av_buffer_ref(framesref);
674  }
675 }
DEINT_DRIVER
@ DEINT_DRIVER
Definition: mythframe.h:73
MythNVDECContext::m_deinterlacer2x
bool m_deinterlacer2x
Definition: mythnvdeccontext.h:74
MythNVDECContext::InitialiseDecoder
static int InitialiseDecoder(AVCodecContext *Context)
Definition: mythnvdeccontext.cpp:133
DEINT_MEDIUM
@ DEINT_MEDIUM
Definition: mythframe.h:69
MythNVDECContext::PostProcessFrame
void PostProcessFrame(AVCodecContext *Context, MythVideoFrame *Frame) override
Definition: mythnvdeccontext.cpp:326
MythCodecContext::m_codecID
MythCodecID m_codecID
Definition: mythcodeccontext.h:168
MythNVDECContext::RetrieveFrame
bool RetrieveFrame(AVCodecContext *Context, MythVideoFrame *Frame, AVFrame *AvFrame) override
Definition: mythnvdeccontext.cpp:376
mythvideoout.h
mythplayerui.h
MythNVDECContext::~MythNVDECContext
~MythNVDECContext() override
Definition: mythnvdeccontext.cpp:22
MythVideoFrame::ParseDeinterlacer
static MythDeintType ParseDeinterlacer(const QString &Deinterlacer)
Definition: mythframe.cpp:541
DEINT_SHADER
@ DEINT_SHADER
Definition: mythframe.h:72
MythNVDECContext::GetSupportedCodec
static MythCodecID GetSupportedCodec(AVCodecContext **CodecContext, AVCodec **Codec, const QString &Decoder, AVStream *Stream, uint StreamType)
Determine whether NVDEC decoding is supported for this codec.
Definition: mythnvdeccontext.cpp:32
MythCodecContext::NoProfile
@ NoProfile
Definition: mythcodeccontext.h:56
Frame
Definition: zmdefines.h:93
MythCodecContext::NewHardwareFramesContext
static void NewHardwareFramesContext(void)
Track the number of concurrent frames contexts.
Definition: mythcodeccontext.cpp:428
kCodec_MPEG1_NVDEC_DEC
@ kCodec_MPEG1_NVDEC_DEC
Definition: mythcodecid.h:165
codec_is_nvdec
#define codec_is_nvdec(id)
Definition: mythcodecid.h:328
DEINT_NONE
@ DEINT_NONE
Definition: mythframe.h:67
arg
arg(title).arg(filename).arg(doDelete))
MythNVDECContext::GetBuffer
static bool GetBuffer(AVCodecContext *Context, MythVideoFrame *Frame, AVFrame *AvFrame, int Flags)
Convert AVFrame data to MythFrame.
Definition: mythnvdeccontext.cpp:392
MythCoreContext::IsUIThread
bool IsUIThread(void)
Definition: mythcorecontext.cpp:1357
MythCodecContext::InitVideoCodec
virtual void InitVideoCodec(AVCodecContext *Context, bool SelectedStream, bool &DirectRendering)
Definition: mythcodeccontext.cpp:302
MythDate::Format
Format
Definition: mythdate.h:12
MythNVDECContext::GetFormat
static enum AVPixelFormat GetFormat(AVCodecContext *Context, const AVPixelFormat *PixFmt)
Definition: mythnvdeccontext.cpp:356
FMT_YUV422P
@ FMT_YUV422P
Definition: mythframe.h:35
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythCodecContext::FramesContextFinished
static void FramesContextFinished(AVHWFramesContext *Context)
Definition: mythcodeccontext.cpp:435
MythVideoFrame::FormatIs422
static bool FormatIs422(VideoFrameType Type)
Definition: mythframe.h:439
MythNVDECContext::GetDecoderList
static void GetDecoderList(QStringList &Decoders)
Definition: mythnvdeccontext.cpp:549
MythPlayer::DiscardVideoFrames
void DiscardVideoFrames(bool KeyFrame, bool Flushed)
Places frames in the available frames queue.
Definition: mythplayer.cpp:636
FMT_YUV444P
@ FMT_YUV444P
Definition: mythframe.h:42
MythNVDECContext::MythNVDECCaps::Supports
bool Supports(cudaVideoCodec Codec, cudaVideoChromaFormat Format, uint Depth, int Width, int Height) const
Definition: mythnvdeccontext.cpp:489
MythVideoProfile
Definition: mythvideoprofile.h:83
MythNVDECContext::InitVideoCodec
void InitVideoCodec(AVCodecContext *Context, bool SelectedStream, bool &DirectRendering) override
Definition: mythnvdeccontext.cpp:188
FMT_NONE
@ FMT_NONE
Definition: mythframe.h:20
MythVideoProfile::GetDoubleRatePreferences
QString GetDoubleRatePreferences() const
Definition: mythvideoprofile.cpp:359
MythNVDECContext::MythNVDECCaps
Definition: mythnvdeccontext.h:50
MythNVDECContext::GetProfiles
static const std::vector< MythNVDECCaps > & GetProfiles(void)
Definition: mythnvdeccontext.cpp:563
DEINT_CPU
@ DEINT_CPU
Definition: mythframe.h:71
MythCodecID
MythCodecID
Definition: mythcodecid.h:10
AVFrame
struct AVFrame AVFrame
Definition: BorderDetector.h:15
Decoder
Definition: decoder.h:65
mythlogging.h
MythCodecContext::DeviceContextFinished
static void DeviceContextFinished(AVHWDeviceContext *Context)
Definition: mythcodeccontext.cpp:445
hardwareprofile.scan.profile
profile
Definition: scan.py:99
MythCodecContext::ReleaseBuffer
static void ReleaseBuffer(void *Opaque, uint8_t *Data)
Definition: mythcodeccontext.cpp:413
MythVideoProfile::GetSingleRatePreferences
QString GetSingleRatePreferences() const
Definition: mythvideoprofile.cpp:354
MythNVDECContext::m_deinterlacer
MythDeintType m_deinterlacer
Definition: mythnvdeccontext.h:73
FMT_YV12
@ FMT_YV12
Definition: mythframe.h:22
kCodec_MPEG1
@ kCodec_MPEG1
Definition: mythcodecid.h:21
kCodec_MPEG1_NVDEC
@ kCodec_MPEG1_NVDEC
Definition: mythcodecid.h:149
MythNVDECContext
Definition: mythnvdeccontext.h:24
MythCodecContext::VP9
@ VP9
Definition: mythcodeccontext.h:107
MythNVDECContext::MythNVDECCaps::m_format
cudaVideoChromaFormat m_format
Definition: mythnvdeccontext.h:62
MythNVDECInterop::CreateCUDAContext
static bool CreateCUDAContext(MythRenderOpenGL *GLContext, CudaFunctions *&CudaFuncs, CUcontext &CudaContext)
Definition: mythnvdecinterop.cpp:372
MythCodecContext::MPEG4
@ MPEG4
Definition: mythcodeccontext.h:65
MythVideoFrame::ColorDepth
static int ColorDepth(int Format)
Definition: mythframe.h:394
MythCodecContext::MPEG1
@ MPEG1
Definition: mythcodeccontext.h:57
MythCodecContext::HEVC
@ HEVC
Definition: mythcodeccontext.h:94
MythCodecContext::RetrieveHWFrame
virtual bool RetrieveHWFrame(MythVideoFrame *Frame, AVFrame *AvFrame)
Definition: mythcodeccontext.cpp:625
AvFormatDecoder
A decoder for media files.
Definition: avformatdecoder.h:85
MythNVDECContext::DecoderWillResetOnFlush
bool DecoderWillResetOnFlush(void) override
Definition: mythnvdeccontext.cpp:340
uint
unsigned int uint
Definition: compat.h:141
MythCodecContext::m_parent
DecoderBase * m_parent
Definition: mythcodeccontext.h:167
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
MythCodecContext::IsUnsupportedProfile
static bool IsUnsupportedProfile(AVCodecContext *Context)
Most hardware decoders do not support these codecs/profiles.
Definition: mythcodeccontext.cpp:595
MythCodecContext::MPEG2
@ MPEG2
Definition: mythcodeccontext.h:58
MythVideoFrame::FormatIs444
static bool FormatIs444(VideoFrameType Type)
Definition: mythframe.h:445
MythAVUtil::PixelFormatToFrameType
static VideoFrameType PixelFormatToFrameType(AVPixelFormat Fmt)
Definition: mythavutil.cpp:70
FMT_NVDEC
@ FMT_NVDEC
Definition: mythframe.h:61
MythVideoFrame::FormatIs420
static bool FormatIs420(VideoFrameType Type)
Definition: mythframe.h:433
MythRenderOpenGL
Definition: mythrenderopengl.h:96
MythDeintType
MythDeintType
Definition: mythframe.h:65
MythNVDECContext::SetDeinterlacing
void SetDeinterlacing(AVCodecContext *Context, MythVideoProfile *Profile, bool DoubleRate) override
Enable NVDEC/CUDA deinterlacing if necessary.
Definition: mythnvdeccontext.cpp:226
MythCodecContext::VC1
@ VC1
Definition: mythcodeccontext.h:101
MythCodecContext::CreateDevice
static AVBufferRef * CreateDevice(AVHWDeviceType Type, MythInteropGPU *Interop, const QString &Device=QString())
Definition: mythcodeccontext.cpp:556
avformatdecoder.h
MythVideoFrame::DeinterlacerName
static QString DeinterlacerName(MythDeintType Deint, bool DoubleRate, VideoFrameType Format=FMT_NONE)
Definition: mythframe.cpp:459
DEINT_HIGH
@ DEINT_HIGH
Definition: mythframe.h:70
MythNVDECContext::HaveNVDEC
static bool HaveNVDEC(bool Reinit=false)
Definition: mythnvdeccontext.cpp:512
codec_is_nvdec_dec
#define codec_is_nvdec_dec(id)
Definition: mythcodecid.h:330
mythnvdeccontext.h
MythCodecContext::FrameTypeIsSupported
static bool FrameTypeIsSupported(AVCodecContext *Context, VideoFrameType Format)
Definition: mythcodeccontext.cpp:512
LOC
#define LOC
Definition: mythnvdeccontext.cpp:14
MythRenderOpenGL::GetOpenGLRender
static MythRenderOpenGL * GetOpenGLRender(void)
Definition: mythrenderopengl.cpp:66
MythNVDECInterop::CreateNVDEC
static MythNVDECInterop * CreateNVDEC(MythPlayerUI *Player, MythRenderOpenGL *Context)
Definition: mythnvdecinterop.cpp:78
MythNVDECContext::HwDecoderInit
int HwDecoderInit(AVCodecContext *Context) override
Definition: mythnvdeccontext.cpp:200
MythNVDECContext::InitFramesContext
void InitFramesContext(AVCodecContext *Context)
Definition: mythnvdeccontext.cpp:634
MythNVDECInterop::CleanupContext
static void CleanupContext(MythRenderOpenGL *GLContext, CudaFunctions *&CudaFuncs, CUcontext &CudaContext)
Definition: mythnvdecinterop.cpp:396
MythCodecContext::InitialiseDecoder2
static int InitialiseDecoder2(AVCodecContext *Context, CreateHWDecoder Callback, const QString &Debug)
Initialise a hardware decoder that is NOT expected to use AVHWFramesContext.
Definition: mythcodeccontext.cpp:540
MythNVDECContext::MythNVDECCaps::m_profile
MythCodecContext::CodecProfile m_profile
Definition: mythnvdeccontext.h:58
MythNVDECContext::MythNVDECCaps::m_codec
cudaVideoCodec m_codec
Definition: mythnvdeccontext.h:60
mythnvdecinterop.h
MythNVDECContext::MythNVDECCaps::MythNVDECCaps
MythNVDECCaps(cudaVideoCodec Codec, uint Depth, cudaVideoChromaFormat Format, QSize Minimum, QSize Maximum, uint MacroBlocks)
Definition: mythnvdeccontext.cpp:448
MythNVDECContext::IsDeinterlacing
bool IsDeinterlacing(bool &DoubleRate, bool StreamChange=false) override
Definition: mythnvdeccontext.cpp:345
MythCodecContext
Definition: mythcodeccontext.h:51
VideoFrameType
VideoFrameType
Definition: mythframe.h:18
MythCodecContext::GetProfileDescription
static QString GetProfileDescription(CodecProfile Profile, QSize Size, VideoFrameType Format=FMT_NONE, uint ColorDepth=0)
Definition: mythcodeccontext.cpp:783
MythVideoFrame
Definition: mythframe.h:85
DEINT_BASIC
@ DEINT_BASIC
Definition: mythframe.h:68
MythNVDECContext::m_framesContext
AVBufferRef * m_framesContext
Definition: mythnvdeccontext.h:70
MythNVDECContext::MythNVDECContext
MythNVDECContext(DecoderBase *Parent, MythCodecID CodecID)
Definition: mythnvdeccontext.cpp:17
mythmainwindow.h
DecoderBase
Definition: decoderbase.h:120
MythCodecContext::GetPlayerUI
static MythPlayerUI * GetPlayerUI(AVCodecContext *Context)
Definition: mythcodeccontext.cpp:503
MythCodecContext::VP8
@ VP8
Definition: mythcodeccontext.h:106
DecoderBase::GetPlayer
MythPlayer * GetPlayer()
Definition: decoderbase.h:152
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:919
MythNVDECContext::MythNVDECCaps::m_type
VideoFrameType m_type
Definition: mythnvdeccontext.h:59
MythCodecContext::H264
@ H264
Definition: mythcodeccontext.h:83
OpenGLLocker
Definition: mythrenderopengl.h:252