It’s not that complicated. There are two standards: “p” (consumer) and “K” (DCI).
if (height > width) {
swap(width, height);
}
if ((width/height) > (16/9)) {
p = width * 9/16;
K = width / 1024;
} else {
p = height;
K = (height * 2048/1080) / 1024;
}
Examples:
1) 3840 by 2160:
3840/2160 is not greater than 16/9 so goes to second branch. p becomes equal to height, i.e., 2160. K becomes equal to 2160 * ((2048/1080)/1024) i.e., exactly 4K.
2) 2560 by 1440:
It again goes to second branch and so is 1440p and exactly 2.5K
3) 5120 by 2160:
It goes to first branch this time so 5120/1024 i.e., exactly 5K
4) 1080 by 1920:
This is vertical video so first swap the width and height. Now it goes to second branch and resolution is 1080p or exactly 2K.
Hope that clears up your doubts! 😊