Welcome to wordorigins!
The “cod” in “codpiece” isn’t “cod” the fish*. “Cod” is an old English word meaning “bag”, now obsolete, but cited in the OED from around 1000 up through the 18th century (in later uses meaning a purse, i.e., a bag for money). The relation between bag and scrotum I trust is clear.
*Actually, the name of the fish might be derived from “cod” = bag. The etymology is uncertain, but the OED says: “One suggestion is that this is the same word as COD n.1, as if = ‘bag-fish’, from its appearance.” (The cod has a large mouth and belly, and if held head up with the mouth gaping open, does bear a certain resemblance to a sack or bag.)