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

Subversion Repositories huffmandecoder

[/] [huffmandecoder/] [trunk/] [huffman_decoder.vhd] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 dose
----------------------------------------------------------------------------------
2
-- Company:   Dossmatik GmbH /Germany
3
-- Engineer:  Rene Doss
4
-- Create Date:     10/12/2009 
5
-- Design Name: 
6
-- Module Name:    huffman decoder for jpeg application
7
----------------------------------------------------------------------------------
8
library IEEE;
9
use IEEE.STD_LOGIC_1164.ALL;
10
use IEEE.numeric_std.all;
11
--  use std.textio.all; -- only for testing
12
--use work.txt_util.all;
13
 
14
--use IEEE.STD_LOGIC_ARITH.ALL;
15
--use IEEE.STD_LOGIC_UNSIGNED.ALL;
16
 
17
 
18
 
19
entity huffman_decoder is
20
port(
21
clk             :in std_logic;
22
 
23
--interface data input
24
wr              :in std_logic;                  --write
25
data_in         : in unsigned (7 downto 0);      --data jpeg stream
26
wr_en           : out std_logic:='1';           --write enable  
27
 
28
--interface  data out
29
output_valid    : buffer std_logic;             --use it as write signal in the follow IDCT
30
data_out        : out signed (15 downto 0);      --decoded and dequantized coefficient
31
next_eob        : buffer std_logic:='0';         --the next data is the last coefficient of block
32
                                                --all higher zigzag coeficients are zero
33
sop             : out std_logic:='0';            --start of picture, can be used as reset in the following IDCT
34
eop             : out std_logic:='0';            --end of picture
35
zrl             : out unsigned (3 downto 0);    -- number of consecutive zeros before the next coefficient
36
decoder_enable  : in std_logic);
37
end huffman_decoder;
38
 
39
 
40
 
41
 
42
 
43
architecture Behavioral of huffman_decoder is
44
-------------------------------------------------
45
subtype by_te is character;
46
type f_byte is file of by_te;
47
 
48
type ubyte_vector16 is array (0 to 15) of unsigned (7 downto 0);
49
type uword_vector16 is array (0 to 15) of unsigned (15 downto 0);
50
 
51
--prepaired type for post IDCT not implemented yet
52
-- type word_vector64 is array (0 to 63) of signed (15 downto 0);
53
 
54
type dual_table is array (0 to 1,0 to 63) of unsigned (7 downto 0); --quant Tables
55
 
56
 
57
type int_vector64 is array (0 to 63) of integer;
58
constant  zigzag :int_vector64:=(        0, 8, 1, 2, 9,16,24,17,
59
                                        10, 3, 4,11,18,25,32,40,
60
                                        33,26,19,12, 5, 6,13,20,
61
                                        27,34,41,48,56,49,42,35,
62
                                        28,21,14, 7,15,22,29,36,
63
                                        43,50,57,58,51,44,37,30,
64
                                        23,31,38,45,52,59,60,53,
65
                                        46,39,47,54,61,62,55,63);
66
 
67
 
68
--hufftable storage place definition
69
constant RAM_addrwidth: integer:=9;
70
-- Entspricht 512 Speicherzellen reicht für 2 DC und 2AC Tabellen
71
--sollte gegebenfalls erhöht werden!!!!!!!!!
72
 
73
type RAM is array (0 to (2**RAM_addrwidth-1)) of unsigned (7 downto 0);
74
subtype RAM_int is integer range 0 to (2**RAM_addrwidth-1);
75
 
76
 
77
--2*4*16  Für 4 DC-Tabellen und 4AC-Tabelle 
78
type pointer_array is array (0 to 127) of RAM_int;
79
type uword_array is array ( 0 to 127) of unsigned (15 downto 0);
80
type int_vectorRAM is array(0 to 15) of integer range 0 to (2**RAM_addrwidth-1);
81
-------------------------------------------------------
82
--the state machine is divide 
83
--some states for sub state machine
84
type sos_type is (decode,decode_post,catch,catch_post,change_comp0,
85
                change_comp,change_DC_AC0,change_DC_AC);
86
type sos_header_type is (selector,table);
87
type SOF0_Header_type is (selector,sampling,table);
88
 
89
 
90
--this is for the main state machine
91
type state_type is (IDLE,
92
                                SOI,  --0xFFD8 start of image
93
                                APP0,   --0xFFE0 application segment
94
 
95
                                DQT,  --0xFFDB define quantisation table
96
                                DQT_length,DQT_length0,
97
                                DQT_active,
98
                                SOF0_length,SOF0_length0, --0xFFC0 baseline DCT
99
                                SOF0_precision,
100
                                SOF0_y_high,SOF0_y_low,
101
                                SOF0_x_high,SOF0_x_low,
102
                                SOF0_nr_comp,SOF0_active,
103
 
104
                                DHT,  --0xFFC4 define Huffman Table
105
                                DHT_length,DHT_length0,
106
                                DHT_destination,
107
                                DHT_Number,
108
 
109
                                DHT_active,
110
                                --0xFFDA start of scan
111
                                SOS_length,SOS_length0,SOS_header,
112
                                SOS_init0,SOS_init1,SOS_init2,SOS_init3,
113
                                SOS_scan,
114
                                EOI); --0xFFD9 end of Image
115
-----------------------------------------------------------------------------
116
 
117
signal value : signed (15 downto 0);
118
 
119
 
