xref: /aosp_15_r20/external/libyuv/util/yuvconstants.c (revision 4e366538070a3a6c5c163c31b791eab742e1657a)
1 /*
2  *  Copyright 2021 The LibYuv Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS. All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <math.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 // This utility computes values needed to generate yuvconstants based on
17 // white point values.
18 // The yuv formulas are tuned for 8 bit YUV channels.
19 
20 // See Also
21 // https://mymusing.co/bt601-yuv-to-rgb-conversion-color/
22 
23 // BT.709 full range YUV to RGB reference
24 //  R = Y               + V * 1.5748
25 //  G = Y - U * 0.18732 - V * 0.46812
26 //  B = Y + U * 1.8556
27 //  KR = 0.2126
28 //  KB = 0.0722
29 
30 // // Y contribution to R,G,B.  Scale and bias.
31 // #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
32 // #define YB 32    /* 64 / 2 */
33 //
34 // // U and V contributions to R,G,B.
35 // #define UB 113 /* round(1.77200 * 64) */
36 // #define UG 22  /* round(0.34414 * 64) */
37 // #define VG 46  /* round(0.71414 * 64) */
38 // #define VR 90  /* round(1.40200 * 64) */
39 //
40 // // Bias values to round, and subtract 128 from U and V.
41 // #define BB (-UB * 128 + YB)
42 // #define BG (UG * 128 + VG * 128 + YB)
43 // #define BR (-VR * 128 + YB)
44 
main(int argc,const char * argv[])45 int main(int argc, const char* argv[]) {
46   if (argc < 3) {
47     printf("yuvconstants [KR] [KB]\n");
48     printf("  e.g. yuvconstants 0.2126 0.0722\n");
49     printf("  MC BT          KR           KB\n");
50     printf("  1  BT.709      KR = 0.2126; KB = 0.0722\n");
51     printf("  4  FCC         KR = 0.30;   KB = 0.11\n");
52     printf("  6  BT.601      KR = 0.299;  KB = 0.114\n");
53     printf("  7  SMPTE 240M  KR = 0.212;  KB = 0.087\n");
54     printf("  9  BT.2020     KR = 0.2627; KB = 0.0593\n");
55     return -1;
56   }
57   float kr = (float)atof(argv[1]);
58   float kb = (float)atof(argv[2]);
59   float kg = 1 - kr - kb;
60 
61   float vr = 2 * (1 - kr);
62   float ug = 2 * ((1 - kb) * kb / kg);
63   float vg = 2 * ((1 - kr) * kr / kg);
64   float ub = 2 * (1 - kb);
65 
66   printf("Full range\n");
67   printf("R = Y                + V * %5f\n", vr);
68   printf("G = Y - U * %6f - V * %6f\n", ug, vg);
69   printf("B = Y + U * %5f\n", ub);
70 
71   printf("KR = %4f; ", kr);
72   printf("KB = %4f\n", kb);
73   // printf("KG = %4f\n", kg);
74   // #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
75   // #define YB 32    /* 64 / 2 */
76   //
77   // // U and V contributions to R,G,B.
78 
79   printf("UB %-3.0f /* round(%f * 64 = %8.4f) */\n", round(ub * 64), ub, ub * 64);
80   printf("UG %-3.0f /* round(%f * 64 = %8.4f) */\n", round(ug * 64), ug, ug * 64);
81   printf("VG %-3.0f /* round(%f * 64 = %8.4f) */\n", round(vg * 64), vg, vg * 64);
82   printf("VR %-3.0f /* round(%f * 64 = %8.4f) */\n", round(vr * 64), vr, vr * 64);
83 
84   vr = 255.f / 224.f * 2 * (1 - kr);
85   ug = 255.f / 224.f * 2 * ((1 - kb) * kb / kg);
86   vg = 255.f / 224.f * 2 * ((1 - kr) * kr / kg);
87   ub = 255.f / 224.f * 2 * (1 - kb);
88 
89   printf("\nLimited range\n");
90   printf("R = (Y - 16) * 1.164                + V * %5f\n", vr);
91   printf("G = (Y - 16) * 1.164 - U * %6f - V * %6f\n", ug, vg);
92   printf("B = (Y - 16) * 1.164 + U * %5f\n", ub);
93 
94   // printf("KG = %4f\n", kg);
95   // #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
96   // #define YB 32    /* 64 / 2 */
97   //
98   // // U and V contributions to R,G,B.
99 
100   printf("UB %-3.0f /* round(%f * 64 = %8.4f) */\n", round(ub * 64), ub, ub * 64);
101   printf("UG %-3.0f /* round(%f * 64 = %8.4f) */\n", round(ug * 64), ug, ug * 64);
102   printf("VG %-3.0f /* round(%f * 64 = %8.4f) */\n", round(vg * 64), vg, vg * 64);
103   printf("VR %-3.0f /* round(%f * 64 = %8.4f) */\n", round(vr * 64), vr, vr * 64);
104 
105   return 0;
106 }
107