YCBCR基本原理:
部分传感器自己定义:
重点RGB转换YCBCR公式:
代码分几个模块分析:
1.RGB565 to RGB 888,但会造成饱和度稍微强一些,用损失图像精度来换取资源消耗
assign rgb888_r = {img_red , img_red[4:2] };
assign rgb888_g = {img_green, img_green[5:4]};
assign rgb888_b = {img_blue , img_blue[4:2] };
2.同步输出数据接口信号,处理后的
assign post_frame_vsync = pre_frame_vsync_d[2] ;
assign post_frame_hsync = pre_frame_hsync_d[2] ;
assign post_frame_de = pre_frame_de_d[2] ;
assign img_y = post_frame_hsync ? img_y1 : 8'd0;
assign img_cb = post_frame_hsync ? img_cb1: 8'd0;
assign img_cr = post_frame_hsync ? img_cr1: 8'd0;
3.加、乘、还原数据
//step1 pipeline mult
always @(posedge clk or negedge rst_n) beginif(!rst_n) beginrgb_r_m0 <= 16'd0;rgb_r_m1 <= 16'd0;rgb_r_m2 <= 16'd0;rgb_g_m0 <= 16'd0;rgb_g_m1 <= 16'd0;rgb_g_m2 <= 16'd0;rgb_b_m0 <= 16'd0;rgb_b_m1 <= 16'd0;rgb_b_m2 <= 16'd0;endelse beginrgb_r_m0 <= rgb888_r * 8'd77 ;rgb_r_m1 <= rgb888_r * 8'd43 ;rgb_r_m2 <= rgb888_r << 3'd7 ;rgb_g_m0 <= rgb888_g * 8'd150;rgb_g_m1 <= rgb888_g * 8'd85 ;rgb_g_m2 <= rgb888_g * 8'd107;rgb_b_m0 <= rgb888_b * 8'd29 ;rgb_b_m1 <= rgb888_b << 3'd7 ;rgb_b_m2 <= rgb888_b * 8'd21 ;end
end//step2 pipeline add
always @(posedge clk or negedge rst_n) beginif(!rst_n) beginimg_y0 <= 16'd0;img_cb0 <= 16'd0;img_cr0 <= 16'd0;endelse beginimg_y0 <= rgb_r_m0 + rgb_g_m0 + rgb_b_m0;img_cb0 <= rgb_b_m1 - rgb_r_m1 - rgb_g_m1 + 16'd32768;img_cr0 <= rgb_r_m2 - rgb_g_m2 - rgb_b_m2 + 16'd32768;endend//step3 pipeline div 右移8位
always @(posedge clk or negedge rst_n) beginif(!rst_n) beginimg_y1 <= 8'd0;img_cb1 <= 8'd0;img_cr1 <= 8'd0;endelse beginimg_y1 <= img_y0 [15:8];img_cb1 <= img_cb0[15:8];img_cr1 <= img_cr0[15:8];end
end
4.延时3拍以同步数据信号,同步信号,输出图像到输入图像有3ns的延迟
assign post_frame_vsync = pre_frame_vsync_d[2] ;
assign post_frame_hsync = pre_frame_hsync_d[2] ;
assign post_frame_de = pre_frame_de_d[2] ;
assign img_y = post_frame_hsync ? img_y1 : 8'd0;
assign img_cb = post_frame_hsync ? img_cb1: 8'd0;
assign img_cr = post_frame_hsync ? img_cr1: 8'd0;always@(posedge clk or negedge rst_n) beginif(!rst_n) beginpre_frame_vsync_d <= 3'd0;pre_frame_hsync_d <= 3'd0;pre_frame_de_d <= 3'd0;endelse beginpre_frame_vsync_d <= {pre_frame_vsync_d[1:0], pre_frame_vsync};pre_frame_hsync_d <= {pre_frame_hsync_d[1:0], pre_frame_hsync};pre_frame_de_d <= {pre_frame_de_d[1:0] , pre_frame_de };end
end