Flush and Restart Encoder

Hello,

Tesla K20X, using the 352.101 on Linux.

I’m a bit lost on how to properly start encoding another stream after flushing the encoder.

Scenario 1:
Stream 1: create encoder and input/output buffers
Stream 1: encode normally
Stream 1: submit an EOS by setting NV_ENC_PIC_PARAMS::encodePicFlags = NV_ENC_PIC_FLAG_EOS
Stream 1: process pending packets
Stream 2: destroy input/output buffers and encoder
Stream 2: create new encoder and input/output buffers
Stream 2: encode normally
Stream 2: submit an EOS…

Scenario 1 works fine, and all of the raw h264 streams can be muxed to mp4 and viewed without issue.

Scenario 2:
Stream 1: create encoder and input/output buffers
Stream 1: encode normally
Stream 1: submit an EOS
Stream 1: process pending packets
Stream 2: for the first frame, set encodePicFlags = NV_ENC_PIC_FLAG_FORCEINTRA
Stream 2: encode normally
Stream 2: submit EOS
Stream 2: process pending packets

Stream N: destroy input/output buffers and encoder

For Scenario 2, the very first stream works fine. FFmpeg throws “[h264 @ 0x235e860] non-existing PPS 0 referenced” errors for every other stream, which implies that the first keyframe (and maybe more) is missing.

The programming guide implies that Scenario 2 should work: “EOS notification effectively flushes the encoder. This can be called multiple times in a single encode session.” However, it doesn’t say that anything is needed besides sending an EOS and finishing the output processing. Any ideas what I might be missing?

For anyone else that stumbles on this, I found a solution (for K20X with 352.101 drivers and SDK v5, at least). What ended up working for me was setting the NV_ENC_PIC_FLAG_FORCEIDR and NV_ENC_PIC_FLAG_OUTPUT_SPSPPS flags in the NV_ENC_PIC_PARAMS::encodePicFlags structure entry for the first frame of the new stream, as described in Section 3.2 “Configuring Per-Frame Encode Parameters” of the programming guide. I also tried reconfiguring the encoder via nvEncReconfigureEncoder, but that ended up being unnecessary. I think the programming guide assumes that the reader should already know that those parameters have to be set, which isn’t super helpful for people like myself who aren’t video encoding experts.