nvjpeg

package
v0.0.0-...-c9f06ed Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2020 License: MIT Imports: 6 Imported by: 3

README

#nvjpeg - go bindings for nvjpeg.h

nvjpeg contains bindings for nvjpeg.

note: CreateEx creates a special handler with a pinned allocator and device allocator. It uses callback functions. Make one if you want, but it will have to be part of the nvjpeg package since go is a pain when it comes to C types and librarys.

I will probably make an nvjpegutil to create wrappers that can be used with golang io package. It seems that stream and handle can be placed into JpegState struct and maybe even EncoderParams. and with that I can make a an interface that will do a reader and writer.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChannelDimHelper

func ChannelDimHelper(o OutputFormat, pitch, height []int32) (w, h []int32)

ChannelDimHelper will return the dims for each channel depending on the ouputformat

func GetLibraryProperties

func GetLibraryProperties() (major, minor, patch int, err error)

GetLibraryProperties returns library's property values. The major ,minor,and patch version

Types

type Backend

type Backend C.nvjpegBackend_t

These are not working on one of my machines. For now I will comment them out

func (p *PinnedBuffer) Retrieve() (ptr cutil.Mem, sib uint, err error) {
	var sizet C.size_t
	ptr = new(gocu.CudaPtr)
	err = status(C.nvjpegBufferPinnedRetrieve(p.b, &sizet, ptr.DPtr())).error()
	return ptr, (uint)(sizet), err

}

func (d *DeviceBuffer) Retrieve() (ptr cutil.Mem, sib uint, err error) {
	var sizet C.size_t
	ptr = new(gocu.CudaPtr)
	err = status(C.nvjpegBufferDeviceRetrieve(d.b, &sizet, ptr.DPtr())).error()
	return ptr, (uint)(sizet), err
}

Backend are flags that are used to set the implimentation.

func (*Backend) Default

func (b *Backend) Default() Backend

Default defaults to GPUHybrid --- Backend(C.NVJPEG_BACKEND_DEFAULT) //binder note. I don't know why this is here. Method changes the underlying value of the flag and also returns that value.

func (*Backend) GPUHybrid

func (b *Backend) GPUHybrid() Backend

GPUHybrid nvjpegDecodeBatched will use GPU decoding for baseline JPEG images with interleaved scan when batch size is bigger than 100, it uses CPU for other JPEG types. Other decode APIs will continue to use CPU for Huffman decode Method changes the underlying value of the flag and also returns that value.

func (*Backend) Hybrid

func (b *Backend) Hybrid() Backend

Hybrid uses CPU for Huffman decode --- Backend(C.NVJPEG_BACKEND_HYBRID) Method changes the underlying value of the flag and also returns that value.

type ChromaSubsampling

type ChromaSubsampling C.nvjpegChromaSubsampling_t

ChromaSubsampling returned by getImageInfo identifies image chroma subsampling stored inside JPEG input stream. In the case of NVJPEG_CSS_GRAY only 1 luminance channel is encoded in JPEG input stream. Otherwise both chroma planes are present.

Initial release support: 4:4:4, 4:2:0, 4:2:2, Grayscale.

Flags can be changed by using methods. switches

func GetImageInfo

func GetImageInfo(handle *Handle, data []byte) (subsampling ChromaSubsampling, w []int32, h []int32, err error)

GetImageInfo gets the image info Retrieve the image info, including channel, width and height of each component, and chroma subsampling. If less than NVJPEG_MAX_COMPONENT channels are encoded, then zeros would be set to absent channels information If the image is 3-channel, all three groups are valid. This function is thread safe.

-IN handle : Library handle

-IN data : Pointer to the buffer containing the jpeg stream data to be decoded

-Return nComponent : Number of componenets of the image, currently only supports 1-channel (grayscale) or 3-channel

-Return subsampling : Chroma subsampling used in this JPEG

-Return w : pointer to NVJPEG_MAX_COMPONENT of ints, returns width of each channel

-Return h : pointer to NVJPEG_MAX_COMPONENT of ints, returns height of each channel

func (*ChromaSubsampling) CSS410

func (ch *ChromaSubsampling) CSS410() ChromaSubsampling

