Exemplo n.º 1
0
        public unsafe void DecodeToHostRgba32(byte *destinationPtr)
        {
            var       width   = Info.Width;
            var       height  = Info.Height;
            var       buffer  = Buffer;
            const int rgbBpp  = 4;
            var       rgbSize = GetRgba32Size();

            switch (buffer.MemoryType)
            {
            case CuMemoryType.Host:
                LibYuvSharp.LibYuv.NV12ToARGB(
                    (byte *)buffer.Bytes, YuvInfo.LumaPitch,
                    (byte *)buffer.Bytes + YuvInfo.ChromaOffset,
                    YuvInfo.ChromaPitch,
                    destinationPtr, width * rgbBpp, width, height);

                break;

            case CuMemoryType.Device:
                using (var destPtr = CuDeviceMemory.Allocate(rgbSize))
                {
                    LibCudaLibrary.Nv12ToBGRA32(
                        buffer.DeviceMemory.Handle, width,
                        destPtr, width * rgbBpp, width, height);

                    destPtr.CopyToHost(destinationPtr, rgbSize);
                }

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(buffer.MemoryType));
            }
        }
Exemplo n.º 2
0
        public void DecodeToDeviceRgba32(
            IntPtr destinationPtr,
            Size?resize = null)
        {
            var       width  = Info.Width;
            var       height = Info.Height;
            var       buffer = Buffer;
            const int rgbBpp = 4;

            var source       = buffer.DeviceMemory;
            var hasNewSource = false;

            try
            {
                // This code path does not appear to properly resize the
                // window.
                if (resize.HasValue)
                {
                    var newWidth  = resize.Value.Width;
                    var newHeight = resize.Value.Height;

                    // This buffer size allocation is incorrect but should be
                    // oversized enough to be fine.
                    source = CuDeviceMemory.Allocate(
                        newWidth * newHeight * rgbBpp);
                    hasNewSource = true;

                    LibCudaLibrary.ResizeNv12(
                        source, newWidth, newWidth, newHeight,
                        buffer.DeviceMemory, Pitch, width, height,
                        CuDevicePtr.Empty);

                    width  = newWidth;
                    height = newHeight;
                }

                switch (buffer.MemoryType)
                {
                case CuMemoryType.Device:
                    LibCudaLibrary.Nv12ToBGRA32(
                        source, width,
                        destinationPtr, width * rgbBpp, width, height);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(
                              nameof(buffer.MemoryType), buffer.MemoryType,
                              "Unsupported memory type.");
                }
            }
            finally
            {
                if (hasNewSource)
                {
                    source.Dispose();
                }
            }
        }