function [ run, level, bits ] = get_AC_codes( stream, intra_mode )

level = -1;
% Escape code
if strcmp(stream(1:6), '000001')
    
    run = bin2dec(stream(7:12));
    level =  bin2dec(stream(13:19))*(-1)^(stream(20));
    switch stream(13)
        case '0'
       level = bin2dec(stream(14:20));
        case '1'
            level = -128+bin2dec(stream(14:20));
        
    end
    bits = 6+6+8;
    return
    
end

% Intra coded
if intra_mode
    if strcmp(stream(1:2),'11')
        run = 0; level = 1; bits = 3; level = level*(-1)^(stream(bits)); return
    end
    % Non-intra
else
    if strcmp(stream(1),'1')
        run = 0; level = 1; bits = 2; level = level*(-1)^(stream(bits)); return
    end
    
end

% EOB
% switch stream(1:2)
%     case '10'
%         run = 0; level = 0; bits = 2;
% end
% 4 bits
switch stream(1:3)
    case '011'
        run = 1; level = 1; bits = 4; level = level*(-1)^(stream(bits)); return
end
% 5 bits
switch stream(1:4)
    case '0100'
        run = 0; level = 2; bits = 5; level = level*(-1)^(stream(bits)); return
    case '0101'
        run = 2; level = 1; bits = 5; level = level*(-1)^(stream(bits)); return
end
% 6 bits
switch stream(1:5)
    case '00101'
        run = 0; level = 3; bits = 6; level = level*(-1)^(stream(bits)); return
    case '00111'
        run = 3; level = 1; bits = 6; level = level*(-1)^(stream(bits)); return
    case '00110'
        run = 4; level = 1; bits = 6; level = level*(-1)^(stream(bits)); return
end
% 7 bits
switch stream(1:6)
    case '000110'
        run = 1; level = 2; bits = 7; level = level*(-1)^(stream(bits)); return
    case '000111'
        run = 5; level = 1; bits = 7; level = level*(-1)^(stream(bits)); return
    case '000101'
        run = 6; level = 1; bits = 7; level = level*(-1)^(stream(bits)); return
    case '000100'
        run = 7; level = 1; bits = 7; level = level*(-1)^(stream(bits)); return
end
% 8 bits
switch stream(1:7)
    case '0000110'
        run = 0; level = 4; bits = 8; level = level*(-1)^(stream(bits)); return
    case '0000100'
        run = 2; level = 2; bits = 8; level = level*(-1)^(stream(bits)); return
    case '0000111'
        run = 8; level = 1; bits = 8; level = level*(-1)^(stream(bits)); return
    case '0000101'
        run = 9; level = 1; bits = 8; level = level*(-1)^(stream(bits)); return
end
% 9 bits
switch stream(1:8)
    case '00100110'
        run = 0; level = 5; bits = 9; level = level*(-1)^(stream(bits)); return
    case '00100001'
        run = 0; level = 6; bits = 9; level = level*(-1)^(stream(bits)); return
    case '00100101'
        run = 1; level = 3; bits = 9; level = level*(-1)^(stream(bits)); return
    case '00100100'
        run = 3; level = 2; bits = 9; level = level*(-1)^(stream(bits)); return
    case '00100111'
        run = 10; level = 1; bits = 9; level = level*(-1)^(stream(bits)); return
    case '00100011'
        run = 11; level = 1; bits = 9; level = level*(-1)^(stream(bits)); return
    case '00100010'
        run = 12; level = 1; bits = 9; level = level*(-1)^(stream(bits)); return
    case '00100000'
        run = 13; level = 1; bits = 9; level = level*(-1)^(stream(bits)); return
end
% 11 bits
switch stream(1:10)
    case '0000001010'
        run = 0; level = 7; bits = 11; level = level*(-1)^(stream(bits)); return
    case '0000001100'
        run = 1; level = 4; bits = 11; level = level*(-1)^(stream(bits)); return
    case '0000001011'
        run = 2; level = 3; bits = 11; level = level*(-1)^(stream(bits)); return
    case '0000001111'
        run = 4; level = 2; bits = 11; level = level*(-1)^(stream(bits)); return
    case '0000001001'
        run = 5; level = 2; bits = 11; level = level*(-1)^(stream(bits)); return
    case '0000001110'
        run = 14; level = 1; bits = 11; level = level*(-1)^(stream(bits)); return
    case '0000001101'
        run = 15; level = 1; bits = 11; level = level*(-1)^(stream(bits)); return
    case '0000001000'
        run = 16; level = 1; bits = 11; level = level*(-1)^(stream(bits)); return