120
signal qtable : dual_table;
121
signal write_active: std_logic;
122
signal input_reg:unsigned (23 downto 0);
123
signal Markerlength: unsigned (15 downto 0);
124
signal next_state,state: state_type:=idle;
125
signal sos_state,sos_state_old:sos_type:=decode;
126
signal DHT_counter: integer;
127
 
128
 
129
signal SOF0_header_state:SOF0_Header_type;
130
signal sof0_header_index:integer range 0 to 15 := 15;
131
signal x_size,y_size:unsigned (15 downto 0);  --picture size
132
signal sof0_number_comp: unsigned(7 downto 0);
133
signal sof0_comp_table: uword_vector16; -- hier ist die Info Qtable genommen werden
134
 
135
 
136
signal output_active: std_logic:='0';
137
 
138
signal huff_wr_en: std_logic:='1';
139
 
140
 
141
 
142
 
143
--Hufftable
144
signal ram_pointer:   pointer_array;
145
signal huff_ram:        RAM;
146
signal huff_code_offset: uword_array;
147
signal ram_offset:RAM_int;
148
 
149
signal dest: integer:=0;
150
signal DC_AC_decode: unsigned (2 downto 0):="000";
151
signal DC_AC_old: unsigned (0 downto 0);
152
signal index: integer range 0 to 15;
153
signal huff_table_end :std_logic:='1';
154
signal addr: integer  range 0 to (2**RAM_addrwidth-1);
155
signal huff_a: unsigned (7 downto 0):="00000011";
156
signal huff_code_number: ubyte_vector16;
157
signal h_code: unsigned (15 downto 0);
158
signal h_delta: uword_vector16;
159
signal code_word: unsigned (7 downto 0);
160
 
161
 
162
 
163
signal comp_table:ubyte_vector16;
164
--data
165
signal read_offset,read_offset_a: integer range 0 to 15;
166
signal shift : integer range 1 to 16;
167
--signal ablock :word_vector64;
168
signal RLD_wr:std_logic;
169
signal stuffing :std_logic;
170
signal scan_data: unsigned (7 downto 0);
171
signal Barrel,ba: unsigned (15 downto 0);
172
signal ba_1: unsigned (0 downto 0);
173
signal rot_buffer: unsigned(31 downto 0):=X"00000000"; --Das ist der Ringspeicher vom Decoder
174
signal barrel_pointer: unsigned (4 downto 0):="00000";
175
signal write_rot_pointer: unsigned (1 downto 0):="11";
176
signal sos_number_comp: unsigned(7 downto 0);
177
signal sos_comp_table: ubyte_vector16; -- hier ist die Info welche Huff-table und Qtable genommen werden
178
signal sos_wr_en:std_logic:='1';
179
signal sos_teiler2: std_logic:='0';
180
 
181
signal sos_header_state:sos_header_type;
182
signal sos_header_index:integer range 0 to 15 := 15;
183
signal sos_matrix_counter:unsigned (5 downto 0):=(others=>'0');
184
Signal sos_scan_index: integer;
185
signal sos_hi :unsigned (3 downto 0);
186
signal sos_vi :unsigned (3 downto 0);
187
signal sos_component: integer range 0 to 15;
188
signal eoi_detect:std_logic_vector (1 downto 0):="00";   --bit 0 eoi  detect, bit 1 last eob detect
189
--------------------------------------------
190
signal addr_table: integer range 0 to 63;
191
 
192
 
193
-- 
194
 begin
195
 
196
data_out<=value;
197
 
198
process(clk)
199
begin
200
if  clk'event and clk='1' then
201
  if sos_state=catch_post then
202
      output_active<='1';
203
  end if;
204
  if state=eoi then
205
      output_active<='0';
206
  end if;
207
end if;
208
end process;
209
 
210
process(clk)
211
begin
212
if  clk'event and clk='1' then
213
  if output_active='1' and sos_state=catch then
214
        output_valid<='1';
215
        zrl<=code_word(7 downto 4);
216
      else
217
        output_valid<='0';
218
  end if;
219
end if;
220
end process;
221
 
