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