1 #include <VLIB/video_codec.h>
2 #include <VLIB/video_packetizer.h>
3 #include <VLIB/Platform/video_utils.h>
4 #include <VLIB/Platform/video_config.h>
6 #include <VP_Os/vp_os_malloc.h>
8 C_RESULT video_packetizer_init( video_controller_t* controller )
10 // Internal buffer configuration
11 controller->in_stream.bytes = vp_os_malloc( DEFAULT_INTERNAL_STREAM_SIZE );
12 controller->in_stream.used = 0;
13 controller->in_stream.size = DEFAULT_INTERNAL_STREAM_SIZE;
14 controller->in_stream.index = 0;
15 controller->in_stream.length = 32;
16 controller->in_stream.code = 0;
17 controller->in_stream.endianess = VIDEO_STREAM_LITTLE_ENDIAN;
22 C_RESULT video_packetizer_close( video_controller_t* controller )
24 vp_os_free( controller->in_stream.bytes );
26 controller->in_stream.bytes = NULL;
27 controller->in_stream.used = 0;
28 controller->in_stream.size = 0;
29 controller->in_stream.index = 0;
30 controller->in_stream.length = 0;
31 controller->in_stream.code = 0;
36 C_RESULT video_cache_stream( video_controller_t* controller, video_stream_t* in )
38 video_codec_t* video_codec = controller->video_codec;
40 return video_codec->cache_stream( controller, in );
43 #ifndef HAS_VIDEO_WRITE_DATA
45 // Fill stream->code from right to left with data in parameters (code & length)
46 // New bits are always always inserted at the rigth of stream->code (least significant bits)
47 // This way old bits are put in most significant bits
48 // 31 .... 0 (length-1) .... 0
49 // stream <= ------------ <= -----------------------
51 void video_write_data( video_stream_t* const stream, uint32_t code, int32_t length )
53 while( length > stream->length )
55 // code's length is bigger than number of our free bits
56 // we put as many bits in cache as possible
57 stream->code <<= stream->length;
58 stream->code |= code >> (length - stream->length);
60 length -= stream->length; // Compute number of bits left
61 code &= (1 << length) - 1; // We keep only bits we didn't push in cache
63 stream->bytes[stream->index] = stream->code;
73 // In this case, previous loop ended with case length < stream->length
74 stream->code <<= length;
77 stream->length -= length;
83 C_RESULT video_stuff8( video_stream_t* const stream )
87 length8 = (stream->length & ~7); // TODO: Check if generated code use bic on arm
89 stream->code <<= ( stream->length - length8 );
90 stream->length = length8;
95 // Fill code from right to left with length bits from stream->code
96 // Next bits in stream->code to take are always at the left (most significant bits)
97 // This way new bits are put in least significant bits
98 // (length-1) .... 0 31 .... 0
99 // ----------------------- <= ------------ <= stream
101 C_RESULT video_read_data( video_stream_t* const stream, uint32_t* code, int32_t length )
103 uint32_t out_code = *code;
105 while( length > (32 - stream->length) )
107 /// We need more bits than available in current read bits
109 out_code = (out_code << (32 - stream->length)) | (stream->code >> stream->length);
110 length -= (32 - stream->length);
112 stream->code = stream->bytes[stream->index];
119 out_code = (out_code << length) | (stream->code >> ( 32 - length ));
121 stream->code <<= length;
122 stream->length += length;
130 C_RESULT video_peek_data( const video_stream_t* const stream, uint32_t* code, int32_t length )
132 uint32_t out_code = *code;
133 uint32_t stream_code = stream->code;
134 uint32_t stream_length = stream->length;
136 while( length > (32 - (int32_t)stream_length) )
138 /// We need more bits than available in current read bits
140 out_code = (out_code << (32 - stream_length)) | (stream_code >> stream_length);
141 length -= (32 - stream_length);
143 stream_code = stream->bytes[stream->index];
149 out_code = (out_code << length) | (stream_code >> ( 32 - length ));
157 C_RESULT video_align8( video_stream_t* const stream )
159 uint32_t length8, length = stream->length;
163 // Do alignment only when stream->length > 0
164 length8 = ( length & ~7); // TODO: Check if generated code use bic on arm
165 if( length8 != length )
168 stream->code <<= ( length8 - length );
169 stream->length = length8;