CSS410 - 4:1:0 changes the underlying value of type to ChromaSubsampling(C.NVJPEG_CSS_420) and returns it

func (*ChromaSubsampling) CSS411

func (ch *ChromaSubsampling) CSS411() ChromaSubsampling

CSS411 - 4:1:1 changes the underlying value of type to ChromaSubsampling(C.NVJPEG_CSS_411) and returns it

func (*ChromaSubsampling) CSS420

func (ch *ChromaSubsampling) CSS420() ChromaSubsampling

CSS420 - 4:4:0 changes the underlying value of type to ChromaSubsampling(C.NVJPEG_CSS_420) and returns it

func (*ChromaSubsampling) CSS422

func (ch *ChromaSubsampling) CSS422() ChromaSubsampling

CSS422 - 4:4:2 changes the underlying value of type to ChromaSubsampling(C.NVJPEG_CSS_422) and returns it

func (*ChromaSubsampling) CSS444

func (ch *ChromaSubsampling) CSS444() ChromaSubsampling

CSS444 - 4:4:4 changes the underlying value of type to ChromaSubsampling(C.NVJPEG_CSS_444) and returns it

func (*ChromaSubsampling) CSSGRAY

func (ch *ChromaSubsampling) CSSGRAY() ChromaSubsampling

CSSGRAY - grayscale changes the underlying value of type to ChromaSubsampling(C.NVJPEG_CSS_GRAY) and returns it

func (*ChromaSubsampling) CSSUNKNOWN

func (ch *ChromaSubsampling) CSSUNKNOWN() ChromaSubsampling

CSSUNKNOWN - changes the underlying value of type to ChromaSubsampling(C.NVJPEG_CSS_UNKNOWN) and returns it

func (ChromaSubsampling) String

func (ch ChromaSubsampling) String() string

String returns a human readable string of the flag. Mostly useful for debugging.

type DecodeParams

type DecodeParams struct {
	// contains filtered or unexported fields
}

DecodeParams - decode parameters structure. Used to set decode-related tweaks

func CreateDecodeParams

func CreateDecodeParams(h *Handle) (d *DecodeParams, err error)

CreateDecodeParams creates a DecodeParam

func (*DecodeParams) SetCMYK

func (d *DecodeParams) SetCMYK(set bool) error

SetCMYK - set to true to allow conversion from CMYK to RGB or YUV that follows simple subtractive scheme

func (*DecodeParams) SetOutputFormat

func (d *DecodeParams) SetOutputFormat(format OutputFormat) error

SetOutputFormat sets the format for the output

func (*DecodeParams) SetROI

func (d *DecodeParams) SetROI(offsetX, offsetY, roiW, roiH int32) error

SetROI - set to desired ROI. Pass (0, 0, -1, -1) to disable ROI decode (decode whole image)

type DeviceBuffer

type DeviceBuffer struct {
	// contains filtered or unexported fields
}

DeviceBuffer buffer for device memory

func CreateNonUnifiedDeviceBuffer

func CreateNonUnifiedDeviceBuffer(h *Handle) (dbuff *DeviceBuffer, err error)

func CreateUnifiedDeviceBuffer

func CreateUnifiedDeviceBuffer(h *Handle) (dbuff *DeviceBuffer, err error)

type EncoderParams

type EncoderParams struct {
	// contains filtered or unexported fields
}

EncoderParams is a struct that contains paramerters for the encoder

func CreateEncoderParams

func CreateEncoderParams(h *Handle, s gocu.Streamer) (*EncoderParams, error)

CreateEncoderParams creates an EncoderParams

func (*EncoderParams) GetBufferSize

func (ep *EncoderParams) GetBufferSize(h *Handle, width, height int32) (maxStreamLength uint, err error)

GetBufferSize - Returns the maximum possible buffer size that is needed to store the compressed JPEG stream, for the given input parameters.

func (*EncoderParams) SetOptimizedHuffman

func (ep *EncoderParams) SetOptimizedHuffman(optimized bool, s gocu.Streamer) error

SetOptimizedHuffman - Sets whether or not to use optimized Huffman. Using optimized Huffman produces smaller JPEG bitstream sizes with the same quality, but with slower performance. Default is false

