Coupled-channel calculations of the giant dipole resonance in the one-particle-one-hole continuum approximation (I). 12C and 40Ca