first of all, that mode that you showed me is single nal unit mode - you CANT do that for all NAL units, (espicially IDR and SLICE ones, they're too big!) you never include the start code prefix, and what your doing is right for single nal unit mode first figure out what your max packet size is goin...