func (*EncoderParams) SetQuality

func (ep *EncoderParams) SetQuality(quality int32, s gocu.Streamer) error

SetQuality sets the quality of the encoder params Quality should be a number between 1 and 100. The default is set to 70

func (*EncoderParams) SetSamplingFactors

func (ep *EncoderParams) SetSamplingFactors(ssfactor ChromaSubsampling, s gocu.Streamer) error

SetSamplingFactors -Sets which chroma subsampling will be used for JPEG compression. ssfactor that will be used for JPEG compression. If the input is in YUV color model and ssfactor is different from the subsampling factors of source image, then the NVJPEG library will convert subsampling to the value of chroma_subsampling. Default value is 4:4:4.

type EncoderState

type EncoderState struct {
	// contains filtered or unexported fields
}

EncoderState is used for encoding functions in nvjpeg

func CreateEncoderState

func CreateEncoderState(h *Handle, s gocu.Streamer) (es *EncoderState, err error)

CreateEncoderState creates an EncoderState

func (*EncoderState) EncodeImage

func (es *EncoderState) EncodeImage(
	h *Handle,
	ep *EncoderParams,
	src *Image,
	fmt InputFormat,
	swidth int32,
	sheight int32,
	s gocu.Streamer) error

EncodeImage - Compresses the image in the provided format to the JPEG stream using the provided parameters, and stores it in the state structure

func (*EncoderState) EncodeYUV

func (es *EncoderState) EncodeYUV(
	h *Handle,
	ep *EncoderParams,
	src *Image,
	srcsampling ChromaSubsampling,
	swidth int32,
	sheight int32,
	s gocu.Streamer) error

EncodeYUV -Compresses the image in YUV colorspace to JPEG stream using the provided parameters, and stores it in the state structure.

func (*EncoderState) GetCompressedBufferSize

func (es *EncoderState) GetCompressedBufferSize(h *Handle, s gocu.Streamer) (uint, error)

GetCompressedBufferSize returns the length of the compressed stream

func (*EncoderState) ReadBitStream

func (es *EncoderState) ReadBitStream(h *Handle, p []byte, s gocu.Streamer) (n int, err error)

ReadBitStream reads the compressed bit stream and puts it into p. Method returns the n the number of bytes written into p. if len(p) is less than the bit stream. nothing will be written and an error will be returned.

func (*EncoderState) RetrieveBitStream

func (es *EncoderState) RetrieveBitStream(h *Handle, s gocu.Streamer) ([]byte, error)

RetrieveBitStream does GetCompressedBufferSize makes a slice of bytes from that, and runs ReadBitStream and returns the bytes filled.

type Handle

type Handle struct {
	// contains filtered or unexported fields
}

Handle - Opaque library handle identifier.

func CreateEx

func CreateEx(backend Backend) (h *Handle, err error)

CreateEx uses cudaMallocManaged. The handle is used for all consecutive nvjpeg calls IN backend : Backend to use. Currently Default or Hybrid (which is the same at the moment) is supported. INT/OUT handle : Codec instance, use for other calls

func CreateSimple

func CreateSimple() (*Handle, error)

CreateSimple creates an nvjpeg handle with default backend and default memory allocators.

Returns handle : Codec instance, use for other calls

func (*Handle) GetDeviceMemoryPadding

func (h *Handle) GetDeviceMemoryPadding() (uint, error)

GetDeviceMemoryPadding - Retrieve the device memory padding that is currently used for the specified library handle.

func (*Handle) GetPinnedMemoryPadding

func (h *Handle) GetPinnedMemoryPadding() (uint, error)

GetPinnedMemoryPadding -Retrieve the pinned host memory padding that is currently used for specified library handle.

func (*Handle) SetDeviceMemoryPadding

func (h *Handle) SetDeviceMemoryPadding(padding uint) error

SetDeviceMemoryPadding -Use the provided padding for all device memory allocations with specified library handle. A large number will help to amortize the need for device memory reallocations when needed.

func (*Handle) SetPinnedMemoryPadding

func (h *Handle) SetPinnedMemoryPadding(padding uint) error

SetPinnedMemoryPadding -Use the provided padding for all pinned host memory allocations with specified library handle. A large number will help to amortize the need for pinned host memory reallocations when needed.

