Ejemplo n.º 1
0
 public FrameInformation(
     BufferStorage buffer,
     int pitch,
     CuVideoDecodeCreateInfo info,
     YuvInformation yuvInfo)
 {
     Buffer  = buffer;
     Pitch   = pitch;
     Info    = info;
     YuvInfo = yuvInfo;
 }
Ejemplo n.º 2
0
        private CuCallbackResult VideoDisplayCallback(
            IntPtr data, IntPtr infoPtr)
        {
            using var _ = _context.Push();

            if (CuVideoParseDisplayInfo.IsFinalFrame(infoPtr, out var info))
            {
                if (!_framesChannel.Writer.TryWrite(
                        FrameInformation.FinalFrame))
                {
                    _renderingCompleted.Set();
                }

                return(CuCallbackResult.Success);
            }

            var processingParam = new CuVideoProcParams
            {
                ProgressiveFrame = info.ProgressiveFrame,
                SecondField      = info.RepeatFirstField + 1,
                TopFieldFirst    = info.TopFieldFirst,
                UnpairedField    = info.RepeatFirstField < 0 ? 1 : 0
            };

            using var frame = _decoder.MapVideoFrame(
                      info.PictureIndex, ref processingParam,
                      out var pitch);

            var yuvInfo = _info.GetYuvInformation(pitch);
            var status  = _decoder.GetDecodeStatus(info.PictureIndex);

            if (status != CuVideoDecodeStatus.Success)
            {
                // TODO: Determine what to do in this situation. This condition
                // is non-exceptional but may require different handling?
            }

            var frameByteSize = _info.GetFrameByteSize(
                pitch, out var chromaHeight);

            var destMemoryType = _useHostMemory
                ? CuMemoryType.Host
                : CuMemoryType.Device;

            AllocateNv12FrameBuffer(
                destMemoryType, frameByteSize,
                out var frameDevicePtr, out var frameLocalPtr);

            var byteWidth = _info.Width * _info.GetBytesPerPixel();

            // Copy luma
            var memcopy = new CuMemcopy2D
            {
                SrcMemoryType = CuMemoryType.Device,
                SrcDevice     = frame,
                SrcPitch      = (IntPtr)pitch,
                DstMemoryType = destMemoryType,
                DstDevice     = frameDevicePtr,
                DstHost       = frameLocalPtr,
                DstPitch      = (IntPtr)byteWidth,
                WidthInBytes  = (IntPtr)byteWidth,
                Height        = (IntPtr)_info.Height
            };

            memcopy.Memcpy2D();

            // Copy chroma
            memcopy.SrcDevice = new CuDevicePtr(frame.Handle + pitch * _info.Height);
            memcopy.DstDevice = new CuDevicePtr(frameDevicePtr.Handle + byteWidth * _info.Height);
            memcopy.DstHost   = frameLocalPtr + byteWidth * _info.Height;
            memcopy.Height    = (IntPtr)chromaHeight;
            memcopy.Memcpy2D();

            var bufferStorage = new BufferStorage(
                frameLocalPtr, destMemoryType,
                frameDevicePtr, frameByteSize);

            var frameInfo = new FrameInformation(
                bufferStorage, pitch, _info, yuvInfo);

            if (!_framesChannel.Writer.TryWrite(frameInfo))
            {
                _nv12BufferPool.Free(ref bufferStorage);
            }

            return(CuCallbackResult.Success);
        }