-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_dither.v
More file actions
166 lines (143 loc) · 6.23 KB
/
filter_dither.v
File metadata and controls
166 lines (143 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
`timescale 1ns/1ps
module filter_dither #(
parameter DATA_WIDTH = 24
)(
input wire clk,
input wire reset_n,
// Control Enable
input wire temporal_en,
input wire dither_2bit_en, // 1 for 2-bit dither, 0 for 4-bit dither
// Pixel Input (Latency = 1 relative to start of row)
input wire [DATA_WIDTH-1:0] pixel_in,
input wire [11:0] x_coord,
input wire [11:0] y_coord,
// Output (Delayed by 2 clocks to match pipeline)
output reg [DATA_WIDTH-1:0] pixel_out
);
// ==========================================
// 1. 4x4 Bayer Matrix LUT (0~15)
// ==========================================
// [ 0, 8, 2, 10 ]
// [12, 4, 14, 6 ]
// [ 3, 11, 1, 9 ]
// [15, 7, 13, 5 ]
// Temporal Dithering: Shift starting point per frame
// Temporal Dithering: Shift starting point per frame
reg [3:0] frame_cnt;
reg [11:0] y_coord_prev;
always @(posedge clk or negedge reset_n) begin
if (!reset_n) begin
frame_cnt <= 0;
y_coord_prev <= 0;
end else begin
y_coord_prev <= y_coord;
// Increment exactly once per frame when y_coord resets to 0
if (y_coord == 0 && y_coord_prev != 0) begin
frame_cnt <= frame_cnt + 1;
end
end
end
// Scramble the 4-bit frame counter into 2D (x, y) offsets
// This avoids diagonal 'scrolling' artifacts by jumping pseudorandomly
// across all 16 matrix positions over 16 frames.
wire [1:0] x_offset = {frame_cnt[0], frame_cnt[2]};
wire [1:0] y_offset = {frame_cnt[1], frame_cnt[3]};
wire [1:0] x_idx = temporal_en ? (x_coord[1:0] + x_offset) : x_coord[1:0];
wire [1:0] y_idx = temporal_en ? (y_coord[1:0] + y_offset) : y_coord[1:0];
// Decorrelate RGB channels by adding spatial offsets
// R: no offset, G: (x+1, y+2), B: (x+2, y+1)
wire [1:0] x_idx_r = x_idx;
wire [1:0] y_idx_r = y_idx;
wire [1:0] x_idx_g = x_idx + 2'd1;
wire [1:0] y_idx_g = y_idx + 2'd2;
wire [1:0] x_idx_b = x_idx + 2'd2;
wire [1:0] y_idx_b = y_idx + 2'd1;
// Bayer ROM lookups
function [3:0] bayer_rom;
input [3:0] idx; // {y, x}
begin
case (idx)
4'b00_00: bayer_rom = 4'd0;
4'b00_01: bayer_rom = 4'd8;
4'b00_10: bayer_rom = 4'd2;
4'b00_11: bayer_rom = 4'd10;
4'b01_00: bayer_rom = 4'd12;
4'b01_01: bayer_rom = 4'd4;
4'b01_10: bayer_rom = 4'd14;
4'b01_11: bayer_rom = 4'd6;
4'b10_00: bayer_rom = 4'd3;
4'b10_01: bayer_rom = 4'd11;
4'b10_10: bayer_rom = 4'd1;
4'b10_11: bayer_rom = 4'd9;
4'b11_00: bayer_rom = 4'd15;
4'b11_01: bayer_rom = 4'd7;
4'b11_10: bayer_rom = 4'd13;
4'b11_11: bayer_rom = 4'd5;
endcase
end
endfunction
wire [3:0] bayer_val_r = bayer_rom({y_idx_r, x_idx_r});
wire [3:0] bayer_val_g = bayer_rom({y_idx_g, x_idx_g});
wire [3:0] bayer_val_b = bayer_rom({y_idx_b, x_idx_b});
// ==========================================
// Stage 1: Noise Addition (9-bit to prevent overflow)
// ==========================================
wire [7:0] r_in = pixel_in[23:16];
wire [7:0] g_in = pixel_in[15:8];
wire [7:0] b_in = pixel_in[7:0];
reg [8:0] sum_r, sum_g, sum_b;
reg [11:0] x_coord_st1; // Propagate x_coord for split screen
reg [7:0] r_in_st1, g_in_st1, b_in_st1;
always @(posedge clk or negedge reset_n) begin
if (!reset_n) begin
sum_r <= 0; sum_g <= 0; sum_b <= 0;
x_coord_st1 <= 0;
r_in_st1 <= 0; g_in_st1 <= 0; b_in_st1 <= 0;
end else begin
// Select noise magnitude depending on mode
if (dither_2bit_en) begin
// 2-bit Bayer noise (0-3)
sum_r <= {1'b0, r_in} + {7'b0, bayer_val_r[3:2]};
sum_g <= {1'b0, g_in} + {7'b0, bayer_val_g[3:2]};
sum_b <= {1'b0, b_in} + {7'b0, bayer_val_b[3:2]};
end else begin
// 4-bit Bayer noise (0-15)
sum_r <= {1'b0, r_in} + {5'b0, bayer_val_r};
sum_g <= {1'b0, g_in} + {5'b0, bayer_val_g};
sum_b <= {1'b0, b_in} + {5'b0, bayer_val_b};
end
x_coord_st1 <= x_coord;
// Pass through originals for the left screen
r_in_st1 <= r_in;
g_in_st1 <= g_in;
b_in_st1 <= b_in;
end
end
// ==========================================
// Stage 2: Clamping, Truncation, and Bypass
// ==========================================
// Determine thresholds and masks based on mode
wire [7:0] bypass_thresh = dither_2bit_en ? 8'h04 : 8'h10;
wire [7:0] trunc_mask = dither_2bit_en ? 8'hFC : 8'hF0;
// Truncate lower bits (handle overflow clamping)
wire [7:0] r_clamp = (sum_r > 255) ? 8'd255 : sum_r[7:0];
wire [7:0] g_clamp = (sum_g > 255) ? 8'd255 : sum_g[7:0];
wire [7:0] b_clamp = (sum_b > 255) ? 8'd255 : sum_b[7:0];
always @(posedge clk or negedge reset_n) begin
if (!reset_n) begin
pixel_out <= 24'd0;
end else begin
if (x_coord_st1 < 12'd480) begin
// Left Screen: Original Truncated (No Dither Noise)
pixel_out[23:16] <= (r_in_st1 >= bypass_thresh) ? r_in_st1 : (r_in_st1 & trunc_mask);
pixel_out[15:8] <= (g_in_st1 >= bypass_thresh) ? g_in_st1 : (g_in_st1 & trunc_mask);
pixel_out[7:0] <= (b_in_st1 >= bypass_thresh) ? b_in_st1 : (b_in_st1 & trunc_mask);
end else begin
// Right Screen: Dither applied for values < threshold
pixel_out[23:16] <= (r_in_st1 >= bypass_thresh) ? r_in_st1 : (r_clamp & trunc_mask);
pixel_out[15:8] <= (g_in_st1 >= bypass_thresh) ? g_in_st1 : (g_clamp & trunc_mask);
pixel_out[7:0] <= (b_in_st1 >= bypass_thresh) ? b_in_st1 : (b_clamp & trunc_mask);
end
end
end
endmodule