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