function ACH = AC_Huffman(AC,n);
%bright AC coeff codewords
T1={'1010' '00' '01' '100' '1011' '11010' '1111000' '11111000' '1111110110' '1111111110000010' '1111111110000011';
'1100' '11011' '1111001' '111110110' '11111110110' '1111111110000100' '1111111110000101' '1111111110000110' '1111111110000111' '1111111110001000' '0';
'11100' '11111001' '1111110111' '111111110100' '1111111110001001' '1111111110001010' '1111111110001011' '1111111110001100' '1111111110001101' '1111111110001110' '0';
'111010' '111110111' '111111110101' '1111111110001111' '1111111110010000' '1111111110010001' '1111111110010010' '1111111110010011' '1111111110010100' '1111111110010101' '0';
'111011' '1111111000' '1111111110010110' '1111111110010111' '1111111110011000' '1111111110011001' '1111111110011010' '1111111110011011' '1111111110011100' '1111111110011101' '0';
'1111010' '11111110111' '1111111110011110' '1111111110011111' '1111111110100000' '1111111110100001' '1111111110100010' '1111111110100011' '1111111110100100' '1111111110100001' '0';
'1111011' '111111110110' '1111111110100110' '1111111110100111' '1111111110101000' '1111111110101001' '1111111110101010' '1111111110101011' '1111111110101100' '1111111110101101' '0';
'11111010' '111111110111' '1111111110101110' '1111111110101111' '1111111110110000' '1111111110110001' '1111111110110010' '1111111110110011' '1111111110110100' '1111111110110101' '0';
'111111000' '111111111000000' '1111111110110110' '1111111110110111' '1111111110111000' '1111111110111001' '1111111110111010' '1111111110111011' '1111111110111100' '1111111110111101' '0';
'111111001' '1111111110111110' '1111111110111111' '1111111111000000' '1111111111000001' '1111111111000010' '1111111111000011' '1111111111000100' '1111111111000101' '1111111111000110' '0';
'111111010' '1111111111000111' '1111111111001000' '1111111111001001' '1111111111001010' '1111111111001011' '1111111111001100' '1111111111001101' '1111111111001110' '1111111111001111' '0';
'1111111001' '1111111111010000' '1111111111010001' '1111111111010010' '1111111111010011' '1111111111010100' '1111111111010101' '1111111111010110' '1111111111010111' '1111111111011000' '0';
'1111111010' '1111111111011001' '1111111111011010' '1111111111011011' '1111111111011100' '1111111111011101' '1111111111011110' '1111111111011111' '1111111111100000' '1111111111100001' '0';
'11111111000' '1111111111100010' '1111111111100011' '1111111111100100' '1111111111100101' '1111111111100110' '1111111111100111' '1111111111101000' '1111111111101001' '1111111111101010' '0';
'1111111111101011' '1111111111101100' '1111111111101101' '1111111111101110' '1111111111101111' '1111111111110000' '1111111111110001' '1111111111110010' '1111111111110011' '1111111111110100' '0';
'11111111001' '1111111111110101' '1111111111110110' '1111111111110111' '1111111111111000' '1111111111111001' '1111111111111010' '1111111111111011' '1111111111111100' '1111111111111101' '1111111111111111'};
%chroma AC coeff codewords
T2={'00' '01' '100' '1010' '11000' '11001' '111000' '1111000' '111110100' '1111110110' '111111110100';
'1011' '111001' '11110110' '111110101' '11111110110' '111111110101' '1111111110001000' '1111111110001001' '1111111110001010' '1111111110001011' '0';
'11010' '11110111' '1111110111' '111111110110' '111111111000010' '1111111110001100' '1111111110001101' '1111111110001110' '1111111110001111' '1111111110010000' '0';
'11010' '111110111' '1111110111' '11111110110' '1111111110010001' '1111111110010010' '1111111110010011' '1111111110010100' '1111111110010101' '1111111110010110' '0';
'111010' '111110110' '1111111110010111' '1111111110011000' '1111111110011001' '1111111110011010' '1111111110011011' '1111111110011100' '1111111110011101' '1111111110011110' '0';
'111011' '1111111001' '1111111110011111' '1111111110100000' '1111111110100001' '1111111110100010' '1111111110100011' '1111111110100100' '1111111110100101' '1111111110100010' '0';
'1111001' '11111110111' '1111111110100111' '1111111110101000' '1111111110101001' '1111111110101010' '1111111110101011' '1111111110101100' '1111111110101101' '1111111110101110' '0';
'1111010' '11111111000' '1111111110101111' '1111111110110000' '1111111110110001' '1111111110110010' '1111111110110011' '1111111110110100' '1111111110110101' '1111111110110110' '0';
'11111001' '1111111110110111' '1111111110111000' '1111111110111001' '1111111110111010' '1111111110111011' '1111111110111100' '1111111110111101' '1111111110111110' '1111111110111111' '0';
'111110111' '1111111111000000' '1111111111000001' '1111111111000010' '1111111111000011' '1111111111000100' '1111111111000101' '1111111111000110' '1111111111000111' '1111111111001000' '0';
'111111000' '1111111111001001' '1111111111001010' '1111111111001011' '1111111111001100' '1111111111001101' '1111111111001110' '1111111111001111' '1111111111010000' '1111111111010001' '0';
'111111001' '1111111111010010' '1111111111010011' '1111111111010100' '1111111111010101' '1111111111010110' '1111111111010111' '1111111111011000' '1111111111011001' '1111111111011010' '0';
'111111010' '1111111111011011' '1111111111011100' '1111111111011101' '1111111111011110' '1111111111011111' '1111111111100000' '1111111111100001' '1111111111100010' '1111111111100011' '0';
'11111111001' '1111111111100100' '1111111111100101' '1111111111100110' '1111111111100111' '1111111111101000' '1111111111101001' '1111111111101010' '1111111111101011' '1111111111101100' '0';
'11111111100000' '1111111111101101' '1111111111101110' '1111111111101111' '1111111111110000' '1111111111110001' '1111111111110010' '1111111111110011' '1111111111110100' '1111111111110101' '0';
'1111111010' '111111111000011' '1111111111110110' '1111111111110111' '1111111111111000' '1111111111111001' '1111111111111010' '1111111111111011' '1111111111111100' '1111111111111101' '1111111111111111'};
%RLC coding
k=0;
j=1;
for i=1:63
if AC(i)==0 && k<15 % zeros<15 before non-zero coeffs
k=k+1;
else
if k==15 % zeros=15 before non-zero coeffs
RLC(j)=15;
RLC(j+1)=0;
else % zeros=0 before non-zero coeffs
RLC(j)=k;
RLC(j+1)=AC(i);
end
k=0;
j=j+2;
end
end
%last one is zero
if AC(63)==0
RLC(j)=0;
RLC(j+1)=0;
end
while(length(RLC)>2)
if ((RLC(length(RLC)) == 0) && (RLC(length(RLC)-2) == 0))
RLC = RLC(1:length(RLC)-2);
RLC(length(RLC)-1) = 0;
else
break
end
end
ACH='';
for i=1:2:length(RLC)
NNNN=RLC(i);
if RLC(i+1)==0
SSSS=0;
elseif abs(RLC(i+1))==1
SSSS=1;
elseif ( abs(RLC(i+1))>=2 && abs(RLC(i+1))<=3 )
SSSS=2;
elseif ( abs(RLC(i+1))>=4 && abs(RLC(i+1))<=7 )
SSSS=3;
elseif ( abs(RLC(i+1))>=8 && abs(RLC(i+1))<=15 )
SSSS=4;
elseif ( abs(RLC(i+1))>=16 && abs(RLC(i+1))<=31 )
SSSS=5;
elseif ( abs(RLC(i+1))>=32 && abs(RLC(i+1))<=63 )
SSSS=6;
elseif ( abs(RLC(i+1))>=64 && abs(RLC(i+1))<=127 )
SSSS=7;
elseif ( abs(RLC(i+1))>=128 && abs(RLC(i+1))<=255 )
SSSS=8;
elseif ( abs(RLC(i+1))>=256 && abs(RLC(i+1))<=511 )
SSSS=9;
elseif ( abs(RLC(i+1))>=512 && abs(RLC(i+1))<=1023 )
SSSS=10;
end
%% prefix code
if n==1
if NNNN==0 | NNNN==15
S1=char(T1(NNNN+1,SSSS+1));
else
S1=char(T1(NNNN+1,SSSS));
end
else
if NNNN==0 | NNNN==15
S1=char(T2(NNNN+1,SSSS+1));
else
S1=char(T2(NNNN+1,SSSS));
end
end
%% suffix code
if RLC(i)==0 && RLC(i+1)==0
S2='';
else
if RLC(i+1)>=0
S2=dec