AFMA works by applying an offset to the calculated amount of lens movement needed as computed by a phase detection AF system. It is used to compensate for several possible focus errors:
- Differences in optical distance from a lens to a dedicated AF sensor compared to the optical distance from a lens to the imaging sensor¹
- Differences in the exact amount a lens actually moves its focusing elements versus the exact amount of movement the camera instructs the lens to move¹
- The effect of minor variations in lens registration distance¹ that will affect various focal lengths (with a zoom lens) and focus distances (with both zooms and primes) differently
There are various different ways that AFMA is implemented in different Canon camera models. There are also various ways other camera manufacturers do the same thing under different names. There are even different ways that lens manufacturers now offer end users the ability to adjust certain lenses to match them to a specific camera body using a USB dock and software applications.
Autofocus is closed loop if using contrast detect, so AFMA should be unnecessary for contrast detect autofocus because the focus is actually confirmed in contrast detect autofocus, at the imaging sensor.
This is usually, but not always, the case. There are some implementations of AF using the main imaging sensor that are prioritized for speed and do not use a fully 'closed-loop' method.
Open loop autofocus such as phase detect autofocus calculates the predicted adjustment to the focus based on the current position and the predicted optimal position, so if this doesn't work, how much it will be off is dependent on the current position of the focusing.
That is one way, but far from the only way, that an uncompensated 'open-loop' PDAF system can produce an error. Pretty much all AFMA implementations are smart enough to compensate for the amount of difference between the lens' initial position and the desired position to avoid the error you are predicting.
Is AFMA per-lens or a global adjustment?
Most all camera based AFMA (or AF Fine-Tune in Nikon nomenclature, or whatever anyone else calls it) versions I've seen allow for choosing either option. Some implementations also allow for different correction values to be used for the same lens when it is at different focal lengths or even at different focus distances.
How many lenes a camera can "remember" will vary by camera model. I think the top-tier Canon models allow storing AFMA information for up to 50 or so different lenses at one time. I've never exceeded whatever the maximum number of lenses is on any of my AFMA capable camera bodies.
The simplest version of Canon's AFMA allows only a single value to be entered for each lens. I've got an older Canon prime lens that needs slightly different adjustment values when shooting at close distances than it needs when shooting at greater distances. Say, on one camera it is '-3' at close focus distances approaching MFD and '-7' at longer distances approaching infinity. It's not ideal, but being able to adjust the AFMA correction to fit whatever range of subject distances I'm using works a lot better than leaving the lens totally uncorrected. And even if I forget to change it from '-5' that I usually set it at right before I remove the lens from that camera, '-5' will get me closer to the ideals of either '-3' or '-7' than '0' will when shooting at any distances.
Other versions of Canon's AFMA on newer and/or higher models allow zoom lenses to have two values entered: one for the widest end of the focal length range and another for the longest end of the focal length range. The amount of offset applied for intermediate focal lengths is then interpolated by the camera based on those two values at either extreme. Canon doesn't reveal whether this interpolation is linear or exponential/logarithmic, but whatever method they use, it works fairly well most of the time.
With camera based AFMA as described above, any adjustments are stored in the camera. If the same lens is placed on a different camera, none of the AFMA adjustments made on the first camera will carry over. Since AFMA corrects for individual variations in both the camera and lens, the same lens may need different correction values when used with a different camera body, even if both cameras are the same model.
Third party lens manufacturers Sigma and Tamron allow some of their lenses to be adjusted, via a USB dock that connects the lens to a computer running the manufacturer's calibration adjustment software, using different values entered for each of several focal length ranges and each of several focus distance ranges. The limits of the specific ranges can even be adjusted with some such lenses. In the case of third party lenses, the calibrations are made by the lens, which "translates" the amount of focus movement instructed by the camera to a different amount of movement. This involves fairly sophisticated LUTs (look-up tables) and possibly interpolation of intermediate values between the data points contained in the LUTs.
With the USB dock used by Sigma and Tamron, the correction data is stored in the lens. If the lens is used on a different camera body than the one for which it was calibrated, the results might not be ideal. Any variations caused by the lens should be the same, but any variations caused by the camera will be different.
Can AFMA actually fully correct issues in poor-focusing lens+body combinations?
No AF system, whether PDAF or CDAF, is perfect. If the camera could perfectly calculate the correct amount of movement and tell the lens exactly how far to move, the actual movement of the lens will have some degree of variation from that instructed amount. Even with a fully closed-loop system, there might be slight mechanical movement in the lens due to gravity or other external sources of acceleration between the time the camera is focused and the image is exposed.
The most sophisticated current implementations of AF calibration can get very close to the goal of fully correcting for the most common causes of focusing errors.
Roger Cicala, the founder and chief lens guru at lensrentals.com, wrote a series of blog entries back in 2012 where he measured the standard shot-to-shot deviation of certain lenses coupled with certain cameras. Some camera model/lens combinations had much less variation from shot-to-shot than other camera model/lens combinations. Although the series is about shot-to-shot variation, rather than AFMA, you might find much of it enlightening as he explains how PDAF (hopefully) works:
And: How Auto Focus (Often) Works
Also: Are zooms always sharper at one end than the other?
What actually is AFMA adjusting? Is it an additive offset between the predicted position and the actually chosen position? Or is it a multiplicative correction for the amount how much focus is adjusted?
That all depends on the specific implementation in a specific camera model. Both of the methods you suggest, as well as even more sophisticated methods, are used by various implementations.
Is AFMA used for contrast detect autofocus?
Until very recently it has not been used for main imaging sensor based AF systems. However, some mirrorless cameras (Sony α7 series, some Olympus models, Nikon Z series) are now beginning to implement some aspects of it to account for the variation between how much different copies of the same lens model actually move when instructed by the camera to move a specific amount. It is also used to account for lens element displacement errors or when using lens mount adapters like those made by Metabones.
Is it used for Canon's dual pixel autofocus?
I have not seen any reference that says Canon is currently (as of early April 2019) using any form of AFMA with Dual Pixel CMOS AF. Perhaps it will eventually find its way to the new RF mount series of mirrorless cameras.
How on earth can AFMA work if the operation of the AF system is dependent on the current focus position? I mean, if the focusing isn't working, shouldn't the amount off be dependent on the current focus position? So, if you are photographing an object/person 2 meters away, the current focus position is 1 meters away, it is different than it would be if the current focus position was at infinity.
Because not all focus errors are attributable to the same cause. Remember our list at the top of this answer? Different implementations of AFMA use different corrective methods to deal with these various sources of AF error. Most AFMA implementations are far more sophisticated than what you seem to be giving them credit for being capable of doing. They are more than capable of interpolating amounts of correction based on how far out of focus the lens is measured to be at the initial AF measurement.
¹ These variations can be due to manufacturing tolerances for both the camera and lens as well as wear over the course of use. This includes camera/lens mating surfaces, PDAF sensor position in the camera, surfaces of the cuts in helicoid collars, the rollers that move within those cuts, etc.
Suggestions for further reading:
How does the lens calibration feature of Sigma Art lenses work?
Can I adjust the autofocusing of my Canon 6D Mk II myself?
Which offers better results: FoCal or LensAlign Pro?
How can lens cause consistent front or back focus?
How important is body microcalibration feature?
Canon 7DM2 w Sigma 150-600 Sport - Lens micro-adjustment