avcodec: refactor avcodec_encode_audio2() to merge common branches