Selecting codec by endpoint subnet

Is there a way to select codec according endpoint subnet?

For example:
Subnet A - Endpoint 1 and 2
Subnet B - Endpoint 3 and 4

Endpoint 1 talking with endpoint 2 use alaw.
Endpoint 3 talking with endpoint 4 use alaw.
Endpoint 1 talking with endpoint 3 use g729.

thanks for your attention