Esempio n. 1
0
        public bool InitDecoder(CudaTools.DECODER_MODE modeDecoder, int outputWidth, int outputHeight, CudaTools.CODEC codec = CudaTools.CODEC.H264)
        {
            bool Success = InitCuda();

            if (Success)
            {
                FreeDecoder();
                if ((m_videoDecoder = CudaTools.VideoDecoder_Create64(m_cudaContext)) != IntPtr.Zero)
                {
                    IntPtr semaphoreInput;
                    IntPtr semaphoreOutput;
                    IntPtr eventStopped;
                    CudaTools.VideoDecoder_GetWindowsHandles64(m_videoDecoder, out semaphoreInput, out semaphoreOutput, out eventStopped);

                    m_InputQueueSemaphore = new Semaphore(0, int.MaxValue);
                    m_InputQueueSemaphore.SafeWaitHandle = new SafeWaitHandle(semaphoreInput, false);

                    m_OutputQueueSemaphore = new Semaphore(0, int.MaxValue);
                    m_OutputQueueSemaphore.SafeWaitHandle = new SafeWaitHandle(semaphoreOutput, false);

                    if (Success = CudaTools.VideoDecoder_Init64(m_videoDecoder))
                    {
                        CudaTools.VideoDecoder_ConfigureDecoder64(m_videoDecoder, outputWidth, outputHeight, (int)modeDecoder, (int)codec);
                        Success = true;
                    }
                }
            }
            return(Success);
        }
Esempio n. 2
0
        public bool Init(CudaTools.DECODER_MODE modeDecoder,
                         CudaTools.ENCODER_MODE modeEncoder,
                         CudaTools.VIDEOSINK_MODE modeSink,
                         int outputWidth, int outputHeight,
                         int bitRate, int frameRate, int gopLength,
                         int invalidateRefFramesEnableFlag = 0,
                         int intraRefreshEnableFlag        = 1,
                         int intraRefreshPeriod            = 5,
                         int intraRefreshDuration          = 5)
        {
            bool success = false;

            if (success = InitDecoder(modeDecoder, outputWidth, outputHeight))
            {
                if (success = InitEncoder(modeEncoder, modeSink, bitRate, frameRate, gopLength, invalidateRefFramesEnableFlag,
                                          intraRefreshEnableFlag, intraRefreshPeriod, intraRefreshDuration))
                {
                    if (success)
                    {
                        IntPtr semaphoreOutput;
                        IntPtr eventStopped;
                        CudaTools.VideoEncoder_GetWindowsHandles64(m_videoEncoder, out eventStopped, out semaphoreOutput);

                        m_OutputQueueEncoderSemaphore = new Semaphore(0, int.MaxValue);
                        m_OutputQueueEncoderSemaphore.SafeWaitHandle = new SafeWaitHandle(semaphoreOutput, false);
                    }
                }
            }
            return(success);
        }
Esempio n. 3
0
        public bool GetOutputFrame(WaitHandle hExit, int msWait = 0)
        {
            bool success = false;

            switch (WaitHandle.WaitAny(new WaitHandle[] { m_OutputQueueEncoderSemaphore, hExit }, msWait))
            {
            case 0:
            {
                IntPtr frame;
                int    numBytes;

                int haveFrame = CudaTools.VideoEncoder_GetNextEncodedFrame64(m_videoEncoder, out frame, out numBytes);
                //Debug.Print(size.ToString());
                if (haveFrame == 1)
                {
                    m_frameStore = new byte[numBytes];
                    Marshal.Copy(frame, m_frameStore, 0, (int)numBytes);
                    CudaTools.VideoEncoder_ReleaseFrame64(m_videoEncoder);
                    success = true;
                }
            }
            break;

            case 1:
                break;

            case WaitHandle.WaitTimeout:
                break;
            }
            return(success);
        }
Esempio n. 4
0
 public void FreeCuda()
 {
     if (m_cuda != IntPtr.Zero)
     {
         CudaTools.Cuda_Free64(m_cuda);
         m_cuda = IntPtr.Zero;
     }
 }
