Color Transform Engine(CTE)
前言:
Color Transform Engine(CTE)
前言:
這是2009年CIC的題目,內容是設計一個YUV跟RGB的轉換電路。 RGB大家都很熟了就不介紹了,YUV是一種色彩編碼方法,很常使用在各個影像處理單元中。 Y代表明亮度(Luminance),U代表色度(Chrominance),V代表濃度(Chroma)。這題主要的難度是負數以及小數的處理(包括四捨五入)
Preface :
The main goal is to Design a transform circuit between YUV and RGB. I believe everyone is familiar with RGB so I am not going to introduce it. YUV is the color model which is easy to be found in image process unit. In the YUV color model, Y stands for Luminance (brightness) component, while U and V represent the Chrominance (color) components. The main difficulty of this problem lies in handling negative numbers and decimals (including rounding)
限制 :
題目中有許多限制在設計中需要遵守,以下為題目中的限制
- U值的輸入從-117 ~ 117、 Y值的輸入從0 ~ 255、V值的輸入從-111 ~ 111,輸入順序為U、Y、V、Y。

圖(一) YUV訊號個別輸入之順序(UYVY格式)
- 輸入為8bits,輸出為24bits,其中RGB各佔8bits

圖(二) RGB訊號輸出格式定義
- RGB值若大於255則輸出255 ; 若為負數則輸出0; 若有小數點則四捨五入

圖(三) CTE正確輸出值之範例
Restriction :
There are some restrictions need to follow in the design.
- The input value of U is between -117 to 117, input value of Y is between 0 to 255 and input value of V is between -111 to 111. The sequence of input is U, Y, V, Y.

Fig 1. Sequence of input.
- input should be 8 bits and output should be 24 bits, RGB take 8 bits respectively.

Fig 2. Definition of 24-bits outputs.
- If the value of RGB are greater than 255, output should be 255. If the value of RGB are negative, output should be 0. If the value of RGB have decimal points, output should round down.

Fig 3. Example of RGB output.
系統設計 :

圖(四) YUV to RGB 架構 Fig 4. Structure of YUV to RGB.
YUV需要經過矩陣的運算才能得到RGB值,接著再根據限制3.處理RGB值,YUV轉換矩陣如下圖所示。

圖(五) YUV轉換成RGB的矩陣 Fig 5. Matrix for transfering YUV to RGB.
矩陣的值有小數點,通常遇到小數點有兩種解法,一種是位移另一種是IEEE 754。很幸運的是這些值都可以透過乘上2的倍數變成整數,這樣讓我們可以透過位移的方式解決小數問題,將整個矩陣的數值乘上8(位移3位)可以得到一個整數的矩陣。

圖(六) 乘8之後的矩陣 Fig 6. Matrix which multiplying by 8.
輸入YUV經過矩陣運算之後得到8R、8G、8B的值後再透過後面的RGB processor 處理四捨五入以及負數問題。由於YUV輸入皆為固定順序,故採用CASE方式實現。後方的RGB processor先判斷數值是否小於0,因為前面有先左移三位,故原本要判斷有符號位的第9位也左移到第12位。接著判斷數值是否有大於255,與上述原因相同判斷第11位(8右移3)是否為1。若皆不符合以上兩種狀況及判斷是否需要四捨五入。上述數值皆採用二的補數來做處理以應付負數可能發生之情況。
module R_Processor(
input clk,
input in_valid,
input rstn,
input signed [12:0] R,
output reg[8-1 : 0] R_out,
output reg out_valid
);
always@(posedge clk) begin
if(!rstn) begin
R_out <= 0;
out_valid <= 0;
end
else if (in_valid) begin
out_valid <= 1;
if(R[12]==1) begin // value < 0
R_out <= 0;
end
else if (R[11] == 1) begin // value >255
R_out <= 255;
end
else begin
if (R[2] == 1)begin// rounding
R_out <= R[10:3] + 1;
end
else begin
R_out <= R[10:3];
end
end
end
else begin
out_valid <= 0;
end
end
endmodule
四捨五入的概念非常簡單,舉個例子,3除以2等於1.5,四捨五入即為2,3的二進位為11,除二即為右移1,接著判斷被右移的最高位元是否為1,若為1則右移後加1,結果為10,即十進制的2。
System Design :
YUV needs to be converted to RGB values through matrix operations. Then, the RGB values are processed according to constraint 3. The YUV conversion matrix is shown in the figure 5.
The matrix contains decimal values, and there are usually two common ways to handle decimals: one is using bit shifting, and the other is using IEEE 754. Fortunately, these values can all be converted to integers by multiplying by a power of 2. This allows us to solve the decimal problem using bit shifting. By multiplying the entire matrix by 8 (which corresponds to a 3-bit shift), we can obtain an integer matrix. The matrix is shown in the figure 5.
“After the input YUV undergoes matrix operations, the values of 8R, 8G, and 8B are obtained. These values are then processed by the subsequent RGB processor to handle rounding and negative numbers. Since the YUV input always follows a fixed order, CASE is employed for implementation. The RGB processor first checks if the value is less than 0. Because the value was previously left-shifted by three bits, the 9th bit, which originally indicated the sign, is now shifted to the 12th bit. Next, it checks if the value is greater than 255. For the same reason as above, it checks if the 11th bit (8 shifted right by 3) is 1. If neither of these conditions is met, it then determines whether rounding is necessary. All the aforementioned values are processed using two’s complement to account for potential negative numbers.”
The concept of rounding is quite simple. For example, 3 divided by 2 equals 1.5, and rounding it gives 2. In binary, 3 is represented as 11. Dividing by 2 is equivalent to right-shifting by 1 bit. Then, we check the most significant bit that was shifted out—if it is 1, we add 1 to the result after the shift. In this case, right-shifting 11 gives 1, and since the shifted-out bit is 1, we add 1, resulting in 10, which is 2 in decimal.
結果 :
假設輸入為 U = -117、Y = 255、V = -111、Y = 0。計算得知輸出RGB為 4B、FF、15,與下圖中的RGB吻合。接著將Y的輸入改為0以符合 U, Y, V, Y的輸入順序,計算得知輸出RGB為 00、71、00,與下圖中的RGB吻合。從輸入開始到輸出RGB值僅花7個clock.

圖(七) 模擬結果 Fig 7. Simulation result.
Result :
Assuming the inputs are U = -117, Y = 255, V = -111, Y = 0. Calculations show the output RGB as 4B, FF, 15, which matches the RGB values in the figure 7. Next, by changing the input for Y to 0 to match the U, Y, V, Y input order, the calculated output RGB is 00, 71, 00, which also matches the RGB values in the figure 7. It only takes 7 clock cycles from input to outputting the RGB values.
메타데이터
- post_id
- 76cd8aa69a94
- slug
- color-transform-engine-cte-76cd8aa69a94
- url
- https://medium.com/@chiantsai1997/color-transform-engine-cte-76cd8aa69a94
- canonical_url
- https://medium.com/@chiantsai1997/color-transform-engine-cte-76cd8aa69a94
- author_url
- https://medium.com/@chiantsai1997
- status
- ok
- fetched_at
- 2026-06-14 11:28:49