-
Notifications
You must be signed in to change notification settings - Fork 0
/
AC_Huffman.m
53 lines (42 loc) · 1.48 KB
/
AC_Huffman.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function AC = AC_Huffman(ac_coeff, ACTAB)
%AC_Huffman: AC Huffman encoding
%ac_coeff: AC coefficient
%ACTAB: AC encoding table
%AC: output of encoding
ZRL = [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1];
EOB = [1, 0, 1, 0];
AC_list = [];
for n = 1:size(ac_coeff, 2)
ac = ac_coeff(:, n).';
not_0 = find(ac);
if isempty(not_0)
AC_list = [AC_list, EOB];
continue;
end
% get AC run
ac_run = [not_0(1), diff(not_0)] - 1;
for i = 1:length(ac_run)
while (ac_run(i) >= 16)
AC_list = [AC_list, ZRL];
ac_run(i) = ac_run(i) - 16;
end
% find AC size
Amp_coeff = ac(not_0(i));
ac_size = ceil(log2(abs(Amp_coeff) + 1));
% find Huffman code use ACTAB
category = find(ACTAB(:, 1) == ac_run(i) & ACTAB(:, 2) == ac_size);
ac_huff = ACTAB(category, 4:(3 + ACTAB(category, 3)));
% Amplitude coding
if (Amp_coeff >= 0)
bin = split(dec2bin(Amp_coeff), '');
Amplitude = str2double(bin(2:end - 1)).';
else
bin = split(dec2bin(-Amp_coeff), '');
Amplitude = ~str2double(bin(2:end - 1)).';
end
AC_list = [AC_list, ac_huff, Amplitude];
end
AC_list = [AC_list, EOB];
end
AC = num2str(AC_list, '%d');
end