mdcttb_pkg.vhd

来自「Pure hardware JPEG Encoder design. Packa」· VHDL 代码 · 共 468 行 · 第 1/2 页

VHD
468
字号
      when 13 => c := 'D';      when 14 => c := 'E';      when 15 => c := 'F';      when 16 => c := 'G';      when 17 => c := 'H';      when 18 => c := 'I';      when 19 => c := 'J';      when 20 => c := 'K';      when 21 => c := 'L';      when 22 => c := 'M';      when 23 => c := 'N';      when 24 => c := 'O';      when 25 => c := 'P';      when 26 => c := 'Q';      when 27 => c := 'R';      when 28 => c := 'S';      when 29 => c := 'T';      when 30 => c := 'U';      when 31 => c := 'V';      when 32 => c := 'W';      when 33 => c := 'X';      when 34 => c := 'Y';      when 35 => c := 'Z';      when others => c := '?';    end case;    return c;  end CHR;                            --------------------------------------------------------------------------  -- convert INTEGER to STRING using specified base   --------------------------------------------------------------------------  function STR(int: INTEGER; base: INTEGER) return STRING is   variable temp:      STRING(1 to 10);   variable num:       INTEGER;   variable abs_int:   INTEGER;   variable len:       INTEGER := 1;   variable power:     INTEGER := 1;  begin   -- bug fix for negative numbers   abs_int := abs(int);   num     := abs_int;   while num >= base loop                          len := len + 1;                               num := num / base;                          end loop ;                                    for i in len downto 1 loop                      temp(i) := chr(abs_int/power mod base);       power := power * base;                      end loop ;                                    -- return result and add sign if required   if int < 0 then      return '-'& temp(1 to len);    else      return temp(1 to len);   end if;  end STR;     ------------------------------------------------  -- computes DCT1D  ------------------------------------------------  function COMPUTE_REF_DCT1D(input_matrix : I_MATRIX_TYPE; shift : BOOLEAN)   return I_MATRIX_TYPE is   variable fXm : VECTOR4 := (0.0,0.0,0.0,0.0);   variable fXs : VECTOR4 := (0.0,0.0,0.0,0.0);   variable fYe : VECTOR4 := (0.0,0.0,0.0,0.0);   variable fYo : VECTOR4 := (0.0,0.0,0.0,0.0);   variable ref_dct_matrix : I_MATRIX_TYPE;   variable norma_input : MATRIX_TYPE;  begin     -- compute reference coefficients     for x in 0 to N-1 loop            for s in 0 to 7 loop         if shift = TRUE then           norma_input(x,s) := (REAL(input_matrix(x,s))- REAL(LEVEL_SHIFT))/2.0;         else           norma_input(x,s) := REAL(input_matrix(x,s))/2.0;         end if;       end loop;       fXs(0) := norma_input(x,0)+norma_input(x,7);       fXs(1) := norma_input(x,1)+norma_input(x,6);       fXs(2) := norma_input(x,2)+norma_input(x,5);       fXs(3) := norma_input(x,3)+norma_input(x,4);              fXm(0) := norma_input(x,0)-norma_input(x,7);       fXm(1) := norma_input(x,1)-norma_input(x,6);       fXm(2) := norma_input(x,2)-norma_input(x,5);       fXm(3) := norma_input(x,3)-norma_input(x,4);              for k in 0 to N/2-1 loop         fYe(k) := REAL(CONV_INTEGER(Ce(k,0)))*fXs(0) +                    REAL(CONV_INTEGER(Ce(k,1)))*fXs(1) +                    REAL(CONV_INTEGER(Ce(k,2)))*fXs(2) +                    REAL(CONV_INTEGER(Ce(k,3)))*fXs(3);         fYo(k) := REAL(CONV_INTEGER(Co(k,0)))*fXm(0) +                    REAL(CONV_INTEGER(Co(k,1)))*fXm(1) +                    REAL(CONV_INTEGER(Co(k,2)))*fXm(2) +                    REAL(CONV_INTEGER(Co(k,3)))*fXm(3);       end loop;                     -- transpose matrix by writing in row order       ref_dct_matrix(0,x) := INTEGER(fYe(0)/REAL((2**(COE_W-1))));       ref_dct_matrix(1,x) := INTEGER(fYo(0)/REAL((2**(COE_W-1))));       ref_dct_matrix(2,x) := INTEGER(fYe(1)/REAL((2**(COE_W-1))));       ref_dct_matrix(3,x) := INTEGER(fYo(1)/REAL((2**(COE_W-1))));       ref_dct_matrix(4,x) := INTEGER(fYe(2)/REAL((2**(COE_W-1))));       ref_dct_matrix(5,x) := INTEGER(fYo(2)/REAL((2**(COE_W-1))));       ref_dct_matrix(6,x) := INTEGER(fYe(3)/REAL((2**(COE_W-1))));       ref_dct_matrix(7,x) := INTEGER(fYo(3)/REAL((2**(COE_W-1))));            end loop;            return ref_dct_matrix;   end COMPUTE_REF_DCT1D;       -----------------------------------------------   -- compares NxN matrices, logs failure if difference   -- greater than maximum error specified   -----------------------------------------------   procedure CMP_MATRIX(ref_matrix    : in I_MATRIX_TYPE;                        dcto_matrix    : in I_MATRIX_TYPE;                       max_error      : in INTEGER;                       error_matrix   : out I_MATRIX_TYPE;                       error_cnt      : inout INTEGER                       ) is     variable error_matrix_v : I_MATRIX_TYPE;   begin      for a in 0 to N - 1 loop       for b in 0 to N - 1 loop         error_matrix_v(a,b) := ref_matrix(a,b) - dcto_matrix(a,b);         if abs(error_matrix_v(a,b)) > max_error then           error_cnt := error_cnt + 1;           assert false             report "E01: DCT max error violated!"             severity Error;         end if;       end loop;     end loop;     error_matrix := error_matrix_v;  end CMP_MATRIX;    ------------------------------------------------  -- computes IDCT on NxN matrix  ------------------------------------------------  function COMPUTE_REF_IDCT(X : I_MATRIX_TYPE)   return I_MATRIX_TYPE is    variable i  : INTEGER := 0;    variable j  : INTEGER := 0;    variable u  : INTEGER := 0;    variable v  : INTEGER := 0;    variable Cu : REAL;    variable Cv : REAL;    variable xi : MATRIX_TYPE := null_data_r;    variable xr : I_MATRIX_TYPE;  begin    -- idct    for i in 0 to N-1 loop      for j in 0 to N-1 loop          for u in 0 to N-1 loop          if u = 0 then            Cu := 1.0/sqrt(2.0);          else            Cu := 1.0;          end if;                   for v in 0 to N-1 loop            if v = 0 then              Cv := 1.0/sqrt(2.0);            else              Cv := 1.0;            end if;            xi(i,j) := xi(i,j) +                      2.0/REAL(N)*Cu*Cv*REAL(X(u,v))*                     cos( ( (2.0*REAL(i)+1.0)*REAL(u)*MATH_PI ) / (2.0*REAL(N)) )*                     cos( ( (2.0*REAL(j)+1.0)*REAL(v)*MATH_PI ) / (2.0*REAL(N)) );            xr(i,j) := INTEGER(ROUND(xi(i,j)))+LEVEL_SHIFT;          end loop;             end loop;      end loop;    end loop;    return xr;  end COMPUTE_REF_IDCT;    ------------------------------------------------  -- computes peak signal to noise ratio  -- for reconstruced and input image data  ------------------------------------------------  function COMPUTE_PSNR(ref_input : I_MATRIX_TYPE;                        reconstr_input : I_MATRIX_TYPE) return REAL is    variable psnr_tmp : REAL := 0.0;  begin    for i in 0 to N-1 loop      for j in 0 to N-1 loop        psnr_tmp := psnr_tmp + (REAL(ref_input(i,j))-REAL(reconstr_input(i,j)))**2;      end loop;    end loop;    psnr_tmp := psnr_tmp / (REAL(N)*REAL(N));    psnr_tmp := 10.0*LOG10( (REAL(MAX_PIX_VAL)**2) / psnr_tmp );    return psnr_tmp;    end COMPUTE_PSNR;    ------------------------------------------------  -- computes peak signal to noise ratio  -- for reconstruced and input image data  ------------------------------------------------  function COMPUTE_PSNR(ref_input : IMAGE_TYPE;                        reconstr_input : IMAGE_TYPE;                       ysize : INTEGER;                       xsize : INTEGER                       ) return REAL is    variable psnr_tmp : REAL := 0.0;    variable lineb    : LINE;  begin    for i in 0 to ysize-1 loop      for j in 0 to xsize-1 loop        psnr_tmp := psnr_tmp +                     (REAL(ref_input(i,j))-REAL(reconstr_input(i,j)))**2;      end loop;    end loop;    psnr_tmp := psnr_tmp / (REAL(ysize)*REAL(xsize));    --WRITE(lineb,STRING'("MSE Mean Squared Error is "));    --WRITE(lineb,psnr_tmp);     --assert false    --  report lineb.all    --  severity Note;    psnr_tmp := 10.0*LOG10( (REAL(MAX_PIX_VAL)**2) / psnr_tmp );    return psnr_tmp;    end COMPUTE_PSNR;                         end MDCTTB_PKG;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?