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

Subversion Repositories mkjpeg

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

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
      divalid    : in  STD_LOGIC;
44
      start_pb   : in  std_logic;
45
      sof        : in  std_logic;
46
      rle_sm_settings : in T_SM_SETTINGS;
47
 
48
      runlength  : out STD_LOGIC_VECTOR(3 downto 0);
49
      size       : out STD_LOGIC_VECTOR(3 downto 0);
50
      amplitude  : out STD_LOGIC_VECTOR(RAMDATA_W-1 downto 0);
51
      dovalid    : out STD_LOGIC
52
    );
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
  signal zero_cnt        : unsigned(3 downto 0);
71
  signal wr_cnt_d1       : unsigned(5 downto 0);
72
  signal wr_cnt          : unsigned(5 downto 0);
73
 
74
begin
75
 
76
  size      <= STD_LOGIC_VECTOR(size_reg);
77
  amplitude <= STD_LOGIC_VECTOR(ampli_vli_reg(11 downto 0));
78
 
79
  -------------------------------------------
80
  -- MAIN PROCESSING
81
  -------------------------------------------
82
  process(clk)
83
  begin
84
    if clk = '1' and clk'event then
85
      if rst = '1' then
86
        wr_cnt_d1       <= (others => '0');
87
        prev_dc_reg_0   <= (others => '0');
88
        prev_dc_reg_1   <= (others => '0');
89
        prev_dc_reg_2   <= (others => '0');
90
        dovalid_reg     <= '0';
91
        acc_reg         <= (others => '0');
92
        runlength_reg   <= (others => '0');
93
        runlength       <= (others => '0');
94
        dovalid         <= '0';
95
        zero_cnt        <= (others => '0');
96
      else
97
        dovalid_reg     <= '0';
98
        runlength_reg   <= (others => '0');
99
 
100
        wr_cnt_d1       <= wr_cnt;
101
        runlength       <= std_logic_vector(runlength_reg);
102
        dovalid         <= dovalid_reg;
103
 
104
        -- input data valid
105
        if divalid = '1' then
106
          wr_cnt <= wr_cnt + 1;
107
 
108
          -- first DCT coefficient received, DC data
109
          if wr_cnt = 0 then
110
            -- differental coding of DC data per component
111
            case rle_sm_settings.cmp_idx is
112
              when "00" =>
113
                acc_reg <= RESIZE(SIGNED(di),RAMDATA_W+1) - RESIZE(prev_dc_reg_0,RAMDATA_W+1);
114
                prev_dc_reg_0 <= SIGNED(di);
115
              when "01" =>
116
                acc_reg <= RESIZE(SIGNED(di),RAMDATA_W+1) - RESIZE(prev_dc_reg_1,RAMDATA_W+1);
117
                prev_dc_reg_1 <= SIGNED(di);
118
              when "10" =>
119
                acc_reg <= RESIZE(SIGNED(di),RAMDATA_W+1) - RESIZE(prev_dc_reg_2,RAMDATA_W+1);
120
                prev_dc_reg_2 <= SIGNED(di);
121
              when others =>
122
                null;
123
            end case;
124
            runlength_reg    <= (others => '0');
125
            dovalid_reg      <= '1';
126
          -- AC coefficient
127
          else
128
            -- zero AC
129
            if signed(di) = 0 then
130
              -- EOB
131
              if wr_cnt = 63 then
132
                acc_reg          <= (others => '0');
133
                runlength_reg    <= (others => '0');
134
                dovalid_reg      <= '1';
135
              -- zero extension symbol
136
              elsif zero_cnt = 15 then
137
                acc_reg          <= (others => '0');
