comparison put_bits.h @ 10344:2b8a327189cd libavcodec

put_bits can only reliably write up to 31 bit bits, above it relies on undefined shift behaviour. Document this, fix the assert and add a put_bits32 to handle writing 32 bits and use that where necessary.
author reimar
date Thu, 01 Oct 2009 15:40:29 +0000
parents 2887f410011f
children 784409693f16
comparison
equal deleted inserted replaced
10343:b1218e0b0f2b 10344:2b8a327189cd
134 * 134 *
135 * @param length the number of bits of src to copy 135 * @param length the number of bits of src to copy
136 */ 136 */
137 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length); 137 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
138 138
139 /**
140 * Write up to 31 bits into a bitstream.
141 * Use put_bits32 to write 32 bits.
142 */
139 static inline void put_bits(PutBitContext *s, int n, unsigned int value) 143 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
140 #ifndef ALT_BITSTREAM_WRITER 144 #ifndef ALT_BITSTREAM_WRITER
141 { 145 {
142 unsigned int bit_buf; 146 unsigned int bit_buf;
143 int bit_left; 147 int bit_left;
144 148
145 // printf("put_bits=%d %x\n", n, value); 149 // printf("put_bits=%d %x\n", n, value);
146 assert(n == 32 || value < (1U << n)); 150 assert(n <= 31 && value < (1U << n));
147 151
148 bit_buf = s->bit_buf; 152 bit_buf = s->bit_buf;
149 bit_left = s->bit_left; 153 bit_left = s->bit_left;
150 154
151 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); 155 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
258 262
259 put_bits(pb, bits, val & ((1<<bits)-1)); 263 put_bits(pb, bits, val & ((1<<bits)-1));
260 } 264 }
261 265
262 /** 266 /**
267 * Write exactly 32 bits into a bitstream
268 */
269 static void av_unused put_bits32(PutBitContext *s, uint32_t value)
270 {
271 int lo = value & 0xffff;
272 int hi = value >> 16;
273 #ifdef ALT_BITSTREAM_WRITER_LE
274 put_bits(s, 16, lo);
275 put_bits(s, 16, hi);
276 #else
277 put_bits(s, 16, hi);
278 put_bits(s, 16, lo);
279 #endif
280 }
281
282 /**
263 * Returns the pointer to the byte where the bitstream writer will put 283 * Returns the pointer to the byte where the bitstream writer will put
264 * the next bit. 284 * the next bit.
265 */ 285 */
266 static inline uint8_t* put_bits_ptr(PutBitContext *s) 286 static inline uint8_t* put_bits_ptr(PutBitContext *s)
267 { 287 {