1 #include <VLIB/Platform/video_utils.h>
2 #include <VLIB/video_packetizer.h>
4 #include <VP_Os/vp_os_assert.h>
6 #include <VP_Os/vp_os_types.h>
8 #include <VLIB/UVLC/uvlc.h>
10 #ifndef HAS_UVLC_ENCODE
12 #define PACK_BITS( bits_out, length_out, bits_in, length_in ) \
13 bits_out <<= length_in; \
14 length_out += length_in; \
17 void uvlc_encode( video_stream_t* const stream, int32_t level, int32_t run, int32_t not_last )
19 int32_t sign, length, data, value_code, value_length;
21 /// Encode number of zeros
29 length = 32 - clz(data); // compute number of bits used in run ( = length of run )
30 data -= 1 << ( length - 1 ); // compute value of run
33 value_length = length + 1;
38 PACK_BITS( value_code, value_length, data, length );
52 // TODO Check saturation & if level == -128
53 length = 32 - clz(data); // number of bits used in level ( = length of level )
56 data -= 1 << (length - 1);
60 PACK_BITS( value_code, value_length, 1, length );
62 VP_OS_ASSERT( length != 2 );
67 PACK_BITS( value_code, value_length, data, length );
70 PACK_BITS( value_code, value_length, sign, 1 );
73 // add sequence for end of block if required
76 PACK_BITS( value_code, value_length, 0x5, 3 );
80 video_write_data( stream, value_code, value_length );
83 #endif // HAS_UVLC_ENCODE
85 C_RESULT uvlc_decode( video_stream_t* const stream, int32_t* run, int32_t* level, int32_t* last)
87 uint32_t stream_code, stream_length;
88 int32_t r = 0, z, sign;
90 stream_code = stream_length = 0;
92 // Peek 32 bits from stream because we know our datas fit in
93 video_peek_data( stream, &stream_code, 32 );
96 /// Decode number of zeros
99 stream_code <<= z + 1; // Skip all zeros & 1
100 stream_length += z + 1;
104 r = stream_code >> (32 - (z-1));
106 stream_code <<= (z-1);
107 stream_length += (z-1);
109 *run = r + (1 << (z-1));
117 /// Decode level / last
118 z = clz(stream_code);
120 stream_code <<= z + 1; // Skip all zeros & 1
121 stream_length += z + 1;
138 stream_code >>= (32 - z);
139 sign = stream_code & 1;
143 r = stream_code >> 1;
147 *level = sign ? -r : r;
151 // Do the real Read in stream to consume what we used
152 video_read_data( stream, &stream_code, stream_length );