NVJPEG throwing NVJPEG_STATUS_INVALID_PARAMETER Error

I am trying to implement a JPEG encoding function by using the NVJPEG library. The function takes data in bitmap format and should compress it to JPEG. Unfortunately some functions eject the error codes from the title and I am not able to fix them. What I have done so far is shown below:

void encodeBmpToJpeg(unsigned char* idata, unsigned char* odata, int* p_jpeg_size, int width, int height)
{
  cudaStream_t stream;
  nvjpegImage_t source;
  nvjpegHandle_t nv_handle;
  nvjpegEncoderState_t nv_enc_state;
  nvjpegEncoderParams_t nv_enc_params;

  source.channel[0] = idata;
  source.pitch[0] = width*3;
  CUDA_CHECK(cudaStreamCreate(&stream));

  // initialize nvjpeg structures
  JPEG_CHECK(nvjpegCreateSimple(&nv_handle));
  JPEG_CHECK(nvjpegEncoderStateCreate(nv_handle, &nv_enc_state, stream));
  JPEG_CHECK(nvjpegEncoderParamsCreate(nv_handle, &nv_enc_params, stream));


  // Compress image
  JPEG_CHECK(nvjpegEncodeImage(nv_handle, nv_enc_state, nv_enc_params, &source, 
    NVJPEG_INPUT_RGBI, width, height, stream));  // Error: NVJPEG_STATUS_INVALID_PARAMETER
  JPEG_CHECK(nvjpegEncodeRetrieveBitstream(nv_handle, nv_enc_state, NULL, 
    (size_t*)p_jpeg_size, stream)); // Error: NVJPEG_STATUS_INVALID_PARAMETER
  CUDA_CHECK(cudaStreamSynchronize(stream));
  odata = (unsigned char*) malloc(*p_jpeg_size);
  JPEG_CHECK(nvjpegEncodeRetrieveBitstream(nv_handle, nv_enc_state, 
    odata, (size_t*)p_jpeg_size, 0)); // Error: NVJPEG_STATUS_INVALID_PARAMETER
  CUDA_CHECK(cudaStreamSynchronize(stream));
  CUDA_CHECK(cudaStreamDestroy(stream));
  JPEG_CHECK(nvjpegEncoderStateDestroy(nv_enc_state));
}

function parameters:
1.) idata points to the first element of a bitmap buffer (RGB888). The memory is located in the global memory of the GPU
2.) odata points to an empty buffer in the host memory. The required memory size is determined within the function and also its memory allocation.
3.) p_jpeg_size is a pointer which contains the image size of the JPEG. The value is equal to size of odata
4/5.) width and height in pixel of images

The error is thrown when calling the functions

nvjpegEncodeRetrieveBitstream(...);
nvjpegEncodeImage(...);

Addtional informations:
OS: Ubuntu 18.04 LTS
Version: cuda 10.1
GPU: Nvidia Quadro P6000

Thanks in advance!

See this post. It worked for me with nvjpegEncodeYUV().
https://devtalk.nvidia.com/default/topic/1048242/nvjpegencodeimage-in-nvjpeg-error-2/?offset=3