type Image

type Image C.nvjpegImage_t

Image is of type C.nvjpegImage_t.

func CreateImageDest

func CreateImageDest(o OutputFormat, pitch, height []int32, allocator gocu.Allocator) (*Image, error)

CreateImageDest allocates cuda memory of an empty Image for Decode methods.

The size of pitch needs to be at least the size of the width.

BGRI and RGBI are not offically supported because didn't give a c I marked them in the table with a question mark

This is not an official nvjpeg function. I made this just for you. Maybe me too, but I didn't have to put it here with the bindings. So, I guess I could say I put it in here for you.

-FORMAT				|SIZE OF PITCH							|SIZE OF CHANNEL
-Y				|width[0] for c = 0       					|pitch[0]*height[0] for c = 0
-YUV				|width[c] for c = 0, 1, 2					|pitch[c]*height[c] for c = 0, 1, 2
-RGB				|width[0] for c = 0, 1, 2					|pitch[0]*height[0] for c = 0, 1, 2
-BGR				|width[0] for c = 0, 1, 2					|pitch[0]*height[0] for c = 0, 1, 2
-RGBI				|width[0]*3	  c=?         					|pitch[0]*height[0] c=?
-BGRI  				|width[0]*3	  c=?         					|pitch[0]*height[0] c=?
-Unchanged			|width[c] for c = [ 0, nComponents - 1 ]			|pitch[c]*height[c] for c = [ 0, nComponents - 1]

func (*Image) Get

func (im *Image) Get() (channel []*gocu.CudaPtr, pitch []uint32)

Get gets the underlying values of image

Get will return the channels that were decoded.

type InputFormat

type InputFormat C.nvjpegInputFormat_t

InputFormat are input format flags for nvjpeg

Flag can be changed using methods

func (*InputFormat) BGR

func (fmt *InputFormat) BGR() InputFormat

BGR -Input is BGR - will be converted to YCbCr before encoding.

Method changes the underlying value of the flag and returns it

func (*InputFormat) BGRI

func (fmt *InputFormat) BGRI() InputFormat

BGRI - Input is interleaved BGR - will be converted to YCbCr before encoding.

Method changes the underlying value of the flag and returns it

func (*InputFormat) RGB

func (fmt *InputFormat) RGB() InputFormat

RGB - Input is RGB - will be converted to YCbCr before encoding.

Method changes the underlying value of the flag and returns it

func (*InputFormat) RGBI

func (fmt *InputFormat) RGBI() InputFormat

RGBI - Input is interleaved RGB - will be converted to YCbCr before encoding.

Method changes the underlying value of the flag and returns it

type JpegDecoder

type JpegDecoder struct {
	// contains filtered or unexported fields
}

JpegDecoder is used for decoding images

func CreateDecoder

func CreateDecoder(h *Handle, implementation Backend) (decoder *JpegDecoder, err error)

CreateDecoder - creates decoder implementation

func (*JpegDecoder) DecodeDevice

func (j *JpegDecoder) DecodeDevice(h *Handle, state *JpegState, dest *Image, s gocu.Streamer) (err error)

func (*JpegDecoder) DecodeHost

func (j *JpegDecoder) DecodeHost(h *Handle, state *JpegState, param *DecodeParams, stream *JpegStream) (err error)

DecodeHost - starts decoding on host and save decode parameters to the state

func (*JpegDecoder) JpegSupported

func (d *JpegDecoder) JpegSupported(s *JpegStream, param *DecodeParams) (supported bool, err error)

JpegSupported if supported == true then decoder is capable to handle jpeg sream with the specified params

func (*JpegDecoder) TransferToDevice

func (j *JpegDecoder) TransferToDevice(h *Handle, state *JpegState, jpgstream *JpegStream, s gocu.Streamer) error

TransferToDevice - hybrid stage of decoding image, involves device async calls note that jpeg stream is a parameter here - because we still might need copy parts of bytestream to device

type JpegState

type JpegState struct {
	// contains filtered or unexported fields
}

JpegState is Opaque jpeg decoding state handle identifier - used to store intermediate information between deccding phases

func CreateDecoderState