138
                runlength_reg    <= to_unsigned(15, runlength_reg'length);
139
                dovalid_reg      <= '1';
140
                zero_cnt         <= (others => '0');
141
              -- zero_cnt < 15 and no EOB
142
              else
143
                zero_cnt <= zero_cnt + 1;
144
              end if;
145
            -- non-zero AC
146
            else
147
              acc_reg        <= RESIZE(SIGNED(di),RAMDATA_W+1);
148
              runlength_reg  <= zero_cnt;
149
              zero_cnt       <= (others => '0');
150
              dovalid_reg    <= '1';
151
            end if;
152
          end if;
153
        end if;
154
 
155
        -- start of 8x8 block processing
156
        if start_pb = '1' then
157
          zero_cnt <= (others => '0');
158
          wr_cnt   <= (others => '0');
159
        end if;
160
 
161
        if sof = '1' then
162
          prev_dc_reg_0 <= (others => '0');
163
          prev_dc_reg_1 <= (others => '0');
164
          prev_dc_reg_2 <= (others => '0');
165
        end if;
166
 
167
      end if;
168
    end if;
169
  end process;
170
 
171
  -------------------------------------------------------------------
172
  -- Entropy Coder
173
  -------------------------------------------------------------------
174
  p_entropy_coder : process(CLK, RST)
175
  begin
176
    if RST = '1' then
177
      ampli_vli_reg <= (others => '0');
178
      size_reg      <= (others => '0');
179
    elsif CLK'event and CLK = '1' then
180
      -- perform VLI (variable length integer) encoding for Symbol-2 (Amplitude)
181
      -- positive input
182
      if acc_reg >= 0 then
183
        ampli_vli_reg <= acc_reg;
184
      else
185
        ampli_vli_reg <= acc_reg - TO_SIGNED(1,RAMDATA_W+1);
186
      end if;
187
 
188
      -- compute Symbol-1 Size
189
      if acc_reg = TO_SIGNED(-1,RAMDATA_W+1) then
190
        size_reg <= TO_UNSIGNED(1,SIZE_REG_C);
191
      elsif (acc_reg < TO_SIGNED(-1,RAMDATA_W+1) and acc_reg > TO_SIGNED(-4,RAMDATA_W+1)) then
192
        size_reg <= TO_UNSIGNED(2,SIZE_REG_C);
193
      elsif (acc_reg < TO_SIGNED(-3,RAMDATA_W+1) and acc_reg > TO_SIGNED(-8,RAMDATA_W+1)) then
194
        size_reg <= TO_UNSIGNED(3,SIZE_REG_C);
195
      elsif (acc_reg < TO_SIGNED(-7,RAMDATA_W+1) and acc_reg > TO_SIGNED(-16,RAMDATA_W+1)) then
196
        size_reg <= TO_UNSIGNED(4,SIZE_REG_C);
197
      elsif (acc_reg < TO_SIGNED(-15,RAMDATA_W+1) and acc_reg > TO_SIGNED(-32,RAMDATA_W+1)) then
198
        size_reg <= TO_UNSIGNED(5,SIZE_REG_C);
199
      elsif (acc_reg < TO_SIGNED(-31,RAMDATA_W+1) and acc_reg > TO_SIGNED(-64,RAMDATA_W+1)) then
200
        size_reg <= TO_UNSIGNED(6,SIZE_REG_C);
201
      elsif (acc_reg < TO_SIGNED(-63,RAMDATA_W+1) and acc_reg > TO_SIGNED(-128,RAMDATA_W+1)) then
202
        size_reg <= TO_UNSIGNED(7,SIZE_REG_C);
203
      elsif (acc_reg < TO_SIGNED(-127,RAMDATA_W+1) and acc_reg > TO_SIGNED(-256,RAMDATA_W+1)) then
204
        size_reg <= TO_UNSIGNED(8,SIZE_REG_C);
205
      elsif (acc_reg < TO_SIGNED(-255,RAMDATA_W+1) and acc_reg > TO_SIGNED(-512,RAMDATA_W+1)) then
206
        size_reg <= TO_UNSIGNED(9,SIZE_REG_C);
207
      elsif (acc_reg < TO_SIGNED(-511,RAMDATA_W+1) and acc_reg > TO_SIGNED(-1024,RAMDATA_W+1)) then
208
        size_reg <= TO_UNSIGNED(10,SIZE_REG_C);
209
      elsif (acc_reg < TO_SIGNED(-1023,RAMDATA_W+1) and acc_reg > TO_SIGNED(-2048,RAMDATA_W+1)) then
210
        size_reg <= TO_UNSIGNED(11,SIZE_REG_C);
211
      end if;
212
 
213
      -- compute Symbol-1 Size
214
      -- positive input
215
      if acc_reg = TO_SIGNED(1,RAMDATA_W+1) then
216
        size_reg <= TO_UNSIGNED(1,SIZE_REG_C);
217
      elsif (acc_reg > TO_SIGNED(1,RAMDATA_W+1) and acc_reg < TO_SIGNED(4,RAMDATA_W+1)) then
218
        size_reg <= TO_UNSIGNED(2,SIZE_REG_C);
219
      elsif (acc_reg > TO_SIGNED(3,RAMDATA_W+1) and acc_reg < TO_SIGNED(8,RAMDATA_W+1)) then
220
        size_reg <= TO_UNSIGNED(3,SIZE_REG_C);
221
      elsif (acc_reg > TO_SIGNED(7,RAMDATA_W+1) and acc_reg < TO_SIGNED(16,RAMDATA_W+1)) then
222
        size_reg <= TO_UNSIGNED(4,SIZE_REG_C);
223
      elsif (acc_reg > TO_SIGNED(15,RAMDATA_W+1) and acc_reg < TO_SIGNED(32,RAMDATA_W+1)) then
224
        size_reg <= TO_UNSIGNED(5,SIZE_REG_C);
225
      elsif (acc_reg > TO_SIGNED(31,RAMDATA_W+1) and acc_reg < TO_SIGNED(64,RAMDATA_W+1)) then
226
        size_reg <= TO_UNSIGNED(6,SIZE_REG_C);
227
      elsif (acc_reg > TO_SIGNED(63,RAMDATA_W+1) and acc_reg < TO_SIGNED(128,RAMDATA_W+1)) then
228
        size_reg <= TO_UNSIGNED(7,SIZE_REG_C);
229
      elsif (acc_reg > TO_SIGNED(127,RAMDATA_W+1) and acc_reg < TO_SIGNED(256,RAMDATA_W+1)) then
230
        size_reg <= TO_UNSIGNED(8,SIZE_REG_C);
231
      elsif (acc_reg > TO_SIGNED(255,RAMDATA_W+1) and acc_reg < TO_SIGNED(512,RAMDATA_W+1)) then
232
        size_reg <= TO_UNSIGNED(9,SIZE_REG_C);
233
      elsif (acc_reg > TO_SIGNED(511,RAMDATA_W+1) and acc_reg < TO_SIGNED(1024,RAMDATA_W+1)) then
234
        size_reg <= TO_UNSIGNED(10,SIZE_REG_C);
235
      elsif (acc_reg > TO_SIGNED(1023,RAMDATA_W+1) and acc_reg < TO_SIGNED(2048,RAMDATA_W+1)) then
236
        size_reg <= TO_UNSIGNED(11,SIZE_REG_C);
237
      end if;
238
 
239
      -- DC coefficient amplitude=0 case OR EOB
240
      if acc_reg = 0 then
241
         size_reg <= TO_UNSIGNED(0,SIZE_REG_C);
242
      end if;
243
    end if;
244
  end process;
245
 
246
end rtl;
247
--------------------------------------------------------------------------------
248
 
249
 

powered by: WebSVN 2.1.0

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