1 |
25 |
mikel262 |
--------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- V H D L F I L E --
|
4 |
|
|
-- COPYRIGHT (C) 2009 --
|
5 |
|
|
-- --
|
6 |
|
|
--------------------------------------------------------------------------------
|
7 |
|
|
-- --
|
8 |
|
|
-- Title : RLE --
|
9 |
|
|
-- Design : MDCT CORE --
|
10 |
|
|
-- Author : Michal Krepa --
|
11 |
|
|
-- --
|
12 |
|
|
--------------------------------------------------------------------------------
|
13 |
|
|
-- --
|
14 |
|
|
-- File : RLE.VHD --
|
15 |
|
|
-- Created : Wed Mar 04 2009 --
|
16 |
|
|
-- --
|
17 |
|
|
--------------------------------------------------------------------------------
|
18 |
|
|
-- --
|
19 |
|
|
-- Description : Run Length Encoder --
|
20 |
|
|
-- Baseline Entropy Coding --
|
21 |
|
|
--------------------------------------------------------------------------------
|
22 |
|
|
|
23 |
|
|
--------------------------------------------------------------------------------
|
24 |
|
|
|
25 |
|
|
library IEEE;
|
26 |
|
|
use IEEE.STD_LOGIC_1164.All;
|
27 |
|
|
use IEEE.NUMERIC_STD.all;
|
28 |
|
|
|
29 |
|
|
library work;
|
30 |
|
|
use work.JPEG_PKG.all;
|
31 |
|
|
|
32 |
|
|
entity rle is
|
33 |
|
|
generic
|
34 |
|
|
(
|
35 |
|
|
RAMADDR_W : INTEGER := 6;
|
36 |
|
|
RAMDATA_W : INTEGER := 12
|
37 |
|
|
);
|
38 |
|
|
port
|
39 |
|
|
(
|
40 |
|
|
rst : in STD_LOGIC;
|
41 |
|
|
clk : in STD_LOGIC;
|
42 |
|
|
di : in STD_LOGIC_VECTOR(RAMDATA_W-1 downto 0);
|
43 |
|
|
start_pb : in std_logic;
|
44 |
|
|
sof : in std_logic;
|
45 |
|
|
rle_sm_settings : in T_SM_SETTINGS;
|
46 |
|
|
|
47 |
|
|
runlength : out STD_LOGIC_VECTOR(3 downto 0);
|
48 |
|
|
size : out STD_LOGIC_VECTOR(3 downto 0);
|
49 |
|
|
amplitude : out STD_LOGIC_VECTOR(RAMDATA_W-1 downto 0);
|
50 |
36 |
mikel262 |
dovalid : out STD_LOGIC;
|
51 |
|
|
rd_addr : out STD_LOGIC_VECTOR(5 downto 0)
|
52 |
25 |
mikel262 |
);
|
53 |
|
|
end rle;
|
54 |
|
|
|
55 |
|
|
architecture rtl of rle is
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
constant SIZE_REG_C : INTEGER := 4;
|
60 |
|
|
constant ZEROS_32_C : UNSIGNED(31 downto 0) := (others => '0');
|
61 |
|
|
|
62 |
|
|
signal prev_dc_reg_0 : SIGNED(RAMDATA_W-1 downto 0);
|
63 |
|
|
signal prev_dc_reg_1 : SIGNED(RAMDATA_W-1 downto 0);
|
64 |
|
|
signal prev_dc_reg_2 : SIGNED(RAMDATA_W-1 downto 0);
|
65 |
|
|
signal acc_reg : SIGNED(RAMDATA_W downto 0);
|
66 |
|
|
signal size_reg : UNSIGNED(SIZE_REG_C-1 downto 0);
|
67 |
|
|
signal ampli_vli_reg : SIGNED(RAMDATA_W downto 0);
|
68 |
|
|
signal runlength_reg : UNSIGNED(3 downto 0);
|
69 |
|
|
signal dovalid_reg : STD_LOGIC;
|
70 |
36 |
mikel262 |
signal zero_cnt : unsigned(5 downto 0);
|
71 |
25 |
mikel262 |
signal wr_cnt_d1 : unsigned(5 downto 0);
|
72 |
|
|
signal wr_cnt : unsigned(5 downto 0);
|
73 |
|
|
|
74 |
36 |
mikel262 |
signal rd_cnt : unsigned(5 downto 0);
|
75 |
|
|
signal rd_en : std_logic;
|
76 |
|
|
|
77 |
|
|
signal divalid : STD_LOGIC;
|
78 |
|
|
signal zrl_proc : std_logic;
|
79 |
|
|
signal zrl_proc_d1 : std_logic;
|
80 |
|
|
signal zrl_di : STD_LOGIC_VECTOR(RAMDATA_W-1 downto 0);
|
81 |
25 |
mikel262 |
begin
|
82 |
|
|
|
83 |
|
|
size <= STD_LOGIC_VECTOR(size_reg);
|
84 |
|
|
amplitude <= STD_LOGIC_VECTOR(ampli_vli_reg(11 downto 0));
|
85 |
|
|
|
86 |
36 |
mikel262 |
rd_addr <= STD_LOGIC_VECTOR(rd_cnt);
|
87 |
|
|
|
88 |
25 |
mikel262 |
-------------------------------------------
|
89 |
36 |
mikel262 |
-- Counter1
|
90 |
|
|
-------------------------------------------
|
91 |
|
|
process(clk,rst)
|
92 |
|
|
begin
|
93 |
|
|
if rst = '1' then
|
94 |
|
|
rd_en <= '0';
|
95 |
|
|
rd_cnt <= (others => '0');
|
96 |
|
|
elsif clk = '1' and clk'event then
|
97 |
|
|
if start_pb = '1' then
|
98 |
|
|
rd_cnt <= (others => '0');
|
99 |
|
|
rd_en <= '1';
|
100 |
|
|
end if;
|
101 |
|
|
|
102 |
|
|
-- input read enable
|
103 |
|
|
if rd_en = '1' and zrl_proc = '0' then
|
104 |
|
|
if rd_cnt = 64-1 then
|
105 |
|
|
rd_cnt <= (others => '0');
|
106 |
|
|
rd_en <= '0';
|
107 |
|
|
else
|
108 |
|
|
rd_cnt <= rd_cnt + 1;
|
109 |
|
|
end if;
|
110 |
|
|
end if;
|
111 |
|
|
end if;
|
112 |
|
|
end process;
|
113 |
|
|
|
114 |
|
|
-------------------------------------------
|
115 |
25 |
mikel262 |
-- MAIN PROCESSING
|
116 |
|
|
-------------------------------------------
|
117 |
36 |
mikel262 |
process(clk,rst)
|
118 |
25 |
mikel262 |
begin
|
119 |
36 |
mikel262 |
if rst = '1' then
|
120 |
|
|
wr_cnt_d1 <= (others => '0');
|
121 |
|
|
prev_dc_reg_0 <= (others => '0');
|
122 |
|
|
prev_dc_reg_1 <= (others => '0');
|
123 |
|
|
prev_dc_reg_2 <= (others => '0');
|
124 |
|
|
dovalid_reg <= '0';
|
125 |
|
|
acc_reg <= (others => '0');
|
126 |
|
|
runlength_reg <= (others => '0');
|
127 |
|
|
runlength <= (others => '0');
|
128 |
|
|
dovalid <= '0';
|
129 |
|
|
zero_cnt <= (others => '0');
|
130 |
|
|
zrl_proc <= '0';
|
131 |
|
|
zrl_proc_d1 <= '0';
|
132 |
|
|
elsif clk = '1' and clk'event then
|
133 |
|
|
dovalid_reg <= '0';
|
134 |
|
|
runlength_reg <= (others => '0');
|
135 |
|
|
wr_cnt_d1 <= wr_cnt;
|
136 |
|
|
runlength <= std_logic_vector(runlength_reg);
|
137 |
|
|
dovalid <= dovalid_reg;
|
138 |
|
|
divalid <= rd_en;
|
139 |
|
|
zrl_proc_d1 <= zrl_proc;
|
140 |
|
|
|
141 |
|
|
-- input data valid
|
142 |
|
|
if divalid = '1' and zrl_proc_d1 = '0' then
|
143 |
|
|
wr_cnt <= wr_cnt + 1;
|
144 |
|
|
|
145 |
|
|
-- first DCT coefficient received, DC data
|
146 |
|
|
if wr_cnt = 0 then
|
147 |
|
|
-- differental coding of DC data per component
|
148 |
|
|
case rle_sm_settings.cmp_idx is
|
149 |
|
|
when "00" =>
|
150 |
|
|
acc_reg <= RESIZE(SIGNED(di),RAMDATA_W+1) - RESIZE(prev_dc_reg_0,RAMDATA_W+1);
|
151 |
|
|
prev_dc_reg_0 <= SIGNED(di);
|
152 |
|
|
when "01" =>
|
153 |
|
|
acc_reg <= RESIZE(SIGNED(di),RAMDATA_W+1) - RESIZE(prev_dc_reg_1,RAMDATA_W+1);
|
154 |
|
|
prev_dc_reg_1 <= SIGNED(di);
|
155 |
|
|
when "10" =>
|
156 |
|
|
acc_reg <= RESIZE(SIGNED(di),RAMDATA_W+1) - RESIZE(prev_dc_reg_2,RAMDATA_W+1);
|
157 |
|
|
prev_dc_reg_2 <= SIGNED(di);
|
158 |
|
|
when others =>
|
159 |
|
|
null;
|
160 |
|
|
end case;
|
161 |
|
|
runlength_reg <= (others => '0');
|
162 |
|
|
dovalid_reg <= '1';
|
163 |
|
|
-- AC coefficient
|
164 |
|
|
else
|
165 |
|
|
-- zero AC
|
166 |
|
|
if signed(di) = 0 then
|
167 |
|
|
-- EOB
|
168 |
|
|
if wr_cnt = 63 then
|
169 |
|
|
acc_reg <= (others => '0');
|
170 |
|
|
runlength_reg <= (others => '0');
|
171 |
|
|
dovalid_reg <= '1';
|
172 |
|
|
-- no EOB
|
173 |
|
|
else
|
174 |
|
|
zero_cnt <= zero_cnt + 1;
|
175 |
|
|
end if;
|
176 |
|
|
-- non-zero AC
|
177 |
25 |
mikel262 |
else
|
178 |
36 |
mikel262 |
-- normal RLE case
|
179 |
|
|
if zero_cnt <= 15 then
|
180 |
25 |
mikel262 |
acc_reg <= RESIZE(SIGNED(di),RAMDATA_W+1);
|
181 |
36 |
mikel262 |
runlength_reg <= zero_cnt(3 downto 0);
|
182 |
25 |
mikel262 |
zero_cnt <= (others => '0');
|
183 |
|
|
dovalid_reg <= '1';
|
184 |
36 |
mikel262 |
-- zero_cnt > 15
|
185 |
|
|
else
|
186 |
|
|
-- generate ZRL
|
187 |
|
|
acc_reg <= (others => '0');
|
188 |
|
|
runlength_reg <= X"F";
|
189 |
|
|
zero_cnt <= zero_cnt - 16;
|
190 |
|
|
dovalid_reg <= '1';
|
191 |
|
|
-- stall input until ZRL is handled
|
192 |
|
|
zrl_proc <= '1';
|
193 |
|
|
zrl_di <= di;
|
194 |
25 |
mikel262 |
end if;
|
195 |
|
|
end if;
|
196 |
|
|
end if;
|
197 |
36 |
mikel262 |
end if;
|
198 |
|
|
|
199 |
|
|
-- ZRL processing
|
200 |
|
|
if zrl_proc = '1' then
|
201 |
|
|
if zero_cnt <= 15 then
|
202 |
|
|
acc_reg <= RESIZE(SIGNED(zrl_di),RAMDATA_W+1);
|
203 |
|
|
runlength_reg <= zero_cnt(3 downto 0);
|
204 |
39 |
mikel262 |
if signed(di) = 0 then
|
205 |
|
|
zero_cnt <= to_unsigned(1,zero_cnt'length);
|
206 |
|
|
else
|
207 |
|
|
zero_cnt <= (others => '0');
|
208 |
|
|
end if;
|
209 |
36 |
mikel262 |
dovalid_reg <= '1';
|
210 |
|
|
-- continue input handling
|
211 |
|
|
zrl_proc <= '0';
|
212 |
|
|
-- zero_cnt > 15
|
213 |
|
|
else
|
214 |
|
|
-- generate ZRL
|
215 |
|
|
acc_reg <= (others => '0');
|
216 |
|
|
runlength_reg <= X"F";
|
217 |
|
|
zero_cnt <= zero_cnt - 16;
|
218 |
|
|
dovalid_reg <= '1';
|
219 |
|
|
end if;
|
220 |
|
|
end if;
|
221 |
|
|
|
222 |
|
|
-- start of 8x8 block processing
|
223 |
|
|
if start_pb = '1' then
|
224 |
|
|
zero_cnt <= (others => '0');
|
225 |
|
|
wr_cnt <= (others => '0');
|
226 |
|
|
end if;
|
227 |
|
|
|
228 |
|
|
if sof = '1' then
|
229 |
|
|
prev_dc_reg_0 <= (others => '0');
|
230 |
|
|
prev_dc_reg_1 <= (others => '0');
|
231 |
|
|
prev_dc_reg_2 <= (others => '0');
|
232 |
|
|
end if;
|
233 |
25 |
mikel262 |
|
234 |
|
|
end if;
|
235 |
|
|
end process;
|
236 |
|
|
|
237 |
|
|
-------------------------------------------------------------------
|
238 |
|
|
-- Entropy Coder
|
239 |
|
|
-------------------------------------------------------------------
|
240 |
|
|
p_entropy_coder : process(CLK, RST)
|
241 |
|
|
begin
|
242 |
|
|
if RST = '1' then
|
243 |
|
|
ampli_vli_reg <= (others => '0');
|
244 |
|
|
size_reg <= (others => '0');
|
245 |
|
|
elsif CLK'event and CLK = '1' then
|
246 |
|
|
-- perform VLI (variable length integer) encoding for Symbol-2 (Amplitude)
|
247 |
|
|
-- positive input
|
248 |
|
|
if acc_reg >= 0 then
|
249 |
|
|
ampli_vli_reg <= acc_reg;
|
250 |
|
|
else
|
251 |
|
|
ampli_vli_reg <= acc_reg - TO_SIGNED(1,RAMDATA_W+1);
|
252 |
|
|
end if;
|
253 |
|
|
|
254 |
|
|
-- compute Symbol-1 Size
|
255 |
|
|
if acc_reg = TO_SIGNED(-1,RAMDATA_W+1) then
|
256 |
|
|
size_reg <= TO_UNSIGNED(1,SIZE_REG_C);
|
257 |
|
|
elsif (acc_reg < TO_SIGNED(-1,RAMDATA_W+1) and acc_reg > TO_SIGNED(-4,RAMDATA_W+1)) then
|
258 |
|
|
size_reg <= TO_UNSIGNED(2,SIZE_REG_C);
|
259 |
|
|
elsif (acc_reg < TO_SIGNED(-3,RAMDATA_W+1) and acc_reg > TO_SIGNED(-8,RAMDATA_W+1)) then
|
260 |
|
|
size_reg <= TO_UNSIGNED(3,SIZE_REG_C);
|
261 |
|
|
elsif (acc_reg < TO_SIGNED(-7,RAMDATA_W+1) and acc_reg > TO_SIGNED(-16,RAMDATA_W+1)) then
|
262 |
|
|
size_reg <= TO_UNSIGNED(4,SIZE_REG_C);
|
263 |
|
|
elsif (acc_reg < TO_SIGNED(-15,RAMDATA_W+1) and acc_reg > TO_SIGNED(-32,RAMDATA_W+1)) then
|
264 |
|
|
size_reg <= TO_UNSIGNED(5,SIZE_REG_C);
|
265 |
|
|
elsif (acc_reg < TO_SIGNED(-31,RAMDATA_W+1) and acc_reg > TO_SIGNED(-64,RAMDATA_W+1)) then
|
266 |
|
|
size_reg <= TO_UNSIGNED(6,SIZE_REG_C);
|
267 |
|
|
elsif (acc_reg < TO_SIGNED(-63,RAMDATA_W+1) and acc_reg > TO_SIGNED(-128,RAMDATA_W+1)) then
|
268 |
|
|
size_reg <= TO_UNSIGNED(7,SIZE_REG_C);
|
269 |
|
|
elsif (acc_reg < TO_SIGNED(-127,RAMDATA_W+1) and acc_reg > TO_SIGNED(-256,RAMDATA_W+1)) then
|
270 |
|
|
size_reg <= TO_UNSIGNED(8,SIZE_REG_C);
|
271 |
|
|
elsif (acc_reg < TO_SIGNED(-255,RAMDATA_W+1) and acc_reg > TO_SIGNED(-512,RAMDATA_W+1)) then
|
272 |
|
|
size_reg <= TO_UNSIGNED(9,SIZE_REG_C);
|
273 |
|
|
elsif (acc_reg < TO_SIGNED(-511,RAMDATA_W+1) and acc_reg > TO_SIGNED(-1024,RAMDATA_W+1)) then
|
274 |
|
|
size_reg <= TO_UNSIGNED(10,SIZE_REG_C);
|
275 |
|
|
elsif (acc_reg < TO_SIGNED(-1023,RAMDATA_W+1) and acc_reg > TO_SIGNED(-2048,RAMDATA_W+1)) then
|
276 |
|
|
size_reg <= TO_UNSIGNED(11,SIZE_REG_C);
|
277 |
|
|
end if;
|
278 |
|
|
|
279 |
|
|
-- compute Symbol-1 Size
|
280 |
|
|
-- positive input
|
281 |
|
|
if acc_reg = TO_SIGNED(1,RAMDATA_W+1) then
|
282 |
|
|
size_reg <= TO_UNSIGNED(1,SIZE_REG_C);
|
283 |
|
|
elsif (acc_reg > TO_SIGNED(1,RAMDATA_W+1) and acc_reg < TO_SIGNED(4,RAMDATA_W+1)) then
|
284 |
|
|
size_reg <= TO_UNSIGNED(2,SIZE_REG_C);
|
285 |
|
|
elsif (acc_reg > TO_SIGNED(3,RAMDATA_W+1) and acc_reg < TO_SIGNED(8,RAMDATA_W+1)) then
|
286 |
|
|
size_reg <= TO_UNSIGNED(3,SIZE_REG_C);
|
287 |
|
|
elsif (acc_reg > TO_SIGNED(7,RAMDATA_W+1) and acc_reg < TO_SIGNED(16,RAMDATA_W+1)) then
|
288 |
|
|
size_reg <= TO_UNSIGNED(4,SIZE_REG_C);
|
289 |
|
|
elsif (acc_reg > TO_SIGNED(15,RAMDATA_W+1) and acc_reg < TO_SIGNED(32,RAMDATA_W+1)) then
|
290 |
|
|
size_reg <= TO_UNSIGNED(5,SIZE_REG_C);
|
291 |
|
|
elsif (acc_reg > TO_SIGNED(31,RAMDATA_W+1) and acc_reg < TO_SIGNED(64,RAMDATA_W+1)) then
|
292 |
|
|
size_reg <= TO_UNSIGNED(6,SIZE_REG_C);
|
293 |
|
|
elsif (acc_reg > TO_SIGNED(63,RAMDATA_W+1) and acc_reg < TO_SIGNED(128,RAMDATA_W+1)) then
|
294 |
|
|
size_reg <= TO_UNSIGNED(7,SIZE_REG_C);
|
295 |
|
|
elsif (acc_reg > TO_SIGNED(127,RAMDATA_W+1) and acc_reg < TO_SIGNED(256,RAMDATA_W+1)) then
|
296 |
|
|
size_reg <= TO_UNSIGNED(8,SIZE_REG_C);
|
297 |
|
|
elsif (acc_reg > TO_SIGNED(255,RAMDATA_W+1) and acc_reg < TO_SIGNED(512,RAMDATA_W+1)) then
|
298 |
|
|
size_reg <= TO_UNSIGNED(9,SIZE_REG_C);
|
299 |
|
|
elsif (acc_reg > TO_SIGNED(511,RAMDATA_W+1) and acc_reg < TO_SIGNED(1024,RAMDATA_W+1)) then
|
300 |
|
|
size_reg <= TO_UNSIGNED(10,SIZE_REG_C);
|
301 |
|
|
elsif (acc_reg > TO_SIGNED(1023,RAMDATA_W+1) and acc_reg < TO_SIGNED(2048,RAMDATA_W+1)) then
|
302 |
|
|
size_reg <= TO_UNSIGNED(11,SIZE_REG_C);
|
303 |
|
|
end if;
|
304 |
|
|
|
305 |
|
|
-- DC coefficient amplitude=0 case OR EOB
|
306 |
|
|
if acc_reg = 0 then
|
307 |
|
|
size_reg <= TO_UNSIGNED(0,SIZE_REG_C);
|
308 |
|
|
end if;
|
309 |
|
|
end if;
|
310 |
|
|
end process;
|
311 |
|
|
|
312 |
|
|
end rtl;
|
313 |
|
|
--------------------------------------------------------------------------------
|
314 |
|
|
|
315 |
|
|
|