How to prepare packets from FFmpeg to decode them with NVDEC? Parser callbacks not triggered!

So this issue was opened many times before. Usually people having streams of packets and feeding them to the NVDEC. I understand that the problem with the CUVID parser callbacks not being called is usually caused by improper initialization of the stream according to this topic. According to this, this and this I understand that in h.265 there are NAL units, which are part of the encoded bitstream. They contain the information about the stream, especially SPS and PPS sections to properly setup the decoder. However, I can’t find the optimal way to solve my issue. Any assistance would be much appreciated!

So what I do now is:

  1. Encode input frames with FFmpeg and libx265
  2. Get the packet data
  3. Store it somewhere in a file and then reload in another app
  4. Send the packet data to nvdec - I am sending an I frame first followed by P frames

My question is: How do I ensure that the input packets to nvdec contain all the necessary information?
Do I need to manually create and inject the NAL units? I create the packets, they are not coming from any unknown sources, so is there any way to easily make them contain all the information? Can’t I force FFmpeg to include the necessary NAL modules in all packets? Or should I send a special packet to initialize the parser? I can even switch to raw NVENC if FFmpeg is not optimal for the encoding.

Right now this code executes with no error but none of the parser callbacks is called:

CUVIDSOURCEDATAPACKET packet{};
packet.payload = packetData;
packet.payload_size = packetSize;
packet.flags = CUVID_PKT_TIMESTAMP;
packet.timestamp = decodedNumber;
decodedNumber++;
if (packetPointer.size == 0) 
    packet.flags |= CUVID_PKT_ENDOFSTREAM;
  
if(cuvidParseVideoData(parser, &packet) != CUDA_SUCCESS)
    throw std::runtime_error("Cannot parse packet.");

If you wish to see the rest of my code, here is the WIP commit I am using right now for the tests. I am trying now to get to the point where the parser actually tries to start the decoding.

Huuge thanks to @electrodynamics who helped me solve the issue. The callbacks are triggered now! So to make things clear:

The packet data coming from FFmpeg in AVPacket can be directly used in the NVDEC. They contain the fully specified NAL units. My mistake was that the data I was sending to the decoder were shifted by a few bytes so the NAL headers were not at the beginning of the packets I passed to the parser. In such cases the parser simply ignores the input without any exception or error code!!!

A good way to check the packet for NAL is to look for the header bytes like 0 0 0 1 that should be at the beginning.

So for other people solving similar issue, the approach I described above is working! Make sure that the input data are really OK.