Skip to content

Instantly share code, notes, and snippets.

@sberryman
Last active November 29, 2021 03:26
Show Gist options
  • Save sberryman/936c3aa7918b94daf07bd4923533e9f6 to your computer and use it in GitHub Desktop.
Save sberryman/936c3aa7918b94daf07bd4923533e9f6 to your computer and use it in GitHub Desktop.
OpenCV 3.4.2 CudaCodec HEVC Patch
diff --git a/modules/cudacodec/src/video_decoder.cpp b/modules/cudacodec/src/video_decoder.cpp
index 35919c3f5..19ebef10d 100644
--- a/modules/cudacodec/src/video_decoder.cpp
+++ b/modules/cudacodec/src/video_decoder.cpp
@@ -63,6 +63,11 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat)
cudaVideoCodec_VC1 == _codec ||
cudaVideoCodec_H264 == _codec ||
cudaVideoCodec_JPEG == _codec ||
+ cudaVideoCodec_H264_SVC == _codec ||
+ cudaVideoCodec_H264_MVC == _codec ||
+ cudaVideoCodec_HEVC == _codec ||
+ cudaVideoCodec_VP8 == _codec ||
+ cudaVideoCodec_VP9 == _codec ||
cudaVideoCodec_YUV420== _codec ||
cudaVideoCodec_YV12 == _codec ||
cudaVideoCodec_NV12 == _codec ||
@@ -84,7 +89,7 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat)
createInfo_.ulNumDecodeSurfaces = FrameQueue::MaximumSize;
// Limit decode memory to 24MB (16M pixels at 4:2:0 = 24M bytes)
- while (createInfo_.ulNumDecodeSurfaces * videoFormat.width * videoFormat.height > 16 * 1024 * 1024)
+ while (createInfo_.ulNumDecodeSurfaces * videoFormat.width * videoFormat.height > 16 * 8192 * 8192)
createInfo_.ulNumDecodeSurfaces--;
createInfo_.ChromaFormat = _chromaFormat;
diff --git a/modules/cudacodec/src/video_parser.cpp b/modules/cudacodec/src/video_parser.cpp
index 737f58511..a3488f16c 100644
--- a/modules/cudacodec/src/video_parser.cpp
+++ b/modules/cudacodec/src/video_parser.cpp
@@ -80,7 +80,7 @@ bool cv::cudacodec::detail::VideoParser::parseVideoData(const unsigned char* dat
return false;
}
- const int maxUnparsedPackets = 15;
+ const int maxUnparsedPackets = 90;
++unparsedPackets_;
if (unparsedPackets_ > maxUnparsedPackets)
diff --git a/modules/videoio/src/cap_ffmpeg_impl.hpp b/modules/videoio/src/cap_ffmpeg_impl.hpp
index de852abd2..367c7064e 100644
--- a/modules/videoio/src/cap_ffmpeg_impl.hpp
+++ b/modules/videoio/src/cap_ffmpeg_impl.hpp
@@ -1585,7 +1585,7 @@ static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
and qmin since they will be set to reasonable defaults by the libx264
preset system. Also, use a crf encode with the default quality rating,
this seems easier than finding an appropriate default bitrate. */
- if (c->codec_id == AV_CODEC_ID_H264) {
+ if (c->codec_id == AV_CODEC_ID_H264 || c->codec_id == AV_CODEC_ID_HEVC) {
c->gop_size = -1;
c->qmin = -1;
c->bit_rate = 0;
@@ -2582,6 +2582,9 @@ enum
VideoCodec_JPEG,
VideoCodec_H264_SVC,
VideoCodec_H264_MVC,
+ VideoCodec_HEVC,
+ VideoCodec_VP8,
+ VideoCodec_VP9,
// Uncompressed YUV
VideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), // Y,U,V (4:2:0)
@@ -2692,6 +2695,10 @@ bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma
*codec = ::VideoCodec_H264;
break;
+ case CV_CODEC(CODEC_ID_HEVC):
+ *codec = ::VideoCodec_HEVC;
+ break;
+
default:
return false;
};
@@ -2699,6 +2706,7 @@ bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma
switch (enc->pix_fmt)
{
case AV_PIX_FMT_YUV420P:
+ case AV_PIX_FMT_YUVJ420P:
*chroma_format = ::VideoChromaFormat_YUV420;
break;
diff --git a/modules/videoio/src/ffmpeg_codecs.hpp b/modules/videoio/src/ffmpeg_codecs.hpp
index 61788e034..38a0a2081 100644
--- a/modules/videoio/src/ffmpeg_codecs.hpp
+++ b/modules/videoio/src/ffmpeg_codecs.hpp
@@ -78,6 +78,7 @@ typedef struct AVCodecTag {
} AVCodecTag;
#if (LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(54, 51, 100))
+#define AV_CODEC_ID_HEVC CODEC_ID_HEVC
#define AV_CODEC_ID_H264 CODEC_ID_H264
#define AV_CODEC_ID_H263 CODEC_ID_H263
#define AV_CODEC_ID_H263P CODEC_ID_H263P
@@ -143,6 +144,13 @@ typedef struct AVCodecTag {
#endif
const AVCodecTag codec_bmp_tags[] = {
+ { AV_CODEC_ID_HEVC, MKTAG('H', 'E', 'V', 'C') },
+ { AV_CODEC_ID_HEVC, MKTAG('h', 'e', 'v', 'c') },
+ { AV_CODEC_ID_HEVC, MKTAG('H', '2', '6', '5') },
+ { AV_CODEC_ID_HEVC, MKTAG('h', '2', '6', '5') },
+ { AV_CODEC_ID_HEVC, MKTAG('X', '2', '6', '5') },
+ { AV_CODEC_ID_HEVC, MKTAG('x', '2', '6', '5') },
+
{ AV_CODEC_ID_H264, MKTAG('H', '2', '6', '4') },
{ AV_CODEC_ID_H264, MKTAG('h', '2', '6', '4') },
{ AV_CODEC_ID_H264, MKTAG('X', '2', '6', '4') },
@sberryman
Copy link
Author

sberryman commented Oct 11, 2018

Thanks to @PaulinoAranda for doing all the work!
Source: PaulinoAranda/opencv@ca68951

@sberryman
Copy link
Author

Changed const int maxUnparsedPackets = 15; from 15 to 30. This fixed decoding issues for me for an RTSP stream over UDP. (HEVC 4K video)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment