How is the en-/decoding of the optOut-URL done ? (algorithm)
Welcome to SE!
You can see the construction of the opt-out URL here and the processing of it here

'jid' is the Job id
'qid' is the Queue id
'h' is the Hash

The contact_id is not included in the URL but can be looked up via the qid.