func CreateDecoderState(h *Handle, decoder *JpegDecoder) (state *JpegState, err error)

func CreateJpegState

func CreateJpegState(h *Handle) (*JpegState, error)

CreateJpegState creates an initialized decode state

func (*JpegState) AttachDeviceBuffer

func (j *JpegState) AttachDeviceBuffer(d *DeviceBuffer) error

func (*JpegState) AttachPinnedBuffer

func (j *JpegState) AttachPinnedBuffer(p *PinnedBuffer) error

func (*JpegState) Decode

func (j *JpegState) Decode(h *Handle, data []byte, frmt OutputFormat, dest *Image, s gocu.Streamer) error

Decode does the nvjpegDecode

Decodes single image. Destination buffers should be large enough to be able to store
output of specified format. For each color plane sizes could be retrieved for image using nvjpegGetImageInfo()
and minimum required memory buffer for each plane is nPlaneHeight*nPlanePitch where nPlanePitch >= nPlaneWidth for
planar output formats and nPlanePitch >= nPlaneWidth*nOutputComponents for interleaved output format.

Function will perform an s.Sync() before returning.

IN/OUT     h             : Library handle

IN         data          : Pointer to the buffer containing the jpeg image to be decoded.

IN         fmt           : Output data format. See nvjpegOutputFormat_t for description

IN/OUT     dest	 	 : Pointer to structure with information about output buffers. See nvjpegImage_t description.

IN/OUT     s             : gocu.Streamer where to submit all GPU work

func (*JpegState) DecodeBatched

func (j *JpegState) DecodeBatched(h *Handle, data [][]byte, dest []*Image, s gocu.Streamer) error

DecodeBatched - Decodes batch of images. Output buffers should be large enough to be able to store outputs of specified format, see single image decoding description for details. Call to nvjpegDecodeBatchedInitialize() is required prior to this call, batch size is expected to be the same as parameter to this batch initialization function.

IN/OUT h : Library handle

IN data : Slice of byte slices of input buffers containing the jpeg images to be decoded.

IN/OUT destinations : Array of pointers to structure with information about output buffers. len(dest) == len(data)

IN/OUT s : gocu.Streamer where to submit all GPU work

Function will perform an s.Sync() before returning.

func (*JpegState) DecodeBatchedInitialize

func (j *JpegState) DecodeBatchedInitialize(h *Handle, batchsize, maxCPUthreads int, frmt OutputFormat) error

DecodeBatchedInitialize - Resets and initizlizes batch decoder for working on the batches of specified size Should be called once for decoding batches of this specific size, also use to reset failed batches

IN/OUT h : Library handle

IN batchsize : Size of the batch

IN maxCPUthreads : Maximum number of CPU threads that will be processing this batch

IN frmt : Output data format. Will be the same for every image in batch

func (*JpegState) DecodeBatchedPhase1

func (j *JpegState) DecodeBatchedPhase1(h *Handle, data []byte, imageidx, threadidx int, s gocu.Streamer) error

DecodeBatchedPhase1 - nvjpegDecodePlanarBatchedCPU should be called [batch_size] times for each image in batch. This function is thread safe and could be called by multiple threads simultaneously, by providing thread_idx (thread_idx should be less than max_cpu_threads from nvjpegDecodeBatchedInitialize()) If error is received restart at phase one.

Function will perform an s.Sync() before returning.

func (*JpegState) DecodeBatchedPhase2

func (j *JpegState) DecodeBatchedPhase2(h *Handle, s gocu.Streamer) error

DecodeBatchedPhase2 - nvjpegDecodePlanarBatchedMixed. Any previous call to nvjpegDecodeBatchedGPU() should be done by this point

If error is received restart at phase one.

Function will perform an s.Sync() before returning.

func (*JpegState) DecodeBatchedPhase3

func (j *JpegState) DecodeBatchedPhase3(h *Handle, dest []*Image, s gocu.Streamer) error

DecodeBatchedPhase3 - nvjpegDecodePlanarBatchedGPU

If error is received restart at phase one.

Function will perform an s.Sync() before returning.

func (*JpegState) DecodePhase1

func (j *JpegState) DecodePhase1(h *Handle, data []byte, frmt OutputFormat, s gocu.Streamer) error