Esempio n. 5
0
 public void FreeEncoder()
 {
     if (m_videoEncoder != IntPtr.Zero)
     {
         CudaTools.VideoEncoder_Free64(m_videoEncoder);
         m_videoEncoder = IntPtr.Zero;
     }
 }
Esempio n. 6
0
        public bool InputNewCompressedFrame(byte[] frame)
        {
            bool success = false;

            if (m_InputQueueSemaphore.WaitOne(5000))
            {
                CudaTools.VideoDecoder_NewInputFrame64(m_videoDecoder, frame, frame.Length);
                success = true;
            }
            return(success);
        }
Esempio n. 7
0
 public void Stop()
 {
     if (m_videoDecoder != IntPtr.Zero)
     {
         CudaTools.VideoDecoder_Stop64(m_videoDecoder);
     }
     if (m_videoEncoder != IntPtr.Zero)
     {
         CudaTools.VideoEncoder_Stop64(m_videoEncoder);
     }
 }
Esempio n. 8
0
        public IntPtr GetUncompressedFrameGPU()
        {
            int    width;
            int    height;
            int    format;
            UInt64 timeStamp;
            IntPtr pFrame;
            int    numBytes;

            CudaTools.VideoDecoder_GetNextDecodedFrameGPU64(m_videoDecoder, out pFrame, out numBytes, out width, out height, out format, out timeStamp);

            return(IntPtr.Zero);
        }
Esempio n. 9
0
 public void CloseDecoder()
 {
     decodedFrame = null;
     if (m_videoDecoder != IntPtr.Zero)
     {
         CudaTools.VideoDecoder_Free64(m_videoDecoder);
         m_videoDecoder = IntPtr.Zero;
     }
     if (m_cuda != IntPtr.Zero)
     {
         CudaTools.Cuda_Free64(m_cuda);
         m_cuda = IntPtr.Zero;
     }
 }
Esempio n. 10
0
        public bool InitCuda()
        {
            bool Success = m_cuda != IntPtr.Zero;

            if (!Success)
            {
                if ((m_cuda = CudaTools.Cuda_Create64()) != IntPtr.Zero)
                {
                    if (CudaTools.Cuda_GetContext64(m_cuda, out m_cudaContext) == true)
                    {
                        Success = true;
                    }
                }
            }
            return(Success);
        }
Esempio n. 11
0
        public bool NewInputFrame(byte[] frame, int length, int msWait = 10000)
        {
            bool retVal = false;

            if (m_InputQueueSemaphore.WaitOne(msWait))
            {
                CudaTools.VideoDecoder_NewInputFrame64(m_videoDecoder, frame, length);
                ++count;
                retVal = true;
            }
            else
            {
                ++dropped;
            }
            return(retVal);
        }
Esempio n. 12
0
        public bool InitEncoder(CudaTools.ENCODER_MODE modeEncoder, CudaTools.VIDEOSINK_MODE modeSink, int bitRate, int frameRate, int gopLength,
                                int invalidateRefFramesEnableFlag,
                                int intraRefreshEnableFlag,
                                int intraRefreshPeriod,
                                int intraRefreshDuration)
        {
            bool Success = false;

            FreeEncoder();
            if ((m_videoEncoder = CudaTools.VideoEncoder_Create64(m_videoDecoder)) != IntPtr.Zero)
            {
                CudaTools.VideoEncoder_ConfigureEncoder64(m_videoEncoder, bitRate, (int)frameRate, (int)modeEncoder, (int)modeSink, (int)gopLength, (int)invalidateRefFramesEnableFlag,
                                                          (int)intraRefreshEnableFlag, (int)intraRefreshPeriod, (int)intraRefreshDuration);
                Success = true;
            }
            return(Success);
        }
