1 #include <nitro/math.h>
2 
3 ///////////////////////////////////////////////////////
4 // DEFINES
5 ///////////////////////////////////////////////////////
6 
7 #define Y_R      (short)0x4c8B  // 19595    unsigned
8 #define Y_G      (short)0x9645  // 38470    unsigned
9 #define Y_B      (short)0x1d2f  // 7471     signed
10 #define U_R      (short)0xd4bc  // -11075   signed
11 #define U_G      (short)0xab44  // -21692   signed
12 #define U_B      (short)0x7fff  // 32767    signed
13 #define V_R      (short)0x7fff  // 32767    signed
14 #define V_G      (short)0x94bd  // -27459   signed
15 #define V_B      (short)0xeb44  // -5308    signed
16 
17 // Blue
18 #define HYB1    (short)0x0e97   // 3735 signed
19 #define HYB2    (short)0x0e98   // 3736 signed
20 #define TYB1    (short)0x09BB   // 2491 signed
21 #define TYB2    (short)0x09BA   // 2490 signed
22 #define TYB3    (short)0x09BA   // 2490 signed
23 #define QYB1    (short)0x074c   // 1868 signed
24 #define QYB2    (short)0x074c   // 1868 signed
25 #define QYB3    (short)0x074c   // 1868 signed
26 #define QYB4    (short)0x074b   // 1867 signed
27 
28 // Red
29 #define HYR1    (short)0x2645   // 9797 signed
30 #define HYR2    (short)0x2646   // 9798 signed
31 #define TYR1    (short)0x1984   // 6532 signed
32 #define TYR2    (short)0x1984   // 6532 signed
33 #define TYR3    (short)0x1983   // 6531 signed
34 #define QYR1    (short)0x1323   // 4899 signed
35 #define QYR2    (short)0x1323   // 4899 signed
36 #define QYR3    (short)0x1323   // 4899 signed
37 #define QYR4    (short)0x1322   // 4898 signed
38 
39 // Green
40 #define HYG1    (short)0x4B23   // 19235 signed
41 #define HYG2    (short)0x4B23   // 19235 signed
42 #define TYG1    (short)0x3218   // 12824 signed
43 #define TYG2    (short)0x3217   // 12823 signed
44 #define TYG3    (short)0x3217   // 12823 signed
45 #define QYG1    (short)0x2591   // 9617 signed
46 #define QYG2    (short)0x2592   // 9618 signed
47 #define QYG3    (short)0x2591   // 9617 signed
48 #define QYG4    (short)0x2592   // 9618 signed
49 
50 #define HUG1    (short)0xd5a2   // -10846 signed
51 #define HUG2    (short)0xd5a2   // -10846 signed
52 #define HVG1    (short)0xca5f   // -13729 signed
53 #define HVG2    (short)0xca5e   // -13730 signed
54 
55 #define Ymin    -128
56 #define Ymax    127
57 #define Umin    -128
58 #define Umax    127
59 #define Vmin    -128
60 #define Vmax    127
61 
62 #define sat2s(a,b,c) (short)MATH_IMin(MATH_IMax(a,b),c)
63 
64 ///////////////////////////////////////////////////////
65 // Constants
66 ///////////////////////////////////////////////////////
67 short TblRGBConst[16]= {
68     Y_R, HYG1, HYG2, Y_B,
69     U_R, HUG1, HUG2, U_B,
70     V_R, HVG1, HVG2, V_B,
71     Y_R, HYG1, HYG2, Y_B
72 };
73 
74 // Convert block of 2*2 pixels from RGB to YUV 4:2:0
75 void DSPi_rgb565_2_yuv(unsigned char in_rgb[4][3], unsigned char out_ycb_cr[6]);
DSPi_rgb565_2_yuv(unsigned char in_rgb[4][3],unsigned char out_ycb_cr[6])76 void DSPi_rgb565_2_yuv(unsigned char in_rgb[4][3], unsigned char out_ycb_cr[6])
77 {
78 
79     short   Bx,By;
80     short   R,G,B;
81     long    Y,Cb,Cr;
82     short   ybuf_index=0;
83 
84 
85     //create Y1,Y2,Y3 & Y4
86     for (By=0;By<2;By++)
87     {
88         //PixelPosX = MB.x*16 + 1;
89         for (Bx=0;Bx<2;Bx++)
90         {
91 
92             R = (short)(in_rgb[By*2+Bx][2]-128);
93             G = (short)(in_rgb[By*2+Bx][1]-128);
94             B = (short)(in_rgb[By*2+Bx][0]-128);
95 
96             Y = (long)TblRGBConst[0]*(long)R
97                 +(long)TblRGBConst[1]*(long)G
98                 +(long)TblRGBConst[2]*(long)G
99                 +(long)TblRGBConst[3]*(long)B;
100             out_ycb_cr[ybuf_index++] = (unsigned char)(sat2s((short)(Y>>16),Ymin,Ymax)+128);
101 
102         }
103     }
104     //Create Cb Cr
105     R = (short)(in_rgb[0][2]-128);
106     G = (short)(in_rgb[0][1]-128);
107     B = (short)(in_rgb[0][0]-128);
108 
109     Cb=U_R*(long)R+HUG1*(long)G+HUG2*(long)G+U_B*(long)B;
110     Cr=V_R*(long)R+HVG1*(long)G+HVG2*(long)G+V_B*(long)B;
111     out_ycb_cr[4] =(unsigned char)(sat2s((short)(Cb>>16),Umin,Umax)+128);
112     out_ycb_cr[5] =(unsigned char)(sat2s((short)(Cr>>16),Vmin,Vmax)+128);\
113 }
114