how to set nvjpegEncoderParams_t [closed]

asked 2019-08-25 23:34:48 -0600

pittie gravatar image

I'm following the example code in nvJpeg transcode example. however, this piece of code is kind of sloppy and I have to adjust some parts to make it work. see below:

cudaStream_t stream;
// create library handle
nvjpegHandle_t handle;
nvjpegCreateSimple(&handle);
/////////////////////////////////// nvJPEG decoding ////////////////////////////////////////
// create bitstream object
nvjpegJpegStream_t jpeg_stream;
nvjpegJpegStreamCreate(handle, &jpeg_stream);
// parse jpeg stream
nvjpegJpegStreamParse(handle,
        input.data(),
        input.size(),
        1, // save metadata in the jpegStream structure
        0,
        jpeg_stream);
// create decoder and decoder state
nvjpegJpegDecoder_t decoder;
nvjpegJpegState_t decoder_state;
nvjpegDecoderCreate(handle, NVJPEG_BACKEND_DEFAULT, &decoder);
nvjpegDecoderStateCreate(handle, decoder, &decoder_state);
// create and set up decoder parameters
nvjpegDecodeParams_t decode_params;
nvjpegDecodeParamsCreate(handle, &decode_params);
nvjpegDecodeParamsSetOutputFormat(decode_params, NVJPEG_OUTPUT_RGBI);
// decode image
nvjpegImage_t output_image;
nvjpegDecodeJpegHost(handle, decoder, decoder_state, decode_params, jpeg_stream);
nvjpegDecodeJpegTransferToDevice(handle, decoder, decoder_state, jpeg_stream, stream);
nvjpegDecodeJpegTransferToDevice(handle, decoder, decoder_state, jpeg_stream, stream);
nvjpegDecodeJpegDevice(handle, decoder, decoder_state, &output_image, stream);
/////////////////////////////////// nvJPEG Transcode and encode API ///////////////////////////////////
nvjpegEncoderState_t encoder_state;
nvjpegEncoderParams_t encode_params;

int st;
st = nvjpegEncoderStateCreate(handle, &encoder_state, stream);
st = nvjpegEncoderParamsCreate(handle, &encode_params, stream);
// get encoding from the jpeg stream and copy it to the encode parameters
nvjpegJpegEncoding_t jpeg_encoding;
st = nvjpegJpegStreamGetJpegEncoding(jpeg_stream, &jpeg_encoding);
st = nvjpegEncoderParamsSetEncoding(encode_params, jpeg_encoding, stream);
// copies according data to the encode parameters
st = nvjpegEncoderParamsCopyMetadata(encoder_state, encode_params, jpeg_stream, stream);
st = nvjpegEncoderParamsCopyQuantizationTables(encode_params, jpeg_stream, stream);
st = nvjpegEncoderParamsCopyHuffmanTables(encoder_state, encode_params, jpeg_stream, stream);
// retrieve frame dimensions
unsigned width, height;
st = nvjpegJpegStreamGetFrameDimensions(jpeg_stream, &width, &height);
// encode using encode parameters
st = nvjpegEncodeImage(handle, encoder_state, encode_params, &output_image,
        NVJPEG_INPUT_RGBI, width, height, stream);
// get compressed stream size
size_t length;
st = nvjpegEncodeRetrieveBitstream(handle, encoder_state, NULL, &length,
        stream);
// get stream itself
cudaStreamSynchronize(stream);
std::vector<unsigned char> jpeg(length);
nvjpegEncodeRetrieveBitstream(handle,
        encoder_state, jpeg.data(),
        &length, 0);

It does run but the nvjpegEncodeImage function returns error code 2 - NVJPEG_STATUS_INVALID_PARAMETER. I cannot find a way to check whether my para is correct or not since I can see nothing in the nvjpegEncoderParams_t structure. Does anyone know how to set it appropriately? It seems that nvJpeg is quite new and I cannot find resources from the Internet to solve the problem.

edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant by LBerger
close date 2019-08-26 01:34:30.689407