A General Approach to Seismic Inversion with Automatic Differentiation