The easyest way to do this, is to use cascaded shift Registers with latch like the 74HC595.
They can be cascaded an need only a few connections to the controller ( RESET, DATA, CLOCK, OE, CS ).
For the Relays I'll suggest You, to use Darlington Drivers like the ULN2803 between the HC595 and the Relais.
The needed Free Wheeling Diodes are included there too.
For the Optocouplers you can use a parallel in - serial Out Shift Register. For a useful Type you had to google a little bit.
So you can built your Project with a ATMEGA 8 Controller when want.
For the input i would take the Hardware SPI of the Controller, because it works 4 to 8 times faster then an Software SPI and relais are very slow for a Microcontroller.