1 /*M/////////////////////////////////////////////////////////////////////////////////////// 2 // 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 4 // 5 // By downloading, copying, installing or using the software you agree to this license. 6 // If you do not agree to this license, do not download, install, 7 // copy or use the software. 8 // 9 // 10 // License Agreement 11 // For Open Source Computer Vision Library 12 // 13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved. 16 // Third party copyrights are property of their respective owners. 17 // 18 // Redistribution and use in source and binary forms, with or without modification, 19 // are permitted provided that the following conditions are met: 20 // 21 // * Redistribution's of source code must retain the above copyright notice, 22 // this list of conditions and the following disclaimer. 23 // 24 // * Redistribution's in binary form must reproduce the above copyright notice, 25 // this list of conditions and the following disclaimer in the documentation 26 // and/or other materials provided with the distribution. 27 // 28 // * The name of the copyright holders may not be used to endorse or promote products 29 // derived from this software without specific prior written permission. 30 // 31 // This software is provided by the copyright holders and contributors "as is" and 32 // any express or implied warranties, including, but not limited to, the implied 33 // warranties of merchantability and fitness for a particular purpose are disclaimed. 34 // In no event shall the Intel Corporation or contributors be liable for any direct, 35 // indirect, incidental, special, exemplary, or consequential damages 36 // (including, but not limited to, procurement of substitute goods or services; 37 // loss of use, data, or profits; or business interruption) however caused 38 // and on any theory of liability, whether in contract, strict liability, 39 // or tort (including negligence or otherwise) arising in any way out of 40 // the use of this software, even if advised of the possibility of such damage. 41 // 42 //M*/ 43 44 #include "precomp.hpp" 45 46 #ifdef HAVE_NVCUVID 47 48 void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat) 49 { 50 release(); 51 52 cudaVideoCodec _codec = static_cast<cudaVideoCodec>(videoFormat.codec); 53 cudaVideoChromaFormat _chromaFormat = static_cast<cudaVideoChromaFormat>(videoFormat.chromaFormat); 54 55 cudaVideoCreateFlags videoCreateFlags = (_codec == cudaVideoCodec_JPEG || _codec == cudaVideoCodec_MPEG2) ? 56 cudaVideoCreate_PreferCUDA : 57 cudaVideoCreate_PreferCUVID; 58 59 // Validate video format. These are the currently supported formats via NVCUVID 60 CV_Assert(cudaVideoCodec_MPEG1 == _codec || 61 cudaVideoCodec_MPEG2 == _codec || 62 cudaVideoCodec_MPEG4 == _codec || 63 cudaVideoCodec_VC1 == _codec || 64 cudaVideoCodec_H264 == _codec || 65 cudaVideoCodec_JPEG == _codec || 66 cudaVideoCodec_YUV420== _codec || 67 cudaVideoCodec_YV12 == _codec || 68 cudaVideoCodec_NV12 == _codec || 69 cudaVideoCodec_YUYV == _codec || 70 cudaVideoCodec_UYVY == _codec ); 71 72 CV_Assert(cudaVideoChromaFormat_Monochrome == _chromaFormat || 73 cudaVideoChromaFormat_420 == _chromaFormat || 74 cudaVideoChromaFormat_422 == _chromaFormat || 75 cudaVideoChromaFormat_444 == _chromaFormat); 76 77 // Fill the decoder-create-info struct from the given video-format struct. 78 std::memset(&createInfo_, 0, sizeof(CUVIDDECODECREATEINFO)); 79 80 // Create video decoder 81 createInfo_.CodecType = _codec; 82 createInfo_.ulWidth = videoFormat.width; 83 createInfo_.ulHeight = videoFormat.height; 84 createInfo_.ulNumDecodeSurfaces = FrameQueue::MaximumSize; 85 86 // Limit decode memory to 24MB (16M pixels at 4:2:0 = 24M bytes) 87 while (createInfo_.ulNumDecodeSurfaces * videoFormat.width * videoFormat.height > 16 * 1024 * 1024) 88 createInfo_.ulNumDecodeSurfaces--; 89 90 createInfo_.ChromaFormat = _chromaFormat; 91 createInfo_.OutputFormat = cudaVideoSurfaceFormat_NV12; 92 createInfo_.DeinterlaceMode = cudaVideoDeinterlaceMode_Adaptive; 93 94 // No scaling 95 static const int MAX_FRAME_COUNT = 2; 96 97 createInfo_.ulTargetWidth = createInfo_.ulWidth; 98 createInfo_.ulTargetHeight = createInfo_.ulHeight; 99 createInfo_.ulNumOutputSurfaces = MAX_FRAME_COUNT; // We won't simultaneously map more than 8 surfaces 100 createInfo_.ulCreationFlags = videoCreateFlags; 101 createInfo_.vidLock = lock_; 102 103 // create the decoder 104 cuSafeCall( cuvidCreateDecoder(&decoder_, &createInfo_) ); 105 } 106 107 void cv::cudacodec::detail::VideoDecoder::release() 108 { 109 if (decoder_) 110 { 111 cuvidDestroyDecoder(decoder_); 112 decoder_ = 0; 113 } 114 } 115 116 #endif // HAVE_NVCUVID 117