00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #ifndef AVCODEC_PUT_BITS_H
00027 #define AVCODEC_PUT_BITS_H
00028
00029 #include <stdint.h>
00030 #include <stdlib.h>
00031 #include <assert.h>
00032 #include "libavutil/bswap.h"
00033 #include "libavutil/common.h"
00034 #include "libavutil/intreadwrite.h"
00035 #include "libavutil/log.h"
00036 #include "mathops.h"
00037 #include "config.h"
00038
00039 typedef struct PutBitContext {
00040 uint32_t bit_buf;
00041 int bit_left;
00042 uint8_t *buf, *buf_ptr, *buf_end;
00043 int size_in_bits;
00044 } PutBitContext;
00045
00052 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
00053 {
00054 if(buffer_size < 0) {
00055 buffer_size = 0;
00056 buffer = NULL;
00057 }
00058
00059 s->size_in_bits= 8*buffer_size;
00060 s->buf = buffer;
00061 s->buf_end = s->buf + buffer_size;
00062 s->buf_ptr = s->buf;
00063 s->bit_left=32;
00064 s->bit_buf=0;
00065 }
00066
00070 static inline int put_bits_count(PutBitContext *s)
00071 {
00072 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
00073 }
00074
00078 static inline void flush_put_bits(PutBitContext *s)
00079 {
00080 #ifndef BITSTREAM_WRITER_LE
00081 if (s->bit_left < 32)
00082 s->bit_buf<<= s->bit_left;
00083 #endif
00084 while (s->bit_left < 32) {
00085
00086 #ifdef BITSTREAM_WRITER_LE
00087 *s->buf_ptr++=s->bit_buf;
00088 s->bit_buf>>=8;
00089 #else
00090 *s->buf_ptr++=s->bit_buf >> 24;
00091 s->bit_buf<<=8;
00092 #endif
00093 s->bit_left+=8;
00094 }
00095 s->bit_left=32;
00096 s->bit_buf=0;
00097 }
00098
00099 #ifdef BITSTREAM_WRITER_LE
00100 #define avpriv_align_put_bits align_put_bits_unsupported_here
00101 #define ff_put_string ff_put_string_unsupported_here
00102 #define avpriv_copy_bits avpriv_copy_bits_unsupported_here
00103 #else
00104
00107 void avpriv_align_put_bits(PutBitContext *s);
00108
00114 void ff_put_string(PutBitContext *pb, const char *string, int terminate_string);
00115
00121 void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
00122 #endif
00123
00128 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
00129 {
00130 unsigned int bit_buf;
00131 int bit_left;
00132
00133
00134 assert(n <= 31 && value < (1U << n));
00135
00136 bit_buf = s->bit_buf;
00137 bit_left = s->bit_left;
00138
00139
00140
00141 #ifdef BITSTREAM_WRITER_LE
00142 bit_buf |= value << (32 - bit_left);
00143 if (n >= bit_left) {
00144 AV_WL32(s->buf_ptr, bit_buf);
00145 s->buf_ptr+=4;
00146 bit_buf = (bit_left==32)?0:value >> bit_left;
00147 bit_left+=32;
00148 }
00149 bit_left-=n;
00150 #else
00151 if (n < bit_left) {
00152 bit_buf = (bit_buf<<n) | value;
00153 bit_left-=n;
00154 } else {
00155 bit_buf<<=bit_left;
00156 bit_buf |= value >> (n - bit_left);
00157 AV_WB32(s->buf_ptr, bit_buf);
00158
00159 s->buf_ptr+=4;
00160 bit_left+=32 - n;
00161 bit_buf = value;
00162 }
00163 #endif
00164
00165 s->bit_buf = bit_buf;
00166 s->bit_left = bit_left;
00167 }
00168
00169 static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
00170 {
00171 assert(n >= 0 && n <= 31);
00172
00173 put_bits(pb, n, value & ((1<<n)-1));
00174 }
00175
00179 static void av_unused put_bits32(PutBitContext *s, uint32_t value)
00180 {
00181 int lo = value & 0xffff;
00182 int hi = value >> 16;
00183 #ifdef BITSTREAM_WRITER_LE
00184 put_bits(s, 16, lo);
00185 put_bits(s, 16, hi);
00186 #else
00187 put_bits(s, 16, hi);
00188 put_bits(s, 16, lo);
00189 #endif
00190 }
00191
00196 static inline uint8_t* put_bits_ptr(PutBitContext *s)
00197 {
00198 return s->buf_ptr;
00199 }
00200
00205 static inline void skip_put_bytes(PutBitContext *s, int n)
00206 {
00207 assert((put_bits_count(s)&7)==0);
00208 assert(s->bit_left==32);
00209 s->buf_ptr += n;
00210 }
00211
00217 static inline void skip_put_bits(PutBitContext *s, int n)
00218 {
00219 s->bit_left -= n;
00220 s->buf_ptr-= 4*(s->bit_left>>5);
00221 s->bit_left &= 31;
00222 }
00223
00229 static inline void set_put_bits_buffer_size(PutBitContext *s, int size)
00230 {
00231 s->buf_end= s->buf + size;
00232 }
00233
00234 #endif