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/P264/p264.h>
10 #ifndef HAS_P264_ENCODE
11 //TODO: rename this file and this function (something like p264_entropic.c)
13 #define PACK_BITS( bits_out, length_out, bits_in, length_in ) \
14 bits_out <<= length_in; \
15 length_out += length_in; \
18 void p264_encode_int(video_stream_t* const stream, int32_t code)
20 int32_t sign, length, data, value_code, value_length;
37 // TODO Check saturation & if level == -128
38 length = 32 - clz(data); // number of bits used in level ( = length of level )
41 data -= 1 << (length - 1);
45 PACK_BITS( value_code, value_length, 1, length );
50 PACK_BITS( value_code, value_length, data, length );
53 PACK_BITS( value_code, value_length, sign, 1 );
56 video_write_data( stream, value_code, value_length );
60 video_write_data( stream, 0x01, 2 );
64 void p264_decode_int(video_stream_t* const stream, int32_t *code)
66 uint32_t stream_code, stream_length;
67 int32_t r = 0, z, sign;
69 stream_code = stream_length = 0;
71 // Peek 32 bits from stream because we know our datas fit in
72 video_peek_data( stream, &stream_code, 32 );
74 /// Decode level / last
77 stream_code <<= z + 1; // Skip all zeros & 1
78 stream_length += z + 1;
94 stream_code >>= (32 - z);
95 sign = stream_code & 1;
103 *code = sign ? -r : r;
106 // Do the real Read in stream to consume what we used
107 video_read_data( stream, &stream_code, stream_length );
111 void p264_encode( video_stream_t* const stream, int32_t level, int32_t run, int32_t not_last )
113 int32_t sign, length, data, value_code, value_length;
115 /// Encode number of zeros
123 length = 32 - clz(data); // compute number of bits used in run ( = length of run )
124 data -= 1 << ( length - 1 ); // compute value of run
127 value_length = length + 1;
132 PACK_BITS( value_code, value_length, data, length );
146 // TODO Check saturation & if level == -128
147 length = 32 - clz(data); // number of bits used in level ( = length of level )
150 data -= 1 << (length - 1);
154 PACK_BITS( value_code, value_length, 1, length );
156 VP_OS_ASSERT( length != 2 );
161 PACK_BITS( value_code, value_length, data, length );
164 PACK_BITS( value_code, value_length, sign, 1 );
167 // add sequence for end of block if required
170 PACK_BITS( value_code, value_length, 0x5, 3 );
174 video_write_data( stream, value_code, value_length );
177 #endif // HAS_UVLC_ENCODE
179 C_RESULT p264_decode( video_stream_t* const stream, int32_t* run, uint32_t* level, int32_t* last)
181 uint32_t stream_code, stream_length;
182 int32_t r = 0, z, sign;
184 stream_code = stream_length = 0;
186 // Peek 32 bits from stream because we know our datas fit in
187 video_peek_data( stream, &stream_code, 32 );
190 /// Decode number of zeros
191 z = clz(stream_code);
193 stream_code <<= z + 1; // Skip all zeros & 1
194 stream_length += z + 1;
198 r = stream_code >> (32 - (z-1));
200 stream_code <<= (z-1);
201 stream_length += (z-1);
203 *run = r + (1 << (z-1));
211 /// Decode level / last
212 z = clz(stream_code);
214 stream_code <<= z + 1; // Skip all zeros & 1
215 stream_length += z + 1;
232 stream_code >>= (32 - z);
233 sign = stream_code & 1;
237 r = stream_code >> 1;
241 *level = sign ? -r : r;
245 // Do the real Read in stream to consume what we used
246 video_read_data( stream, &stream_code, stream_length );