Returns
0 on success, otherwise negative error code: AVERROR(EAGAIN): output is not available in the current state - user must try to send input AVERROR_EOF: the encoder has been fully flushed, and there will be no more output packets AVERROR(EINVAL): codec not opened, or it is an encoder other errors: legitimate decoding errors
EAGAIN isn't a fatal error, yet you treat it as one.
On a more general point, maximise the information in your error messages. Like always printing the result of whatever comparison failed.
Examples:
encode_audio.c, encode_video.c, transcode_aac.c, vaapi_encode.c, and vaapi_transcode.c.