Upon further investigation, I’ve found what was causing the library to try and free a non-existent dma buffer, although I don’t really understand why. In my dequeue thread, I was using select() to query when the encoder was ready for me to dequeue the buffer.
After swapping over to V4L2_CID_MPEG_VIDEO_DEVICE_POLL, this no longer occurred, however the time to encode a frame increased from 6ms to 40ms. As this was unacceptable, I’ve ended with a compromise of using select() for the runtime operation, but when I close the encoder, I dequeue all capture buffers using the V4L2_CID_MPEG_VIDEO_DEVICE_POLL method.
However, extended testing of this problem has led to other issues.
Over the course of opening → encoding → closing the encoder ~ 300 times, the memory usage of the system increases until it reaches about 3.5G used (2.5 of this seems to be the encoder), then it crashes and refuses to encode any more frames, throwing a whole lot of warning messages.
Opening in O_NONBLOCKING MODE
Opening in O_NONBLOCKING MODE
NvMMLiteOpen : Block : BlockType = 8
NvMMLiteOpen : Block : BlockType = 8
===== NVMEDIA: NVENC =====
===== NVMEDIA: NVENC =====
NvMMLiteBlockCreate : Block : BlockType = 8
NvMMLiteBlockCreate : Block : BlockType = 8
NVMEDIA: NvMMAttributeVideoEnc_QPRange Get Parameter
VENC: NvMMLiteVideoEncSetAttribute: 4543: NvMMLiteAttributeVideoEnc_QPRange: Bad Parameter
NVMEDIA: NvMMAttributeVideoEnc_QPRange Get Parameter
VENC: NvMMLiteVideoEncSetAttribute: 4543: NvMMLiteAttributeVideoEnc_QPRange: Bad Parameter
NVMEDIA: H265 : Profile : 1
NVMEDIA: H265 : Profile : 1
[ 839.132964] iommu_context_dev 13e10000.host1x:ctx2: pin_array_ids: could not map attachment err=-12
[ 839.132991] iommu_context_dev 13e10000.host1x:ctx2: pin_array_ids: could not map attachment err=-12
[ 839.132997] falcon 15340000.vic: nvhost_ioctl_channel_submit: failed with err -12
NvRmChannelSubmit: NvError_IoctlFailed with error code 22
[ 839.133049] falcon 15340000.vic: submit_add_gathers: failed to copy user inputs: class_ids=0000000080a3d038 num_cmdbufs=2
[ 839.133051] falcon 15340000.vic: nvhost_ioctl_channel_submit: failed with err -22
[ 839.142579] iommu_context_dev 13e10000.host1x:ctx2: pin_array_ids: could not map attachment err=-12
[ 839.142586] falcon 154c0000.nvenc: nvhost_ioctl_channel_submit: failed with err -12
[ 839.142597] falcon 154c0000.nvenc: submit_add_gathers: failed to copy user inputs: class_ids=0000000080a29038 num_cmdbufs=2
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 13, SyncPointValue = 0)
[ 839.142599] falcon 154c0000.nvenc: nvhost_ioctl_channel_submit: failed with err -22
[ 839.225692] falcon 15340000.vic: nvhost_ioctl_channel_submit: failed with err -12
[ 839.235305] falcon 15340000.vic: submit_add_gathers: failed to copy user inputs: class_ids=0000000080a33038 num_cmdbufs=2
NvRmChannelSubmit: NvError_IoctlFailed with error code 22
[ 839.248938] falcon 15340000.vic: nvhost_ioctl_channel_submit: failed with err -22
[ 839.259753] iommu_context_dev 13e10000.host1x:ctx2: pin_array_ids: could not map attachment err=-12
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 13, SyncPointValue = 0)
[ 839.271986] falcon 154c0000.nvenc: nvhost_ioctl_channel_submit: failed with err -12
NvRmChannelSubmit: NvError_IoctlFailed with error code 22
[ 839.287550] falcon 154c0000.nvenc: submit_add_gathers: failed to copy user inputs: class_ids=0000000080a15038 num_cmdbufs=2
[ 839.301811] falcon 154c0000.nvenc: nvhost_ioctl_channel_submit: failed with err -22
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 11, Sync PointValue = 0)
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 12, SyncPointValue = 0)
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 13, SyncPointValue = 0)
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 13, SyncPointValue = 0)
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 12, SyncPointValue = 0)
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 12, SyncPointValue = 0)
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 12, SyncPointValue = 0)
It repeats this until the encoder is closed and I attempt to reopen it, before following the whole process again.