What is the maximum and minimum of 'q' value in ffmpeg?

Without going too much in the details of video encoding, you can look at both Quantization Parameters (QP) or the Lambda parameter to express the quality of an encoded frame.

This figure was taken from Xiaoyin, Cheng – Subjectively Optimized HDTV Video Coding, 2009. You can think of Lambda as a the Lagrangian multiplier in the Rate-Distortion Optimization process.

If you take a macroblock and encode it with a low QP, the distortion (D) in comparison to the original image will be low, but the bit rate (R) will be high. Likewise, if you choose a high QP, the distortion will be high, but the bit rate will be low. So, the cost of compressing a frame is calculated by J = D + λR – and this cost needs to be minimized.

In your specific case, I'm fairly certain that enc->coded_frame->quality is assigned Lambda values from the encoder. With the FF_QP2LAMBDA constant you can convert to Lambda units, e.g. let QP = 21, then λ = 21 × FF_QP2LAMBDA. The other way round, you get the QP by QP = λ / FF_QP2LAMBDA, which is what you're seeing in the output.

For example, in H.264, the basic relation is λ = 0.85 × 2(QP-12)/3. Since QP values range from 0 to 51, you get Lambda values between 0.053 and 6,963.2 – but FFmpeg will output the QP values anyway, by converting Lambda back into QP.

Now, when you say "range of quality value is 1 to 32,767", you mean the range of possible Lambda values. Since the Lagrangian function only comes into play after the QP is applied to the coded macroblock, there is a practical limit to the maximum number that coded_picture->quality can get. For example, in MPEG-4 Part 2, QP values go from 1–31, and like I said above, in MPEG-4 Part 10, it's from 0–51. In return, the q= will output the correct QP again, because the possible Lambda values are limited by what QP you chose before.