end
% 13 bits
switch stream(1:12)
    case '000000011101'
        run = 0; level = 8; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000011000'
        run = 0; level = 9; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000010011'
        run = 0; level = 10; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000010000'
        run = 0; level = 11; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000011011'
        run = 1; level = 5; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000010100'
        run = 2; level = 4; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000011100'
        run = 3; level = 3; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000010010'
        run = 4; level = 3; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000011110'
        run = 6; level = 2; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000010101'
        run = 7; level = 2; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000010001'
        run = 8; level = 2; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000011111'
        run = 17; level = 1; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000011010'
        run = 18; level = 1; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000011001'
        run = 19; level = 1; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000010111'
        run = 20; level = 1; bits = 13; level = level*(-1)^(stream(bits)); return
    case '000000010110'
        run = 21; level = 1; bits = 13; level = level*(-1)^(stream(bits)); return
end
% 14 bits
switch stream(1:13)
    case '0000000011010'
        run = 0; level = 12; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000011001'
        run = 0; level = 13; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000011000'
        run = 0; level = 14; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000010111'
        run = 0; level = 15; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000010110'
        run = 1; level = 6; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000010101'
        run = 1; level = 7; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000010100'
        run = 2; level = 5; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000010011'
        run = 3; level = 4; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000010010'
        run = 5; level = 3; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000010001'
        run = 9; level = 2; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000010000'
        run = 10; level = 2; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000011111'
        run = 22; level = 1; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000011110'
        run = 23; level = 1; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000011101'
        run = 24; level = 1; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000011100'
        run = 25; level = 1; bits = 14; level = level*(-1)^(stream(bits)); return
    case '0000000011011'
        run = 26; level = 1; bits = 14; level = level*(-1)^(stream(bits)); return
end
% 15 bits
switch stream(1:14)
    case '00000000011111'
        run = 0; level = 16; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000011110'
        run = 0; level = 17; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000011101'
        run = 0; level = 18; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000011100'
        run = 0; level = 19; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000011011'
        run = 0; level = 20; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000011010'
        run = 0; level = 21; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000011001'
        run = 0; level = 22; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000011000'
        run = 0; level = 23; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000010111'
        run = 0; level = 24; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000010110'
        run = 0; level = 25; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000010101'
        run = 0; level = 26; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000010100'
        run = 0; level = 27; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000010011'
        run = 0; level = 28; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000010010'
        run = 0; level = 29; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000010001'
        run = 0; level = 30; bits = 15; level = level*(-1)^(stream(bits)); return
    case '00000000010000'
        run = 0; level = 31; bits = 15; level = level*(-1)^(stream(bits)); return
end

% 16 bits
switch stream(1:15)
    case '000000000011000'
        run = 0; level = 32; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000010111'
        run = 0; level = 33; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000010110'
        run = 0; level = 34; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000010101'
        run = 0; level = 35; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000010100'
        run = 0; level = 36; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000010011'
        run = 0; level = 37; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000010010'
        run = 0; level = 38; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000010001'
        run = 0; level = 39; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000010000'
        run = 0; level = 40; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000011111'
        run = 1; level = 8; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000011110'
        run = 1; level = 9; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000011101'
        run = 1; level = 10; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000011100'
        run = 1; level = 11; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000011011'
        run = 1; level = 12; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000011010'
        run = 1; level = 13; bits = 16; level = level*(-1)^(stream(bits)); return
    case '000000000011001'
        run = 1; level = 14; bits = 16; level = level*(-1)^(stream(bits)); return
end
% 17 bits
switch stream(1:16)
    case '0000000000010011'
        run = 1; level = 15; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000010010'
        run = 1; level = 16; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000010001'
        run = 1; level = 17; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000010000'
        run = 1; level = 18; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000010100'
        run = 6; level = 3; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000011010'
        run = 11; level = 2; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000011001'
        run = 12; level = 2; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000011000'
        run = 13; level = 2; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000010111'
        run = 14; level = 2; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000010110'
        run = 15; level = 2; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000010101'
        run = 16; level = 2; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000011111'
        run = 27; level = 1; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000011110'
        run = 28; level = 1; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000011101'
        run = 29; level = 1; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000011100'
        run = 30; level = 1; bits = 17; level = level*(-1)^(stream(bits)); return
    case '0000000000011011'
        run = 31; level = 1; bits = 17;
end

if 	level<0
    msg = strcat( 'symbol .',stream,' not found');
    error(msg);
        run = bin2dec(stream(7:13));
    level = bin2dec(stream(14:21));
    bits = 14;
else
    level = level*(-1)^(stream(bits));
end
end