222
--    --this is my testing process at simulation 
223
--process(clk)
224
--constant file_name: string:="output.txt";
225
--file log: text open write_mode is file_name;
226
--variable myline:line;
227
--begin
228
--if  clk'event and clk='1' then
229
--      if sos_state=catch then
230
--      write(myline,now);
231
--      write(myline,string'("   "));
232
--      write(myline,str(to_integer(DC_AC_decode)));
233
--      write(myline,string'("   "));
234
--      write(myline,str(to_integer(value)));
235
--      writeline(log,myline);
236
--      end if;
237
--end if;
238
--end process;
239
 
240
 
241
--sm Huffdecoder
242
 
243
 
244
--state decode  huffman decode
245
--state catch  mantissa bits 
246
--state catch post an additional delay
247
 
248
-- decode -> catch -> catch_post ->decode...........this is the cycle in work
249
--some waits needed when the table is changed that all values are valid
250
 
251
 
252
process (clk,state,decoder_enable)
253
begin
254
if state=sos_init1 then
255
                barrel_pointer<="11111";
256
 
257
elsif clk'event and clk='1' and decoder_enable='1' then
258
        if state=sos_init0 then
259
              sos_state<=decode;
260
        end if;
261
 
262
        if state=sos_scan then
263
                if sos_state=catch then
264
                        sos_state<=catch_post;
265
 
266
                end if;
267
                --normaler Ablauf
268
                if (sos_state=catch_post and  (DC_AC_decode(0 downto 0)/="0" and code_word/=to_unsigned(0,8)))then
269
                                barrel_pointer<=barrel_pointer - ('0'& code_word(3 downto 0));
270
                                sos_state<=decode;
271
                end if;
272
                --letzte Zeichen
273
                if(sos_state=catch_post and  (DC_AC_decode(0 downto 0)/="0" and code_word=to_unsigned(0,8))) then
274
                                sos_state<=change_comp0;
275
                end if;
276
                if sos_state=change_comp0 then
277
                                sos_state<=change_comp;
278
                end if;
279
 
280
                --DC zu AC Uebergang
281
                if (sos_state=catch_post and  (DC_AC_decode(0 downto 0)="0")) then
282
                                sos_state<=change_DC_AC0;
283
                end if;
284
                if sos_state=change_DC_AC0 then
285
                                sos_state<=change_DC_AC;
286
                end if;
287
                --
288
                if  sos_state=change_DC_AC or sos_state=change_comp then
289
                                barrel_pointer<=barrel_pointer - ('0'& code_word(3 downto 0));
290
                                sos_state<=decode;
291
                end if;
292
 
293
 
294
                if ((write_rot_pointer=barrel_pointer(4 downto 3)) or
295
                   (write_rot_pointer="00" and barrel_pointer(4 downto 3)="11") or
296
                   (write_rot_pointer="01" and barrel_pointer(4 downto 3)="00") or
297
                   (write_rot_pointer="10" and barrel_pointer(4 downto 3)="01") or
298
                   (write_rot_pointer="11" and barrel_pointer(4 downto 3)="10")) then
299
 
300
                        if sos_state= decode then
301
                                barrel_pointer<=barrel_pointer-to_unsigned(shift,5);
302
                                sos_state<=catch;
303
 
304
                        end if;
305
 
306
 
307
                end if;
308
        end if;
309
 
310
 
311
end if;
312
end process;
313
 
314
 
315
 
316
 
317
--chose the correct table
318
--xx0 DC Tables
319
--xx1 AC Table
320
process (clk)
321
begin
322
if clk'event and clk='1' then
323
sos_state_old<=sos_state;
324
 
325
        if state=sos_init0 then
326
                DC_AC_decode<=SOS_comp_table(0)(1 downto 0)&"0";
327
                sos_matrix_counter<=(others=>'0');
328
                sos_scan_index<=0;
329
                sos_component<=0;
330
                sos_vi<=to_unsigned(1,4);
331
                sos_hi<=to_unsigned(1,4);
332
        end if;
333
 
334
        if state=sos_scan then
335
        if sos_state=change_comp0 then
336
                DC_AC_decode<=SOS_comp_table(sos_component)(1 downto 0)&"0";
337
 
338
        end if;
339
        if sos_state_old=catch  then
340
                if (((code_word=0) and (sos_matrix_counter>0)) or sos_matrix_counter=63) then
341
                --darf erst ab Position 2 zurückgesetzt werden
342
                --mindestens ein DC und ein AC Wert
343
                        sos_matrix_counter<=(others=>'0');
344
 
345
                else
346
                        sos_matrix_counter<=sos_matrix_counter+1;
347
                end if;
348
 
349
 
350
                if sos_matrix_counter=0 then
351
                        DC_AC_decode<=SOS_comp_table(sos_component)(1 downto 0)&"1";
352
 
353
                end if;
354
                if sos_matrix_counter/=0 then
355
 
356
--              
357
                        if code_word=0 or sos_matrix_counter=63 then
358
 
359
                                if sos_vi<sof0_comp_table(sos_component)(15 downto 12) then
360
                                        sos_vi<=sos_vi+1;
361
 
362
                                else
363
                                        sos_vi<=to_unsigned(1,4);
364
                                        if sos_hi<sof0_comp_table(sos_component)(11 downto 8) then
365
                                                sos_hi<=sos_hi+1;
366
                                        else
367
                                                sos_hi<=to_unsigned(1,4);
368
                                        end if;
369
                                        if sos_hi=sof0_comp_table(sos_component)(11 downto 8) then
370
                                                if sos_component=to_integer(sos_number_comp-1) then
371
                                                        sos_component<=0;
372
                                                else
373
                                                        sos_component<=sos_component+1;
374
                                                end if;
375
                                        end if;
376
                                end if;
377
                        end if;
378
                end if;
379
        end if;
380
        end if;
381
end if;
382
end process;
383
 
384
 
385
 
386
 
387
 
388
wr_en<=huff_wr_en and sos_wr_en and not eoi_detect(0) after 2 ns;
389
 
390
 
391
---decoder
392
 
393
write_active<=wr; --notwendig für die Simulation
394
 
395
 
396
-- waiting for the last block
397
 
398
process(clk)
399
begin
400
if clk'event and clk='1' then
401
if next_state=eoi then
402
      eoi_detect<="00";
403
end if;
404
    if input_reg(15 downto 0)=x"FFD9"then
405
          eoi_detect(0)<='1';
406
    end if;
407
    if eoi_detect(0)='1' and next_eob='1' then
408
          eoi_detect(1)<='1';
409
    end if;
410
end if;
411
end process;
412
 
413
 
414
--destuffing
415
process(clk,write_active)
416
begin
417
if write_active='1' then
418
        if clk'event and clk='1' then
419
                if input_reg(7 downto 0)=X"FF"  then
420
                        stuffing<='1';
421
                else
422
                        stuffing<='0';
423
                end  if;
424
        end if;
425
end if;
426
end process;
427
 
428
 
429
 
430
sos_wr_en<='0' when state=sos_scan and barrel_pointer(4 downto 3) = write_rot_pointer else '1';
431
 
432
---load fifo 
433
--it is a cirular memory 32bit long
434
-- this is the importent part in ths design
435
process(clk,write_active,sos_wr_en,eoi_detect(0))
436
 
437
begin
438
--wr_en<=huff_wr_en and sos_wr_en and not eoi_detect after 2 ns;
439
if clk'event and clk='1' then
440
 
441
if (write_active='1' and stuffing='0')or (sos_wr_en='1' and eoi_detect(0)='1' ) then
442
 
443
 
444
        if  state=SOS_init0 then
445
                write_rot_pointer<="11";
446
                rot_buffer(31 downto 24) <=input_reg(15 downto 8);
447
        end if;
448
        if  state=SOS_init1 then
449
                rot_buffer(23 downto 16) <=input_reg(15 downto 8);
450
                end if;
451
        if  state=SOS_init2 then
452
                rot_buffer(15 downto 8) <=input_reg (15 downto 8);
453
                end if;
454
        if  state=SOS_init3 then
455
                        rot_buffer(7 downto 0)<= input_reg(15 downto 8);
456
 
457
                end if;
458
 
459
 
460
        if  state=sos_scan then
461
                if barrel_pointer(4 downto 3) /= write_rot_pointer then
462
 
463
                        --sos_wr_en<='0';
464
 
465
                write_rot_pointer<=write_rot_pointer-1;
466
                --sos_wr_en<='1';
467
                case write_rot_pointer is
468
                when "00"=>  rot_buffer(7 downto 0)<= input_reg(15 downto 8);
469
                when "01"=>  rot_buffer(15 downto 8) <=input_reg (15 downto 8);
470
                when "10"=> rot_buffer(23 downto 16) <=input_reg(15 downto 8);
471
                when "11"=>   rot_buffer(31 downto 24) <=input_reg(15 downto 8);
472
                when others=> null;
473
                end case;
474
                end if;
475
        end if;
476
end if;
477
end if;
478
end process;
479
 
480
process (barrel_pointer,rot_buffer)
481
   begin
482
      case to_integer(barrel_pointer) is
483
         when 0 => barrel<= rot_buffer (0 downto 0) &  rot_buffer (31 downto 17);
484
         when 1 => barrel<= rot_buffer (1 downto 0) &  rot_buffer (31 downto 18);
485
         when 2 => barrel<= rot_buffer (2 downto 0) &  rot_buffer (31 downto 19);
486
         when 3 => barrel<= rot_buffer (3 downto 0) &  rot_buffer (31 downto 20);
487
         when 4 => barrel<= rot_buffer (4 downto 0) &  rot_buffer (31 downto 21);
488
         when 5 => barrel<= rot_buffer (5 downto 0) &  rot_buffer (31 downto 22);
489
         when 6 => barrel<= rot_buffer (6 downto 0)  &  rot_buffer (31 downto 23);
490
         when 7 => barrel<= rot_buffer (7 downto 0)  &  rot_buffer (31 downto 24);
491
         when 8 => barrel<= rot_buffer (8 downto 0)  &  rot_buffer (31 downto 25);
492
         when 9 => barrel<= rot_buffer (9 downto 0)  &  rot_buffer (31 downto 26);
493
         when 10 => barrel<= rot_buffer (10 downto 0) &  rot_buffer (31 downto 27);
494
         when 11 => barrel<= rot_buffer (11 downto 0) &  rot_buffer (31 downto 28);
495
         when 12 => barrel<= rot_buffer (12 downto 0) &  rot_buffer (31 downto 29);
496
         when 13 => barrel<= rot_buffer (13 downto 0) &  rot_buffer (31 downto 30);
497
         when 14 => barrel<= rot_buffer (14 downto 0) &  rot_buffer (31 downto 31);
498
         when 15 => barrel<= rot_buffer (15 downto 0);
499
         when 16 => barrel<= rot_buffer (16 downto 1);
500
         when 17 => barrel<= rot_buffer (17 downto 2);
501
         when 18 => barrel<= rot_buffer (18 downto 3);
502
         when 19 => barrel<= rot_buffer (19 downto 4);
503
         when 20 => barrel<= rot_buffer (20 downto 5);
504
         when 21 => barrel<= rot_buffer (21 downto 6);
505
         when 22 => barrel<= rot_buffer (22 downto 7);
506
         when 23 => barrel<= rot_buffer (23 downto 8);
507
         when 24 => barrel<= rot_buffer (24 downto 9);
508
         when 25 => barrel<= rot_buffer (25 downto 10);
509
         when 26 => barrel<= rot_buffer (26 downto 11);
510
         when 27 => barrel<= rot_buffer (27 downto 12);
511
         when 28 => barrel<= rot_buffer (28 downto 13);
512
         when 29 => barrel<= rot_buffer (29 downto 14);
513
         when 30 => barrel<= rot_buffer (30 downto 15);
514
         when 31 => barrel<= rot_buffer (31 downto 16);
515
         when others => null;
516
      end case;
517
end process;
518
 
519
--preprocessing calculate the distance of different codelengths
520
--this is parallel at the same periode detect codelength
521
process(clk)
522
begin
523
        if clk'event and clk='1' then
524
                        h_delta(0)<="000000000000000"&(Barrel(15 downto 15));
525
                        h_delta(1)<=(Barrel(15 downto 14)- ("00000000000000"& huff_code_offset(to_integer(DC_AC_decode &"0001")) (0 downto 0)&'0'));
526
                        h_delta(2)<=(Barrel(15 downto 13)- ("0000000000000"&huff_code_offset(to_integer(DC_AC_decode&"0010")) (1 downto 0) &'0'));
527
                        h_delta(3)<=(Barrel(15 downto 12)- ("000000000000"&huff_code_offset(to_integer(DC_AC_decode &"0011")) (2 downto 0) &'0'));
528
                        h_delta(4)<=(Barrel(15 downto 11)- ("00000000000"&huff_code_offset(to_integer(DC_AC_decode &"0100")) (3 downto 0) &'0'));
529
                        h_delta(5)<=(Barrel(15 downto 10)- ("0000000000"&huff_code_offset(to_integer(DC_AC_decode &"0101")) (4 downto 0) &'0'));
530
                        h_delta(6)<=(Barrel(15 downto 9)- ("000000000"&huff_code_offset(to_integer(DC_AC_decode &"0110")) (5 downto 0) &'0'));
531
                        h_delta(7)<=(Barrel(15 downto 8) - ("00000000"&huff_code_offset(to_integer(DC_AC_decode &"0111")) (6 downto 0) &'0'));
532
                        h_delta(8)<=(Barrel(15 downto 7) - ("0000000"&huff_code_offset(to_integer(DC_AC_decode &"1000")) (7 downto 0) &'0'));
533
                        h_delta(9)<=(Barrel(15 downto 6) - ("000000"&huff_code_offset(to_integer(DC_AC_decode &"1001")) (8 downto 0) &'0'));
534
                        h_delta(10)<=(Barrel(15 downto 5)- ("00000"&huff_code_offset(to_integer(DC_AC_decode &"1010")) (9 downto 0) &'0'));
535
                        h_delta(11)<=(Barrel(15 downto 4)- ("0000"&huff_code_offset(to_integer(DC_AC_decode &"1011")) (10 downto 0) &'0'));
536
                        h_delta(12)<=(Barrel(15 downto 3)- ("000"&huff_code_offset(to_integer(DC_AC_decode &"1100")) (11 downto 0) &'0'));
537
                        h_delta(13)<=(Barrel(15 downto 2)- ("00"&huff_code_offset(to_integer(DC_AC_decode &"1101")) (12 downto 0) &'0'));
538
                        h_delta(14)<=(Barrel(15 downto 1)- ("0"&huff_code_offset(to_integer(DC_AC_decode &"1110")) (13 downto 0) &'0'));
539
                        h_delta(15)<=(Barrel(15 downto 0)- (huff_code_offset(to_integer(DC_AC_decode &"1111")) (14 downto 0) &'0'));
540
end if;
541
end process;
542
 
543
process(clk)
544
begin
545
if clk'event and clk='1' then
546
      DC_AC_old(0)<=DC_AC_decode(0);
547
      if state=sos_scan then
548
          if DC_AC_decode(0)='0' and DC_AC_old(0)='1' then
549
              next_eob<='1';
550
          else
551
              next_eob<='0';
552
          end if;
553
      end if;
554
end if;
555
end process;
556
-------------------------------------------------------------------------------------------------------------------------
557
--code_word<=HUFF_RAM(to_integer(ram_pointer(DC_AC_decode,0,read_offset)+h_delta(read_offset)(8 downto 0)));
558
 
559
ram_offset<=ram_pointer(to_integer(DC_AC_decode & to_unsigned(read_offset,4))); --begin the actual codeword table
560
 
561
--recognise code word
562
 
563
process(clk,write_active)
564
begin
565
        if clk'event and clk='1' then
566
                read_offset<=read_offset_a;
567
 
568
        if state=sos_header then
569
                code_word<=to_unsigned(1,code_word'length);
570
        end if;
571
 
572
        if sos_state=catch then
573
                code_word<=HUFF_RAM(to_integer(ram_offset+h_delta(read_offset)(8 downto 0)));
574
        end if;
575
        end if;
576
 
577
end process;
578
 
579
read_offset_a<=
580
 
581
        1 when  Barrel(15 downto 14)< huff_code_offset(to_integer(DC_AC_decode &"0010")) (1 downto 0)  else
582
        2 when  Barrel(15 downto 13)< huff_code_offset(to_integer(DC_AC_decode &"0011")) (2 downto 0)  else
583
        3 when  Barrel(15 downto 12)< huff_code_offset(to_integer(DC_AC_decode &"0100")) (3 downto 0)  else
584
        4 when  Barrel(15 downto 11)< huff_code_offset(to_integer(DC_AC_decode &"0101")) (4 downto 0)  else
585
        5 when  Barrel(15 downto 10)< huff_code_offset(to_integer(DC_AC_decode &"0110")) (5 downto 0)  else
586
        6 when  Barrel(15 downto 9)< huff_code_offset(to_integer(DC_AC_decode &"0111")) (6 downto 0)  else
587
        7 when  Barrel(15 downto 8)< huff_code_offset(to_integer(DC_AC_decode &"1000")) (7 downto 0)  else
588
        8 when  Barrel(15 downto 7)< huff_code_offset(to_integer(DC_AC_decode &"1001")) (8 downto 0)  else
589
        9 when  Barrel(15 downto 6)< huff_code_offset(to_integer(DC_AC_decode &"1010")) (9 downto 0)  else
590
        10 when  Barrel(15 downto 5)< huff_code_offset(to_integer(DC_AC_decode &"1011")) (10 downto 0)  else
591
        11 when  Barrel(15 downto 4)< huff_code_offset(to_integer(DC_AC_decode &"1100")) (11 downto 0)  else
592
        12 when  Barrel(15 downto 3)< huff_code_offset(to_integer(DC_AC_decode &"1101")) (12 downto 0)  else
593
        13 when  Barrel(15 downto 2)< huff_code_offset(to_integer(DC_AC_decode &"1110")) (13 downto 0)  else
594
        14 when  Barrel(15 downto 1) < huff_code_offset(to_integer(DC_AC_decode &"1111")) (14 downto 0)  else
595
        15;
596
 
597
shift<=
598
         1 when  Barrel(15 downto 15)< huff_code_offset(to_integer(DC_AC_decode &"0001")) (0 downto 0)  else
599
        2 when  Barrel(15 downto 14)< huff_code_offset(to_integer(DC_AC_decode &"0010")) (1 downto 0)  else
600
        3 when  Barrel(15 downto 13)< huff_code_offset(to_integer(DC_AC_decode &"0011")) (2 downto 0)  else
601
        4 when  Barrel(15 downto 12)< huff_code_offset(to_integer(DC_AC_decode &"0100")) (3 downto 0)  else
602
        5 when  Barrel(15 downto 11)< huff_code_offset(to_integer(DC_AC_decode &"0101")) (4 downto 0)  else
603
        6 when  Barrel(15 downto 10)< huff_code_offset(to_integer(DC_AC_decode &"0110")) (5 downto 0)  else
604
        7 when  Barrel(15 downto 9)< huff_code_offset(to_integer(DC_AC_decode &"0111")) (6 downto 0)  else
605
        8 when  Barrel(15 downto 8)< huff_code_offset(to_integer(DC_AC_decode &"1000")) (7 downto 0)  else
606
        9 when  Barrel(15 downto 7)< huff_code_offset(to_integer(DC_AC_decode &"1001")) (8 downto 0)  else
607
        10 when  Barrel(15 downto 6)< huff_code_offset(to_integer(DC_AC_decode &"1010")) (9 downto 0)  else
608
        11 when  Barrel(15 downto 5)< huff_code_offset(to_integer(DC_AC_decode &"1011")) (10 downto 0)  else
609
        12 when  Barrel(15 downto 4)< huff_code_offset(to_integer(DC_AC_decode &"1100")) (11 downto 0)  else
610
        13 when  Barrel(15 downto 3)< huff_code_offset(to_integer(DC_AC_decode &"1101")) (12 downto 0)  else
611
        14 when  Barrel(15 downto 2)< huff_code_offset(to_integer(DC_AC_decode &"1110")) (13 downto 0)  else
612
        15 when  Barrel(15 downto 1) < huff_code_offset(to_integer(DC_AC_decode &"1111")) (14 downto 0)  else
613
        16;
614
 
615
 
616
---------------------------------------
617
process(clk,write_active)
618
begin
619
        if clk'event and clk='1' then
620
                if sos_state=catch then
621
                        ba<=barrel;
622
                end if;
623
        end if;
624
end process;
625
 
626
process(clk)
627
begin
628
 
629
        if clk'event and clk='1' then
630
                if sos_state=decode then
631
                        -- ba_1<=ba(15); --entscheide positiver Wert oder negativ 1 posiver Wert!!!!!
632
 
633
                if ba(15)='1' then
634
                        case code_word(3 downto 0) is
635
                        when X"0" => value<=X"0000";
636
                        when X"1" => value<=(0=>'1',others=>'0');
637
                        when X"2" => value<=(1=>'1',0=>ba(14),others=>'0');
638
                        when X"3" => value<=(2=>'1',1=>ba(14),0=>ba(13),others=>'0');
639
                        when X"4" => value<=(3=>'1',2=>ba(14),1=>ba(13),0=>ba(12),others=>'0');
640
                        when X"5" => value<=(4=>'1',3=>ba(14),2=>ba(13),1=>ba(12),0=>ba(11),others=>'0');
641
                        when X"6" => value<=(5=>'1',4=>ba(14),3=>ba(13),2=>ba(12),1=>ba(11),0=>ba(10),others=>'0');
642
                        when X"7" => value<=(6=>'1',5=>ba(14),4=>ba(13),3=>ba(12),2=>ba(11),1=>ba(10),0=>ba(9),others=>'0');
643
                        when others=>null;
644
                        end case;
645
                else
646
                        case code_word(3 downto 0) is
647
                        when X"0" => value<=X"0000";
648
                        when X"1" => value<=to_signed(-1,16);
649
                        when X"2" => value<="000000000000000"&ba(14)-3;
650
                        when X"3" => value<="00000000000000"&ba(14)&ba(13)-7;
651
                        when X"4" => value<="0000000000000"&ba(14)&ba(13)&ba(12)-15;
652
                        when X"5" => value<="000000000000"&ba(14)&ba(13)&ba(12)&ba(11)-31;
653
                        when X"6" => value<="00000000000"&ba(14)&ba(13)&ba(12)&ba(11)&ba(10)-63;
654
                        when X"7" => value<="0000000000"&ba(14)&ba(13)&ba(12)&ba(11)&ba(10)&ba(9)-127;
655
                        when others=>null;
656
                        end case;
657
 
658
 
659
                end if;
660
                end if;
661
        end if;
662
 
663
end process;
664
 
665
--------------------------------------------------------------------------------------------
666
--Anzahl der Componenten lesen und welche Hufftabe genommen wird
667
process (clk,write_active)
668
begin
669
if write_active='1' then
670
        if clk'event and clk='1' then
671
                if next_state=SOS_Header and state/=SOS_Header then
672
                        SOS_number_comp<=input_reg(7 downto 0);
673
                        SOS_Header_state<=selector;
674
                        SOS_Header_index<=0;
675
                end if;
676
 
677
                if SOS_Header_Index < SOS_number_comp then
678
                        if SOS_Header_state=selector  then
679
                                --SOS_selector<=input_reg(15 downto 7);
680
                                SOS_header_state<=table;
681
                         end if;
682
                        if SOS_Header_state=table then
683
                                SOS_comp_table(SOS_Header_index)<= input_reg(7 downto 0);
684
 
685
                                SOS_header_state<=selector;
686
                                SOS_Header_index<=SOS_Header_index+1;
687
                        end if;
688
                end if;
689
 
690
        end if;
691
end if;
692
end process;
693
--------------------------------------------------------------------------------------------
694
process(clk,write_active)
695
begin
696
if write_active='1' then
697
        if clk'event and clk='1' then
698
                if ((next_state=SOF0_length) or (next_state=DQT_length) or (next_state=DHT_length)or (next_state=SOS_length)) then
699
                        Markerlength<=unsigned(input_reg(15 downto 0));
700
                else
701
                        Markerlength<=Markerlength-1;
702
                end if;
703
        end if;
704
end if;
705
end process;
706
 
707
 
708
process(clk,write_active) --DQT  read
709
 
710
variable element: integer range 0 to 64:=0;
711
variable destination: integer range 0 to 1;
712
variable multible : integer range 0 to 1;
713
begin
714
if write_active='1' then
715
        if clk'event and clk='1' then
716
                if state=DQT_length0 then
717
                        multible:=1;
718
 
719
                end if;
720
                if state=DQT_length then
721
                                element:=64;   --paralle wird Markerlength gesetzt
722
 
723
                end if;
724
                if state=DQT_active then
725
                        if element=62 then
726
                                if Markerlength<10 then  -- Wenn mehrere Tabellen hintereinander hängen
727
                        --der wert muss ausreichend gross sein 10 nur gewaehlt
728
                                multible:=0;
729
                                end if;
730
                        end if;
731
                        if (element=64 and multible=1) then
732
 
733
                                destination:=to_integer(unsigned(input_reg (8 downto 8)));
734
                                element:=0;
735
 
736
                        end if;
737
                        if element <64 then
738
                                qtable(destination,zigzag(element))<=unsigned(input_reg(7 downto 0));
739
                                element:=element +1;
740
                        end if;
741
                end if;
742
        end if;
743
end if;
744
end process;
745
 
746
 
747
process(clk,write_active)
748
begin
749
 
750
if clk'event and clk='1' then
751
          if write_active='1' then
752
                input_reg (7 downto 0)<= data_in;
753
                input_reg (15 downto 8)<= input_reg(7 downto 0);
754
                input_reg (23 downto 16)<= input_reg (15 downto 8);
755
          end if;
756
          if eoi_detect(0)='1' then     --else bit stuffing at 0xFFD9 !!
757
                 input_reg (7 downto 0)<= X"00";
758
                input_reg (15 downto 8)<= input_reg(7 downto 0);
759
                input_reg (23 downto 16)<= input_reg (15 downto 8);
760
          end if;
761
end if;
762
end process;
763
 
764
 
765
state_machine: process(clk,write_active)
766
begin
767
 
768
 
769
 
770
        if clk'event and clk='1' then
771
        if  eoi_detect="11" and output_valid='1' then next_state<=EOI;
772
                end if;
773
        if (state=DHT_active and huff_table_end='1') then
774
                state<=DHT_destination;
775
                next_state<=DHT_number;
776
                dht_counter<=0;
777
        end if;
778
            if next_state=EOI then
779
                state<=EOI;
780
            end if;
781
            if write_active='1' then
782
                        state<=next_state;
783
                        case input_reg(15 downto 0) is
784
                        when  X"FFD8"=>         next_state<=SOI;
785
                        when  x"FFE0"=>         next_state<=APP0;
786
                        when  x"FFDB"=>         next_state<=DQT_length0;
787
                        when  x"FFC0"=>         next_state<=SOF0_length0;
788
                        when  x"FFC4"=>         next_state<=DHT_length0;
789
                        when  x"FFDA"=>         next_state<=SOS_length0;
790
                        --when  x"FFD9"=>               eoi_detect(0)<='1';
791
                        when others =>
792
 
793
                        if next_state=DQT_length0 then next_state<=DQT_length; end if;
794
                        if next_state=DQT_length then next_state<=DQT_active; end if;
795
 
796
                        if next_state=SOF0_length0 then next_state<=SOF0_length; end if;
797
                        if next_state=SOF0_length then next_state<=SOF0_precision; end if;
798
                        if next_state=SOF0_precision then next_state<=SOF0_y_high; end if;
799
                        if next_state=SOF0_y_high then next_state<=SOF0_y_low; end if;
800
                        if next_state=SOF0_y_low then next_state<=SOF0_x_high; end if;
801
                        if next_state=SOF0_x_high then next_state<=SOF0_x_low; end if;
802
                        if next_state=SOF0_x_low then next_state<=SOF0_nr_comp; end if;
803
                        if next_state=SOF0_nr_comp then next_state<=SOF0_active; end if;
804
 
805
                        --SOS
806
                        if next_state=SOS_length0 then next_state<=SOS_length; end if;
807
                        if next_state=SOS_length then next_state<=SOS_header; end if;
808
                        if next_state=SOS_header and markerlength=3 then --der scan hat keine Laengenmarke
809
                        next_state<=SOS_init0; end if;
810
                        if next_state=SOS_init0 then next_state<=SOS_init1; end if;
811
                        if next_state=SOS_init1 then next_state<=SOS_init2; end if;
812
                        if next_state=SOS_init2 then next_state<=SOS_init3; end if;
813
                        if next_state=SOS_init3 then next_state<=SOS_scan; end if;
814
 
815
                        --Hufftable
816
                        if next_state=DHT_length0 then next_state<=DHT_length; end if;
817
                        if next_state=DHT_length then next_state<=DHT_destination; end if;
818
                        if next_state=DHT_destination then
819
                                next_state<=DHT_Number;
820
                                dht_counter<=0;
821
                         end if;
822
                        if next_state=DHT_Number then
823
                                dht_counter<=dht_counter+1;
824
                        end if;
825
                        if next_state=DHT_Number and dht_counter=15 then
826
                        next_state<=DHT_active; end if;
827
                        --multible tables
828
 
829
                        if next_state=DHT_active and Markerlength=2 then next_state<=idle; end if;
830
                                        end case;
831
 
832
end if;
833
end if;
834
end process state_machine;
835
 
836
--------------------------------------------------
837
 --x_size,y_size:unsigned (15 downto 0);  --picture size
838
--------------------------------------------------
839
 
840
process(clk,write_active) --SOF0 einlesen
841
 
842
begin
843
if write_active='1' then
844
        if clk'event and clk='1' then
845
                if state=SOF0_y_high then
846
                        y_size(15 downto 8) <= input_reg(15 downto 8);
847
                end if;
848
                if state=SOF0_y_low  then
849
                        y_size(7 downto 0) <= input_reg(15 downto 8);
850
                end if;
851
                if state=SOF0_x_high then
852
                        x_size(15 downto 8) <= input_reg(15 downto 8);
853
                end if;
854
                if state=SOF0_x_low  then
855
                        x_size(7 downto 0) <= input_reg(15 downto 8);
856
                end if;
857
                if state=SOF0_nr_comp  then
858
                        SOF0_number_comp <= input_reg(15 downto 8);
859
                        SOF0_Header_index<= 0;
860
                        SOF0_header_state<=selector;
861
                end if;
862
                if SOF0_Header_Index < SOF0_number_comp then
863
 
864
                        if SOF0_Header_state=selector then
865
                                SOF0_Header_state<=Sampling;
866
                        end if;
867
                        if SOF0_Header_state=sampling then
868
                                SOF0_comp_table(SOF0_Header_index)(15 downto 8)<=input_reg(15 downto 8);
869
                                SOF0_Header_state<=table;
870
                        end if;
871
                        if SOF0_Header_state=table then
872
                                SOF0_comp_table(SOF0_Header_index)(7 downto 0)<=input_reg(15 downto 8);
873
                                SOF0_Header_state<=selector;
874
                                SOF0_Header_index<=SOF0_Header_index+1;
875
                        end if;
876
                end if;
877
 
878
        end if;
879
end if;
880
end process;
881
----------------------------------------------------
882
process(clk,write_active) --Hufftable read
883
variable dest:unsigned(1 downto 0); --destination
884
variable DC_AC:unsigned(0 downto 0); --AC oder DC Table
885
                                        --'0' entspricht DC
886
begin
887
 
888
 
889
if clk'event and clk='1' then
890
        if wr='1' then
891
                if state=SOI then
892
                        ADDR<=0;
893
                end if;
894
                if state=DHT_destination then
895
                        index<=0;
896
                        dest:=input_reg(9 downto 8);--0-1 Baseline 
897
                        DC_AC:=input_reg(12 downto 12);  --0 DC / 1 AC
898
 
899
                end if;
900
 
901
                if state=DHT_Number then
902
                        huff_table_end<='0';
903
                        huff_a<=to_unsigned(0,8);
904
                        huff_code_number(index)<=input_reg(15 downto 8);
905
                        h_code<=X"0000";
906
                        if index< 15 then
907
                                index<=index+1;
908
                        end if;
909
                        if index=15 then
910
                                index<=0;
911
                        end if;
912
                end if;
913
 
914
                if state=DHT_active and huff_a>0 then
915
                                huff_a<=huff_a-1;
916
                                Huff_RAM(addr)<=input_reg(15 downto 8);
917
                                addr<=addr+1;
918
                                h_code<=h_code+1;
919
                end if;
920
        end if;
921
        --if next_state=DHT_active
922
        if state=DHT_active and huff_a=0 then
923
                        huff_a<=huff_code_number(index);
924
                        ram_pointer(to_integer(dest&DC_AC&to_unsigned(index,4)))<= addr;
925
                        if index<15 then
926
                                index<=index+1;
927
                                h_code<=h_code (14 downto 0) &'0' ; --shift left
928
 
929
                                huff_code_offset(to_integer(dest& DC_AC & to_unsigned(index,4)))<=h_code;
930
                        end if;
931
                        if index=15 then
932
                                huff_table_end<='1';
933
                        end if;
934
 
935
        end if;
936
end if;
937
 
938
end process;
939
huff_wr_en<='0' when ((huff_a=0 or huff_table_end='1')and  (state=dht_active) and not (next_state=idle)) else '1';
940
 
941
 
942
--- syncronisation information
943
-- sop.......... start of picture 
944
-- eop.......... end of picture
945
process(clk)
946
begin
947
if  clk'event and clk='1' then
948
    if state=sos_length then
949
        sop<='1';
950
    else
951
        sop<='0';
952
    end if;
953
 
954
    if state=EOI then
955
        eop<='1';
956
    else
957
        eop<='0';
958
    end if;
959
end if;
960
end process;
961
 
962
end Behavioral;
963
 

powered by: WebSVN 2.1.0

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