Esempio n. 13
0
        public bool Init(CudaTools.CODEC codec = CudaTools.CODEC.H264, CudaTools.DECODER_MODE decoderMode = CudaTools.DECODER_MODE.CPU_ARGB, int skipCount = 1)
        {
            bool retVal = false;

            CloseDecoder();
            if ((m_cuda = CudaTools.Cuda_Create64()) != IntPtr.Zero)
            {
                ulong t;
                ulong f;
                CudaTools.Cuda_GetDeviceMemory64(m_cuda, out t, out f);
                if (CudaTools.Cuda_GetContext64(m_cuda, out m_cudaContext) == true)
                {
                    if ((m_videoDecoder = CudaTools.VideoDecoder_Create64(m_cudaContext)) != IntPtr.Zero)
                    {
                        IntPtr semaphoreInput;
                        IntPtr semaphoreOutput;
                        IntPtr eventStopped;
                        CudaTools.VideoDecoder_GetWindowsHandles64(m_videoDecoder, out semaphoreInput, out semaphoreOutput, out eventStopped);

                        m_InputQueueSemaphore = new Semaphore(0, int.MaxValue);
                        m_InputQueueSemaphore.SafeWaitHandle = new SafeWaitHandle(semaphoreInput, false);

                        m_OutputQueueSemaphore = new Semaphore(0, int.MaxValue);
                        m_OutputQueueSemaphore.SafeWaitHandle = new SafeWaitHandle(semaphoreOutput, false);


                        if (CudaTools.VideoDecoder_Init64(m_videoDecoder))
                        {
                            CudaTools.VideoDecoder_ConfigureDecoder64(m_videoDecoder, (int)m_width, (int)m_height, (int)decoderMode, (int)codec);
                            CudaTools.VideoDecoder_SetSkipCount(m_videoDecoder, skipCount);
                            CudaTools.VideoDecoder_Start64(m_videoDecoder);
                            retVal = true;
                        }
                    }
                }
            }
            return(retVal);
        }
Esempio n. 14
0
        public byte[] GetUncompressedFrame()
        {
            byte[] result = null;
            // Get the new frame
            if (decodedFrame == null)
            {
                decodedFrame = new byte[m_width * m_height * 4];
            }
            int    width;
            int    height;
            int    format;
            UInt64 timeStamp;
            IntPtr frame;
            int    numBytes;

            CudaTools.VideoDecoder_GetNextDecodedFrame64(m_videoDecoder, out frame, out numBytes, out width, out height, out format, out timeStamp);
            // Why the double copy?
            Marshal.Copy(frame, decodedFrame, 0, (int)numBytes);
            CudaTools.VideoDecoder_ReleaseFrame(m_videoDecoder);
            result = new byte[decodedFrame.Length];
            Buffer.BlockCopy(decodedFrame, 0, result, 0, decodedFrame.Length);
            return(result);
        }
Esempio n. 15
0
 public void SetJpegQuality(int jpegQuality)
 {
     CudaTools.VideoEncoder_SetJpegQuality(m_videoEncoder, jpegQuality);
 }
Esempio n. 16
0
 public void Flush()
 {
     CudaTools.VideoDecoder_Flush64(m_videoDecoder);
 }
Esempio n. 17
0
 public void SetSkipCount(int skipCount)
 {
     CudaTools.VideoDecoder_SetSkipCount(m_videoDecoder, skipCount);
 }
Esempio n. 18
0
 public void Start()
 {
     CudaTools.VideoDecoder_Start64(m_videoDecoder);
     CudaTools.VideoEncoder_Start64(m_videoEncoder);
 }
Esempio n. 19
0
 public void SetOutputFile(string fileName)
 {
     CudaTools.VideoEncoder_SetOutputFilename64(m_videoEncoder, fileName);
 }
Esempio n. 20
0
 public void Stop()
 {
     CudaTools.VideoDecoder_Stop64(m_videoDecoder);
 }
Esempio n. 21
0
 public void AddCompressedFrame(byte[] frame, int width, int height, byte keyFlag)
 {
     CudaTools.VideoDecoder_NewInputFrame64(m_videoDecoder, frame, frame.Length);
 }