DecodePhase1 - CPU processing

If error is received restart at phase one

Function will not call an s.Sync(). It will be advised to sync your stream before you go to DecodePhase2.

func (*JpegState) DecodePhase2

func (j *JpegState) DecodePhase2(h *Handle, s gocu.Streamer) error

DecodePhase2 - Mixed processing that requires interaction of both GPU and CPU.

Any previous call DecodeGPU() with same handle should be finished before this call, i.e Sync() using a gocu.Streamer.

DecodePhase1 must be ran before DecodePhase2.

If error is received restart at phase one.

Function will not call an s.Sync(). It will be advised to sync your stream before you go to DecodePhase3.

func (*JpegState) DecodePhase3

func (j *JpegState) DecodePhase3(h *Handle, dest *Image, s gocu.Streamer) error

DecodePhase3 - GPU processing.

DecodePhase2() must be ran before DecodePhase3().

If error is received restart at phase one.

Function will not call an s.Sync(). It will be advised to sync your stream before you do anything with the dest image.

type JpegStream

type JpegStream struct {
	// contains filtered or unexported fields
}

JpegStream - handle that stores stream information metadata, encoded image parameters, encoded stream parameters stores everything on CPU side. This allows us parse header separately from implementation and retrieve more information on the stream. Also can be used for transcoding and transfering metadata to encoder

func CreateJpegStream

func CreateJpegStream(h *Handle) (stream *JpegStream, err error)

CreateJpegStream creates a Jpeg Stream

func (*JpegStream) GetChromaSubsampling

func (s *JpegStream) GetChromaSubsampling() (subsamp ChromaSubsampling, err error)

func (*JpegStream) GetComponentDims

func (s *JpegStream) GetComponentDims(componenet uint32) (width, height uint32, err error)

func (*JpegStream) GetComponentsNum

func (s *JpegStream) GetComponentsNum() (num uint32, err error)

func (*JpegStream) GetFrameDimensions

func (s *JpegStream) GetFrameDimensions() (width, height uint32, err error)

func (*JpegStream) Parse

func (s *JpegStream) Parse(h *Handle, data []byte, saveMetaData, saveStream bool) error

type OutputFormat

type OutputFormat C.nvjpegOutputFormat_t

OutputFormat specifies what type of output user wants for image decoding

Flag can be changed using methods

func (*OutputFormat) BGR

func (o *OutputFormat) BGR() OutputFormat

BGR convert to planar BGR.

Method changes underlying value of the type and returns it.

func (*OutputFormat) BGRI

func (o *OutputFormat) BGRI() OutputFormat

BGRI convert to interleaved BGR and write to 1-st channel of nvjpegImage_t.

Method changes underlying value of the type and returns it.

func (*OutputFormat) RGB

func (o *OutputFormat) RGB() OutputFormat

RGB convert to planar RGB.

Method changes underlying value of the type and returns it.

func (*OutputFormat) RGBI

func (o *OutputFormat) RGBI() OutputFormat

RGBI convert to interleaved RGB and write to 1-st channel of nvjpegImage_t.

Method changes underlying value of the type and returns it.

func (*OutputFormat) Unchanged

func (o *OutputFormat) Unchanged() OutputFormat

Unchanged returns decoded image as it is - write planar output.

Method changes underlying value of the type and returns it.

func (*OutputFormat) Y

func (o *OutputFormat) Y() OutputFormat

Y return luma component only, write to 1-st channel of nvjpegImage_t.

Method changes underlying value of the type and returns it.

func (*OutputFormat) YUV

func (o *OutputFormat) YUV() OutputFormat

YUV returns planar luma and chroma.

Method changes underlying value of the type and returns it.

type PinnedBuffer

type PinnedBuffer struct {
	// contains filtered or unexported fields
}

PinnedBuffer buffer for pinned host memory

func CreateNonUnifiedPinnedBuffer

func CreateNonUnifiedPinnedBuffer(h *Handle) (pbuff *PinnedBuffer, err error)

func CreateUnifiedPinnedBuffer

func CreateUnifiedPinnedBuffer(h *Handle) (pbuff *PinnedBuffer, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL