OpenCores
URL https://opencores.org/ocsvn/mkjpeg/mkjpeg/trunk

Subversion Repositories mkjpeg

[/] [mkjpeg/] [trunk/] [design/] [rle/] [RLE.VHD] - Blame information for rev 63

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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 61 mikel262
  signal prev_dc_reg_3   : SIGNED(RAMDATA_W-1 downto 0);
66 25 mikel262
  signal acc_reg         : SIGNED(RAMDATA_W downto 0);
67
  signal size_reg        : UNSIGNED(SIZE_REG_C-1 downto 0);
68
  signal ampli_vli_reg   : SIGNED(RAMDATA_W downto 0);
69
  signal runlength_reg   : UNSIGNED(3 downto 0);
70
  signal dovalid_reg     : STD_LOGIC;
71 36 mikel262
  signal zero_cnt        : unsigned(5 downto 0);
72 25 mikel262
  signal wr_cnt_d1       : unsigned(5 downto 0);
73
  signal wr_cnt          : unsigned(5 downto 0);
74
 
75 36 mikel262
  signal rd_cnt         : unsigned(5 downto 0);
76
  signal rd_en          : std_logic;
77
 
78
  signal divalid        : STD_LOGIC;
79 41 mikel262
  signal divalid_en     : std_logic;
80 36 mikel262
  signal zrl_proc       : std_logic;
81
  signal zrl_di         : STD_LOGIC_VECTOR(RAMDATA_W-1 downto 0);
82 25 mikel262
begin
83
 
84
  size      <= STD_LOGIC_VECTOR(size_reg);
85
  amplitude <= STD_LOGIC_VECTOR(ampli_vli_reg(11 downto 0));
86
 
87 36 mikel262
  rd_addr <= STD_LOGIC_VECTOR(rd_cnt);
88
 
89
  -------------------------------------------
90 25 mikel262
  -- MAIN PROCESSING
91
  -------------------------------------------
92 36 mikel262
  process(clk,rst)
93 25 mikel262
  begin
94 36 mikel262
    if rst = '1' then
95
      wr_cnt_d1       <= (others => '0');
96
      prev_dc_reg_0   <= (others => '0');
97
      prev_dc_reg_1   <= (others => '0');
98
      prev_dc_reg_2   <= (others => '0');
99 61 mikel262
      prev_dc_reg_3   <= (others => '0');
100 36 mikel262
      dovalid_reg     <= '0';
101
      acc_reg         <= (others => '0');
102
      runlength_reg   <= (others => '0');
103
      runlength       <= (others => '0');
104
      dovalid         <= '0';
105
      zero_cnt        <= (others => '0');
106
      zrl_proc        <= '0';
107 40 mikel262
      rd_en           <= '0';
108
      rd_cnt          <= (others => '0');
109 41 mikel262
      divalid_en      <= '0';
110 36 mikel262
    elsif clk = '1' and clk'event then
111
      dovalid_reg     <= '0';
112
      runlength_reg   <= (others => '0');
113
      wr_cnt_d1       <= wr_cnt;
114
      runlength       <= std_logic_vector(runlength_reg);
115
      dovalid         <= dovalid_reg;
116
      divalid         <= rd_en;
117 40 mikel262
 
118
      if start_pb = '1' then
119 41 mikel262
        rd_cnt     <= (others => '0');
120
        rd_en      <= '1';
121
        divalid_en <= '1';
122 40 mikel262
      end if;
123
 
124 41 mikel262
      if divalid = '1' and wr_cnt = 63 then
125
        divalid_en <= '0';
126
      end if;
127
 
128 40 mikel262
      -- input read enable
129
      if rd_en = '1' then
130
        if rd_cnt = 64-1 then
131
          rd_cnt <= (others => '0');
132
          rd_en  <= '0';
133
        else
134
          rd_cnt <= rd_cnt + 1;
135
        end if;
136
      end if;
137 36 mikel262
 
138
      -- input data valid
139 40 mikel262
      if divalid = '1' then
140 36 mikel262
        wr_cnt <= wr_cnt + 1;
141
 
142
        -- first DCT coefficient received, DC data
143
        if wr_cnt = 0 then
144
          -- differental coding of DC data per component
145
          case rle_sm_settings.cmp_idx is
146 61 mikel262
            when "000" | "001" =>
147 36 mikel262
              acc_reg <= RESIZE(SIGNED(di),RAMDATA_W+1) - RESIZE(prev_dc_reg_0,RAMDATA_W+1);
148
              prev_dc_reg_0 <= SIGNED(di);
149 61 mikel262
            when "010" =>
150 36 mikel262
              acc_reg <= RESIZE(SIGNED(di),RAMDATA_W+1) - RESIZE(prev_dc_reg_1,RAMDATA_W+1);
151
              prev_dc_reg_1 <= SIGNED(di);
152 61 mikel262
            when "011" =>
153 36 mikel262
              acc_reg <= RESIZE(SIGNED(di),RAMDATA_W+1) - RESIZE(prev_dc_reg_2,RAMDATA_W+1);
154
              prev_dc_reg_2 <= SIGNED(di);
155
            when others =>
156
              null;
157
          end case;
158
          runlength_reg    <= (others => '0');
159
          dovalid_reg      <= '1';
160
        -- AC coefficient
161
        else
162
          -- zero AC
163
          if signed(di) = 0 then
164
            -- EOB
165
            if wr_cnt = 63 then
166
              acc_reg          <= (others => '0');
167
              runlength_reg    <= (others => '0');
168
              dovalid_reg      <= '1';
169
            -- no EOB
170
            else
171
              zero_cnt <= zero_cnt + 1;
172
            end if;
173
          -- non-zero AC
174 25 mikel262
          else
175 36 mikel262
            -- normal RLE case
176
            if zero_cnt <= 15 then
177 25 mikel262
              acc_reg        <= RESIZE(SIGNED(di),RAMDATA_W+1);
178 36 mikel262
              runlength_reg  <= zero_cnt(3 downto 0);
179 25 mikel262
              zero_cnt       <= (others => '0');
180
              dovalid_reg    <= '1';
181 36 mikel262
            -- zero_cnt > 15
182
            else
183
              -- generate ZRL
184
              acc_reg        <= (others => '0');
185
              runlength_reg  <= X"F";
186
              zero_cnt       <= zero_cnt - 16;
187
              dovalid_reg    <= '1';
188
              -- stall input until ZRL is handled
189
              zrl_proc       <= '1';
190
              zrl_di         <= di;
191 40 mikel262
              divalid <= '0';
192
              rd_cnt  <= rd_cnt;
193 25 mikel262
            end if;
194
          end if;
195
        end if;
196 36 mikel262
      end if;
197
 
198
      -- ZRL processing
199
      if zrl_proc = '1' then
200
        if zero_cnt <= 15 then
201
          acc_reg        <= RESIZE(SIGNED(zrl_di),RAMDATA_W+1);
202
          runlength_reg  <= zero_cnt(3 downto 0);
203 40 mikel262
          if signed(zrl_di) = 0 then
204 39 mikel262
            zero_cnt     <= to_unsigned(1,zero_cnt'length);
205
          else
206
            zero_cnt     <= (others => '0');
207
          end if;
208 36 mikel262
          dovalid_reg    <= '1';
209 41 mikel262
          divalid <= divalid_en;
210 36 mikel262
          -- 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 40 mikel262
          divalid <= '0';
220
          rd_cnt <= rd_cnt;
221 36 mikel262
        end if;
222
      end if;
223
 
224
      -- start of 8x8 block processing
225
      if start_pb = '1' then
226
        zero_cnt <= (others => '0');
227
        wr_cnt   <= (others => '0');
228
      end if;
229
 
230
      if sof = '1' then
231
        prev_dc_reg_0 <= (others => '0');
232
        prev_dc_reg_1 <= (others => '0');
233
        prev_dc_reg_2 <= (others => '0');
234 61 mikel262
        prev_dc_reg_3 <= (others => '0');
235 36 mikel262
      end if;
236 25 mikel262
 
237
    end if;
238
  end process;
239
 
240
  -------------------------------------------------------------------
241
  -- Entropy Coder
242
  -------------------------------------------------------------------
243
  p_entropy_coder : process(CLK, RST)
244
  begin
245
    if RST = '1' then
246
      ampli_vli_reg <= (others => '0');
247
      size_reg      <= (others => '0');
248
    elsif CLK'event and CLK = '1' then
249
      -- perform VLI (variable length integer) encoding for Symbol-2 (Amplitude)
250
      -- positive input
251
      if acc_reg >= 0 then
252
        ampli_vli_reg <= acc_reg;
253
      else
254
        ampli_vli_reg <= acc_reg - TO_SIGNED(1,RAMDATA_W+1);
255
      end if;
256
 
257
      -- compute Symbol-1 Size
258
      if acc_reg = TO_SIGNED(-1,RAMDATA_W+1) then
259
        size_reg <= TO_UNSIGNED(1,SIZE_REG_C);
260
      elsif (acc_reg < TO_SIGNED(-1,RAMDATA_W+1) and acc_reg > TO_SIGNED(-4,RAMDATA_W+1)) then
261
        size_reg <= TO_UNSIGNED(2,SIZE_REG_C);
262
      elsif (acc_reg < TO_SIGNED(-3,RAMDATA_W+1) and acc_reg > TO_SIGNED(-8,RAMDATA_W+1)) then
263
        size_reg <= TO_UNSIGNED(3,SIZE_REG_C);
264
      elsif (acc_reg < TO_SIGNED(-7,RAMDATA_W+1) and acc_reg > TO_SIGNED(-16,RAMDATA_W+1)) then
265
        size_reg <= TO_UNSIGNED(4,SIZE_REG_C);
266
      elsif (acc_reg < TO_SIGNED(-15,RAMDATA_W+1) and acc_reg > TO_SIGNED(-32,RAMDATA_W+1)) then
267
        size_reg <= TO_UNSIGNED(5,SIZE_REG_C);
268
      elsif (acc_reg < TO_SIGNED(-31,RAMDATA_W+1) and acc_reg > TO_SIGNED(-64,RAMDATA_W+1)) then
269
        size_reg <= TO_UNSIGNED(6,SIZE_REG_C);
270
      elsif (acc_reg < TO_SIGNED(-63,RAMDATA_W+1) and acc_reg > TO_SIGNED(-128,RAMDATA_W+1)) then
271
        size_reg <= TO_UNSIGNED(7,SIZE_REG_C);
272
      elsif (acc_reg < TO_SIGNED(-127,RAMDATA_W+1) and acc_reg > TO_SIGNED(-256,RAMDATA_W+1)) then
273
        size_reg <= TO_UNSIGNED(8,SIZE_REG_C);
274
      elsif (acc_reg < TO_SIGNED(-255,RAMDATA_W+1) and acc_reg > TO_SIGNED(-512,RAMDATA_W+1)) then
275
        size_reg <= TO_UNSIGNED(9,SIZE_REG_C);
276
      elsif (acc_reg < TO_SIGNED(-511,RAMDATA_W+1) and acc_reg > TO_SIGNED(-1024,RAMDATA_W+1)) then
277
        size_reg <= TO_UNSIGNED(10,SIZE_REG_C);
278
      elsif (acc_reg < TO_SIGNED(-1023,RAMDATA_W+1) and acc_reg > TO_SIGNED(-2048,RAMDATA_W+1)) then
279
        size_reg <= TO_UNSIGNED(11,SIZE_REG_C);
280
      end if;
281
 
282
      -- compute Symbol-1 Size
283
      -- positive input
284
      if acc_reg = TO_SIGNED(1,RAMDATA_W+1) then
285
        size_reg <= TO_UNSIGNED(1,SIZE_REG_C);
286
      elsif (acc_reg > TO_SIGNED(1,RAMDATA_W+1) and acc_reg < TO_SIGNED(4,RAMDATA_W+1)) then
287
        size_reg <= TO_UNSIGNED(2,SIZE_REG_C);
288
      elsif (acc_reg > TO_SIGNED(3,RAMDATA_W+1) and acc_reg < TO_SIGNED(8,RAMDATA_W+1)) then
289
        size_reg <= TO_UNSIGNED(3,SIZE_REG_C);
290
      elsif (acc_reg > TO_SIGNED(7,RAMDATA_W+1) and acc_reg < TO_SIGNED(16,RAMDATA_W+1)) then
291
        size_reg <= TO_UNSIGNED(4,SIZE_REG_C);
292
      elsif (acc_reg > TO_SIGNED(15,RAMDATA_W+1) and acc_reg < TO_SIGNED(32,RAMDATA_W+1)) then
293
        size_reg <= TO_UNSIGNED(5,SIZE_REG_C);
294
      elsif (acc_reg > TO_SIGNED(31,RAMDATA_W+1) and acc_reg < TO_SIGNED(64,RAMDATA_W+1)) then
295
        size_reg <= TO_UNSIGNED(6,SIZE_REG_C);
296
      elsif (acc_reg > TO_SIGNED(63,RAMDATA_W+1) and acc_reg < TO_SIGNED(128,RAMDATA_W+1)) then
297
        size_reg <= TO_UNSIGNED(7,SIZE_REG_C);
298
      elsif (acc_reg > TO_SIGNED(127,RAMDATA_W+1) and acc_reg < TO_SIGNED(256,RAMDATA_W+1)) then
299
        size_reg <= TO_UNSIGNED(8,SIZE_REG_C);
300
      elsif (acc_reg > TO_SIGNED(255,RAMDATA_W+1) and acc_reg < TO_SIGNED(512,RAMDATA_W+1)) then
301
        size_reg <= TO_UNSIGNED(9,SIZE_REG_C);
302
      elsif (acc_reg > TO_SIGNED(511,RAMDATA_W+1) and acc_reg < TO_SIGNED(1024,RAMDATA_W+1)) then
303
        size_reg <= TO_UNSIGNED(10,SIZE_REG_C);
304
      elsif (acc_reg > TO_SIGNED(1023,RAMDATA_W+1) and acc_reg < TO_SIGNED(2048,RAMDATA_W+1)) then
305
        size_reg <= TO_UNSIGNED(11,SIZE_REG_C);
306
      end if;
307
 
308
      -- DC coefficient amplitude=0 case OR EOB
309
      if acc_reg = 0 then
310
         size_reg <= TO_UNSIGNED(0,SIZE_REG_C);
311
      end if;
312
    end if;
313
  end process;
314
 
315
end rtl;
316
--------------------------------------------------------------------------------
317
 
318
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.